Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
2571583a | 2 | Copyright (C) 1995-2017 Free Software Foundation, Inc. |
252b5132 | 3 | |
8fdd7217 | 4 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 5 | |
8fdd7217 NC |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 8 | the Free Software Foundation; either version 3 of the License, or |
8fdd7217 | 9 | (at your option) any later version. |
252b5132 | 10 | |
8fdd7217 NC |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
252b5132 | 15 | |
8fdd7217 NC |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
19 | MA 02110-1301, USA. */ | |
252b5132 | 20 | |
252b5132 | 21 | #include "sysdep.h" |
3db64b00 | 22 | #include "bfd.h" |
53df40a4 | 23 | #include "bfd_stdint.h" |
252b5132 RH |
24 | #include "bfdlink.h" |
25 | #include "libbfd.h" | |
26 | #define ARCH_SIZE 0 | |
27 | #include "elf-bfd.h" | |
4ad4eba5 | 28 | #include "safe-ctype.h" |
ccf2f652 | 29 | #include "libiberty.h" |
66eb6687 | 30 | #include "objalloc.h" |
08ce1d72 | 31 | #if BFD_SUPPORTS_PLUGINS |
7d0b9ebc | 32 | #include "plugin-api.h" |
7dc3990e L |
33 | #include "plugin.h" |
34 | #endif | |
252b5132 | 35 | |
28caa186 AM |
36 | /* This struct is used to pass information to routines called via |
37 | elf_link_hash_traverse which must return failure. */ | |
38 | ||
39 | struct elf_info_failed | |
40 | { | |
41 | struct bfd_link_info *info; | |
28caa186 AM |
42 | bfd_boolean failed; |
43 | }; | |
44 | ||
45 | /* This structure is used to pass information to | |
46 | _bfd_elf_link_find_version_dependencies. */ | |
47 | ||
48 | struct elf_find_verdep_info | |
49 | { | |
50 | /* General link information. */ | |
51 | struct bfd_link_info *info; | |
52 | /* The number of dependencies. */ | |
53 | unsigned int vers; | |
54 | /* Whether we had a failure. */ | |
55 | bfd_boolean failed; | |
56 | }; | |
57 | ||
58 | static bfd_boolean _bfd_elf_fix_symbol_flags | |
59 | (struct elf_link_hash_entry *, struct elf_info_failed *); | |
60 | ||
2f0c68f2 CM |
61 | asection * |
62 | _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie, | |
63 | unsigned long r_symndx, | |
64 | bfd_boolean discard) | |
65 | { | |
66 | if (r_symndx >= cookie->locsymcount | |
67 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
68 | { | |
69 | struct elf_link_hash_entry *h; | |
70 | ||
71 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
72 | ||
73 | while (h->root.type == bfd_link_hash_indirect | |
74 | || h->root.type == bfd_link_hash_warning) | |
75 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
76 | ||
77 | if ((h->root.type == bfd_link_hash_defined | |
78 | || h->root.type == bfd_link_hash_defweak) | |
79 | && discarded_section (h->root.u.def.section)) | |
80 | return h->root.u.def.section; | |
81 | else | |
82 | return NULL; | |
83 | } | |
84 | else | |
85 | { | |
86 | /* It's not a relocation against a global symbol, | |
87 | but it could be a relocation against a local | |
88 | symbol for a discarded section. */ | |
89 | asection *isec; | |
90 | Elf_Internal_Sym *isym; | |
91 | ||
92 | /* Need to: get the symbol; get the section. */ | |
93 | isym = &cookie->locsyms[r_symndx]; | |
94 | isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx); | |
95 | if (isec != NULL | |
96 | && discard ? discarded_section (isec) : 1) | |
97 | return isec; | |
98 | } | |
99 | return NULL; | |
100 | } | |
101 | ||
d98685ac AM |
102 | /* Define a symbol in a dynamic linkage section. */ |
103 | ||
104 | struct elf_link_hash_entry * | |
105 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
106 | struct bfd_link_info *info, | |
107 | asection *sec, | |
108 | const char *name) | |
109 | { | |
110 | struct elf_link_hash_entry *h; | |
111 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 112 | const struct elf_backend_data *bed; |
d98685ac AM |
113 | |
114 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
115 | if (h != NULL) | |
116 | { | |
117 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
118 | This is a symptom of a larger problem: Absolute symbols | |
119 | defined in shared libraries can't be overridden, because we | |
120 | lose the link to the bfd which is via the symbol section. */ | |
121 | h->root.type = bfd_link_hash_new; | |
ad32986f | 122 | bh = &h->root; |
d98685ac | 123 | } |
ad32986f NC |
124 | else |
125 | bh = NULL; | |
d98685ac | 126 | |
cf18fda4 | 127 | bed = get_elf_backend_data (abfd); |
d98685ac | 128 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, |
cf18fda4 | 129 | sec, 0, NULL, FALSE, bed->collect, |
d98685ac AM |
130 | &bh)) |
131 | return NULL; | |
132 | h = (struct elf_link_hash_entry *) bh; | |
ad32986f | 133 | BFD_ASSERT (h != NULL); |
d98685ac | 134 | h->def_regular = 1; |
e28df02b | 135 | h->non_elf = 0; |
12b2843a | 136 | h->root.linker_def = 1; |
d98685ac | 137 | h->type = STT_OBJECT; |
00b7642b AM |
138 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
139 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
d98685ac | 140 | |
ccabcbe5 | 141 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
d98685ac AM |
142 | return h; |
143 | } | |
144 | ||
b34976b6 | 145 | bfd_boolean |
268b6b39 | 146 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
147 | { |
148 | flagword flags; | |
aad5d350 | 149 | asection *s; |
252b5132 | 150 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 151 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 152 | struct elf_link_hash_table *htab = elf_hash_table (info); |
252b5132 RH |
153 | |
154 | /* This function may be called more than once. */ | |
ce558b89 | 155 | if (htab->sgot != NULL) |
b34976b6 | 156 | return TRUE; |
252b5132 | 157 | |
e5a52504 | 158 | flags = bed->dynamic_sec_flags; |
252b5132 | 159 | |
14b2f831 AM |
160 | s = bfd_make_section_anyway_with_flags (abfd, |
161 | (bed->rela_plts_and_copies_p | |
162 | ? ".rela.got" : ".rel.got"), | |
163 | (bed->dynamic_sec_flags | |
164 | | SEC_READONLY)); | |
6de2ae4a L |
165 | if (s == NULL |
166 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
167 | return FALSE; | |
168 | htab->srelgot = s; | |
252b5132 | 169 | |
14b2f831 | 170 | s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); |
64e77c6d L |
171 | if (s == NULL |
172 | || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
173 | return FALSE; | |
174 | htab->sgot = s; | |
175 | ||
252b5132 RH |
176 | if (bed->want_got_plt) |
177 | { | |
14b2f831 | 178 | s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); |
252b5132 | 179 | if (s == NULL |
6de2ae4a L |
180 | || !bfd_set_section_alignment (abfd, s, |
181 | bed->s->log_file_align)) | |
b34976b6 | 182 | return FALSE; |
6de2ae4a | 183 | htab->sgotplt = s; |
252b5132 RH |
184 | } |
185 | ||
64e77c6d L |
186 | /* The first bit of the global offset table is the header. */ |
187 | s->size += bed->got_header_size; | |
188 | ||
2517a57f AM |
189 | if (bed->want_got_sym) |
190 | { | |
191 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
192 | (or .got.plt) section. We don't do this in the linker script | |
193 | because we don't want to define the symbol if we are not creating | |
194 | a global offset table. */ | |
6de2ae4a L |
195 | h = _bfd_elf_define_linkage_sym (abfd, info, s, |
196 | "_GLOBAL_OFFSET_TABLE_"); | |
2517a57f | 197 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
198 | if (h == NULL) |
199 | return FALSE; | |
2517a57f | 200 | } |
252b5132 | 201 | |
b34976b6 | 202 | return TRUE; |
252b5132 RH |
203 | } |
204 | \f | |
7e9f0867 AM |
205 | /* Create a strtab to hold the dynamic symbol names. */ |
206 | static bfd_boolean | |
207 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
208 | { | |
209 | struct elf_link_hash_table *hash_table; | |
210 | ||
211 | hash_table = elf_hash_table (info); | |
212 | if (hash_table->dynobj == NULL) | |
6cd255ca L |
213 | { |
214 | /* We may not set dynobj, an input file holding linker created | |
215 | dynamic sections to abfd, which may be a dynamic object with | |
216 | its own dynamic sections. We need to find a normal input file | |
217 | to hold linker created sections if possible. */ | |
218 | if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0) | |
219 | { | |
220 | bfd *ibfd; | |
57963c05 | 221 | asection *s; |
6cd255ca | 222 | for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next) |
6645479e | 223 | if ((ibfd->flags |
57963c05 AM |
224 | & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0 |
225 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
226 | && !((s = ibfd->sections) != NULL | |
227 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)) | |
6cd255ca L |
228 | { |
229 | abfd = ibfd; | |
230 | break; | |
231 | } | |
232 | } | |
233 | hash_table->dynobj = abfd; | |
234 | } | |
7e9f0867 AM |
235 | |
236 | if (hash_table->dynstr == NULL) | |
237 | { | |
238 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
239 | if (hash_table->dynstr == NULL) | |
240 | return FALSE; | |
241 | } | |
242 | return TRUE; | |
243 | } | |
244 | ||
45d6a902 AM |
245 | /* Create some sections which will be filled in with dynamic linking |
246 | information. ABFD is an input file which requires dynamic sections | |
247 | to be created. The dynamic sections take up virtual memory space | |
248 | when the final executable is run, so we need to create them before | |
249 | addresses are assigned to the output sections. We work out the | |
250 | actual contents and size of these sections later. */ | |
252b5132 | 251 | |
b34976b6 | 252 | bfd_boolean |
268b6b39 | 253 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 254 | { |
45d6a902 | 255 | flagword flags; |
91d6fa6a | 256 | asection *s; |
9c5bfbb7 | 257 | const struct elf_backend_data *bed; |
9637f6ef | 258 | struct elf_link_hash_entry *h; |
252b5132 | 259 | |
0eddce27 | 260 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
261 | return FALSE; |
262 | ||
263 | if (elf_hash_table (info)->dynamic_sections_created) | |
264 | return TRUE; | |
265 | ||
7e9f0867 AM |
266 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
267 | return FALSE; | |
45d6a902 | 268 | |
7e9f0867 | 269 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
270 | bed = get_elf_backend_data (abfd); |
271 | ||
272 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
273 | |
274 | /* A dynamically linked executable has a .interp section, but a | |
275 | shared library does not. */ | |
9b8b325a | 276 | if (bfd_link_executable (info) && !info->nointerp) |
252b5132 | 277 | { |
14b2f831 AM |
278 | s = bfd_make_section_anyway_with_flags (abfd, ".interp", |
279 | flags | SEC_READONLY); | |
3496cb2a | 280 | if (s == NULL) |
45d6a902 AM |
281 | return FALSE; |
282 | } | |
bb0deeff | 283 | |
45d6a902 AM |
284 | /* Create sections to hold version informations. These are removed |
285 | if they are not needed. */ | |
14b2f831 AM |
286 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d", |
287 | flags | SEC_READONLY); | |
45d6a902 | 288 | if (s == NULL |
45d6a902 AM |
289 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
290 | return FALSE; | |
291 | ||
14b2f831 AM |
292 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version", |
293 | flags | SEC_READONLY); | |
45d6a902 | 294 | if (s == NULL |
45d6a902 AM |
295 | || ! bfd_set_section_alignment (abfd, s, 1)) |
296 | return FALSE; | |
297 | ||
14b2f831 AM |
298 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r", |
299 | flags | SEC_READONLY); | |
45d6a902 | 300 | if (s == NULL |
45d6a902 AM |
301 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
302 | return FALSE; | |
303 | ||
14b2f831 AM |
304 | s = bfd_make_section_anyway_with_flags (abfd, ".dynsym", |
305 | flags | SEC_READONLY); | |
45d6a902 | 306 | if (s == NULL |
45d6a902 AM |
307 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
308 | return FALSE; | |
cae1fbbb | 309 | elf_hash_table (info)->dynsym = s; |
45d6a902 | 310 | |
14b2f831 AM |
311 | s = bfd_make_section_anyway_with_flags (abfd, ".dynstr", |
312 | flags | SEC_READONLY); | |
3496cb2a | 313 | if (s == NULL) |
45d6a902 AM |
314 | return FALSE; |
315 | ||
14b2f831 | 316 | s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 317 | if (s == NULL |
45d6a902 AM |
318 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
319 | return FALSE; | |
320 | ||
321 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
322 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
323 | only want to define it if we are, in fact, creating a .dynamic | |
324 | section. We don't want to define it if there is no .dynamic | |
325 | section, since on some ELF platforms the start up code examines it | |
326 | to decide how to initialize the process. */ | |
9637f6ef L |
327 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"); |
328 | elf_hash_table (info)->hdynamic = h; | |
329 | if (h == NULL) | |
45d6a902 AM |
330 | return FALSE; |
331 | ||
fdc90cb4 JJ |
332 | if (info->emit_hash) |
333 | { | |
14b2f831 AM |
334 | s = bfd_make_section_anyway_with_flags (abfd, ".hash", |
335 | flags | SEC_READONLY); | |
fdc90cb4 JJ |
336 | if (s == NULL |
337 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
338 | return FALSE; | |
339 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
340 | } | |
341 | ||
342 | if (info->emit_gnu_hash) | |
343 | { | |
14b2f831 AM |
344 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash", |
345 | flags | SEC_READONLY); | |
fdc90cb4 JJ |
346 | if (s == NULL |
347 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
348 | return FALSE; | |
349 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: | |
350 | 4 32-bit words followed by variable count of 64-bit words, then | |
351 | variable count of 32-bit words. */ | |
352 | if (bed->s->arch_size == 64) | |
353 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
354 | else | |
355 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
356 | } | |
45d6a902 AM |
357 | |
358 | /* Let the backend create the rest of the sections. This lets the | |
359 | backend set the right flags. The backend will normally create | |
360 | the .got and .plt sections. */ | |
894891db NC |
361 | if (bed->elf_backend_create_dynamic_sections == NULL |
362 | || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
45d6a902 AM |
363 | return FALSE; |
364 | ||
365 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
366 | ||
367 | return TRUE; | |
368 | } | |
369 | ||
370 | /* Create dynamic sections when linking against a dynamic object. */ | |
371 | ||
372 | bfd_boolean | |
268b6b39 | 373 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
374 | { |
375 | flagword flags, pltflags; | |
7325306f | 376 | struct elf_link_hash_entry *h; |
45d6a902 | 377 | asection *s; |
9c5bfbb7 | 378 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 379 | struct elf_link_hash_table *htab = elf_hash_table (info); |
45d6a902 | 380 | |
252b5132 RH |
381 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
382 | .rel[a].bss sections. */ | |
e5a52504 | 383 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
384 | |
385 | pltflags = flags; | |
252b5132 | 386 | if (bed->plt_not_loaded) |
6df4d94c MM |
387 | /* We do not clear SEC_ALLOC here because we still want the OS to |
388 | allocate space for the section; it's just that there's nothing | |
389 | to read in from the object file. */ | |
5d1634d7 | 390 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
391 | else |
392 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
393 | if (bed->plt_readonly) |
394 | pltflags |= SEC_READONLY; | |
395 | ||
14b2f831 | 396 | s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags); |
252b5132 | 397 | if (s == NULL |
252b5132 | 398 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 399 | return FALSE; |
6de2ae4a | 400 | htab->splt = s; |
252b5132 | 401 | |
d98685ac AM |
402 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
403 | .plt section. */ | |
7325306f RS |
404 | if (bed->want_plt_sym) |
405 | { | |
406 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
407 | "_PROCEDURE_LINKAGE_TABLE_"); | |
408 | elf_hash_table (info)->hplt = h; | |
409 | if (h == NULL) | |
410 | return FALSE; | |
411 | } | |
252b5132 | 412 | |
14b2f831 AM |
413 | s = bfd_make_section_anyway_with_flags (abfd, |
414 | (bed->rela_plts_and_copies_p | |
415 | ? ".rela.plt" : ".rel.plt"), | |
416 | flags | SEC_READONLY); | |
252b5132 | 417 | if (s == NULL |
45d6a902 | 418 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 419 | return FALSE; |
6de2ae4a | 420 | htab->srelplt = s; |
252b5132 RH |
421 | |
422 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 423 | return FALSE; |
252b5132 | 424 | |
3018b441 RH |
425 | if (bed->want_dynbss) |
426 | { | |
427 | /* The .dynbss section is a place to put symbols which are defined | |
428 | by dynamic objects, are referenced by regular objects, and are | |
429 | not functions. We must allocate space for them in the process | |
430 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
431 | initialize them at run time. The linker script puts the .dynbss | |
432 | section into the .bss section of the final image. */ | |
14b2f831 | 433 | s = bfd_make_section_anyway_with_flags (abfd, ".dynbss", |
afbf7e8e | 434 | SEC_ALLOC | SEC_LINKER_CREATED); |
3496cb2a | 435 | if (s == NULL) |
b34976b6 | 436 | return FALSE; |
9d19e4fd | 437 | htab->sdynbss = s; |
252b5132 | 438 | |
5474d94f AM |
439 | if (bed->want_dynrelro) |
440 | { | |
441 | /* Similarly, but for symbols that were originally in read-only | |
afbf7e8e AM |
442 | sections. This section doesn't really need to have contents, |
443 | but make it like other .data.rel.ro sections. */ | |
5474d94f | 444 | s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro", |
afbf7e8e | 445 | flags); |
5474d94f AM |
446 | if (s == NULL) |
447 | return FALSE; | |
448 | htab->sdynrelro = s; | |
449 | } | |
450 | ||
3018b441 | 451 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
452 | normally needed. We need to create it here, though, so that the |
453 | linker will map it to an output section. We can't just create it | |
454 | only if we need it, because we will not know whether we need it | |
455 | until we have seen all the input files, and the first time the | |
456 | main linker code calls BFD after examining all the input files | |
457 | (size_dynamic_sections) the input sections have already been | |
458 | mapped to the output sections. If the section turns out not to | |
459 | be needed, we can discard it later. We will never need this | |
460 | section when generating a shared object, since they do not use | |
461 | copy relocs. */ | |
9d19e4fd | 462 | if (bfd_link_executable (info)) |
3018b441 | 463 | { |
14b2f831 AM |
464 | s = bfd_make_section_anyway_with_flags (abfd, |
465 | (bed->rela_plts_and_copies_p | |
466 | ? ".rela.bss" : ".rel.bss"), | |
467 | flags | SEC_READONLY); | |
3018b441 | 468 | if (s == NULL |
45d6a902 | 469 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 470 | return FALSE; |
9d19e4fd | 471 | htab->srelbss = s; |
5474d94f AM |
472 | |
473 | if (bed->want_dynrelro) | |
474 | { | |
475 | s = (bfd_make_section_anyway_with_flags | |
476 | (abfd, (bed->rela_plts_and_copies_p | |
477 | ? ".rela.data.rel.ro" : ".rel.data.rel.ro"), | |
478 | flags | SEC_READONLY)); | |
479 | if (s == NULL | |
480 | || ! bfd_set_section_alignment (abfd, s, | |
481 | bed->s->log_file_align)) | |
482 | return FALSE; | |
483 | htab->sreldynrelro = s; | |
484 | } | |
3018b441 | 485 | } |
252b5132 RH |
486 | } |
487 | ||
b34976b6 | 488 | return TRUE; |
252b5132 RH |
489 | } |
490 | \f | |
252b5132 RH |
491 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
492 | read the input files, since we need to have a list of all of them | |
493 | before we can determine the final sizes of the output sections. | |
494 | Note that we may actually call this function even though we are not | |
495 | going to output any dynamic symbols; in some cases we know that a | |
496 | symbol should be in the dynamic symbol table, but only if there is | |
497 | one. */ | |
498 | ||
b34976b6 | 499 | bfd_boolean |
c152c796 AM |
500 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
501 | struct elf_link_hash_entry *h) | |
252b5132 RH |
502 | { |
503 | if (h->dynindx == -1) | |
504 | { | |
2b0f7ef9 | 505 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 506 | char *p; |
252b5132 | 507 | const char *name; |
ef53be89 | 508 | size_t indx; |
252b5132 | 509 | |
7a13edea NC |
510 | /* XXX: The ABI draft says the linker must turn hidden and |
511 | internal symbols into STB_LOCAL symbols when producing the | |
512 | DSO. However, if ld.so honors st_other in the dynamic table, | |
513 | this would not be necessary. */ | |
514 | switch (ELF_ST_VISIBILITY (h->other)) | |
515 | { | |
516 | case STV_INTERNAL: | |
517 | case STV_HIDDEN: | |
9d6eee78 L |
518 | if (h->root.type != bfd_link_hash_undefined |
519 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 520 | { |
f5385ebf | 521 | h->forced_local = 1; |
67687978 PB |
522 | if (!elf_hash_table (info)->is_relocatable_executable) |
523 | return TRUE; | |
7a13edea | 524 | } |
0444bdd4 | 525 | |
7a13edea NC |
526 | default: |
527 | break; | |
528 | } | |
529 | ||
252b5132 RH |
530 | h->dynindx = elf_hash_table (info)->dynsymcount; |
531 | ++elf_hash_table (info)->dynsymcount; | |
532 | ||
533 | dynstr = elf_hash_table (info)->dynstr; | |
534 | if (dynstr == NULL) | |
535 | { | |
536 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 537 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 538 | if (dynstr == NULL) |
b34976b6 | 539 | return FALSE; |
252b5132 RH |
540 | } |
541 | ||
542 | /* We don't put any version information in the dynamic string | |
aad5d350 | 543 | table. */ |
252b5132 RH |
544 | name = h->root.root.string; |
545 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
546 | if (p != NULL) |
547 | /* We know that the p points into writable memory. In fact, | |
548 | there are only a few symbols that have read-only names, being | |
549 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
550 | by the backends. Most symbols will have names pointing into | |
551 | an ELF string table read from a file, or to objalloc memory. */ | |
552 | *p = 0; | |
553 | ||
554 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
555 | ||
556 | if (p != NULL) | |
557 | *p = ELF_VER_CHR; | |
252b5132 | 558 | |
ef53be89 | 559 | if (indx == (size_t) -1) |
b34976b6 | 560 | return FALSE; |
252b5132 RH |
561 | h->dynstr_index = indx; |
562 | } | |
563 | ||
b34976b6 | 564 | return TRUE; |
252b5132 | 565 | } |
45d6a902 | 566 | \f |
55255dae L |
567 | /* Mark a symbol dynamic. */ |
568 | ||
28caa186 | 569 | static void |
55255dae | 570 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, |
40b36307 L |
571 | struct elf_link_hash_entry *h, |
572 | Elf_Internal_Sym *sym) | |
55255dae | 573 | { |
40b36307 | 574 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
55255dae | 575 | |
40b36307 | 576 | /* It may be called more than once on the same H. */ |
0e1862bb | 577 | if(h->dynamic || bfd_link_relocatable (info)) |
55255dae L |
578 | return; |
579 | ||
40b36307 L |
580 | if ((info->dynamic_data |
581 | && (h->type == STT_OBJECT | |
b8871f35 | 582 | || h->type == STT_COMMON |
40b36307 | 583 | || (sym != NULL |
b8871f35 L |
584 | && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT |
585 | || ELF_ST_TYPE (sym->st_info) == STT_COMMON)))) | |
a0c8462f | 586 | || (d != NULL |
73ec947d | 587 | && h->non_elf |
40b36307 | 588 | && (*d->match) (&d->head, NULL, h->root.root.string))) |
55255dae L |
589 | h->dynamic = 1; |
590 | } | |
591 | ||
45d6a902 AM |
592 | /* Record an assignment to a symbol made by a linker script. We need |
593 | this in case some dynamic object refers to this symbol. */ | |
594 | ||
595 | bfd_boolean | |
fe21a8fc L |
596 | bfd_elf_record_link_assignment (bfd *output_bfd, |
597 | struct bfd_link_info *info, | |
268b6b39 | 598 | const char *name, |
fe21a8fc L |
599 | bfd_boolean provide, |
600 | bfd_boolean hidden) | |
45d6a902 | 601 | { |
00cbee0a | 602 | struct elf_link_hash_entry *h, *hv; |
4ea42fb7 | 603 | struct elf_link_hash_table *htab; |
00cbee0a | 604 | const struct elf_backend_data *bed; |
45d6a902 | 605 | |
0eddce27 | 606 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
607 | return TRUE; |
608 | ||
4ea42fb7 AM |
609 | htab = elf_hash_table (info); |
610 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 611 | if (h == NULL) |
4ea42fb7 | 612 | return provide; |
45d6a902 | 613 | |
8e2a4f11 AM |
614 | if (h->root.type == bfd_link_hash_warning) |
615 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
616 | ||
0f550b3d L |
617 | if (h->versioned == unknown) |
618 | { | |
619 | /* Set versioned if symbol version is unknown. */ | |
620 | char *version = strrchr (name, ELF_VER_CHR); | |
621 | if (version) | |
622 | { | |
623 | if (version > name && version[-1] != ELF_VER_CHR) | |
624 | h->versioned = versioned_hidden; | |
625 | else | |
626 | h->versioned = versioned; | |
627 | } | |
628 | } | |
629 | ||
73ec947d AM |
630 | /* Symbols defined in a linker script but not referenced anywhere |
631 | else will have non_elf set. */ | |
632 | if (h->non_elf) | |
633 | { | |
634 | bfd_elf_link_mark_dynamic_symbol (info, h, NULL); | |
635 | h->non_elf = 0; | |
636 | } | |
637 | ||
00cbee0a | 638 | switch (h->root.type) |
77cfaee6 | 639 | { |
00cbee0a L |
640 | case bfd_link_hash_defined: |
641 | case bfd_link_hash_defweak: | |
642 | case bfd_link_hash_common: | |
643 | break; | |
644 | case bfd_link_hash_undefweak: | |
645 | case bfd_link_hash_undefined: | |
646 | /* Since we're defining the symbol, don't let it seem to have not | |
647 | been defined. record_dynamic_symbol and size_dynamic_sections | |
648 | may depend on this. */ | |
4ea42fb7 | 649 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
650 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
651 | bfd_link_repair_undef_list (&htab->root); | |
00cbee0a L |
652 | break; |
653 | case bfd_link_hash_new: | |
00cbee0a L |
654 | break; |
655 | case bfd_link_hash_indirect: | |
656 | /* We had a versioned symbol in a dynamic library. We make the | |
a0c8462f | 657 | the versioned symbol point to this one. */ |
00cbee0a L |
658 | bed = get_elf_backend_data (output_bfd); |
659 | hv = h; | |
660 | while (hv->root.type == bfd_link_hash_indirect | |
661 | || hv->root.type == bfd_link_hash_warning) | |
662 | hv = (struct elf_link_hash_entry *) hv->root.u.i.link; | |
663 | /* We don't need to update h->root.u since linker will set them | |
664 | later. */ | |
665 | h->root.type = bfd_link_hash_undefined; | |
666 | hv->root.type = bfd_link_hash_indirect; | |
667 | hv->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
668 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); | |
669 | break; | |
8e2a4f11 AM |
670 | default: |
671 | BFD_FAIL (); | |
c2596ca5 | 672 | return FALSE; |
55255dae | 673 | } |
45d6a902 AM |
674 | |
675 | /* If this symbol is being provided by the linker script, and it is | |
676 | currently defined by a dynamic object, but not by a regular | |
677 | object, then mark it as undefined so that the generic linker will | |
678 | force the correct value. */ | |
679 | if (provide | |
f5385ebf AM |
680 | && h->def_dynamic |
681 | && !h->def_regular) | |
45d6a902 AM |
682 | h->root.type = bfd_link_hash_undefined; |
683 | ||
684 | /* If this symbol is not being provided by the linker script, and it is | |
685 | currently defined by a dynamic object, but not by a regular object, | |
b531344c MR |
686 | then clear out any version information because the symbol will not be |
687 | associated with the dynamic object any more. */ | |
45d6a902 | 688 | if (!provide |
f5385ebf AM |
689 | && h->def_dynamic |
690 | && !h->def_regular) | |
b531344c MR |
691 | h->verinfo.verdef = NULL; |
692 | ||
693 | /* Make sure this symbol is not garbage collected. */ | |
694 | h->mark = 1; | |
45d6a902 | 695 | |
f5385ebf | 696 | h->def_regular = 1; |
45d6a902 | 697 | |
eb8476a6 | 698 | if (hidden) |
fe21a8fc | 699 | { |
91d6fa6a | 700 | bed = get_elf_backend_data (output_bfd); |
b8297068 AM |
701 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
702 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
fe21a8fc L |
703 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
704 | } | |
705 | ||
6fa3860b PB |
706 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
707 | and executables. */ | |
0e1862bb | 708 | if (!bfd_link_relocatable (info) |
6fa3860b PB |
709 | && h->dynindx != -1 |
710 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
711 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
712 | h->forced_local = 1; | |
713 | ||
f5385ebf AM |
714 | if ((h->def_dynamic |
715 | || h->ref_dynamic | |
6b3b0ab8 L |
716 | || bfd_link_dll (info) |
717 | || elf_hash_table (info)->is_relocatable_executable) | |
45d6a902 AM |
718 | && h->dynindx == -1) |
719 | { | |
c152c796 | 720 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
721 | return FALSE; |
722 | ||
723 | /* If this is a weak defined symbol, and we know a corresponding | |
724 | real symbol from the same dynamic object, make sure the real | |
725 | symbol is also made into a dynamic symbol. */ | |
60d67dc8 | 726 | if (h->is_weakalias) |
45d6a902 | 727 | { |
60d67dc8 AM |
728 | struct elf_link_hash_entry *def = weakdef (h); |
729 | ||
730 | if (def->dynindx == -1 | |
731 | && !bfd_elf_link_record_dynamic_symbol (info, def)) | |
45d6a902 AM |
732 | return FALSE; |
733 | } | |
734 | } | |
735 | ||
736 | return TRUE; | |
737 | } | |
42751cf3 | 738 | |
8c58d23b AM |
739 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
740 | success, and 2 on a failure caused by attempting to record a symbol | |
741 | in a discarded section, eg. a discarded link-once section symbol. */ | |
742 | ||
743 | int | |
c152c796 AM |
744 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
745 | bfd *input_bfd, | |
746 | long input_indx) | |
8c58d23b AM |
747 | { |
748 | bfd_size_type amt; | |
749 | struct elf_link_local_dynamic_entry *entry; | |
750 | struct elf_link_hash_table *eht; | |
751 | struct elf_strtab_hash *dynstr; | |
ef53be89 | 752 | size_t dynstr_index; |
8c58d23b AM |
753 | char *name; |
754 | Elf_External_Sym_Shndx eshndx; | |
755 | char esym[sizeof (Elf64_External_Sym)]; | |
756 | ||
0eddce27 | 757 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
758 | return 0; |
759 | ||
760 | /* See if the entry exists already. */ | |
761 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
762 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
763 | return 1; | |
764 | ||
765 | amt = sizeof (*entry); | |
a50b1753 | 766 | entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); |
8c58d23b AM |
767 | if (entry == NULL) |
768 | return 0; | |
769 | ||
770 | /* Go find the symbol, so that we can find it's name. */ | |
771 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 772 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
773 | { |
774 | bfd_release (input_bfd, entry); | |
775 | return 0; | |
776 | } | |
777 | ||
778 | if (entry->isym.st_shndx != SHN_UNDEF | |
4fbb74a6 | 779 | && entry->isym.st_shndx < SHN_LORESERVE) |
8c58d23b AM |
780 | { |
781 | asection *s; | |
782 | ||
783 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
784 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
785 | { | |
786 | /* We can still bfd_release here as nothing has done another | |
787 | bfd_alloc. We can't do this later in this function. */ | |
788 | bfd_release (input_bfd, entry); | |
789 | return 2; | |
790 | } | |
791 | } | |
792 | ||
793 | name = (bfd_elf_string_from_elf_section | |
794 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
795 | entry->isym.st_name)); | |
796 | ||
797 | dynstr = elf_hash_table (info)->dynstr; | |
798 | if (dynstr == NULL) | |
799 | { | |
800 | /* Create a strtab to hold the dynamic symbol names. */ | |
801 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
802 | if (dynstr == NULL) | |
803 | return 0; | |
804 | } | |
805 | ||
b34976b6 | 806 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
ef53be89 | 807 | if (dynstr_index == (size_t) -1) |
8c58d23b AM |
808 | return 0; |
809 | entry->isym.st_name = dynstr_index; | |
810 | ||
811 | eht = elf_hash_table (info); | |
812 | ||
813 | entry->next = eht->dynlocal; | |
814 | eht->dynlocal = entry; | |
815 | entry->input_bfd = input_bfd; | |
816 | entry->input_indx = input_indx; | |
817 | eht->dynsymcount++; | |
818 | ||
819 | /* Whatever binding the symbol had before, it's now local. */ | |
820 | entry->isym.st_info | |
821 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
822 | ||
823 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
824 | ||
825 | return 1; | |
826 | } | |
827 | ||
30b30c21 | 828 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 829 | |
30b30c21 | 830 | long |
268b6b39 AM |
831 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
832 | bfd *input_bfd, | |
833 | long input_indx) | |
30b30c21 RH |
834 | { |
835 | struct elf_link_local_dynamic_entry *e; | |
836 | ||
837 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
838 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
839 | return e->dynindx; | |
840 | return -1; | |
841 | } | |
842 | ||
843 | /* This function is used to renumber the dynamic symbols, if some of | |
844 | them are removed because they are marked as local. This is called | |
845 | via elf_link_hash_traverse. */ | |
846 | ||
b34976b6 | 847 | static bfd_boolean |
268b6b39 AM |
848 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
849 | void *data) | |
42751cf3 | 850 | { |
a50b1753 | 851 | size_t *count = (size_t *) data; |
30b30c21 | 852 | |
6fa3860b PB |
853 | if (h->forced_local) |
854 | return TRUE; | |
855 | ||
856 | if (h->dynindx != -1) | |
857 | h->dynindx = ++(*count); | |
858 | ||
859 | return TRUE; | |
860 | } | |
861 | ||
862 | ||
863 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
864 | STB_LOCAL binding. */ | |
865 | ||
866 | static bfd_boolean | |
867 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
868 | void *data) | |
869 | { | |
a50b1753 | 870 | size_t *count = (size_t *) data; |
6fa3860b | 871 | |
6fa3860b PB |
872 | if (!h->forced_local) |
873 | return TRUE; | |
874 | ||
42751cf3 | 875 | if (h->dynindx != -1) |
30b30c21 RH |
876 | h->dynindx = ++(*count); |
877 | ||
b34976b6 | 878 | return TRUE; |
42751cf3 | 879 | } |
30b30c21 | 880 | |
aee6f5b4 AO |
881 | /* Return true if the dynamic symbol for a given section should be |
882 | omitted when creating a shared library. */ | |
883 | bfd_boolean | |
884 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
885 | struct bfd_link_info *info, | |
886 | asection *p) | |
887 | { | |
74541ad4 | 888 | struct elf_link_hash_table *htab; |
ca55926c | 889 | asection *ip; |
74541ad4 | 890 | |
aee6f5b4 AO |
891 | switch (elf_section_data (p)->this_hdr.sh_type) |
892 | { | |
893 | case SHT_PROGBITS: | |
894 | case SHT_NOBITS: | |
895 | /* If sh_type is yet undecided, assume it could be | |
896 | SHT_PROGBITS/SHT_NOBITS. */ | |
897 | case SHT_NULL: | |
74541ad4 AM |
898 | htab = elf_hash_table (info); |
899 | if (p == htab->tls_sec) | |
900 | return FALSE; | |
901 | ||
902 | if (htab->text_index_section != NULL) | |
903 | return p != htab->text_index_section && p != htab->data_index_section; | |
904 | ||
ca55926c | 905 | return (htab->dynobj != NULL |
3d4d4302 | 906 | && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL |
ca55926c | 907 | && ip->output_section == p); |
aee6f5b4 AO |
908 | |
909 | /* There shouldn't be section relative relocations | |
910 | against any other section. */ | |
911 | default: | |
912 | return TRUE; | |
913 | } | |
914 | } | |
915 | ||
062e2358 | 916 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
917 | symbol for each output section, which come first. Next come symbols |
918 | which have been forced to local binding. Then all of the back-end | |
919 | allocated local dynamic syms, followed by the rest of the global | |
920 | symbols. */ | |
30b30c21 | 921 | |
554220db AM |
922 | static unsigned long |
923 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
924 | struct bfd_link_info *info, | |
925 | unsigned long *section_sym_count) | |
30b30c21 RH |
926 | { |
927 | unsigned long dynsymcount = 0; | |
928 | ||
0e1862bb L |
929 | if (bfd_link_pic (info) |
930 | || elf_hash_table (info)->is_relocatable_executable) | |
30b30c21 | 931 | { |
aee6f5b4 | 932 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
933 | asection *p; |
934 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 935 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
936 | && (p->flags & SEC_ALLOC) != 0 |
937 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
938 | elf_section_data (p)->dynindx = ++dynsymcount; | |
74541ad4 AM |
939 | else |
940 | elf_section_data (p)->dynindx = 0; | |
30b30c21 | 941 | } |
554220db | 942 | *section_sym_count = dynsymcount; |
30b30c21 | 943 | |
6fa3860b PB |
944 | elf_link_hash_traverse (elf_hash_table (info), |
945 | elf_link_renumber_local_hash_table_dynsyms, | |
946 | &dynsymcount); | |
947 | ||
30b30c21 RH |
948 | if (elf_hash_table (info)->dynlocal) |
949 | { | |
950 | struct elf_link_local_dynamic_entry *p; | |
951 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
952 | p->dynindx = ++dynsymcount; | |
953 | } | |
90ac2420 | 954 | elf_hash_table (info)->local_dynsymcount = dynsymcount; |
30b30c21 RH |
955 | |
956 | elf_link_hash_traverse (elf_hash_table (info), | |
957 | elf_link_renumber_hash_table_dynsyms, | |
958 | &dynsymcount); | |
959 | ||
d5486c43 L |
960 | /* There is an unused NULL entry at the head of the table which we |
961 | must account for in our count even if the table is empty since it | |
962 | is intended for the mandatory DT_SYMTAB tag (.dynsym section) in | |
963 | .dynamic section. */ | |
964 | dynsymcount++; | |
30b30c21 | 965 | |
ccabcbe5 AM |
966 | elf_hash_table (info)->dynsymcount = dynsymcount; |
967 | return dynsymcount; | |
30b30c21 | 968 | } |
252b5132 | 969 | |
54ac0771 L |
970 | /* Merge st_other field. */ |
971 | ||
972 | static void | |
973 | elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, | |
b8417128 | 974 | const Elf_Internal_Sym *isym, asection *sec, |
cd3416da | 975 | bfd_boolean definition, bfd_boolean dynamic) |
54ac0771 L |
976 | { |
977 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
978 | ||
979 | /* If st_other has a processor-specific meaning, specific | |
cd3416da | 980 | code might be needed here. */ |
54ac0771 L |
981 | if (bed->elf_backend_merge_symbol_attribute) |
982 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
983 | dynamic); | |
984 | ||
cd3416da | 985 | if (!dynamic) |
54ac0771 | 986 | { |
cd3416da AM |
987 | unsigned symvis = ELF_ST_VISIBILITY (isym->st_other); |
988 | unsigned hvis = ELF_ST_VISIBILITY (h->other); | |
54ac0771 | 989 | |
cd3416da AM |
990 | /* Keep the most constraining visibility. Leave the remainder |
991 | of the st_other field to elf_backend_merge_symbol_attribute. */ | |
992 | if (symvis - 1 < hvis - 1) | |
993 | h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); | |
54ac0771 | 994 | } |
b8417128 AM |
995 | else if (definition |
996 | && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT | |
997 | && (sec->flags & SEC_READONLY) == 0) | |
6cabe1ea | 998 | h->protected_def = 1; |
54ac0771 L |
999 | } |
1000 | ||
4f3fedcf AM |
1001 | /* This function is called when we want to merge a new symbol with an |
1002 | existing symbol. It handles the various cases which arise when we | |
1003 | find a definition in a dynamic object, or when there is already a | |
1004 | definition in a dynamic object. The new symbol is described by | |
1005 | NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table | |
1006 | entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK | |
1007 | if the old symbol was weak. We set POLD_ALIGNMENT to the alignment | |
1008 | of an old common symbol. We set OVERRIDE if the old symbol is | |
1009 | overriding a new definition. We set TYPE_CHANGE_OK if it is OK for | |
1010 | the type to change. We set SIZE_CHANGE_OK if it is OK for the size | |
1011 | to change. By OK to change, we mean that we shouldn't warn if the | |
1012 | type or size does change. */ | |
45d6a902 | 1013 | |
8a56bd02 | 1014 | static bfd_boolean |
268b6b39 AM |
1015 | _bfd_elf_merge_symbol (bfd *abfd, |
1016 | struct bfd_link_info *info, | |
1017 | const char *name, | |
1018 | Elf_Internal_Sym *sym, | |
1019 | asection **psec, | |
1020 | bfd_vma *pvalue, | |
4f3fedcf AM |
1021 | struct elf_link_hash_entry **sym_hash, |
1022 | bfd **poldbfd, | |
37a9e49a | 1023 | bfd_boolean *pold_weak, |
af44c138 | 1024 | unsigned int *pold_alignment, |
268b6b39 AM |
1025 | bfd_boolean *skip, |
1026 | bfd_boolean *override, | |
1027 | bfd_boolean *type_change_ok, | |
6e33951e L |
1028 | bfd_boolean *size_change_ok, |
1029 | bfd_boolean *matched) | |
252b5132 | 1030 | { |
7479dfd4 | 1031 | asection *sec, *oldsec; |
45d6a902 | 1032 | struct elf_link_hash_entry *h; |
90c984fc | 1033 | struct elf_link_hash_entry *hi; |
45d6a902 AM |
1034 | struct elf_link_hash_entry *flip; |
1035 | int bind; | |
1036 | bfd *oldbfd; | |
1037 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
0a36a439 | 1038 | bfd_boolean newweak, oldweak, newfunc, oldfunc; |
a4d8e49b | 1039 | const struct elf_backend_data *bed; |
6e33951e | 1040 | char *new_version; |
93f4de39 | 1041 | bfd_boolean default_sym = *matched; |
45d6a902 AM |
1042 | |
1043 | *skip = FALSE; | |
1044 | *override = FALSE; | |
1045 | ||
1046 | sec = *psec; | |
1047 | bind = ELF_ST_BIND (sym->st_info); | |
1048 | ||
1049 | if (! bfd_is_und_section (sec)) | |
1050 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
1051 | else | |
1052 | h = ((struct elf_link_hash_entry *) | |
1053 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
1054 | if (h == NULL) | |
1055 | return FALSE; | |
1056 | *sym_hash = h; | |
252b5132 | 1057 | |
88ba32a0 L |
1058 | bed = get_elf_backend_data (abfd); |
1059 | ||
6e33951e | 1060 | /* NEW_VERSION is the symbol version of the new symbol. */ |
422f1182 | 1061 | if (h->versioned != unversioned) |
6e33951e | 1062 | { |
422f1182 L |
1063 | /* Symbol version is unknown or versioned. */ |
1064 | new_version = strrchr (name, ELF_VER_CHR); | |
1065 | if (new_version) | |
1066 | { | |
1067 | if (h->versioned == unknown) | |
1068 | { | |
1069 | if (new_version > name && new_version[-1] != ELF_VER_CHR) | |
1070 | h->versioned = versioned_hidden; | |
1071 | else | |
1072 | h->versioned = versioned; | |
1073 | } | |
1074 | new_version += 1; | |
1075 | if (new_version[0] == '\0') | |
1076 | new_version = NULL; | |
1077 | } | |
1078 | else | |
1079 | h->versioned = unversioned; | |
6e33951e | 1080 | } |
422f1182 L |
1081 | else |
1082 | new_version = NULL; | |
6e33951e | 1083 | |
90c984fc L |
1084 | /* For merging, we only care about real symbols. But we need to make |
1085 | sure that indirect symbol dynamic flags are updated. */ | |
1086 | hi = h; | |
45d6a902 AM |
1087 | while (h->root.type == bfd_link_hash_indirect |
1088 | || h->root.type == bfd_link_hash_warning) | |
1089 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1090 | ||
6e33951e L |
1091 | if (!*matched) |
1092 | { | |
1093 | if (hi == h || h->root.type == bfd_link_hash_new) | |
1094 | *matched = TRUE; | |
1095 | else | |
1096 | { | |
ae7683d2 | 1097 | /* OLD_HIDDEN is true if the existing symbol is only visible |
6e33951e | 1098 | to the symbol with the same symbol version. NEW_HIDDEN is |
ae7683d2 | 1099 | true if the new symbol is only visible to the symbol with |
6e33951e | 1100 | the same symbol version. */ |
422f1182 L |
1101 | bfd_boolean old_hidden = h->versioned == versioned_hidden; |
1102 | bfd_boolean new_hidden = hi->versioned == versioned_hidden; | |
6e33951e L |
1103 | if (!old_hidden && !new_hidden) |
1104 | /* The new symbol matches the existing symbol if both | |
1105 | aren't hidden. */ | |
1106 | *matched = TRUE; | |
1107 | else | |
1108 | { | |
1109 | /* OLD_VERSION is the symbol version of the existing | |
1110 | symbol. */ | |
422f1182 L |
1111 | char *old_version; |
1112 | ||
1113 | if (h->versioned >= versioned) | |
1114 | old_version = strrchr (h->root.root.string, | |
1115 | ELF_VER_CHR) + 1; | |
1116 | else | |
1117 | old_version = NULL; | |
6e33951e L |
1118 | |
1119 | /* The new symbol matches the existing symbol if they | |
1120 | have the same symbol version. */ | |
1121 | *matched = (old_version == new_version | |
1122 | || (old_version != NULL | |
1123 | && new_version != NULL | |
1124 | && strcmp (old_version, new_version) == 0)); | |
1125 | } | |
1126 | } | |
1127 | } | |
1128 | ||
934bce08 AM |
1129 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
1130 | existing symbol. */ | |
1131 | ||
1132 | oldbfd = NULL; | |
1133 | oldsec = NULL; | |
1134 | switch (h->root.type) | |
1135 | { | |
1136 | default: | |
1137 | break; | |
1138 | ||
1139 | case bfd_link_hash_undefined: | |
1140 | case bfd_link_hash_undefweak: | |
1141 | oldbfd = h->root.u.undef.abfd; | |
1142 | break; | |
1143 | ||
1144 | case bfd_link_hash_defined: | |
1145 | case bfd_link_hash_defweak: | |
1146 | oldbfd = h->root.u.def.section->owner; | |
1147 | oldsec = h->root.u.def.section; | |
1148 | break; | |
1149 | ||
1150 | case bfd_link_hash_common: | |
1151 | oldbfd = h->root.u.c.p->section->owner; | |
1152 | oldsec = h->root.u.c.p->section; | |
1153 | if (pold_alignment) | |
1154 | *pold_alignment = h->root.u.c.p->alignment_power; | |
1155 | break; | |
1156 | } | |
1157 | if (poldbfd && *poldbfd == NULL) | |
1158 | *poldbfd = oldbfd; | |
1159 | ||
1160 | /* Differentiate strong and weak symbols. */ | |
1161 | newweak = bind == STB_WEAK; | |
1162 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1163 | || h->root.type == bfd_link_hash_undefweak); | |
1164 | if (pold_weak) | |
1165 | *pold_weak = oldweak; | |
1166 | ||
40b36307 | 1167 | /* We have to check it for every instance since the first few may be |
ee659f1f | 1168 | references and not all compilers emit symbol type for undefined |
40b36307 L |
1169 | symbols. */ |
1170 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
1171 | ||
ee659f1f AM |
1172 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, |
1173 | respectively, is from a dynamic object. */ | |
1174 | ||
1175 | newdyn = (abfd->flags & DYNAMIC) != 0; | |
1176 | ||
1177 | /* ref_dynamic_nonweak and dynamic_def flags track actual undefined | |
1178 | syms and defined syms in dynamic libraries respectively. | |
1179 | ref_dynamic on the other hand can be set for a symbol defined in | |
1180 | a dynamic library, and def_dynamic may not be set; When the | |
1181 | definition in a dynamic lib is overridden by a definition in the | |
1182 | executable use of the symbol in the dynamic lib becomes a | |
1183 | reference to the executable symbol. */ | |
1184 | if (newdyn) | |
1185 | { | |
1186 | if (bfd_is_und_section (sec)) | |
1187 | { | |
1188 | if (bind != STB_WEAK) | |
1189 | { | |
1190 | h->ref_dynamic_nonweak = 1; | |
1191 | hi->ref_dynamic_nonweak = 1; | |
1192 | } | |
1193 | } | |
1194 | else | |
1195 | { | |
6e33951e L |
1196 | /* Update the existing symbol only if they match. */ |
1197 | if (*matched) | |
1198 | h->dynamic_def = 1; | |
ee659f1f AM |
1199 | hi->dynamic_def = 1; |
1200 | } | |
1201 | } | |
1202 | ||
45d6a902 AM |
1203 | /* If we just created the symbol, mark it as being an ELF symbol. |
1204 | Other than that, there is nothing to do--there is no merge issue | |
1205 | with a newly defined symbol--so we just return. */ | |
1206 | ||
1207 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 1208 | { |
f5385ebf | 1209 | h->non_elf = 0; |
45d6a902 AM |
1210 | return TRUE; |
1211 | } | |
252b5132 | 1212 | |
45d6a902 AM |
1213 | /* In cases involving weak versioned symbols, we may wind up trying |
1214 | to merge a symbol with itself. Catch that here, to avoid the | |
1215 | confusion that results if we try to override a symbol with | |
1216 | itself. The additional tests catch cases like | |
1217 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
1218 | dynamic object, which we do want to handle here. */ | |
1219 | if (abfd == oldbfd | |
895fa45f | 1220 | && (newweak || oldweak) |
45d6a902 | 1221 | && ((abfd->flags & DYNAMIC) == 0 |
f5385ebf | 1222 | || !h->def_regular)) |
45d6a902 AM |
1223 | return TRUE; |
1224 | ||
707bba77 | 1225 | olddyn = FALSE; |
45d6a902 AM |
1226 | if (oldbfd != NULL) |
1227 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 1228 | else if (oldsec != NULL) |
45d6a902 | 1229 | { |
707bba77 | 1230 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 1231 | indices used by MIPS ELF. */ |
707bba77 | 1232 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 1233 | } |
252b5132 | 1234 | |
1a3b5c34 AM |
1235 | /* Handle a case where plugin_notice won't be called and thus won't |
1236 | set the non_ir_ref flags on the first pass over symbols. */ | |
1237 | if (oldbfd != NULL | |
1238 | && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN) | |
1239 | && newdyn != olddyn) | |
1240 | { | |
1241 | h->root.non_ir_ref_dynamic = TRUE; | |
1242 | hi->root.non_ir_ref_dynamic = TRUE; | |
1243 | } | |
1244 | ||
45d6a902 AM |
1245 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
1246 | respectively, appear to be a definition rather than reference. */ | |
1247 | ||
707bba77 | 1248 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 1249 | |
707bba77 AM |
1250 | olddef = (h->root.type != bfd_link_hash_undefined |
1251 | && h->root.type != bfd_link_hash_undefweak | |
202ac193 | 1252 | && h->root.type != bfd_link_hash_common); |
45d6a902 | 1253 | |
0a36a439 L |
1254 | /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, |
1255 | respectively, appear to be a function. */ | |
1256 | ||
1257 | newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1258 | && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); | |
1259 | ||
1260 | oldfunc = (h->type != STT_NOTYPE | |
1261 | && bed->is_function_type (h->type)); | |
1262 | ||
c5d37467 | 1263 | if (!(newfunc && oldfunc) |
5b677558 AM |
1264 | && ELF_ST_TYPE (sym->st_info) != h->type |
1265 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1266 | && h->type != STT_NOTYPE | |
c5d37467 AM |
1267 | && (newdef || bfd_is_com_section (sec)) |
1268 | && (olddef || h->root.type == bfd_link_hash_common)) | |
580a2b6e | 1269 | { |
c5d37467 AM |
1270 | /* If creating a default indirect symbol ("foo" or "foo@") from |
1271 | a dynamic versioned definition ("foo@@") skip doing so if | |
1272 | there is an existing regular definition with a different | |
1273 | type. We don't want, for example, a "time" variable in the | |
1274 | executable overriding a "time" function in a shared library. */ | |
1275 | if (newdyn | |
1276 | && !olddyn) | |
1277 | { | |
1278 | *skip = TRUE; | |
1279 | return TRUE; | |
1280 | } | |
1281 | ||
1282 | /* When adding a symbol from a regular object file after we have | |
1283 | created indirect symbols, undo the indirection and any | |
1284 | dynamic state. */ | |
1285 | if (hi != h | |
1286 | && !newdyn | |
1287 | && olddyn) | |
1288 | { | |
1289 | h = hi; | |
1290 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1291 | h->forced_local = 0; | |
1292 | h->ref_dynamic = 0; | |
1293 | h->def_dynamic = 0; | |
1294 | h->dynamic_def = 0; | |
1295 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1296 | { | |
1297 | h->root.type = bfd_link_hash_undefined; | |
1298 | h->root.u.undef.abfd = abfd; | |
1299 | } | |
1300 | else | |
1301 | { | |
1302 | h->root.type = bfd_link_hash_new; | |
1303 | h->root.u.undef.abfd = NULL; | |
1304 | } | |
1305 | return TRUE; | |
1306 | } | |
580a2b6e L |
1307 | } |
1308 | ||
4c34aff8 AM |
1309 | /* Check TLS symbols. We don't check undefined symbols introduced |
1310 | by "ld -u" which have no type (and oldbfd NULL), and we don't | |
1311 | check symbols from plugins because they also have no type. */ | |
1312 | if (oldbfd != NULL | |
1313 | && (oldbfd->flags & BFD_PLUGIN) == 0 | |
1314 | && (abfd->flags & BFD_PLUGIN) == 0 | |
1315 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1316 | && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) | |
7479dfd4 L |
1317 | { |
1318 | bfd *ntbfd, *tbfd; | |
1319 | bfd_boolean ntdef, tdef; | |
1320 | asection *ntsec, *tsec; | |
1321 | ||
1322 | if (h->type == STT_TLS) | |
1323 | { | |
3b36f7e6 | 1324 | ntbfd = abfd; |
7479dfd4 L |
1325 | ntsec = sec; |
1326 | ntdef = newdef; | |
1327 | tbfd = oldbfd; | |
1328 | tsec = oldsec; | |
1329 | tdef = olddef; | |
1330 | } | |
1331 | else | |
1332 | { | |
1333 | ntbfd = oldbfd; | |
1334 | ntsec = oldsec; | |
1335 | ntdef = olddef; | |
1336 | tbfd = abfd; | |
1337 | tsec = sec; | |
1338 | tdef = newdef; | |
1339 | } | |
1340 | ||
1341 | if (tdef && ntdef) | |
4eca0228 | 1342 | _bfd_error_handler |
695344c0 | 1343 | /* xgettext:c-format */ |
191c0c42 AM |
1344 | (_("%s: TLS definition in %B section %A " |
1345 | "mismatches non-TLS definition in %B section %A"), | |
c08bb8dd | 1346 | h->root.root.string, tbfd, tsec, ntbfd, ntsec); |
7479dfd4 | 1347 | else if (!tdef && !ntdef) |
4eca0228 | 1348 | _bfd_error_handler |
695344c0 | 1349 | /* xgettext:c-format */ |
191c0c42 AM |
1350 | (_("%s: TLS reference in %B " |
1351 | "mismatches non-TLS reference in %B"), | |
c08bb8dd | 1352 | h->root.root.string, tbfd, ntbfd); |
7479dfd4 | 1353 | else if (tdef) |
4eca0228 | 1354 | _bfd_error_handler |
695344c0 | 1355 | /* xgettext:c-format */ |
191c0c42 AM |
1356 | (_("%s: TLS definition in %B section %A " |
1357 | "mismatches non-TLS reference in %B"), | |
c08bb8dd | 1358 | h->root.root.string, tbfd, tsec, ntbfd); |
7479dfd4 | 1359 | else |
4eca0228 | 1360 | _bfd_error_handler |
695344c0 | 1361 | /* xgettext:c-format */ |
191c0c42 AM |
1362 | (_("%s: TLS reference in %B " |
1363 | "mismatches non-TLS definition in %B section %A"), | |
c08bb8dd | 1364 | h->root.root.string, tbfd, ntbfd, ntsec); |
7479dfd4 L |
1365 | |
1366 | bfd_set_error (bfd_error_bad_value); | |
1367 | return FALSE; | |
1368 | } | |
1369 | ||
45d6a902 AM |
1370 | /* If the old symbol has non-default visibility, we ignore the new |
1371 | definition from a dynamic object. */ | |
1372 | if (newdyn | |
9c7a29a3 | 1373 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1374 | && !bfd_is_und_section (sec)) |
1375 | { | |
1376 | *skip = TRUE; | |
1377 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1378 | h->ref_dynamic = 1; |
90c984fc | 1379 | hi->ref_dynamic = 1; |
45d6a902 AM |
1380 | /* A protected symbol has external availability. Make sure it is |
1381 | recorded as dynamic. | |
1382 | ||
1383 | FIXME: Should we check type and size for protected symbol? */ | |
1384 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1385 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1386 | else |
1387 | return TRUE; | |
1388 | } | |
1389 | else if (!newdyn | |
9c7a29a3 | 1390 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1391 | && h->def_dynamic) |
45d6a902 AM |
1392 | { |
1393 | /* If the new symbol with non-default visibility comes from a | |
1394 | relocatable file and the old definition comes from a dynamic | |
1395 | object, we remove the old definition. */ | |
6c9b78e6 | 1396 | if (hi->root.type == bfd_link_hash_indirect) |
d2dee3b2 L |
1397 | { |
1398 | /* Handle the case where the old dynamic definition is | |
1399 | default versioned. We need to copy the symbol info from | |
1400 | the symbol with default version to the normal one if it | |
1401 | was referenced before. */ | |
1402 | if (h->ref_regular) | |
1403 | { | |
6c9b78e6 | 1404 | hi->root.type = h->root.type; |
d2dee3b2 | 1405 | h->root.type = bfd_link_hash_indirect; |
6c9b78e6 | 1406 | (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); |
aed81c4e | 1407 | |
6c9b78e6 | 1408 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; |
aed81c4e | 1409 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
d2dee3b2 | 1410 | { |
aed81c4e MR |
1411 | /* If the new symbol is hidden or internal, completely undo |
1412 | any dynamic link state. */ | |
1413 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1414 | h->forced_local = 0; | |
1415 | h->ref_dynamic = 0; | |
d2dee3b2 L |
1416 | } |
1417 | else | |
aed81c4e MR |
1418 | h->ref_dynamic = 1; |
1419 | ||
1420 | h->def_dynamic = 0; | |
aed81c4e MR |
1421 | /* FIXME: Should we check type and size for protected symbol? */ |
1422 | h->size = 0; | |
1423 | h->type = 0; | |
1424 | ||
6c9b78e6 | 1425 | h = hi; |
d2dee3b2 L |
1426 | } |
1427 | else | |
6c9b78e6 | 1428 | h = hi; |
d2dee3b2 | 1429 | } |
1de1a317 | 1430 | |
f5eda473 AM |
1431 | /* If the old symbol was undefined before, then it will still be |
1432 | on the undefs list. If the new symbol is undefined or | |
1433 | common, we can't make it bfd_link_hash_new here, because new | |
1434 | undefined or common symbols will be added to the undefs list | |
1435 | by _bfd_generic_link_add_one_symbol. Symbols may not be | |
1436 | added twice to the undefs list. Also, if the new symbol is | |
1437 | undefweak then we don't want to lose the strong undef. */ | |
1438 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1de1a317 | 1439 | { |
1de1a317 | 1440 | h->root.type = bfd_link_hash_undefined; |
1de1a317 L |
1441 | h->root.u.undef.abfd = abfd; |
1442 | } | |
1443 | else | |
1444 | { | |
1445 | h->root.type = bfd_link_hash_new; | |
1446 | h->root.u.undef.abfd = NULL; | |
1447 | } | |
1448 | ||
f5eda473 | 1449 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
252b5132 | 1450 | { |
f5eda473 AM |
1451 | /* If the new symbol is hidden or internal, completely undo |
1452 | any dynamic link state. */ | |
1453 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1454 | h->forced_local = 0; | |
1455 | h->ref_dynamic = 0; | |
45d6a902 | 1456 | } |
f5eda473 AM |
1457 | else |
1458 | h->ref_dynamic = 1; | |
1459 | h->def_dynamic = 0; | |
45d6a902 AM |
1460 | /* FIXME: Should we check type and size for protected symbol? */ |
1461 | h->size = 0; | |
1462 | h->type = 0; | |
1463 | return TRUE; | |
1464 | } | |
14a793b2 | 1465 | |
15b43f48 AM |
1466 | /* If a new weak symbol definition comes from a regular file and the |
1467 | old symbol comes from a dynamic library, we treat the new one as | |
1468 | strong. Similarly, an old weak symbol definition from a regular | |
1469 | file is treated as strong when the new symbol comes from a dynamic | |
1470 | library. Further, an old weak symbol from a dynamic library is | |
1471 | treated as strong if the new symbol is from a dynamic library. | |
1472 | This reflects the way glibc's ld.so works. | |
1473 | ||
1474 | Do this before setting *type_change_ok or *size_change_ok so that | |
1475 | we warn properly when dynamic library symbols are overridden. */ | |
1476 | ||
1477 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1478 | newweak = FALSE; |
15b43f48 | 1479 | if (olddef && newdyn) |
0f8a2703 AM |
1480 | oldweak = FALSE; |
1481 | ||
d334575b | 1482 | /* Allow changes between different types of function symbol. */ |
0a36a439 | 1483 | if (newfunc && oldfunc) |
fcb93ecf PB |
1484 | *type_change_ok = TRUE; |
1485 | ||
79349b09 AM |
1486 | /* It's OK to change the type if either the existing symbol or the |
1487 | new symbol is weak. A type change is also OK if the old symbol | |
1488 | is undefined and the new symbol is defined. */ | |
252b5132 | 1489 | |
79349b09 AM |
1490 | if (oldweak |
1491 | || newweak | |
1492 | || (newdef | |
1493 | && h->root.type == bfd_link_hash_undefined)) | |
1494 | *type_change_ok = TRUE; | |
1495 | ||
1496 | /* It's OK to change the size if either the existing symbol or the | |
1497 | new symbol is weak, or if the old symbol is undefined. */ | |
1498 | ||
1499 | if (*type_change_ok | |
1500 | || h->root.type == bfd_link_hash_undefined) | |
1501 | *size_change_ok = TRUE; | |
45d6a902 | 1502 | |
45d6a902 AM |
1503 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1504 | symbol, respectively, appears to be a common symbol in a dynamic | |
1505 | object. If a symbol appears in an uninitialized section, and is | |
1506 | not weak, and is not a function, then it may be a common symbol | |
1507 | which was resolved when the dynamic object was created. We want | |
1508 | to treat such symbols specially, because they raise special | |
1509 | considerations when setting the symbol size: if the symbol | |
1510 | appears as a common symbol in a regular object, and the size in | |
1511 | the regular object is larger, we must make sure that we use the | |
1512 | larger size. This problematic case can always be avoided in C, | |
1513 | but it must be handled correctly when using Fortran shared | |
1514 | libraries. | |
1515 | ||
1516 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1517 | likewise for OLDDYNCOMMON and OLDDEF. | |
1518 | ||
1519 | Note that this test is just a heuristic, and that it is quite | |
1520 | possible to have an uninitialized symbol in a shared object which | |
1521 | is really a definition, rather than a common symbol. This could | |
1522 | lead to some minor confusion when the symbol really is a common | |
1523 | symbol in some regular object. However, I think it will be | |
1524 | harmless. */ | |
1525 | ||
1526 | if (newdyn | |
1527 | && newdef | |
79349b09 | 1528 | && !newweak |
45d6a902 AM |
1529 | && (sec->flags & SEC_ALLOC) != 0 |
1530 | && (sec->flags & SEC_LOAD) == 0 | |
1531 | && sym->st_size > 0 | |
0a36a439 | 1532 | && !newfunc) |
45d6a902 AM |
1533 | newdyncommon = TRUE; |
1534 | else | |
1535 | newdyncommon = FALSE; | |
1536 | ||
1537 | if (olddyn | |
1538 | && olddef | |
1539 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1540 | && h->def_dynamic |
45d6a902 AM |
1541 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1542 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1543 | && h->size > 0 | |
0a36a439 | 1544 | && !oldfunc) |
45d6a902 AM |
1545 | olddyncommon = TRUE; |
1546 | else | |
1547 | olddyncommon = FALSE; | |
1548 | ||
a4d8e49b L |
1549 | /* We now know everything about the old and new symbols. We ask the |
1550 | backend to check if we can merge them. */ | |
5d13b3b3 AM |
1551 | if (bed->merge_symbol != NULL) |
1552 | { | |
1553 | if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) | |
1554 | return FALSE; | |
1555 | sec = *psec; | |
1556 | } | |
a4d8e49b | 1557 | |
93f4de39 RL |
1558 | /* There are multiple definitions of a normal symbol. |
1559 | Skip the default symbol as well. */ | |
1560 | if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak | |
1561 | && !default_sym && h->def_regular) | |
1562 | { | |
1563 | /* Handle a multiple definition. */ | |
1564 | (*info->callbacks->multiple_definition) (info, &h->root, | |
1565 | abfd, sec, *pvalue); | |
1566 | *skip = TRUE; | |
1567 | return TRUE; | |
1568 | } | |
1569 | ||
45d6a902 AM |
1570 | /* If both the old and the new symbols look like common symbols in a |
1571 | dynamic object, set the size of the symbol to the larger of the | |
1572 | two. */ | |
1573 | ||
1574 | if (olddyncommon | |
1575 | && newdyncommon | |
1576 | && sym->st_size != h->size) | |
1577 | { | |
1578 | /* Since we think we have two common symbols, issue a multiple | |
1579 | common warning if desired. Note that we only warn if the | |
1580 | size is different. If the size is the same, we simply let | |
1581 | the old symbol override the new one as normally happens with | |
1582 | symbols defined in dynamic objects. */ | |
1583 | ||
1a72702b AM |
1584 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1585 | bfd_link_hash_common, sym->st_size); | |
45d6a902 AM |
1586 | if (sym->st_size > h->size) |
1587 | h->size = sym->st_size; | |
252b5132 | 1588 | |
45d6a902 | 1589 | *size_change_ok = TRUE; |
252b5132 RH |
1590 | } |
1591 | ||
45d6a902 AM |
1592 | /* If we are looking at a dynamic object, and we have found a |
1593 | definition, we need to see if the symbol was already defined by | |
1594 | some other object. If so, we want to use the existing | |
1595 | definition, and we do not want to report a multiple symbol | |
1596 | definition error; we do this by clobbering *PSEC to be | |
1597 | bfd_und_section_ptr. | |
1598 | ||
1599 | We treat a common symbol as a definition if the symbol in the | |
1600 | shared library is a function, since common symbols always | |
1601 | represent variables; this can cause confusion in principle, but | |
1602 | any such confusion would seem to indicate an erroneous program or | |
1603 | shared library. We also permit a common symbol in a regular | |
8170f769 | 1604 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1605 | |
1606 | if (newdyn | |
1607 | && newdef | |
77cfaee6 | 1608 | && (olddef |
45d6a902 | 1609 | || (h->root.type == bfd_link_hash_common |
8170f769 | 1610 | && (newweak || newfunc)))) |
45d6a902 AM |
1611 | { |
1612 | *override = TRUE; | |
1613 | newdef = FALSE; | |
1614 | newdyncommon = FALSE; | |
252b5132 | 1615 | |
45d6a902 AM |
1616 | *psec = sec = bfd_und_section_ptr; |
1617 | *size_change_ok = TRUE; | |
252b5132 | 1618 | |
45d6a902 AM |
1619 | /* If we get here when the old symbol is a common symbol, then |
1620 | we are explicitly letting it override a weak symbol or | |
1621 | function in a dynamic object, and we don't want to warn about | |
1622 | a type change. If the old symbol is a defined symbol, a type | |
1623 | change warning may still be appropriate. */ | |
252b5132 | 1624 | |
45d6a902 AM |
1625 | if (h->root.type == bfd_link_hash_common) |
1626 | *type_change_ok = TRUE; | |
1627 | } | |
1628 | ||
1629 | /* Handle the special case of an old common symbol merging with a | |
1630 | new symbol which looks like a common symbol in a shared object. | |
1631 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1632 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1633 | right thing. */ | |
45d6a902 AM |
1634 | |
1635 | if (newdyncommon | |
1636 | && h->root.type == bfd_link_hash_common) | |
1637 | { | |
1638 | *override = TRUE; | |
1639 | newdef = FALSE; | |
1640 | newdyncommon = FALSE; | |
1641 | *pvalue = sym->st_size; | |
a4d8e49b | 1642 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1643 | *size_change_ok = TRUE; |
1644 | } | |
1645 | ||
c5e2cead | 1646 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1647 | if (newdef && olddef && newweak) |
54ac0771 | 1648 | { |
35ed3f94 | 1649 | /* Don't skip new non-IR weak syms. */ |
3a5dbfb2 AM |
1650 | if (!(oldbfd != NULL |
1651 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
35ed3f94 | 1652 | && (abfd->flags & BFD_PLUGIN) == 0)) |
57fa7b8c AM |
1653 | { |
1654 | newdef = FALSE; | |
1655 | *skip = TRUE; | |
1656 | } | |
54ac0771 L |
1657 | |
1658 | /* Merge st_other. If the symbol already has a dynamic index, | |
1659 | but visibility says it should not be visible, turn it into a | |
1660 | local symbol. */ | |
b8417128 | 1661 | elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn); |
54ac0771 L |
1662 | if (h->dynindx != -1) |
1663 | switch (ELF_ST_VISIBILITY (h->other)) | |
1664 | { | |
1665 | case STV_INTERNAL: | |
1666 | case STV_HIDDEN: | |
1667 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1668 | break; | |
1669 | } | |
1670 | } | |
c5e2cead | 1671 | |
45d6a902 AM |
1672 | /* If the old symbol is from a dynamic object, and the new symbol is |
1673 | a definition which is not from a dynamic object, then the new | |
1674 | symbol overrides the old symbol. Symbols from regular files | |
1675 | always take precedence over symbols from dynamic objects, even if | |
1676 | they are defined after the dynamic object in the link. | |
1677 | ||
1678 | As above, we again permit a common symbol in a regular object to | |
1679 | override a definition in a shared object if the shared object | |
0f8a2703 | 1680 | symbol is a function or is weak. */ |
45d6a902 AM |
1681 | |
1682 | flip = NULL; | |
77cfaee6 | 1683 | if (!newdyn |
45d6a902 AM |
1684 | && (newdef |
1685 | || (bfd_is_com_section (sec) | |
0a36a439 | 1686 | && (oldweak || oldfunc))) |
45d6a902 AM |
1687 | && olddyn |
1688 | && olddef | |
f5385ebf | 1689 | && h->def_dynamic) |
45d6a902 AM |
1690 | { |
1691 | /* Change the hash table entry to undefined, and let | |
1692 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1693 | new definition. */ | |
1694 | ||
1695 | h->root.type = bfd_link_hash_undefined; | |
1696 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1697 | *size_change_ok = TRUE; | |
1698 | ||
1699 | olddef = FALSE; | |
1700 | olddyncommon = FALSE; | |
1701 | ||
1702 | /* We again permit a type change when a common symbol may be | |
1703 | overriding a function. */ | |
1704 | ||
1705 | if (bfd_is_com_section (sec)) | |
0a36a439 L |
1706 | { |
1707 | if (oldfunc) | |
1708 | { | |
1709 | /* If a common symbol overrides a function, make sure | |
1710 | that it isn't defined dynamically nor has type | |
1711 | function. */ | |
1712 | h->def_dynamic = 0; | |
1713 | h->type = STT_NOTYPE; | |
1714 | } | |
1715 | *type_change_ok = TRUE; | |
1716 | } | |
45d6a902 | 1717 | |
6c9b78e6 AM |
1718 | if (hi->root.type == bfd_link_hash_indirect) |
1719 | flip = hi; | |
45d6a902 AM |
1720 | else |
1721 | /* This union may have been set to be non-NULL when this symbol | |
1722 | was seen in a dynamic object. We must force the union to be | |
1723 | NULL, so that it is correct for a regular symbol. */ | |
1724 | h->verinfo.vertree = NULL; | |
1725 | } | |
1726 | ||
1727 | /* Handle the special case of a new common symbol merging with an | |
1728 | old symbol that looks like it might be a common symbol defined in | |
1729 | a shared object. Note that we have already handled the case in | |
1730 | which a new common symbol should simply override the definition | |
1731 | in the shared library. */ | |
1732 | ||
1733 | if (! newdyn | |
1734 | && bfd_is_com_section (sec) | |
1735 | && olddyncommon) | |
1736 | { | |
1737 | /* It would be best if we could set the hash table entry to a | |
1738 | common symbol, but we don't know what to use for the section | |
1739 | or the alignment. */ | |
1a72702b AM |
1740 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1741 | bfd_link_hash_common, sym->st_size); | |
45d6a902 | 1742 | |
4cc11e76 | 1743 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1744 | larger, pretend that the new symbol has its size. */ |
1745 | ||
1746 | if (h->size > *pvalue) | |
1747 | *pvalue = h->size; | |
1748 | ||
af44c138 L |
1749 | /* We need to remember the alignment required by the symbol |
1750 | in the dynamic object. */ | |
1751 | BFD_ASSERT (pold_alignment); | |
1752 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1753 | |
1754 | olddef = FALSE; | |
1755 | olddyncommon = FALSE; | |
1756 | ||
1757 | h->root.type = bfd_link_hash_undefined; | |
1758 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1759 | ||
1760 | *size_change_ok = TRUE; | |
1761 | *type_change_ok = TRUE; | |
1762 | ||
6c9b78e6 AM |
1763 | if (hi->root.type == bfd_link_hash_indirect) |
1764 | flip = hi; | |
45d6a902 AM |
1765 | else |
1766 | h->verinfo.vertree = NULL; | |
1767 | } | |
1768 | ||
1769 | if (flip != NULL) | |
1770 | { | |
1771 | /* Handle the case where we had a versioned symbol in a dynamic | |
1772 | library and now find a definition in a normal object. In this | |
1773 | case, we make the versioned symbol point to the normal one. */ | |
45d6a902 | 1774 | flip->root.type = h->root.type; |
00cbee0a | 1775 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1776 | h->root.type = bfd_link_hash_indirect; |
1777 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1778 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1779 | if (h->def_dynamic) |
45d6a902 | 1780 | { |
f5385ebf AM |
1781 | h->def_dynamic = 0; |
1782 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1783 | } |
1784 | } | |
1785 | ||
45d6a902 AM |
1786 | return TRUE; |
1787 | } | |
1788 | ||
1789 | /* This function is called to create an indirect symbol from the | |
1790 | default for the symbol with the default version if needed. The | |
4f3fedcf | 1791 | symbol is described by H, NAME, SYM, SEC, and VALUE. We |
0f8a2703 | 1792 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 | 1793 | |
28caa186 | 1794 | static bfd_boolean |
268b6b39 AM |
1795 | _bfd_elf_add_default_symbol (bfd *abfd, |
1796 | struct bfd_link_info *info, | |
1797 | struct elf_link_hash_entry *h, | |
1798 | const char *name, | |
1799 | Elf_Internal_Sym *sym, | |
4f3fedcf AM |
1800 | asection *sec, |
1801 | bfd_vma value, | |
1802 | bfd **poldbfd, | |
e3c9d234 | 1803 | bfd_boolean *dynsym) |
45d6a902 AM |
1804 | { |
1805 | bfd_boolean type_change_ok; | |
1806 | bfd_boolean size_change_ok; | |
1807 | bfd_boolean skip; | |
1808 | char *shortname; | |
1809 | struct elf_link_hash_entry *hi; | |
1810 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1811 | const struct elf_backend_data *bed; |
45d6a902 AM |
1812 | bfd_boolean collect; |
1813 | bfd_boolean dynamic; | |
e3c9d234 | 1814 | bfd_boolean override; |
45d6a902 AM |
1815 | char *p; |
1816 | size_t len, shortlen; | |
ffd65175 | 1817 | asection *tmp_sec; |
6e33951e | 1818 | bfd_boolean matched; |
45d6a902 | 1819 | |
422f1182 L |
1820 | if (h->versioned == unversioned || h->versioned == versioned_hidden) |
1821 | return TRUE; | |
1822 | ||
45d6a902 AM |
1823 | /* If this symbol has a version, and it is the default version, we |
1824 | create an indirect symbol from the default name to the fully | |
1825 | decorated name. This will cause external references which do not | |
1826 | specify a version to be bound to this version of the symbol. */ | |
1827 | p = strchr (name, ELF_VER_CHR); | |
422f1182 L |
1828 | if (h->versioned == unknown) |
1829 | { | |
1830 | if (p == NULL) | |
1831 | { | |
1832 | h->versioned = unversioned; | |
1833 | return TRUE; | |
1834 | } | |
1835 | else | |
1836 | { | |
1837 | if (p[1] != ELF_VER_CHR) | |
1838 | { | |
1839 | h->versioned = versioned_hidden; | |
1840 | return TRUE; | |
1841 | } | |
1842 | else | |
1843 | h->versioned = versioned; | |
1844 | } | |
1845 | } | |
4373f8af L |
1846 | else |
1847 | { | |
1848 | /* PR ld/19073: We may see an unversioned definition after the | |
1849 | default version. */ | |
1850 | if (p == NULL) | |
1851 | return TRUE; | |
1852 | } | |
45d6a902 | 1853 | |
45d6a902 AM |
1854 | bed = get_elf_backend_data (abfd); |
1855 | collect = bed->collect; | |
1856 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1857 | ||
1858 | shortlen = p - name; | |
a50b1753 | 1859 | shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); |
45d6a902 AM |
1860 | if (shortname == NULL) |
1861 | return FALSE; | |
1862 | memcpy (shortname, name, shortlen); | |
1863 | shortname[shortlen] = '\0'; | |
1864 | ||
1865 | /* We are going to create a new symbol. Merge it with any existing | |
1866 | symbol with this name. For the purposes of the merge, act as | |
1867 | though we were defining the symbol we just defined, although we | |
1868 | actually going to define an indirect symbol. */ | |
1869 | type_change_ok = FALSE; | |
1870 | size_change_ok = FALSE; | |
6e33951e | 1871 | matched = TRUE; |
ffd65175 AM |
1872 | tmp_sec = sec; |
1873 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
4f3fedcf | 1874 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 1875 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
1876 | return FALSE; |
1877 | ||
1878 | if (skip) | |
1879 | goto nondefault; | |
1880 | ||
5b677558 AM |
1881 | if (hi->def_regular) |
1882 | { | |
1883 | /* If the undecorated symbol will have a version added by a | |
1884 | script different to H, then don't indirect to/from the | |
1885 | undecorated symbol. This isn't ideal because we may not yet | |
1886 | have seen symbol versions, if given by a script on the | |
1887 | command line rather than via --version-script. */ | |
1888 | if (hi->verinfo.vertree == NULL && info->version_info != NULL) | |
1889 | { | |
1890 | bfd_boolean hide; | |
1891 | ||
1892 | hi->verinfo.vertree | |
1893 | = bfd_find_version_for_sym (info->version_info, | |
1894 | hi->root.root.string, &hide); | |
1895 | if (hi->verinfo.vertree != NULL && hide) | |
1896 | { | |
1897 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
1898 | goto nondefault; | |
1899 | } | |
1900 | } | |
1901 | if (hi->verinfo.vertree != NULL | |
1902 | && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0) | |
1903 | goto nondefault; | |
1904 | } | |
1905 | ||
45d6a902 AM |
1906 | if (! override) |
1907 | { | |
c6e8a9a8 | 1908 | /* Add the default symbol if not performing a relocatable link. */ |
0e1862bb | 1909 | if (! bfd_link_relocatable (info)) |
c6e8a9a8 L |
1910 | { |
1911 | bh = &hi->root; | |
1912 | if (! (_bfd_generic_link_add_one_symbol | |
1913 | (info, abfd, shortname, BSF_INDIRECT, | |
1914 | bfd_ind_section_ptr, | |
1915 | 0, name, FALSE, collect, &bh))) | |
1916 | return FALSE; | |
1917 | hi = (struct elf_link_hash_entry *) bh; | |
1918 | } | |
45d6a902 AM |
1919 | } |
1920 | else | |
1921 | { | |
1922 | /* In this case the symbol named SHORTNAME is overriding the | |
1923 | indirect symbol we want to add. We were planning on making | |
1924 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1925 | is the name without a version. NAME is the fully versioned | |
1926 | name, and it is the default version. | |
1927 | ||
1928 | Overriding means that we already saw a definition for the | |
1929 | symbol SHORTNAME in a regular object, and it is overriding | |
1930 | the symbol defined in the dynamic object. | |
1931 | ||
1932 | When this happens, we actually want to change NAME, the | |
1933 | symbol we just added, to refer to SHORTNAME. This will cause | |
1934 | references to NAME in the shared object to become references | |
1935 | to SHORTNAME in the regular object. This is what we expect | |
1936 | when we override a function in a shared object: that the | |
1937 | references in the shared object will be mapped to the | |
1938 | definition in the regular object. */ | |
1939 | ||
1940 | while (hi->root.type == bfd_link_hash_indirect | |
1941 | || hi->root.type == bfd_link_hash_warning) | |
1942 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1943 | ||
1944 | h->root.type = bfd_link_hash_indirect; | |
1945 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1946 | if (h->def_dynamic) |
45d6a902 | 1947 | { |
f5385ebf AM |
1948 | h->def_dynamic = 0; |
1949 | hi->ref_dynamic = 1; | |
1950 | if (hi->ref_regular | |
1951 | || hi->def_regular) | |
45d6a902 | 1952 | { |
c152c796 | 1953 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1954 | return FALSE; |
1955 | } | |
1956 | } | |
1957 | ||
1958 | /* Now set HI to H, so that the following code will set the | |
1959 | other fields correctly. */ | |
1960 | hi = h; | |
1961 | } | |
1962 | ||
fab4a87f L |
1963 | /* Check if HI is a warning symbol. */ |
1964 | if (hi->root.type == bfd_link_hash_warning) | |
1965 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1966 | ||
45d6a902 AM |
1967 | /* If there is a duplicate definition somewhere, then HI may not |
1968 | point to an indirect symbol. We will have reported an error to | |
1969 | the user in that case. */ | |
1970 | ||
1971 | if (hi->root.type == bfd_link_hash_indirect) | |
1972 | { | |
1973 | struct elf_link_hash_entry *ht; | |
1974 | ||
45d6a902 | 1975 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 1976 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 | 1977 | |
68c88cd4 AM |
1978 | /* A reference to the SHORTNAME symbol from a dynamic library |
1979 | will be satisfied by the versioned symbol at runtime. In | |
1980 | effect, we have a reference to the versioned symbol. */ | |
1981 | ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; | |
1982 | hi->dynamic_def |= ht->dynamic_def; | |
1983 | ||
45d6a902 AM |
1984 | /* See if the new flags lead us to realize that the symbol must |
1985 | be dynamic. */ | |
1986 | if (! *dynsym) | |
1987 | { | |
1988 | if (! dynamic) | |
1989 | { | |
0e1862bb | 1990 | if (! bfd_link_executable (info) |
90c984fc | 1991 | || hi->def_dynamic |
f5385ebf | 1992 | || hi->ref_dynamic) |
45d6a902 AM |
1993 | *dynsym = TRUE; |
1994 | } | |
1995 | else | |
1996 | { | |
f5385ebf | 1997 | if (hi->ref_regular) |
45d6a902 AM |
1998 | *dynsym = TRUE; |
1999 | } | |
2000 | } | |
2001 | } | |
2002 | ||
2003 | /* We also need to define an indirection from the nondefault version | |
2004 | of the symbol. */ | |
2005 | ||
2006 | nondefault: | |
2007 | len = strlen (name); | |
a50b1753 | 2008 | shortname = (char *) bfd_hash_allocate (&info->hash->table, len); |
45d6a902 AM |
2009 | if (shortname == NULL) |
2010 | return FALSE; | |
2011 | memcpy (shortname, name, shortlen); | |
2012 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
2013 | ||
2014 | /* Once again, merge with any existing symbol. */ | |
2015 | type_change_ok = FALSE; | |
2016 | size_change_ok = FALSE; | |
ffd65175 AM |
2017 | tmp_sec = sec; |
2018 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
115c6d5c | 2019 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 2020 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
2021 | return FALSE; |
2022 | ||
2023 | if (skip) | |
2024 | return TRUE; | |
2025 | ||
2026 | if (override) | |
2027 | { | |
2028 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
2029 | the type of override we do in the case above unless it is | |
4cc11e76 | 2030 | overridden by a versioned definition. */ |
45d6a902 AM |
2031 | if (hi->root.type != bfd_link_hash_defined |
2032 | && hi->root.type != bfd_link_hash_defweak) | |
4eca0228 | 2033 | _bfd_error_handler |
695344c0 | 2034 | /* xgettext:c-format */ |
d003868e AM |
2035 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
2036 | abfd, shortname); | |
45d6a902 AM |
2037 | } |
2038 | else | |
2039 | { | |
2040 | bh = &hi->root; | |
2041 | if (! (_bfd_generic_link_add_one_symbol | |
2042 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 2043 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
2044 | return FALSE; |
2045 | hi = (struct elf_link_hash_entry *) bh; | |
2046 | ||
2047 | /* If there is a duplicate definition somewhere, then HI may not | |
2048 | point to an indirect symbol. We will have reported an error | |
2049 | to the user in that case. */ | |
2050 | ||
2051 | if (hi->root.type == bfd_link_hash_indirect) | |
2052 | { | |
fcfa13d2 | 2053 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
68c88cd4 AM |
2054 | h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; |
2055 | hi->dynamic_def |= h->dynamic_def; | |
45d6a902 AM |
2056 | |
2057 | /* See if the new flags lead us to realize that the symbol | |
2058 | must be dynamic. */ | |
2059 | if (! *dynsym) | |
2060 | { | |
2061 | if (! dynamic) | |
2062 | { | |
0e1862bb | 2063 | if (! bfd_link_executable (info) |
f5385ebf | 2064 | || hi->ref_dynamic) |
45d6a902 AM |
2065 | *dynsym = TRUE; |
2066 | } | |
2067 | else | |
2068 | { | |
f5385ebf | 2069 | if (hi->ref_regular) |
45d6a902 AM |
2070 | *dynsym = TRUE; |
2071 | } | |
2072 | } | |
2073 | } | |
2074 | } | |
2075 | ||
2076 | return TRUE; | |
2077 | } | |
2078 | \f | |
2079 | /* This routine is used to export all defined symbols into the dynamic | |
2080 | symbol table. It is called via elf_link_hash_traverse. */ | |
2081 | ||
28caa186 | 2082 | static bfd_boolean |
268b6b39 | 2083 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2084 | { |
a50b1753 | 2085 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 AM |
2086 | |
2087 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2088 | if (h->root.type == bfd_link_hash_indirect) | |
2089 | return TRUE; | |
2090 | ||
7686d77d AM |
2091 | /* Ignore this if we won't export it. */ |
2092 | if (!eif->info->export_dynamic && !h->dynamic) | |
2093 | return TRUE; | |
45d6a902 AM |
2094 | |
2095 | if (h->dynindx == -1 | |
fd91d419 L |
2096 | && (h->def_regular || h->ref_regular) |
2097 | && ! bfd_hide_sym_by_version (eif->info->version_info, | |
2098 | h->root.root.string)) | |
45d6a902 | 2099 | { |
fd91d419 | 2100 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 | 2101 | { |
fd91d419 L |
2102 | eif->failed = TRUE; |
2103 | return FALSE; | |
45d6a902 AM |
2104 | } |
2105 | } | |
2106 | ||
2107 | return TRUE; | |
2108 | } | |
2109 | \f | |
2110 | /* Look through the symbols which are defined in other shared | |
2111 | libraries and referenced here. Update the list of version | |
2112 | dependencies. This will be put into the .gnu.version_r section. | |
2113 | This function is called via elf_link_hash_traverse. */ | |
2114 | ||
28caa186 | 2115 | static bfd_boolean |
268b6b39 AM |
2116 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
2117 | void *data) | |
45d6a902 | 2118 | { |
a50b1753 | 2119 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; |
45d6a902 AM |
2120 | Elf_Internal_Verneed *t; |
2121 | Elf_Internal_Vernaux *a; | |
2122 | bfd_size_type amt; | |
2123 | ||
45d6a902 AM |
2124 | /* We only care about symbols defined in shared objects with version |
2125 | information. */ | |
f5385ebf AM |
2126 | if (!h->def_dynamic |
2127 | || h->def_regular | |
45d6a902 | 2128 | || h->dynindx == -1 |
7b20f099 AM |
2129 | || h->verinfo.verdef == NULL |
2130 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
2131 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
45d6a902 AM |
2132 | return TRUE; |
2133 | ||
2134 | /* See if we already know about this version. */ | |
28caa186 AM |
2135 | for (t = elf_tdata (rinfo->info->output_bfd)->verref; |
2136 | t != NULL; | |
2137 | t = t->vn_nextref) | |
45d6a902 AM |
2138 | { |
2139 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
2140 | continue; | |
2141 | ||
2142 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2143 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
2144 | return TRUE; | |
2145 | ||
2146 | break; | |
2147 | } | |
2148 | ||
2149 | /* This is a new version. Add it to tree we are building. */ | |
2150 | ||
2151 | if (t == NULL) | |
2152 | { | |
2153 | amt = sizeof *t; | |
a50b1753 | 2154 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); |
45d6a902 AM |
2155 | if (t == NULL) |
2156 | { | |
2157 | rinfo->failed = TRUE; | |
2158 | return FALSE; | |
2159 | } | |
2160 | ||
2161 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
28caa186 AM |
2162 | t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; |
2163 | elf_tdata (rinfo->info->output_bfd)->verref = t; | |
45d6a902 AM |
2164 | } |
2165 | ||
2166 | amt = sizeof *a; | |
a50b1753 | 2167 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); |
14b1c01e AM |
2168 | if (a == NULL) |
2169 | { | |
2170 | rinfo->failed = TRUE; | |
2171 | return FALSE; | |
2172 | } | |
45d6a902 AM |
2173 | |
2174 | /* Note that we are copying a string pointer here, and testing it | |
2175 | above. If bfd_elf_string_from_elf_section is ever changed to | |
2176 | discard the string data when low in memory, this will have to be | |
2177 | fixed. */ | |
2178 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
2179 | ||
2180 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
2181 | a->vna_nextptr = t->vn_auxptr; | |
2182 | ||
2183 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
2184 | ++rinfo->vers; | |
2185 | ||
2186 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
2187 | ||
2188 | t->vn_auxptr = a; | |
2189 | ||
2190 | return TRUE; | |
2191 | } | |
2192 | ||
2193 | /* Figure out appropriate versions for all the symbols. We may not | |
2194 | have the version number script until we have read all of the input | |
2195 | files, so until that point we don't know which symbols should be | |
2196 | local. This function is called via elf_link_hash_traverse. */ | |
2197 | ||
28caa186 | 2198 | static bfd_boolean |
268b6b39 | 2199 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2200 | { |
28caa186 | 2201 | struct elf_info_failed *sinfo; |
45d6a902 | 2202 | struct bfd_link_info *info; |
9c5bfbb7 | 2203 | const struct elf_backend_data *bed; |
45d6a902 AM |
2204 | struct elf_info_failed eif; |
2205 | char *p; | |
45d6a902 | 2206 | |
a50b1753 | 2207 | sinfo = (struct elf_info_failed *) data; |
45d6a902 AM |
2208 | info = sinfo->info; |
2209 | ||
45d6a902 AM |
2210 | /* Fix the symbol flags. */ |
2211 | eif.failed = FALSE; | |
2212 | eif.info = info; | |
2213 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
2214 | { | |
2215 | if (eif.failed) | |
2216 | sinfo->failed = TRUE; | |
2217 | return FALSE; | |
2218 | } | |
2219 | ||
2220 | /* We only need version numbers for symbols defined in regular | |
2221 | objects. */ | |
f5385ebf | 2222 | if (!h->def_regular) |
45d6a902 AM |
2223 | return TRUE; |
2224 | ||
28caa186 | 2225 | bed = get_elf_backend_data (info->output_bfd); |
45d6a902 AM |
2226 | p = strchr (h->root.root.string, ELF_VER_CHR); |
2227 | if (p != NULL && h->verinfo.vertree == NULL) | |
2228 | { | |
2229 | struct bfd_elf_version_tree *t; | |
45d6a902 | 2230 | |
45d6a902 AM |
2231 | ++p; |
2232 | if (*p == ELF_VER_CHR) | |
6e33951e | 2233 | ++p; |
45d6a902 AM |
2234 | |
2235 | /* If there is no version string, we can just return out. */ | |
2236 | if (*p == '\0') | |
6e33951e | 2237 | return TRUE; |
45d6a902 AM |
2238 | |
2239 | /* Look for the version. If we find it, it is no longer weak. */ | |
fd91d419 | 2240 | for (t = sinfo->info->version_info; t != NULL; t = t->next) |
45d6a902 AM |
2241 | { |
2242 | if (strcmp (t->name, p) == 0) | |
2243 | { | |
2244 | size_t len; | |
2245 | char *alc; | |
2246 | struct bfd_elf_version_expr *d; | |
2247 | ||
2248 | len = p - h->root.root.string; | |
a50b1753 | 2249 | alc = (char *) bfd_malloc (len); |
45d6a902 | 2250 | if (alc == NULL) |
14b1c01e AM |
2251 | { |
2252 | sinfo->failed = TRUE; | |
2253 | return FALSE; | |
2254 | } | |
45d6a902 AM |
2255 | memcpy (alc, h->root.root.string, len - 1); |
2256 | alc[len - 1] = '\0'; | |
2257 | if (alc[len - 2] == ELF_VER_CHR) | |
2258 | alc[len - 2] = '\0'; | |
2259 | ||
2260 | h->verinfo.vertree = t; | |
2261 | t->used = TRUE; | |
2262 | d = NULL; | |
2263 | ||
108ba305 JJ |
2264 | if (t->globals.list != NULL) |
2265 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
2266 | |
2267 | /* See if there is anything to force this symbol to | |
2268 | local scope. */ | |
108ba305 | 2269 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 2270 | { |
108ba305 JJ |
2271 | d = (*t->match) (&t->locals, NULL, alc); |
2272 | if (d != NULL | |
2273 | && h->dynindx != -1 | |
108ba305 JJ |
2274 | && ! info->export_dynamic) |
2275 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2276 | } |
2277 | ||
2278 | free (alc); | |
2279 | break; | |
2280 | } | |
2281 | } | |
2282 | ||
2283 | /* If we are building an application, we need to create a | |
2284 | version node for this version. */ | |
0e1862bb | 2285 | if (t == NULL && bfd_link_executable (info)) |
45d6a902 AM |
2286 | { |
2287 | struct bfd_elf_version_tree **pp; | |
2288 | int version_index; | |
2289 | ||
2290 | /* If we aren't going to export this symbol, we don't need | |
2291 | to worry about it. */ | |
2292 | if (h->dynindx == -1) | |
2293 | return TRUE; | |
2294 | ||
ef53be89 AM |
2295 | t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, |
2296 | sizeof *t); | |
45d6a902 AM |
2297 | if (t == NULL) |
2298 | { | |
2299 | sinfo->failed = TRUE; | |
2300 | return FALSE; | |
2301 | } | |
2302 | ||
45d6a902 | 2303 | t->name = p; |
45d6a902 AM |
2304 | t->name_indx = (unsigned int) -1; |
2305 | t->used = TRUE; | |
2306 | ||
2307 | version_index = 1; | |
2308 | /* Don't count anonymous version tag. */ | |
fd91d419 L |
2309 | if (sinfo->info->version_info != NULL |
2310 | && sinfo->info->version_info->vernum == 0) | |
45d6a902 | 2311 | version_index = 0; |
fd91d419 L |
2312 | for (pp = &sinfo->info->version_info; |
2313 | *pp != NULL; | |
2314 | pp = &(*pp)->next) | |
45d6a902 AM |
2315 | ++version_index; |
2316 | t->vernum = version_index; | |
2317 | ||
2318 | *pp = t; | |
2319 | ||
2320 | h->verinfo.vertree = t; | |
2321 | } | |
2322 | else if (t == NULL) | |
2323 | { | |
2324 | /* We could not find the version for a symbol when | |
2325 | generating a shared archive. Return an error. */ | |
4eca0228 | 2326 | _bfd_error_handler |
695344c0 | 2327 | /* xgettext:c-format */ |
c55fe096 | 2328 | (_("%B: version node not found for symbol %s"), |
28caa186 | 2329 | info->output_bfd, h->root.root.string); |
45d6a902 AM |
2330 | bfd_set_error (bfd_error_bad_value); |
2331 | sinfo->failed = TRUE; | |
2332 | return FALSE; | |
2333 | } | |
45d6a902 AM |
2334 | } |
2335 | ||
2336 | /* If we don't have a version for this symbol, see if we can find | |
2337 | something. */ | |
fd91d419 | 2338 | if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL) |
45d6a902 | 2339 | { |
1e8fa21e | 2340 | bfd_boolean hide; |
ae5a3597 | 2341 | |
fd91d419 L |
2342 | h->verinfo.vertree |
2343 | = bfd_find_version_for_sym (sinfo->info->version_info, | |
2344 | h->root.root.string, &hide); | |
1e8fa21e AM |
2345 | if (h->verinfo.vertree != NULL && hide) |
2346 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2347 | } |
2348 | ||
2349 | return TRUE; | |
2350 | } | |
2351 | \f | |
45d6a902 AM |
2352 | /* Read and swap the relocs from the section indicated by SHDR. This |
2353 | may be either a REL or a RELA section. The relocations are | |
2354 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2355 | which should have already been allocated to contain enough space. | |
2356 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2357 | relocations should be stored. | |
2358 | ||
2359 | Returns FALSE if something goes wrong. */ | |
2360 | ||
2361 | static bfd_boolean | |
268b6b39 | 2362 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2363 | asection *sec, |
268b6b39 AM |
2364 | Elf_Internal_Shdr *shdr, |
2365 | void *external_relocs, | |
2366 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2367 | { |
9c5bfbb7 | 2368 | const struct elf_backend_data *bed; |
268b6b39 | 2369 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2370 | const bfd_byte *erela; |
2371 | const bfd_byte *erelaend; | |
2372 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2373 | Elf_Internal_Shdr *symtab_hdr; |
2374 | size_t nsyms; | |
45d6a902 | 2375 | |
45d6a902 AM |
2376 | /* Position ourselves at the start of the section. */ |
2377 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2378 | return FALSE; | |
2379 | ||
2380 | /* Read the relocations. */ | |
2381 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2382 | return FALSE; | |
2383 | ||
243ef1e0 | 2384 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
ce98a316 | 2385 | nsyms = NUM_SHDR_ENTRIES (symtab_hdr); |
243ef1e0 | 2386 | |
45d6a902 AM |
2387 | bed = get_elf_backend_data (abfd); |
2388 | ||
2389 | /* Convert the external relocations to the internal format. */ | |
2390 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2391 | swap_in = bed->s->swap_reloc_in; | |
2392 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2393 | swap_in = bed->s->swap_reloca_in; | |
2394 | else | |
2395 | { | |
2396 | bfd_set_error (bfd_error_wrong_format); | |
2397 | return FALSE; | |
2398 | } | |
2399 | ||
a50b1753 | 2400 | erela = (const bfd_byte *) external_relocs; |
51992aec | 2401 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2402 | irela = internal_relocs; |
2403 | while (erela < erelaend) | |
2404 | { | |
243ef1e0 L |
2405 | bfd_vma r_symndx; |
2406 | ||
45d6a902 | 2407 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2408 | r_symndx = ELF32_R_SYM (irela->r_info); |
2409 | if (bed->s->arch_size == 64) | |
2410 | r_symndx >>= 24; | |
ce98a316 NC |
2411 | if (nsyms > 0) |
2412 | { | |
2413 | if ((size_t) r_symndx >= nsyms) | |
2414 | { | |
4eca0228 | 2415 | _bfd_error_handler |
695344c0 | 2416 | /* xgettext:c-format */ |
d42c267e | 2417 | (_("%B: bad reloc symbol index (%#Lx >= %#lx)" |
76cfced5 | 2418 | " for offset %#Lx in section `%A'"), |
d42c267e | 2419 | abfd, r_symndx, (unsigned long) nsyms, |
c08bb8dd | 2420 | irela->r_offset, sec); |
ce98a316 NC |
2421 | bfd_set_error (bfd_error_bad_value); |
2422 | return FALSE; | |
2423 | } | |
2424 | } | |
cf35638d | 2425 | else if (r_symndx != STN_UNDEF) |
243ef1e0 | 2426 | { |
4eca0228 | 2427 | _bfd_error_handler |
695344c0 | 2428 | /* xgettext:c-format */ |
d42c267e | 2429 | (_("%B: non-zero symbol index (%#Lx)" |
76cfced5 | 2430 | " for offset %#Lx in section `%A'" |
ce98a316 | 2431 | " when the object file has no symbol table"), |
d42c267e | 2432 | abfd, r_symndx, |
c08bb8dd | 2433 | irela->r_offset, sec); |
243ef1e0 L |
2434 | bfd_set_error (bfd_error_bad_value); |
2435 | return FALSE; | |
2436 | } | |
45d6a902 AM |
2437 | irela += bed->s->int_rels_per_ext_rel; |
2438 | erela += shdr->sh_entsize; | |
2439 | } | |
2440 | ||
2441 | return TRUE; | |
2442 | } | |
2443 | ||
2444 | /* Read and swap the relocs for a section O. They may have been | |
2445 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2446 | not NULL, they are used as buffers to read into. They are known to | |
2447 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2448 | the return value is allocated using either malloc or bfd_alloc, | |
2449 | according to the KEEP_MEMORY argument. If O has two relocation | |
2450 | sections (both REL and RELA relocations), then the REL_HDR | |
2451 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
d4730f92 | 2452 | RELA_HDR relocations. */ |
45d6a902 AM |
2453 | |
2454 | Elf_Internal_Rela * | |
268b6b39 AM |
2455 | _bfd_elf_link_read_relocs (bfd *abfd, |
2456 | asection *o, | |
2457 | void *external_relocs, | |
2458 | Elf_Internal_Rela *internal_relocs, | |
2459 | bfd_boolean keep_memory) | |
45d6a902 | 2460 | { |
268b6b39 | 2461 | void *alloc1 = NULL; |
45d6a902 | 2462 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2463 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2464 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
2465 | Elf_Internal_Rela *internal_rela_relocs; | |
45d6a902 | 2466 | |
d4730f92 BS |
2467 | if (esdo->relocs != NULL) |
2468 | return esdo->relocs; | |
45d6a902 AM |
2469 | |
2470 | if (o->reloc_count == 0) | |
2471 | return NULL; | |
2472 | ||
45d6a902 AM |
2473 | if (internal_relocs == NULL) |
2474 | { | |
2475 | bfd_size_type size; | |
2476 | ||
056bafd4 | 2477 | size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela); |
45d6a902 | 2478 | if (keep_memory) |
a50b1753 | 2479 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); |
45d6a902 | 2480 | else |
a50b1753 | 2481 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
45d6a902 AM |
2482 | if (internal_relocs == NULL) |
2483 | goto error_return; | |
2484 | } | |
2485 | ||
2486 | if (external_relocs == NULL) | |
2487 | { | |
d4730f92 BS |
2488 | bfd_size_type size = 0; |
2489 | ||
2490 | if (esdo->rel.hdr) | |
2491 | size += esdo->rel.hdr->sh_size; | |
2492 | if (esdo->rela.hdr) | |
2493 | size += esdo->rela.hdr->sh_size; | |
45d6a902 | 2494 | |
268b6b39 | 2495 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2496 | if (alloc1 == NULL) |
2497 | goto error_return; | |
2498 | external_relocs = alloc1; | |
2499 | } | |
2500 | ||
d4730f92 BS |
2501 | internal_rela_relocs = internal_relocs; |
2502 | if (esdo->rel.hdr) | |
2503 | { | |
2504 | if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, | |
2505 | external_relocs, | |
2506 | internal_relocs)) | |
2507 | goto error_return; | |
2508 | external_relocs = (((bfd_byte *) external_relocs) | |
2509 | + esdo->rel.hdr->sh_size); | |
2510 | internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) | |
2511 | * bed->s->int_rels_per_ext_rel); | |
2512 | } | |
2513 | ||
2514 | if (esdo->rela.hdr | |
2515 | && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, | |
2516 | external_relocs, | |
2517 | internal_rela_relocs))) | |
45d6a902 AM |
2518 | goto error_return; |
2519 | ||
2520 | /* Cache the results for next time, if we can. */ | |
2521 | if (keep_memory) | |
d4730f92 | 2522 | esdo->relocs = internal_relocs; |
45d6a902 AM |
2523 | |
2524 | if (alloc1 != NULL) | |
2525 | free (alloc1); | |
2526 | ||
2527 | /* Don't free alloc2, since if it was allocated we are passing it | |
2528 | back (under the name of internal_relocs). */ | |
2529 | ||
2530 | return internal_relocs; | |
2531 | ||
2532 | error_return: | |
2533 | if (alloc1 != NULL) | |
2534 | free (alloc1); | |
2535 | if (alloc2 != NULL) | |
4dd07732 AM |
2536 | { |
2537 | if (keep_memory) | |
2538 | bfd_release (abfd, alloc2); | |
2539 | else | |
2540 | free (alloc2); | |
2541 | } | |
45d6a902 AM |
2542 | return NULL; |
2543 | } | |
2544 | ||
2545 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2546 | section header for a section containing relocations for O. */ | |
2547 | ||
28caa186 | 2548 | static bfd_boolean |
9eaff861 AO |
2549 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2550 | struct bfd_elf_section_reloc_data *reldata) | |
45d6a902 | 2551 | { |
9eaff861 | 2552 | Elf_Internal_Shdr *rel_hdr = reldata->hdr; |
45d6a902 AM |
2553 | |
2554 | /* That allows us to calculate the size of the section. */ | |
9eaff861 | 2555 | rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; |
45d6a902 AM |
2556 | |
2557 | /* The contents field must last into write_object_contents, so we | |
2558 | allocate it with bfd_alloc rather than malloc. Also since we | |
2559 | cannot be sure that the contents will actually be filled in, | |
2560 | we zero the allocated space. */ | |
a50b1753 | 2561 | rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2562 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2563 | return FALSE; | |
2564 | ||
d4730f92 | 2565 | if (reldata->hashes == NULL && reldata->count) |
45d6a902 AM |
2566 | { |
2567 | struct elf_link_hash_entry **p; | |
2568 | ||
ca4be51c AM |
2569 | p = ((struct elf_link_hash_entry **) |
2570 | bfd_zmalloc (reldata->count * sizeof (*p))); | |
45d6a902 AM |
2571 | if (p == NULL) |
2572 | return FALSE; | |
2573 | ||
d4730f92 | 2574 | reldata->hashes = p; |
45d6a902 AM |
2575 | } |
2576 | ||
2577 | return TRUE; | |
2578 | } | |
2579 | ||
2580 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2581 | originated from the section given by INPUT_REL_HDR) to the | |
2582 | OUTPUT_BFD. */ | |
2583 | ||
2584 | bfd_boolean | |
268b6b39 AM |
2585 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2586 | asection *input_section, | |
2587 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2588 | Elf_Internal_Rela *internal_relocs, |
2589 | struct elf_link_hash_entry **rel_hash | |
2590 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2591 | { |
2592 | Elf_Internal_Rela *irela; | |
2593 | Elf_Internal_Rela *irelaend; | |
2594 | bfd_byte *erel; | |
d4730f92 | 2595 | struct bfd_elf_section_reloc_data *output_reldata; |
45d6a902 | 2596 | asection *output_section; |
9c5bfbb7 | 2597 | const struct elf_backend_data *bed; |
268b6b39 | 2598 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
d4730f92 | 2599 | struct bfd_elf_section_data *esdo; |
45d6a902 AM |
2600 | |
2601 | output_section = input_section->output_section; | |
45d6a902 | 2602 | |
d4730f92 BS |
2603 | bed = get_elf_backend_data (output_bfd); |
2604 | esdo = elf_section_data (output_section); | |
2605 | if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2606 | { |
d4730f92 BS |
2607 | output_reldata = &esdo->rel; |
2608 | swap_out = bed->s->swap_reloc_out; | |
45d6a902 | 2609 | } |
d4730f92 BS |
2610 | else if (esdo->rela.hdr |
2611 | && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2612 | { |
d4730f92 BS |
2613 | output_reldata = &esdo->rela; |
2614 | swap_out = bed->s->swap_reloca_out; | |
45d6a902 AM |
2615 | } |
2616 | else | |
2617 | { | |
4eca0228 | 2618 | _bfd_error_handler |
695344c0 | 2619 | /* xgettext:c-format */ |
d003868e AM |
2620 | (_("%B: relocation size mismatch in %B section %A"), |
2621 | output_bfd, input_section->owner, input_section); | |
297d8443 | 2622 | bfd_set_error (bfd_error_wrong_format); |
45d6a902 AM |
2623 | return FALSE; |
2624 | } | |
2625 | ||
d4730f92 BS |
2626 | erel = output_reldata->hdr->contents; |
2627 | erel += output_reldata->count * input_rel_hdr->sh_entsize; | |
45d6a902 AM |
2628 | irela = internal_relocs; |
2629 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2630 | * bed->s->int_rels_per_ext_rel); | |
2631 | while (irela < irelaend) | |
2632 | { | |
2633 | (*swap_out) (output_bfd, irela, erel); | |
2634 | irela += bed->s->int_rels_per_ext_rel; | |
2635 | erel += input_rel_hdr->sh_entsize; | |
2636 | } | |
2637 | ||
2638 | /* Bump the counter, so that we know where to add the next set of | |
2639 | relocations. */ | |
d4730f92 | 2640 | output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); |
45d6a902 AM |
2641 | |
2642 | return TRUE; | |
2643 | } | |
2644 | \f | |
508c3946 L |
2645 | /* Make weak undefined symbols in PIE dynamic. */ |
2646 | ||
2647 | bfd_boolean | |
2648 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2649 | struct elf_link_hash_entry *h) | |
2650 | { | |
0e1862bb | 2651 | if (bfd_link_pie (info) |
508c3946 L |
2652 | && h->dynindx == -1 |
2653 | && h->root.type == bfd_link_hash_undefweak) | |
2654 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2655 | ||
2656 | return TRUE; | |
2657 | } | |
2658 | ||
45d6a902 AM |
2659 | /* Fix up the flags for a symbol. This handles various cases which |
2660 | can only be fixed after all the input files are seen. This is | |
2661 | currently called by both adjust_dynamic_symbol and | |
2662 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2663 | the face of future changes. */ | |
2664 | ||
28caa186 | 2665 | static bfd_boolean |
268b6b39 AM |
2666 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2667 | struct elf_info_failed *eif) | |
45d6a902 | 2668 | { |
33774f08 | 2669 | const struct elf_backend_data *bed; |
508c3946 | 2670 | |
45d6a902 AM |
2671 | /* If this symbol was mentioned in a non-ELF file, try to set |
2672 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2673 | permit a non-ELF file to correctly refer to a symbol defined in | |
2674 | an ELF dynamic object. */ | |
f5385ebf | 2675 | if (h->non_elf) |
45d6a902 AM |
2676 | { |
2677 | while (h->root.type == bfd_link_hash_indirect) | |
2678 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2679 | ||
2680 | if (h->root.type != bfd_link_hash_defined | |
2681 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2682 | { |
2683 | h->ref_regular = 1; | |
2684 | h->ref_regular_nonweak = 1; | |
2685 | } | |
45d6a902 AM |
2686 | else |
2687 | { | |
2688 | if (h->root.u.def.section->owner != NULL | |
2689 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2690 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2691 | { |
2692 | h->ref_regular = 1; | |
2693 | h->ref_regular_nonweak = 1; | |
2694 | } | |
45d6a902 | 2695 | else |
f5385ebf | 2696 | h->def_regular = 1; |
45d6a902 AM |
2697 | } |
2698 | ||
2699 | if (h->dynindx == -1 | |
f5385ebf AM |
2700 | && (h->def_dynamic |
2701 | || h->ref_dynamic)) | |
45d6a902 | 2702 | { |
c152c796 | 2703 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2704 | { |
2705 | eif->failed = TRUE; | |
2706 | return FALSE; | |
2707 | } | |
2708 | } | |
2709 | } | |
2710 | else | |
2711 | { | |
f5385ebf | 2712 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2713 | was first seen in a non-ELF file. Fortunately, if the symbol |
2714 | was first seen in an ELF file, we're probably OK unless the | |
2715 | symbol was defined in a non-ELF file. Catch that case here. | |
2716 | FIXME: We're still in trouble if the symbol was first seen in | |
2717 | a dynamic object, and then later in a non-ELF regular object. */ | |
2718 | if ((h->root.type == bfd_link_hash_defined | |
2719 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2720 | && !h->def_regular |
45d6a902 AM |
2721 | && (h->root.u.def.section->owner != NULL |
2722 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2723 | != bfd_target_elf_flavour) | |
2724 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2725 | && !h->def_dynamic))) |
2726 | h->def_regular = 1; | |
45d6a902 AM |
2727 | } |
2728 | ||
508c3946 | 2729 | /* Backend specific symbol fixup. */ |
33774f08 AM |
2730 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2731 | if (bed->elf_backend_fixup_symbol | |
2732 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2733 | return FALSE; | |
508c3946 | 2734 | |
45d6a902 AM |
2735 | /* If this is a final link, and the symbol was defined as a common |
2736 | symbol in a regular object file, and there was no definition in | |
2737 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2738 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2739 | flag will not have been set. */ |
2740 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2741 | && !h->def_regular |
2742 | && h->ref_regular | |
2743 | && !h->def_dynamic | |
96f29d96 | 2744 | && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) |
f5385ebf | 2745 | h->def_regular = 1; |
45d6a902 | 2746 | |
4deb8f71 L |
2747 | /* If a weak undefined symbol has non-default visibility, we also |
2748 | hide it from the dynamic linker. */ | |
2749 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT | |
2750 | && h->root.type == bfd_link_hash_undefweak) | |
2751 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2752 | ||
2753 | /* A hidden versioned symbol in executable should be forced local if | |
2754 | it is is locally defined, not referenced by shared library and not | |
2755 | exported. */ | |
2756 | else if (bfd_link_executable (eif->info) | |
2757 | && h->versioned == versioned_hidden | |
2758 | && !eif->info->export_dynamic | |
2759 | && !h->dynamic | |
2760 | && !h->ref_dynamic | |
2761 | && h->def_regular) | |
2762 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2763 | ||
45d6a902 AM |
2764 | /* If -Bsymbolic was used (which means to bind references to global |
2765 | symbols to the definition within the shared object), and this | |
2766 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2767 | need a PLT entry. Likewise, if the symbol has non-default |
2768 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2769 | will force it local. */ |
4deb8f71 L |
2770 | else if (h->needs_plt |
2771 | && bfd_link_pic (eif->info) | |
2772 | && is_elf_hash_table (eif->info->hash) | |
2773 | && (SYMBOLIC_BIND (eif->info, h) | |
2774 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) | |
2775 | && h->def_regular) | |
45d6a902 | 2776 | { |
45d6a902 AM |
2777 | bfd_boolean force_local; |
2778 | ||
45d6a902 AM |
2779 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2780 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2781 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2782 | } | |
2783 | ||
45d6a902 AM |
2784 | /* If this is a weak defined symbol in a dynamic object, and we know |
2785 | the real definition in the dynamic object, copy interesting flags | |
2786 | over to the real definition. */ | |
60d67dc8 | 2787 | if (h->is_weakalias) |
45d6a902 | 2788 | { |
60d67dc8 AM |
2789 | struct elf_link_hash_entry *def = weakdef (h); |
2790 | ||
45d6a902 AM |
2791 | /* If the real definition is defined by a regular object file, |
2792 | don't do anything special. See the longer description in | |
2793 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
60d67dc8 AM |
2794 | if (def->def_regular) |
2795 | { | |
2796 | h = def; | |
2797 | while ((h = h->u.alias) != def) | |
2798 | h->is_weakalias = 0; | |
2799 | } | |
45d6a902 | 2800 | else |
a26587ba | 2801 | { |
4e6b54a6 AM |
2802 | while (h->root.type == bfd_link_hash_indirect) |
2803 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4e6b54a6 AM |
2804 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
2805 | || h->root.type == bfd_link_hash_defweak); | |
60d67dc8 AM |
2806 | BFD_ASSERT (def->def_dynamic); |
2807 | BFD_ASSERT (def->root.type == bfd_link_hash_defined); | |
2808 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h); | |
a26587ba | 2809 | } |
45d6a902 AM |
2810 | } |
2811 | ||
2812 | return TRUE; | |
2813 | } | |
2814 | ||
2815 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2816 | called via elf_link_hash_traverse, and also calls itself | |
2817 | recursively. */ | |
2818 | ||
28caa186 | 2819 | static bfd_boolean |
268b6b39 | 2820 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2821 | { |
a50b1753 | 2822 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
559192d8 | 2823 | struct elf_link_hash_table *htab; |
9c5bfbb7 | 2824 | const struct elf_backend_data *bed; |
45d6a902 | 2825 | |
0eddce27 | 2826 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2827 | return FALSE; |
2828 | ||
45d6a902 AM |
2829 | /* Ignore indirect symbols. These are added by the versioning code. */ |
2830 | if (h->root.type == bfd_link_hash_indirect) | |
2831 | return TRUE; | |
2832 | ||
2833 | /* Fix the symbol flags. */ | |
2834 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2835 | return FALSE; | |
2836 | ||
559192d8 AM |
2837 | htab = elf_hash_table (eif->info); |
2838 | bed = get_elf_backend_data (htab->dynobj); | |
2839 | ||
954b63d4 AM |
2840 | if (h->root.type == bfd_link_hash_undefweak) |
2841 | { | |
2842 | if (eif->info->dynamic_undefined_weak == 0) | |
559192d8 | 2843 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
954b63d4 AM |
2844 | else if (eif->info->dynamic_undefined_weak > 0 |
2845 | && h->ref_regular | |
2846 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
2847 | && !bfd_hide_sym_by_version (eif->info->version_info, | |
2848 | h->root.root.string)) | |
2849 | { | |
2850 | if (!bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
2851 | { | |
2852 | eif->failed = TRUE; | |
2853 | return FALSE; | |
2854 | } | |
2855 | } | |
2856 | } | |
2857 | ||
45d6a902 AM |
2858 | /* If this symbol does not require a PLT entry, and it is not |
2859 | defined by a dynamic object, or is not referenced by a regular | |
2860 | object, ignore it. We do have to handle a weak defined symbol, | |
2861 | even if no regular object refers to it, if we decided to add it | |
2862 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2863 | about symbols which are defined by one dynamic object and | |
2864 | referenced by another one? */ | |
f5385ebf | 2865 | if (!h->needs_plt |
91e21fb7 | 2866 | && h->type != STT_GNU_IFUNC |
f5385ebf AM |
2867 | && (h->def_regular |
2868 | || !h->def_dynamic | |
2869 | || (!h->ref_regular | |
60d67dc8 | 2870 | && (!h->is_weakalias || weakdef (h)->dynindx == -1)))) |
45d6a902 | 2871 | { |
a6aa5195 | 2872 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2873 | return TRUE; |
2874 | } | |
2875 | ||
2876 | /* If we've already adjusted this symbol, don't do it again. This | |
2877 | can happen via a recursive call. */ | |
f5385ebf | 2878 | if (h->dynamic_adjusted) |
45d6a902 AM |
2879 | return TRUE; |
2880 | ||
2881 | /* Don't look at this symbol again. Note that we must set this | |
2882 | after checking the above conditions, because we may look at a | |
2883 | symbol once, decide not to do anything, and then get called | |
2884 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2885 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2886 | |
2887 | /* If this is a weak definition, and we know a real definition, and | |
2888 | the real symbol is not itself defined by a regular object file, | |
2889 | then get a good value for the real definition. We handle the | |
2890 | real symbol first, for the convenience of the backend routine. | |
2891 | ||
2892 | Note that there is a confusing case here. If the real definition | |
2893 | is defined by a regular object file, we don't get the real symbol | |
2894 | from the dynamic object, but we do get the weak symbol. If the | |
2895 | processor backend uses a COPY reloc, then if some routine in the | |
2896 | dynamic object changes the real symbol, we will not see that | |
2897 | change in the corresponding weak symbol. This is the way other | |
2898 | ELF linkers work as well, and seems to be a result of the shared | |
2899 | library model. | |
2900 | ||
2901 | I will clarify this issue. Most SVR4 shared libraries define the | |
2902 | variable _timezone and define timezone as a weak synonym. The | |
2903 | tzset call changes _timezone. If you write | |
2904 | extern int timezone; | |
2905 | int _timezone = 5; | |
2906 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2907 | you might expect that, since timezone is a synonym for _timezone, | |
2908 | the same number will print both times. However, if the processor | |
2909 | backend uses a COPY reloc, then actually timezone will be copied | |
2910 | into your process image, and, since you define _timezone | |
2911 | yourself, _timezone will not. Thus timezone and _timezone will | |
2912 | wind up at different memory locations. The tzset call will set | |
2913 | _timezone, leaving timezone unchanged. */ | |
2914 | ||
60d67dc8 | 2915 | if (h->is_weakalias) |
45d6a902 | 2916 | { |
60d67dc8 AM |
2917 | struct elf_link_hash_entry *def = weakdef (h); |
2918 | ||
ec24dc88 | 2919 | /* If we get to this point, there is an implicit reference to |
60d67dc8 AM |
2920 | the alias by a regular object file via the weak symbol H. */ |
2921 | def->ref_regular = 1; | |
45d6a902 | 2922 | |
ec24dc88 | 2923 | /* Ensure that the backend adjust_dynamic_symbol function sees |
60d67dc8 AM |
2924 | the strong alias before H by recursively calling ourselves. */ |
2925 | if (!_bfd_elf_adjust_dynamic_symbol (def, eif)) | |
45d6a902 AM |
2926 | return FALSE; |
2927 | } | |
2928 | ||
2929 | /* If a symbol has no type and no size and does not require a PLT | |
2930 | entry, then we are probably about to do the wrong thing here: we | |
2931 | are probably going to create a COPY reloc for an empty object. | |
2932 | This case can arise when a shared object is built with assembly | |
2933 | code, and the assembly code fails to set the symbol type. */ | |
2934 | if (h->size == 0 | |
2935 | && h->type == STT_NOTYPE | |
f5385ebf | 2936 | && !h->needs_plt) |
4eca0228 | 2937 | _bfd_error_handler |
45d6a902 AM |
2938 | (_("warning: type and size of dynamic symbol `%s' are not defined"), |
2939 | h->root.root.string); | |
2940 | ||
45d6a902 AM |
2941 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) |
2942 | { | |
2943 | eif->failed = TRUE; | |
2944 | return FALSE; | |
2945 | } | |
2946 | ||
2947 | return TRUE; | |
2948 | } | |
2949 | ||
027297b7 L |
2950 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
2951 | DYNBSS. */ | |
2952 | ||
2953 | bfd_boolean | |
6cabe1ea AM |
2954 | _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, |
2955 | struct elf_link_hash_entry *h, | |
027297b7 L |
2956 | asection *dynbss) |
2957 | { | |
91ac5911 | 2958 | unsigned int power_of_two; |
027297b7 L |
2959 | bfd_vma mask; |
2960 | asection *sec = h->root.u.def.section; | |
2961 | ||
de194d85 | 2962 | /* The section alignment of the definition is the maximum alignment |
91ac5911 L |
2963 | requirement of symbols defined in the section. Since we don't |
2964 | know the symbol alignment requirement, we start with the | |
2965 | maximum alignment and check low bits of the symbol address | |
2966 | for the minimum alignment. */ | |
2967 | power_of_two = bfd_get_section_alignment (sec->owner, sec); | |
2968 | mask = ((bfd_vma) 1 << power_of_two) - 1; | |
2969 | while ((h->root.u.def.value & mask) != 0) | |
2970 | { | |
2971 | mask >>= 1; | |
2972 | --power_of_two; | |
2973 | } | |
027297b7 | 2974 | |
91ac5911 L |
2975 | if (power_of_two > bfd_get_section_alignment (dynbss->owner, |
2976 | dynbss)) | |
027297b7 L |
2977 | { |
2978 | /* Adjust the section alignment if needed. */ | |
2979 | if (! bfd_set_section_alignment (dynbss->owner, dynbss, | |
91ac5911 | 2980 | power_of_two)) |
027297b7 L |
2981 | return FALSE; |
2982 | } | |
2983 | ||
91ac5911 | 2984 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
2985 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
2986 | ||
2987 | /* Define the symbol as being at this point in DYNBSS. */ | |
2988 | h->root.u.def.section = dynbss; | |
2989 | h->root.u.def.value = dynbss->size; | |
2990 | ||
2991 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
2992 | dynbss->size += h->size; | |
2993 | ||
f7483970 L |
2994 | /* No error if extern_protected_data is true. */ |
2995 | if (h->protected_def | |
889c2a67 L |
2996 | && (!info->extern_protected_data |
2997 | || (info->extern_protected_data < 0 | |
2998 | && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) | |
d07a1b05 AM |
2999 | info->callbacks->einfo |
3000 | (_("%P: copy reloc against protected `%T' is dangerous\n"), | |
3001 | h->root.root.string); | |
6cabe1ea | 3002 | |
027297b7 L |
3003 | return TRUE; |
3004 | } | |
3005 | ||
45d6a902 AM |
3006 | /* Adjust all external symbols pointing into SEC_MERGE sections |
3007 | to reflect the object merging within the sections. */ | |
3008 | ||
28caa186 | 3009 | static bfd_boolean |
268b6b39 | 3010 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
3011 | { |
3012 | asection *sec; | |
3013 | ||
45d6a902 AM |
3014 | if ((h->root.type == bfd_link_hash_defined |
3015 | || h->root.type == bfd_link_hash_defweak) | |
3016 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
dbaa2011 | 3017 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
45d6a902 | 3018 | { |
a50b1753 | 3019 | bfd *output_bfd = (bfd *) data; |
45d6a902 AM |
3020 | |
3021 | h->root.u.def.value = | |
3022 | _bfd_merged_section_offset (output_bfd, | |
3023 | &h->root.u.def.section, | |
3024 | elf_section_data (sec)->sec_info, | |
753731ee | 3025 | h->root.u.def.value); |
45d6a902 AM |
3026 | } |
3027 | ||
3028 | return TRUE; | |
3029 | } | |
986a241f RH |
3030 | |
3031 | /* Returns false if the symbol referred to by H should be considered | |
3032 | to resolve local to the current module, and true if it should be | |
3033 | considered to bind dynamically. */ | |
3034 | ||
3035 | bfd_boolean | |
268b6b39 AM |
3036 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
3037 | struct bfd_link_info *info, | |
89a2ee5a | 3038 | bfd_boolean not_local_protected) |
986a241f RH |
3039 | { |
3040 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
3041 | const struct elf_backend_data *bed; |
3042 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
3043 | |
3044 | if (h == NULL) | |
3045 | return FALSE; | |
3046 | ||
3047 | while (h->root.type == bfd_link_hash_indirect | |
3048 | || h->root.type == bfd_link_hash_warning) | |
3049 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3050 | ||
3051 | /* If it was forced local, then clearly it's not dynamic. */ | |
3052 | if (h->dynindx == -1) | |
3053 | return FALSE; | |
f5385ebf | 3054 | if (h->forced_local) |
986a241f RH |
3055 | return FALSE; |
3056 | ||
3057 | /* Identify the cases where name binding rules say that a | |
3058 | visible symbol resolves locally. */ | |
0e1862bb L |
3059 | binding_stays_local_p = (bfd_link_executable (info) |
3060 | || SYMBOLIC_BIND (info, h)); | |
986a241f RH |
3061 | |
3062 | switch (ELF_ST_VISIBILITY (h->other)) | |
3063 | { | |
3064 | case STV_INTERNAL: | |
3065 | case STV_HIDDEN: | |
3066 | return FALSE; | |
3067 | ||
3068 | case STV_PROTECTED: | |
fcb93ecf PB |
3069 | hash_table = elf_hash_table (info); |
3070 | if (!is_elf_hash_table (hash_table)) | |
3071 | return FALSE; | |
3072 | ||
3073 | bed = get_elf_backend_data (hash_table->dynobj); | |
3074 | ||
986a241f RH |
3075 | /* Proper resolution for function pointer equality may require |
3076 | that these symbols perhaps be resolved dynamically, even though | |
3077 | we should be resolving them to the current module. */ | |
89a2ee5a | 3078 | if (!not_local_protected || !bed->is_function_type (h->type)) |
986a241f RH |
3079 | binding_stays_local_p = TRUE; |
3080 | break; | |
3081 | ||
3082 | default: | |
986a241f RH |
3083 | break; |
3084 | } | |
3085 | ||
aa37626c | 3086 | /* If it isn't defined locally, then clearly it's dynamic. */ |
89a2ee5a | 3087 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
aa37626c L |
3088 | return TRUE; |
3089 | ||
986a241f RH |
3090 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
3091 | us that it remains local. */ | |
3092 | return !binding_stays_local_p; | |
3093 | } | |
f6c52c13 AM |
3094 | |
3095 | /* Return true if the symbol referred to by H should be considered | |
3096 | to resolve local to the current module, and false otherwise. Differs | |
3097 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2e76e85a | 3098 | undefined symbols. The two functions are virtually identical except |
0fad2956 MR |
3099 | for the place where dynindx == -1 is tested. If that test is true, |
3100 | _bfd_elf_dynamic_symbol_p will say the symbol is local, while | |
3101 | _bfd_elf_symbol_refs_local_p will say the symbol is local only for | |
3102 | defined symbols. | |
89a2ee5a AM |
3103 | It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as |
3104 | !_bfd_elf_symbol_refs_local_p, except that targets differ in their | |
3105 | treatment of undefined weak symbols. For those that do not make | |
3106 | undefined weak symbols dynamic, both functions may return false. */ | |
f6c52c13 AM |
3107 | |
3108 | bfd_boolean | |
268b6b39 AM |
3109 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
3110 | struct bfd_link_info *info, | |
3111 | bfd_boolean local_protected) | |
f6c52c13 | 3112 | { |
fcb93ecf PB |
3113 | const struct elf_backend_data *bed; |
3114 | struct elf_link_hash_table *hash_table; | |
3115 | ||
f6c52c13 AM |
3116 | /* If it's a local sym, of course we resolve locally. */ |
3117 | if (h == NULL) | |
3118 | return TRUE; | |
3119 | ||
d95edcac L |
3120 | /* STV_HIDDEN or STV_INTERNAL ones must be local. */ |
3121 | if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
3122 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
3123 | return TRUE; | |
3124 | ||
0fad2956 MR |
3125 | /* Forced local symbols resolve locally. */ |
3126 | if (h->forced_local) | |
3127 | return TRUE; | |
3128 | ||
7e2294f9 AO |
3129 | /* Common symbols that become definitions don't get the DEF_REGULAR |
3130 | flag set, so test it first, and don't bail out. */ | |
3131 | if (ELF_COMMON_DEF_P (h)) | |
3132 | /* Do nothing. */; | |
f6c52c13 | 3133 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
3134 | resolve locally. The sym is either undefined or dynamic. */ |
3135 | else if (!h->def_regular) | |
f6c52c13 AM |
3136 | return FALSE; |
3137 | ||
0fad2956 | 3138 | /* Non-dynamic symbols resolve locally. */ |
f6c52c13 AM |
3139 | if (h->dynindx == -1) |
3140 | return TRUE; | |
3141 | ||
3142 | /* At this point, we know the symbol is defined and dynamic. In an | |
3143 | executable it must resolve locally, likewise when building symbolic | |
3144 | shared libraries. */ | |
0e1862bb | 3145 | if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
3146 | return TRUE; |
3147 | ||
3148 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
3149 | with default visibility might not resolve locally. */ | |
3150 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
3151 | return FALSE; | |
3152 | ||
fcb93ecf PB |
3153 | hash_table = elf_hash_table (info); |
3154 | if (!is_elf_hash_table (hash_table)) | |
3155 | return TRUE; | |
3156 | ||
3157 | bed = get_elf_backend_data (hash_table->dynobj); | |
3158 | ||
f7483970 L |
3159 | /* If extern_protected_data is false, STV_PROTECTED non-function |
3160 | symbols are local. */ | |
889c2a67 L |
3161 | if ((!info->extern_protected_data |
3162 | || (info->extern_protected_data < 0 | |
3163 | && !bed->extern_protected_data)) | |
3164 | && !bed->is_function_type (h->type)) | |
1c16dfa5 L |
3165 | return TRUE; |
3166 | ||
f6c52c13 | 3167 | /* Function pointer equality tests may require that STV_PROTECTED |
2676a7d9 AM |
3168 | symbols be treated as dynamic symbols. If the address of a |
3169 | function not defined in an executable is set to that function's | |
3170 | plt entry in the executable, then the address of the function in | |
3171 | a shared library must also be the plt entry in the executable. */ | |
f6c52c13 AM |
3172 | return local_protected; |
3173 | } | |
e1918d23 AM |
3174 | |
3175 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
3176 | aligned. Returns the first TLS output section. */ | |
3177 | ||
3178 | struct bfd_section * | |
3179 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
3180 | { | |
3181 | struct bfd_section *sec, *tls; | |
3182 | unsigned int align = 0; | |
3183 | ||
3184 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
3185 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
3186 | break; | |
3187 | tls = sec; | |
3188 | ||
3189 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
3190 | if (sec->alignment_power > align) | |
3191 | align = sec->alignment_power; | |
3192 | ||
3193 | elf_hash_table (info)->tls_sec = tls; | |
3194 | ||
3195 | /* Ensure the alignment of the first section is the largest alignment, | |
3196 | so that the tls segment starts aligned. */ | |
3197 | if (tls != NULL) | |
3198 | tls->alignment_power = align; | |
3199 | ||
3200 | return tls; | |
3201 | } | |
0ad989f9 L |
3202 | |
3203 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
3204 | static bfd_boolean | |
3205 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
3206 | Elf_Internal_Sym *sym) | |
3207 | { | |
a4d8e49b L |
3208 | const struct elf_backend_data *bed; |
3209 | ||
0ad989f9 L |
3210 | /* Local symbols do not count, but target specific ones might. */ |
3211 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
3212 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
3213 | return FALSE; | |
3214 | ||
fcb93ecf | 3215 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 3216 | /* Function symbols do not count. */ |
fcb93ecf | 3217 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
3218 | return FALSE; |
3219 | ||
3220 | /* If the section is undefined, then so is the symbol. */ | |
3221 | if (sym->st_shndx == SHN_UNDEF) | |
3222 | return FALSE; | |
3223 | ||
3224 | /* If the symbol is defined in the common section, then | |
3225 | it is a common definition and so does not count. */ | |
a4d8e49b | 3226 | if (bed->common_definition (sym)) |
0ad989f9 L |
3227 | return FALSE; |
3228 | ||
3229 | /* If the symbol is in a target specific section then we | |
3230 | must rely upon the backend to tell us what it is. */ | |
3231 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
3232 | /* FIXME - this function is not coded yet: | |
3233 | ||
3234 | return _bfd_is_global_symbol_definition (abfd, sym); | |
3235 | ||
3236 | Instead for now assume that the definition is not global, | |
3237 | Even if this is wrong, at least the linker will behave | |
3238 | in the same way that it used to do. */ | |
3239 | return FALSE; | |
3240 | ||
3241 | return TRUE; | |
3242 | } | |
3243 | ||
3244 | /* Search the symbol table of the archive element of the archive ABFD | |
3245 | whose archive map contains a mention of SYMDEF, and determine if | |
3246 | the symbol is defined in this element. */ | |
3247 | static bfd_boolean | |
3248 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
3249 | { | |
3250 | Elf_Internal_Shdr * hdr; | |
ef53be89 AM |
3251 | size_t symcount; |
3252 | size_t extsymcount; | |
3253 | size_t extsymoff; | |
0ad989f9 L |
3254 | Elf_Internal_Sym *isymbuf; |
3255 | Elf_Internal_Sym *isym; | |
3256 | Elf_Internal_Sym *isymend; | |
3257 | bfd_boolean result; | |
3258 | ||
3259 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
3260 | if (abfd == NULL) | |
3261 | return FALSE; | |
3262 | ||
3263 | if (! bfd_check_format (abfd, bfd_object)) | |
3264 | return FALSE; | |
3265 | ||
7dc3990e L |
3266 | /* Select the appropriate symbol table. If we don't know if the |
3267 | object file is an IR object, give linker LTO plugin a chance to | |
3268 | get the correct symbol table. */ | |
3269 | if (abfd->plugin_format == bfd_plugin_yes | |
08ce1d72 | 3270 | #if BFD_SUPPORTS_PLUGINS |
7dc3990e L |
3271 | || (abfd->plugin_format == bfd_plugin_unknown |
3272 | && bfd_link_plugin_object_p (abfd)) | |
3273 | #endif | |
3274 | ) | |
3275 | { | |
3276 | /* Use the IR symbol table if the object has been claimed by | |
3277 | plugin. */ | |
3278 | abfd = abfd->plugin_dummy_bfd; | |
3279 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3280 | } | |
3281 | else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
0ad989f9 L |
3282 | hdr = &elf_tdata (abfd)->symtab_hdr; |
3283 | else | |
3284 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3285 | ||
3286 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3287 | ||
3288 | /* The sh_info field of the symtab header tells us where the | |
3289 | external symbols start. We don't care about the local symbols. */ | |
3290 | if (elf_bad_symtab (abfd)) | |
3291 | { | |
3292 | extsymcount = symcount; | |
3293 | extsymoff = 0; | |
3294 | } | |
3295 | else | |
3296 | { | |
3297 | extsymcount = symcount - hdr->sh_info; | |
3298 | extsymoff = hdr->sh_info; | |
3299 | } | |
3300 | ||
3301 | if (extsymcount == 0) | |
3302 | return FALSE; | |
3303 | ||
3304 | /* Read in the symbol table. */ | |
3305 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3306 | NULL, NULL, NULL); | |
3307 | if (isymbuf == NULL) | |
3308 | return FALSE; | |
3309 | ||
3310 | /* Scan the symbol table looking for SYMDEF. */ | |
3311 | result = FALSE; | |
3312 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
3313 | { | |
3314 | const char *name; | |
3315 | ||
3316 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3317 | isym->st_name); | |
3318 | if (name == NULL) | |
3319 | break; | |
3320 | ||
3321 | if (strcmp (name, symdef->name) == 0) | |
3322 | { | |
3323 | result = is_global_data_symbol_definition (abfd, isym); | |
3324 | break; | |
3325 | } | |
3326 | } | |
3327 | ||
3328 | free (isymbuf); | |
3329 | ||
3330 | return result; | |
3331 | } | |
3332 | \f | |
5a580b3a AM |
3333 | /* Add an entry to the .dynamic table. */ |
3334 | ||
3335 | bfd_boolean | |
3336 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
3337 | bfd_vma tag, | |
3338 | bfd_vma val) | |
3339 | { | |
3340 | struct elf_link_hash_table *hash_table; | |
3341 | const struct elf_backend_data *bed; | |
3342 | asection *s; | |
3343 | bfd_size_type newsize; | |
3344 | bfd_byte *newcontents; | |
3345 | Elf_Internal_Dyn dyn; | |
3346 | ||
3347 | hash_table = elf_hash_table (info); | |
3348 | if (! is_elf_hash_table (hash_table)) | |
3349 | return FALSE; | |
3350 | ||
3351 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3352 | s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
5a580b3a AM |
3353 | BFD_ASSERT (s != NULL); |
3354 | ||
eea6121a | 3355 | newsize = s->size + bed->s->sizeof_dyn; |
a50b1753 | 3356 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
5a580b3a AM |
3357 | if (newcontents == NULL) |
3358 | return FALSE; | |
3359 | ||
3360 | dyn.d_tag = tag; | |
3361 | dyn.d_un.d_val = val; | |
eea6121a | 3362 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3363 | |
eea6121a | 3364 | s->size = newsize; |
5a580b3a AM |
3365 | s->contents = newcontents; |
3366 | ||
3367 | return TRUE; | |
3368 | } | |
3369 | ||
3370 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
3371 | otherwise just check whether one already exists. Returns -1 on error, | |
3372 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
3373 | ||
4ad4eba5 | 3374 | static int |
7e9f0867 AM |
3375 | elf_add_dt_needed_tag (bfd *abfd, |
3376 | struct bfd_link_info *info, | |
4ad4eba5 AM |
3377 | const char *soname, |
3378 | bfd_boolean do_it) | |
5a580b3a AM |
3379 | { |
3380 | struct elf_link_hash_table *hash_table; | |
ef53be89 | 3381 | size_t strindex; |
5a580b3a | 3382 | |
7e9f0867 AM |
3383 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3384 | return -1; | |
3385 | ||
5a580b3a | 3386 | hash_table = elf_hash_table (info); |
5a580b3a | 3387 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); |
ef53be89 | 3388 | if (strindex == (size_t) -1) |
5a580b3a AM |
3389 | return -1; |
3390 | ||
02be4619 | 3391 | if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) |
5a580b3a AM |
3392 | { |
3393 | asection *sdyn; | |
3394 | const struct elf_backend_data *bed; | |
3395 | bfd_byte *extdyn; | |
3396 | ||
3397 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3398 | sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
7e9f0867 AM |
3399 | if (sdyn != NULL) |
3400 | for (extdyn = sdyn->contents; | |
3401 | extdyn < sdyn->contents + sdyn->size; | |
3402 | extdyn += bed->s->sizeof_dyn) | |
3403 | { | |
3404 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3405 | |
7e9f0867 AM |
3406 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3407 | if (dyn.d_tag == DT_NEEDED | |
3408 | && dyn.d_un.d_val == strindex) | |
3409 | { | |
3410 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3411 | return 1; | |
3412 | } | |
3413 | } | |
5a580b3a AM |
3414 | } |
3415 | ||
3416 | if (do_it) | |
3417 | { | |
7e9f0867 AM |
3418 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3419 | return -1; | |
3420 | ||
5a580b3a AM |
3421 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3422 | return -1; | |
3423 | } | |
3424 | else | |
3425 | /* We were just checking for existence of the tag. */ | |
3426 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3427 | ||
3428 | return 0; | |
3429 | } | |
3430 | ||
7b15fa7a AM |
3431 | /* Return true if SONAME is on the needed list between NEEDED and STOP |
3432 | (or the end of list if STOP is NULL), and needed by a library that | |
3433 | will be loaded. */ | |
3434 | ||
010e5ae2 | 3435 | static bfd_boolean |
7b15fa7a AM |
3436 | on_needed_list (const char *soname, |
3437 | struct bfd_link_needed_list *needed, | |
3438 | struct bfd_link_needed_list *stop) | |
010e5ae2 | 3439 | { |
7b15fa7a AM |
3440 | struct bfd_link_needed_list *look; |
3441 | for (look = needed; look != stop; look = look->next) | |
3442 | if (strcmp (soname, look->name) == 0 | |
3443 | && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 | |
3444 | /* If needed by a library that itself is not directly | |
3445 | needed, recursively check whether that library is | |
3446 | indirectly needed. Since we add DT_NEEDED entries to | |
3447 | the end of the list, library dependencies appear after | |
3448 | the library. Therefore search prior to the current | |
3449 | LOOK, preventing possible infinite recursion. */ | |
3450 | || on_needed_list (elf_dt_name (look->by), needed, look))) | |
010e5ae2 AM |
3451 | return TRUE; |
3452 | ||
3453 | return FALSE; | |
3454 | } | |
3455 | ||
14160578 | 3456 | /* Sort symbol by value, section, and size. */ |
4ad4eba5 AM |
3457 | static int |
3458 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3459 | { |
3460 | const struct elf_link_hash_entry *h1; | |
3461 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3462 | bfd_signed_vma vdiff; |
5a580b3a AM |
3463 | |
3464 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3465 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3466 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3467 | if (vdiff != 0) | |
3468 | return vdiff > 0 ? 1 : -1; | |
3469 | else | |
3470 | { | |
d3435ae8 | 3471 | int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; |
10b7e05b NC |
3472 | if (sdiff != 0) |
3473 | return sdiff > 0 ? 1 : -1; | |
3474 | } | |
14160578 AM |
3475 | vdiff = h1->size - h2->size; |
3476 | return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1; | |
5a580b3a | 3477 | } |
4ad4eba5 | 3478 | |
5a580b3a AM |
3479 | /* This function is used to adjust offsets into .dynstr for |
3480 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3481 | ||
3482 | static bfd_boolean | |
3483 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3484 | { | |
a50b1753 | 3485 | struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; |
5a580b3a | 3486 | |
5a580b3a AM |
3487 | if (h->dynindx != -1) |
3488 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3489 | return TRUE; | |
3490 | } | |
3491 | ||
3492 | /* Assign string offsets in .dynstr, update all structures referencing | |
3493 | them. */ | |
3494 | ||
4ad4eba5 AM |
3495 | static bfd_boolean |
3496 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3497 | { |
3498 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3499 | struct elf_link_local_dynamic_entry *entry; | |
3500 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3501 | bfd *dynobj = hash_table->dynobj; | |
3502 | asection *sdyn; | |
3503 | bfd_size_type size; | |
3504 | const struct elf_backend_data *bed; | |
3505 | bfd_byte *extdyn; | |
3506 | ||
3507 | _bfd_elf_strtab_finalize (dynstr); | |
3508 | size = _bfd_elf_strtab_size (dynstr); | |
3509 | ||
3510 | bed = get_elf_backend_data (dynobj); | |
3d4d4302 | 3511 | sdyn = bfd_get_linker_section (dynobj, ".dynamic"); |
5a580b3a AM |
3512 | BFD_ASSERT (sdyn != NULL); |
3513 | ||
3514 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3515 | for (extdyn = sdyn->contents; | |
eea6121a | 3516 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3517 | extdyn += bed->s->sizeof_dyn) |
3518 | { | |
3519 | Elf_Internal_Dyn dyn; | |
3520 | ||
3521 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3522 | switch (dyn.d_tag) | |
3523 | { | |
3524 | case DT_STRSZ: | |
3525 | dyn.d_un.d_val = size; | |
3526 | break; | |
3527 | case DT_NEEDED: | |
3528 | case DT_SONAME: | |
3529 | case DT_RPATH: | |
3530 | case DT_RUNPATH: | |
3531 | case DT_FILTER: | |
3532 | case DT_AUXILIARY: | |
7ee314fa AM |
3533 | case DT_AUDIT: |
3534 | case DT_DEPAUDIT: | |
5a580b3a AM |
3535 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); |
3536 | break; | |
3537 | default: | |
3538 | continue; | |
3539 | } | |
3540 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3541 | } | |
3542 | ||
3543 | /* Now update local dynamic symbols. */ | |
3544 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3545 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3546 | entry->isym.st_name); | |
3547 | ||
3548 | /* And the rest of dynamic symbols. */ | |
3549 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3550 | ||
3551 | /* Adjust version definitions. */ | |
3552 | if (elf_tdata (output_bfd)->cverdefs) | |
3553 | { | |
3554 | asection *s; | |
3555 | bfd_byte *p; | |
ef53be89 | 3556 | size_t i; |
5a580b3a AM |
3557 | Elf_Internal_Verdef def; |
3558 | Elf_Internal_Verdaux defaux; | |
3559 | ||
3d4d4302 | 3560 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
3561 | p = s->contents; |
3562 | do | |
3563 | { | |
3564 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3565 | &def); | |
3566 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3567 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3568 | continue; | |
5a580b3a AM |
3569 | for (i = 0; i < def.vd_cnt; ++i) |
3570 | { | |
3571 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3572 | (Elf_External_Verdaux *) p, &defaux); | |
3573 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3574 | defaux.vda_name); | |
3575 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3576 | &defaux, (Elf_External_Verdaux *) p); | |
3577 | p += sizeof (Elf_External_Verdaux); | |
3578 | } | |
3579 | } | |
3580 | while (def.vd_next); | |
3581 | } | |
3582 | ||
3583 | /* Adjust version references. */ | |
3584 | if (elf_tdata (output_bfd)->verref) | |
3585 | { | |
3586 | asection *s; | |
3587 | bfd_byte *p; | |
ef53be89 | 3588 | size_t i; |
5a580b3a AM |
3589 | Elf_Internal_Verneed need; |
3590 | Elf_Internal_Vernaux needaux; | |
3591 | ||
3d4d4302 | 3592 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
3593 | p = s->contents; |
3594 | do | |
3595 | { | |
3596 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3597 | &need); | |
3598 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3599 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3600 | (Elf_External_Verneed *) p); | |
3601 | p += sizeof (Elf_External_Verneed); | |
3602 | for (i = 0; i < need.vn_cnt; ++i) | |
3603 | { | |
3604 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3605 | (Elf_External_Vernaux *) p, &needaux); | |
3606 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3607 | needaux.vna_name); | |
3608 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3609 | &needaux, | |
3610 | (Elf_External_Vernaux *) p); | |
3611 | p += sizeof (Elf_External_Vernaux); | |
3612 | } | |
3613 | } | |
3614 | while (need.vn_next); | |
3615 | } | |
3616 | ||
3617 | return TRUE; | |
3618 | } | |
3619 | \f | |
13285a1b AM |
3620 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
3621 | The default is to only match when the INPUT and OUTPUT are exactly | |
3622 | the same target. */ | |
3623 | ||
3624 | bfd_boolean | |
3625 | _bfd_elf_default_relocs_compatible (const bfd_target *input, | |
3626 | const bfd_target *output) | |
3627 | { | |
3628 | return input == output; | |
3629 | } | |
3630 | ||
3631 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
3632 | This version is used when different targets for the same architecture | |
3633 | are virtually identical. */ | |
3634 | ||
3635 | bfd_boolean | |
3636 | _bfd_elf_relocs_compatible (const bfd_target *input, | |
3637 | const bfd_target *output) | |
3638 | { | |
3639 | const struct elf_backend_data *obed, *ibed; | |
3640 | ||
3641 | if (input == output) | |
3642 | return TRUE; | |
3643 | ||
3644 | ibed = xvec_get_elf_backend_data (input); | |
3645 | obed = xvec_get_elf_backend_data (output); | |
3646 | ||
3647 | if (ibed->arch != obed->arch) | |
3648 | return FALSE; | |
3649 | ||
3650 | /* If both backends are using this function, deem them compatible. */ | |
3651 | return ibed->relocs_compatible == obed->relocs_compatible; | |
3652 | } | |
3653 | ||
e5034e59 AM |
3654 | /* Make a special call to the linker "notice" function to tell it that |
3655 | we are about to handle an as-needed lib, or have finished | |
1b786873 | 3656 | processing the lib. */ |
e5034e59 AM |
3657 | |
3658 | bfd_boolean | |
3659 | _bfd_elf_notice_as_needed (bfd *ibfd, | |
3660 | struct bfd_link_info *info, | |
3661 | enum notice_asneeded_action act) | |
3662 | { | |
46135103 | 3663 | return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); |
e5034e59 AM |
3664 | } |
3665 | ||
d9689752 L |
3666 | /* Check relocations an ELF object file. */ |
3667 | ||
3668 | bfd_boolean | |
3669 | _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) | |
3670 | { | |
3671 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3672 | struct elf_link_hash_table *htab = elf_hash_table (info); | |
3673 | ||
3674 | /* If this object is the same format as the output object, and it is | |
3675 | not a shared library, then let the backend look through the | |
3676 | relocs. | |
3677 | ||
3678 | This is required to build global offset table entries and to | |
3679 | arrange for dynamic relocs. It is not required for the | |
3680 | particular common case of linking non PIC code, even when linking | |
3681 | against shared libraries, but unfortunately there is no way of | |
3682 | knowing whether an object file has been compiled PIC or not. | |
3683 | Looking through the relocs is not particularly time consuming. | |
3684 | The problem is that we must either (1) keep the relocs in memory, | |
3685 | which causes the linker to require additional runtime memory or | |
3686 | (2) read the relocs twice from the input file, which wastes time. | |
3687 | This would be a good case for using mmap. | |
3688 | ||
3689 | I have no idea how to handle linking PIC code into a file of a | |
3690 | different format. It probably can't be done. */ | |
3691 | if ((abfd->flags & DYNAMIC) == 0 | |
3692 | && is_elf_hash_table (htab) | |
3693 | && bed->check_relocs != NULL | |
3694 | && elf_object_id (abfd) == elf_hash_table_id (htab) | |
3695 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
3696 | { | |
3697 | asection *o; | |
3698 | ||
3699 | for (o = abfd->sections; o != NULL; o = o->next) | |
3700 | { | |
3701 | Elf_Internal_Rela *internal_relocs; | |
3702 | bfd_boolean ok; | |
3703 | ||
5ce03cea | 3704 | /* Don't check relocations in excluded sections. */ |
d9689752 | 3705 | if ((o->flags & SEC_RELOC) == 0 |
5ce03cea | 3706 | || (o->flags & SEC_EXCLUDE) != 0 |
d9689752 L |
3707 | || o->reloc_count == 0 |
3708 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
3709 | && (o->flags & SEC_DEBUGGING) != 0) | |
3710 | || bfd_is_abs_section (o->output_section)) | |
3711 | continue; | |
3712 | ||
3713 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
3714 | info->keep_memory); | |
3715 | if (internal_relocs == NULL) | |
3716 | return FALSE; | |
3717 | ||
3718 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); | |
3719 | ||
3720 | if (elf_section_data (o)->relocs != internal_relocs) | |
3721 | free (internal_relocs); | |
3722 | ||
3723 | if (! ok) | |
3724 | return FALSE; | |
3725 | } | |
3726 | } | |
3727 | ||
3728 | return TRUE; | |
3729 | } | |
3730 | ||
4ad4eba5 AM |
3731 | /* Add symbols from an ELF object file to the linker hash table. */ |
3732 | ||
3733 | static bfd_boolean | |
3734 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3735 | { | |
a0c402a5 | 3736 | Elf_Internal_Ehdr *ehdr; |
4ad4eba5 | 3737 | Elf_Internal_Shdr *hdr; |
ef53be89 AM |
3738 | size_t symcount; |
3739 | size_t extsymcount; | |
3740 | size_t extsymoff; | |
4ad4eba5 AM |
3741 | struct elf_link_hash_entry **sym_hash; |
3742 | bfd_boolean dynamic; | |
3743 | Elf_External_Versym *extversym = NULL; | |
3744 | Elf_External_Versym *ever; | |
3745 | struct elf_link_hash_entry *weaks; | |
3746 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
ef53be89 | 3747 | size_t nondeflt_vers_cnt = 0; |
4ad4eba5 AM |
3748 | Elf_Internal_Sym *isymbuf = NULL; |
3749 | Elf_Internal_Sym *isym; | |
3750 | Elf_Internal_Sym *isymend; | |
3751 | const struct elf_backend_data *bed; | |
3752 | bfd_boolean add_needed; | |
66eb6687 | 3753 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3754 | bfd_size_type amt; |
66eb6687 | 3755 | void *alloc_mark = NULL; |
4f87808c AM |
3756 | struct bfd_hash_entry **old_table = NULL; |
3757 | unsigned int old_size = 0; | |
3758 | unsigned int old_count = 0; | |
66eb6687 | 3759 | void *old_tab = NULL; |
66eb6687 AM |
3760 | void *old_ent; |
3761 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3762 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
5b677558 | 3763 | void *old_strtab = NULL; |
66eb6687 | 3764 | size_t tabsize = 0; |
db6a5d5f | 3765 | asection *s; |
29a9f53e | 3766 | bfd_boolean just_syms; |
4ad4eba5 | 3767 | |
66eb6687 | 3768 | htab = elf_hash_table (info); |
4ad4eba5 | 3769 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3770 | |
3771 | if ((abfd->flags & DYNAMIC) == 0) | |
3772 | dynamic = FALSE; | |
3773 | else | |
3774 | { | |
3775 | dynamic = TRUE; | |
3776 | ||
3777 | /* You can't use -r against a dynamic object. Also, there's no | |
3778 | hope of using a dynamic object which does not exactly match | |
3779 | the format of the output file. */ | |
0e1862bb | 3780 | if (bfd_link_relocatable (info) |
66eb6687 | 3781 | || !is_elf_hash_table (htab) |
f13a99db | 3782 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 3783 | { |
0e1862bb | 3784 | if (bfd_link_relocatable (info)) |
9a0789ec NC |
3785 | bfd_set_error (bfd_error_invalid_operation); |
3786 | else | |
3787 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3788 | goto error_return; |
3789 | } | |
3790 | } | |
3791 | ||
a0c402a5 L |
3792 | ehdr = elf_elfheader (abfd); |
3793 | if (info->warn_alternate_em | |
3794 | && bed->elf_machine_code != ehdr->e_machine | |
3795 | && ((bed->elf_machine_alt1 != 0 | |
3796 | && ehdr->e_machine == bed->elf_machine_alt1) | |
3797 | || (bed->elf_machine_alt2 != 0 | |
3798 | && ehdr->e_machine == bed->elf_machine_alt2))) | |
3799 | info->callbacks->einfo | |
695344c0 | 3800 | /* xgettext:c-format */ |
a0c402a5 L |
3801 | (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"), |
3802 | ehdr->e_machine, abfd, bed->elf_machine_code); | |
3803 | ||
4ad4eba5 AM |
3804 | /* As a GNU extension, any input sections which are named |
3805 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3806 | symbol. This differs from .gnu.warning sections, which generate | |
3807 | warnings when they are included in an output file. */ | |
dd98f8d2 | 3808 | /* PR 12761: Also generate this warning when building shared libraries. */ |
db6a5d5f | 3809 | for (s = abfd->sections; s != NULL; s = s->next) |
4ad4eba5 | 3810 | { |
db6a5d5f | 3811 | const char *name; |
4ad4eba5 | 3812 | |
db6a5d5f AM |
3813 | name = bfd_get_section_name (abfd, s); |
3814 | if (CONST_STRNEQ (name, ".gnu.warning.")) | |
4ad4eba5 | 3815 | { |
db6a5d5f AM |
3816 | char *msg; |
3817 | bfd_size_type sz; | |
3818 | ||
3819 | name += sizeof ".gnu.warning." - 1; | |
3820 | ||
3821 | /* If this is a shared object, then look up the symbol | |
3822 | in the hash table. If it is there, and it is already | |
3823 | been defined, then we will not be using the entry | |
3824 | from this shared object, so we don't need to warn. | |
3825 | FIXME: If we see the definition in a regular object | |
3826 | later on, we will warn, but we shouldn't. The only | |
3827 | fix is to keep track of what warnings we are supposed | |
3828 | to emit, and then handle them all at the end of the | |
3829 | link. */ | |
3830 | if (dynamic) | |
4ad4eba5 | 3831 | { |
db6a5d5f AM |
3832 | struct elf_link_hash_entry *h; |
3833 | ||
3834 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); | |
3835 | ||
3836 | /* FIXME: What about bfd_link_hash_common? */ | |
3837 | if (h != NULL | |
3838 | && (h->root.type == bfd_link_hash_defined | |
3839 | || h->root.type == bfd_link_hash_defweak)) | |
3840 | continue; | |
3841 | } | |
4ad4eba5 | 3842 | |
db6a5d5f AM |
3843 | sz = s->size; |
3844 | msg = (char *) bfd_alloc (abfd, sz + 1); | |
3845 | if (msg == NULL) | |
3846 | goto error_return; | |
4ad4eba5 | 3847 | |
db6a5d5f AM |
3848 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
3849 | goto error_return; | |
4ad4eba5 | 3850 | |
db6a5d5f | 3851 | msg[sz] = '\0'; |
4ad4eba5 | 3852 | |
db6a5d5f AM |
3853 | if (! (_bfd_generic_link_add_one_symbol |
3854 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3855 | FALSE, bed->collect, NULL))) | |
3856 | goto error_return; | |
4ad4eba5 | 3857 | |
0e1862bb | 3858 | if (bfd_link_executable (info)) |
db6a5d5f AM |
3859 | { |
3860 | /* Clobber the section size so that the warning does | |
3861 | not get copied into the output file. */ | |
3862 | s->size = 0; | |
11d2f718 | 3863 | |
db6a5d5f AM |
3864 | /* Also set SEC_EXCLUDE, so that symbols defined in |
3865 | the warning section don't get copied to the output. */ | |
3866 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3867 | } |
3868 | } | |
3869 | } | |
3870 | ||
29a9f53e L |
3871 | just_syms = ((s = abfd->sections) != NULL |
3872 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); | |
3873 | ||
4ad4eba5 AM |
3874 | add_needed = TRUE; |
3875 | if (! dynamic) | |
3876 | { | |
3877 | /* If we are creating a shared library, create all the dynamic | |
3878 | sections immediately. We need to attach them to something, | |
3879 | so we attach them to this BFD, provided it is the right | |
bf89386a L |
3880 | format and is not from ld --just-symbols. Always create the |
3881 | dynamic sections for -E/--dynamic-list. FIXME: If there | |
29a9f53e L |
3882 | are no input BFD's of the same format as the output, we can't |
3883 | make a shared library. */ | |
3884 | if (!just_syms | |
bf89386a | 3885 | && (bfd_link_pic (info) |
9c1d7a08 | 3886 | || (!bfd_link_relocatable (info) |
3c5fce9b | 3887 | && info->nointerp |
9c1d7a08 | 3888 | && (info->export_dynamic || info->dynamic))) |
66eb6687 | 3889 | && is_elf_hash_table (htab) |
f13a99db | 3890 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 3891 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
3892 | { |
3893 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3894 | goto error_return; | |
3895 | } | |
3896 | } | |
66eb6687 | 3897 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
3898 | goto error_return; |
3899 | else | |
3900 | { | |
4ad4eba5 | 3901 | const char *soname = NULL; |
7ee314fa | 3902 | char *audit = NULL; |
4ad4eba5 | 3903 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; |
9acc85a6 | 3904 | const Elf_Internal_Phdr *phdr; |
4ad4eba5 AM |
3905 | int ret; |
3906 | ||
3907 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3908 | ld shouldn't allow it. */ |
29a9f53e | 3909 | if (just_syms) |
92fd189d | 3910 | abort (); |
4ad4eba5 AM |
3911 | |
3912 | /* If this dynamic lib was specified on the command line with | |
3913 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3914 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3915 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3916 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3917 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3918 | all. */ | |
3919 | add_needed = (elf_dyn_lib_class (abfd) | |
3920 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3921 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3922 | |
3923 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3924 | if (s != NULL) | |
3925 | { | |
3926 | bfd_byte *dynbuf; | |
3927 | bfd_byte *extdyn; | |
cb33740c | 3928 | unsigned int elfsec; |
4ad4eba5 AM |
3929 | unsigned long shlink; |
3930 | ||
eea6121a | 3931 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
f8703194 L |
3932 | { |
3933 | error_free_dyn: | |
3934 | free (dynbuf); | |
3935 | goto error_return; | |
3936 | } | |
4ad4eba5 AM |
3937 | |
3938 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 3939 | if (elfsec == SHN_BAD) |
4ad4eba5 AM |
3940 | goto error_free_dyn; |
3941 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3942 | ||
3943 | for (extdyn = dynbuf; | |
eea6121a | 3944 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3945 | extdyn += bed->s->sizeof_dyn) |
3946 | { | |
3947 | Elf_Internal_Dyn dyn; | |
3948 | ||
3949 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3950 | if (dyn.d_tag == DT_SONAME) | |
3951 | { | |
3952 | unsigned int tagv = dyn.d_un.d_val; | |
3953 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3954 | if (soname == NULL) | |
3955 | goto error_free_dyn; | |
3956 | } | |
3957 | if (dyn.d_tag == DT_NEEDED) | |
3958 | { | |
3959 | struct bfd_link_needed_list *n, **pn; | |
3960 | char *fnm, *anm; | |
3961 | unsigned int tagv = dyn.d_un.d_val; | |
3962 | ||
3963 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3964 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3965 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3966 | if (n == NULL || fnm == NULL) | |
3967 | goto error_free_dyn; | |
3968 | amt = strlen (fnm) + 1; | |
a50b1753 | 3969 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3970 | if (anm == NULL) |
3971 | goto error_free_dyn; | |
3972 | memcpy (anm, fnm, amt); | |
3973 | n->name = anm; | |
3974 | n->by = abfd; | |
3975 | n->next = NULL; | |
66eb6687 | 3976 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3977 | ; |
3978 | *pn = n; | |
3979 | } | |
3980 | if (dyn.d_tag == DT_RUNPATH) | |
3981 | { | |
3982 | struct bfd_link_needed_list *n, **pn; | |
3983 | char *fnm, *anm; | |
3984 | unsigned int tagv = dyn.d_un.d_val; | |
3985 | ||
3986 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3987 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3988 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3989 | if (n == NULL || fnm == NULL) | |
3990 | goto error_free_dyn; | |
3991 | amt = strlen (fnm) + 1; | |
a50b1753 | 3992 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3993 | if (anm == NULL) |
3994 | goto error_free_dyn; | |
3995 | memcpy (anm, fnm, amt); | |
3996 | n->name = anm; | |
3997 | n->by = abfd; | |
3998 | n->next = NULL; | |
3999 | for (pn = & runpath; | |
4000 | *pn != NULL; | |
4001 | pn = &(*pn)->next) | |
4002 | ; | |
4003 | *pn = n; | |
4004 | } | |
4005 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
4006 | if (!runpath && dyn.d_tag == DT_RPATH) | |
4007 | { | |
4008 | struct bfd_link_needed_list *n, **pn; | |
4009 | char *fnm, *anm; | |
4010 | unsigned int tagv = dyn.d_un.d_val; | |
4011 | ||
4012 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 4013 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4014 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4015 | if (n == NULL || fnm == NULL) | |
4016 | goto error_free_dyn; | |
4017 | amt = strlen (fnm) + 1; | |
a50b1753 | 4018 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 | 4019 | if (anm == NULL) |
f8703194 | 4020 | goto error_free_dyn; |
4ad4eba5 AM |
4021 | memcpy (anm, fnm, amt); |
4022 | n->name = anm; | |
4023 | n->by = abfd; | |
4024 | n->next = NULL; | |
4025 | for (pn = & rpath; | |
4026 | *pn != NULL; | |
4027 | pn = &(*pn)->next) | |
4028 | ; | |
4029 | *pn = n; | |
4030 | } | |
7ee314fa AM |
4031 | if (dyn.d_tag == DT_AUDIT) |
4032 | { | |
4033 | unsigned int tagv = dyn.d_un.d_val; | |
4034 | audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
4035 | } | |
4ad4eba5 AM |
4036 | } |
4037 | ||
4038 | free (dynbuf); | |
4039 | } | |
4040 | ||
4041 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
4042 | frees all more recently bfd_alloc'd blocks as well. */ | |
4043 | if (runpath) | |
4044 | rpath = runpath; | |
4045 | ||
4046 | if (rpath) | |
4047 | { | |
4048 | struct bfd_link_needed_list **pn; | |
66eb6687 | 4049 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
4050 | ; |
4051 | *pn = rpath; | |
4052 | } | |
4053 | ||
9acc85a6 AM |
4054 | /* If we have a PT_GNU_RELRO program header, mark as read-only |
4055 | all sections contained fully therein. This makes relro | |
4056 | shared library sections appear as they will at run-time. */ | |
4057 | phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum; | |
4058 | while (--phdr >= elf_tdata (abfd)->phdr) | |
4059 | if (phdr->p_type == PT_GNU_RELRO) | |
4060 | { | |
4061 | for (s = abfd->sections; s != NULL; s = s->next) | |
4062 | if ((s->flags & SEC_ALLOC) != 0 | |
4063 | && s->vma >= phdr->p_vaddr | |
4064 | && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz) | |
4065 | s->flags |= SEC_READONLY; | |
4066 | break; | |
4067 | } | |
4068 | ||
4ad4eba5 AM |
4069 | /* We do not want to include any of the sections in a dynamic |
4070 | object in the output file. We hack by simply clobbering the | |
4071 | list of sections in the BFD. This could be handled more | |
4072 | cleanly by, say, a new section flag; the existing | |
4073 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
4074 | still implies that the section takes up space in the output | |
4075 | file. */ | |
4076 | bfd_section_list_clear (abfd); | |
4077 | ||
4ad4eba5 AM |
4078 | /* Find the name to use in a DT_NEEDED entry that refers to this |
4079 | object. If the object has a DT_SONAME entry, we use it. | |
4080 | Otherwise, if the generic linker stuck something in | |
4081 | elf_dt_name, we use that. Otherwise, we just use the file | |
4082 | name. */ | |
4083 | if (soname == NULL || *soname == '\0') | |
4084 | { | |
4085 | soname = elf_dt_name (abfd); | |
4086 | if (soname == NULL || *soname == '\0') | |
4087 | soname = bfd_get_filename (abfd); | |
4088 | } | |
4089 | ||
4090 | /* Save the SONAME because sometimes the linker emulation code | |
4091 | will need to know it. */ | |
4092 | elf_dt_name (abfd) = soname; | |
4093 | ||
7e9f0867 | 4094 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4095 | if (ret < 0) |
4096 | goto error_return; | |
4097 | ||
4098 | /* If we have already included this dynamic object in the | |
4099 | link, just ignore it. There is no reason to include a | |
4100 | particular dynamic object more than once. */ | |
4101 | if (ret > 0) | |
4102 | return TRUE; | |
7ee314fa AM |
4103 | |
4104 | /* Save the DT_AUDIT entry for the linker emulation code. */ | |
68ffbac6 | 4105 | elf_dt_audit (abfd) = audit; |
4ad4eba5 AM |
4106 | } |
4107 | ||
4108 | /* If this is a dynamic object, we always link against the .dynsym | |
4109 | symbol table, not the .symtab symbol table. The dynamic linker | |
4110 | will only see the .dynsym symbol table, so there is no reason to | |
4111 | look at .symtab for a dynamic object. */ | |
4112 | ||
4113 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
4114 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
4115 | else | |
4116 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
4117 | ||
4118 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
4119 | ||
4120 | /* The sh_info field of the symtab header tells us where the | |
4121 | external symbols start. We don't care about the local symbols at | |
4122 | this point. */ | |
4123 | if (elf_bad_symtab (abfd)) | |
4124 | { | |
4125 | extsymcount = symcount; | |
4126 | extsymoff = 0; | |
4127 | } | |
4128 | else | |
4129 | { | |
4130 | extsymcount = symcount - hdr->sh_info; | |
4131 | extsymoff = hdr->sh_info; | |
4132 | } | |
4133 | ||
f45794cb | 4134 | sym_hash = elf_sym_hashes (abfd); |
012b2306 | 4135 | if (extsymcount != 0) |
4ad4eba5 AM |
4136 | { |
4137 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
4138 | NULL, NULL, NULL); | |
4139 | if (isymbuf == NULL) | |
4140 | goto error_return; | |
4141 | ||
4ad4eba5 | 4142 | if (sym_hash == NULL) |
012b2306 AM |
4143 | { |
4144 | /* We store a pointer to the hash table entry for each | |
4145 | external symbol. */ | |
ef53be89 AM |
4146 | amt = extsymcount; |
4147 | amt *= sizeof (struct elf_link_hash_entry *); | |
012b2306 AM |
4148 | sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); |
4149 | if (sym_hash == NULL) | |
4150 | goto error_free_sym; | |
4151 | elf_sym_hashes (abfd) = sym_hash; | |
4152 | } | |
4ad4eba5 AM |
4153 | } |
4154 | ||
4155 | if (dynamic) | |
4156 | { | |
4157 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
4158 | if (!_bfd_elf_slurp_version_tables (abfd, |
4159 | info->default_imported_symver)) | |
4ad4eba5 AM |
4160 | goto error_free_sym; |
4161 | ||
4162 | /* Read in the symbol versions, but don't bother to convert them | |
4163 | to internal format. */ | |
4164 | if (elf_dynversym (abfd) != 0) | |
4165 | { | |
4166 | Elf_Internal_Shdr *versymhdr; | |
4167 | ||
4168 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
a50b1753 | 4169 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
4ad4eba5 AM |
4170 | if (extversym == NULL) |
4171 | goto error_free_sym; | |
4172 | amt = versymhdr->sh_size; | |
4173 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
4174 | || bfd_bread (extversym, amt, abfd) != amt) | |
4175 | goto error_free_vers; | |
4176 | } | |
4177 | } | |
4178 | ||
66eb6687 AM |
4179 | /* If we are loading an as-needed shared lib, save the symbol table |
4180 | state before we start adding symbols. If the lib turns out | |
4181 | to be unneeded, restore the state. */ | |
4182 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4183 | { | |
4184 | unsigned int i; | |
4185 | size_t entsize; | |
4186 | ||
4187 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
4188 | { | |
4189 | struct bfd_hash_entry *p; | |
2de92251 | 4190 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4191 | |
4192 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
4193 | { |
4194 | h = (struct elf_link_hash_entry *) p; | |
4195 | entsize += htab->root.table.entsize; | |
4196 | if (h->root.type == bfd_link_hash_warning) | |
4197 | entsize += htab->root.table.entsize; | |
4198 | } | |
66eb6687 AM |
4199 | } |
4200 | ||
4201 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
f45794cb | 4202 | old_tab = bfd_malloc (tabsize + entsize); |
66eb6687 AM |
4203 | if (old_tab == NULL) |
4204 | goto error_free_vers; | |
4205 | ||
4206 | /* Remember the current objalloc pointer, so that all mem for | |
4207 | symbols added can later be reclaimed. */ | |
4208 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
4209 | if (alloc_mark == NULL) | |
4210 | goto error_free_vers; | |
4211 | ||
5061a885 AM |
4212 | /* Make a special call to the linker "notice" function to |
4213 | tell it that we are about to handle an as-needed lib. */ | |
e5034e59 | 4214 | if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) |
9af2a943 | 4215 | goto error_free_vers; |
5061a885 | 4216 | |
f45794cb AM |
4217 | /* Clone the symbol table. Remember some pointers into the |
4218 | symbol table, and dynamic symbol count. */ | |
4219 | old_ent = (char *) old_tab + tabsize; | |
66eb6687 | 4220 | memcpy (old_tab, htab->root.table.table, tabsize); |
66eb6687 AM |
4221 | old_undefs = htab->root.undefs; |
4222 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
4223 | old_table = htab->root.table.table; |
4224 | old_size = htab->root.table.size; | |
4225 | old_count = htab->root.table.count; | |
5b677558 AM |
4226 | old_strtab = _bfd_elf_strtab_save (htab->dynstr); |
4227 | if (old_strtab == NULL) | |
4228 | goto error_free_vers; | |
66eb6687 AM |
4229 | |
4230 | for (i = 0; i < htab->root.table.size; i++) | |
4231 | { | |
4232 | struct bfd_hash_entry *p; | |
2de92251 | 4233 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4234 | |
4235 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4236 | { | |
4237 | memcpy (old_ent, p, htab->root.table.entsize); | |
4238 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4239 | h = (struct elf_link_hash_entry *) p; |
4240 | if (h->root.type == bfd_link_hash_warning) | |
4241 | { | |
4242 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
4243 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4244 | } | |
66eb6687 AM |
4245 | } |
4246 | } | |
4247 | } | |
4ad4eba5 | 4248 | |
66eb6687 | 4249 | weaks = NULL; |
4ad4eba5 AM |
4250 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
4251 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
4252 | isym < isymend; | |
4253 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
4254 | { | |
4255 | int bind; | |
4256 | bfd_vma value; | |
af44c138 | 4257 | asection *sec, *new_sec; |
4ad4eba5 AM |
4258 | flagword flags; |
4259 | const char *name; | |
4260 | struct elf_link_hash_entry *h; | |
90c984fc | 4261 | struct elf_link_hash_entry *hi; |
4ad4eba5 AM |
4262 | bfd_boolean definition; |
4263 | bfd_boolean size_change_ok; | |
4264 | bfd_boolean type_change_ok; | |
37a9e49a L |
4265 | bfd_boolean new_weak; |
4266 | bfd_boolean old_weak; | |
4ad4eba5 | 4267 | bfd_boolean override; |
a4d8e49b | 4268 | bfd_boolean common; |
97196564 | 4269 | bfd_boolean discarded; |
4ad4eba5 AM |
4270 | unsigned int old_alignment; |
4271 | bfd *old_bfd; | |
6e33951e | 4272 | bfd_boolean matched; |
4ad4eba5 AM |
4273 | |
4274 | override = FALSE; | |
4275 | ||
4276 | flags = BSF_NO_FLAGS; | |
4277 | sec = NULL; | |
4278 | value = isym->st_value; | |
a4d8e49b | 4279 | common = bed->common_definition (isym); |
2980ccad L |
4280 | if (common && info->inhibit_common_definition) |
4281 | { | |
4282 | /* Treat common symbol as undefined for --no-define-common. */ | |
4283 | isym->st_shndx = SHN_UNDEF; | |
4284 | common = FALSE; | |
4285 | } | |
97196564 | 4286 | discarded = FALSE; |
4ad4eba5 AM |
4287 | |
4288 | bind = ELF_ST_BIND (isym->st_info); | |
3e7a7d11 | 4289 | switch (bind) |
4ad4eba5 | 4290 | { |
3e7a7d11 | 4291 | case STB_LOCAL: |
4ad4eba5 AM |
4292 | /* This should be impossible, since ELF requires that all |
4293 | global symbols follow all local symbols, and that sh_info | |
4294 | point to the first global symbol. Unfortunately, Irix 5 | |
4295 | screws this up. */ | |
4296 | continue; | |
3e7a7d11 NC |
4297 | |
4298 | case STB_GLOBAL: | |
a4d8e49b | 4299 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 | 4300 | flags = BSF_GLOBAL; |
3e7a7d11 NC |
4301 | break; |
4302 | ||
4303 | case STB_WEAK: | |
4304 | flags = BSF_WEAK; | |
4305 | break; | |
4306 | ||
4307 | case STB_GNU_UNIQUE: | |
4308 | flags = BSF_GNU_UNIQUE; | |
4309 | break; | |
4310 | ||
4311 | default: | |
4ad4eba5 | 4312 | /* Leave it up to the processor backend. */ |
3e7a7d11 | 4313 | break; |
4ad4eba5 AM |
4314 | } |
4315 | ||
4316 | if (isym->st_shndx == SHN_UNDEF) | |
4317 | sec = bfd_und_section_ptr; | |
cb33740c AM |
4318 | else if (isym->st_shndx == SHN_ABS) |
4319 | sec = bfd_abs_section_ptr; | |
4320 | else if (isym->st_shndx == SHN_COMMON) | |
4321 | { | |
4322 | sec = bfd_com_section_ptr; | |
4323 | /* What ELF calls the size we call the value. What ELF | |
4324 | calls the value we call the alignment. */ | |
4325 | value = isym->st_size; | |
4326 | } | |
4327 | else | |
4ad4eba5 AM |
4328 | { |
4329 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
4330 | if (sec == NULL) | |
4331 | sec = bfd_abs_section_ptr; | |
dbaa2011 | 4332 | else if (discarded_section (sec)) |
529fcb95 | 4333 | { |
e5d08002 L |
4334 | /* Symbols from discarded section are undefined. We keep |
4335 | its visibility. */ | |
529fcb95 | 4336 | sec = bfd_und_section_ptr; |
97196564 | 4337 | discarded = TRUE; |
529fcb95 PB |
4338 | isym->st_shndx = SHN_UNDEF; |
4339 | } | |
4ad4eba5 AM |
4340 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
4341 | value -= sec->vma; | |
4342 | } | |
4ad4eba5 AM |
4343 | |
4344 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
4345 | isym->st_name); | |
4346 | if (name == NULL) | |
4347 | goto error_free_vers; | |
4348 | ||
4349 | if (isym->st_shndx == SHN_COMMON | |
02d00247 AM |
4350 | && (abfd->flags & BFD_PLUGIN) != 0) |
4351 | { | |
4352 | asection *xc = bfd_get_section_by_name (abfd, "COMMON"); | |
4353 | ||
4354 | if (xc == NULL) | |
4355 | { | |
4356 | flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP | |
4357 | | SEC_EXCLUDE); | |
4358 | xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); | |
4359 | if (xc == NULL) | |
4360 | goto error_free_vers; | |
4361 | } | |
4362 | sec = xc; | |
4363 | } | |
4364 | else if (isym->st_shndx == SHN_COMMON | |
4365 | && ELF_ST_TYPE (isym->st_info) == STT_TLS | |
0e1862bb | 4366 | && !bfd_link_relocatable (info)) |
4ad4eba5 AM |
4367 | { |
4368 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
4369 | ||
4370 | if (tcomm == NULL) | |
4371 | { | |
02d00247 AM |
4372 | flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON |
4373 | | SEC_LINKER_CREATED); | |
4374 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); | |
3496cb2a | 4375 | if (tcomm == NULL) |
4ad4eba5 AM |
4376 | goto error_free_vers; |
4377 | } | |
4378 | sec = tcomm; | |
4379 | } | |
66eb6687 | 4380 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 4381 | { |
66eb6687 AM |
4382 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
4383 | &sec, &value)) | |
4ad4eba5 AM |
4384 | goto error_free_vers; |
4385 | ||
4386 | /* The hook function sets the name to NULL if this symbol | |
4387 | should be skipped for some reason. */ | |
4388 | if (name == NULL) | |
4389 | continue; | |
4390 | } | |
4391 | ||
4392 | /* Sanity check that all possibilities were handled. */ | |
4393 | if (sec == NULL) | |
4394 | { | |
4395 | bfd_set_error (bfd_error_bad_value); | |
4396 | goto error_free_vers; | |
4397 | } | |
4398 | ||
191c0c42 AM |
4399 | /* Silently discard TLS symbols from --just-syms. There's |
4400 | no way to combine a static TLS block with a new TLS block | |
4401 | for this executable. */ | |
4402 | if (ELF_ST_TYPE (isym->st_info) == STT_TLS | |
4403 | && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
4404 | continue; | |
4405 | ||
4ad4eba5 AM |
4406 | if (bfd_is_und_section (sec) |
4407 | || bfd_is_com_section (sec)) | |
4408 | definition = FALSE; | |
4409 | else | |
4410 | definition = TRUE; | |
4411 | ||
4412 | size_change_ok = FALSE; | |
66eb6687 | 4413 | type_change_ok = bed->type_change_ok; |
37a9e49a | 4414 | old_weak = FALSE; |
6e33951e | 4415 | matched = FALSE; |
4ad4eba5 AM |
4416 | old_alignment = 0; |
4417 | old_bfd = NULL; | |
af44c138 | 4418 | new_sec = sec; |
4ad4eba5 | 4419 | |
66eb6687 | 4420 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
4421 | { |
4422 | Elf_Internal_Versym iver; | |
4423 | unsigned int vernum = 0; | |
4424 | bfd_boolean skip; | |
4425 | ||
fc0e6df6 | 4426 | if (ever == NULL) |
4ad4eba5 | 4427 | { |
fc0e6df6 PB |
4428 | if (info->default_imported_symver) |
4429 | /* Use the default symbol version created earlier. */ | |
4430 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
4431 | else | |
4432 | iver.vs_vers = 0; | |
4433 | } | |
4434 | else | |
4435 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
4436 | ||
4437 | vernum = iver.vs_vers & VERSYM_VERSION; | |
4438 | ||
4439 | /* If this is a hidden symbol, or if it is not version | |
4440 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
4441 | However, we do not modify a non-hidden absolute symbol |
4442 | if it is not a function, because it might be the version | |
4443 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 4444 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
4445 | || (vernum > 1 |
4446 | && (!bfd_is_abs_section (sec) | |
4447 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
4448 | { |
4449 | const char *verstr; | |
4450 | size_t namelen, verlen, newlen; | |
4451 | char *newname, *p; | |
4452 | ||
4453 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 4454 | { |
fc0e6df6 PB |
4455 | if (vernum > elf_tdata (abfd)->cverdefs) |
4456 | verstr = NULL; | |
4457 | else if (vernum > 1) | |
4458 | verstr = | |
4459 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
4460 | else | |
4461 | verstr = ""; | |
4ad4eba5 | 4462 | |
fc0e6df6 | 4463 | if (verstr == NULL) |
4ad4eba5 | 4464 | { |
4eca0228 | 4465 | _bfd_error_handler |
695344c0 | 4466 | /* xgettext:c-format */ |
fc0e6df6 PB |
4467 | (_("%B: %s: invalid version %u (max %d)"), |
4468 | abfd, name, vernum, | |
4469 | elf_tdata (abfd)->cverdefs); | |
4470 | bfd_set_error (bfd_error_bad_value); | |
4471 | goto error_free_vers; | |
4ad4eba5 | 4472 | } |
fc0e6df6 PB |
4473 | } |
4474 | else | |
4475 | { | |
4476 | /* We cannot simply test for the number of | |
4477 | entries in the VERNEED section since the | |
4478 | numbers for the needed versions do not start | |
4479 | at 0. */ | |
4480 | Elf_Internal_Verneed *t; | |
4481 | ||
4482 | verstr = NULL; | |
4483 | for (t = elf_tdata (abfd)->verref; | |
4484 | t != NULL; | |
4485 | t = t->vn_nextref) | |
4ad4eba5 | 4486 | { |
fc0e6df6 | 4487 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 4488 | |
fc0e6df6 PB |
4489 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
4490 | { | |
4491 | if (a->vna_other == vernum) | |
4ad4eba5 | 4492 | { |
fc0e6df6 PB |
4493 | verstr = a->vna_nodename; |
4494 | break; | |
4ad4eba5 | 4495 | } |
4ad4eba5 | 4496 | } |
fc0e6df6 PB |
4497 | if (a != NULL) |
4498 | break; | |
4499 | } | |
4500 | if (verstr == NULL) | |
4501 | { | |
4eca0228 | 4502 | _bfd_error_handler |
695344c0 | 4503 | /* xgettext:c-format */ |
fc0e6df6 PB |
4504 | (_("%B: %s: invalid needed version %d"), |
4505 | abfd, name, vernum); | |
4506 | bfd_set_error (bfd_error_bad_value); | |
4507 | goto error_free_vers; | |
4ad4eba5 | 4508 | } |
4ad4eba5 | 4509 | } |
fc0e6df6 PB |
4510 | |
4511 | namelen = strlen (name); | |
4512 | verlen = strlen (verstr); | |
4513 | newlen = namelen + verlen + 2; | |
4514 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4515 | && isym->st_shndx != SHN_UNDEF) | |
4516 | ++newlen; | |
4517 | ||
a50b1753 | 4518 | newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
4519 | if (newname == NULL) |
4520 | goto error_free_vers; | |
4521 | memcpy (newname, name, namelen); | |
4522 | p = newname + namelen; | |
4523 | *p++ = ELF_VER_CHR; | |
4524 | /* If this is a defined non-hidden version symbol, | |
4525 | we add another @ to the name. This indicates the | |
4526 | default version of the symbol. */ | |
4527 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4528 | && isym->st_shndx != SHN_UNDEF) | |
4529 | *p++ = ELF_VER_CHR; | |
4530 | memcpy (p, verstr, verlen + 1); | |
4531 | ||
4532 | name = newname; | |
4ad4eba5 AM |
4533 | } |
4534 | ||
cd3416da AM |
4535 | /* If this symbol has default visibility and the user has |
4536 | requested we not re-export it, then mark it as hidden. */ | |
a0d49154 | 4537 | if (!bfd_is_und_section (sec) |
cd3416da | 4538 | && !dynamic |
ce875075 | 4539 | && abfd->no_export |
cd3416da AM |
4540 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) |
4541 | isym->st_other = (STV_HIDDEN | |
4542 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
4543 | ||
4f3fedcf AM |
4544 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, |
4545 | sym_hash, &old_bfd, &old_weak, | |
4546 | &old_alignment, &skip, &override, | |
6e33951e L |
4547 | &type_change_ok, &size_change_ok, |
4548 | &matched)) | |
4ad4eba5 AM |
4549 | goto error_free_vers; |
4550 | ||
4551 | if (skip) | |
4552 | continue; | |
4553 | ||
6e33951e L |
4554 | /* Override a definition only if the new symbol matches the |
4555 | existing one. */ | |
4556 | if (override && matched) | |
4ad4eba5 AM |
4557 | definition = FALSE; |
4558 | ||
4559 | h = *sym_hash; | |
4560 | while (h->root.type == bfd_link_hash_indirect | |
4561 | || h->root.type == bfd_link_hash_warning) | |
4562 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4563 | ||
4ad4eba5 | 4564 | if (elf_tdata (abfd)->verdef != NULL |
4ad4eba5 AM |
4565 | && vernum > 1 |
4566 | && definition) | |
4567 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4568 | } | |
4569 | ||
4570 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4571 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4572 | (struct bfd_link_hash_entry **) sym_hash))) |
4573 | goto error_free_vers; | |
4574 | ||
a43942db MR |
4575 | if ((flags & BSF_GNU_UNIQUE) |
4576 | && (abfd->flags & DYNAMIC) == 0 | |
4577 | && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour) | |
4578 | elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique; | |
4579 | ||
4ad4eba5 | 4580 | h = *sym_hash; |
90c984fc L |
4581 | /* We need to make sure that indirect symbol dynamic flags are |
4582 | updated. */ | |
4583 | hi = h; | |
4ad4eba5 AM |
4584 | while (h->root.type == bfd_link_hash_indirect |
4585 | || h->root.type == bfd_link_hash_warning) | |
4586 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3e7a7d11 | 4587 | |
97196564 L |
4588 | /* Setting the index to -3 tells elf_link_output_extsym that |
4589 | this symbol is defined in a discarded section. */ | |
4590 | if (discarded) | |
4591 | h->indx = -3; | |
4592 | ||
4ad4eba5 AM |
4593 | *sym_hash = h; |
4594 | ||
37a9e49a | 4595 | new_weak = (flags & BSF_WEAK) != 0; |
4ad4eba5 AM |
4596 | if (dynamic |
4597 | && definition | |
37a9e49a | 4598 | && new_weak |
fcb93ecf | 4599 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4600 | && is_elf_hash_table (htab) |
60d67dc8 | 4601 | && h->u.alias == NULL) |
4ad4eba5 AM |
4602 | { |
4603 | /* Keep a list of all weak defined non function symbols from | |
60d67dc8 AM |
4604 | a dynamic object, using the alias field. Later in this |
4605 | function we will set the alias field to the correct | |
4ad4eba5 AM |
4606 | value. We only put non-function symbols from dynamic |
4607 | objects on this list, because that happens to be the only | |
4608 | time we need to know the normal symbol corresponding to a | |
4609 | weak symbol, and the information is time consuming to | |
60d67dc8 | 4610 | figure out. If the alias field is not already NULL, |
4ad4eba5 AM |
4611 | then this symbol was already defined by some previous |
4612 | dynamic object, and we will be using that previous | |
4613 | definition anyhow. */ | |
4614 | ||
60d67dc8 | 4615 | h->u.alias = weaks; |
4ad4eba5 | 4616 | weaks = h; |
4ad4eba5 AM |
4617 | } |
4618 | ||
4619 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4620 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4621 | && h->root.type == bfd_link_hash_common) |
4622 | { | |
4623 | unsigned int align; | |
4624 | ||
a4d8e49b | 4625 | if (common) |
af44c138 L |
4626 | align = bfd_log2 (isym->st_value); |
4627 | else | |
4628 | { | |
4629 | /* The new symbol is a common symbol in a shared object. | |
4630 | We need to get the alignment from the section. */ | |
4631 | align = new_sec->alignment_power; | |
4632 | } | |
595213d4 | 4633 | if (align > old_alignment) |
4ad4eba5 AM |
4634 | h->root.u.c.p->alignment_power = align; |
4635 | else | |
4636 | h->root.u.c.p->alignment_power = old_alignment; | |
4637 | } | |
4638 | ||
66eb6687 | 4639 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4640 | { |
4f3fedcf AM |
4641 | /* Set a flag in the hash table entry indicating the type of |
4642 | reference or definition we just found. A dynamic symbol | |
4643 | is one which is referenced or defined by both a regular | |
4644 | object and a shared object. */ | |
4645 | bfd_boolean dynsym = FALSE; | |
4646 | ||
4647 | /* Plugin symbols aren't normal. Don't set def_regular or | |
4648 | ref_regular for them, or make them dynamic. */ | |
4649 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4650 | ; | |
4651 | else if (! dynamic) | |
4652 | { | |
4653 | if (! definition) | |
4654 | { | |
4655 | h->ref_regular = 1; | |
4656 | if (bind != STB_WEAK) | |
4657 | h->ref_regular_nonweak = 1; | |
4658 | } | |
4659 | else | |
4660 | { | |
4661 | h->def_regular = 1; | |
4662 | if (h->def_dynamic) | |
4663 | { | |
4664 | h->def_dynamic = 0; | |
4665 | h->ref_dynamic = 1; | |
4666 | } | |
4667 | } | |
4668 | ||
4669 | /* If the indirect symbol has been forced local, don't | |
4670 | make the real symbol dynamic. */ | |
4671 | if ((h == hi || !hi->forced_local) | |
0e1862bb | 4672 | && (bfd_link_dll (info) |
4f3fedcf AM |
4673 | || h->def_dynamic |
4674 | || h->ref_dynamic)) | |
4675 | dynsym = TRUE; | |
4676 | } | |
4677 | else | |
4678 | { | |
4679 | if (! definition) | |
4680 | { | |
4681 | h->ref_dynamic = 1; | |
4682 | hi->ref_dynamic = 1; | |
4683 | } | |
4684 | else | |
4685 | { | |
4686 | h->def_dynamic = 1; | |
4687 | hi->def_dynamic = 1; | |
4688 | } | |
4689 | ||
4690 | /* If the indirect symbol has been forced local, don't | |
4691 | make the real symbol dynamic. */ | |
4692 | if ((h == hi || !hi->forced_local) | |
4693 | && (h->def_regular | |
4694 | || h->ref_regular | |
60d67dc8 AM |
4695 | || (h->is_weakalias |
4696 | && weakdef (h)->dynindx != -1))) | |
4f3fedcf AM |
4697 | dynsym = TRUE; |
4698 | } | |
4699 | ||
4700 | /* Check to see if we need to add an indirect symbol for | |
4701 | the default name. */ | |
4702 | if (definition | |
4703 | || (!override && h->root.type == bfd_link_hash_common)) | |
4704 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4705 | sec, value, &old_bfd, &dynsym)) | |
4706 | goto error_free_vers; | |
4ad4eba5 AM |
4707 | |
4708 | /* Check the alignment when a common symbol is involved. This | |
4709 | can change when a common symbol is overridden by a normal | |
4710 | definition or a common symbol is ignored due to the old | |
4711 | normal definition. We need to make sure the maximum | |
4712 | alignment is maintained. */ | |
a4d8e49b | 4713 | if ((old_alignment || common) |
4ad4eba5 AM |
4714 | && h->root.type != bfd_link_hash_common) |
4715 | { | |
4716 | unsigned int common_align; | |
4717 | unsigned int normal_align; | |
4718 | unsigned int symbol_align; | |
4719 | bfd *normal_bfd; | |
4720 | bfd *common_bfd; | |
4721 | ||
3a81e825 AM |
4722 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
4723 | || h->root.type == bfd_link_hash_defweak); | |
4724 | ||
4ad4eba5 AM |
4725 | symbol_align = ffs (h->root.u.def.value) - 1; |
4726 | if (h->root.u.def.section->owner != NULL | |
0616a280 AM |
4727 | && (h->root.u.def.section->owner->flags |
4728 | & (DYNAMIC | BFD_PLUGIN)) == 0) | |
4ad4eba5 AM |
4729 | { |
4730 | normal_align = h->root.u.def.section->alignment_power; | |
4731 | if (normal_align > symbol_align) | |
4732 | normal_align = symbol_align; | |
4733 | } | |
4734 | else | |
4735 | normal_align = symbol_align; | |
4736 | ||
4737 | if (old_alignment) | |
4738 | { | |
4739 | common_align = old_alignment; | |
4740 | common_bfd = old_bfd; | |
4741 | normal_bfd = abfd; | |
4742 | } | |
4743 | else | |
4744 | { | |
4745 | common_align = bfd_log2 (isym->st_value); | |
4746 | common_bfd = abfd; | |
4747 | normal_bfd = old_bfd; | |
4748 | } | |
4749 | ||
4750 | if (normal_align < common_align) | |
d07676f8 NC |
4751 | { |
4752 | /* PR binutils/2735 */ | |
4753 | if (normal_bfd == NULL) | |
4eca0228 | 4754 | _bfd_error_handler |
695344c0 | 4755 | /* xgettext:c-format */ |
4f3fedcf AM |
4756 | (_("Warning: alignment %u of common symbol `%s' in %B is" |
4757 | " greater than the alignment (%u) of its section %A"), | |
c08bb8dd AM |
4758 | 1 << common_align, name, common_bfd, |
4759 | 1 << normal_align, h->root.u.def.section); | |
d07676f8 | 4760 | else |
4eca0228 | 4761 | _bfd_error_handler |
695344c0 | 4762 | /* xgettext:c-format */ |
d07676f8 NC |
4763 | (_("Warning: alignment %u of symbol `%s' in %B" |
4764 | " is smaller than %u in %B"), | |
c08bb8dd AM |
4765 | 1 << normal_align, name, normal_bfd, |
4766 | 1 << common_align, common_bfd); | |
d07676f8 | 4767 | } |
4ad4eba5 AM |
4768 | } |
4769 | ||
83ad0046 | 4770 | /* Remember the symbol size if it isn't undefined. */ |
3a81e825 AM |
4771 | if (isym->st_size != 0 |
4772 | && isym->st_shndx != SHN_UNDEF | |
4ad4eba5 AM |
4773 | && (definition || h->size == 0)) |
4774 | { | |
83ad0046 L |
4775 | if (h->size != 0 |
4776 | && h->size != isym->st_size | |
4777 | && ! size_change_ok) | |
4eca0228 | 4778 | _bfd_error_handler |
695344c0 | 4779 | /* xgettext:c-format */ |
d003868e | 4780 | (_("Warning: size of symbol `%s' changed" |
76cfced5 AM |
4781 | " from %Lu in %B to %Lu in %B"), |
4782 | name, h->size, old_bfd, isym->st_size, abfd); | |
4ad4eba5 AM |
4783 | |
4784 | h->size = isym->st_size; | |
4785 | } | |
4786 | ||
4787 | /* If this is a common symbol, then we always want H->SIZE | |
4788 | to be the size of the common symbol. The code just above | |
4789 | won't fix the size if a common symbol becomes larger. We | |
4790 | don't warn about a size change here, because that is | |
4f3fedcf | 4791 | covered by --warn-common. Allow changes between different |
fcb93ecf | 4792 | function types. */ |
4ad4eba5 AM |
4793 | if (h->root.type == bfd_link_hash_common) |
4794 | h->size = h->root.u.c.size; | |
4795 | ||
4796 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
37a9e49a L |
4797 | && ((definition && !new_weak) |
4798 | || (old_weak && h->root.type == bfd_link_hash_common) | |
4799 | || h->type == STT_NOTYPE)) | |
4ad4eba5 | 4800 | { |
2955ec4c L |
4801 | unsigned int type = ELF_ST_TYPE (isym->st_info); |
4802 | ||
4803 | /* Turn an IFUNC symbol from a DSO into a normal FUNC | |
4804 | symbol. */ | |
4805 | if (type == STT_GNU_IFUNC | |
4806 | && (abfd->flags & DYNAMIC) != 0) | |
4807 | type = STT_FUNC; | |
4ad4eba5 | 4808 | |
2955ec4c L |
4809 | if (h->type != type) |
4810 | { | |
4811 | if (h->type != STT_NOTYPE && ! type_change_ok) | |
695344c0 | 4812 | /* xgettext:c-format */ |
4eca0228 | 4813 | _bfd_error_handler |
2955ec4c L |
4814 | (_("Warning: type of symbol `%s' changed" |
4815 | " from %d to %d in %B"), | |
c08bb8dd | 4816 | name, h->type, type, abfd); |
2955ec4c L |
4817 | |
4818 | h->type = type; | |
4819 | } | |
4ad4eba5 AM |
4820 | } |
4821 | ||
54ac0771 | 4822 | /* Merge st_other field. */ |
b8417128 | 4823 | elf_merge_st_other (abfd, h, isym, sec, definition, dynamic); |
4ad4eba5 | 4824 | |
c3df8c14 | 4825 | /* We don't want to make debug symbol dynamic. */ |
0e1862bb L |
4826 | if (definition |
4827 | && (sec->flags & SEC_DEBUGGING) | |
4828 | && !bfd_link_relocatable (info)) | |
c3df8c14 AM |
4829 | dynsym = FALSE; |
4830 | ||
4f3fedcf AM |
4831 | /* Nor should we make plugin symbols dynamic. */ |
4832 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4833 | dynsym = FALSE; | |
4834 | ||
35fc36a8 | 4835 | if (definition) |
35399224 L |
4836 | { |
4837 | h->target_internal = isym->st_target_internal; | |
4838 | h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; | |
4839 | } | |
35fc36a8 | 4840 | |
4ad4eba5 AM |
4841 | if (definition && !dynamic) |
4842 | { | |
4843 | char *p = strchr (name, ELF_VER_CHR); | |
4844 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4845 | { | |
4846 | /* Queue non-default versions so that .symver x, x@FOO | |
4847 | aliases can be checked. */ | |
66eb6687 | 4848 | if (!nondeflt_vers) |
4ad4eba5 | 4849 | { |
66eb6687 AM |
4850 | amt = ((isymend - isym + 1) |
4851 | * sizeof (struct elf_link_hash_entry *)); | |
ca4be51c AM |
4852 | nondeflt_vers |
4853 | = (struct elf_link_hash_entry **) bfd_malloc (amt); | |
14b1c01e AM |
4854 | if (!nondeflt_vers) |
4855 | goto error_free_vers; | |
4ad4eba5 | 4856 | } |
66eb6687 | 4857 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4858 | } |
4859 | } | |
4860 | ||
4861 | if (dynsym && h->dynindx == -1) | |
4862 | { | |
c152c796 | 4863 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4864 | goto error_free_vers; |
60d67dc8 AM |
4865 | if (h->is_weakalias |
4866 | && weakdef (h)->dynindx == -1) | |
4ad4eba5 | 4867 | { |
60d67dc8 | 4868 | if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h))) |
4ad4eba5 AM |
4869 | goto error_free_vers; |
4870 | } | |
4871 | } | |
1f599d0e | 4872 | else if (h->dynindx != -1) |
4ad4eba5 AM |
4873 | /* If the symbol already has a dynamic index, but |
4874 | visibility says it should not be visible, turn it into | |
4875 | a local symbol. */ | |
4876 | switch (ELF_ST_VISIBILITY (h->other)) | |
4877 | { | |
4878 | case STV_INTERNAL: | |
4879 | case STV_HIDDEN: | |
4880 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4881 | dynsym = FALSE; | |
4882 | break; | |
4883 | } | |
4884 | ||
aef28989 L |
4885 | /* Don't add DT_NEEDED for references from the dummy bfd nor |
4886 | for unmatched symbol. */ | |
4ad4eba5 | 4887 | if (!add_needed |
aef28989 | 4888 | && matched |
4ad4eba5 | 4889 | && definition |
010e5ae2 | 4890 | && ((dynsym |
ffa9430d | 4891 | && h->ref_regular_nonweak |
4f3fedcf AM |
4892 | && (old_bfd == NULL |
4893 | || (old_bfd->flags & BFD_PLUGIN) == 0)) | |
ffa9430d | 4894 | || (h->ref_dynamic_nonweak |
010e5ae2 | 4895 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 |
7b15fa7a AM |
4896 | && !on_needed_list (elf_dt_name (abfd), |
4897 | htab->needed, NULL)))) | |
4ad4eba5 AM |
4898 | { |
4899 | int ret; | |
4900 | const char *soname = elf_dt_name (abfd); | |
4901 | ||
16e4ecc0 AM |
4902 | info->callbacks->minfo ("%!", soname, old_bfd, |
4903 | h->root.root.string); | |
4904 | ||
4ad4eba5 AM |
4905 | /* A symbol from a library loaded via DT_NEEDED of some |
4906 | other library is referenced by a regular object. | |
e56f61be | 4907 | Add a DT_NEEDED entry for it. Issue an error if |
b918acf9 NC |
4908 | --no-add-needed is used and the reference was not |
4909 | a weak one. */ | |
4f3fedcf | 4910 | if (old_bfd != NULL |
b918acf9 | 4911 | && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) |
e56f61be | 4912 | { |
4eca0228 | 4913 | _bfd_error_handler |
695344c0 | 4914 | /* xgettext:c-format */ |
3cbc5de0 | 4915 | (_("%B: undefined reference to symbol '%s'"), |
4f3fedcf | 4916 | old_bfd, name); |
ff5ac77b | 4917 | bfd_set_error (bfd_error_missing_dso); |
e56f61be L |
4918 | goto error_free_vers; |
4919 | } | |
4920 | ||
a50b1753 | 4921 | elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) |
ca4be51c | 4922 | (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); |
a5db907e | 4923 | |
4ad4eba5 | 4924 | add_needed = TRUE; |
7e9f0867 | 4925 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4926 | if (ret < 0) |
4927 | goto error_free_vers; | |
4928 | ||
4929 | BFD_ASSERT (ret == 0); | |
4930 | } | |
4931 | } | |
4932 | } | |
4933 | ||
66eb6687 AM |
4934 | if (extversym != NULL) |
4935 | { | |
4936 | free (extversym); | |
4937 | extversym = NULL; | |
4938 | } | |
4939 | ||
4940 | if (isymbuf != NULL) | |
4941 | { | |
4942 | free (isymbuf); | |
4943 | isymbuf = NULL; | |
4944 | } | |
4945 | ||
4946 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4947 | { | |
4948 | unsigned int i; | |
4949 | ||
4950 | /* Restore the symbol table. */ | |
f45794cb AM |
4951 | old_ent = (char *) old_tab + tabsize; |
4952 | memset (elf_sym_hashes (abfd), 0, | |
4953 | extsymcount * sizeof (struct elf_link_hash_entry *)); | |
4f87808c AM |
4954 | htab->root.table.table = old_table; |
4955 | htab->root.table.size = old_size; | |
4956 | htab->root.table.count = old_count; | |
66eb6687 | 4957 | memcpy (htab->root.table.table, old_tab, tabsize); |
66eb6687 AM |
4958 | htab->root.undefs = old_undefs; |
4959 | htab->root.undefs_tail = old_undefs_tail; | |
5b677558 AM |
4960 | _bfd_elf_strtab_restore (htab->dynstr, old_strtab); |
4961 | free (old_strtab); | |
4962 | old_strtab = NULL; | |
66eb6687 AM |
4963 | for (i = 0; i < htab->root.table.size; i++) |
4964 | { | |
4965 | struct bfd_hash_entry *p; | |
4966 | struct elf_link_hash_entry *h; | |
3e0882af L |
4967 | bfd_size_type size; |
4968 | unsigned int alignment_power; | |
4070765b | 4969 | unsigned int non_ir_ref_dynamic; |
66eb6687 AM |
4970 | |
4971 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4972 | { | |
4973 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
4974 | if (h->root.type == bfd_link_hash_warning) |
4975 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2de92251 | 4976 | |
3e0882af L |
4977 | /* Preserve the maximum alignment and size for common |
4978 | symbols even if this dynamic lib isn't on DT_NEEDED | |
a4542f1b | 4979 | since it can still be loaded at run time by another |
3e0882af L |
4980 | dynamic lib. */ |
4981 | if (h->root.type == bfd_link_hash_common) | |
4982 | { | |
4983 | size = h->root.u.c.size; | |
4984 | alignment_power = h->root.u.c.p->alignment_power; | |
4985 | } | |
4986 | else | |
4987 | { | |
4988 | size = 0; | |
4989 | alignment_power = 0; | |
4990 | } | |
4070765b | 4991 | /* Preserve non_ir_ref_dynamic so that this symbol |
59fa66c5 L |
4992 | will be exported when the dynamic lib becomes needed |
4993 | in the second pass. */ | |
4070765b | 4994 | non_ir_ref_dynamic = h->root.non_ir_ref_dynamic; |
66eb6687 AM |
4995 | memcpy (p, old_ent, htab->root.table.entsize); |
4996 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4997 | h = (struct elf_link_hash_entry *) p; |
4998 | if (h->root.type == bfd_link_hash_warning) | |
4999 | { | |
5000 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
5001 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
a4542f1b | 5002 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
2de92251 | 5003 | } |
a4542f1b | 5004 | if (h->root.type == bfd_link_hash_common) |
3e0882af L |
5005 | { |
5006 | if (size > h->root.u.c.size) | |
5007 | h->root.u.c.size = size; | |
5008 | if (alignment_power > h->root.u.c.p->alignment_power) | |
5009 | h->root.u.c.p->alignment_power = alignment_power; | |
5010 | } | |
4070765b | 5011 | h->root.non_ir_ref_dynamic = non_ir_ref_dynamic; |
66eb6687 AM |
5012 | } |
5013 | } | |
5014 | ||
5061a885 AM |
5015 | /* Make a special call to the linker "notice" function to |
5016 | tell it that symbols added for crefs may need to be removed. */ | |
e5034e59 | 5017 | if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) |
9af2a943 | 5018 | goto error_free_vers; |
5061a885 | 5019 | |
66eb6687 AM |
5020 | free (old_tab); |
5021 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
5022 | alloc_mark); | |
5023 | if (nondeflt_vers != NULL) | |
5024 | free (nondeflt_vers); | |
5025 | return TRUE; | |
5026 | } | |
2de92251 | 5027 | |
66eb6687 AM |
5028 | if (old_tab != NULL) |
5029 | { | |
e5034e59 | 5030 | if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) |
9af2a943 | 5031 | goto error_free_vers; |
66eb6687 AM |
5032 | free (old_tab); |
5033 | old_tab = NULL; | |
5034 | } | |
5035 | ||
c6e8a9a8 L |
5036 | /* Now that all the symbols from this input file are created, if |
5037 | not performing a relocatable link, handle .symver foo, foo@BAR | |
5038 | such that any relocs against foo become foo@BAR. */ | |
0e1862bb | 5039 | if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) |
4ad4eba5 | 5040 | { |
ef53be89 | 5041 | size_t cnt, symidx; |
4ad4eba5 AM |
5042 | |
5043 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
5044 | { | |
5045 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
5046 | char *shortname, *p; | |
5047 | ||
5048 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
5049 | if (p == NULL | |
5050 | || (h->root.type != bfd_link_hash_defined | |
5051 | && h->root.type != bfd_link_hash_defweak)) | |
5052 | continue; | |
5053 | ||
5054 | amt = p - h->root.root.string; | |
a50b1753 | 5055 | shortname = (char *) bfd_malloc (amt + 1); |
14b1c01e AM |
5056 | if (!shortname) |
5057 | goto error_free_vers; | |
4ad4eba5 AM |
5058 | memcpy (shortname, h->root.root.string, amt); |
5059 | shortname[amt] = '\0'; | |
5060 | ||
5061 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 5062 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
5063 | FALSE, FALSE, FALSE); |
5064 | if (hi != NULL | |
5065 | && hi->root.type == h->root.type | |
5066 | && hi->root.u.def.value == h->root.u.def.value | |
5067 | && hi->root.u.def.section == h->root.u.def.section) | |
5068 | { | |
5069 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
5070 | hi->root.type = bfd_link_hash_indirect; | |
5071 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 5072 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
5073 | sym_hash = elf_sym_hashes (abfd); |
5074 | if (sym_hash) | |
5075 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
5076 | if (sym_hash[symidx] == hi) | |
5077 | { | |
5078 | sym_hash[symidx] = h; | |
5079 | break; | |
5080 | } | |
5081 | } | |
5082 | free (shortname); | |
5083 | } | |
5084 | free (nondeflt_vers); | |
5085 | nondeflt_vers = NULL; | |
5086 | } | |
5087 | ||
60d67dc8 | 5088 | /* Now set the alias field correctly for all the weak defined |
4ad4eba5 AM |
5089 | symbols we found. The only way to do this is to search all the |
5090 | symbols. Since we only need the information for non functions in | |
5091 | dynamic objects, that's the only time we actually put anything on | |
5092 | the list WEAKS. We need this information so that if a regular | |
5093 | object refers to a symbol defined weakly in a dynamic object, the | |
5094 | real symbol in the dynamic object is also put in the dynamic | |
5095 | symbols; we also must arrange for both symbols to point to the | |
5096 | same memory location. We could handle the general case of symbol | |
5097 | aliasing, but a general symbol alias can only be generated in | |
5098 | assembler code, handling it correctly would be very time | |
5099 | consuming, and other ELF linkers don't handle general aliasing | |
5100 | either. */ | |
5101 | if (weaks != NULL) | |
5102 | { | |
5103 | struct elf_link_hash_entry **hpp; | |
5104 | struct elf_link_hash_entry **hppend; | |
5105 | struct elf_link_hash_entry **sorted_sym_hash; | |
5106 | struct elf_link_hash_entry *h; | |
5107 | size_t sym_count; | |
5108 | ||
5109 | /* Since we have to search the whole symbol list for each weak | |
5110 | defined symbol, search time for N weak defined symbols will be | |
5111 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
ef53be89 AM |
5112 | amt = extsymcount; |
5113 | amt *= sizeof (struct elf_link_hash_entry *); | |
a50b1753 | 5114 | sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); |
4ad4eba5 AM |
5115 | if (sorted_sym_hash == NULL) |
5116 | goto error_return; | |
5117 | sym_hash = sorted_sym_hash; | |
5118 | hpp = elf_sym_hashes (abfd); | |
5119 | hppend = hpp + extsymcount; | |
5120 | sym_count = 0; | |
5121 | for (; hpp < hppend; hpp++) | |
5122 | { | |
5123 | h = *hpp; | |
5124 | if (h != NULL | |
5125 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 5126 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
5127 | { |
5128 | *sym_hash = h; | |
5129 | sym_hash++; | |
5130 | sym_count++; | |
5131 | } | |
5132 | } | |
5133 | ||
5134 | qsort (sorted_sym_hash, sym_count, | |
5135 | sizeof (struct elf_link_hash_entry *), | |
5136 | elf_sort_symbol); | |
5137 | ||
5138 | while (weaks != NULL) | |
5139 | { | |
5140 | struct elf_link_hash_entry *hlook; | |
5141 | asection *slook; | |
5142 | bfd_vma vlook; | |
ed54588d | 5143 | size_t i, j, idx = 0; |
4ad4eba5 AM |
5144 | |
5145 | hlook = weaks; | |
60d67dc8 AM |
5146 | weaks = hlook->u.alias; |
5147 | hlook->u.alias = NULL; | |
4ad4eba5 | 5148 | |
e3e53eed AM |
5149 | if (hlook->root.type != bfd_link_hash_defined |
5150 | && hlook->root.type != bfd_link_hash_defweak) | |
5151 | continue; | |
5152 | ||
4ad4eba5 AM |
5153 | slook = hlook->root.u.def.section; |
5154 | vlook = hlook->root.u.def.value; | |
5155 | ||
4ad4eba5 AM |
5156 | i = 0; |
5157 | j = sym_count; | |
14160578 | 5158 | while (i != j) |
4ad4eba5 AM |
5159 | { |
5160 | bfd_signed_vma vdiff; | |
5161 | idx = (i + j) / 2; | |
14160578 | 5162 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5163 | vdiff = vlook - h->root.u.def.value; |
5164 | if (vdiff < 0) | |
5165 | j = idx; | |
5166 | else if (vdiff > 0) | |
5167 | i = idx + 1; | |
5168 | else | |
5169 | { | |
d3435ae8 | 5170 | int sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
5171 | if (sdiff < 0) |
5172 | j = idx; | |
5173 | else if (sdiff > 0) | |
5174 | i = idx + 1; | |
5175 | else | |
14160578 | 5176 | break; |
4ad4eba5 AM |
5177 | } |
5178 | } | |
5179 | ||
5180 | /* We didn't find a value/section match. */ | |
14160578 | 5181 | if (i == j) |
4ad4eba5 AM |
5182 | continue; |
5183 | ||
14160578 AM |
5184 | /* With multiple aliases, or when the weak symbol is already |
5185 | strongly defined, we have multiple matching symbols and | |
5186 | the binary search above may land on any of them. Step | |
5187 | one past the matching symbol(s). */ | |
5188 | while (++idx != j) | |
5189 | { | |
5190 | h = sorted_sym_hash[idx]; | |
5191 | if (h->root.u.def.section != slook | |
5192 | || h->root.u.def.value != vlook) | |
5193 | break; | |
5194 | } | |
5195 | ||
5196 | /* Now look back over the aliases. Since we sorted by size | |
5197 | as well as value and section, we'll choose the one with | |
5198 | the largest size. */ | |
5199 | while (idx-- != i) | |
4ad4eba5 | 5200 | { |
14160578 | 5201 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5202 | |
5203 | /* Stop if value or section doesn't match. */ | |
14160578 AM |
5204 | if (h->root.u.def.section != slook |
5205 | || h->root.u.def.value != vlook) | |
4ad4eba5 AM |
5206 | break; |
5207 | else if (h != hlook) | |
5208 | { | |
60d67dc8 AM |
5209 | struct elf_link_hash_entry *t; |
5210 | ||
5211 | hlook->u.alias = h; | |
5212 | hlook->is_weakalias = 1; | |
5213 | t = h; | |
5214 | if (t->u.alias != NULL) | |
5215 | while (t->u.alias != h) | |
5216 | t = t->u.alias; | |
5217 | t->u.alias = hlook; | |
4ad4eba5 AM |
5218 | |
5219 | /* If the weak definition is in the list of dynamic | |
5220 | symbols, make sure the real definition is put | |
5221 | there as well. */ | |
5222 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
5223 | { | |
c152c796 | 5224 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4dd07732 AM |
5225 | { |
5226 | err_free_sym_hash: | |
5227 | free (sorted_sym_hash); | |
5228 | goto error_return; | |
5229 | } | |
4ad4eba5 AM |
5230 | } |
5231 | ||
5232 | /* If the real definition is in the list of dynamic | |
5233 | symbols, make sure the weak definition is put | |
5234 | there as well. If we don't do this, then the | |
5235 | dynamic loader might not merge the entries for the | |
5236 | real definition and the weak definition. */ | |
5237 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
5238 | { | |
c152c796 | 5239 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4dd07732 | 5240 | goto err_free_sym_hash; |
4ad4eba5 AM |
5241 | } |
5242 | break; | |
5243 | } | |
5244 | } | |
5245 | } | |
5246 | ||
5247 | free (sorted_sym_hash); | |
5248 | } | |
5249 | ||
33177bb1 AM |
5250 | if (bed->check_directives |
5251 | && !(*bed->check_directives) (abfd, info)) | |
5252 | return FALSE; | |
85fbca6a | 5253 | |
4ad4eba5 AM |
5254 | /* If this is a non-traditional link, try to optimize the handling |
5255 | of the .stab/.stabstr sections. */ | |
5256 | if (! dynamic | |
5257 | && ! info->traditional_format | |
66eb6687 | 5258 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
5259 | && (info->strip != strip_all && info->strip != strip_debugger)) |
5260 | { | |
5261 | asection *stabstr; | |
5262 | ||
5263 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
5264 | if (stabstr != NULL) | |
5265 | { | |
5266 | bfd_size_type string_offset = 0; | |
5267 | asection *stab; | |
5268 | ||
5269 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 5270 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
5271 | && (!stab->name[5] || |
5272 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
5273 | && (stab->flags & SEC_MERGE) == 0 | |
5274 | && !bfd_is_abs_section (stab->output_section)) | |
5275 | { | |
5276 | struct bfd_elf_section_data *secdata; | |
5277 | ||
5278 | secdata = elf_section_data (stab); | |
66eb6687 AM |
5279 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
5280 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
5281 | &string_offset)) |
5282 | goto error_return; | |
5283 | if (secdata->sec_info) | |
dbaa2011 | 5284 | stab->sec_info_type = SEC_INFO_TYPE_STABS; |
4ad4eba5 AM |
5285 | } |
5286 | } | |
5287 | } | |
5288 | ||
66eb6687 | 5289 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
5290 | { |
5291 | /* Add this bfd to the loaded list. */ | |
5292 | struct elf_link_loaded_list *n; | |
5293 | ||
ca4be51c | 5294 | n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); |
4ad4eba5 AM |
5295 | if (n == NULL) |
5296 | goto error_return; | |
5297 | n->abfd = abfd; | |
66eb6687 AM |
5298 | n->next = htab->loaded; |
5299 | htab->loaded = n; | |
4ad4eba5 AM |
5300 | } |
5301 | ||
5302 | return TRUE; | |
5303 | ||
5304 | error_free_vers: | |
66eb6687 AM |
5305 | if (old_tab != NULL) |
5306 | free (old_tab); | |
5b677558 AM |
5307 | if (old_strtab != NULL) |
5308 | free (old_strtab); | |
4ad4eba5 AM |
5309 | if (nondeflt_vers != NULL) |
5310 | free (nondeflt_vers); | |
5311 | if (extversym != NULL) | |
5312 | free (extversym); | |
5313 | error_free_sym: | |
5314 | if (isymbuf != NULL) | |
5315 | free (isymbuf); | |
5316 | error_return: | |
5317 | return FALSE; | |
5318 | } | |
5319 | ||
8387904d AM |
5320 | /* Return the linker hash table entry of a symbol that might be |
5321 | satisfied by an archive symbol. Return -1 on error. */ | |
5322 | ||
5323 | struct elf_link_hash_entry * | |
5324 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
5325 | struct bfd_link_info *info, | |
5326 | const char *name) | |
5327 | { | |
5328 | struct elf_link_hash_entry *h; | |
5329 | char *p, *copy; | |
5330 | size_t len, first; | |
5331 | ||
2a41f396 | 5332 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); |
8387904d AM |
5333 | if (h != NULL) |
5334 | return h; | |
5335 | ||
5336 | /* If this is a default version (the name contains @@), look up the | |
5337 | symbol again with only one `@' as well as without the version. | |
5338 | The effect is that references to the symbol with and without the | |
5339 | version will be matched by the default symbol in the archive. */ | |
5340 | ||
5341 | p = strchr (name, ELF_VER_CHR); | |
5342 | if (p == NULL || p[1] != ELF_VER_CHR) | |
5343 | return h; | |
5344 | ||
5345 | /* First check with only one `@'. */ | |
5346 | len = strlen (name); | |
a50b1753 | 5347 | copy = (char *) bfd_alloc (abfd, len); |
8387904d AM |
5348 | if (copy == NULL) |
5349 | return (struct elf_link_hash_entry *) 0 - 1; | |
5350 | ||
5351 | first = p - name + 1; | |
5352 | memcpy (copy, name, first); | |
5353 | memcpy (copy + first, name + first + 1, len - first); | |
5354 | ||
2a41f396 | 5355 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); |
8387904d AM |
5356 | if (h == NULL) |
5357 | { | |
5358 | /* We also need to check references to the symbol without the | |
5359 | version. */ | |
5360 | copy[first - 1] = '\0'; | |
5361 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
2a41f396 | 5362 | FALSE, FALSE, TRUE); |
8387904d AM |
5363 | } |
5364 | ||
5365 | bfd_release (abfd, copy); | |
5366 | return h; | |
5367 | } | |
5368 | ||
0ad989f9 | 5369 | /* Add symbols from an ELF archive file to the linker hash table. We |
13e570f8 AM |
5370 | don't use _bfd_generic_link_add_archive_symbols because we need to |
5371 | handle versioned symbols. | |
0ad989f9 L |
5372 | |
5373 | Fortunately, ELF archive handling is simpler than that done by | |
5374 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
5375 | oddities. In ELF, if we find a symbol in the archive map, and the | |
5376 | symbol is currently undefined, we know that we must pull in that | |
5377 | object file. | |
5378 | ||
5379 | Unfortunately, we do have to make multiple passes over the symbol | |
5380 | table until nothing further is resolved. */ | |
5381 | ||
4ad4eba5 AM |
5382 | static bfd_boolean |
5383 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
5384 | { |
5385 | symindex c; | |
13e570f8 | 5386 | unsigned char *included = NULL; |
0ad989f9 L |
5387 | carsym *symdefs; |
5388 | bfd_boolean loop; | |
5389 | bfd_size_type amt; | |
8387904d AM |
5390 | const struct elf_backend_data *bed; |
5391 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
5392 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
5393 | |
5394 | if (! bfd_has_map (abfd)) | |
5395 | { | |
5396 | /* An empty archive is a special case. */ | |
5397 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
5398 | return TRUE; | |
5399 | bfd_set_error (bfd_error_no_armap); | |
5400 | return FALSE; | |
5401 | } | |
5402 | ||
5403 | /* Keep track of all symbols we know to be already defined, and all | |
5404 | files we know to be already included. This is to speed up the | |
5405 | second and subsequent passes. */ | |
5406 | c = bfd_ardata (abfd)->symdef_count; | |
5407 | if (c == 0) | |
5408 | return TRUE; | |
5409 | amt = c; | |
13e570f8 AM |
5410 | amt *= sizeof (*included); |
5411 | included = (unsigned char *) bfd_zmalloc (amt); | |
5412 | if (included == NULL) | |
5413 | return FALSE; | |
0ad989f9 L |
5414 | |
5415 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
5416 | bed = get_elf_backend_data (abfd); |
5417 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
5418 | |
5419 | do | |
5420 | { | |
5421 | file_ptr last; | |
5422 | symindex i; | |
5423 | carsym *symdef; | |
5424 | carsym *symdefend; | |
5425 | ||
5426 | loop = FALSE; | |
5427 | last = -1; | |
5428 | ||
5429 | symdef = symdefs; | |
5430 | symdefend = symdef + c; | |
5431 | for (i = 0; symdef < symdefend; symdef++, i++) | |
5432 | { | |
5433 | struct elf_link_hash_entry *h; | |
5434 | bfd *element; | |
5435 | struct bfd_link_hash_entry *undefs_tail; | |
5436 | symindex mark; | |
5437 | ||
13e570f8 | 5438 | if (included[i]) |
0ad989f9 L |
5439 | continue; |
5440 | if (symdef->file_offset == last) | |
5441 | { | |
5442 | included[i] = TRUE; | |
5443 | continue; | |
5444 | } | |
5445 | ||
8387904d AM |
5446 | h = archive_symbol_lookup (abfd, info, symdef->name); |
5447 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
5448 | goto error_return; | |
0ad989f9 L |
5449 | |
5450 | if (h == NULL) | |
5451 | continue; | |
5452 | ||
5453 | if (h->root.type == bfd_link_hash_common) | |
5454 | { | |
5455 | /* We currently have a common symbol. The archive map contains | |
5456 | a reference to this symbol, so we may want to include it. We | |
5457 | only want to include it however, if this archive element | |
5458 | contains a definition of the symbol, not just another common | |
5459 | declaration of it. | |
5460 | ||
5461 | Unfortunately some archivers (including GNU ar) will put | |
5462 | declarations of common symbols into their archive maps, as | |
5463 | well as real definitions, so we cannot just go by the archive | |
5464 | map alone. Instead we must read in the element's symbol | |
5465 | table and check that to see what kind of symbol definition | |
5466 | this is. */ | |
5467 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
5468 | continue; | |
5469 | } | |
5470 | else if (h->root.type != bfd_link_hash_undefined) | |
5471 | { | |
5472 | if (h->root.type != bfd_link_hash_undefweak) | |
13e570f8 AM |
5473 | /* Symbol must be defined. Don't check it again. */ |
5474 | included[i] = TRUE; | |
0ad989f9 L |
5475 | continue; |
5476 | } | |
5477 | ||
5478 | /* We need to include this archive member. */ | |
5479 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
5480 | if (element == NULL) | |
5481 | goto error_return; | |
5482 | ||
5483 | if (! bfd_check_format (element, bfd_object)) | |
5484 | goto error_return; | |
5485 | ||
0ad989f9 L |
5486 | undefs_tail = info->hash->undefs_tail; |
5487 | ||
0e144ba7 AM |
5488 | if (!(*info->callbacks |
5489 | ->add_archive_element) (info, element, symdef->name, &element)) | |
b95a0a31 | 5490 | continue; |
0e144ba7 | 5491 | if (!bfd_link_add_symbols (element, info)) |
0ad989f9 L |
5492 | goto error_return; |
5493 | ||
5494 | /* If there are any new undefined symbols, we need to make | |
5495 | another pass through the archive in order to see whether | |
5496 | they can be defined. FIXME: This isn't perfect, because | |
5497 | common symbols wind up on undefs_tail and because an | |
5498 | undefined symbol which is defined later on in this pass | |
5499 | does not require another pass. This isn't a bug, but it | |
5500 | does make the code less efficient than it could be. */ | |
5501 | if (undefs_tail != info->hash->undefs_tail) | |
5502 | loop = TRUE; | |
5503 | ||
5504 | /* Look backward to mark all symbols from this object file | |
5505 | which we have already seen in this pass. */ | |
5506 | mark = i; | |
5507 | do | |
5508 | { | |
5509 | included[mark] = TRUE; | |
5510 | if (mark == 0) | |
5511 | break; | |
5512 | --mark; | |
5513 | } | |
5514 | while (symdefs[mark].file_offset == symdef->file_offset); | |
5515 | ||
5516 | /* We mark subsequent symbols from this object file as we go | |
5517 | on through the loop. */ | |
5518 | last = symdef->file_offset; | |
5519 | } | |
5520 | } | |
5521 | while (loop); | |
5522 | ||
0ad989f9 L |
5523 | free (included); |
5524 | ||
5525 | return TRUE; | |
5526 | ||
5527 | error_return: | |
0ad989f9 L |
5528 | if (included != NULL) |
5529 | free (included); | |
5530 | return FALSE; | |
5531 | } | |
4ad4eba5 AM |
5532 | |
5533 | /* Given an ELF BFD, add symbols to the global hash table as | |
5534 | appropriate. */ | |
5535 | ||
5536 | bfd_boolean | |
5537 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
5538 | { | |
5539 | switch (bfd_get_format (abfd)) | |
5540 | { | |
5541 | case bfd_object: | |
5542 | return elf_link_add_object_symbols (abfd, info); | |
5543 | case bfd_archive: | |
5544 | return elf_link_add_archive_symbols (abfd, info); | |
5545 | default: | |
5546 | bfd_set_error (bfd_error_wrong_format); | |
5547 | return FALSE; | |
5548 | } | |
5549 | } | |
5a580b3a | 5550 | \f |
14b1c01e AM |
5551 | struct hash_codes_info |
5552 | { | |
5553 | unsigned long *hashcodes; | |
5554 | bfd_boolean error; | |
5555 | }; | |
a0c8462f | 5556 | |
5a580b3a AM |
5557 | /* This function will be called though elf_link_hash_traverse to store |
5558 | all hash value of the exported symbols in an array. */ | |
5559 | ||
5560 | static bfd_boolean | |
5561 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5562 | { | |
a50b1753 | 5563 | struct hash_codes_info *inf = (struct hash_codes_info *) data; |
5a580b3a | 5564 | const char *name; |
5a580b3a AM |
5565 | unsigned long ha; |
5566 | char *alc = NULL; | |
5567 | ||
5a580b3a AM |
5568 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5569 | if (h->dynindx == -1) | |
5570 | return TRUE; | |
5571 | ||
5572 | name = h->root.root.string; | |
422f1182 | 5573 | if (h->versioned >= versioned) |
5a580b3a | 5574 | { |
422f1182 L |
5575 | char *p = strchr (name, ELF_VER_CHR); |
5576 | if (p != NULL) | |
14b1c01e | 5577 | { |
422f1182 L |
5578 | alc = (char *) bfd_malloc (p - name + 1); |
5579 | if (alc == NULL) | |
5580 | { | |
5581 | inf->error = TRUE; | |
5582 | return FALSE; | |
5583 | } | |
5584 | memcpy (alc, name, p - name); | |
5585 | alc[p - name] = '\0'; | |
5586 | name = alc; | |
14b1c01e | 5587 | } |
5a580b3a AM |
5588 | } |
5589 | ||
5590 | /* Compute the hash value. */ | |
5591 | ha = bfd_elf_hash (name); | |
5592 | ||
5593 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 5594 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
5595 | |
5596 | /* And store it in the struct so that we can put it in the hash table | |
5597 | later. */ | |
f6e332e6 | 5598 | h->u.elf_hash_value = ha; |
5a580b3a AM |
5599 | |
5600 | if (alc != NULL) | |
5601 | free (alc); | |
5602 | ||
5603 | return TRUE; | |
5604 | } | |
5605 | ||
fdc90cb4 JJ |
5606 | struct collect_gnu_hash_codes |
5607 | { | |
5608 | bfd *output_bfd; | |
5609 | const struct elf_backend_data *bed; | |
5610 | unsigned long int nsyms; | |
5611 | unsigned long int maskbits; | |
5612 | unsigned long int *hashcodes; | |
5613 | unsigned long int *hashval; | |
5614 | unsigned long int *indx; | |
5615 | unsigned long int *counts; | |
5616 | bfd_vma *bitmask; | |
5617 | bfd_byte *contents; | |
5618 | long int min_dynindx; | |
5619 | unsigned long int bucketcount; | |
5620 | unsigned long int symindx; | |
5621 | long int local_indx; | |
5622 | long int shift1, shift2; | |
5623 | unsigned long int mask; | |
14b1c01e | 5624 | bfd_boolean error; |
fdc90cb4 JJ |
5625 | }; |
5626 | ||
5627 | /* This function will be called though elf_link_hash_traverse to store | |
5628 | all hash value of the exported symbols in an array. */ | |
5629 | ||
5630 | static bfd_boolean | |
5631 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5632 | { | |
a50b1753 | 5633 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 | 5634 | const char *name; |
fdc90cb4 JJ |
5635 | unsigned long ha; |
5636 | char *alc = NULL; | |
5637 | ||
fdc90cb4 JJ |
5638 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5639 | if (h->dynindx == -1) | |
5640 | return TRUE; | |
5641 | ||
5642 | /* Ignore also local symbols and undefined symbols. */ | |
5643 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5644 | return TRUE; | |
5645 | ||
5646 | name = h->root.root.string; | |
422f1182 | 5647 | if (h->versioned >= versioned) |
fdc90cb4 | 5648 | { |
422f1182 L |
5649 | char *p = strchr (name, ELF_VER_CHR); |
5650 | if (p != NULL) | |
14b1c01e | 5651 | { |
422f1182 L |
5652 | alc = (char *) bfd_malloc (p - name + 1); |
5653 | if (alc == NULL) | |
5654 | { | |
5655 | s->error = TRUE; | |
5656 | return FALSE; | |
5657 | } | |
5658 | memcpy (alc, name, p - name); | |
5659 | alc[p - name] = '\0'; | |
5660 | name = alc; | |
14b1c01e | 5661 | } |
fdc90cb4 JJ |
5662 | } |
5663 | ||
5664 | /* Compute the hash value. */ | |
5665 | ha = bfd_elf_gnu_hash (name); | |
5666 | ||
5667 | /* Store the found hash value in the array for compute_bucket_count, | |
5668 | and also for .dynsym reordering purposes. */ | |
5669 | s->hashcodes[s->nsyms] = ha; | |
5670 | s->hashval[h->dynindx] = ha; | |
5671 | ++s->nsyms; | |
5672 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5673 | s->min_dynindx = h->dynindx; | |
5674 | ||
5675 | if (alc != NULL) | |
5676 | free (alc); | |
5677 | ||
5678 | return TRUE; | |
5679 | } | |
5680 | ||
5681 | /* This function will be called though elf_link_hash_traverse to do | |
5682 | final dynaminc symbol renumbering. */ | |
5683 | ||
5684 | static bfd_boolean | |
5685 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5686 | { | |
a50b1753 | 5687 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
5688 | unsigned long int bucket; |
5689 | unsigned long int val; | |
5690 | ||
fdc90cb4 JJ |
5691 | /* Ignore indirect symbols. */ |
5692 | if (h->dynindx == -1) | |
5693 | return TRUE; | |
5694 | ||
5695 | /* Ignore also local symbols and undefined symbols. */ | |
5696 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5697 | { | |
5698 | if (h->dynindx >= s->min_dynindx) | |
5699 | h->dynindx = s->local_indx++; | |
5700 | return TRUE; | |
5701 | } | |
5702 | ||
5703 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5704 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5705 | & ((s->maskbits >> s->shift1) - 1); | |
5706 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5707 | s->bitmask[val] | |
5708 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5709 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5710 | if (s->counts[bucket] == 1) | |
5711 | /* Last element terminates the chain. */ | |
5712 | val |= 1; | |
5713 | bfd_put_32 (s->output_bfd, val, | |
5714 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5715 | --s->counts[bucket]; | |
5716 | h->dynindx = s->indx[bucket]++; | |
5717 | return TRUE; | |
5718 | } | |
5719 | ||
5720 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5721 | ||
5722 | bfd_boolean | |
5723 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5724 | { | |
5725 | return !(h->forced_local | |
5726 | || h->root.type == bfd_link_hash_undefined | |
5727 | || h->root.type == bfd_link_hash_undefweak | |
5728 | || ((h->root.type == bfd_link_hash_defined | |
5729 | || h->root.type == bfd_link_hash_defweak) | |
5730 | && h->root.u.def.section->output_section == NULL)); | |
5731 | } | |
5732 | ||
5a580b3a AM |
5733 | /* Array used to determine the number of hash table buckets to use |
5734 | based on the number of symbols there are. If there are fewer than | |
5735 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5736 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5737 | than 32771 buckets. */ | |
5738 | ||
5739 | static const size_t elf_buckets[] = | |
5740 | { | |
5741 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5742 | 16411, 32771, 0 | |
5743 | }; | |
5744 | ||
5745 | /* Compute bucket count for hashing table. We do not use a static set | |
5746 | of possible tables sizes anymore. Instead we determine for all | |
5747 | possible reasonable sizes of the table the outcome (i.e., the | |
5748 | number of collisions etc) and choose the best solution. The | |
5749 | weighting functions are not too simple to allow the table to grow | |
5750 | without bounds. Instead one of the weighting factors is the size. | |
5751 | Therefore the result is always a good payoff between few collisions | |
5752 | (= short chain lengths) and table size. */ | |
5753 | static size_t | |
b20dd2ce | 5754 | compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
d40f3da9 AM |
5755 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, |
5756 | unsigned long int nsyms, | |
5757 | int gnu_hash) | |
5a580b3a | 5758 | { |
5a580b3a | 5759 | size_t best_size = 0; |
5a580b3a | 5760 | unsigned long int i; |
5a580b3a | 5761 | |
5a580b3a AM |
5762 | /* We have a problem here. The following code to optimize the table |
5763 | size requires an integer type with more the 32 bits. If | |
5764 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5765 | #ifdef BFD_HOST_U_64_BIT | |
5766 | if (info->optimize) | |
5767 | { | |
5a580b3a AM |
5768 | size_t minsize; |
5769 | size_t maxsize; | |
5770 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a | 5771 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 5772 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 5773 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 5774 | unsigned long int *counts; |
d40f3da9 | 5775 | bfd_size_type amt; |
0883b6e0 | 5776 | unsigned int no_improvement_count = 0; |
5a580b3a AM |
5777 | |
5778 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5779 | that the hashing table must at least have NSYMS/4 and at most | |
5780 | 2*NSYMS buckets. */ | |
5781 | minsize = nsyms / 4; | |
5782 | if (minsize == 0) | |
5783 | minsize = 1; | |
5784 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5785 | if (gnu_hash) |
5786 | { | |
5787 | if (minsize < 2) | |
5788 | minsize = 2; | |
5789 | if ((best_size & 31) == 0) | |
5790 | ++best_size; | |
5791 | } | |
5a580b3a AM |
5792 | |
5793 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5794 | since the size could be large. */ | |
5795 | amt = maxsize; | |
5796 | amt *= sizeof (unsigned long int); | |
a50b1753 | 5797 | counts = (unsigned long int *) bfd_malloc (amt); |
5a580b3a | 5798 | if (counts == NULL) |
fdc90cb4 | 5799 | return 0; |
5a580b3a AM |
5800 | |
5801 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5802 | minimal chain length. The minor criteria is (of course) the size | |
5803 | of the table. */ | |
5804 | for (i = minsize; i < maxsize; ++i) | |
5805 | { | |
5806 | /* Walk through the array of hashcodes and count the collisions. */ | |
5807 | BFD_HOST_U_64_BIT max; | |
5808 | unsigned long int j; | |
5809 | unsigned long int fact; | |
5810 | ||
fdc90cb4 JJ |
5811 | if (gnu_hash && (i & 31) == 0) |
5812 | continue; | |
5813 | ||
5a580b3a AM |
5814 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5815 | ||
5816 | /* Determine how often each hash bucket is used. */ | |
5817 | for (j = 0; j < nsyms; ++j) | |
5818 | ++counts[hashcodes[j] % i]; | |
5819 | ||
5820 | /* For the weight function we need some information about the | |
5821 | pagesize on the target. This is information need not be 100% | |
5822 | accurate. Since this information is not available (so far) we | |
5823 | define it here to a reasonable default value. If it is crucial | |
5824 | to have a better value some day simply define this value. */ | |
5825 | # ifndef BFD_TARGET_PAGESIZE | |
5826 | # define BFD_TARGET_PAGESIZE (4096) | |
5827 | # endif | |
5828 | ||
fdc90cb4 JJ |
5829 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5830 | and the chains. */ | |
5831 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
5832 | |
5833 | # if 1 | |
5834 | /* Variant 1: optimize for short chains. We add the squares | |
5835 | of all the chain lengths (which favors many small chain | |
5836 | over a few long chains). */ | |
5837 | for (j = 0; j < i; ++j) | |
5838 | max += counts[j] * counts[j]; | |
5839 | ||
5840 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 5841 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5842 | max *= fact * fact; |
5843 | # else | |
5844 | /* Variant 2: Optimize a lot more for small table. Here we | |
5845 | also add squares of the size but we also add penalties for | |
5846 | empty slots (the +1 term). */ | |
5847 | for (j = 0; j < i; ++j) | |
5848 | max += (1 + counts[j]) * (1 + counts[j]); | |
5849 | ||
5850 | /* The overall size of the table is considered, but not as | |
5851 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 5852 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5853 | max *= fact; |
5854 | # endif | |
5855 | ||
5856 | /* Compare with current best results. */ | |
5857 | if (max < best_chlen) | |
5858 | { | |
5859 | best_chlen = max; | |
5860 | best_size = i; | |
ca4be51c | 5861 | no_improvement_count = 0; |
5a580b3a | 5862 | } |
0883b6e0 NC |
5863 | /* PR 11843: Avoid futile long searches for the best bucket size |
5864 | when there are a large number of symbols. */ | |
5865 | else if (++no_improvement_count == 100) | |
5866 | break; | |
5a580b3a AM |
5867 | } |
5868 | ||
5869 | free (counts); | |
5870 | } | |
5871 | else | |
5872 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
5873 | { | |
5874 | /* This is the fallback solution if no 64bit type is available or if we | |
5875 | are not supposed to spend much time on optimizations. We select the | |
5876 | bucket count using a fixed set of numbers. */ | |
5877 | for (i = 0; elf_buckets[i] != 0; i++) | |
5878 | { | |
5879 | best_size = elf_buckets[i]; | |
fdc90cb4 | 5880 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
5881 | break; |
5882 | } | |
fdc90cb4 JJ |
5883 | if (gnu_hash && best_size < 2) |
5884 | best_size = 2; | |
5a580b3a AM |
5885 | } |
5886 | ||
5a580b3a AM |
5887 | return best_size; |
5888 | } | |
5889 | ||
d0bf826b AM |
5890 | /* Size any SHT_GROUP section for ld -r. */ |
5891 | ||
5892 | bfd_boolean | |
5893 | _bfd_elf_size_group_sections (struct bfd_link_info *info) | |
5894 | { | |
5895 | bfd *ibfd; | |
57963c05 | 5896 | asection *s; |
d0bf826b | 5897 | |
c72f2fb2 | 5898 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
d0bf826b | 5899 | if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour |
57963c05 AM |
5900 | && (s = ibfd->sections) != NULL |
5901 | && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS | |
d0bf826b AM |
5902 | && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) |
5903 | return FALSE; | |
5904 | return TRUE; | |
5905 | } | |
5906 | ||
04c3a755 NS |
5907 | /* Set a default stack segment size. The value in INFO wins. If it |
5908 | is unset, LEGACY_SYMBOL's value is used, and if that symbol is | |
5909 | undefined it is initialized. */ | |
5910 | ||
5911 | bfd_boolean | |
5912 | bfd_elf_stack_segment_size (bfd *output_bfd, | |
5913 | struct bfd_link_info *info, | |
5914 | const char *legacy_symbol, | |
5915 | bfd_vma default_size) | |
5916 | { | |
5917 | struct elf_link_hash_entry *h = NULL; | |
5918 | ||
5919 | /* Look for legacy symbol. */ | |
5920 | if (legacy_symbol) | |
5921 | h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, | |
5922 | FALSE, FALSE, FALSE); | |
5923 | if (h && (h->root.type == bfd_link_hash_defined | |
5924 | || h->root.type == bfd_link_hash_defweak) | |
5925 | && h->def_regular | |
5926 | && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) | |
5927 | { | |
5928 | /* The symbol has no type if specified on the command line. */ | |
5929 | h->type = STT_OBJECT; | |
5930 | if (info->stacksize) | |
695344c0 | 5931 | /* xgettext:c-format */ |
4eca0228 AM |
5932 | _bfd_error_handler (_("%B: stack size specified and %s set"), |
5933 | output_bfd, legacy_symbol); | |
04c3a755 | 5934 | else if (h->root.u.def.section != bfd_abs_section_ptr) |
695344c0 | 5935 | /* xgettext:c-format */ |
4eca0228 AM |
5936 | _bfd_error_handler (_("%B: %s not absolute"), |
5937 | output_bfd, legacy_symbol); | |
04c3a755 NS |
5938 | else |
5939 | info->stacksize = h->root.u.def.value; | |
5940 | } | |
5941 | ||
5942 | if (!info->stacksize) | |
5943 | /* If the user didn't set a size, or explicitly inhibit the | |
5944 | size, set it now. */ | |
5945 | info->stacksize = default_size; | |
5946 | ||
5947 | /* Provide the legacy symbol, if it is referenced. */ | |
5948 | if (h && (h->root.type == bfd_link_hash_undefined | |
5949 | || h->root.type == bfd_link_hash_undefweak)) | |
5950 | { | |
5951 | struct bfd_link_hash_entry *bh = NULL; | |
5952 | ||
5953 | if (!(_bfd_generic_link_add_one_symbol | |
5954 | (info, output_bfd, legacy_symbol, | |
5955 | BSF_GLOBAL, bfd_abs_section_ptr, | |
5956 | info->stacksize >= 0 ? info->stacksize : 0, | |
5957 | NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh))) | |
5958 | return FALSE; | |
5959 | ||
5960 | h = (struct elf_link_hash_entry *) bh; | |
5961 | h->def_regular = 1; | |
5962 | h->type = STT_OBJECT; | |
5963 | } | |
5964 | ||
5965 | return TRUE; | |
5966 | } | |
5967 | ||
b531344c MR |
5968 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ |
5969 | ||
5970 | struct elf_gc_sweep_symbol_info | |
5971 | { | |
5972 | struct bfd_link_info *info; | |
5973 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
5974 | bfd_boolean); | |
5975 | }; | |
5976 | ||
5977 | static bfd_boolean | |
5978 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) | |
5979 | { | |
5980 | if (!h->mark | |
5981 | && (((h->root.type == bfd_link_hash_defined | |
5982 | || h->root.type == bfd_link_hash_defweak) | |
5983 | && !((h->def_regular || ELF_COMMON_DEF_P (h)) | |
5984 | && h->root.u.def.section->gc_mark)) | |
5985 | || h->root.type == bfd_link_hash_undefined | |
5986 | || h->root.type == bfd_link_hash_undefweak)) | |
5987 | { | |
5988 | struct elf_gc_sweep_symbol_info *inf; | |
5989 | ||
5990 | inf = (struct elf_gc_sweep_symbol_info *) data; | |
5991 | (*inf->hide_symbol) (inf->info, h, TRUE); | |
5992 | h->def_regular = 0; | |
5993 | h->ref_regular = 0; | |
5994 | h->ref_regular_nonweak = 0; | |
5995 | } | |
5996 | ||
5997 | return TRUE; | |
5998 | } | |
5999 | ||
5a580b3a AM |
6000 | /* Set up the sizes and contents of the ELF dynamic sections. This is |
6001 | called by the ELF linker emulation before_allocation routine. We | |
6002 | must set the sizes of the sections before the linker sets the | |
6003 | addresses of the various sections. */ | |
6004 | ||
6005 | bfd_boolean | |
6006 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
6007 | const char *soname, | |
6008 | const char *rpath, | |
6009 | const char *filter_shlib, | |
7ee314fa AM |
6010 | const char *audit, |
6011 | const char *depaudit, | |
5a580b3a AM |
6012 | const char * const *auxiliary_filters, |
6013 | struct bfd_link_info *info, | |
fd91d419 | 6014 | asection **sinterpptr) |
5a580b3a | 6015 | { |
5a580b3a AM |
6016 | bfd *dynobj; |
6017 | const struct elf_backend_data *bed; | |
5a580b3a AM |
6018 | |
6019 | *sinterpptr = NULL; | |
6020 | ||
5a580b3a AM |
6021 | if (!is_elf_hash_table (info->hash)) |
6022 | return TRUE; | |
6023 | ||
5a580b3a AM |
6024 | dynobj = elf_hash_table (info)->dynobj; |
6025 | ||
9a2a56cc | 6026 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a | 6027 | { |
902e9fc7 MR |
6028 | struct bfd_elf_version_tree *verdefs; |
6029 | struct elf_info_failed asvinfo; | |
5a580b3a AM |
6030 | struct bfd_elf_version_tree *t; |
6031 | struct bfd_elf_version_expr *d; | |
902e9fc7 | 6032 | asection *s; |
e6699019 | 6033 | size_t soname_indx; |
7ee314fa | 6034 | |
5a580b3a AM |
6035 | /* If we are supposed to export all symbols into the dynamic symbol |
6036 | table (this is not the normal case), then do so. */ | |
55255dae | 6037 | if (info->export_dynamic |
0e1862bb | 6038 | || (bfd_link_executable (info) && info->dynamic)) |
5a580b3a | 6039 | { |
3d13f3e9 AM |
6040 | struct elf_info_failed eif; |
6041 | ||
6042 | eif.info = info; | |
6043 | eif.failed = FALSE; | |
5a580b3a AM |
6044 | elf_link_hash_traverse (elf_hash_table (info), |
6045 | _bfd_elf_export_symbol, | |
6046 | &eif); | |
6047 | if (eif.failed) | |
6048 | return FALSE; | |
6049 | } | |
6050 | ||
e6699019 L |
6051 | if (soname != NULL) |
6052 | { | |
6053 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6054 | soname, TRUE); | |
6055 | if (soname_indx == (size_t) -1 | |
6056 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
6057 | return FALSE; | |
6058 | } | |
6059 | else | |
6060 | soname_indx = (size_t) -1; | |
6061 | ||
5a580b3a | 6062 | /* Make all global versions with definition. */ |
fd91d419 | 6063 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6064 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6065 | if (!d->symver && d->literal) |
5a580b3a AM |
6066 | { |
6067 | const char *verstr, *name; | |
6068 | size_t namelen, verlen, newlen; | |
93252b1c | 6069 | char *newname, *p, leading_char; |
5a580b3a AM |
6070 | struct elf_link_hash_entry *newh; |
6071 | ||
93252b1c | 6072 | leading_char = bfd_get_symbol_leading_char (output_bfd); |
ae5a3597 | 6073 | name = d->pattern; |
93252b1c | 6074 | namelen = strlen (name) + (leading_char != '\0'); |
5a580b3a AM |
6075 | verstr = t->name; |
6076 | verlen = strlen (verstr); | |
6077 | newlen = namelen + verlen + 3; | |
6078 | ||
a50b1753 | 6079 | newname = (char *) bfd_malloc (newlen); |
5a580b3a AM |
6080 | if (newname == NULL) |
6081 | return FALSE; | |
93252b1c MF |
6082 | newname[0] = leading_char; |
6083 | memcpy (newname + (leading_char != '\0'), name, namelen); | |
5a580b3a AM |
6084 | |
6085 | /* Check the hidden versioned definition. */ | |
6086 | p = newname + namelen; | |
6087 | *p++ = ELF_VER_CHR; | |
6088 | memcpy (p, verstr, verlen + 1); | |
6089 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6090 | newname, FALSE, FALSE, | |
6091 | FALSE); | |
6092 | if (newh == NULL | |
6093 | || (newh->root.type != bfd_link_hash_defined | |
6094 | && newh->root.type != bfd_link_hash_defweak)) | |
6095 | { | |
6096 | /* Check the default versioned definition. */ | |
6097 | *p++ = ELF_VER_CHR; | |
6098 | memcpy (p, verstr, verlen + 1); | |
6099 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6100 | newname, FALSE, FALSE, | |
6101 | FALSE); | |
6102 | } | |
6103 | free (newname); | |
6104 | ||
6105 | /* Mark this version if there is a definition and it is | |
6106 | not defined in a shared object. */ | |
6107 | if (newh != NULL | |
f5385ebf | 6108 | && !newh->def_dynamic |
5a580b3a AM |
6109 | && (newh->root.type == bfd_link_hash_defined |
6110 | || newh->root.type == bfd_link_hash_defweak)) | |
6111 | d->symver = 1; | |
6112 | } | |
6113 | ||
6114 | /* Attach all the symbols to their version information. */ | |
5a580b3a | 6115 | asvinfo.info = info; |
5a580b3a AM |
6116 | asvinfo.failed = FALSE; |
6117 | ||
6118 | elf_link_hash_traverse (elf_hash_table (info), | |
6119 | _bfd_elf_link_assign_sym_version, | |
6120 | &asvinfo); | |
6121 | if (asvinfo.failed) | |
6122 | return FALSE; | |
6123 | ||
6124 | if (!info->allow_undefined_version) | |
6125 | { | |
6126 | /* Check if all global versions have a definition. */ | |
3d13f3e9 | 6127 | bfd_boolean all_defined = TRUE; |
fd91d419 | 6128 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6129 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6130 | if (d->literal && !d->symver && !d->script) |
5a580b3a | 6131 | { |
4eca0228 | 6132 | _bfd_error_handler |
5a580b3a AM |
6133 | (_("%s: undefined version: %s"), |
6134 | d->pattern, t->name); | |
6135 | all_defined = FALSE; | |
6136 | } | |
6137 | ||
6138 | if (!all_defined) | |
6139 | { | |
6140 | bfd_set_error (bfd_error_bad_value); | |
6141 | return FALSE; | |
6142 | } | |
6143 | } | |
6144 | ||
902e9fc7 MR |
6145 | /* Set up the version definition section. */ |
6146 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); | |
6147 | BFD_ASSERT (s != NULL); | |
5a580b3a | 6148 | |
902e9fc7 MR |
6149 | /* We may have created additional version definitions if we are |
6150 | just linking a regular application. */ | |
6151 | verdefs = info->version_info; | |
5a580b3a | 6152 | |
902e9fc7 MR |
6153 | /* Skip anonymous version tag. */ |
6154 | if (verdefs != NULL && verdefs->vernum == 0) | |
6155 | verdefs = verdefs->next; | |
5a580b3a | 6156 | |
902e9fc7 MR |
6157 | if (verdefs == NULL && !info->create_default_symver) |
6158 | s->flags |= SEC_EXCLUDE; | |
6159 | else | |
5a580b3a | 6160 | { |
902e9fc7 MR |
6161 | unsigned int cdefs; |
6162 | bfd_size_type size; | |
6163 | bfd_byte *p; | |
6164 | Elf_Internal_Verdef def; | |
6165 | Elf_Internal_Verdaux defaux; | |
6166 | struct bfd_link_hash_entry *bh; | |
6167 | struct elf_link_hash_entry *h; | |
6168 | const char *name; | |
5a580b3a | 6169 | |
902e9fc7 MR |
6170 | cdefs = 0; |
6171 | size = 0; | |
5a580b3a | 6172 | |
902e9fc7 MR |
6173 | /* Make space for the base version. */ |
6174 | size += sizeof (Elf_External_Verdef); | |
6175 | size += sizeof (Elf_External_Verdaux); | |
6176 | ++cdefs; | |
6177 | ||
6178 | /* Make space for the default version. */ | |
6179 | if (info->create_default_symver) | |
6180 | { | |
6181 | size += sizeof (Elf_External_Verdef); | |
6182 | ++cdefs; | |
3e3b46e5 PB |
6183 | } |
6184 | ||
5a580b3a AM |
6185 | for (t = verdefs; t != NULL; t = t->next) |
6186 | { | |
6187 | struct bfd_elf_version_deps *n; | |
6188 | ||
a6cc6b3b RO |
6189 | /* Don't emit base version twice. */ |
6190 | if (t->vernum == 0) | |
6191 | continue; | |
6192 | ||
5a580b3a AM |
6193 | size += sizeof (Elf_External_Verdef); |
6194 | size += sizeof (Elf_External_Verdaux); | |
6195 | ++cdefs; | |
6196 | ||
6197 | for (n = t->deps; n != NULL; n = n->next) | |
6198 | size += sizeof (Elf_External_Verdaux); | |
6199 | } | |
6200 | ||
eea6121a | 6201 | s->size = size; |
a50b1753 | 6202 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
eea6121a | 6203 | if (s->contents == NULL && s->size != 0) |
5a580b3a AM |
6204 | return FALSE; |
6205 | ||
6206 | /* Fill in the version definition section. */ | |
6207 | ||
6208 | p = s->contents; | |
6209 | ||
6210 | def.vd_version = VER_DEF_CURRENT; | |
6211 | def.vd_flags = VER_FLG_BASE; | |
6212 | def.vd_ndx = 1; | |
6213 | def.vd_cnt = 1; | |
3e3b46e5 PB |
6214 | if (info->create_default_symver) |
6215 | { | |
6216 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
6217 | def.vd_next = sizeof (Elf_External_Verdef); | |
6218 | } | |
6219 | else | |
6220 | { | |
6221 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6222 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6223 | + sizeof (Elf_External_Verdaux)); | |
6224 | } | |
5a580b3a | 6225 | |
ef53be89 | 6226 | if (soname_indx != (size_t) -1) |
5a580b3a AM |
6227 | { |
6228 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6229 | soname_indx); | |
6230 | def.vd_hash = bfd_elf_hash (soname); | |
6231 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 6232 | name = soname; |
5a580b3a AM |
6233 | } |
6234 | else | |
6235 | { | |
ef53be89 | 6236 | size_t indx; |
5a580b3a | 6237 | |
06084812 | 6238 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
6239 | def.vd_hash = bfd_elf_hash (name); |
6240 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6241 | name, FALSE); | |
ef53be89 | 6242 | if (indx == (size_t) -1) |
5a580b3a AM |
6243 | return FALSE; |
6244 | defaux.vda_name = indx; | |
6245 | } | |
6246 | defaux.vda_next = 0; | |
6247 | ||
6248 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6249 | (Elf_External_Verdef *) p); | |
6250 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
6251 | if (info->create_default_symver) |
6252 | { | |
6253 | /* Add a symbol representing this version. */ | |
6254 | bh = NULL; | |
6255 | if (! (_bfd_generic_link_add_one_symbol | |
6256 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6257 | 0, NULL, FALSE, | |
6258 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6259 | return FALSE; | |
6260 | h = (struct elf_link_hash_entry *) bh; | |
6261 | h->non_elf = 0; | |
6262 | h->def_regular = 1; | |
6263 | h->type = STT_OBJECT; | |
6264 | h->verinfo.vertree = NULL; | |
6265 | ||
6266 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
6267 | return FALSE; | |
6268 | ||
6269 | /* Create a duplicate of the base version with the same | |
6270 | aux block, but different flags. */ | |
6271 | def.vd_flags = 0; | |
6272 | def.vd_ndx = 2; | |
6273 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6274 | if (verdefs) | |
6275 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6276 | + sizeof (Elf_External_Verdaux)); | |
6277 | else | |
6278 | def.vd_next = 0; | |
6279 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6280 | (Elf_External_Verdef *) p); | |
6281 | p += sizeof (Elf_External_Verdef); | |
6282 | } | |
5a580b3a AM |
6283 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
6284 | (Elf_External_Verdaux *) p); | |
6285 | p += sizeof (Elf_External_Verdaux); | |
6286 | ||
6287 | for (t = verdefs; t != NULL; t = t->next) | |
6288 | { | |
6289 | unsigned int cdeps; | |
6290 | struct bfd_elf_version_deps *n; | |
5a580b3a | 6291 | |
a6cc6b3b RO |
6292 | /* Don't emit the base version twice. */ |
6293 | if (t->vernum == 0) | |
6294 | continue; | |
6295 | ||
5a580b3a AM |
6296 | cdeps = 0; |
6297 | for (n = t->deps; n != NULL; n = n->next) | |
6298 | ++cdeps; | |
6299 | ||
6300 | /* Add a symbol representing this version. */ | |
6301 | bh = NULL; | |
6302 | if (! (_bfd_generic_link_add_one_symbol | |
6303 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6304 | 0, NULL, FALSE, | |
6305 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6306 | return FALSE; | |
6307 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
6308 | h->non_elf = 0; |
6309 | h->def_regular = 1; | |
5a580b3a AM |
6310 | h->type = STT_OBJECT; |
6311 | h->verinfo.vertree = t; | |
6312 | ||
c152c796 | 6313 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
6314 | return FALSE; |
6315 | ||
6316 | def.vd_version = VER_DEF_CURRENT; | |
6317 | def.vd_flags = 0; | |
6318 | if (t->globals.list == NULL | |
6319 | && t->locals.list == NULL | |
6320 | && ! t->used) | |
6321 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 6322 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
6323 | def.vd_cnt = cdeps + 1; |
6324 | def.vd_hash = bfd_elf_hash (t->name); | |
6325 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6326 | def.vd_next = 0; | |
a6cc6b3b RO |
6327 | |
6328 | /* If a basever node is next, it *must* be the last node in | |
6329 | the chain, otherwise Verdef construction breaks. */ | |
6330 | if (t->next != NULL && t->next->vernum == 0) | |
6331 | BFD_ASSERT (t->next->next == NULL); | |
6332 | ||
6333 | if (t->next != NULL && t->next->vernum != 0) | |
5a580b3a AM |
6334 | def.vd_next = (sizeof (Elf_External_Verdef) |
6335 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
6336 | ||
6337 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6338 | (Elf_External_Verdef *) p); | |
6339 | p += sizeof (Elf_External_Verdef); | |
6340 | ||
6341 | defaux.vda_name = h->dynstr_index; | |
6342 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6343 | h->dynstr_index); | |
6344 | defaux.vda_next = 0; | |
6345 | if (t->deps != NULL) | |
6346 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6347 | t->name_indx = defaux.vda_name; | |
6348 | ||
6349 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6350 | (Elf_External_Verdaux *) p); | |
6351 | p += sizeof (Elf_External_Verdaux); | |
6352 | ||
6353 | for (n = t->deps; n != NULL; n = n->next) | |
6354 | { | |
6355 | if (n->version_needed == NULL) | |
6356 | { | |
6357 | /* This can happen if there was an error in the | |
6358 | version script. */ | |
6359 | defaux.vda_name = 0; | |
6360 | } | |
6361 | else | |
6362 | { | |
6363 | defaux.vda_name = n->version_needed->name_indx; | |
6364 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6365 | defaux.vda_name); | |
6366 | } | |
6367 | if (n->next == NULL) | |
6368 | defaux.vda_next = 0; | |
6369 | else | |
6370 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6371 | ||
6372 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6373 | (Elf_External_Verdaux *) p); | |
6374 | p += sizeof (Elf_External_Verdaux); | |
6375 | } | |
6376 | } | |
6377 | ||
5a580b3a AM |
6378 | elf_tdata (output_bfd)->cverdefs = cdefs; |
6379 | } | |
902e9fc7 MR |
6380 | } |
6381 | ||
6382 | bed = get_elf_backend_data (output_bfd); | |
6383 | ||
6384 | if (info->gc_sections && bed->can_gc_sections) | |
6385 | { | |
6386 | struct elf_gc_sweep_symbol_info sweep_info; | |
902e9fc7 MR |
6387 | |
6388 | /* Remove the symbols that were in the swept sections from the | |
3d13f3e9 | 6389 | dynamic symbol table. */ |
902e9fc7 MR |
6390 | sweep_info.info = info; |
6391 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
6392 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
6393 | &sweep_info); | |
3d13f3e9 AM |
6394 | } |
6395 | ||
6396 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6397 | { | |
6398 | asection *s; | |
6399 | struct elf_find_verdep_info sinfo; | |
6400 | ||
6401 | /* Work out the size of the version reference section. */ | |
6402 | ||
6403 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); | |
6404 | BFD_ASSERT (s != NULL); | |
902e9fc7 | 6405 | |
3d13f3e9 AM |
6406 | sinfo.info = info; |
6407 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
6408 | if (sinfo.vers == 0) | |
6409 | sinfo.vers = 1; | |
6410 | sinfo.failed = FALSE; | |
6411 | ||
6412 | elf_link_hash_traverse (elf_hash_table (info), | |
6413 | _bfd_elf_link_find_version_dependencies, | |
6414 | &sinfo); | |
6415 | if (sinfo.failed) | |
6416 | return FALSE; | |
6417 | ||
6418 | if (elf_tdata (output_bfd)->verref == NULL) | |
6419 | s->flags |= SEC_EXCLUDE; | |
6420 | else | |
6421 | { | |
6422 | Elf_Internal_Verneed *vn; | |
6423 | unsigned int size; | |
6424 | unsigned int crefs; | |
6425 | bfd_byte *p; | |
6426 | ||
6427 | /* Build the version dependency section. */ | |
6428 | size = 0; | |
6429 | crefs = 0; | |
6430 | for (vn = elf_tdata (output_bfd)->verref; | |
6431 | vn != NULL; | |
6432 | vn = vn->vn_nextref) | |
6433 | { | |
6434 | Elf_Internal_Vernaux *a; | |
6435 | ||
6436 | size += sizeof (Elf_External_Verneed); | |
6437 | ++crefs; | |
6438 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6439 | size += sizeof (Elf_External_Vernaux); | |
6440 | } | |
6441 | ||
6442 | s->size = size; | |
6443 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); | |
6444 | if (s->contents == NULL) | |
6445 | return FALSE; | |
6446 | ||
6447 | p = s->contents; | |
6448 | for (vn = elf_tdata (output_bfd)->verref; | |
6449 | vn != NULL; | |
6450 | vn = vn->vn_nextref) | |
6451 | { | |
6452 | unsigned int caux; | |
6453 | Elf_Internal_Vernaux *a; | |
6454 | size_t indx; | |
6455 | ||
6456 | caux = 0; | |
6457 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6458 | ++caux; | |
6459 | ||
6460 | vn->vn_version = VER_NEED_CURRENT; | |
6461 | vn->vn_cnt = caux; | |
6462 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6463 | elf_dt_name (vn->vn_bfd) != NULL | |
6464 | ? elf_dt_name (vn->vn_bfd) | |
6465 | : lbasename (vn->vn_bfd->filename), | |
6466 | FALSE); | |
6467 | if (indx == (size_t) -1) | |
6468 | return FALSE; | |
6469 | vn->vn_file = indx; | |
6470 | vn->vn_aux = sizeof (Elf_External_Verneed); | |
6471 | if (vn->vn_nextref == NULL) | |
6472 | vn->vn_next = 0; | |
6473 | else | |
6474 | vn->vn_next = (sizeof (Elf_External_Verneed) | |
6475 | + caux * sizeof (Elf_External_Vernaux)); | |
6476 | ||
6477 | _bfd_elf_swap_verneed_out (output_bfd, vn, | |
6478 | (Elf_External_Verneed *) p); | |
6479 | p += sizeof (Elf_External_Verneed); | |
6480 | ||
6481 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6482 | { | |
6483 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6484 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6485 | a->vna_nodename, FALSE); | |
6486 | if (indx == (size_t) -1) | |
6487 | return FALSE; | |
6488 | a->vna_name = indx; | |
6489 | if (a->vna_nextptr == NULL) | |
6490 | a->vna_next = 0; | |
6491 | else | |
6492 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6493 | ||
6494 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6495 | (Elf_External_Vernaux *) p); | |
6496 | p += sizeof (Elf_External_Vernaux); | |
6497 | } | |
6498 | } | |
6499 | ||
6500 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6501 | } | |
902e9fc7 MR |
6502 | } |
6503 | ||
6504 | /* Any syms created from now on start with -1 in | |
6505 | got.refcount/offset and plt.refcount/offset. */ | |
6506 | elf_hash_table (info)->init_got_refcount | |
6507 | = elf_hash_table (info)->init_got_offset; | |
6508 | elf_hash_table (info)->init_plt_refcount | |
6509 | = elf_hash_table (info)->init_plt_offset; | |
6510 | ||
6511 | if (bfd_link_relocatable (info) | |
6512 | && !_bfd_elf_size_group_sections (info)) | |
6513 | return FALSE; | |
6514 | ||
6515 | /* The backend may have to create some sections regardless of whether | |
6516 | we're dynamic or not. */ | |
6517 | if (bed->elf_backend_always_size_sections | |
6518 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
6519 | return FALSE; | |
6520 | ||
6521 | /* Determine any GNU_STACK segment requirements, after the backend | |
6522 | has had a chance to set a default segment size. */ | |
6523 | if (info->execstack) | |
6524 | elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; | |
6525 | else if (info->noexecstack) | |
6526 | elf_stack_flags (output_bfd) = PF_R | PF_W; | |
6527 | else | |
6528 | { | |
6529 | bfd *inputobj; | |
6530 | asection *notesec = NULL; | |
6531 | int exec = 0; | |
6532 | ||
6533 | for (inputobj = info->input_bfds; | |
6534 | inputobj; | |
6535 | inputobj = inputobj->link.next) | |
6536 | { | |
6537 | asection *s; | |
6538 | ||
6539 | if (inputobj->flags | |
6540 | & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) | |
6541 | continue; | |
57963c05 AM |
6542 | s = inputobj->sections; |
6543 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
6544 | continue; | |
6545 | ||
902e9fc7 MR |
6546 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); |
6547 | if (s) | |
6548 | { | |
6549 | if (s->flags & SEC_CODE) | |
6550 | exec = PF_X; | |
6551 | notesec = s; | |
6552 | } | |
6553 | else if (bed->default_execstack) | |
6554 | exec = PF_X; | |
6555 | } | |
6556 | if (notesec || info->stacksize > 0) | |
6557 | elf_stack_flags (output_bfd) = PF_R | PF_W | exec; | |
6558 | if (notesec && exec && bfd_link_relocatable (info) | |
6559 | && notesec->output_section != bfd_abs_section_ptr) | |
6560 | notesec->output_section->flags |= SEC_CODE; | |
6561 | } | |
6562 | ||
6563 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6564 | { | |
6565 | struct elf_info_failed eif; | |
6566 | struct elf_link_hash_entry *h; | |
6567 | asection *dynstr; | |
6568 | asection *s; | |
6569 | ||
6570 | *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); | |
6571 | BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); | |
6572 | ||
902e9fc7 MR |
6573 | if (info->symbolic) |
6574 | { | |
6575 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
6576 | return FALSE; | |
6577 | info->flags |= DF_SYMBOLIC; | |
6578 | } | |
6579 | ||
6580 | if (rpath != NULL) | |
6581 | { | |
6582 | size_t indx; | |
6583 | bfd_vma tag; | |
6584 | ||
6585 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
6586 | TRUE); | |
6587 | if (indx == (size_t) -1) | |
6588 | return FALSE; | |
6589 | ||
6590 | tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; | |
6591 | if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) | |
6592 | return FALSE; | |
6593 | } | |
6594 | ||
6595 | if (filter_shlib != NULL) | |
6596 | { | |
6597 | size_t indx; | |
6598 | ||
6599 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6600 | filter_shlib, TRUE); | |
6601 | if (indx == (size_t) -1 | |
6602 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
6603 | return FALSE; | |
6604 | } | |
6605 | ||
6606 | if (auxiliary_filters != NULL) | |
6607 | { | |
6608 | const char * const *p; | |
6609 | ||
6610 | for (p = auxiliary_filters; *p != NULL; p++) | |
6611 | { | |
6612 | size_t indx; | |
6613 | ||
6614 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6615 | *p, TRUE); | |
6616 | if (indx == (size_t) -1 | |
6617 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
6618 | return FALSE; | |
6619 | } | |
6620 | } | |
6621 | ||
6622 | if (audit != NULL) | |
6623 | { | |
6624 | size_t indx; | |
6625 | ||
6626 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, | |
6627 | TRUE); | |
6628 | if (indx == (size_t) -1 | |
6629 | || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) | |
6630 | return FALSE; | |
6631 | } | |
6632 | ||
6633 | if (depaudit != NULL) | |
6634 | { | |
6635 | size_t indx; | |
6636 | ||
6637 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, | |
6638 | TRUE); | |
6639 | if (indx == (size_t) -1 | |
6640 | || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) | |
6641 | return FALSE; | |
6642 | } | |
6643 | ||
6644 | eif.info = info; | |
6645 | eif.failed = FALSE; | |
6646 | ||
6647 | /* Find all symbols which were defined in a dynamic object and make | |
6648 | the backend pick a reasonable value for them. */ | |
6649 | elf_link_hash_traverse (elf_hash_table (info), | |
6650 | _bfd_elf_adjust_dynamic_symbol, | |
6651 | &eif); | |
6652 | if (eif.failed) | |
6653 | return FALSE; | |
6654 | ||
6655 | /* Add some entries to the .dynamic section. We fill in some of the | |
6656 | values later, in bfd_elf_final_link, but we must add the entries | |
6657 | now so that we know the final size of the .dynamic section. */ | |
6658 | ||
6659 | /* If there are initialization and/or finalization functions to | |
6660 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
6661 | h = (info->init_function | |
6662 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6663 | info->init_function, FALSE, | |
6664 | FALSE, FALSE) | |
6665 | : NULL); | |
6666 | if (h != NULL | |
6667 | && (h->ref_regular | |
6668 | || h->def_regular)) | |
6669 | { | |
6670 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
6671 | return FALSE; | |
6672 | } | |
6673 | h = (info->fini_function | |
6674 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6675 | info->fini_function, FALSE, | |
6676 | FALSE, FALSE) | |
6677 | : NULL); | |
6678 | if (h != NULL | |
6679 | && (h->ref_regular | |
6680 | || h->def_regular)) | |
6681 | { | |
6682 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
6683 | return FALSE; | |
6684 | } | |
6685 | ||
6686 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); | |
6687 | if (s != NULL && s->linker_has_input) | |
6688 | { | |
6689 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
6690 | if (! bfd_link_executable (info)) | |
6691 | { | |
6692 | bfd *sub; | |
6693 | asection *o; | |
6694 | ||
57963c05 AM |
6695 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
6696 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
6697 | && (o = sub->sections) != NULL | |
6698 | && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS) | |
902e9fc7 MR |
6699 | for (o = sub->sections; o != NULL; o = o->next) |
6700 | if (elf_section_data (o)->this_hdr.sh_type | |
6701 | == SHT_PREINIT_ARRAY) | |
6702 | { | |
6703 | _bfd_error_handler | |
6704 | (_("%B: .preinit_array section is not allowed in DSO"), | |
6705 | sub); | |
6706 | break; | |
6707 | } | |
6708 | ||
6709 | bfd_set_error (bfd_error_nonrepresentable_section); | |
6710 | return FALSE; | |
6711 | } | |
6712 | ||
6713 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
6714 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
6715 | return FALSE; | |
6716 | } | |
6717 | s = bfd_get_section_by_name (output_bfd, ".init_array"); | |
6718 | if (s != NULL && s->linker_has_input) | |
6719 | { | |
6720 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
6721 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
6722 | return FALSE; | |
6723 | } | |
6724 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); | |
6725 | if (s != NULL && s->linker_has_input) | |
6726 | { | |
6727 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
6728 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
6729 | return FALSE; | |
6730 | } | |
6731 | ||
6732 | dynstr = bfd_get_linker_section (dynobj, ".dynstr"); | |
6733 | /* If .dynstr is excluded from the link, we don't want any of | |
6734 | these tags. Strictly, we should be checking each section | |
6735 | individually; This quick check covers for the case where | |
6736 | someone does a /DISCARD/ : { *(*) }. */ | |
6737 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
6738 | { | |
6739 | bfd_size_type strsize; | |
6740 | ||
6741 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
6742 | if ((info->emit_hash | |
6743 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
6744 | || (info->emit_gnu_hash | |
6745 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
6746 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
6747 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
6748 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
6749 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
6750 | bed->s->sizeof_sym)) | |
6751 | return FALSE; | |
6752 | } | |
6753 | } | |
6754 | ||
6755 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
6756 | return FALSE; | |
6757 | ||
6758 | /* The backend must work out the sizes of all the other dynamic | |
6759 | sections. */ | |
6760 | if (dynobj != NULL | |
6761 | && bed->elf_backend_size_dynamic_sections != NULL | |
6762 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
6763 | return FALSE; | |
6764 | ||
6765 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6766 | { | |
6767 | unsigned long section_sym_count; | |
6768 | ||
6769 | if (elf_tdata (output_bfd)->cverdefs) | |
6770 | { | |
6771 | unsigned int crefs = elf_tdata (output_bfd)->cverdefs; | |
6772 | ||
6773 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
6774 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs)) | |
6775 | return FALSE; | |
6776 | } | |
6777 | ||
6778 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
6779 | { | |
6780 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
6781 | return FALSE; | |
6782 | } | |
6783 | else if (info->flags & DF_BIND_NOW) | |
6784 | { | |
6785 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
6786 | return FALSE; | |
6787 | } | |
6788 | ||
6789 | if (info->flags_1) | |
6790 | { | |
6791 | if (bfd_link_executable (info)) | |
6792 | info->flags_1 &= ~ (DF_1_INITFIRST | |
6793 | | DF_1_NODELETE | |
6794 | | DF_1_NOOPEN); | |
6795 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
6796 | return FALSE; | |
6797 | } | |
6798 | ||
6799 | if (elf_tdata (output_bfd)->cverrefs) | |
6800 | { | |
6801 | unsigned int crefs = elf_tdata (output_bfd)->cverrefs; | |
6802 | ||
6803 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
6804 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
6805 | return FALSE; | |
6806 | } | |
5a580b3a | 6807 | |
8423293d AM |
6808 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
6809 | && elf_tdata (output_bfd)->cverdefs == 0) | |
6810 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
3d13f3e9 | 6811 | §ion_sym_count) <= 1) |
8423293d | 6812 | { |
902e9fc7 MR |
6813 | asection *s; |
6814 | ||
3d4d4302 | 6815 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
8423293d AM |
6816 | s->flags |= SEC_EXCLUDE; |
6817 | } | |
6818 | } | |
6819 | return TRUE; | |
6820 | } | |
6821 | ||
74541ad4 AM |
6822 | /* Find the first non-excluded output section. We'll use its |
6823 | section symbol for some emitted relocs. */ | |
6824 | void | |
6825 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
6826 | { | |
6827 | asection *s; | |
6828 | ||
6829 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6830 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
6831 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6832 | { | |
6833 | elf_hash_table (info)->text_index_section = s; | |
6834 | break; | |
6835 | } | |
6836 | } | |
6837 | ||
6838 | /* Find two non-excluded output sections, one for code, one for data. | |
6839 | We'll use their section symbols for some emitted relocs. */ | |
6840 | void | |
6841 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
6842 | { | |
6843 | asection *s; | |
6844 | ||
266b05cf DJ |
6845 | /* Data first, since setting text_index_section changes |
6846 | _bfd_elf_link_omit_section_dynsym. */ | |
74541ad4 | 6847 | for (s = output_bfd->sections; s != NULL; s = s->next) |
266b05cf | 6848 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) |
74541ad4 AM |
6849 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6850 | { | |
266b05cf | 6851 | elf_hash_table (info)->data_index_section = s; |
74541ad4 AM |
6852 | break; |
6853 | } | |
6854 | ||
6855 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
266b05cf DJ |
6856 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) |
6857 | == (SEC_ALLOC | SEC_READONLY)) | |
74541ad4 AM |
6858 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6859 | { | |
266b05cf | 6860 | elf_hash_table (info)->text_index_section = s; |
74541ad4 AM |
6861 | break; |
6862 | } | |
6863 | ||
6864 | if (elf_hash_table (info)->text_index_section == NULL) | |
6865 | elf_hash_table (info)->text_index_section | |
6866 | = elf_hash_table (info)->data_index_section; | |
6867 | } | |
6868 | ||
8423293d AM |
6869 | bfd_boolean |
6870 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
6871 | { | |
74541ad4 | 6872 | const struct elf_backend_data *bed; |
23ec1e32 | 6873 | unsigned long section_sym_count; |
96d01d93 | 6874 | bfd_size_type dynsymcount = 0; |
74541ad4 | 6875 | |
8423293d AM |
6876 | if (!is_elf_hash_table (info->hash)) |
6877 | return TRUE; | |
6878 | ||
74541ad4 AM |
6879 | bed = get_elf_backend_data (output_bfd); |
6880 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
6881 | ||
23ec1e32 MR |
6882 | /* Assign dynsym indices. In a shared library we generate a section |
6883 | symbol for each output section, which come first. Next come all | |
6884 | of the back-end allocated local dynamic syms, followed by the rest | |
6885 | of the global symbols. | |
6886 | ||
6887 | This is usually not needed for static binaries, however backends | |
6888 | can request to always do it, e.g. the MIPS backend uses dynamic | |
6889 | symbol counts to lay out GOT, which will be produced in the | |
6890 | presence of GOT relocations even in static binaries (holding fixed | |
6891 | data in that case, to satisfy those relocations). */ | |
6892 | ||
6893 | if (elf_hash_table (info)->dynamic_sections_created | |
6894 | || bed->always_renumber_dynsyms) | |
6895 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
6896 | §ion_sym_count); | |
6897 | ||
8423293d AM |
6898 | if (elf_hash_table (info)->dynamic_sections_created) |
6899 | { | |
6900 | bfd *dynobj; | |
8423293d | 6901 | asection *s; |
8423293d AM |
6902 | unsigned int dtagcount; |
6903 | ||
6904 | dynobj = elf_hash_table (info)->dynobj; | |
6905 | ||
5a580b3a | 6906 | /* Work out the size of the symbol version section. */ |
3d4d4302 | 6907 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
5a580b3a | 6908 | BFD_ASSERT (s != NULL); |
d5486c43 | 6909 | if ((s->flags & SEC_EXCLUDE) == 0) |
5a580b3a | 6910 | { |
eea6121a | 6911 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
a50b1753 | 6912 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
5a580b3a AM |
6913 | if (s->contents == NULL) |
6914 | return FALSE; | |
6915 | ||
6916 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
6917 | return FALSE; | |
6918 | } | |
6919 | ||
6920 | /* Set the size of the .dynsym and .hash sections. We counted | |
6921 | the number of dynamic symbols in elf_link_add_object_symbols. | |
6922 | We will build the contents of .dynsym and .hash when we build | |
6923 | the final symbol table, because until then we do not know the | |
6924 | correct value to give the symbols. We built the .dynstr | |
6925 | section as we went along in elf_link_add_object_symbols. */ | |
cae1fbbb | 6926 | s = elf_hash_table (info)->dynsym; |
5a580b3a | 6927 | BFD_ASSERT (s != NULL); |
eea6121a | 6928 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a | 6929 | |
d5486c43 L |
6930 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
6931 | if (s->contents == NULL) | |
6932 | return FALSE; | |
5a580b3a | 6933 | |
d5486c43 L |
6934 | /* The first entry in .dynsym is a dummy symbol. Clear all the |
6935 | section syms, in case we don't output them all. */ | |
6936 | ++section_sym_count; | |
6937 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a | 6938 | |
fdc90cb4 JJ |
6939 | elf_hash_table (info)->bucketcount = 0; |
6940 | ||
5a580b3a AM |
6941 | /* Compute the size of the hashing table. As a side effect this |
6942 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
6943 | if (info->emit_hash) |
6944 | { | |
6945 | unsigned long int *hashcodes; | |
14b1c01e | 6946 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
6947 | bfd_size_type amt; |
6948 | unsigned long int nsyms; | |
6949 | size_t bucketcount; | |
6950 | size_t hash_entry_size; | |
6951 | ||
6952 | /* Compute the hash values for all exported symbols. At the same | |
6953 | time store the values in an array so that we could use them for | |
6954 | optimizations. */ | |
6955 | amt = dynsymcount * sizeof (unsigned long int); | |
a50b1753 | 6956 | hashcodes = (unsigned long int *) bfd_malloc (amt); |
fdc90cb4 JJ |
6957 | if (hashcodes == NULL) |
6958 | return FALSE; | |
14b1c01e AM |
6959 | hashinf.hashcodes = hashcodes; |
6960 | hashinf.error = FALSE; | |
5a580b3a | 6961 | |
fdc90cb4 JJ |
6962 | /* Put all hash values in HASHCODES. */ |
6963 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
6964 | elf_collect_hash_codes, &hashinf); |
6965 | if (hashinf.error) | |
4dd07732 AM |
6966 | { |
6967 | free (hashcodes); | |
6968 | return FALSE; | |
6969 | } | |
5a580b3a | 6970 | |
14b1c01e | 6971 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
6972 | bucketcount |
6973 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
6974 | free (hashcodes); | |
6975 | ||
4b48e2f6 | 6976 | if (bucketcount == 0 && nsyms > 0) |
fdc90cb4 | 6977 | return FALSE; |
5a580b3a | 6978 | |
fdc90cb4 JJ |
6979 | elf_hash_table (info)->bucketcount = bucketcount; |
6980 | ||
3d4d4302 | 6981 | s = bfd_get_linker_section (dynobj, ".hash"); |
fdc90cb4 JJ |
6982 | BFD_ASSERT (s != NULL); |
6983 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
6984 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
a50b1753 | 6985 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6986 | if (s->contents == NULL) |
6987 | return FALSE; | |
6988 | ||
6989 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
6990 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
6991 | s->contents + hash_entry_size); | |
6992 | } | |
6993 | ||
6994 | if (info->emit_gnu_hash) | |
6995 | { | |
6996 | size_t i, cnt; | |
6997 | unsigned char *contents; | |
6998 | struct collect_gnu_hash_codes cinfo; | |
6999 | bfd_size_type amt; | |
7000 | size_t bucketcount; | |
7001 | ||
7002 | memset (&cinfo, 0, sizeof (cinfo)); | |
7003 | ||
7004 | /* Compute the hash values for all exported symbols. At the same | |
7005 | time store the values in an array so that we could use them for | |
7006 | optimizations. */ | |
7007 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
a50b1753 | 7008 | cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); |
fdc90cb4 JJ |
7009 | if (cinfo.hashcodes == NULL) |
7010 | return FALSE; | |
7011 | ||
7012 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
7013 | cinfo.min_dynindx = -1; | |
7014 | cinfo.output_bfd = output_bfd; | |
7015 | cinfo.bed = bed; | |
7016 | ||
7017 | /* Put all hash values in HASHCODES. */ | |
7018 | elf_link_hash_traverse (elf_hash_table (info), | |
7019 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e | 7020 | if (cinfo.error) |
4dd07732 AM |
7021 | { |
7022 | free (cinfo.hashcodes); | |
7023 | return FALSE; | |
7024 | } | |
fdc90cb4 JJ |
7025 | |
7026 | bucketcount | |
7027 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
7028 | ||
7029 | if (bucketcount == 0) | |
7030 | { | |
7031 | free (cinfo.hashcodes); | |
7032 | return FALSE; | |
7033 | } | |
7034 | ||
3d4d4302 | 7035 | s = bfd_get_linker_section (dynobj, ".gnu.hash"); |
fdc90cb4 JJ |
7036 | BFD_ASSERT (s != NULL); |
7037 | ||
7038 | if (cinfo.nsyms == 0) | |
7039 | { | |
7040 | /* Empty .gnu.hash section is special. */ | |
7041 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
7042 | free (cinfo.hashcodes); | |
7043 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
a50b1753 | 7044 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7045 | if (contents == NULL) |
7046 | return FALSE; | |
7047 | s->contents = contents; | |
7048 | /* 1 empty bucket. */ | |
7049 | bfd_put_32 (output_bfd, 1, contents); | |
7050 | /* SYMIDX above the special symbol 0. */ | |
7051 | bfd_put_32 (output_bfd, 1, contents + 4); | |
7052 | /* Just one word for bitmask. */ | |
7053 | bfd_put_32 (output_bfd, 1, contents + 8); | |
7054 | /* Only hash fn bloom filter. */ | |
7055 | bfd_put_32 (output_bfd, 0, contents + 12); | |
7056 | /* No hashes are valid - empty bitmask. */ | |
7057 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
7058 | /* No hashes in the only bucket. */ | |
7059 | bfd_put_32 (output_bfd, 0, | |
7060 | contents + 16 + bed->s->arch_size / 8); | |
7061 | } | |
7062 | else | |
7063 | { | |
9e6619e2 | 7064 | unsigned long int maskwords, maskbitslog2, x; |
0b33793d | 7065 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 | 7066 | |
9e6619e2 AM |
7067 | x = cinfo.nsyms; |
7068 | maskbitslog2 = 1; | |
7069 | while ((x >>= 1) != 0) | |
7070 | ++maskbitslog2; | |
fdc90cb4 JJ |
7071 | if (maskbitslog2 < 3) |
7072 | maskbitslog2 = 5; | |
7073 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
7074 | maskbitslog2 = maskbitslog2 + 3; | |
7075 | else | |
7076 | maskbitslog2 = maskbitslog2 + 2; | |
7077 | if (bed->s->arch_size == 64) | |
7078 | { | |
7079 | if (maskbitslog2 == 5) | |
7080 | maskbitslog2 = 6; | |
7081 | cinfo.shift1 = 6; | |
7082 | } | |
7083 | else | |
7084 | cinfo.shift1 = 5; | |
7085 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 7086 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
7087 | cinfo.maskbits = 1 << maskbitslog2; |
7088 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
7089 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
7090 | amt += maskwords * sizeof (bfd_vma); | |
a50b1753 | 7091 | cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); |
fdc90cb4 JJ |
7092 | if (cinfo.bitmask == NULL) |
7093 | { | |
7094 | free (cinfo.hashcodes); | |
7095 | return FALSE; | |
7096 | } | |
7097 | ||
a50b1753 | 7098 | cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); |
fdc90cb4 JJ |
7099 | cinfo.indx = cinfo.counts + bucketcount; |
7100 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
7101 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
7102 | ||
7103 | /* Determine how often each hash bucket is used. */ | |
7104 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
7105 | for (i = 0; i < cinfo.nsyms; ++i) | |
7106 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
7107 | ||
7108 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
7109 | if (cinfo.counts[i] != 0) | |
7110 | { | |
7111 | cinfo.indx[i] = cnt; | |
7112 | cnt += cinfo.counts[i]; | |
7113 | } | |
7114 | BFD_ASSERT (cnt == dynsymcount); | |
7115 | cinfo.bucketcount = bucketcount; | |
7116 | cinfo.local_indx = cinfo.min_dynindx; | |
7117 | ||
7118 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
7119 | s->size += cinfo.maskbits / 8; | |
a50b1753 | 7120 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7121 | if (contents == NULL) |
7122 | { | |
7123 | free (cinfo.bitmask); | |
7124 | free (cinfo.hashcodes); | |
7125 | return FALSE; | |
7126 | } | |
7127 | ||
7128 | s->contents = contents; | |
7129 | bfd_put_32 (output_bfd, bucketcount, contents); | |
7130 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
7131 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
7132 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
7133 | contents += 16 + cinfo.maskbits / 8; | |
7134 | ||
7135 | for (i = 0; i < bucketcount; ++i) | |
7136 | { | |
7137 | if (cinfo.counts[i] == 0) | |
7138 | bfd_put_32 (output_bfd, 0, contents); | |
7139 | else | |
7140 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
7141 | contents += 4; | |
7142 | } | |
7143 | ||
7144 | cinfo.contents = contents; | |
7145 | ||
7146 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
7147 | elf_link_hash_traverse (elf_hash_table (info), | |
7148 | elf_renumber_gnu_hash_syms, &cinfo); | |
7149 | ||
7150 | contents = s->contents + 16; | |
7151 | for (i = 0; i < maskwords; ++i) | |
7152 | { | |
7153 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
7154 | contents); | |
7155 | contents += bed->s->arch_size / 8; | |
7156 | } | |
7157 | ||
7158 | free (cinfo.bitmask); | |
7159 | free (cinfo.hashcodes); | |
7160 | } | |
7161 | } | |
5a580b3a | 7162 | |
3d4d4302 | 7163 | s = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
7164 | BFD_ASSERT (s != NULL); |
7165 | ||
4ad4eba5 | 7166 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 7167 | |
eea6121a | 7168 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
7169 | |
7170 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
7171 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
7172 | return FALSE; | |
7173 | } | |
7174 | ||
7175 | return TRUE; | |
7176 | } | |
4d269e42 | 7177 | \f |
4d269e42 AM |
7178 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ |
7179 | ||
7180 | static void | |
7181 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
7182 | asection *sec) | |
7183 | { | |
dbaa2011 AM |
7184 | BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); |
7185 | sec->sec_info_type = SEC_INFO_TYPE_NONE; | |
4d269e42 AM |
7186 | } |
7187 | ||
7188 | /* Finish SHF_MERGE section merging. */ | |
7189 | ||
7190 | bfd_boolean | |
630993ec | 7191 | _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) |
4d269e42 AM |
7192 | { |
7193 | bfd *ibfd; | |
7194 | asection *sec; | |
7195 | ||
7196 | if (!is_elf_hash_table (info->hash)) | |
7197 | return FALSE; | |
7198 | ||
c72f2fb2 | 7199 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
630993ec AM |
7200 | if ((ibfd->flags & DYNAMIC) == 0 |
7201 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
017e6bce AM |
7202 | && (elf_elfheader (ibfd)->e_ident[EI_CLASS] |
7203 | == get_elf_backend_data (obfd)->s->elfclass)) | |
4d269e42 AM |
7204 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) |
7205 | if ((sec->flags & SEC_MERGE) != 0 | |
7206 | && !bfd_is_abs_section (sec->output_section)) | |
7207 | { | |
7208 | struct bfd_elf_section_data *secdata; | |
7209 | ||
7210 | secdata = elf_section_data (sec); | |
630993ec | 7211 | if (! _bfd_add_merge_section (obfd, |
4d269e42 AM |
7212 | &elf_hash_table (info)->merge_info, |
7213 | sec, &secdata->sec_info)) | |
7214 | return FALSE; | |
7215 | else if (secdata->sec_info) | |
dbaa2011 | 7216 | sec->sec_info_type = SEC_INFO_TYPE_MERGE; |
4d269e42 AM |
7217 | } |
7218 | ||
7219 | if (elf_hash_table (info)->merge_info != NULL) | |
630993ec | 7220 | _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, |
4d269e42 AM |
7221 | merge_sections_remove_hook); |
7222 | return TRUE; | |
7223 | } | |
7224 | ||
7225 | /* Create an entry in an ELF linker hash table. */ | |
7226 | ||
7227 | struct bfd_hash_entry * | |
7228 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
7229 | struct bfd_hash_table *table, | |
7230 | const char *string) | |
7231 | { | |
7232 | /* Allocate the structure if it has not already been allocated by a | |
7233 | subclass. */ | |
7234 | if (entry == NULL) | |
7235 | { | |
a50b1753 | 7236 | entry = (struct bfd_hash_entry *) |
ca4be51c | 7237 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); |
4d269e42 AM |
7238 | if (entry == NULL) |
7239 | return entry; | |
7240 | } | |
7241 | ||
7242 | /* Call the allocation method of the superclass. */ | |
7243 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
7244 | if (entry != NULL) | |
7245 | { | |
7246 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
7247 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
7248 | ||
7249 | /* Set local fields. */ | |
7250 | ret->indx = -1; | |
7251 | ret->dynindx = -1; | |
7252 | ret->got = htab->init_got_refcount; | |
7253 | ret->plt = htab->init_plt_refcount; | |
7254 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
7255 | - offsetof (struct elf_link_hash_entry, size))); | |
7256 | /* Assume that we have been called by a non-ELF symbol reader. | |
7257 | This flag is then reset by the code which reads an ELF input | |
7258 | file. This ensures that a symbol created by a non-ELF symbol | |
7259 | reader will have the flag set correctly. */ | |
7260 | ret->non_elf = 1; | |
7261 | } | |
7262 | ||
7263 | return entry; | |
7264 | } | |
7265 | ||
7266 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
7267 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
7268 | ||
7269 | void | |
7270 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
7271 | struct elf_link_hash_entry *dir, | |
7272 | struct elf_link_hash_entry *ind) | |
7273 | { | |
7274 | struct elf_link_hash_table *htab; | |
7275 | ||
7276 | /* Copy down any references that we may have already seen to the | |
e81830c5 | 7277 | symbol which just became indirect. */ |
4d269e42 | 7278 | |
422f1182 | 7279 | if (dir->versioned != versioned_hidden) |
e81830c5 AM |
7280 | dir->ref_dynamic |= ind->ref_dynamic; |
7281 | dir->ref_regular |= ind->ref_regular; | |
7282 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
7283 | dir->non_got_ref |= ind->non_got_ref; | |
7284 | dir->needs_plt |= ind->needs_plt; | |
7285 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
4d269e42 AM |
7286 | |
7287 | if (ind->root.type != bfd_link_hash_indirect) | |
7288 | return; | |
7289 | ||
7290 | /* Copy over the global and procedure linkage table refcount entries. | |
7291 | These may have been already set up by a check_relocs routine. */ | |
7292 | htab = elf_hash_table (info); | |
7293 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
7294 | { | |
7295 | if (dir->got.refcount < 0) | |
7296 | dir->got.refcount = 0; | |
7297 | dir->got.refcount += ind->got.refcount; | |
7298 | ind->got.refcount = htab->init_got_refcount.refcount; | |
7299 | } | |
7300 | ||
7301 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
7302 | { | |
7303 | if (dir->plt.refcount < 0) | |
7304 | dir->plt.refcount = 0; | |
7305 | dir->plt.refcount += ind->plt.refcount; | |
7306 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
7307 | } | |
7308 | ||
7309 | if (ind->dynindx != -1) | |
7310 | { | |
7311 | if (dir->dynindx != -1) | |
7312 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
7313 | dir->dynindx = ind->dynindx; | |
7314 | dir->dynstr_index = ind->dynstr_index; | |
7315 | ind->dynindx = -1; | |
7316 | ind->dynstr_index = 0; | |
7317 | } | |
7318 | } | |
7319 | ||
7320 | void | |
7321 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
7322 | struct elf_link_hash_entry *h, | |
7323 | bfd_boolean force_local) | |
7324 | { | |
3aa14d16 L |
7325 | /* STT_GNU_IFUNC symbol must go through PLT. */ |
7326 | if (h->type != STT_GNU_IFUNC) | |
7327 | { | |
7328 | h->plt = elf_hash_table (info)->init_plt_offset; | |
7329 | h->needs_plt = 0; | |
7330 | } | |
4d269e42 AM |
7331 | if (force_local) |
7332 | { | |
7333 | h->forced_local = 1; | |
7334 | if (h->dynindx != -1) | |
7335 | { | |
4d269e42 AM |
7336 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, |
7337 | h->dynstr_index); | |
641338d8 AM |
7338 | h->dynindx = -1; |
7339 | h->dynstr_index = 0; | |
4d269e42 AM |
7340 | } |
7341 | } | |
7342 | } | |
7343 | ||
7bf52ea2 AM |
7344 | /* Initialize an ELF linker hash table. *TABLE has been zeroed by our |
7345 | caller. */ | |
4d269e42 AM |
7346 | |
7347 | bfd_boolean | |
7348 | _bfd_elf_link_hash_table_init | |
7349 | (struct elf_link_hash_table *table, | |
7350 | bfd *abfd, | |
7351 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
7352 | struct bfd_hash_table *, | |
7353 | const char *), | |
4dfe6ac6 NC |
7354 | unsigned int entsize, |
7355 | enum elf_target_id target_id) | |
4d269e42 AM |
7356 | { |
7357 | bfd_boolean ret; | |
7358 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; | |
7359 | ||
4d269e42 AM |
7360 | table->init_got_refcount.refcount = can_refcount - 1; |
7361 | table->init_plt_refcount.refcount = can_refcount - 1; | |
7362 | table->init_got_offset.offset = -(bfd_vma) 1; | |
7363 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
7364 | /* The first dynamic symbol is a dummy. */ | |
7365 | table->dynsymcount = 1; | |
7366 | ||
7367 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
4dfe6ac6 | 7368 | |
4d269e42 | 7369 | table->root.type = bfd_link_elf_hash_table; |
4dfe6ac6 | 7370 | table->hash_table_id = target_id; |
4d269e42 AM |
7371 | |
7372 | return ret; | |
7373 | } | |
7374 | ||
7375 | /* Create an ELF linker hash table. */ | |
7376 | ||
7377 | struct bfd_link_hash_table * | |
7378 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
7379 | { | |
7380 | struct elf_link_hash_table *ret; | |
7381 | bfd_size_type amt = sizeof (struct elf_link_hash_table); | |
7382 | ||
7bf52ea2 | 7383 | ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); |
4d269e42 AM |
7384 | if (ret == NULL) |
7385 | return NULL; | |
7386 | ||
7387 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
4dfe6ac6 NC |
7388 | sizeof (struct elf_link_hash_entry), |
7389 | GENERIC_ELF_DATA)) | |
4d269e42 AM |
7390 | { |
7391 | free (ret); | |
7392 | return NULL; | |
7393 | } | |
d495ab0d | 7394 | ret->root.hash_table_free = _bfd_elf_link_hash_table_free; |
4d269e42 AM |
7395 | |
7396 | return &ret->root; | |
7397 | } | |
7398 | ||
9f7c3e5e AM |
7399 | /* Destroy an ELF linker hash table. */ |
7400 | ||
7401 | void | |
d495ab0d | 7402 | _bfd_elf_link_hash_table_free (bfd *obfd) |
9f7c3e5e | 7403 | { |
d495ab0d AM |
7404 | struct elf_link_hash_table *htab; |
7405 | ||
7406 | htab = (struct elf_link_hash_table *) obfd->link.hash; | |
9f7c3e5e AM |
7407 | if (htab->dynstr != NULL) |
7408 | _bfd_elf_strtab_free (htab->dynstr); | |
7409 | _bfd_merge_sections_free (htab->merge_info); | |
d495ab0d | 7410 | _bfd_generic_link_hash_table_free (obfd); |
9f7c3e5e AM |
7411 | } |
7412 | ||
4d269e42 AM |
7413 | /* This is a hook for the ELF emulation code in the generic linker to |
7414 | tell the backend linker what file name to use for the DT_NEEDED | |
7415 | entry for a dynamic object. */ | |
7416 | ||
7417 | void | |
7418 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
7419 | { | |
7420 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7421 | && bfd_get_format (abfd) == bfd_object) | |
7422 | elf_dt_name (abfd) = name; | |
7423 | } | |
7424 | ||
7425 | int | |
7426 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
7427 | { | |
7428 | int lib_class; | |
7429 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7430 | && bfd_get_format (abfd) == bfd_object) | |
7431 | lib_class = elf_dyn_lib_class (abfd); | |
7432 | else | |
7433 | lib_class = 0; | |
7434 | return lib_class; | |
7435 | } | |
7436 | ||
7437 | void | |
7438 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
7439 | { | |
7440 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7441 | && bfd_get_format (abfd) == bfd_object) | |
7442 | elf_dyn_lib_class (abfd) = lib_class; | |
7443 | } | |
7444 | ||
7445 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
7446 | the linker ELF emulation code. */ | |
7447 | ||
7448 | struct bfd_link_needed_list * | |
7449 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7450 | struct bfd_link_info *info) | |
7451 | { | |
7452 | if (! is_elf_hash_table (info->hash)) | |
7453 | return NULL; | |
7454 | return elf_hash_table (info)->needed; | |
7455 | } | |
7456 | ||
7457 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
7458 | hook for the linker ELF emulation code. */ | |
7459 | ||
7460 | struct bfd_link_needed_list * | |
7461 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7462 | struct bfd_link_info *info) | |
7463 | { | |
7464 | if (! is_elf_hash_table (info->hash)) | |
7465 | return NULL; | |
7466 | return elf_hash_table (info)->runpath; | |
7467 | } | |
7468 | ||
7469 | /* Get the name actually used for a dynamic object for a link. This | |
7470 | is the SONAME entry if there is one. Otherwise, it is the string | |
7471 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
7472 | ||
7473 | const char * | |
7474 | bfd_elf_get_dt_soname (bfd *abfd) | |
7475 | { | |
7476 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7477 | && bfd_get_format (abfd) == bfd_object) | |
7478 | return elf_dt_name (abfd); | |
7479 | return NULL; | |
7480 | } | |
7481 | ||
7482 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
7483 | the ELF linker emulation code. */ | |
7484 | ||
7485 | bfd_boolean | |
7486 | bfd_elf_get_bfd_needed_list (bfd *abfd, | |
7487 | struct bfd_link_needed_list **pneeded) | |
7488 | { | |
7489 | asection *s; | |
7490 | bfd_byte *dynbuf = NULL; | |
cb33740c | 7491 | unsigned int elfsec; |
4d269e42 AM |
7492 | unsigned long shlink; |
7493 | bfd_byte *extdyn, *extdynend; | |
7494 | size_t extdynsize; | |
7495 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
7496 | ||
7497 | *pneeded = NULL; | |
7498 | ||
7499 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
7500 | || bfd_get_format (abfd) != bfd_object) | |
7501 | return TRUE; | |
7502 | ||
7503 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
7504 | if (s == NULL || s->size == 0) | |
7505 | return TRUE; | |
7506 | ||
7507 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
7508 | goto error_return; | |
7509 | ||
7510 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 7511 | if (elfsec == SHN_BAD) |
4d269e42 AM |
7512 | goto error_return; |
7513 | ||
7514 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 7515 | |
4d269e42 AM |
7516 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
7517 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
7518 | ||
7519 | extdyn = dynbuf; | |
7520 | extdynend = extdyn + s->size; | |
7521 | for (; extdyn < extdynend; extdyn += extdynsize) | |
7522 | { | |
7523 | Elf_Internal_Dyn dyn; | |
7524 | ||
7525 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
7526 | ||
7527 | if (dyn.d_tag == DT_NULL) | |
7528 | break; | |
7529 | ||
7530 | if (dyn.d_tag == DT_NEEDED) | |
7531 | { | |
7532 | const char *string; | |
7533 | struct bfd_link_needed_list *l; | |
7534 | unsigned int tagv = dyn.d_un.d_val; | |
7535 | bfd_size_type amt; | |
7536 | ||
7537 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
7538 | if (string == NULL) | |
7539 | goto error_return; | |
7540 | ||
7541 | amt = sizeof *l; | |
a50b1753 | 7542 | l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4d269e42 AM |
7543 | if (l == NULL) |
7544 | goto error_return; | |
7545 | ||
7546 | l->by = abfd; | |
7547 | l->name = string; | |
7548 | l->next = *pneeded; | |
7549 | *pneeded = l; | |
7550 | } | |
7551 | } | |
7552 | ||
7553 | free (dynbuf); | |
7554 | ||
7555 | return TRUE; | |
7556 | ||
7557 | error_return: | |
7558 | if (dynbuf != NULL) | |
7559 | free (dynbuf); | |
7560 | return FALSE; | |
7561 | } | |
7562 | ||
7563 | struct elf_symbuf_symbol | |
7564 | { | |
7565 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
7566 | unsigned char st_info; /* Type and binding attributes */ | |
7567 | unsigned char st_other; /* Visibilty, and target specific */ | |
7568 | }; | |
7569 | ||
7570 | struct elf_symbuf_head | |
7571 | { | |
7572 | struct elf_symbuf_symbol *ssym; | |
ef53be89 | 7573 | size_t count; |
4d269e42 AM |
7574 | unsigned int st_shndx; |
7575 | }; | |
7576 | ||
7577 | struct elf_symbol | |
7578 | { | |
7579 | union | |
7580 | { | |
7581 | Elf_Internal_Sym *isym; | |
7582 | struct elf_symbuf_symbol *ssym; | |
7583 | } u; | |
7584 | const char *name; | |
7585 | }; | |
7586 | ||
7587 | /* Sort references to symbols by ascending section number. */ | |
7588 | ||
7589 | static int | |
7590 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
7591 | { | |
7592 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
7593 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
7594 | ||
7595 | return s1->st_shndx - s2->st_shndx; | |
7596 | } | |
7597 | ||
7598 | static int | |
7599 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
7600 | { | |
7601 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
7602 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
7603 | return strcmp (s1->name, s2->name); | |
7604 | } | |
7605 | ||
7606 | static struct elf_symbuf_head * | |
ef53be89 | 7607 | elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) |
4d269e42 | 7608 | { |
14b1c01e | 7609 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
7610 | struct elf_symbuf_symbol *ssym; |
7611 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
ef53be89 | 7612 | size_t i, shndx_count, total_size; |
4d269e42 | 7613 | |
a50b1753 | 7614 | indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); |
4d269e42 AM |
7615 | if (indbuf == NULL) |
7616 | return NULL; | |
7617 | ||
7618 | for (ind = indbuf, i = 0; i < symcount; i++) | |
7619 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
7620 | *ind++ = &isymbuf[i]; | |
7621 | indbufend = ind; | |
7622 | ||
7623 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
7624 | elf_sort_elf_symbol); | |
7625 | ||
7626 | shndx_count = 0; | |
7627 | if (indbufend > indbuf) | |
7628 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
7629 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
7630 | shndx_count++; | |
7631 | ||
3ae181ee L |
7632 | total_size = ((shndx_count + 1) * sizeof (*ssymbuf) |
7633 | + (indbufend - indbuf) * sizeof (*ssym)); | |
a50b1753 | 7634 | ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); |
4d269e42 AM |
7635 | if (ssymbuf == NULL) |
7636 | { | |
7637 | free (indbuf); | |
7638 | return NULL; | |
7639 | } | |
7640 | ||
3ae181ee | 7641 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); |
4d269e42 AM |
7642 | ssymbuf->ssym = NULL; |
7643 | ssymbuf->count = shndx_count; | |
7644 | ssymbuf->st_shndx = 0; | |
7645 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
7646 | { | |
7647 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
7648 | { | |
7649 | ssymhead++; | |
7650 | ssymhead->ssym = ssym; | |
7651 | ssymhead->count = 0; | |
7652 | ssymhead->st_shndx = (*ind)->st_shndx; | |
7653 | } | |
7654 | ssym->st_name = (*ind)->st_name; | |
7655 | ssym->st_info = (*ind)->st_info; | |
7656 | ssym->st_other = (*ind)->st_other; | |
7657 | ssymhead->count++; | |
7658 | } | |
ef53be89 | 7659 | BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count |
3ae181ee L |
7660 | && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) |
7661 | == total_size)); | |
4d269e42 AM |
7662 | |
7663 | free (indbuf); | |
7664 | return ssymbuf; | |
7665 | } | |
7666 | ||
7667 | /* Check if 2 sections define the same set of local and global | |
7668 | symbols. */ | |
7669 | ||
8f317e31 | 7670 | static bfd_boolean |
4d269e42 AM |
7671 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
7672 | struct bfd_link_info *info) | |
7673 | { | |
7674 | bfd *bfd1, *bfd2; | |
7675 | const struct elf_backend_data *bed1, *bed2; | |
7676 | Elf_Internal_Shdr *hdr1, *hdr2; | |
ef53be89 | 7677 | size_t symcount1, symcount2; |
4d269e42 AM |
7678 | Elf_Internal_Sym *isymbuf1, *isymbuf2; |
7679 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
7680 | Elf_Internal_Sym *isym, *isymend; | |
7681 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
ef53be89 | 7682 | size_t count1, count2, i; |
cb33740c | 7683 | unsigned int shndx1, shndx2; |
4d269e42 AM |
7684 | bfd_boolean result; |
7685 | ||
7686 | bfd1 = sec1->owner; | |
7687 | bfd2 = sec2->owner; | |
7688 | ||
4d269e42 AM |
7689 | /* Both sections have to be in ELF. */ |
7690 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
7691 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
7692 | return FALSE; | |
7693 | ||
7694 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
7695 | return FALSE; | |
7696 | ||
4d269e42 AM |
7697 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
7698 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
cb33740c | 7699 | if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) |
4d269e42 AM |
7700 | return FALSE; |
7701 | ||
7702 | bed1 = get_elf_backend_data (bfd1); | |
7703 | bed2 = get_elf_backend_data (bfd2); | |
7704 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
7705 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
7706 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
7707 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
7708 | ||
7709 | if (symcount1 == 0 || symcount2 == 0) | |
7710 | return FALSE; | |
7711 | ||
7712 | result = FALSE; | |
7713 | isymbuf1 = NULL; | |
7714 | isymbuf2 = NULL; | |
a50b1753 NC |
7715 | ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; |
7716 | ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; | |
4d269e42 AM |
7717 | |
7718 | if (ssymbuf1 == NULL) | |
7719 | { | |
7720 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
7721 | NULL, NULL, NULL); | |
7722 | if (isymbuf1 == NULL) | |
7723 | goto done; | |
7724 | ||
7725 | if (!info->reduce_memory_overheads) | |
7726 | elf_tdata (bfd1)->symbuf = ssymbuf1 | |
7727 | = elf_create_symbuf (symcount1, isymbuf1); | |
7728 | } | |
7729 | ||
7730 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
7731 | { | |
7732 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
7733 | NULL, NULL, NULL); | |
7734 | if (isymbuf2 == NULL) | |
7735 | goto done; | |
7736 | ||
7737 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
7738 | elf_tdata (bfd2)->symbuf = ssymbuf2 | |
7739 | = elf_create_symbuf (symcount2, isymbuf2); | |
7740 | } | |
7741 | ||
7742 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
7743 | { | |
7744 | /* Optimized faster version. */ | |
ef53be89 | 7745 | size_t lo, hi, mid; |
4d269e42 AM |
7746 | struct elf_symbol *symp; |
7747 | struct elf_symbuf_symbol *ssym, *ssymend; | |
7748 | ||
7749 | lo = 0; | |
7750 | hi = ssymbuf1->count; | |
7751 | ssymbuf1++; | |
7752 | count1 = 0; | |
7753 | while (lo < hi) | |
7754 | { | |
7755 | mid = (lo + hi) / 2; | |
cb33740c | 7756 | if (shndx1 < ssymbuf1[mid].st_shndx) |
4d269e42 | 7757 | hi = mid; |
cb33740c | 7758 | else if (shndx1 > ssymbuf1[mid].st_shndx) |
4d269e42 AM |
7759 | lo = mid + 1; |
7760 | else | |
7761 | { | |
7762 | count1 = ssymbuf1[mid].count; | |
7763 | ssymbuf1 += mid; | |
7764 | break; | |
7765 | } | |
7766 | } | |
7767 | ||
7768 | lo = 0; | |
7769 | hi = ssymbuf2->count; | |
7770 | ssymbuf2++; | |
7771 | count2 = 0; | |
7772 | while (lo < hi) | |
7773 | { | |
7774 | mid = (lo + hi) / 2; | |
cb33740c | 7775 | if (shndx2 < ssymbuf2[mid].st_shndx) |
4d269e42 | 7776 | hi = mid; |
cb33740c | 7777 | else if (shndx2 > ssymbuf2[mid].st_shndx) |
4d269e42 AM |
7778 | lo = mid + 1; |
7779 | else | |
7780 | { | |
7781 | count2 = ssymbuf2[mid].count; | |
7782 | ssymbuf2 += mid; | |
7783 | break; | |
7784 | } | |
7785 | } | |
7786 | ||
7787 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7788 | goto done; | |
7789 | ||
ca4be51c AM |
7790 | symtable1 |
7791 | = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); | |
7792 | symtable2 | |
7793 | = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); | |
4d269e42 AM |
7794 | if (symtable1 == NULL || symtable2 == NULL) |
7795 | goto done; | |
7796 | ||
7797 | symp = symtable1; | |
7798 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
7799 | ssym < ssymend; ssym++, symp++) | |
7800 | { | |
7801 | symp->u.ssym = ssym; | |
7802 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
7803 | hdr1->sh_link, | |
7804 | ssym->st_name); | |
7805 | } | |
7806 | ||
7807 | symp = symtable2; | |
7808 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
7809 | ssym < ssymend; ssym++, symp++) | |
7810 | { | |
7811 | symp->u.ssym = ssym; | |
7812 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
7813 | hdr2->sh_link, | |
7814 | ssym->st_name); | |
7815 | } | |
7816 | ||
7817 | /* Sort symbol by name. */ | |
7818 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7819 | elf_sym_name_compare); | |
7820 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7821 | elf_sym_name_compare); | |
7822 | ||
7823 | for (i = 0; i < count1; i++) | |
7824 | /* Two symbols must have the same binding, type and name. */ | |
7825 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
7826 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
7827 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7828 | goto done; | |
7829 | ||
7830 | result = TRUE; | |
7831 | goto done; | |
7832 | } | |
7833 | ||
a50b1753 NC |
7834 | symtable1 = (struct elf_symbol *) |
7835 | bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
7836 | symtable2 = (struct elf_symbol *) | |
7837 | bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
4d269e42 AM |
7838 | if (symtable1 == NULL || symtable2 == NULL) |
7839 | goto done; | |
7840 | ||
7841 | /* Count definitions in the section. */ | |
7842 | count1 = 0; | |
7843 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
cb33740c | 7844 | if (isym->st_shndx == shndx1) |
4d269e42 AM |
7845 | symtable1[count1++].u.isym = isym; |
7846 | ||
7847 | count2 = 0; | |
7848 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
cb33740c | 7849 | if (isym->st_shndx == shndx2) |
4d269e42 AM |
7850 | symtable2[count2++].u.isym = isym; |
7851 | ||
7852 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7853 | goto done; | |
7854 | ||
7855 | for (i = 0; i < count1; i++) | |
7856 | symtable1[i].name | |
7857 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
7858 | symtable1[i].u.isym->st_name); | |
7859 | ||
7860 | for (i = 0; i < count2; i++) | |
7861 | symtable2[i].name | |
7862 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
7863 | symtable2[i].u.isym->st_name); | |
7864 | ||
7865 | /* Sort symbol by name. */ | |
7866 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7867 | elf_sym_name_compare); | |
7868 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7869 | elf_sym_name_compare); | |
7870 | ||
7871 | for (i = 0; i < count1; i++) | |
7872 | /* Two symbols must have the same binding, type and name. */ | |
7873 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
7874 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
7875 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7876 | goto done; | |
7877 | ||
7878 | result = TRUE; | |
7879 | ||
7880 | done: | |
7881 | if (symtable1) | |
7882 | free (symtable1); | |
7883 | if (symtable2) | |
7884 | free (symtable2); | |
7885 | if (isymbuf1) | |
7886 | free (isymbuf1); | |
7887 | if (isymbuf2) | |
7888 | free (isymbuf2); | |
7889 | ||
7890 | return result; | |
7891 | } | |
7892 | ||
7893 | /* Return TRUE if 2 section types are compatible. */ | |
7894 | ||
7895 | bfd_boolean | |
7896 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
7897 | bfd *bbfd, const asection *bsec) | |
7898 | { | |
7899 | if (asec == NULL | |
7900 | || bsec == NULL | |
7901 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
7902 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
7903 | return TRUE; | |
7904 | ||
7905 | return elf_section_type (asec) == elf_section_type (bsec); | |
7906 | } | |
7907 | \f | |
c152c796 AM |
7908 | /* Final phase of ELF linker. */ |
7909 | ||
7910 | /* A structure we use to avoid passing large numbers of arguments. */ | |
7911 | ||
7912 | struct elf_final_link_info | |
7913 | { | |
7914 | /* General link information. */ | |
7915 | struct bfd_link_info *info; | |
7916 | /* Output BFD. */ | |
7917 | bfd *output_bfd; | |
7918 | /* Symbol string table. */ | |
ef10c3ac | 7919 | struct elf_strtab_hash *symstrtab; |
c152c796 AM |
7920 | /* .hash section. */ |
7921 | asection *hash_sec; | |
7922 | /* symbol version section (.gnu.version). */ | |
7923 | asection *symver_sec; | |
7924 | /* Buffer large enough to hold contents of any section. */ | |
7925 | bfd_byte *contents; | |
7926 | /* Buffer large enough to hold external relocs of any section. */ | |
7927 | void *external_relocs; | |
7928 | /* Buffer large enough to hold internal relocs of any section. */ | |
7929 | Elf_Internal_Rela *internal_relocs; | |
7930 | /* Buffer large enough to hold external local symbols of any input | |
7931 | BFD. */ | |
7932 | bfd_byte *external_syms; | |
7933 | /* And a buffer for symbol section indices. */ | |
7934 | Elf_External_Sym_Shndx *locsym_shndx; | |
7935 | /* Buffer large enough to hold internal local symbols of any input | |
7936 | BFD. */ | |
7937 | Elf_Internal_Sym *internal_syms; | |
7938 | /* Array large enough to hold a symbol index for each local symbol | |
7939 | of any input BFD. */ | |
7940 | long *indices; | |
7941 | /* Array large enough to hold a section pointer for each local | |
7942 | symbol of any input BFD. */ | |
7943 | asection **sections; | |
ef10c3ac | 7944 | /* Buffer for SHT_SYMTAB_SHNDX section. */ |
c152c796 | 7945 | Elf_External_Sym_Shndx *symshndxbuf; |
ffbc01cc AM |
7946 | /* Number of STT_FILE syms seen. */ |
7947 | size_t filesym_count; | |
c152c796 AM |
7948 | }; |
7949 | ||
7950 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
7951 | ||
7952 | struct elf_outext_info | |
7953 | { | |
7954 | bfd_boolean failed; | |
7955 | bfd_boolean localsyms; | |
34a79995 | 7956 | bfd_boolean file_sym_done; |
8b127cbc | 7957 | struct elf_final_link_info *flinfo; |
c152c796 AM |
7958 | }; |
7959 | ||
d9352518 DB |
7960 | |
7961 | /* Support for evaluating a complex relocation. | |
7962 | ||
7963 | Complex relocations are generalized, self-describing relocations. The | |
7964 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 7965 | relocations themselves. |
d9352518 DB |
7966 | |
7967 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
7968 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
7969 | information (start bit, end bit, word width, etc) into the addend. This | |
7970 | information is extracted from CGEN-generated operand tables within gas. | |
7971 | ||
7972 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
7973 | internal) representing prefix-notation expressions, including but not | |
7974 | limited to those sorts of expressions normally encoded as addends in the | |
7975 | addend field. The symbol mangling format is: | |
7976 | ||
7977 | <node> := <literal> | |
7978 | | <unary-operator> ':' <node> | |
7979 | | <binary-operator> ':' <node> ':' <node> | |
7980 | ; | |
7981 | ||
7982 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
7983 | | 'S' <digits=N> ':' <N character section name> | |
7984 | | '#' <hexdigits> | |
7985 | ; | |
7986 | ||
7987 | <binary-operator> := as in C | |
7988 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
7989 | ||
7990 | static void | |
a0c8462f AM |
7991 | set_symbol_value (bfd *bfd_with_globals, |
7992 | Elf_Internal_Sym *isymbuf, | |
7993 | size_t locsymcount, | |
7994 | size_t symidx, | |
7995 | bfd_vma val) | |
d9352518 | 7996 | { |
8977835c AM |
7997 | struct elf_link_hash_entry **sym_hashes; |
7998 | struct elf_link_hash_entry *h; | |
7999 | size_t extsymoff = locsymcount; | |
d9352518 | 8000 | |
8977835c | 8001 | if (symidx < locsymcount) |
d9352518 | 8002 | { |
8977835c AM |
8003 | Elf_Internal_Sym *sym; |
8004 | ||
8005 | sym = isymbuf + symidx; | |
8006 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
8007 | { | |
8008 | /* It is a local symbol: move it to the | |
8009 | "absolute" section and give it a value. */ | |
8010 | sym->st_shndx = SHN_ABS; | |
8011 | sym->st_value = val; | |
8012 | return; | |
8013 | } | |
8014 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
8015 | extsymoff = 0; | |
d9352518 | 8016 | } |
8977835c AM |
8017 | |
8018 | /* It is a global symbol: set its link type | |
8019 | to "defined" and give it a value. */ | |
8020 | ||
8021 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
8022 | h = sym_hashes [symidx - extsymoff]; | |
8023 | while (h->root.type == bfd_link_hash_indirect | |
8024 | || h->root.type == bfd_link_hash_warning) | |
8025 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8026 | h->root.type = bfd_link_hash_defined; | |
8027 | h->root.u.def.value = val; | |
8028 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
8029 | } |
8030 | ||
a0c8462f AM |
8031 | static bfd_boolean |
8032 | resolve_symbol (const char *name, | |
8033 | bfd *input_bfd, | |
8b127cbc | 8034 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8035 | bfd_vma *result, |
8036 | Elf_Internal_Sym *isymbuf, | |
8037 | size_t locsymcount) | |
d9352518 | 8038 | { |
a0c8462f AM |
8039 | Elf_Internal_Sym *sym; |
8040 | struct bfd_link_hash_entry *global_entry; | |
8041 | const char *candidate = NULL; | |
8042 | Elf_Internal_Shdr *symtab_hdr; | |
8043 | size_t i; | |
8044 | ||
d9352518 DB |
8045 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
8046 | ||
8047 | for (i = 0; i < locsymcount; ++ i) | |
8048 | { | |
8977835c | 8049 | sym = isymbuf + i; |
d9352518 DB |
8050 | |
8051 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
8052 | continue; | |
8053 | ||
8054 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
8055 | symtab_hdr->sh_link, | |
8056 | sym->st_name); | |
8057 | #ifdef DEBUG | |
0f02bbd9 AM |
8058 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
8059 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
8060 | #endif |
8061 | if (candidate && strcmp (candidate, name) == 0) | |
8062 | { | |
8b127cbc | 8063 | asection *sec = flinfo->sections [i]; |
d9352518 | 8064 | |
0f02bbd9 AM |
8065 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
8066 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 8067 | #ifdef DEBUG |
0f02bbd9 AM |
8068 | printf ("Found symbol with value %8.8lx\n", |
8069 | (unsigned long) *result); | |
d9352518 DB |
8070 | #endif |
8071 | return TRUE; | |
8072 | } | |
8073 | } | |
8074 | ||
8075 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
8b127cbc | 8076 | global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, |
a0c8462f | 8077 | FALSE, FALSE, TRUE); |
d9352518 DB |
8078 | if (!global_entry) |
8079 | return FALSE; | |
a0c8462f | 8080 | |
d9352518 DB |
8081 | if (global_entry->type == bfd_link_hash_defined |
8082 | || global_entry->type == bfd_link_hash_defweak) | |
8083 | { | |
a0c8462f AM |
8084 | *result = (global_entry->u.def.value |
8085 | + global_entry->u.def.section->output_section->vma | |
8086 | + global_entry->u.def.section->output_offset); | |
d9352518 | 8087 | #ifdef DEBUG |
0f02bbd9 AM |
8088 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
8089 | global_entry->root.string, (unsigned long) *result); | |
d9352518 DB |
8090 | #endif |
8091 | return TRUE; | |
a0c8462f | 8092 | } |
d9352518 | 8093 | |
d9352518 DB |
8094 | return FALSE; |
8095 | } | |
8096 | ||
37b01f6a DG |
8097 | /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in |
8098 | bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section | |
8099 | names like "foo.end" which is the end address of section "foo". */ | |
8100 | ||
d9352518 | 8101 | static bfd_boolean |
a0c8462f AM |
8102 | resolve_section (const char *name, |
8103 | asection *sections, | |
37b01f6a DG |
8104 | bfd_vma *result, |
8105 | bfd * abfd) | |
d9352518 | 8106 | { |
a0c8462f AM |
8107 | asection *curr; |
8108 | unsigned int len; | |
d9352518 | 8109 | |
a0c8462f | 8110 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8111 | if (strcmp (curr->name, name) == 0) |
8112 | { | |
8113 | *result = curr->vma; | |
8114 | return TRUE; | |
8115 | } | |
8116 | ||
8117 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
37b01f6a | 8118 | /* FIXME: This could be coded more efficiently... */ |
a0c8462f | 8119 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8120 | { |
8121 | len = strlen (curr->name); | |
a0c8462f | 8122 | if (len > strlen (name)) |
d9352518 DB |
8123 | continue; |
8124 | ||
8125 | if (strncmp (curr->name, name, len) == 0) | |
8126 | { | |
8127 | if (strncmp (".end", name + len, 4) == 0) | |
8128 | { | |
37b01f6a | 8129 | *result = curr->vma + curr->size / bfd_octets_per_byte (abfd); |
d9352518 DB |
8130 | return TRUE; |
8131 | } | |
8132 | ||
8133 | /* Insert more pseudo-section names here, if you like. */ | |
8134 | } | |
8135 | } | |
a0c8462f | 8136 | |
d9352518 DB |
8137 | return FALSE; |
8138 | } | |
8139 | ||
8140 | static void | |
a0c8462f | 8141 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 8142 | { |
695344c0 | 8143 | /* xgettext:c-format */ |
a0c8462f AM |
8144 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
8145 | reftype, name); | |
d9352518 DB |
8146 | } |
8147 | ||
8148 | static bfd_boolean | |
a0c8462f AM |
8149 | eval_symbol (bfd_vma *result, |
8150 | const char **symp, | |
8151 | bfd *input_bfd, | |
8b127cbc | 8152 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8153 | bfd_vma dot, |
8154 | Elf_Internal_Sym *isymbuf, | |
8155 | size_t locsymcount, | |
8156 | int signed_p) | |
d9352518 | 8157 | { |
4b93929b NC |
8158 | size_t len; |
8159 | size_t symlen; | |
a0c8462f AM |
8160 | bfd_vma a; |
8161 | bfd_vma b; | |
4b93929b | 8162 | char symbuf[4096]; |
0f02bbd9 | 8163 | const char *sym = *symp; |
a0c8462f AM |
8164 | const char *symend; |
8165 | bfd_boolean symbol_is_section = FALSE; | |
d9352518 DB |
8166 | |
8167 | len = strlen (sym); | |
8168 | symend = sym + len; | |
8169 | ||
4b93929b | 8170 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
8171 | { |
8172 | bfd_set_error (bfd_error_invalid_operation); | |
8173 | return FALSE; | |
8174 | } | |
a0c8462f | 8175 | |
d9352518 DB |
8176 | switch (* sym) |
8177 | { | |
8178 | case '.': | |
0f02bbd9 AM |
8179 | *result = dot; |
8180 | *symp = sym + 1; | |
d9352518 DB |
8181 | return TRUE; |
8182 | ||
8183 | case '#': | |
0f02bbd9 AM |
8184 | ++sym; |
8185 | *result = strtoul (sym, (char **) symp, 16); | |
d9352518 DB |
8186 | return TRUE; |
8187 | ||
8188 | case 'S': | |
8189 | symbol_is_section = TRUE; | |
1a0670f3 | 8190 | /* Fall through. */ |
a0c8462f | 8191 | case 's': |
0f02bbd9 AM |
8192 | ++sym; |
8193 | symlen = strtol (sym, (char **) symp, 10); | |
8194 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 8195 | |
4b93929b | 8196 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
8197 | { |
8198 | bfd_set_error (bfd_error_invalid_operation); | |
8199 | return FALSE; | |
8200 | } | |
8201 | ||
8202 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 8203 | symbuf[symlen] = '\0'; |
0f02bbd9 | 8204 | *symp = sym + symlen; |
a0c8462f AM |
8205 | |
8206 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
8207 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
8208 | interpretation here; section means "try section first", not "must be a | |
8209 | section", and likewise with symbol. */ | |
8210 | ||
a0c8462f | 8211 | if (symbol_is_section) |
d9352518 | 8212 | { |
37b01f6a | 8213 | if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) |
8b127cbc | 8214 | && !resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8215 | isymbuf, locsymcount)) |
d9352518 DB |
8216 | { |
8217 | undefined_reference ("section", symbuf); | |
8218 | return FALSE; | |
8219 | } | |
a0c8462f AM |
8220 | } |
8221 | else | |
d9352518 | 8222 | { |
8b127cbc | 8223 | if (!resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8224 | isymbuf, locsymcount) |
8b127cbc | 8225 | && !resolve_section (symbuf, flinfo->output_bfd->sections, |
37b01f6a | 8226 | result, input_bfd)) |
d9352518 DB |
8227 | { |
8228 | undefined_reference ("symbol", symbuf); | |
8229 | return FALSE; | |
8230 | } | |
8231 | } | |
8232 | ||
8233 | return TRUE; | |
a0c8462f | 8234 | |
d9352518 DB |
8235 | /* All that remains are operators. */ |
8236 | ||
8237 | #define UNARY_OP(op) \ | |
8238 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8239 | { \ | |
8240 | sym += strlen (#op); \ | |
a0c8462f AM |
8241 | if (*sym == ':') \ |
8242 | ++sym; \ | |
0f02bbd9 | 8243 | *symp = sym; \ |
8b127cbc | 8244 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8245 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8246 | return FALSE; \ |
8247 | if (signed_p) \ | |
0f02bbd9 | 8248 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
8249 | else \ |
8250 | *result = op a; \ | |
d9352518 DB |
8251 | return TRUE; \ |
8252 | } | |
8253 | ||
8254 | #define BINARY_OP(op) \ | |
8255 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8256 | { \ | |
8257 | sym += strlen (#op); \ | |
a0c8462f AM |
8258 | if (*sym == ':') \ |
8259 | ++sym; \ | |
0f02bbd9 | 8260 | *symp = sym; \ |
8b127cbc | 8261 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8262 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f | 8263 | return FALSE; \ |
0f02bbd9 | 8264 | ++*symp; \ |
8b127cbc | 8265 | if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8266 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8267 | return FALSE; \ |
8268 | if (signed_p) \ | |
0f02bbd9 | 8269 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
8270 | else \ |
8271 | *result = a op b; \ | |
d9352518 DB |
8272 | return TRUE; \ |
8273 | } | |
8274 | ||
8275 | default: | |
8276 | UNARY_OP (0-); | |
8277 | BINARY_OP (<<); | |
8278 | BINARY_OP (>>); | |
8279 | BINARY_OP (==); | |
8280 | BINARY_OP (!=); | |
8281 | BINARY_OP (<=); | |
8282 | BINARY_OP (>=); | |
8283 | BINARY_OP (&&); | |
8284 | BINARY_OP (||); | |
8285 | UNARY_OP (~); | |
8286 | UNARY_OP (!); | |
8287 | BINARY_OP (*); | |
8288 | BINARY_OP (/); | |
8289 | BINARY_OP (%); | |
8290 | BINARY_OP (^); | |
8291 | BINARY_OP (|); | |
8292 | BINARY_OP (&); | |
8293 | BINARY_OP (+); | |
8294 | BINARY_OP (-); | |
8295 | BINARY_OP (<); | |
8296 | BINARY_OP (>); | |
8297 | #undef UNARY_OP | |
8298 | #undef BINARY_OP | |
8299 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
8300 | bfd_set_error (bfd_error_invalid_operation); | |
8301 | return FALSE; | |
8302 | } | |
8303 | } | |
8304 | ||
d9352518 | 8305 | static void |
a0c8462f AM |
8306 | put_value (bfd_vma size, |
8307 | unsigned long chunksz, | |
8308 | bfd *input_bfd, | |
8309 | bfd_vma x, | |
8310 | bfd_byte *location) | |
d9352518 DB |
8311 | { |
8312 | location += (size - chunksz); | |
8313 | ||
41cd1ad1 | 8314 | for (; size; size -= chunksz, location -= chunksz) |
d9352518 DB |
8315 | { |
8316 | switch (chunksz) | |
8317 | { | |
d9352518 DB |
8318 | case 1: |
8319 | bfd_put_8 (input_bfd, x, location); | |
41cd1ad1 | 8320 | x >>= 8; |
d9352518 DB |
8321 | break; |
8322 | case 2: | |
8323 | bfd_put_16 (input_bfd, x, location); | |
41cd1ad1 | 8324 | x >>= 16; |
d9352518 DB |
8325 | break; |
8326 | case 4: | |
8327 | bfd_put_32 (input_bfd, x, location); | |
65164438 NC |
8328 | /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ |
8329 | x >>= 16; | |
8330 | x >>= 16; | |
d9352518 | 8331 | break; |
d9352518 | 8332 | #ifdef BFD64 |
41cd1ad1 | 8333 | case 8: |
d9352518 | 8334 | bfd_put_64 (input_bfd, x, location); |
41cd1ad1 NC |
8335 | /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ |
8336 | x >>= 32; | |
8337 | x >>= 32; | |
8338 | break; | |
d9352518 | 8339 | #endif |
41cd1ad1 NC |
8340 | default: |
8341 | abort (); | |
d9352518 DB |
8342 | break; |
8343 | } | |
8344 | } | |
8345 | } | |
8346 | ||
a0c8462f AM |
8347 | static bfd_vma |
8348 | get_value (bfd_vma size, | |
8349 | unsigned long chunksz, | |
8350 | bfd *input_bfd, | |
8351 | bfd_byte *location) | |
d9352518 | 8352 | { |
9b239e0e | 8353 | int shift; |
d9352518 DB |
8354 | bfd_vma x = 0; |
8355 | ||
9b239e0e NC |
8356 | /* Sanity checks. */ |
8357 | BFD_ASSERT (chunksz <= sizeof (x) | |
8358 | && size >= chunksz | |
8359 | && chunksz != 0 | |
8360 | && (size % chunksz) == 0 | |
8361 | && input_bfd != NULL | |
8362 | && location != NULL); | |
8363 | ||
8364 | if (chunksz == sizeof (x)) | |
8365 | { | |
8366 | BFD_ASSERT (size == chunksz); | |
8367 | ||
8368 | /* Make sure that we do not perform an undefined shift operation. | |
8369 | We know that size == chunksz so there will only be one iteration | |
8370 | of the loop below. */ | |
8371 | shift = 0; | |
8372 | } | |
8373 | else | |
8374 | shift = 8 * chunksz; | |
8375 | ||
a0c8462f | 8376 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
8377 | { |
8378 | switch (chunksz) | |
8379 | { | |
d9352518 | 8380 | case 1: |
9b239e0e | 8381 | x = (x << shift) | bfd_get_8 (input_bfd, location); |
d9352518 DB |
8382 | break; |
8383 | case 2: | |
9b239e0e | 8384 | x = (x << shift) | bfd_get_16 (input_bfd, location); |
d9352518 DB |
8385 | break; |
8386 | case 4: | |
9b239e0e | 8387 | x = (x << shift) | bfd_get_32 (input_bfd, location); |
d9352518 | 8388 | break; |
d9352518 | 8389 | #ifdef BFD64 |
9b239e0e NC |
8390 | case 8: |
8391 | x = (x << shift) | bfd_get_64 (input_bfd, location); | |
d9352518 | 8392 | break; |
9b239e0e NC |
8393 | #endif |
8394 | default: | |
8395 | abort (); | |
d9352518 DB |
8396 | } |
8397 | } | |
8398 | return x; | |
8399 | } | |
8400 | ||
a0c8462f AM |
8401 | static void |
8402 | decode_complex_addend (unsigned long *start, /* in bits */ | |
8403 | unsigned long *oplen, /* in bits */ | |
8404 | unsigned long *len, /* in bits */ | |
8405 | unsigned long *wordsz, /* in bytes */ | |
8406 | unsigned long *chunksz, /* in bytes */ | |
8407 | unsigned long *lsb0_p, | |
8408 | unsigned long *signed_p, | |
8409 | unsigned long *trunc_p, | |
8410 | unsigned long encoded) | |
d9352518 DB |
8411 | { |
8412 | * start = encoded & 0x3F; | |
8413 | * len = (encoded >> 6) & 0x3F; | |
8414 | * oplen = (encoded >> 12) & 0x3F; | |
8415 | * wordsz = (encoded >> 18) & 0xF; | |
8416 | * chunksz = (encoded >> 22) & 0xF; | |
8417 | * lsb0_p = (encoded >> 27) & 1; | |
8418 | * signed_p = (encoded >> 28) & 1; | |
8419 | * trunc_p = (encoded >> 29) & 1; | |
8420 | } | |
8421 | ||
cdfeee4f | 8422 | bfd_reloc_status_type |
0f02bbd9 | 8423 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
cdfeee4f | 8424 | asection *input_section ATTRIBUTE_UNUSED, |
0f02bbd9 AM |
8425 | bfd_byte *contents, |
8426 | Elf_Internal_Rela *rel, | |
8427 | bfd_vma relocation) | |
d9352518 | 8428 | { |
0f02bbd9 AM |
8429 | bfd_vma shift, x, mask; |
8430 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 8431 | bfd_reloc_status_type r; |
d9352518 DB |
8432 | |
8433 | /* Perform this reloc, since it is complex. | |
8434 | (this is not to say that it necessarily refers to a complex | |
8435 | symbol; merely that it is a self-describing CGEN based reloc. | |
8436 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 8437 | word size, etc) encoded within it.). */ |
d9352518 | 8438 | |
a0c8462f AM |
8439 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
8440 | &chunksz, &lsb0_p, &signed_p, | |
8441 | &trunc_p, rel->r_addend); | |
d9352518 DB |
8442 | |
8443 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
8444 | ||
8445 | if (lsb0_p) | |
8446 | shift = (start + 1) - len; | |
8447 | else | |
8448 | shift = (8 * wordsz) - (start + len); | |
8449 | ||
37b01f6a DG |
8450 | x = get_value (wordsz, chunksz, input_bfd, |
8451 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
d9352518 DB |
8452 | |
8453 | #ifdef DEBUG | |
8454 | printf ("Doing complex reloc: " | |
8455 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
8456 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
8457 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
8458 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
9ccb8af9 AM |
8459 | oplen, (unsigned long) x, (unsigned long) mask, |
8460 | (unsigned long) relocation); | |
d9352518 DB |
8461 | #endif |
8462 | ||
cdfeee4f | 8463 | r = bfd_reloc_ok; |
d9352518 | 8464 | if (! trunc_p) |
cdfeee4f AM |
8465 | /* Now do an overflow check. */ |
8466 | r = bfd_check_overflow ((signed_p | |
8467 | ? complain_overflow_signed | |
8468 | : complain_overflow_unsigned), | |
8469 | len, 0, (8 * wordsz), | |
8470 | relocation); | |
a0c8462f | 8471 | |
d9352518 DB |
8472 | /* Do the deed. */ |
8473 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
8474 | ||
8475 | #ifdef DEBUG | |
8476 | printf (" relocation: %8.8lx\n" | |
8477 | " shifted mask: %8.8lx\n" | |
8478 | " shifted/masked reloc: %8.8lx\n" | |
8479 | " result: %8.8lx\n", | |
9ccb8af9 AM |
8480 | (unsigned long) relocation, (unsigned long) (mask << shift), |
8481 | (unsigned long) ((relocation & mask) << shift), (unsigned long) x); | |
d9352518 | 8482 | #endif |
37b01f6a DG |
8483 | put_value (wordsz, chunksz, input_bfd, x, |
8484 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
cdfeee4f | 8485 | return r; |
d9352518 DB |
8486 | } |
8487 | ||
0e287786 AM |
8488 | /* Functions to read r_offset from external (target order) reloc |
8489 | entry. Faster than bfd_getl32 et al, because we let the compiler | |
8490 | know the value is aligned. */ | |
53df40a4 | 8491 | |
0e287786 AM |
8492 | static bfd_vma |
8493 | ext32l_r_offset (const void *p) | |
53df40a4 AM |
8494 | { |
8495 | union aligned32 | |
8496 | { | |
8497 | uint32_t v; | |
8498 | unsigned char c[4]; | |
8499 | }; | |
8500 | const union aligned32 *a | |
0e287786 | 8501 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8502 | |
8503 | uint32_t aval = ( (uint32_t) a->c[0] | |
8504 | | (uint32_t) a->c[1] << 8 | |
8505 | | (uint32_t) a->c[2] << 16 | |
8506 | | (uint32_t) a->c[3] << 24); | |
0e287786 | 8507 | return aval; |
53df40a4 AM |
8508 | } |
8509 | ||
0e287786 AM |
8510 | static bfd_vma |
8511 | ext32b_r_offset (const void *p) | |
53df40a4 AM |
8512 | { |
8513 | union aligned32 | |
8514 | { | |
8515 | uint32_t v; | |
8516 | unsigned char c[4]; | |
8517 | }; | |
8518 | const union aligned32 *a | |
0e287786 | 8519 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8520 | |
8521 | uint32_t aval = ( (uint32_t) a->c[0] << 24 | |
8522 | | (uint32_t) a->c[1] << 16 | |
8523 | | (uint32_t) a->c[2] << 8 | |
8524 | | (uint32_t) a->c[3]); | |
0e287786 | 8525 | return aval; |
53df40a4 AM |
8526 | } |
8527 | ||
8528 | #ifdef BFD_HOST_64_BIT | |
0e287786 AM |
8529 | static bfd_vma |
8530 | ext64l_r_offset (const void *p) | |
53df40a4 AM |
8531 | { |
8532 | union aligned64 | |
8533 | { | |
8534 | uint64_t v; | |
8535 | unsigned char c[8]; | |
8536 | }; | |
8537 | const union aligned64 *a | |
0e287786 | 8538 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8539 | |
8540 | uint64_t aval = ( (uint64_t) a->c[0] | |
8541 | | (uint64_t) a->c[1] << 8 | |
8542 | | (uint64_t) a->c[2] << 16 | |
8543 | | (uint64_t) a->c[3] << 24 | |
8544 | | (uint64_t) a->c[4] << 32 | |
8545 | | (uint64_t) a->c[5] << 40 | |
8546 | | (uint64_t) a->c[6] << 48 | |
8547 | | (uint64_t) a->c[7] << 56); | |
0e287786 | 8548 | return aval; |
53df40a4 AM |
8549 | } |
8550 | ||
0e287786 AM |
8551 | static bfd_vma |
8552 | ext64b_r_offset (const void *p) | |
53df40a4 AM |
8553 | { |
8554 | union aligned64 | |
8555 | { | |
8556 | uint64_t v; | |
8557 | unsigned char c[8]; | |
8558 | }; | |
8559 | const union aligned64 *a | |
0e287786 | 8560 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8561 | |
8562 | uint64_t aval = ( (uint64_t) a->c[0] << 56 | |
8563 | | (uint64_t) a->c[1] << 48 | |
8564 | | (uint64_t) a->c[2] << 40 | |
8565 | | (uint64_t) a->c[3] << 32 | |
8566 | | (uint64_t) a->c[4] << 24 | |
8567 | | (uint64_t) a->c[5] << 16 | |
8568 | | (uint64_t) a->c[6] << 8 | |
8569 | | (uint64_t) a->c[7]); | |
0e287786 | 8570 | return aval; |
53df40a4 AM |
8571 | } |
8572 | #endif | |
8573 | ||
c152c796 AM |
8574 | /* When performing a relocatable link, the input relocations are |
8575 | preserved. But, if they reference global symbols, the indices | |
d4730f92 BS |
8576 | referenced must be updated. Update all the relocations found in |
8577 | RELDATA. */ | |
c152c796 | 8578 | |
bca6d0e3 | 8579 | static bfd_boolean |
c152c796 | 8580 | elf_link_adjust_relocs (bfd *abfd, |
9eaff861 | 8581 | asection *sec, |
28dbcedc | 8582 | struct bfd_elf_section_reloc_data *reldata, |
10bbbc1d NC |
8583 | bfd_boolean sort, |
8584 | struct bfd_link_info *info) | |
c152c796 AM |
8585 | { |
8586 | unsigned int i; | |
8587 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8588 | bfd_byte *erela; | |
8589 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
8590 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8591 | bfd_vma r_type_mask; | |
8592 | int r_sym_shift; | |
d4730f92 BS |
8593 | unsigned int count = reldata->count; |
8594 | struct elf_link_hash_entry **rel_hash = reldata->hashes; | |
c152c796 | 8595 | |
d4730f92 | 8596 | if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) |
c152c796 AM |
8597 | { |
8598 | swap_in = bed->s->swap_reloc_in; | |
8599 | swap_out = bed->s->swap_reloc_out; | |
8600 | } | |
d4730f92 | 8601 | else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) |
c152c796 AM |
8602 | { |
8603 | swap_in = bed->s->swap_reloca_in; | |
8604 | swap_out = bed->s->swap_reloca_out; | |
8605 | } | |
8606 | else | |
8607 | abort (); | |
8608 | ||
8609 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
8610 | abort (); | |
8611 | ||
8612 | if (bed->s->arch_size == 32) | |
8613 | { | |
8614 | r_type_mask = 0xff; | |
8615 | r_sym_shift = 8; | |
8616 | } | |
8617 | else | |
8618 | { | |
8619 | r_type_mask = 0xffffffff; | |
8620 | r_sym_shift = 32; | |
8621 | } | |
8622 | ||
d4730f92 BS |
8623 | erela = reldata->hdr->contents; |
8624 | for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) | |
c152c796 AM |
8625 | { |
8626 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
8627 | unsigned int j; | |
8628 | ||
8629 | if (*rel_hash == NULL) | |
8630 | continue; | |
8631 | ||
10bbbc1d NC |
8632 | if ((*rel_hash)->indx == -2 |
8633 | && info->gc_sections | |
8634 | && ! info->gc_keep_exported) | |
8635 | { | |
8636 | /* PR 21524: Let the user know if a symbol was removed by garbage collection. */ | |
8637 | _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."), | |
8638 | abfd, sec, | |
8639 | (*rel_hash)->root.root.string); | |
8640 | _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."), | |
d42c267e | 8641 | abfd, sec); |
10bbbc1d NC |
8642 | bfd_set_error (bfd_error_invalid_operation); |
8643 | return FALSE; | |
8644 | } | |
c152c796 AM |
8645 | BFD_ASSERT ((*rel_hash)->indx >= 0); |
8646 | ||
8647 | (*swap_in) (abfd, erela, irela); | |
8648 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
8649 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
8650 | | (irela[j].r_info & r_type_mask)); | |
8651 | (*swap_out) (abfd, irela, erela); | |
8652 | } | |
53df40a4 | 8653 | |
9eaff861 AO |
8654 | if (bed->elf_backend_update_relocs) |
8655 | (*bed->elf_backend_update_relocs) (sec, reldata); | |
8656 | ||
0e287786 | 8657 | if (sort && count != 0) |
53df40a4 | 8658 | { |
0e287786 AM |
8659 | bfd_vma (*ext_r_off) (const void *); |
8660 | bfd_vma r_off; | |
8661 | size_t elt_size; | |
8662 | bfd_byte *base, *end, *p, *loc; | |
bca6d0e3 | 8663 | bfd_byte *buf = NULL; |
28dbcedc AM |
8664 | |
8665 | if (bed->s->arch_size == 32) | |
8666 | { | |
8667 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) | |
0e287786 | 8668 | ext_r_off = ext32l_r_offset; |
28dbcedc | 8669 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8670 | ext_r_off = ext32b_r_offset; |
28dbcedc AM |
8671 | else |
8672 | abort (); | |
8673 | } | |
53df40a4 | 8674 | else |
28dbcedc | 8675 | { |
53df40a4 | 8676 | #ifdef BFD_HOST_64_BIT |
28dbcedc | 8677 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) |
0e287786 | 8678 | ext_r_off = ext64l_r_offset; |
28dbcedc | 8679 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8680 | ext_r_off = ext64b_r_offset; |
28dbcedc | 8681 | else |
53df40a4 | 8682 | #endif |
28dbcedc AM |
8683 | abort (); |
8684 | } | |
0e287786 | 8685 | |
bca6d0e3 AM |
8686 | /* Must use a stable sort here. A modified insertion sort, |
8687 | since the relocs are mostly sorted already. */ | |
0e287786 AM |
8688 | elt_size = reldata->hdr->sh_entsize; |
8689 | base = reldata->hdr->contents; | |
8690 | end = base + count * elt_size; | |
bca6d0e3 | 8691 | if (elt_size > sizeof (Elf64_External_Rela)) |
0e287786 AM |
8692 | abort (); |
8693 | ||
8694 | /* Ensure the first element is lowest. This acts as a sentinel, | |
8695 | speeding the main loop below. */ | |
8696 | r_off = (*ext_r_off) (base); | |
8697 | for (p = loc = base; (p += elt_size) < end; ) | |
8698 | { | |
8699 | bfd_vma r_off2 = (*ext_r_off) (p); | |
8700 | if (r_off > r_off2) | |
8701 | { | |
8702 | r_off = r_off2; | |
8703 | loc = p; | |
8704 | } | |
8705 | } | |
8706 | if (loc != base) | |
8707 | { | |
8708 | /* Don't just swap *base and *loc as that changes the order | |
8709 | of the original base[0] and base[1] if they happen to | |
8710 | have the same r_offset. */ | |
bca6d0e3 AM |
8711 | bfd_byte onebuf[sizeof (Elf64_External_Rela)]; |
8712 | memcpy (onebuf, loc, elt_size); | |
0e287786 | 8713 | memmove (base + elt_size, base, loc - base); |
bca6d0e3 | 8714 | memcpy (base, onebuf, elt_size); |
0e287786 AM |
8715 | } |
8716 | ||
b29b8669 | 8717 | for (p = base + elt_size; (p += elt_size) < end; ) |
0e287786 AM |
8718 | { |
8719 | /* base to p is sorted, *p is next to insert. */ | |
8720 | r_off = (*ext_r_off) (p); | |
8721 | /* Search the sorted region for location to insert. */ | |
8722 | loc = p - elt_size; | |
8723 | while (r_off < (*ext_r_off) (loc)) | |
8724 | loc -= elt_size; | |
8725 | loc += elt_size; | |
8726 | if (loc != p) | |
8727 | { | |
bca6d0e3 AM |
8728 | /* Chances are there is a run of relocs to insert here, |
8729 | from one of more input files. Files are not always | |
8730 | linked in order due to the way elf_link_input_bfd is | |
8731 | called. See pr17666. */ | |
8732 | size_t sortlen = p - loc; | |
8733 | bfd_vma r_off2 = (*ext_r_off) (loc); | |
8734 | size_t runlen = elt_size; | |
8735 | size_t buf_size = 96 * 1024; | |
8736 | while (p + runlen < end | |
8737 | && (sortlen <= buf_size | |
8738 | || runlen + elt_size <= buf_size) | |
8739 | && r_off2 > (*ext_r_off) (p + runlen)) | |
8740 | runlen += elt_size; | |
8741 | if (buf == NULL) | |
8742 | { | |
8743 | buf = bfd_malloc (buf_size); | |
8744 | if (buf == NULL) | |
8745 | return FALSE; | |
8746 | } | |
8747 | if (runlen < sortlen) | |
8748 | { | |
8749 | memcpy (buf, p, runlen); | |
8750 | memmove (loc + runlen, loc, sortlen); | |
8751 | memcpy (loc, buf, runlen); | |
8752 | } | |
8753 | else | |
8754 | { | |
8755 | memcpy (buf, loc, sortlen); | |
8756 | memmove (loc, p, runlen); | |
8757 | memcpy (loc + runlen, buf, sortlen); | |
8758 | } | |
b29b8669 | 8759 | p += runlen - elt_size; |
0e287786 AM |
8760 | } |
8761 | } | |
8762 | /* Hashes are no longer valid. */ | |
28dbcedc AM |
8763 | free (reldata->hashes); |
8764 | reldata->hashes = NULL; | |
bca6d0e3 | 8765 | free (buf); |
53df40a4 | 8766 | } |
bca6d0e3 | 8767 | return TRUE; |
c152c796 AM |
8768 | } |
8769 | ||
8770 | struct elf_link_sort_rela | |
8771 | { | |
8772 | union { | |
8773 | bfd_vma offset; | |
8774 | bfd_vma sym_mask; | |
8775 | } u; | |
8776 | enum elf_reloc_type_class type; | |
8777 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
8778 | Elf_Internal_Rela rela[1]; | |
8779 | }; | |
8780 | ||
8781 | static int | |
8782 | elf_link_sort_cmp1 (const void *A, const void *B) | |
8783 | { | |
a50b1753 NC |
8784 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8785 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 AM |
8786 | int relativea, relativeb; |
8787 | ||
8788 | relativea = a->type == reloc_class_relative; | |
8789 | relativeb = b->type == reloc_class_relative; | |
8790 | ||
8791 | if (relativea < relativeb) | |
8792 | return 1; | |
8793 | if (relativea > relativeb) | |
8794 | return -1; | |
8795 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
8796 | return -1; | |
8797 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
8798 | return 1; | |
8799 | if (a->rela->r_offset < b->rela->r_offset) | |
8800 | return -1; | |
8801 | if (a->rela->r_offset > b->rela->r_offset) | |
8802 | return 1; | |
8803 | return 0; | |
8804 | } | |
8805 | ||
8806 | static int | |
8807 | elf_link_sort_cmp2 (const void *A, const void *B) | |
8808 | { | |
a50b1753 NC |
8809 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8810 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 | 8811 | |
7e612e98 | 8812 | if (a->type < b->type) |
c152c796 | 8813 | return -1; |
7e612e98 | 8814 | if (a->type > b->type) |
c152c796 | 8815 | return 1; |
7e612e98 | 8816 | if (a->u.offset < b->u.offset) |
c152c796 | 8817 | return -1; |
7e612e98 | 8818 | if (a->u.offset > b->u.offset) |
c152c796 AM |
8819 | return 1; |
8820 | if (a->rela->r_offset < b->rela->r_offset) | |
8821 | return -1; | |
8822 | if (a->rela->r_offset > b->rela->r_offset) | |
8823 | return 1; | |
8824 | return 0; | |
8825 | } | |
8826 | ||
8827 | static size_t | |
8828 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
8829 | { | |
3410fea8 | 8830 | asection *dynamic_relocs; |
fc66a176 L |
8831 | asection *rela_dyn; |
8832 | asection *rel_dyn; | |
c152c796 AM |
8833 | bfd_size_type count, size; |
8834 | size_t i, ret, sort_elt, ext_size; | |
8835 | bfd_byte *sort, *s_non_relative, *p; | |
8836 | struct elf_link_sort_rela *sq; | |
8837 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8838 | int i2e = bed->s->int_rels_per_ext_rel; | |
c8e44c6d | 8839 | unsigned int opb = bfd_octets_per_byte (abfd); |
c152c796 AM |
8840 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
8841 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8842 | struct bfd_link_order *lo; | |
8843 | bfd_vma r_sym_mask; | |
3410fea8 | 8844 | bfd_boolean use_rela; |
c152c796 | 8845 | |
3410fea8 NC |
8846 | /* Find a dynamic reloc section. */ |
8847 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
8848 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
8849 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
8850 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 8851 | { |
3410fea8 NC |
8852 | bfd_boolean use_rela_initialised = FALSE; |
8853 | ||
8854 | /* This is just here to stop gcc from complaining. | |
c8e44c6d | 8855 | Its initialization checking code is not perfect. */ |
3410fea8 NC |
8856 | use_rela = TRUE; |
8857 | ||
8858 | /* Both sections are present. Examine the sizes | |
8859 | of the indirect sections to help us choose. */ | |
8860 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8861 | if (lo->type == bfd_indirect_link_order) | |
8862 | { | |
8863 | asection *o = lo->u.indirect.section; | |
8864 | ||
8865 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8866 | { | |
8867 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8868 | /* Section size is divisible by both rel and rela sizes. | |
8869 | It is of no help to us. */ | |
8870 | ; | |
8871 | else | |
8872 | { | |
8873 | /* Section size is only divisible by rela. */ | |
535b785f | 8874 | if (use_rela_initialised && !use_rela) |
3410fea8 | 8875 | { |
c8e44c6d AM |
8876 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8877 | "they are in more than one size"), | |
8878 | abfd); | |
3410fea8 NC |
8879 | bfd_set_error (bfd_error_invalid_operation); |
8880 | return 0; | |
8881 | } | |
8882 | else | |
8883 | { | |
8884 | use_rela = TRUE; | |
8885 | use_rela_initialised = TRUE; | |
8886 | } | |
8887 | } | |
8888 | } | |
8889 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
8890 | { | |
8891 | /* Section size is only divisible by rel. */ | |
535b785f | 8892 | if (use_rela_initialised && use_rela) |
3410fea8 | 8893 | { |
c8e44c6d AM |
8894 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8895 | "they are in more than one size"), | |
8896 | abfd); | |
3410fea8 NC |
8897 | bfd_set_error (bfd_error_invalid_operation); |
8898 | return 0; | |
8899 | } | |
8900 | else | |
8901 | { | |
8902 | use_rela = FALSE; | |
8903 | use_rela_initialised = TRUE; | |
8904 | } | |
8905 | } | |
8906 | else | |
8907 | { | |
c8e44c6d AM |
8908 | /* The section size is not divisible by either - |
8909 | something is wrong. */ | |
8910 | _bfd_error_handler (_("%B: Unable to sort relocs - " | |
8911 | "they are of an unknown size"), abfd); | |
3410fea8 NC |
8912 | bfd_set_error (bfd_error_invalid_operation); |
8913 | return 0; | |
8914 | } | |
8915 | } | |
8916 | ||
8917 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8918 | if (lo->type == bfd_indirect_link_order) | |
8919 | { | |
8920 | asection *o = lo->u.indirect.section; | |
8921 | ||
8922 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8923 | { | |
8924 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8925 | /* Section size is divisible by both rel and rela sizes. | |
8926 | It is of no help to us. */ | |
8927 | ; | |
8928 | else | |
8929 | { | |
8930 | /* Section size is only divisible by rela. */ | |
535b785f | 8931 | if (use_rela_initialised && !use_rela) |
3410fea8 | 8932 | { |
c8e44c6d AM |
8933 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8934 | "they are in more than one size"), | |
8935 | abfd); | |
3410fea8 NC |
8936 | bfd_set_error (bfd_error_invalid_operation); |
8937 | return 0; | |
8938 | } | |
8939 | else | |
8940 | { | |
8941 | use_rela = TRUE; | |
8942 | use_rela_initialised = TRUE; | |
8943 | } | |
8944 | } | |
8945 | } | |
8946 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
8947 | { | |
8948 | /* Section size is only divisible by rel. */ | |
535b785f | 8949 | if (use_rela_initialised && use_rela) |
3410fea8 | 8950 | { |
c8e44c6d AM |
8951 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8952 | "they are in more than one size"), | |
8953 | abfd); | |
3410fea8 NC |
8954 | bfd_set_error (bfd_error_invalid_operation); |
8955 | return 0; | |
8956 | } | |
8957 | else | |
8958 | { | |
8959 | use_rela = FALSE; | |
8960 | use_rela_initialised = TRUE; | |
8961 | } | |
8962 | } | |
8963 | else | |
8964 | { | |
c8e44c6d AM |
8965 | /* The section size is not divisible by either - |
8966 | something is wrong. */ | |
8967 | _bfd_error_handler (_("%B: Unable to sort relocs - " | |
8968 | "they are of an unknown size"), abfd); | |
3410fea8 NC |
8969 | bfd_set_error (bfd_error_invalid_operation); |
8970 | return 0; | |
8971 | } | |
8972 | } | |
8973 | ||
8974 | if (! use_rela_initialised) | |
8975 | /* Make a guess. */ | |
8976 | use_rela = TRUE; | |
c152c796 | 8977 | } |
fc66a176 L |
8978 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
8979 | use_rela = TRUE; | |
8980 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 8981 | use_rela = FALSE; |
c152c796 | 8982 | else |
fc66a176 | 8983 | return 0; |
3410fea8 NC |
8984 | |
8985 | if (use_rela) | |
c152c796 | 8986 | { |
3410fea8 | 8987 | dynamic_relocs = rela_dyn; |
c152c796 AM |
8988 | ext_size = bed->s->sizeof_rela; |
8989 | swap_in = bed->s->swap_reloca_in; | |
8990 | swap_out = bed->s->swap_reloca_out; | |
8991 | } | |
3410fea8 NC |
8992 | else |
8993 | { | |
8994 | dynamic_relocs = rel_dyn; | |
8995 | ext_size = bed->s->sizeof_rel; | |
8996 | swap_in = bed->s->swap_reloc_in; | |
8997 | swap_out = bed->s->swap_reloc_out; | |
8998 | } | |
c152c796 AM |
8999 | |
9000 | size = 0; | |
3410fea8 | 9001 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 9002 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 9003 | size += lo->u.indirect.section->size; |
c152c796 | 9004 | |
3410fea8 | 9005 | if (size != dynamic_relocs->size) |
c152c796 AM |
9006 | return 0; |
9007 | ||
9008 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
9009 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
9010 | |
9011 | count = dynamic_relocs->size / ext_size; | |
5e486aa1 NC |
9012 | if (count == 0) |
9013 | return 0; | |
a50b1753 | 9014 | sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); |
3410fea8 | 9015 | |
c152c796 AM |
9016 | if (sort == NULL) |
9017 | { | |
9018 | (*info->callbacks->warning) | |
9019 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
9020 | return 0; | |
9021 | } | |
9022 | ||
9023 | if (bed->s->arch_size == 32) | |
9024 | r_sym_mask = ~(bfd_vma) 0xff; | |
9025 | else | |
9026 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
9027 | ||
3410fea8 | 9028 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9029 | if (lo->type == bfd_indirect_link_order) |
9030 | { | |
9031 | bfd_byte *erel, *erelend; | |
9032 | asection *o = lo->u.indirect.section; | |
9033 | ||
1da212d6 AM |
9034 | if (o->contents == NULL && o->size != 0) |
9035 | { | |
9036 | /* This is a reloc section that is being handled as a normal | |
9037 | section. See bfd_section_from_shdr. We can't combine | |
9038 | relocs in this case. */ | |
9039 | free (sort); | |
9040 | return 0; | |
9041 | } | |
c152c796 | 9042 | erel = o->contents; |
eea6121a | 9043 | erelend = o->contents + o->size; |
c8e44c6d | 9044 | p = sort + o->output_offset * opb / ext_size * sort_elt; |
3410fea8 | 9045 | |
c152c796 AM |
9046 | while (erel < erelend) |
9047 | { | |
9048 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 9049 | |
c152c796 | 9050 | (*swap_in) (abfd, erel, s->rela); |
7e612e98 | 9051 | s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); |
c152c796 AM |
9052 | s->u.sym_mask = r_sym_mask; |
9053 | p += sort_elt; | |
9054 | erel += ext_size; | |
9055 | } | |
9056 | } | |
9057 | ||
9058 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
9059 | ||
9060 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
9061 | { | |
9062 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9063 | if (s->type != reloc_class_relative) | |
9064 | break; | |
9065 | } | |
9066 | ret = i; | |
9067 | s_non_relative = p; | |
9068 | ||
9069 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
9070 | for (; i < count; i++, p += sort_elt) | |
9071 | { | |
9072 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
9073 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
9074 | sq = sp; | |
9075 | sp->u.offset = sq->rela->r_offset; | |
9076 | } | |
9077 | ||
9078 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
9079 | ||
c8e44c6d AM |
9080 | struct elf_link_hash_table *htab = elf_hash_table (info); |
9081 | if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) | |
9082 | { | |
9083 | /* We have plt relocs in .rela.dyn. */ | |
9084 | sq = (struct elf_link_sort_rela *) sort; | |
9085 | for (i = 0; i < count; i++) | |
9086 | if (sq[count - i - 1].type != reloc_class_plt) | |
9087 | break; | |
9088 | if (i != 0 && htab->srelplt->size == i * ext_size) | |
9089 | { | |
9090 | struct bfd_link_order **plo; | |
9091 | /* Put srelplt link_order last. This is so the output_offset | |
9092 | set in the next loop is correct for DT_JMPREL. */ | |
9093 | for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) | |
9094 | if ((*plo)->type == bfd_indirect_link_order | |
9095 | && (*plo)->u.indirect.section == htab->srelplt) | |
9096 | { | |
9097 | lo = *plo; | |
9098 | *plo = lo->next; | |
9099 | } | |
9100 | else | |
9101 | plo = &(*plo)->next; | |
9102 | *plo = lo; | |
9103 | lo->next = NULL; | |
9104 | dynamic_relocs->map_tail.link_order = lo; | |
9105 | } | |
9106 | } | |
9107 | ||
9108 | p = sort; | |
3410fea8 | 9109 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9110 | if (lo->type == bfd_indirect_link_order) |
9111 | { | |
9112 | bfd_byte *erel, *erelend; | |
9113 | asection *o = lo->u.indirect.section; | |
9114 | ||
9115 | erel = o->contents; | |
eea6121a | 9116 | erelend = o->contents + o->size; |
c8e44c6d | 9117 | o->output_offset = (p - sort) / sort_elt * ext_size / opb; |
c152c796 AM |
9118 | while (erel < erelend) |
9119 | { | |
9120 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9121 | (*swap_out) (abfd, s->rela, erel); | |
9122 | p += sort_elt; | |
9123 | erel += ext_size; | |
9124 | } | |
9125 | } | |
9126 | ||
9127 | free (sort); | |
3410fea8 | 9128 | *psec = dynamic_relocs; |
c152c796 AM |
9129 | return ret; |
9130 | } | |
9131 | ||
ef10c3ac | 9132 | /* Add a symbol to the output symbol string table. */ |
c152c796 | 9133 | |
6e0b88f1 | 9134 | static int |
ef10c3ac L |
9135 | elf_link_output_symstrtab (struct elf_final_link_info *flinfo, |
9136 | const char *name, | |
9137 | Elf_Internal_Sym *elfsym, | |
9138 | asection *input_sec, | |
9139 | struct elf_link_hash_entry *h) | |
c152c796 | 9140 | { |
6e0b88f1 | 9141 | int (*output_symbol_hook) |
c152c796 AM |
9142 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, |
9143 | struct elf_link_hash_entry *); | |
ef10c3ac | 9144 | struct elf_link_hash_table *hash_table; |
c152c796 | 9145 | const struct elf_backend_data *bed; |
ef10c3ac | 9146 | bfd_size_type strtabsize; |
c152c796 | 9147 | |
8539e4e8 AM |
9148 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); |
9149 | ||
8b127cbc | 9150 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 AM |
9151 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; |
9152 | if (output_symbol_hook != NULL) | |
9153 | { | |
8b127cbc | 9154 | int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); |
6e0b88f1 AM |
9155 | if (ret != 1) |
9156 | return ret; | |
c152c796 AM |
9157 | } |
9158 | ||
ef10c3ac L |
9159 | if (name == NULL |
9160 | || *name == '\0' | |
9161 | || (input_sec->flags & SEC_EXCLUDE)) | |
9162 | elfsym->st_name = (unsigned long) -1; | |
c152c796 AM |
9163 | else |
9164 | { | |
ef10c3ac L |
9165 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
9166 | to get the final offset for st_name. */ | |
9167 | elfsym->st_name | |
9168 | = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, | |
9169 | name, FALSE); | |
c152c796 | 9170 | if (elfsym->st_name == (unsigned long) -1) |
6e0b88f1 | 9171 | return 0; |
c152c796 AM |
9172 | } |
9173 | ||
ef10c3ac L |
9174 | hash_table = elf_hash_table (flinfo->info); |
9175 | strtabsize = hash_table->strtabsize; | |
9176 | if (strtabsize <= hash_table->strtabcount) | |
c152c796 | 9177 | { |
ef10c3ac L |
9178 | strtabsize += strtabsize; |
9179 | hash_table->strtabsize = strtabsize; | |
9180 | strtabsize *= sizeof (*hash_table->strtab); | |
9181 | hash_table->strtab | |
9182 | = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, | |
9183 | strtabsize); | |
9184 | if (hash_table->strtab == NULL) | |
6e0b88f1 | 9185 | return 0; |
c152c796 | 9186 | } |
ef10c3ac L |
9187 | hash_table->strtab[hash_table->strtabcount].sym = *elfsym; |
9188 | hash_table->strtab[hash_table->strtabcount].dest_index | |
9189 | = hash_table->strtabcount; | |
9190 | hash_table->strtab[hash_table->strtabcount].destshndx_index | |
9191 | = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0; | |
9192 | ||
9193 | bfd_get_symcount (flinfo->output_bfd) += 1; | |
9194 | hash_table->strtabcount += 1; | |
9195 | ||
9196 | return 1; | |
9197 | } | |
9198 | ||
9199 | /* Swap symbols out to the symbol table and flush the output symbols to | |
9200 | the file. */ | |
9201 | ||
9202 | static bfd_boolean | |
9203 | elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) | |
9204 | { | |
9205 | struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); | |
ef53be89 AM |
9206 | bfd_size_type amt; |
9207 | size_t i; | |
ef10c3ac L |
9208 | const struct elf_backend_data *bed; |
9209 | bfd_byte *symbuf; | |
9210 | Elf_Internal_Shdr *hdr; | |
9211 | file_ptr pos; | |
9212 | bfd_boolean ret; | |
9213 | ||
9214 | if (!hash_table->strtabcount) | |
9215 | return TRUE; | |
9216 | ||
9217 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); | |
9218 | ||
9219 | bed = get_elf_backend_data (flinfo->output_bfd); | |
c152c796 | 9220 | |
ef10c3ac L |
9221 | amt = bed->s->sizeof_sym * hash_table->strtabcount; |
9222 | symbuf = (bfd_byte *) bfd_malloc (amt); | |
9223 | if (symbuf == NULL) | |
9224 | return FALSE; | |
1b786873 | 9225 | |
ef10c3ac | 9226 | if (flinfo->symshndxbuf) |
c152c796 | 9227 | { |
ef53be89 AM |
9228 | amt = sizeof (Elf_External_Sym_Shndx); |
9229 | amt *= bfd_get_symcount (flinfo->output_bfd); | |
ef10c3ac L |
9230 | flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); |
9231 | if (flinfo->symshndxbuf == NULL) | |
c152c796 | 9232 | { |
ef10c3ac L |
9233 | free (symbuf); |
9234 | return FALSE; | |
c152c796 | 9235 | } |
c152c796 AM |
9236 | } |
9237 | ||
ef10c3ac L |
9238 | for (i = 0; i < hash_table->strtabcount; i++) |
9239 | { | |
9240 | struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; | |
9241 | if (elfsym->sym.st_name == (unsigned long) -1) | |
9242 | elfsym->sym.st_name = 0; | |
9243 | else | |
9244 | elfsym->sym.st_name | |
9245 | = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, | |
9246 | elfsym->sym.st_name); | |
9247 | bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, | |
9248 | ((bfd_byte *) symbuf | |
9249 | + (elfsym->dest_index | |
9250 | * bed->s->sizeof_sym)), | |
9251 | (flinfo->symshndxbuf | |
9252 | + elfsym->destshndx_index)); | |
9253 | } | |
9254 | ||
9255 | hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; | |
9256 | pos = hdr->sh_offset + hdr->sh_size; | |
9257 | amt = hash_table->strtabcount * bed->s->sizeof_sym; | |
9258 | if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 | |
9259 | && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt) | |
9260 | { | |
9261 | hdr->sh_size += amt; | |
9262 | ret = TRUE; | |
9263 | } | |
9264 | else | |
9265 | ret = FALSE; | |
c152c796 | 9266 | |
ef10c3ac L |
9267 | free (symbuf); |
9268 | ||
9269 | free (hash_table->strtab); | |
9270 | hash_table->strtab = NULL; | |
9271 | ||
9272 | return ret; | |
c152c796 AM |
9273 | } |
9274 | ||
c0d5a53d L |
9275 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
9276 | ||
9277 | static bfd_boolean | |
9278 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
9279 | { | |
4fbb74a6 AM |
9280 | if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) |
9281 | && sym->st_shndx < SHN_LORESERVE) | |
c0d5a53d L |
9282 | { |
9283 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 9284 | beyond 64k. */ |
4eca0228 | 9285 | _bfd_error_handler |
695344c0 | 9286 | /* xgettext:c-format */ |
c0d5a53d | 9287 | (_("%B: Too many sections: %d (>= %d)"), |
4fbb74a6 | 9288 | abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); |
c0d5a53d L |
9289 | bfd_set_error (bfd_error_nonrepresentable_section); |
9290 | return FALSE; | |
9291 | } | |
9292 | return TRUE; | |
9293 | } | |
9294 | ||
c152c796 AM |
9295 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
9296 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
9297 | versioned symbol that would normally require an explicit version. | |
9298 | We also handle the case that a DSO references a hidden symbol | |
9299 | which may be satisfied by a versioned symbol in another DSO. */ | |
9300 | ||
9301 | static bfd_boolean | |
9302 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
9303 | const struct elf_backend_data *bed, | |
9304 | struct elf_link_hash_entry *h) | |
9305 | { | |
9306 | bfd *abfd; | |
9307 | struct elf_link_loaded_list *loaded; | |
9308 | ||
9309 | if (!is_elf_hash_table (info->hash)) | |
9310 | return FALSE; | |
9311 | ||
90c984fc L |
9312 | /* Check indirect symbol. */ |
9313 | while (h->root.type == bfd_link_hash_indirect) | |
9314 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9315 | ||
c152c796 AM |
9316 | switch (h->root.type) |
9317 | { | |
9318 | default: | |
9319 | abfd = NULL; | |
9320 | break; | |
9321 | ||
9322 | case bfd_link_hash_undefined: | |
9323 | case bfd_link_hash_undefweak: | |
9324 | abfd = h->root.u.undef.abfd; | |
f4ab0e2d L |
9325 | if (abfd == NULL |
9326 | || (abfd->flags & DYNAMIC) == 0 | |
e56f61be | 9327 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
9328 | return FALSE; |
9329 | break; | |
9330 | ||
9331 | case bfd_link_hash_defined: | |
9332 | case bfd_link_hash_defweak: | |
9333 | abfd = h->root.u.def.section->owner; | |
9334 | break; | |
9335 | ||
9336 | case bfd_link_hash_common: | |
9337 | abfd = h->root.u.c.p->section->owner; | |
9338 | break; | |
9339 | } | |
9340 | BFD_ASSERT (abfd != NULL); | |
9341 | ||
9342 | for (loaded = elf_hash_table (info)->loaded; | |
9343 | loaded != NULL; | |
9344 | loaded = loaded->next) | |
9345 | { | |
9346 | bfd *input; | |
9347 | Elf_Internal_Shdr *hdr; | |
ef53be89 AM |
9348 | size_t symcount; |
9349 | size_t extsymcount; | |
9350 | size_t extsymoff; | |
c152c796 AM |
9351 | Elf_Internal_Shdr *versymhdr; |
9352 | Elf_Internal_Sym *isym; | |
9353 | Elf_Internal_Sym *isymend; | |
9354 | Elf_Internal_Sym *isymbuf; | |
9355 | Elf_External_Versym *ever; | |
9356 | Elf_External_Versym *extversym; | |
9357 | ||
9358 | input = loaded->abfd; | |
9359 | ||
9360 | /* We check each DSO for a possible hidden versioned definition. */ | |
9361 | if (input == abfd | |
9362 | || (input->flags & DYNAMIC) == 0 | |
9363 | || elf_dynversym (input) == 0) | |
9364 | continue; | |
9365 | ||
9366 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
9367 | ||
9368 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
9369 | if (elf_bad_symtab (input)) | |
9370 | { | |
9371 | extsymcount = symcount; | |
9372 | extsymoff = 0; | |
9373 | } | |
9374 | else | |
9375 | { | |
9376 | extsymcount = symcount - hdr->sh_info; | |
9377 | extsymoff = hdr->sh_info; | |
9378 | } | |
9379 | ||
9380 | if (extsymcount == 0) | |
9381 | continue; | |
9382 | ||
9383 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
9384 | NULL, NULL, NULL); | |
9385 | if (isymbuf == NULL) | |
9386 | return FALSE; | |
9387 | ||
9388 | /* Read in any version definitions. */ | |
9389 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
a50b1753 | 9390 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
c152c796 AM |
9391 | if (extversym == NULL) |
9392 | goto error_ret; | |
9393 | ||
9394 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
9395 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
9396 | != versymhdr->sh_size)) | |
9397 | { | |
9398 | free (extversym); | |
9399 | error_ret: | |
9400 | free (isymbuf); | |
9401 | return FALSE; | |
9402 | } | |
9403 | ||
9404 | ever = extversym + extsymoff; | |
9405 | isymend = isymbuf + extsymcount; | |
9406 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
9407 | { | |
9408 | const char *name; | |
9409 | Elf_Internal_Versym iver; | |
9410 | unsigned short version_index; | |
9411 | ||
9412 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
9413 | || isym->st_shndx == SHN_UNDEF) | |
9414 | continue; | |
9415 | ||
9416 | name = bfd_elf_string_from_elf_section (input, | |
9417 | hdr->sh_link, | |
9418 | isym->st_name); | |
9419 | if (strcmp (name, h->root.root.string) != 0) | |
9420 | continue; | |
9421 | ||
9422 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
9423 | ||
d023c380 L |
9424 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 |
9425 | && !(h->def_regular | |
9426 | && h->forced_local)) | |
c152c796 AM |
9427 | { |
9428 | /* If we have a non-hidden versioned sym, then it should | |
d023c380 L |
9429 | have provided a definition for the undefined sym unless |
9430 | it is defined in a non-shared object and forced local. | |
9431 | */ | |
c152c796 AM |
9432 | abort (); |
9433 | } | |
9434 | ||
9435 | version_index = iver.vs_vers & VERSYM_VERSION; | |
9436 | if (version_index == 1 || version_index == 2) | |
9437 | { | |
9438 | /* This is the base or first version. We can use it. */ | |
9439 | free (extversym); | |
9440 | free (isymbuf); | |
9441 | return TRUE; | |
9442 | } | |
9443 | } | |
9444 | ||
9445 | free (extversym); | |
9446 | free (isymbuf); | |
9447 | } | |
9448 | ||
9449 | return FALSE; | |
9450 | } | |
9451 | ||
b8871f35 L |
9452 | /* Convert ELF common symbol TYPE. */ |
9453 | ||
9454 | static int | |
9455 | elf_link_convert_common_type (struct bfd_link_info *info, int type) | |
9456 | { | |
9457 | /* Commom symbol can only appear in relocatable link. */ | |
9458 | if (!bfd_link_relocatable (info)) | |
9459 | abort (); | |
9460 | switch (info->elf_stt_common) | |
9461 | { | |
9462 | case unchanged: | |
9463 | break; | |
9464 | case elf_stt_common: | |
9465 | type = STT_COMMON; | |
9466 | break; | |
9467 | case no_elf_stt_common: | |
9468 | type = STT_OBJECT; | |
9469 | break; | |
9470 | } | |
9471 | return type; | |
9472 | } | |
9473 | ||
c152c796 AM |
9474 | /* Add an external symbol to the symbol table. This is called from |
9475 | the hash table traversal routine. When generating a shared object, | |
9476 | we go through the symbol table twice. The first time we output | |
9477 | anything that might have been forced to local scope in a version | |
9478 | script. The second time we output the symbols that are still | |
9479 | global symbols. */ | |
9480 | ||
9481 | static bfd_boolean | |
7686d77d | 9482 | elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) |
c152c796 | 9483 | { |
7686d77d | 9484 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; |
a50b1753 | 9485 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
8b127cbc | 9486 | struct elf_final_link_info *flinfo = eoinfo->flinfo; |
c152c796 AM |
9487 | bfd_boolean strip; |
9488 | Elf_Internal_Sym sym; | |
9489 | asection *input_sec; | |
9490 | const struct elf_backend_data *bed; | |
6e0b88f1 AM |
9491 | long indx; |
9492 | int ret; | |
b8871f35 | 9493 | unsigned int type; |
c152c796 AM |
9494 | |
9495 | if (h->root.type == bfd_link_hash_warning) | |
9496 | { | |
9497 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9498 | if (h->root.type == bfd_link_hash_new) | |
9499 | return TRUE; | |
9500 | } | |
9501 | ||
9502 | /* Decide whether to output this symbol in this pass. */ | |
9503 | if (eoinfo->localsyms) | |
9504 | { | |
4deb8f71 | 9505 | if (!h->forced_local) |
c152c796 AM |
9506 | return TRUE; |
9507 | } | |
9508 | else | |
9509 | { | |
4deb8f71 | 9510 | if (h->forced_local) |
c152c796 AM |
9511 | return TRUE; |
9512 | } | |
9513 | ||
8b127cbc | 9514 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 | 9515 | |
12ac1cf5 | 9516 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 9517 | { |
12ac1cf5 NC |
9518 | /* If we have an undefined symbol reference here then it must have |
9519 | come from a shared library that is being linked in. (Undefined | |
98da7939 L |
9520 | references in regular files have already been handled unless |
9521 | they are in unreferenced sections which are removed by garbage | |
9522 | collection). */ | |
12ac1cf5 NC |
9523 | bfd_boolean ignore_undef = FALSE; |
9524 | ||
9525 | /* Some symbols may be special in that the fact that they're | |
9526 | undefined can be safely ignored - let backend determine that. */ | |
9527 | if (bed->elf_backend_ignore_undef_symbol) | |
9528 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
9529 | ||
9530 | /* If we are reporting errors for this situation then do so now. */ | |
89a2ee5a | 9531 | if (!ignore_undef |
12ac1cf5 | 9532 | && h->ref_dynamic |
8b127cbc AM |
9533 | && (!h->ref_regular || flinfo->info->gc_sections) |
9534 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h) | |
9535 | && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
1a72702b AM |
9536 | (*flinfo->info->callbacks->undefined_symbol) |
9537 | (flinfo->info, h->root.root.string, | |
9538 | h->ref_regular ? NULL : h->root.u.undef.abfd, | |
9539 | NULL, 0, | |
9540 | flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR); | |
97196564 L |
9541 | |
9542 | /* Strip a global symbol defined in a discarded section. */ | |
9543 | if (h->indx == -3) | |
9544 | return TRUE; | |
c152c796 AM |
9545 | } |
9546 | ||
9547 | /* We should also warn if a forced local symbol is referenced from | |
9548 | shared libraries. */ | |
0e1862bb | 9549 | if (bfd_link_executable (flinfo->info) |
f5385ebf AM |
9550 | && h->forced_local |
9551 | && h->ref_dynamic | |
371a5866 | 9552 | && h->def_regular |
f5385ebf | 9553 | && !h->dynamic_def |
ee659f1f | 9554 | && h->ref_dynamic_nonweak |
8b127cbc | 9555 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) |
c152c796 | 9556 | { |
17d078c5 AM |
9557 | bfd *def_bfd; |
9558 | const char *msg; | |
90c984fc L |
9559 | struct elf_link_hash_entry *hi = h; |
9560 | ||
9561 | /* Check indirect symbol. */ | |
9562 | while (hi->root.type == bfd_link_hash_indirect) | |
9563 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
17d078c5 AM |
9564 | |
9565 | if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
695344c0 | 9566 | /* xgettext:c-format */ |
17d078c5 AM |
9567 | msg = _("%B: internal symbol `%s' in %B is referenced by DSO"); |
9568 | else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) | |
695344c0 | 9569 | /* xgettext:c-format */ |
17d078c5 AM |
9570 | msg = _("%B: hidden symbol `%s' in %B is referenced by DSO"); |
9571 | else | |
695344c0 | 9572 | /* xgettext:c-format */ |
17d078c5 | 9573 | msg = _("%B: local symbol `%s' in %B is referenced by DSO"); |
8b127cbc | 9574 | def_bfd = flinfo->output_bfd; |
90c984fc L |
9575 | if (hi->root.u.def.section != bfd_abs_section_ptr) |
9576 | def_bfd = hi->root.u.def.section->owner; | |
c08bb8dd AM |
9577 | _bfd_error_handler (msg, flinfo->output_bfd, |
9578 | h->root.root.string, def_bfd); | |
17d078c5 | 9579 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9580 | eoinfo->failed = TRUE; |
9581 | return FALSE; | |
9582 | } | |
9583 | ||
9584 | /* We don't want to output symbols that have never been mentioned by | |
9585 | a regular file, or that we have been told to strip. However, if | |
9586 | h->indx is set to -2, the symbol is used by a reloc and we must | |
9587 | output it. */ | |
d983c8c5 | 9588 | strip = FALSE; |
c152c796 | 9589 | if (h->indx == -2) |
d983c8c5 | 9590 | ; |
f5385ebf | 9591 | else if ((h->def_dynamic |
77cfaee6 AM |
9592 | || h->ref_dynamic |
9593 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
9594 | && !h->def_regular |
9595 | && !h->ref_regular) | |
c152c796 | 9596 | strip = TRUE; |
8b127cbc | 9597 | else if (flinfo->info->strip == strip_all) |
c152c796 | 9598 | strip = TRUE; |
8b127cbc AM |
9599 | else if (flinfo->info->strip == strip_some |
9600 | && bfd_hash_lookup (flinfo->info->keep_hash, | |
c152c796 AM |
9601 | h->root.root.string, FALSE, FALSE) == NULL) |
9602 | strip = TRUE; | |
d56d55e7 AM |
9603 | else if ((h->root.type == bfd_link_hash_defined |
9604 | || h->root.type == bfd_link_hash_defweak) | |
8b127cbc | 9605 | && ((flinfo->info->strip_discarded |
dbaa2011 | 9606 | && discarded_section (h->root.u.def.section)) |
ca4be51c AM |
9607 | || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 |
9608 | && h->root.u.def.section->owner != NULL | |
d56d55e7 | 9609 | && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) |
c152c796 | 9610 | strip = TRUE; |
9e2278f5 AM |
9611 | else if ((h->root.type == bfd_link_hash_undefined |
9612 | || h->root.type == bfd_link_hash_undefweak) | |
9613 | && h->root.u.undef.abfd != NULL | |
9614 | && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) | |
9615 | strip = TRUE; | |
c152c796 | 9616 | |
b8871f35 L |
9617 | type = h->type; |
9618 | ||
c152c796 | 9619 | /* If we're stripping it, and it's not a dynamic symbol, there's |
d983c8c5 AM |
9620 | nothing else to do. However, if it is a forced local symbol or |
9621 | an ifunc symbol we need to give the backend finish_dynamic_symbol | |
9622 | function a chance to make it dynamic. */ | |
c152c796 AM |
9623 | if (strip |
9624 | && h->dynindx == -1 | |
b8871f35 | 9625 | && type != STT_GNU_IFUNC |
f5385ebf | 9626 | && !h->forced_local) |
c152c796 AM |
9627 | return TRUE; |
9628 | ||
9629 | sym.st_value = 0; | |
9630 | sym.st_size = h->size; | |
9631 | sym.st_other = h->other; | |
c152c796 AM |
9632 | switch (h->root.type) |
9633 | { | |
9634 | default: | |
9635 | case bfd_link_hash_new: | |
9636 | case bfd_link_hash_warning: | |
9637 | abort (); | |
9638 | return FALSE; | |
9639 | ||
9640 | case bfd_link_hash_undefined: | |
9641 | case bfd_link_hash_undefweak: | |
9642 | input_sec = bfd_und_section_ptr; | |
9643 | sym.st_shndx = SHN_UNDEF; | |
9644 | break; | |
9645 | ||
9646 | case bfd_link_hash_defined: | |
9647 | case bfd_link_hash_defweak: | |
9648 | { | |
9649 | input_sec = h->root.u.def.section; | |
9650 | if (input_sec->output_section != NULL) | |
9651 | { | |
9652 | sym.st_shndx = | |
8b127cbc | 9653 | _bfd_elf_section_from_bfd_section (flinfo->output_bfd, |
c152c796 AM |
9654 | input_sec->output_section); |
9655 | if (sym.st_shndx == SHN_BAD) | |
9656 | { | |
4eca0228 | 9657 | _bfd_error_handler |
695344c0 | 9658 | /* xgettext:c-format */ |
d003868e | 9659 | (_("%B: could not find output section %A for input section %A"), |
8b127cbc | 9660 | flinfo->output_bfd, input_sec->output_section, input_sec); |
17d078c5 | 9661 | bfd_set_error (bfd_error_nonrepresentable_section); |
c152c796 AM |
9662 | eoinfo->failed = TRUE; |
9663 | return FALSE; | |
9664 | } | |
9665 | ||
9666 | /* ELF symbols in relocatable files are section relative, | |
9667 | but in nonrelocatable files they are virtual | |
9668 | addresses. */ | |
9669 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
0e1862bb | 9670 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
9671 | { |
9672 | sym.st_value += input_sec->output_section->vma; | |
9673 | if (h->type == STT_TLS) | |
9674 | { | |
8b127cbc | 9675 | asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; |
430a16a5 NC |
9676 | if (tls_sec != NULL) |
9677 | sym.st_value -= tls_sec->vma; | |
c152c796 AM |
9678 | } |
9679 | } | |
9680 | } | |
9681 | else | |
9682 | { | |
9683 | BFD_ASSERT (input_sec->owner == NULL | |
9684 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
9685 | sym.st_shndx = SHN_UNDEF; | |
9686 | input_sec = bfd_und_section_ptr; | |
9687 | } | |
9688 | } | |
9689 | break; | |
9690 | ||
9691 | case bfd_link_hash_common: | |
9692 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 9693 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
9694 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
9695 | break; | |
9696 | ||
9697 | case bfd_link_hash_indirect: | |
9698 | /* These symbols are created by symbol versioning. They point | |
9699 | to the decorated version of the name. For example, if the | |
9700 | symbol foo@@GNU_1.2 is the default, which should be used when | |
9701 | foo is used with no version, then we add an indirect symbol | |
9702 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
9703 | since the indirected symbol is already in the hash table. */ | |
9704 | return TRUE; | |
9705 | } | |
9706 | ||
b8871f35 L |
9707 | if (type == STT_COMMON || type == STT_OBJECT) |
9708 | switch (h->root.type) | |
9709 | { | |
9710 | case bfd_link_hash_common: | |
9711 | type = elf_link_convert_common_type (flinfo->info, type); | |
9712 | break; | |
9713 | case bfd_link_hash_defined: | |
9714 | case bfd_link_hash_defweak: | |
9715 | if (bed->common_definition (&sym)) | |
9716 | type = elf_link_convert_common_type (flinfo->info, type); | |
9717 | else | |
9718 | type = STT_OBJECT; | |
9719 | break; | |
9720 | case bfd_link_hash_undefined: | |
9721 | case bfd_link_hash_undefweak: | |
9722 | break; | |
9723 | default: | |
9724 | abort (); | |
9725 | } | |
9726 | ||
4deb8f71 | 9727 | if (h->forced_local) |
b8871f35 L |
9728 | { |
9729 | sym.st_info = ELF_ST_INFO (STB_LOCAL, type); | |
9730 | /* Turn off visibility on local symbol. */ | |
9731 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); | |
9732 | } | |
9733 | /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ | |
9734 | else if (h->unique_global && h->def_regular) | |
9735 | sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); | |
9736 | else if (h->root.type == bfd_link_hash_undefweak | |
9737 | || h->root.type == bfd_link_hash_defweak) | |
9738 | sym.st_info = ELF_ST_INFO (STB_WEAK, type); | |
9739 | else | |
9740 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
9741 | sym.st_target_internal = h->target_internal; | |
9742 | ||
c152c796 AM |
9743 | /* Give the processor backend a chance to tweak the symbol value, |
9744 | and also to finish up anything that needs to be done for this | |
9745 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
3aa14d16 | 9746 | forced local syms when non-shared is due to a historical quirk. |
5f35ea9c | 9747 | STT_GNU_IFUNC symbol must go through PLT. */ |
3aa14d16 | 9748 | if ((h->type == STT_GNU_IFUNC |
5f35ea9c | 9749 | && h->def_regular |
0e1862bb | 9750 | && !bfd_link_relocatable (flinfo->info)) |
3aa14d16 L |
9751 | || ((h->dynindx != -1 |
9752 | || h->forced_local) | |
0e1862bb | 9753 | && ((bfd_link_pic (flinfo->info) |
3aa14d16 L |
9754 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
9755 | || h->root.type != bfd_link_hash_undefweak)) | |
9756 | || !h->forced_local) | |
8b127cbc | 9757 | && elf_hash_table (flinfo->info)->dynamic_sections_created)) |
c152c796 AM |
9758 | { |
9759 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8b127cbc | 9760 | (flinfo->output_bfd, flinfo->info, h, &sym))) |
c152c796 AM |
9761 | { |
9762 | eoinfo->failed = TRUE; | |
9763 | return FALSE; | |
9764 | } | |
9765 | } | |
9766 | ||
9767 | /* If we are marking the symbol as undefined, and there are no | |
9768 | non-weak references to this symbol from a regular object, then | |
9769 | mark the symbol as weak undefined; if there are non-weak | |
9770 | references, mark the symbol as strong. We can't do this earlier, | |
9771 | because it might not be marked as undefined until the | |
9772 | finish_dynamic_symbol routine gets through with it. */ | |
9773 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 9774 | && h->ref_regular |
c152c796 AM |
9775 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
9776 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
9777 | { | |
9778 | int bindtype; | |
b8871f35 | 9779 | type = ELF_ST_TYPE (sym.st_info); |
2955ec4c L |
9780 | |
9781 | /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ | |
9782 | if (type == STT_GNU_IFUNC) | |
9783 | type = STT_FUNC; | |
c152c796 | 9784 | |
f5385ebf | 9785 | if (h->ref_regular_nonweak) |
c152c796 AM |
9786 | bindtype = STB_GLOBAL; |
9787 | else | |
9788 | bindtype = STB_WEAK; | |
2955ec4c | 9789 | sym.st_info = ELF_ST_INFO (bindtype, type); |
c152c796 AM |
9790 | } |
9791 | ||
bda987c2 CD |
9792 | /* If this is a symbol defined in a dynamic library, don't use the |
9793 | symbol size from the dynamic library. Relinking an executable | |
9794 | against a new library may introduce gratuitous changes in the | |
9795 | executable's symbols if we keep the size. */ | |
9796 | if (sym.st_shndx == SHN_UNDEF | |
9797 | && !h->def_regular | |
9798 | && h->def_dynamic) | |
9799 | sym.st_size = 0; | |
9800 | ||
c152c796 AM |
9801 | /* If a non-weak symbol with non-default visibility is not defined |
9802 | locally, it is a fatal error. */ | |
0e1862bb | 9803 | if (!bfd_link_relocatable (flinfo->info) |
c152c796 AM |
9804 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT |
9805 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
9806 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 9807 | && !h->def_regular) |
c152c796 | 9808 | { |
17d078c5 AM |
9809 | const char *msg; |
9810 | ||
9811 | if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) | |
695344c0 | 9812 | /* xgettext:c-format */ |
17d078c5 AM |
9813 | msg = _("%B: protected symbol `%s' isn't defined"); |
9814 | else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) | |
695344c0 | 9815 | /* xgettext:c-format */ |
17d078c5 AM |
9816 | msg = _("%B: internal symbol `%s' isn't defined"); |
9817 | else | |
695344c0 | 9818 | /* xgettext:c-format */ |
17d078c5 | 9819 | msg = _("%B: hidden symbol `%s' isn't defined"); |
4eca0228 | 9820 | _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string); |
17d078c5 | 9821 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9822 | eoinfo->failed = TRUE; |
9823 | return FALSE; | |
9824 | } | |
9825 | ||
9826 | /* If this symbol should be put in the .dynsym section, then put it | |
9827 | there now. We already know the symbol index. We also fill in | |
9828 | the entry in the .hash section. */ | |
cae1fbbb | 9829 | if (elf_hash_table (flinfo->info)->dynsym != NULL |
202e2356 | 9830 | && h->dynindx != -1 |
8b127cbc | 9831 | && elf_hash_table (flinfo->info)->dynamic_sections_created) |
c152c796 | 9832 | { |
c152c796 AM |
9833 | bfd_byte *esym; |
9834 | ||
90c984fc L |
9835 | /* Since there is no version information in the dynamic string, |
9836 | if there is no version info in symbol version section, we will | |
1659f720 | 9837 | have a run-time problem if not linking executable, referenced |
4deb8f71 | 9838 | by shared library, or not bound locally. */ |
1659f720 | 9839 | if (h->verinfo.verdef == NULL |
0e1862bb | 9840 | && (!bfd_link_executable (flinfo->info) |
1659f720 L |
9841 | || h->ref_dynamic |
9842 | || !h->def_regular)) | |
90c984fc L |
9843 | { |
9844 | char *p = strrchr (h->root.root.string, ELF_VER_CHR); | |
9845 | ||
9846 | if (p && p [1] != '\0') | |
9847 | { | |
4eca0228 | 9848 | _bfd_error_handler |
695344c0 | 9849 | /* xgettext:c-format */ |
90c984fc L |
9850 | (_("%B: No symbol version section for versioned symbol `%s'"), |
9851 | flinfo->output_bfd, h->root.root.string); | |
9852 | eoinfo->failed = TRUE; | |
9853 | return FALSE; | |
9854 | } | |
9855 | } | |
9856 | ||
c152c796 | 9857 | sym.st_name = h->dynstr_index; |
cae1fbbb L |
9858 | esym = (elf_hash_table (flinfo->info)->dynsym->contents |
9859 | + h->dynindx * bed->s->sizeof_sym); | |
8b127cbc | 9860 | if (!check_dynsym (flinfo->output_bfd, &sym)) |
c0d5a53d L |
9861 | { |
9862 | eoinfo->failed = TRUE; | |
9863 | return FALSE; | |
9864 | } | |
8b127cbc | 9865 | bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); |
c152c796 | 9866 | |
8b127cbc | 9867 | if (flinfo->hash_sec != NULL) |
fdc90cb4 JJ |
9868 | { |
9869 | size_t hash_entry_size; | |
9870 | bfd_byte *bucketpos; | |
9871 | bfd_vma chain; | |
41198d0c L |
9872 | size_t bucketcount; |
9873 | size_t bucket; | |
9874 | ||
8b127cbc | 9875 | bucketcount = elf_hash_table (flinfo->info)->bucketcount; |
41198d0c | 9876 | bucket = h->u.elf_hash_value % bucketcount; |
fdc90cb4 JJ |
9877 | |
9878 | hash_entry_size | |
8b127cbc AM |
9879 | = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; |
9880 | bucketpos = ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 | 9881 | + (bucket + 2) * hash_entry_size); |
8b127cbc AM |
9882 | chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); |
9883 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, | |
9884 | bucketpos); | |
9885 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, | |
9886 | ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 JJ |
9887 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); |
9888 | } | |
c152c796 | 9889 | |
8b127cbc | 9890 | if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) |
c152c796 AM |
9891 | { |
9892 | Elf_Internal_Versym iversym; | |
9893 | Elf_External_Versym *eversym; | |
9894 | ||
f5385ebf | 9895 | if (!h->def_regular) |
c152c796 | 9896 | { |
7b20f099 AM |
9897 | if (h->verinfo.verdef == NULL |
9898 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
9899 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
c152c796 AM |
9900 | iversym.vs_vers = 0; |
9901 | else | |
9902 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
9903 | } | |
9904 | else | |
9905 | { | |
9906 | if (h->verinfo.vertree == NULL) | |
9907 | iversym.vs_vers = 1; | |
9908 | else | |
9909 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
8b127cbc | 9910 | if (flinfo->info->create_default_symver) |
3e3b46e5 | 9911 | iversym.vs_vers++; |
c152c796 AM |
9912 | } |
9913 | ||
422f1182 | 9914 | /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is |
6e33951e | 9915 | defined locally. */ |
422f1182 | 9916 | if (h->versioned == versioned_hidden && h->def_regular) |
c152c796 AM |
9917 | iversym.vs_vers |= VERSYM_HIDDEN; |
9918 | ||
8b127cbc | 9919 | eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; |
c152c796 | 9920 | eversym += h->dynindx; |
8b127cbc | 9921 | _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); |
c152c796 AM |
9922 | } |
9923 | } | |
9924 | ||
d983c8c5 AM |
9925 | /* If the symbol is undefined, and we didn't output it to .dynsym, |
9926 | strip it from .symtab too. Obviously we can't do this for | |
9927 | relocatable output or when needed for --emit-relocs. */ | |
9928 | else if (input_sec == bfd_und_section_ptr | |
9929 | && h->indx != -2 | |
66cae560 NC |
9930 | /* PR 22319 Do not strip global undefined symbols marked as being needed. */ |
9931 | && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL) | |
0e1862bb | 9932 | && !bfd_link_relocatable (flinfo->info)) |
d983c8c5 | 9933 | return TRUE; |
66cae560 | 9934 | |
d983c8c5 AM |
9935 | /* Also strip others that we couldn't earlier due to dynamic symbol |
9936 | processing. */ | |
9937 | if (strip) | |
9938 | return TRUE; | |
9939 | if ((input_sec->flags & SEC_EXCLUDE) != 0) | |
c152c796 AM |
9940 | return TRUE; |
9941 | ||
2ec55de3 AM |
9942 | /* Output a FILE symbol so that following locals are not associated |
9943 | with the wrong input file. We need one for forced local symbols | |
9944 | if we've seen more than one FILE symbol or when we have exactly | |
9945 | one FILE symbol but global symbols are present in a file other | |
9946 | than the one with the FILE symbol. We also need one if linker | |
9947 | defined symbols are present. In practice these conditions are | |
9948 | always met, so just emit the FILE symbol unconditionally. */ | |
9949 | if (eoinfo->localsyms | |
9950 | && !eoinfo->file_sym_done | |
9951 | && eoinfo->flinfo->filesym_count != 0) | |
9952 | { | |
9953 | Elf_Internal_Sym fsym; | |
9954 | ||
9955 | memset (&fsym, 0, sizeof (fsym)); | |
9956 | fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
9957 | fsym.st_shndx = SHN_ABS; | |
ef10c3ac L |
9958 | if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, |
9959 | bfd_und_section_ptr, NULL)) | |
2ec55de3 AM |
9960 | return FALSE; |
9961 | ||
9962 | eoinfo->file_sym_done = TRUE; | |
9963 | } | |
9964 | ||
8b127cbc | 9965 | indx = bfd_get_symcount (flinfo->output_bfd); |
ef10c3ac L |
9966 | ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, |
9967 | input_sec, h); | |
6e0b88f1 | 9968 | if (ret == 0) |
c152c796 AM |
9969 | { |
9970 | eoinfo->failed = TRUE; | |
9971 | return FALSE; | |
9972 | } | |
6e0b88f1 AM |
9973 | else if (ret == 1) |
9974 | h->indx = indx; | |
9975 | else if (h->indx == -2) | |
9976 | abort(); | |
c152c796 AM |
9977 | |
9978 | return TRUE; | |
9979 | } | |
9980 | ||
cdd3575c AM |
9981 | /* Return TRUE if special handling is done for relocs in SEC against |
9982 | symbols defined in discarded sections. */ | |
9983 | ||
c152c796 AM |
9984 | static bfd_boolean |
9985 | elf_section_ignore_discarded_relocs (asection *sec) | |
9986 | { | |
9987 | const struct elf_backend_data *bed; | |
9988 | ||
cdd3575c AM |
9989 | switch (sec->sec_info_type) |
9990 | { | |
dbaa2011 AM |
9991 | case SEC_INFO_TYPE_STABS: |
9992 | case SEC_INFO_TYPE_EH_FRAME: | |
2f0c68f2 | 9993 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
cdd3575c AM |
9994 | return TRUE; |
9995 | default: | |
9996 | break; | |
9997 | } | |
c152c796 AM |
9998 | |
9999 | bed = get_elf_backend_data (sec->owner); | |
10000 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
10001 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
10002 | return TRUE; | |
10003 | ||
10004 | return FALSE; | |
10005 | } | |
10006 | ||
9e66c942 AM |
10007 | /* Return a mask saying how ld should treat relocations in SEC against |
10008 | symbols defined in discarded sections. If this function returns | |
10009 | COMPLAIN set, ld will issue a warning message. If this function | |
10010 | returns PRETEND set, and the discarded section was link-once and the | |
10011 | same size as the kept link-once section, ld will pretend that the | |
10012 | symbol was actually defined in the kept section. Otherwise ld will | |
10013 | zero the reloc (at least that is the intent, but some cooperation by | |
10014 | the target dependent code is needed, particularly for REL targets). */ | |
10015 | ||
8a696751 AM |
10016 | unsigned int |
10017 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 10018 | { |
9e66c942 | 10019 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 10020 | return PRETEND; |
cdd3575c AM |
10021 | |
10022 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 10023 | return 0; |
cdd3575c AM |
10024 | |
10025 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 10026 | return 0; |
cdd3575c | 10027 | |
9e66c942 | 10028 | return COMPLAIN | PRETEND; |
cdd3575c AM |
10029 | } |
10030 | ||
3d7f7666 L |
10031 | /* Find a match between a section and a member of a section group. */ |
10032 | ||
10033 | static asection * | |
c0f00686 L |
10034 | match_group_member (asection *sec, asection *group, |
10035 | struct bfd_link_info *info) | |
3d7f7666 L |
10036 | { |
10037 | asection *first = elf_next_in_group (group); | |
10038 | asection *s = first; | |
10039 | ||
10040 | while (s != NULL) | |
10041 | { | |
c0f00686 | 10042 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
10043 | return s; |
10044 | ||
83180ade | 10045 | s = elf_next_in_group (s); |
3d7f7666 L |
10046 | if (s == first) |
10047 | break; | |
10048 | } | |
10049 | ||
10050 | return NULL; | |
10051 | } | |
10052 | ||
01b3c8ab | 10053 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
10054 | to replace it. Return the replacement if it is OK. Otherwise return |
10055 | NULL. */ | |
01b3c8ab L |
10056 | |
10057 | asection * | |
c0f00686 | 10058 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
10059 | { |
10060 | asection *kept; | |
10061 | ||
10062 | kept = sec->kept_section; | |
10063 | if (kept != NULL) | |
10064 | { | |
c2370991 | 10065 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 10066 | kept = match_group_member (sec, kept, info); |
1dd2625f BW |
10067 | if (kept != NULL |
10068 | && ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
10069 | != (kept->rawsize != 0 ? kept->rawsize : kept->size))) | |
01b3c8ab | 10070 | kept = NULL; |
c2370991 | 10071 | sec->kept_section = kept; |
01b3c8ab L |
10072 | } |
10073 | return kept; | |
10074 | } | |
10075 | ||
c152c796 AM |
10076 | /* Link an input file into the linker output file. This function |
10077 | handles all the sections and relocations of the input file at once. | |
10078 | This is so that we only have to read the local symbols once, and | |
10079 | don't have to keep them in memory. */ | |
10080 | ||
10081 | static bfd_boolean | |
8b127cbc | 10082 | elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) |
c152c796 | 10083 | { |
ece5ef60 | 10084 | int (*relocate_section) |
c152c796 AM |
10085 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
10086 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
10087 | bfd *output_bfd; | |
10088 | Elf_Internal_Shdr *symtab_hdr; | |
10089 | size_t locsymcount; | |
10090 | size_t extsymoff; | |
10091 | Elf_Internal_Sym *isymbuf; | |
10092 | Elf_Internal_Sym *isym; | |
10093 | Elf_Internal_Sym *isymend; | |
10094 | long *pindex; | |
10095 | asection **ppsection; | |
10096 | asection *o; | |
10097 | const struct elf_backend_data *bed; | |
c152c796 | 10098 | struct elf_link_hash_entry **sym_hashes; |
310fd250 L |
10099 | bfd_size_type address_size; |
10100 | bfd_vma r_type_mask; | |
10101 | int r_sym_shift; | |
ffbc01cc | 10102 | bfd_boolean have_file_sym = FALSE; |
c152c796 | 10103 | |
8b127cbc | 10104 | output_bfd = flinfo->output_bfd; |
c152c796 AM |
10105 | bed = get_elf_backend_data (output_bfd); |
10106 | relocate_section = bed->elf_backend_relocate_section; | |
10107 | ||
10108 | /* If this is a dynamic object, we don't want to do anything here: | |
10109 | we don't want the local symbols, and we don't want the section | |
10110 | contents. */ | |
10111 | if ((input_bfd->flags & DYNAMIC) != 0) | |
10112 | return TRUE; | |
10113 | ||
c152c796 AM |
10114 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
10115 | if (elf_bad_symtab (input_bfd)) | |
10116 | { | |
10117 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10118 | extsymoff = 0; | |
10119 | } | |
10120 | else | |
10121 | { | |
10122 | locsymcount = symtab_hdr->sh_info; | |
10123 | extsymoff = symtab_hdr->sh_info; | |
10124 | } | |
10125 | ||
10126 | /* Read the local symbols. */ | |
10127 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
10128 | if (isymbuf == NULL && locsymcount != 0) | |
10129 | { | |
10130 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8b127cbc AM |
10131 | flinfo->internal_syms, |
10132 | flinfo->external_syms, | |
10133 | flinfo->locsym_shndx); | |
c152c796 AM |
10134 | if (isymbuf == NULL) |
10135 | return FALSE; | |
10136 | } | |
10137 | ||
10138 | /* Find local symbol sections and adjust values of symbols in | |
10139 | SEC_MERGE sections. Write out those local symbols we know are | |
10140 | going into the output file. */ | |
10141 | isymend = isymbuf + locsymcount; | |
8b127cbc | 10142 | for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; |
c152c796 AM |
10143 | isym < isymend; |
10144 | isym++, pindex++, ppsection++) | |
10145 | { | |
10146 | asection *isec; | |
10147 | const char *name; | |
10148 | Elf_Internal_Sym osym; | |
6e0b88f1 AM |
10149 | long indx; |
10150 | int ret; | |
c152c796 AM |
10151 | |
10152 | *pindex = -1; | |
10153 | ||
10154 | if (elf_bad_symtab (input_bfd)) | |
10155 | { | |
10156 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
10157 | { | |
10158 | *ppsection = NULL; | |
10159 | continue; | |
10160 | } | |
10161 | } | |
10162 | ||
10163 | if (isym->st_shndx == SHN_UNDEF) | |
10164 | isec = bfd_und_section_ptr; | |
c152c796 AM |
10165 | else if (isym->st_shndx == SHN_ABS) |
10166 | isec = bfd_abs_section_ptr; | |
10167 | else if (isym->st_shndx == SHN_COMMON) | |
10168 | isec = bfd_com_section_ptr; | |
10169 | else | |
10170 | { | |
cb33740c AM |
10171 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
10172 | if (isec == NULL) | |
10173 | { | |
10174 | /* Don't attempt to output symbols with st_shnx in the | |
10175 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
10176 | *ppsection = NULL; | |
10177 | continue; | |
10178 | } | |
dbaa2011 | 10179 | else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE |
cb33740c AM |
10180 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) |
10181 | isym->st_value = | |
10182 | _bfd_merged_section_offset (output_bfd, &isec, | |
10183 | elf_section_data (isec)->sec_info, | |
10184 | isym->st_value); | |
c152c796 AM |
10185 | } |
10186 | ||
10187 | *ppsection = isec; | |
10188 | ||
d983c8c5 AM |
10189 | /* Don't output the first, undefined, symbol. In fact, don't |
10190 | output any undefined local symbol. */ | |
10191 | if (isec == bfd_und_section_ptr) | |
c152c796 AM |
10192 | continue; |
10193 | ||
10194 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
10195 | { | |
10196 | /* We never output section symbols. Instead, we use the | |
10197 | section symbol of the corresponding section in the output | |
10198 | file. */ | |
10199 | continue; | |
10200 | } | |
10201 | ||
10202 | /* If we are stripping all symbols, we don't want to output this | |
10203 | one. */ | |
8b127cbc | 10204 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
10205 | continue; |
10206 | ||
10207 | /* If we are discarding all local symbols, we don't want to | |
10208 | output this one. If we are generating a relocatable output | |
10209 | file, then some of the local symbols may be required by | |
10210 | relocs; we output them below as we discover that they are | |
10211 | needed. */ | |
8b127cbc | 10212 | if (flinfo->info->discard == discard_all) |
c152c796 AM |
10213 | continue; |
10214 | ||
10215 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
10216 | discarding, we don't need to keep it. */ |
10217 | if (isym->st_shndx != SHN_UNDEF | |
4fbb74a6 AM |
10218 | && isym->st_shndx < SHN_LORESERVE |
10219 | && bfd_section_removed_from_list (output_bfd, | |
10220 | isec->output_section)) | |
e75a280b L |
10221 | continue; |
10222 | ||
c152c796 AM |
10223 | /* Get the name of the symbol. */ |
10224 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
10225 | isym->st_name); | |
10226 | if (name == NULL) | |
10227 | return FALSE; | |
10228 | ||
10229 | /* See if we are discarding symbols with this name. */ | |
8b127cbc AM |
10230 | if ((flinfo->info->strip == strip_some |
10231 | && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) | |
c152c796 | 10232 | == NULL)) |
8b127cbc | 10233 | || (((flinfo->info->discard == discard_sec_merge |
0e1862bb L |
10234 | && (isec->flags & SEC_MERGE) |
10235 | && !bfd_link_relocatable (flinfo->info)) | |
8b127cbc | 10236 | || flinfo->info->discard == discard_l) |
c152c796 AM |
10237 | && bfd_is_local_label_name (input_bfd, name))) |
10238 | continue; | |
10239 | ||
ffbc01cc AM |
10240 | if (ELF_ST_TYPE (isym->st_info) == STT_FILE) |
10241 | { | |
ce875075 AM |
10242 | if (input_bfd->lto_output) |
10243 | /* -flto puts a temp file name here. This means builds | |
10244 | are not reproducible. Discard the symbol. */ | |
10245 | continue; | |
ffbc01cc AM |
10246 | have_file_sym = TRUE; |
10247 | flinfo->filesym_count += 1; | |
10248 | } | |
10249 | if (!have_file_sym) | |
10250 | { | |
10251 | /* In the absence of debug info, bfd_find_nearest_line uses | |
10252 | FILE symbols to determine the source file for local | |
10253 | function symbols. Provide a FILE symbol here if input | |
10254 | files lack such, so that their symbols won't be | |
10255 | associated with a previous input file. It's not the | |
10256 | source file, but the best we can do. */ | |
10257 | have_file_sym = TRUE; | |
10258 | flinfo->filesym_count += 1; | |
10259 | memset (&osym, 0, sizeof (osym)); | |
10260 | osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10261 | osym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10262 | if (!elf_link_output_symstrtab (flinfo, |
10263 | (input_bfd->lto_output ? NULL | |
10264 | : input_bfd->filename), | |
10265 | &osym, bfd_abs_section_ptr, | |
10266 | NULL)) | |
ffbc01cc AM |
10267 | return FALSE; |
10268 | } | |
10269 | ||
c152c796 AM |
10270 | osym = *isym; |
10271 | ||
10272 | /* Adjust the section index for the output file. */ | |
10273 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10274 | isec->output_section); | |
10275 | if (osym.st_shndx == SHN_BAD) | |
10276 | return FALSE; | |
10277 | ||
c152c796 AM |
10278 | /* ELF symbols in relocatable files are section relative, but |
10279 | in executable files they are virtual addresses. Note that | |
10280 | this code assumes that all ELF sections have an associated | |
10281 | BFD section with a reasonable value for output_offset; below | |
10282 | we assume that they also have a reasonable value for | |
10283 | output_section. Any special sections must be set up to meet | |
10284 | these requirements. */ | |
10285 | osym.st_value += isec->output_offset; | |
0e1862bb | 10286 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10287 | { |
10288 | osym.st_value += isec->output_section->vma; | |
10289 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
10290 | { | |
10291 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
8b127cbc AM |
10292 | BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL); |
10293 | osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; | |
c152c796 AM |
10294 | } |
10295 | } | |
10296 | ||
6e0b88f1 | 10297 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac | 10298 | ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); |
6e0b88f1 | 10299 | if (ret == 0) |
c152c796 | 10300 | return FALSE; |
6e0b88f1 AM |
10301 | else if (ret == 1) |
10302 | *pindex = indx; | |
c152c796 AM |
10303 | } |
10304 | ||
310fd250 L |
10305 | if (bed->s->arch_size == 32) |
10306 | { | |
10307 | r_type_mask = 0xff; | |
10308 | r_sym_shift = 8; | |
10309 | address_size = 4; | |
10310 | } | |
10311 | else | |
10312 | { | |
10313 | r_type_mask = 0xffffffff; | |
10314 | r_sym_shift = 32; | |
10315 | address_size = 8; | |
10316 | } | |
10317 | ||
c152c796 AM |
10318 | /* Relocate the contents of each section. */ |
10319 | sym_hashes = elf_sym_hashes (input_bfd); | |
10320 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
10321 | { | |
10322 | bfd_byte *contents; | |
10323 | ||
10324 | if (! o->linker_mark) | |
10325 | { | |
10326 | /* This section was omitted from the link. */ | |
10327 | continue; | |
10328 | } | |
10329 | ||
7bdf4127 | 10330 | if (!flinfo->info->resolve_section_groups |
bcacc0f5 AM |
10331 | && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) |
10332 | { | |
10333 | /* Deal with the group signature symbol. */ | |
10334 | struct bfd_elf_section_data *sec_data = elf_section_data (o); | |
10335 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
10336 | asection *osec = o->output_section; | |
10337 | ||
7bdf4127 | 10338 | BFD_ASSERT (bfd_link_relocatable (flinfo->info)); |
bcacc0f5 AM |
10339 | if (symndx >= locsymcount |
10340 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10341 | && flinfo->sections[symndx] == NULL)) |
bcacc0f5 AM |
10342 | { |
10343 | struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; | |
10344 | while (h->root.type == bfd_link_hash_indirect | |
10345 | || h->root.type == bfd_link_hash_warning) | |
10346 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10347 | /* Arrange for symbol to be output. */ | |
10348 | h->indx = -2; | |
10349 | elf_section_data (osec)->this_hdr.sh_info = -2; | |
10350 | } | |
10351 | else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) | |
10352 | { | |
10353 | /* We'll use the output section target_index. */ | |
8b127cbc | 10354 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 AM |
10355 | elf_section_data (osec)->this_hdr.sh_info = sec->target_index; |
10356 | } | |
10357 | else | |
10358 | { | |
8b127cbc | 10359 | if (flinfo->indices[symndx] == -1) |
bcacc0f5 AM |
10360 | { |
10361 | /* Otherwise output the local symbol now. */ | |
10362 | Elf_Internal_Sym sym = isymbuf[symndx]; | |
8b127cbc | 10363 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 | 10364 | const char *name; |
6e0b88f1 AM |
10365 | long indx; |
10366 | int ret; | |
bcacc0f5 AM |
10367 | |
10368 | name = bfd_elf_string_from_elf_section (input_bfd, | |
10369 | symtab_hdr->sh_link, | |
10370 | sym.st_name); | |
10371 | if (name == NULL) | |
10372 | return FALSE; | |
10373 | ||
10374 | sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10375 | sec); | |
10376 | if (sym.st_shndx == SHN_BAD) | |
10377 | return FALSE; | |
10378 | ||
10379 | sym.st_value += o->output_offset; | |
10380 | ||
6e0b88f1 | 10381 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10382 | ret = elf_link_output_symstrtab (flinfo, name, &sym, o, |
10383 | NULL); | |
6e0b88f1 | 10384 | if (ret == 0) |
bcacc0f5 | 10385 | return FALSE; |
6e0b88f1 | 10386 | else if (ret == 1) |
8b127cbc | 10387 | flinfo->indices[symndx] = indx; |
6e0b88f1 AM |
10388 | else |
10389 | abort (); | |
bcacc0f5 AM |
10390 | } |
10391 | elf_section_data (osec)->this_hdr.sh_info | |
8b127cbc | 10392 | = flinfo->indices[symndx]; |
bcacc0f5 AM |
10393 | } |
10394 | } | |
10395 | ||
c152c796 | 10396 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 10397 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
10398 | continue; |
10399 | ||
10400 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
10401 | { | |
10402 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
10403 | or somesuch. */ | |
10404 | continue; | |
10405 | } | |
10406 | ||
10407 | /* Get the contents of the section. They have been cached by a | |
10408 | relaxation routine. Note that o is a section in an input | |
10409 | file, so the contents field will not have been set by any of | |
10410 | the routines which work on output files. */ | |
10411 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
53291d1f AM |
10412 | { |
10413 | contents = elf_section_data (o)->this_hdr.contents; | |
10414 | if (bed->caches_rawsize | |
10415 | && o->rawsize != 0 | |
10416 | && o->rawsize < o->size) | |
10417 | { | |
10418 | memcpy (flinfo->contents, contents, o->rawsize); | |
10419 | contents = flinfo->contents; | |
10420 | } | |
10421 | } | |
c152c796 AM |
10422 | else |
10423 | { | |
8b127cbc | 10424 | contents = flinfo->contents; |
4a114e3e | 10425 | if (! bfd_get_full_section_contents (input_bfd, o, &contents)) |
c152c796 AM |
10426 | return FALSE; |
10427 | } | |
10428 | ||
10429 | if ((o->flags & SEC_RELOC) != 0) | |
10430 | { | |
10431 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 10432 | Elf_Internal_Rela *rel, *relend; |
0f02bbd9 | 10433 | int action_discarded; |
ece5ef60 | 10434 | int ret; |
c152c796 AM |
10435 | |
10436 | /* Get the swapped relocs. */ | |
10437 | internal_relocs | |
8b127cbc AM |
10438 | = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, |
10439 | flinfo->internal_relocs, FALSE); | |
c152c796 AM |
10440 | if (internal_relocs == NULL |
10441 | && o->reloc_count > 0) | |
10442 | return FALSE; | |
10443 | ||
310fd250 L |
10444 | /* We need to reverse-copy input .ctors/.dtors sections if |
10445 | they are placed in .init_array/.finit_array for output. */ | |
10446 | if (o->size > address_size | |
10447 | && ((strncmp (o->name, ".ctors", 6) == 0 | |
10448 | && strcmp (o->output_section->name, | |
10449 | ".init_array") == 0) | |
10450 | || (strncmp (o->name, ".dtors", 6) == 0 | |
10451 | && strcmp (o->output_section->name, | |
10452 | ".fini_array") == 0)) | |
10453 | && (o->name[6] == 0 || o->name[6] == '.')) | |
c152c796 | 10454 | { |
056bafd4 MR |
10455 | if (o->size * bed->s->int_rels_per_ext_rel |
10456 | != o->reloc_count * address_size) | |
310fd250 | 10457 | { |
4eca0228 | 10458 | _bfd_error_handler |
695344c0 | 10459 | /* xgettext:c-format */ |
310fd250 L |
10460 | (_("error: %B: size of section %A is not " |
10461 | "multiple of address size"), | |
10462 | input_bfd, o); | |
8c6716e5 | 10463 | bfd_set_error (bfd_error_bad_value); |
310fd250 L |
10464 | return FALSE; |
10465 | } | |
10466 | o->flags |= SEC_ELF_REVERSE_COPY; | |
c152c796 AM |
10467 | } |
10468 | ||
0f02bbd9 | 10469 | action_discarded = -1; |
c152c796 | 10470 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
10471 | action_discarded = (*bed->action_discarded) (o); |
10472 | ||
10473 | /* Run through the relocs evaluating complex reloc symbols and | |
10474 | looking for relocs against symbols from discarded sections | |
10475 | or section symbols from removed link-once sections. | |
10476 | Complain about relocs against discarded sections. Zero | |
10477 | relocs against removed link-once sections. */ | |
10478 | ||
10479 | rel = internal_relocs; | |
056bafd4 | 10480 | relend = rel + o->reloc_count; |
0f02bbd9 | 10481 | for ( ; rel < relend; rel++) |
c152c796 | 10482 | { |
0f02bbd9 AM |
10483 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
10484 | unsigned int s_type; | |
10485 | asection **ps, *sec; | |
10486 | struct elf_link_hash_entry *h = NULL; | |
10487 | const char *sym_name; | |
c152c796 | 10488 | |
0f02bbd9 AM |
10489 | if (r_symndx == STN_UNDEF) |
10490 | continue; | |
c152c796 | 10491 | |
0f02bbd9 AM |
10492 | if (r_symndx >= locsymcount |
10493 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10494 | && flinfo->sections[r_symndx] == NULL)) |
0f02bbd9 AM |
10495 | { |
10496 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 10497 | |
0f02bbd9 AM |
10498 | /* Badly formatted input files can contain relocs that |
10499 | reference non-existant symbols. Check here so that | |
10500 | we do not seg fault. */ | |
10501 | if (h == NULL) | |
c152c796 | 10502 | { |
4eca0228 | 10503 | _bfd_error_handler |
695344c0 | 10504 | /* xgettext:c-format */ |
76cfced5 | 10505 | (_("error: %B contains a reloc (%#Lx) for section %A " |
0f02bbd9 | 10506 | "that references a non-existent global symbol"), |
76cfced5 | 10507 | input_bfd, rel->r_info, o); |
0f02bbd9 AM |
10508 | bfd_set_error (bfd_error_bad_value); |
10509 | return FALSE; | |
10510 | } | |
3b36f7e6 | 10511 | |
0f02bbd9 AM |
10512 | while (h->root.type == bfd_link_hash_indirect |
10513 | || h->root.type == bfd_link_hash_warning) | |
10514 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 10515 | |
0f02bbd9 | 10516 | s_type = h->type; |
cdd3575c | 10517 | |
9e2dec47 | 10518 | /* If a plugin symbol is referenced from a non-IR file, |
ca4be51c AM |
10519 | mark the symbol as undefined. Note that the |
10520 | linker may attach linker created dynamic sections | |
10521 | to the plugin bfd. Symbols defined in linker | |
10522 | created sections are not plugin symbols. */ | |
bc4e12de | 10523 | if ((h->root.non_ir_ref_regular |
4070765b | 10524 | || h->root.non_ir_ref_dynamic) |
9e2dec47 L |
10525 | && (h->root.type == bfd_link_hash_defined |
10526 | || h->root.type == bfd_link_hash_defweak) | |
10527 | && (h->root.u.def.section->flags | |
10528 | & SEC_LINKER_CREATED) == 0 | |
10529 | && h->root.u.def.section->owner != NULL | |
10530 | && (h->root.u.def.section->owner->flags | |
10531 | & BFD_PLUGIN) != 0) | |
10532 | { | |
10533 | h->root.type = bfd_link_hash_undefined; | |
10534 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
10535 | } | |
10536 | ||
0f02bbd9 AM |
10537 | ps = NULL; |
10538 | if (h->root.type == bfd_link_hash_defined | |
10539 | || h->root.type == bfd_link_hash_defweak) | |
10540 | ps = &h->root.u.def.section; | |
10541 | ||
10542 | sym_name = h->root.root.string; | |
10543 | } | |
10544 | else | |
10545 | { | |
10546 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
10547 | ||
10548 | s_type = ELF_ST_TYPE (sym->st_info); | |
8b127cbc | 10549 | ps = &flinfo->sections[r_symndx]; |
0f02bbd9 AM |
10550 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, |
10551 | sym, *ps); | |
10552 | } | |
c152c796 | 10553 | |
c301e700 | 10554 | if ((s_type == STT_RELC || s_type == STT_SRELC) |
0e1862bb | 10555 | && !bfd_link_relocatable (flinfo->info)) |
0f02bbd9 AM |
10556 | { |
10557 | bfd_vma val; | |
10558 | bfd_vma dot = (rel->r_offset | |
10559 | + o->output_offset + o->output_section->vma); | |
10560 | #ifdef DEBUG | |
10561 | printf ("Encountered a complex symbol!"); | |
10562 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
9ccb8af9 AM |
10563 | input_bfd->filename, o->name, |
10564 | (long) (rel - internal_relocs)); | |
0f02bbd9 AM |
10565 | printf (" symbol: idx %8.8lx, name %s\n", |
10566 | r_symndx, sym_name); | |
10567 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
10568 | (unsigned long) rel->r_info, | |
10569 | (unsigned long) rel->r_offset); | |
10570 | #endif | |
8b127cbc | 10571 | if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, |
0f02bbd9 AM |
10572 | isymbuf, locsymcount, s_type == STT_SRELC)) |
10573 | return FALSE; | |
10574 | ||
10575 | /* Symbol evaluated OK. Update to absolute value. */ | |
10576 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
10577 | r_symndx, val); | |
10578 | continue; | |
10579 | } | |
10580 | ||
10581 | if (action_discarded != -1 && ps != NULL) | |
10582 | { | |
cdd3575c AM |
10583 | /* Complain if the definition comes from a |
10584 | discarded section. */ | |
dbaa2011 | 10585 | if ((sec = *ps) != NULL && discarded_section (sec)) |
cdd3575c | 10586 | { |
cf35638d | 10587 | BFD_ASSERT (r_symndx != STN_UNDEF); |
0f02bbd9 | 10588 | if (action_discarded & COMPLAIN) |
8b127cbc | 10589 | (*flinfo->info->callbacks->einfo) |
695344c0 | 10590 | /* xgettext:c-format */ |
e1fffbe6 | 10591 | (_("%X`%s' referenced in section `%A' of %B: " |
58ac56d0 | 10592 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 10593 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 10594 | |
87e5235d | 10595 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 10596 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
10597 | really defined in the kept linkonce section. |
10598 | FIXME: This is quite broken. Modifying the | |
10599 | symbol here means we will be changing all later | |
e0ae6d6f | 10600 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 10601 | if (action_discarded & PRETEND) |
87e5235d | 10602 | { |
01b3c8ab L |
10603 | asection *kept; |
10604 | ||
c0f00686 | 10605 | kept = _bfd_elf_check_kept_section (sec, |
8b127cbc | 10606 | flinfo->info); |
01b3c8ab | 10607 | if (kept != NULL) |
87e5235d AM |
10608 | { |
10609 | *ps = kept; | |
10610 | continue; | |
10611 | } | |
10612 | } | |
c152c796 AM |
10613 | } |
10614 | } | |
10615 | } | |
10616 | ||
10617 | /* Relocate the section by invoking a back end routine. | |
10618 | ||
10619 | The back end routine is responsible for adjusting the | |
10620 | section contents as necessary, and (if using Rela relocs | |
10621 | and generating a relocatable output file) adjusting the | |
10622 | reloc addend as necessary. | |
10623 | ||
10624 | The back end routine does not have to worry about setting | |
10625 | the reloc address or the reloc symbol index. | |
10626 | ||
10627 | The back end routine is given a pointer to the swapped in | |
10628 | internal symbols, and can access the hash table entries | |
10629 | for the external symbols via elf_sym_hashes (input_bfd). | |
10630 | ||
10631 | When generating relocatable output, the back end routine | |
10632 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
10633 | output symbol is going to be a section symbol | |
10634 | corresponding to the output section, which will require | |
10635 | the addend to be adjusted. */ | |
10636 | ||
8b127cbc | 10637 | ret = (*relocate_section) (output_bfd, flinfo->info, |
c152c796 AM |
10638 | input_bfd, o, contents, |
10639 | internal_relocs, | |
10640 | isymbuf, | |
8b127cbc | 10641 | flinfo->sections); |
ece5ef60 | 10642 | if (!ret) |
c152c796 AM |
10643 | return FALSE; |
10644 | ||
ece5ef60 | 10645 | if (ret == 2 |
0e1862bb | 10646 | || bfd_link_relocatable (flinfo->info) |
8b127cbc | 10647 | || flinfo->info->emitrelocations) |
c152c796 AM |
10648 | { |
10649 | Elf_Internal_Rela *irela; | |
d4730f92 | 10650 | Elf_Internal_Rela *irelaend, *irelamid; |
c152c796 AM |
10651 | bfd_vma last_offset; |
10652 | struct elf_link_hash_entry **rel_hash; | |
d4730f92 BS |
10653 | struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; |
10654 | Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; | |
c152c796 | 10655 | unsigned int next_erel; |
c152c796 | 10656 | bfd_boolean rela_normal; |
d4730f92 | 10657 | struct bfd_elf_section_data *esdi, *esdo; |
c152c796 | 10658 | |
d4730f92 BS |
10659 | esdi = elf_section_data (o); |
10660 | esdo = elf_section_data (o->output_section); | |
10661 | rela_normal = FALSE; | |
c152c796 AM |
10662 | |
10663 | /* Adjust the reloc addresses and symbol indices. */ | |
10664 | ||
10665 | irela = internal_relocs; | |
056bafd4 | 10666 | irelaend = irela + o->reloc_count; |
d4730f92 BS |
10667 | rel_hash = esdo->rel.hashes + esdo->rel.count; |
10668 | /* We start processing the REL relocs, if any. When we reach | |
10669 | IRELAMID in the loop, we switch to the RELA relocs. */ | |
10670 | irelamid = irela; | |
10671 | if (esdi->rel.hdr != NULL) | |
10672 | irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) | |
10673 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10674 | rel_hash_list = rel_hash; |
d4730f92 | 10675 | rela_hash_list = NULL; |
c152c796 | 10676 | last_offset = o->output_offset; |
0e1862bb | 10677 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10678 | last_offset += o->output_section->vma; |
10679 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
10680 | { | |
10681 | unsigned long r_symndx; | |
10682 | asection *sec; | |
10683 | Elf_Internal_Sym sym; | |
10684 | ||
10685 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
10686 | { | |
10687 | rel_hash++; | |
10688 | next_erel = 0; | |
10689 | } | |
10690 | ||
d4730f92 BS |
10691 | if (irela == irelamid) |
10692 | { | |
10693 | rel_hash = esdo->rela.hashes + esdo->rela.count; | |
10694 | rela_hash_list = rel_hash; | |
10695 | rela_normal = bed->rela_normal; | |
10696 | } | |
10697 | ||
c152c796 | 10698 | irela->r_offset = _bfd_elf_section_offset (output_bfd, |
8b127cbc | 10699 | flinfo->info, o, |
c152c796 AM |
10700 | irela->r_offset); |
10701 | if (irela->r_offset >= (bfd_vma) -2) | |
10702 | { | |
10703 | /* This is a reloc for a deleted entry or somesuch. | |
10704 | Turn it into an R_*_NONE reloc, at the same | |
10705 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 10706 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
10707 | being ordered. */ |
10708 | irela->r_offset = last_offset; | |
10709 | irela->r_info = 0; | |
10710 | irela->r_addend = 0; | |
10711 | continue; | |
10712 | } | |
10713 | ||
10714 | irela->r_offset += o->output_offset; | |
10715 | ||
10716 | /* Relocs in an executable have to be virtual addresses. */ | |
0e1862bb | 10717 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10718 | irela->r_offset += o->output_section->vma; |
10719 | ||
10720 | last_offset = irela->r_offset; | |
10721 | ||
10722 | r_symndx = irela->r_info >> r_sym_shift; | |
10723 | if (r_symndx == STN_UNDEF) | |
10724 | continue; | |
10725 | ||
10726 | if (r_symndx >= locsymcount | |
10727 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10728 | && flinfo->sections[r_symndx] == NULL)) |
c152c796 AM |
10729 | { |
10730 | struct elf_link_hash_entry *rh; | |
10731 | unsigned long indx; | |
10732 | ||
10733 | /* This is a reloc against a global symbol. We | |
10734 | have not yet output all the local symbols, so | |
10735 | we do not know the symbol index of any global | |
10736 | symbol. We set the rel_hash entry for this | |
10737 | reloc to point to the global hash table entry | |
10738 | for this symbol. The symbol index is then | |
ee75fd95 | 10739 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
10740 | indx = r_symndx - extsymoff; |
10741 | rh = elf_sym_hashes (input_bfd)[indx]; | |
10742 | while (rh->root.type == bfd_link_hash_indirect | |
10743 | || rh->root.type == bfd_link_hash_warning) | |
10744 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
10745 | ||
10746 | /* Setting the index to -2 tells | |
10747 | elf_link_output_extsym that this symbol is | |
10748 | used by a reloc. */ | |
10749 | BFD_ASSERT (rh->indx < 0); | |
10750 | rh->indx = -2; | |
c152c796 AM |
10751 | *rel_hash = rh; |
10752 | ||
10753 | continue; | |
10754 | } | |
10755 | ||
10756 | /* This is a reloc against a local symbol. */ | |
10757 | ||
10758 | *rel_hash = NULL; | |
10759 | sym = isymbuf[r_symndx]; | |
8b127cbc | 10760 | sec = flinfo->sections[r_symndx]; |
c152c796 AM |
10761 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) |
10762 | { | |
10763 | /* I suppose the backend ought to fill in the | |
10764 | section of any STT_SECTION symbol against a | |
6a8d1586 | 10765 | processor specific section. */ |
cf35638d | 10766 | r_symndx = STN_UNDEF; |
6a8d1586 AM |
10767 | if (bfd_is_abs_section (sec)) |
10768 | ; | |
c152c796 AM |
10769 | else if (sec == NULL || sec->owner == NULL) |
10770 | { | |
10771 | bfd_set_error (bfd_error_bad_value); | |
10772 | return FALSE; | |
10773 | } | |
10774 | else | |
10775 | { | |
6a8d1586 AM |
10776 | asection *osec = sec->output_section; |
10777 | ||
10778 | /* If we have discarded a section, the output | |
10779 | section will be the absolute section. In | |
ab96bf03 AM |
10780 | case of discarded SEC_MERGE sections, use |
10781 | the kept section. relocate_section should | |
10782 | have already handled discarded linkonce | |
10783 | sections. */ | |
6a8d1586 AM |
10784 | if (bfd_is_abs_section (osec) |
10785 | && sec->kept_section != NULL | |
10786 | && sec->kept_section->output_section != NULL) | |
10787 | { | |
10788 | osec = sec->kept_section->output_section; | |
10789 | irela->r_addend -= osec->vma; | |
10790 | } | |
10791 | ||
10792 | if (!bfd_is_abs_section (osec)) | |
10793 | { | |
10794 | r_symndx = osec->target_index; | |
cf35638d | 10795 | if (r_symndx == STN_UNDEF) |
74541ad4 | 10796 | { |
051d833a AM |
10797 | irela->r_addend += osec->vma; |
10798 | osec = _bfd_nearby_section (output_bfd, osec, | |
10799 | osec->vma); | |
10800 | irela->r_addend -= osec->vma; | |
10801 | r_symndx = osec->target_index; | |
74541ad4 | 10802 | } |
6a8d1586 | 10803 | } |
c152c796 AM |
10804 | } |
10805 | ||
10806 | /* Adjust the addend according to where the | |
10807 | section winds up in the output section. */ | |
10808 | if (rela_normal) | |
10809 | irela->r_addend += sec->output_offset; | |
10810 | } | |
10811 | else | |
10812 | { | |
8b127cbc | 10813 | if (flinfo->indices[r_symndx] == -1) |
c152c796 AM |
10814 | { |
10815 | unsigned long shlink; | |
10816 | const char *name; | |
10817 | asection *osec; | |
6e0b88f1 | 10818 | long indx; |
c152c796 | 10819 | |
8b127cbc | 10820 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
10821 | { |
10822 | /* You can't do ld -r -s. */ | |
10823 | bfd_set_error (bfd_error_invalid_operation); | |
10824 | return FALSE; | |
10825 | } | |
10826 | ||
10827 | /* This symbol was skipped earlier, but | |
10828 | since it is needed by a reloc, we | |
10829 | must output it now. */ | |
10830 | shlink = symtab_hdr->sh_link; | |
10831 | name = (bfd_elf_string_from_elf_section | |
10832 | (input_bfd, shlink, sym.st_name)); | |
10833 | if (name == NULL) | |
10834 | return FALSE; | |
10835 | ||
10836 | osec = sec->output_section; | |
10837 | sym.st_shndx = | |
10838 | _bfd_elf_section_from_bfd_section (output_bfd, | |
10839 | osec); | |
10840 | if (sym.st_shndx == SHN_BAD) | |
10841 | return FALSE; | |
10842 | ||
10843 | sym.st_value += sec->output_offset; | |
0e1862bb | 10844 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10845 | { |
10846 | sym.st_value += osec->vma; | |
10847 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
10848 | { | |
10849 | /* STT_TLS symbols are relative to PT_TLS | |
10850 | segment base. */ | |
8b127cbc | 10851 | BFD_ASSERT (elf_hash_table (flinfo->info) |
c152c796 | 10852 | ->tls_sec != NULL); |
8b127cbc | 10853 | sym.st_value -= (elf_hash_table (flinfo->info) |
c152c796 AM |
10854 | ->tls_sec->vma); |
10855 | } | |
10856 | } | |
10857 | ||
6e0b88f1 | 10858 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10859 | ret = elf_link_output_symstrtab (flinfo, name, |
10860 | &sym, sec, | |
10861 | NULL); | |
6e0b88f1 | 10862 | if (ret == 0) |
c152c796 | 10863 | return FALSE; |
6e0b88f1 | 10864 | else if (ret == 1) |
8b127cbc | 10865 | flinfo->indices[r_symndx] = indx; |
6e0b88f1 AM |
10866 | else |
10867 | abort (); | |
c152c796 AM |
10868 | } |
10869 | ||
8b127cbc | 10870 | r_symndx = flinfo->indices[r_symndx]; |
c152c796 AM |
10871 | } |
10872 | ||
10873 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
10874 | | (irela->r_info & r_type_mask)); | |
10875 | } | |
10876 | ||
10877 | /* Swap out the relocs. */ | |
d4730f92 BS |
10878 | input_rel_hdr = esdi->rel.hdr; |
10879 | if (input_rel_hdr && input_rel_hdr->sh_size != 0) | |
c152c796 | 10880 | { |
d4730f92 BS |
10881 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
10882 | input_rel_hdr, | |
10883 | internal_relocs, | |
10884 | rel_hash_list)) | |
10885 | return FALSE; | |
c152c796 AM |
10886 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) |
10887 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10888 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
d4730f92 BS |
10889 | } |
10890 | ||
10891 | input_rela_hdr = esdi->rela.hdr; | |
10892 | if (input_rela_hdr && input_rela_hdr->sh_size != 0) | |
10893 | { | |
eac338cf | 10894 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
d4730f92 | 10895 | input_rela_hdr, |
eac338cf | 10896 | internal_relocs, |
d4730f92 | 10897 | rela_hash_list)) |
c152c796 AM |
10898 | return FALSE; |
10899 | } | |
10900 | } | |
10901 | } | |
10902 | ||
10903 | /* Write out the modified section contents. */ | |
10904 | if (bed->elf_backend_write_section | |
8b127cbc | 10905 | && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, |
c7b8f16e | 10906 | contents)) |
c152c796 AM |
10907 | { |
10908 | /* Section written out. */ | |
10909 | } | |
10910 | else switch (o->sec_info_type) | |
10911 | { | |
dbaa2011 | 10912 | case SEC_INFO_TYPE_STABS: |
c152c796 AM |
10913 | if (! (_bfd_write_section_stabs |
10914 | (output_bfd, | |
8b127cbc | 10915 | &elf_hash_table (flinfo->info)->stab_info, |
c152c796 AM |
10916 | o, &elf_section_data (o)->sec_info, contents))) |
10917 | return FALSE; | |
10918 | break; | |
dbaa2011 | 10919 | case SEC_INFO_TYPE_MERGE: |
c152c796 AM |
10920 | if (! _bfd_write_merged_section (output_bfd, o, |
10921 | elf_section_data (o)->sec_info)) | |
10922 | return FALSE; | |
10923 | break; | |
dbaa2011 | 10924 | case SEC_INFO_TYPE_EH_FRAME: |
c152c796 | 10925 | { |
8b127cbc | 10926 | if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, |
c152c796 AM |
10927 | o, contents)) |
10928 | return FALSE; | |
10929 | } | |
10930 | break; | |
2f0c68f2 CM |
10931 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
10932 | { | |
10933 | if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, | |
10934 | flinfo->info, | |
10935 | o, contents)) | |
10936 | return FALSE; | |
10937 | } | |
10938 | break; | |
c152c796 AM |
10939 | default: |
10940 | { | |
310fd250 L |
10941 | if (! (o->flags & SEC_EXCLUDE)) |
10942 | { | |
10943 | file_ptr offset = (file_ptr) o->output_offset; | |
10944 | bfd_size_type todo = o->size; | |
37b01f6a DG |
10945 | |
10946 | offset *= bfd_octets_per_byte (output_bfd); | |
10947 | ||
310fd250 L |
10948 | if ((o->flags & SEC_ELF_REVERSE_COPY)) |
10949 | { | |
10950 | /* Reverse-copy input section to output. */ | |
10951 | do | |
10952 | { | |
10953 | todo -= address_size; | |
10954 | if (! bfd_set_section_contents (output_bfd, | |
10955 | o->output_section, | |
10956 | contents + todo, | |
10957 | offset, | |
10958 | address_size)) | |
10959 | return FALSE; | |
10960 | if (todo == 0) | |
10961 | break; | |
10962 | offset += address_size; | |
10963 | } | |
10964 | while (1); | |
10965 | } | |
10966 | else if (! bfd_set_section_contents (output_bfd, | |
10967 | o->output_section, | |
10968 | contents, | |
10969 | offset, todo)) | |
10970 | return FALSE; | |
10971 | } | |
c152c796 AM |
10972 | } |
10973 | break; | |
10974 | } | |
10975 | } | |
10976 | ||
10977 | return TRUE; | |
10978 | } | |
10979 | ||
10980 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 10981 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
10982 | is used to build constructor and destructor tables when linking |
10983 | with -Ur. */ | |
10984 | ||
10985 | static bfd_boolean | |
10986 | elf_reloc_link_order (bfd *output_bfd, | |
10987 | struct bfd_link_info *info, | |
10988 | asection *output_section, | |
10989 | struct bfd_link_order *link_order) | |
10990 | { | |
10991 | reloc_howto_type *howto; | |
10992 | long indx; | |
10993 | bfd_vma offset; | |
10994 | bfd_vma addend; | |
d4730f92 | 10995 | struct bfd_elf_section_reloc_data *reldata; |
c152c796 AM |
10996 | struct elf_link_hash_entry **rel_hash_ptr; |
10997 | Elf_Internal_Shdr *rel_hdr; | |
10998 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
10999 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
11000 | bfd_byte *erel; | |
11001 | unsigned int i; | |
d4730f92 | 11002 | struct bfd_elf_section_data *esdo = elf_section_data (output_section); |
c152c796 AM |
11003 | |
11004 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
11005 | if (howto == NULL) | |
11006 | { | |
11007 | bfd_set_error (bfd_error_bad_value); | |
11008 | return FALSE; | |
11009 | } | |
11010 | ||
11011 | addend = link_order->u.reloc.p->addend; | |
11012 | ||
d4730f92 BS |
11013 | if (esdo->rel.hdr) |
11014 | reldata = &esdo->rel; | |
11015 | else if (esdo->rela.hdr) | |
11016 | reldata = &esdo->rela; | |
11017 | else | |
11018 | { | |
11019 | reldata = NULL; | |
11020 | BFD_ASSERT (0); | |
11021 | } | |
11022 | ||
c152c796 | 11023 | /* Figure out the symbol index. */ |
d4730f92 | 11024 | rel_hash_ptr = reldata->hashes + reldata->count; |
c152c796 AM |
11025 | if (link_order->type == bfd_section_reloc_link_order) |
11026 | { | |
11027 | indx = link_order->u.reloc.p->u.section->target_index; | |
11028 | BFD_ASSERT (indx != 0); | |
11029 | *rel_hash_ptr = NULL; | |
11030 | } | |
11031 | else | |
11032 | { | |
11033 | struct elf_link_hash_entry *h; | |
11034 | ||
11035 | /* Treat a reloc against a defined symbol as though it were | |
11036 | actually against the section. */ | |
11037 | h = ((struct elf_link_hash_entry *) | |
11038 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
11039 | link_order->u.reloc.p->u.name, | |
11040 | FALSE, FALSE, TRUE)); | |
11041 | if (h != NULL | |
11042 | && (h->root.type == bfd_link_hash_defined | |
11043 | || h->root.type == bfd_link_hash_defweak)) | |
11044 | { | |
11045 | asection *section; | |
11046 | ||
11047 | section = h->root.u.def.section; | |
11048 | indx = section->output_section->target_index; | |
11049 | *rel_hash_ptr = NULL; | |
11050 | /* It seems that we ought to add the symbol value to the | |
11051 | addend here, but in practice it has already been added | |
11052 | because it was passed to constructor_callback. */ | |
11053 | addend += section->output_section->vma + section->output_offset; | |
11054 | } | |
11055 | else if (h != NULL) | |
11056 | { | |
11057 | /* Setting the index to -2 tells elf_link_output_extsym that | |
11058 | this symbol is used by a reloc. */ | |
11059 | h->indx = -2; | |
11060 | *rel_hash_ptr = h; | |
11061 | indx = 0; | |
11062 | } | |
11063 | else | |
11064 | { | |
1a72702b AM |
11065 | (*info->callbacks->unattached_reloc) |
11066 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); | |
c152c796 AM |
11067 | indx = 0; |
11068 | } | |
11069 | } | |
11070 | ||
11071 | /* If this is an inplace reloc, we must write the addend into the | |
11072 | object file. */ | |
11073 | if (howto->partial_inplace && addend != 0) | |
11074 | { | |
11075 | bfd_size_type size; | |
11076 | bfd_reloc_status_type rstat; | |
11077 | bfd_byte *buf; | |
11078 | bfd_boolean ok; | |
11079 | const char *sym_name; | |
11080 | ||
a50b1753 NC |
11081 | size = (bfd_size_type) bfd_get_reloc_size (howto); |
11082 | buf = (bfd_byte *) bfd_zmalloc (size); | |
6346d5ca | 11083 | if (buf == NULL && size != 0) |
c152c796 AM |
11084 | return FALSE; |
11085 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
11086 | switch (rstat) | |
11087 | { | |
11088 | case bfd_reloc_ok: | |
11089 | break; | |
11090 | ||
11091 | default: | |
11092 | case bfd_reloc_outofrange: | |
11093 | abort (); | |
11094 | ||
11095 | case bfd_reloc_overflow: | |
11096 | if (link_order->type == bfd_section_reloc_link_order) | |
11097 | sym_name = bfd_section_name (output_bfd, | |
11098 | link_order->u.reloc.p->u.section); | |
11099 | else | |
11100 | sym_name = link_order->u.reloc.p->u.name; | |
1a72702b AM |
11101 | (*info->callbacks->reloc_overflow) (info, NULL, sym_name, |
11102 | howto->name, addend, NULL, NULL, | |
11103 | (bfd_vma) 0); | |
c152c796 AM |
11104 | break; |
11105 | } | |
37b01f6a | 11106 | |
c152c796 | 11107 | ok = bfd_set_section_contents (output_bfd, output_section, buf, |
37b01f6a DG |
11108 | link_order->offset |
11109 | * bfd_octets_per_byte (output_bfd), | |
11110 | size); | |
c152c796 AM |
11111 | free (buf); |
11112 | if (! ok) | |
11113 | return FALSE; | |
11114 | } | |
11115 | ||
11116 | /* The address of a reloc is relative to the section in a | |
11117 | relocatable file, and is a virtual address in an executable | |
11118 | file. */ | |
11119 | offset = link_order->offset; | |
0e1862bb | 11120 | if (! bfd_link_relocatable (info)) |
c152c796 AM |
11121 | offset += output_section->vma; |
11122 | ||
11123 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
11124 | { | |
11125 | irel[i].r_offset = offset; | |
11126 | irel[i].r_info = 0; | |
11127 | irel[i].r_addend = 0; | |
11128 | } | |
11129 | if (bed->s->arch_size == 32) | |
11130 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
11131 | else | |
11132 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
11133 | ||
d4730f92 | 11134 | rel_hdr = reldata->hdr; |
c152c796 AM |
11135 | erel = rel_hdr->contents; |
11136 | if (rel_hdr->sh_type == SHT_REL) | |
11137 | { | |
d4730f92 | 11138 | erel += reldata->count * bed->s->sizeof_rel; |
c152c796 AM |
11139 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); |
11140 | } | |
11141 | else | |
11142 | { | |
11143 | irel[0].r_addend = addend; | |
d4730f92 | 11144 | erel += reldata->count * bed->s->sizeof_rela; |
c152c796 AM |
11145 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); |
11146 | } | |
11147 | ||
d4730f92 | 11148 | ++reldata->count; |
c152c796 AM |
11149 | |
11150 | return TRUE; | |
11151 | } | |
11152 | ||
0b52efa6 PB |
11153 | |
11154 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
11155 | ||
11156 | static bfd_vma | |
11157 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
11158 | { | |
11159 | Elf_Internal_Shdr **elf_shdrp; | |
11160 | asection *s; | |
11161 | int elfsec; | |
11162 | ||
11163 | s = p->u.indirect.section; | |
11164 | elf_shdrp = elf_elfsections (s->owner); | |
11165 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
11166 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
11167 | /* PR 290: |
11168 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 11169 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
11170 | sh_info fields. Hence we could get the situation |
11171 | where elfsec is 0. */ | |
11172 | if (elfsec == 0) | |
11173 | { | |
11174 | const struct elf_backend_data *bed | |
11175 | = get_elf_backend_data (s->owner); | |
11176 | if (bed->link_order_error_handler) | |
d003868e | 11177 | bed->link_order_error_handler |
695344c0 | 11178 | /* xgettext:c-format */ |
d003868e | 11179 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); |
185d09ad L |
11180 | return 0; |
11181 | } | |
11182 | else | |
11183 | { | |
11184 | s = elf_shdrp[elfsec]->bfd_section; | |
11185 | return s->output_section->vma + s->output_offset; | |
11186 | } | |
0b52efa6 PB |
11187 | } |
11188 | ||
11189 | ||
11190 | /* Compare two sections based on the locations of the sections they are | |
11191 | linked to. Used by elf_fixup_link_order. */ | |
11192 | ||
11193 | static int | |
11194 | compare_link_order (const void * a, const void * b) | |
11195 | { | |
11196 | bfd_vma apos; | |
11197 | bfd_vma bpos; | |
11198 | ||
11199 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
11200 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
11201 | if (apos < bpos) | |
11202 | return -1; | |
11203 | return apos > bpos; | |
11204 | } | |
11205 | ||
11206 | ||
11207 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
11208 | order as their linked sections. Returns false if this could not be done | |
11209 | because an output section includes both ordered and unordered | |
11210 | sections. Ideally we'd do this in the linker proper. */ | |
11211 | ||
11212 | static bfd_boolean | |
11213 | elf_fixup_link_order (bfd *abfd, asection *o) | |
11214 | { | |
11215 | int seen_linkorder; | |
11216 | int seen_other; | |
11217 | int n; | |
11218 | struct bfd_link_order *p; | |
11219 | bfd *sub; | |
11220 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 11221 | unsigned elfsec; |
0b52efa6 | 11222 | struct bfd_link_order **sections; |
d33cdfe3 | 11223 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 11224 | bfd_vma offset; |
3b36f7e6 | 11225 | |
d33cdfe3 L |
11226 | other_sec = NULL; |
11227 | linkorder_sec = NULL; | |
0b52efa6 PB |
11228 | seen_other = 0; |
11229 | seen_linkorder = 0; | |
8423293d | 11230 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 11231 | { |
d33cdfe3 | 11232 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
11233 | { |
11234 | s = p->u.indirect.section; | |
d33cdfe3 L |
11235 | sub = s->owner; |
11236 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
11237 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
11238 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
11239 | && elfsec < elf_numsections (sub) | |
4fbb74a6 AM |
11240 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER |
11241 | && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) | |
d33cdfe3 L |
11242 | { |
11243 | seen_linkorder++; | |
11244 | linkorder_sec = s; | |
11245 | } | |
0b52efa6 | 11246 | else |
d33cdfe3 L |
11247 | { |
11248 | seen_other++; | |
11249 | other_sec = s; | |
11250 | } | |
0b52efa6 PB |
11251 | } |
11252 | else | |
11253 | seen_other++; | |
d33cdfe3 L |
11254 | |
11255 | if (seen_other && seen_linkorder) | |
11256 | { | |
11257 | if (other_sec && linkorder_sec) | |
4eca0228 | 11258 | _bfd_error_handler |
695344c0 | 11259 | /* xgettext:c-format */ |
4eca0228 AM |
11260 | (_("%A has both ordered [`%A' in %B] " |
11261 | "and unordered [`%A' in %B] sections"), | |
63a5468a AM |
11262 | o, linkorder_sec, linkorder_sec->owner, |
11263 | other_sec, other_sec->owner); | |
d33cdfe3 | 11264 | else |
4eca0228 AM |
11265 | _bfd_error_handler |
11266 | (_("%A has both ordered and unordered sections"), o); | |
d33cdfe3 L |
11267 | bfd_set_error (bfd_error_bad_value); |
11268 | return FALSE; | |
11269 | } | |
0b52efa6 PB |
11270 | } |
11271 | ||
11272 | if (!seen_linkorder) | |
11273 | return TRUE; | |
11274 | ||
0b52efa6 | 11275 | sections = (struct bfd_link_order **) |
14b1c01e AM |
11276 | bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); |
11277 | if (sections == NULL) | |
11278 | return FALSE; | |
0b52efa6 | 11279 | seen_linkorder = 0; |
3b36f7e6 | 11280 | |
8423293d | 11281 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
11282 | { |
11283 | sections[seen_linkorder++] = p; | |
11284 | } | |
11285 | /* Sort the input sections in the order of their linked section. */ | |
11286 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
11287 | compare_link_order); | |
11288 | ||
11289 | /* Change the offsets of the sections. */ | |
11290 | offset = 0; | |
11291 | for (n = 0; n < seen_linkorder; n++) | |
11292 | { | |
11293 | s = sections[n]->u.indirect.section; | |
461686a3 | 11294 | offset &= ~(bfd_vma) 0 << s->alignment_power; |
37b01f6a | 11295 | s->output_offset = offset / bfd_octets_per_byte (abfd); |
0b52efa6 PB |
11296 | sections[n]->offset = offset; |
11297 | offset += sections[n]->size; | |
11298 | } | |
11299 | ||
4dd07732 | 11300 | free (sections); |
0b52efa6 PB |
11301 | return TRUE; |
11302 | } | |
11303 | ||
76359541 TP |
11304 | /* Generate an import library in INFO->implib_bfd from symbols in ABFD. |
11305 | Returns TRUE upon success, FALSE otherwise. */ | |
11306 | ||
11307 | static bfd_boolean | |
11308 | elf_output_implib (bfd *abfd, struct bfd_link_info *info) | |
11309 | { | |
11310 | bfd_boolean ret = FALSE; | |
11311 | bfd *implib_bfd; | |
11312 | const struct elf_backend_data *bed; | |
11313 | flagword flags; | |
11314 | enum bfd_architecture arch; | |
11315 | unsigned int mach; | |
11316 | asymbol **sympp = NULL; | |
11317 | long symsize; | |
11318 | long symcount; | |
11319 | long src_count; | |
11320 | elf_symbol_type *osymbuf; | |
11321 | ||
11322 | implib_bfd = info->out_implib_bfd; | |
11323 | bed = get_elf_backend_data (abfd); | |
11324 | ||
11325 | if (!bfd_set_format (implib_bfd, bfd_object)) | |
11326 | return FALSE; | |
11327 | ||
046734ff | 11328 | /* Use flag from executable but make it a relocatable object. */ |
76359541 TP |
11329 | flags = bfd_get_file_flags (abfd); |
11330 | flags &= ~HAS_RELOC; | |
11331 | if (!bfd_set_start_address (implib_bfd, 0) | |
046734ff | 11332 | || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P)) |
76359541 TP |
11333 | return FALSE; |
11334 | ||
11335 | /* Copy architecture of output file to import library file. */ | |
11336 | arch = bfd_get_arch (abfd); | |
11337 | mach = bfd_get_mach (abfd); | |
11338 | if (!bfd_set_arch_mach (implib_bfd, arch, mach) | |
11339 | && (abfd->target_defaulted | |
11340 | || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) | |
11341 | return FALSE; | |
11342 | ||
11343 | /* Get symbol table size. */ | |
11344 | symsize = bfd_get_symtab_upper_bound (abfd); | |
11345 | if (symsize < 0) | |
11346 | return FALSE; | |
11347 | ||
11348 | /* Read in the symbol table. */ | |
11349 | sympp = (asymbol **) xmalloc (symsize); | |
11350 | symcount = bfd_canonicalize_symtab (abfd, sympp); | |
11351 | if (symcount < 0) | |
11352 | goto free_sym_buf; | |
11353 | ||
11354 | /* Allow the BFD backend to copy any private header data it | |
11355 | understands from the output BFD to the import library BFD. */ | |
11356 | if (! bfd_copy_private_header_data (abfd, implib_bfd)) | |
11357 | goto free_sym_buf; | |
11358 | ||
11359 | /* Filter symbols to appear in the import library. */ | |
11360 | if (bed->elf_backend_filter_implib_symbols) | |
11361 | symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, | |
11362 | symcount); | |
11363 | else | |
11364 | symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); | |
11365 | if (symcount == 0) | |
11366 | { | |
5df1bc57 | 11367 | bfd_set_error (bfd_error_no_symbols); |
4eca0228 AM |
11368 | _bfd_error_handler (_("%B: no symbol found for import library"), |
11369 | implib_bfd); | |
76359541 TP |
11370 | goto free_sym_buf; |
11371 | } | |
11372 | ||
11373 | ||
11374 | /* Make symbols absolute. */ | |
11375 | osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount, | |
11376 | sizeof (*osymbuf)); | |
11377 | for (src_count = 0; src_count < symcount; src_count++) | |
11378 | { | |
11379 | memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], | |
11380 | sizeof (*osymbuf)); | |
11381 | osymbuf[src_count].symbol.section = bfd_abs_section_ptr; | |
11382 | osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; | |
11383 | osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; | |
11384 | osymbuf[src_count].internal_elf_sym.st_value = | |
11385 | osymbuf[src_count].symbol.value; | |
11386 | sympp[src_count] = &osymbuf[src_count].symbol; | |
11387 | } | |
11388 | ||
11389 | bfd_set_symtab (implib_bfd, sympp, symcount); | |
11390 | ||
11391 | /* Allow the BFD backend to copy any private data it understands | |
11392 | from the output BFD to the import library BFD. This is done last | |
11393 | to permit the routine to look at the filtered symbol table. */ | |
11394 | if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) | |
11395 | goto free_sym_buf; | |
11396 | ||
11397 | if (!bfd_close (implib_bfd)) | |
11398 | goto free_sym_buf; | |
11399 | ||
11400 | ret = TRUE; | |
11401 | ||
11402 | free_sym_buf: | |
11403 | free (sympp); | |
11404 | return ret; | |
11405 | } | |
11406 | ||
9f7c3e5e AM |
11407 | static void |
11408 | elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) | |
11409 | { | |
11410 | asection *o; | |
11411 | ||
11412 | if (flinfo->symstrtab != NULL) | |
ef10c3ac | 11413 | _bfd_elf_strtab_free (flinfo->symstrtab); |
9f7c3e5e AM |
11414 | if (flinfo->contents != NULL) |
11415 | free (flinfo->contents); | |
11416 | if (flinfo->external_relocs != NULL) | |
11417 | free (flinfo->external_relocs); | |
11418 | if (flinfo->internal_relocs != NULL) | |
11419 | free (flinfo->internal_relocs); | |
11420 | if (flinfo->external_syms != NULL) | |
11421 | free (flinfo->external_syms); | |
11422 | if (flinfo->locsym_shndx != NULL) | |
11423 | free (flinfo->locsym_shndx); | |
11424 | if (flinfo->internal_syms != NULL) | |
11425 | free (flinfo->internal_syms); | |
11426 | if (flinfo->indices != NULL) | |
11427 | free (flinfo->indices); | |
11428 | if (flinfo->sections != NULL) | |
11429 | free (flinfo->sections); | |
9f7c3e5e AM |
11430 | if (flinfo->symshndxbuf != NULL) |
11431 | free (flinfo->symshndxbuf); | |
11432 | for (o = obfd->sections; o != NULL; o = o->next) | |
11433 | { | |
11434 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11435 | if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) | |
11436 | free (esdo->rel.hashes); | |
11437 | if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) | |
11438 | free (esdo->rela.hashes); | |
11439 | } | |
11440 | } | |
0b52efa6 | 11441 | |
c152c796 AM |
11442 | /* Do the final step of an ELF link. */ |
11443 | ||
11444 | bfd_boolean | |
11445 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
11446 | { | |
11447 | bfd_boolean dynamic; | |
11448 | bfd_boolean emit_relocs; | |
11449 | bfd *dynobj; | |
8b127cbc | 11450 | struct elf_final_link_info flinfo; |
91d6fa6a NC |
11451 | asection *o; |
11452 | struct bfd_link_order *p; | |
11453 | bfd *sub; | |
c152c796 AM |
11454 | bfd_size_type max_contents_size; |
11455 | bfd_size_type max_external_reloc_size; | |
11456 | bfd_size_type max_internal_reloc_count; | |
11457 | bfd_size_type max_sym_count; | |
11458 | bfd_size_type max_sym_shndx_count; | |
c152c796 AM |
11459 | Elf_Internal_Sym elfsym; |
11460 | unsigned int i; | |
11461 | Elf_Internal_Shdr *symtab_hdr; | |
11462 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
c152c796 AM |
11463 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11464 | struct elf_outext_info eoinfo; | |
11465 | bfd_boolean merged; | |
11466 | size_t relativecount = 0; | |
11467 | asection *reldyn = 0; | |
11468 | bfd_size_type amt; | |
104d59d1 JM |
11469 | asection *attr_section = NULL; |
11470 | bfd_vma attr_size = 0; | |
11471 | const char *std_attrs_section; | |
64f52338 | 11472 | struct elf_link_hash_table *htab = elf_hash_table (info); |
c152c796 | 11473 | |
64f52338 | 11474 | if (!is_elf_hash_table (htab)) |
c152c796 AM |
11475 | return FALSE; |
11476 | ||
0e1862bb | 11477 | if (bfd_link_pic (info)) |
c152c796 AM |
11478 | abfd->flags |= DYNAMIC; |
11479 | ||
64f52338 AM |
11480 | dynamic = htab->dynamic_sections_created; |
11481 | dynobj = htab->dynobj; | |
c152c796 | 11482 | |
0e1862bb | 11483 | emit_relocs = (bfd_link_relocatable (info) |
a4676736 | 11484 | || info->emitrelocations); |
c152c796 | 11485 | |
8b127cbc AM |
11486 | flinfo.info = info; |
11487 | flinfo.output_bfd = abfd; | |
ef10c3ac | 11488 | flinfo.symstrtab = _bfd_elf_strtab_init (); |
8b127cbc | 11489 | if (flinfo.symstrtab == NULL) |
c152c796 AM |
11490 | return FALSE; |
11491 | ||
11492 | if (! dynamic) | |
11493 | { | |
8b127cbc AM |
11494 | flinfo.hash_sec = NULL; |
11495 | flinfo.symver_sec = NULL; | |
c152c796 AM |
11496 | } |
11497 | else | |
11498 | { | |
3d4d4302 | 11499 | flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); |
202e2356 | 11500 | /* Note that dynsym_sec can be NULL (on VMS). */ |
3d4d4302 | 11501 | flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); |
c152c796 AM |
11502 | /* Note that it is OK if symver_sec is NULL. */ |
11503 | } | |
11504 | ||
8b127cbc AM |
11505 | flinfo.contents = NULL; |
11506 | flinfo.external_relocs = NULL; | |
11507 | flinfo.internal_relocs = NULL; | |
11508 | flinfo.external_syms = NULL; | |
11509 | flinfo.locsym_shndx = NULL; | |
11510 | flinfo.internal_syms = NULL; | |
11511 | flinfo.indices = NULL; | |
11512 | flinfo.sections = NULL; | |
8b127cbc | 11513 | flinfo.symshndxbuf = NULL; |
ffbc01cc | 11514 | flinfo.filesym_count = 0; |
c152c796 | 11515 | |
104d59d1 JM |
11516 | /* The object attributes have been merged. Remove the input |
11517 | sections from the link, and set the contents of the output | |
11518 | secton. */ | |
11519 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; | |
11520 | for (o = abfd->sections; o != NULL; o = o->next) | |
11521 | { | |
11522 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) | |
11523 | || strcmp (o->name, ".gnu.attributes") == 0) | |
11524 | { | |
11525 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
11526 | { | |
11527 | asection *input_section; | |
11528 | ||
11529 | if (p->type != bfd_indirect_link_order) | |
11530 | continue; | |
11531 | input_section = p->u.indirect.section; | |
11532 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
11533 | elf_link_input_bfd ignores this section. */ | |
11534 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
11535 | } | |
a0c8462f | 11536 | |
104d59d1 JM |
11537 | attr_size = bfd_elf_obj_attr_size (abfd); |
11538 | if (attr_size) | |
11539 | { | |
11540 | bfd_set_section_size (abfd, o, attr_size); | |
11541 | attr_section = o; | |
11542 | /* Skip this section later on. */ | |
11543 | o->map_head.link_order = NULL; | |
11544 | } | |
11545 | else | |
11546 | o->flags |= SEC_EXCLUDE; | |
11547 | } | |
11548 | } | |
11549 | ||
c152c796 AM |
11550 | /* Count up the number of relocations we will output for each output |
11551 | section, so that we know the sizes of the reloc sections. We | |
11552 | also figure out some maximum sizes. */ | |
11553 | max_contents_size = 0; | |
11554 | max_external_reloc_size = 0; | |
11555 | max_internal_reloc_count = 0; | |
11556 | max_sym_count = 0; | |
11557 | max_sym_shndx_count = 0; | |
11558 | merged = FALSE; | |
11559 | for (o = abfd->sections; o != NULL; o = o->next) | |
11560 | { | |
11561 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11562 | o->reloc_count = 0; | |
11563 | ||
8423293d | 11564 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
11565 | { |
11566 | unsigned int reloc_count = 0; | |
9eaff861 | 11567 | unsigned int additional_reloc_count = 0; |
c152c796 | 11568 | struct bfd_elf_section_data *esdi = NULL; |
c152c796 AM |
11569 | |
11570 | if (p->type == bfd_section_reloc_link_order | |
11571 | || p->type == bfd_symbol_reloc_link_order) | |
11572 | reloc_count = 1; | |
11573 | else if (p->type == bfd_indirect_link_order) | |
11574 | { | |
11575 | asection *sec; | |
11576 | ||
11577 | sec = p->u.indirect.section; | |
c152c796 AM |
11578 | |
11579 | /* Mark all sections which are to be included in the | |
11580 | link. This will normally be every section. We need | |
11581 | to do this so that we can identify any sections which | |
11582 | the linker has decided to not include. */ | |
11583 | sec->linker_mark = TRUE; | |
11584 | ||
11585 | if (sec->flags & SEC_MERGE) | |
11586 | merged = TRUE; | |
11587 | ||
eea6121a AM |
11588 | if (sec->rawsize > max_contents_size) |
11589 | max_contents_size = sec->rawsize; | |
11590 | if (sec->size > max_contents_size) | |
11591 | max_contents_size = sec->size; | |
c152c796 | 11592 | |
c152c796 AM |
11593 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour |
11594 | && (sec->owner->flags & DYNAMIC) == 0) | |
11595 | { | |
11596 | size_t sym_count; | |
11597 | ||
a961cdd5 AM |
11598 | /* We are interested in just local symbols, not all |
11599 | symbols. */ | |
c152c796 AM |
11600 | if (elf_bad_symtab (sec->owner)) |
11601 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
11602 | / bed->s->sizeof_sym); | |
11603 | else | |
11604 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
11605 | ||
11606 | if (sym_count > max_sym_count) | |
11607 | max_sym_count = sym_count; | |
11608 | ||
11609 | if (sym_count > max_sym_shndx_count | |
6a40cf0c | 11610 | && elf_symtab_shndx_list (sec->owner) != NULL) |
c152c796 AM |
11611 | max_sym_shndx_count = sym_count; |
11612 | ||
a961cdd5 AM |
11613 | if (esdo->this_hdr.sh_type == SHT_REL |
11614 | || esdo->this_hdr.sh_type == SHT_RELA) | |
11615 | /* Some backends use reloc_count in relocation sections | |
11616 | to count particular types of relocs. Of course, | |
11617 | reloc sections themselves can't have relocations. */ | |
11618 | ; | |
11619 | else if (emit_relocs) | |
11620 | { | |
11621 | reloc_count = sec->reloc_count; | |
11622 | if (bed->elf_backend_count_additional_relocs) | |
11623 | { | |
11624 | int c; | |
11625 | c = (*bed->elf_backend_count_additional_relocs) (sec); | |
11626 | additional_reloc_count += c; | |
11627 | } | |
11628 | } | |
11629 | else if (bed->elf_backend_count_relocs) | |
11630 | reloc_count = (*bed->elf_backend_count_relocs) (info, sec); | |
11631 | ||
11632 | esdi = elf_section_data (sec); | |
11633 | ||
c152c796 AM |
11634 | if ((sec->flags & SEC_RELOC) != 0) |
11635 | { | |
d4730f92 | 11636 | size_t ext_size = 0; |
c152c796 | 11637 | |
d4730f92 BS |
11638 | if (esdi->rel.hdr != NULL) |
11639 | ext_size = esdi->rel.hdr->sh_size; | |
11640 | if (esdi->rela.hdr != NULL) | |
11641 | ext_size += esdi->rela.hdr->sh_size; | |
7326c758 | 11642 | |
c152c796 AM |
11643 | if (ext_size > max_external_reloc_size) |
11644 | max_external_reloc_size = ext_size; | |
11645 | if (sec->reloc_count > max_internal_reloc_count) | |
11646 | max_internal_reloc_count = sec->reloc_count; | |
11647 | } | |
11648 | } | |
11649 | } | |
11650 | ||
11651 | if (reloc_count == 0) | |
11652 | continue; | |
11653 | ||
9eaff861 | 11654 | reloc_count += additional_reloc_count; |
c152c796 AM |
11655 | o->reloc_count += reloc_count; |
11656 | ||
0e1862bb | 11657 | if (p->type == bfd_indirect_link_order && emit_relocs) |
c152c796 | 11658 | { |
d4730f92 | 11659 | if (esdi->rel.hdr) |
9eaff861 | 11660 | { |
491d01d3 | 11661 | esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); |
9eaff861 AO |
11662 | esdo->rel.count += additional_reloc_count; |
11663 | } | |
d4730f92 | 11664 | if (esdi->rela.hdr) |
9eaff861 | 11665 | { |
491d01d3 | 11666 | esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); |
9eaff861 AO |
11667 | esdo->rela.count += additional_reloc_count; |
11668 | } | |
d4730f92 BS |
11669 | } |
11670 | else | |
11671 | { | |
11672 | if (o->use_rela_p) | |
11673 | esdo->rela.count += reloc_count; | |
2c2b4ed4 | 11674 | else |
d4730f92 | 11675 | esdo->rel.count += reloc_count; |
c152c796 | 11676 | } |
c152c796 AM |
11677 | } |
11678 | ||
9eaff861 | 11679 | if (o->reloc_count > 0) |
c152c796 AM |
11680 | o->flags |= SEC_RELOC; |
11681 | else | |
11682 | { | |
11683 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
11684 | set it (this is probably a bug) and if it is set | |
11685 | assign_section_numbers will create a reloc section. */ | |
11686 | o->flags &=~ SEC_RELOC; | |
11687 | } | |
11688 | ||
11689 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
11690 | zero. This is done in elf_fake_sections as well, but forcing | |
11691 | the VMA to 0 here will ensure that relocs against these | |
11692 | sections are handled correctly. */ | |
11693 | if ((o->flags & SEC_ALLOC) == 0 | |
11694 | && ! o->user_set_vma) | |
11695 | o->vma = 0; | |
11696 | } | |
11697 | ||
0e1862bb | 11698 | if (! bfd_link_relocatable (info) && merged) |
64f52338 | 11699 | elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd); |
c152c796 AM |
11700 | |
11701 | /* Figure out the file positions for everything but the symbol table | |
11702 | and the relocs. We set symcount to force assign_section_numbers | |
11703 | to create a symbol table. */ | |
8539e4e8 | 11704 | bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs; |
c152c796 AM |
11705 | BFD_ASSERT (! abfd->output_has_begun); |
11706 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
11707 | goto error_return; | |
11708 | ||
ee75fd95 | 11709 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
11710 | for (o = abfd->sections; o != NULL; o = o->next) |
11711 | { | |
d4730f92 | 11712 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
c152c796 AM |
11713 | if ((o->flags & SEC_RELOC) != 0) |
11714 | { | |
d4730f92 | 11715 | if (esdo->rel.hdr |
9eaff861 | 11716 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) |
c152c796 AM |
11717 | goto error_return; |
11718 | ||
d4730f92 | 11719 | if (esdo->rela.hdr |
9eaff861 | 11720 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) |
c152c796 AM |
11721 | goto error_return; |
11722 | } | |
11723 | ||
11724 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
11725 | to count upwards while actually outputting the relocations. */ | |
d4730f92 BS |
11726 | esdo->rel.count = 0; |
11727 | esdo->rela.count = 0; | |
0ce398f1 L |
11728 | |
11729 | if (esdo->this_hdr.sh_offset == (file_ptr) -1) | |
11730 | { | |
11731 | /* Cache the section contents so that they can be compressed | |
11732 | later. Use bfd_malloc since it will be freed by | |
11733 | bfd_compress_section_contents. */ | |
11734 | unsigned char *contents = esdo->this_hdr.contents; | |
11735 | if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) | |
11736 | abort (); | |
11737 | contents | |
11738 | = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); | |
11739 | if (contents == NULL) | |
11740 | goto error_return; | |
11741 | esdo->this_hdr.contents = contents; | |
11742 | } | |
c152c796 AM |
11743 | } |
11744 | ||
c152c796 | 11745 | /* We have now assigned file positions for all the sections except |
a485e98e AM |
11746 | .symtab, .strtab, and non-loaded reloc sections. We start the |
11747 | .symtab section at the current file position, and write directly | |
11748 | to it. We build the .strtab section in memory. */ | |
c152c796 AM |
11749 | bfd_get_symcount (abfd) = 0; |
11750 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11751 | /* sh_name is set in prep_headers. */ | |
11752 | symtab_hdr->sh_type = SHT_SYMTAB; | |
11753 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
11754 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
11755 | /* sh_link is set in assign_section_numbers. */ | |
11756 | /* sh_info is set below. */ | |
11757 | /* sh_offset is set just below. */ | |
72de5009 | 11758 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
c152c796 | 11759 | |
ef10c3ac L |
11760 | if (max_sym_count < 20) |
11761 | max_sym_count = 20; | |
64f52338 | 11762 | htab->strtabsize = max_sym_count; |
ef10c3ac | 11763 | amt = max_sym_count * sizeof (struct elf_sym_strtab); |
64f52338 AM |
11764 | htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt); |
11765 | if (htab->strtab == NULL) | |
c152c796 | 11766 | goto error_return; |
ef10c3ac L |
11767 | /* The real buffer will be allocated in elf_link_swap_symbols_out. */ |
11768 | flinfo.symshndxbuf | |
11769 | = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) | |
11770 | ? (Elf_External_Sym_Shndx *) -1 : NULL); | |
c152c796 | 11771 | |
8539e4e8 | 11772 | if (info->strip != strip_all || emit_relocs) |
c152c796 | 11773 | { |
8539e4e8 AM |
11774 | file_ptr off = elf_next_file_pos (abfd); |
11775 | ||
11776 | _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
11777 | ||
11778 | /* Note that at this point elf_next_file_pos (abfd) is | |
11779 | incorrect. We do not yet know the size of the .symtab section. | |
11780 | We correct next_file_pos below, after we do know the size. */ | |
11781 | ||
11782 | /* Start writing out the symbol table. The first symbol is always a | |
11783 | dummy symbol. */ | |
c152c796 AM |
11784 | elfsym.st_value = 0; |
11785 | elfsym.st_size = 0; | |
11786 | elfsym.st_info = 0; | |
11787 | elfsym.st_other = 0; | |
11788 | elfsym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 11789 | elfsym.st_target_internal = 0; |
ef10c3ac L |
11790 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, |
11791 | bfd_und_section_ptr, NULL) != 1) | |
c152c796 | 11792 | goto error_return; |
c152c796 | 11793 | |
8539e4e8 AM |
11794 | /* Output a symbol for each section. We output these even if we are |
11795 | discarding local symbols, since they are used for relocs. These | |
11796 | symbols have no names. We store the index of each one in the | |
11797 | index field of the section, so that we can find it again when | |
11798 | outputting relocs. */ | |
11799 | ||
c152c796 AM |
11800 | elfsym.st_size = 0; |
11801 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
11802 | elfsym.st_other = 0; | |
f0b5bb34 | 11803 | elfsym.st_value = 0; |
35fc36a8 | 11804 | elfsym.st_target_internal = 0; |
c152c796 AM |
11805 | for (i = 1; i < elf_numsections (abfd); i++) |
11806 | { | |
11807 | o = bfd_section_from_elf_index (abfd, i); | |
11808 | if (o != NULL) | |
f0b5bb34 AM |
11809 | { |
11810 | o->target_index = bfd_get_symcount (abfd); | |
11811 | elfsym.st_shndx = i; | |
0e1862bb | 11812 | if (!bfd_link_relocatable (info)) |
f0b5bb34 | 11813 | elfsym.st_value = o->vma; |
ef10c3ac L |
11814 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o, |
11815 | NULL) != 1) | |
f0b5bb34 AM |
11816 | goto error_return; |
11817 | } | |
c152c796 AM |
11818 | } |
11819 | } | |
11820 | ||
11821 | /* Allocate some memory to hold information read in from the input | |
11822 | files. */ | |
11823 | if (max_contents_size != 0) | |
11824 | { | |
8b127cbc AM |
11825 | flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
11826 | if (flinfo.contents == NULL) | |
c152c796 AM |
11827 | goto error_return; |
11828 | } | |
11829 | ||
11830 | if (max_external_reloc_size != 0) | |
11831 | { | |
8b127cbc AM |
11832 | flinfo.external_relocs = bfd_malloc (max_external_reloc_size); |
11833 | if (flinfo.external_relocs == NULL) | |
c152c796 AM |
11834 | goto error_return; |
11835 | } | |
11836 | ||
11837 | if (max_internal_reloc_count != 0) | |
11838 | { | |
056bafd4 | 11839 | amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela); |
8b127cbc AM |
11840 | flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); |
11841 | if (flinfo.internal_relocs == NULL) | |
c152c796 AM |
11842 | goto error_return; |
11843 | } | |
11844 | ||
11845 | if (max_sym_count != 0) | |
11846 | { | |
11847 | amt = max_sym_count * bed->s->sizeof_sym; | |
8b127cbc AM |
11848 | flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); |
11849 | if (flinfo.external_syms == NULL) | |
c152c796 AM |
11850 | goto error_return; |
11851 | ||
11852 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8b127cbc AM |
11853 | flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); |
11854 | if (flinfo.internal_syms == NULL) | |
c152c796 AM |
11855 | goto error_return; |
11856 | ||
11857 | amt = max_sym_count * sizeof (long); | |
8b127cbc AM |
11858 | flinfo.indices = (long int *) bfd_malloc (amt); |
11859 | if (flinfo.indices == NULL) | |
c152c796 AM |
11860 | goto error_return; |
11861 | ||
11862 | amt = max_sym_count * sizeof (asection *); | |
8b127cbc AM |
11863 | flinfo.sections = (asection **) bfd_malloc (amt); |
11864 | if (flinfo.sections == NULL) | |
c152c796 AM |
11865 | goto error_return; |
11866 | } | |
11867 | ||
11868 | if (max_sym_shndx_count != 0) | |
11869 | { | |
11870 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8b127cbc AM |
11871 | flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
11872 | if (flinfo.locsym_shndx == NULL) | |
c152c796 AM |
11873 | goto error_return; |
11874 | } | |
11875 | ||
64f52338 | 11876 | if (htab->tls_sec) |
c152c796 AM |
11877 | { |
11878 | bfd_vma base, end = 0; | |
11879 | asection *sec; | |
11880 | ||
64f52338 | 11881 | for (sec = htab->tls_sec; |
c152c796 AM |
11882 | sec && (sec->flags & SEC_THREAD_LOCAL); |
11883 | sec = sec->next) | |
11884 | { | |
3a800eb9 | 11885 | bfd_size_type size = sec->size; |
c152c796 | 11886 | |
3a800eb9 AM |
11887 | if (size == 0 |
11888 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 11889 | { |
91d6fa6a NC |
11890 | struct bfd_link_order *ord = sec->map_tail.link_order; |
11891 | ||
11892 | if (ord != NULL) | |
11893 | size = ord->offset + ord->size; | |
c152c796 AM |
11894 | } |
11895 | end = sec->vma + size; | |
11896 | } | |
64f52338 | 11897 | base = htab->tls_sec->vma; |
7dc98aea RO |
11898 | /* Only align end of TLS section if static TLS doesn't have special |
11899 | alignment requirements. */ | |
11900 | if (bed->static_tls_alignment == 1) | |
64f52338 AM |
11901 | end = align_power (end, htab->tls_sec->alignment_power); |
11902 | htab->tls_size = end - base; | |
c152c796 AM |
11903 | } |
11904 | ||
0b52efa6 PB |
11905 | /* Reorder SHF_LINK_ORDER sections. */ |
11906 | for (o = abfd->sections; o != NULL; o = o->next) | |
11907 | { | |
11908 | if (!elf_fixup_link_order (abfd, o)) | |
11909 | return FALSE; | |
11910 | } | |
11911 | ||
2f0c68f2 CM |
11912 | if (!_bfd_elf_fixup_eh_frame_hdr (info)) |
11913 | return FALSE; | |
11914 | ||
c152c796 AM |
11915 | /* Since ELF permits relocations to be against local symbols, we |
11916 | must have the local symbols available when we do the relocations. | |
11917 | Since we would rather only read the local symbols once, and we | |
11918 | would rather not keep them in memory, we handle all the | |
11919 | relocations for a single input file at the same time. | |
11920 | ||
11921 | Unfortunately, there is no way to know the total number of local | |
11922 | symbols until we have seen all of them, and the local symbol | |
11923 | indices precede the global symbol indices. This means that when | |
11924 | we are generating relocatable output, and we see a reloc against | |
11925 | a global symbol, we can not know the symbol index until we have | |
11926 | finished examining all the local symbols to see which ones we are | |
11927 | going to output. To deal with this, we keep the relocations in | |
11928 | memory, and don't output them until the end of the link. This is | |
11929 | an unfortunate waste of memory, but I don't see a good way around | |
11930 | it. Fortunately, it only happens when performing a relocatable | |
11931 | link, which is not the common case. FIXME: If keep_memory is set | |
11932 | we could write the relocs out and then read them again; I don't | |
11933 | know how bad the memory loss will be. */ | |
11934 | ||
c72f2fb2 | 11935 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
11936 | sub->output_has_begun = FALSE; |
11937 | for (o = abfd->sections; o != NULL; o = o->next) | |
11938 | { | |
8423293d | 11939 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
11940 | { |
11941 | if (p->type == bfd_indirect_link_order | |
11942 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
11943 | == bfd_target_elf_flavour) | |
11944 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
11945 | { | |
11946 | if (! sub->output_has_begun) | |
11947 | { | |
8b127cbc | 11948 | if (! elf_link_input_bfd (&flinfo, sub)) |
c152c796 AM |
11949 | goto error_return; |
11950 | sub->output_has_begun = TRUE; | |
11951 | } | |
11952 | } | |
11953 | else if (p->type == bfd_section_reloc_link_order | |
11954 | || p->type == bfd_symbol_reloc_link_order) | |
11955 | { | |
11956 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
11957 | goto error_return; | |
11958 | } | |
11959 | else | |
11960 | { | |
11961 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
351f65ca L |
11962 | { |
11963 | if (p->type == bfd_indirect_link_order | |
11964 | && (bfd_get_flavour (sub) | |
11965 | == bfd_target_elf_flavour) | |
11966 | && (elf_elfheader (sub)->e_ident[EI_CLASS] | |
11967 | != bed->s->elfclass)) | |
11968 | { | |
11969 | const char *iclass, *oclass; | |
11970 | ||
aebf9be7 | 11971 | switch (bed->s->elfclass) |
351f65ca | 11972 | { |
aebf9be7 NC |
11973 | case ELFCLASS64: oclass = "ELFCLASS64"; break; |
11974 | case ELFCLASS32: oclass = "ELFCLASS32"; break; | |
11975 | case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; | |
11976 | default: abort (); | |
351f65ca | 11977 | } |
aebf9be7 NC |
11978 | |
11979 | switch (elf_elfheader (sub)->e_ident[EI_CLASS]) | |
351f65ca | 11980 | { |
aebf9be7 NC |
11981 | case ELFCLASS64: iclass = "ELFCLASS64"; break; |
11982 | case ELFCLASS32: iclass = "ELFCLASS32"; break; | |
11983 | case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; | |
11984 | default: abort (); | |
351f65ca L |
11985 | } |
11986 | ||
11987 | bfd_set_error (bfd_error_wrong_format); | |
4eca0228 | 11988 | _bfd_error_handler |
695344c0 | 11989 | /* xgettext:c-format */ |
351f65ca L |
11990 | (_("%B: file class %s incompatible with %s"), |
11991 | sub, iclass, oclass); | |
11992 | } | |
11993 | ||
11994 | goto error_return; | |
11995 | } | |
c152c796 AM |
11996 | } |
11997 | } | |
11998 | } | |
11999 | ||
c0f00686 L |
12000 | /* Free symbol buffer if needed. */ |
12001 | if (!info->reduce_memory_overheads) | |
12002 | { | |
c72f2fb2 | 12003 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
3fcd97f1 JJ |
12004 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
12005 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
12006 | { |
12007 | free (elf_tdata (sub)->symbuf); | |
12008 | elf_tdata (sub)->symbuf = NULL; | |
12009 | } | |
12010 | } | |
12011 | ||
c152c796 AM |
12012 | /* Output any global symbols that got converted to local in a |
12013 | version script or due to symbol visibility. We do this in a | |
12014 | separate step since ELF requires all local symbols to appear | |
12015 | prior to any global symbols. FIXME: We should only do this if | |
12016 | some global symbols were, in fact, converted to become local. | |
12017 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
12018 | eoinfo.failed = FALSE; | |
8b127cbc | 12019 | eoinfo.flinfo = &flinfo; |
c152c796 | 12020 | eoinfo.localsyms = TRUE; |
34a79995 | 12021 | eoinfo.file_sym_done = FALSE; |
7686d77d | 12022 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
12023 | if (eoinfo.failed) |
12024 | return FALSE; | |
12025 | ||
4e617b1e PB |
12026 | /* If backend needs to output some local symbols not present in the hash |
12027 | table, do it now. */ | |
8539e4e8 AM |
12028 | if (bed->elf_backend_output_arch_local_syms |
12029 | && (info->strip != strip_all || emit_relocs)) | |
4e617b1e | 12030 | { |
6e0b88f1 | 12031 | typedef int (*out_sym_func) |
4e617b1e PB |
12032 | (void *, const char *, Elf_Internal_Sym *, asection *, |
12033 | struct elf_link_hash_entry *); | |
12034 | ||
12035 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
ef10c3ac L |
12036 | (abfd, info, &flinfo, |
12037 | (out_sym_func) elf_link_output_symstrtab))) | |
4e617b1e PB |
12038 | return FALSE; |
12039 | } | |
12040 | ||
c152c796 AM |
12041 | /* That wrote out all the local symbols. Finish up the symbol table |
12042 | with the global symbols. Even if we want to strip everything we | |
12043 | can, we still need to deal with those global symbols that got | |
12044 | converted to local in a version script. */ | |
12045 | ||
12046 | /* The sh_info field records the index of the first non local symbol. */ | |
12047 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
12048 | ||
12049 | if (dynamic | |
64f52338 AM |
12050 | && htab->dynsym != NULL |
12051 | && htab->dynsym->output_section != bfd_abs_section_ptr) | |
c152c796 AM |
12052 | { |
12053 | Elf_Internal_Sym sym; | |
64f52338 | 12054 | bfd_byte *dynsym = htab->dynsym->contents; |
90ac2420 | 12055 | |
64f52338 AM |
12056 | o = htab->dynsym->output_section; |
12057 | elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1; | |
c152c796 AM |
12058 | |
12059 | /* Write out the section symbols for the output sections. */ | |
0e1862bb | 12060 | if (bfd_link_pic (info) |
64f52338 | 12061 | || htab->is_relocatable_executable) |
c152c796 AM |
12062 | { |
12063 | asection *s; | |
12064 | ||
12065 | sym.st_size = 0; | |
12066 | sym.st_name = 0; | |
12067 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
12068 | sym.st_other = 0; | |
35fc36a8 | 12069 | sym.st_target_internal = 0; |
c152c796 AM |
12070 | |
12071 | for (s = abfd->sections; s != NULL; s = s->next) | |
12072 | { | |
12073 | int indx; | |
12074 | bfd_byte *dest; | |
12075 | long dynindx; | |
12076 | ||
c152c796 | 12077 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
12078 | if (dynindx <= 0) |
12079 | continue; | |
12080 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
12081 | BFD_ASSERT (indx > 0); |
12082 | sym.st_shndx = indx; | |
c0d5a53d L |
12083 | if (! check_dynsym (abfd, &sym)) |
12084 | return FALSE; | |
c152c796 AM |
12085 | sym.st_value = s->vma; |
12086 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
12087 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
12088 | } | |
c152c796 AM |
12089 | } |
12090 | ||
12091 | /* Write out the local dynsyms. */ | |
64f52338 | 12092 | if (htab->dynlocal) |
c152c796 AM |
12093 | { |
12094 | struct elf_link_local_dynamic_entry *e; | |
64f52338 | 12095 | for (e = htab->dynlocal; e ; e = e->next) |
c152c796 AM |
12096 | { |
12097 | asection *s; | |
12098 | bfd_byte *dest; | |
12099 | ||
935bd1e0 | 12100 | /* Copy the internal symbol and turn off visibility. |
c152c796 AM |
12101 | Note that we saved a word of storage and overwrote |
12102 | the original st_name with the dynstr_index. */ | |
12103 | sym = e->isym; | |
935bd1e0 | 12104 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); |
c152c796 | 12105 | |
cb33740c AM |
12106 | s = bfd_section_from_elf_index (e->input_bfd, |
12107 | e->isym.st_shndx); | |
12108 | if (s != NULL) | |
c152c796 | 12109 | { |
c152c796 AM |
12110 | sym.st_shndx = |
12111 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
12112 | if (! check_dynsym (abfd, &sym)) |
12113 | return FALSE; | |
c152c796 AM |
12114 | sym.st_value = (s->output_section->vma |
12115 | + s->output_offset | |
12116 | + e->isym.st_value); | |
12117 | } | |
12118 | ||
c152c796 AM |
12119 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; |
12120 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
12121 | } | |
12122 | } | |
c152c796 AM |
12123 | } |
12124 | ||
12125 | /* We get the global symbols from the hash table. */ | |
12126 | eoinfo.failed = FALSE; | |
12127 | eoinfo.localsyms = FALSE; | |
8b127cbc | 12128 | eoinfo.flinfo = &flinfo; |
7686d77d | 12129 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
12130 | if (eoinfo.failed) |
12131 | return FALSE; | |
12132 | ||
12133 | /* If backend needs to output some symbols not present in the hash | |
12134 | table, do it now. */ | |
8539e4e8 AM |
12135 | if (bed->elf_backend_output_arch_syms |
12136 | && (info->strip != strip_all || emit_relocs)) | |
c152c796 | 12137 | { |
6e0b88f1 | 12138 | typedef int (*out_sym_func) |
c152c796 AM |
12139 | (void *, const char *, Elf_Internal_Sym *, asection *, |
12140 | struct elf_link_hash_entry *); | |
12141 | ||
12142 | if (! ((*bed->elf_backend_output_arch_syms) | |
ef10c3ac L |
12143 | (abfd, info, &flinfo, |
12144 | (out_sym_func) elf_link_output_symstrtab))) | |
c152c796 AM |
12145 | return FALSE; |
12146 | } | |
12147 | ||
ef10c3ac L |
12148 | /* Finalize the .strtab section. */ |
12149 | _bfd_elf_strtab_finalize (flinfo.symstrtab); | |
12150 | ||
12151 | /* Swap out the .strtab section. */ | |
12152 | if (!elf_link_swap_symbols_out (&flinfo)) | |
c152c796 AM |
12153 | return FALSE; |
12154 | ||
12155 | /* Now we know the size of the symtab section. */ | |
c152c796 AM |
12156 | if (bfd_get_symcount (abfd) > 0) |
12157 | { | |
ee3b52e9 L |
12158 | /* Finish up and write out the symbol string table (.strtab) |
12159 | section. */ | |
ad32986f | 12160 | Elf_Internal_Shdr *symstrtab_hdr = NULL; |
8539e4e8 AM |
12161 | file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; |
12162 | ||
ad32986f | 12163 | if (elf_symtab_shndx_list (abfd)) |
8539e4e8 | 12164 | { |
ad32986f | 12165 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
8539e4e8 | 12166 | |
ad32986f NC |
12167 | if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) |
12168 | { | |
12169 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
12170 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
12171 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
12172 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
12173 | symtab_shndx_hdr->sh_size = amt; | |
8539e4e8 | 12174 | |
ad32986f NC |
12175 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, |
12176 | off, TRUE); | |
12177 | ||
12178 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
12179 | || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) | |
12180 | return FALSE; | |
12181 | } | |
8539e4e8 | 12182 | } |
ee3b52e9 L |
12183 | |
12184 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
12185 | /* sh_name was set in prep_headers. */ | |
12186 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 12187 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
ee3b52e9 | 12188 | symstrtab_hdr->sh_addr = 0; |
ef10c3ac | 12189 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); |
ee3b52e9 L |
12190 | symstrtab_hdr->sh_entsize = 0; |
12191 | symstrtab_hdr->sh_link = 0; | |
12192 | symstrtab_hdr->sh_info = 0; | |
12193 | /* sh_offset is set just below. */ | |
12194 | symstrtab_hdr->sh_addralign = 1; | |
12195 | ||
12196 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, | |
12197 | off, TRUE); | |
12198 | elf_next_file_pos (abfd) = off; | |
12199 | ||
c152c796 | 12200 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 12201 | || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) |
c152c796 AM |
12202 | return FALSE; |
12203 | } | |
12204 | ||
76359541 TP |
12205 | if (info->out_implib_bfd && !elf_output_implib (abfd, info)) |
12206 | { | |
4eca0228 AM |
12207 | _bfd_error_handler (_("%B: failed to generate import library"), |
12208 | info->out_implib_bfd); | |
76359541 TP |
12209 | return FALSE; |
12210 | } | |
12211 | ||
c152c796 AM |
12212 | /* Adjust the relocs to have the correct symbol indices. */ |
12213 | for (o = abfd->sections; o != NULL; o = o->next) | |
12214 | { | |
d4730f92 | 12215 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
28dbcedc | 12216 | bfd_boolean sort; |
10bbbc1d | 12217 | |
c152c796 AM |
12218 | if ((o->flags & SEC_RELOC) == 0) |
12219 | continue; | |
12220 | ||
28dbcedc | 12221 | sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); |
bca6d0e3 | 12222 | if (esdo->rel.hdr != NULL |
10bbbc1d | 12223 | && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info)) |
bca6d0e3 AM |
12224 | return FALSE; |
12225 | if (esdo->rela.hdr != NULL | |
10bbbc1d | 12226 | && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info)) |
bca6d0e3 | 12227 | return FALSE; |
c152c796 AM |
12228 | |
12229 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
12230 | trying to swap the relocs out itself. */ | |
12231 | o->reloc_count = 0; | |
12232 | } | |
12233 | ||
12234 | if (dynamic && info->combreloc && dynobj != NULL) | |
12235 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
12236 | ||
12237 | /* If we are linking against a dynamic object, or generating a | |
12238 | shared library, finish up the dynamic linking information. */ | |
12239 | if (dynamic) | |
12240 | { | |
12241 | bfd_byte *dyncon, *dynconend; | |
12242 | ||
12243 | /* Fix up .dynamic entries. */ | |
3d4d4302 | 12244 | o = bfd_get_linker_section (dynobj, ".dynamic"); |
c152c796 AM |
12245 | BFD_ASSERT (o != NULL); |
12246 | ||
12247 | dyncon = o->contents; | |
eea6121a | 12248 | dynconend = o->contents + o->size; |
c152c796 AM |
12249 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
12250 | { | |
12251 | Elf_Internal_Dyn dyn; | |
12252 | const char *name; | |
12253 | unsigned int type; | |
64487780 AM |
12254 | bfd_size_type sh_size; |
12255 | bfd_vma sh_addr; | |
c152c796 AM |
12256 | |
12257 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12258 | ||
12259 | switch (dyn.d_tag) | |
12260 | { | |
12261 | default: | |
12262 | continue; | |
12263 | case DT_NULL: | |
12264 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
12265 | { | |
12266 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
12267 | { | |
12268 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
12269 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
12270 | default: continue; | |
12271 | } | |
12272 | dyn.d_un.d_val = relativecount; | |
12273 | relativecount = 0; | |
12274 | break; | |
12275 | } | |
12276 | continue; | |
12277 | ||
12278 | case DT_INIT: | |
12279 | name = info->init_function; | |
12280 | goto get_sym; | |
12281 | case DT_FINI: | |
12282 | name = info->fini_function; | |
12283 | get_sym: | |
12284 | { | |
12285 | struct elf_link_hash_entry *h; | |
12286 | ||
64f52338 | 12287 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
c152c796 AM |
12288 | if (h != NULL |
12289 | && (h->root.type == bfd_link_hash_defined | |
12290 | || h->root.type == bfd_link_hash_defweak)) | |
12291 | { | |
bef26483 | 12292 | dyn.d_un.d_ptr = h->root.u.def.value; |
c152c796 AM |
12293 | o = h->root.u.def.section; |
12294 | if (o->output_section != NULL) | |
bef26483 | 12295 | dyn.d_un.d_ptr += (o->output_section->vma |
c152c796 AM |
12296 | + o->output_offset); |
12297 | else | |
12298 | { | |
12299 | /* The symbol is imported from another shared | |
12300 | library and does not apply to this one. */ | |
bef26483 | 12301 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
12302 | } |
12303 | break; | |
12304 | } | |
12305 | } | |
12306 | continue; | |
12307 | ||
12308 | case DT_PREINIT_ARRAYSZ: | |
12309 | name = ".preinit_array"; | |
4ade44b7 | 12310 | goto get_out_size; |
c152c796 AM |
12311 | case DT_INIT_ARRAYSZ: |
12312 | name = ".init_array"; | |
4ade44b7 | 12313 | goto get_out_size; |
c152c796 AM |
12314 | case DT_FINI_ARRAYSZ: |
12315 | name = ".fini_array"; | |
4ade44b7 | 12316 | get_out_size: |
c152c796 AM |
12317 | o = bfd_get_section_by_name (abfd, name); |
12318 | if (o == NULL) | |
12319 | { | |
4eca0228 | 12320 | _bfd_error_handler |
4ade44b7 | 12321 | (_("could not find section %s"), name); |
c152c796 AM |
12322 | goto error_return; |
12323 | } | |
eea6121a | 12324 | if (o->size == 0) |
4eca0228 | 12325 | _bfd_error_handler |
c152c796 | 12326 | (_("warning: %s section has zero size"), name); |
eea6121a | 12327 | dyn.d_un.d_val = o->size; |
c152c796 AM |
12328 | break; |
12329 | ||
12330 | case DT_PREINIT_ARRAY: | |
12331 | name = ".preinit_array"; | |
4ade44b7 | 12332 | goto get_out_vma; |
c152c796 AM |
12333 | case DT_INIT_ARRAY: |
12334 | name = ".init_array"; | |
4ade44b7 | 12335 | goto get_out_vma; |
c152c796 AM |
12336 | case DT_FINI_ARRAY: |
12337 | name = ".fini_array"; | |
4ade44b7 AM |
12338 | get_out_vma: |
12339 | o = bfd_get_section_by_name (abfd, name); | |
12340 | goto do_vma; | |
c152c796 AM |
12341 | |
12342 | case DT_HASH: | |
12343 | name = ".hash"; | |
12344 | goto get_vma; | |
fdc90cb4 JJ |
12345 | case DT_GNU_HASH: |
12346 | name = ".gnu.hash"; | |
12347 | goto get_vma; | |
c152c796 AM |
12348 | case DT_STRTAB: |
12349 | name = ".dynstr"; | |
12350 | goto get_vma; | |
12351 | case DT_SYMTAB: | |
12352 | name = ".dynsym"; | |
12353 | goto get_vma; | |
12354 | case DT_VERDEF: | |
12355 | name = ".gnu.version_d"; | |
12356 | goto get_vma; | |
12357 | case DT_VERNEED: | |
12358 | name = ".gnu.version_r"; | |
12359 | goto get_vma; | |
12360 | case DT_VERSYM: | |
12361 | name = ".gnu.version"; | |
12362 | get_vma: | |
4ade44b7 AM |
12363 | o = bfd_get_linker_section (dynobj, name); |
12364 | do_vma: | |
b3293efa | 12365 | if (o == NULL || bfd_is_abs_section (o->output_section)) |
c152c796 | 12366 | { |
4eca0228 | 12367 | _bfd_error_handler |
4ade44b7 | 12368 | (_("could not find section %s"), name); |
c152c796 AM |
12369 | goto error_return; |
12370 | } | |
894891db NC |
12371 | if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) |
12372 | { | |
4eca0228 | 12373 | _bfd_error_handler |
894891db NC |
12374 | (_("warning: section '%s' is being made into a note"), name); |
12375 | bfd_set_error (bfd_error_nonrepresentable_section); | |
12376 | goto error_return; | |
12377 | } | |
4ade44b7 | 12378 | dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; |
c152c796 AM |
12379 | break; |
12380 | ||
12381 | case DT_REL: | |
12382 | case DT_RELA: | |
12383 | case DT_RELSZ: | |
12384 | case DT_RELASZ: | |
12385 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
12386 | type = SHT_REL; | |
12387 | else | |
12388 | type = SHT_RELA; | |
64487780 AM |
12389 | sh_size = 0; |
12390 | sh_addr = 0; | |
c152c796 AM |
12391 | for (i = 1; i < elf_numsections (abfd); i++) |
12392 | { | |
12393 | Elf_Internal_Shdr *hdr; | |
12394 | ||
12395 | hdr = elf_elfsections (abfd)[i]; | |
12396 | if (hdr->sh_type == type | |
12397 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
12398 | { | |
64487780 AM |
12399 | sh_size += hdr->sh_size; |
12400 | if (sh_addr == 0 | |
12401 | || sh_addr > hdr->sh_addr) | |
12402 | sh_addr = hdr->sh_addr; | |
c152c796 AM |
12403 | } |
12404 | } | |
64487780 | 12405 | |
64f52338 AM |
12406 | if (bed->dtrel_excludes_plt && htab->srelplt != NULL) |
12407 | { | |
12408 | /* Don't count procedure linkage table relocs in the | |
12409 | overall reloc count. */ | |
64487780 AM |
12410 | sh_size -= htab->srelplt->size; |
12411 | if (sh_size == 0) | |
12412 | /* If the size is zero, make the address zero too. | |
12413 | This is to avoid a glibc bug. If the backend | |
12414 | emits DT_RELA/DT_RELASZ even when DT_RELASZ is | |
12415 | zero, then we'll put DT_RELA at the end of | |
12416 | DT_JMPREL. glibc will interpret the end of | |
12417 | DT_RELA matching the end of DT_JMPREL as the | |
12418 | case where DT_RELA includes DT_JMPREL, and for | |
12419 | LD_BIND_NOW will decide that processing DT_RELA | |
12420 | will process the PLT relocs too. Net result: | |
12421 | No PLT relocs applied. */ | |
12422 | sh_addr = 0; | |
12423 | ||
64f52338 AM |
12424 | /* If .rela.plt is the first .rela section, exclude |
12425 | it from DT_RELA. */ | |
64487780 AM |
12426 | else if (sh_addr == (htab->srelplt->output_section->vma |
12427 | + htab->srelplt->output_offset)) | |
12428 | sh_addr += htab->srelplt->size; | |
64f52338 | 12429 | } |
64487780 AM |
12430 | |
12431 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
12432 | dyn.d_un.d_val = sh_size; | |
12433 | else | |
12434 | dyn.d_un.d_ptr = sh_addr; | |
c152c796 AM |
12435 | break; |
12436 | } | |
12437 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
12438 | } | |
12439 | } | |
12440 | ||
12441 | /* If we have created any dynamic sections, then output them. */ | |
12442 | if (dynobj != NULL) | |
12443 | { | |
12444 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
12445 | goto error_return; | |
12446 | ||
943284cc | 12447 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
0e1862bb | 12448 | if (((info->warn_shared_textrel && bfd_link_pic (info)) |
be7b303d | 12449 | || info->error_textrel) |
3d4d4302 | 12450 | && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) |
943284cc DJ |
12451 | { |
12452 | bfd_byte *dyncon, *dynconend; | |
12453 | ||
943284cc DJ |
12454 | dyncon = o->contents; |
12455 | dynconend = o->contents + o->size; | |
12456 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
12457 | { | |
12458 | Elf_Internal_Dyn dyn; | |
12459 | ||
12460 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12461 | ||
12462 | if (dyn.d_tag == DT_TEXTREL) | |
12463 | { | |
c192a133 AM |
12464 | if (info->error_textrel) |
12465 | info->callbacks->einfo | |
12466 | (_("%P%X: read-only segment has dynamic relocations.\n")); | |
12467 | else | |
12468 | info->callbacks->einfo | |
12469 | (_("%P: warning: creating a DT_TEXTREL in a shared object.\n")); | |
943284cc DJ |
12470 | break; |
12471 | } | |
12472 | } | |
12473 | } | |
12474 | ||
c152c796 AM |
12475 | for (o = dynobj->sections; o != NULL; o = o->next) |
12476 | { | |
12477 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 12478 | || o->size == 0 |
c152c796 AM |
12479 | || o->output_section == bfd_abs_section_ptr) |
12480 | continue; | |
12481 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
12482 | { | |
12483 | /* At this point, we are only interested in sections | |
12484 | created by _bfd_elf_link_create_dynamic_sections. */ | |
12485 | continue; | |
12486 | } | |
64f52338 | 12487 | if (htab->stab_info.stabstr == o) |
3722b82f | 12488 | continue; |
64f52338 | 12489 | if (htab->eh_info.hdr_sec == o) |
eea6121a | 12490 | continue; |
3d4d4302 | 12491 | if (strcmp (o->name, ".dynstr") != 0) |
c152c796 AM |
12492 | { |
12493 | if (! bfd_set_section_contents (abfd, o->output_section, | |
12494 | o->contents, | |
37b01f6a DG |
12495 | (file_ptr) o->output_offset |
12496 | * bfd_octets_per_byte (abfd), | |
eea6121a | 12497 | o->size)) |
c152c796 AM |
12498 | goto error_return; |
12499 | } | |
12500 | else | |
12501 | { | |
12502 | /* The contents of the .dynstr section are actually in a | |
12503 | stringtab. */ | |
8539e4e8 AM |
12504 | file_ptr off; |
12505 | ||
c152c796 AM |
12506 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; |
12507 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
64f52338 | 12508 | || !_bfd_elf_strtab_emit (abfd, htab->dynstr)) |
c152c796 AM |
12509 | goto error_return; |
12510 | } | |
12511 | } | |
12512 | } | |
12513 | ||
7bdf4127 | 12514 | if (!info->resolve_section_groups) |
c152c796 AM |
12515 | { |
12516 | bfd_boolean failed = FALSE; | |
12517 | ||
7bdf4127 | 12518 | BFD_ASSERT (bfd_link_relocatable (info)); |
c152c796 AM |
12519 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
12520 | if (failed) | |
12521 | goto error_return; | |
12522 | } | |
12523 | ||
12524 | /* If we have optimized stabs strings, output them. */ | |
64f52338 | 12525 | if (htab->stab_info.stabstr != NULL) |
c152c796 | 12526 | { |
64f52338 | 12527 | if (!_bfd_write_stab_strings (abfd, &htab->stab_info)) |
c152c796 AM |
12528 | goto error_return; |
12529 | } | |
12530 | ||
9f7c3e5e AM |
12531 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) |
12532 | goto error_return; | |
c152c796 | 12533 | |
9f7c3e5e | 12534 | elf_final_link_free (abfd, &flinfo); |
c152c796 | 12535 | |
12bd6957 | 12536 | elf_linker (abfd) = TRUE; |
c152c796 | 12537 | |
104d59d1 JM |
12538 | if (attr_section) |
12539 | { | |
a50b1753 | 12540 | bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); |
104d59d1 | 12541 | if (contents == NULL) |
d0f16d5e | 12542 | return FALSE; /* Bail out and fail. */ |
104d59d1 JM |
12543 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
12544 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
12545 | free (contents); | |
12546 | } | |
12547 | ||
c152c796 AM |
12548 | return TRUE; |
12549 | ||
12550 | error_return: | |
9f7c3e5e | 12551 | elf_final_link_free (abfd, &flinfo); |
c152c796 AM |
12552 | return FALSE; |
12553 | } | |
12554 | \f | |
5241d853 RS |
12555 | /* Initialize COOKIE for input bfd ABFD. */ |
12556 | ||
12557 | static bfd_boolean | |
12558 | init_reloc_cookie (struct elf_reloc_cookie *cookie, | |
12559 | struct bfd_link_info *info, bfd *abfd) | |
12560 | { | |
12561 | Elf_Internal_Shdr *symtab_hdr; | |
12562 | const struct elf_backend_data *bed; | |
12563 | ||
12564 | bed = get_elf_backend_data (abfd); | |
12565 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12566 | ||
12567 | cookie->abfd = abfd; | |
12568 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
12569 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
12570 | if (cookie->bad_symtab) | |
12571 | { | |
12572 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
12573 | cookie->extsymoff = 0; | |
12574 | } | |
12575 | else | |
12576 | { | |
12577 | cookie->locsymcount = symtab_hdr->sh_info; | |
12578 | cookie->extsymoff = symtab_hdr->sh_info; | |
12579 | } | |
12580 | ||
12581 | if (bed->s->arch_size == 32) | |
12582 | cookie->r_sym_shift = 8; | |
12583 | else | |
12584 | cookie->r_sym_shift = 32; | |
12585 | ||
12586 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
12587 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
12588 | { | |
12589 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
12590 | cookie->locsymcount, 0, | |
12591 | NULL, NULL, NULL); | |
12592 | if (cookie->locsyms == NULL) | |
12593 | { | |
12594 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
12595 | return FALSE; | |
12596 | } | |
12597 | if (info->keep_memory) | |
12598 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
12599 | } | |
12600 | return TRUE; | |
12601 | } | |
12602 | ||
12603 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
12604 | ||
12605 | static void | |
12606 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
12607 | { | |
12608 | Elf_Internal_Shdr *symtab_hdr; | |
12609 | ||
12610 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12611 | if (cookie->locsyms != NULL | |
12612 | && symtab_hdr->contents != (unsigned char *) cookie->locsyms) | |
12613 | free (cookie->locsyms); | |
12614 | } | |
12615 | ||
12616 | /* Initialize the relocation information in COOKIE for input section SEC | |
12617 | of input bfd ABFD. */ | |
12618 | ||
12619 | static bfd_boolean | |
12620 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12621 | struct bfd_link_info *info, bfd *abfd, | |
12622 | asection *sec) | |
12623 | { | |
5241d853 RS |
12624 | if (sec->reloc_count == 0) |
12625 | { | |
12626 | cookie->rels = NULL; | |
12627 | cookie->relend = NULL; | |
12628 | } | |
12629 | else | |
12630 | { | |
5241d853 RS |
12631 | cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, |
12632 | info->keep_memory); | |
12633 | if (cookie->rels == NULL) | |
12634 | return FALSE; | |
12635 | cookie->rel = cookie->rels; | |
056bafd4 | 12636 | cookie->relend = cookie->rels + sec->reloc_count; |
5241d853 RS |
12637 | } |
12638 | cookie->rel = cookie->rels; | |
12639 | return TRUE; | |
12640 | } | |
12641 | ||
12642 | /* Free the memory allocated by init_reloc_cookie_rels, | |
12643 | if appropriate. */ | |
12644 | ||
12645 | static void | |
12646 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12647 | asection *sec) | |
12648 | { | |
12649 | if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) | |
12650 | free (cookie->rels); | |
12651 | } | |
12652 | ||
12653 | /* Initialize the whole of COOKIE for input section SEC. */ | |
12654 | ||
12655 | static bfd_boolean | |
12656 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12657 | struct bfd_link_info *info, | |
12658 | asection *sec) | |
12659 | { | |
12660 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
12661 | goto error1; | |
12662 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
12663 | goto error2; | |
12664 | return TRUE; | |
12665 | ||
12666 | error2: | |
12667 | fini_reloc_cookie (cookie, sec->owner); | |
12668 | error1: | |
12669 | return FALSE; | |
12670 | } | |
12671 | ||
12672 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
12673 | if appropriate. */ | |
12674 | ||
12675 | static void | |
12676 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12677 | asection *sec) | |
12678 | { | |
12679 | fini_reloc_cookie_rels (cookie, sec); | |
12680 | fini_reloc_cookie (cookie, sec->owner); | |
12681 | } | |
12682 | \f | |
c152c796 AM |
12683 | /* Garbage collect unused sections. */ |
12684 | ||
07adf181 AM |
12685 | /* Default gc_mark_hook. */ |
12686 | ||
12687 | asection * | |
12688 | _bfd_elf_gc_mark_hook (asection *sec, | |
12689 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12690 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
12691 | struct elf_link_hash_entry *h, | |
12692 | Elf_Internal_Sym *sym) | |
12693 | { | |
12694 | if (h != NULL) | |
12695 | { | |
12696 | switch (h->root.type) | |
12697 | { | |
12698 | case bfd_link_hash_defined: | |
12699 | case bfd_link_hash_defweak: | |
12700 | return h->root.u.def.section; | |
12701 | ||
12702 | case bfd_link_hash_common: | |
12703 | return h->root.u.c.p->section; | |
12704 | ||
12705 | default: | |
12706 | break; | |
12707 | } | |
12708 | } | |
12709 | else | |
12710 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
12711 | ||
12712 | return NULL; | |
12713 | } | |
12714 | ||
b7c871ed L |
12715 | /* Return the global debug definition section. */ |
12716 | ||
12717 | static asection * | |
12718 | elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED, | |
12719 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12720 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
12721 | struct elf_link_hash_entry *h, | |
12722 | Elf_Internal_Sym *sym ATTRIBUTE_UNUSED) | |
12723 | { | |
12724 | if (h != NULL | |
12725 | && (h->root.type == bfd_link_hash_defined | |
12726 | || h->root.type == bfd_link_hash_defweak) | |
12727 | && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0) | |
12728 | return h->root.u.def.section; | |
12729 | ||
12730 | return NULL; | |
12731 | } | |
12732 | ||
5241d853 RS |
12733 | /* COOKIE->rel describes a relocation against section SEC, which is |
12734 | a section we've decided to keep. Return the section that contains | |
12735 | the relocation symbol, or NULL if no section contains it. */ | |
12736 | ||
12737 | asection * | |
12738 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
12739 | elf_gc_mark_hook_fn gc_mark_hook, | |
1cce69b9 AM |
12740 | struct elf_reloc_cookie *cookie, |
12741 | bfd_boolean *start_stop) | |
5241d853 RS |
12742 | { |
12743 | unsigned long r_symndx; | |
12744 | struct elf_link_hash_entry *h; | |
12745 | ||
12746 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
cf35638d | 12747 | if (r_symndx == STN_UNDEF) |
5241d853 RS |
12748 | return NULL; |
12749 | ||
12750 | if (r_symndx >= cookie->locsymcount | |
12751 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
12752 | { | |
12753 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
263ddf68 L |
12754 | if (h == NULL) |
12755 | { | |
12756 | info->callbacks->einfo (_("%F%P: corrupt input: %B\n"), | |
12757 | sec->owner); | |
12758 | return NULL; | |
12759 | } | |
5241d853 RS |
12760 | while (h->root.type == bfd_link_hash_indirect |
12761 | || h->root.type == bfd_link_hash_warning) | |
12762 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1d5316ab | 12763 | h->mark = 1; |
4e6b54a6 AM |
12764 | /* If this symbol is weak and there is a non-weak definition, we |
12765 | keep the non-weak definition because many backends put | |
12766 | dynamic reloc info on the non-weak definition for code | |
12767 | handling copy relocs. */ | |
60d67dc8 AM |
12768 | if (h->is_weakalias) |
12769 | weakdef (h)->mark = 1; | |
1cce69b9 | 12770 | |
a6a4679f | 12771 | if (start_stop != NULL) |
1cce69b9 | 12772 | { |
7dba9362 AM |
12773 | /* To work around a glibc bug, mark XXX input sections |
12774 | when there is a reference to __start_XXX or __stop_XXX | |
12775 | symbols. */ | |
cbd0eecf | 12776 | if (h->start_stop) |
1cce69b9 | 12777 | { |
cbd0eecf | 12778 | asection *s = h->u2.start_stop_section; |
a6a4679f AM |
12779 | *start_stop = !s->gc_mark; |
12780 | return s; | |
1cce69b9 AM |
12781 | } |
12782 | } | |
12783 | ||
5241d853 RS |
12784 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); |
12785 | } | |
12786 | ||
12787 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
12788 | &cookie->locsyms[r_symndx]); | |
12789 | } | |
12790 | ||
12791 | /* COOKIE->rel describes a relocation against section SEC, which is | |
12792 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 12793 | the relocation symbol. */ |
5241d853 RS |
12794 | |
12795 | bfd_boolean | |
12796 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, | |
12797 | asection *sec, | |
12798 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 12799 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
12800 | { |
12801 | asection *rsec; | |
1cce69b9 | 12802 | bfd_boolean start_stop = FALSE; |
5241d853 | 12803 | |
1cce69b9 AM |
12804 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); |
12805 | while (rsec != NULL) | |
5241d853 | 12806 | { |
1cce69b9 AM |
12807 | if (!rsec->gc_mark) |
12808 | { | |
12809 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour | |
12810 | || (rsec->owner->flags & DYNAMIC) != 0) | |
12811 | rsec->gc_mark = 1; | |
12812 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) | |
12813 | return FALSE; | |
12814 | } | |
12815 | if (!start_stop) | |
12816 | break; | |
199af150 | 12817 | rsec = bfd_get_next_section_by_name (rsec->owner, rsec); |
5241d853 RS |
12818 | } |
12819 | return TRUE; | |
12820 | } | |
12821 | ||
07adf181 AM |
12822 | /* The mark phase of garbage collection. For a given section, mark |
12823 | it and any sections in this section's group, and all the sections | |
12824 | which define symbols to which it refers. */ | |
12825 | ||
ccfa59ea AM |
12826 | bfd_boolean |
12827 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
12828 | asection *sec, | |
6a5bb875 | 12829 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
12830 | { |
12831 | bfd_boolean ret; | |
9d0a14d3 | 12832 | asection *group_sec, *eh_frame; |
c152c796 AM |
12833 | |
12834 | sec->gc_mark = 1; | |
12835 | ||
12836 | /* Mark all the sections in the group. */ | |
12837 | group_sec = elf_section_data (sec)->next_in_group; | |
12838 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 12839 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
12840 | return FALSE; |
12841 | ||
12842 | /* Look through the section relocs. */ | |
12843 | ret = TRUE; | |
9d0a14d3 RS |
12844 | eh_frame = elf_eh_frame_section (sec->owner); |
12845 | if ((sec->flags & SEC_RELOC) != 0 | |
12846 | && sec->reloc_count > 0 | |
12847 | && sec != eh_frame) | |
c152c796 | 12848 | { |
5241d853 | 12849 | struct elf_reloc_cookie cookie; |
c152c796 | 12850 | |
5241d853 RS |
12851 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
12852 | ret = FALSE; | |
c152c796 | 12853 | else |
c152c796 | 12854 | { |
5241d853 | 12855 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 12856 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 RS |
12857 | { |
12858 | ret = FALSE; | |
12859 | break; | |
12860 | } | |
12861 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
12862 | } |
12863 | } | |
9d0a14d3 RS |
12864 | |
12865 | if (ret && eh_frame && elf_fde_list (sec)) | |
12866 | { | |
12867 | struct elf_reloc_cookie cookie; | |
12868 | ||
12869 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
12870 | ret = FALSE; | |
12871 | else | |
12872 | { | |
12873 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
12874 | gc_mark_hook, &cookie)) | |
12875 | ret = FALSE; | |
12876 | fini_reloc_cookie_for_section (&cookie, eh_frame); | |
12877 | } | |
12878 | } | |
12879 | ||
2f0c68f2 CM |
12880 | eh_frame = elf_section_eh_frame_entry (sec); |
12881 | if (ret && eh_frame && !eh_frame->gc_mark) | |
12882 | if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) | |
12883 | ret = FALSE; | |
12884 | ||
c152c796 AM |
12885 | return ret; |
12886 | } | |
12887 | ||
3c758495 TG |
12888 | /* Scan and mark sections in a special or debug section group. */ |
12889 | ||
12890 | static void | |
12891 | _bfd_elf_gc_mark_debug_special_section_group (asection *grp) | |
12892 | { | |
12893 | /* Point to first section of section group. */ | |
12894 | asection *ssec; | |
12895 | /* Used to iterate the section group. */ | |
12896 | asection *msec; | |
12897 | ||
12898 | bfd_boolean is_special_grp = TRUE; | |
12899 | bfd_boolean is_debug_grp = TRUE; | |
12900 | ||
12901 | /* First scan to see if group contains any section other than debug | |
12902 | and special section. */ | |
12903 | ssec = msec = elf_next_in_group (grp); | |
12904 | do | |
12905 | { | |
12906 | if ((msec->flags & SEC_DEBUGGING) == 0) | |
12907 | is_debug_grp = FALSE; | |
12908 | ||
12909 | if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) | |
12910 | is_special_grp = FALSE; | |
12911 | ||
12912 | msec = elf_next_in_group (msec); | |
12913 | } | |
12914 | while (msec != ssec); | |
12915 | ||
12916 | /* If this is a pure debug section group or pure special section group, | |
12917 | keep all sections in this group. */ | |
12918 | if (is_debug_grp || is_special_grp) | |
12919 | { | |
12920 | do | |
12921 | { | |
12922 | msec->gc_mark = 1; | |
12923 | msec = elf_next_in_group (msec); | |
12924 | } | |
12925 | while (msec != ssec); | |
12926 | } | |
12927 | } | |
12928 | ||
7f6ab9f8 AM |
12929 | /* Keep debug and special sections. */ |
12930 | ||
12931 | bfd_boolean | |
12932 | _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, | |
12933 | elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) | |
12934 | { | |
12935 | bfd *ibfd; | |
12936 | ||
c72f2fb2 | 12937 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
7f6ab9f8 AM |
12938 | { |
12939 | asection *isec; | |
12940 | bfd_boolean some_kept; | |
b40bf0a2 | 12941 | bfd_boolean debug_frag_seen; |
b7c871ed | 12942 | bfd_boolean has_kept_debug_info; |
7f6ab9f8 AM |
12943 | |
12944 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
12945 | continue; | |
57963c05 AM |
12946 | isec = ibfd->sections; |
12947 | if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
12948 | continue; | |
7f6ab9f8 | 12949 | |
b40bf0a2 NC |
12950 | /* Ensure all linker created sections are kept, |
12951 | see if any other section is already marked, | |
12952 | and note if we have any fragmented debug sections. */ | |
b7c871ed | 12953 | debug_frag_seen = some_kept = has_kept_debug_info = FALSE; |
7f6ab9f8 AM |
12954 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
12955 | { | |
12956 | if ((isec->flags & SEC_LINKER_CREATED) != 0) | |
12957 | isec->gc_mark = 1; | |
eb026f09 AM |
12958 | else if (isec->gc_mark |
12959 | && (isec->flags & SEC_ALLOC) != 0 | |
12960 | && elf_section_type (isec) != SHT_NOTE) | |
7f6ab9f8 | 12961 | some_kept = TRUE; |
b40bf0a2 | 12962 | |
535b785f | 12963 | if (!debug_frag_seen |
b40bf0a2 NC |
12964 | && (isec->flags & SEC_DEBUGGING) |
12965 | && CONST_STRNEQ (isec->name, ".debug_line.")) | |
12966 | debug_frag_seen = TRUE; | |
7f6ab9f8 AM |
12967 | } |
12968 | ||
eb026f09 AM |
12969 | /* If no non-note alloc section in this file will be kept, then |
12970 | we can toss out the debug and special sections. */ | |
7f6ab9f8 AM |
12971 | if (!some_kept) |
12972 | continue; | |
12973 | ||
12974 | /* Keep debug and special sections like .comment when they are | |
3c758495 TG |
12975 | not part of a group. Also keep section groups that contain |
12976 | just debug sections or special sections. */ | |
7f6ab9f8 | 12977 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
3c758495 TG |
12978 | { |
12979 | if ((isec->flags & SEC_GROUP) != 0) | |
12980 | _bfd_elf_gc_mark_debug_special_section_group (isec); | |
12981 | else if (((isec->flags & SEC_DEBUGGING) != 0 | |
12982 | || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) | |
12983 | && elf_next_in_group (isec) == NULL) | |
12984 | isec->gc_mark = 1; | |
b7c871ed L |
12985 | if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0) |
12986 | has_kept_debug_info = TRUE; | |
3c758495 | 12987 | } |
b40bf0a2 | 12988 | |
b40bf0a2 NC |
12989 | /* Look for CODE sections which are going to be discarded, |
12990 | and find and discard any fragmented debug sections which | |
12991 | are associated with that code section. */ | |
b7c871ed L |
12992 | if (debug_frag_seen) |
12993 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
12994 | if ((isec->flags & SEC_CODE) != 0 | |
12995 | && isec->gc_mark == 0) | |
12996 | { | |
12997 | unsigned int ilen; | |
12998 | asection *dsec; | |
b40bf0a2 | 12999 | |
b7c871ed | 13000 | ilen = strlen (isec->name); |
b40bf0a2 | 13001 | |
b7c871ed L |
13002 | /* Association is determined by the name of the debug |
13003 | section containing the name of the code section as | |
13004 | a suffix. For example .debug_line.text.foo is a | |
13005 | debug section associated with .text.foo. */ | |
13006 | for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) | |
13007 | { | |
13008 | unsigned int dlen; | |
b40bf0a2 | 13009 | |
b7c871ed L |
13010 | if (dsec->gc_mark == 0 |
13011 | || (dsec->flags & SEC_DEBUGGING) == 0) | |
13012 | continue; | |
b40bf0a2 | 13013 | |
b7c871ed | 13014 | dlen = strlen (dsec->name); |
b40bf0a2 | 13015 | |
b7c871ed L |
13016 | if (dlen > ilen |
13017 | && strncmp (dsec->name + (dlen - ilen), | |
13018 | isec->name, ilen) == 0) | |
b40bf0a2 | 13019 | dsec->gc_mark = 0; |
b7c871ed | 13020 | } |
b40bf0a2 | 13021 | } |
b7c871ed L |
13022 | |
13023 | /* Mark debug sections referenced by kept debug sections. */ | |
13024 | if (has_kept_debug_info) | |
13025 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
13026 | if (isec->gc_mark | |
13027 | && (isec->flags & SEC_DEBUGGING) != 0) | |
13028 | if (!_bfd_elf_gc_mark (info, isec, | |
13029 | elf_gc_mark_debug_section)) | |
13030 | return FALSE; | |
7f6ab9f8 AM |
13031 | } |
13032 | return TRUE; | |
13033 | } | |
13034 | ||
c152c796 | 13035 | static bfd_boolean |
ccabcbe5 | 13036 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
13037 | { |
13038 | bfd *sub; | |
ccabcbe5 | 13039 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 13040 | |
c72f2fb2 | 13041 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13042 | { |
13043 | asection *o; | |
13044 | ||
b19a8f85 | 13045 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 13046 | || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info)) |
b19a8f85 | 13047 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 | 13048 | continue; |
57963c05 AM |
13049 | o = sub->sections; |
13050 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13051 | continue; | |
c152c796 AM |
13052 | |
13053 | for (o = sub->sections; o != NULL; o = o->next) | |
13054 | { | |
a33dafc3 L |
13055 | /* When any section in a section group is kept, we keep all |
13056 | sections in the section group. If the first member of | |
13057 | the section group is excluded, we will also exclude the | |
13058 | group section. */ | |
13059 | if (o->flags & SEC_GROUP) | |
13060 | { | |
13061 | asection *first = elf_next_in_group (o); | |
13062 | o->gc_mark = first->gc_mark; | |
13063 | } | |
c152c796 | 13064 | |
1e7eae0d | 13065 | if (o->gc_mark) |
c152c796 AM |
13066 | continue; |
13067 | ||
13068 | /* Skip sweeping sections already excluded. */ | |
13069 | if (o->flags & SEC_EXCLUDE) | |
13070 | continue; | |
13071 | ||
13072 | /* Since this is early in the link process, it is simple | |
13073 | to remove a section from the output. */ | |
13074 | o->flags |= SEC_EXCLUDE; | |
13075 | ||
c55fe096 | 13076 | if (info->print_gc_sections && o->size != 0) |
695344c0 | 13077 | /* xgettext:c-format */ |
c08bb8dd AM |
13078 | _bfd_error_handler (_("Removing unused section '%A' in file '%B'"), |
13079 | o, sub); | |
c152c796 AM |
13080 | } |
13081 | } | |
13082 | ||
c152c796 AM |
13083 | return TRUE; |
13084 | } | |
13085 | ||
13086 | /* Propagate collected vtable information. This is called through | |
13087 | elf_link_hash_traverse. */ | |
13088 | ||
13089 | static bfd_boolean | |
13090 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
13091 | { | |
c152c796 | 13092 | /* Those that are not vtables. */ |
cbd0eecf L |
13093 | if (h->start_stop |
13094 | || h->u2.vtable == NULL | |
13095 | || h->u2.vtable->parent == NULL) | |
c152c796 AM |
13096 | return TRUE; |
13097 | ||
13098 | /* Those vtables that do not have parents, we cannot merge. */ | |
cbd0eecf | 13099 | if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
13100 | return TRUE; |
13101 | ||
13102 | /* If we've already been done, exit. */ | |
cbd0eecf | 13103 | if (h->u2.vtable->used && h->u2.vtable->used[-1]) |
c152c796 AM |
13104 | return TRUE; |
13105 | ||
13106 | /* Make sure the parent's table is up to date. */ | |
cbd0eecf | 13107 | elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp); |
c152c796 | 13108 | |
cbd0eecf | 13109 | if (h->u2.vtable->used == NULL) |
c152c796 AM |
13110 | { |
13111 | /* None of this table's entries were referenced. Re-use the | |
13112 | parent's table. */ | |
cbd0eecf L |
13113 | h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used; |
13114 | h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size; | |
c152c796 AM |
13115 | } |
13116 | else | |
13117 | { | |
13118 | size_t n; | |
13119 | bfd_boolean *cu, *pu; | |
13120 | ||
13121 | /* Or the parent's entries into ours. */ | |
cbd0eecf | 13122 | cu = h->u2.vtable->used; |
c152c796 | 13123 | cu[-1] = TRUE; |
cbd0eecf | 13124 | pu = h->u2.vtable->parent->u2.vtable->used; |
c152c796 AM |
13125 | if (pu != NULL) |
13126 | { | |
13127 | const struct elf_backend_data *bed; | |
13128 | unsigned int log_file_align; | |
13129 | ||
13130 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
13131 | log_file_align = bed->s->log_file_align; | |
cbd0eecf | 13132 | n = h->u2.vtable->parent->u2.vtable->size >> log_file_align; |
c152c796 AM |
13133 | while (n--) |
13134 | { | |
13135 | if (*pu) | |
13136 | *cu = TRUE; | |
13137 | pu++; | |
13138 | cu++; | |
13139 | } | |
13140 | } | |
13141 | } | |
13142 | ||
13143 | return TRUE; | |
13144 | } | |
13145 | ||
13146 | static bfd_boolean | |
13147 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
13148 | { | |
13149 | asection *sec; | |
13150 | bfd_vma hstart, hend; | |
13151 | Elf_Internal_Rela *relstart, *relend, *rel; | |
13152 | const struct elf_backend_data *bed; | |
13153 | unsigned int log_file_align; | |
13154 | ||
c152c796 AM |
13155 | /* Take care of both those symbols that do not describe vtables as |
13156 | well as those that are not loaded. */ | |
cbd0eecf L |
13157 | if (h->start_stop |
13158 | || h->u2.vtable == NULL | |
13159 | || h->u2.vtable->parent == NULL) | |
c152c796 AM |
13160 | return TRUE; |
13161 | ||
13162 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
13163 | || h->root.type == bfd_link_hash_defweak); | |
13164 | ||
13165 | sec = h->root.u.def.section; | |
13166 | hstart = h->root.u.def.value; | |
13167 | hend = hstart + h->size; | |
13168 | ||
13169 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
13170 | if (!relstart) | |
13171 | return *(bfd_boolean *) okp = FALSE; | |
13172 | bed = get_elf_backend_data (sec->owner); | |
13173 | log_file_align = bed->s->log_file_align; | |
13174 | ||
056bafd4 | 13175 | relend = relstart + sec->reloc_count; |
c152c796 AM |
13176 | |
13177 | for (rel = relstart; rel < relend; ++rel) | |
13178 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
13179 | { | |
13180 | /* If the entry is in use, do nothing. */ | |
cbd0eecf L |
13181 | if (h->u2.vtable->used |
13182 | && (rel->r_offset - hstart) < h->u2.vtable->size) | |
c152c796 AM |
13183 | { |
13184 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
cbd0eecf | 13185 | if (h->u2.vtable->used[entry]) |
c152c796 AM |
13186 | continue; |
13187 | } | |
13188 | /* Otherwise, kill it. */ | |
13189 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
13190 | } | |
13191 | ||
13192 | return TRUE; | |
13193 | } | |
13194 | ||
87538722 AM |
13195 | /* Mark sections containing dynamically referenced symbols. When |
13196 | building shared libraries, we must assume that any visible symbol is | |
13197 | referenced. */ | |
715df9b8 | 13198 | |
64d03ab5 AM |
13199 | bfd_boolean |
13200 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 13201 | { |
87538722 | 13202 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
d6f6f455 | 13203 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
87538722 | 13204 | |
715df9b8 EB |
13205 | if ((h->root.type == bfd_link_hash_defined |
13206 | || h->root.type == bfd_link_hash_defweak) | |
87538722 | 13207 | && (h->ref_dynamic |
c4621b33 | 13208 | || ((h->def_regular || ELF_COMMON_DEF_P (h)) |
87538722 | 13209 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL |
fd91d419 | 13210 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN |
0e1862bb | 13211 | && (!bfd_link_executable (info) |
22185505 | 13212 | || info->gc_keep_exported |
b407645f AM |
13213 | || info->export_dynamic |
13214 | || (h->dynamic | |
13215 | && d != NULL | |
13216 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
422f1182 | 13217 | && (h->versioned >= versioned |
54e8959c L |
13218 | || !bfd_hide_sym_by_version (info->version_info, |
13219 | h->root.root.string))))) | |
715df9b8 EB |
13220 | h->root.u.def.section->flags |= SEC_KEEP; |
13221 | ||
13222 | return TRUE; | |
13223 | } | |
3b36f7e6 | 13224 | |
74f0fb50 AM |
13225 | /* Keep all sections containing symbols undefined on the command-line, |
13226 | and the section containing the entry symbol. */ | |
13227 | ||
13228 | void | |
13229 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
13230 | { | |
13231 | struct bfd_sym_chain *sym; | |
13232 | ||
13233 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
13234 | { | |
13235 | struct elf_link_hash_entry *h; | |
13236 | ||
13237 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
13238 | FALSE, FALSE, FALSE); | |
13239 | ||
13240 | if (h != NULL | |
13241 | && (h->root.type == bfd_link_hash_defined | |
13242 | || h->root.type == bfd_link_hash_defweak) | |
f02cb058 AM |
13243 | && !bfd_is_abs_section (h->root.u.def.section) |
13244 | && !bfd_is_und_section (h->root.u.def.section)) | |
74f0fb50 AM |
13245 | h->root.u.def.section->flags |= SEC_KEEP; |
13246 | } | |
13247 | } | |
13248 | ||
2f0c68f2 CM |
13249 | bfd_boolean |
13250 | bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, | |
13251 | struct bfd_link_info *info) | |
13252 | { | |
13253 | bfd *ibfd = info->input_bfds; | |
13254 | ||
13255 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) | |
13256 | { | |
13257 | asection *sec; | |
13258 | struct elf_reloc_cookie cookie; | |
13259 | ||
13260 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13261 | continue; | |
57963c05 AM |
13262 | sec = ibfd->sections; |
13263 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13264 | continue; | |
2f0c68f2 CM |
13265 | |
13266 | if (!init_reloc_cookie (&cookie, info, ibfd)) | |
13267 | return FALSE; | |
13268 | ||
13269 | for (sec = ibfd->sections; sec; sec = sec->next) | |
13270 | { | |
13271 | if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry") | |
13272 | && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) | |
13273 | { | |
13274 | _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); | |
13275 | fini_reloc_cookie_rels (&cookie, sec); | |
13276 | } | |
13277 | } | |
13278 | } | |
13279 | return TRUE; | |
13280 | } | |
13281 | ||
c152c796 AM |
13282 | /* Do mark and sweep of unused sections. */ |
13283 | ||
13284 | bfd_boolean | |
13285 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
13286 | { | |
13287 | bfd_boolean ok = TRUE; | |
13288 | bfd *sub; | |
6a5bb875 | 13289 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 13290 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
da44f4e5 | 13291 | struct elf_link_hash_table *htab; |
c152c796 | 13292 | |
64d03ab5 | 13293 | if (!bed->can_gc_sections |
715df9b8 | 13294 | || !is_elf_hash_table (info->hash)) |
c152c796 | 13295 | { |
4eca0228 | 13296 | _bfd_error_handler(_("Warning: gc-sections option ignored")); |
c152c796 AM |
13297 | return TRUE; |
13298 | } | |
13299 | ||
74f0fb50 | 13300 | bed->gc_keep (info); |
da44f4e5 | 13301 | htab = elf_hash_table (info); |
74f0fb50 | 13302 | |
9d0a14d3 RS |
13303 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
13304 | at the .eh_frame section if we can mark the FDEs individually. */ | |
2f0c68f2 CM |
13305 | for (sub = info->input_bfds; |
13306 | info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; | |
13307 | sub = sub->link.next) | |
9d0a14d3 RS |
13308 | { |
13309 | asection *sec; | |
13310 | struct elf_reloc_cookie cookie; | |
13311 | ||
57963c05 AM |
13312 | sec = sub->sections; |
13313 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13314 | continue; | |
9d0a14d3 | 13315 | sec = bfd_get_section_by_name (sub, ".eh_frame"); |
9a2a56cc | 13316 | while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) |
9d0a14d3 RS |
13317 | { |
13318 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
9a2a56cc AM |
13319 | if (elf_section_data (sec)->sec_info |
13320 | && (sec->flags & SEC_LINKER_CREATED) == 0) | |
9d0a14d3 RS |
13321 | elf_eh_frame_section (sub) = sec; |
13322 | fini_reloc_cookie_for_section (&cookie, sec); | |
199af150 | 13323 | sec = bfd_get_next_section_by_name (NULL, sec); |
9d0a14d3 RS |
13324 | } |
13325 | } | |
9d0a14d3 | 13326 | |
c152c796 | 13327 | /* Apply transitive closure to the vtable entry usage info. */ |
da44f4e5 | 13328 | elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); |
c152c796 AM |
13329 | if (!ok) |
13330 | return FALSE; | |
13331 | ||
13332 | /* Kill the vtable relocations that were not used. */ | |
da44f4e5 | 13333 | elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok); |
c152c796 AM |
13334 | if (!ok) |
13335 | return FALSE; | |
13336 | ||
715df9b8 | 13337 | /* Mark dynamically referenced symbols. */ |
22185505 | 13338 | if (htab->dynamic_sections_created || info->gc_keep_exported) |
da44f4e5 | 13339 | elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); |
c152c796 | 13340 | |
715df9b8 | 13341 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 13342 | gc_mark_hook = bed->gc_mark_hook; |
c72f2fb2 | 13343 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13344 | { |
13345 | asection *o; | |
13346 | ||
b19a8f85 | 13347 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 13348 | || elf_object_id (sub) != elf_hash_table_id (htab) |
b19a8f85 | 13349 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 AM |
13350 | continue; |
13351 | ||
57963c05 AM |
13352 | o = sub->sections; |
13353 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13354 | continue; | |
13355 | ||
7f6ab9f8 AM |
13356 | /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). |
13357 | Also treat note sections as a root, if the section is not part | |
13358 | of a group. */ | |
c152c796 | 13359 | for (o = sub->sections; o != NULL; o = o->next) |
7f6ab9f8 AM |
13360 | if (!o->gc_mark |
13361 | && (o->flags & SEC_EXCLUDE) == 0 | |
24007750 | 13362 | && ((o->flags & SEC_KEEP) != 0 |
7f6ab9f8 AM |
13363 | || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE |
13364 | && elf_next_in_group (o) == NULL ))) | |
13365 | { | |
13366 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
13367 | return FALSE; | |
13368 | } | |
c152c796 AM |
13369 | } |
13370 | ||
6a5bb875 | 13371 | /* Allow the backend to mark additional target specific sections. */ |
7f6ab9f8 | 13372 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 13373 | |
c152c796 | 13374 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 13375 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
13376 | } |
13377 | \f | |
13378 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
13379 | ||
13380 | bfd_boolean | |
13381 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
13382 | asection *sec, | |
13383 | struct elf_link_hash_entry *h, | |
13384 | bfd_vma offset) | |
13385 | { | |
13386 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
13387 | struct elf_link_hash_entry **search, *child; | |
ef53be89 | 13388 | size_t extsymcount; |
c152c796 AM |
13389 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
13390 | ||
13391 | /* The sh_info field of the symtab header tells us where the | |
13392 | external symbols start. We don't care about the local symbols at | |
13393 | this point. */ | |
13394 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
13395 | if (!elf_bad_symtab (abfd)) | |
13396 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
13397 | ||
13398 | sym_hashes = elf_sym_hashes (abfd); | |
13399 | sym_hashes_end = sym_hashes + extsymcount; | |
13400 | ||
13401 | /* Hunt down the child symbol, which is in this section at the same | |
13402 | offset as the relocation. */ | |
13403 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
13404 | { | |
13405 | if ((child = *search) != NULL | |
13406 | && (child->root.type == bfd_link_hash_defined | |
13407 | || child->root.type == bfd_link_hash_defweak) | |
13408 | && child->root.u.def.section == sec | |
13409 | && child->root.u.def.value == offset) | |
13410 | goto win; | |
13411 | } | |
13412 | ||
695344c0 | 13413 | /* xgettext:c-format */ |
76cfced5 AM |
13414 | _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"), |
13415 | abfd, sec, offset); | |
c152c796 AM |
13416 | bfd_set_error (bfd_error_invalid_operation); |
13417 | return FALSE; | |
13418 | ||
13419 | win: | |
cbd0eecf | 13420 | if (!child->u2.vtable) |
f6e332e6 | 13421 | { |
cbd0eecf L |
13422 | child->u2.vtable = ((struct elf_link_virtual_table_entry *) |
13423 | bfd_zalloc (abfd, sizeof (*child->u2.vtable))); | |
13424 | if (!child->u2.vtable) | |
f6e332e6 AM |
13425 | return FALSE; |
13426 | } | |
c152c796 AM |
13427 | if (!h) |
13428 | { | |
13429 | /* This *should* only be the absolute section. It could potentially | |
13430 | be that someone has defined a non-global vtable though, which | |
13431 | would be bad. It isn't worth paging in the local symbols to be | |
13432 | sure though; that case should simply be handled by the assembler. */ | |
13433 | ||
cbd0eecf | 13434 | child->u2.vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
13435 | } |
13436 | else | |
cbd0eecf | 13437 | child->u2.vtable->parent = h; |
c152c796 AM |
13438 | |
13439 | return TRUE; | |
13440 | } | |
13441 | ||
13442 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
13443 | ||
13444 | bfd_boolean | |
13445 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
13446 | asection *sec ATTRIBUTE_UNUSED, | |
13447 | struct elf_link_hash_entry *h, | |
13448 | bfd_vma addend) | |
13449 | { | |
13450 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13451 | unsigned int log_file_align = bed->s->log_file_align; | |
13452 | ||
cbd0eecf | 13453 | if (!h->u2.vtable) |
f6e332e6 | 13454 | { |
cbd0eecf L |
13455 | h->u2.vtable = ((struct elf_link_virtual_table_entry *) |
13456 | bfd_zalloc (abfd, sizeof (*h->u2.vtable))); | |
13457 | if (!h->u2.vtable) | |
f6e332e6 AM |
13458 | return FALSE; |
13459 | } | |
13460 | ||
cbd0eecf | 13461 | if (addend >= h->u2.vtable->size) |
c152c796 AM |
13462 | { |
13463 | size_t size, bytes, file_align; | |
cbd0eecf | 13464 | bfd_boolean *ptr = h->u2.vtable->used; |
c152c796 AM |
13465 | |
13466 | /* While the symbol is undefined, we have to be prepared to handle | |
13467 | a zero size. */ | |
13468 | file_align = 1 << log_file_align; | |
13469 | if (h->root.type == bfd_link_hash_undefined) | |
13470 | size = addend + file_align; | |
13471 | else | |
13472 | { | |
13473 | size = h->size; | |
13474 | if (addend >= size) | |
13475 | { | |
13476 | /* Oops! We've got a reference past the defined end of | |
13477 | the table. This is probably a bug -- shall we warn? */ | |
13478 | size = addend + file_align; | |
13479 | } | |
13480 | } | |
13481 | size = (size + file_align - 1) & -file_align; | |
13482 | ||
13483 | /* Allocate one extra entry for use as a "done" flag for the | |
13484 | consolidation pass. */ | |
13485 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
13486 | ||
13487 | if (ptr) | |
13488 | { | |
a50b1753 | 13489 | ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); |
c152c796 AM |
13490 | |
13491 | if (ptr != NULL) | |
13492 | { | |
13493 | size_t oldbytes; | |
13494 | ||
cbd0eecf | 13495 | oldbytes = (((h->u2.vtable->size >> log_file_align) + 1) |
c152c796 AM |
13496 | * sizeof (bfd_boolean)); |
13497 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
13498 | } | |
13499 | } | |
13500 | else | |
a50b1753 | 13501 | ptr = (bfd_boolean *) bfd_zmalloc (bytes); |
c152c796 AM |
13502 | |
13503 | if (ptr == NULL) | |
13504 | return FALSE; | |
13505 | ||
13506 | /* And arrange for that done flag to be at index -1. */ | |
cbd0eecf L |
13507 | h->u2.vtable->used = ptr + 1; |
13508 | h->u2.vtable->size = size; | |
c152c796 AM |
13509 | } |
13510 | ||
cbd0eecf | 13511 | h->u2.vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
13512 | |
13513 | return TRUE; | |
13514 | } | |
13515 | ||
ae17ab41 CM |
13516 | /* Map an ELF section header flag to its corresponding string. */ |
13517 | typedef struct | |
13518 | { | |
13519 | char *flag_name; | |
13520 | flagword flag_value; | |
13521 | } elf_flags_to_name_table; | |
13522 | ||
13523 | static elf_flags_to_name_table elf_flags_to_names [] = | |
13524 | { | |
13525 | { "SHF_WRITE", SHF_WRITE }, | |
13526 | { "SHF_ALLOC", SHF_ALLOC }, | |
13527 | { "SHF_EXECINSTR", SHF_EXECINSTR }, | |
13528 | { "SHF_MERGE", SHF_MERGE }, | |
13529 | { "SHF_STRINGS", SHF_STRINGS }, | |
13530 | { "SHF_INFO_LINK", SHF_INFO_LINK}, | |
13531 | { "SHF_LINK_ORDER", SHF_LINK_ORDER}, | |
13532 | { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, | |
13533 | { "SHF_GROUP", SHF_GROUP }, | |
13534 | { "SHF_TLS", SHF_TLS }, | |
13535 | { "SHF_MASKOS", SHF_MASKOS }, | |
13536 | { "SHF_EXCLUDE", SHF_EXCLUDE }, | |
13537 | }; | |
13538 | ||
b9c361e0 JL |
13539 | /* Returns TRUE if the section is to be included, otherwise FALSE. */ |
13540 | bfd_boolean | |
ae17ab41 | 13541 | bfd_elf_lookup_section_flags (struct bfd_link_info *info, |
8b127cbc | 13542 | struct flag_info *flaginfo, |
b9c361e0 | 13543 | asection *section) |
ae17ab41 | 13544 | { |
8b127cbc | 13545 | const bfd_vma sh_flags = elf_section_flags (section); |
ae17ab41 | 13546 | |
8b127cbc | 13547 | if (!flaginfo->flags_initialized) |
ae17ab41 | 13548 | { |
8b127cbc AM |
13549 | bfd *obfd = info->output_bfd; |
13550 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
13551 | struct flag_info_list *tf = flaginfo->flag_list; | |
b9c361e0 JL |
13552 | int with_hex = 0; |
13553 | int without_hex = 0; | |
13554 | ||
8b127cbc | 13555 | for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) |
ae17ab41 | 13556 | { |
b9c361e0 | 13557 | unsigned i; |
8b127cbc | 13558 | flagword (*lookup) (char *); |
ae17ab41 | 13559 | |
8b127cbc AM |
13560 | lookup = bed->elf_backend_lookup_section_flags_hook; |
13561 | if (lookup != NULL) | |
ae17ab41 | 13562 | { |
8b127cbc | 13563 | flagword hexval = (*lookup) ((char *) tf->name); |
b9c361e0 JL |
13564 | |
13565 | if (hexval != 0) | |
13566 | { | |
13567 | if (tf->with == with_flags) | |
13568 | with_hex |= hexval; | |
13569 | else if (tf->with == without_flags) | |
13570 | without_hex |= hexval; | |
13571 | tf->valid = TRUE; | |
13572 | continue; | |
13573 | } | |
ae17ab41 | 13574 | } |
8b127cbc | 13575 | for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) |
ae17ab41 | 13576 | { |
8b127cbc | 13577 | if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) |
b9c361e0 JL |
13578 | { |
13579 | if (tf->with == with_flags) | |
13580 | with_hex |= elf_flags_to_names[i].flag_value; | |
13581 | else if (tf->with == without_flags) | |
13582 | without_hex |= elf_flags_to_names[i].flag_value; | |
13583 | tf->valid = TRUE; | |
13584 | break; | |
13585 | } | |
13586 | } | |
8b127cbc | 13587 | if (!tf->valid) |
b9c361e0 | 13588 | { |
68ffbac6 | 13589 | info->callbacks->einfo |
8b127cbc | 13590 | (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); |
b9c361e0 | 13591 | return FALSE; |
ae17ab41 CM |
13592 | } |
13593 | } | |
8b127cbc AM |
13594 | flaginfo->flags_initialized = TRUE; |
13595 | flaginfo->only_with_flags |= with_hex; | |
13596 | flaginfo->not_with_flags |= without_hex; | |
ae17ab41 | 13597 | } |
ae17ab41 | 13598 | |
8b127cbc | 13599 | if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) |
b9c361e0 JL |
13600 | return FALSE; |
13601 | ||
8b127cbc | 13602 | if ((flaginfo->not_with_flags & sh_flags) != 0) |
b9c361e0 JL |
13603 | return FALSE; |
13604 | ||
13605 | return TRUE; | |
ae17ab41 CM |
13606 | } |
13607 | ||
c152c796 AM |
13608 | struct alloc_got_off_arg { |
13609 | bfd_vma gotoff; | |
10455f89 | 13610 | struct bfd_link_info *info; |
c152c796 AM |
13611 | }; |
13612 | ||
13613 | /* We need a special top-level link routine to convert got reference counts | |
13614 | to real got offsets. */ | |
13615 | ||
13616 | static bfd_boolean | |
13617 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
13618 | { | |
a50b1753 | 13619 | struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; |
10455f89 HPN |
13620 | bfd *obfd = gofarg->info->output_bfd; |
13621 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
c152c796 | 13622 | |
c152c796 AM |
13623 | if (h->got.refcount > 0) |
13624 | { | |
13625 | h->got.offset = gofarg->gotoff; | |
10455f89 | 13626 | gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); |
c152c796 AM |
13627 | } |
13628 | else | |
13629 | h->got.offset = (bfd_vma) -1; | |
13630 | ||
13631 | return TRUE; | |
13632 | } | |
13633 | ||
13634 | /* And an accompanying bit to work out final got entry offsets once | |
13635 | we're done. Should be called from final_link. */ | |
13636 | ||
13637 | bfd_boolean | |
13638 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
13639 | struct bfd_link_info *info) | |
13640 | { | |
13641 | bfd *i; | |
13642 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13643 | bfd_vma gotoff; | |
c152c796 AM |
13644 | struct alloc_got_off_arg gofarg; |
13645 | ||
10455f89 HPN |
13646 | BFD_ASSERT (abfd == info->output_bfd); |
13647 | ||
c152c796 AM |
13648 | if (! is_elf_hash_table (info->hash)) |
13649 | return FALSE; | |
13650 | ||
13651 | /* The GOT offset is relative to the .got section, but the GOT header is | |
13652 | put into the .got.plt section, if the backend uses it. */ | |
13653 | if (bed->want_got_plt) | |
13654 | gotoff = 0; | |
13655 | else | |
13656 | gotoff = bed->got_header_size; | |
13657 | ||
13658 | /* Do the local .got entries first. */ | |
c72f2fb2 | 13659 | for (i = info->input_bfds; i; i = i->link.next) |
c152c796 AM |
13660 | { |
13661 | bfd_signed_vma *local_got; | |
ef53be89 | 13662 | size_t j, locsymcount; |
c152c796 AM |
13663 | Elf_Internal_Shdr *symtab_hdr; |
13664 | ||
13665 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
13666 | continue; | |
13667 | ||
13668 | local_got = elf_local_got_refcounts (i); | |
13669 | if (!local_got) | |
13670 | continue; | |
13671 | ||
13672 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
13673 | if (elf_bad_symtab (i)) | |
13674 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
13675 | else | |
13676 | locsymcount = symtab_hdr->sh_info; | |
13677 | ||
13678 | for (j = 0; j < locsymcount; ++j) | |
13679 | { | |
13680 | if (local_got[j] > 0) | |
13681 | { | |
13682 | local_got[j] = gotoff; | |
10455f89 | 13683 | gotoff += bed->got_elt_size (abfd, info, NULL, i, j); |
c152c796 AM |
13684 | } |
13685 | else | |
13686 | local_got[j] = (bfd_vma) -1; | |
13687 | } | |
13688 | } | |
13689 | ||
13690 | /* Then the global .got entries. .plt refcounts are handled by | |
13691 | adjust_dynamic_symbol */ | |
13692 | gofarg.gotoff = gotoff; | |
10455f89 | 13693 | gofarg.info = info; |
c152c796 AM |
13694 | elf_link_hash_traverse (elf_hash_table (info), |
13695 | elf_gc_allocate_got_offsets, | |
13696 | &gofarg); | |
13697 | return TRUE; | |
13698 | } | |
13699 | ||
13700 | /* Many folk need no more in the way of final link than this, once | |
13701 | got entry reference counting is enabled. */ | |
13702 | ||
13703 | bfd_boolean | |
13704 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
13705 | { | |
13706 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
13707 | return FALSE; | |
13708 | ||
13709 | /* Invoke the regular ELF backend linker to do all the work. */ | |
13710 | return bfd_elf_final_link (abfd, info); | |
13711 | } | |
13712 | ||
13713 | bfd_boolean | |
13714 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
13715 | { | |
a50b1753 | 13716 | struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; |
c152c796 AM |
13717 | |
13718 | if (rcookie->bad_symtab) | |
13719 | rcookie->rel = rcookie->rels; | |
13720 | ||
13721 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
13722 | { | |
13723 | unsigned long r_symndx; | |
13724 | ||
13725 | if (! rcookie->bad_symtab) | |
13726 | if (rcookie->rel->r_offset > offset) | |
13727 | return FALSE; | |
13728 | if (rcookie->rel->r_offset != offset) | |
13729 | continue; | |
13730 | ||
13731 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
2c2fa401 | 13732 | if (r_symndx == STN_UNDEF) |
c152c796 AM |
13733 | return TRUE; |
13734 | ||
13735 | if (r_symndx >= rcookie->locsymcount | |
13736 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
13737 | { | |
13738 | struct elf_link_hash_entry *h; | |
13739 | ||
13740 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
13741 | ||
13742 | while (h->root.type == bfd_link_hash_indirect | |
13743 | || h->root.type == bfd_link_hash_warning) | |
13744 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
13745 | ||
13746 | if ((h->root.type == bfd_link_hash_defined | |
13747 | || h->root.type == bfd_link_hash_defweak) | |
5b69e357 AM |
13748 | && (h->root.u.def.section->owner != rcookie->abfd |
13749 | || h->root.u.def.section->kept_section != NULL | |
13750 | || discarded_section (h->root.u.def.section))) | |
c152c796 | 13751 | return TRUE; |
c152c796 AM |
13752 | } |
13753 | else | |
13754 | { | |
13755 | /* It's not a relocation against a global symbol, | |
13756 | but it could be a relocation against a local | |
13757 | symbol for a discarded section. */ | |
13758 | asection *isec; | |
13759 | Elf_Internal_Sym *isym; | |
13760 | ||
13761 | /* Need to: get the symbol; get the section. */ | |
13762 | isym = &rcookie->locsyms[r_symndx]; | |
cb33740c | 13763 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); |
5b69e357 AM |
13764 | if (isec != NULL |
13765 | && (isec->kept_section != NULL | |
13766 | || discarded_section (isec))) | |
cb33740c | 13767 | return TRUE; |
c152c796 AM |
13768 | } |
13769 | return FALSE; | |
13770 | } | |
13771 | return FALSE; | |
13772 | } | |
13773 | ||
13774 | /* Discard unneeded references to discarded sections. | |
75938853 AM |
13775 | Returns -1 on error, 1 if any section's size was changed, 0 if |
13776 | nothing changed. This function assumes that the relocations are in | |
13777 | sorted order, which is true for all known assemblers. */ | |
c152c796 | 13778 | |
75938853 | 13779 | int |
c152c796 AM |
13780 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) |
13781 | { | |
13782 | struct elf_reloc_cookie cookie; | |
18cd5bce | 13783 | asection *o; |
c152c796 | 13784 | bfd *abfd; |
75938853 | 13785 | int changed = 0; |
c152c796 AM |
13786 | |
13787 | if (info->traditional_format | |
13788 | || !is_elf_hash_table (info->hash)) | |
75938853 | 13789 | return 0; |
c152c796 | 13790 | |
18cd5bce AM |
13791 | o = bfd_get_section_by_name (output_bfd, ".stab"); |
13792 | if (o != NULL) | |
c152c796 | 13793 | { |
18cd5bce | 13794 | asection *i; |
c152c796 | 13795 | |
18cd5bce | 13796 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
8da3dbc5 | 13797 | { |
18cd5bce AM |
13798 | if (i->size == 0 |
13799 | || i->reloc_count == 0 | |
13800 | || i->sec_info_type != SEC_INFO_TYPE_STABS) | |
13801 | continue; | |
c152c796 | 13802 | |
18cd5bce AM |
13803 | abfd = i->owner; |
13804 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
13805 | continue; | |
c152c796 | 13806 | |
18cd5bce | 13807 | if (!init_reloc_cookie_for_section (&cookie, info, i)) |
75938853 | 13808 | return -1; |
c152c796 | 13809 | |
18cd5bce AM |
13810 | if (_bfd_discard_section_stabs (abfd, i, |
13811 | elf_section_data (i)->sec_info, | |
5241d853 RS |
13812 | bfd_elf_reloc_symbol_deleted_p, |
13813 | &cookie)) | |
75938853 | 13814 | changed = 1; |
18cd5bce AM |
13815 | |
13816 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 13817 | } |
18cd5bce AM |
13818 | } |
13819 | ||
2f0c68f2 CM |
13820 | o = NULL; |
13821 | if (info->eh_frame_hdr_type != COMPACT_EH_HDR) | |
13822 | o = bfd_get_section_by_name (output_bfd, ".eh_frame"); | |
18cd5bce AM |
13823 | if (o != NULL) |
13824 | { | |
13825 | asection *i; | |
d7153c4a | 13826 | int eh_changed = 0; |
79a94a2a | 13827 | unsigned int eh_alignment; |
c152c796 | 13828 | |
18cd5bce | 13829 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
c152c796 | 13830 | { |
18cd5bce AM |
13831 | if (i->size == 0) |
13832 | continue; | |
13833 | ||
13834 | abfd = i->owner; | |
13835 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
13836 | continue; | |
13837 | ||
13838 | if (!init_reloc_cookie_for_section (&cookie, info, i)) | |
75938853 | 13839 | return -1; |
18cd5bce AM |
13840 | |
13841 | _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); | |
13842 | if (_bfd_elf_discard_section_eh_frame (abfd, info, i, | |
c152c796 AM |
13843 | bfd_elf_reloc_symbol_deleted_p, |
13844 | &cookie)) | |
d7153c4a AM |
13845 | { |
13846 | eh_changed = 1; | |
13847 | if (i->size != i->rawsize) | |
13848 | changed = 1; | |
13849 | } | |
18cd5bce AM |
13850 | |
13851 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 13852 | } |
9866ffe2 | 13853 | |
79a94a2a | 13854 | eh_alignment = 1 << o->alignment_power; |
9866ffe2 AM |
13855 | /* Skip over zero terminator, and prevent empty sections from |
13856 | adding alignment padding at the end. */ | |
13857 | for (i = o->map_tail.s; i != NULL; i = i->map_tail.s) | |
13858 | if (i->size == 0) | |
13859 | i->flags |= SEC_EXCLUDE; | |
13860 | else if (i->size > 4) | |
13861 | break; | |
13862 | /* The last non-empty eh_frame section doesn't need padding. */ | |
13863 | if (i != NULL) | |
13864 | i = i->map_tail.s; | |
13865 | /* Any prior sections must pad the last FDE out to the output | |
13866 | section alignment. Otherwise we might have zero padding | |
13867 | between sections, which would be seen as a terminator. */ | |
13868 | for (; i != NULL; i = i->map_tail.s) | |
13869 | if (i->size == 4) | |
13870 | /* All but the last zero terminator should have been removed. */ | |
13871 | BFD_FAIL (); | |
13872 | else | |
13873 | { | |
13874 | bfd_size_type size | |
13875 | = (i->size + eh_alignment - 1) & -eh_alignment; | |
13876 | if (i->size != size) | |
af471f82 | 13877 | { |
9866ffe2 AM |
13878 | i->size = size; |
13879 | changed = 1; | |
13880 | eh_changed = 1; | |
af471f82 | 13881 | } |
9866ffe2 | 13882 | } |
d7153c4a AM |
13883 | if (eh_changed) |
13884 | elf_link_hash_traverse (elf_hash_table (info), | |
13885 | _bfd_elf_adjust_eh_frame_global_symbol, NULL); | |
18cd5bce | 13886 | } |
c152c796 | 13887 | |
18cd5bce AM |
13888 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) |
13889 | { | |
13890 | const struct elf_backend_data *bed; | |
57963c05 | 13891 | asection *s; |
c152c796 | 13892 | |
18cd5bce AM |
13893 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) |
13894 | continue; | |
57963c05 AM |
13895 | s = abfd->sections; |
13896 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13897 | continue; | |
18cd5bce AM |
13898 | |
13899 | bed = get_elf_backend_data (abfd); | |
13900 | ||
13901 | if (bed->elf_backend_discard_info != NULL) | |
13902 | { | |
13903 | if (!init_reloc_cookie (&cookie, info, abfd)) | |
75938853 | 13904 | return -1; |
18cd5bce AM |
13905 | |
13906 | if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
75938853 | 13907 | changed = 1; |
18cd5bce AM |
13908 | |
13909 | fini_reloc_cookie (&cookie, abfd); | |
13910 | } | |
c152c796 AM |
13911 | } |
13912 | ||
2f0c68f2 CM |
13913 | if (info->eh_frame_hdr_type == COMPACT_EH_HDR) |
13914 | _bfd_elf_end_eh_frame_parsing (info); | |
13915 | ||
13916 | if (info->eh_frame_hdr_type | |
0e1862bb | 13917 | && !bfd_link_relocatable (info) |
c152c796 | 13918 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) |
75938853 | 13919 | changed = 1; |
c152c796 | 13920 | |
75938853 | 13921 | return changed; |
c152c796 | 13922 | } |
082b7297 | 13923 | |
43e1669b | 13924 | bfd_boolean |
0c511000 | 13925 | _bfd_elf_section_already_linked (bfd *abfd, |
c77ec726 | 13926 | asection *sec, |
c0f00686 | 13927 | struct bfd_link_info *info) |
082b7297 L |
13928 | { |
13929 | flagword flags; | |
c77ec726 | 13930 | const char *name, *key; |
082b7297 L |
13931 | struct bfd_section_already_linked *l; |
13932 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
0c511000 | 13933 | |
c77ec726 AM |
13934 | if (sec->output_section == bfd_abs_section_ptr) |
13935 | return FALSE; | |
0c511000 | 13936 | |
c77ec726 | 13937 | flags = sec->flags; |
0c511000 | 13938 | |
c77ec726 AM |
13939 | /* Return if it isn't a linkonce section. A comdat group section |
13940 | also has SEC_LINK_ONCE set. */ | |
13941 | if ((flags & SEC_LINK_ONCE) == 0) | |
13942 | return FALSE; | |
0c511000 | 13943 | |
c77ec726 AM |
13944 | /* Don't put group member sections on our list of already linked |
13945 | sections. They are handled as a group via their group section. */ | |
13946 | if (elf_sec_group (sec) != NULL) | |
13947 | return FALSE; | |
0c511000 | 13948 | |
c77ec726 AM |
13949 | /* For a SHT_GROUP section, use the group signature as the key. */ |
13950 | name = sec->name; | |
13951 | if ((flags & SEC_GROUP) != 0 | |
13952 | && elf_next_in_group (sec) != NULL | |
13953 | && elf_group_name (elf_next_in_group (sec)) != NULL) | |
13954 | key = elf_group_name (elf_next_in_group (sec)); | |
13955 | else | |
13956 | { | |
13957 | /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ | |
0c511000 | 13958 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
c77ec726 AM |
13959 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
13960 | key++; | |
0c511000 | 13961 | else |
c77ec726 AM |
13962 | /* Must be a user linkonce section that doesn't follow gcc's |
13963 | naming convention. In this case we won't be matching | |
13964 | single member groups. */ | |
13965 | key = name; | |
0c511000 | 13966 | } |
6d2cd210 | 13967 | |
c77ec726 | 13968 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
082b7297 L |
13969 | |
13970 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13971 | { | |
c2370991 | 13972 | /* We may have 2 different types of sections on the list: group |
c77ec726 AM |
13973 | sections with a signature of <key> (<key> is some string), |
13974 | and linkonce sections named .gnu.linkonce.<type>.<key>. | |
13975 | Match like sections. LTO plugin sections are an exception. | |
13976 | They are always named .gnu.linkonce.t.<key> and match either | |
13977 | type of section. */ | |
13978 | if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) | |
13979 | && ((flags & SEC_GROUP) != 0 | |
13980 | || strcmp (name, l->sec->name) == 0)) | |
13981 | || (l->sec->owner->flags & BFD_PLUGIN) != 0) | |
082b7297 L |
13982 | { |
13983 | /* The section has already been linked. See if we should | |
6d2cd210 | 13984 | issue a warning. */ |
c77ec726 AM |
13985 | if (!_bfd_handle_already_linked (sec, l, info)) |
13986 | return FALSE; | |
082b7297 | 13987 | |
c77ec726 | 13988 | if (flags & SEC_GROUP) |
3d7f7666 | 13989 | { |
c77ec726 AM |
13990 | asection *first = elf_next_in_group (sec); |
13991 | asection *s = first; | |
3d7f7666 | 13992 | |
c77ec726 | 13993 | while (s != NULL) |
3d7f7666 | 13994 | { |
c77ec726 AM |
13995 | s->output_section = bfd_abs_section_ptr; |
13996 | /* Record which group discards it. */ | |
13997 | s->kept_section = l->sec; | |
13998 | s = elf_next_in_group (s); | |
13999 | /* These lists are circular. */ | |
14000 | if (s == first) | |
14001 | break; | |
3d7f7666 L |
14002 | } |
14003 | } | |
082b7297 | 14004 | |
43e1669b | 14005 | return TRUE; |
082b7297 L |
14006 | } |
14007 | } | |
14008 | ||
c77ec726 AM |
14009 | /* A single member comdat group section may be discarded by a |
14010 | linkonce section and vice versa. */ | |
14011 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 14012 | { |
c77ec726 | 14013 | asection *first = elf_next_in_group (sec); |
c2370991 | 14014 | |
c77ec726 AM |
14015 | if (first != NULL && elf_next_in_group (first) == first) |
14016 | /* Check this single member group against linkonce sections. */ | |
14017 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14018 | if ((l->sec->flags & SEC_GROUP) == 0 | |
14019 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
14020 | { | |
14021 | first->output_section = bfd_abs_section_ptr; | |
14022 | first->kept_section = l->sec; | |
14023 | sec->output_section = bfd_abs_section_ptr; | |
14024 | break; | |
14025 | } | |
14026 | } | |
14027 | else | |
14028 | /* Check this linkonce section against single member groups. */ | |
14029 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14030 | if (l->sec->flags & SEC_GROUP) | |
6d2cd210 | 14031 | { |
c77ec726 | 14032 | asection *first = elf_next_in_group (l->sec); |
6d2cd210 | 14033 | |
c77ec726 AM |
14034 | if (first != NULL |
14035 | && elf_next_in_group (first) == first | |
14036 | && bfd_elf_match_symbols_in_sections (first, sec, info)) | |
14037 | { | |
14038 | sec->output_section = bfd_abs_section_ptr; | |
14039 | sec->kept_section = first; | |
14040 | break; | |
14041 | } | |
6d2cd210 | 14042 | } |
0c511000 | 14043 | |
c77ec726 AM |
14044 | /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' |
14045 | referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 | |
14046 | specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' | |
14047 | prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its | |
14048 | matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded | |
14049 | but its `.gnu.linkonce.t.F' is discarded means we chose one-only | |
14050 | `.gnu.linkonce.t.F' section from a different bfd not requiring any | |
14051 | `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. | |
14052 | The reverse order cannot happen as there is never a bfd with only the | |
14053 | `.gnu.linkonce.r.F' section. The order of sections in a bfd does not | |
14054 | matter as here were are looking only for cross-bfd sections. */ | |
14055 | ||
14056 | if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) | |
14057 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14058 | if ((l->sec->flags & SEC_GROUP) == 0 | |
14059 | && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) | |
14060 | { | |
14061 | if (abfd != l->sec->owner) | |
14062 | sec->output_section = bfd_abs_section_ptr; | |
14063 | break; | |
14064 | } | |
80c29487 | 14065 | |
082b7297 | 14066 | /* This is the first section with this name. Record it. */ |
c77ec726 | 14067 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
bb6198d2 | 14068 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
c77ec726 | 14069 | return sec->output_section == bfd_abs_section_ptr; |
082b7297 | 14070 | } |
81e1b023 | 14071 | |
a4d8e49b L |
14072 | bfd_boolean |
14073 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
14074 | { | |
14075 | return sym->st_shndx == SHN_COMMON; | |
14076 | } | |
14077 | ||
14078 | unsigned int | |
14079 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
14080 | { | |
14081 | return SHN_COMMON; | |
14082 | } | |
14083 | ||
14084 | asection * | |
14085 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
14086 | { | |
14087 | return bfd_com_section_ptr; | |
14088 | } | |
10455f89 HPN |
14089 | |
14090 | bfd_vma | |
14091 | _bfd_elf_default_got_elt_size (bfd *abfd, | |
14092 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
14093 | struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, | |
14094 | bfd *ibfd ATTRIBUTE_UNUSED, | |
14095 | unsigned long symndx ATTRIBUTE_UNUSED) | |
14096 | { | |
14097 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14098 | return bed->s->arch_size / 8; | |
14099 | } | |
83bac4b0 NC |
14100 | |
14101 | /* Routines to support the creation of dynamic relocs. */ | |
14102 | ||
83bac4b0 NC |
14103 | /* Returns the name of the dynamic reloc section associated with SEC. */ |
14104 | ||
14105 | static const char * | |
14106 | get_dynamic_reloc_section_name (bfd * abfd, | |
14107 | asection * sec, | |
14108 | bfd_boolean is_rela) | |
14109 | { | |
ddcf1fcf BS |
14110 | char *name; |
14111 | const char *old_name = bfd_get_section_name (NULL, sec); | |
14112 | const char *prefix = is_rela ? ".rela" : ".rel"; | |
83bac4b0 | 14113 | |
ddcf1fcf | 14114 | if (old_name == NULL) |
83bac4b0 NC |
14115 | return NULL; |
14116 | ||
ddcf1fcf | 14117 | name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); |
68ffbac6 | 14118 | sprintf (name, "%s%s", prefix, old_name); |
83bac4b0 NC |
14119 | |
14120 | return name; | |
14121 | } | |
14122 | ||
14123 | /* Returns the dynamic reloc section associated with SEC. | |
14124 | If necessary compute the name of the dynamic reloc section based | |
14125 | on SEC's name (looked up in ABFD's string table) and the setting | |
14126 | of IS_RELA. */ | |
14127 | ||
14128 | asection * | |
14129 | _bfd_elf_get_dynamic_reloc_section (bfd * abfd, | |
14130 | asection * sec, | |
14131 | bfd_boolean is_rela) | |
14132 | { | |
14133 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
14134 | ||
14135 | if (reloc_sec == NULL) | |
14136 | { | |
14137 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
14138 | ||
14139 | if (name != NULL) | |
14140 | { | |
3d4d4302 | 14141 | reloc_sec = bfd_get_linker_section (abfd, name); |
83bac4b0 NC |
14142 | |
14143 | if (reloc_sec != NULL) | |
14144 | elf_section_data (sec)->sreloc = reloc_sec; | |
14145 | } | |
14146 | } | |
14147 | ||
14148 | return reloc_sec; | |
14149 | } | |
14150 | ||
14151 | /* Returns the dynamic reloc section associated with SEC. If the | |
14152 | section does not exist it is created and attached to the DYNOBJ | |
14153 | bfd and stored in the SRELOC field of SEC's elf_section_data | |
14154 | structure. | |
f8076f98 | 14155 | |
83bac4b0 NC |
14156 | ALIGNMENT is the alignment for the newly created section and |
14157 | IS_RELA defines whether the name should be .rela.<SEC's name> | |
14158 | or .rel.<SEC's name>. The section name is looked up in the | |
14159 | string table associated with ABFD. */ | |
14160 | ||
14161 | asection * | |
ca4be51c AM |
14162 | _bfd_elf_make_dynamic_reloc_section (asection *sec, |
14163 | bfd *dynobj, | |
14164 | unsigned int alignment, | |
14165 | bfd *abfd, | |
14166 | bfd_boolean is_rela) | |
83bac4b0 NC |
14167 | { |
14168 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
14169 | ||
14170 | if (reloc_sec == NULL) | |
14171 | { | |
14172 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
14173 | ||
14174 | if (name == NULL) | |
14175 | return NULL; | |
14176 | ||
3d4d4302 | 14177 | reloc_sec = bfd_get_linker_section (dynobj, name); |
83bac4b0 NC |
14178 | |
14179 | if (reloc_sec == NULL) | |
14180 | { | |
3d4d4302 AM |
14181 | flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY |
14182 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
83bac4b0 NC |
14183 | if ((sec->flags & SEC_ALLOC) != 0) |
14184 | flags |= SEC_ALLOC | SEC_LOAD; | |
14185 | ||
3d4d4302 | 14186 | reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); |
83bac4b0 NC |
14187 | if (reloc_sec != NULL) |
14188 | { | |
8877b5e5 AM |
14189 | /* _bfd_elf_get_sec_type_attr chooses a section type by |
14190 | name. Override as it may be wrong, eg. for a user | |
14191 | section named "auto" we'll get ".relauto" which is | |
14192 | seen to be a .rela section. */ | |
14193 | elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; | |
83bac4b0 NC |
14194 | if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) |
14195 | reloc_sec = NULL; | |
14196 | } | |
14197 | } | |
14198 | ||
14199 | elf_section_data (sec)->sreloc = reloc_sec; | |
14200 | } | |
14201 | ||
14202 | return reloc_sec; | |
14203 | } | |
1338dd10 | 14204 | |
bffebb6b AM |
14205 | /* Copy the ELF symbol type and other attributes for a linker script |
14206 | assignment from HSRC to HDEST. Generally this should be treated as | |
14207 | if we found a strong non-dynamic definition for HDEST (except that | |
14208 | ld ignores multiple definition errors). */ | |
1338dd10 | 14209 | void |
bffebb6b AM |
14210 | _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, |
14211 | struct bfd_link_hash_entry *hdest, | |
14212 | struct bfd_link_hash_entry *hsrc) | |
1338dd10 | 14213 | { |
bffebb6b AM |
14214 | struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; |
14215 | struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; | |
14216 | Elf_Internal_Sym isym; | |
1338dd10 PB |
14217 | |
14218 | ehdest->type = ehsrc->type; | |
35fc36a8 | 14219 | ehdest->target_internal = ehsrc->target_internal; |
bffebb6b AM |
14220 | |
14221 | isym.st_other = ehsrc->other; | |
b8417128 | 14222 | elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE); |
1338dd10 | 14223 | } |
351f65ca L |
14224 | |
14225 | /* Append a RELA relocation REL to section S in BFD. */ | |
14226 | ||
14227 | void | |
14228 | elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14229 | { | |
14230 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14231 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); | |
14232 | BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); | |
14233 | bed->s->swap_reloca_out (abfd, rel, loc); | |
14234 | } | |
14235 | ||
14236 | /* Append a REL relocation REL to section S in BFD. */ | |
14237 | ||
14238 | void | |
14239 | elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14240 | { | |
14241 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14242 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); | |
14243 | BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); | |
59d6ffb2 | 14244 | bed->s->swap_reloc_out (abfd, rel, loc); |
351f65ca | 14245 | } |
7dba9362 AM |
14246 | |
14247 | /* Define __start, __stop, .startof. or .sizeof. symbol. */ | |
14248 | ||
14249 | struct bfd_link_hash_entry * | |
14250 | bfd_elf_define_start_stop (struct bfd_link_info *info, | |
14251 | const char *symbol, asection *sec) | |
14252 | { | |
487b6440 | 14253 | struct elf_link_hash_entry *h; |
7dba9362 | 14254 | |
487b6440 AM |
14255 | h = elf_link_hash_lookup (elf_hash_table (info), symbol, |
14256 | FALSE, FALSE, TRUE); | |
14257 | if (h != NULL | |
14258 | && (h->root.type == bfd_link_hash_undefined | |
14259 | || h->root.type == bfd_link_hash_undefweak | |
14260 | || (h->ref_regular && !h->def_regular))) | |
7dba9362 | 14261 | { |
487b6440 AM |
14262 | h->root.type = bfd_link_hash_defined; |
14263 | h->root.u.def.section = sec; | |
14264 | h->root.u.def.value = 0; | |
14265 | h->def_regular = 1; | |
14266 | h->def_dynamic = 0; | |
14267 | h->start_stop = 1; | |
14268 | h->u2.start_stop_section = sec; | |
14269 | if (symbol[0] == '.') | |
14270 | { | |
14271 | /* .startof. and .sizeof. symbols are local. */ | |
559192d8 AM |
14272 | const struct elf_backend_data *bed; |
14273 | bed = get_elf_backend_data (info->output_bfd); | |
14274 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
487b6440 AM |
14275 | } |
14276 | else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
14277 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED; | |
14278 | return &h->root; | |
7dba9362 | 14279 | } |
487b6440 | 14280 | return NULL; |
7dba9362 | 14281 | } |