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)) | |
07d6d2b8 | 80 | return h->root.u.def.section; |
2f0c68f2 CM |
81 | else |
82 | return NULL; | |
83 | } | |
84 | else | |
85 | { | |
86 | /* It's not a relocation against a global symbol, | |
87 | but it could be a relocation against a local | |
88 | symbol for a discarded section. */ | |
89 | asection *isec; | |
90 | Elf_Internal_Sym *isym; | |
91 | ||
92 | /* Need to: get the symbol; get the section. */ | |
93 | isym = &cookie->locsyms[r_symndx]; | |
94 | isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx); | |
95 | if (isec != NULL | |
96 | && discard ? discarded_section (isec) : 1) | |
97 | return isec; | |
98 | } | |
99 | return NULL; | |
100 | } | |
101 | ||
d98685ac AM |
102 | /* Define a symbol in a dynamic linkage section. */ |
103 | ||
104 | struct elf_link_hash_entry * | |
105 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
106 | struct bfd_link_info *info, | |
107 | asection *sec, | |
108 | const char *name) | |
109 | { | |
110 | struct elf_link_hash_entry *h; | |
111 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 112 | const struct elf_backend_data *bed; |
d98685ac AM |
113 | |
114 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
115 | if (h != NULL) | |
116 | { | |
117 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
118 | This is a symptom of a larger problem: Absolute symbols | |
119 | defined in shared libraries can't be overridden, because we | |
120 | lose the link to the bfd which is via the symbol section. */ | |
121 | h->root.type = bfd_link_hash_new; | |
ad32986f | 122 | bh = &h->root; |
d98685ac | 123 | } |
ad32986f NC |
124 | else |
125 | bh = NULL; | |
d98685ac | 126 | |
cf18fda4 | 127 | bed = get_elf_backend_data (abfd); |
d98685ac | 128 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, |
cf18fda4 | 129 | sec, 0, NULL, FALSE, bed->collect, |
d98685ac AM |
130 | &bh)) |
131 | return NULL; | |
132 | h = (struct elf_link_hash_entry *) bh; | |
ad32986f | 133 | BFD_ASSERT (h != NULL); |
d98685ac | 134 | h->def_regular = 1; |
e28df02b | 135 | h->non_elf = 0; |
12b2843a | 136 | h->root.linker_def = 1; |
d98685ac | 137 | h->type = STT_OBJECT; |
00b7642b AM |
138 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
139 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
d98685ac | 140 | |
ccabcbe5 | 141 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
d98685ac AM |
142 | return h; |
143 | } | |
144 | ||
b34976b6 | 145 | bfd_boolean |
268b6b39 | 146 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
147 | { |
148 | flagword flags; | |
aad5d350 | 149 | asection *s; |
252b5132 | 150 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 151 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 152 | struct elf_link_hash_table *htab = elf_hash_table (info); |
252b5132 RH |
153 | |
154 | /* This function may be called more than once. */ | |
ce558b89 | 155 | if (htab->sgot != NULL) |
b34976b6 | 156 | return TRUE; |
252b5132 | 157 | |
e5a52504 | 158 | flags = bed->dynamic_sec_flags; |
252b5132 | 159 | |
14b2f831 AM |
160 | s = bfd_make_section_anyway_with_flags (abfd, |
161 | (bed->rela_plts_and_copies_p | |
162 | ? ".rela.got" : ".rel.got"), | |
163 | (bed->dynamic_sec_flags | |
164 | | SEC_READONLY)); | |
6de2ae4a L |
165 | if (s == NULL |
166 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
167 | return FALSE; | |
168 | htab->srelgot = s; | |
252b5132 | 169 | |
14b2f831 | 170 | s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); |
64e77c6d L |
171 | if (s == NULL |
172 | || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
173 | return FALSE; | |
174 | htab->sgot = s; | |
175 | ||
252b5132 RH |
176 | if (bed->want_got_plt) |
177 | { | |
14b2f831 | 178 | s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); |
252b5132 | 179 | if (s == NULL |
6de2ae4a L |
180 | || !bfd_set_section_alignment (abfd, s, |
181 | bed->s->log_file_align)) | |
b34976b6 | 182 | return FALSE; |
6de2ae4a | 183 | htab->sgotplt = s; |
252b5132 RH |
184 | } |
185 | ||
64e77c6d L |
186 | /* The first bit of the global offset table is the header. */ |
187 | s->size += bed->got_header_size; | |
188 | ||
2517a57f AM |
189 | if (bed->want_got_sym) |
190 | { | |
191 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
192 | (or .got.plt) section. We don't do this in the linker script | |
193 | because we don't want to define the symbol if we are not creating | |
194 | a global offset table. */ | |
6de2ae4a L |
195 | h = _bfd_elf_define_linkage_sym (abfd, info, s, |
196 | "_GLOBAL_OFFSET_TABLE_"); | |
2517a57f | 197 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
198 | if (h == NULL) |
199 | return FALSE; | |
2517a57f | 200 | } |
252b5132 | 201 | |
b34976b6 | 202 | return TRUE; |
252b5132 RH |
203 | } |
204 | \f | |
7e9f0867 AM |
205 | /* Create a strtab to hold the dynamic symbol names. */ |
206 | static bfd_boolean | |
207 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
208 | { | |
209 | struct elf_link_hash_table *hash_table; | |
210 | ||
211 | hash_table = elf_hash_table (info); | |
212 | if (hash_table->dynobj == NULL) | |
6cd255ca L |
213 | { |
214 | /* We may not set dynobj, an input file holding linker created | |
215 | dynamic sections to abfd, which may be a dynamic object with | |
216 | its own dynamic sections. We need to find a normal input file | |
217 | to hold linker created sections if possible. */ | |
218 | if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0) | |
219 | { | |
220 | bfd *ibfd; | |
57963c05 | 221 | asection *s; |
6cd255ca | 222 | for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next) |
6645479e | 223 | if ((ibfd->flags |
57963c05 AM |
224 | & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0 |
225 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
226 | && !((s = ibfd->sections) != NULL | |
227 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)) | |
6cd255ca L |
228 | { |
229 | abfd = ibfd; | |
230 | break; | |
231 | } | |
232 | } | |
233 | hash_table->dynobj = abfd; | |
234 | } | |
7e9f0867 AM |
235 | |
236 | if (hash_table->dynstr == NULL) | |
237 | { | |
238 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
239 | if (hash_table->dynstr == NULL) | |
240 | return FALSE; | |
241 | } | |
242 | return TRUE; | |
243 | } | |
244 | ||
45d6a902 AM |
245 | /* Create some sections which will be filled in with dynamic linking |
246 | information. ABFD is an input file which requires dynamic sections | |
247 | to be created. The dynamic sections take up virtual memory space | |
248 | when the final executable is run, so we need to create them before | |
249 | addresses are assigned to the output sections. We work out the | |
250 | actual contents and size of these sections later. */ | |
252b5132 | 251 | |
b34976b6 | 252 | bfd_boolean |
268b6b39 | 253 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 254 | { |
45d6a902 | 255 | flagword flags; |
91d6fa6a | 256 | asection *s; |
9c5bfbb7 | 257 | const struct elf_backend_data *bed; |
9637f6ef | 258 | struct elf_link_hash_entry *h; |
252b5132 | 259 | |
0eddce27 | 260 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
261 | return FALSE; |
262 | ||
263 | if (elf_hash_table (info)->dynamic_sections_created) | |
264 | return TRUE; | |
265 | ||
7e9f0867 AM |
266 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
267 | return FALSE; | |
45d6a902 | 268 | |
7e9f0867 | 269 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
270 | bed = get_elf_backend_data (abfd); |
271 | ||
272 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
273 | |
274 | /* A dynamically linked executable has a .interp section, but a | |
275 | shared library does not. */ | |
9b8b325a | 276 | if (bfd_link_executable (info) && !info->nointerp) |
252b5132 | 277 | { |
14b2f831 AM |
278 | s = bfd_make_section_anyway_with_flags (abfd, ".interp", |
279 | flags | SEC_READONLY); | |
3496cb2a | 280 | if (s == NULL) |
45d6a902 AM |
281 | return FALSE; |
282 | } | |
bb0deeff | 283 | |
45d6a902 AM |
284 | /* Create sections to hold version informations. These are removed |
285 | if they are not needed. */ | |
14b2f831 AM |
286 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d", |
287 | flags | SEC_READONLY); | |
45d6a902 | 288 | if (s == NULL |
45d6a902 AM |
289 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
290 | return FALSE; | |
291 | ||
14b2f831 AM |
292 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version", |
293 | flags | SEC_READONLY); | |
45d6a902 | 294 | if (s == NULL |
45d6a902 AM |
295 | || ! bfd_set_section_alignment (abfd, s, 1)) |
296 | return FALSE; | |
297 | ||
14b2f831 AM |
298 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r", |
299 | flags | SEC_READONLY); | |
45d6a902 | 300 | if (s == NULL |
45d6a902 AM |
301 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
302 | return FALSE; | |
303 | ||
14b2f831 AM |
304 | s = bfd_make_section_anyway_with_flags (abfd, ".dynsym", |
305 | flags | SEC_READONLY); | |
45d6a902 | 306 | if (s == NULL |
45d6a902 AM |
307 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
308 | return FALSE; | |
cae1fbbb | 309 | elf_hash_table (info)->dynsym = s; |
45d6a902 | 310 | |
14b2f831 AM |
311 | s = bfd_make_section_anyway_with_flags (abfd, ".dynstr", |
312 | flags | SEC_READONLY); | |
3496cb2a | 313 | if (s == NULL) |
45d6a902 AM |
314 | return FALSE; |
315 | ||
14b2f831 | 316 | s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 317 | if (s == NULL |
45d6a902 AM |
318 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
319 | return FALSE; | |
320 | ||
321 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
322 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
323 | only want to define it if we are, in fact, creating a .dynamic | |
324 | section. We don't want to define it if there is no .dynamic | |
325 | section, since on some ELF platforms the start up code examines it | |
326 | to decide how to initialize the process. */ | |
9637f6ef L |
327 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"); |
328 | elf_hash_table (info)->hdynamic = h; | |
329 | if (h == NULL) | |
45d6a902 AM |
330 | return FALSE; |
331 | ||
fdc90cb4 JJ |
332 | if (info->emit_hash) |
333 | { | |
14b2f831 AM |
334 | s = bfd_make_section_anyway_with_flags (abfd, ".hash", |
335 | flags | SEC_READONLY); | |
fdc90cb4 JJ |
336 | if (s == NULL |
337 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
338 | return FALSE; | |
339 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
340 | } | |
341 | ||
342 | if (info->emit_gnu_hash) | |
343 | { | |
14b2f831 AM |
344 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash", |
345 | flags | SEC_READONLY); | |
fdc90cb4 JJ |
346 | if (s == NULL |
347 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
348 | return FALSE; | |
349 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: | |
350 | 4 32-bit words followed by variable count of 64-bit words, then | |
351 | variable count of 32-bit words. */ | |
352 | if (bed->s->arch_size == 64) | |
353 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
354 | else | |
355 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
356 | } | |
45d6a902 AM |
357 | |
358 | /* Let the backend create the rest of the sections. This lets the | |
359 | backend set the right flags. The backend will normally create | |
360 | the .got and .plt sections. */ | |
894891db NC |
361 | if (bed->elf_backend_create_dynamic_sections == NULL |
362 | || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
45d6a902 AM |
363 | return FALSE; |
364 | ||
365 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
366 | ||
367 | return TRUE; | |
368 | } | |
369 | ||
370 | /* Create dynamic sections when linking against a dynamic object. */ | |
371 | ||
372 | bfd_boolean | |
268b6b39 | 373 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
374 | { |
375 | flagword flags, pltflags; | |
7325306f | 376 | struct elf_link_hash_entry *h; |
45d6a902 | 377 | asection *s; |
9c5bfbb7 | 378 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 379 | struct elf_link_hash_table *htab = elf_hash_table (info); |
45d6a902 | 380 | |
252b5132 RH |
381 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
382 | .rel[a].bss sections. */ | |
e5a52504 | 383 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
384 | |
385 | pltflags = flags; | |
252b5132 | 386 | if (bed->plt_not_loaded) |
6df4d94c MM |
387 | /* We do not clear SEC_ALLOC here because we still want the OS to |
388 | allocate space for the section; it's just that there's nothing | |
389 | to read in from the object file. */ | |
5d1634d7 | 390 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
391 | else |
392 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
393 | if (bed->plt_readonly) |
394 | pltflags |= SEC_READONLY; | |
395 | ||
14b2f831 | 396 | s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags); |
252b5132 | 397 | if (s == NULL |
252b5132 | 398 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 399 | return FALSE; |
6de2ae4a | 400 | htab->splt = s; |
252b5132 | 401 | |
d98685ac AM |
402 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
403 | .plt section. */ | |
7325306f RS |
404 | if (bed->want_plt_sym) |
405 | { | |
406 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
407 | "_PROCEDURE_LINKAGE_TABLE_"); | |
408 | elf_hash_table (info)->hplt = h; | |
409 | if (h == NULL) | |
410 | return FALSE; | |
411 | } | |
252b5132 | 412 | |
14b2f831 AM |
413 | s = bfd_make_section_anyway_with_flags (abfd, |
414 | (bed->rela_plts_and_copies_p | |
415 | ? ".rela.plt" : ".rel.plt"), | |
416 | flags | SEC_READONLY); | |
252b5132 | 417 | if (s == NULL |
45d6a902 | 418 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 419 | return FALSE; |
6de2ae4a | 420 | htab->srelplt = s; |
252b5132 RH |
421 | |
422 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 423 | return FALSE; |
252b5132 | 424 | |
3018b441 RH |
425 | if (bed->want_dynbss) |
426 | { | |
427 | /* The .dynbss section is a place to put symbols which are defined | |
428 | by dynamic objects, are referenced by regular objects, and are | |
429 | not functions. We must allocate space for them in the process | |
430 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
431 | initialize them at run time. The linker script puts the .dynbss | |
432 | section into the .bss section of the final image. */ | |
14b2f831 | 433 | s = bfd_make_section_anyway_with_flags (abfd, ".dynbss", |
afbf7e8e | 434 | SEC_ALLOC | SEC_LINKER_CREATED); |
3496cb2a | 435 | if (s == NULL) |
b34976b6 | 436 | return FALSE; |
9d19e4fd | 437 | htab->sdynbss = s; |
252b5132 | 438 | |
5474d94f AM |
439 | if (bed->want_dynrelro) |
440 | { | |
441 | /* Similarly, but for symbols that were originally in read-only | |
afbf7e8e AM |
442 | sections. This section doesn't really need to have contents, |
443 | but make it like other .data.rel.ro sections. */ | |
5474d94f | 444 | s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro", |
afbf7e8e | 445 | flags); |
5474d94f AM |
446 | if (s == NULL) |
447 | return FALSE; | |
448 | htab->sdynrelro = s; | |
449 | } | |
450 | ||
3018b441 | 451 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
452 | normally needed. We need to create it here, though, so that the |
453 | linker will map it to an output section. We can't just create it | |
454 | only if we need it, because we will not know whether we need it | |
455 | until we have seen all the input files, and the first time the | |
456 | main linker code calls BFD after examining all the input files | |
457 | (size_dynamic_sections) the input sections have already been | |
458 | mapped to the output sections. If the section turns out not to | |
459 | be needed, we can discard it later. We will never need this | |
460 | section when generating a shared object, since they do not use | |
461 | copy relocs. */ | |
9d19e4fd | 462 | if (bfd_link_executable (info)) |
3018b441 | 463 | { |
14b2f831 AM |
464 | s = bfd_make_section_anyway_with_flags (abfd, |
465 | (bed->rela_plts_and_copies_p | |
466 | ? ".rela.bss" : ".rel.bss"), | |
467 | flags | SEC_READONLY); | |
3018b441 | 468 | if (s == NULL |
45d6a902 | 469 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 470 | return FALSE; |
9d19e4fd | 471 | htab->srelbss = s; |
5474d94f AM |
472 | |
473 | if (bed->want_dynrelro) | |
474 | { | |
475 | s = (bfd_make_section_anyway_with_flags | |
476 | (abfd, (bed->rela_plts_and_copies_p | |
477 | ? ".rela.data.rel.ro" : ".rel.data.rel.ro"), | |
478 | flags | SEC_READONLY)); | |
479 | if (s == NULL | |
480 | || ! bfd_set_section_alignment (abfd, s, | |
481 | bed->s->log_file_align)) | |
482 | return FALSE; | |
483 | htab->sreldynrelro = s; | |
484 | } | |
3018b441 | 485 | } |
252b5132 RH |
486 | } |
487 | ||
b34976b6 | 488 | return TRUE; |
252b5132 RH |
489 | } |
490 | \f | |
252b5132 RH |
491 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
492 | read the input files, since we need to have a list of all of them | |
493 | before we can determine the final sizes of the output sections. | |
494 | Note that we may actually call this function even though we are not | |
495 | going to output any dynamic symbols; in some cases we know that a | |
496 | symbol should be in the dynamic symbol table, but only if there is | |
497 | one. */ | |
498 | ||
b34976b6 | 499 | bfd_boolean |
c152c796 AM |
500 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
501 | struct elf_link_hash_entry *h) | |
252b5132 RH |
502 | { |
503 | if (h->dynindx == -1) | |
504 | { | |
2b0f7ef9 | 505 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 506 | char *p; |
252b5132 | 507 | const char *name; |
ef53be89 | 508 | size_t indx; |
252b5132 | 509 | |
7a13edea NC |
510 | /* XXX: The ABI draft says the linker must turn hidden and |
511 | internal symbols into STB_LOCAL symbols when producing the | |
512 | DSO. However, if ld.so honors st_other in the dynamic table, | |
513 | this would not be necessary. */ | |
514 | switch (ELF_ST_VISIBILITY (h->other)) | |
515 | { | |
516 | case STV_INTERNAL: | |
517 | case STV_HIDDEN: | |
9d6eee78 L |
518 | if (h->root.type != bfd_link_hash_undefined |
519 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 520 | { |
f5385ebf | 521 | h->forced_local = 1; |
67687978 PB |
522 | if (!elf_hash_table (info)->is_relocatable_executable) |
523 | return TRUE; | |
7a13edea | 524 | } |
0444bdd4 | 525 | |
7a13edea NC |
526 | default: |
527 | break; | |
528 | } | |
529 | ||
252b5132 RH |
530 | h->dynindx = elf_hash_table (info)->dynsymcount; |
531 | ++elf_hash_table (info)->dynsymcount; | |
532 | ||
533 | dynstr = elf_hash_table (info)->dynstr; | |
534 | if (dynstr == NULL) | |
535 | { | |
536 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 537 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 538 | if (dynstr == NULL) |
b34976b6 | 539 | return FALSE; |
252b5132 RH |
540 | } |
541 | ||
542 | /* We don't put any version information in the dynamic string | |
aad5d350 | 543 | table. */ |
252b5132 RH |
544 | name = h->root.root.string; |
545 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
546 | if (p != NULL) |
547 | /* We know that the p points into writable memory. In fact, | |
548 | there are only a few symbols that have read-only names, being | |
549 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
550 | by the backends. Most symbols will have names pointing into | |
551 | an ELF string table read from a file, or to objalloc memory. */ | |
552 | *p = 0; | |
553 | ||
554 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
555 | ||
556 | if (p != NULL) | |
557 | *p = ELF_VER_CHR; | |
252b5132 | 558 | |
ef53be89 | 559 | if (indx == (size_t) -1) |
b34976b6 | 560 | return FALSE; |
252b5132 RH |
561 | h->dynstr_index = indx; |
562 | } | |
563 | ||
b34976b6 | 564 | return TRUE; |
252b5132 | 565 | } |
45d6a902 | 566 | \f |
55255dae L |
567 | /* Mark a symbol dynamic. */ |
568 | ||
28caa186 | 569 | static void |
55255dae | 570 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, |
40b36307 L |
571 | struct elf_link_hash_entry *h, |
572 | Elf_Internal_Sym *sym) | |
55255dae | 573 | { |
40b36307 | 574 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
55255dae | 575 | |
40b36307 | 576 | /* It may be called more than once on the same H. */ |
0e1862bb | 577 | if(h->dynamic || bfd_link_relocatable (info)) |
55255dae L |
578 | return; |
579 | ||
40b36307 L |
580 | if ((info->dynamic_data |
581 | && (h->type == STT_OBJECT | |
b8871f35 | 582 | || h->type == STT_COMMON |
40b36307 | 583 | || (sym != NULL |
b8871f35 L |
584 | && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT |
585 | || ELF_ST_TYPE (sym->st_info) == STT_COMMON)))) | |
a0c8462f | 586 | || (d != NULL |
73ec947d | 587 | && h->non_elf |
40b36307 | 588 | && (*d->match) (&d->head, NULL, h->root.root.string))) |
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 | |
63f452a8 AM |
920 | symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set. |
921 | (This prevents the early call before elf_backend_init_index_section | |
922 | and strip_excluded_output_sections setting dynindx for sections | |
923 | that are stripped.) */ | |
30b30c21 | 924 | |
554220db AM |
925 | static unsigned long |
926 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
927 | struct bfd_link_info *info, | |
928 | unsigned long *section_sym_count) | |
30b30c21 RH |
929 | { |
930 | unsigned long dynsymcount = 0; | |
63f452a8 | 931 | bfd_boolean do_sec = section_sym_count != NULL; |
30b30c21 | 932 | |
0e1862bb L |
933 | if (bfd_link_pic (info) |
934 | || elf_hash_table (info)->is_relocatable_executable) | |
30b30c21 | 935 | { |
aee6f5b4 | 936 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
937 | asection *p; |
938 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 939 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
940 | && (p->flags & SEC_ALLOC) != 0 |
941 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
63f452a8 AM |
942 | { |
943 | ++dynsymcount; | |
944 | if (do_sec) | |
945 | elf_section_data (p)->dynindx = dynsymcount; | |
946 | } | |
947 | else if (do_sec) | |
74541ad4 | 948 | elf_section_data (p)->dynindx = 0; |
30b30c21 | 949 | } |
63f452a8 AM |
950 | if (do_sec) |
951 | *section_sym_count = dynsymcount; | |
30b30c21 | 952 | |
6fa3860b PB |
953 | elf_link_hash_traverse (elf_hash_table (info), |
954 | elf_link_renumber_local_hash_table_dynsyms, | |
955 | &dynsymcount); | |
956 | ||
30b30c21 RH |
957 | if (elf_hash_table (info)->dynlocal) |
958 | { | |
959 | struct elf_link_local_dynamic_entry *p; | |
960 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
961 | p->dynindx = ++dynsymcount; | |
962 | } | |
90ac2420 | 963 | elf_hash_table (info)->local_dynsymcount = dynsymcount; |
30b30c21 RH |
964 | |
965 | elf_link_hash_traverse (elf_hash_table (info), | |
966 | elf_link_renumber_hash_table_dynsyms, | |
967 | &dynsymcount); | |
968 | ||
d5486c43 L |
969 | /* There is an unused NULL entry at the head of the table which we |
970 | must account for in our count even if the table is empty since it | |
971 | is intended for the mandatory DT_SYMTAB tag (.dynsym section) in | |
972 | .dynamic section. */ | |
973 | dynsymcount++; | |
30b30c21 | 974 | |
ccabcbe5 AM |
975 | elf_hash_table (info)->dynsymcount = dynsymcount; |
976 | return dynsymcount; | |
30b30c21 | 977 | } |
252b5132 | 978 | |
54ac0771 L |
979 | /* Merge st_other field. */ |
980 | ||
981 | static void | |
982 | elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, | |
b8417128 | 983 | const Elf_Internal_Sym *isym, asection *sec, |
cd3416da | 984 | bfd_boolean definition, bfd_boolean dynamic) |
54ac0771 L |
985 | { |
986 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
987 | ||
988 | /* If st_other has a processor-specific meaning, specific | |
cd3416da | 989 | code might be needed here. */ |
54ac0771 L |
990 | if (bed->elf_backend_merge_symbol_attribute) |
991 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
992 | dynamic); | |
993 | ||
cd3416da | 994 | if (!dynamic) |
54ac0771 | 995 | { |
cd3416da AM |
996 | unsigned symvis = ELF_ST_VISIBILITY (isym->st_other); |
997 | unsigned hvis = ELF_ST_VISIBILITY (h->other); | |
54ac0771 | 998 | |
cd3416da AM |
999 | /* Keep the most constraining visibility. Leave the remainder |
1000 | of the st_other field to elf_backend_merge_symbol_attribute. */ | |
1001 | if (symvis - 1 < hvis - 1) | |
1002 | h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); | |
54ac0771 | 1003 | } |
b8417128 AM |
1004 | else if (definition |
1005 | && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT | |
1006 | && (sec->flags & SEC_READONLY) == 0) | |
6cabe1ea | 1007 | h->protected_def = 1; |
54ac0771 L |
1008 | } |
1009 | ||
4f3fedcf AM |
1010 | /* This function is called when we want to merge a new symbol with an |
1011 | existing symbol. It handles the various cases which arise when we | |
1012 | find a definition in a dynamic object, or when there is already a | |
1013 | definition in a dynamic object. The new symbol is described by | |
1014 | NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table | |
1015 | entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK | |
1016 | if the old symbol was weak. We set POLD_ALIGNMENT to the alignment | |
1017 | of an old common symbol. We set OVERRIDE if the old symbol is | |
1018 | overriding a new definition. We set TYPE_CHANGE_OK if it is OK for | |
1019 | the type to change. We set SIZE_CHANGE_OK if it is OK for the size | |
1020 | to change. By OK to change, we mean that we shouldn't warn if the | |
1021 | type or size does change. */ | |
45d6a902 | 1022 | |
8a56bd02 | 1023 | static bfd_boolean |
268b6b39 AM |
1024 | _bfd_elf_merge_symbol (bfd *abfd, |
1025 | struct bfd_link_info *info, | |
1026 | const char *name, | |
1027 | Elf_Internal_Sym *sym, | |
1028 | asection **psec, | |
1029 | bfd_vma *pvalue, | |
4f3fedcf AM |
1030 | struct elf_link_hash_entry **sym_hash, |
1031 | bfd **poldbfd, | |
37a9e49a | 1032 | bfd_boolean *pold_weak, |
af44c138 | 1033 | unsigned int *pold_alignment, |
268b6b39 AM |
1034 | bfd_boolean *skip, |
1035 | bfd_boolean *override, | |
1036 | bfd_boolean *type_change_ok, | |
6e33951e L |
1037 | bfd_boolean *size_change_ok, |
1038 | bfd_boolean *matched) | |
252b5132 | 1039 | { |
7479dfd4 | 1040 | asection *sec, *oldsec; |
45d6a902 | 1041 | struct elf_link_hash_entry *h; |
90c984fc | 1042 | struct elf_link_hash_entry *hi; |
45d6a902 AM |
1043 | struct elf_link_hash_entry *flip; |
1044 | int bind; | |
1045 | bfd *oldbfd; | |
1046 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
0a36a439 | 1047 | bfd_boolean newweak, oldweak, newfunc, oldfunc; |
a4d8e49b | 1048 | const struct elf_backend_data *bed; |
6e33951e | 1049 | char *new_version; |
93f4de39 | 1050 | bfd_boolean default_sym = *matched; |
45d6a902 AM |
1051 | |
1052 | *skip = FALSE; | |
1053 | *override = FALSE; | |
1054 | ||
1055 | sec = *psec; | |
1056 | bind = ELF_ST_BIND (sym->st_info); | |
1057 | ||
1058 | if (! bfd_is_und_section (sec)) | |
1059 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
1060 | else | |
1061 | h = ((struct elf_link_hash_entry *) | |
1062 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
1063 | if (h == NULL) | |
1064 | return FALSE; | |
1065 | *sym_hash = h; | |
252b5132 | 1066 | |
88ba32a0 L |
1067 | bed = get_elf_backend_data (abfd); |
1068 | ||
6e33951e | 1069 | /* NEW_VERSION is the symbol version of the new symbol. */ |
422f1182 | 1070 | if (h->versioned != unversioned) |
6e33951e | 1071 | { |
422f1182 L |
1072 | /* Symbol version is unknown or versioned. */ |
1073 | new_version = strrchr (name, ELF_VER_CHR); | |
1074 | if (new_version) | |
1075 | { | |
1076 | if (h->versioned == unknown) | |
1077 | { | |
1078 | if (new_version > name && new_version[-1] != ELF_VER_CHR) | |
1079 | h->versioned = versioned_hidden; | |
1080 | else | |
1081 | h->versioned = versioned; | |
1082 | } | |
1083 | new_version += 1; | |
1084 | if (new_version[0] == '\0') | |
1085 | new_version = NULL; | |
1086 | } | |
1087 | else | |
1088 | h->versioned = unversioned; | |
6e33951e | 1089 | } |
422f1182 L |
1090 | else |
1091 | new_version = NULL; | |
6e33951e | 1092 | |
90c984fc L |
1093 | /* For merging, we only care about real symbols. But we need to make |
1094 | sure that indirect symbol dynamic flags are updated. */ | |
1095 | hi = h; | |
45d6a902 AM |
1096 | while (h->root.type == bfd_link_hash_indirect |
1097 | || h->root.type == bfd_link_hash_warning) | |
1098 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1099 | ||
6e33951e L |
1100 | if (!*matched) |
1101 | { | |
1102 | if (hi == h || h->root.type == bfd_link_hash_new) | |
1103 | *matched = TRUE; | |
1104 | else | |
1105 | { | |
ae7683d2 | 1106 | /* OLD_HIDDEN is true if the existing symbol is only visible |
6e33951e | 1107 | to the symbol with the same symbol version. NEW_HIDDEN is |
ae7683d2 | 1108 | true if the new symbol is only visible to the symbol with |
6e33951e | 1109 | the same symbol version. */ |
422f1182 L |
1110 | bfd_boolean old_hidden = h->versioned == versioned_hidden; |
1111 | bfd_boolean new_hidden = hi->versioned == versioned_hidden; | |
6e33951e L |
1112 | if (!old_hidden && !new_hidden) |
1113 | /* The new symbol matches the existing symbol if both | |
1114 | aren't hidden. */ | |
1115 | *matched = TRUE; | |
1116 | else | |
1117 | { | |
1118 | /* OLD_VERSION is the symbol version of the existing | |
1119 | symbol. */ | |
422f1182 L |
1120 | char *old_version; |
1121 | ||
1122 | if (h->versioned >= versioned) | |
1123 | old_version = strrchr (h->root.root.string, | |
1124 | ELF_VER_CHR) + 1; | |
1125 | else | |
1126 | old_version = NULL; | |
6e33951e L |
1127 | |
1128 | /* The new symbol matches the existing symbol if they | |
1129 | have the same symbol version. */ | |
1130 | *matched = (old_version == new_version | |
1131 | || (old_version != NULL | |
1132 | && new_version != NULL | |
1133 | && strcmp (old_version, new_version) == 0)); | |
1134 | } | |
1135 | } | |
1136 | } | |
1137 | ||
934bce08 AM |
1138 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
1139 | existing symbol. */ | |
1140 | ||
1141 | oldbfd = NULL; | |
1142 | oldsec = NULL; | |
1143 | switch (h->root.type) | |
1144 | { | |
1145 | default: | |
1146 | break; | |
1147 | ||
1148 | case bfd_link_hash_undefined: | |
1149 | case bfd_link_hash_undefweak: | |
1150 | oldbfd = h->root.u.undef.abfd; | |
1151 | break; | |
1152 | ||
1153 | case bfd_link_hash_defined: | |
1154 | case bfd_link_hash_defweak: | |
1155 | oldbfd = h->root.u.def.section->owner; | |
1156 | oldsec = h->root.u.def.section; | |
1157 | break; | |
1158 | ||
1159 | case bfd_link_hash_common: | |
1160 | oldbfd = h->root.u.c.p->section->owner; | |
1161 | oldsec = h->root.u.c.p->section; | |
1162 | if (pold_alignment) | |
1163 | *pold_alignment = h->root.u.c.p->alignment_power; | |
1164 | break; | |
1165 | } | |
1166 | if (poldbfd && *poldbfd == NULL) | |
1167 | *poldbfd = oldbfd; | |
1168 | ||
1169 | /* Differentiate strong and weak symbols. */ | |
1170 | newweak = bind == STB_WEAK; | |
1171 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1172 | || h->root.type == bfd_link_hash_undefweak); | |
1173 | if (pold_weak) | |
1174 | *pold_weak = oldweak; | |
1175 | ||
40b36307 | 1176 | /* We have to check it for every instance since the first few may be |
ee659f1f | 1177 | references and not all compilers emit symbol type for undefined |
40b36307 L |
1178 | symbols. */ |
1179 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
1180 | ||
ee659f1f AM |
1181 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, |
1182 | respectively, is from a dynamic object. */ | |
1183 | ||
1184 | newdyn = (abfd->flags & DYNAMIC) != 0; | |
1185 | ||
1186 | /* ref_dynamic_nonweak and dynamic_def flags track actual undefined | |
1187 | syms and defined syms in dynamic libraries respectively. | |
1188 | ref_dynamic on the other hand can be set for a symbol defined in | |
1189 | a dynamic library, and def_dynamic may not be set; When the | |
1190 | definition in a dynamic lib is overridden by a definition in the | |
1191 | executable use of the symbol in the dynamic lib becomes a | |
1192 | reference to the executable symbol. */ | |
1193 | if (newdyn) | |
1194 | { | |
1195 | if (bfd_is_und_section (sec)) | |
1196 | { | |
1197 | if (bind != STB_WEAK) | |
1198 | { | |
1199 | h->ref_dynamic_nonweak = 1; | |
1200 | hi->ref_dynamic_nonweak = 1; | |
1201 | } | |
1202 | } | |
1203 | else | |
1204 | { | |
6e33951e L |
1205 | /* Update the existing symbol only if they match. */ |
1206 | if (*matched) | |
1207 | h->dynamic_def = 1; | |
ee659f1f AM |
1208 | hi->dynamic_def = 1; |
1209 | } | |
1210 | } | |
1211 | ||
45d6a902 AM |
1212 | /* If we just created the symbol, mark it as being an ELF symbol. |
1213 | Other than that, there is nothing to do--there is no merge issue | |
1214 | with a newly defined symbol--so we just return. */ | |
1215 | ||
1216 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 1217 | { |
f5385ebf | 1218 | h->non_elf = 0; |
45d6a902 AM |
1219 | return TRUE; |
1220 | } | |
252b5132 | 1221 | |
45d6a902 AM |
1222 | /* In cases involving weak versioned symbols, we may wind up trying |
1223 | to merge a symbol with itself. Catch that here, to avoid the | |
1224 | confusion that results if we try to override a symbol with | |
1225 | itself. The additional tests catch cases like | |
1226 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
1227 | dynamic object, which we do want to handle here. */ | |
1228 | if (abfd == oldbfd | |
895fa45f | 1229 | && (newweak || oldweak) |
45d6a902 | 1230 | && ((abfd->flags & DYNAMIC) == 0 |
f5385ebf | 1231 | || !h->def_regular)) |
45d6a902 AM |
1232 | return TRUE; |
1233 | ||
707bba77 | 1234 | olddyn = FALSE; |
45d6a902 AM |
1235 | if (oldbfd != NULL) |
1236 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 1237 | else if (oldsec != NULL) |
45d6a902 | 1238 | { |
707bba77 | 1239 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 1240 | indices used by MIPS ELF. */ |
707bba77 | 1241 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 1242 | } |
252b5132 | 1243 | |
1a3b5c34 AM |
1244 | /* Handle a case where plugin_notice won't be called and thus won't |
1245 | set the non_ir_ref flags on the first pass over symbols. */ | |
1246 | if (oldbfd != NULL | |
1247 | && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN) | |
1248 | && newdyn != olddyn) | |
1249 | { | |
1250 | h->root.non_ir_ref_dynamic = TRUE; | |
1251 | hi->root.non_ir_ref_dynamic = TRUE; | |
1252 | } | |
1253 | ||
45d6a902 AM |
1254 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
1255 | respectively, appear to be a definition rather than reference. */ | |
1256 | ||
707bba77 | 1257 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 1258 | |
707bba77 AM |
1259 | olddef = (h->root.type != bfd_link_hash_undefined |
1260 | && h->root.type != bfd_link_hash_undefweak | |
202ac193 | 1261 | && h->root.type != bfd_link_hash_common); |
45d6a902 | 1262 | |
0a36a439 L |
1263 | /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, |
1264 | respectively, appear to be a function. */ | |
1265 | ||
1266 | newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1267 | && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); | |
1268 | ||
1269 | oldfunc = (h->type != STT_NOTYPE | |
1270 | && bed->is_function_type (h->type)); | |
1271 | ||
c5d37467 | 1272 | if (!(newfunc && oldfunc) |
5b677558 AM |
1273 | && ELF_ST_TYPE (sym->st_info) != h->type |
1274 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1275 | && h->type != STT_NOTYPE | |
c5d37467 AM |
1276 | && (newdef || bfd_is_com_section (sec)) |
1277 | && (olddef || h->root.type == bfd_link_hash_common)) | |
580a2b6e | 1278 | { |
c5d37467 AM |
1279 | /* If creating a default indirect symbol ("foo" or "foo@") from |
1280 | a dynamic versioned definition ("foo@@") skip doing so if | |
1281 | there is an existing regular definition with a different | |
1282 | type. We don't want, for example, a "time" variable in the | |
1283 | executable overriding a "time" function in a shared library. */ | |
1284 | if (newdyn | |
1285 | && !olddyn) | |
1286 | { | |
1287 | *skip = TRUE; | |
1288 | return TRUE; | |
1289 | } | |
1290 | ||
1291 | /* When adding a symbol from a regular object file after we have | |
1292 | created indirect symbols, undo the indirection and any | |
1293 | dynamic state. */ | |
1294 | if (hi != h | |
1295 | && !newdyn | |
1296 | && olddyn) | |
1297 | { | |
1298 | h = hi; | |
1299 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1300 | h->forced_local = 0; | |
1301 | h->ref_dynamic = 0; | |
1302 | h->def_dynamic = 0; | |
1303 | h->dynamic_def = 0; | |
1304 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1305 | { | |
1306 | h->root.type = bfd_link_hash_undefined; | |
1307 | h->root.u.undef.abfd = abfd; | |
1308 | } | |
1309 | else | |
1310 | { | |
1311 | h->root.type = bfd_link_hash_new; | |
1312 | h->root.u.undef.abfd = NULL; | |
1313 | } | |
1314 | return TRUE; | |
1315 | } | |
580a2b6e L |
1316 | } |
1317 | ||
4c34aff8 AM |
1318 | /* Check TLS symbols. We don't check undefined symbols introduced |
1319 | by "ld -u" which have no type (and oldbfd NULL), and we don't | |
1320 | check symbols from plugins because they also have no type. */ | |
1321 | if (oldbfd != NULL | |
1322 | && (oldbfd->flags & BFD_PLUGIN) == 0 | |
1323 | && (abfd->flags & BFD_PLUGIN) == 0 | |
1324 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1325 | && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) | |
7479dfd4 L |
1326 | { |
1327 | bfd *ntbfd, *tbfd; | |
1328 | bfd_boolean ntdef, tdef; | |
1329 | asection *ntsec, *tsec; | |
1330 | ||
1331 | if (h->type == STT_TLS) | |
1332 | { | |
3b36f7e6 | 1333 | ntbfd = abfd; |
7479dfd4 L |
1334 | ntsec = sec; |
1335 | ntdef = newdef; | |
1336 | tbfd = oldbfd; | |
1337 | tsec = oldsec; | |
1338 | tdef = olddef; | |
1339 | } | |
1340 | else | |
1341 | { | |
1342 | ntbfd = oldbfd; | |
1343 | ntsec = oldsec; | |
1344 | ntdef = olddef; | |
1345 | tbfd = abfd; | |
1346 | tsec = sec; | |
1347 | tdef = newdef; | |
1348 | } | |
1349 | ||
1350 | if (tdef && ntdef) | |
4eca0228 | 1351 | _bfd_error_handler |
695344c0 | 1352 | /* xgettext:c-format */ |
191c0c42 AM |
1353 | (_("%s: TLS definition in %B section %A " |
1354 | "mismatches non-TLS definition in %B section %A"), | |
c08bb8dd | 1355 | h->root.root.string, tbfd, tsec, ntbfd, ntsec); |
7479dfd4 | 1356 | else if (!tdef && !ntdef) |
4eca0228 | 1357 | _bfd_error_handler |
695344c0 | 1358 | /* xgettext:c-format */ |
191c0c42 AM |
1359 | (_("%s: TLS reference in %B " |
1360 | "mismatches non-TLS reference in %B"), | |
c08bb8dd | 1361 | h->root.root.string, tbfd, ntbfd); |
7479dfd4 | 1362 | else if (tdef) |
4eca0228 | 1363 | _bfd_error_handler |
695344c0 | 1364 | /* xgettext:c-format */ |
191c0c42 AM |
1365 | (_("%s: TLS definition in %B section %A " |
1366 | "mismatches non-TLS reference in %B"), | |
c08bb8dd | 1367 | h->root.root.string, tbfd, tsec, ntbfd); |
7479dfd4 | 1368 | else |
4eca0228 | 1369 | _bfd_error_handler |
695344c0 | 1370 | /* xgettext:c-format */ |
191c0c42 AM |
1371 | (_("%s: TLS reference in %B " |
1372 | "mismatches non-TLS definition in %B section %A"), | |
c08bb8dd | 1373 | h->root.root.string, tbfd, ntbfd, ntsec); |
7479dfd4 L |
1374 | |
1375 | bfd_set_error (bfd_error_bad_value); | |
1376 | return FALSE; | |
1377 | } | |
1378 | ||
45d6a902 AM |
1379 | /* If the old symbol has non-default visibility, we ignore the new |
1380 | definition from a dynamic object. */ | |
1381 | if (newdyn | |
9c7a29a3 | 1382 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1383 | && !bfd_is_und_section (sec)) |
1384 | { | |
1385 | *skip = TRUE; | |
1386 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1387 | h->ref_dynamic = 1; |
90c984fc | 1388 | hi->ref_dynamic = 1; |
45d6a902 AM |
1389 | /* A protected symbol has external availability. Make sure it is |
1390 | recorded as dynamic. | |
1391 | ||
1392 | FIXME: Should we check type and size for protected symbol? */ | |
1393 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1394 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1395 | else |
1396 | return TRUE; | |
1397 | } | |
1398 | else if (!newdyn | |
9c7a29a3 | 1399 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1400 | && h->def_dynamic) |
45d6a902 AM |
1401 | { |
1402 | /* If the new symbol with non-default visibility comes from a | |
1403 | relocatable file and the old definition comes from a dynamic | |
1404 | object, we remove the old definition. */ | |
6c9b78e6 | 1405 | if (hi->root.type == bfd_link_hash_indirect) |
d2dee3b2 L |
1406 | { |
1407 | /* Handle the case where the old dynamic definition is | |
1408 | default versioned. We need to copy the symbol info from | |
1409 | the symbol with default version to the normal one if it | |
1410 | was referenced before. */ | |
1411 | if (h->ref_regular) | |
1412 | { | |
6c9b78e6 | 1413 | hi->root.type = h->root.type; |
d2dee3b2 | 1414 | h->root.type = bfd_link_hash_indirect; |
6c9b78e6 | 1415 | (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); |
aed81c4e | 1416 | |
6c9b78e6 | 1417 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; |
aed81c4e | 1418 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
d2dee3b2 | 1419 | { |
aed81c4e MR |
1420 | /* If the new symbol is hidden or internal, completely undo |
1421 | any dynamic link state. */ | |
1422 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1423 | h->forced_local = 0; | |
1424 | h->ref_dynamic = 0; | |
d2dee3b2 L |
1425 | } |
1426 | else | |
aed81c4e MR |
1427 | h->ref_dynamic = 1; |
1428 | ||
1429 | h->def_dynamic = 0; | |
aed81c4e MR |
1430 | /* FIXME: Should we check type and size for protected symbol? */ |
1431 | h->size = 0; | |
1432 | h->type = 0; | |
1433 | ||
6c9b78e6 | 1434 | h = hi; |
d2dee3b2 L |
1435 | } |
1436 | else | |
6c9b78e6 | 1437 | h = hi; |
d2dee3b2 | 1438 | } |
1de1a317 | 1439 | |
f5eda473 AM |
1440 | /* If the old symbol was undefined before, then it will still be |
1441 | on the undefs list. If the new symbol is undefined or | |
1442 | common, we can't make it bfd_link_hash_new here, because new | |
1443 | undefined or common symbols will be added to the undefs list | |
1444 | by _bfd_generic_link_add_one_symbol. Symbols may not be | |
1445 | added twice to the undefs list. Also, if the new symbol is | |
1446 | undefweak then we don't want to lose the strong undef. */ | |
1447 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1de1a317 | 1448 | { |
1de1a317 | 1449 | h->root.type = bfd_link_hash_undefined; |
1de1a317 L |
1450 | h->root.u.undef.abfd = abfd; |
1451 | } | |
1452 | else | |
1453 | { | |
1454 | h->root.type = bfd_link_hash_new; | |
1455 | h->root.u.undef.abfd = NULL; | |
1456 | } | |
1457 | ||
f5eda473 | 1458 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
252b5132 | 1459 | { |
f5eda473 AM |
1460 | /* If the new symbol is hidden or internal, completely undo |
1461 | any dynamic link state. */ | |
1462 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1463 | h->forced_local = 0; | |
1464 | h->ref_dynamic = 0; | |
45d6a902 | 1465 | } |
f5eda473 AM |
1466 | else |
1467 | h->ref_dynamic = 1; | |
1468 | h->def_dynamic = 0; | |
45d6a902 AM |
1469 | /* FIXME: Should we check type and size for protected symbol? */ |
1470 | h->size = 0; | |
1471 | h->type = 0; | |
1472 | return TRUE; | |
1473 | } | |
14a793b2 | 1474 | |
15b43f48 AM |
1475 | /* If a new weak symbol definition comes from a regular file and the |
1476 | old symbol comes from a dynamic library, we treat the new one as | |
1477 | strong. Similarly, an old weak symbol definition from a regular | |
1478 | file is treated as strong when the new symbol comes from a dynamic | |
1479 | library. Further, an old weak symbol from a dynamic library is | |
1480 | treated as strong if the new symbol is from a dynamic library. | |
1481 | This reflects the way glibc's ld.so works. | |
1482 | ||
165f707a AM |
1483 | Also allow a weak symbol to override a linker script symbol |
1484 | defined by an early pass over the script. This is done so the | |
1485 | linker knows the symbol is defined in an object file, for the | |
1486 | DEFINED script function. | |
1487 | ||
15b43f48 AM |
1488 | Do this before setting *type_change_ok or *size_change_ok so that |
1489 | we warn properly when dynamic library symbols are overridden. */ | |
1490 | ||
165f707a | 1491 | if (newdef && !newdyn && (olddyn || h->root.ldscript_def)) |
0f8a2703 | 1492 | newweak = FALSE; |
15b43f48 | 1493 | if (olddef && newdyn) |
0f8a2703 AM |
1494 | oldweak = FALSE; |
1495 | ||
d334575b | 1496 | /* Allow changes between different types of function symbol. */ |
0a36a439 | 1497 | if (newfunc && oldfunc) |
fcb93ecf PB |
1498 | *type_change_ok = TRUE; |
1499 | ||
79349b09 AM |
1500 | /* It's OK to change the type if either the existing symbol or the |
1501 | new symbol is weak. A type change is also OK if the old symbol | |
1502 | is undefined and the new symbol is defined. */ | |
252b5132 | 1503 | |
79349b09 AM |
1504 | if (oldweak |
1505 | || newweak | |
1506 | || (newdef | |
1507 | && h->root.type == bfd_link_hash_undefined)) | |
1508 | *type_change_ok = TRUE; | |
1509 | ||
1510 | /* It's OK to change the size if either the existing symbol or the | |
1511 | new symbol is weak, or if the old symbol is undefined. */ | |
1512 | ||
1513 | if (*type_change_ok | |
1514 | || h->root.type == bfd_link_hash_undefined) | |
1515 | *size_change_ok = TRUE; | |
45d6a902 | 1516 | |
45d6a902 AM |
1517 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1518 | symbol, respectively, appears to be a common symbol in a dynamic | |
1519 | object. If a symbol appears in an uninitialized section, and is | |
1520 | not weak, and is not a function, then it may be a common symbol | |
1521 | which was resolved when the dynamic object was created. We want | |
1522 | to treat such symbols specially, because they raise special | |
1523 | considerations when setting the symbol size: if the symbol | |
1524 | appears as a common symbol in a regular object, and the size in | |
1525 | the regular object is larger, we must make sure that we use the | |
1526 | larger size. This problematic case can always be avoided in C, | |
1527 | but it must be handled correctly when using Fortran shared | |
1528 | libraries. | |
1529 | ||
1530 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1531 | likewise for OLDDYNCOMMON and OLDDEF. | |
1532 | ||
1533 | Note that this test is just a heuristic, and that it is quite | |
1534 | possible to have an uninitialized symbol in a shared object which | |
1535 | is really a definition, rather than a common symbol. This could | |
1536 | lead to some minor confusion when the symbol really is a common | |
1537 | symbol in some regular object. However, I think it will be | |
1538 | harmless. */ | |
1539 | ||
1540 | if (newdyn | |
1541 | && newdef | |
79349b09 | 1542 | && !newweak |
45d6a902 AM |
1543 | && (sec->flags & SEC_ALLOC) != 0 |
1544 | && (sec->flags & SEC_LOAD) == 0 | |
1545 | && sym->st_size > 0 | |
0a36a439 | 1546 | && !newfunc) |
45d6a902 AM |
1547 | newdyncommon = TRUE; |
1548 | else | |
1549 | newdyncommon = FALSE; | |
1550 | ||
1551 | if (olddyn | |
1552 | && olddef | |
1553 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1554 | && h->def_dynamic |
45d6a902 AM |
1555 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1556 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1557 | && h->size > 0 | |
0a36a439 | 1558 | && !oldfunc) |
45d6a902 AM |
1559 | olddyncommon = TRUE; |
1560 | else | |
1561 | olddyncommon = FALSE; | |
1562 | ||
a4d8e49b L |
1563 | /* We now know everything about the old and new symbols. We ask the |
1564 | backend to check if we can merge them. */ | |
5d13b3b3 AM |
1565 | if (bed->merge_symbol != NULL) |
1566 | { | |
1567 | if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) | |
1568 | return FALSE; | |
1569 | sec = *psec; | |
1570 | } | |
a4d8e49b | 1571 | |
a83ef4d1 L |
1572 | /* There are multiple definitions of a normal symbol. Skip the |
1573 | default symbol as well as definition from an IR object. */ | |
93f4de39 | 1574 | if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak |
a83ef4d1 L |
1575 | && !default_sym && h->def_regular |
1576 | && !(oldbfd != NULL | |
1577 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
1578 | && (abfd->flags & BFD_PLUGIN) == 0)) | |
93f4de39 RL |
1579 | { |
1580 | /* Handle a multiple definition. */ | |
1581 | (*info->callbacks->multiple_definition) (info, &h->root, | |
1582 | abfd, sec, *pvalue); | |
1583 | *skip = TRUE; | |
1584 | return TRUE; | |
1585 | } | |
1586 | ||
45d6a902 AM |
1587 | /* If both the old and the new symbols look like common symbols in a |
1588 | dynamic object, set the size of the symbol to the larger of the | |
1589 | two. */ | |
1590 | ||
1591 | if (olddyncommon | |
1592 | && newdyncommon | |
1593 | && sym->st_size != h->size) | |
1594 | { | |
1595 | /* Since we think we have two common symbols, issue a multiple | |
1596 | common warning if desired. Note that we only warn if the | |
1597 | size is different. If the size is the same, we simply let | |
1598 | the old symbol override the new one as normally happens with | |
1599 | symbols defined in dynamic objects. */ | |
1600 | ||
1a72702b AM |
1601 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1602 | bfd_link_hash_common, sym->st_size); | |
45d6a902 AM |
1603 | if (sym->st_size > h->size) |
1604 | h->size = sym->st_size; | |
252b5132 | 1605 | |
45d6a902 | 1606 | *size_change_ok = TRUE; |
252b5132 RH |
1607 | } |
1608 | ||
45d6a902 AM |
1609 | /* If we are looking at a dynamic object, and we have found a |
1610 | definition, we need to see if the symbol was already defined by | |
1611 | some other object. If so, we want to use the existing | |
1612 | definition, and we do not want to report a multiple symbol | |
1613 | definition error; we do this by clobbering *PSEC to be | |
1614 | bfd_und_section_ptr. | |
1615 | ||
1616 | We treat a common symbol as a definition if the symbol in the | |
1617 | shared library is a function, since common symbols always | |
1618 | represent variables; this can cause confusion in principle, but | |
1619 | any such confusion would seem to indicate an erroneous program or | |
1620 | shared library. We also permit a common symbol in a regular | |
8170f769 | 1621 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1622 | |
1623 | if (newdyn | |
1624 | && newdef | |
77cfaee6 | 1625 | && (olddef |
45d6a902 | 1626 | || (h->root.type == bfd_link_hash_common |
8170f769 | 1627 | && (newweak || newfunc)))) |
45d6a902 AM |
1628 | { |
1629 | *override = TRUE; | |
1630 | newdef = FALSE; | |
1631 | newdyncommon = FALSE; | |
252b5132 | 1632 | |
45d6a902 AM |
1633 | *psec = sec = bfd_und_section_ptr; |
1634 | *size_change_ok = TRUE; | |
252b5132 | 1635 | |
45d6a902 AM |
1636 | /* If we get here when the old symbol is a common symbol, then |
1637 | we are explicitly letting it override a weak symbol or | |
1638 | function in a dynamic object, and we don't want to warn about | |
1639 | a type change. If the old symbol is a defined symbol, a type | |
1640 | change warning may still be appropriate. */ | |
252b5132 | 1641 | |
45d6a902 AM |
1642 | if (h->root.type == bfd_link_hash_common) |
1643 | *type_change_ok = TRUE; | |
1644 | } | |
1645 | ||
1646 | /* Handle the special case of an old common symbol merging with a | |
1647 | new symbol which looks like a common symbol in a shared object. | |
1648 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1649 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1650 | right thing. */ | |
45d6a902 AM |
1651 | |
1652 | if (newdyncommon | |
1653 | && h->root.type == bfd_link_hash_common) | |
1654 | { | |
1655 | *override = TRUE; | |
1656 | newdef = FALSE; | |
1657 | newdyncommon = FALSE; | |
1658 | *pvalue = sym->st_size; | |
a4d8e49b | 1659 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1660 | *size_change_ok = TRUE; |
1661 | } | |
1662 | ||
c5e2cead | 1663 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1664 | if (newdef && olddef && newweak) |
54ac0771 | 1665 | { |
35ed3f94 | 1666 | /* Don't skip new non-IR weak syms. */ |
3a5dbfb2 AM |
1667 | if (!(oldbfd != NULL |
1668 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
35ed3f94 | 1669 | && (abfd->flags & BFD_PLUGIN) == 0)) |
57fa7b8c AM |
1670 | { |
1671 | newdef = FALSE; | |
1672 | *skip = TRUE; | |
1673 | } | |
54ac0771 L |
1674 | |
1675 | /* Merge st_other. If the symbol already has a dynamic index, | |
1676 | but visibility says it should not be visible, turn it into a | |
1677 | local symbol. */ | |
b8417128 | 1678 | elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn); |
54ac0771 L |
1679 | if (h->dynindx != -1) |
1680 | switch (ELF_ST_VISIBILITY (h->other)) | |
1681 | { | |
1682 | case STV_INTERNAL: | |
1683 | case STV_HIDDEN: | |
1684 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1685 | break; | |
1686 | } | |
1687 | } | |
c5e2cead | 1688 | |
45d6a902 AM |
1689 | /* If the old symbol is from a dynamic object, and the new symbol is |
1690 | a definition which is not from a dynamic object, then the new | |
1691 | symbol overrides the old symbol. Symbols from regular files | |
1692 | always take precedence over symbols from dynamic objects, even if | |
1693 | they are defined after the dynamic object in the link. | |
1694 | ||
1695 | As above, we again permit a common symbol in a regular object to | |
1696 | override a definition in a shared object if the shared object | |
0f8a2703 | 1697 | symbol is a function or is weak. */ |
45d6a902 AM |
1698 | |
1699 | flip = NULL; | |
77cfaee6 | 1700 | if (!newdyn |
45d6a902 AM |
1701 | && (newdef |
1702 | || (bfd_is_com_section (sec) | |
0a36a439 | 1703 | && (oldweak || oldfunc))) |
45d6a902 AM |
1704 | && olddyn |
1705 | && olddef | |
f5385ebf | 1706 | && h->def_dynamic) |
45d6a902 AM |
1707 | { |
1708 | /* Change the hash table entry to undefined, and let | |
1709 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1710 | new definition. */ | |
1711 | ||
1712 | h->root.type = bfd_link_hash_undefined; | |
1713 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1714 | *size_change_ok = TRUE; | |
1715 | ||
1716 | olddef = FALSE; | |
1717 | olddyncommon = FALSE; | |
1718 | ||
1719 | /* We again permit a type change when a common symbol may be | |
1720 | overriding a function. */ | |
1721 | ||
1722 | if (bfd_is_com_section (sec)) | |
0a36a439 L |
1723 | { |
1724 | if (oldfunc) | |
1725 | { | |
1726 | /* If a common symbol overrides a function, make sure | |
1727 | that it isn't defined dynamically nor has type | |
1728 | function. */ | |
1729 | h->def_dynamic = 0; | |
1730 | h->type = STT_NOTYPE; | |
1731 | } | |
1732 | *type_change_ok = TRUE; | |
1733 | } | |
45d6a902 | 1734 | |
6c9b78e6 AM |
1735 | if (hi->root.type == bfd_link_hash_indirect) |
1736 | flip = hi; | |
45d6a902 AM |
1737 | else |
1738 | /* This union may have been set to be non-NULL when this symbol | |
1739 | was seen in a dynamic object. We must force the union to be | |
1740 | NULL, so that it is correct for a regular symbol. */ | |
1741 | h->verinfo.vertree = NULL; | |
1742 | } | |
1743 | ||
1744 | /* Handle the special case of a new common symbol merging with an | |
1745 | old symbol that looks like it might be a common symbol defined in | |
1746 | a shared object. Note that we have already handled the case in | |
1747 | which a new common symbol should simply override the definition | |
1748 | in the shared library. */ | |
1749 | ||
1750 | if (! newdyn | |
1751 | && bfd_is_com_section (sec) | |
1752 | && olddyncommon) | |
1753 | { | |
1754 | /* It would be best if we could set the hash table entry to a | |
1755 | common symbol, but we don't know what to use for the section | |
1756 | or the alignment. */ | |
1a72702b AM |
1757 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1758 | bfd_link_hash_common, sym->st_size); | |
45d6a902 | 1759 | |
4cc11e76 | 1760 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1761 | larger, pretend that the new symbol has its size. */ |
1762 | ||
1763 | if (h->size > *pvalue) | |
1764 | *pvalue = h->size; | |
1765 | ||
af44c138 L |
1766 | /* We need to remember the alignment required by the symbol |
1767 | in the dynamic object. */ | |
1768 | BFD_ASSERT (pold_alignment); | |
1769 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1770 | |
1771 | olddef = FALSE; | |
1772 | olddyncommon = FALSE; | |
1773 | ||
1774 | h->root.type = bfd_link_hash_undefined; | |
1775 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1776 | ||
1777 | *size_change_ok = TRUE; | |
1778 | *type_change_ok = TRUE; | |
1779 | ||
6c9b78e6 AM |
1780 | if (hi->root.type == bfd_link_hash_indirect) |
1781 | flip = hi; | |
45d6a902 AM |
1782 | else |
1783 | h->verinfo.vertree = NULL; | |
1784 | } | |
1785 | ||
1786 | if (flip != NULL) | |
1787 | { | |
1788 | /* Handle the case where we had a versioned symbol in a dynamic | |
1789 | library and now find a definition in a normal object. In this | |
1790 | case, we make the versioned symbol point to the normal one. */ | |
45d6a902 | 1791 | flip->root.type = h->root.type; |
00cbee0a | 1792 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1793 | h->root.type = bfd_link_hash_indirect; |
1794 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1795 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1796 | if (h->def_dynamic) |
45d6a902 | 1797 | { |
f5385ebf AM |
1798 | h->def_dynamic = 0; |
1799 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1800 | } |
1801 | } | |
1802 | ||
45d6a902 AM |
1803 | return TRUE; |
1804 | } | |
1805 | ||
1806 | /* This function is called to create an indirect symbol from the | |
1807 | default for the symbol with the default version if needed. The | |
4f3fedcf | 1808 | symbol is described by H, NAME, SYM, SEC, and VALUE. We |
0f8a2703 | 1809 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 | 1810 | |
28caa186 | 1811 | static bfd_boolean |
268b6b39 AM |
1812 | _bfd_elf_add_default_symbol (bfd *abfd, |
1813 | struct bfd_link_info *info, | |
1814 | struct elf_link_hash_entry *h, | |
1815 | const char *name, | |
1816 | Elf_Internal_Sym *sym, | |
4f3fedcf AM |
1817 | asection *sec, |
1818 | bfd_vma value, | |
1819 | bfd **poldbfd, | |
e3c9d234 | 1820 | bfd_boolean *dynsym) |
45d6a902 AM |
1821 | { |
1822 | bfd_boolean type_change_ok; | |
1823 | bfd_boolean size_change_ok; | |
1824 | bfd_boolean skip; | |
1825 | char *shortname; | |
1826 | struct elf_link_hash_entry *hi; | |
1827 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1828 | const struct elf_backend_data *bed; |
45d6a902 AM |
1829 | bfd_boolean collect; |
1830 | bfd_boolean dynamic; | |
e3c9d234 | 1831 | bfd_boolean override; |
45d6a902 AM |
1832 | char *p; |
1833 | size_t len, shortlen; | |
ffd65175 | 1834 | asection *tmp_sec; |
6e33951e | 1835 | bfd_boolean matched; |
45d6a902 | 1836 | |
422f1182 L |
1837 | if (h->versioned == unversioned || h->versioned == versioned_hidden) |
1838 | return TRUE; | |
1839 | ||
45d6a902 AM |
1840 | /* If this symbol has a version, and it is the default version, we |
1841 | create an indirect symbol from the default name to the fully | |
1842 | decorated name. This will cause external references which do not | |
1843 | specify a version to be bound to this version of the symbol. */ | |
1844 | p = strchr (name, ELF_VER_CHR); | |
422f1182 L |
1845 | if (h->versioned == unknown) |
1846 | { | |
1847 | if (p == NULL) | |
1848 | { | |
1849 | h->versioned = unversioned; | |
1850 | return TRUE; | |
1851 | } | |
1852 | else | |
1853 | { | |
1854 | if (p[1] != ELF_VER_CHR) | |
1855 | { | |
1856 | h->versioned = versioned_hidden; | |
1857 | return TRUE; | |
1858 | } | |
1859 | else | |
1860 | h->versioned = versioned; | |
1861 | } | |
1862 | } | |
4373f8af L |
1863 | else |
1864 | { | |
1865 | /* PR ld/19073: We may see an unversioned definition after the | |
1866 | default version. */ | |
1867 | if (p == NULL) | |
1868 | return TRUE; | |
1869 | } | |
45d6a902 | 1870 | |
45d6a902 AM |
1871 | bed = get_elf_backend_data (abfd); |
1872 | collect = bed->collect; | |
1873 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1874 | ||
1875 | shortlen = p - name; | |
a50b1753 | 1876 | shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); |
45d6a902 AM |
1877 | if (shortname == NULL) |
1878 | return FALSE; | |
1879 | memcpy (shortname, name, shortlen); | |
1880 | shortname[shortlen] = '\0'; | |
1881 | ||
1882 | /* We are going to create a new symbol. Merge it with any existing | |
1883 | symbol with this name. For the purposes of the merge, act as | |
1884 | though we were defining the symbol we just defined, although we | |
1885 | actually going to define an indirect symbol. */ | |
1886 | type_change_ok = FALSE; | |
1887 | size_change_ok = FALSE; | |
6e33951e | 1888 | matched = TRUE; |
ffd65175 AM |
1889 | tmp_sec = sec; |
1890 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
4f3fedcf | 1891 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 1892 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
1893 | return FALSE; |
1894 | ||
1895 | if (skip) | |
1896 | goto nondefault; | |
1897 | ||
5b677558 AM |
1898 | if (hi->def_regular) |
1899 | { | |
1900 | /* If the undecorated symbol will have a version added by a | |
1901 | script different to H, then don't indirect to/from the | |
1902 | undecorated symbol. This isn't ideal because we may not yet | |
1903 | have seen symbol versions, if given by a script on the | |
1904 | command line rather than via --version-script. */ | |
1905 | if (hi->verinfo.vertree == NULL && info->version_info != NULL) | |
1906 | { | |
1907 | bfd_boolean hide; | |
1908 | ||
1909 | hi->verinfo.vertree | |
1910 | = bfd_find_version_for_sym (info->version_info, | |
1911 | hi->root.root.string, &hide); | |
1912 | if (hi->verinfo.vertree != NULL && hide) | |
1913 | { | |
1914 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
1915 | goto nondefault; | |
1916 | } | |
1917 | } | |
1918 | if (hi->verinfo.vertree != NULL | |
1919 | && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0) | |
1920 | goto nondefault; | |
1921 | } | |
1922 | ||
45d6a902 AM |
1923 | if (! override) |
1924 | { | |
c6e8a9a8 | 1925 | /* Add the default symbol if not performing a relocatable link. */ |
0e1862bb | 1926 | if (! bfd_link_relocatable (info)) |
c6e8a9a8 L |
1927 | { |
1928 | bh = &hi->root; | |
1929 | if (! (_bfd_generic_link_add_one_symbol | |
1930 | (info, abfd, shortname, BSF_INDIRECT, | |
1931 | bfd_ind_section_ptr, | |
1932 | 0, name, FALSE, collect, &bh))) | |
1933 | return FALSE; | |
1934 | hi = (struct elf_link_hash_entry *) bh; | |
1935 | } | |
45d6a902 AM |
1936 | } |
1937 | else | |
1938 | { | |
1939 | /* In this case the symbol named SHORTNAME is overriding the | |
1940 | indirect symbol we want to add. We were planning on making | |
1941 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1942 | is the name without a version. NAME is the fully versioned | |
1943 | name, and it is the default version. | |
1944 | ||
1945 | Overriding means that we already saw a definition for the | |
1946 | symbol SHORTNAME in a regular object, and it is overriding | |
1947 | the symbol defined in the dynamic object. | |
1948 | ||
1949 | When this happens, we actually want to change NAME, the | |
1950 | symbol we just added, to refer to SHORTNAME. This will cause | |
1951 | references to NAME in the shared object to become references | |
1952 | to SHORTNAME in the regular object. This is what we expect | |
1953 | when we override a function in a shared object: that the | |
1954 | references in the shared object will be mapped to the | |
1955 | definition in the regular object. */ | |
1956 | ||
1957 | while (hi->root.type == bfd_link_hash_indirect | |
1958 | || hi->root.type == bfd_link_hash_warning) | |
1959 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1960 | ||
1961 | h->root.type = bfd_link_hash_indirect; | |
1962 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1963 | if (h->def_dynamic) |
45d6a902 | 1964 | { |
f5385ebf AM |
1965 | h->def_dynamic = 0; |
1966 | hi->ref_dynamic = 1; | |
1967 | if (hi->ref_regular | |
1968 | || hi->def_regular) | |
45d6a902 | 1969 | { |
c152c796 | 1970 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1971 | return FALSE; |
1972 | } | |
1973 | } | |
1974 | ||
1975 | /* Now set HI to H, so that the following code will set the | |
1976 | other fields correctly. */ | |
1977 | hi = h; | |
1978 | } | |
1979 | ||
fab4a87f L |
1980 | /* Check if HI is a warning symbol. */ |
1981 | if (hi->root.type == bfd_link_hash_warning) | |
1982 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1983 | ||
45d6a902 AM |
1984 | /* If there is a duplicate definition somewhere, then HI may not |
1985 | point to an indirect symbol. We will have reported an error to | |
1986 | the user in that case. */ | |
1987 | ||
1988 | if (hi->root.type == bfd_link_hash_indirect) | |
1989 | { | |
1990 | struct elf_link_hash_entry *ht; | |
1991 | ||
45d6a902 | 1992 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 1993 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 | 1994 | |
68c88cd4 AM |
1995 | /* A reference to the SHORTNAME symbol from a dynamic library |
1996 | will be satisfied by the versioned symbol at runtime. In | |
1997 | effect, we have a reference to the versioned symbol. */ | |
1998 | ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; | |
1999 | hi->dynamic_def |= ht->dynamic_def; | |
2000 | ||
45d6a902 AM |
2001 | /* See if the new flags lead us to realize that the symbol must |
2002 | be dynamic. */ | |
2003 | if (! *dynsym) | |
2004 | { | |
2005 | if (! dynamic) | |
2006 | { | |
0e1862bb | 2007 | if (! bfd_link_executable (info) |
90c984fc | 2008 | || hi->def_dynamic |
f5385ebf | 2009 | || hi->ref_dynamic) |
45d6a902 AM |
2010 | *dynsym = TRUE; |
2011 | } | |
2012 | else | |
2013 | { | |
f5385ebf | 2014 | if (hi->ref_regular) |
45d6a902 AM |
2015 | *dynsym = TRUE; |
2016 | } | |
2017 | } | |
2018 | } | |
2019 | ||
2020 | /* We also need to define an indirection from the nondefault version | |
2021 | of the symbol. */ | |
2022 | ||
2023 | nondefault: | |
2024 | len = strlen (name); | |
a50b1753 | 2025 | shortname = (char *) bfd_hash_allocate (&info->hash->table, len); |
45d6a902 AM |
2026 | if (shortname == NULL) |
2027 | return FALSE; | |
2028 | memcpy (shortname, name, shortlen); | |
2029 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
2030 | ||
2031 | /* Once again, merge with any existing symbol. */ | |
2032 | type_change_ok = FALSE; | |
2033 | size_change_ok = FALSE; | |
ffd65175 AM |
2034 | tmp_sec = sec; |
2035 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
115c6d5c | 2036 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 2037 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
2038 | return FALSE; |
2039 | ||
2040 | if (skip) | |
2041 | return TRUE; | |
2042 | ||
2043 | if (override) | |
2044 | { | |
2045 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
2046 | the type of override we do in the case above unless it is | |
4cc11e76 | 2047 | overridden by a versioned definition. */ |
45d6a902 AM |
2048 | if (hi->root.type != bfd_link_hash_defined |
2049 | && hi->root.type != bfd_link_hash_defweak) | |
4eca0228 | 2050 | _bfd_error_handler |
695344c0 | 2051 | /* xgettext:c-format */ |
d003868e AM |
2052 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
2053 | abfd, shortname); | |
45d6a902 AM |
2054 | } |
2055 | else | |
2056 | { | |
2057 | bh = &hi->root; | |
2058 | if (! (_bfd_generic_link_add_one_symbol | |
2059 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 2060 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
2061 | return FALSE; |
2062 | hi = (struct elf_link_hash_entry *) bh; | |
2063 | ||
2064 | /* If there is a duplicate definition somewhere, then HI may not | |
2065 | point to an indirect symbol. We will have reported an error | |
2066 | to the user in that case. */ | |
2067 | ||
2068 | if (hi->root.type == bfd_link_hash_indirect) | |
2069 | { | |
fcfa13d2 | 2070 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
68c88cd4 AM |
2071 | h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; |
2072 | hi->dynamic_def |= h->dynamic_def; | |
45d6a902 AM |
2073 | |
2074 | /* See if the new flags lead us to realize that the symbol | |
2075 | must be dynamic. */ | |
2076 | if (! *dynsym) | |
2077 | { | |
2078 | if (! dynamic) | |
2079 | { | |
0e1862bb | 2080 | if (! bfd_link_executable (info) |
f5385ebf | 2081 | || hi->ref_dynamic) |
45d6a902 AM |
2082 | *dynsym = TRUE; |
2083 | } | |
2084 | else | |
2085 | { | |
f5385ebf | 2086 | if (hi->ref_regular) |
45d6a902 AM |
2087 | *dynsym = TRUE; |
2088 | } | |
2089 | } | |
2090 | } | |
2091 | } | |
2092 | ||
2093 | return TRUE; | |
2094 | } | |
2095 | \f | |
2096 | /* This routine is used to export all defined symbols into the dynamic | |
2097 | symbol table. It is called via elf_link_hash_traverse. */ | |
2098 | ||
28caa186 | 2099 | static bfd_boolean |
268b6b39 | 2100 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2101 | { |
a50b1753 | 2102 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 AM |
2103 | |
2104 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2105 | if (h->root.type == bfd_link_hash_indirect) | |
2106 | return TRUE; | |
2107 | ||
7686d77d AM |
2108 | /* Ignore this if we won't export it. */ |
2109 | if (!eif->info->export_dynamic && !h->dynamic) | |
2110 | return TRUE; | |
45d6a902 AM |
2111 | |
2112 | if (h->dynindx == -1 | |
fd91d419 L |
2113 | && (h->def_regular || h->ref_regular) |
2114 | && ! bfd_hide_sym_by_version (eif->info->version_info, | |
2115 | h->root.root.string)) | |
45d6a902 | 2116 | { |
fd91d419 | 2117 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 | 2118 | { |
fd91d419 L |
2119 | eif->failed = TRUE; |
2120 | return FALSE; | |
45d6a902 AM |
2121 | } |
2122 | } | |
2123 | ||
2124 | return TRUE; | |
2125 | } | |
2126 | \f | |
2127 | /* Look through the symbols which are defined in other shared | |
2128 | libraries and referenced here. Update the list of version | |
2129 | dependencies. This will be put into the .gnu.version_r section. | |
2130 | This function is called via elf_link_hash_traverse. */ | |
2131 | ||
28caa186 | 2132 | static bfd_boolean |
268b6b39 AM |
2133 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
2134 | void *data) | |
45d6a902 | 2135 | { |
a50b1753 | 2136 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; |
45d6a902 AM |
2137 | Elf_Internal_Verneed *t; |
2138 | Elf_Internal_Vernaux *a; | |
2139 | bfd_size_type amt; | |
2140 | ||
45d6a902 AM |
2141 | /* We only care about symbols defined in shared objects with version |
2142 | information. */ | |
f5385ebf AM |
2143 | if (!h->def_dynamic |
2144 | || h->def_regular | |
45d6a902 | 2145 | || h->dynindx == -1 |
7b20f099 AM |
2146 | || h->verinfo.verdef == NULL |
2147 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
2148 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
45d6a902 AM |
2149 | return TRUE; |
2150 | ||
2151 | /* See if we already know about this version. */ | |
28caa186 AM |
2152 | for (t = elf_tdata (rinfo->info->output_bfd)->verref; |
2153 | t != NULL; | |
2154 | t = t->vn_nextref) | |
45d6a902 AM |
2155 | { |
2156 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
2157 | continue; | |
2158 | ||
2159 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2160 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
2161 | return TRUE; | |
2162 | ||
2163 | break; | |
2164 | } | |
2165 | ||
2166 | /* This is a new version. Add it to tree we are building. */ | |
2167 | ||
2168 | if (t == NULL) | |
2169 | { | |
2170 | amt = sizeof *t; | |
a50b1753 | 2171 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); |
45d6a902 AM |
2172 | if (t == NULL) |
2173 | { | |
2174 | rinfo->failed = TRUE; | |
2175 | return FALSE; | |
2176 | } | |
2177 | ||
2178 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
28caa186 AM |
2179 | t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; |
2180 | elf_tdata (rinfo->info->output_bfd)->verref = t; | |
45d6a902 AM |
2181 | } |
2182 | ||
2183 | amt = sizeof *a; | |
a50b1753 | 2184 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); |
14b1c01e AM |
2185 | if (a == NULL) |
2186 | { | |
2187 | rinfo->failed = TRUE; | |
2188 | return FALSE; | |
2189 | } | |
45d6a902 AM |
2190 | |
2191 | /* Note that we are copying a string pointer here, and testing it | |
2192 | above. If bfd_elf_string_from_elf_section is ever changed to | |
2193 | discard the string data when low in memory, this will have to be | |
2194 | fixed. */ | |
2195 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
2196 | ||
2197 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
2198 | a->vna_nextptr = t->vn_auxptr; | |
2199 | ||
2200 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
2201 | ++rinfo->vers; | |
2202 | ||
2203 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
2204 | ||
2205 | t->vn_auxptr = a; | |
2206 | ||
2207 | return TRUE; | |
2208 | } | |
2209 | ||
2210 | /* Figure out appropriate versions for all the symbols. We may not | |
2211 | have the version number script until we have read all of the input | |
2212 | files, so until that point we don't know which symbols should be | |
2213 | local. This function is called via elf_link_hash_traverse. */ | |
2214 | ||
28caa186 | 2215 | static bfd_boolean |
268b6b39 | 2216 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2217 | { |
28caa186 | 2218 | struct elf_info_failed *sinfo; |
45d6a902 | 2219 | struct bfd_link_info *info; |
9c5bfbb7 | 2220 | const struct elf_backend_data *bed; |
45d6a902 AM |
2221 | struct elf_info_failed eif; |
2222 | char *p; | |
45d6a902 | 2223 | |
a50b1753 | 2224 | sinfo = (struct elf_info_failed *) data; |
45d6a902 AM |
2225 | info = sinfo->info; |
2226 | ||
45d6a902 AM |
2227 | /* Fix the symbol flags. */ |
2228 | eif.failed = FALSE; | |
2229 | eif.info = info; | |
2230 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
2231 | { | |
2232 | if (eif.failed) | |
2233 | sinfo->failed = TRUE; | |
2234 | return FALSE; | |
2235 | } | |
2236 | ||
2237 | /* We only need version numbers for symbols defined in regular | |
2238 | objects. */ | |
f5385ebf | 2239 | if (!h->def_regular) |
45d6a902 AM |
2240 | return TRUE; |
2241 | ||
28caa186 | 2242 | bed = get_elf_backend_data (info->output_bfd); |
45d6a902 AM |
2243 | p = strchr (h->root.root.string, ELF_VER_CHR); |
2244 | if (p != NULL && h->verinfo.vertree == NULL) | |
2245 | { | |
2246 | struct bfd_elf_version_tree *t; | |
45d6a902 | 2247 | |
45d6a902 AM |
2248 | ++p; |
2249 | if (*p == ELF_VER_CHR) | |
6e33951e | 2250 | ++p; |
45d6a902 AM |
2251 | |
2252 | /* If there is no version string, we can just return out. */ | |
2253 | if (*p == '\0') | |
6e33951e | 2254 | return TRUE; |
45d6a902 AM |
2255 | |
2256 | /* Look for the version. If we find it, it is no longer weak. */ | |
fd91d419 | 2257 | for (t = sinfo->info->version_info; t != NULL; t = t->next) |
45d6a902 AM |
2258 | { |
2259 | if (strcmp (t->name, p) == 0) | |
2260 | { | |
2261 | size_t len; | |
2262 | char *alc; | |
2263 | struct bfd_elf_version_expr *d; | |
2264 | ||
2265 | len = p - h->root.root.string; | |
a50b1753 | 2266 | alc = (char *) bfd_malloc (len); |
45d6a902 | 2267 | if (alc == NULL) |
14b1c01e AM |
2268 | { |
2269 | sinfo->failed = TRUE; | |
2270 | return FALSE; | |
2271 | } | |
45d6a902 AM |
2272 | memcpy (alc, h->root.root.string, len - 1); |
2273 | alc[len - 1] = '\0'; | |
2274 | if (alc[len - 2] == ELF_VER_CHR) | |
2275 | alc[len - 2] = '\0'; | |
2276 | ||
2277 | h->verinfo.vertree = t; | |
2278 | t->used = TRUE; | |
2279 | d = NULL; | |
2280 | ||
108ba305 JJ |
2281 | if (t->globals.list != NULL) |
2282 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
2283 | |
2284 | /* See if there is anything to force this symbol to | |
2285 | local scope. */ | |
108ba305 | 2286 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 2287 | { |
108ba305 JJ |
2288 | d = (*t->match) (&t->locals, NULL, alc); |
2289 | if (d != NULL | |
2290 | && h->dynindx != -1 | |
108ba305 JJ |
2291 | && ! info->export_dynamic) |
2292 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2293 | } |
2294 | ||
2295 | free (alc); | |
2296 | break; | |
2297 | } | |
2298 | } | |
2299 | ||
2300 | /* If we are building an application, we need to create a | |
2301 | version node for this version. */ | |
0e1862bb | 2302 | if (t == NULL && bfd_link_executable (info)) |
45d6a902 AM |
2303 | { |
2304 | struct bfd_elf_version_tree **pp; | |
2305 | int version_index; | |
2306 | ||
2307 | /* If we aren't going to export this symbol, we don't need | |
2308 | to worry about it. */ | |
2309 | if (h->dynindx == -1) | |
2310 | return TRUE; | |
2311 | ||
ef53be89 AM |
2312 | t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, |
2313 | sizeof *t); | |
45d6a902 AM |
2314 | if (t == NULL) |
2315 | { | |
2316 | sinfo->failed = TRUE; | |
2317 | return FALSE; | |
2318 | } | |
2319 | ||
45d6a902 | 2320 | t->name = p; |
45d6a902 AM |
2321 | t->name_indx = (unsigned int) -1; |
2322 | t->used = TRUE; | |
2323 | ||
2324 | version_index = 1; | |
2325 | /* Don't count anonymous version tag. */ | |
fd91d419 L |
2326 | if (sinfo->info->version_info != NULL |
2327 | && sinfo->info->version_info->vernum == 0) | |
45d6a902 | 2328 | version_index = 0; |
fd91d419 L |
2329 | for (pp = &sinfo->info->version_info; |
2330 | *pp != NULL; | |
2331 | pp = &(*pp)->next) | |
45d6a902 AM |
2332 | ++version_index; |
2333 | t->vernum = version_index; | |
2334 | ||
2335 | *pp = t; | |
2336 | ||
2337 | h->verinfo.vertree = t; | |
2338 | } | |
2339 | else if (t == NULL) | |
2340 | { | |
2341 | /* We could not find the version for a symbol when | |
2342 | generating a shared archive. Return an error. */ | |
4eca0228 | 2343 | _bfd_error_handler |
695344c0 | 2344 | /* xgettext:c-format */ |
c55fe096 | 2345 | (_("%B: version node not found for symbol %s"), |
28caa186 | 2346 | info->output_bfd, h->root.root.string); |
45d6a902 AM |
2347 | bfd_set_error (bfd_error_bad_value); |
2348 | sinfo->failed = TRUE; | |
2349 | return FALSE; | |
2350 | } | |
45d6a902 AM |
2351 | } |
2352 | ||
2353 | /* If we don't have a version for this symbol, see if we can find | |
2354 | something. */ | |
fd91d419 | 2355 | if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL) |
45d6a902 | 2356 | { |
1e8fa21e | 2357 | bfd_boolean hide; |
ae5a3597 | 2358 | |
fd91d419 L |
2359 | h->verinfo.vertree |
2360 | = bfd_find_version_for_sym (sinfo->info->version_info, | |
2361 | h->root.root.string, &hide); | |
1e8fa21e AM |
2362 | if (h->verinfo.vertree != NULL && hide) |
2363 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2364 | } |
2365 | ||
2366 | return TRUE; | |
2367 | } | |
2368 | \f | |
45d6a902 AM |
2369 | /* Read and swap the relocs from the section indicated by SHDR. This |
2370 | may be either a REL or a RELA section. The relocations are | |
2371 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2372 | which should have already been allocated to contain enough space. | |
2373 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2374 | relocations should be stored. | |
2375 | ||
2376 | Returns FALSE if something goes wrong. */ | |
2377 | ||
2378 | static bfd_boolean | |
268b6b39 | 2379 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2380 | asection *sec, |
268b6b39 AM |
2381 | Elf_Internal_Shdr *shdr, |
2382 | void *external_relocs, | |
2383 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2384 | { |
9c5bfbb7 | 2385 | const struct elf_backend_data *bed; |
268b6b39 | 2386 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2387 | const bfd_byte *erela; |
2388 | const bfd_byte *erelaend; | |
2389 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2390 | Elf_Internal_Shdr *symtab_hdr; |
2391 | size_t nsyms; | |
45d6a902 | 2392 | |
45d6a902 AM |
2393 | /* Position ourselves at the start of the section. */ |
2394 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2395 | return FALSE; | |
2396 | ||
2397 | /* Read the relocations. */ | |
2398 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2399 | return FALSE; | |
2400 | ||
243ef1e0 | 2401 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
ce98a316 | 2402 | nsyms = NUM_SHDR_ENTRIES (symtab_hdr); |
243ef1e0 | 2403 | |
45d6a902 AM |
2404 | bed = get_elf_backend_data (abfd); |
2405 | ||
2406 | /* Convert the external relocations to the internal format. */ | |
2407 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2408 | swap_in = bed->s->swap_reloc_in; | |
2409 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2410 | swap_in = bed->s->swap_reloca_in; | |
2411 | else | |
2412 | { | |
2413 | bfd_set_error (bfd_error_wrong_format); | |
2414 | return FALSE; | |
2415 | } | |
2416 | ||
a50b1753 | 2417 | erela = (const bfd_byte *) external_relocs; |
51992aec | 2418 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2419 | irela = internal_relocs; |
2420 | while (erela < erelaend) | |
2421 | { | |
243ef1e0 L |
2422 | bfd_vma r_symndx; |
2423 | ||
45d6a902 | 2424 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2425 | r_symndx = ELF32_R_SYM (irela->r_info); |
2426 | if (bed->s->arch_size == 64) | |
2427 | r_symndx >>= 24; | |
ce98a316 NC |
2428 | if (nsyms > 0) |
2429 | { | |
2430 | if ((size_t) r_symndx >= nsyms) | |
2431 | { | |
4eca0228 | 2432 | _bfd_error_handler |
695344c0 | 2433 | /* xgettext:c-format */ |
d42c267e | 2434 | (_("%B: bad reloc symbol index (%#Lx >= %#lx)" |
76cfced5 | 2435 | " for offset %#Lx in section `%A'"), |
d42c267e | 2436 | abfd, r_symndx, (unsigned long) nsyms, |
c08bb8dd | 2437 | irela->r_offset, sec); |
ce98a316 NC |
2438 | bfd_set_error (bfd_error_bad_value); |
2439 | return FALSE; | |
2440 | } | |
2441 | } | |
cf35638d | 2442 | else if (r_symndx != STN_UNDEF) |
243ef1e0 | 2443 | { |
4eca0228 | 2444 | _bfd_error_handler |
695344c0 | 2445 | /* xgettext:c-format */ |
d42c267e | 2446 | (_("%B: non-zero symbol index (%#Lx)" |
76cfced5 | 2447 | " for offset %#Lx in section `%A'" |
ce98a316 | 2448 | " when the object file has no symbol table"), |
d42c267e | 2449 | abfd, r_symndx, |
c08bb8dd | 2450 | irela->r_offset, sec); |
243ef1e0 L |
2451 | bfd_set_error (bfd_error_bad_value); |
2452 | return FALSE; | |
2453 | } | |
45d6a902 AM |
2454 | irela += bed->s->int_rels_per_ext_rel; |
2455 | erela += shdr->sh_entsize; | |
2456 | } | |
2457 | ||
2458 | return TRUE; | |
2459 | } | |
2460 | ||
2461 | /* Read and swap the relocs for a section O. They may have been | |
2462 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2463 | not NULL, they are used as buffers to read into. They are known to | |
2464 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2465 | the return value is allocated using either malloc or bfd_alloc, | |
2466 | according to the KEEP_MEMORY argument. If O has two relocation | |
2467 | sections (both REL and RELA relocations), then the REL_HDR | |
2468 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
d4730f92 | 2469 | RELA_HDR relocations. */ |
45d6a902 AM |
2470 | |
2471 | Elf_Internal_Rela * | |
268b6b39 AM |
2472 | _bfd_elf_link_read_relocs (bfd *abfd, |
2473 | asection *o, | |
2474 | void *external_relocs, | |
2475 | Elf_Internal_Rela *internal_relocs, | |
2476 | bfd_boolean keep_memory) | |
45d6a902 | 2477 | { |
268b6b39 | 2478 | void *alloc1 = NULL; |
45d6a902 | 2479 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2480 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2481 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
2482 | Elf_Internal_Rela *internal_rela_relocs; | |
45d6a902 | 2483 | |
d4730f92 BS |
2484 | if (esdo->relocs != NULL) |
2485 | return esdo->relocs; | |
45d6a902 AM |
2486 | |
2487 | if (o->reloc_count == 0) | |
2488 | return NULL; | |
2489 | ||
45d6a902 AM |
2490 | if (internal_relocs == NULL) |
2491 | { | |
2492 | bfd_size_type size; | |
2493 | ||
056bafd4 | 2494 | size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela); |
45d6a902 | 2495 | if (keep_memory) |
a50b1753 | 2496 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); |
45d6a902 | 2497 | else |
a50b1753 | 2498 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
45d6a902 AM |
2499 | if (internal_relocs == NULL) |
2500 | goto error_return; | |
2501 | } | |
2502 | ||
2503 | if (external_relocs == NULL) | |
2504 | { | |
d4730f92 BS |
2505 | bfd_size_type size = 0; |
2506 | ||
2507 | if (esdo->rel.hdr) | |
2508 | size += esdo->rel.hdr->sh_size; | |
2509 | if (esdo->rela.hdr) | |
2510 | size += esdo->rela.hdr->sh_size; | |
45d6a902 | 2511 | |
268b6b39 | 2512 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2513 | if (alloc1 == NULL) |
2514 | goto error_return; | |
2515 | external_relocs = alloc1; | |
2516 | } | |
2517 | ||
d4730f92 BS |
2518 | internal_rela_relocs = internal_relocs; |
2519 | if (esdo->rel.hdr) | |
2520 | { | |
2521 | if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, | |
2522 | external_relocs, | |
2523 | internal_relocs)) | |
2524 | goto error_return; | |
2525 | external_relocs = (((bfd_byte *) external_relocs) | |
2526 | + esdo->rel.hdr->sh_size); | |
2527 | internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) | |
2528 | * bed->s->int_rels_per_ext_rel); | |
2529 | } | |
2530 | ||
2531 | if (esdo->rela.hdr | |
2532 | && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, | |
2533 | external_relocs, | |
2534 | internal_rela_relocs))) | |
45d6a902 AM |
2535 | goto error_return; |
2536 | ||
2537 | /* Cache the results for next time, if we can. */ | |
2538 | if (keep_memory) | |
d4730f92 | 2539 | esdo->relocs = internal_relocs; |
45d6a902 AM |
2540 | |
2541 | if (alloc1 != NULL) | |
2542 | free (alloc1); | |
2543 | ||
2544 | /* Don't free alloc2, since if it was allocated we are passing it | |
2545 | back (under the name of internal_relocs). */ | |
2546 | ||
2547 | return internal_relocs; | |
2548 | ||
2549 | error_return: | |
2550 | if (alloc1 != NULL) | |
2551 | free (alloc1); | |
2552 | if (alloc2 != NULL) | |
4dd07732 AM |
2553 | { |
2554 | if (keep_memory) | |
2555 | bfd_release (abfd, alloc2); | |
2556 | else | |
2557 | free (alloc2); | |
2558 | } | |
45d6a902 AM |
2559 | return NULL; |
2560 | } | |
2561 | ||
2562 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2563 | section header for a section containing relocations for O. */ | |
2564 | ||
28caa186 | 2565 | static bfd_boolean |
9eaff861 AO |
2566 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2567 | struct bfd_elf_section_reloc_data *reldata) | |
45d6a902 | 2568 | { |
9eaff861 | 2569 | Elf_Internal_Shdr *rel_hdr = reldata->hdr; |
45d6a902 AM |
2570 | |
2571 | /* That allows us to calculate the size of the section. */ | |
9eaff861 | 2572 | rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; |
45d6a902 AM |
2573 | |
2574 | /* The contents field must last into write_object_contents, so we | |
2575 | allocate it with bfd_alloc rather than malloc. Also since we | |
2576 | cannot be sure that the contents will actually be filled in, | |
2577 | we zero the allocated space. */ | |
a50b1753 | 2578 | rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2579 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2580 | return FALSE; | |
2581 | ||
d4730f92 | 2582 | if (reldata->hashes == NULL && reldata->count) |
45d6a902 AM |
2583 | { |
2584 | struct elf_link_hash_entry **p; | |
2585 | ||
ca4be51c AM |
2586 | p = ((struct elf_link_hash_entry **) |
2587 | bfd_zmalloc (reldata->count * sizeof (*p))); | |
45d6a902 AM |
2588 | if (p == NULL) |
2589 | return FALSE; | |
2590 | ||
d4730f92 | 2591 | reldata->hashes = p; |
45d6a902 AM |
2592 | } |
2593 | ||
2594 | return TRUE; | |
2595 | } | |
2596 | ||
2597 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2598 | originated from the section given by INPUT_REL_HDR) to the | |
2599 | OUTPUT_BFD. */ | |
2600 | ||
2601 | bfd_boolean | |
268b6b39 AM |
2602 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2603 | asection *input_section, | |
2604 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2605 | Elf_Internal_Rela *internal_relocs, |
2606 | struct elf_link_hash_entry **rel_hash | |
2607 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2608 | { |
2609 | Elf_Internal_Rela *irela; | |
2610 | Elf_Internal_Rela *irelaend; | |
2611 | bfd_byte *erel; | |
d4730f92 | 2612 | struct bfd_elf_section_reloc_data *output_reldata; |
45d6a902 | 2613 | asection *output_section; |
9c5bfbb7 | 2614 | const struct elf_backend_data *bed; |
268b6b39 | 2615 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
d4730f92 | 2616 | struct bfd_elf_section_data *esdo; |
45d6a902 AM |
2617 | |
2618 | output_section = input_section->output_section; | |
45d6a902 | 2619 | |
d4730f92 BS |
2620 | bed = get_elf_backend_data (output_bfd); |
2621 | esdo = elf_section_data (output_section); | |
2622 | if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2623 | { |
d4730f92 BS |
2624 | output_reldata = &esdo->rel; |
2625 | swap_out = bed->s->swap_reloc_out; | |
45d6a902 | 2626 | } |
d4730f92 BS |
2627 | else if (esdo->rela.hdr |
2628 | && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2629 | { |
d4730f92 BS |
2630 | output_reldata = &esdo->rela; |
2631 | swap_out = bed->s->swap_reloca_out; | |
45d6a902 AM |
2632 | } |
2633 | else | |
2634 | { | |
4eca0228 | 2635 | _bfd_error_handler |
695344c0 | 2636 | /* xgettext:c-format */ |
d003868e AM |
2637 | (_("%B: relocation size mismatch in %B section %A"), |
2638 | output_bfd, input_section->owner, input_section); | |
297d8443 | 2639 | bfd_set_error (bfd_error_wrong_format); |
45d6a902 AM |
2640 | return FALSE; |
2641 | } | |
2642 | ||
d4730f92 BS |
2643 | erel = output_reldata->hdr->contents; |
2644 | erel += output_reldata->count * input_rel_hdr->sh_entsize; | |
45d6a902 AM |
2645 | irela = internal_relocs; |
2646 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2647 | * bed->s->int_rels_per_ext_rel); | |
2648 | while (irela < irelaend) | |
2649 | { | |
2650 | (*swap_out) (output_bfd, irela, erel); | |
2651 | irela += bed->s->int_rels_per_ext_rel; | |
2652 | erel += input_rel_hdr->sh_entsize; | |
2653 | } | |
2654 | ||
2655 | /* Bump the counter, so that we know where to add the next set of | |
2656 | relocations. */ | |
d4730f92 | 2657 | output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); |
45d6a902 AM |
2658 | |
2659 | return TRUE; | |
2660 | } | |
2661 | \f | |
508c3946 L |
2662 | /* Make weak undefined symbols in PIE dynamic. */ |
2663 | ||
2664 | bfd_boolean | |
2665 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2666 | struct elf_link_hash_entry *h) | |
2667 | { | |
0e1862bb | 2668 | if (bfd_link_pie (info) |
508c3946 L |
2669 | && h->dynindx == -1 |
2670 | && h->root.type == bfd_link_hash_undefweak) | |
2671 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2672 | ||
2673 | return TRUE; | |
2674 | } | |
2675 | ||
45d6a902 AM |
2676 | /* Fix up the flags for a symbol. This handles various cases which |
2677 | can only be fixed after all the input files are seen. This is | |
2678 | currently called by both adjust_dynamic_symbol and | |
2679 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2680 | the face of future changes. */ | |
2681 | ||
28caa186 | 2682 | static bfd_boolean |
268b6b39 AM |
2683 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2684 | struct elf_info_failed *eif) | |
45d6a902 | 2685 | { |
33774f08 | 2686 | const struct elf_backend_data *bed; |
508c3946 | 2687 | |
45d6a902 AM |
2688 | /* If this symbol was mentioned in a non-ELF file, try to set |
2689 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2690 | permit a non-ELF file to correctly refer to a symbol defined in | |
2691 | an ELF dynamic object. */ | |
f5385ebf | 2692 | if (h->non_elf) |
45d6a902 AM |
2693 | { |
2694 | while (h->root.type == bfd_link_hash_indirect) | |
2695 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2696 | ||
2697 | if (h->root.type != bfd_link_hash_defined | |
2698 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2699 | { |
2700 | h->ref_regular = 1; | |
2701 | h->ref_regular_nonweak = 1; | |
2702 | } | |
45d6a902 AM |
2703 | else |
2704 | { | |
2705 | if (h->root.u.def.section->owner != NULL | |
2706 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2707 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2708 | { |
2709 | h->ref_regular = 1; | |
2710 | h->ref_regular_nonweak = 1; | |
2711 | } | |
45d6a902 | 2712 | else |
f5385ebf | 2713 | h->def_regular = 1; |
45d6a902 AM |
2714 | } |
2715 | ||
2716 | if (h->dynindx == -1 | |
f5385ebf AM |
2717 | && (h->def_dynamic |
2718 | || h->ref_dynamic)) | |
45d6a902 | 2719 | { |
c152c796 | 2720 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2721 | { |
2722 | eif->failed = TRUE; | |
2723 | return FALSE; | |
2724 | } | |
2725 | } | |
2726 | } | |
2727 | else | |
2728 | { | |
f5385ebf | 2729 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2730 | was first seen in a non-ELF file. Fortunately, if the symbol |
2731 | was first seen in an ELF file, we're probably OK unless the | |
2732 | symbol was defined in a non-ELF file. Catch that case here. | |
2733 | FIXME: We're still in trouble if the symbol was first seen in | |
2734 | a dynamic object, and then later in a non-ELF regular object. */ | |
2735 | if ((h->root.type == bfd_link_hash_defined | |
2736 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2737 | && !h->def_regular |
45d6a902 AM |
2738 | && (h->root.u.def.section->owner != NULL |
2739 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2740 | != bfd_target_elf_flavour) | |
2741 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2742 | && !h->def_dynamic))) |
2743 | h->def_regular = 1; | |
45d6a902 AM |
2744 | } |
2745 | ||
508c3946 | 2746 | /* Backend specific symbol fixup. */ |
33774f08 AM |
2747 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2748 | if (bed->elf_backend_fixup_symbol | |
2749 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2750 | return FALSE; | |
508c3946 | 2751 | |
45d6a902 AM |
2752 | /* If this is a final link, and the symbol was defined as a common |
2753 | symbol in a regular object file, and there was no definition in | |
2754 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2755 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2756 | flag will not have been set. */ |
2757 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2758 | && !h->def_regular |
2759 | && h->ref_regular | |
2760 | && !h->def_dynamic | |
96f29d96 | 2761 | && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) |
f5385ebf | 2762 | h->def_regular = 1; |
45d6a902 | 2763 | |
4deb8f71 L |
2764 | /* If a weak undefined symbol has non-default visibility, we also |
2765 | hide it from the dynamic linker. */ | |
2766 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT | |
2767 | && h->root.type == bfd_link_hash_undefweak) | |
2768 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2769 | ||
2770 | /* A hidden versioned symbol in executable should be forced local if | |
2771 | it is is locally defined, not referenced by shared library and not | |
2772 | exported. */ | |
2773 | else if (bfd_link_executable (eif->info) | |
2774 | && h->versioned == versioned_hidden | |
2775 | && !eif->info->export_dynamic | |
2776 | && !h->dynamic | |
2777 | && !h->ref_dynamic | |
2778 | && h->def_regular) | |
2779 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2780 | ||
45d6a902 AM |
2781 | /* If -Bsymbolic was used (which means to bind references to global |
2782 | symbols to the definition within the shared object), and this | |
2783 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2784 | need a PLT entry. Likewise, if the symbol has non-default |
2785 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2786 | will force it local. */ |
4deb8f71 L |
2787 | else if (h->needs_plt |
2788 | && bfd_link_pic (eif->info) | |
2789 | && is_elf_hash_table (eif->info->hash) | |
2790 | && (SYMBOLIC_BIND (eif->info, h) | |
2791 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) | |
2792 | && h->def_regular) | |
45d6a902 | 2793 | { |
45d6a902 AM |
2794 | bfd_boolean force_local; |
2795 | ||
45d6a902 AM |
2796 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2797 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2798 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2799 | } | |
2800 | ||
45d6a902 AM |
2801 | /* If this is a weak defined symbol in a dynamic object, and we know |
2802 | the real definition in the dynamic object, copy interesting flags | |
2803 | over to the real definition. */ | |
60d67dc8 | 2804 | if (h->is_weakalias) |
45d6a902 | 2805 | { |
60d67dc8 AM |
2806 | struct elf_link_hash_entry *def = weakdef (h); |
2807 | ||
45d6a902 AM |
2808 | /* If the real definition is defined by a regular object file, |
2809 | don't do anything special. See the longer description in | |
2810 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
60d67dc8 AM |
2811 | if (def->def_regular) |
2812 | { | |
2813 | h = def; | |
2814 | while ((h = h->u.alias) != def) | |
2815 | h->is_weakalias = 0; | |
2816 | } | |
45d6a902 | 2817 | else |
a26587ba | 2818 | { |
4e6b54a6 AM |
2819 | while (h->root.type == bfd_link_hash_indirect) |
2820 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4e6b54a6 AM |
2821 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
2822 | || h->root.type == bfd_link_hash_defweak); | |
60d67dc8 AM |
2823 | BFD_ASSERT (def->def_dynamic); |
2824 | BFD_ASSERT (def->root.type == bfd_link_hash_defined); | |
2825 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h); | |
a26587ba | 2826 | } |
45d6a902 AM |
2827 | } |
2828 | ||
2829 | return TRUE; | |
2830 | } | |
2831 | ||
2832 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2833 | called via elf_link_hash_traverse, and also calls itself | |
2834 | recursively. */ | |
2835 | ||
28caa186 | 2836 | static bfd_boolean |
268b6b39 | 2837 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2838 | { |
a50b1753 | 2839 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
559192d8 | 2840 | struct elf_link_hash_table *htab; |
9c5bfbb7 | 2841 | const struct elf_backend_data *bed; |
45d6a902 | 2842 | |
0eddce27 | 2843 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2844 | return FALSE; |
2845 | ||
45d6a902 AM |
2846 | /* Ignore indirect symbols. These are added by the versioning code. */ |
2847 | if (h->root.type == bfd_link_hash_indirect) | |
2848 | return TRUE; | |
2849 | ||
2850 | /* Fix the symbol flags. */ | |
2851 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2852 | return FALSE; | |
2853 | ||
559192d8 AM |
2854 | htab = elf_hash_table (eif->info); |
2855 | bed = get_elf_backend_data (htab->dynobj); | |
2856 | ||
954b63d4 AM |
2857 | if (h->root.type == bfd_link_hash_undefweak) |
2858 | { | |
2859 | if (eif->info->dynamic_undefined_weak == 0) | |
559192d8 | 2860 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
954b63d4 AM |
2861 | else if (eif->info->dynamic_undefined_weak > 0 |
2862 | && h->ref_regular | |
2863 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
2864 | && !bfd_hide_sym_by_version (eif->info->version_info, | |
2865 | h->root.root.string)) | |
2866 | { | |
2867 | if (!bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
2868 | { | |
2869 | eif->failed = TRUE; | |
2870 | return FALSE; | |
2871 | } | |
2872 | } | |
2873 | } | |
2874 | ||
45d6a902 AM |
2875 | /* If this symbol does not require a PLT entry, and it is not |
2876 | defined by a dynamic object, or is not referenced by a regular | |
2877 | object, ignore it. We do have to handle a weak defined symbol, | |
2878 | even if no regular object refers to it, if we decided to add it | |
2879 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2880 | about symbols which are defined by one dynamic object and | |
2881 | referenced by another one? */ | |
f5385ebf | 2882 | if (!h->needs_plt |
91e21fb7 | 2883 | && h->type != STT_GNU_IFUNC |
f5385ebf AM |
2884 | && (h->def_regular |
2885 | || !h->def_dynamic | |
2886 | || (!h->ref_regular | |
60d67dc8 | 2887 | && (!h->is_weakalias || weakdef (h)->dynindx == -1)))) |
45d6a902 | 2888 | { |
a6aa5195 | 2889 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2890 | return TRUE; |
2891 | } | |
2892 | ||
2893 | /* If we've already adjusted this symbol, don't do it again. This | |
2894 | can happen via a recursive call. */ | |
f5385ebf | 2895 | if (h->dynamic_adjusted) |
45d6a902 AM |
2896 | return TRUE; |
2897 | ||
2898 | /* Don't look at this symbol again. Note that we must set this | |
2899 | after checking the above conditions, because we may look at a | |
2900 | symbol once, decide not to do anything, and then get called | |
2901 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2902 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2903 | |
2904 | /* If this is a weak definition, and we know a real definition, and | |
2905 | the real symbol is not itself defined by a regular object file, | |
2906 | then get a good value for the real definition. We handle the | |
2907 | real symbol first, for the convenience of the backend routine. | |
2908 | ||
2909 | Note that there is a confusing case here. If the real definition | |
2910 | is defined by a regular object file, we don't get the real symbol | |
2911 | from the dynamic object, but we do get the weak symbol. If the | |
2912 | processor backend uses a COPY reloc, then if some routine in the | |
2913 | dynamic object changes the real symbol, we will not see that | |
2914 | change in the corresponding weak symbol. This is the way other | |
2915 | ELF linkers work as well, and seems to be a result of the shared | |
2916 | library model. | |
2917 | ||
2918 | I will clarify this issue. Most SVR4 shared libraries define the | |
2919 | variable _timezone and define timezone as a weak synonym. The | |
2920 | tzset call changes _timezone. If you write | |
2921 | extern int timezone; | |
2922 | int _timezone = 5; | |
2923 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2924 | you might expect that, since timezone is a synonym for _timezone, | |
2925 | the same number will print both times. However, if the processor | |
2926 | backend uses a COPY reloc, then actually timezone will be copied | |
2927 | into your process image, and, since you define _timezone | |
2928 | yourself, _timezone will not. Thus timezone and _timezone will | |
2929 | wind up at different memory locations. The tzset call will set | |
2930 | _timezone, leaving timezone unchanged. */ | |
2931 | ||
60d67dc8 | 2932 | if (h->is_weakalias) |
45d6a902 | 2933 | { |
60d67dc8 AM |
2934 | struct elf_link_hash_entry *def = weakdef (h); |
2935 | ||
ec24dc88 | 2936 | /* If we get to this point, there is an implicit reference to |
60d67dc8 AM |
2937 | the alias by a regular object file via the weak symbol H. */ |
2938 | def->ref_regular = 1; | |
45d6a902 | 2939 | |
ec24dc88 | 2940 | /* Ensure that the backend adjust_dynamic_symbol function sees |
60d67dc8 AM |
2941 | the strong alias before H by recursively calling ourselves. */ |
2942 | if (!_bfd_elf_adjust_dynamic_symbol (def, eif)) | |
45d6a902 AM |
2943 | return FALSE; |
2944 | } | |
2945 | ||
2946 | /* If a symbol has no type and no size and does not require a PLT | |
2947 | entry, then we are probably about to do the wrong thing here: we | |
2948 | are probably going to create a COPY reloc for an empty object. | |
2949 | This case can arise when a shared object is built with assembly | |
2950 | code, and the assembly code fails to set the symbol type. */ | |
2951 | if (h->size == 0 | |
2952 | && h->type == STT_NOTYPE | |
f5385ebf | 2953 | && !h->needs_plt) |
4eca0228 | 2954 | _bfd_error_handler |
45d6a902 AM |
2955 | (_("warning: type and size of dynamic symbol `%s' are not defined"), |
2956 | h->root.root.string); | |
2957 | ||
45d6a902 AM |
2958 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) |
2959 | { | |
2960 | eif->failed = TRUE; | |
2961 | return FALSE; | |
2962 | } | |
2963 | ||
2964 | return TRUE; | |
2965 | } | |
2966 | ||
027297b7 L |
2967 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
2968 | DYNBSS. */ | |
2969 | ||
2970 | bfd_boolean | |
6cabe1ea AM |
2971 | _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, |
2972 | struct elf_link_hash_entry *h, | |
027297b7 L |
2973 | asection *dynbss) |
2974 | { | |
91ac5911 | 2975 | unsigned int power_of_two; |
027297b7 L |
2976 | bfd_vma mask; |
2977 | asection *sec = h->root.u.def.section; | |
2978 | ||
de194d85 | 2979 | /* The section alignment of the definition is the maximum alignment |
91ac5911 L |
2980 | requirement of symbols defined in the section. Since we don't |
2981 | know the symbol alignment requirement, we start with the | |
2982 | maximum alignment and check low bits of the symbol address | |
2983 | for the minimum alignment. */ | |
2984 | power_of_two = bfd_get_section_alignment (sec->owner, sec); | |
2985 | mask = ((bfd_vma) 1 << power_of_two) - 1; | |
2986 | while ((h->root.u.def.value & mask) != 0) | |
2987 | { | |
2988 | mask >>= 1; | |
2989 | --power_of_two; | |
2990 | } | |
027297b7 | 2991 | |
91ac5911 L |
2992 | if (power_of_two > bfd_get_section_alignment (dynbss->owner, |
2993 | dynbss)) | |
027297b7 L |
2994 | { |
2995 | /* Adjust the section alignment if needed. */ | |
2996 | if (! bfd_set_section_alignment (dynbss->owner, dynbss, | |
91ac5911 | 2997 | power_of_two)) |
027297b7 L |
2998 | return FALSE; |
2999 | } | |
3000 | ||
91ac5911 | 3001 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
3002 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
3003 | ||
3004 | /* Define the symbol as being at this point in DYNBSS. */ | |
3005 | h->root.u.def.section = dynbss; | |
3006 | h->root.u.def.value = dynbss->size; | |
3007 | ||
3008 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
3009 | dynbss->size += h->size; | |
3010 | ||
f7483970 L |
3011 | /* No error if extern_protected_data is true. */ |
3012 | if (h->protected_def | |
889c2a67 L |
3013 | && (!info->extern_protected_data |
3014 | || (info->extern_protected_data < 0 | |
3015 | && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) | |
d07a1b05 AM |
3016 | info->callbacks->einfo |
3017 | (_("%P: copy reloc against protected `%T' is dangerous\n"), | |
3018 | h->root.root.string); | |
6cabe1ea | 3019 | |
027297b7 L |
3020 | return TRUE; |
3021 | } | |
3022 | ||
45d6a902 AM |
3023 | /* Adjust all external symbols pointing into SEC_MERGE sections |
3024 | to reflect the object merging within the sections. */ | |
3025 | ||
28caa186 | 3026 | static bfd_boolean |
268b6b39 | 3027 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
3028 | { |
3029 | asection *sec; | |
3030 | ||
45d6a902 AM |
3031 | if ((h->root.type == bfd_link_hash_defined |
3032 | || h->root.type == bfd_link_hash_defweak) | |
3033 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
dbaa2011 | 3034 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
45d6a902 | 3035 | { |
a50b1753 | 3036 | bfd *output_bfd = (bfd *) data; |
45d6a902 AM |
3037 | |
3038 | h->root.u.def.value = | |
3039 | _bfd_merged_section_offset (output_bfd, | |
3040 | &h->root.u.def.section, | |
3041 | elf_section_data (sec)->sec_info, | |
753731ee | 3042 | h->root.u.def.value); |
45d6a902 AM |
3043 | } |
3044 | ||
3045 | return TRUE; | |
3046 | } | |
986a241f RH |
3047 | |
3048 | /* Returns false if the symbol referred to by H should be considered | |
3049 | to resolve local to the current module, and true if it should be | |
3050 | considered to bind dynamically. */ | |
3051 | ||
3052 | bfd_boolean | |
268b6b39 AM |
3053 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
3054 | struct bfd_link_info *info, | |
89a2ee5a | 3055 | bfd_boolean not_local_protected) |
986a241f RH |
3056 | { |
3057 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
3058 | const struct elf_backend_data *bed; |
3059 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
3060 | |
3061 | if (h == NULL) | |
3062 | return FALSE; | |
3063 | ||
3064 | while (h->root.type == bfd_link_hash_indirect | |
3065 | || h->root.type == bfd_link_hash_warning) | |
3066 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3067 | ||
3068 | /* If it was forced local, then clearly it's not dynamic. */ | |
3069 | if (h->dynindx == -1) | |
3070 | return FALSE; | |
f5385ebf | 3071 | if (h->forced_local) |
986a241f RH |
3072 | return FALSE; |
3073 | ||
3074 | /* Identify the cases where name binding rules say that a | |
3075 | visible symbol resolves locally. */ | |
0e1862bb L |
3076 | binding_stays_local_p = (bfd_link_executable (info) |
3077 | || SYMBOLIC_BIND (info, h)); | |
986a241f RH |
3078 | |
3079 | switch (ELF_ST_VISIBILITY (h->other)) | |
3080 | { | |
3081 | case STV_INTERNAL: | |
3082 | case STV_HIDDEN: | |
3083 | return FALSE; | |
3084 | ||
3085 | case STV_PROTECTED: | |
fcb93ecf PB |
3086 | hash_table = elf_hash_table (info); |
3087 | if (!is_elf_hash_table (hash_table)) | |
3088 | return FALSE; | |
3089 | ||
3090 | bed = get_elf_backend_data (hash_table->dynobj); | |
3091 | ||
986a241f RH |
3092 | /* Proper resolution for function pointer equality may require |
3093 | that these symbols perhaps be resolved dynamically, even though | |
3094 | we should be resolving them to the current module. */ | |
89a2ee5a | 3095 | if (!not_local_protected || !bed->is_function_type (h->type)) |
986a241f RH |
3096 | binding_stays_local_p = TRUE; |
3097 | break; | |
3098 | ||
3099 | default: | |
986a241f RH |
3100 | break; |
3101 | } | |
3102 | ||
aa37626c | 3103 | /* If it isn't defined locally, then clearly it's dynamic. */ |
89a2ee5a | 3104 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
aa37626c L |
3105 | return TRUE; |
3106 | ||
986a241f RH |
3107 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
3108 | us that it remains local. */ | |
3109 | return !binding_stays_local_p; | |
3110 | } | |
f6c52c13 AM |
3111 | |
3112 | /* Return true if the symbol referred to by H should be considered | |
3113 | to resolve local to the current module, and false otherwise. Differs | |
3114 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2e76e85a | 3115 | undefined symbols. The two functions are virtually identical except |
0fad2956 MR |
3116 | for the place where dynindx == -1 is tested. If that test is true, |
3117 | _bfd_elf_dynamic_symbol_p will say the symbol is local, while | |
3118 | _bfd_elf_symbol_refs_local_p will say the symbol is local only for | |
3119 | defined symbols. | |
89a2ee5a AM |
3120 | It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as |
3121 | !_bfd_elf_symbol_refs_local_p, except that targets differ in their | |
3122 | treatment of undefined weak symbols. For those that do not make | |
3123 | undefined weak symbols dynamic, both functions may return false. */ | |
f6c52c13 AM |
3124 | |
3125 | bfd_boolean | |
268b6b39 AM |
3126 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
3127 | struct bfd_link_info *info, | |
3128 | bfd_boolean local_protected) | |
f6c52c13 | 3129 | { |
fcb93ecf PB |
3130 | const struct elf_backend_data *bed; |
3131 | struct elf_link_hash_table *hash_table; | |
3132 | ||
f6c52c13 AM |
3133 | /* If it's a local sym, of course we resolve locally. */ |
3134 | if (h == NULL) | |
3135 | return TRUE; | |
3136 | ||
d95edcac L |
3137 | /* STV_HIDDEN or STV_INTERNAL ones must be local. */ |
3138 | if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
3139 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
3140 | return TRUE; | |
3141 | ||
0fad2956 MR |
3142 | /* Forced local symbols resolve locally. */ |
3143 | if (h->forced_local) | |
3144 | return TRUE; | |
3145 | ||
7e2294f9 AO |
3146 | /* Common symbols that become definitions don't get the DEF_REGULAR |
3147 | flag set, so test it first, and don't bail out. */ | |
3148 | if (ELF_COMMON_DEF_P (h)) | |
3149 | /* Do nothing. */; | |
f6c52c13 | 3150 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
3151 | resolve locally. The sym is either undefined or dynamic. */ |
3152 | else if (!h->def_regular) | |
f6c52c13 AM |
3153 | return FALSE; |
3154 | ||
0fad2956 | 3155 | /* Non-dynamic symbols resolve locally. */ |
f6c52c13 AM |
3156 | if (h->dynindx == -1) |
3157 | return TRUE; | |
3158 | ||
3159 | /* At this point, we know the symbol is defined and dynamic. In an | |
3160 | executable it must resolve locally, likewise when building symbolic | |
3161 | shared libraries. */ | |
0e1862bb | 3162 | if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
3163 | return TRUE; |
3164 | ||
3165 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
3166 | with default visibility might not resolve locally. */ | |
3167 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
3168 | return FALSE; | |
3169 | ||
fcb93ecf PB |
3170 | hash_table = elf_hash_table (info); |
3171 | if (!is_elf_hash_table (hash_table)) | |
3172 | return TRUE; | |
3173 | ||
3174 | bed = get_elf_backend_data (hash_table->dynobj); | |
3175 | ||
f7483970 L |
3176 | /* If extern_protected_data is false, STV_PROTECTED non-function |
3177 | symbols are local. */ | |
889c2a67 L |
3178 | if ((!info->extern_protected_data |
3179 | || (info->extern_protected_data < 0 | |
3180 | && !bed->extern_protected_data)) | |
3181 | && !bed->is_function_type (h->type)) | |
1c16dfa5 L |
3182 | return TRUE; |
3183 | ||
f6c52c13 | 3184 | /* Function pointer equality tests may require that STV_PROTECTED |
2676a7d9 AM |
3185 | symbols be treated as dynamic symbols. If the address of a |
3186 | function not defined in an executable is set to that function's | |
3187 | plt entry in the executable, then the address of the function in | |
3188 | a shared library must also be the plt entry in the executable. */ | |
f6c52c13 AM |
3189 | return local_protected; |
3190 | } | |
e1918d23 AM |
3191 | |
3192 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
3193 | aligned. Returns the first TLS output section. */ | |
3194 | ||
3195 | struct bfd_section * | |
3196 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
3197 | { | |
3198 | struct bfd_section *sec, *tls; | |
3199 | unsigned int align = 0; | |
3200 | ||
3201 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
3202 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
3203 | break; | |
3204 | tls = sec; | |
3205 | ||
3206 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
3207 | if (sec->alignment_power > align) | |
3208 | align = sec->alignment_power; | |
3209 | ||
3210 | elf_hash_table (info)->tls_sec = tls; | |
3211 | ||
3212 | /* Ensure the alignment of the first section is the largest alignment, | |
3213 | so that the tls segment starts aligned. */ | |
3214 | if (tls != NULL) | |
3215 | tls->alignment_power = align; | |
3216 | ||
3217 | return tls; | |
3218 | } | |
0ad989f9 L |
3219 | |
3220 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
3221 | static bfd_boolean | |
3222 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
3223 | Elf_Internal_Sym *sym) | |
3224 | { | |
a4d8e49b L |
3225 | const struct elf_backend_data *bed; |
3226 | ||
0ad989f9 L |
3227 | /* Local symbols do not count, but target specific ones might. */ |
3228 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
3229 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
3230 | return FALSE; | |
3231 | ||
fcb93ecf | 3232 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 3233 | /* Function symbols do not count. */ |
fcb93ecf | 3234 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
3235 | return FALSE; |
3236 | ||
3237 | /* If the section is undefined, then so is the symbol. */ | |
3238 | if (sym->st_shndx == SHN_UNDEF) | |
3239 | return FALSE; | |
3240 | ||
3241 | /* If the symbol is defined in the common section, then | |
3242 | it is a common definition and so does not count. */ | |
a4d8e49b | 3243 | if (bed->common_definition (sym)) |
0ad989f9 L |
3244 | return FALSE; |
3245 | ||
3246 | /* If the symbol is in a target specific section then we | |
3247 | must rely upon the backend to tell us what it is. */ | |
3248 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
3249 | /* FIXME - this function is not coded yet: | |
3250 | ||
3251 | return _bfd_is_global_symbol_definition (abfd, sym); | |
3252 | ||
3253 | Instead for now assume that the definition is not global, | |
3254 | Even if this is wrong, at least the linker will behave | |
3255 | in the same way that it used to do. */ | |
3256 | return FALSE; | |
3257 | ||
3258 | return TRUE; | |
3259 | } | |
3260 | ||
3261 | /* Search the symbol table of the archive element of the archive ABFD | |
3262 | whose archive map contains a mention of SYMDEF, and determine if | |
3263 | the symbol is defined in this element. */ | |
3264 | static bfd_boolean | |
3265 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
3266 | { | |
3267 | Elf_Internal_Shdr * hdr; | |
ef53be89 AM |
3268 | size_t symcount; |
3269 | size_t extsymcount; | |
3270 | size_t extsymoff; | |
0ad989f9 L |
3271 | Elf_Internal_Sym *isymbuf; |
3272 | Elf_Internal_Sym *isym; | |
3273 | Elf_Internal_Sym *isymend; | |
3274 | bfd_boolean result; | |
3275 | ||
3276 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
3277 | if (abfd == NULL) | |
3278 | return FALSE; | |
3279 | ||
3280 | if (! bfd_check_format (abfd, bfd_object)) | |
3281 | return FALSE; | |
3282 | ||
7dc3990e L |
3283 | /* Select the appropriate symbol table. If we don't know if the |
3284 | object file is an IR object, give linker LTO plugin a chance to | |
3285 | get the correct symbol table. */ | |
3286 | if (abfd->plugin_format == bfd_plugin_yes | |
08ce1d72 | 3287 | #if BFD_SUPPORTS_PLUGINS |
7dc3990e L |
3288 | || (abfd->plugin_format == bfd_plugin_unknown |
3289 | && bfd_link_plugin_object_p (abfd)) | |
3290 | #endif | |
3291 | ) | |
3292 | { | |
3293 | /* Use the IR symbol table if the object has been claimed by | |
3294 | plugin. */ | |
3295 | abfd = abfd->plugin_dummy_bfd; | |
3296 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3297 | } | |
3298 | else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
0ad989f9 L |
3299 | hdr = &elf_tdata (abfd)->symtab_hdr; |
3300 | else | |
3301 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3302 | ||
3303 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3304 | ||
3305 | /* The sh_info field of the symtab header tells us where the | |
3306 | external symbols start. We don't care about the local symbols. */ | |
3307 | if (elf_bad_symtab (abfd)) | |
3308 | { | |
3309 | extsymcount = symcount; | |
3310 | extsymoff = 0; | |
3311 | } | |
3312 | else | |
3313 | { | |
3314 | extsymcount = symcount - hdr->sh_info; | |
3315 | extsymoff = hdr->sh_info; | |
3316 | } | |
3317 | ||
3318 | if (extsymcount == 0) | |
3319 | return FALSE; | |
3320 | ||
3321 | /* Read in the symbol table. */ | |
3322 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3323 | NULL, NULL, NULL); | |
3324 | if (isymbuf == NULL) | |
3325 | return FALSE; | |
3326 | ||
3327 | /* Scan the symbol table looking for SYMDEF. */ | |
3328 | result = FALSE; | |
3329 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
3330 | { | |
3331 | const char *name; | |
3332 | ||
3333 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3334 | isym->st_name); | |
3335 | if (name == NULL) | |
3336 | break; | |
3337 | ||
3338 | if (strcmp (name, symdef->name) == 0) | |
3339 | { | |
3340 | result = is_global_data_symbol_definition (abfd, isym); | |
3341 | break; | |
3342 | } | |
3343 | } | |
3344 | ||
3345 | free (isymbuf); | |
3346 | ||
3347 | return result; | |
3348 | } | |
3349 | \f | |
5a580b3a AM |
3350 | /* Add an entry to the .dynamic table. */ |
3351 | ||
3352 | bfd_boolean | |
3353 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
3354 | bfd_vma tag, | |
3355 | bfd_vma val) | |
3356 | { | |
3357 | struct elf_link_hash_table *hash_table; | |
3358 | const struct elf_backend_data *bed; | |
3359 | asection *s; | |
3360 | bfd_size_type newsize; | |
3361 | bfd_byte *newcontents; | |
3362 | Elf_Internal_Dyn dyn; | |
3363 | ||
3364 | hash_table = elf_hash_table (info); | |
3365 | if (! is_elf_hash_table (hash_table)) | |
3366 | return FALSE; | |
3367 | ||
3368 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3369 | s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
5a580b3a AM |
3370 | BFD_ASSERT (s != NULL); |
3371 | ||
eea6121a | 3372 | newsize = s->size + bed->s->sizeof_dyn; |
a50b1753 | 3373 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
5a580b3a AM |
3374 | if (newcontents == NULL) |
3375 | return FALSE; | |
3376 | ||
3377 | dyn.d_tag = tag; | |
3378 | dyn.d_un.d_val = val; | |
eea6121a | 3379 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3380 | |
eea6121a | 3381 | s->size = newsize; |
5a580b3a AM |
3382 | s->contents = newcontents; |
3383 | ||
3384 | return TRUE; | |
3385 | } | |
3386 | ||
3387 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
3388 | otherwise just check whether one already exists. Returns -1 on error, | |
3389 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
3390 | ||
4ad4eba5 | 3391 | static int |
7e9f0867 AM |
3392 | elf_add_dt_needed_tag (bfd *abfd, |
3393 | struct bfd_link_info *info, | |
4ad4eba5 AM |
3394 | const char *soname, |
3395 | bfd_boolean do_it) | |
5a580b3a AM |
3396 | { |
3397 | struct elf_link_hash_table *hash_table; | |
ef53be89 | 3398 | size_t strindex; |
5a580b3a | 3399 | |
7e9f0867 AM |
3400 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3401 | return -1; | |
3402 | ||
5a580b3a | 3403 | hash_table = elf_hash_table (info); |
5a580b3a | 3404 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); |
ef53be89 | 3405 | if (strindex == (size_t) -1) |
5a580b3a AM |
3406 | return -1; |
3407 | ||
02be4619 | 3408 | if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) |
5a580b3a AM |
3409 | { |
3410 | asection *sdyn; | |
3411 | const struct elf_backend_data *bed; | |
3412 | bfd_byte *extdyn; | |
3413 | ||
3414 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3415 | sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
7e9f0867 AM |
3416 | if (sdyn != NULL) |
3417 | for (extdyn = sdyn->contents; | |
3418 | extdyn < sdyn->contents + sdyn->size; | |
3419 | extdyn += bed->s->sizeof_dyn) | |
3420 | { | |
3421 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3422 | |
7e9f0867 AM |
3423 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3424 | if (dyn.d_tag == DT_NEEDED | |
3425 | && dyn.d_un.d_val == strindex) | |
3426 | { | |
3427 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3428 | return 1; | |
3429 | } | |
3430 | } | |
5a580b3a AM |
3431 | } |
3432 | ||
3433 | if (do_it) | |
3434 | { | |
7e9f0867 AM |
3435 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3436 | return -1; | |
3437 | ||
5a580b3a AM |
3438 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3439 | return -1; | |
3440 | } | |
3441 | else | |
3442 | /* We were just checking for existence of the tag. */ | |
3443 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3444 | ||
3445 | return 0; | |
3446 | } | |
3447 | ||
7b15fa7a AM |
3448 | /* Return true if SONAME is on the needed list between NEEDED and STOP |
3449 | (or the end of list if STOP is NULL), and needed by a library that | |
3450 | will be loaded. */ | |
3451 | ||
010e5ae2 | 3452 | static bfd_boolean |
7b15fa7a AM |
3453 | on_needed_list (const char *soname, |
3454 | struct bfd_link_needed_list *needed, | |
3455 | struct bfd_link_needed_list *stop) | |
010e5ae2 | 3456 | { |
7b15fa7a AM |
3457 | struct bfd_link_needed_list *look; |
3458 | for (look = needed; look != stop; look = look->next) | |
3459 | if (strcmp (soname, look->name) == 0 | |
3460 | && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 | |
3461 | /* If needed by a library that itself is not directly | |
3462 | needed, recursively check whether that library is | |
3463 | indirectly needed. Since we add DT_NEEDED entries to | |
3464 | the end of the list, library dependencies appear after | |
3465 | the library. Therefore search prior to the current | |
3466 | LOOK, preventing possible infinite recursion. */ | |
3467 | || on_needed_list (elf_dt_name (look->by), needed, look))) | |
010e5ae2 AM |
3468 | return TRUE; |
3469 | ||
3470 | return FALSE; | |
3471 | } | |
3472 | ||
14160578 | 3473 | /* Sort symbol by value, section, and size. */ |
4ad4eba5 AM |
3474 | static int |
3475 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3476 | { |
3477 | const struct elf_link_hash_entry *h1; | |
3478 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3479 | bfd_signed_vma vdiff; |
5a580b3a AM |
3480 | |
3481 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3482 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3483 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3484 | if (vdiff != 0) | |
3485 | return vdiff > 0 ? 1 : -1; | |
3486 | else | |
3487 | { | |
d3435ae8 | 3488 | int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; |
10b7e05b NC |
3489 | if (sdiff != 0) |
3490 | return sdiff > 0 ? 1 : -1; | |
3491 | } | |
14160578 AM |
3492 | vdiff = h1->size - h2->size; |
3493 | return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1; | |
5a580b3a | 3494 | } |
4ad4eba5 | 3495 | |
5a580b3a AM |
3496 | /* This function is used to adjust offsets into .dynstr for |
3497 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3498 | ||
3499 | static bfd_boolean | |
3500 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3501 | { | |
a50b1753 | 3502 | struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; |
5a580b3a | 3503 | |
5a580b3a AM |
3504 | if (h->dynindx != -1) |
3505 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3506 | return TRUE; | |
3507 | } | |
3508 | ||
3509 | /* Assign string offsets in .dynstr, update all structures referencing | |
3510 | them. */ | |
3511 | ||
4ad4eba5 AM |
3512 | static bfd_boolean |
3513 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3514 | { |
3515 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3516 | struct elf_link_local_dynamic_entry *entry; | |
3517 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3518 | bfd *dynobj = hash_table->dynobj; | |
3519 | asection *sdyn; | |
3520 | bfd_size_type size; | |
3521 | const struct elf_backend_data *bed; | |
3522 | bfd_byte *extdyn; | |
3523 | ||
3524 | _bfd_elf_strtab_finalize (dynstr); | |
3525 | size = _bfd_elf_strtab_size (dynstr); | |
3526 | ||
3527 | bed = get_elf_backend_data (dynobj); | |
3d4d4302 | 3528 | sdyn = bfd_get_linker_section (dynobj, ".dynamic"); |
5a580b3a AM |
3529 | BFD_ASSERT (sdyn != NULL); |
3530 | ||
3531 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3532 | for (extdyn = sdyn->contents; | |
eea6121a | 3533 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3534 | extdyn += bed->s->sizeof_dyn) |
3535 | { | |
3536 | Elf_Internal_Dyn dyn; | |
3537 | ||
3538 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3539 | switch (dyn.d_tag) | |
3540 | { | |
3541 | case DT_STRSZ: | |
3542 | dyn.d_un.d_val = size; | |
3543 | break; | |
3544 | case DT_NEEDED: | |
3545 | case DT_SONAME: | |
3546 | case DT_RPATH: | |
3547 | case DT_RUNPATH: | |
3548 | case DT_FILTER: | |
3549 | case DT_AUXILIARY: | |
7ee314fa AM |
3550 | case DT_AUDIT: |
3551 | case DT_DEPAUDIT: | |
5a580b3a AM |
3552 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); |
3553 | break; | |
3554 | default: | |
3555 | continue; | |
3556 | } | |
3557 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3558 | } | |
3559 | ||
3560 | /* Now update local dynamic symbols. */ | |
3561 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3562 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3563 | entry->isym.st_name); | |
3564 | ||
3565 | /* And the rest of dynamic symbols. */ | |
3566 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3567 | ||
3568 | /* Adjust version definitions. */ | |
3569 | if (elf_tdata (output_bfd)->cverdefs) | |
3570 | { | |
3571 | asection *s; | |
3572 | bfd_byte *p; | |
ef53be89 | 3573 | size_t i; |
5a580b3a AM |
3574 | Elf_Internal_Verdef def; |
3575 | Elf_Internal_Verdaux defaux; | |
3576 | ||
3d4d4302 | 3577 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
3578 | p = s->contents; |
3579 | do | |
3580 | { | |
3581 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3582 | &def); | |
3583 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3584 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3585 | continue; | |
5a580b3a AM |
3586 | for (i = 0; i < def.vd_cnt; ++i) |
3587 | { | |
3588 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3589 | (Elf_External_Verdaux *) p, &defaux); | |
3590 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3591 | defaux.vda_name); | |
3592 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3593 | &defaux, (Elf_External_Verdaux *) p); | |
3594 | p += sizeof (Elf_External_Verdaux); | |
3595 | } | |
3596 | } | |
3597 | while (def.vd_next); | |
3598 | } | |
3599 | ||
3600 | /* Adjust version references. */ | |
3601 | if (elf_tdata (output_bfd)->verref) | |
3602 | { | |
3603 | asection *s; | |
3604 | bfd_byte *p; | |
ef53be89 | 3605 | size_t i; |
5a580b3a AM |
3606 | Elf_Internal_Verneed need; |
3607 | Elf_Internal_Vernaux needaux; | |
3608 | ||
3d4d4302 | 3609 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
3610 | p = s->contents; |
3611 | do | |
3612 | { | |
3613 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3614 | &need); | |
3615 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3616 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3617 | (Elf_External_Verneed *) p); | |
3618 | p += sizeof (Elf_External_Verneed); | |
3619 | for (i = 0; i < need.vn_cnt; ++i) | |
3620 | { | |
3621 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3622 | (Elf_External_Vernaux *) p, &needaux); | |
3623 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3624 | needaux.vna_name); | |
3625 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3626 | &needaux, | |
3627 | (Elf_External_Vernaux *) p); | |
3628 | p += sizeof (Elf_External_Vernaux); | |
3629 | } | |
3630 | } | |
3631 | while (need.vn_next); | |
3632 | } | |
3633 | ||
3634 | return TRUE; | |
3635 | } | |
3636 | \f | |
13285a1b AM |
3637 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
3638 | The default is to only match when the INPUT and OUTPUT are exactly | |
3639 | the same target. */ | |
3640 | ||
3641 | bfd_boolean | |
3642 | _bfd_elf_default_relocs_compatible (const bfd_target *input, | |
3643 | const bfd_target *output) | |
3644 | { | |
3645 | return input == output; | |
3646 | } | |
3647 | ||
3648 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
3649 | This version is used when different targets for the same architecture | |
3650 | are virtually identical. */ | |
3651 | ||
3652 | bfd_boolean | |
3653 | _bfd_elf_relocs_compatible (const bfd_target *input, | |
3654 | const bfd_target *output) | |
3655 | { | |
3656 | const struct elf_backend_data *obed, *ibed; | |
3657 | ||
3658 | if (input == output) | |
3659 | return TRUE; | |
3660 | ||
3661 | ibed = xvec_get_elf_backend_data (input); | |
3662 | obed = xvec_get_elf_backend_data (output); | |
3663 | ||
3664 | if (ibed->arch != obed->arch) | |
3665 | return FALSE; | |
3666 | ||
3667 | /* If both backends are using this function, deem them compatible. */ | |
3668 | return ibed->relocs_compatible == obed->relocs_compatible; | |
3669 | } | |
3670 | ||
e5034e59 AM |
3671 | /* Make a special call to the linker "notice" function to tell it that |
3672 | we are about to handle an as-needed lib, or have finished | |
1b786873 | 3673 | processing the lib. */ |
e5034e59 AM |
3674 | |
3675 | bfd_boolean | |
3676 | _bfd_elf_notice_as_needed (bfd *ibfd, | |
3677 | struct bfd_link_info *info, | |
3678 | enum notice_asneeded_action act) | |
3679 | { | |
46135103 | 3680 | return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); |
e5034e59 AM |
3681 | } |
3682 | ||
d9689752 L |
3683 | /* Check relocations an ELF object file. */ |
3684 | ||
3685 | bfd_boolean | |
3686 | _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) | |
3687 | { | |
3688 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3689 | struct elf_link_hash_table *htab = elf_hash_table (info); | |
3690 | ||
3691 | /* If this object is the same format as the output object, and it is | |
3692 | not a shared library, then let the backend look through the | |
3693 | relocs. | |
3694 | ||
3695 | This is required to build global offset table entries and to | |
3696 | arrange for dynamic relocs. It is not required for the | |
3697 | particular common case of linking non PIC code, even when linking | |
3698 | against shared libraries, but unfortunately there is no way of | |
3699 | knowing whether an object file has been compiled PIC or not. | |
3700 | Looking through the relocs is not particularly time consuming. | |
3701 | The problem is that we must either (1) keep the relocs in memory, | |
3702 | which causes the linker to require additional runtime memory or | |
3703 | (2) read the relocs twice from the input file, which wastes time. | |
3704 | This would be a good case for using mmap. | |
3705 | ||
3706 | I have no idea how to handle linking PIC code into a file of a | |
3707 | different format. It probably can't be done. */ | |
3708 | if ((abfd->flags & DYNAMIC) == 0 | |
3709 | && is_elf_hash_table (htab) | |
3710 | && bed->check_relocs != NULL | |
3711 | && elf_object_id (abfd) == elf_hash_table_id (htab) | |
3712 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
3713 | { | |
3714 | asection *o; | |
3715 | ||
3716 | for (o = abfd->sections; o != NULL; o = o->next) | |
3717 | { | |
3718 | Elf_Internal_Rela *internal_relocs; | |
3719 | bfd_boolean ok; | |
3720 | ||
5ce03cea | 3721 | /* Don't check relocations in excluded sections. */ |
d9689752 | 3722 | if ((o->flags & SEC_RELOC) == 0 |
5ce03cea | 3723 | || (o->flags & SEC_EXCLUDE) != 0 |
d9689752 L |
3724 | || o->reloc_count == 0 |
3725 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
3726 | && (o->flags & SEC_DEBUGGING) != 0) | |
3727 | || bfd_is_abs_section (o->output_section)) | |
3728 | continue; | |
3729 | ||
3730 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
3731 | info->keep_memory); | |
3732 | if (internal_relocs == NULL) | |
3733 | return FALSE; | |
3734 | ||
3735 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); | |
3736 | ||
3737 | if (elf_section_data (o)->relocs != internal_relocs) | |
3738 | free (internal_relocs); | |
3739 | ||
3740 | if (! ok) | |
3741 | return FALSE; | |
3742 | } | |
3743 | } | |
3744 | ||
3745 | return TRUE; | |
3746 | } | |
3747 | ||
4ad4eba5 AM |
3748 | /* Add symbols from an ELF object file to the linker hash table. */ |
3749 | ||
3750 | static bfd_boolean | |
3751 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3752 | { | |
a0c402a5 | 3753 | Elf_Internal_Ehdr *ehdr; |
4ad4eba5 | 3754 | Elf_Internal_Shdr *hdr; |
ef53be89 AM |
3755 | size_t symcount; |
3756 | size_t extsymcount; | |
3757 | size_t extsymoff; | |
4ad4eba5 AM |
3758 | struct elf_link_hash_entry **sym_hash; |
3759 | bfd_boolean dynamic; | |
3760 | Elf_External_Versym *extversym = NULL; | |
3761 | Elf_External_Versym *ever; | |
3762 | struct elf_link_hash_entry *weaks; | |
3763 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
ef53be89 | 3764 | size_t nondeflt_vers_cnt = 0; |
4ad4eba5 AM |
3765 | Elf_Internal_Sym *isymbuf = NULL; |
3766 | Elf_Internal_Sym *isym; | |
3767 | Elf_Internal_Sym *isymend; | |
3768 | const struct elf_backend_data *bed; | |
3769 | bfd_boolean add_needed; | |
66eb6687 | 3770 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3771 | bfd_size_type amt; |
66eb6687 | 3772 | void *alloc_mark = NULL; |
4f87808c AM |
3773 | struct bfd_hash_entry **old_table = NULL; |
3774 | unsigned int old_size = 0; | |
3775 | unsigned int old_count = 0; | |
66eb6687 | 3776 | void *old_tab = NULL; |
66eb6687 AM |
3777 | void *old_ent; |
3778 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3779 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
5b677558 | 3780 | void *old_strtab = NULL; |
66eb6687 | 3781 | size_t tabsize = 0; |
db6a5d5f | 3782 | asection *s; |
29a9f53e | 3783 | bfd_boolean just_syms; |
4ad4eba5 | 3784 | |
66eb6687 | 3785 | htab = elf_hash_table (info); |
4ad4eba5 | 3786 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3787 | |
3788 | if ((abfd->flags & DYNAMIC) == 0) | |
3789 | dynamic = FALSE; | |
3790 | else | |
3791 | { | |
3792 | dynamic = TRUE; | |
3793 | ||
3794 | /* You can't use -r against a dynamic object. Also, there's no | |
3795 | hope of using a dynamic object which does not exactly match | |
3796 | the format of the output file. */ | |
0e1862bb | 3797 | if (bfd_link_relocatable (info) |
66eb6687 | 3798 | || !is_elf_hash_table (htab) |
f13a99db | 3799 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 3800 | { |
0e1862bb | 3801 | if (bfd_link_relocatable (info)) |
9a0789ec NC |
3802 | bfd_set_error (bfd_error_invalid_operation); |
3803 | else | |
3804 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3805 | goto error_return; |
3806 | } | |
3807 | } | |
3808 | ||
a0c402a5 L |
3809 | ehdr = elf_elfheader (abfd); |
3810 | if (info->warn_alternate_em | |
3811 | && bed->elf_machine_code != ehdr->e_machine | |
3812 | && ((bed->elf_machine_alt1 != 0 | |
3813 | && ehdr->e_machine == bed->elf_machine_alt1) | |
3814 | || (bed->elf_machine_alt2 != 0 | |
3815 | && ehdr->e_machine == bed->elf_machine_alt2))) | |
3816 | info->callbacks->einfo | |
695344c0 | 3817 | /* xgettext:c-format */ |
a0c402a5 L |
3818 | (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"), |
3819 | ehdr->e_machine, abfd, bed->elf_machine_code); | |
3820 | ||
4ad4eba5 AM |
3821 | /* As a GNU extension, any input sections which are named |
3822 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3823 | symbol. This differs from .gnu.warning sections, which generate | |
3824 | warnings when they are included in an output file. */ | |
dd98f8d2 | 3825 | /* PR 12761: Also generate this warning when building shared libraries. */ |
db6a5d5f | 3826 | for (s = abfd->sections; s != NULL; s = s->next) |
4ad4eba5 | 3827 | { |
db6a5d5f | 3828 | const char *name; |
4ad4eba5 | 3829 | |
db6a5d5f AM |
3830 | name = bfd_get_section_name (abfd, s); |
3831 | if (CONST_STRNEQ (name, ".gnu.warning.")) | |
4ad4eba5 | 3832 | { |
db6a5d5f AM |
3833 | char *msg; |
3834 | bfd_size_type sz; | |
3835 | ||
3836 | name += sizeof ".gnu.warning." - 1; | |
3837 | ||
3838 | /* If this is a shared object, then look up the symbol | |
3839 | in the hash table. If it is there, and it is already | |
3840 | been defined, then we will not be using the entry | |
3841 | from this shared object, so we don't need to warn. | |
3842 | FIXME: If we see the definition in a regular object | |
3843 | later on, we will warn, but we shouldn't. The only | |
3844 | fix is to keep track of what warnings we are supposed | |
3845 | to emit, and then handle them all at the end of the | |
3846 | link. */ | |
3847 | if (dynamic) | |
4ad4eba5 | 3848 | { |
db6a5d5f AM |
3849 | struct elf_link_hash_entry *h; |
3850 | ||
3851 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); | |
3852 | ||
3853 | /* FIXME: What about bfd_link_hash_common? */ | |
3854 | if (h != NULL | |
3855 | && (h->root.type == bfd_link_hash_defined | |
3856 | || h->root.type == bfd_link_hash_defweak)) | |
3857 | continue; | |
3858 | } | |
4ad4eba5 | 3859 | |
db6a5d5f AM |
3860 | sz = s->size; |
3861 | msg = (char *) bfd_alloc (abfd, sz + 1); | |
3862 | if (msg == NULL) | |
3863 | goto error_return; | |
4ad4eba5 | 3864 | |
db6a5d5f AM |
3865 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
3866 | goto error_return; | |
4ad4eba5 | 3867 | |
db6a5d5f | 3868 | msg[sz] = '\0'; |
4ad4eba5 | 3869 | |
db6a5d5f AM |
3870 | if (! (_bfd_generic_link_add_one_symbol |
3871 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3872 | FALSE, bed->collect, NULL))) | |
3873 | goto error_return; | |
4ad4eba5 | 3874 | |
0e1862bb | 3875 | if (bfd_link_executable (info)) |
db6a5d5f AM |
3876 | { |
3877 | /* Clobber the section size so that the warning does | |
3878 | not get copied into the output file. */ | |
3879 | s->size = 0; | |
11d2f718 | 3880 | |
db6a5d5f AM |
3881 | /* Also set SEC_EXCLUDE, so that symbols defined in |
3882 | the warning section don't get copied to the output. */ | |
3883 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3884 | } |
3885 | } | |
3886 | } | |
3887 | ||
29a9f53e L |
3888 | just_syms = ((s = abfd->sections) != NULL |
3889 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); | |
3890 | ||
4ad4eba5 AM |
3891 | add_needed = TRUE; |
3892 | if (! dynamic) | |
3893 | { | |
3894 | /* If we are creating a shared library, create all the dynamic | |
3895 | sections immediately. We need to attach them to something, | |
3896 | so we attach them to this BFD, provided it is the right | |
bf89386a L |
3897 | format and is not from ld --just-symbols. Always create the |
3898 | dynamic sections for -E/--dynamic-list. FIXME: If there | |
29a9f53e L |
3899 | are no input BFD's of the same format as the output, we can't |
3900 | make a shared library. */ | |
3901 | if (!just_syms | |
bf89386a | 3902 | && (bfd_link_pic (info) |
9c1d7a08 | 3903 | || (!bfd_link_relocatable (info) |
3c5fce9b | 3904 | && info->nointerp |
9c1d7a08 | 3905 | && (info->export_dynamic || info->dynamic))) |
66eb6687 | 3906 | && is_elf_hash_table (htab) |
f13a99db | 3907 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 3908 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
3909 | { |
3910 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3911 | goto error_return; | |
3912 | } | |
3913 | } | |
66eb6687 | 3914 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
3915 | goto error_return; |
3916 | else | |
3917 | { | |
4ad4eba5 | 3918 | const char *soname = NULL; |
7ee314fa | 3919 | char *audit = NULL; |
4ad4eba5 | 3920 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; |
9acc85a6 | 3921 | const Elf_Internal_Phdr *phdr; |
4ad4eba5 AM |
3922 | int ret; |
3923 | ||
3924 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3925 | ld shouldn't allow it. */ |
29a9f53e | 3926 | if (just_syms) |
92fd189d | 3927 | abort (); |
4ad4eba5 AM |
3928 | |
3929 | /* If this dynamic lib was specified on the command line with | |
3930 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3931 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3932 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3933 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3934 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3935 | all. */ | |
3936 | add_needed = (elf_dyn_lib_class (abfd) | |
3937 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3938 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3939 | |
3940 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3941 | if (s != NULL) | |
3942 | { | |
3943 | bfd_byte *dynbuf; | |
3944 | bfd_byte *extdyn; | |
cb33740c | 3945 | unsigned int elfsec; |
4ad4eba5 AM |
3946 | unsigned long shlink; |
3947 | ||
eea6121a | 3948 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
f8703194 L |
3949 | { |
3950 | error_free_dyn: | |
3951 | free (dynbuf); | |
3952 | goto error_return; | |
3953 | } | |
4ad4eba5 AM |
3954 | |
3955 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 3956 | if (elfsec == SHN_BAD) |
4ad4eba5 AM |
3957 | goto error_free_dyn; |
3958 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3959 | ||
3960 | for (extdyn = dynbuf; | |
eea6121a | 3961 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3962 | extdyn += bed->s->sizeof_dyn) |
3963 | { | |
3964 | Elf_Internal_Dyn dyn; | |
3965 | ||
3966 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3967 | if (dyn.d_tag == DT_SONAME) | |
3968 | { | |
3969 | unsigned int tagv = dyn.d_un.d_val; | |
3970 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3971 | if (soname == NULL) | |
3972 | goto error_free_dyn; | |
3973 | } | |
3974 | if (dyn.d_tag == DT_NEEDED) | |
3975 | { | |
3976 | struct bfd_link_needed_list *n, **pn; | |
3977 | char *fnm, *anm; | |
3978 | unsigned int tagv = dyn.d_un.d_val; | |
3979 | ||
3980 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3981 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3982 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3983 | if (n == NULL || fnm == NULL) | |
3984 | goto error_free_dyn; | |
3985 | amt = strlen (fnm) + 1; | |
a50b1753 | 3986 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3987 | if (anm == NULL) |
3988 | goto error_free_dyn; | |
3989 | memcpy (anm, fnm, amt); | |
3990 | n->name = anm; | |
3991 | n->by = abfd; | |
3992 | n->next = NULL; | |
66eb6687 | 3993 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3994 | ; |
3995 | *pn = n; | |
3996 | } | |
3997 | if (dyn.d_tag == DT_RUNPATH) | |
3998 | { | |
3999 | struct bfd_link_needed_list *n, **pn; | |
4000 | char *fnm, *anm; | |
4001 | unsigned int tagv = dyn.d_un.d_val; | |
4002 | ||
4003 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 4004 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4005 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4006 | if (n == NULL || fnm == NULL) | |
4007 | goto error_free_dyn; | |
4008 | amt = strlen (fnm) + 1; | |
a50b1753 | 4009 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4010 | if (anm == NULL) |
4011 | goto error_free_dyn; | |
4012 | memcpy (anm, fnm, amt); | |
4013 | n->name = anm; | |
4014 | n->by = abfd; | |
4015 | n->next = NULL; | |
4016 | for (pn = & runpath; | |
4017 | *pn != NULL; | |
4018 | pn = &(*pn)->next) | |
4019 | ; | |
4020 | *pn = n; | |
4021 | } | |
4022 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
4023 | if (!runpath && dyn.d_tag == DT_RPATH) | |
4024 | { | |
4025 | struct bfd_link_needed_list *n, **pn; | |
4026 | char *fnm, *anm; | |
4027 | unsigned int tagv = dyn.d_un.d_val; | |
4028 | ||
4029 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 4030 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4031 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4032 | if (n == NULL || fnm == NULL) | |
4033 | goto error_free_dyn; | |
4034 | amt = strlen (fnm) + 1; | |
a50b1753 | 4035 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 | 4036 | if (anm == NULL) |
f8703194 | 4037 | goto error_free_dyn; |
4ad4eba5 AM |
4038 | memcpy (anm, fnm, amt); |
4039 | n->name = anm; | |
4040 | n->by = abfd; | |
4041 | n->next = NULL; | |
4042 | for (pn = & rpath; | |
4043 | *pn != NULL; | |
4044 | pn = &(*pn)->next) | |
4045 | ; | |
4046 | *pn = n; | |
4047 | } | |
7ee314fa AM |
4048 | if (dyn.d_tag == DT_AUDIT) |
4049 | { | |
4050 | unsigned int tagv = dyn.d_un.d_val; | |
4051 | audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
4052 | } | |
4ad4eba5 AM |
4053 | } |
4054 | ||
4055 | free (dynbuf); | |
4056 | } | |
4057 | ||
4058 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
4059 | frees all more recently bfd_alloc'd blocks as well. */ | |
4060 | if (runpath) | |
4061 | rpath = runpath; | |
4062 | ||
4063 | if (rpath) | |
4064 | { | |
4065 | struct bfd_link_needed_list **pn; | |
66eb6687 | 4066 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
4067 | ; |
4068 | *pn = rpath; | |
4069 | } | |
4070 | ||
9acc85a6 AM |
4071 | /* If we have a PT_GNU_RELRO program header, mark as read-only |
4072 | all sections contained fully therein. This makes relro | |
4073 | shared library sections appear as they will at run-time. */ | |
4074 | phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum; | |
4075 | while (--phdr >= elf_tdata (abfd)->phdr) | |
4076 | if (phdr->p_type == PT_GNU_RELRO) | |
4077 | { | |
4078 | for (s = abfd->sections; s != NULL; s = s->next) | |
4079 | if ((s->flags & SEC_ALLOC) != 0 | |
4080 | && s->vma >= phdr->p_vaddr | |
4081 | && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz) | |
4082 | s->flags |= SEC_READONLY; | |
4083 | break; | |
4084 | } | |
4085 | ||
4ad4eba5 AM |
4086 | /* We do not want to include any of the sections in a dynamic |
4087 | object in the output file. We hack by simply clobbering the | |
4088 | list of sections in the BFD. This could be handled more | |
4089 | cleanly by, say, a new section flag; the existing | |
4090 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
4091 | still implies that the section takes up space in the output | |
4092 | file. */ | |
4093 | bfd_section_list_clear (abfd); | |
4094 | ||
4ad4eba5 AM |
4095 | /* Find the name to use in a DT_NEEDED entry that refers to this |
4096 | object. If the object has a DT_SONAME entry, we use it. | |
4097 | Otherwise, if the generic linker stuck something in | |
4098 | elf_dt_name, we use that. Otherwise, we just use the file | |
4099 | name. */ | |
4100 | if (soname == NULL || *soname == '\0') | |
4101 | { | |
4102 | soname = elf_dt_name (abfd); | |
4103 | if (soname == NULL || *soname == '\0') | |
4104 | soname = bfd_get_filename (abfd); | |
4105 | } | |
4106 | ||
4107 | /* Save the SONAME because sometimes the linker emulation code | |
4108 | will need to know it. */ | |
4109 | elf_dt_name (abfd) = soname; | |
4110 | ||
7e9f0867 | 4111 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4112 | if (ret < 0) |
4113 | goto error_return; | |
4114 | ||
4115 | /* If we have already included this dynamic object in the | |
4116 | link, just ignore it. There is no reason to include a | |
4117 | particular dynamic object more than once. */ | |
4118 | if (ret > 0) | |
4119 | return TRUE; | |
7ee314fa AM |
4120 | |
4121 | /* Save the DT_AUDIT entry for the linker emulation code. */ | |
68ffbac6 | 4122 | elf_dt_audit (abfd) = audit; |
4ad4eba5 AM |
4123 | } |
4124 | ||
4125 | /* If this is a dynamic object, we always link against the .dynsym | |
4126 | symbol table, not the .symtab symbol table. The dynamic linker | |
4127 | will only see the .dynsym symbol table, so there is no reason to | |
4128 | look at .symtab for a dynamic object. */ | |
4129 | ||
4130 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
4131 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
4132 | else | |
4133 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
4134 | ||
4135 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
4136 | ||
4137 | /* The sh_info field of the symtab header tells us where the | |
4138 | external symbols start. We don't care about the local symbols at | |
4139 | this point. */ | |
4140 | if (elf_bad_symtab (abfd)) | |
4141 | { | |
4142 | extsymcount = symcount; | |
4143 | extsymoff = 0; | |
4144 | } | |
4145 | else | |
4146 | { | |
4147 | extsymcount = symcount - hdr->sh_info; | |
4148 | extsymoff = hdr->sh_info; | |
4149 | } | |
4150 | ||
f45794cb | 4151 | sym_hash = elf_sym_hashes (abfd); |
012b2306 | 4152 | if (extsymcount != 0) |
4ad4eba5 AM |
4153 | { |
4154 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
4155 | NULL, NULL, NULL); | |
4156 | if (isymbuf == NULL) | |
4157 | goto error_return; | |
4158 | ||
4ad4eba5 | 4159 | if (sym_hash == NULL) |
012b2306 AM |
4160 | { |
4161 | /* We store a pointer to the hash table entry for each | |
4162 | external symbol. */ | |
ef53be89 AM |
4163 | amt = extsymcount; |
4164 | amt *= sizeof (struct elf_link_hash_entry *); | |
012b2306 AM |
4165 | sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); |
4166 | if (sym_hash == NULL) | |
4167 | goto error_free_sym; | |
4168 | elf_sym_hashes (abfd) = sym_hash; | |
4169 | } | |
4ad4eba5 AM |
4170 | } |
4171 | ||
4172 | if (dynamic) | |
4173 | { | |
4174 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
4175 | if (!_bfd_elf_slurp_version_tables (abfd, |
4176 | info->default_imported_symver)) | |
4ad4eba5 AM |
4177 | goto error_free_sym; |
4178 | ||
4179 | /* Read in the symbol versions, but don't bother to convert them | |
4180 | to internal format. */ | |
4181 | if (elf_dynversym (abfd) != 0) | |
4182 | { | |
4183 | Elf_Internal_Shdr *versymhdr; | |
4184 | ||
4185 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
a50b1753 | 4186 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
4ad4eba5 AM |
4187 | if (extversym == NULL) |
4188 | goto error_free_sym; | |
4189 | amt = versymhdr->sh_size; | |
4190 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
4191 | || bfd_bread (extversym, amt, abfd) != amt) | |
4192 | goto error_free_vers; | |
4193 | } | |
4194 | } | |
4195 | ||
66eb6687 AM |
4196 | /* If we are loading an as-needed shared lib, save the symbol table |
4197 | state before we start adding symbols. If the lib turns out | |
4198 | to be unneeded, restore the state. */ | |
4199 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4200 | { | |
4201 | unsigned int i; | |
4202 | size_t entsize; | |
4203 | ||
4204 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
4205 | { | |
4206 | struct bfd_hash_entry *p; | |
2de92251 | 4207 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4208 | |
4209 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
4210 | { |
4211 | h = (struct elf_link_hash_entry *) p; | |
4212 | entsize += htab->root.table.entsize; | |
4213 | if (h->root.type == bfd_link_hash_warning) | |
4214 | entsize += htab->root.table.entsize; | |
4215 | } | |
66eb6687 AM |
4216 | } |
4217 | ||
4218 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
f45794cb | 4219 | old_tab = bfd_malloc (tabsize + entsize); |
66eb6687 AM |
4220 | if (old_tab == NULL) |
4221 | goto error_free_vers; | |
4222 | ||
4223 | /* Remember the current objalloc pointer, so that all mem for | |
4224 | symbols added can later be reclaimed. */ | |
4225 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
4226 | if (alloc_mark == NULL) | |
4227 | goto error_free_vers; | |
4228 | ||
5061a885 AM |
4229 | /* Make a special call to the linker "notice" function to |
4230 | tell it that we are about to handle an as-needed lib. */ | |
e5034e59 | 4231 | if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) |
9af2a943 | 4232 | goto error_free_vers; |
5061a885 | 4233 | |
f45794cb AM |
4234 | /* Clone the symbol table. Remember some pointers into the |
4235 | symbol table, and dynamic symbol count. */ | |
4236 | old_ent = (char *) old_tab + tabsize; | |
66eb6687 | 4237 | memcpy (old_tab, htab->root.table.table, tabsize); |
66eb6687 AM |
4238 | old_undefs = htab->root.undefs; |
4239 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
4240 | old_table = htab->root.table.table; |
4241 | old_size = htab->root.table.size; | |
4242 | old_count = htab->root.table.count; | |
5b677558 AM |
4243 | old_strtab = _bfd_elf_strtab_save (htab->dynstr); |
4244 | if (old_strtab == NULL) | |
4245 | goto error_free_vers; | |
66eb6687 AM |
4246 | |
4247 | for (i = 0; i < htab->root.table.size; i++) | |
4248 | { | |
4249 | struct bfd_hash_entry *p; | |
2de92251 | 4250 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4251 | |
4252 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4253 | { | |
4254 | memcpy (old_ent, p, htab->root.table.entsize); | |
4255 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4256 | h = (struct elf_link_hash_entry *) p; |
4257 | if (h->root.type == bfd_link_hash_warning) | |
4258 | { | |
4259 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
4260 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4261 | } | |
66eb6687 AM |
4262 | } |
4263 | } | |
4264 | } | |
4ad4eba5 | 4265 | |
66eb6687 | 4266 | weaks = NULL; |
4ad4eba5 AM |
4267 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
4268 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
4269 | isym < isymend; | |
4270 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
4271 | { | |
4272 | int bind; | |
4273 | bfd_vma value; | |
af44c138 | 4274 | asection *sec, *new_sec; |
4ad4eba5 AM |
4275 | flagword flags; |
4276 | const char *name; | |
4277 | struct elf_link_hash_entry *h; | |
90c984fc | 4278 | struct elf_link_hash_entry *hi; |
4ad4eba5 AM |
4279 | bfd_boolean definition; |
4280 | bfd_boolean size_change_ok; | |
4281 | bfd_boolean type_change_ok; | |
37a9e49a L |
4282 | bfd_boolean new_weak; |
4283 | bfd_boolean old_weak; | |
4ad4eba5 | 4284 | bfd_boolean override; |
a4d8e49b | 4285 | bfd_boolean common; |
97196564 | 4286 | bfd_boolean discarded; |
4ad4eba5 AM |
4287 | unsigned int old_alignment; |
4288 | bfd *old_bfd; | |
6e33951e | 4289 | bfd_boolean matched; |
4ad4eba5 AM |
4290 | |
4291 | override = FALSE; | |
4292 | ||
4293 | flags = BSF_NO_FLAGS; | |
4294 | sec = NULL; | |
4295 | value = isym->st_value; | |
a4d8e49b | 4296 | common = bed->common_definition (isym); |
2980ccad L |
4297 | if (common && info->inhibit_common_definition) |
4298 | { | |
4299 | /* Treat common symbol as undefined for --no-define-common. */ | |
4300 | isym->st_shndx = SHN_UNDEF; | |
4301 | common = FALSE; | |
4302 | } | |
97196564 | 4303 | discarded = FALSE; |
4ad4eba5 AM |
4304 | |
4305 | bind = ELF_ST_BIND (isym->st_info); | |
3e7a7d11 | 4306 | switch (bind) |
4ad4eba5 | 4307 | { |
3e7a7d11 | 4308 | case STB_LOCAL: |
4ad4eba5 AM |
4309 | /* This should be impossible, since ELF requires that all |
4310 | global symbols follow all local symbols, and that sh_info | |
4311 | point to the first global symbol. Unfortunately, Irix 5 | |
4312 | screws this up. */ | |
4313 | continue; | |
3e7a7d11 NC |
4314 | |
4315 | case STB_GLOBAL: | |
a4d8e49b | 4316 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 | 4317 | flags = BSF_GLOBAL; |
3e7a7d11 NC |
4318 | break; |
4319 | ||
4320 | case STB_WEAK: | |
4321 | flags = BSF_WEAK; | |
4322 | break; | |
4323 | ||
4324 | case STB_GNU_UNIQUE: | |
4325 | flags = BSF_GNU_UNIQUE; | |
4326 | break; | |
4327 | ||
4328 | default: | |
4ad4eba5 | 4329 | /* Leave it up to the processor backend. */ |
3e7a7d11 | 4330 | break; |
4ad4eba5 AM |
4331 | } |
4332 | ||
4333 | if (isym->st_shndx == SHN_UNDEF) | |
4334 | sec = bfd_und_section_ptr; | |
cb33740c AM |
4335 | else if (isym->st_shndx == SHN_ABS) |
4336 | sec = bfd_abs_section_ptr; | |
4337 | else if (isym->st_shndx == SHN_COMMON) | |
4338 | { | |
4339 | sec = bfd_com_section_ptr; | |
4340 | /* What ELF calls the size we call the value. What ELF | |
4341 | calls the value we call the alignment. */ | |
4342 | value = isym->st_size; | |
4343 | } | |
4344 | else | |
4ad4eba5 AM |
4345 | { |
4346 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
4347 | if (sec == NULL) | |
4348 | sec = bfd_abs_section_ptr; | |
dbaa2011 | 4349 | else if (discarded_section (sec)) |
529fcb95 | 4350 | { |
e5d08002 L |
4351 | /* Symbols from discarded section are undefined. We keep |
4352 | its visibility. */ | |
529fcb95 | 4353 | sec = bfd_und_section_ptr; |
97196564 | 4354 | discarded = TRUE; |
529fcb95 PB |
4355 | isym->st_shndx = SHN_UNDEF; |
4356 | } | |
4ad4eba5 AM |
4357 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
4358 | value -= sec->vma; | |
4359 | } | |
4ad4eba5 AM |
4360 | |
4361 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
4362 | isym->st_name); | |
4363 | if (name == NULL) | |
4364 | goto error_free_vers; | |
4365 | ||
4366 | if (isym->st_shndx == SHN_COMMON | |
02d00247 AM |
4367 | && (abfd->flags & BFD_PLUGIN) != 0) |
4368 | { | |
4369 | asection *xc = bfd_get_section_by_name (abfd, "COMMON"); | |
4370 | ||
4371 | if (xc == NULL) | |
4372 | { | |
4373 | flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP | |
4374 | | SEC_EXCLUDE); | |
4375 | xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); | |
4376 | if (xc == NULL) | |
4377 | goto error_free_vers; | |
4378 | } | |
4379 | sec = xc; | |
4380 | } | |
4381 | else if (isym->st_shndx == SHN_COMMON | |
4382 | && ELF_ST_TYPE (isym->st_info) == STT_TLS | |
0e1862bb | 4383 | && !bfd_link_relocatable (info)) |
4ad4eba5 AM |
4384 | { |
4385 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
4386 | ||
4387 | if (tcomm == NULL) | |
4388 | { | |
02d00247 AM |
4389 | flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON |
4390 | | SEC_LINKER_CREATED); | |
4391 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); | |
3496cb2a | 4392 | if (tcomm == NULL) |
4ad4eba5 AM |
4393 | goto error_free_vers; |
4394 | } | |
4395 | sec = tcomm; | |
4396 | } | |
66eb6687 | 4397 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 4398 | { |
66eb6687 AM |
4399 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
4400 | &sec, &value)) | |
4ad4eba5 AM |
4401 | goto error_free_vers; |
4402 | ||
4403 | /* The hook function sets the name to NULL if this symbol | |
4404 | should be skipped for some reason. */ | |
4405 | if (name == NULL) | |
4406 | continue; | |
4407 | } | |
4408 | ||
4409 | /* Sanity check that all possibilities were handled. */ | |
4410 | if (sec == NULL) | |
4411 | { | |
4412 | bfd_set_error (bfd_error_bad_value); | |
4413 | goto error_free_vers; | |
4414 | } | |
4415 | ||
191c0c42 AM |
4416 | /* Silently discard TLS symbols from --just-syms. There's |
4417 | no way to combine a static TLS block with a new TLS block | |
4418 | for this executable. */ | |
4419 | if (ELF_ST_TYPE (isym->st_info) == STT_TLS | |
4420 | && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
4421 | continue; | |
4422 | ||
4ad4eba5 AM |
4423 | if (bfd_is_und_section (sec) |
4424 | || bfd_is_com_section (sec)) | |
4425 | definition = FALSE; | |
4426 | else | |
4427 | definition = TRUE; | |
4428 | ||
4429 | size_change_ok = FALSE; | |
66eb6687 | 4430 | type_change_ok = bed->type_change_ok; |
37a9e49a | 4431 | old_weak = FALSE; |
6e33951e | 4432 | matched = FALSE; |
4ad4eba5 AM |
4433 | old_alignment = 0; |
4434 | old_bfd = NULL; | |
af44c138 | 4435 | new_sec = sec; |
4ad4eba5 | 4436 | |
66eb6687 | 4437 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
4438 | { |
4439 | Elf_Internal_Versym iver; | |
4440 | unsigned int vernum = 0; | |
4441 | bfd_boolean skip; | |
4442 | ||
fc0e6df6 | 4443 | if (ever == NULL) |
4ad4eba5 | 4444 | { |
fc0e6df6 PB |
4445 | if (info->default_imported_symver) |
4446 | /* Use the default symbol version created earlier. */ | |
4447 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
4448 | else | |
4449 | iver.vs_vers = 0; | |
4450 | } | |
4451 | else | |
4452 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
4453 | ||
4454 | vernum = iver.vs_vers & VERSYM_VERSION; | |
4455 | ||
4456 | /* If this is a hidden symbol, or if it is not version | |
4457 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
4458 | However, we do not modify a non-hidden absolute symbol |
4459 | if it is not a function, because it might be the version | |
4460 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 4461 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
4462 | || (vernum > 1 |
4463 | && (!bfd_is_abs_section (sec) | |
4464 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
4465 | { |
4466 | const char *verstr; | |
4467 | size_t namelen, verlen, newlen; | |
4468 | char *newname, *p; | |
4469 | ||
4470 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 4471 | { |
fc0e6df6 PB |
4472 | if (vernum > elf_tdata (abfd)->cverdefs) |
4473 | verstr = NULL; | |
4474 | else if (vernum > 1) | |
4475 | verstr = | |
4476 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
4477 | else | |
4478 | verstr = ""; | |
4ad4eba5 | 4479 | |
fc0e6df6 | 4480 | if (verstr == NULL) |
4ad4eba5 | 4481 | { |
4eca0228 | 4482 | _bfd_error_handler |
695344c0 | 4483 | /* xgettext:c-format */ |
fc0e6df6 PB |
4484 | (_("%B: %s: invalid version %u (max %d)"), |
4485 | abfd, name, vernum, | |
4486 | elf_tdata (abfd)->cverdefs); | |
4487 | bfd_set_error (bfd_error_bad_value); | |
4488 | goto error_free_vers; | |
4ad4eba5 | 4489 | } |
fc0e6df6 PB |
4490 | } |
4491 | else | |
4492 | { | |
4493 | /* We cannot simply test for the number of | |
4494 | entries in the VERNEED section since the | |
4495 | numbers for the needed versions do not start | |
4496 | at 0. */ | |
4497 | Elf_Internal_Verneed *t; | |
4498 | ||
4499 | verstr = NULL; | |
4500 | for (t = elf_tdata (abfd)->verref; | |
4501 | t != NULL; | |
4502 | t = t->vn_nextref) | |
4ad4eba5 | 4503 | { |
fc0e6df6 | 4504 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 4505 | |
fc0e6df6 PB |
4506 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
4507 | { | |
4508 | if (a->vna_other == vernum) | |
4ad4eba5 | 4509 | { |
fc0e6df6 PB |
4510 | verstr = a->vna_nodename; |
4511 | break; | |
4ad4eba5 | 4512 | } |
4ad4eba5 | 4513 | } |
fc0e6df6 PB |
4514 | if (a != NULL) |
4515 | break; | |
4516 | } | |
4517 | if (verstr == NULL) | |
4518 | { | |
4eca0228 | 4519 | _bfd_error_handler |
695344c0 | 4520 | /* xgettext:c-format */ |
fc0e6df6 PB |
4521 | (_("%B: %s: invalid needed version %d"), |
4522 | abfd, name, vernum); | |
4523 | bfd_set_error (bfd_error_bad_value); | |
4524 | goto error_free_vers; | |
4ad4eba5 | 4525 | } |
4ad4eba5 | 4526 | } |
fc0e6df6 PB |
4527 | |
4528 | namelen = strlen (name); | |
4529 | verlen = strlen (verstr); | |
4530 | newlen = namelen + verlen + 2; | |
4531 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4532 | && isym->st_shndx != SHN_UNDEF) | |
4533 | ++newlen; | |
4534 | ||
a50b1753 | 4535 | newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
4536 | if (newname == NULL) |
4537 | goto error_free_vers; | |
4538 | memcpy (newname, name, namelen); | |
4539 | p = newname + namelen; | |
4540 | *p++ = ELF_VER_CHR; | |
4541 | /* If this is a defined non-hidden version symbol, | |
4542 | we add another @ to the name. This indicates the | |
4543 | default version of the symbol. */ | |
4544 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4545 | && isym->st_shndx != SHN_UNDEF) | |
4546 | *p++ = ELF_VER_CHR; | |
4547 | memcpy (p, verstr, verlen + 1); | |
4548 | ||
4549 | name = newname; | |
4ad4eba5 AM |
4550 | } |
4551 | ||
cd3416da AM |
4552 | /* If this symbol has default visibility and the user has |
4553 | requested we not re-export it, then mark it as hidden. */ | |
a0d49154 | 4554 | if (!bfd_is_und_section (sec) |
cd3416da | 4555 | && !dynamic |
ce875075 | 4556 | && abfd->no_export |
cd3416da AM |
4557 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) |
4558 | isym->st_other = (STV_HIDDEN | |
4559 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
4560 | ||
4f3fedcf AM |
4561 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, |
4562 | sym_hash, &old_bfd, &old_weak, | |
4563 | &old_alignment, &skip, &override, | |
6e33951e L |
4564 | &type_change_ok, &size_change_ok, |
4565 | &matched)) | |
4ad4eba5 AM |
4566 | goto error_free_vers; |
4567 | ||
4568 | if (skip) | |
4569 | continue; | |
4570 | ||
6e33951e L |
4571 | /* Override a definition only if the new symbol matches the |
4572 | existing one. */ | |
4573 | if (override && matched) | |
4ad4eba5 AM |
4574 | definition = FALSE; |
4575 | ||
4576 | h = *sym_hash; | |
4577 | while (h->root.type == bfd_link_hash_indirect | |
4578 | || h->root.type == bfd_link_hash_warning) | |
4579 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4580 | ||
4ad4eba5 | 4581 | if (elf_tdata (abfd)->verdef != NULL |
4ad4eba5 AM |
4582 | && vernum > 1 |
4583 | && definition) | |
4584 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4585 | } | |
4586 | ||
4587 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4588 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4589 | (struct bfd_link_hash_entry **) sym_hash))) |
4590 | goto error_free_vers; | |
4591 | ||
a43942db MR |
4592 | if ((flags & BSF_GNU_UNIQUE) |
4593 | && (abfd->flags & DYNAMIC) == 0 | |
4594 | && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour) | |
4595 | elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique; | |
4596 | ||
4ad4eba5 | 4597 | h = *sym_hash; |
90c984fc L |
4598 | /* We need to make sure that indirect symbol dynamic flags are |
4599 | updated. */ | |
4600 | hi = h; | |
4ad4eba5 AM |
4601 | while (h->root.type == bfd_link_hash_indirect |
4602 | || h->root.type == bfd_link_hash_warning) | |
4603 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3e7a7d11 | 4604 | |
97196564 L |
4605 | /* Setting the index to -3 tells elf_link_output_extsym that |
4606 | this symbol is defined in a discarded section. */ | |
4607 | if (discarded) | |
4608 | h->indx = -3; | |
4609 | ||
4ad4eba5 AM |
4610 | *sym_hash = h; |
4611 | ||
37a9e49a | 4612 | new_weak = (flags & BSF_WEAK) != 0; |
4ad4eba5 AM |
4613 | if (dynamic |
4614 | && definition | |
37a9e49a | 4615 | && new_weak |
fcb93ecf | 4616 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4617 | && is_elf_hash_table (htab) |
60d67dc8 | 4618 | && h->u.alias == NULL) |
4ad4eba5 AM |
4619 | { |
4620 | /* Keep a list of all weak defined non function symbols from | |
60d67dc8 AM |
4621 | a dynamic object, using the alias field. Later in this |
4622 | function we will set the alias field to the correct | |
4ad4eba5 AM |
4623 | value. We only put non-function symbols from dynamic |
4624 | objects on this list, because that happens to be the only | |
4625 | time we need to know the normal symbol corresponding to a | |
4626 | weak symbol, and the information is time consuming to | |
60d67dc8 | 4627 | figure out. If the alias field is not already NULL, |
4ad4eba5 AM |
4628 | then this symbol was already defined by some previous |
4629 | dynamic object, and we will be using that previous | |
4630 | definition anyhow. */ | |
4631 | ||
60d67dc8 | 4632 | h->u.alias = weaks; |
4ad4eba5 | 4633 | weaks = h; |
4ad4eba5 AM |
4634 | } |
4635 | ||
4636 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4637 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4638 | && h->root.type == bfd_link_hash_common) |
4639 | { | |
4640 | unsigned int align; | |
4641 | ||
a4d8e49b | 4642 | if (common) |
af44c138 L |
4643 | align = bfd_log2 (isym->st_value); |
4644 | else | |
4645 | { | |
4646 | /* The new symbol is a common symbol in a shared object. | |
4647 | We need to get the alignment from the section. */ | |
4648 | align = new_sec->alignment_power; | |
4649 | } | |
595213d4 | 4650 | if (align > old_alignment) |
4ad4eba5 AM |
4651 | h->root.u.c.p->alignment_power = align; |
4652 | else | |
4653 | h->root.u.c.p->alignment_power = old_alignment; | |
4654 | } | |
4655 | ||
66eb6687 | 4656 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4657 | { |
4f3fedcf AM |
4658 | /* Set a flag in the hash table entry indicating the type of |
4659 | reference or definition we just found. A dynamic symbol | |
4660 | is one which is referenced or defined by both a regular | |
4661 | object and a shared object. */ | |
4662 | bfd_boolean dynsym = FALSE; | |
4663 | ||
4664 | /* Plugin symbols aren't normal. Don't set def_regular or | |
4665 | ref_regular for them, or make them dynamic. */ | |
4666 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4667 | ; | |
4668 | else if (! dynamic) | |
4669 | { | |
4670 | if (! definition) | |
4671 | { | |
4672 | h->ref_regular = 1; | |
4673 | if (bind != STB_WEAK) | |
4674 | h->ref_regular_nonweak = 1; | |
4675 | } | |
4676 | else | |
4677 | { | |
4678 | h->def_regular = 1; | |
4679 | if (h->def_dynamic) | |
4680 | { | |
4681 | h->def_dynamic = 0; | |
4682 | h->ref_dynamic = 1; | |
4683 | } | |
4684 | } | |
4685 | ||
4686 | /* If the indirect symbol has been forced local, don't | |
4687 | make the real symbol dynamic. */ | |
4688 | if ((h == hi || !hi->forced_local) | |
0e1862bb | 4689 | && (bfd_link_dll (info) |
4f3fedcf AM |
4690 | || h->def_dynamic |
4691 | || h->ref_dynamic)) | |
4692 | dynsym = TRUE; | |
4693 | } | |
4694 | else | |
4695 | { | |
4696 | if (! definition) | |
4697 | { | |
4698 | h->ref_dynamic = 1; | |
4699 | hi->ref_dynamic = 1; | |
4700 | } | |
4701 | else | |
4702 | { | |
4703 | h->def_dynamic = 1; | |
4704 | hi->def_dynamic = 1; | |
4705 | } | |
4706 | ||
4707 | /* If the indirect symbol has been forced local, don't | |
4708 | make the real symbol dynamic. */ | |
4709 | if ((h == hi || !hi->forced_local) | |
4710 | && (h->def_regular | |
4711 | || h->ref_regular | |
60d67dc8 AM |
4712 | || (h->is_weakalias |
4713 | && weakdef (h)->dynindx != -1))) | |
4f3fedcf AM |
4714 | dynsym = TRUE; |
4715 | } | |
4716 | ||
4717 | /* Check to see if we need to add an indirect symbol for | |
4718 | the default name. */ | |
4719 | if (definition | |
4720 | || (!override && h->root.type == bfd_link_hash_common)) | |
4721 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4722 | sec, value, &old_bfd, &dynsym)) | |
4723 | goto error_free_vers; | |
4ad4eba5 AM |
4724 | |
4725 | /* Check the alignment when a common symbol is involved. This | |
4726 | can change when a common symbol is overridden by a normal | |
4727 | definition or a common symbol is ignored due to the old | |
4728 | normal definition. We need to make sure the maximum | |
4729 | alignment is maintained. */ | |
a4d8e49b | 4730 | if ((old_alignment || common) |
4ad4eba5 AM |
4731 | && h->root.type != bfd_link_hash_common) |
4732 | { | |
4733 | unsigned int common_align; | |
4734 | unsigned int normal_align; | |
4735 | unsigned int symbol_align; | |
4736 | bfd *normal_bfd; | |
4737 | bfd *common_bfd; | |
4738 | ||
3a81e825 AM |
4739 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
4740 | || h->root.type == bfd_link_hash_defweak); | |
4741 | ||
4ad4eba5 AM |
4742 | symbol_align = ffs (h->root.u.def.value) - 1; |
4743 | if (h->root.u.def.section->owner != NULL | |
0616a280 AM |
4744 | && (h->root.u.def.section->owner->flags |
4745 | & (DYNAMIC | BFD_PLUGIN)) == 0) | |
4ad4eba5 AM |
4746 | { |
4747 | normal_align = h->root.u.def.section->alignment_power; | |
4748 | if (normal_align > symbol_align) | |
4749 | normal_align = symbol_align; | |
4750 | } | |
4751 | else | |
4752 | normal_align = symbol_align; | |
4753 | ||
4754 | if (old_alignment) | |
4755 | { | |
4756 | common_align = old_alignment; | |
4757 | common_bfd = old_bfd; | |
4758 | normal_bfd = abfd; | |
4759 | } | |
4760 | else | |
4761 | { | |
4762 | common_align = bfd_log2 (isym->st_value); | |
4763 | common_bfd = abfd; | |
4764 | normal_bfd = old_bfd; | |
4765 | } | |
4766 | ||
4767 | if (normal_align < common_align) | |
d07676f8 NC |
4768 | { |
4769 | /* PR binutils/2735 */ | |
4770 | if (normal_bfd == NULL) | |
4eca0228 | 4771 | _bfd_error_handler |
695344c0 | 4772 | /* xgettext:c-format */ |
4f3fedcf AM |
4773 | (_("Warning: alignment %u of common symbol `%s' in %B is" |
4774 | " greater than the alignment (%u) of its section %A"), | |
c08bb8dd AM |
4775 | 1 << common_align, name, common_bfd, |
4776 | 1 << normal_align, h->root.u.def.section); | |
d07676f8 | 4777 | else |
4eca0228 | 4778 | _bfd_error_handler |
695344c0 | 4779 | /* xgettext:c-format */ |
d07676f8 NC |
4780 | (_("Warning: alignment %u of symbol `%s' in %B" |
4781 | " is smaller than %u in %B"), | |
c08bb8dd AM |
4782 | 1 << normal_align, name, normal_bfd, |
4783 | 1 << common_align, common_bfd); | |
d07676f8 | 4784 | } |
4ad4eba5 AM |
4785 | } |
4786 | ||
83ad0046 | 4787 | /* Remember the symbol size if it isn't undefined. */ |
3a81e825 AM |
4788 | if (isym->st_size != 0 |
4789 | && isym->st_shndx != SHN_UNDEF | |
4ad4eba5 AM |
4790 | && (definition || h->size == 0)) |
4791 | { | |
83ad0046 L |
4792 | if (h->size != 0 |
4793 | && h->size != isym->st_size | |
4794 | && ! size_change_ok) | |
4eca0228 | 4795 | _bfd_error_handler |
695344c0 | 4796 | /* xgettext:c-format */ |
d003868e | 4797 | (_("Warning: size of symbol `%s' changed" |
76cfced5 AM |
4798 | " from %Lu in %B to %Lu in %B"), |
4799 | name, h->size, old_bfd, isym->st_size, abfd); | |
4ad4eba5 AM |
4800 | |
4801 | h->size = isym->st_size; | |
4802 | } | |
4803 | ||
4804 | /* If this is a common symbol, then we always want H->SIZE | |
4805 | to be the size of the common symbol. The code just above | |
4806 | won't fix the size if a common symbol becomes larger. We | |
4807 | don't warn about a size change here, because that is | |
4f3fedcf | 4808 | covered by --warn-common. Allow changes between different |
fcb93ecf | 4809 | function types. */ |
4ad4eba5 AM |
4810 | if (h->root.type == bfd_link_hash_common) |
4811 | h->size = h->root.u.c.size; | |
4812 | ||
4813 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
37a9e49a L |
4814 | && ((definition && !new_weak) |
4815 | || (old_weak && h->root.type == bfd_link_hash_common) | |
4816 | || h->type == STT_NOTYPE)) | |
4ad4eba5 | 4817 | { |
2955ec4c L |
4818 | unsigned int type = ELF_ST_TYPE (isym->st_info); |
4819 | ||
4820 | /* Turn an IFUNC symbol from a DSO into a normal FUNC | |
4821 | symbol. */ | |
4822 | if (type == STT_GNU_IFUNC | |
4823 | && (abfd->flags & DYNAMIC) != 0) | |
4824 | type = STT_FUNC; | |
4ad4eba5 | 4825 | |
2955ec4c L |
4826 | if (h->type != type) |
4827 | { | |
4828 | if (h->type != STT_NOTYPE && ! type_change_ok) | |
695344c0 | 4829 | /* xgettext:c-format */ |
4eca0228 | 4830 | _bfd_error_handler |
2955ec4c L |
4831 | (_("Warning: type of symbol `%s' changed" |
4832 | " from %d to %d in %B"), | |
c08bb8dd | 4833 | name, h->type, type, abfd); |
2955ec4c L |
4834 | |
4835 | h->type = type; | |
4836 | } | |
4ad4eba5 AM |
4837 | } |
4838 | ||
54ac0771 | 4839 | /* Merge st_other field. */ |
b8417128 | 4840 | elf_merge_st_other (abfd, h, isym, sec, definition, dynamic); |
4ad4eba5 | 4841 | |
c3df8c14 | 4842 | /* We don't want to make debug symbol dynamic. */ |
0e1862bb L |
4843 | if (definition |
4844 | && (sec->flags & SEC_DEBUGGING) | |
4845 | && !bfd_link_relocatable (info)) | |
c3df8c14 AM |
4846 | dynsym = FALSE; |
4847 | ||
4f3fedcf AM |
4848 | /* Nor should we make plugin symbols dynamic. */ |
4849 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4850 | dynsym = FALSE; | |
4851 | ||
35fc36a8 | 4852 | if (definition) |
35399224 L |
4853 | { |
4854 | h->target_internal = isym->st_target_internal; | |
4855 | h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; | |
4856 | } | |
35fc36a8 | 4857 | |
4ad4eba5 AM |
4858 | if (definition && !dynamic) |
4859 | { | |
4860 | char *p = strchr (name, ELF_VER_CHR); | |
4861 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4862 | { | |
4863 | /* Queue non-default versions so that .symver x, x@FOO | |
4864 | aliases can be checked. */ | |
66eb6687 | 4865 | if (!nondeflt_vers) |
4ad4eba5 | 4866 | { |
66eb6687 AM |
4867 | amt = ((isymend - isym + 1) |
4868 | * sizeof (struct elf_link_hash_entry *)); | |
ca4be51c AM |
4869 | nondeflt_vers |
4870 | = (struct elf_link_hash_entry **) bfd_malloc (amt); | |
14b1c01e AM |
4871 | if (!nondeflt_vers) |
4872 | goto error_free_vers; | |
4ad4eba5 | 4873 | } |
66eb6687 | 4874 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4875 | } |
4876 | } | |
4877 | ||
4878 | if (dynsym && h->dynindx == -1) | |
4879 | { | |
c152c796 | 4880 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4881 | goto error_free_vers; |
60d67dc8 AM |
4882 | if (h->is_weakalias |
4883 | && weakdef (h)->dynindx == -1) | |
4ad4eba5 | 4884 | { |
60d67dc8 | 4885 | if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h))) |
4ad4eba5 AM |
4886 | goto error_free_vers; |
4887 | } | |
4888 | } | |
1f599d0e | 4889 | else if (h->dynindx != -1) |
4ad4eba5 AM |
4890 | /* If the symbol already has a dynamic index, but |
4891 | visibility says it should not be visible, turn it into | |
4892 | a local symbol. */ | |
4893 | switch (ELF_ST_VISIBILITY (h->other)) | |
4894 | { | |
4895 | case STV_INTERNAL: | |
4896 | case STV_HIDDEN: | |
4897 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4898 | dynsym = FALSE; | |
4899 | break; | |
4900 | } | |
4901 | ||
aef28989 L |
4902 | /* Don't add DT_NEEDED for references from the dummy bfd nor |
4903 | for unmatched symbol. */ | |
4ad4eba5 | 4904 | if (!add_needed |
aef28989 | 4905 | && matched |
4ad4eba5 | 4906 | && definition |
010e5ae2 | 4907 | && ((dynsym |
ffa9430d | 4908 | && h->ref_regular_nonweak |
4f3fedcf AM |
4909 | && (old_bfd == NULL |
4910 | || (old_bfd->flags & BFD_PLUGIN) == 0)) | |
ffa9430d | 4911 | || (h->ref_dynamic_nonweak |
010e5ae2 | 4912 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 |
7b15fa7a AM |
4913 | && !on_needed_list (elf_dt_name (abfd), |
4914 | htab->needed, NULL)))) | |
4ad4eba5 AM |
4915 | { |
4916 | int ret; | |
4917 | const char *soname = elf_dt_name (abfd); | |
4918 | ||
16e4ecc0 AM |
4919 | info->callbacks->minfo ("%!", soname, old_bfd, |
4920 | h->root.root.string); | |
4921 | ||
4ad4eba5 AM |
4922 | /* A symbol from a library loaded via DT_NEEDED of some |
4923 | other library is referenced by a regular object. | |
e56f61be | 4924 | Add a DT_NEEDED entry for it. Issue an error if |
b918acf9 NC |
4925 | --no-add-needed is used and the reference was not |
4926 | a weak one. */ | |
4f3fedcf | 4927 | if (old_bfd != NULL |
b918acf9 | 4928 | && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) |
e56f61be | 4929 | { |
4eca0228 | 4930 | _bfd_error_handler |
695344c0 | 4931 | /* xgettext:c-format */ |
3cbc5de0 | 4932 | (_("%B: undefined reference to symbol '%s'"), |
4f3fedcf | 4933 | old_bfd, name); |
ff5ac77b | 4934 | bfd_set_error (bfd_error_missing_dso); |
e56f61be L |
4935 | goto error_free_vers; |
4936 | } | |
4937 | ||
a50b1753 | 4938 | elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) |
ca4be51c | 4939 | (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); |
a5db907e | 4940 | |
4ad4eba5 | 4941 | add_needed = TRUE; |
7e9f0867 | 4942 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4943 | if (ret < 0) |
4944 | goto error_free_vers; | |
4945 | ||
4946 | BFD_ASSERT (ret == 0); | |
4947 | } | |
4948 | } | |
4949 | } | |
4950 | ||
a83ef4d1 L |
4951 | if (info->lto_plugin_active |
4952 | && !bfd_link_relocatable (info) | |
4953 | && (abfd->flags & BFD_PLUGIN) == 0 | |
4954 | && !just_syms | |
4955 | && extsymcount) | |
4956 | { | |
4957 | int r_sym_shift; | |
4958 | ||
4959 | if (bed->s->arch_size == 32) | |
4960 | r_sym_shift = 8; | |
4961 | else | |
4962 | r_sym_shift = 32; | |
4963 | ||
4964 | /* If linker plugin is enabled, set non_ir_ref_regular on symbols | |
4965 | referenced in regular objects so that linker plugin will get | |
4966 | the correct symbol resolution. */ | |
4967 | ||
4968 | sym_hash = elf_sym_hashes (abfd); | |
4969 | for (s = abfd->sections; s != NULL; s = s->next) | |
4970 | { | |
4971 | Elf_Internal_Rela *internal_relocs; | |
4972 | Elf_Internal_Rela *rel, *relend; | |
4973 | ||
4974 | /* Don't check relocations in excluded sections. */ | |
4975 | if ((s->flags & SEC_RELOC) == 0 | |
4976 | || s->reloc_count == 0 | |
4977 | || (s->flags & SEC_EXCLUDE) != 0 | |
4978 | || ((info->strip == strip_all | |
4979 | || info->strip == strip_debugger) | |
4980 | && (s->flags & SEC_DEBUGGING) != 0)) | |
4981 | continue; | |
4982 | ||
4983 | internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL, | |
4984 | NULL, | |
4985 | info->keep_memory); | |
4986 | if (internal_relocs == NULL) | |
4987 | goto error_free_vers; | |
4988 | ||
4989 | rel = internal_relocs; | |
4990 | relend = rel + s->reloc_count; | |
4991 | for ( ; rel < relend; rel++) | |
4992 | { | |
4993 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
4994 | struct elf_link_hash_entry *h; | |
4995 | ||
4996 | /* Skip local symbols. */ | |
4997 | if (r_symndx < extsymoff) | |
4998 | continue; | |
4999 | ||
5000 | h = sym_hash[r_symndx - extsymoff]; | |
5001 | if (h != NULL) | |
5002 | h->root.non_ir_ref_regular = 1; | |
5003 | } | |
5004 | ||
5005 | if (elf_section_data (s)->relocs != internal_relocs) | |
5006 | free (internal_relocs); | |
5007 | } | |
5008 | } | |
5009 | ||
66eb6687 AM |
5010 | if (extversym != NULL) |
5011 | { | |
5012 | free (extversym); | |
5013 | extversym = NULL; | |
5014 | } | |
5015 | ||
5016 | if (isymbuf != NULL) | |
5017 | { | |
5018 | free (isymbuf); | |
5019 | isymbuf = NULL; | |
5020 | } | |
5021 | ||
5022 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
5023 | { | |
5024 | unsigned int i; | |
5025 | ||
5026 | /* Restore the symbol table. */ | |
f45794cb AM |
5027 | old_ent = (char *) old_tab + tabsize; |
5028 | memset (elf_sym_hashes (abfd), 0, | |
5029 | extsymcount * sizeof (struct elf_link_hash_entry *)); | |
4f87808c AM |
5030 | htab->root.table.table = old_table; |
5031 | htab->root.table.size = old_size; | |
5032 | htab->root.table.count = old_count; | |
66eb6687 | 5033 | memcpy (htab->root.table.table, old_tab, tabsize); |
66eb6687 AM |
5034 | htab->root.undefs = old_undefs; |
5035 | htab->root.undefs_tail = old_undefs_tail; | |
5b677558 AM |
5036 | _bfd_elf_strtab_restore (htab->dynstr, old_strtab); |
5037 | free (old_strtab); | |
5038 | old_strtab = NULL; | |
66eb6687 AM |
5039 | for (i = 0; i < htab->root.table.size; i++) |
5040 | { | |
5041 | struct bfd_hash_entry *p; | |
5042 | struct elf_link_hash_entry *h; | |
3e0882af L |
5043 | bfd_size_type size; |
5044 | unsigned int alignment_power; | |
4070765b | 5045 | unsigned int non_ir_ref_dynamic; |
66eb6687 AM |
5046 | |
5047 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
5048 | { | |
5049 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
5050 | if (h->root.type == bfd_link_hash_warning) |
5051 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2de92251 | 5052 | |
3e0882af L |
5053 | /* Preserve the maximum alignment and size for common |
5054 | symbols even if this dynamic lib isn't on DT_NEEDED | |
a4542f1b | 5055 | since it can still be loaded at run time by another |
3e0882af L |
5056 | dynamic lib. */ |
5057 | if (h->root.type == bfd_link_hash_common) | |
5058 | { | |
5059 | size = h->root.u.c.size; | |
5060 | alignment_power = h->root.u.c.p->alignment_power; | |
5061 | } | |
5062 | else | |
5063 | { | |
5064 | size = 0; | |
5065 | alignment_power = 0; | |
5066 | } | |
4070765b | 5067 | /* Preserve non_ir_ref_dynamic so that this symbol |
59fa66c5 L |
5068 | will be exported when the dynamic lib becomes needed |
5069 | in the second pass. */ | |
4070765b | 5070 | non_ir_ref_dynamic = h->root.non_ir_ref_dynamic; |
66eb6687 AM |
5071 | memcpy (p, old_ent, htab->root.table.entsize); |
5072 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
5073 | h = (struct elf_link_hash_entry *) p; |
5074 | if (h->root.type == bfd_link_hash_warning) | |
5075 | { | |
5076 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
5077 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
a4542f1b | 5078 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
2de92251 | 5079 | } |
a4542f1b | 5080 | if (h->root.type == bfd_link_hash_common) |
3e0882af L |
5081 | { |
5082 | if (size > h->root.u.c.size) | |
5083 | h->root.u.c.size = size; | |
5084 | if (alignment_power > h->root.u.c.p->alignment_power) | |
5085 | h->root.u.c.p->alignment_power = alignment_power; | |
5086 | } | |
4070765b | 5087 | h->root.non_ir_ref_dynamic = non_ir_ref_dynamic; |
66eb6687 AM |
5088 | } |
5089 | } | |
5090 | ||
5061a885 AM |
5091 | /* Make a special call to the linker "notice" function to |
5092 | tell it that symbols added for crefs may need to be removed. */ | |
e5034e59 | 5093 | if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) |
9af2a943 | 5094 | goto error_free_vers; |
5061a885 | 5095 | |
66eb6687 AM |
5096 | free (old_tab); |
5097 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
5098 | alloc_mark); | |
5099 | if (nondeflt_vers != NULL) | |
5100 | free (nondeflt_vers); | |
5101 | return TRUE; | |
5102 | } | |
2de92251 | 5103 | |
66eb6687 AM |
5104 | if (old_tab != NULL) |
5105 | { | |
e5034e59 | 5106 | if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) |
9af2a943 | 5107 | goto error_free_vers; |
66eb6687 AM |
5108 | free (old_tab); |
5109 | old_tab = NULL; | |
5110 | } | |
5111 | ||
c6e8a9a8 L |
5112 | /* Now that all the symbols from this input file are created, if |
5113 | not performing a relocatable link, handle .symver foo, foo@BAR | |
5114 | such that any relocs against foo become foo@BAR. */ | |
0e1862bb | 5115 | if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) |
4ad4eba5 | 5116 | { |
ef53be89 | 5117 | size_t cnt, symidx; |
4ad4eba5 AM |
5118 | |
5119 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
5120 | { | |
5121 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
5122 | char *shortname, *p; | |
5123 | ||
5124 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
5125 | if (p == NULL | |
5126 | || (h->root.type != bfd_link_hash_defined | |
5127 | && h->root.type != bfd_link_hash_defweak)) | |
5128 | continue; | |
5129 | ||
5130 | amt = p - h->root.root.string; | |
a50b1753 | 5131 | shortname = (char *) bfd_malloc (amt + 1); |
14b1c01e AM |
5132 | if (!shortname) |
5133 | goto error_free_vers; | |
4ad4eba5 AM |
5134 | memcpy (shortname, h->root.root.string, amt); |
5135 | shortname[amt] = '\0'; | |
5136 | ||
5137 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 5138 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
5139 | FALSE, FALSE, FALSE); |
5140 | if (hi != NULL | |
5141 | && hi->root.type == h->root.type | |
5142 | && hi->root.u.def.value == h->root.u.def.value | |
5143 | && hi->root.u.def.section == h->root.u.def.section) | |
5144 | { | |
5145 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
5146 | hi->root.type = bfd_link_hash_indirect; | |
5147 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 5148 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
5149 | sym_hash = elf_sym_hashes (abfd); |
5150 | if (sym_hash) | |
5151 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
5152 | if (sym_hash[symidx] == hi) | |
5153 | { | |
5154 | sym_hash[symidx] = h; | |
5155 | break; | |
5156 | } | |
5157 | } | |
5158 | free (shortname); | |
5159 | } | |
5160 | free (nondeflt_vers); | |
5161 | nondeflt_vers = NULL; | |
5162 | } | |
5163 | ||
60d67dc8 | 5164 | /* Now set the alias field correctly for all the weak defined |
4ad4eba5 AM |
5165 | symbols we found. The only way to do this is to search all the |
5166 | symbols. Since we only need the information for non functions in | |
5167 | dynamic objects, that's the only time we actually put anything on | |
5168 | the list WEAKS. We need this information so that if a regular | |
5169 | object refers to a symbol defined weakly in a dynamic object, the | |
5170 | real symbol in the dynamic object is also put in the dynamic | |
5171 | symbols; we also must arrange for both symbols to point to the | |
5172 | same memory location. We could handle the general case of symbol | |
5173 | aliasing, but a general symbol alias can only be generated in | |
5174 | assembler code, handling it correctly would be very time | |
5175 | consuming, and other ELF linkers don't handle general aliasing | |
5176 | either. */ | |
5177 | if (weaks != NULL) | |
5178 | { | |
5179 | struct elf_link_hash_entry **hpp; | |
5180 | struct elf_link_hash_entry **hppend; | |
5181 | struct elf_link_hash_entry **sorted_sym_hash; | |
5182 | struct elf_link_hash_entry *h; | |
5183 | size_t sym_count; | |
5184 | ||
5185 | /* Since we have to search the whole symbol list for each weak | |
5186 | defined symbol, search time for N weak defined symbols will be | |
5187 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
ef53be89 AM |
5188 | amt = extsymcount; |
5189 | amt *= sizeof (struct elf_link_hash_entry *); | |
a50b1753 | 5190 | sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); |
4ad4eba5 AM |
5191 | if (sorted_sym_hash == NULL) |
5192 | goto error_return; | |
5193 | sym_hash = sorted_sym_hash; | |
5194 | hpp = elf_sym_hashes (abfd); | |
5195 | hppend = hpp + extsymcount; | |
5196 | sym_count = 0; | |
5197 | for (; hpp < hppend; hpp++) | |
5198 | { | |
5199 | h = *hpp; | |
5200 | if (h != NULL | |
5201 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 5202 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
5203 | { |
5204 | *sym_hash = h; | |
5205 | sym_hash++; | |
5206 | sym_count++; | |
5207 | } | |
5208 | } | |
5209 | ||
5210 | qsort (sorted_sym_hash, sym_count, | |
5211 | sizeof (struct elf_link_hash_entry *), | |
5212 | elf_sort_symbol); | |
5213 | ||
5214 | while (weaks != NULL) | |
5215 | { | |
5216 | struct elf_link_hash_entry *hlook; | |
5217 | asection *slook; | |
5218 | bfd_vma vlook; | |
ed54588d | 5219 | size_t i, j, idx = 0; |
4ad4eba5 AM |
5220 | |
5221 | hlook = weaks; | |
60d67dc8 AM |
5222 | weaks = hlook->u.alias; |
5223 | hlook->u.alias = NULL; | |
4ad4eba5 | 5224 | |
e3e53eed AM |
5225 | if (hlook->root.type != bfd_link_hash_defined |
5226 | && hlook->root.type != bfd_link_hash_defweak) | |
5227 | continue; | |
5228 | ||
4ad4eba5 AM |
5229 | slook = hlook->root.u.def.section; |
5230 | vlook = hlook->root.u.def.value; | |
5231 | ||
4ad4eba5 AM |
5232 | i = 0; |
5233 | j = sym_count; | |
14160578 | 5234 | while (i != j) |
4ad4eba5 AM |
5235 | { |
5236 | bfd_signed_vma vdiff; | |
5237 | idx = (i + j) / 2; | |
14160578 | 5238 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5239 | vdiff = vlook - h->root.u.def.value; |
5240 | if (vdiff < 0) | |
5241 | j = idx; | |
5242 | else if (vdiff > 0) | |
5243 | i = idx + 1; | |
5244 | else | |
5245 | { | |
d3435ae8 | 5246 | int sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
5247 | if (sdiff < 0) |
5248 | j = idx; | |
5249 | else if (sdiff > 0) | |
5250 | i = idx + 1; | |
5251 | else | |
14160578 | 5252 | break; |
4ad4eba5 AM |
5253 | } |
5254 | } | |
5255 | ||
5256 | /* We didn't find a value/section match. */ | |
14160578 | 5257 | if (i == j) |
4ad4eba5 AM |
5258 | continue; |
5259 | ||
14160578 AM |
5260 | /* With multiple aliases, or when the weak symbol is already |
5261 | strongly defined, we have multiple matching symbols and | |
5262 | the binary search above may land on any of them. Step | |
5263 | one past the matching symbol(s). */ | |
5264 | while (++idx != j) | |
5265 | { | |
5266 | h = sorted_sym_hash[idx]; | |
5267 | if (h->root.u.def.section != slook | |
5268 | || h->root.u.def.value != vlook) | |
5269 | break; | |
5270 | } | |
5271 | ||
5272 | /* Now look back over the aliases. Since we sorted by size | |
5273 | as well as value and section, we'll choose the one with | |
5274 | the largest size. */ | |
5275 | while (idx-- != i) | |
4ad4eba5 | 5276 | { |
14160578 | 5277 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5278 | |
5279 | /* Stop if value or section doesn't match. */ | |
14160578 AM |
5280 | if (h->root.u.def.section != slook |
5281 | || h->root.u.def.value != vlook) | |
4ad4eba5 AM |
5282 | break; |
5283 | else if (h != hlook) | |
5284 | { | |
60d67dc8 AM |
5285 | struct elf_link_hash_entry *t; |
5286 | ||
5287 | hlook->u.alias = h; | |
5288 | hlook->is_weakalias = 1; | |
5289 | t = h; | |
5290 | if (t->u.alias != NULL) | |
5291 | while (t->u.alias != h) | |
5292 | t = t->u.alias; | |
5293 | t->u.alias = hlook; | |
4ad4eba5 AM |
5294 | |
5295 | /* If the weak definition is in the list of dynamic | |
5296 | symbols, make sure the real definition is put | |
5297 | there as well. */ | |
5298 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
5299 | { | |
c152c796 | 5300 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4dd07732 AM |
5301 | { |
5302 | err_free_sym_hash: | |
5303 | free (sorted_sym_hash); | |
5304 | goto error_return; | |
5305 | } | |
4ad4eba5 AM |
5306 | } |
5307 | ||
5308 | /* If the real definition is in the list of dynamic | |
5309 | symbols, make sure the weak definition is put | |
5310 | there as well. If we don't do this, then the | |
5311 | dynamic loader might not merge the entries for the | |
5312 | real definition and the weak definition. */ | |
5313 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
5314 | { | |
c152c796 | 5315 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4dd07732 | 5316 | goto err_free_sym_hash; |
4ad4eba5 AM |
5317 | } |
5318 | break; | |
5319 | } | |
5320 | } | |
5321 | } | |
5322 | ||
5323 | free (sorted_sym_hash); | |
5324 | } | |
5325 | ||
33177bb1 AM |
5326 | if (bed->check_directives |
5327 | && !(*bed->check_directives) (abfd, info)) | |
5328 | return FALSE; | |
85fbca6a | 5329 | |
4ad4eba5 AM |
5330 | /* If this is a non-traditional link, try to optimize the handling |
5331 | of the .stab/.stabstr sections. */ | |
5332 | if (! dynamic | |
5333 | && ! info->traditional_format | |
66eb6687 | 5334 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
5335 | && (info->strip != strip_all && info->strip != strip_debugger)) |
5336 | { | |
5337 | asection *stabstr; | |
5338 | ||
5339 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
5340 | if (stabstr != NULL) | |
5341 | { | |
5342 | bfd_size_type string_offset = 0; | |
5343 | asection *stab; | |
5344 | ||
5345 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 5346 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
5347 | && (!stab->name[5] || |
5348 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
5349 | && (stab->flags & SEC_MERGE) == 0 | |
5350 | && !bfd_is_abs_section (stab->output_section)) | |
5351 | { | |
5352 | struct bfd_elf_section_data *secdata; | |
5353 | ||
5354 | secdata = elf_section_data (stab); | |
66eb6687 AM |
5355 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
5356 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
5357 | &string_offset)) |
5358 | goto error_return; | |
5359 | if (secdata->sec_info) | |
dbaa2011 | 5360 | stab->sec_info_type = SEC_INFO_TYPE_STABS; |
4ad4eba5 AM |
5361 | } |
5362 | } | |
5363 | } | |
5364 | ||
66eb6687 | 5365 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
5366 | { |
5367 | /* Add this bfd to the loaded list. */ | |
5368 | struct elf_link_loaded_list *n; | |
5369 | ||
ca4be51c | 5370 | n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); |
4ad4eba5 AM |
5371 | if (n == NULL) |
5372 | goto error_return; | |
5373 | n->abfd = abfd; | |
66eb6687 AM |
5374 | n->next = htab->loaded; |
5375 | htab->loaded = n; | |
4ad4eba5 AM |
5376 | } |
5377 | ||
5378 | return TRUE; | |
5379 | ||
5380 | error_free_vers: | |
66eb6687 AM |
5381 | if (old_tab != NULL) |
5382 | free (old_tab); | |
5b677558 AM |
5383 | if (old_strtab != NULL) |
5384 | free (old_strtab); | |
4ad4eba5 AM |
5385 | if (nondeflt_vers != NULL) |
5386 | free (nondeflt_vers); | |
5387 | if (extversym != NULL) | |
5388 | free (extversym); | |
5389 | error_free_sym: | |
5390 | if (isymbuf != NULL) | |
5391 | free (isymbuf); | |
5392 | error_return: | |
5393 | return FALSE; | |
5394 | } | |
5395 | ||
8387904d AM |
5396 | /* Return the linker hash table entry of a symbol that might be |
5397 | satisfied by an archive symbol. Return -1 on error. */ | |
5398 | ||
5399 | struct elf_link_hash_entry * | |
5400 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
5401 | struct bfd_link_info *info, | |
5402 | const char *name) | |
5403 | { | |
5404 | struct elf_link_hash_entry *h; | |
5405 | char *p, *copy; | |
5406 | size_t len, first; | |
5407 | ||
2a41f396 | 5408 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); |
8387904d AM |
5409 | if (h != NULL) |
5410 | return h; | |
5411 | ||
5412 | /* If this is a default version (the name contains @@), look up the | |
5413 | symbol again with only one `@' as well as without the version. | |
5414 | The effect is that references to the symbol with and without the | |
5415 | version will be matched by the default symbol in the archive. */ | |
5416 | ||
5417 | p = strchr (name, ELF_VER_CHR); | |
5418 | if (p == NULL || p[1] != ELF_VER_CHR) | |
5419 | return h; | |
5420 | ||
5421 | /* First check with only one `@'. */ | |
5422 | len = strlen (name); | |
a50b1753 | 5423 | copy = (char *) bfd_alloc (abfd, len); |
8387904d AM |
5424 | if (copy == NULL) |
5425 | return (struct elf_link_hash_entry *) 0 - 1; | |
5426 | ||
5427 | first = p - name + 1; | |
5428 | memcpy (copy, name, first); | |
5429 | memcpy (copy + first, name + first + 1, len - first); | |
5430 | ||
2a41f396 | 5431 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); |
8387904d AM |
5432 | if (h == NULL) |
5433 | { | |
5434 | /* We also need to check references to the symbol without the | |
5435 | version. */ | |
5436 | copy[first - 1] = '\0'; | |
5437 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
2a41f396 | 5438 | FALSE, FALSE, TRUE); |
8387904d AM |
5439 | } |
5440 | ||
5441 | bfd_release (abfd, copy); | |
5442 | return h; | |
5443 | } | |
5444 | ||
0ad989f9 | 5445 | /* Add symbols from an ELF archive file to the linker hash table. We |
13e570f8 AM |
5446 | don't use _bfd_generic_link_add_archive_symbols because we need to |
5447 | handle versioned symbols. | |
0ad989f9 L |
5448 | |
5449 | Fortunately, ELF archive handling is simpler than that done by | |
5450 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
5451 | oddities. In ELF, if we find a symbol in the archive map, and the | |
5452 | symbol is currently undefined, we know that we must pull in that | |
5453 | object file. | |
5454 | ||
5455 | Unfortunately, we do have to make multiple passes over the symbol | |
5456 | table until nothing further is resolved. */ | |
5457 | ||
4ad4eba5 AM |
5458 | static bfd_boolean |
5459 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
5460 | { |
5461 | symindex c; | |
13e570f8 | 5462 | unsigned char *included = NULL; |
0ad989f9 L |
5463 | carsym *symdefs; |
5464 | bfd_boolean loop; | |
5465 | bfd_size_type amt; | |
8387904d AM |
5466 | const struct elf_backend_data *bed; |
5467 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
5468 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
5469 | |
5470 | if (! bfd_has_map (abfd)) | |
5471 | { | |
5472 | /* An empty archive is a special case. */ | |
5473 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
5474 | return TRUE; | |
5475 | bfd_set_error (bfd_error_no_armap); | |
5476 | return FALSE; | |
5477 | } | |
5478 | ||
5479 | /* Keep track of all symbols we know to be already defined, and all | |
5480 | files we know to be already included. This is to speed up the | |
5481 | second and subsequent passes. */ | |
5482 | c = bfd_ardata (abfd)->symdef_count; | |
5483 | if (c == 0) | |
5484 | return TRUE; | |
5485 | amt = c; | |
13e570f8 AM |
5486 | amt *= sizeof (*included); |
5487 | included = (unsigned char *) bfd_zmalloc (amt); | |
5488 | if (included == NULL) | |
5489 | return FALSE; | |
0ad989f9 L |
5490 | |
5491 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
5492 | bed = get_elf_backend_data (abfd); |
5493 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
5494 | |
5495 | do | |
5496 | { | |
5497 | file_ptr last; | |
5498 | symindex i; | |
5499 | carsym *symdef; | |
5500 | carsym *symdefend; | |
5501 | ||
5502 | loop = FALSE; | |
5503 | last = -1; | |
5504 | ||
5505 | symdef = symdefs; | |
5506 | symdefend = symdef + c; | |
5507 | for (i = 0; symdef < symdefend; symdef++, i++) | |
5508 | { | |
5509 | struct elf_link_hash_entry *h; | |
5510 | bfd *element; | |
5511 | struct bfd_link_hash_entry *undefs_tail; | |
5512 | symindex mark; | |
5513 | ||
13e570f8 | 5514 | if (included[i]) |
0ad989f9 L |
5515 | continue; |
5516 | if (symdef->file_offset == last) | |
5517 | { | |
5518 | included[i] = TRUE; | |
5519 | continue; | |
5520 | } | |
5521 | ||
8387904d AM |
5522 | h = archive_symbol_lookup (abfd, info, symdef->name); |
5523 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
5524 | goto error_return; | |
0ad989f9 L |
5525 | |
5526 | if (h == NULL) | |
5527 | continue; | |
5528 | ||
5529 | if (h->root.type == bfd_link_hash_common) | |
5530 | { | |
5531 | /* We currently have a common symbol. The archive map contains | |
5532 | a reference to this symbol, so we may want to include it. We | |
5533 | only want to include it however, if this archive element | |
5534 | contains a definition of the symbol, not just another common | |
5535 | declaration of it. | |
5536 | ||
5537 | Unfortunately some archivers (including GNU ar) will put | |
5538 | declarations of common symbols into their archive maps, as | |
5539 | well as real definitions, so we cannot just go by the archive | |
5540 | map alone. Instead we must read in the element's symbol | |
5541 | table and check that to see what kind of symbol definition | |
5542 | this is. */ | |
5543 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
5544 | continue; | |
5545 | } | |
5546 | else if (h->root.type != bfd_link_hash_undefined) | |
5547 | { | |
5548 | if (h->root.type != bfd_link_hash_undefweak) | |
13e570f8 AM |
5549 | /* Symbol must be defined. Don't check it again. */ |
5550 | included[i] = TRUE; | |
0ad989f9 L |
5551 | continue; |
5552 | } | |
5553 | ||
5554 | /* We need to include this archive member. */ | |
5555 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
5556 | if (element == NULL) | |
5557 | goto error_return; | |
5558 | ||
5559 | if (! bfd_check_format (element, bfd_object)) | |
5560 | goto error_return; | |
5561 | ||
0ad989f9 L |
5562 | undefs_tail = info->hash->undefs_tail; |
5563 | ||
0e144ba7 AM |
5564 | if (!(*info->callbacks |
5565 | ->add_archive_element) (info, element, symdef->name, &element)) | |
b95a0a31 | 5566 | continue; |
0e144ba7 | 5567 | if (!bfd_link_add_symbols (element, info)) |
0ad989f9 L |
5568 | goto error_return; |
5569 | ||
5570 | /* If there are any new undefined symbols, we need to make | |
5571 | another pass through the archive in order to see whether | |
5572 | they can be defined. FIXME: This isn't perfect, because | |
5573 | common symbols wind up on undefs_tail and because an | |
5574 | undefined symbol which is defined later on in this pass | |
5575 | does not require another pass. This isn't a bug, but it | |
5576 | does make the code less efficient than it could be. */ | |
5577 | if (undefs_tail != info->hash->undefs_tail) | |
5578 | loop = TRUE; | |
5579 | ||
5580 | /* Look backward to mark all symbols from this object file | |
5581 | which we have already seen in this pass. */ | |
5582 | mark = i; | |
5583 | do | |
5584 | { | |
5585 | included[mark] = TRUE; | |
5586 | if (mark == 0) | |
5587 | break; | |
5588 | --mark; | |
5589 | } | |
5590 | while (symdefs[mark].file_offset == symdef->file_offset); | |
5591 | ||
5592 | /* We mark subsequent symbols from this object file as we go | |
5593 | on through the loop. */ | |
5594 | last = symdef->file_offset; | |
5595 | } | |
5596 | } | |
5597 | while (loop); | |
5598 | ||
0ad989f9 L |
5599 | free (included); |
5600 | ||
5601 | return TRUE; | |
5602 | ||
5603 | error_return: | |
0ad989f9 L |
5604 | if (included != NULL) |
5605 | free (included); | |
5606 | return FALSE; | |
5607 | } | |
4ad4eba5 AM |
5608 | |
5609 | /* Given an ELF BFD, add symbols to the global hash table as | |
5610 | appropriate. */ | |
5611 | ||
5612 | bfd_boolean | |
5613 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
5614 | { | |
5615 | switch (bfd_get_format (abfd)) | |
5616 | { | |
5617 | case bfd_object: | |
5618 | return elf_link_add_object_symbols (abfd, info); | |
5619 | case bfd_archive: | |
5620 | return elf_link_add_archive_symbols (abfd, info); | |
5621 | default: | |
5622 | bfd_set_error (bfd_error_wrong_format); | |
5623 | return FALSE; | |
5624 | } | |
5625 | } | |
5a580b3a | 5626 | \f |
14b1c01e AM |
5627 | struct hash_codes_info |
5628 | { | |
5629 | unsigned long *hashcodes; | |
5630 | bfd_boolean error; | |
5631 | }; | |
a0c8462f | 5632 | |
5a580b3a AM |
5633 | /* This function will be called though elf_link_hash_traverse to store |
5634 | all hash value of the exported symbols in an array. */ | |
5635 | ||
5636 | static bfd_boolean | |
5637 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5638 | { | |
a50b1753 | 5639 | struct hash_codes_info *inf = (struct hash_codes_info *) data; |
5a580b3a | 5640 | const char *name; |
5a580b3a AM |
5641 | unsigned long ha; |
5642 | char *alc = NULL; | |
5643 | ||
5a580b3a AM |
5644 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5645 | if (h->dynindx == -1) | |
5646 | return TRUE; | |
5647 | ||
5648 | name = h->root.root.string; | |
422f1182 | 5649 | if (h->versioned >= versioned) |
5a580b3a | 5650 | { |
422f1182 L |
5651 | char *p = strchr (name, ELF_VER_CHR); |
5652 | if (p != NULL) | |
14b1c01e | 5653 | { |
422f1182 L |
5654 | alc = (char *) bfd_malloc (p - name + 1); |
5655 | if (alc == NULL) | |
5656 | { | |
5657 | inf->error = TRUE; | |
5658 | return FALSE; | |
5659 | } | |
5660 | memcpy (alc, name, p - name); | |
5661 | alc[p - name] = '\0'; | |
5662 | name = alc; | |
14b1c01e | 5663 | } |
5a580b3a AM |
5664 | } |
5665 | ||
5666 | /* Compute the hash value. */ | |
5667 | ha = bfd_elf_hash (name); | |
5668 | ||
5669 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 5670 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
5671 | |
5672 | /* And store it in the struct so that we can put it in the hash table | |
5673 | later. */ | |
f6e332e6 | 5674 | h->u.elf_hash_value = ha; |
5a580b3a AM |
5675 | |
5676 | if (alc != NULL) | |
5677 | free (alc); | |
5678 | ||
5679 | return TRUE; | |
5680 | } | |
5681 | ||
fdc90cb4 JJ |
5682 | struct collect_gnu_hash_codes |
5683 | { | |
5684 | bfd *output_bfd; | |
5685 | const struct elf_backend_data *bed; | |
5686 | unsigned long int nsyms; | |
5687 | unsigned long int maskbits; | |
5688 | unsigned long int *hashcodes; | |
5689 | unsigned long int *hashval; | |
5690 | unsigned long int *indx; | |
5691 | unsigned long int *counts; | |
5692 | bfd_vma *bitmask; | |
5693 | bfd_byte *contents; | |
5694 | long int min_dynindx; | |
5695 | unsigned long int bucketcount; | |
5696 | unsigned long int symindx; | |
5697 | long int local_indx; | |
5698 | long int shift1, shift2; | |
5699 | unsigned long int mask; | |
14b1c01e | 5700 | bfd_boolean error; |
fdc90cb4 JJ |
5701 | }; |
5702 | ||
5703 | /* This function will be called though elf_link_hash_traverse to store | |
5704 | all hash value of the exported symbols in an array. */ | |
5705 | ||
5706 | static bfd_boolean | |
5707 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5708 | { | |
a50b1753 | 5709 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 | 5710 | const char *name; |
fdc90cb4 JJ |
5711 | unsigned long ha; |
5712 | char *alc = NULL; | |
5713 | ||
fdc90cb4 JJ |
5714 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5715 | if (h->dynindx == -1) | |
5716 | return TRUE; | |
5717 | ||
5718 | /* Ignore also local symbols and undefined symbols. */ | |
5719 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5720 | return TRUE; | |
5721 | ||
5722 | name = h->root.root.string; | |
422f1182 | 5723 | if (h->versioned >= versioned) |
fdc90cb4 | 5724 | { |
422f1182 L |
5725 | char *p = strchr (name, ELF_VER_CHR); |
5726 | if (p != NULL) | |
14b1c01e | 5727 | { |
422f1182 L |
5728 | alc = (char *) bfd_malloc (p - name + 1); |
5729 | if (alc == NULL) | |
5730 | { | |
5731 | s->error = TRUE; | |
5732 | return FALSE; | |
5733 | } | |
5734 | memcpy (alc, name, p - name); | |
5735 | alc[p - name] = '\0'; | |
5736 | name = alc; | |
14b1c01e | 5737 | } |
fdc90cb4 JJ |
5738 | } |
5739 | ||
5740 | /* Compute the hash value. */ | |
5741 | ha = bfd_elf_gnu_hash (name); | |
5742 | ||
5743 | /* Store the found hash value in the array for compute_bucket_count, | |
5744 | and also for .dynsym reordering purposes. */ | |
5745 | s->hashcodes[s->nsyms] = ha; | |
5746 | s->hashval[h->dynindx] = ha; | |
5747 | ++s->nsyms; | |
5748 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5749 | s->min_dynindx = h->dynindx; | |
5750 | ||
5751 | if (alc != NULL) | |
5752 | free (alc); | |
5753 | ||
5754 | return TRUE; | |
5755 | } | |
5756 | ||
5757 | /* This function will be called though elf_link_hash_traverse to do | |
5758 | final dynaminc symbol renumbering. */ | |
5759 | ||
5760 | static bfd_boolean | |
5761 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5762 | { | |
a50b1753 | 5763 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
5764 | unsigned long int bucket; |
5765 | unsigned long int val; | |
5766 | ||
fdc90cb4 JJ |
5767 | /* Ignore indirect symbols. */ |
5768 | if (h->dynindx == -1) | |
5769 | return TRUE; | |
5770 | ||
5771 | /* Ignore also local symbols and undefined symbols. */ | |
5772 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5773 | { | |
5774 | if (h->dynindx >= s->min_dynindx) | |
5775 | h->dynindx = s->local_indx++; | |
5776 | return TRUE; | |
5777 | } | |
5778 | ||
5779 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5780 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5781 | & ((s->maskbits >> s->shift1) - 1); | |
5782 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5783 | s->bitmask[val] | |
5784 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5785 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5786 | if (s->counts[bucket] == 1) | |
5787 | /* Last element terminates the chain. */ | |
5788 | val |= 1; | |
5789 | bfd_put_32 (s->output_bfd, val, | |
5790 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5791 | --s->counts[bucket]; | |
5792 | h->dynindx = s->indx[bucket]++; | |
5793 | return TRUE; | |
5794 | } | |
5795 | ||
5796 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5797 | ||
5798 | bfd_boolean | |
5799 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5800 | { | |
5801 | return !(h->forced_local | |
5802 | || h->root.type == bfd_link_hash_undefined | |
5803 | || h->root.type == bfd_link_hash_undefweak | |
5804 | || ((h->root.type == bfd_link_hash_defined | |
5805 | || h->root.type == bfd_link_hash_defweak) | |
5806 | && h->root.u.def.section->output_section == NULL)); | |
5807 | } | |
5808 | ||
5a580b3a AM |
5809 | /* Array used to determine the number of hash table buckets to use |
5810 | based on the number of symbols there are. If there are fewer than | |
5811 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5812 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5813 | than 32771 buckets. */ | |
5814 | ||
5815 | static const size_t elf_buckets[] = | |
5816 | { | |
5817 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5818 | 16411, 32771, 0 | |
5819 | }; | |
5820 | ||
5821 | /* Compute bucket count for hashing table. We do not use a static set | |
5822 | of possible tables sizes anymore. Instead we determine for all | |
5823 | possible reasonable sizes of the table the outcome (i.e., the | |
5824 | number of collisions etc) and choose the best solution. The | |
5825 | weighting functions are not too simple to allow the table to grow | |
5826 | without bounds. Instead one of the weighting factors is the size. | |
5827 | Therefore the result is always a good payoff between few collisions | |
5828 | (= short chain lengths) and table size. */ | |
5829 | static size_t | |
b20dd2ce | 5830 | compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
d40f3da9 AM |
5831 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, |
5832 | unsigned long int nsyms, | |
5833 | int gnu_hash) | |
5a580b3a | 5834 | { |
5a580b3a | 5835 | size_t best_size = 0; |
5a580b3a | 5836 | unsigned long int i; |
5a580b3a | 5837 | |
5a580b3a AM |
5838 | /* We have a problem here. The following code to optimize the table |
5839 | size requires an integer type with more the 32 bits. If | |
5840 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5841 | #ifdef BFD_HOST_U_64_BIT | |
5842 | if (info->optimize) | |
5843 | { | |
5a580b3a AM |
5844 | size_t minsize; |
5845 | size_t maxsize; | |
5846 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a | 5847 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 5848 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 5849 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 5850 | unsigned long int *counts; |
d40f3da9 | 5851 | bfd_size_type amt; |
0883b6e0 | 5852 | unsigned int no_improvement_count = 0; |
5a580b3a AM |
5853 | |
5854 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5855 | that the hashing table must at least have NSYMS/4 and at most | |
5856 | 2*NSYMS buckets. */ | |
5857 | minsize = nsyms / 4; | |
5858 | if (minsize == 0) | |
5859 | minsize = 1; | |
5860 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5861 | if (gnu_hash) |
5862 | { | |
5863 | if (minsize < 2) | |
5864 | minsize = 2; | |
5865 | if ((best_size & 31) == 0) | |
5866 | ++best_size; | |
5867 | } | |
5a580b3a AM |
5868 | |
5869 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5870 | since the size could be large. */ | |
5871 | amt = maxsize; | |
5872 | amt *= sizeof (unsigned long int); | |
a50b1753 | 5873 | counts = (unsigned long int *) bfd_malloc (amt); |
5a580b3a | 5874 | if (counts == NULL) |
fdc90cb4 | 5875 | return 0; |
5a580b3a AM |
5876 | |
5877 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5878 | minimal chain length. The minor criteria is (of course) the size | |
5879 | of the table. */ | |
5880 | for (i = minsize; i < maxsize; ++i) | |
5881 | { | |
5882 | /* Walk through the array of hashcodes and count the collisions. */ | |
5883 | BFD_HOST_U_64_BIT max; | |
5884 | unsigned long int j; | |
5885 | unsigned long int fact; | |
5886 | ||
fdc90cb4 JJ |
5887 | if (gnu_hash && (i & 31) == 0) |
5888 | continue; | |
5889 | ||
5a580b3a AM |
5890 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5891 | ||
5892 | /* Determine how often each hash bucket is used. */ | |
5893 | for (j = 0; j < nsyms; ++j) | |
5894 | ++counts[hashcodes[j] % i]; | |
5895 | ||
5896 | /* For the weight function we need some information about the | |
5897 | pagesize on the target. This is information need not be 100% | |
5898 | accurate. Since this information is not available (so far) we | |
5899 | define it here to a reasonable default value. If it is crucial | |
5900 | to have a better value some day simply define this value. */ | |
5901 | # ifndef BFD_TARGET_PAGESIZE | |
5902 | # define BFD_TARGET_PAGESIZE (4096) | |
5903 | # endif | |
5904 | ||
fdc90cb4 JJ |
5905 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5906 | and the chains. */ | |
5907 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
5908 | |
5909 | # if 1 | |
5910 | /* Variant 1: optimize for short chains. We add the squares | |
5911 | of all the chain lengths (which favors many small chain | |
5912 | over a few long chains). */ | |
5913 | for (j = 0; j < i; ++j) | |
5914 | max += counts[j] * counts[j]; | |
5915 | ||
5916 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 5917 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5918 | max *= fact * fact; |
5919 | # else | |
5920 | /* Variant 2: Optimize a lot more for small table. Here we | |
5921 | also add squares of the size but we also add penalties for | |
5922 | empty slots (the +1 term). */ | |
5923 | for (j = 0; j < i; ++j) | |
5924 | max += (1 + counts[j]) * (1 + counts[j]); | |
5925 | ||
5926 | /* The overall size of the table is considered, but not as | |
5927 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 5928 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5929 | max *= fact; |
5930 | # endif | |
5931 | ||
5932 | /* Compare with current best results. */ | |
5933 | if (max < best_chlen) | |
5934 | { | |
5935 | best_chlen = max; | |
5936 | best_size = i; | |
ca4be51c | 5937 | no_improvement_count = 0; |
5a580b3a | 5938 | } |
0883b6e0 NC |
5939 | /* PR 11843: Avoid futile long searches for the best bucket size |
5940 | when there are a large number of symbols. */ | |
5941 | else if (++no_improvement_count == 100) | |
5942 | break; | |
5a580b3a AM |
5943 | } |
5944 | ||
5945 | free (counts); | |
5946 | } | |
5947 | else | |
5948 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
5949 | { | |
5950 | /* This is the fallback solution if no 64bit type is available or if we | |
5951 | are not supposed to spend much time on optimizations. We select the | |
5952 | bucket count using a fixed set of numbers. */ | |
5953 | for (i = 0; elf_buckets[i] != 0; i++) | |
5954 | { | |
5955 | best_size = elf_buckets[i]; | |
fdc90cb4 | 5956 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
5957 | break; |
5958 | } | |
fdc90cb4 JJ |
5959 | if (gnu_hash && best_size < 2) |
5960 | best_size = 2; | |
5a580b3a AM |
5961 | } |
5962 | ||
5a580b3a AM |
5963 | return best_size; |
5964 | } | |
5965 | ||
d0bf826b AM |
5966 | /* Size any SHT_GROUP section for ld -r. */ |
5967 | ||
5968 | bfd_boolean | |
5969 | _bfd_elf_size_group_sections (struct bfd_link_info *info) | |
5970 | { | |
5971 | bfd *ibfd; | |
57963c05 | 5972 | asection *s; |
d0bf826b | 5973 | |
c72f2fb2 | 5974 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
d0bf826b | 5975 | if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour |
57963c05 AM |
5976 | && (s = ibfd->sections) != NULL |
5977 | && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS | |
d0bf826b AM |
5978 | && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) |
5979 | return FALSE; | |
5980 | return TRUE; | |
5981 | } | |
5982 | ||
04c3a755 NS |
5983 | /* Set a default stack segment size. The value in INFO wins. If it |
5984 | is unset, LEGACY_SYMBOL's value is used, and if that symbol is | |
5985 | undefined it is initialized. */ | |
5986 | ||
5987 | bfd_boolean | |
5988 | bfd_elf_stack_segment_size (bfd *output_bfd, | |
5989 | struct bfd_link_info *info, | |
5990 | const char *legacy_symbol, | |
5991 | bfd_vma default_size) | |
5992 | { | |
5993 | struct elf_link_hash_entry *h = NULL; | |
5994 | ||
5995 | /* Look for legacy symbol. */ | |
5996 | if (legacy_symbol) | |
5997 | h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, | |
5998 | FALSE, FALSE, FALSE); | |
5999 | if (h && (h->root.type == bfd_link_hash_defined | |
6000 | || h->root.type == bfd_link_hash_defweak) | |
6001 | && h->def_regular | |
6002 | && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) | |
6003 | { | |
6004 | /* The symbol has no type if specified on the command line. */ | |
6005 | h->type = STT_OBJECT; | |
6006 | if (info->stacksize) | |
695344c0 | 6007 | /* xgettext:c-format */ |
4eca0228 AM |
6008 | _bfd_error_handler (_("%B: stack size specified and %s set"), |
6009 | output_bfd, legacy_symbol); | |
04c3a755 | 6010 | else if (h->root.u.def.section != bfd_abs_section_ptr) |
695344c0 | 6011 | /* xgettext:c-format */ |
4eca0228 AM |
6012 | _bfd_error_handler (_("%B: %s not absolute"), |
6013 | output_bfd, legacy_symbol); | |
04c3a755 NS |
6014 | else |
6015 | info->stacksize = h->root.u.def.value; | |
6016 | } | |
6017 | ||
6018 | if (!info->stacksize) | |
6019 | /* If the user didn't set a size, or explicitly inhibit the | |
6020 | size, set it now. */ | |
6021 | info->stacksize = default_size; | |
6022 | ||
6023 | /* Provide the legacy symbol, if it is referenced. */ | |
6024 | if (h && (h->root.type == bfd_link_hash_undefined | |
6025 | || h->root.type == bfd_link_hash_undefweak)) | |
6026 | { | |
6027 | struct bfd_link_hash_entry *bh = NULL; | |
6028 | ||
6029 | if (!(_bfd_generic_link_add_one_symbol | |
6030 | (info, output_bfd, legacy_symbol, | |
6031 | BSF_GLOBAL, bfd_abs_section_ptr, | |
6032 | info->stacksize >= 0 ? info->stacksize : 0, | |
6033 | NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh))) | |
6034 | return FALSE; | |
6035 | ||
6036 | h = (struct elf_link_hash_entry *) bh; | |
6037 | h->def_regular = 1; | |
6038 | h->type = STT_OBJECT; | |
6039 | } | |
6040 | ||
6041 | return TRUE; | |
6042 | } | |
6043 | ||
b531344c MR |
6044 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ |
6045 | ||
6046 | struct elf_gc_sweep_symbol_info | |
6047 | { | |
6048 | struct bfd_link_info *info; | |
6049 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
6050 | bfd_boolean); | |
6051 | }; | |
6052 | ||
6053 | static bfd_boolean | |
6054 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) | |
6055 | { | |
6056 | if (!h->mark | |
6057 | && (((h->root.type == bfd_link_hash_defined | |
6058 | || h->root.type == bfd_link_hash_defweak) | |
6059 | && !((h->def_regular || ELF_COMMON_DEF_P (h)) | |
6060 | && h->root.u.def.section->gc_mark)) | |
6061 | || h->root.type == bfd_link_hash_undefined | |
6062 | || h->root.type == bfd_link_hash_undefweak)) | |
6063 | { | |
6064 | struct elf_gc_sweep_symbol_info *inf; | |
6065 | ||
6066 | inf = (struct elf_gc_sweep_symbol_info *) data; | |
6067 | (*inf->hide_symbol) (inf->info, h, TRUE); | |
6068 | h->def_regular = 0; | |
6069 | h->ref_regular = 0; | |
6070 | h->ref_regular_nonweak = 0; | |
6071 | } | |
6072 | ||
6073 | return TRUE; | |
6074 | } | |
6075 | ||
5a580b3a AM |
6076 | /* Set up the sizes and contents of the ELF dynamic sections. This is |
6077 | called by the ELF linker emulation before_allocation routine. We | |
6078 | must set the sizes of the sections before the linker sets the | |
6079 | addresses of the various sections. */ | |
6080 | ||
6081 | bfd_boolean | |
6082 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
6083 | const char *soname, | |
6084 | const char *rpath, | |
6085 | const char *filter_shlib, | |
7ee314fa AM |
6086 | const char *audit, |
6087 | const char *depaudit, | |
5a580b3a AM |
6088 | const char * const *auxiliary_filters, |
6089 | struct bfd_link_info *info, | |
fd91d419 | 6090 | asection **sinterpptr) |
5a580b3a | 6091 | { |
5a580b3a AM |
6092 | bfd *dynobj; |
6093 | const struct elf_backend_data *bed; | |
5a580b3a AM |
6094 | |
6095 | *sinterpptr = NULL; | |
6096 | ||
5a580b3a AM |
6097 | if (!is_elf_hash_table (info->hash)) |
6098 | return TRUE; | |
6099 | ||
5a580b3a AM |
6100 | dynobj = elf_hash_table (info)->dynobj; |
6101 | ||
9a2a56cc | 6102 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a | 6103 | { |
902e9fc7 MR |
6104 | struct bfd_elf_version_tree *verdefs; |
6105 | struct elf_info_failed asvinfo; | |
5a580b3a AM |
6106 | struct bfd_elf_version_tree *t; |
6107 | struct bfd_elf_version_expr *d; | |
902e9fc7 | 6108 | asection *s; |
e6699019 | 6109 | size_t soname_indx; |
7ee314fa | 6110 | |
5a580b3a AM |
6111 | /* If we are supposed to export all symbols into the dynamic symbol |
6112 | table (this is not the normal case), then do so. */ | |
55255dae | 6113 | if (info->export_dynamic |
0e1862bb | 6114 | || (bfd_link_executable (info) && info->dynamic)) |
5a580b3a | 6115 | { |
3d13f3e9 AM |
6116 | struct elf_info_failed eif; |
6117 | ||
6118 | eif.info = info; | |
6119 | eif.failed = FALSE; | |
5a580b3a AM |
6120 | elf_link_hash_traverse (elf_hash_table (info), |
6121 | _bfd_elf_export_symbol, | |
6122 | &eif); | |
6123 | if (eif.failed) | |
6124 | return FALSE; | |
6125 | } | |
6126 | ||
e6699019 L |
6127 | if (soname != NULL) |
6128 | { | |
6129 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6130 | soname, TRUE); | |
6131 | if (soname_indx == (size_t) -1 | |
6132 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
6133 | return FALSE; | |
6134 | } | |
6135 | else | |
6136 | soname_indx = (size_t) -1; | |
6137 | ||
5a580b3a | 6138 | /* Make all global versions with definition. */ |
fd91d419 | 6139 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6140 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6141 | if (!d->symver && d->literal) |
5a580b3a AM |
6142 | { |
6143 | const char *verstr, *name; | |
6144 | size_t namelen, verlen, newlen; | |
93252b1c | 6145 | char *newname, *p, leading_char; |
5a580b3a AM |
6146 | struct elf_link_hash_entry *newh; |
6147 | ||
93252b1c | 6148 | leading_char = bfd_get_symbol_leading_char (output_bfd); |
ae5a3597 | 6149 | name = d->pattern; |
93252b1c | 6150 | namelen = strlen (name) + (leading_char != '\0'); |
5a580b3a AM |
6151 | verstr = t->name; |
6152 | verlen = strlen (verstr); | |
6153 | newlen = namelen + verlen + 3; | |
6154 | ||
a50b1753 | 6155 | newname = (char *) bfd_malloc (newlen); |
5a580b3a AM |
6156 | if (newname == NULL) |
6157 | return FALSE; | |
93252b1c MF |
6158 | newname[0] = leading_char; |
6159 | memcpy (newname + (leading_char != '\0'), name, namelen); | |
5a580b3a AM |
6160 | |
6161 | /* Check the hidden versioned definition. */ | |
6162 | p = newname + namelen; | |
6163 | *p++ = ELF_VER_CHR; | |
6164 | memcpy (p, verstr, verlen + 1); | |
6165 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6166 | newname, FALSE, FALSE, | |
6167 | FALSE); | |
6168 | if (newh == NULL | |
6169 | || (newh->root.type != bfd_link_hash_defined | |
6170 | && newh->root.type != bfd_link_hash_defweak)) | |
6171 | { | |
6172 | /* Check the default versioned definition. */ | |
6173 | *p++ = ELF_VER_CHR; | |
6174 | memcpy (p, verstr, verlen + 1); | |
6175 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6176 | newname, FALSE, FALSE, | |
6177 | FALSE); | |
6178 | } | |
6179 | free (newname); | |
6180 | ||
6181 | /* Mark this version if there is a definition and it is | |
6182 | not defined in a shared object. */ | |
6183 | if (newh != NULL | |
f5385ebf | 6184 | && !newh->def_dynamic |
5a580b3a AM |
6185 | && (newh->root.type == bfd_link_hash_defined |
6186 | || newh->root.type == bfd_link_hash_defweak)) | |
6187 | d->symver = 1; | |
6188 | } | |
6189 | ||
6190 | /* Attach all the symbols to their version information. */ | |
5a580b3a | 6191 | asvinfo.info = info; |
5a580b3a AM |
6192 | asvinfo.failed = FALSE; |
6193 | ||
6194 | elf_link_hash_traverse (elf_hash_table (info), | |
6195 | _bfd_elf_link_assign_sym_version, | |
6196 | &asvinfo); | |
6197 | if (asvinfo.failed) | |
6198 | return FALSE; | |
6199 | ||
6200 | if (!info->allow_undefined_version) | |
6201 | { | |
6202 | /* Check if all global versions have a definition. */ | |
3d13f3e9 | 6203 | bfd_boolean all_defined = TRUE; |
fd91d419 | 6204 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6205 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6206 | if (d->literal && !d->symver && !d->script) |
5a580b3a | 6207 | { |
4eca0228 | 6208 | _bfd_error_handler |
5a580b3a AM |
6209 | (_("%s: undefined version: %s"), |
6210 | d->pattern, t->name); | |
6211 | all_defined = FALSE; | |
6212 | } | |
6213 | ||
6214 | if (!all_defined) | |
6215 | { | |
6216 | bfd_set_error (bfd_error_bad_value); | |
6217 | return FALSE; | |
6218 | } | |
6219 | } | |
6220 | ||
902e9fc7 MR |
6221 | /* Set up the version definition section. */ |
6222 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); | |
6223 | BFD_ASSERT (s != NULL); | |
5a580b3a | 6224 | |
902e9fc7 MR |
6225 | /* We may have created additional version definitions if we are |
6226 | just linking a regular application. */ | |
6227 | verdefs = info->version_info; | |
5a580b3a | 6228 | |
902e9fc7 MR |
6229 | /* Skip anonymous version tag. */ |
6230 | if (verdefs != NULL && verdefs->vernum == 0) | |
6231 | verdefs = verdefs->next; | |
5a580b3a | 6232 | |
902e9fc7 MR |
6233 | if (verdefs == NULL && !info->create_default_symver) |
6234 | s->flags |= SEC_EXCLUDE; | |
6235 | else | |
5a580b3a | 6236 | { |
902e9fc7 MR |
6237 | unsigned int cdefs; |
6238 | bfd_size_type size; | |
6239 | bfd_byte *p; | |
6240 | Elf_Internal_Verdef def; | |
6241 | Elf_Internal_Verdaux defaux; | |
6242 | struct bfd_link_hash_entry *bh; | |
6243 | struct elf_link_hash_entry *h; | |
6244 | const char *name; | |
5a580b3a | 6245 | |
902e9fc7 MR |
6246 | cdefs = 0; |
6247 | size = 0; | |
5a580b3a | 6248 | |
902e9fc7 MR |
6249 | /* Make space for the base version. */ |
6250 | size += sizeof (Elf_External_Verdef); | |
6251 | size += sizeof (Elf_External_Verdaux); | |
6252 | ++cdefs; | |
6253 | ||
6254 | /* Make space for the default version. */ | |
6255 | if (info->create_default_symver) | |
6256 | { | |
6257 | size += sizeof (Elf_External_Verdef); | |
6258 | ++cdefs; | |
3e3b46e5 PB |
6259 | } |
6260 | ||
5a580b3a AM |
6261 | for (t = verdefs; t != NULL; t = t->next) |
6262 | { | |
6263 | struct bfd_elf_version_deps *n; | |
6264 | ||
a6cc6b3b RO |
6265 | /* Don't emit base version twice. */ |
6266 | if (t->vernum == 0) | |
6267 | continue; | |
6268 | ||
5a580b3a AM |
6269 | size += sizeof (Elf_External_Verdef); |
6270 | size += sizeof (Elf_External_Verdaux); | |
6271 | ++cdefs; | |
6272 | ||
6273 | for (n = t->deps; n != NULL; n = n->next) | |
6274 | size += sizeof (Elf_External_Verdaux); | |
6275 | } | |
6276 | ||
eea6121a | 6277 | s->size = size; |
a50b1753 | 6278 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
eea6121a | 6279 | if (s->contents == NULL && s->size != 0) |
5a580b3a AM |
6280 | return FALSE; |
6281 | ||
6282 | /* Fill in the version definition section. */ | |
6283 | ||
6284 | p = s->contents; | |
6285 | ||
6286 | def.vd_version = VER_DEF_CURRENT; | |
6287 | def.vd_flags = VER_FLG_BASE; | |
6288 | def.vd_ndx = 1; | |
6289 | def.vd_cnt = 1; | |
3e3b46e5 PB |
6290 | if (info->create_default_symver) |
6291 | { | |
6292 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
6293 | def.vd_next = sizeof (Elf_External_Verdef); | |
6294 | } | |
6295 | else | |
6296 | { | |
6297 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6298 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6299 | + sizeof (Elf_External_Verdaux)); | |
6300 | } | |
5a580b3a | 6301 | |
ef53be89 | 6302 | if (soname_indx != (size_t) -1) |
5a580b3a AM |
6303 | { |
6304 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6305 | soname_indx); | |
6306 | def.vd_hash = bfd_elf_hash (soname); | |
6307 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 6308 | name = soname; |
5a580b3a AM |
6309 | } |
6310 | else | |
6311 | { | |
ef53be89 | 6312 | size_t indx; |
5a580b3a | 6313 | |
06084812 | 6314 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
6315 | def.vd_hash = bfd_elf_hash (name); |
6316 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6317 | name, FALSE); | |
ef53be89 | 6318 | if (indx == (size_t) -1) |
5a580b3a AM |
6319 | return FALSE; |
6320 | defaux.vda_name = indx; | |
6321 | } | |
6322 | defaux.vda_next = 0; | |
6323 | ||
6324 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6325 | (Elf_External_Verdef *) p); | |
6326 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
6327 | if (info->create_default_symver) |
6328 | { | |
6329 | /* Add a symbol representing this version. */ | |
6330 | bh = NULL; | |
6331 | if (! (_bfd_generic_link_add_one_symbol | |
6332 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6333 | 0, NULL, FALSE, | |
6334 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6335 | return FALSE; | |
6336 | h = (struct elf_link_hash_entry *) bh; | |
6337 | h->non_elf = 0; | |
6338 | h->def_regular = 1; | |
6339 | h->type = STT_OBJECT; | |
6340 | h->verinfo.vertree = NULL; | |
6341 | ||
6342 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
6343 | return FALSE; | |
6344 | ||
6345 | /* Create a duplicate of the base version with the same | |
6346 | aux block, but different flags. */ | |
6347 | def.vd_flags = 0; | |
6348 | def.vd_ndx = 2; | |
6349 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6350 | if (verdefs) | |
6351 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6352 | + sizeof (Elf_External_Verdaux)); | |
6353 | else | |
6354 | def.vd_next = 0; | |
6355 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6356 | (Elf_External_Verdef *) p); | |
6357 | p += sizeof (Elf_External_Verdef); | |
6358 | } | |
5a580b3a AM |
6359 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
6360 | (Elf_External_Verdaux *) p); | |
6361 | p += sizeof (Elf_External_Verdaux); | |
6362 | ||
6363 | for (t = verdefs; t != NULL; t = t->next) | |
6364 | { | |
6365 | unsigned int cdeps; | |
6366 | struct bfd_elf_version_deps *n; | |
5a580b3a | 6367 | |
a6cc6b3b RO |
6368 | /* Don't emit the base version twice. */ |
6369 | if (t->vernum == 0) | |
6370 | continue; | |
6371 | ||
5a580b3a AM |
6372 | cdeps = 0; |
6373 | for (n = t->deps; n != NULL; n = n->next) | |
6374 | ++cdeps; | |
6375 | ||
6376 | /* Add a symbol representing this version. */ | |
6377 | bh = NULL; | |
6378 | if (! (_bfd_generic_link_add_one_symbol | |
6379 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6380 | 0, NULL, FALSE, | |
6381 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6382 | return FALSE; | |
6383 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
6384 | h->non_elf = 0; |
6385 | h->def_regular = 1; | |
5a580b3a AM |
6386 | h->type = STT_OBJECT; |
6387 | h->verinfo.vertree = t; | |
6388 | ||
c152c796 | 6389 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
6390 | return FALSE; |
6391 | ||
6392 | def.vd_version = VER_DEF_CURRENT; | |
6393 | def.vd_flags = 0; | |
6394 | if (t->globals.list == NULL | |
6395 | && t->locals.list == NULL | |
6396 | && ! t->used) | |
6397 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 6398 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
6399 | def.vd_cnt = cdeps + 1; |
6400 | def.vd_hash = bfd_elf_hash (t->name); | |
6401 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6402 | def.vd_next = 0; | |
a6cc6b3b RO |
6403 | |
6404 | /* If a basever node is next, it *must* be the last node in | |
6405 | the chain, otherwise Verdef construction breaks. */ | |
6406 | if (t->next != NULL && t->next->vernum == 0) | |
6407 | BFD_ASSERT (t->next->next == NULL); | |
6408 | ||
6409 | if (t->next != NULL && t->next->vernum != 0) | |
5a580b3a AM |
6410 | def.vd_next = (sizeof (Elf_External_Verdef) |
6411 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
6412 | ||
6413 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6414 | (Elf_External_Verdef *) p); | |
6415 | p += sizeof (Elf_External_Verdef); | |
6416 | ||
6417 | defaux.vda_name = h->dynstr_index; | |
6418 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6419 | h->dynstr_index); | |
6420 | defaux.vda_next = 0; | |
6421 | if (t->deps != NULL) | |
6422 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6423 | t->name_indx = defaux.vda_name; | |
6424 | ||
6425 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6426 | (Elf_External_Verdaux *) p); | |
6427 | p += sizeof (Elf_External_Verdaux); | |
6428 | ||
6429 | for (n = t->deps; n != NULL; n = n->next) | |
6430 | { | |
6431 | if (n->version_needed == NULL) | |
6432 | { | |
6433 | /* This can happen if there was an error in the | |
6434 | version script. */ | |
6435 | defaux.vda_name = 0; | |
6436 | } | |
6437 | else | |
6438 | { | |
6439 | defaux.vda_name = n->version_needed->name_indx; | |
6440 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6441 | defaux.vda_name); | |
6442 | } | |
6443 | if (n->next == NULL) | |
6444 | defaux.vda_next = 0; | |
6445 | else | |
6446 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6447 | ||
6448 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6449 | (Elf_External_Verdaux *) p); | |
6450 | p += sizeof (Elf_External_Verdaux); | |
6451 | } | |
6452 | } | |
6453 | ||
5a580b3a AM |
6454 | elf_tdata (output_bfd)->cverdefs = cdefs; |
6455 | } | |
902e9fc7 MR |
6456 | } |
6457 | ||
6458 | bed = get_elf_backend_data (output_bfd); | |
6459 | ||
6460 | if (info->gc_sections && bed->can_gc_sections) | |
6461 | { | |
6462 | struct elf_gc_sweep_symbol_info sweep_info; | |
902e9fc7 MR |
6463 | |
6464 | /* Remove the symbols that were in the swept sections from the | |
3d13f3e9 | 6465 | dynamic symbol table. */ |
902e9fc7 MR |
6466 | sweep_info.info = info; |
6467 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
6468 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
6469 | &sweep_info); | |
3d13f3e9 AM |
6470 | } |
6471 | ||
6472 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6473 | { | |
6474 | asection *s; | |
6475 | struct elf_find_verdep_info sinfo; | |
6476 | ||
6477 | /* Work out the size of the version reference section. */ | |
6478 | ||
6479 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); | |
6480 | BFD_ASSERT (s != NULL); | |
902e9fc7 | 6481 | |
3d13f3e9 AM |
6482 | sinfo.info = info; |
6483 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
6484 | if (sinfo.vers == 0) | |
6485 | sinfo.vers = 1; | |
6486 | sinfo.failed = FALSE; | |
6487 | ||
6488 | elf_link_hash_traverse (elf_hash_table (info), | |
6489 | _bfd_elf_link_find_version_dependencies, | |
6490 | &sinfo); | |
6491 | if (sinfo.failed) | |
6492 | return FALSE; | |
6493 | ||
6494 | if (elf_tdata (output_bfd)->verref == NULL) | |
6495 | s->flags |= SEC_EXCLUDE; | |
6496 | else | |
6497 | { | |
6498 | Elf_Internal_Verneed *vn; | |
6499 | unsigned int size; | |
6500 | unsigned int crefs; | |
6501 | bfd_byte *p; | |
6502 | ||
6503 | /* Build the version dependency section. */ | |
6504 | size = 0; | |
6505 | crefs = 0; | |
6506 | for (vn = elf_tdata (output_bfd)->verref; | |
6507 | vn != NULL; | |
6508 | vn = vn->vn_nextref) | |
6509 | { | |
6510 | Elf_Internal_Vernaux *a; | |
6511 | ||
6512 | size += sizeof (Elf_External_Verneed); | |
6513 | ++crefs; | |
6514 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6515 | size += sizeof (Elf_External_Vernaux); | |
6516 | } | |
6517 | ||
6518 | s->size = size; | |
6519 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); | |
6520 | if (s->contents == NULL) | |
6521 | return FALSE; | |
6522 | ||
6523 | p = s->contents; | |
6524 | for (vn = elf_tdata (output_bfd)->verref; | |
6525 | vn != NULL; | |
6526 | vn = vn->vn_nextref) | |
6527 | { | |
6528 | unsigned int caux; | |
6529 | Elf_Internal_Vernaux *a; | |
6530 | size_t indx; | |
6531 | ||
6532 | caux = 0; | |
6533 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6534 | ++caux; | |
6535 | ||
6536 | vn->vn_version = VER_NEED_CURRENT; | |
6537 | vn->vn_cnt = caux; | |
6538 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6539 | elf_dt_name (vn->vn_bfd) != NULL | |
6540 | ? elf_dt_name (vn->vn_bfd) | |
6541 | : lbasename (vn->vn_bfd->filename), | |
6542 | FALSE); | |
6543 | if (indx == (size_t) -1) | |
6544 | return FALSE; | |
6545 | vn->vn_file = indx; | |
6546 | vn->vn_aux = sizeof (Elf_External_Verneed); | |
6547 | if (vn->vn_nextref == NULL) | |
6548 | vn->vn_next = 0; | |
6549 | else | |
6550 | vn->vn_next = (sizeof (Elf_External_Verneed) | |
6551 | + caux * sizeof (Elf_External_Vernaux)); | |
6552 | ||
6553 | _bfd_elf_swap_verneed_out (output_bfd, vn, | |
6554 | (Elf_External_Verneed *) p); | |
6555 | p += sizeof (Elf_External_Verneed); | |
6556 | ||
6557 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6558 | { | |
6559 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6560 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6561 | a->vna_nodename, FALSE); | |
6562 | if (indx == (size_t) -1) | |
6563 | return FALSE; | |
6564 | a->vna_name = indx; | |
6565 | if (a->vna_nextptr == NULL) | |
6566 | a->vna_next = 0; | |
6567 | else | |
6568 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6569 | ||
6570 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6571 | (Elf_External_Vernaux *) p); | |
6572 | p += sizeof (Elf_External_Vernaux); | |
6573 | } | |
6574 | } | |
6575 | ||
6576 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6577 | } | |
902e9fc7 MR |
6578 | } |
6579 | ||
6580 | /* Any syms created from now on start with -1 in | |
6581 | got.refcount/offset and plt.refcount/offset. */ | |
6582 | elf_hash_table (info)->init_got_refcount | |
6583 | = elf_hash_table (info)->init_got_offset; | |
6584 | elf_hash_table (info)->init_plt_refcount | |
6585 | = elf_hash_table (info)->init_plt_offset; | |
6586 | ||
6587 | if (bfd_link_relocatable (info) | |
6588 | && !_bfd_elf_size_group_sections (info)) | |
6589 | return FALSE; | |
6590 | ||
6591 | /* The backend may have to create some sections regardless of whether | |
6592 | we're dynamic or not. */ | |
6593 | if (bed->elf_backend_always_size_sections | |
6594 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
6595 | return FALSE; | |
6596 | ||
6597 | /* Determine any GNU_STACK segment requirements, after the backend | |
6598 | has had a chance to set a default segment size. */ | |
6599 | if (info->execstack) | |
6600 | elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; | |
6601 | else if (info->noexecstack) | |
6602 | elf_stack_flags (output_bfd) = PF_R | PF_W; | |
6603 | else | |
6604 | { | |
6605 | bfd *inputobj; | |
6606 | asection *notesec = NULL; | |
6607 | int exec = 0; | |
6608 | ||
6609 | for (inputobj = info->input_bfds; | |
6610 | inputobj; | |
6611 | inputobj = inputobj->link.next) | |
6612 | { | |
6613 | asection *s; | |
6614 | ||
6615 | if (inputobj->flags | |
6616 | & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) | |
6617 | continue; | |
57963c05 AM |
6618 | s = inputobj->sections; |
6619 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
6620 | continue; | |
6621 | ||
902e9fc7 MR |
6622 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); |
6623 | if (s) | |
6624 | { | |
6625 | if (s->flags & SEC_CODE) | |
6626 | exec = PF_X; | |
6627 | notesec = s; | |
6628 | } | |
6629 | else if (bed->default_execstack) | |
6630 | exec = PF_X; | |
6631 | } | |
6632 | if (notesec || info->stacksize > 0) | |
6633 | elf_stack_flags (output_bfd) = PF_R | PF_W | exec; | |
6634 | if (notesec && exec && bfd_link_relocatable (info) | |
6635 | && notesec->output_section != bfd_abs_section_ptr) | |
6636 | notesec->output_section->flags |= SEC_CODE; | |
6637 | } | |
6638 | ||
6639 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6640 | { | |
6641 | struct elf_info_failed eif; | |
6642 | struct elf_link_hash_entry *h; | |
6643 | asection *dynstr; | |
6644 | asection *s; | |
6645 | ||
6646 | *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); | |
6647 | BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); | |
6648 | ||
902e9fc7 MR |
6649 | if (info->symbolic) |
6650 | { | |
6651 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
6652 | return FALSE; | |
6653 | info->flags |= DF_SYMBOLIC; | |
6654 | } | |
6655 | ||
6656 | if (rpath != NULL) | |
6657 | { | |
6658 | size_t indx; | |
6659 | bfd_vma tag; | |
6660 | ||
6661 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
6662 | TRUE); | |
6663 | if (indx == (size_t) -1) | |
6664 | return FALSE; | |
6665 | ||
6666 | tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; | |
6667 | if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) | |
6668 | return FALSE; | |
6669 | } | |
6670 | ||
6671 | if (filter_shlib != NULL) | |
6672 | { | |
6673 | size_t indx; | |
6674 | ||
6675 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6676 | filter_shlib, TRUE); | |
6677 | if (indx == (size_t) -1 | |
6678 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
6679 | return FALSE; | |
6680 | } | |
6681 | ||
6682 | if (auxiliary_filters != NULL) | |
6683 | { | |
6684 | const char * const *p; | |
6685 | ||
6686 | for (p = auxiliary_filters; *p != NULL; p++) | |
6687 | { | |
6688 | size_t indx; | |
6689 | ||
6690 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6691 | *p, TRUE); | |
6692 | if (indx == (size_t) -1 | |
6693 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
6694 | return FALSE; | |
6695 | } | |
6696 | } | |
6697 | ||
6698 | if (audit != NULL) | |
6699 | { | |
6700 | size_t indx; | |
6701 | ||
6702 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, | |
6703 | TRUE); | |
6704 | if (indx == (size_t) -1 | |
6705 | || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) | |
6706 | return FALSE; | |
6707 | } | |
6708 | ||
6709 | if (depaudit != NULL) | |
6710 | { | |
6711 | size_t indx; | |
6712 | ||
6713 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, | |
6714 | TRUE); | |
6715 | if (indx == (size_t) -1 | |
6716 | || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) | |
6717 | return FALSE; | |
6718 | } | |
6719 | ||
6720 | eif.info = info; | |
6721 | eif.failed = FALSE; | |
6722 | ||
6723 | /* Find all symbols which were defined in a dynamic object and make | |
6724 | the backend pick a reasonable value for them. */ | |
6725 | elf_link_hash_traverse (elf_hash_table (info), | |
6726 | _bfd_elf_adjust_dynamic_symbol, | |
6727 | &eif); | |
6728 | if (eif.failed) | |
6729 | return FALSE; | |
6730 | ||
6731 | /* Add some entries to the .dynamic section. We fill in some of the | |
6732 | values later, in bfd_elf_final_link, but we must add the entries | |
6733 | now so that we know the final size of the .dynamic section. */ | |
6734 | ||
6735 | /* If there are initialization and/or finalization functions to | |
6736 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
6737 | h = (info->init_function | |
6738 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6739 | info->init_function, FALSE, | |
6740 | FALSE, FALSE) | |
6741 | : NULL); | |
6742 | if (h != NULL | |
6743 | && (h->ref_regular | |
6744 | || h->def_regular)) | |
6745 | { | |
6746 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
6747 | return FALSE; | |
6748 | } | |
6749 | h = (info->fini_function | |
6750 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6751 | info->fini_function, FALSE, | |
6752 | FALSE, FALSE) | |
6753 | : NULL); | |
6754 | if (h != NULL | |
6755 | && (h->ref_regular | |
6756 | || h->def_regular)) | |
6757 | { | |
6758 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
6759 | return FALSE; | |
6760 | } | |
6761 | ||
6762 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); | |
6763 | if (s != NULL && s->linker_has_input) | |
6764 | { | |
6765 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
6766 | if (! bfd_link_executable (info)) | |
6767 | { | |
6768 | bfd *sub; | |
6769 | asection *o; | |
6770 | ||
57963c05 AM |
6771 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
6772 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
6773 | && (o = sub->sections) != NULL | |
6774 | && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS) | |
902e9fc7 MR |
6775 | for (o = sub->sections; o != NULL; o = o->next) |
6776 | if (elf_section_data (o)->this_hdr.sh_type | |
6777 | == SHT_PREINIT_ARRAY) | |
6778 | { | |
6779 | _bfd_error_handler | |
6780 | (_("%B: .preinit_array section is not allowed in DSO"), | |
6781 | sub); | |
6782 | break; | |
6783 | } | |
6784 | ||
6785 | bfd_set_error (bfd_error_nonrepresentable_section); | |
6786 | return FALSE; | |
6787 | } | |
6788 | ||
6789 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
6790 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
6791 | return FALSE; | |
6792 | } | |
6793 | s = bfd_get_section_by_name (output_bfd, ".init_array"); | |
6794 | if (s != NULL && s->linker_has_input) | |
6795 | { | |
6796 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
6797 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
6798 | return FALSE; | |
6799 | } | |
6800 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); | |
6801 | if (s != NULL && s->linker_has_input) | |
6802 | { | |
6803 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
6804 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
6805 | return FALSE; | |
6806 | } | |
6807 | ||
6808 | dynstr = bfd_get_linker_section (dynobj, ".dynstr"); | |
6809 | /* If .dynstr is excluded from the link, we don't want any of | |
6810 | these tags. Strictly, we should be checking each section | |
6811 | individually; This quick check covers for the case where | |
6812 | someone does a /DISCARD/ : { *(*) }. */ | |
6813 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
6814 | { | |
6815 | bfd_size_type strsize; | |
6816 | ||
6817 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
6818 | if ((info->emit_hash | |
6819 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
6820 | || (info->emit_gnu_hash | |
6821 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
6822 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
6823 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
6824 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
6825 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
6826 | bed->s->sizeof_sym)) | |
6827 | return FALSE; | |
6828 | } | |
6829 | } | |
6830 | ||
6831 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
6832 | return FALSE; | |
6833 | ||
6834 | /* The backend must work out the sizes of all the other dynamic | |
6835 | sections. */ | |
6836 | if (dynobj != NULL | |
6837 | && bed->elf_backend_size_dynamic_sections != NULL | |
6838 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
6839 | return FALSE; | |
6840 | ||
6841 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6842 | { | |
902e9fc7 MR |
6843 | if (elf_tdata (output_bfd)->cverdefs) |
6844 | { | |
6845 | unsigned int crefs = elf_tdata (output_bfd)->cverdefs; | |
6846 | ||
6847 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
6848 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs)) | |
6849 | return FALSE; | |
6850 | } | |
6851 | ||
6852 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
6853 | { | |
6854 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
6855 | return FALSE; | |
6856 | } | |
6857 | else if (info->flags & DF_BIND_NOW) | |
6858 | { | |
6859 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
6860 | return FALSE; | |
6861 | } | |
6862 | ||
6863 | if (info->flags_1) | |
6864 | { | |
6865 | if (bfd_link_executable (info)) | |
6866 | info->flags_1 &= ~ (DF_1_INITFIRST | |
6867 | | DF_1_NODELETE | |
6868 | | DF_1_NOOPEN); | |
6869 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
6870 | return FALSE; | |
6871 | } | |
6872 | ||
6873 | if (elf_tdata (output_bfd)->cverrefs) | |
6874 | { | |
6875 | unsigned int crefs = elf_tdata (output_bfd)->cverrefs; | |
6876 | ||
6877 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
6878 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
6879 | return FALSE; | |
6880 | } | |
5a580b3a | 6881 | |
8423293d AM |
6882 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
6883 | && elf_tdata (output_bfd)->cverdefs == 0) | |
63f452a8 | 6884 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1) |
8423293d | 6885 | { |
902e9fc7 MR |
6886 | asection *s; |
6887 | ||
3d4d4302 | 6888 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
8423293d AM |
6889 | s->flags |= SEC_EXCLUDE; |
6890 | } | |
6891 | } | |
6892 | return TRUE; | |
6893 | } | |
6894 | ||
74541ad4 AM |
6895 | /* Find the first non-excluded output section. We'll use its |
6896 | section symbol for some emitted relocs. */ | |
6897 | void | |
6898 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
6899 | { | |
6900 | asection *s; | |
6901 | ||
6902 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6903 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
6904 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6905 | { | |
6906 | elf_hash_table (info)->text_index_section = s; | |
6907 | break; | |
6908 | } | |
6909 | } | |
6910 | ||
6911 | /* Find two non-excluded output sections, one for code, one for data. | |
6912 | We'll use their section symbols for some emitted relocs. */ | |
6913 | void | |
6914 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
6915 | { | |
6916 | asection *s; | |
6917 | ||
266b05cf DJ |
6918 | /* Data first, since setting text_index_section changes |
6919 | _bfd_elf_link_omit_section_dynsym. */ | |
74541ad4 | 6920 | for (s = output_bfd->sections; s != NULL; s = s->next) |
266b05cf | 6921 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) |
74541ad4 AM |
6922 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6923 | { | |
266b05cf | 6924 | elf_hash_table (info)->data_index_section = s; |
74541ad4 AM |
6925 | break; |
6926 | } | |
6927 | ||
6928 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
266b05cf DJ |
6929 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) |
6930 | == (SEC_ALLOC | SEC_READONLY)) | |
74541ad4 AM |
6931 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6932 | { | |
266b05cf | 6933 | elf_hash_table (info)->text_index_section = s; |
74541ad4 AM |
6934 | break; |
6935 | } | |
6936 | ||
6937 | if (elf_hash_table (info)->text_index_section == NULL) | |
6938 | elf_hash_table (info)->text_index_section | |
6939 | = elf_hash_table (info)->data_index_section; | |
6940 | } | |
6941 | ||
8423293d AM |
6942 | bfd_boolean |
6943 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
6944 | { | |
74541ad4 | 6945 | const struct elf_backend_data *bed; |
23ec1e32 | 6946 | unsigned long section_sym_count; |
96d01d93 | 6947 | bfd_size_type dynsymcount = 0; |
74541ad4 | 6948 | |
8423293d AM |
6949 | if (!is_elf_hash_table (info->hash)) |
6950 | return TRUE; | |
6951 | ||
74541ad4 AM |
6952 | bed = get_elf_backend_data (output_bfd); |
6953 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
6954 | ||
23ec1e32 MR |
6955 | /* Assign dynsym indices. In a shared library we generate a section |
6956 | symbol for each output section, which come first. Next come all | |
6957 | of the back-end allocated local dynamic syms, followed by the rest | |
6958 | of the global symbols. | |
6959 | ||
6960 | This is usually not needed for static binaries, however backends | |
6961 | can request to always do it, e.g. the MIPS backend uses dynamic | |
6962 | symbol counts to lay out GOT, which will be produced in the | |
6963 | presence of GOT relocations even in static binaries (holding fixed | |
6964 | data in that case, to satisfy those relocations). */ | |
6965 | ||
6966 | if (elf_hash_table (info)->dynamic_sections_created | |
6967 | || bed->always_renumber_dynsyms) | |
6968 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
6969 | §ion_sym_count); | |
6970 | ||
8423293d AM |
6971 | if (elf_hash_table (info)->dynamic_sections_created) |
6972 | { | |
6973 | bfd *dynobj; | |
8423293d | 6974 | asection *s; |
8423293d AM |
6975 | unsigned int dtagcount; |
6976 | ||
6977 | dynobj = elf_hash_table (info)->dynobj; | |
6978 | ||
5a580b3a | 6979 | /* Work out the size of the symbol version section. */ |
3d4d4302 | 6980 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
5a580b3a | 6981 | BFD_ASSERT (s != NULL); |
d5486c43 | 6982 | if ((s->flags & SEC_EXCLUDE) == 0) |
5a580b3a | 6983 | { |
eea6121a | 6984 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
a50b1753 | 6985 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
5a580b3a AM |
6986 | if (s->contents == NULL) |
6987 | return FALSE; | |
6988 | ||
6989 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
6990 | return FALSE; | |
6991 | } | |
6992 | ||
6993 | /* Set the size of the .dynsym and .hash sections. We counted | |
6994 | the number of dynamic symbols in elf_link_add_object_symbols. | |
6995 | We will build the contents of .dynsym and .hash when we build | |
6996 | the final symbol table, because until then we do not know the | |
6997 | correct value to give the symbols. We built the .dynstr | |
6998 | section as we went along in elf_link_add_object_symbols. */ | |
cae1fbbb | 6999 | s = elf_hash_table (info)->dynsym; |
5a580b3a | 7000 | BFD_ASSERT (s != NULL); |
eea6121a | 7001 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a | 7002 | |
d5486c43 L |
7003 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
7004 | if (s->contents == NULL) | |
7005 | return FALSE; | |
5a580b3a | 7006 | |
d5486c43 L |
7007 | /* The first entry in .dynsym is a dummy symbol. Clear all the |
7008 | section syms, in case we don't output them all. */ | |
7009 | ++section_sym_count; | |
7010 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a | 7011 | |
fdc90cb4 JJ |
7012 | elf_hash_table (info)->bucketcount = 0; |
7013 | ||
5a580b3a AM |
7014 | /* Compute the size of the hashing table. As a side effect this |
7015 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
7016 | if (info->emit_hash) |
7017 | { | |
7018 | unsigned long int *hashcodes; | |
14b1c01e | 7019 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
7020 | bfd_size_type amt; |
7021 | unsigned long int nsyms; | |
7022 | size_t bucketcount; | |
7023 | size_t hash_entry_size; | |
7024 | ||
7025 | /* Compute the hash values for all exported symbols. At the same | |
7026 | time store the values in an array so that we could use them for | |
7027 | optimizations. */ | |
7028 | amt = dynsymcount * sizeof (unsigned long int); | |
a50b1753 | 7029 | hashcodes = (unsigned long int *) bfd_malloc (amt); |
fdc90cb4 JJ |
7030 | if (hashcodes == NULL) |
7031 | return FALSE; | |
14b1c01e AM |
7032 | hashinf.hashcodes = hashcodes; |
7033 | hashinf.error = FALSE; | |
5a580b3a | 7034 | |
fdc90cb4 JJ |
7035 | /* Put all hash values in HASHCODES. */ |
7036 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
7037 | elf_collect_hash_codes, &hashinf); |
7038 | if (hashinf.error) | |
4dd07732 AM |
7039 | { |
7040 | free (hashcodes); | |
7041 | return FALSE; | |
7042 | } | |
5a580b3a | 7043 | |
14b1c01e | 7044 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
7045 | bucketcount |
7046 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
7047 | free (hashcodes); | |
7048 | ||
4b48e2f6 | 7049 | if (bucketcount == 0 && nsyms > 0) |
fdc90cb4 | 7050 | return FALSE; |
5a580b3a | 7051 | |
fdc90cb4 JJ |
7052 | elf_hash_table (info)->bucketcount = bucketcount; |
7053 | ||
3d4d4302 | 7054 | s = bfd_get_linker_section (dynobj, ".hash"); |
fdc90cb4 JJ |
7055 | BFD_ASSERT (s != NULL); |
7056 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
7057 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
a50b1753 | 7058 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7059 | if (s->contents == NULL) |
7060 | return FALSE; | |
7061 | ||
7062 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
7063 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
7064 | s->contents + hash_entry_size); | |
7065 | } | |
7066 | ||
7067 | if (info->emit_gnu_hash) | |
7068 | { | |
7069 | size_t i, cnt; | |
7070 | unsigned char *contents; | |
7071 | struct collect_gnu_hash_codes cinfo; | |
7072 | bfd_size_type amt; | |
7073 | size_t bucketcount; | |
7074 | ||
7075 | memset (&cinfo, 0, sizeof (cinfo)); | |
7076 | ||
7077 | /* Compute the hash values for all exported symbols. At the same | |
7078 | time store the values in an array so that we could use them for | |
7079 | optimizations. */ | |
7080 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
a50b1753 | 7081 | cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); |
fdc90cb4 JJ |
7082 | if (cinfo.hashcodes == NULL) |
7083 | return FALSE; | |
7084 | ||
7085 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
7086 | cinfo.min_dynindx = -1; | |
7087 | cinfo.output_bfd = output_bfd; | |
7088 | cinfo.bed = bed; | |
7089 | ||
7090 | /* Put all hash values in HASHCODES. */ | |
7091 | elf_link_hash_traverse (elf_hash_table (info), | |
7092 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e | 7093 | if (cinfo.error) |
4dd07732 AM |
7094 | { |
7095 | free (cinfo.hashcodes); | |
7096 | return FALSE; | |
7097 | } | |
fdc90cb4 JJ |
7098 | |
7099 | bucketcount | |
7100 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
7101 | ||
7102 | if (bucketcount == 0) | |
7103 | { | |
7104 | free (cinfo.hashcodes); | |
7105 | return FALSE; | |
7106 | } | |
7107 | ||
3d4d4302 | 7108 | s = bfd_get_linker_section (dynobj, ".gnu.hash"); |
fdc90cb4 JJ |
7109 | BFD_ASSERT (s != NULL); |
7110 | ||
7111 | if (cinfo.nsyms == 0) | |
7112 | { | |
7113 | /* Empty .gnu.hash section is special. */ | |
7114 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
7115 | free (cinfo.hashcodes); | |
7116 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
a50b1753 | 7117 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7118 | if (contents == NULL) |
7119 | return FALSE; | |
7120 | s->contents = contents; | |
7121 | /* 1 empty bucket. */ | |
7122 | bfd_put_32 (output_bfd, 1, contents); | |
7123 | /* SYMIDX above the special symbol 0. */ | |
7124 | bfd_put_32 (output_bfd, 1, contents + 4); | |
7125 | /* Just one word for bitmask. */ | |
7126 | bfd_put_32 (output_bfd, 1, contents + 8); | |
7127 | /* Only hash fn bloom filter. */ | |
7128 | bfd_put_32 (output_bfd, 0, contents + 12); | |
7129 | /* No hashes are valid - empty bitmask. */ | |
7130 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
7131 | /* No hashes in the only bucket. */ | |
7132 | bfd_put_32 (output_bfd, 0, | |
7133 | contents + 16 + bed->s->arch_size / 8); | |
7134 | } | |
7135 | else | |
7136 | { | |
9e6619e2 | 7137 | unsigned long int maskwords, maskbitslog2, x; |
0b33793d | 7138 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 | 7139 | |
9e6619e2 AM |
7140 | x = cinfo.nsyms; |
7141 | maskbitslog2 = 1; | |
7142 | while ((x >>= 1) != 0) | |
7143 | ++maskbitslog2; | |
fdc90cb4 JJ |
7144 | if (maskbitslog2 < 3) |
7145 | maskbitslog2 = 5; | |
7146 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
7147 | maskbitslog2 = maskbitslog2 + 3; | |
7148 | else | |
7149 | maskbitslog2 = maskbitslog2 + 2; | |
7150 | if (bed->s->arch_size == 64) | |
7151 | { | |
7152 | if (maskbitslog2 == 5) | |
7153 | maskbitslog2 = 6; | |
7154 | cinfo.shift1 = 6; | |
7155 | } | |
7156 | else | |
7157 | cinfo.shift1 = 5; | |
7158 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 7159 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
7160 | cinfo.maskbits = 1 << maskbitslog2; |
7161 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
7162 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
7163 | amt += maskwords * sizeof (bfd_vma); | |
a50b1753 | 7164 | cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); |
fdc90cb4 JJ |
7165 | if (cinfo.bitmask == NULL) |
7166 | { | |
7167 | free (cinfo.hashcodes); | |
7168 | return FALSE; | |
7169 | } | |
7170 | ||
a50b1753 | 7171 | cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); |
fdc90cb4 JJ |
7172 | cinfo.indx = cinfo.counts + bucketcount; |
7173 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
7174 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
7175 | ||
7176 | /* Determine how often each hash bucket is used. */ | |
7177 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
7178 | for (i = 0; i < cinfo.nsyms; ++i) | |
7179 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
7180 | ||
7181 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
7182 | if (cinfo.counts[i] != 0) | |
7183 | { | |
7184 | cinfo.indx[i] = cnt; | |
7185 | cnt += cinfo.counts[i]; | |
7186 | } | |
7187 | BFD_ASSERT (cnt == dynsymcount); | |
7188 | cinfo.bucketcount = bucketcount; | |
7189 | cinfo.local_indx = cinfo.min_dynindx; | |
7190 | ||
7191 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
7192 | s->size += cinfo.maskbits / 8; | |
a50b1753 | 7193 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7194 | if (contents == NULL) |
7195 | { | |
7196 | free (cinfo.bitmask); | |
7197 | free (cinfo.hashcodes); | |
7198 | return FALSE; | |
7199 | } | |
7200 | ||
7201 | s->contents = contents; | |
7202 | bfd_put_32 (output_bfd, bucketcount, contents); | |
7203 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
7204 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
7205 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
7206 | contents += 16 + cinfo.maskbits / 8; | |
7207 | ||
7208 | for (i = 0; i < bucketcount; ++i) | |
7209 | { | |
7210 | if (cinfo.counts[i] == 0) | |
7211 | bfd_put_32 (output_bfd, 0, contents); | |
7212 | else | |
7213 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
7214 | contents += 4; | |
7215 | } | |
7216 | ||
7217 | cinfo.contents = contents; | |
7218 | ||
7219 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
7220 | elf_link_hash_traverse (elf_hash_table (info), | |
7221 | elf_renumber_gnu_hash_syms, &cinfo); | |
7222 | ||
7223 | contents = s->contents + 16; | |
7224 | for (i = 0; i < maskwords; ++i) | |
7225 | { | |
7226 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
7227 | contents); | |
7228 | contents += bed->s->arch_size / 8; | |
7229 | } | |
7230 | ||
7231 | free (cinfo.bitmask); | |
7232 | free (cinfo.hashcodes); | |
7233 | } | |
7234 | } | |
5a580b3a | 7235 | |
3d4d4302 | 7236 | s = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
7237 | BFD_ASSERT (s != NULL); |
7238 | ||
4ad4eba5 | 7239 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 7240 | |
eea6121a | 7241 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
7242 | |
7243 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
7244 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
7245 | return FALSE; | |
7246 | } | |
7247 | ||
7248 | return TRUE; | |
7249 | } | |
4d269e42 | 7250 | \f |
4d269e42 AM |
7251 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ |
7252 | ||
7253 | static void | |
7254 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
7255 | asection *sec) | |
7256 | { | |
dbaa2011 AM |
7257 | BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); |
7258 | sec->sec_info_type = SEC_INFO_TYPE_NONE; | |
4d269e42 AM |
7259 | } |
7260 | ||
7261 | /* Finish SHF_MERGE section merging. */ | |
7262 | ||
7263 | bfd_boolean | |
630993ec | 7264 | _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) |
4d269e42 AM |
7265 | { |
7266 | bfd *ibfd; | |
7267 | asection *sec; | |
7268 | ||
7269 | if (!is_elf_hash_table (info->hash)) | |
7270 | return FALSE; | |
7271 | ||
c72f2fb2 | 7272 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
630993ec AM |
7273 | if ((ibfd->flags & DYNAMIC) == 0 |
7274 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
017e6bce AM |
7275 | && (elf_elfheader (ibfd)->e_ident[EI_CLASS] |
7276 | == get_elf_backend_data (obfd)->s->elfclass)) | |
4d269e42 AM |
7277 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) |
7278 | if ((sec->flags & SEC_MERGE) != 0 | |
7279 | && !bfd_is_abs_section (sec->output_section)) | |
7280 | { | |
7281 | struct bfd_elf_section_data *secdata; | |
7282 | ||
7283 | secdata = elf_section_data (sec); | |
630993ec | 7284 | if (! _bfd_add_merge_section (obfd, |
4d269e42 AM |
7285 | &elf_hash_table (info)->merge_info, |
7286 | sec, &secdata->sec_info)) | |
7287 | return FALSE; | |
7288 | else if (secdata->sec_info) | |
dbaa2011 | 7289 | sec->sec_info_type = SEC_INFO_TYPE_MERGE; |
4d269e42 AM |
7290 | } |
7291 | ||
7292 | if (elf_hash_table (info)->merge_info != NULL) | |
630993ec | 7293 | _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, |
4d269e42 AM |
7294 | merge_sections_remove_hook); |
7295 | return TRUE; | |
7296 | } | |
7297 | ||
7298 | /* Create an entry in an ELF linker hash table. */ | |
7299 | ||
7300 | struct bfd_hash_entry * | |
7301 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
7302 | struct bfd_hash_table *table, | |
7303 | const char *string) | |
7304 | { | |
7305 | /* Allocate the structure if it has not already been allocated by a | |
7306 | subclass. */ | |
7307 | if (entry == NULL) | |
7308 | { | |
a50b1753 | 7309 | entry = (struct bfd_hash_entry *) |
ca4be51c | 7310 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); |
4d269e42 AM |
7311 | if (entry == NULL) |
7312 | return entry; | |
7313 | } | |
7314 | ||
7315 | /* Call the allocation method of the superclass. */ | |
7316 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
7317 | if (entry != NULL) | |
7318 | { | |
7319 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
7320 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
7321 | ||
7322 | /* Set local fields. */ | |
7323 | ret->indx = -1; | |
7324 | ret->dynindx = -1; | |
7325 | ret->got = htab->init_got_refcount; | |
7326 | ret->plt = htab->init_plt_refcount; | |
7327 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
7328 | - offsetof (struct elf_link_hash_entry, size))); | |
7329 | /* Assume that we have been called by a non-ELF symbol reader. | |
7330 | This flag is then reset by the code which reads an ELF input | |
7331 | file. This ensures that a symbol created by a non-ELF symbol | |
7332 | reader will have the flag set correctly. */ | |
7333 | ret->non_elf = 1; | |
7334 | } | |
7335 | ||
7336 | return entry; | |
7337 | } | |
7338 | ||
7339 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
7340 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
7341 | ||
7342 | void | |
7343 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
7344 | struct elf_link_hash_entry *dir, | |
7345 | struct elf_link_hash_entry *ind) | |
7346 | { | |
7347 | struct elf_link_hash_table *htab; | |
7348 | ||
7349 | /* Copy down any references that we may have already seen to the | |
e81830c5 | 7350 | symbol which just became indirect. */ |
4d269e42 | 7351 | |
422f1182 | 7352 | if (dir->versioned != versioned_hidden) |
e81830c5 AM |
7353 | dir->ref_dynamic |= ind->ref_dynamic; |
7354 | dir->ref_regular |= ind->ref_regular; | |
7355 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
7356 | dir->non_got_ref |= ind->non_got_ref; | |
7357 | dir->needs_plt |= ind->needs_plt; | |
7358 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
4d269e42 AM |
7359 | |
7360 | if (ind->root.type != bfd_link_hash_indirect) | |
7361 | return; | |
7362 | ||
7363 | /* Copy over the global and procedure linkage table refcount entries. | |
7364 | These may have been already set up by a check_relocs routine. */ | |
7365 | htab = elf_hash_table (info); | |
7366 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
7367 | { | |
7368 | if (dir->got.refcount < 0) | |
7369 | dir->got.refcount = 0; | |
7370 | dir->got.refcount += ind->got.refcount; | |
7371 | ind->got.refcount = htab->init_got_refcount.refcount; | |
7372 | } | |
7373 | ||
7374 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
7375 | { | |
7376 | if (dir->plt.refcount < 0) | |
7377 | dir->plt.refcount = 0; | |
7378 | dir->plt.refcount += ind->plt.refcount; | |
7379 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
7380 | } | |
7381 | ||
7382 | if (ind->dynindx != -1) | |
7383 | { | |
7384 | if (dir->dynindx != -1) | |
7385 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
7386 | dir->dynindx = ind->dynindx; | |
7387 | dir->dynstr_index = ind->dynstr_index; | |
7388 | ind->dynindx = -1; | |
7389 | ind->dynstr_index = 0; | |
7390 | } | |
7391 | } | |
7392 | ||
7393 | void | |
7394 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
7395 | struct elf_link_hash_entry *h, | |
7396 | bfd_boolean force_local) | |
7397 | { | |
3aa14d16 L |
7398 | /* STT_GNU_IFUNC symbol must go through PLT. */ |
7399 | if (h->type != STT_GNU_IFUNC) | |
7400 | { | |
7401 | h->plt = elf_hash_table (info)->init_plt_offset; | |
7402 | h->needs_plt = 0; | |
7403 | } | |
4d269e42 AM |
7404 | if (force_local) |
7405 | { | |
7406 | h->forced_local = 1; | |
7407 | if (h->dynindx != -1) | |
7408 | { | |
4d269e42 AM |
7409 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, |
7410 | h->dynstr_index); | |
641338d8 AM |
7411 | h->dynindx = -1; |
7412 | h->dynstr_index = 0; | |
4d269e42 AM |
7413 | } |
7414 | } | |
7415 | } | |
7416 | ||
7bf52ea2 AM |
7417 | /* Initialize an ELF linker hash table. *TABLE has been zeroed by our |
7418 | caller. */ | |
4d269e42 AM |
7419 | |
7420 | bfd_boolean | |
7421 | _bfd_elf_link_hash_table_init | |
7422 | (struct elf_link_hash_table *table, | |
7423 | bfd *abfd, | |
7424 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
7425 | struct bfd_hash_table *, | |
7426 | const char *), | |
4dfe6ac6 NC |
7427 | unsigned int entsize, |
7428 | enum elf_target_id target_id) | |
4d269e42 AM |
7429 | { |
7430 | bfd_boolean ret; | |
7431 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; | |
7432 | ||
4d269e42 AM |
7433 | table->init_got_refcount.refcount = can_refcount - 1; |
7434 | table->init_plt_refcount.refcount = can_refcount - 1; | |
7435 | table->init_got_offset.offset = -(bfd_vma) 1; | |
7436 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
7437 | /* The first dynamic symbol is a dummy. */ | |
7438 | table->dynsymcount = 1; | |
7439 | ||
7440 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
4dfe6ac6 | 7441 | |
4d269e42 | 7442 | table->root.type = bfd_link_elf_hash_table; |
4dfe6ac6 | 7443 | table->hash_table_id = target_id; |
4d269e42 AM |
7444 | |
7445 | return ret; | |
7446 | } | |
7447 | ||
7448 | /* Create an ELF linker hash table. */ | |
7449 | ||
7450 | struct bfd_link_hash_table * | |
7451 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
7452 | { | |
7453 | struct elf_link_hash_table *ret; | |
7454 | bfd_size_type amt = sizeof (struct elf_link_hash_table); | |
7455 | ||
7bf52ea2 | 7456 | ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); |
4d269e42 AM |
7457 | if (ret == NULL) |
7458 | return NULL; | |
7459 | ||
7460 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
4dfe6ac6 NC |
7461 | sizeof (struct elf_link_hash_entry), |
7462 | GENERIC_ELF_DATA)) | |
4d269e42 AM |
7463 | { |
7464 | free (ret); | |
7465 | return NULL; | |
7466 | } | |
d495ab0d | 7467 | ret->root.hash_table_free = _bfd_elf_link_hash_table_free; |
4d269e42 AM |
7468 | |
7469 | return &ret->root; | |
7470 | } | |
7471 | ||
9f7c3e5e AM |
7472 | /* Destroy an ELF linker hash table. */ |
7473 | ||
7474 | void | |
d495ab0d | 7475 | _bfd_elf_link_hash_table_free (bfd *obfd) |
9f7c3e5e | 7476 | { |
d495ab0d AM |
7477 | struct elf_link_hash_table *htab; |
7478 | ||
7479 | htab = (struct elf_link_hash_table *) obfd->link.hash; | |
9f7c3e5e AM |
7480 | if (htab->dynstr != NULL) |
7481 | _bfd_elf_strtab_free (htab->dynstr); | |
7482 | _bfd_merge_sections_free (htab->merge_info); | |
d495ab0d | 7483 | _bfd_generic_link_hash_table_free (obfd); |
9f7c3e5e AM |
7484 | } |
7485 | ||
4d269e42 AM |
7486 | /* This is a hook for the ELF emulation code in the generic linker to |
7487 | tell the backend linker what file name to use for the DT_NEEDED | |
7488 | entry for a dynamic object. */ | |
7489 | ||
7490 | void | |
7491 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
7492 | { | |
7493 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7494 | && bfd_get_format (abfd) == bfd_object) | |
7495 | elf_dt_name (abfd) = name; | |
7496 | } | |
7497 | ||
7498 | int | |
7499 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
7500 | { | |
7501 | int lib_class; | |
7502 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7503 | && bfd_get_format (abfd) == bfd_object) | |
7504 | lib_class = elf_dyn_lib_class (abfd); | |
7505 | else | |
7506 | lib_class = 0; | |
7507 | return lib_class; | |
7508 | } | |
7509 | ||
7510 | void | |
7511 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
7512 | { | |
7513 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7514 | && bfd_get_format (abfd) == bfd_object) | |
7515 | elf_dyn_lib_class (abfd) = lib_class; | |
7516 | } | |
7517 | ||
7518 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
7519 | the linker ELF emulation code. */ | |
7520 | ||
7521 | struct bfd_link_needed_list * | |
7522 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7523 | struct bfd_link_info *info) | |
7524 | { | |
7525 | if (! is_elf_hash_table (info->hash)) | |
7526 | return NULL; | |
7527 | return elf_hash_table (info)->needed; | |
7528 | } | |
7529 | ||
7530 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
7531 | hook for the linker ELF emulation code. */ | |
7532 | ||
7533 | struct bfd_link_needed_list * | |
7534 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7535 | struct bfd_link_info *info) | |
7536 | { | |
7537 | if (! is_elf_hash_table (info->hash)) | |
7538 | return NULL; | |
7539 | return elf_hash_table (info)->runpath; | |
7540 | } | |
7541 | ||
7542 | /* Get the name actually used for a dynamic object for a link. This | |
7543 | is the SONAME entry if there is one. Otherwise, it is the string | |
7544 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
7545 | ||
7546 | const char * | |
7547 | bfd_elf_get_dt_soname (bfd *abfd) | |
7548 | { | |
7549 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7550 | && bfd_get_format (abfd) == bfd_object) | |
7551 | return elf_dt_name (abfd); | |
7552 | return NULL; | |
7553 | } | |
7554 | ||
7555 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
7556 | the ELF linker emulation code. */ | |
7557 | ||
7558 | bfd_boolean | |
7559 | bfd_elf_get_bfd_needed_list (bfd *abfd, | |
7560 | struct bfd_link_needed_list **pneeded) | |
7561 | { | |
7562 | asection *s; | |
7563 | bfd_byte *dynbuf = NULL; | |
cb33740c | 7564 | unsigned int elfsec; |
4d269e42 AM |
7565 | unsigned long shlink; |
7566 | bfd_byte *extdyn, *extdynend; | |
7567 | size_t extdynsize; | |
7568 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
7569 | ||
7570 | *pneeded = NULL; | |
7571 | ||
7572 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
7573 | || bfd_get_format (abfd) != bfd_object) | |
7574 | return TRUE; | |
7575 | ||
7576 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
7577 | if (s == NULL || s->size == 0) | |
7578 | return TRUE; | |
7579 | ||
7580 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
7581 | goto error_return; | |
7582 | ||
7583 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 7584 | if (elfsec == SHN_BAD) |
4d269e42 AM |
7585 | goto error_return; |
7586 | ||
7587 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 7588 | |
4d269e42 AM |
7589 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
7590 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
7591 | ||
7592 | extdyn = dynbuf; | |
7593 | extdynend = extdyn + s->size; | |
7594 | for (; extdyn < extdynend; extdyn += extdynsize) | |
7595 | { | |
7596 | Elf_Internal_Dyn dyn; | |
7597 | ||
7598 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
7599 | ||
7600 | if (dyn.d_tag == DT_NULL) | |
7601 | break; | |
7602 | ||
7603 | if (dyn.d_tag == DT_NEEDED) | |
7604 | { | |
7605 | const char *string; | |
7606 | struct bfd_link_needed_list *l; | |
7607 | unsigned int tagv = dyn.d_un.d_val; | |
7608 | bfd_size_type amt; | |
7609 | ||
7610 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
7611 | if (string == NULL) | |
7612 | goto error_return; | |
7613 | ||
7614 | amt = sizeof *l; | |
a50b1753 | 7615 | l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4d269e42 AM |
7616 | if (l == NULL) |
7617 | goto error_return; | |
7618 | ||
7619 | l->by = abfd; | |
7620 | l->name = string; | |
7621 | l->next = *pneeded; | |
7622 | *pneeded = l; | |
7623 | } | |
7624 | } | |
7625 | ||
7626 | free (dynbuf); | |
7627 | ||
7628 | return TRUE; | |
7629 | ||
7630 | error_return: | |
7631 | if (dynbuf != NULL) | |
7632 | free (dynbuf); | |
7633 | return FALSE; | |
7634 | } | |
7635 | ||
7636 | struct elf_symbuf_symbol | |
7637 | { | |
7638 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
7639 | unsigned char st_info; /* Type and binding attributes */ | |
7640 | unsigned char st_other; /* Visibilty, and target specific */ | |
7641 | }; | |
7642 | ||
7643 | struct elf_symbuf_head | |
7644 | { | |
7645 | struct elf_symbuf_symbol *ssym; | |
ef53be89 | 7646 | size_t count; |
4d269e42 AM |
7647 | unsigned int st_shndx; |
7648 | }; | |
7649 | ||
7650 | struct elf_symbol | |
7651 | { | |
7652 | union | |
7653 | { | |
7654 | Elf_Internal_Sym *isym; | |
7655 | struct elf_symbuf_symbol *ssym; | |
7656 | } u; | |
7657 | const char *name; | |
7658 | }; | |
7659 | ||
7660 | /* Sort references to symbols by ascending section number. */ | |
7661 | ||
7662 | static int | |
7663 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
7664 | { | |
7665 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
7666 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
7667 | ||
7668 | return s1->st_shndx - s2->st_shndx; | |
7669 | } | |
7670 | ||
7671 | static int | |
7672 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
7673 | { | |
7674 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
7675 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
7676 | return strcmp (s1->name, s2->name); | |
7677 | } | |
7678 | ||
7679 | static struct elf_symbuf_head * | |
ef53be89 | 7680 | elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) |
4d269e42 | 7681 | { |
14b1c01e | 7682 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
7683 | struct elf_symbuf_symbol *ssym; |
7684 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
ef53be89 | 7685 | size_t i, shndx_count, total_size; |
4d269e42 | 7686 | |
a50b1753 | 7687 | indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); |
4d269e42 AM |
7688 | if (indbuf == NULL) |
7689 | return NULL; | |
7690 | ||
7691 | for (ind = indbuf, i = 0; i < symcount; i++) | |
7692 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
7693 | *ind++ = &isymbuf[i]; | |
7694 | indbufend = ind; | |
7695 | ||
7696 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
7697 | elf_sort_elf_symbol); | |
7698 | ||
7699 | shndx_count = 0; | |
7700 | if (indbufend > indbuf) | |
7701 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
7702 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
7703 | shndx_count++; | |
7704 | ||
3ae181ee L |
7705 | total_size = ((shndx_count + 1) * sizeof (*ssymbuf) |
7706 | + (indbufend - indbuf) * sizeof (*ssym)); | |
a50b1753 | 7707 | ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); |
4d269e42 AM |
7708 | if (ssymbuf == NULL) |
7709 | { | |
7710 | free (indbuf); | |
7711 | return NULL; | |
7712 | } | |
7713 | ||
3ae181ee | 7714 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); |
4d269e42 AM |
7715 | ssymbuf->ssym = NULL; |
7716 | ssymbuf->count = shndx_count; | |
7717 | ssymbuf->st_shndx = 0; | |
7718 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
7719 | { | |
7720 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
7721 | { | |
7722 | ssymhead++; | |
7723 | ssymhead->ssym = ssym; | |
7724 | ssymhead->count = 0; | |
7725 | ssymhead->st_shndx = (*ind)->st_shndx; | |
7726 | } | |
7727 | ssym->st_name = (*ind)->st_name; | |
7728 | ssym->st_info = (*ind)->st_info; | |
7729 | ssym->st_other = (*ind)->st_other; | |
7730 | ssymhead->count++; | |
7731 | } | |
ef53be89 | 7732 | BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count |
3ae181ee L |
7733 | && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) |
7734 | == total_size)); | |
4d269e42 AM |
7735 | |
7736 | free (indbuf); | |
7737 | return ssymbuf; | |
7738 | } | |
7739 | ||
7740 | /* Check if 2 sections define the same set of local and global | |
7741 | symbols. */ | |
7742 | ||
8f317e31 | 7743 | static bfd_boolean |
4d269e42 AM |
7744 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
7745 | struct bfd_link_info *info) | |
7746 | { | |
7747 | bfd *bfd1, *bfd2; | |
7748 | const struct elf_backend_data *bed1, *bed2; | |
7749 | Elf_Internal_Shdr *hdr1, *hdr2; | |
ef53be89 | 7750 | size_t symcount1, symcount2; |
4d269e42 AM |
7751 | Elf_Internal_Sym *isymbuf1, *isymbuf2; |
7752 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
7753 | Elf_Internal_Sym *isym, *isymend; | |
7754 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
ef53be89 | 7755 | size_t count1, count2, i; |
cb33740c | 7756 | unsigned int shndx1, shndx2; |
4d269e42 AM |
7757 | bfd_boolean result; |
7758 | ||
7759 | bfd1 = sec1->owner; | |
7760 | bfd2 = sec2->owner; | |
7761 | ||
4d269e42 AM |
7762 | /* Both sections have to be in ELF. */ |
7763 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
7764 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
7765 | return FALSE; | |
7766 | ||
7767 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
7768 | return FALSE; | |
7769 | ||
4d269e42 AM |
7770 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
7771 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
cb33740c | 7772 | if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) |
4d269e42 AM |
7773 | return FALSE; |
7774 | ||
7775 | bed1 = get_elf_backend_data (bfd1); | |
7776 | bed2 = get_elf_backend_data (bfd2); | |
7777 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
7778 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
7779 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
7780 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
7781 | ||
7782 | if (symcount1 == 0 || symcount2 == 0) | |
7783 | return FALSE; | |
7784 | ||
7785 | result = FALSE; | |
7786 | isymbuf1 = NULL; | |
7787 | isymbuf2 = NULL; | |
a50b1753 NC |
7788 | ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; |
7789 | ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; | |
4d269e42 AM |
7790 | |
7791 | if (ssymbuf1 == NULL) | |
7792 | { | |
7793 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
7794 | NULL, NULL, NULL); | |
7795 | if (isymbuf1 == NULL) | |
7796 | goto done; | |
7797 | ||
7798 | if (!info->reduce_memory_overheads) | |
7799 | elf_tdata (bfd1)->symbuf = ssymbuf1 | |
7800 | = elf_create_symbuf (symcount1, isymbuf1); | |
7801 | } | |
7802 | ||
7803 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
7804 | { | |
7805 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
7806 | NULL, NULL, NULL); | |
7807 | if (isymbuf2 == NULL) | |
7808 | goto done; | |
7809 | ||
7810 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
7811 | elf_tdata (bfd2)->symbuf = ssymbuf2 | |
7812 | = elf_create_symbuf (symcount2, isymbuf2); | |
7813 | } | |
7814 | ||
7815 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
7816 | { | |
7817 | /* Optimized faster version. */ | |
ef53be89 | 7818 | size_t lo, hi, mid; |
4d269e42 AM |
7819 | struct elf_symbol *symp; |
7820 | struct elf_symbuf_symbol *ssym, *ssymend; | |
7821 | ||
7822 | lo = 0; | |
7823 | hi = ssymbuf1->count; | |
7824 | ssymbuf1++; | |
7825 | count1 = 0; | |
7826 | while (lo < hi) | |
7827 | { | |
7828 | mid = (lo + hi) / 2; | |
cb33740c | 7829 | if (shndx1 < ssymbuf1[mid].st_shndx) |
4d269e42 | 7830 | hi = mid; |
cb33740c | 7831 | else if (shndx1 > ssymbuf1[mid].st_shndx) |
4d269e42 AM |
7832 | lo = mid + 1; |
7833 | else | |
7834 | { | |
7835 | count1 = ssymbuf1[mid].count; | |
7836 | ssymbuf1 += mid; | |
7837 | break; | |
7838 | } | |
7839 | } | |
7840 | ||
7841 | lo = 0; | |
7842 | hi = ssymbuf2->count; | |
7843 | ssymbuf2++; | |
7844 | count2 = 0; | |
7845 | while (lo < hi) | |
7846 | { | |
7847 | mid = (lo + hi) / 2; | |
cb33740c | 7848 | if (shndx2 < ssymbuf2[mid].st_shndx) |
4d269e42 | 7849 | hi = mid; |
cb33740c | 7850 | else if (shndx2 > ssymbuf2[mid].st_shndx) |
4d269e42 AM |
7851 | lo = mid + 1; |
7852 | else | |
7853 | { | |
7854 | count2 = ssymbuf2[mid].count; | |
7855 | ssymbuf2 += mid; | |
7856 | break; | |
7857 | } | |
7858 | } | |
7859 | ||
7860 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7861 | goto done; | |
7862 | ||
ca4be51c AM |
7863 | symtable1 |
7864 | = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); | |
7865 | symtable2 | |
7866 | = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); | |
4d269e42 AM |
7867 | if (symtable1 == NULL || symtable2 == NULL) |
7868 | goto done; | |
7869 | ||
7870 | symp = symtable1; | |
7871 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
7872 | ssym < ssymend; ssym++, symp++) | |
7873 | { | |
7874 | symp->u.ssym = ssym; | |
7875 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
7876 | hdr1->sh_link, | |
7877 | ssym->st_name); | |
7878 | } | |
7879 | ||
7880 | symp = symtable2; | |
7881 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
7882 | ssym < ssymend; ssym++, symp++) | |
7883 | { | |
7884 | symp->u.ssym = ssym; | |
7885 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
7886 | hdr2->sh_link, | |
7887 | ssym->st_name); | |
7888 | } | |
7889 | ||
7890 | /* Sort symbol by name. */ | |
7891 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7892 | elf_sym_name_compare); | |
7893 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7894 | elf_sym_name_compare); | |
7895 | ||
7896 | for (i = 0; i < count1; i++) | |
7897 | /* Two symbols must have the same binding, type and name. */ | |
7898 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
7899 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
7900 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7901 | goto done; | |
7902 | ||
7903 | result = TRUE; | |
7904 | goto done; | |
7905 | } | |
7906 | ||
a50b1753 NC |
7907 | symtable1 = (struct elf_symbol *) |
7908 | bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
7909 | symtable2 = (struct elf_symbol *) | |
7910 | bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
4d269e42 AM |
7911 | if (symtable1 == NULL || symtable2 == NULL) |
7912 | goto done; | |
7913 | ||
7914 | /* Count definitions in the section. */ | |
7915 | count1 = 0; | |
7916 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
cb33740c | 7917 | if (isym->st_shndx == shndx1) |
4d269e42 AM |
7918 | symtable1[count1++].u.isym = isym; |
7919 | ||
7920 | count2 = 0; | |
7921 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
cb33740c | 7922 | if (isym->st_shndx == shndx2) |
4d269e42 AM |
7923 | symtable2[count2++].u.isym = isym; |
7924 | ||
7925 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7926 | goto done; | |
7927 | ||
7928 | for (i = 0; i < count1; i++) | |
7929 | symtable1[i].name | |
7930 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
7931 | symtable1[i].u.isym->st_name); | |
7932 | ||
7933 | for (i = 0; i < count2; i++) | |
7934 | symtable2[i].name | |
7935 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
7936 | symtable2[i].u.isym->st_name); | |
7937 | ||
7938 | /* Sort symbol by name. */ | |
7939 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7940 | elf_sym_name_compare); | |
7941 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7942 | elf_sym_name_compare); | |
7943 | ||
7944 | for (i = 0; i < count1; i++) | |
7945 | /* Two symbols must have the same binding, type and name. */ | |
7946 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
7947 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
7948 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7949 | goto done; | |
7950 | ||
7951 | result = TRUE; | |
7952 | ||
7953 | done: | |
7954 | if (symtable1) | |
7955 | free (symtable1); | |
7956 | if (symtable2) | |
7957 | free (symtable2); | |
7958 | if (isymbuf1) | |
7959 | free (isymbuf1); | |
7960 | if (isymbuf2) | |
7961 | free (isymbuf2); | |
7962 | ||
7963 | return result; | |
7964 | } | |
7965 | ||
7966 | /* Return TRUE if 2 section types are compatible. */ | |
7967 | ||
7968 | bfd_boolean | |
7969 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
7970 | bfd *bbfd, const asection *bsec) | |
7971 | { | |
7972 | if (asec == NULL | |
7973 | || bsec == NULL | |
7974 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
7975 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
7976 | return TRUE; | |
7977 | ||
7978 | return elf_section_type (asec) == elf_section_type (bsec); | |
7979 | } | |
7980 | \f | |
c152c796 AM |
7981 | /* Final phase of ELF linker. */ |
7982 | ||
7983 | /* A structure we use to avoid passing large numbers of arguments. */ | |
7984 | ||
7985 | struct elf_final_link_info | |
7986 | { | |
7987 | /* General link information. */ | |
7988 | struct bfd_link_info *info; | |
7989 | /* Output BFD. */ | |
7990 | bfd *output_bfd; | |
7991 | /* Symbol string table. */ | |
ef10c3ac | 7992 | struct elf_strtab_hash *symstrtab; |
c152c796 AM |
7993 | /* .hash section. */ |
7994 | asection *hash_sec; | |
7995 | /* symbol version section (.gnu.version). */ | |
7996 | asection *symver_sec; | |
7997 | /* Buffer large enough to hold contents of any section. */ | |
7998 | bfd_byte *contents; | |
7999 | /* Buffer large enough to hold external relocs of any section. */ | |
8000 | void *external_relocs; | |
8001 | /* Buffer large enough to hold internal relocs of any section. */ | |
8002 | Elf_Internal_Rela *internal_relocs; | |
8003 | /* Buffer large enough to hold external local symbols of any input | |
8004 | BFD. */ | |
8005 | bfd_byte *external_syms; | |
8006 | /* And a buffer for symbol section indices. */ | |
8007 | Elf_External_Sym_Shndx *locsym_shndx; | |
8008 | /* Buffer large enough to hold internal local symbols of any input | |
8009 | BFD. */ | |
8010 | Elf_Internal_Sym *internal_syms; | |
8011 | /* Array large enough to hold a symbol index for each local symbol | |
8012 | of any input BFD. */ | |
8013 | long *indices; | |
8014 | /* Array large enough to hold a section pointer for each local | |
8015 | symbol of any input BFD. */ | |
8016 | asection **sections; | |
ef10c3ac | 8017 | /* Buffer for SHT_SYMTAB_SHNDX section. */ |
c152c796 | 8018 | Elf_External_Sym_Shndx *symshndxbuf; |
ffbc01cc AM |
8019 | /* Number of STT_FILE syms seen. */ |
8020 | size_t filesym_count; | |
c152c796 AM |
8021 | }; |
8022 | ||
8023 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
8024 | ||
8025 | struct elf_outext_info | |
8026 | { | |
8027 | bfd_boolean failed; | |
8028 | bfd_boolean localsyms; | |
34a79995 | 8029 | bfd_boolean file_sym_done; |
8b127cbc | 8030 | struct elf_final_link_info *flinfo; |
c152c796 AM |
8031 | }; |
8032 | ||
d9352518 DB |
8033 | |
8034 | /* Support for evaluating a complex relocation. | |
8035 | ||
8036 | Complex relocations are generalized, self-describing relocations. The | |
8037 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 8038 | relocations themselves. |
d9352518 DB |
8039 | |
8040 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
8041 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
8042 | information (start bit, end bit, word width, etc) into the addend. This | |
8043 | information is extracted from CGEN-generated operand tables within gas. | |
8044 | ||
8045 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
8046 | internal) representing prefix-notation expressions, including but not | |
8047 | limited to those sorts of expressions normally encoded as addends in the | |
8048 | addend field. The symbol mangling format is: | |
8049 | ||
8050 | <node> := <literal> | |
07d6d2b8 AM |
8051 | | <unary-operator> ':' <node> |
8052 | | <binary-operator> ':' <node> ':' <node> | |
d9352518 DB |
8053 | ; |
8054 | ||
8055 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
07d6d2b8 | 8056 | | 'S' <digits=N> ':' <N character section name> |
d9352518 DB |
8057 | | '#' <hexdigits> |
8058 | ; | |
8059 | ||
8060 | <binary-operator> := as in C | |
8061 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
8062 | ||
8063 | static void | |
a0c8462f AM |
8064 | set_symbol_value (bfd *bfd_with_globals, |
8065 | Elf_Internal_Sym *isymbuf, | |
8066 | size_t locsymcount, | |
8067 | size_t symidx, | |
8068 | bfd_vma val) | |
d9352518 | 8069 | { |
8977835c AM |
8070 | struct elf_link_hash_entry **sym_hashes; |
8071 | struct elf_link_hash_entry *h; | |
8072 | size_t extsymoff = locsymcount; | |
d9352518 | 8073 | |
8977835c | 8074 | if (symidx < locsymcount) |
d9352518 | 8075 | { |
8977835c AM |
8076 | Elf_Internal_Sym *sym; |
8077 | ||
8078 | sym = isymbuf + symidx; | |
8079 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
8080 | { | |
8081 | /* It is a local symbol: move it to the | |
8082 | "absolute" section and give it a value. */ | |
8083 | sym->st_shndx = SHN_ABS; | |
8084 | sym->st_value = val; | |
8085 | return; | |
8086 | } | |
8087 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
8088 | extsymoff = 0; | |
d9352518 | 8089 | } |
8977835c AM |
8090 | |
8091 | /* It is a global symbol: set its link type | |
8092 | to "defined" and give it a value. */ | |
8093 | ||
8094 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
8095 | h = sym_hashes [symidx - extsymoff]; | |
8096 | while (h->root.type == bfd_link_hash_indirect | |
8097 | || h->root.type == bfd_link_hash_warning) | |
8098 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8099 | h->root.type = bfd_link_hash_defined; | |
8100 | h->root.u.def.value = val; | |
8101 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
8102 | } |
8103 | ||
a0c8462f AM |
8104 | static bfd_boolean |
8105 | resolve_symbol (const char *name, | |
8106 | bfd *input_bfd, | |
8b127cbc | 8107 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8108 | bfd_vma *result, |
8109 | Elf_Internal_Sym *isymbuf, | |
8110 | size_t locsymcount) | |
d9352518 | 8111 | { |
a0c8462f AM |
8112 | Elf_Internal_Sym *sym; |
8113 | struct bfd_link_hash_entry *global_entry; | |
8114 | const char *candidate = NULL; | |
8115 | Elf_Internal_Shdr *symtab_hdr; | |
8116 | size_t i; | |
8117 | ||
d9352518 DB |
8118 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
8119 | ||
8120 | for (i = 0; i < locsymcount; ++ i) | |
8121 | { | |
8977835c | 8122 | sym = isymbuf + i; |
d9352518 DB |
8123 | |
8124 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
8125 | continue; | |
8126 | ||
8127 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
8128 | symtab_hdr->sh_link, | |
8129 | sym->st_name); | |
8130 | #ifdef DEBUG | |
0f02bbd9 AM |
8131 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
8132 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
8133 | #endif |
8134 | if (candidate && strcmp (candidate, name) == 0) | |
8135 | { | |
8b127cbc | 8136 | asection *sec = flinfo->sections [i]; |
d9352518 | 8137 | |
0f02bbd9 AM |
8138 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
8139 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 8140 | #ifdef DEBUG |
0f02bbd9 AM |
8141 | printf ("Found symbol with value %8.8lx\n", |
8142 | (unsigned long) *result); | |
d9352518 DB |
8143 | #endif |
8144 | return TRUE; | |
8145 | } | |
8146 | } | |
8147 | ||
8148 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
8b127cbc | 8149 | global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, |
a0c8462f | 8150 | FALSE, FALSE, TRUE); |
d9352518 DB |
8151 | if (!global_entry) |
8152 | return FALSE; | |
a0c8462f | 8153 | |
d9352518 DB |
8154 | if (global_entry->type == bfd_link_hash_defined |
8155 | || global_entry->type == bfd_link_hash_defweak) | |
8156 | { | |
a0c8462f AM |
8157 | *result = (global_entry->u.def.value |
8158 | + global_entry->u.def.section->output_section->vma | |
8159 | + global_entry->u.def.section->output_offset); | |
d9352518 | 8160 | #ifdef DEBUG |
0f02bbd9 AM |
8161 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
8162 | global_entry->root.string, (unsigned long) *result); | |
d9352518 DB |
8163 | #endif |
8164 | return TRUE; | |
a0c8462f | 8165 | } |
d9352518 | 8166 | |
d9352518 DB |
8167 | return FALSE; |
8168 | } | |
8169 | ||
37b01f6a DG |
8170 | /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in |
8171 | bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section | |
8172 | names like "foo.end" which is the end address of section "foo". */ | |
07d6d2b8 | 8173 | |
d9352518 | 8174 | static bfd_boolean |
a0c8462f AM |
8175 | resolve_section (const char *name, |
8176 | asection *sections, | |
37b01f6a DG |
8177 | bfd_vma *result, |
8178 | bfd * abfd) | |
d9352518 | 8179 | { |
a0c8462f AM |
8180 | asection *curr; |
8181 | unsigned int len; | |
d9352518 | 8182 | |
a0c8462f | 8183 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8184 | if (strcmp (curr->name, name) == 0) |
8185 | { | |
8186 | *result = curr->vma; | |
8187 | return TRUE; | |
8188 | } | |
8189 | ||
8190 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
37b01f6a | 8191 | /* FIXME: This could be coded more efficiently... */ |
a0c8462f | 8192 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8193 | { |
8194 | len = strlen (curr->name); | |
a0c8462f | 8195 | if (len > strlen (name)) |
d9352518 DB |
8196 | continue; |
8197 | ||
8198 | if (strncmp (curr->name, name, len) == 0) | |
8199 | { | |
8200 | if (strncmp (".end", name + len, 4) == 0) | |
8201 | { | |
37b01f6a | 8202 | *result = curr->vma + curr->size / bfd_octets_per_byte (abfd); |
d9352518 DB |
8203 | return TRUE; |
8204 | } | |
8205 | ||
8206 | /* Insert more pseudo-section names here, if you like. */ | |
8207 | } | |
8208 | } | |
a0c8462f | 8209 | |
d9352518 DB |
8210 | return FALSE; |
8211 | } | |
8212 | ||
8213 | static void | |
a0c8462f | 8214 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 8215 | { |
695344c0 | 8216 | /* xgettext:c-format */ |
a0c8462f AM |
8217 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
8218 | reftype, name); | |
d9352518 DB |
8219 | } |
8220 | ||
8221 | static bfd_boolean | |
a0c8462f AM |
8222 | eval_symbol (bfd_vma *result, |
8223 | const char **symp, | |
8224 | bfd *input_bfd, | |
8b127cbc | 8225 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8226 | bfd_vma dot, |
8227 | Elf_Internal_Sym *isymbuf, | |
8228 | size_t locsymcount, | |
8229 | int signed_p) | |
d9352518 | 8230 | { |
4b93929b NC |
8231 | size_t len; |
8232 | size_t symlen; | |
a0c8462f AM |
8233 | bfd_vma a; |
8234 | bfd_vma b; | |
4b93929b | 8235 | char symbuf[4096]; |
0f02bbd9 | 8236 | const char *sym = *symp; |
a0c8462f AM |
8237 | const char *symend; |
8238 | bfd_boolean symbol_is_section = FALSE; | |
d9352518 DB |
8239 | |
8240 | len = strlen (sym); | |
8241 | symend = sym + len; | |
8242 | ||
4b93929b | 8243 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
8244 | { |
8245 | bfd_set_error (bfd_error_invalid_operation); | |
8246 | return FALSE; | |
8247 | } | |
a0c8462f | 8248 | |
d9352518 DB |
8249 | switch (* sym) |
8250 | { | |
8251 | case '.': | |
0f02bbd9 AM |
8252 | *result = dot; |
8253 | *symp = sym + 1; | |
d9352518 DB |
8254 | return TRUE; |
8255 | ||
8256 | case '#': | |
0f02bbd9 AM |
8257 | ++sym; |
8258 | *result = strtoul (sym, (char **) symp, 16); | |
d9352518 DB |
8259 | return TRUE; |
8260 | ||
8261 | case 'S': | |
8262 | symbol_is_section = TRUE; | |
1a0670f3 | 8263 | /* Fall through. */ |
a0c8462f | 8264 | case 's': |
0f02bbd9 AM |
8265 | ++sym; |
8266 | symlen = strtol (sym, (char **) symp, 10); | |
8267 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 8268 | |
4b93929b | 8269 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
8270 | { |
8271 | bfd_set_error (bfd_error_invalid_operation); | |
8272 | return FALSE; | |
8273 | } | |
8274 | ||
8275 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 8276 | symbuf[symlen] = '\0'; |
0f02bbd9 | 8277 | *symp = sym + symlen; |
a0c8462f AM |
8278 | |
8279 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
8280 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
8281 | interpretation here; section means "try section first", not "must be a | |
8282 | section", and likewise with symbol. */ | |
8283 | ||
a0c8462f | 8284 | if (symbol_is_section) |
d9352518 | 8285 | { |
37b01f6a | 8286 | if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) |
8b127cbc | 8287 | && !resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8288 | isymbuf, locsymcount)) |
d9352518 DB |
8289 | { |
8290 | undefined_reference ("section", symbuf); | |
8291 | return FALSE; | |
8292 | } | |
a0c8462f AM |
8293 | } |
8294 | else | |
d9352518 | 8295 | { |
8b127cbc | 8296 | if (!resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8297 | isymbuf, locsymcount) |
8b127cbc | 8298 | && !resolve_section (symbuf, flinfo->output_bfd->sections, |
37b01f6a | 8299 | result, input_bfd)) |
d9352518 DB |
8300 | { |
8301 | undefined_reference ("symbol", symbuf); | |
8302 | return FALSE; | |
8303 | } | |
8304 | } | |
8305 | ||
8306 | return TRUE; | |
a0c8462f | 8307 | |
d9352518 DB |
8308 | /* All that remains are operators. */ |
8309 | ||
8310 | #define UNARY_OP(op) \ | |
8311 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8312 | { \ | |
8313 | sym += strlen (#op); \ | |
a0c8462f AM |
8314 | if (*sym == ':') \ |
8315 | ++sym; \ | |
0f02bbd9 | 8316 | *symp = sym; \ |
8b127cbc | 8317 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8318 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8319 | return FALSE; \ |
8320 | if (signed_p) \ | |
0f02bbd9 | 8321 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
8322 | else \ |
8323 | *result = op a; \ | |
d9352518 DB |
8324 | return TRUE; \ |
8325 | } | |
8326 | ||
8327 | #define BINARY_OP(op) \ | |
8328 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8329 | { \ | |
8330 | sym += strlen (#op); \ | |
a0c8462f AM |
8331 | if (*sym == ':') \ |
8332 | ++sym; \ | |
0f02bbd9 | 8333 | *symp = sym; \ |
8b127cbc | 8334 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8335 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f | 8336 | return FALSE; \ |
0f02bbd9 | 8337 | ++*symp; \ |
8b127cbc | 8338 | if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8339 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8340 | return FALSE; \ |
8341 | if (signed_p) \ | |
0f02bbd9 | 8342 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
8343 | else \ |
8344 | *result = a op b; \ | |
d9352518 DB |
8345 | return TRUE; \ |
8346 | } | |
8347 | ||
8348 | default: | |
8349 | UNARY_OP (0-); | |
8350 | BINARY_OP (<<); | |
8351 | BINARY_OP (>>); | |
8352 | BINARY_OP (==); | |
8353 | BINARY_OP (!=); | |
8354 | BINARY_OP (<=); | |
8355 | BINARY_OP (>=); | |
8356 | BINARY_OP (&&); | |
8357 | BINARY_OP (||); | |
8358 | UNARY_OP (~); | |
8359 | UNARY_OP (!); | |
8360 | BINARY_OP (*); | |
8361 | BINARY_OP (/); | |
8362 | BINARY_OP (%); | |
8363 | BINARY_OP (^); | |
8364 | BINARY_OP (|); | |
8365 | BINARY_OP (&); | |
8366 | BINARY_OP (+); | |
8367 | BINARY_OP (-); | |
8368 | BINARY_OP (<); | |
8369 | BINARY_OP (>); | |
8370 | #undef UNARY_OP | |
8371 | #undef BINARY_OP | |
8372 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
8373 | bfd_set_error (bfd_error_invalid_operation); | |
8374 | return FALSE; | |
8375 | } | |
8376 | } | |
8377 | ||
d9352518 | 8378 | static void |
a0c8462f AM |
8379 | put_value (bfd_vma size, |
8380 | unsigned long chunksz, | |
8381 | bfd *input_bfd, | |
8382 | bfd_vma x, | |
8383 | bfd_byte *location) | |
d9352518 DB |
8384 | { |
8385 | location += (size - chunksz); | |
8386 | ||
41cd1ad1 | 8387 | for (; size; size -= chunksz, location -= chunksz) |
d9352518 DB |
8388 | { |
8389 | switch (chunksz) | |
8390 | { | |
d9352518 DB |
8391 | case 1: |
8392 | bfd_put_8 (input_bfd, x, location); | |
41cd1ad1 | 8393 | x >>= 8; |
d9352518 DB |
8394 | break; |
8395 | case 2: | |
8396 | bfd_put_16 (input_bfd, x, location); | |
41cd1ad1 | 8397 | x >>= 16; |
d9352518 DB |
8398 | break; |
8399 | case 4: | |
8400 | bfd_put_32 (input_bfd, x, location); | |
65164438 NC |
8401 | /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ |
8402 | x >>= 16; | |
8403 | x >>= 16; | |
d9352518 | 8404 | break; |
d9352518 | 8405 | #ifdef BFD64 |
41cd1ad1 | 8406 | case 8: |
d9352518 | 8407 | bfd_put_64 (input_bfd, x, location); |
41cd1ad1 NC |
8408 | /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ |
8409 | x >>= 32; | |
8410 | x >>= 32; | |
8411 | break; | |
d9352518 | 8412 | #endif |
41cd1ad1 NC |
8413 | default: |
8414 | abort (); | |
d9352518 DB |
8415 | break; |
8416 | } | |
8417 | } | |
8418 | } | |
8419 | ||
a0c8462f AM |
8420 | static bfd_vma |
8421 | get_value (bfd_vma size, | |
8422 | unsigned long chunksz, | |
8423 | bfd *input_bfd, | |
8424 | bfd_byte *location) | |
d9352518 | 8425 | { |
9b239e0e | 8426 | int shift; |
d9352518 DB |
8427 | bfd_vma x = 0; |
8428 | ||
9b239e0e NC |
8429 | /* Sanity checks. */ |
8430 | BFD_ASSERT (chunksz <= sizeof (x) | |
8431 | && size >= chunksz | |
8432 | && chunksz != 0 | |
8433 | && (size % chunksz) == 0 | |
8434 | && input_bfd != NULL | |
8435 | && location != NULL); | |
8436 | ||
8437 | if (chunksz == sizeof (x)) | |
8438 | { | |
8439 | BFD_ASSERT (size == chunksz); | |
8440 | ||
8441 | /* Make sure that we do not perform an undefined shift operation. | |
8442 | We know that size == chunksz so there will only be one iteration | |
8443 | of the loop below. */ | |
8444 | shift = 0; | |
8445 | } | |
8446 | else | |
8447 | shift = 8 * chunksz; | |
8448 | ||
a0c8462f | 8449 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
8450 | { |
8451 | switch (chunksz) | |
8452 | { | |
d9352518 | 8453 | case 1: |
9b239e0e | 8454 | x = (x << shift) | bfd_get_8 (input_bfd, location); |
d9352518 DB |
8455 | break; |
8456 | case 2: | |
9b239e0e | 8457 | x = (x << shift) | bfd_get_16 (input_bfd, location); |
d9352518 DB |
8458 | break; |
8459 | case 4: | |
9b239e0e | 8460 | x = (x << shift) | bfd_get_32 (input_bfd, location); |
d9352518 | 8461 | break; |
d9352518 | 8462 | #ifdef BFD64 |
9b239e0e NC |
8463 | case 8: |
8464 | x = (x << shift) | bfd_get_64 (input_bfd, location); | |
d9352518 | 8465 | break; |
9b239e0e NC |
8466 | #endif |
8467 | default: | |
8468 | abort (); | |
d9352518 DB |
8469 | } |
8470 | } | |
8471 | return x; | |
8472 | } | |
8473 | ||
a0c8462f AM |
8474 | static void |
8475 | decode_complex_addend (unsigned long *start, /* in bits */ | |
8476 | unsigned long *oplen, /* in bits */ | |
8477 | unsigned long *len, /* in bits */ | |
8478 | unsigned long *wordsz, /* in bytes */ | |
8479 | unsigned long *chunksz, /* in bytes */ | |
8480 | unsigned long *lsb0_p, | |
8481 | unsigned long *signed_p, | |
8482 | unsigned long *trunc_p, | |
8483 | unsigned long encoded) | |
d9352518 | 8484 | { |
07d6d2b8 AM |
8485 | * start = encoded & 0x3F; |
8486 | * len = (encoded >> 6) & 0x3F; | |
d9352518 DB |
8487 | * oplen = (encoded >> 12) & 0x3F; |
8488 | * wordsz = (encoded >> 18) & 0xF; | |
8489 | * chunksz = (encoded >> 22) & 0xF; | |
8490 | * lsb0_p = (encoded >> 27) & 1; | |
8491 | * signed_p = (encoded >> 28) & 1; | |
8492 | * trunc_p = (encoded >> 29) & 1; | |
8493 | } | |
8494 | ||
cdfeee4f | 8495 | bfd_reloc_status_type |
0f02bbd9 | 8496 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
cdfeee4f | 8497 | asection *input_section ATTRIBUTE_UNUSED, |
0f02bbd9 AM |
8498 | bfd_byte *contents, |
8499 | Elf_Internal_Rela *rel, | |
8500 | bfd_vma relocation) | |
d9352518 | 8501 | { |
0f02bbd9 AM |
8502 | bfd_vma shift, x, mask; |
8503 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 8504 | bfd_reloc_status_type r; |
d9352518 DB |
8505 | |
8506 | /* Perform this reloc, since it is complex. | |
8507 | (this is not to say that it necessarily refers to a complex | |
8508 | symbol; merely that it is a self-describing CGEN based reloc. | |
8509 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 8510 | word size, etc) encoded within it.). */ |
d9352518 | 8511 | |
a0c8462f AM |
8512 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
8513 | &chunksz, &lsb0_p, &signed_p, | |
8514 | &trunc_p, rel->r_addend); | |
d9352518 DB |
8515 | |
8516 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
8517 | ||
8518 | if (lsb0_p) | |
8519 | shift = (start + 1) - len; | |
8520 | else | |
8521 | shift = (8 * wordsz) - (start + len); | |
8522 | ||
37b01f6a DG |
8523 | x = get_value (wordsz, chunksz, input_bfd, |
8524 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
d9352518 DB |
8525 | |
8526 | #ifdef DEBUG | |
8527 | printf ("Doing complex reloc: " | |
8528 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
8529 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
8530 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
8531 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
9ccb8af9 AM |
8532 | oplen, (unsigned long) x, (unsigned long) mask, |
8533 | (unsigned long) relocation); | |
d9352518 DB |
8534 | #endif |
8535 | ||
cdfeee4f | 8536 | r = bfd_reloc_ok; |
d9352518 | 8537 | if (! trunc_p) |
cdfeee4f AM |
8538 | /* Now do an overflow check. */ |
8539 | r = bfd_check_overflow ((signed_p | |
8540 | ? complain_overflow_signed | |
8541 | : complain_overflow_unsigned), | |
8542 | len, 0, (8 * wordsz), | |
8543 | relocation); | |
a0c8462f | 8544 | |
d9352518 DB |
8545 | /* Do the deed. */ |
8546 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
8547 | ||
8548 | #ifdef DEBUG | |
8549 | printf (" relocation: %8.8lx\n" | |
8550 | " shifted mask: %8.8lx\n" | |
8551 | " shifted/masked reloc: %8.8lx\n" | |
8552 | " result: %8.8lx\n", | |
9ccb8af9 AM |
8553 | (unsigned long) relocation, (unsigned long) (mask << shift), |
8554 | (unsigned long) ((relocation & mask) << shift), (unsigned long) x); | |
d9352518 | 8555 | #endif |
37b01f6a DG |
8556 | put_value (wordsz, chunksz, input_bfd, x, |
8557 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
cdfeee4f | 8558 | return r; |
d9352518 DB |
8559 | } |
8560 | ||
0e287786 AM |
8561 | /* Functions to read r_offset from external (target order) reloc |
8562 | entry. Faster than bfd_getl32 et al, because we let the compiler | |
8563 | know the value is aligned. */ | |
53df40a4 | 8564 | |
0e287786 AM |
8565 | static bfd_vma |
8566 | ext32l_r_offset (const void *p) | |
53df40a4 AM |
8567 | { |
8568 | union aligned32 | |
8569 | { | |
8570 | uint32_t v; | |
8571 | unsigned char c[4]; | |
8572 | }; | |
8573 | const union aligned32 *a | |
0e287786 | 8574 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8575 | |
8576 | uint32_t aval = ( (uint32_t) a->c[0] | |
8577 | | (uint32_t) a->c[1] << 8 | |
8578 | | (uint32_t) a->c[2] << 16 | |
8579 | | (uint32_t) a->c[3] << 24); | |
0e287786 | 8580 | return aval; |
53df40a4 AM |
8581 | } |
8582 | ||
0e287786 AM |
8583 | static bfd_vma |
8584 | ext32b_r_offset (const void *p) | |
53df40a4 AM |
8585 | { |
8586 | union aligned32 | |
8587 | { | |
8588 | uint32_t v; | |
8589 | unsigned char c[4]; | |
8590 | }; | |
8591 | const union aligned32 *a | |
0e287786 | 8592 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8593 | |
8594 | uint32_t aval = ( (uint32_t) a->c[0] << 24 | |
8595 | | (uint32_t) a->c[1] << 16 | |
8596 | | (uint32_t) a->c[2] << 8 | |
8597 | | (uint32_t) a->c[3]); | |
0e287786 | 8598 | return aval; |
53df40a4 AM |
8599 | } |
8600 | ||
8601 | #ifdef BFD_HOST_64_BIT | |
0e287786 AM |
8602 | static bfd_vma |
8603 | ext64l_r_offset (const void *p) | |
53df40a4 AM |
8604 | { |
8605 | union aligned64 | |
8606 | { | |
8607 | uint64_t v; | |
8608 | unsigned char c[8]; | |
8609 | }; | |
8610 | const union aligned64 *a | |
0e287786 | 8611 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8612 | |
8613 | uint64_t aval = ( (uint64_t) a->c[0] | |
8614 | | (uint64_t) a->c[1] << 8 | |
8615 | | (uint64_t) a->c[2] << 16 | |
8616 | | (uint64_t) a->c[3] << 24 | |
8617 | | (uint64_t) a->c[4] << 32 | |
8618 | | (uint64_t) a->c[5] << 40 | |
8619 | | (uint64_t) a->c[6] << 48 | |
8620 | | (uint64_t) a->c[7] << 56); | |
0e287786 | 8621 | return aval; |
53df40a4 AM |
8622 | } |
8623 | ||
0e287786 AM |
8624 | static bfd_vma |
8625 | ext64b_r_offset (const void *p) | |
53df40a4 AM |
8626 | { |
8627 | union aligned64 | |
8628 | { | |
8629 | uint64_t v; | |
8630 | unsigned char c[8]; | |
8631 | }; | |
8632 | const union aligned64 *a | |
0e287786 | 8633 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8634 | |
8635 | uint64_t aval = ( (uint64_t) a->c[0] << 56 | |
8636 | | (uint64_t) a->c[1] << 48 | |
8637 | | (uint64_t) a->c[2] << 40 | |
8638 | | (uint64_t) a->c[3] << 32 | |
8639 | | (uint64_t) a->c[4] << 24 | |
8640 | | (uint64_t) a->c[5] << 16 | |
8641 | | (uint64_t) a->c[6] << 8 | |
8642 | | (uint64_t) a->c[7]); | |
0e287786 | 8643 | return aval; |
53df40a4 AM |
8644 | } |
8645 | #endif | |
8646 | ||
c152c796 AM |
8647 | /* When performing a relocatable link, the input relocations are |
8648 | preserved. But, if they reference global symbols, the indices | |
d4730f92 BS |
8649 | referenced must be updated. Update all the relocations found in |
8650 | RELDATA. */ | |
c152c796 | 8651 | |
bca6d0e3 | 8652 | static bfd_boolean |
c152c796 | 8653 | elf_link_adjust_relocs (bfd *abfd, |
9eaff861 | 8654 | asection *sec, |
28dbcedc | 8655 | struct bfd_elf_section_reloc_data *reldata, |
10bbbc1d NC |
8656 | bfd_boolean sort, |
8657 | struct bfd_link_info *info) | |
c152c796 AM |
8658 | { |
8659 | unsigned int i; | |
8660 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8661 | bfd_byte *erela; | |
8662 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
8663 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8664 | bfd_vma r_type_mask; | |
8665 | int r_sym_shift; | |
d4730f92 BS |
8666 | unsigned int count = reldata->count; |
8667 | struct elf_link_hash_entry **rel_hash = reldata->hashes; | |
c152c796 | 8668 | |
d4730f92 | 8669 | if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) |
c152c796 AM |
8670 | { |
8671 | swap_in = bed->s->swap_reloc_in; | |
8672 | swap_out = bed->s->swap_reloc_out; | |
8673 | } | |
d4730f92 | 8674 | else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) |
c152c796 AM |
8675 | { |
8676 | swap_in = bed->s->swap_reloca_in; | |
8677 | swap_out = bed->s->swap_reloca_out; | |
8678 | } | |
8679 | else | |
8680 | abort (); | |
8681 | ||
8682 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
8683 | abort (); | |
8684 | ||
8685 | if (bed->s->arch_size == 32) | |
8686 | { | |
8687 | r_type_mask = 0xff; | |
8688 | r_sym_shift = 8; | |
8689 | } | |
8690 | else | |
8691 | { | |
8692 | r_type_mask = 0xffffffff; | |
8693 | r_sym_shift = 32; | |
8694 | } | |
8695 | ||
d4730f92 BS |
8696 | erela = reldata->hdr->contents; |
8697 | for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) | |
c152c796 AM |
8698 | { |
8699 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
8700 | unsigned int j; | |
8701 | ||
8702 | if (*rel_hash == NULL) | |
8703 | continue; | |
8704 | ||
10bbbc1d NC |
8705 | if ((*rel_hash)->indx == -2 |
8706 | && info->gc_sections | |
8707 | && ! info->gc_keep_exported) | |
8708 | { | |
8709 | /* PR 21524: Let the user know if a symbol was removed by garbage collection. */ | |
8710 | _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."), | |
8711 | abfd, sec, | |
8712 | (*rel_hash)->root.root.string); | |
8713 | _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."), | |
d42c267e | 8714 | abfd, sec); |
10bbbc1d NC |
8715 | bfd_set_error (bfd_error_invalid_operation); |
8716 | return FALSE; | |
8717 | } | |
c152c796 AM |
8718 | BFD_ASSERT ((*rel_hash)->indx >= 0); |
8719 | ||
8720 | (*swap_in) (abfd, erela, irela); | |
8721 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
8722 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
8723 | | (irela[j].r_info & r_type_mask)); | |
8724 | (*swap_out) (abfd, irela, erela); | |
8725 | } | |
53df40a4 | 8726 | |
9eaff861 AO |
8727 | if (bed->elf_backend_update_relocs) |
8728 | (*bed->elf_backend_update_relocs) (sec, reldata); | |
8729 | ||
0e287786 | 8730 | if (sort && count != 0) |
53df40a4 | 8731 | { |
0e287786 AM |
8732 | bfd_vma (*ext_r_off) (const void *); |
8733 | bfd_vma r_off; | |
8734 | size_t elt_size; | |
8735 | bfd_byte *base, *end, *p, *loc; | |
bca6d0e3 | 8736 | bfd_byte *buf = NULL; |
28dbcedc AM |
8737 | |
8738 | if (bed->s->arch_size == 32) | |
8739 | { | |
8740 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) | |
0e287786 | 8741 | ext_r_off = ext32l_r_offset; |
28dbcedc | 8742 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8743 | ext_r_off = ext32b_r_offset; |
28dbcedc AM |
8744 | else |
8745 | abort (); | |
8746 | } | |
53df40a4 | 8747 | else |
28dbcedc | 8748 | { |
53df40a4 | 8749 | #ifdef BFD_HOST_64_BIT |
28dbcedc | 8750 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) |
0e287786 | 8751 | ext_r_off = ext64l_r_offset; |
28dbcedc | 8752 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8753 | ext_r_off = ext64b_r_offset; |
28dbcedc | 8754 | else |
53df40a4 | 8755 | #endif |
28dbcedc AM |
8756 | abort (); |
8757 | } | |
0e287786 | 8758 | |
bca6d0e3 AM |
8759 | /* Must use a stable sort here. A modified insertion sort, |
8760 | since the relocs are mostly sorted already. */ | |
0e287786 AM |
8761 | elt_size = reldata->hdr->sh_entsize; |
8762 | base = reldata->hdr->contents; | |
8763 | end = base + count * elt_size; | |
bca6d0e3 | 8764 | if (elt_size > sizeof (Elf64_External_Rela)) |
0e287786 AM |
8765 | abort (); |
8766 | ||
8767 | /* Ensure the first element is lowest. This acts as a sentinel, | |
8768 | speeding the main loop below. */ | |
8769 | r_off = (*ext_r_off) (base); | |
8770 | for (p = loc = base; (p += elt_size) < end; ) | |
8771 | { | |
8772 | bfd_vma r_off2 = (*ext_r_off) (p); | |
8773 | if (r_off > r_off2) | |
8774 | { | |
8775 | r_off = r_off2; | |
8776 | loc = p; | |
8777 | } | |
8778 | } | |
8779 | if (loc != base) | |
8780 | { | |
8781 | /* Don't just swap *base and *loc as that changes the order | |
8782 | of the original base[0] and base[1] if they happen to | |
8783 | have the same r_offset. */ | |
bca6d0e3 AM |
8784 | bfd_byte onebuf[sizeof (Elf64_External_Rela)]; |
8785 | memcpy (onebuf, loc, elt_size); | |
0e287786 | 8786 | memmove (base + elt_size, base, loc - base); |
bca6d0e3 | 8787 | memcpy (base, onebuf, elt_size); |
0e287786 AM |
8788 | } |
8789 | ||
b29b8669 | 8790 | for (p = base + elt_size; (p += elt_size) < end; ) |
0e287786 AM |
8791 | { |
8792 | /* base to p is sorted, *p is next to insert. */ | |
8793 | r_off = (*ext_r_off) (p); | |
8794 | /* Search the sorted region for location to insert. */ | |
8795 | loc = p - elt_size; | |
8796 | while (r_off < (*ext_r_off) (loc)) | |
8797 | loc -= elt_size; | |
8798 | loc += elt_size; | |
8799 | if (loc != p) | |
8800 | { | |
bca6d0e3 AM |
8801 | /* Chances are there is a run of relocs to insert here, |
8802 | from one of more input files. Files are not always | |
8803 | linked in order due to the way elf_link_input_bfd is | |
8804 | called. See pr17666. */ | |
8805 | size_t sortlen = p - loc; | |
8806 | bfd_vma r_off2 = (*ext_r_off) (loc); | |
8807 | size_t runlen = elt_size; | |
8808 | size_t buf_size = 96 * 1024; | |
8809 | while (p + runlen < end | |
8810 | && (sortlen <= buf_size | |
8811 | || runlen + elt_size <= buf_size) | |
8812 | && r_off2 > (*ext_r_off) (p + runlen)) | |
8813 | runlen += elt_size; | |
8814 | if (buf == NULL) | |
8815 | { | |
8816 | buf = bfd_malloc (buf_size); | |
8817 | if (buf == NULL) | |
8818 | return FALSE; | |
8819 | } | |
8820 | if (runlen < sortlen) | |
8821 | { | |
8822 | memcpy (buf, p, runlen); | |
8823 | memmove (loc + runlen, loc, sortlen); | |
8824 | memcpy (loc, buf, runlen); | |
8825 | } | |
8826 | else | |
8827 | { | |
8828 | memcpy (buf, loc, sortlen); | |
8829 | memmove (loc, p, runlen); | |
8830 | memcpy (loc + runlen, buf, sortlen); | |
8831 | } | |
b29b8669 | 8832 | p += runlen - elt_size; |
0e287786 AM |
8833 | } |
8834 | } | |
8835 | /* Hashes are no longer valid. */ | |
28dbcedc AM |
8836 | free (reldata->hashes); |
8837 | reldata->hashes = NULL; | |
bca6d0e3 | 8838 | free (buf); |
53df40a4 | 8839 | } |
bca6d0e3 | 8840 | return TRUE; |
c152c796 AM |
8841 | } |
8842 | ||
8843 | struct elf_link_sort_rela | |
8844 | { | |
8845 | union { | |
8846 | bfd_vma offset; | |
8847 | bfd_vma sym_mask; | |
8848 | } u; | |
8849 | enum elf_reloc_type_class type; | |
8850 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
8851 | Elf_Internal_Rela rela[1]; | |
8852 | }; | |
8853 | ||
8854 | static int | |
8855 | elf_link_sort_cmp1 (const void *A, const void *B) | |
8856 | { | |
a50b1753 NC |
8857 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8858 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 AM |
8859 | int relativea, relativeb; |
8860 | ||
8861 | relativea = a->type == reloc_class_relative; | |
8862 | relativeb = b->type == reloc_class_relative; | |
8863 | ||
8864 | if (relativea < relativeb) | |
8865 | return 1; | |
8866 | if (relativea > relativeb) | |
8867 | return -1; | |
8868 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
8869 | return -1; | |
8870 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
8871 | return 1; | |
8872 | if (a->rela->r_offset < b->rela->r_offset) | |
8873 | return -1; | |
8874 | if (a->rela->r_offset > b->rela->r_offset) | |
8875 | return 1; | |
8876 | return 0; | |
8877 | } | |
8878 | ||
8879 | static int | |
8880 | elf_link_sort_cmp2 (const void *A, const void *B) | |
8881 | { | |
a50b1753 NC |
8882 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8883 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 | 8884 | |
7e612e98 | 8885 | if (a->type < b->type) |
c152c796 | 8886 | return -1; |
7e612e98 | 8887 | if (a->type > b->type) |
c152c796 | 8888 | return 1; |
7e612e98 | 8889 | if (a->u.offset < b->u.offset) |
c152c796 | 8890 | return -1; |
7e612e98 | 8891 | if (a->u.offset > b->u.offset) |
c152c796 AM |
8892 | return 1; |
8893 | if (a->rela->r_offset < b->rela->r_offset) | |
8894 | return -1; | |
8895 | if (a->rela->r_offset > b->rela->r_offset) | |
8896 | return 1; | |
8897 | return 0; | |
8898 | } | |
8899 | ||
8900 | static size_t | |
8901 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
8902 | { | |
3410fea8 | 8903 | asection *dynamic_relocs; |
fc66a176 L |
8904 | asection *rela_dyn; |
8905 | asection *rel_dyn; | |
c152c796 AM |
8906 | bfd_size_type count, size; |
8907 | size_t i, ret, sort_elt, ext_size; | |
8908 | bfd_byte *sort, *s_non_relative, *p; | |
8909 | struct elf_link_sort_rela *sq; | |
8910 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8911 | int i2e = bed->s->int_rels_per_ext_rel; | |
c8e44c6d | 8912 | unsigned int opb = bfd_octets_per_byte (abfd); |
c152c796 AM |
8913 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
8914 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8915 | struct bfd_link_order *lo; | |
8916 | bfd_vma r_sym_mask; | |
3410fea8 | 8917 | bfd_boolean use_rela; |
c152c796 | 8918 | |
3410fea8 NC |
8919 | /* Find a dynamic reloc section. */ |
8920 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
8921 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
8922 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
8923 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 8924 | { |
3410fea8 NC |
8925 | bfd_boolean use_rela_initialised = FALSE; |
8926 | ||
8927 | /* This is just here to stop gcc from complaining. | |
c8e44c6d | 8928 | Its initialization checking code is not perfect. */ |
3410fea8 NC |
8929 | use_rela = TRUE; |
8930 | ||
8931 | /* Both sections are present. Examine the sizes | |
8932 | of the indirect sections to help us choose. */ | |
8933 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8934 | if (lo->type == bfd_indirect_link_order) | |
8935 | { | |
8936 | asection *o = lo->u.indirect.section; | |
8937 | ||
8938 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8939 | { | |
8940 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8941 | /* Section size is divisible by both rel and rela sizes. | |
8942 | It is of no help to us. */ | |
8943 | ; | |
8944 | else | |
8945 | { | |
8946 | /* Section size is only divisible by rela. */ | |
535b785f | 8947 | if (use_rela_initialised && !use_rela) |
3410fea8 | 8948 | { |
c8e44c6d AM |
8949 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8950 | "they are in more than one size"), | |
8951 | abfd); | |
3410fea8 NC |
8952 | bfd_set_error (bfd_error_invalid_operation); |
8953 | return 0; | |
8954 | } | |
8955 | else | |
8956 | { | |
8957 | use_rela = TRUE; | |
8958 | use_rela_initialised = TRUE; | |
8959 | } | |
8960 | } | |
8961 | } | |
8962 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
8963 | { | |
8964 | /* Section size is only divisible by rel. */ | |
535b785f | 8965 | if (use_rela_initialised && use_rela) |
3410fea8 | 8966 | { |
c8e44c6d AM |
8967 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8968 | "they are in more than one size"), | |
8969 | abfd); | |
3410fea8 NC |
8970 | bfd_set_error (bfd_error_invalid_operation); |
8971 | return 0; | |
8972 | } | |
8973 | else | |
8974 | { | |
8975 | use_rela = FALSE; | |
8976 | use_rela_initialised = TRUE; | |
8977 | } | |
8978 | } | |
8979 | else | |
8980 | { | |
c8e44c6d AM |
8981 | /* The section size is not divisible by either - |
8982 | something is wrong. */ | |
8983 | _bfd_error_handler (_("%B: Unable to sort relocs - " | |
8984 | "they are of an unknown size"), abfd); | |
3410fea8 NC |
8985 | bfd_set_error (bfd_error_invalid_operation); |
8986 | return 0; | |
8987 | } | |
8988 | } | |
8989 | ||
8990 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8991 | if (lo->type == bfd_indirect_link_order) | |
8992 | { | |
8993 | asection *o = lo->u.indirect.section; | |
8994 | ||
8995 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8996 | { | |
8997 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8998 | /* Section size is divisible by both rel and rela sizes. | |
8999 | It is of no help to us. */ | |
9000 | ; | |
9001 | else | |
9002 | { | |
9003 | /* Section size is only divisible by rela. */ | |
535b785f | 9004 | if (use_rela_initialised && !use_rela) |
3410fea8 | 9005 | { |
c8e44c6d AM |
9006 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
9007 | "they are in more than one size"), | |
9008 | abfd); | |
3410fea8 NC |
9009 | bfd_set_error (bfd_error_invalid_operation); |
9010 | return 0; | |
9011 | } | |
9012 | else | |
9013 | { | |
9014 | use_rela = TRUE; | |
9015 | use_rela_initialised = TRUE; | |
9016 | } | |
9017 | } | |
9018 | } | |
9019 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
9020 | { | |
9021 | /* Section size is only divisible by rel. */ | |
535b785f | 9022 | if (use_rela_initialised && use_rela) |
3410fea8 | 9023 | { |
c8e44c6d AM |
9024 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
9025 | "they are in more than one size"), | |
9026 | abfd); | |
3410fea8 NC |
9027 | bfd_set_error (bfd_error_invalid_operation); |
9028 | return 0; | |
9029 | } | |
9030 | else | |
9031 | { | |
9032 | use_rela = FALSE; | |
9033 | use_rela_initialised = TRUE; | |
9034 | } | |
9035 | } | |
9036 | else | |
9037 | { | |
c8e44c6d AM |
9038 | /* The section size is not divisible by either - |
9039 | something is wrong. */ | |
9040 | _bfd_error_handler (_("%B: Unable to sort relocs - " | |
9041 | "they are of an unknown size"), abfd); | |
3410fea8 NC |
9042 | bfd_set_error (bfd_error_invalid_operation); |
9043 | return 0; | |
9044 | } | |
9045 | } | |
9046 | ||
9047 | if (! use_rela_initialised) | |
9048 | /* Make a guess. */ | |
9049 | use_rela = TRUE; | |
c152c796 | 9050 | } |
fc66a176 L |
9051 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
9052 | use_rela = TRUE; | |
9053 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 9054 | use_rela = FALSE; |
c152c796 | 9055 | else |
fc66a176 | 9056 | return 0; |
3410fea8 NC |
9057 | |
9058 | if (use_rela) | |
c152c796 | 9059 | { |
3410fea8 | 9060 | dynamic_relocs = rela_dyn; |
c152c796 AM |
9061 | ext_size = bed->s->sizeof_rela; |
9062 | swap_in = bed->s->swap_reloca_in; | |
9063 | swap_out = bed->s->swap_reloca_out; | |
9064 | } | |
3410fea8 NC |
9065 | else |
9066 | { | |
9067 | dynamic_relocs = rel_dyn; | |
9068 | ext_size = bed->s->sizeof_rel; | |
9069 | swap_in = bed->s->swap_reloc_in; | |
9070 | swap_out = bed->s->swap_reloc_out; | |
9071 | } | |
c152c796 AM |
9072 | |
9073 | size = 0; | |
3410fea8 | 9074 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 9075 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 9076 | size += lo->u.indirect.section->size; |
c152c796 | 9077 | |
3410fea8 | 9078 | if (size != dynamic_relocs->size) |
c152c796 AM |
9079 | return 0; |
9080 | ||
9081 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
9082 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
9083 | |
9084 | count = dynamic_relocs->size / ext_size; | |
5e486aa1 NC |
9085 | if (count == 0) |
9086 | return 0; | |
a50b1753 | 9087 | sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); |
3410fea8 | 9088 | |
c152c796 AM |
9089 | if (sort == NULL) |
9090 | { | |
9091 | (*info->callbacks->warning) | |
9092 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
9093 | return 0; | |
9094 | } | |
9095 | ||
9096 | if (bed->s->arch_size == 32) | |
9097 | r_sym_mask = ~(bfd_vma) 0xff; | |
9098 | else | |
9099 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
9100 | ||
3410fea8 | 9101 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9102 | if (lo->type == bfd_indirect_link_order) |
9103 | { | |
9104 | bfd_byte *erel, *erelend; | |
9105 | asection *o = lo->u.indirect.section; | |
9106 | ||
1da212d6 AM |
9107 | if (o->contents == NULL && o->size != 0) |
9108 | { | |
9109 | /* This is a reloc section that is being handled as a normal | |
9110 | section. See bfd_section_from_shdr. We can't combine | |
9111 | relocs in this case. */ | |
9112 | free (sort); | |
9113 | return 0; | |
9114 | } | |
c152c796 | 9115 | erel = o->contents; |
eea6121a | 9116 | erelend = o->contents + o->size; |
c8e44c6d | 9117 | p = sort + o->output_offset * opb / ext_size * sort_elt; |
3410fea8 | 9118 | |
c152c796 AM |
9119 | while (erel < erelend) |
9120 | { | |
9121 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 9122 | |
c152c796 | 9123 | (*swap_in) (abfd, erel, s->rela); |
7e612e98 | 9124 | s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); |
c152c796 AM |
9125 | s->u.sym_mask = r_sym_mask; |
9126 | p += sort_elt; | |
9127 | erel += ext_size; | |
9128 | } | |
9129 | } | |
9130 | ||
9131 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
9132 | ||
9133 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
9134 | { | |
9135 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9136 | if (s->type != reloc_class_relative) | |
9137 | break; | |
9138 | } | |
9139 | ret = i; | |
9140 | s_non_relative = p; | |
9141 | ||
9142 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
9143 | for (; i < count; i++, p += sort_elt) | |
9144 | { | |
9145 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
9146 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
9147 | sq = sp; | |
9148 | sp->u.offset = sq->rela->r_offset; | |
9149 | } | |
9150 | ||
9151 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
9152 | ||
c8e44c6d AM |
9153 | struct elf_link_hash_table *htab = elf_hash_table (info); |
9154 | if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) | |
9155 | { | |
9156 | /* We have plt relocs in .rela.dyn. */ | |
9157 | sq = (struct elf_link_sort_rela *) sort; | |
9158 | for (i = 0; i < count; i++) | |
9159 | if (sq[count - i - 1].type != reloc_class_plt) | |
9160 | break; | |
9161 | if (i != 0 && htab->srelplt->size == i * ext_size) | |
9162 | { | |
9163 | struct bfd_link_order **plo; | |
9164 | /* Put srelplt link_order last. This is so the output_offset | |
9165 | set in the next loop is correct for DT_JMPREL. */ | |
9166 | for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) | |
9167 | if ((*plo)->type == bfd_indirect_link_order | |
9168 | && (*plo)->u.indirect.section == htab->srelplt) | |
9169 | { | |
9170 | lo = *plo; | |
9171 | *plo = lo->next; | |
9172 | } | |
9173 | else | |
9174 | plo = &(*plo)->next; | |
9175 | *plo = lo; | |
9176 | lo->next = NULL; | |
9177 | dynamic_relocs->map_tail.link_order = lo; | |
9178 | } | |
9179 | } | |
9180 | ||
9181 | p = sort; | |
3410fea8 | 9182 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9183 | if (lo->type == bfd_indirect_link_order) |
9184 | { | |
9185 | bfd_byte *erel, *erelend; | |
9186 | asection *o = lo->u.indirect.section; | |
9187 | ||
9188 | erel = o->contents; | |
eea6121a | 9189 | erelend = o->contents + o->size; |
c8e44c6d | 9190 | o->output_offset = (p - sort) / sort_elt * ext_size / opb; |
c152c796 AM |
9191 | while (erel < erelend) |
9192 | { | |
9193 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9194 | (*swap_out) (abfd, s->rela, erel); | |
9195 | p += sort_elt; | |
9196 | erel += ext_size; | |
9197 | } | |
9198 | } | |
9199 | ||
9200 | free (sort); | |
3410fea8 | 9201 | *psec = dynamic_relocs; |
c152c796 AM |
9202 | return ret; |
9203 | } | |
9204 | ||
ef10c3ac | 9205 | /* Add a symbol to the output symbol string table. */ |
c152c796 | 9206 | |
6e0b88f1 | 9207 | static int |
ef10c3ac L |
9208 | elf_link_output_symstrtab (struct elf_final_link_info *flinfo, |
9209 | const char *name, | |
9210 | Elf_Internal_Sym *elfsym, | |
9211 | asection *input_sec, | |
9212 | struct elf_link_hash_entry *h) | |
c152c796 | 9213 | { |
6e0b88f1 | 9214 | int (*output_symbol_hook) |
c152c796 AM |
9215 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, |
9216 | struct elf_link_hash_entry *); | |
ef10c3ac | 9217 | struct elf_link_hash_table *hash_table; |
c152c796 | 9218 | const struct elf_backend_data *bed; |
ef10c3ac | 9219 | bfd_size_type strtabsize; |
c152c796 | 9220 | |
8539e4e8 AM |
9221 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); |
9222 | ||
8b127cbc | 9223 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 AM |
9224 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; |
9225 | if (output_symbol_hook != NULL) | |
9226 | { | |
8b127cbc | 9227 | int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); |
6e0b88f1 AM |
9228 | if (ret != 1) |
9229 | return ret; | |
c152c796 AM |
9230 | } |
9231 | ||
ef10c3ac L |
9232 | if (name == NULL |
9233 | || *name == '\0' | |
9234 | || (input_sec->flags & SEC_EXCLUDE)) | |
9235 | elfsym->st_name = (unsigned long) -1; | |
c152c796 AM |
9236 | else |
9237 | { | |
ef10c3ac L |
9238 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
9239 | to get the final offset for st_name. */ | |
9240 | elfsym->st_name | |
9241 | = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, | |
9242 | name, FALSE); | |
c152c796 | 9243 | if (elfsym->st_name == (unsigned long) -1) |
6e0b88f1 | 9244 | return 0; |
c152c796 AM |
9245 | } |
9246 | ||
ef10c3ac L |
9247 | hash_table = elf_hash_table (flinfo->info); |
9248 | strtabsize = hash_table->strtabsize; | |
9249 | if (strtabsize <= hash_table->strtabcount) | |
c152c796 | 9250 | { |
ef10c3ac L |
9251 | strtabsize += strtabsize; |
9252 | hash_table->strtabsize = strtabsize; | |
9253 | strtabsize *= sizeof (*hash_table->strtab); | |
9254 | hash_table->strtab | |
9255 | = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, | |
9256 | strtabsize); | |
9257 | if (hash_table->strtab == NULL) | |
6e0b88f1 | 9258 | return 0; |
c152c796 | 9259 | } |
ef10c3ac L |
9260 | hash_table->strtab[hash_table->strtabcount].sym = *elfsym; |
9261 | hash_table->strtab[hash_table->strtabcount].dest_index | |
9262 | = hash_table->strtabcount; | |
9263 | hash_table->strtab[hash_table->strtabcount].destshndx_index | |
9264 | = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0; | |
9265 | ||
9266 | bfd_get_symcount (flinfo->output_bfd) += 1; | |
9267 | hash_table->strtabcount += 1; | |
9268 | ||
9269 | return 1; | |
9270 | } | |
9271 | ||
9272 | /* Swap symbols out to the symbol table and flush the output symbols to | |
9273 | the file. */ | |
9274 | ||
9275 | static bfd_boolean | |
9276 | elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) | |
9277 | { | |
9278 | struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); | |
ef53be89 AM |
9279 | bfd_size_type amt; |
9280 | size_t i; | |
ef10c3ac L |
9281 | const struct elf_backend_data *bed; |
9282 | bfd_byte *symbuf; | |
9283 | Elf_Internal_Shdr *hdr; | |
9284 | file_ptr pos; | |
9285 | bfd_boolean ret; | |
9286 | ||
9287 | if (!hash_table->strtabcount) | |
9288 | return TRUE; | |
9289 | ||
9290 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); | |
9291 | ||
9292 | bed = get_elf_backend_data (flinfo->output_bfd); | |
c152c796 | 9293 | |
ef10c3ac L |
9294 | amt = bed->s->sizeof_sym * hash_table->strtabcount; |
9295 | symbuf = (bfd_byte *) bfd_malloc (amt); | |
9296 | if (symbuf == NULL) | |
9297 | return FALSE; | |
1b786873 | 9298 | |
ef10c3ac | 9299 | if (flinfo->symshndxbuf) |
c152c796 | 9300 | { |
ef53be89 AM |
9301 | amt = sizeof (Elf_External_Sym_Shndx); |
9302 | amt *= bfd_get_symcount (flinfo->output_bfd); | |
ef10c3ac L |
9303 | flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); |
9304 | if (flinfo->symshndxbuf == NULL) | |
c152c796 | 9305 | { |
ef10c3ac L |
9306 | free (symbuf); |
9307 | return FALSE; | |
c152c796 | 9308 | } |
c152c796 AM |
9309 | } |
9310 | ||
ef10c3ac L |
9311 | for (i = 0; i < hash_table->strtabcount; i++) |
9312 | { | |
9313 | struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; | |
9314 | if (elfsym->sym.st_name == (unsigned long) -1) | |
9315 | elfsym->sym.st_name = 0; | |
9316 | else | |
9317 | elfsym->sym.st_name | |
9318 | = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, | |
9319 | elfsym->sym.st_name); | |
9320 | bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, | |
9321 | ((bfd_byte *) symbuf | |
9322 | + (elfsym->dest_index | |
9323 | * bed->s->sizeof_sym)), | |
9324 | (flinfo->symshndxbuf | |
9325 | + elfsym->destshndx_index)); | |
9326 | } | |
9327 | ||
9328 | hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; | |
9329 | pos = hdr->sh_offset + hdr->sh_size; | |
9330 | amt = hash_table->strtabcount * bed->s->sizeof_sym; | |
9331 | if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 | |
9332 | && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt) | |
9333 | { | |
9334 | hdr->sh_size += amt; | |
9335 | ret = TRUE; | |
9336 | } | |
9337 | else | |
9338 | ret = FALSE; | |
c152c796 | 9339 | |
ef10c3ac L |
9340 | free (symbuf); |
9341 | ||
9342 | free (hash_table->strtab); | |
9343 | hash_table->strtab = NULL; | |
9344 | ||
9345 | return ret; | |
c152c796 AM |
9346 | } |
9347 | ||
c0d5a53d L |
9348 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
9349 | ||
9350 | static bfd_boolean | |
9351 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
9352 | { | |
4fbb74a6 AM |
9353 | if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) |
9354 | && sym->st_shndx < SHN_LORESERVE) | |
c0d5a53d L |
9355 | { |
9356 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 9357 | beyond 64k. */ |
4eca0228 | 9358 | _bfd_error_handler |
695344c0 | 9359 | /* xgettext:c-format */ |
c0d5a53d | 9360 | (_("%B: Too many sections: %d (>= %d)"), |
4fbb74a6 | 9361 | abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); |
c0d5a53d L |
9362 | bfd_set_error (bfd_error_nonrepresentable_section); |
9363 | return FALSE; | |
9364 | } | |
9365 | return TRUE; | |
9366 | } | |
9367 | ||
c152c796 AM |
9368 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
9369 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
9370 | versioned symbol that would normally require an explicit version. | |
9371 | We also handle the case that a DSO references a hidden symbol | |
9372 | which may be satisfied by a versioned symbol in another DSO. */ | |
9373 | ||
9374 | static bfd_boolean | |
9375 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
9376 | const struct elf_backend_data *bed, | |
9377 | struct elf_link_hash_entry *h) | |
9378 | { | |
9379 | bfd *abfd; | |
9380 | struct elf_link_loaded_list *loaded; | |
9381 | ||
9382 | if (!is_elf_hash_table (info->hash)) | |
9383 | return FALSE; | |
9384 | ||
90c984fc L |
9385 | /* Check indirect symbol. */ |
9386 | while (h->root.type == bfd_link_hash_indirect) | |
9387 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9388 | ||
c152c796 AM |
9389 | switch (h->root.type) |
9390 | { | |
9391 | default: | |
9392 | abfd = NULL; | |
9393 | break; | |
9394 | ||
9395 | case bfd_link_hash_undefined: | |
9396 | case bfd_link_hash_undefweak: | |
9397 | abfd = h->root.u.undef.abfd; | |
f4ab0e2d L |
9398 | if (abfd == NULL |
9399 | || (abfd->flags & DYNAMIC) == 0 | |
e56f61be | 9400 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
9401 | return FALSE; |
9402 | break; | |
9403 | ||
9404 | case bfd_link_hash_defined: | |
9405 | case bfd_link_hash_defweak: | |
9406 | abfd = h->root.u.def.section->owner; | |
9407 | break; | |
9408 | ||
9409 | case bfd_link_hash_common: | |
9410 | abfd = h->root.u.c.p->section->owner; | |
9411 | break; | |
9412 | } | |
9413 | BFD_ASSERT (abfd != NULL); | |
9414 | ||
9415 | for (loaded = elf_hash_table (info)->loaded; | |
9416 | loaded != NULL; | |
9417 | loaded = loaded->next) | |
9418 | { | |
9419 | bfd *input; | |
9420 | Elf_Internal_Shdr *hdr; | |
ef53be89 AM |
9421 | size_t symcount; |
9422 | size_t extsymcount; | |
9423 | size_t extsymoff; | |
c152c796 AM |
9424 | Elf_Internal_Shdr *versymhdr; |
9425 | Elf_Internal_Sym *isym; | |
9426 | Elf_Internal_Sym *isymend; | |
9427 | Elf_Internal_Sym *isymbuf; | |
9428 | Elf_External_Versym *ever; | |
9429 | Elf_External_Versym *extversym; | |
9430 | ||
9431 | input = loaded->abfd; | |
9432 | ||
9433 | /* We check each DSO for a possible hidden versioned definition. */ | |
9434 | if (input == abfd | |
9435 | || (input->flags & DYNAMIC) == 0 | |
9436 | || elf_dynversym (input) == 0) | |
9437 | continue; | |
9438 | ||
9439 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
9440 | ||
9441 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
9442 | if (elf_bad_symtab (input)) | |
9443 | { | |
9444 | extsymcount = symcount; | |
9445 | extsymoff = 0; | |
9446 | } | |
9447 | else | |
9448 | { | |
9449 | extsymcount = symcount - hdr->sh_info; | |
9450 | extsymoff = hdr->sh_info; | |
9451 | } | |
9452 | ||
9453 | if (extsymcount == 0) | |
9454 | continue; | |
9455 | ||
9456 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
9457 | NULL, NULL, NULL); | |
9458 | if (isymbuf == NULL) | |
9459 | return FALSE; | |
9460 | ||
9461 | /* Read in any version definitions. */ | |
9462 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
a50b1753 | 9463 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
c152c796 AM |
9464 | if (extversym == NULL) |
9465 | goto error_ret; | |
9466 | ||
9467 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
9468 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
9469 | != versymhdr->sh_size)) | |
9470 | { | |
9471 | free (extversym); | |
9472 | error_ret: | |
9473 | free (isymbuf); | |
9474 | return FALSE; | |
9475 | } | |
9476 | ||
9477 | ever = extversym + extsymoff; | |
9478 | isymend = isymbuf + extsymcount; | |
9479 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
9480 | { | |
9481 | const char *name; | |
9482 | Elf_Internal_Versym iver; | |
9483 | unsigned short version_index; | |
9484 | ||
9485 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
9486 | || isym->st_shndx == SHN_UNDEF) | |
9487 | continue; | |
9488 | ||
9489 | name = bfd_elf_string_from_elf_section (input, | |
9490 | hdr->sh_link, | |
9491 | isym->st_name); | |
9492 | if (strcmp (name, h->root.root.string) != 0) | |
9493 | continue; | |
9494 | ||
9495 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
9496 | ||
d023c380 L |
9497 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 |
9498 | && !(h->def_regular | |
9499 | && h->forced_local)) | |
c152c796 AM |
9500 | { |
9501 | /* If we have a non-hidden versioned sym, then it should | |
d023c380 L |
9502 | have provided a definition for the undefined sym unless |
9503 | it is defined in a non-shared object and forced local. | |
9504 | */ | |
c152c796 AM |
9505 | abort (); |
9506 | } | |
9507 | ||
9508 | version_index = iver.vs_vers & VERSYM_VERSION; | |
9509 | if (version_index == 1 || version_index == 2) | |
9510 | { | |
9511 | /* This is the base or first version. We can use it. */ | |
9512 | free (extversym); | |
9513 | free (isymbuf); | |
9514 | return TRUE; | |
9515 | } | |
9516 | } | |
9517 | ||
9518 | free (extversym); | |
9519 | free (isymbuf); | |
9520 | } | |
9521 | ||
9522 | return FALSE; | |
9523 | } | |
9524 | ||
b8871f35 L |
9525 | /* Convert ELF common symbol TYPE. */ |
9526 | ||
9527 | static int | |
9528 | elf_link_convert_common_type (struct bfd_link_info *info, int type) | |
9529 | { | |
9530 | /* Commom symbol can only appear in relocatable link. */ | |
9531 | if (!bfd_link_relocatable (info)) | |
9532 | abort (); | |
9533 | switch (info->elf_stt_common) | |
9534 | { | |
9535 | case unchanged: | |
9536 | break; | |
9537 | case elf_stt_common: | |
9538 | type = STT_COMMON; | |
9539 | break; | |
9540 | case no_elf_stt_common: | |
9541 | type = STT_OBJECT; | |
9542 | break; | |
9543 | } | |
9544 | return type; | |
9545 | } | |
9546 | ||
c152c796 AM |
9547 | /* Add an external symbol to the symbol table. This is called from |
9548 | the hash table traversal routine. When generating a shared object, | |
9549 | we go through the symbol table twice. The first time we output | |
9550 | anything that might have been forced to local scope in a version | |
9551 | script. The second time we output the symbols that are still | |
9552 | global symbols. */ | |
9553 | ||
9554 | static bfd_boolean | |
7686d77d | 9555 | elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) |
c152c796 | 9556 | { |
7686d77d | 9557 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; |
a50b1753 | 9558 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
8b127cbc | 9559 | struct elf_final_link_info *flinfo = eoinfo->flinfo; |
c152c796 AM |
9560 | bfd_boolean strip; |
9561 | Elf_Internal_Sym sym; | |
9562 | asection *input_sec; | |
9563 | const struct elf_backend_data *bed; | |
6e0b88f1 AM |
9564 | long indx; |
9565 | int ret; | |
b8871f35 | 9566 | unsigned int type; |
c152c796 AM |
9567 | |
9568 | if (h->root.type == bfd_link_hash_warning) | |
9569 | { | |
9570 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9571 | if (h->root.type == bfd_link_hash_new) | |
9572 | return TRUE; | |
9573 | } | |
9574 | ||
9575 | /* Decide whether to output this symbol in this pass. */ | |
9576 | if (eoinfo->localsyms) | |
9577 | { | |
4deb8f71 | 9578 | if (!h->forced_local) |
c152c796 AM |
9579 | return TRUE; |
9580 | } | |
9581 | else | |
9582 | { | |
4deb8f71 | 9583 | if (h->forced_local) |
c152c796 AM |
9584 | return TRUE; |
9585 | } | |
9586 | ||
8b127cbc | 9587 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 | 9588 | |
12ac1cf5 | 9589 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 9590 | { |
12ac1cf5 NC |
9591 | /* If we have an undefined symbol reference here then it must have |
9592 | come from a shared library that is being linked in. (Undefined | |
98da7939 L |
9593 | references in regular files have already been handled unless |
9594 | they are in unreferenced sections which are removed by garbage | |
9595 | collection). */ | |
12ac1cf5 NC |
9596 | bfd_boolean ignore_undef = FALSE; |
9597 | ||
9598 | /* Some symbols may be special in that the fact that they're | |
9599 | undefined can be safely ignored - let backend determine that. */ | |
9600 | if (bed->elf_backend_ignore_undef_symbol) | |
9601 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
9602 | ||
9603 | /* If we are reporting errors for this situation then do so now. */ | |
89a2ee5a | 9604 | if (!ignore_undef |
12ac1cf5 | 9605 | && h->ref_dynamic |
8b127cbc AM |
9606 | && (!h->ref_regular || flinfo->info->gc_sections) |
9607 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h) | |
9608 | && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
1a72702b AM |
9609 | (*flinfo->info->callbacks->undefined_symbol) |
9610 | (flinfo->info, h->root.root.string, | |
9611 | h->ref_regular ? NULL : h->root.u.undef.abfd, | |
9612 | NULL, 0, | |
9613 | flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR); | |
97196564 L |
9614 | |
9615 | /* Strip a global symbol defined in a discarded section. */ | |
9616 | if (h->indx == -3) | |
9617 | return TRUE; | |
c152c796 AM |
9618 | } |
9619 | ||
9620 | /* We should also warn if a forced local symbol is referenced from | |
9621 | shared libraries. */ | |
0e1862bb | 9622 | if (bfd_link_executable (flinfo->info) |
f5385ebf AM |
9623 | && h->forced_local |
9624 | && h->ref_dynamic | |
371a5866 | 9625 | && h->def_regular |
f5385ebf | 9626 | && !h->dynamic_def |
ee659f1f | 9627 | && h->ref_dynamic_nonweak |
8b127cbc | 9628 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) |
c152c796 | 9629 | { |
17d078c5 AM |
9630 | bfd *def_bfd; |
9631 | const char *msg; | |
90c984fc L |
9632 | struct elf_link_hash_entry *hi = h; |
9633 | ||
9634 | /* Check indirect symbol. */ | |
9635 | while (hi->root.type == bfd_link_hash_indirect) | |
9636 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
17d078c5 AM |
9637 | |
9638 | if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
695344c0 | 9639 | /* xgettext:c-format */ |
17d078c5 AM |
9640 | msg = _("%B: internal symbol `%s' in %B is referenced by DSO"); |
9641 | else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) | |
695344c0 | 9642 | /* xgettext:c-format */ |
17d078c5 AM |
9643 | msg = _("%B: hidden symbol `%s' in %B is referenced by DSO"); |
9644 | else | |
695344c0 | 9645 | /* xgettext:c-format */ |
17d078c5 | 9646 | msg = _("%B: local symbol `%s' in %B is referenced by DSO"); |
8b127cbc | 9647 | def_bfd = flinfo->output_bfd; |
90c984fc L |
9648 | if (hi->root.u.def.section != bfd_abs_section_ptr) |
9649 | def_bfd = hi->root.u.def.section->owner; | |
c08bb8dd AM |
9650 | _bfd_error_handler (msg, flinfo->output_bfd, |
9651 | h->root.root.string, def_bfd); | |
17d078c5 | 9652 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9653 | eoinfo->failed = TRUE; |
9654 | return FALSE; | |
9655 | } | |
9656 | ||
9657 | /* We don't want to output symbols that have never been mentioned by | |
9658 | a regular file, or that we have been told to strip. However, if | |
9659 | h->indx is set to -2, the symbol is used by a reloc and we must | |
9660 | output it. */ | |
d983c8c5 | 9661 | strip = FALSE; |
c152c796 | 9662 | if (h->indx == -2) |
d983c8c5 | 9663 | ; |
f5385ebf | 9664 | else if ((h->def_dynamic |
77cfaee6 AM |
9665 | || h->ref_dynamic |
9666 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
9667 | && !h->def_regular |
9668 | && !h->ref_regular) | |
c152c796 | 9669 | strip = TRUE; |
8b127cbc | 9670 | else if (flinfo->info->strip == strip_all) |
c152c796 | 9671 | strip = TRUE; |
8b127cbc AM |
9672 | else if (flinfo->info->strip == strip_some |
9673 | && bfd_hash_lookup (flinfo->info->keep_hash, | |
c152c796 AM |
9674 | h->root.root.string, FALSE, FALSE) == NULL) |
9675 | strip = TRUE; | |
d56d55e7 AM |
9676 | else if ((h->root.type == bfd_link_hash_defined |
9677 | || h->root.type == bfd_link_hash_defweak) | |
8b127cbc | 9678 | && ((flinfo->info->strip_discarded |
dbaa2011 | 9679 | && discarded_section (h->root.u.def.section)) |
ca4be51c AM |
9680 | || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 |
9681 | && h->root.u.def.section->owner != NULL | |
d56d55e7 | 9682 | && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) |
c152c796 | 9683 | strip = TRUE; |
9e2278f5 AM |
9684 | else if ((h->root.type == bfd_link_hash_undefined |
9685 | || h->root.type == bfd_link_hash_undefweak) | |
9686 | && h->root.u.undef.abfd != NULL | |
9687 | && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) | |
9688 | strip = TRUE; | |
c152c796 | 9689 | |
b8871f35 L |
9690 | type = h->type; |
9691 | ||
c152c796 | 9692 | /* If we're stripping it, and it's not a dynamic symbol, there's |
d983c8c5 AM |
9693 | nothing else to do. However, if it is a forced local symbol or |
9694 | an ifunc symbol we need to give the backend finish_dynamic_symbol | |
9695 | function a chance to make it dynamic. */ | |
c152c796 AM |
9696 | if (strip |
9697 | && h->dynindx == -1 | |
b8871f35 | 9698 | && type != STT_GNU_IFUNC |
f5385ebf | 9699 | && !h->forced_local) |
c152c796 AM |
9700 | return TRUE; |
9701 | ||
9702 | sym.st_value = 0; | |
9703 | sym.st_size = h->size; | |
9704 | sym.st_other = h->other; | |
c152c796 AM |
9705 | switch (h->root.type) |
9706 | { | |
9707 | default: | |
9708 | case bfd_link_hash_new: | |
9709 | case bfd_link_hash_warning: | |
9710 | abort (); | |
9711 | return FALSE; | |
9712 | ||
9713 | case bfd_link_hash_undefined: | |
9714 | case bfd_link_hash_undefweak: | |
9715 | input_sec = bfd_und_section_ptr; | |
9716 | sym.st_shndx = SHN_UNDEF; | |
9717 | break; | |
9718 | ||
9719 | case bfd_link_hash_defined: | |
9720 | case bfd_link_hash_defweak: | |
9721 | { | |
9722 | input_sec = h->root.u.def.section; | |
9723 | if (input_sec->output_section != NULL) | |
9724 | { | |
9725 | sym.st_shndx = | |
8b127cbc | 9726 | _bfd_elf_section_from_bfd_section (flinfo->output_bfd, |
c152c796 AM |
9727 | input_sec->output_section); |
9728 | if (sym.st_shndx == SHN_BAD) | |
9729 | { | |
4eca0228 | 9730 | _bfd_error_handler |
695344c0 | 9731 | /* xgettext:c-format */ |
d003868e | 9732 | (_("%B: could not find output section %A for input section %A"), |
8b127cbc | 9733 | flinfo->output_bfd, input_sec->output_section, input_sec); |
17d078c5 | 9734 | bfd_set_error (bfd_error_nonrepresentable_section); |
c152c796 AM |
9735 | eoinfo->failed = TRUE; |
9736 | return FALSE; | |
9737 | } | |
9738 | ||
9739 | /* ELF symbols in relocatable files are section relative, | |
9740 | but in nonrelocatable files they are virtual | |
9741 | addresses. */ | |
9742 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
0e1862bb | 9743 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
9744 | { |
9745 | sym.st_value += input_sec->output_section->vma; | |
9746 | if (h->type == STT_TLS) | |
9747 | { | |
8b127cbc | 9748 | asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; |
430a16a5 NC |
9749 | if (tls_sec != NULL) |
9750 | sym.st_value -= tls_sec->vma; | |
c152c796 AM |
9751 | } |
9752 | } | |
9753 | } | |
9754 | else | |
9755 | { | |
9756 | BFD_ASSERT (input_sec->owner == NULL | |
9757 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
9758 | sym.st_shndx = SHN_UNDEF; | |
9759 | input_sec = bfd_und_section_ptr; | |
9760 | } | |
9761 | } | |
9762 | break; | |
9763 | ||
9764 | case bfd_link_hash_common: | |
9765 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 9766 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
9767 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
9768 | break; | |
9769 | ||
9770 | case bfd_link_hash_indirect: | |
9771 | /* These symbols are created by symbol versioning. They point | |
9772 | to the decorated version of the name. For example, if the | |
9773 | symbol foo@@GNU_1.2 is the default, which should be used when | |
9774 | foo is used with no version, then we add an indirect symbol | |
9775 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
9776 | since the indirected symbol is already in the hash table. */ | |
9777 | return TRUE; | |
9778 | } | |
9779 | ||
b8871f35 L |
9780 | if (type == STT_COMMON || type == STT_OBJECT) |
9781 | switch (h->root.type) | |
9782 | { | |
9783 | case bfd_link_hash_common: | |
9784 | type = elf_link_convert_common_type (flinfo->info, type); | |
9785 | break; | |
9786 | case bfd_link_hash_defined: | |
9787 | case bfd_link_hash_defweak: | |
9788 | if (bed->common_definition (&sym)) | |
9789 | type = elf_link_convert_common_type (flinfo->info, type); | |
9790 | else | |
9791 | type = STT_OBJECT; | |
9792 | break; | |
9793 | case bfd_link_hash_undefined: | |
9794 | case bfd_link_hash_undefweak: | |
9795 | break; | |
9796 | default: | |
9797 | abort (); | |
9798 | } | |
9799 | ||
4deb8f71 | 9800 | if (h->forced_local) |
b8871f35 L |
9801 | { |
9802 | sym.st_info = ELF_ST_INFO (STB_LOCAL, type); | |
9803 | /* Turn off visibility on local symbol. */ | |
9804 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); | |
9805 | } | |
9806 | /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ | |
9807 | else if (h->unique_global && h->def_regular) | |
9808 | sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); | |
9809 | else if (h->root.type == bfd_link_hash_undefweak | |
9810 | || h->root.type == bfd_link_hash_defweak) | |
9811 | sym.st_info = ELF_ST_INFO (STB_WEAK, type); | |
9812 | else | |
9813 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
9814 | sym.st_target_internal = h->target_internal; | |
9815 | ||
c152c796 AM |
9816 | /* Give the processor backend a chance to tweak the symbol value, |
9817 | and also to finish up anything that needs to be done for this | |
9818 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
3aa14d16 | 9819 | forced local syms when non-shared is due to a historical quirk. |
5f35ea9c | 9820 | STT_GNU_IFUNC symbol must go through PLT. */ |
3aa14d16 | 9821 | if ((h->type == STT_GNU_IFUNC |
5f35ea9c | 9822 | && h->def_regular |
0e1862bb | 9823 | && !bfd_link_relocatable (flinfo->info)) |
3aa14d16 L |
9824 | || ((h->dynindx != -1 |
9825 | || h->forced_local) | |
0e1862bb | 9826 | && ((bfd_link_pic (flinfo->info) |
3aa14d16 L |
9827 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
9828 | || h->root.type != bfd_link_hash_undefweak)) | |
9829 | || !h->forced_local) | |
8b127cbc | 9830 | && elf_hash_table (flinfo->info)->dynamic_sections_created)) |
c152c796 AM |
9831 | { |
9832 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8b127cbc | 9833 | (flinfo->output_bfd, flinfo->info, h, &sym))) |
c152c796 AM |
9834 | { |
9835 | eoinfo->failed = TRUE; | |
9836 | return FALSE; | |
9837 | } | |
9838 | } | |
9839 | ||
9840 | /* If we are marking the symbol as undefined, and there are no | |
9841 | non-weak references to this symbol from a regular object, then | |
9842 | mark the symbol as weak undefined; if there are non-weak | |
9843 | references, mark the symbol as strong. We can't do this earlier, | |
9844 | because it might not be marked as undefined until the | |
9845 | finish_dynamic_symbol routine gets through with it. */ | |
9846 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 9847 | && h->ref_regular |
c152c796 AM |
9848 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
9849 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
9850 | { | |
9851 | int bindtype; | |
b8871f35 | 9852 | type = ELF_ST_TYPE (sym.st_info); |
2955ec4c L |
9853 | |
9854 | /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ | |
9855 | if (type == STT_GNU_IFUNC) | |
9856 | type = STT_FUNC; | |
c152c796 | 9857 | |
f5385ebf | 9858 | if (h->ref_regular_nonweak) |
c152c796 AM |
9859 | bindtype = STB_GLOBAL; |
9860 | else | |
9861 | bindtype = STB_WEAK; | |
2955ec4c | 9862 | sym.st_info = ELF_ST_INFO (bindtype, type); |
c152c796 AM |
9863 | } |
9864 | ||
bda987c2 CD |
9865 | /* If this is a symbol defined in a dynamic library, don't use the |
9866 | symbol size from the dynamic library. Relinking an executable | |
9867 | against a new library may introduce gratuitous changes in the | |
9868 | executable's symbols if we keep the size. */ | |
9869 | if (sym.st_shndx == SHN_UNDEF | |
9870 | && !h->def_regular | |
9871 | && h->def_dynamic) | |
9872 | sym.st_size = 0; | |
9873 | ||
c152c796 AM |
9874 | /* If a non-weak symbol with non-default visibility is not defined |
9875 | locally, it is a fatal error. */ | |
0e1862bb | 9876 | if (!bfd_link_relocatable (flinfo->info) |
c152c796 AM |
9877 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT |
9878 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
9879 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 9880 | && !h->def_regular) |
c152c796 | 9881 | { |
17d078c5 AM |
9882 | const char *msg; |
9883 | ||
9884 | if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) | |
695344c0 | 9885 | /* xgettext:c-format */ |
17d078c5 AM |
9886 | msg = _("%B: protected symbol `%s' isn't defined"); |
9887 | else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) | |
695344c0 | 9888 | /* xgettext:c-format */ |
17d078c5 AM |
9889 | msg = _("%B: internal symbol `%s' isn't defined"); |
9890 | else | |
695344c0 | 9891 | /* xgettext:c-format */ |
17d078c5 | 9892 | msg = _("%B: hidden symbol `%s' isn't defined"); |
4eca0228 | 9893 | _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string); |
17d078c5 | 9894 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9895 | eoinfo->failed = TRUE; |
9896 | return FALSE; | |
9897 | } | |
9898 | ||
9899 | /* If this symbol should be put in the .dynsym section, then put it | |
9900 | there now. We already know the symbol index. We also fill in | |
9901 | the entry in the .hash section. */ | |
cae1fbbb | 9902 | if (elf_hash_table (flinfo->info)->dynsym != NULL |
202e2356 | 9903 | && h->dynindx != -1 |
8b127cbc | 9904 | && elf_hash_table (flinfo->info)->dynamic_sections_created) |
c152c796 | 9905 | { |
c152c796 AM |
9906 | bfd_byte *esym; |
9907 | ||
90c984fc L |
9908 | /* Since there is no version information in the dynamic string, |
9909 | if there is no version info in symbol version section, we will | |
1659f720 | 9910 | have a run-time problem if not linking executable, referenced |
4deb8f71 | 9911 | by shared library, or not bound locally. */ |
1659f720 | 9912 | if (h->verinfo.verdef == NULL |
0e1862bb | 9913 | && (!bfd_link_executable (flinfo->info) |
1659f720 L |
9914 | || h->ref_dynamic |
9915 | || !h->def_regular)) | |
90c984fc L |
9916 | { |
9917 | char *p = strrchr (h->root.root.string, ELF_VER_CHR); | |
9918 | ||
9919 | if (p && p [1] != '\0') | |
9920 | { | |
4eca0228 | 9921 | _bfd_error_handler |
695344c0 | 9922 | /* xgettext:c-format */ |
90c984fc L |
9923 | (_("%B: No symbol version section for versioned symbol `%s'"), |
9924 | flinfo->output_bfd, h->root.root.string); | |
9925 | eoinfo->failed = TRUE; | |
9926 | return FALSE; | |
9927 | } | |
9928 | } | |
9929 | ||
c152c796 | 9930 | sym.st_name = h->dynstr_index; |
cae1fbbb L |
9931 | esym = (elf_hash_table (flinfo->info)->dynsym->contents |
9932 | + h->dynindx * bed->s->sizeof_sym); | |
8b127cbc | 9933 | if (!check_dynsym (flinfo->output_bfd, &sym)) |
c0d5a53d L |
9934 | { |
9935 | eoinfo->failed = TRUE; | |
9936 | return FALSE; | |
9937 | } | |
8b127cbc | 9938 | bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); |
c152c796 | 9939 | |
8b127cbc | 9940 | if (flinfo->hash_sec != NULL) |
fdc90cb4 JJ |
9941 | { |
9942 | size_t hash_entry_size; | |
9943 | bfd_byte *bucketpos; | |
9944 | bfd_vma chain; | |
41198d0c L |
9945 | size_t bucketcount; |
9946 | size_t bucket; | |
9947 | ||
8b127cbc | 9948 | bucketcount = elf_hash_table (flinfo->info)->bucketcount; |
41198d0c | 9949 | bucket = h->u.elf_hash_value % bucketcount; |
fdc90cb4 JJ |
9950 | |
9951 | hash_entry_size | |
8b127cbc AM |
9952 | = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; |
9953 | bucketpos = ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 | 9954 | + (bucket + 2) * hash_entry_size); |
8b127cbc AM |
9955 | chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); |
9956 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, | |
9957 | bucketpos); | |
9958 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, | |
9959 | ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 JJ |
9960 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); |
9961 | } | |
c152c796 | 9962 | |
8b127cbc | 9963 | if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) |
c152c796 AM |
9964 | { |
9965 | Elf_Internal_Versym iversym; | |
9966 | Elf_External_Versym *eversym; | |
9967 | ||
f5385ebf | 9968 | if (!h->def_regular) |
c152c796 | 9969 | { |
7b20f099 AM |
9970 | if (h->verinfo.verdef == NULL |
9971 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
9972 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
c152c796 AM |
9973 | iversym.vs_vers = 0; |
9974 | else | |
9975 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
9976 | } | |
9977 | else | |
9978 | { | |
9979 | if (h->verinfo.vertree == NULL) | |
9980 | iversym.vs_vers = 1; | |
9981 | else | |
9982 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
8b127cbc | 9983 | if (flinfo->info->create_default_symver) |
3e3b46e5 | 9984 | iversym.vs_vers++; |
c152c796 AM |
9985 | } |
9986 | ||
422f1182 | 9987 | /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is |
6e33951e | 9988 | defined locally. */ |
422f1182 | 9989 | if (h->versioned == versioned_hidden && h->def_regular) |
c152c796 AM |
9990 | iversym.vs_vers |= VERSYM_HIDDEN; |
9991 | ||
8b127cbc | 9992 | eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; |
c152c796 | 9993 | eversym += h->dynindx; |
8b127cbc | 9994 | _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); |
c152c796 AM |
9995 | } |
9996 | } | |
9997 | ||
d983c8c5 AM |
9998 | /* If the symbol is undefined, and we didn't output it to .dynsym, |
9999 | strip it from .symtab too. Obviously we can't do this for | |
10000 | relocatable output or when needed for --emit-relocs. */ | |
10001 | else if (input_sec == bfd_und_section_ptr | |
10002 | && h->indx != -2 | |
66cae560 NC |
10003 | /* PR 22319 Do not strip global undefined symbols marked as being needed. */ |
10004 | && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL) | |
0e1862bb | 10005 | && !bfd_link_relocatable (flinfo->info)) |
d983c8c5 | 10006 | return TRUE; |
66cae560 | 10007 | |
d983c8c5 AM |
10008 | /* Also strip others that we couldn't earlier due to dynamic symbol |
10009 | processing. */ | |
10010 | if (strip) | |
10011 | return TRUE; | |
10012 | if ((input_sec->flags & SEC_EXCLUDE) != 0) | |
c152c796 AM |
10013 | return TRUE; |
10014 | ||
2ec55de3 AM |
10015 | /* Output a FILE symbol so that following locals are not associated |
10016 | with the wrong input file. We need one for forced local symbols | |
10017 | if we've seen more than one FILE symbol or when we have exactly | |
10018 | one FILE symbol but global symbols are present in a file other | |
10019 | than the one with the FILE symbol. We also need one if linker | |
10020 | defined symbols are present. In practice these conditions are | |
10021 | always met, so just emit the FILE symbol unconditionally. */ | |
10022 | if (eoinfo->localsyms | |
10023 | && !eoinfo->file_sym_done | |
10024 | && eoinfo->flinfo->filesym_count != 0) | |
10025 | { | |
10026 | Elf_Internal_Sym fsym; | |
10027 | ||
10028 | memset (&fsym, 0, sizeof (fsym)); | |
10029 | fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10030 | fsym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10031 | if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, |
10032 | bfd_und_section_ptr, NULL)) | |
2ec55de3 AM |
10033 | return FALSE; |
10034 | ||
10035 | eoinfo->file_sym_done = TRUE; | |
10036 | } | |
10037 | ||
8b127cbc | 10038 | indx = bfd_get_symcount (flinfo->output_bfd); |
ef10c3ac L |
10039 | ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, |
10040 | input_sec, h); | |
6e0b88f1 | 10041 | if (ret == 0) |
c152c796 AM |
10042 | { |
10043 | eoinfo->failed = TRUE; | |
10044 | return FALSE; | |
10045 | } | |
6e0b88f1 AM |
10046 | else if (ret == 1) |
10047 | h->indx = indx; | |
10048 | else if (h->indx == -2) | |
10049 | abort(); | |
c152c796 AM |
10050 | |
10051 | return TRUE; | |
10052 | } | |
10053 | ||
cdd3575c AM |
10054 | /* Return TRUE if special handling is done for relocs in SEC against |
10055 | symbols defined in discarded sections. */ | |
10056 | ||
c152c796 AM |
10057 | static bfd_boolean |
10058 | elf_section_ignore_discarded_relocs (asection *sec) | |
10059 | { | |
10060 | const struct elf_backend_data *bed; | |
10061 | ||
cdd3575c AM |
10062 | switch (sec->sec_info_type) |
10063 | { | |
dbaa2011 AM |
10064 | case SEC_INFO_TYPE_STABS: |
10065 | case SEC_INFO_TYPE_EH_FRAME: | |
2f0c68f2 | 10066 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
cdd3575c AM |
10067 | return TRUE; |
10068 | default: | |
10069 | break; | |
10070 | } | |
c152c796 AM |
10071 | |
10072 | bed = get_elf_backend_data (sec->owner); | |
10073 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
10074 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
10075 | return TRUE; | |
10076 | ||
10077 | return FALSE; | |
10078 | } | |
10079 | ||
9e66c942 AM |
10080 | /* Return a mask saying how ld should treat relocations in SEC against |
10081 | symbols defined in discarded sections. If this function returns | |
10082 | COMPLAIN set, ld will issue a warning message. If this function | |
10083 | returns PRETEND set, and the discarded section was link-once and the | |
10084 | same size as the kept link-once section, ld will pretend that the | |
10085 | symbol was actually defined in the kept section. Otherwise ld will | |
10086 | zero the reloc (at least that is the intent, but some cooperation by | |
10087 | the target dependent code is needed, particularly for REL targets). */ | |
10088 | ||
8a696751 AM |
10089 | unsigned int |
10090 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 10091 | { |
9e66c942 | 10092 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 10093 | return PRETEND; |
cdd3575c AM |
10094 | |
10095 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 10096 | return 0; |
cdd3575c AM |
10097 | |
10098 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 10099 | return 0; |
cdd3575c | 10100 | |
9e66c942 | 10101 | return COMPLAIN | PRETEND; |
cdd3575c AM |
10102 | } |
10103 | ||
3d7f7666 L |
10104 | /* Find a match between a section and a member of a section group. */ |
10105 | ||
10106 | static asection * | |
c0f00686 L |
10107 | match_group_member (asection *sec, asection *group, |
10108 | struct bfd_link_info *info) | |
3d7f7666 L |
10109 | { |
10110 | asection *first = elf_next_in_group (group); | |
10111 | asection *s = first; | |
10112 | ||
10113 | while (s != NULL) | |
10114 | { | |
c0f00686 | 10115 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
10116 | return s; |
10117 | ||
83180ade | 10118 | s = elf_next_in_group (s); |
3d7f7666 L |
10119 | if (s == first) |
10120 | break; | |
10121 | } | |
10122 | ||
10123 | return NULL; | |
10124 | } | |
10125 | ||
01b3c8ab | 10126 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
10127 | to replace it. Return the replacement if it is OK. Otherwise return |
10128 | NULL. */ | |
01b3c8ab L |
10129 | |
10130 | asection * | |
c0f00686 | 10131 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
10132 | { |
10133 | asection *kept; | |
10134 | ||
10135 | kept = sec->kept_section; | |
10136 | if (kept != NULL) | |
10137 | { | |
c2370991 | 10138 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 10139 | kept = match_group_member (sec, kept, info); |
1dd2625f BW |
10140 | if (kept != NULL |
10141 | && ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
10142 | != (kept->rawsize != 0 ? kept->rawsize : kept->size))) | |
01b3c8ab | 10143 | kept = NULL; |
c2370991 | 10144 | sec->kept_section = kept; |
01b3c8ab L |
10145 | } |
10146 | return kept; | |
10147 | } | |
10148 | ||
c152c796 AM |
10149 | /* Link an input file into the linker output file. This function |
10150 | handles all the sections and relocations of the input file at once. | |
10151 | This is so that we only have to read the local symbols once, and | |
10152 | don't have to keep them in memory. */ | |
10153 | ||
10154 | static bfd_boolean | |
8b127cbc | 10155 | elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) |
c152c796 | 10156 | { |
ece5ef60 | 10157 | int (*relocate_section) |
c152c796 AM |
10158 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
10159 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
10160 | bfd *output_bfd; | |
10161 | Elf_Internal_Shdr *symtab_hdr; | |
10162 | size_t locsymcount; | |
10163 | size_t extsymoff; | |
10164 | Elf_Internal_Sym *isymbuf; | |
10165 | Elf_Internal_Sym *isym; | |
10166 | Elf_Internal_Sym *isymend; | |
10167 | long *pindex; | |
10168 | asection **ppsection; | |
10169 | asection *o; | |
10170 | const struct elf_backend_data *bed; | |
c152c796 | 10171 | struct elf_link_hash_entry **sym_hashes; |
310fd250 L |
10172 | bfd_size_type address_size; |
10173 | bfd_vma r_type_mask; | |
10174 | int r_sym_shift; | |
ffbc01cc | 10175 | bfd_boolean have_file_sym = FALSE; |
c152c796 | 10176 | |
8b127cbc | 10177 | output_bfd = flinfo->output_bfd; |
c152c796 AM |
10178 | bed = get_elf_backend_data (output_bfd); |
10179 | relocate_section = bed->elf_backend_relocate_section; | |
10180 | ||
10181 | /* If this is a dynamic object, we don't want to do anything here: | |
10182 | we don't want the local symbols, and we don't want the section | |
10183 | contents. */ | |
10184 | if ((input_bfd->flags & DYNAMIC) != 0) | |
10185 | return TRUE; | |
10186 | ||
c152c796 AM |
10187 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
10188 | if (elf_bad_symtab (input_bfd)) | |
10189 | { | |
10190 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10191 | extsymoff = 0; | |
10192 | } | |
10193 | else | |
10194 | { | |
10195 | locsymcount = symtab_hdr->sh_info; | |
10196 | extsymoff = symtab_hdr->sh_info; | |
10197 | } | |
10198 | ||
10199 | /* Read the local symbols. */ | |
10200 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
10201 | if (isymbuf == NULL && locsymcount != 0) | |
10202 | { | |
10203 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8b127cbc AM |
10204 | flinfo->internal_syms, |
10205 | flinfo->external_syms, | |
10206 | flinfo->locsym_shndx); | |
c152c796 AM |
10207 | if (isymbuf == NULL) |
10208 | return FALSE; | |
10209 | } | |
10210 | ||
10211 | /* Find local symbol sections and adjust values of symbols in | |
10212 | SEC_MERGE sections. Write out those local symbols we know are | |
10213 | going into the output file. */ | |
10214 | isymend = isymbuf + locsymcount; | |
8b127cbc | 10215 | for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; |
c152c796 AM |
10216 | isym < isymend; |
10217 | isym++, pindex++, ppsection++) | |
10218 | { | |
10219 | asection *isec; | |
10220 | const char *name; | |
10221 | Elf_Internal_Sym osym; | |
6e0b88f1 AM |
10222 | long indx; |
10223 | int ret; | |
c152c796 AM |
10224 | |
10225 | *pindex = -1; | |
10226 | ||
10227 | if (elf_bad_symtab (input_bfd)) | |
10228 | { | |
10229 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
10230 | { | |
10231 | *ppsection = NULL; | |
10232 | continue; | |
10233 | } | |
10234 | } | |
10235 | ||
10236 | if (isym->st_shndx == SHN_UNDEF) | |
10237 | isec = bfd_und_section_ptr; | |
c152c796 AM |
10238 | else if (isym->st_shndx == SHN_ABS) |
10239 | isec = bfd_abs_section_ptr; | |
10240 | else if (isym->st_shndx == SHN_COMMON) | |
10241 | isec = bfd_com_section_ptr; | |
10242 | else | |
10243 | { | |
cb33740c AM |
10244 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
10245 | if (isec == NULL) | |
10246 | { | |
10247 | /* Don't attempt to output symbols with st_shnx in the | |
10248 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
10249 | *ppsection = NULL; | |
10250 | continue; | |
10251 | } | |
dbaa2011 | 10252 | else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE |
cb33740c AM |
10253 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) |
10254 | isym->st_value = | |
10255 | _bfd_merged_section_offset (output_bfd, &isec, | |
10256 | elf_section_data (isec)->sec_info, | |
10257 | isym->st_value); | |
c152c796 AM |
10258 | } |
10259 | ||
10260 | *ppsection = isec; | |
10261 | ||
d983c8c5 AM |
10262 | /* Don't output the first, undefined, symbol. In fact, don't |
10263 | output any undefined local symbol. */ | |
10264 | if (isec == bfd_und_section_ptr) | |
c152c796 AM |
10265 | continue; |
10266 | ||
10267 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
10268 | { | |
10269 | /* We never output section symbols. Instead, we use the | |
10270 | section symbol of the corresponding section in the output | |
10271 | file. */ | |
10272 | continue; | |
10273 | } | |
10274 | ||
10275 | /* If we are stripping all symbols, we don't want to output this | |
10276 | one. */ | |
8b127cbc | 10277 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
10278 | continue; |
10279 | ||
10280 | /* If we are discarding all local symbols, we don't want to | |
10281 | output this one. If we are generating a relocatable output | |
10282 | file, then some of the local symbols may be required by | |
10283 | relocs; we output them below as we discover that they are | |
10284 | needed. */ | |
8b127cbc | 10285 | if (flinfo->info->discard == discard_all) |
c152c796 AM |
10286 | continue; |
10287 | ||
10288 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
10289 | discarding, we don't need to keep it. */ |
10290 | if (isym->st_shndx != SHN_UNDEF | |
4fbb74a6 AM |
10291 | && isym->st_shndx < SHN_LORESERVE |
10292 | && bfd_section_removed_from_list (output_bfd, | |
10293 | isec->output_section)) | |
e75a280b L |
10294 | continue; |
10295 | ||
c152c796 AM |
10296 | /* Get the name of the symbol. */ |
10297 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
10298 | isym->st_name); | |
10299 | if (name == NULL) | |
10300 | return FALSE; | |
10301 | ||
10302 | /* See if we are discarding symbols with this name. */ | |
8b127cbc AM |
10303 | if ((flinfo->info->strip == strip_some |
10304 | && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) | |
c152c796 | 10305 | == NULL)) |
8b127cbc | 10306 | || (((flinfo->info->discard == discard_sec_merge |
0e1862bb L |
10307 | && (isec->flags & SEC_MERGE) |
10308 | && !bfd_link_relocatable (flinfo->info)) | |
8b127cbc | 10309 | || flinfo->info->discard == discard_l) |
c152c796 AM |
10310 | && bfd_is_local_label_name (input_bfd, name))) |
10311 | continue; | |
10312 | ||
ffbc01cc AM |
10313 | if (ELF_ST_TYPE (isym->st_info) == STT_FILE) |
10314 | { | |
ce875075 AM |
10315 | if (input_bfd->lto_output) |
10316 | /* -flto puts a temp file name here. This means builds | |
10317 | are not reproducible. Discard the symbol. */ | |
10318 | continue; | |
ffbc01cc AM |
10319 | have_file_sym = TRUE; |
10320 | flinfo->filesym_count += 1; | |
10321 | } | |
10322 | if (!have_file_sym) | |
10323 | { | |
10324 | /* In the absence of debug info, bfd_find_nearest_line uses | |
10325 | FILE symbols to determine the source file for local | |
10326 | function symbols. Provide a FILE symbol here if input | |
10327 | files lack such, so that their symbols won't be | |
10328 | associated with a previous input file. It's not the | |
10329 | source file, but the best we can do. */ | |
10330 | have_file_sym = TRUE; | |
10331 | flinfo->filesym_count += 1; | |
10332 | memset (&osym, 0, sizeof (osym)); | |
10333 | osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10334 | osym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10335 | if (!elf_link_output_symstrtab (flinfo, |
10336 | (input_bfd->lto_output ? NULL | |
10337 | : input_bfd->filename), | |
10338 | &osym, bfd_abs_section_ptr, | |
10339 | NULL)) | |
ffbc01cc AM |
10340 | return FALSE; |
10341 | } | |
10342 | ||
c152c796 AM |
10343 | osym = *isym; |
10344 | ||
10345 | /* Adjust the section index for the output file. */ | |
10346 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10347 | isec->output_section); | |
10348 | if (osym.st_shndx == SHN_BAD) | |
10349 | return FALSE; | |
10350 | ||
c152c796 AM |
10351 | /* ELF symbols in relocatable files are section relative, but |
10352 | in executable files they are virtual addresses. Note that | |
10353 | this code assumes that all ELF sections have an associated | |
10354 | BFD section with a reasonable value for output_offset; below | |
10355 | we assume that they also have a reasonable value for | |
10356 | output_section. Any special sections must be set up to meet | |
10357 | these requirements. */ | |
10358 | osym.st_value += isec->output_offset; | |
0e1862bb | 10359 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10360 | { |
10361 | osym.st_value += isec->output_section->vma; | |
10362 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
10363 | { | |
10364 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
8b127cbc AM |
10365 | BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL); |
10366 | osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; | |
c152c796 AM |
10367 | } |
10368 | } | |
10369 | ||
6e0b88f1 | 10370 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac | 10371 | ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); |
6e0b88f1 | 10372 | if (ret == 0) |
c152c796 | 10373 | return FALSE; |
6e0b88f1 AM |
10374 | else if (ret == 1) |
10375 | *pindex = indx; | |
c152c796 AM |
10376 | } |
10377 | ||
310fd250 L |
10378 | if (bed->s->arch_size == 32) |
10379 | { | |
10380 | r_type_mask = 0xff; | |
10381 | r_sym_shift = 8; | |
10382 | address_size = 4; | |
10383 | } | |
10384 | else | |
10385 | { | |
10386 | r_type_mask = 0xffffffff; | |
10387 | r_sym_shift = 32; | |
10388 | address_size = 8; | |
10389 | } | |
10390 | ||
c152c796 AM |
10391 | /* Relocate the contents of each section. */ |
10392 | sym_hashes = elf_sym_hashes (input_bfd); | |
10393 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
10394 | { | |
10395 | bfd_byte *contents; | |
10396 | ||
10397 | if (! o->linker_mark) | |
10398 | { | |
10399 | /* This section was omitted from the link. */ | |
10400 | continue; | |
10401 | } | |
10402 | ||
7bdf4127 | 10403 | if (!flinfo->info->resolve_section_groups |
bcacc0f5 AM |
10404 | && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) |
10405 | { | |
10406 | /* Deal with the group signature symbol. */ | |
10407 | struct bfd_elf_section_data *sec_data = elf_section_data (o); | |
10408 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
10409 | asection *osec = o->output_section; | |
10410 | ||
7bdf4127 | 10411 | BFD_ASSERT (bfd_link_relocatable (flinfo->info)); |
bcacc0f5 AM |
10412 | if (symndx >= locsymcount |
10413 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10414 | && flinfo->sections[symndx] == NULL)) |
bcacc0f5 AM |
10415 | { |
10416 | struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; | |
10417 | while (h->root.type == bfd_link_hash_indirect | |
10418 | || h->root.type == bfd_link_hash_warning) | |
10419 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10420 | /* Arrange for symbol to be output. */ | |
10421 | h->indx = -2; | |
10422 | elf_section_data (osec)->this_hdr.sh_info = -2; | |
10423 | } | |
10424 | else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) | |
10425 | { | |
10426 | /* We'll use the output section target_index. */ | |
8b127cbc | 10427 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 AM |
10428 | elf_section_data (osec)->this_hdr.sh_info = sec->target_index; |
10429 | } | |
10430 | else | |
10431 | { | |
8b127cbc | 10432 | if (flinfo->indices[symndx] == -1) |
bcacc0f5 AM |
10433 | { |
10434 | /* Otherwise output the local symbol now. */ | |
10435 | Elf_Internal_Sym sym = isymbuf[symndx]; | |
8b127cbc | 10436 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 | 10437 | const char *name; |
6e0b88f1 AM |
10438 | long indx; |
10439 | int ret; | |
bcacc0f5 AM |
10440 | |
10441 | name = bfd_elf_string_from_elf_section (input_bfd, | |
10442 | symtab_hdr->sh_link, | |
10443 | sym.st_name); | |
10444 | if (name == NULL) | |
10445 | return FALSE; | |
10446 | ||
10447 | sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10448 | sec); | |
10449 | if (sym.st_shndx == SHN_BAD) | |
10450 | return FALSE; | |
10451 | ||
10452 | sym.st_value += o->output_offset; | |
10453 | ||
6e0b88f1 | 10454 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10455 | ret = elf_link_output_symstrtab (flinfo, name, &sym, o, |
10456 | NULL); | |
6e0b88f1 | 10457 | if (ret == 0) |
bcacc0f5 | 10458 | return FALSE; |
6e0b88f1 | 10459 | else if (ret == 1) |
8b127cbc | 10460 | flinfo->indices[symndx] = indx; |
6e0b88f1 AM |
10461 | else |
10462 | abort (); | |
bcacc0f5 AM |
10463 | } |
10464 | elf_section_data (osec)->this_hdr.sh_info | |
8b127cbc | 10465 | = flinfo->indices[symndx]; |
bcacc0f5 AM |
10466 | } |
10467 | } | |
10468 | ||
c152c796 | 10469 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 10470 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
10471 | continue; |
10472 | ||
10473 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
10474 | { | |
10475 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
10476 | or somesuch. */ | |
10477 | continue; | |
10478 | } | |
10479 | ||
10480 | /* Get the contents of the section. They have been cached by a | |
10481 | relaxation routine. Note that o is a section in an input | |
10482 | file, so the contents field will not have been set by any of | |
10483 | the routines which work on output files. */ | |
10484 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
53291d1f AM |
10485 | { |
10486 | contents = elf_section_data (o)->this_hdr.contents; | |
10487 | if (bed->caches_rawsize | |
10488 | && o->rawsize != 0 | |
10489 | && o->rawsize < o->size) | |
10490 | { | |
10491 | memcpy (flinfo->contents, contents, o->rawsize); | |
10492 | contents = flinfo->contents; | |
10493 | } | |
10494 | } | |
c152c796 AM |
10495 | else |
10496 | { | |
8b127cbc | 10497 | contents = flinfo->contents; |
4a114e3e | 10498 | if (! bfd_get_full_section_contents (input_bfd, o, &contents)) |
c152c796 AM |
10499 | return FALSE; |
10500 | } | |
10501 | ||
10502 | if ((o->flags & SEC_RELOC) != 0) | |
10503 | { | |
10504 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 10505 | Elf_Internal_Rela *rel, *relend; |
0f02bbd9 | 10506 | int action_discarded; |
ece5ef60 | 10507 | int ret; |
c152c796 AM |
10508 | |
10509 | /* Get the swapped relocs. */ | |
10510 | internal_relocs | |
8b127cbc AM |
10511 | = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, |
10512 | flinfo->internal_relocs, FALSE); | |
c152c796 AM |
10513 | if (internal_relocs == NULL |
10514 | && o->reloc_count > 0) | |
10515 | return FALSE; | |
10516 | ||
310fd250 L |
10517 | /* We need to reverse-copy input .ctors/.dtors sections if |
10518 | they are placed in .init_array/.finit_array for output. */ | |
10519 | if (o->size > address_size | |
10520 | && ((strncmp (o->name, ".ctors", 6) == 0 | |
10521 | && strcmp (o->output_section->name, | |
10522 | ".init_array") == 0) | |
10523 | || (strncmp (o->name, ".dtors", 6) == 0 | |
10524 | && strcmp (o->output_section->name, | |
10525 | ".fini_array") == 0)) | |
10526 | && (o->name[6] == 0 || o->name[6] == '.')) | |
c152c796 | 10527 | { |
056bafd4 MR |
10528 | if (o->size * bed->s->int_rels_per_ext_rel |
10529 | != o->reloc_count * address_size) | |
310fd250 | 10530 | { |
4eca0228 | 10531 | _bfd_error_handler |
695344c0 | 10532 | /* xgettext:c-format */ |
310fd250 L |
10533 | (_("error: %B: size of section %A is not " |
10534 | "multiple of address size"), | |
10535 | input_bfd, o); | |
8c6716e5 | 10536 | bfd_set_error (bfd_error_bad_value); |
310fd250 L |
10537 | return FALSE; |
10538 | } | |
10539 | o->flags |= SEC_ELF_REVERSE_COPY; | |
c152c796 AM |
10540 | } |
10541 | ||
0f02bbd9 | 10542 | action_discarded = -1; |
c152c796 | 10543 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
10544 | action_discarded = (*bed->action_discarded) (o); |
10545 | ||
10546 | /* Run through the relocs evaluating complex reloc symbols and | |
10547 | looking for relocs against symbols from discarded sections | |
10548 | or section symbols from removed link-once sections. | |
10549 | Complain about relocs against discarded sections. Zero | |
10550 | relocs against removed link-once sections. */ | |
10551 | ||
10552 | rel = internal_relocs; | |
056bafd4 | 10553 | relend = rel + o->reloc_count; |
0f02bbd9 | 10554 | for ( ; rel < relend; rel++) |
c152c796 | 10555 | { |
0f02bbd9 AM |
10556 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
10557 | unsigned int s_type; | |
10558 | asection **ps, *sec; | |
10559 | struct elf_link_hash_entry *h = NULL; | |
10560 | const char *sym_name; | |
c152c796 | 10561 | |
0f02bbd9 AM |
10562 | if (r_symndx == STN_UNDEF) |
10563 | continue; | |
c152c796 | 10564 | |
0f02bbd9 AM |
10565 | if (r_symndx >= locsymcount |
10566 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10567 | && flinfo->sections[r_symndx] == NULL)) |
0f02bbd9 AM |
10568 | { |
10569 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 10570 | |
0f02bbd9 AM |
10571 | /* Badly formatted input files can contain relocs that |
10572 | reference non-existant symbols. Check here so that | |
10573 | we do not seg fault. */ | |
10574 | if (h == NULL) | |
c152c796 | 10575 | { |
4eca0228 | 10576 | _bfd_error_handler |
695344c0 | 10577 | /* xgettext:c-format */ |
76cfced5 | 10578 | (_("error: %B contains a reloc (%#Lx) for section %A " |
0f02bbd9 | 10579 | "that references a non-existent global symbol"), |
76cfced5 | 10580 | input_bfd, rel->r_info, o); |
0f02bbd9 AM |
10581 | bfd_set_error (bfd_error_bad_value); |
10582 | return FALSE; | |
10583 | } | |
3b36f7e6 | 10584 | |
0f02bbd9 AM |
10585 | while (h->root.type == bfd_link_hash_indirect |
10586 | || h->root.type == bfd_link_hash_warning) | |
10587 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 10588 | |
0f02bbd9 | 10589 | s_type = h->type; |
cdd3575c | 10590 | |
9e2dec47 | 10591 | /* If a plugin symbol is referenced from a non-IR file, |
ca4be51c AM |
10592 | mark the symbol as undefined. Note that the |
10593 | linker may attach linker created dynamic sections | |
10594 | to the plugin bfd. Symbols defined in linker | |
10595 | created sections are not plugin symbols. */ | |
bc4e12de | 10596 | if ((h->root.non_ir_ref_regular |
4070765b | 10597 | || h->root.non_ir_ref_dynamic) |
9e2dec47 L |
10598 | && (h->root.type == bfd_link_hash_defined |
10599 | || h->root.type == bfd_link_hash_defweak) | |
10600 | && (h->root.u.def.section->flags | |
10601 | & SEC_LINKER_CREATED) == 0 | |
10602 | && h->root.u.def.section->owner != NULL | |
10603 | && (h->root.u.def.section->owner->flags | |
10604 | & BFD_PLUGIN) != 0) | |
10605 | { | |
10606 | h->root.type = bfd_link_hash_undefined; | |
10607 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
10608 | } | |
10609 | ||
0f02bbd9 AM |
10610 | ps = NULL; |
10611 | if (h->root.type == bfd_link_hash_defined | |
10612 | || h->root.type == bfd_link_hash_defweak) | |
10613 | ps = &h->root.u.def.section; | |
10614 | ||
10615 | sym_name = h->root.root.string; | |
10616 | } | |
10617 | else | |
10618 | { | |
10619 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
10620 | ||
10621 | s_type = ELF_ST_TYPE (sym->st_info); | |
8b127cbc | 10622 | ps = &flinfo->sections[r_symndx]; |
0f02bbd9 AM |
10623 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, |
10624 | sym, *ps); | |
10625 | } | |
c152c796 | 10626 | |
c301e700 | 10627 | if ((s_type == STT_RELC || s_type == STT_SRELC) |
0e1862bb | 10628 | && !bfd_link_relocatable (flinfo->info)) |
0f02bbd9 AM |
10629 | { |
10630 | bfd_vma val; | |
10631 | bfd_vma dot = (rel->r_offset | |
10632 | + o->output_offset + o->output_section->vma); | |
10633 | #ifdef DEBUG | |
10634 | printf ("Encountered a complex symbol!"); | |
10635 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
9ccb8af9 AM |
10636 | input_bfd->filename, o->name, |
10637 | (long) (rel - internal_relocs)); | |
0f02bbd9 AM |
10638 | printf (" symbol: idx %8.8lx, name %s\n", |
10639 | r_symndx, sym_name); | |
10640 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
10641 | (unsigned long) rel->r_info, | |
10642 | (unsigned long) rel->r_offset); | |
10643 | #endif | |
8b127cbc | 10644 | if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, |
0f02bbd9 AM |
10645 | isymbuf, locsymcount, s_type == STT_SRELC)) |
10646 | return FALSE; | |
10647 | ||
10648 | /* Symbol evaluated OK. Update to absolute value. */ | |
10649 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
10650 | r_symndx, val); | |
10651 | continue; | |
10652 | } | |
10653 | ||
10654 | if (action_discarded != -1 && ps != NULL) | |
10655 | { | |
cdd3575c AM |
10656 | /* Complain if the definition comes from a |
10657 | discarded section. */ | |
dbaa2011 | 10658 | if ((sec = *ps) != NULL && discarded_section (sec)) |
cdd3575c | 10659 | { |
cf35638d | 10660 | BFD_ASSERT (r_symndx != STN_UNDEF); |
0f02bbd9 | 10661 | if (action_discarded & COMPLAIN) |
8b127cbc | 10662 | (*flinfo->info->callbacks->einfo) |
695344c0 | 10663 | /* xgettext:c-format */ |
e1fffbe6 | 10664 | (_("%X`%s' referenced in section `%A' of %B: " |
58ac56d0 | 10665 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 10666 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 10667 | |
87e5235d | 10668 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 10669 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
10670 | really defined in the kept linkonce section. |
10671 | FIXME: This is quite broken. Modifying the | |
10672 | symbol here means we will be changing all later | |
e0ae6d6f | 10673 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 10674 | if (action_discarded & PRETEND) |
87e5235d | 10675 | { |
01b3c8ab L |
10676 | asection *kept; |
10677 | ||
c0f00686 | 10678 | kept = _bfd_elf_check_kept_section (sec, |
8b127cbc | 10679 | flinfo->info); |
01b3c8ab | 10680 | if (kept != NULL) |
87e5235d AM |
10681 | { |
10682 | *ps = kept; | |
10683 | continue; | |
10684 | } | |
10685 | } | |
c152c796 AM |
10686 | } |
10687 | } | |
10688 | } | |
10689 | ||
10690 | /* Relocate the section by invoking a back end routine. | |
10691 | ||
10692 | The back end routine is responsible for adjusting the | |
10693 | section contents as necessary, and (if using Rela relocs | |
10694 | and generating a relocatable output file) adjusting the | |
10695 | reloc addend as necessary. | |
10696 | ||
10697 | The back end routine does not have to worry about setting | |
10698 | the reloc address or the reloc symbol index. | |
10699 | ||
10700 | The back end routine is given a pointer to the swapped in | |
10701 | internal symbols, and can access the hash table entries | |
10702 | for the external symbols via elf_sym_hashes (input_bfd). | |
10703 | ||
10704 | When generating relocatable output, the back end routine | |
10705 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
10706 | output symbol is going to be a section symbol | |
10707 | corresponding to the output section, which will require | |
10708 | the addend to be adjusted. */ | |
10709 | ||
8b127cbc | 10710 | ret = (*relocate_section) (output_bfd, flinfo->info, |
c152c796 AM |
10711 | input_bfd, o, contents, |
10712 | internal_relocs, | |
10713 | isymbuf, | |
8b127cbc | 10714 | flinfo->sections); |
ece5ef60 | 10715 | if (!ret) |
c152c796 AM |
10716 | return FALSE; |
10717 | ||
ece5ef60 | 10718 | if (ret == 2 |
0e1862bb | 10719 | || bfd_link_relocatable (flinfo->info) |
8b127cbc | 10720 | || flinfo->info->emitrelocations) |
c152c796 AM |
10721 | { |
10722 | Elf_Internal_Rela *irela; | |
d4730f92 | 10723 | Elf_Internal_Rela *irelaend, *irelamid; |
c152c796 AM |
10724 | bfd_vma last_offset; |
10725 | struct elf_link_hash_entry **rel_hash; | |
d4730f92 BS |
10726 | struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; |
10727 | Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; | |
c152c796 | 10728 | unsigned int next_erel; |
c152c796 | 10729 | bfd_boolean rela_normal; |
d4730f92 | 10730 | struct bfd_elf_section_data *esdi, *esdo; |
c152c796 | 10731 | |
d4730f92 BS |
10732 | esdi = elf_section_data (o); |
10733 | esdo = elf_section_data (o->output_section); | |
10734 | rela_normal = FALSE; | |
c152c796 AM |
10735 | |
10736 | /* Adjust the reloc addresses and symbol indices. */ | |
10737 | ||
10738 | irela = internal_relocs; | |
056bafd4 | 10739 | irelaend = irela + o->reloc_count; |
d4730f92 BS |
10740 | rel_hash = esdo->rel.hashes + esdo->rel.count; |
10741 | /* We start processing the REL relocs, if any. When we reach | |
10742 | IRELAMID in the loop, we switch to the RELA relocs. */ | |
10743 | irelamid = irela; | |
10744 | if (esdi->rel.hdr != NULL) | |
10745 | irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) | |
10746 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10747 | rel_hash_list = rel_hash; |
d4730f92 | 10748 | rela_hash_list = NULL; |
c152c796 | 10749 | last_offset = o->output_offset; |
0e1862bb | 10750 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10751 | last_offset += o->output_section->vma; |
10752 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
10753 | { | |
10754 | unsigned long r_symndx; | |
10755 | asection *sec; | |
10756 | Elf_Internal_Sym sym; | |
10757 | ||
10758 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
10759 | { | |
10760 | rel_hash++; | |
10761 | next_erel = 0; | |
10762 | } | |
10763 | ||
d4730f92 BS |
10764 | if (irela == irelamid) |
10765 | { | |
10766 | rel_hash = esdo->rela.hashes + esdo->rela.count; | |
10767 | rela_hash_list = rel_hash; | |
10768 | rela_normal = bed->rela_normal; | |
10769 | } | |
10770 | ||
c152c796 | 10771 | irela->r_offset = _bfd_elf_section_offset (output_bfd, |
8b127cbc | 10772 | flinfo->info, o, |
c152c796 AM |
10773 | irela->r_offset); |
10774 | if (irela->r_offset >= (bfd_vma) -2) | |
10775 | { | |
10776 | /* This is a reloc for a deleted entry or somesuch. | |
10777 | Turn it into an R_*_NONE reloc, at the same | |
10778 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 10779 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
10780 | being ordered. */ |
10781 | irela->r_offset = last_offset; | |
10782 | irela->r_info = 0; | |
10783 | irela->r_addend = 0; | |
10784 | continue; | |
10785 | } | |
10786 | ||
10787 | irela->r_offset += o->output_offset; | |
10788 | ||
10789 | /* Relocs in an executable have to be virtual addresses. */ | |
0e1862bb | 10790 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10791 | irela->r_offset += o->output_section->vma; |
10792 | ||
10793 | last_offset = irela->r_offset; | |
10794 | ||
10795 | r_symndx = irela->r_info >> r_sym_shift; | |
10796 | if (r_symndx == STN_UNDEF) | |
10797 | continue; | |
10798 | ||
10799 | if (r_symndx >= locsymcount | |
10800 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10801 | && flinfo->sections[r_symndx] == NULL)) |
c152c796 AM |
10802 | { |
10803 | struct elf_link_hash_entry *rh; | |
10804 | unsigned long indx; | |
10805 | ||
10806 | /* This is a reloc against a global symbol. We | |
10807 | have not yet output all the local symbols, so | |
10808 | we do not know the symbol index of any global | |
10809 | symbol. We set the rel_hash entry for this | |
10810 | reloc to point to the global hash table entry | |
10811 | for this symbol. The symbol index is then | |
ee75fd95 | 10812 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
10813 | indx = r_symndx - extsymoff; |
10814 | rh = elf_sym_hashes (input_bfd)[indx]; | |
10815 | while (rh->root.type == bfd_link_hash_indirect | |
10816 | || rh->root.type == bfd_link_hash_warning) | |
10817 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
10818 | ||
10819 | /* Setting the index to -2 tells | |
10820 | elf_link_output_extsym that this symbol is | |
10821 | used by a reloc. */ | |
10822 | BFD_ASSERT (rh->indx < 0); | |
10823 | rh->indx = -2; | |
c152c796 AM |
10824 | *rel_hash = rh; |
10825 | ||
10826 | continue; | |
10827 | } | |
10828 | ||
10829 | /* This is a reloc against a local symbol. */ | |
10830 | ||
10831 | *rel_hash = NULL; | |
10832 | sym = isymbuf[r_symndx]; | |
8b127cbc | 10833 | sec = flinfo->sections[r_symndx]; |
c152c796 AM |
10834 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) |
10835 | { | |
10836 | /* I suppose the backend ought to fill in the | |
10837 | section of any STT_SECTION symbol against a | |
6a8d1586 | 10838 | processor specific section. */ |
cf35638d | 10839 | r_symndx = STN_UNDEF; |
6a8d1586 AM |
10840 | if (bfd_is_abs_section (sec)) |
10841 | ; | |
c152c796 AM |
10842 | else if (sec == NULL || sec->owner == NULL) |
10843 | { | |
10844 | bfd_set_error (bfd_error_bad_value); | |
10845 | return FALSE; | |
10846 | } | |
10847 | else | |
10848 | { | |
6a8d1586 AM |
10849 | asection *osec = sec->output_section; |
10850 | ||
10851 | /* If we have discarded a section, the output | |
10852 | section will be the absolute section. In | |
ab96bf03 AM |
10853 | case of discarded SEC_MERGE sections, use |
10854 | the kept section. relocate_section should | |
10855 | have already handled discarded linkonce | |
10856 | sections. */ | |
6a8d1586 AM |
10857 | if (bfd_is_abs_section (osec) |
10858 | && sec->kept_section != NULL | |
10859 | && sec->kept_section->output_section != NULL) | |
10860 | { | |
10861 | osec = sec->kept_section->output_section; | |
10862 | irela->r_addend -= osec->vma; | |
10863 | } | |
10864 | ||
10865 | if (!bfd_is_abs_section (osec)) | |
10866 | { | |
10867 | r_symndx = osec->target_index; | |
cf35638d | 10868 | if (r_symndx == STN_UNDEF) |
74541ad4 | 10869 | { |
051d833a AM |
10870 | irela->r_addend += osec->vma; |
10871 | osec = _bfd_nearby_section (output_bfd, osec, | |
10872 | osec->vma); | |
10873 | irela->r_addend -= osec->vma; | |
10874 | r_symndx = osec->target_index; | |
74541ad4 | 10875 | } |
6a8d1586 | 10876 | } |
c152c796 AM |
10877 | } |
10878 | ||
10879 | /* Adjust the addend according to where the | |
10880 | section winds up in the output section. */ | |
10881 | if (rela_normal) | |
10882 | irela->r_addend += sec->output_offset; | |
10883 | } | |
10884 | else | |
10885 | { | |
8b127cbc | 10886 | if (flinfo->indices[r_symndx] == -1) |
c152c796 AM |
10887 | { |
10888 | unsigned long shlink; | |
10889 | const char *name; | |
10890 | asection *osec; | |
6e0b88f1 | 10891 | long indx; |
c152c796 | 10892 | |
8b127cbc | 10893 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
10894 | { |
10895 | /* You can't do ld -r -s. */ | |
10896 | bfd_set_error (bfd_error_invalid_operation); | |
10897 | return FALSE; | |
10898 | } | |
10899 | ||
10900 | /* This symbol was skipped earlier, but | |
10901 | since it is needed by a reloc, we | |
10902 | must output it now. */ | |
10903 | shlink = symtab_hdr->sh_link; | |
10904 | name = (bfd_elf_string_from_elf_section | |
10905 | (input_bfd, shlink, sym.st_name)); | |
10906 | if (name == NULL) | |
10907 | return FALSE; | |
10908 | ||
10909 | osec = sec->output_section; | |
10910 | sym.st_shndx = | |
10911 | _bfd_elf_section_from_bfd_section (output_bfd, | |
10912 | osec); | |
10913 | if (sym.st_shndx == SHN_BAD) | |
10914 | return FALSE; | |
10915 | ||
10916 | sym.st_value += sec->output_offset; | |
0e1862bb | 10917 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10918 | { |
10919 | sym.st_value += osec->vma; | |
10920 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
10921 | { | |
10922 | /* STT_TLS symbols are relative to PT_TLS | |
10923 | segment base. */ | |
8b127cbc | 10924 | BFD_ASSERT (elf_hash_table (flinfo->info) |
c152c796 | 10925 | ->tls_sec != NULL); |
8b127cbc | 10926 | sym.st_value -= (elf_hash_table (flinfo->info) |
c152c796 AM |
10927 | ->tls_sec->vma); |
10928 | } | |
10929 | } | |
10930 | ||
6e0b88f1 | 10931 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10932 | ret = elf_link_output_symstrtab (flinfo, name, |
10933 | &sym, sec, | |
10934 | NULL); | |
6e0b88f1 | 10935 | if (ret == 0) |
c152c796 | 10936 | return FALSE; |
6e0b88f1 | 10937 | else if (ret == 1) |
8b127cbc | 10938 | flinfo->indices[r_symndx] = indx; |
6e0b88f1 AM |
10939 | else |
10940 | abort (); | |
c152c796 AM |
10941 | } |
10942 | ||
8b127cbc | 10943 | r_symndx = flinfo->indices[r_symndx]; |
c152c796 AM |
10944 | } |
10945 | ||
10946 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
10947 | | (irela->r_info & r_type_mask)); | |
10948 | } | |
10949 | ||
10950 | /* Swap out the relocs. */ | |
d4730f92 BS |
10951 | input_rel_hdr = esdi->rel.hdr; |
10952 | if (input_rel_hdr && input_rel_hdr->sh_size != 0) | |
c152c796 | 10953 | { |
d4730f92 BS |
10954 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
10955 | input_rel_hdr, | |
10956 | internal_relocs, | |
10957 | rel_hash_list)) | |
10958 | return FALSE; | |
c152c796 AM |
10959 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) |
10960 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10961 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
d4730f92 BS |
10962 | } |
10963 | ||
10964 | input_rela_hdr = esdi->rela.hdr; | |
10965 | if (input_rela_hdr && input_rela_hdr->sh_size != 0) | |
10966 | { | |
eac338cf | 10967 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
d4730f92 | 10968 | input_rela_hdr, |
eac338cf | 10969 | internal_relocs, |
d4730f92 | 10970 | rela_hash_list)) |
c152c796 AM |
10971 | return FALSE; |
10972 | } | |
10973 | } | |
10974 | } | |
10975 | ||
10976 | /* Write out the modified section contents. */ | |
10977 | if (bed->elf_backend_write_section | |
8b127cbc | 10978 | && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, |
c7b8f16e | 10979 | contents)) |
c152c796 AM |
10980 | { |
10981 | /* Section written out. */ | |
10982 | } | |
10983 | else switch (o->sec_info_type) | |
10984 | { | |
dbaa2011 | 10985 | case SEC_INFO_TYPE_STABS: |
c152c796 AM |
10986 | if (! (_bfd_write_section_stabs |
10987 | (output_bfd, | |
8b127cbc | 10988 | &elf_hash_table (flinfo->info)->stab_info, |
c152c796 AM |
10989 | o, &elf_section_data (o)->sec_info, contents))) |
10990 | return FALSE; | |
10991 | break; | |
dbaa2011 | 10992 | case SEC_INFO_TYPE_MERGE: |
c152c796 AM |
10993 | if (! _bfd_write_merged_section (output_bfd, o, |
10994 | elf_section_data (o)->sec_info)) | |
10995 | return FALSE; | |
10996 | break; | |
dbaa2011 | 10997 | case SEC_INFO_TYPE_EH_FRAME: |
c152c796 | 10998 | { |
8b127cbc | 10999 | if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, |
c152c796 AM |
11000 | o, contents)) |
11001 | return FALSE; | |
11002 | } | |
11003 | break; | |
2f0c68f2 CM |
11004 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
11005 | { | |
11006 | if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, | |
11007 | flinfo->info, | |
11008 | o, contents)) | |
11009 | return FALSE; | |
11010 | } | |
11011 | break; | |
c152c796 AM |
11012 | default: |
11013 | { | |
310fd250 L |
11014 | if (! (o->flags & SEC_EXCLUDE)) |
11015 | { | |
11016 | file_ptr offset = (file_ptr) o->output_offset; | |
11017 | bfd_size_type todo = o->size; | |
37b01f6a DG |
11018 | |
11019 | offset *= bfd_octets_per_byte (output_bfd); | |
11020 | ||
310fd250 L |
11021 | if ((o->flags & SEC_ELF_REVERSE_COPY)) |
11022 | { | |
11023 | /* Reverse-copy input section to output. */ | |
11024 | do | |
11025 | { | |
11026 | todo -= address_size; | |
11027 | if (! bfd_set_section_contents (output_bfd, | |
11028 | o->output_section, | |
11029 | contents + todo, | |
11030 | offset, | |
11031 | address_size)) | |
11032 | return FALSE; | |
11033 | if (todo == 0) | |
11034 | break; | |
11035 | offset += address_size; | |
11036 | } | |
11037 | while (1); | |
11038 | } | |
11039 | else if (! bfd_set_section_contents (output_bfd, | |
11040 | o->output_section, | |
11041 | contents, | |
11042 | offset, todo)) | |
11043 | return FALSE; | |
11044 | } | |
c152c796 AM |
11045 | } |
11046 | break; | |
11047 | } | |
11048 | } | |
11049 | ||
11050 | return TRUE; | |
11051 | } | |
11052 | ||
11053 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 11054 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
11055 | is used to build constructor and destructor tables when linking |
11056 | with -Ur. */ | |
11057 | ||
11058 | static bfd_boolean | |
11059 | elf_reloc_link_order (bfd *output_bfd, | |
11060 | struct bfd_link_info *info, | |
11061 | asection *output_section, | |
11062 | struct bfd_link_order *link_order) | |
11063 | { | |
11064 | reloc_howto_type *howto; | |
11065 | long indx; | |
11066 | bfd_vma offset; | |
11067 | bfd_vma addend; | |
d4730f92 | 11068 | struct bfd_elf_section_reloc_data *reldata; |
c152c796 AM |
11069 | struct elf_link_hash_entry **rel_hash_ptr; |
11070 | Elf_Internal_Shdr *rel_hdr; | |
11071 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
11072 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
11073 | bfd_byte *erel; | |
11074 | unsigned int i; | |
d4730f92 | 11075 | struct bfd_elf_section_data *esdo = elf_section_data (output_section); |
c152c796 AM |
11076 | |
11077 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
11078 | if (howto == NULL) | |
11079 | { | |
11080 | bfd_set_error (bfd_error_bad_value); | |
11081 | return FALSE; | |
11082 | } | |
11083 | ||
11084 | addend = link_order->u.reloc.p->addend; | |
11085 | ||
d4730f92 BS |
11086 | if (esdo->rel.hdr) |
11087 | reldata = &esdo->rel; | |
11088 | else if (esdo->rela.hdr) | |
11089 | reldata = &esdo->rela; | |
11090 | else | |
11091 | { | |
11092 | reldata = NULL; | |
11093 | BFD_ASSERT (0); | |
11094 | } | |
11095 | ||
c152c796 | 11096 | /* Figure out the symbol index. */ |
d4730f92 | 11097 | rel_hash_ptr = reldata->hashes + reldata->count; |
c152c796 AM |
11098 | if (link_order->type == bfd_section_reloc_link_order) |
11099 | { | |
11100 | indx = link_order->u.reloc.p->u.section->target_index; | |
11101 | BFD_ASSERT (indx != 0); | |
11102 | *rel_hash_ptr = NULL; | |
11103 | } | |
11104 | else | |
11105 | { | |
11106 | struct elf_link_hash_entry *h; | |
11107 | ||
11108 | /* Treat a reloc against a defined symbol as though it were | |
11109 | actually against the section. */ | |
11110 | h = ((struct elf_link_hash_entry *) | |
11111 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
11112 | link_order->u.reloc.p->u.name, | |
11113 | FALSE, FALSE, TRUE)); | |
11114 | if (h != NULL | |
11115 | && (h->root.type == bfd_link_hash_defined | |
11116 | || h->root.type == bfd_link_hash_defweak)) | |
11117 | { | |
11118 | asection *section; | |
11119 | ||
11120 | section = h->root.u.def.section; | |
11121 | indx = section->output_section->target_index; | |
11122 | *rel_hash_ptr = NULL; | |
11123 | /* It seems that we ought to add the symbol value to the | |
11124 | addend here, but in practice it has already been added | |
11125 | because it was passed to constructor_callback. */ | |
11126 | addend += section->output_section->vma + section->output_offset; | |
11127 | } | |
11128 | else if (h != NULL) | |
11129 | { | |
11130 | /* Setting the index to -2 tells elf_link_output_extsym that | |
11131 | this symbol is used by a reloc. */ | |
11132 | h->indx = -2; | |
11133 | *rel_hash_ptr = h; | |
11134 | indx = 0; | |
11135 | } | |
11136 | else | |
11137 | { | |
1a72702b AM |
11138 | (*info->callbacks->unattached_reloc) |
11139 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); | |
c152c796 AM |
11140 | indx = 0; |
11141 | } | |
11142 | } | |
11143 | ||
11144 | /* If this is an inplace reloc, we must write the addend into the | |
11145 | object file. */ | |
11146 | if (howto->partial_inplace && addend != 0) | |
11147 | { | |
11148 | bfd_size_type size; | |
11149 | bfd_reloc_status_type rstat; | |
11150 | bfd_byte *buf; | |
11151 | bfd_boolean ok; | |
11152 | const char *sym_name; | |
11153 | ||
a50b1753 NC |
11154 | size = (bfd_size_type) bfd_get_reloc_size (howto); |
11155 | buf = (bfd_byte *) bfd_zmalloc (size); | |
6346d5ca | 11156 | if (buf == NULL && size != 0) |
c152c796 AM |
11157 | return FALSE; |
11158 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
11159 | switch (rstat) | |
11160 | { | |
11161 | case bfd_reloc_ok: | |
11162 | break; | |
11163 | ||
11164 | default: | |
11165 | case bfd_reloc_outofrange: | |
11166 | abort (); | |
11167 | ||
11168 | case bfd_reloc_overflow: | |
11169 | if (link_order->type == bfd_section_reloc_link_order) | |
11170 | sym_name = bfd_section_name (output_bfd, | |
11171 | link_order->u.reloc.p->u.section); | |
11172 | else | |
11173 | sym_name = link_order->u.reloc.p->u.name; | |
1a72702b AM |
11174 | (*info->callbacks->reloc_overflow) (info, NULL, sym_name, |
11175 | howto->name, addend, NULL, NULL, | |
11176 | (bfd_vma) 0); | |
c152c796 AM |
11177 | break; |
11178 | } | |
37b01f6a | 11179 | |
c152c796 | 11180 | ok = bfd_set_section_contents (output_bfd, output_section, buf, |
37b01f6a DG |
11181 | link_order->offset |
11182 | * bfd_octets_per_byte (output_bfd), | |
11183 | size); | |
c152c796 AM |
11184 | free (buf); |
11185 | if (! ok) | |
11186 | return FALSE; | |
11187 | } | |
11188 | ||
11189 | /* The address of a reloc is relative to the section in a | |
11190 | relocatable file, and is a virtual address in an executable | |
11191 | file. */ | |
11192 | offset = link_order->offset; | |
0e1862bb | 11193 | if (! bfd_link_relocatable (info)) |
c152c796 AM |
11194 | offset += output_section->vma; |
11195 | ||
11196 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
11197 | { | |
11198 | irel[i].r_offset = offset; | |
11199 | irel[i].r_info = 0; | |
11200 | irel[i].r_addend = 0; | |
11201 | } | |
11202 | if (bed->s->arch_size == 32) | |
11203 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
11204 | else | |
11205 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
11206 | ||
d4730f92 | 11207 | rel_hdr = reldata->hdr; |
c152c796 AM |
11208 | erel = rel_hdr->contents; |
11209 | if (rel_hdr->sh_type == SHT_REL) | |
11210 | { | |
d4730f92 | 11211 | erel += reldata->count * bed->s->sizeof_rel; |
c152c796 AM |
11212 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); |
11213 | } | |
11214 | else | |
11215 | { | |
11216 | irel[0].r_addend = addend; | |
d4730f92 | 11217 | erel += reldata->count * bed->s->sizeof_rela; |
c152c796 AM |
11218 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); |
11219 | } | |
11220 | ||
d4730f92 | 11221 | ++reldata->count; |
c152c796 AM |
11222 | |
11223 | return TRUE; | |
11224 | } | |
11225 | ||
0b52efa6 PB |
11226 | |
11227 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
11228 | ||
11229 | static bfd_vma | |
11230 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
11231 | { | |
11232 | Elf_Internal_Shdr **elf_shdrp; | |
11233 | asection *s; | |
11234 | int elfsec; | |
11235 | ||
11236 | s = p->u.indirect.section; | |
11237 | elf_shdrp = elf_elfsections (s->owner); | |
11238 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
11239 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
11240 | /* PR 290: |
11241 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 11242 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
11243 | sh_info fields. Hence we could get the situation |
11244 | where elfsec is 0. */ | |
11245 | if (elfsec == 0) | |
11246 | { | |
11247 | const struct elf_backend_data *bed | |
11248 | = get_elf_backend_data (s->owner); | |
11249 | if (bed->link_order_error_handler) | |
d003868e | 11250 | bed->link_order_error_handler |
695344c0 | 11251 | /* xgettext:c-format */ |
d003868e | 11252 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); |
185d09ad L |
11253 | return 0; |
11254 | } | |
11255 | else | |
11256 | { | |
11257 | s = elf_shdrp[elfsec]->bfd_section; | |
11258 | return s->output_section->vma + s->output_offset; | |
11259 | } | |
0b52efa6 PB |
11260 | } |
11261 | ||
11262 | ||
11263 | /* Compare two sections based on the locations of the sections they are | |
11264 | linked to. Used by elf_fixup_link_order. */ | |
11265 | ||
11266 | static int | |
11267 | compare_link_order (const void * a, const void * b) | |
11268 | { | |
11269 | bfd_vma apos; | |
11270 | bfd_vma bpos; | |
11271 | ||
11272 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
11273 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
11274 | if (apos < bpos) | |
11275 | return -1; | |
11276 | return apos > bpos; | |
11277 | } | |
11278 | ||
11279 | ||
11280 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
11281 | order as their linked sections. Returns false if this could not be done | |
11282 | because an output section includes both ordered and unordered | |
11283 | sections. Ideally we'd do this in the linker proper. */ | |
11284 | ||
11285 | static bfd_boolean | |
11286 | elf_fixup_link_order (bfd *abfd, asection *o) | |
11287 | { | |
11288 | int seen_linkorder; | |
11289 | int seen_other; | |
11290 | int n; | |
11291 | struct bfd_link_order *p; | |
11292 | bfd *sub; | |
11293 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 11294 | unsigned elfsec; |
0b52efa6 | 11295 | struct bfd_link_order **sections; |
d33cdfe3 | 11296 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 11297 | bfd_vma offset; |
3b36f7e6 | 11298 | |
d33cdfe3 L |
11299 | other_sec = NULL; |
11300 | linkorder_sec = NULL; | |
0b52efa6 PB |
11301 | seen_other = 0; |
11302 | seen_linkorder = 0; | |
8423293d | 11303 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 11304 | { |
d33cdfe3 | 11305 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
11306 | { |
11307 | s = p->u.indirect.section; | |
d33cdfe3 L |
11308 | sub = s->owner; |
11309 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
11310 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
11311 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
11312 | && elfsec < elf_numsections (sub) | |
4fbb74a6 AM |
11313 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER |
11314 | && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) | |
d33cdfe3 L |
11315 | { |
11316 | seen_linkorder++; | |
11317 | linkorder_sec = s; | |
11318 | } | |
0b52efa6 | 11319 | else |
d33cdfe3 L |
11320 | { |
11321 | seen_other++; | |
11322 | other_sec = s; | |
11323 | } | |
0b52efa6 PB |
11324 | } |
11325 | else | |
11326 | seen_other++; | |
d33cdfe3 L |
11327 | |
11328 | if (seen_other && seen_linkorder) | |
11329 | { | |
11330 | if (other_sec && linkorder_sec) | |
4eca0228 | 11331 | _bfd_error_handler |
695344c0 | 11332 | /* xgettext:c-format */ |
4eca0228 AM |
11333 | (_("%A has both ordered [`%A' in %B] " |
11334 | "and unordered [`%A' in %B] sections"), | |
63a5468a AM |
11335 | o, linkorder_sec, linkorder_sec->owner, |
11336 | other_sec, other_sec->owner); | |
d33cdfe3 | 11337 | else |
4eca0228 AM |
11338 | _bfd_error_handler |
11339 | (_("%A has both ordered and unordered sections"), o); | |
d33cdfe3 L |
11340 | bfd_set_error (bfd_error_bad_value); |
11341 | return FALSE; | |
11342 | } | |
0b52efa6 PB |
11343 | } |
11344 | ||
11345 | if (!seen_linkorder) | |
11346 | return TRUE; | |
11347 | ||
0b52efa6 | 11348 | sections = (struct bfd_link_order **) |
14b1c01e AM |
11349 | bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); |
11350 | if (sections == NULL) | |
11351 | return FALSE; | |
0b52efa6 | 11352 | seen_linkorder = 0; |
3b36f7e6 | 11353 | |
8423293d | 11354 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
11355 | { |
11356 | sections[seen_linkorder++] = p; | |
11357 | } | |
11358 | /* Sort the input sections in the order of their linked section. */ | |
11359 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
11360 | compare_link_order); | |
11361 | ||
11362 | /* Change the offsets of the sections. */ | |
11363 | offset = 0; | |
11364 | for (n = 0; n < seen_linkorder; n++) | |
11365 | { | |
11366 | s = sections[n]->u.indirect.section; | |
461686a3 | 11367 | offset &= ~(bfd_vma) 0 << s->alignment_power; |
37b01f6a | 11368 | s->output_offset = offset / bfd_octets_per_byte (abfd); |
0b52efa6 PB |
11369 | sections[n]->offset = offset; |
11370 | offset += sections[n]->size; | |
11371 | } | |
11372 | ||
4dd07732 | 11373 | free (sections); |
0b52efa6 PB |
11374 | return TRUE; |
11375 | } | |
11376 | ||
76359541 TP |
11377 | /* Generate an import library in INFO->implib_bfd from symbols in ABFD. |
11378 | Returns TRUE upon success, FALSE otherwise. */ | |
11379 | ||
11380 | static bfd_boolean | |
11381 | elf_output_implib (bfd *abfd, struct bfd_link_info *info) | |
11382 | { | |
11383 | bfd_boolean ret = FALSE; | |
11384 | bfd *implib_bfd; | |
11385 | const struct elf_backend_data *bed; | |
11386 | flagword flags; | |
11387 | enum bfd_architecture arch; | |
11388 | unsigned int mach; | |
11389 | asymbol **sympp = NULL; | |
11390 | long symsize; | |
11391 | long symcount; | |
11392 | long src_count; | |
11393 | elf_symbol_type *osymbuf; | |
11394 | ||
11395 | implib_bfd = info->out_implib_bfd; | |
11396 | bed = get_elf_backend_data (abfd); | |
11397 | ||
11398 | if (!bfd_set_format (implib_bfd, bfd_object)) | |
11399 | return FALSE; | |
11400 | ||
046734ff | 11401 | /* Use flag from executable but make it a relocatable object. */ |
76359541 TP |
11402 | flags = bfd_get_file_flags (abfd); |
11403 | flags &= ~HAS_RELOC; | |
11404 | if (!bfd_set_start_address (implib_bfd, 0) | |
046734ff | 11405 | || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P)) |
76359541 TP |
11406 | return FALSE; |
11407 | ||
11408 | /* Copy architecture of output file to import library file. */ | |
11409 | arch = bfd_get_arch (abfd); | |
11410 | mach = bfd_get_mach (abfd); | |
11411 | if (!bfd_set_arch_mach (implib_bfd, arch, mach) | |
11412 | && (abfd->target_defaulted | |
11413 | || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) | |
11414 | return FALSE; | |
11415 | ||
11416 | /* Get symbol table size. */ | |
11417 | symsize = bfd_get_symtab_upper_bound (abfd); | |
11418 | if (symsize < 0) | |
11419 | return FALSE; | |
11420 | ||
11421 | /* Read in the symbol table. */ | |
11422 | sympp = (asymbol **) xmalloc (symsize); | |
11423 | symcount = bfd_canonicalize_symtab (abfd, sympp); | |
11424 | if (symcount < 0) | |
11425 | goto free_sym_buf; | |
11426 | ||
11427 | /* Allow the BFD backend to copy any private header data it | |
11428 | understands from the output BFD to the import library BFD. */ | |
11429 | if (! bfd_copy_private_header_data (abfd, implib_bfd)) | |
11430 | goto free_sym_buf; | |
11431 | ||
11432 | /* Filter symbols to appear in the import library. */ | |
11433 | if (bed->elf_backend_filter_implib_symbols) | |
11434 | symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, | |
11435 | symcount); | |
11436 | else | |
11437 | symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); | |
11438 | if (symcount == 0) | |
11439 | { | |
5df1bc57 | 11440 | bfd_set_error (bfd_error_no_symbols); |
4eca0228 AM |
11441 | _bfd_error_handler (_("%B: no symbol found for import library"), |
11442 | implib_bfd); | |
76359541 TP |
11443 | goto free_sym_buf; |
11444 | } | |
11445 | ||
11446 | ||
11447 | /* Make symbols absolute. */ | |
11448 | osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount, | |
11449 | sizeof (*osymbuf)); | |
11450 | for (src_count = 0; src_count < symcount; src_count++) | |
11451 | { | |
11452 | memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], | |
11453 | sizeof (*osymbuf)); | |
11454 | osymbuf[src_count].symbol.section = bfd_abs_section_ptr; | |
11455 | osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; | |
11456 | osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; | |
11457 | osymbuf[src_count].internal_elf_sym.st_value = | |
11458 | osymbuf[src_count].symbol.value; | |
11459 | sympp[src_count] = &osymbuf[src_count].symbol; | |
11460 | } | |
11461 | ||
11462 | bfd_set_symtab (implib_bfd, sympp, symcount); | |
11463 | ||
11464 | /* Allow the BFD backend to copy any private data it understands | |
11465 | from the output BFD to the import library BFD. This is done last | |
11466 | to permit the routine to look at the filtered symbol table. */ | |
11467 | if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) | |
11468 | goto free_sym_buf; | |
11469 | ||
11470 | if (!bfd_close (implib_bfd)) | |
11471 | goto free_sym_buf; | |
11472 | ||
11473 | ret = TRUE; | |
11474 | ||
11475 | free_sym_buf: | |
11476 | free (sympp); | |
11477 | return ret; | |
11478 | } | |
11479 | ||
9f7c3e5e AM |
11480 | static void |
11481 | elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) | |
11482 | { | |
11483 | asection *o; | |
11484 | ||
11485 | if (flinfo->symstrtab != NULL) | |
ef10c3ac | 11486 | _bfd_elf_strtab_free (flinfo->symstrtab); |
9f7c3e5e AM |
11487 | if (flinfo->contents != NULL) |
11488 | free (flinfo->contents); | |
11489 | if (flinfo->external_relocs != NULL) | |
11490 | free (flinfo->external_relocs); | |
11491 | if (flinfo->internal_relocs != NULL) | |
11492 | free (flinfo->internal_relocs); | |
11493 | if (flinfo->external_syms != NULL) | |
11494 | free (flinfo->external_syms); | |
11495 | if (flinfo->locsym_shndx != NULL) | |
11496 | free (flinfo->locsym_shndx); | |
11497 | if (flinfo->internal_syms != NULL) | |
11498 | free (flinfo->internal_syms); | |
11499 | if (flinfo->indices != NULL) | |
11500 | free (flinfo->indices); | |
11501 | if (flinfo->sections != NULL) | |
11502 | free (flinfo->sections); | |
9f7c3e5e AM |
11503 | if (flinfo->symshndxbuf != NULL) |
11504 | free (flinfo->symshndxbuf); | |
11505 | for (o = obfd->sections; o != NULL; o = o->next) | |
11506 | { | |
11507 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11508 | if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) | |
11509 | free (esdo->rel.hashes); | |
11510 | if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) | |
11511 | free (esdo->rela.hashes); | |
11512 | } | |
11513 | } | |
0b52efa6 | 11514 | |
c152c796 AM |
11515 | /* Do the final step of an ELF link. */ |
11516 | ||
11517 | bfd_boolean | |
11518 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
11519 | { | |
11520 | bfd_boolean dynamic; | |
11521 | bfd_boolean emit_relocs; | |
11522 | bfd *dynobj; | |
8b127cbc | 11523 | struct elf_final_link_info flinfo; |
91d6fa6a NC |
11524 | asection *o; |
11525 | struct bfd_link_order *p; | |
11526 | bfd *sub; | |
c152c796 AM |
11527 | bfd_size_type max_contents_size; |
11528 | bfd_size_type max_external_reloc_size; | |
11529 | bfd_size_type max_internal_reloc_count; | |
11530 | bfd_size_type max_sym_count; | |
11531 | bfd_size_type max_sym_shndx_count; | |
c152c796 AM |
11532 | Elf_Internal_Sym elfsym; |
11533 | unsigned int i; | |
11534 | Elf_Internal_Shdr *symtab_hdr; | |
11535 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
c152c796 AM |
11536 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11537 | struct elf_outext_info eoinfo; | |
11538 | bfd_boolean merged; | |
11539 | size_t relativecount = 0; | |
11540 | asection *reldyn = 0; | |
11541 | bfd_size_type amt; | |
104d59d1 JM |
11542 | asection *attr_section = NULL; |
11543 | bfd_vma attr_size = 0; | |
11544 | const char *std_attrs_section; | |
64f52338 | 11545 | struct elf_link_hash_table *htab = elf_hash_table (info); |
c152c796 | 11546 | |
64f52338 | 11547 | if (!is_elf_hash_table (htab)) |
c152c796 AM |
11548 | return FALSE; |
11549 | ||
0e1862bb | 11550 | if (bfd_link_pic (info)) |
c152c796 AM |
11551 | abfd->flags |= DYNAMIC; |
11552 | ||
64f52338 AM |
11553 | dynamic = htab->dynamic_sections_created; |
11554 | dynobj = htab->dynobj; | |
c152c796 | 11555 | |
0e1862bb | 11556 | emit_relocs = (bfd_link_relocatable (info) |
a4676736 | 11557 | || info->emitrelocations); |
c152c796 | 11558 | |
8b127cbc AM |
11559 | flinfo.info = info; |
11560 | flinfo.output_bfd = abfd; | |
ef10c3ac | 11561 | flinfo.symstrtab = _bfd_elf_strtab_init (); |
8b127cbc | 11562 | if (flinfo.symstrtab == NULL) |
c152c796 AM |
11563 | return FALSE; |
11564 | ||
11565 | if (! dynamic) | |
11566 | { | |
8b127cbc AM |
11567 | flinfo.hash_sec = NULL; |
11568 | flinfo.symver_sec = NULL; | |
c152c796 AM |
11569 | } |
11570 | else | |
11571 | { | |
3d4d4302 | 11572 | flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); |
202e2356 | 11573 | /* Note that dynsym_sec can be NULL (on VMS). */ |
3d4d4302 | 11574 | flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); |
c152c796 AM |
11575 | /* Note that it is OK if symver_sec is NULL. */ |
11576 | } | |
11577 | ||
8b127cbc AM |
11578 | flinfo.contents = NULL; |
11579 | flinfo.external_relocs = NULL; | |
11580 | flinfo.internal_relocs = NULL; | |
11581 | flinfo.external_syms = NULL; | |
11582 | flinfo.locsym_shndx = NULL; | |
11583 | flinfo.internal_syms = NULL; | |
11584 | flinfo.indices = NULL; | |
11585 | flinfo.sections = NULL; | |
8b127cbc | 11586 | flinfo.symshndxbuf = NULL; |
ffbc01cc | 11587 | flinfo.filesym_count = 0; |
c152c796 | 11588 | |
104d59d1 JM |
11589 | /* The object attributes have been merged. Remove the input |
11590 | sections from the link, and set the contents of the output | |
11591 | secton. */ | |
11592 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; | |
11593 | for (o = abfd->sections; o != NULL; o = o->next) | |
11594 | { | |
11595 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) | |
11596 | || strcmp (o->name, ".gnu.attributes") == 0) | |
11597 | { | |
11598 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
11599 | { | |
11600 | asection *input_section; | |
11601 | ||
11602 | if (p->type != bfd_indirect_link_order) | |
11603 | continue; | |
11604 | input_section = p->u.indirect.section; | |
11605 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
11606 | elf_link_input_bfd ignores this section. */ | |
11607 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
11608 | } | |
a0c8462f | 11609 | |
104d59d1 JM |
11610 | attr_size = bfd_elf_obj_attr_size (abfd); |
11611 | if (attr_size) | |
11612 | { | |
11613 | bfd_set_section_size (abfd, o, attr_size); | |
11614 | attr_section = o; | |
11615 | /* Skip this section later on. */ | |
11616 | o->map_head.link_order = NULL; | |
11617 | } | |
11618 | else | |
11619 | o->flags |= SEC_EXCLUDE; | |
11620 | } | |
11621 | } | |
11622 | ||
c152c796 AM |
11623 | /* Count up the number of relocations we will output for each output |
11624 | section, so that we know the sizes of the reloc sections. We | |
11625 | also figure out some maximum sizes. */ | |
11626 | max_contents_size = 0; | |
11627 | max_external_reloc_size = 0; | |
11628 | max_internal_reloc_count = 0; | |
11629 | max_sym_count = 0; | |
11630 | max_sym_shndx_count = 0; | |
11631 | merged = FALSE; | |
11632 | for (o = abfd->sections; o != NULL; o = o->next) | |
11633 | { | |
11634 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11635 | o->reloc_count = 0; | |
11636 | ||
8423293d | 11637 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
11638 | { |
11639 | unsigned int reloc_count = 0; | |
9eaff861 | 11640 | unsigned int additional_reloc_count = 0; |
c152c796 | 11641 | struct bfd_elf_section_data *esdi = NULL; |
c152c796 AM |
11642 | |
11643 | if (p->type == bfd_section_reloc_link_order | |
11644 | || p->type == bfd_symbol_reloc_link_order) | |
11645 | reloc_count = 1; | |
11646 | else if (p->type == bfd_indirect_link_order) | |
11647 | { | |
11648 | asection *sec; | |
11649 | ||
11650 | sec = p->u.indirect.section; | |
c152c796 AM |
11651 | |
11652 | /* Mark all sections which are to be included in the | |
11653 | link. This will normally be every section. We need | |
11654 | to do this so that we can identify any sections which | |
11655 | the linker has decided to not include. */ | |
11656 | sec->linker_mark = TRUE; | |
11657 | ||
11658 | if (sec->flags & SEC_MERGE) | |
11659 | merged = TRUE; | |
11660 | ||
eea6121a AM |
11661 | if (sec->rawsize > max_contents_size) |
11662 | max_contents_size = sec->rawsize; | |
11663 | if (sec->size > max_contents_size) | |
11664 | max_contents_size = sec->size; | |
c152c796 | 11665 | |
c152c796 AM |
11666 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour |
11667 | && (sec->owner->flags & DYNAMIC) == 0) | |
11668 | { | |
11669 | size_t sym_count; | |
11670 | ||
a961cdd5 AM |
11671 | /* We are interested in just local symbols, not all |
11672 | symbols. */ | |
c152c796 AM |
11673 | if (elf_bad_symtab (sec->owner)) |
11674 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
11675 | / bed->s->sizeof_sym); | |
11676 | else | |
11677 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
11678 | ||
11679 | if (sym_count > max_sym_count) | |
11680 | max_sym_count = sym_count; | |
11681 | ||
11682 | if (sym_count > max_sym_shndx_count | |
6a40cf0c | 11683 | && elf_symtab_shndx_list (sec->owner) != NULL) |
c152c796 AM |
11684 | max_sym_shndx_count = sym_count; |
11685 | ||
a961cdd5 AM |
11686 | if (esdo->this_hdr.sh_type == SHT_REL |
11687 | || esdo->this_hdr.sh_type == SHT_RELA) | |
11688 | /* Some backends use reloc_count in relocation sections | |
11689 | to count particular types of relocs. Of course, | |
11690 | reloc sections themselves can't have relocations. */ | |
11691 | ; | |
11692 | else if (emit_relocs) | |
11693 | { | |
11694 | reloc_count = sec->reloc_count; | |
11695 | if (bed->elf_backend_count_additional_relocs) | |
11696 | { | |
11697 | int c; | |
11698 | c = (*bed->elf_backend_count_additional_relocs) (sec); | |
11699 | additional_reloc_count += c; | |
11700 | } | |
11701 | } | |
11702 | else if (bed->elf_backend_count_relocs) | |
11703 | reloc_count = (*bed->elf_backend_count_relocs) (info, sec); | |
11704 | ||
11705 | esdi = elf_section_data (sec); | |
11706 | ||
c152c796 AM |
11707 | if ((sec->flags & SEC_RELOC) != 0) |
11708 | { | |
d4730f92 | 11709 | size_t ext_size = 0; |
c152c796 | 11710 | |
d4730f92 BS |
11711 | if (esdi->rel.hdr != NULL) |
11712 | ext_size = esdi->rel.hdr->sh_size; | |
11713 | if (esdi->rela.hdr != NULL) | |
11714 | ext_size += esdi->rela.hdr->sh_size; | |
7326c758 | 11715 | |
c152c796 AM |
11716 | if (ext_size > max_external_reloc_size) |
11717 | max_external_reloc_size = ext_size; | |
11718 | if (sec->reloc_count > max_internal_reloc_count) | |
11719 | max_internal_reloc_count = sec->reloc_count; | |
11720 | } | |
11721 | } | |
11722 | } | |
11723 | ||
11724 | if (reloc_count == 0) | |
11725 | continue; | |
11726 | ||
9eaff861 | 11727 | reloc_count += additional_reloc_count; |
c152c796 AM |
11728 | o->reloc_count += reloc_count; |
11729 | ||
0e1862bb | 11730 | if (p->type == bfd_indirect_link_order && emit_relocs) |
c152c796 | 11731 | { |
d4730f92 | 11732 | if (esdi->rel.hdr) |
9eaff861 | 11733 | { |
491d01d3 | 11734 | esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); |
9eaff861 AO |
11735 | esdo->rel.count += additional_reloc_count; |
11736 | } | |
d4730f92 | 11737 | if (esdi->rela.hdr) |
9eaff861 | 11738 | { |
491d01d3 | 11739 | esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); |
9eaff861 AO |
11740 | esdo->rela.count += additional_reloc_count; |
11741 | } | |
d4730f92 BS |
11742 | } |
11743 | else | |
11744 | { | |
11745 | if (o->use_rela_p) | |
11746 | esdo->rela.count += reloc_count; | |
2c2b4ed4 | 11747 | else |
d4730f92 | 11748 | esdo->rel.count += reloc_count; |
c152c796 | 11749 | } |
c152c796 AM |
11750 | } |
11751 | ||
9eaff861 | 11752 | if (o->reloc_count > 0) |
c152c796 AM |
11753 | o->flags |= SEC_RELOC; |
11754 | else | |
11755 | { | |
11756 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
11757 | set it (this is probably a bug) and if it is set | |
11758 | assign_section_numbers will create a reloc section. */ | |
11759 | o->flags &=~ SEC_RELOC; | |
11760 | } | |
11761 | ||
11762 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
11763 | zero. This is done in elf_fake_sections as well, but forcing | |
11764 | the VMA to 0 here will ensure that relocs against these | |
11765 | sections are handled correctly. */ | |
11766 | if ((o->flags & SEC_ALLOC) == 0 | |
11767 | && ! o->user_set_vma) | |
11768 | o->vma = 0; | |
11769 | } | |
11770 | ||
0e1862bb | 11771 | if (! bfd_link_relocatable (info) && merged) |
64f52338 | 11772 | elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd); |
c152c796 AM |
11773 | |
11774 | /* Figure out the file positions for everything but the symbol table | |
11775 | and the relocs. We set symcount to force assign_section_numbers | |
11776 | to create a symbol table. */ | |
8539e4e8 | 11777 | bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs; |
c152c796 AM |
11778 | BFD_ASSERT (! abfd->output_has_begun); |
11779 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
11780 | goto error_return; | |
11781 | ||
ee75fd95 | 11782 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
11783 | for (o = abfd->sections; o != NULL; o = o->next) |
11784 | { | |
d4730f92 | 11785 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
c152c796 AM |
11786 | if ((o->flags & SEC_RELOC) != 0) |
11787 | { | |
d4730f92 | 11788 | if (esdo->rel.hdr |
9eaff861 | 11789 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) |
c152c796 AM |
11790 | goto error_return; |
11791 | ||
d4730f92 | 11792 | if (esdo->rela.hdr |
9eaff861 | 11793 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) |
c152c796 AM |
11794 | goto error_return; |
11795 | } | |
11796 | ||
11797 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
11798 | to count upwards while actually outputting the relocations. */ | |
d4730f92 BS |
11799 | esdo->rel.count = 0; |
11800 | esdo->rela.count = 0; | |
0ce398f1 L |
11801 | |
11802 | if (esdo->this_hdr.sh_offset == (file_ptr) -1) | |
11803 | { | |
11804 | /* Cache the section contents so that they can be compressed | |
11805 | later. Use bfd_malloc since it will be freed by | |
11806 | bfd_compress_section_contents. */ | |
11807 | unsigned char *contents = esdo->this_hdr.contents; | |
11808 | if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) | |
11809 | abort (); | |
11810 | contents | |
11811 | = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); | |
11812 | if (contents == NULL) | |
11813 | goto error_return; | |
11814 | esdo->this_hdr.contents = contents; | |
11815 | } | |
c152c796 AM |
11816 | } |
11817 | ||
c152c796 | 11818 | /* We have now assigned file positions for all the sections except |
a485e98e AM |
11819 | .symtab, .strtab, and non-loaded reloc sections. We start the |
11820 | .symtab section at the current file position, and write directly | |
11821 | to it. We build the .strtab section in memory. */ | |
c152c796 AM |
11822 | bfd_get_symcount (abfd) = 0; |
11823 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11824 | /* sh_name is set in prep_headers. */ | |
11825 | symtab_hdr->sh_type = SHT_SYMTAB; | |
11826 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
11827 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
11828 | /* sh_link is set in assign_section_numbers. */ | |
11829 | /* sh_info is set below. */ | |
11830 | /* sh_offset is set just below. */ | |
72de5009 | 11831 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
c152c796 | 11832 | |
ef10c3ac L |
11833 | if (max_sym_count < 20) |
11834 | max_sym_count = 20; | |
64f52338 | 11835 | htab->strtabsize = max_sym_count; |
ef10c3ac | 11836 | amt = max_sym_count * sizeof (struct elf_sym_strtab); |
64f52338 AM |
11837 | htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt); |
11838 | if (htab->strtab == NULL) | |
c152c796 | 11839 | goto error_return; |
ef10c3ac L |
11840 | /* The real buffer will be allocated in elf_link_swap_symbols_out. */ |
11841 | flinfo.symshndxbuf | |
11842 | = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) | |
11843 | ? (Elf_External_Sym_Shndx *) -1 : NULL); | |
c152c796 | 11844 | |
8539e4e8 | 11845 | if (info->strip != strip_all || emit_relocs) |
c152c796 | 11846 | { |
8539e4e8 AM |
11847 | file_ptr off = elf_next_file_pos (abfd); |
11848 | ||
11849 | _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
11850 | ||
11851 | /* Note that at this point elf_next_file_pos (abfd) is | |
11852 | incorrect. We do not yet know the size of the .symtab section. | |
11853 | We correct next_file_pos below, after we do know the size. */ | |
11854 | ||
11855 | /* Start writing out the symbol table. The first symbol is always a | |
11856 | dummy symbol. */ | |
c152c796 AM |
11857 | elfsym.st_value = 0; |
11858 | elfsym.st_size = 0; | |
11859 | elfsym.st_info = 0; | |
11860 | elfsym.st_other = 0; | |
11861 | elfsym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 11862 | elfsym.st_target_internal = 0; |
ef10c3ac L |
11863 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, |
11864 | bfd_und_section_ptr, NULL) != 1) | |
c152c796 | 11865 | goto error_return; |
c152c796 | 11866 | |
8539e4e8 AM |
11867 | /* Output a symbol for each section. We output these even if we are |
11868 | discarding local symbols, since they are used for relocs. These | |
11869 | symbols have no names. We store the index of each one in the | |
11870 | index field of the section, so that we can find it again when | |
11871 | outputting relocs. */ | |
11872 | ||
c152c796 AM |
11873 | elfsym.st_size = 0; |
11874 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
11875 | elfsym.st_other = 0; | |
f0b5bb34 | 11876 | elfsym.st_value = 0; |
35fc36a8 | 11877 | elfsym.st_target_internal = 0; |
c152c796 AM |
11878 | for (i = 1; i < elf_numsections (abfd); i++) |
11879 | { | |
11880 | o = bfd_section_from_elf_index (abfd, i); | |
11881 | if (o != NULL) | |
f0b5bb34 AM |
11882 | { |
11883 | o->target_index = bfd_get_symcount (abfd); | |
11884 | elfsym.st_shndx = i; | |
0e1862bb | 11885 | if (!bfd_link_relocatable (info)) |
f0b5bb34 | 11886 | elfsym.st_value = o->vma; |
ef10c3ac L |
11887 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o, |
11888 | NULL) != 1) | |
f0b5bb34 AM |
11889 | goto error_return; |
11890 | } | |
c152c796 AM |
11891 | } |
11892 | } | |
11893 | ||
11894 | /* Allocate some memory to hold information read in from the input | |
11895 | files. */ | |
11896 | if (max_contents_size != 0) | |
11897 | { | |
8b127cbc AM |
11898 | flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
11899 | if (flinfo.contents == NULL) | |
c152c796 AM |
11900 | goto error_return; |
11901 | } | |
11902 | ||
11903 | if (max_external_reloc_size != 0) | |
11904 | { | |
8b127cbc AM |
11905 | flinfo.external_relocs = bfd_malloc (max_external_reloc_size); |
11906 | if (flinfo.external_relocs == NULL) | |
c152c796 AM |
11907 | goto error_return; |
11908 | } | |
11909 | ||
11910 | if (max_internal_reloc_count != 0) | |
11911 | { | |
056bafd4 | 11912 | amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela); |
8b127cbc AM |
11913 | flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); |
11914 | if (flinfo.internal_relocs == NULL) | |
c152c796 AM |
11915 | goto error_return; |
11916 | } | |
11917 | ||
11918 | if (max_sym_count != 0) | |
11919 | { | |
11920 | amt = max_sym_count * bed->s->sizeof_sym; | |
8b127cbc AM |
11921 | flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); |
11922 | if (flinfo.external_syms == NULL) | |
c152c796 AM |
11923 | goto error_return; |
11924 | ||
11925 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8b127cbc AM |
11926 | flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); |
11927 | if (flinfo.internal_syms == NULL) | |
c152c796 AM |
11928 | goto error_return; |
11929 | ||
11930 | amt = max_sym_count * sizeof (long); | |
8b127cbc AM |
11931 | flinfo.indices = (long int *) bfd_malloc (amt); |
11932 | if (flinfo.indices == NULL) | |
c152c796 AM |
11933 | goto error_return; |
11934 | ||
11935 | amt = max_sym_count * sizeof (asection *); | |
8b127cbc AM |
11936 | flinfo.sections = (asection **) bfd_malloc (amt); |
11937 | if (flinfo.sections == NULL) | |
c152c796 AM |
11938 | goto error_return; |
11939 | } | |
11940 | ||
11941 | if (max_sym_shndx_count != 0) | |
11942 | { | |
11943 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8b127cbc AM |
11944 | flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
11945 | if (flinfo.locsym_shndx == NULL) | |
c152c796 AM |
11946 | goto error_return; |
11947 | } | |
11948 | ||
64f52338 | 11949 | if (htab->tls_sec) |
c152c796 AM |
11950 | { |
11951 | bfd_vma base, end = 0; | |
11952 | asection *sec; | |
11953 | ||
64f52338 | 11954 | for (sec = htab->tls_sec; |
c152c796 AM |
11955 | sec && (sec->flags & SEC_THREAD_LOCAL); |
11956 | sec = sec->next) | |
11957 | { | |
3a800eb9 | 11958 | bfd_size_type size = sec->size; |
c152c796 | 11959 | |
3a800eb9 AM |
11960 | if (size == 0 |
11961 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 11962 | { |
91d6fa6a NC |
11963 | struct bfd_link_order *ord = sec->map_tail.link_order; |
11964 | ||
11965 | if (ord != NULL) | |
11966 | size = ord->offset + ord->size; | |
c152c796 AM |
11967 | } |
11968 | end = sec->vma + size; | |
11969 | } | |
64f52338 | 11970 | base = htab->tls_sec->vma; |
7dc98aea RO |
11971 | /* Only align end of TLS section if static TLS doesn't have special |
11972 | alignment requirements. */ | |
11973 | if (bed->static_tls_alignment == 1) | |
64f52338 AM |
11974 | end = align_power (end, htab->tls_sec->alignment_power); |
11975 | htab->tls_size = end - base; | |
c152c796 AM |
11976 | } |
11977 | ||
0b52efa6 PB |
11978 | /* Reorder SHF_LINK_ORDER sections. */ |
11979 | for (o = abfd->sections; o != NULL; o = o->next) | |
11980 | { | |
11981 | if (!elf_fixup_link_order (abfd, o)) | |
11982 | return FALSE; | |
11983 | } | |
11984 | ||
2f0c68f2 CM |
11985 | if (!_bfd_elf_fixup_eh_frame_hdr (info)) |
11986 | return FALSE; | |
11987 | ||
c152c796 AM |
11988 | /* Since ELF permits relocations to be against local symbols, we |
11989 | must have the local symbols available when we do the relocations. | |
11990 | Since we would rather only read the local symbols once, and we | |
11991 | would rather not keep them in memory, we handle all the | |
11992 | relocations for a single input file at the same time. | |
11993 | ||
11994 | Unfortunately, there is no way to know the total number of local | |
11995 | symbols until we have seen all of them, and the local symbol | |
11996 | indices precede the global symbol indices. This means that when | |
11997 | we are generating relocatable output, and we see a reloc against | |
11998 | a global symbol, we can not know the symbol index until we have | |
11999 | finished examining all the local symbols to see which ones we are | |
12000 | going to output. To deal with this, we keep the relocations in | |
12001 | memory, and don't output them until the end of the link. This is | |
12002 | an unfortunate waste of memory, but I don't see a good way around | |
12003 | it. Fortunately, it only happens when performing a relocatable | |
12004 | link, which is not the common case. FIXME: If keep_memory is set | |
12005 | we could write the relocs out and then read them again; I don't | |
12006 | know how bad the memory loss will be. */ | |
12007 | ||
c72f2fb2 | 12008 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
12009 | sub->output_has_begun = FALSE; |
12010 | for (o = abfd->sections; o != NULL; o = o->next) | |
12011 | { | |
8423293d | 12012 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
12013 | { |
12014 | if (p->type == bfd_indirect_link_order | |
12015 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
12016 | == bfd_target_elf_flavour) | |
12017 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
12018 | { | |
12019 | if (! sub->output_has_begun) | |
12020 | { | |
8b127cbc | 12021 | if (! elf_link_input_bfd (&flinfo, sub)) |
c152c796 AM |
12022 | goto error_return; |
12023 | sub->output_has_begun = TRUE; | |
12024 | } | |
12025 | } | |
12026 | else if (p->type == bfd_section_reloc_link_order | |
12027 | || p->type == bfd_symbol_reloc_link_order) | |
12028 | { | |
12029 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
12030 | goto error_return; | |
12031 | } | |
12032 | else | |
12033 | { | |
12034 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
351f65ca L |
12035 | { |
12036 | if (p->type == bfd_indirect_link_order | |
12037 | && (bfd_get_flavour (sub) | |
12038 | == bfd_target_elf_flavour) | |
12039 | && (elf_elfheader (sub)->e_ident[EI_CLASS] | |
12040 | != bed->s->elfclass)) | |
12041 | { | |
12042 | const char *iclass, *oclass; | |
12043 | ||
aebf9be7 | 12044 | switch (bed->s->elfclass) |
351f65ca | 12045 | { |
aebf9be7 NC |
12046 | case ELFCLASS64: oclass = "ELFCLASS64"; break; |
12047 | case ELFCLASS32: oclass = "ELFCLASS32"; break; | |
12048 | case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; | |
12049 | default: abort (); | |
351f65ca | 12050 | } |
aebf9be7 NC |
12051 | |
12052 | switch (elf_elfheader (sub)->e_ident[EI_CLASS]) | |
351f65ca | 12053 | { |
aebf9be7 NC |
12054 | case ELFCLASS64: iclass = "ELFCLASS64"; break; |
12055 | case ELFCLASS32: iclass = "ELFCLASS32"; break; | |
12056 | case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; | |
12057 | default: abort (); | |
351f65ca L |
12058 | } |
12059 | ||
12060 | bfd_set_error (bfd_error_wrong_format); | |
4eca0228 | 12061 | _bfd_error_handler |
695344c0 | 12062 | /* xgettext:c-format */ |
351f65ca L |
12063 | (_("%B: file class %s incompatible with %s"), |
12064 | sub, iclass, oclass); | |
12065 | } | |
12066 | ||
12067 | goto error_return; | |
12068 | } | |
c152c796 AM |
12069 | } |
12070 | } | |
12071 | } | |
12072 | ||
c0f00686 L |
12073 | /* Free symbol buffer if needed. */ |
12074 | if (!info->reduce_memory_overheads) | |
12075 | { | |
c72f2fb2 | 12076 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
3fcd97f1 JJ |
12077 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
12078 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
12079 | { |
12080 | free (elf_tdata (sub)->symbuf); | |
12081 | elf_tdata (sub)->symbuf = NULL; | |
12082 | } | |
12083 | } | |
12084 | ||
c152c796 AM |
12085 | /* Output any global symbols that got converted to local in a |
12086 | version script or due to symbol visibility. We do this in a | |
12087 | separate step since ELF requires all local symbols to appear | |
12088 | prior to any global symbols. FIXME: We should only do this if | |
12089 | some global symbols were, in fact, converted to become local. | |
12090 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
12091 | eoinfo.failed = FALSE; | |
8b127cbc | 12092 | eoinfo.flinfo = &flinfo; |
c152c796 | 12093 | eoinfo.localsyms = TRUE; |
34a79995 | 12094 | eoinfo.file_sym_done = FALSE; |
7686d77d | 12095 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
12096 | if (eoinfo.failed) |
12097 | return FALSE; | |
12098 | ||
4e617b1e PB |
12099 | /* If backend needs to output some local symbols not present in the hash |
12100 | table, do it now. */ | |
8539e4e8 AM |
12101 | if (bed->elf_backend_output_arch_local_syms |
12102 | && (info->strip != strip_all || emit_relocs)) | |
4e617b1e | 12103 | { |
6e0b88f1 | 12104 | typedef int (*out_sym_func) |
4e617b1e PB |
12105 | (void *, const char *, Elf_Internal_Sym *, asection *, |
12106 | struct elf_link_hash_entry *); | |
12107 | ||
12108 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
ef10c3ac L |
12109 | (abfd, info, &flinfo, |
12110 | (out_sym_func) elf_link_output_symstrtab))) | |
4e617b1e PB |
12111 | return FALSE; |
12112 | } | |
12113 | ||
c152c796 AM |
12114 | /* That wrote out all the local symbols. Finish up the symbol table |
12115 | with the global symbols. Even if we want to strip everything we | |
12116 | can, we still need to deal with those global symbols that got | |
12117 | converted to local in a version script. */ | |
12118 | ||
12119 | /* The sh_info field records the index of the first non local symbol. */ | |
12120 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
12121 | ||
12122 | if (dynamic | |
64f52338 AM |
12123 | && htab->dynsym != NULL |
12124 | && htab->dynsym->output_section != bfd_abs_section_ptr) | |
c152c796 AM |
12125 | { |
12126 | Elf_Internal_Sym sym; | |
64f52338 | 12127 | bfd_byte *dynsym = htab->dynsym->contents; |
90ac2420 | 12128 | |
64f52338 AM |
12129 | o = htab->dynsym->output_section; |
12130 | elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1; | |
c152c796 AM |
12131 | |
12132 | /* Write out the section symbols for the output sections. */ | |
0e1862bb | 12133 | if (bfd_link_pic (info) |
64f52338 | 12134 | || htab->is_relocatable_executable) |
c152c796 AM |
12135 | { |
12136 | asection *s; | |
12137 | ||
12138 | sym.st_size = 0; | |
12139 | sym.st_name = 0; | |
12140 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
12141 | sym.st_other = 0; | |
35fc36a8 | 12142 | sym.st_target_internal = 0; |
c152c796 AM |
12143 | |
12144 | for (s = abfd->sections; s != NULL; s = s->next) | |
12145 | { | |
12146 | int indx; | |
12147 | bfd_byte *dest; | |
12148 | long dynindx; | |
12149 | ||
c152c796 | 12150 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
12151 | if (dynindx <= 0) |
12152 | continue; | |
12153 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
12154 | BFD_ASSERT (indx > 0); |
12155 | sym.st_shndx = indx; | |
c0d5a53d L |
12156 | if (! check_dynsym (abfd, &sym)) |
12157 | return FALSE; | |
c152c796 AM |
12158 | sym.st_value = s->vma; |
12159 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
12160 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
12161 | } | |
c152c796 AM |
12162 | } |
12163 | ||
12164 | /* Write out the local dynsyms. */ | |
64f52338 | 12165 | if (htab->dynlocal) |
c152c796 AM |
12166 | { |
12167 | struct elf_link_local_dynamic_entry *e; | |
64f52338 | 12168 | for (e = htab->dynlocal; e ; e = e->next) |
c152c796 AM |
12169 | { |
12170 | asection *s; | |
12171 | bfd_byte *dest; | |
12172 | ||
935bd1e0 | 12173 | /* Copy the internal symbol and turn off visibility. |
c152c796 AM |
12174 | Note that we saved a word of storage and overwrote |
12175 | the original st_name with the dynstr_index. */ | |
12176 | sym = e->isym; | |
935bd1e0 | 12177 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); |
c152c796 | 12178 | |
cb33740c AM |
12179 | s = bfd_section_from_elf_index (e->input_bfd, |
12180 | e->isym.st_shndx); | |
12181 | if (s != NULL) | |
c152c796 | 12182 | { |
c152c796 AM |
12183 | sym.st_shndx = |
12184 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
12185 | if (! check_dynsym (abfd, &sym)) |
12186 | return FALSE; | |
c152c796 AM |
12187 | sym.st_value = (s->output_section->vma |
12188 | + s->output_offset | |
12189 | + e->isym.st_value); | |
12190 | } | |
12191 | ||
c152c796 AM |
12192 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; |
12193 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
12194 | } | |
12195 | } | |
c152c796 AM |
12196 | } |
12197 | ||
12198 | /* We get the global symbols from the hash table. */ | |
12199 | eoinfo.failed = FALSE; | |
12200 | eoinfo.localsyms = FALSE; | |
8b127cbc | 12201 | eoinfo.flinfo = &flinfo; |
7686d77d | 12202 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
12203 | if (eoinfo.failed) |
12204 | return FALSE; | |
12205 | ||
12206 | /* If backend needs to output some symbols not present in the hash | |
12207 | table, do it now. */ | |
8539e4e8 AM |
12208 | if (bed->elf_backend_output_arch_syms |
12209 | && (info->strip != strip_all || emit_relocs)) | |
c152c796 | 12210 | { |
6e0b88f1 | 12211 | typedef int (*out_sym_func) |
c152c796 AM |
12212 | (void *, const char *, Elf_Internal_Sym *, asection *, |
12213 | struct elf_link_hash_entry *); | |
12214 | ||
12215 | if (! ((*bed->elf_backend_output_arch_syms) | |
ef10c3ac L |
12216 | (abfd, info, &flinfo, |
12217 | (out_sym_func) elf_link_output_symstrtab))) | |
c152c796 AM |
12218 | return FALSE; |
12219 | } | |
12220 | ||
ef10c3ac L |
12221 | /* Finalize the .strtab section. */ |
12222 | _bfd_elf_strtab_finalize (flinfo.symstrtab); | |
12223 | ||
12224 | /* Swap out the .strtab section. */ | |
12225 | if (!elf_link_swap_symbols_out (&flinfo)) | |
c152c796 AM |
12226 | return FALSE; |
12227 | ||
12228 | /* Now we know the size of the symtab section. */ | |
c152c796 AM |
12229 | if (bfd_get_symcount (abfd) > 0) |
12230 | { | |
ee3b52e9 L |
12231 | /* Finish up and write out the symbol string table (.strtab) |
12232 | section. */ | |
ad32986f | 12233 | Elf_Internal_Shdr *symstrtab_hdr = NULL; |
8539e4e8 AM |
12234 | file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; |
12235 | ||
ad32986f | 12236 | if (elf_symtab_shndx_list (abfd)) |
8539e4e8 | 12237 | { |
ad32986f | 12238 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
8539e4e8 | 12239 | |
ad32986f NC |
12240 | if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) |
12241 | { | |
12242 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
12243 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
12244 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
12245 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
12246 | symtab_shndx_hdr->sh_size = amt; | |
8539e4e8 | 12247 | |
ad32986f NC |
12248 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, |
12249 | off, TRUE); | |
12250 | ||
12251 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
12252 | || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) | |
12253 | return FALSE; | |
12254 | } | |
8539e4e8 | 12255 | } |
ee3b52e9 L |
12256 | |
12257 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
12258 | /* sh_name was set in prep_headers. */ | |
12259 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 12260 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
ee3b52e9 | 12261 | symstrtab_hdr->sh_addr = 0; |
ef10c3ac | 12262 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); |
ee3b52e9 L |
12263 | symstrtab_hdr->sh_entsize = 0; |
12264 | symstrtab_hdr->sh_link = 0; | |
12265 | symstrtab_hdr->sh_info = 0; | |
12266 | /* sh_offset is set just below. */ | |
12267 | symstrtab_hdr->sh_addralign = 1; | |
12268 | ||
12269 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, | |
12270 | off, TRUE); | |
12271 | elf_next_file_pos (abfd) = off; | |
12272 | ||
c152c796 | 12273 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 12274 | || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) |
c152c796 AM |
12275 | return FALSE; |
12276 | } | |
12277 | ||
76359541 TP |
12278 | if (info->out_implib_bfd && !elf_output_implib (abfd, info)) |
12279 | { | |
4eca0228 AM |
12280 | _bfd_error_handler (_("%B: failed to generate import library"), |
12281 | info->out_implib_bfd); | |
76359541 TP |
12282 | return FALSE; |
12283 | } | |
12284 | ||
c152c796 AM |
12285 | /* Adjust the relocs to have the correct symbol indices. */ |
12286 | for (o = abfd->sections; o != NULL; o = o->next) | |
12287 | { | |
d4730f92 | 12288 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
28dbcedc | 12289 | bfd_boolean sort; |
10bbbc1d | 12290 | |
c152c796 AM |
12291 | if ((o->flags & SEC_RELOC) == 0) |
12292 | continue; | |
12293 | ||
28dbcedc | 12294 | sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); |
bca6d0e3 | 12295 | if (esdo->rel.hdr != NULL |
10bbbc1d | 12296 | && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info)) |
bca6d0e3 AM |
12297 | return FALSE; |
12298 | if (esdo->rela.hdr != NULL | |
10bbbc1d | 12299 | && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info)) |
bca6d0e3 | 12300 | return FALSE; |
c152c796 AM |
12301 | |
12302 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
12303 | trying to swap the relocs out itself. */ | |
12304 | o->reloc_count = 0; | |
12305 | } | |
12306 | ||
12307 | if (dynamic && info->combreloc && dynobj != NULL) | |
12308 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
12309 | ||
12310 | /* If we are linking against a dynamic object, or generating a | |
12311 | shared library, finish up the dynamic linking information. */ | |
12312 | if (dynamic) | |
12313 | { | |
12314 | bfd_byte *dyncon, *dynconend; | |
12315 | ||
12316 | /* Fix up .dynamic entries. */ | |
3d4d4302 | 12317 | o = bfd_get_linker_section (dynobj, ".dynamic"); |
c152c796 AM |
12318 | BFD_ASSERT (o != NULL); |
12319 | ||
12320 | dyncon = o->contents; | |
eea6121a | 12321 | dynconend = o->contents + o->size; |
c152c796 AM |
12322 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
12323 | { | |
12324 | Elf_Internal_Dyn dyn; | |
12325 | const char *name; | |
12326 | unsigned int type; | |
64487780 AM |
12327 | bfd_size_type sh_size; |
12328 | bfd_vma sh_addr; | |
c152c796 AM |
12329 | |
12330 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12331 | ||
12332 | switch (dyn.d_tag) | |
12333 | { | |
12334 | default: | |
12335 | continue; | |
12336 | case DT_NULL: | |
12337 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
12338 | { | |
12339 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
12340 | { | |
12341 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
12342 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
12343 | default: continue; | |
12344 | } | |
12345 | dyn.d_un.d_val = relativecount; | |
12346 | relativecount = 0; | |
12347 | break; | |
12348 | } | |
12349 | continue; | |
12350 | ||
12351 | case DT_INIT: | |
12352 | name = info->init_function; | |
12353 | goto get_sym; | |
12354 | case DT_FINI: | |
12355 | name = info->fini_function; | |
12356 | get_sym: | |
12357 | { | |
12358 | struct elf_link_hash_entry *h; | |
12359 | ||
64f52338 | 12360 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
c152c796 AM |
12361 | if (h != NULL |
12362 | && (h->root.type == bfd_link_hash_defined | |
12363 | || h->root.type == bfd_link_hash_defweak)) | |
12364 | { | |
bef26483 | 12365 | dyn.d_un.d_ptr = h->root.u.def.value; |
c152c796 AM |
12366 | o = h->root.u.def.section; |
12367 | if (o->output_section != NULL) | |
bef26483 | 12368 | dyn.d_un.d_ptr += (o->output_section->vma |
c152c796 AM |
12369 | + o->output_offset); |
12370 | else | |
12371 | { | |
12372 | /* The symbol is imported from another shared | |
12373 | library and does not apply to this one. */ | |
bef26483 | 12374 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
12375 | } |
12376 | break; | |
12377 | } | |
12378 | } | |
12379 | continue; | |
12380 | ||
12381 | case DT_PREINIT_ARRAYSZ: | |
12382 | name = ".preinit_array"; | |
4ade44b7 | 12383 | goto get_out_size; |
c152c796 AM |
12384 | case DT_INIT_ARRAYSZ: |
12385 | name = ".init_array"; | |
4ade44b7 | 12386 | goto get_out_size; |
c152c796 AM |
12387 | case DT_FINI_ARRAYSZ: |
12388 | name = ".fini_array"; | |
4ade44b7 | 12389 | get_out_size: |
c152c796 AM |
12390 | o = bfd_get_section_by_name (abfd, name); |
12391 | if (o == NULL) | |
12392 | { | |
4eca0228 | 12393 | _bfd_error_handler |
4ade44b7 | 12394 | (_("could not find section %s"), name); |
c152c796 AM |
12395 | goto error_return; |
12396 | } | |
eea6121a | 12397 | if (o->size == 0) |
4eca0228 | 12398 | _bfd_error_handler |
c152c796 | 12399 | (_("warning: %s section has zero size"), name); |
eea6121a | 12400 | dyn.d_un.d_val = o->size; |
c152c796 AM |
12401 | break; |
12402 | ||
12403 | case DT_PREINIT_ARRAY: | |
12404 | name = ".preinit_array"; | |
4ade44b7 | 12405 | goto get_out_vma; |
c152c796 AM |
12406 | case DT_INIT_ARRAY: |
12407 | name = ".init_array"; | |
4ade44b7 | 12408 | goto get_out_vma; |
c152c796 AM |
12409 | case DT_FINI_ARRAY: |
12410 | name = ".fini_array"; | |
4ade44b7 AM |
12411 | get_out_vma: |
12412 | o = bfd_get_section_by_name (abfd, name); | |
12413 | goto do_vma; | |
c152c796 AM |
12414 | |
12415 | case DT_HASH: | |
12416 | name = ".hash"; | |
12417 | goto get_vma; | |
fdc90cb4 JJ |
12418 | case DT_GNU_HASH: |
12419 | name = ".gnu.hash"; | |
12420 | goto get_vma; | |
c152c796 AM |
12421 | case DT_STRTAB: |
12422 | name = ".dynstr"; | |
12423 | goto get_vma; | |
12424 | case DT_SYMTAB: | |
12425 | name = ".dynsym"; | |
12426 | goto get_vma; | |
12427 | case DT_VERDEF: | |
12428 | name = ".gnu.version_d"; | |
12429 | goto get_vma; | |
12430 | case DT_VERNEED: | |
12431 | name = ".gnu.version_r"; | |
12432 | goto get_vma; | |
12433 | case DT_VERSYM: | |
12434 | name = ".gnu.version"; | |
12435 | get_vma: | |
4ade44b7 AM |
12436 | o = bfd_get_linker_section (dynobj, name); |
12437 | do_vma: | |
b3293efa | 12438 | if (o == NULL || bfd_is_abs_section (o->output_section)) |
c152c796 | 12439 | { |
4eca0228 | 12440 | _bfd_error_handler |
4ade44b7 | 12441 | (_("could not find section %s"), name); |
c152c796 AM |
12442 | goto error_return; |
12443 | } | |
894891db NC |
12444 | if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) |
12445 | { | |
4eca0228 | 12446 | _bfd_error_handler |
894891db NC |
12447 | (_("warning: section '%s' is being made into a note"), name); |
12448 | bfd_set_error (bfd_error_nonrepresentable_section); | |
12449 | goto error_return; | |
12450 | } | |
4ade44b7 | 12451 | dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; |
c152c796 AM |
12452 | break; |
12453 | ||
12454 | case DT_REL: | |
12455 | case DT_RELA: | |
12456 | case DT_RELSZ: | |
12457 | case DT_RELASZ: | |
12458 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
12459 | type = SHT_REL; | |
12460 | else | |
12461 | type = SHT_RELA; | |
64487780 AM |
12462 | sh_size = 0; |
12463 | sh_addr = 0; | |
c152c796 AM |
12464 | for (i = 1; i < elf_numsections (abfd); i++) |
12465 | { | |
12466 | Elf_Internal_Shdr *hdr; | |
12467 | ||
12468 | hdr = elf_elfsections (abfd)[i]; | |
12469 | if (hdr->sh_type == type | |
12470 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
12471 | { | |
64487780 AM |
12472 | sh_size += hdr->sh_size; |
12473 | if (sh_addr == 0 | |
12474 | || sh_addr > hdr->sh_addr) | |
12475 | sh_addr = hdr->sh_addr; | |
c152c796 AM |
12476 | } |
12477 | } | |
64487780 | 12478 | |
64f52338 AM |
12479 | if (bed->dtrel_excludes_plt && htab->srelplt != NULL) |
12480 | { | |
12481 | /* Don't count procedure linkage table relocs in the | |
12482 | overall reloc count. */ | |
64487780 AM |
12483 | sh_size -= htab->srelplt->size; |
12484 | if (sh_size == 0) | |
12485 | /* If the size is zero, make the address zero too. | |
12486 | This is to avoid a glibc bug. If the backend | |
12487 | emits DT_RELA/DT_RELASZ even when DT_RELASZ is | |
12488 | zero, then we'll put DT_RELA at the end of | |
12489 | DT_JMPREL. glibc will interpret the end of | |
12490 | DT_RELA matching the end of DT_JMPREL as the | |
12491 | case where DT_RELA includes DT_JMPREL, and for | |
12492 | LD_BIND_NOW will decide that processing DT_RELA | |
12493 | will process the PLT relocs too. Net result: | |
12494 | No PLT relocs applied. */ | |
12495 | sh_addr = 0; | |
12496 | ||
64f52338 AM |
12497 | /* If .rela.plt is the first .rela section, exclude |
12498 | it from DT_RELA. */ | |
64487780 AM |
12499 | else if (sh_addr == (htab->srelplt->output_section->vma |
12500 | + htab->srelplt->output_offset)) | |
12501 | sh_addr += htab->srelplt->size; | |
64f52338 | 12502 | } |
64487780 AM |
12503 | |
12504 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
12505 | dyn.d_un.d_val = sh_size; | |
12506 | else | |
12507 | dyn.d_un.d_ptr = sh_addr; | |
c152c796 AM |
12508 | break; |
12509 | } | |
12510 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
12511 | } | |
12512 | } | |
12513 | ||
12514 | /* If we have created any dynamic sections, then output them. */ | |
12515 | if (dynobj != NULL) | |
12516 | { | |
12517 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
12518 | goto error_return; | |
12519 | ||
943284cc | 12520 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
0e1862bb | 12521 | if (((info->warn_shared_textrel && bfd_link_pic (info)) |
be7b303d | 12522 | || info->error_textrel) |
3d4d4302 | 12523 | && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) |
943284cc DJ |
12524 | { |
12525 | bfd_byte *dyncon, *dynconend; | |
12526 | ||
943284cc DJ |
12527 | dyncon = o->contents; |
12528 | dynconend = o->contents + o->size; | |
12529 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
12530 | { | |
12531 | Elf_Internal_Dyn dyn; | |
12532 | ||
12533 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12534 | ||
12535 | if (dyn.d_tag == DT_TEXTREL) | |
12536 | { | |
c192a133 AM |
12537 | if (info->error_textrel) |
12538 | info->callbacks->einfo | |
12539 | (_("%P%X: read-only segment has dynamic relocations.\n")); | |
12540 | else | |
12541 | info->callbacks->einfo | |
12542 | (_("%P: warning: creating a DT_TEXTREL in a shared object.\n")); | |
943284cc DJ |
12543 | break; |
12544 | } | |
12545 | } | |
12546 | } | |
12547 | ||
c152c796 AM |
12548 | for (o = dynobj->sections; o != NULL; o = o->next) |
12549 | { | |
12550 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 12551 | || o->size == 0 |
c152c796 AM |
12552 | || o->output_section == bfd_abs_section_ptr) |
12553 | continue; | |
12554 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
12555 | { | |
12556 | /* At this point, we are only interested in sections | |
12557 | created by _bfd_elf_link_create_dynamic_sections. */ | |
12558 | continue; | |
12559 | } | |
64f52338 | 12560 | if (htab->stab_info.stabstr == o) |
3722b82f | 12561 | continue; |
64f52338 | 12562 | if (htab->eh_info.hdr_sec == o) |
eea6121a | 12563 | continue; |
3d4d4302 | 12564 | if (strcmp (o->name, ".dynstr") != 0) |
c152c796 AM |
12565 | { |
12566 | if (! bfd_set_section_contents (abfd, o->output_section, | |
12567 | o->contents, | |
37b01f6a DG |
12568 | (file_ptr) o->output_offset |
12569 | * bfd_octets_per_byte (abfd), | |
eea6121a | 12570 | o->size)) |
c152c796 AM |
12571 | goto error_return; |
12572 | } | |
12573 | else | |
12574 | { | |
12575 | /* The contents of the .dynstr section are actually in a | |
12576 | stringtab. */ | |
8539e4e8 AM |
12577 | file_ptr off; |
12578 | ||
c152c796 AM |
12579 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; |
12580 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
64f52338 | 12581 | || !_bfd_elf_strtab_emit (abfd, htab->dynstr)) |
c152c796 AM |
12582 | goto error_return; |
12583 | } | |
12584 | } | |
12585 | } | |
12586 | ||
7bdf4127 | 12587 | if (!info->resolve_section_groups) |
c152c796 AM |
12588 | { |
12589 | bfd_boolean failed = FALSE; | |
12590 | ||
7bdf4127 | 12591 | BFD_ASSERT (bfd_link_relocatable (info)); |
c152c796 AM |
12592 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
12593 | if (failed) | |
12594 | goto error_return; | |
12595 | } | |
12596 | ||
12597 | /* If we have optimized stabs strings, output them. */ | |
64f52338 | 12598 | if (htab->stab_info.stabstr != NULL) |
c152c796 | 12599 | { |
64f52338 | 12600 | if (!_bfd_write_stab_strings (abfd, &htab->stab_info)) |
c152c796 AM |
12601 | goto error_return; |
12602 | } | |
12603 | ||
9f7c3e5e AM |
12604 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) |
12605 | goto error_return; | |
c152c796 | 12606 | |
9f7c3e5e | 12607 | elf_final_link_free (abfd, &flinfo); |
c152c796 | 12608 | |
12bd6957 | 12609 | elf_linker (abfd) = TRUE; |
c152c796 | 12610 | |
104d59d1 JM |
12611 | if (attr_section) |
12612 | { | |
a50b1753 | 12613 | bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); |
104d59d1 | 12614 | if (contents == NULL) |
d0f16d5e | 12615 | return FALSE; /* Bail out and fail. */ |
104d59d1 JM |
12616 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
12617 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
12618 | free (contents); | |
12619 | } | |
12620 | ||
c152c796 AM |
12621 | return TRUE; |
12622 | ||
12623 | error_return: | |
9f7c3e5e | 12624 | elf_final_link_free (abfd, &flinfo); |
c152c796 AM |
12625 | return FALSE; |
12626 | } | |
12627 | \f | |
5241d853 RS |
12628 | /* Initialize COOKIE for input bfd ABFD. */ |
12629 | ||
12630 | static bfd_boolean | |
12631 | init_reloc_cookie (struct elf_reloc_cookie *cookie, | |
12632 | struct bfd_link_info *info, bfd *abfd) | |
12633 | { | |
12634 | Elf_Internal_Shdr *symtab_hdr; | |
12635 | const struct elf_backend_data *bed; | |
12636 | ||
12637 | bed = get_elf_backend_data (abfd); | |
12638 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12639 | ||
12640 | cookie->abfd = abfd; | |
12641 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
12642 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
12643 | if (cookie->bad_symtab) | |
12644 | { | |
12645 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
12646 | cookie->extsymoff = 0; | |
12647 | } | |
12648 | else | |
12649 | { | |
12650 | cookie->locsymcount = symtab_hdr->sh_info; | |
12651 | cookie->extsymoff = symtab_hdr->sh_info; | |
12652 | } | |
12653 | ||
12654 | if (bed->s->arch_size == 32) | |
12655 | cookie->r_sym_shift = 8; | |
12656 | else | |
12657 | cookie->r_sym_shift = 32; | |
12658 | ||
12659 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
12660 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
12661 | { | |
12662 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
12663 | cookie->locsymcount, 0, | |
12664 | NULL, NULL, NULL); | |
12665 | if (cookie->locsyms == NULL) | |
12666 | { | |
12667 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
12668 | return FALSE; | |
12669 | } | |
12670 | if (info->keep_memory) | |
12671 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
12672 | } | |
12673 | return TRUE; | |
12674 | } | |
12675 | ||
12676 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
12677 | ||
12678 | static void | |
12679 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
12680 | { | |
12681 | Elf_Internal_Shdr *symtab_hdr; | |
12682 | ||
12683 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12684 | if (cookie->locsyms != NULL | |
12685 | && symtab_hdr->contents != (unsigned char *) cookie->locsyms) | |
12686 | free (cookie->locsyms); | |
12687 | } | |
12688 | ||
12689 | /* Initialize the relocation information in COOKIE for input section SEC | |
12690 | of input bfd ABFD. */ | |
12691 | ||
12692 | static bfd_boolean | |
12693 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12694 | struct bfd_link_info *info, bfd *abfd, | |
12695 | asection *sec) | |
12696 | { | |
5241d853 RS |
12697 | if (sec->reloc_count == 0) |
12698 | { | |
12699 | cookie->rels = NULL; | |
12700 | cookie->relend = NULL; | |
12701 | } | |
12702 | else | |
12703 | { | |
5241d853 RS |
12704 | cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, |
12705 | info->keep_memory); | |
12706 | if (cookie->rels == NULL) | |
12707 | return FALSE; | |
12708 | cookie->rel = cookie->rels; | |
056bafd4 | 12709 | cookie->relend = cookie->rels + sec->reloc_count; |
5241d853 RS |
12710 | } |
12711 | cookie->rel = cookie->rels; | |
12712 | return TRUE; | |
12713 | } | |
12714 | ||
12715 | /* Free the memory allocated by init_reloc_cookie_rels, | |
12716 | if appropriate. */ | |
12717 | ||
12718 | static void | |
12719 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12720 | asection *sec) | |
12721 | { | |
12722 | if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) | |
12723 | free (cookie->rels); | |
12724 | } | |
12725 | ||
12726 | /* Initialize the whole of COOKIE for input section SEC. */ | |
12727 | ||
12728 | static bfd_boolean | |
12729 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12730 | struct bfd_link_info *info, | |
12731 | asection *sec) | |
12732 | { | |
12733 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
12734 | goto error1; | |
12735 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
12736 | goto error2; | |
12737 | return TRUE; | |
12738 | ||
12739 | error2: | |
12740 | fini_reloc_cookie (cookie, sec->owner); | |
12741 | error1: | |
12742 | return FALSE; | |
12743 | } | |
12744 | ||
12745 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
12746 | if appropriate. */ | |
12747 | ||
12748 | static void | |
12749 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12750 | asection *sec) | |
12751 | { | |
12752 | fini_reloc_cookie_rels (cookie, sec); | |
12753 | fini_reloc_cookie (cookie, sec->owner); | |
12754 | } | |
12755 | \f | |
c152c796 AM |
12756 | /* Garbage collect unused sections. */ |
12757 | ||
07adf181 AM |
12758 | /* Default gc_mark_hook. */ |
12759 | ||
12760 | asection * | |
12761 | _bfd_elf_gc_mark_hook (asection *sec, | |
12762 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12763 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
12764 | struct elf_link_hash_entry *h, | |
12765 | Elf_Internal_Sym *sym) | |
12766 | { | |
12767 | if (h != NULL) | |
12768 | { | |
12769 | switch (h->root.type) | |
12770 | { | |
12771 | case bfd_link_hash_defined: | |
12772 | case bfd_link_hash_defweak: | |
12773 | return h->root.u.def.section; | |
12774 | ||
12775 | case bfd_link_hash_common: | |
12776 | return h->root.u.c.p->section; | |
12777 | ||
12778 | default: | |
12779 | break; | |
12780 | } | |
12781 | } | |
12782 | else | |
12783 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
12784 | ||
12785 | return NULL; | |
12786 | } | |
12787 | ||
b7c871ed L |
12788 | /* Return the global debug definition section. */ |
12789 | ||
12790 | static asection * | |
12791 | elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED, | |
12792 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12793 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
12794 | struct elf_link_hash_entry *h, | |
12795 | Elf_Internal_Sym *sym ATTRIBUTE_UNUSED) | |
12796 | { | |
12797 | if (h != NULL | |
12798 | && (h->root.type == bfd_link_hash_defined | |
12799 | || h->root.type == bfd_link_hash_defweak) | |
12800 | && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0) | |
12801 | return h->root.u.def.section; | |
12802 | ||
12803 | return NULL; | |
12804 | } | |
12805 | ||
5241d853 RS |
12806 | /* COOKIE->rel describes a relocation against section SEC, which is |
12807 | a section we've decided to keep. Return the section that contains | |
12808 | the relocation symbol, or NULL if no section contains it. */ | |
12809 | ||
12810 | asection * | |
12811 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
12812 | elf_gc_mark_hook_fn gc_mark_hook, | |
1cce69b9 AM |
12813 | struct elf_reloc_cookie *cookie, |
12814 | bfd_boolean *start_stop) | |
5241d853 RS |
12815 | { |
12816 | unsigned long r_symndx; | |
12817 | struct elf_link_hash_entry *h; | |
12818 | ||
12819 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
cf35638d | 12820 | if (r_symndx == STN_UNDEF) |
5241d853 RS |
12821 | return NULL; |
12822 | ||
12823 | if (r_symndx >= cookie->locsymcount | |
12824 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
12825 | { | |
12826 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
263ddf68 L |
12827 | if (h == NULL) |
12828 | { | |
12829 | info->callbacks->einfo (_("%F%P: corrupt input: %B\n"), | |
12830 | sec->owner); | |
12831 | return NULL; | |
12832 | } | |
5241d853 RS |
12833 | while (h->root.type == bfd_link_hash_indirect |
12834 | || h->root.type == bfd_link_hash_warning) | |
12835 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1d5316ab | 12836 | h->mark = 1; |
4e6b54a6 AM |
12837 | /* If this symbol is weak and there is a non-weak definition, we |
12838 | keep the non-weak definition because many backends put | |
12839 | dynamic reloc info on the non-weak definition for code | |
12840 | handling copy relocs. */ | |
60d67dc8 AM |
12841 | if (h->is_weakalias) |
12842 | weakdef (h)->mark = 1; | |
1cce69b9 | 12843 | |
a6a4679f | 12844 | if (start_stop != NULL) |
1cce69b9 | 12845 | { |
7dba9362 AM |
12846 | /* To work around a glibc bug, mark XXX input sections |
12847 | when there is a reference to __start_XXX or __stop_XXX | |
12848 | symbols. */ | |
cbd0eecf | 12849 | if (h->start_stop) |
1cce69b9 | 12850 | { |
cbd0eecf | 12851 | asection *s = h->u2.start_stop_section; |
a6a4679f AM |
12852 | *start_stop = !s->gc_mark; |
12853 | return s; | |
1cce69b9 AM |
12854 | } |
12855 | } | |
12856 | ||
5241d853 RS |
12857 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); |
12858 | } | |
12859 | ||
12860 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
12861 | &cookie->locsyms[r_symndx]); | |
12862 | } | |
12863 | ||
12864 | /* COOKIE->rel describes a relocation against section SEC, which is | |
12865 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 12866 | the relocation symbol. */ |
5241d853 RS |
12867 | |
12868 | bfd_boolean | |
12869 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, | |
12870 | asection *sec, | |
12871 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 12872 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
12873 | { |
12874 | asection *rsec; | |
1cce69b9 | 12875 | bfd_boolean start_stop = FALSE; |
5241d853 | 12876 | |
1cce69b9 AM |
12877 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); |
12878 | while (rsec != NULL) | |
5241d853 | 12879 | { |
1cce69b9 AM |
12880 | if (!rsec->gc_mark) |
12881 | { | |
12882 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour | |
12883 | || (rsec->owner->flags & DYNAMIC) != 0) | |
12884 | rsec->gc_mark = 1; | |
12885 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) | |
12886 | return FALSE; | |
12887 | } | |
12888 | if (!start_stop) | |
12889 | break; | |
199af150 | 12890 | rsec = bfd_get_next_section_by_name (rsec->owner, rsec); |
5241d853 RS |
12891 | } |
12892 | return TRUE; | |
12893 | } | |
12894 | ||
07adf181 AM |
12895 | /* The mark phase of garbage collection. For a given section, mark |
12896 | it and any sections in this section's group, and all the sections | |
12897 | which define symbols to which it refers. */ | |
12898 | ||
ccfa59ea AM |
12899 | bfd_boolean |
12900 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
12901 | asection *sec, | |
6a5bb875 | 12902 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
12903 | { |
12904 | bfd_boolean ret; | |
9d0a14d3 | 12905 | asection *group_sec, *eh_frame; |
c152c796 AM |
12906 | |
12907 | sec->gc_mark = 1; | |
12908 | ||
12909 | /* Mark all the sections in the group. */ | |
12910 | group_sec = elf_section_data (sec)->next_in_group; | |
12911 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 12912 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
12913 | return FALSE; |
12914 | ||
12915 | /* Look through the section relocs. */ | |
12916 | ret = TRUE; | |
9d0a14d3 RS |
12917 | eh_frame = elf_eh_frame_section (sec->owner); |
12918 | if ((sec->flags & SEC_RELOC) != 0 | |
12919 | && sec->reloc_count > 0 | |
12920 | && sec != eh_frame) | |
c152c796 | 12921 | { |
5241d853 | 12922 | struct elf_reloc_cookie cookie; |
c152c796 | 12923 | |
5241d853 RS |
12924 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
12925 | ret = FALSE; | |
c152c796 | 12926 | else |
c152c796 | 12927 | { |
5241d853 | 12928 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 12929 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 RS |
12930 | { |
12931 | ret = FALSE; | |
12932 | break; | |
12933 | } | |
12934 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
12935 | } |
12936 | } | |
9d0a14d3 RS |
12937 | |
12938 | if (ret && eh_frame && elf_fde_list (sec)) | |
12939 | { | |
12940 | struct elf_reloc_cookie cookie; | |
12941 | ||
12942 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
12943 | ret = FALSE; | |
12944 | else | |
12945 | { | |
12946 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
12947 | gc_mark_hook, &cookie)) | |
12948 | ret = FALSE; | |
12949 | fini_reloc_cookie_for_section (&cookie, eh_frame); | |
12950 | } | |
12951 | } | |
12952 | ||
2f0c68f2 CM |
12953 | eh_frame = elf_section_eh_frame_entry (sec); |
12954 | if (ret && eh_frame && !eh_frame->gc_mark) | |
12955 | if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) | |
12956 | ret = FALSE; | |
12957 | ||
c152c796 AM |
12958 | return ret; |
12959 | } | |
12960 | ||
3c758495 TG |
12961 | /* Scan and mark sections in a special or debug section group. */ |
12962 | ||
12963 | static void | |
12964 | _bfd_elf_gc_mark_debug_special_section_group (asection *grp) | |
12965 | { | |
12966 | /* Point to first section of section group. */ | |
12967 | asection *ssec; | |
12968 | /* Used to iterate the section group. */ | |
12969 | asection *msec; | |
12970 | ||
12971 | bfd_boolean is_special_grp = TRUE; | |
12972 | bfd_boolean is_debug_grp = TRUE; | |
12973 | ||
12974 | /* First scan to see if group contains any section other than debug | |
12975 | and special section. */ | |
12976 | ssec = msec = elf_next_in_group (grp); | |
12977 | do | |
12978 | { | |
12979 | if ((msec->flags & SEC_DEBUGGING) == 0) | |
12980 | is_debug_grp = FALSE; | |
12981 | ||
12982 | if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) | |
12983 | is_special_grp = FALSE; | |
12984 | ||
12985 | msec = elf_next_in_group (msec); | |
12986 | } | |
12987 | while (msec != ssec); | |
12988 | ||
12989 | /* If this is a pure debug section group or pure special section group, | |
12990 | keep all sections in this group. */ | |
12991 | if (is_debug_grp || is_special_grp) | |
12992 | { | |
12993 | do | |
12994 | { | |
12995 | msec->gc_mark = 1; | |
12996 | msec = elf_next_in_group (msec); | |
12997 | } | |
12998 | while (msec != ssec); | |
12999 | } | |
13000 | } | |
13001 | ||
7f6ab9f8 AM |
13002 | /* Keep debug and special sections. */ |
13003 | ||
13004 | bfd_boolean | |
13005 | _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, | |
13006 | elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) | |
13007 | { | |
13008 | bfd *ibfd; | |
13009 | ||
c72f2fb2 | 13010 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
7f6ab9f8 AM |
13011 | { |
13012 | asection *isec; | |
13013 | bfd_boolean some_kept; | |
b40bf0a2 | 13014 | bfd_boolean debug_frag_seen; |
b7c871ed | 13015 | bfd_boolean has_kept_debug_info; |
7f6ab9f8 AM |
13016 | |
13017 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13018 | continue; | |
57963c05 AM |
13019 | isec = ibfd->sections; |
13020 | if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13021 | continue; | |
7f6ab9f8 | 13022 | |
b40bf0a2 NC |
13023 | /* Ensure all linker created sections are kept, |
13024 | see if any other section is already marked, | |
13025 | and note if we have any fragmented debug sections. */ | |
b7c871ed | 13026 | debug_frag_seen = some_kept = has_kept_debug_info = FALSE; |
7f6ab9f8 AM |
13027 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
13028 | { | |
13029 | if ((isec->flags & SEC_LINKER_CREATED) != 0) | |
13030 | isec->gc_mark = 1; | |
eb026f09 AM |
13031 | else if (isec->gc_mark |
13032 | && (isec->flags & SEC_ALLOC) != 0 | |
13033 | && elf_section_type (isec) != SHT_NOTE) | |
7f6ab9f8 | 13034 | some_kept = TRUE; |
b40bf0a2 | 13035 | |
535b785f | 13036 | if (!debug_frag_seen |
b40bf0a2 NC |
13037 | && (isec->flags & SEC_DEBUGGING) |
13038 | && CONST_STRNEQ (isec->name, ".debug_line.")) | |
13039 | debug_frag_seen = TRUE; | |
7f6ab9f8 AM |
13040 | } |
13041 | ||
eb026f09 AM |
13042 | /* If no non-note alloc section in this file will be kept, then |
13043 | we can toss out the debug and special sections. */ | |
7f6ab9f8 AM |
13044 | if (!some_kept) |
13045 | continue; | |
13046 | ||
13047 | /* Keep debug and special sections like .comment when they are | |
3c758495 TG |
13048 | not part of a group. Also keep section groups that contain |
13049 | just debug sections or special sections. */ | |
7f6ab9f8 | 13050 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
3c758495 TG |
13051 | { |
13052 | if ((isec->flags & SEC_GROUP) != 0) | |
13053 | _bfd_elf_gc_mark_debug_special_section_group (isec); | |
13054 | else if (((isec->flags & SEC_DEBUGGING) != 0 | |
13055 | || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) | |
13056 | && elf_next_in_group (isec) == NULL) | |
13057 | isec->gc_mark = 1; | |
b7c871ed L |
13058 | if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0) |
13059 | has_kept_debug_info = TRUE; | |
3c758495 | 13060 | } |
b40bf0a2 | 13061 | |
b40bf0a2 NC |
13062 | /* Look for CODE sections which are going to be discarded, |
13063 | and find and discard any fragmented debug sections which | |
13064 | are associated with that code section. */ | |
b7c871ed L |
13065 | if (debug_frag_seen) |
13066 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
13067 | if ((isec->flags & SEC_CODE) != 0 | |
13068 | && isec->gc_mark == 0) | |
13069 | { | |
13070 | unsigned int ilen; | |
13071 | asection *dsec; | |
b40bf0a2 | 13072 | |
b7c871ed | 13073 | ilen = strlen (isec->name); |
b40bf0a2 | 13074 | |
b7c871ed | 13075 | /* Association is determined by the name of the debug |
07d6d2b8 | 13076 | section containing the name of the code section as |
b7c871ed L |
13077 | a suffix. For example .debug_line.text.foo is a |
13078 | debug section associated with .text.foo. */ | |
13079 | for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) | |
13080 | { | |
13081 | unsigned int dlen; | |
b40bf0a2 | 13082 | |
b7c871ed L |
13083 | if (dsec->gc_mark == 0 |
13084 | || (dsec->flags & SEC_DEBUGGING) == 0) | |
13085 | continue; | |
b40bf0a2 | 13086 | |
b7c871ed | 13087 | dlen = strlen (dsec->name); |
b40bf0a2 | 13088 | |
b7c871ed L |
13089 | if (dlen > ilen |
13090 | && strncmp (dsec->name + (dlen - ilen), | |
13091 | isec->name, ilen) == 0) | |
b40bf0a2 | 13092 | dsec->gc_mark = 0; |
b7c871ed | 13093 | } |
b40bf0a2 | 13094 | } |
b7c871ed L |
13095 | |
13096 | /* Mark debug sections referenced by kept debug sections. */ | |
13097 | if (has_kept_debug_info) | |
13098 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
13099 | if (isec->gc_mark | |
13100 | && (isec->flags & SEC_DEBUGGING) != 0) | |
13101 | if (!_bfd_elf_gc_mark (info, isec, | |
13102 | elf_gc_mark_debug_section)) | |
13103 | return FALSE; | |
7f6ab9f8 AM |
13104 | } |
13105 | return TRUE; | |
13106 | } | |
13107 | ||
c152c796 | 13108 | static bfd_boolean |
ccabcbe5 | 13109 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
13110 | { |
13111 | bfd *sub; | |
ccabcbe5 | 13112 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 13113 | |
c72f2fb2 | 13114 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13115 | { |
13116 | asection *o; | |
13117 | ||
b19a8f85 | 13118 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 13119 | || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info)) |
b19a8f85 | 13120 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 | 13121 | continue; |
57963c05 AM |
13122 | o = sub->sections; |
13123 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13124 | continue; | |
c152c796 AM |
13125 | |
13126 | for (o = sub->sections; o != NULL; o = o->next) | |
13127 | { | |
a33dafc3 L |
13128 | /* When any section in a section group is kept, we keep all |
13129 | sections in the section group. If the first member of | |
13130 | the section group is excluded, we will also exclude the | |
13131 | group section. */ | |
13132 | if (o->flags & SEC_GROUP) | |
13133 | { | |
13134 | asection *first = elf_next_in_group (o); | |
13135 | o->gc_mark = first->gc_mark; | |
13136 | } | |
c152c796 | 13137 | |
1e7eae0d | 13138 | if (o->gc_mark) |
c152c796 AM |
13139 | continue; |
13140 | ||
13141 | /* Skip sweeping sections already excluded. */ | |
13142 | if (o->flags & SEC_EXCLUDE) | |
13143 | continue; | |
13144 | ||
13145 | /* Since this is early in the link process, it is simple | |
13146 | to remove a section from the output. */ | |
13147 | o->flags |= SEC_EXCLUDE; | |
13148 | ||
c55fe096 | 13149 | if (info->print_gc_sections && o->size != 0) |
695344c0 | 13150 | /* xgettext:c-format */ |
c08bb8dd AM |
13151 | _bfd_error_handler (_("Removing unused section '%A' in file '%B'"), |
13152 | o, sub); | |
c152c796 AM |
13153 | } |
13154 | } | |
13155 | ||
c152c796 AM |
13156 | return TRUE; |
13157 | } | |
13158 | ||
13159 | /* Propagate collected vtable information. This is called through | |
13160 | elf_link_hash_traverse. */ | |
13161 | ||
13162 | static bfd_boolean | |
13163 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
13164 | { | |
c152c796 | 13165 | /* Those that are not vtables. */ |
cbd0eecf L |
13166 | if (h->start_stop |
13167 | || h->u2.vtable == NULL | |
13168 | || h->u2.vtable->parent == NULL) | |
c152c796 AM |
13169 | return TRUE; |
13170 | ||
13171 | /* Those vtables that do not have parents, we cannot merge. */ | |
cbd0eecf | 13172 | if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
13173 | return TRUE; |
13174 | ||
13175 | /* If we've already been done, exit. */ | |
cbd0eecf | 13176 | if (h->u2.vtable->used && h->u2.vtable->used[-1]) |
c152c796 AM |
13177 | return TRUE; |
13178 | ||
13179 | /* Make sure the parent's table is up to date. */ | |
cbd0eecf | 13180 | elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp); |
c152c796 | 13181 | |
cbd0eecf | 13182 | if (h->u2.vtable->used == NULL) |
c152c796 AM |
13183 | { |
13184 | /* None of this table's entries were referenced. Re-use the | |
13185 | parent's table. */ | |
cbd0eecf L |
13186 | h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used; |
13187 | h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size; | |
c152c796 AM |
13188 | } |
13189 | else | |
13190 | { | |
13191 | size_t n; | |
13192 | bfd_boolean *cu, *pu; | |
13193 | ||
13194 | /* Or the parent's entries into ours. */ | |
cbd0eecf | 13195 | cu = h->u2.vtable->used; |
c152c796 | 13196 | cu[-1] = TRUE; |
cbd0eecf | 13197 | pu = h->u2.vtable->parent->u2.vtable->used; |
c152c796 AM |
13198 | if (pu != NULL) |
13199 | { | |
13200 | const struct elf_backend_data *bed; | |
13201 | unsigned int log_file_align; | |
13202 | ||
13203 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
13204 | log_file_align = bed->s->log_file_align; | |
cbd0eecf | 13205 | n = h->u2.vtable->parent->u2.vtable->size >> log_file_align; |
c152c796 AM |
13206 | while (n--) |
13207 | { | |
13208 | if (*pu) | |
13209 | *cu = TRUE; | |
13210 | pu++; | |
13211 | cu++; | |
13212 | } | |
13213 | } | |
13214 | } | |
13215 | ||
13216 | return TRUE; | |
13217 | } | |
13218 | ||
13219 | static bfd_boolean | |
13220 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
13221 | { | |
13222 | asection *sec; | |
13223 | bfd_vma hstart, hend; | |
13224 | Elf_Internal_Rela *relstart, *relend, *rel; | |
13225 | const struct elf_backend_data *bed; | |
13226 | unsigned int log_file_align; | |
13227 | ||
c152c796 AM |
13228 | /* Take care of both those symbols that do not describe vtables as |
13229 | well as those that are not loaded. */ | |
cbd0eecf L |
13230 | if (h->start_stop |
13231 | || h->u2.vtable == NULL | |
13232 | || h->u2.vtable->parent == NULL) | |
c152c796 AM |
13233 | return TRUE; |
13234 | ||
13235 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
13236 | || h->root.type == bfd_link_hash_defweak); | |
13237 | ||
13238 | sec = h->root.u.def.section; | |
13239 | hstart = h->root.u.def.value; | |
13240 | hend = hstart + h->size; | |
13241 | ||
13242 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
13243 | if (!relstart) | |
13244 | return *(bfd_boolean *) okp = FALSE; | |
13245 | bed = get_elf_backend_data (sec->owner); | |
13246 | log_file_align = bed->s->log_file_align; | |
13247 | ||
056bafd4 | 13248 | relend = relstart + sec->reloc_count; |
c152c796 AM |
13249 | |
13250 | for (rel = relstart; rel < relend; ++rel) | |
13251 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
13252 | { | |
13253 | /* If the entry is in use, do nothing. */ | |
cbd0eecf L |
13254 | if (h->u2.vtable->used |
13255 | && (rel->r_offset - hstart) < h->u2.vtable->size) | |
c152c796 AM |
13256 | { |
13257 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
cbd0eecf | 13258 | if (h->u2.vtable->used[entry]) |
c152c796 AM |
13259 | continue; |
13260 | } | |
13261 | /* Otherwise, kill it. */ | |
13262 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
13263 | } | |
13264 | ||
13265 | return TRUE; | |
13266 | } | |
13267 | ||
87538722 AM |
13268 | /* Mark sections containing dynamically referenced symbols. When |
13269 | building shared libraries, we must assume that any visible symbol is | |
13270 | referenced. */ | |
715df9b8 | 13271 | |
64d03ab5 AM |
13272 | bfd_boolean |
13273 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 13274 | { |
87538722 | 13275 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
d6f6f455 | 13276 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
87538722 | 13277 | |
715df9b8 EB |
13278 | if ((h->root.type == bfd_link_hash_defined |
13279 | || h->root.type == bfd_link_hash_defweak) | |
87538722 | 13280 | && (h->ref_dynamic |
c4621b33 | 13281 | || ((h->def_regular || ELF_COMMON_DEF_P (h)) |
87538722 | 13282 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL |
fd91d419 | 13283 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN |
0e1862bb | 13284 | && (!bfd_link_executable (info) |
22185505 | 13285 | || info->gc_keep_exported |
b407645f AM |
13286 | || info->export_dynamic |
13287 | || (h->dynamic | |
13288 | && d != NULL | |
13289 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
422f1182 | 13290 | && (h->versioned >= versioned |
54e8959c L |
13291 | || !bfd_hide_sym_by_version (info->version_info, |
13292 | h->root.root.string))))) | |
715df9b8 EB |
13293 | h->root.u.def.section->flags |= SEC_KEEP; |
13294 | ||
13295 | return TRUE; | |
13296 | } | |
3b36f7e6 | 13297 | |
74f0fb50 AM |
13298 | /* Keep all sections containing symbols undefined on the command-line, |
13299 | and the section containing the entry symbol. */ | |
13300 | ||
13301 | void | |
13302 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
13303 | { | |
13304 | struct bfd_sym_chain *sym; | |
13305 | ||
13306 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
13307 | { | |
13308 | struct elf_link_hash_entry *h; | |
13309 | ||
13310 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
13311 | FALSE, FALSE, FALSE); | |
13312 | ||
13313 | if (h != NULL | |
13314 | && (h->root.type == bfd_link_hash_defined | |
13315 | || h->root.type == bfd_link_hash_defweak) | |
f02cb058 AM |
13316 | && !bfd_is_abs_section (h->root.u.def.section) |
13317 | && !bfd_is_und_section (h->root.u.def.section)) | |
74f0fb50 AM |
13318 | h->root.u.def.section->flags |= SEC_KEEP; |
13319 | } | |
13320 | } | |
13321 | ||
2f0c68f2 CM |
13322 | bfd_boolean |
13323 | bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, | |
13324 | struct bfd_link_info *info) | |
13325 | { | |
13326 | bfd *ibfd = info->input_bfds; | |
13327 | ||
13328 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) | |
13329 | { | |
13330 | asection *sec; | |
13331 | struct elf_reloc_cookie cookie; | |
13332 | ||
13333 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13334 | continue; | |
57963c05 AM |
13335 | sec = ibfd->sections; |
13336 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13337 | continue; | |
2f0c68f2 CM |
13338 | |
13339 | if (!init_reloc_cookie (&cookie, info, ibfd)) | |
13340 | return FALSE; | |
13341 | ||
13342 | for (sec = ibfd->sections; sec; sec = sec->next) | |
13343 | { | |
13344 | if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry") | |
13345 | && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) | |
13346 | { | |
13347 | _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); | |
13348 | fini_reloc_cookie_rels (&cookie, sec); | |
13349 | } | |
13350 | } | |
13351 | } | |
13352 | return TRUE; | |
13353 | } | |
13354 | ||
c152c796 AM |
13355 | /* Do mark and sweep of unused sections. */ |
13356 | ||
13357 | bfd_boolean | |
13358 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
13359 | { | |
13360 | bfd_boolean ok = TRUE; | |
13361 | bfd *sub; | |
6a5bb875 | 13362 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 13363 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
da44f4e5 | 13364 | struct elf_link_hash_table *htab; |
c152c796 | 13365 | |
64d03ab5 | 13366 | if (!bed->can_gc_sections |
715df9b8 | 13367 | || !is_elf_hash_table (info->hash)) |
c152c796 | 13368 | { |
4eca0228 | 13369 | _bfd_error_handler(_("Warning: gc-sections option ignored")); |
c152c796 AM |
13370 | return TRUE; |
13371 | } | |
13372 | ||
74f0fb50 | 13373 | bed->gc_keep (info); |
da44f4e5 | 13374 | htab = elf_hash_table (info); |
74f0fb50 | 13375 | |
9d0a14d3 RS |
13376 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
13377 | at the .eh_frame section if we can mark the FDEs individually. */ | |
2f0c68f2 CM |
13378 | for (sub = info->input_bfds; |
13379 | info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; | |
13380 | sub = sub->link.next) | |
9d0a14d3 RS |
13381 | { |
13382 | asection *sec; | |
13383 | struct elf_reloc_cookie cookie; | |
13384 | ||
57963c05 AM |
13385 | sec = sub->sections; |
13386 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13387 | continue; | |
9d0a14d3 | 13388 | sec = bfd_get_section_by_name (sub, ".eh_frame"); |
9a2a56cc | 13389 | while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) |
9d0a14d3 RS |
13390 | { |
13391 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
9a2a56cc AM |
13392 | if (elf_section_data (sec)->sec_info |
13393 | && (sec->flags & SEC_LINKER_CREATED) == 0) | |
9d0a14d3 RS |
13394 | elf_eh_frame_section (sub) = sec; |
13395 | fini_reloc_cookie_for_section (&cookie, sec); | |
199af150 | 13396 | sec = bfd_get_next_section_by_name (NULL, sec); |
9d0a14d3 RS |
13397 | } |
13398 | } | |
9d0a14d3 | 13399 | |
c152c796 | 13400 | /* Apply transitive closure to the vtable entry usage info. */ |
da44f4e5 | 13401 | elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); |
c152c796 AM |
13402 | if (!ok) |
13403 | return FALSE; | |
13404 | ||
13405 | /* Kill the vtable relocations that were not used. */ | |
da44f4e5 | 13406 | elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok); |
c152c796 AM |
13407 | if (!ok) |
13408 | return FALSE; | |
13409 | ||
715df9b8 | 13410 | /* Mark dynamically referenced symbols. */ |
22185505 | 13411 | if (htab->dynamic_sections_created || info->gc_keep_exported) |
da44f4e5 | 13412 | elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); |
c152c796 | 13413 | |
715df9b8 | 13414 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 13415 | gc_mark_hook = bed->gc_mark_hook; |
c72f2fb2 | 13416 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13417 | { |
13418 | asection *o; | |
13419 | ||
b19a8f85 | 13420 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 13421 | || elf_object_id (sub) != elf_hash_table_id (htab) |
b19a8f85 | 13422 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 AM |
13423 | continue; |
13424 | ||
57963c05 AM |
13425 | o = sub->sections; |
13426 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13427 | continue; | |
13428 | ||
7f6ab9f8 AM |
13429 | /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). |
13430 | Also treat note sections as a root, if the section is not part | |
13431 | of a group. */ | |
c152c796 | 13432 | for (o = sub->sections; o != NULL; o = o->next) |
7f6ab9f8 AM |
13433 | if (!o->gc_mark |
13434 | && (o->flags & SEC_EXCLUDE) == 0 | |
24007750 | 13435 | && ((o->flags & SEC_KEEP) != 0 |
7f6ab9f8 AM |
13436 | || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE |
13437 | && elf_next_in_group (o) == NULL ))) | |
13438 | { | |
13439 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
13440 | return FALSE; | |
13441 | } | |
c152c796 AM |
13442 | } |
13443 | ||
6a5bb875 | 13444 | /* Allow the backend to mark additional target specific sections. */ |
7f6ab9f8 | 13445 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 13446 | |
c152c796 | 13447 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 13448 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
13449 | } |
13450 | \f | |
13451 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
13452 | ||
13453 | bfd_boolean | |
13454 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
13455 | asection *sec, | |
13456 | struct elf_link_hash_entry *h, | |
13457 | bfd_vma offset) | |
13458 | { | |
13459 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
13460 | struct elf_link_hash_entry **search, *child; | |
ef53be89 | 13461 | size_t extsymcount; |
c152c796 AM |
13462 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
13463 | ||
13464 | /* The sh_info field of the symtab header tells us where the | |
13465 | external symbols start. We don't care about the local symbols at | |
13466 | this point. */ | |
13467 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
13468 | if (!elf_bad_symtab (abfd)) | |
13469 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
13470 | ||
13471 | sym_hashes = elf_sym_hashes (abfd); | |
13472 | sym_hashes_end = sym_hashes + extsymcount; | |
13473 | ||
13474 | /* Hunt down the child symbol, which is in this section at the same | |
13475 | offset as the relocation. */ | |
13476 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
13477 | { | |
13478 | if ((child = *search) != NULL | |
13479 | && (child->root.type == bfd_link_hash_defined | |
13480 | || child->root.type == bfd_link_hash_defweak) | |
13481 | && child->root.u.def.section == sec | |
13482 | && child->root.u.def.value == offset) | |
13483 | goto win; | |
13484 | } | |
13485 | ||
695344c0 | 13486 | /* xgettext:c-format */ |
76cfced5 AM |
13487 | _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"), |
13488 | abfd, sec, offset); | |
c152c796 AM |
13489 | bfd_set_error (bfd_error_invalid_operation); |
13490 | return FALSE; | |
13491 | ||
13492 | win: | |
cbd0eecf | 13493 | if (!child->u2.vtable) |
f6e332e6 | 13494 | { |
cbd0eecf L |
13495 | child->u2.vtable = ((struct elf_link_virtual_table_entry *) |
13496 | bfd_zalloc (abfd, sizeof (*child->u2.vtable))); | |
13497 | if (!child->u2.vtable) | |
f6e332e6 AM |
13498 | return FALSE; |
13499 | } | |
c152c796 AM |
13500 | if (!h) |
13501 | { | |
13502 | /* This *should* only be the absolute section. It could potentially | |
13503 | be that someone has defined a non-global vtable though, which | |
13504 | would be bad. It isn't worth paging in the local symbols to be | |
13505 | sure though; that case should simply be handled by the assembler. */ | |
13506 | ||
cbd0eecf | 13507 | child->u2.vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
13508 | } |
13509 | else | |
cbd0eecf | 13510 | child->u2.vtable->parent = h; |
c152c796 AM |
13511 | |
13512 | return TRUE; | |
13513 | } | |
13514 | ||
13515 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
13516 | ||
13517 | bfd_boolean | |
13518 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
13519 | asection *sec ATTRIBUTE_UNUSED, | |
13520 | struct elf_link_hash_entry *h, | |
13521 | bfd_vma addend) | |
13522 | { | |
13523 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13524 | unsigned int log_file_align = bed->s->log_file_align; | |
13525 | ||
cbd0eecf | 13526 | if (!h->u2.vtable) |
f6e332e6 | 13527 | { |
cbd0eecf L |
13528 | h->u2.vtable = ((struct elf_link_virtual_table_entry *) |
13529 | bfd_zalloc (abfd, sizeof (*h->u2.vtable))); | |
13530 | if (!h->u2.vtable) | |
f6e332e6 AM |
13531 | return FALSE; |
13532 | } | |
13533 | ||
cbd0eecf | 13534 | if (addend >= h->u2.vtable->size) |
c152c796 AM |
13535 | { |
13536 | size_t size, bytes, file_align; | |
cbd0eecf | 13537 | bfd_boolean *ptr = h->u2.vtable->used; |
c152c796 AM |
13538 | |
13539 | /* While the symbol is undefined, we have to be prepared to handle | |
13540 | a zero size. */ | |
13541 | file_align = 1 << log_file_align; | |
13542 | if (h->root.type == bfd_link_hash_undefined) | |
13543 | size = addend + file_align; | |
13544 | else | |
13545 | { | |
13546 | size = h->size; | |
13547 | if (addend >= size) | |
13548 | { | |
13549 | /* Oops! We've got a reference past the defined end of | |
13550 | the table. This is probably a bug -- shall we warn? */ | |
13551 | size = addend + file_align; | |
13552 | } | |
13553 | } | |
13554 | size = (size + file_align - 1) & -file_align; | |
13555 | ||
13556 | /* Allocate one extra entry for use as a "done" flag for the | |
13557 | consolidation pass. */ | |
13558 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
13559 | ||
13560 | if (ptr) | |
13561 | { | |
a50b1753 | 13562 | ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); |
c152c796 AM |
13563 | |
13564 | if (ptr != NULL) | |
13565 | { | |
13566 | size_t oldbytes; | |
13567 | ||
cbd0eecf | 13568 | oldbytes = (((h->u2.vtable->size >> log_file_align) + 1) |
c152c796 AM |
13569 | * sizeof (bfd_boolean)); |
13570 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
13571 | } | |
13572 | } | |
13573 | else | |
a50b1753 | 13574 | ptr = (bfd_boolean *) bfd_zmalloc (bytes); |
c152c796 AM |
13575 | |
13576 | if (ptr == NULL) | |
13577 | return FALSE; | |
13578 | ||
13579 | /* And arrange for that done flag to be at index -1. */ | |
cbd0eecf L |
13580 | h->u2.vtable->used = ptr + 1; |
13581 | h->u2.vtable->size = size; | |
c152c796 AM |
13582 | } |
13583 | ||
cbd0eecf | 13584 | h->u2.vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
13585 | |
13586 | return TRUE; | |
13587 | } | |
13588 | ||
ae17ab41 CM |
13589 | /* Map an ELF section header flag to its corresponding string. */ |
13590 | typedef struct | |
13591 | { | |
13592 | char *flag_name; | |
13593 | flagword flag_value; | |
13594 | } elf_flags_to_name_table; | |
13595 | ||
13596 | static elf_flags_to_name_table elf_flags_to_names [] = | |
13597 | { | |
13598 | { "SHF_WRITE", SHF_WRITE }, | |
13599 | { "SHF_ALLOC", SHF_ALLOC }, | |
13600 | { "SHF_EXECINSTR", SHF_EXECINSTR }, | |
13601 | { "SHF_MERGE", SHF_MERGE }, | |
13602 | { "SHF_STRINGS", SHF_STRINGS }, | |
13603 | { "SHF_INFO_LINK", SHF_INFO_LINK}, | |
13604 | { "SHF_LINK_ORDER", SHF_LINK_ORDER}, | |
13605 | { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, | |
13606 | { "SHF_GROUP", SHF_GROUP }, | |
13607 | { "SHF_TLS", SHF_TLS }, | |
13608 | { "SHF_MASKOS", SHF_MASKOS }, | |
13609 | { "SHF_EXCLUDE", SHF_EXCLUDE }, | |
13610 | }; | |
13611 | ||
b9c361e0 JL |
13612 | /* Returns TRUE if the section is to be included, otherwise FALSE. */ |
13613 | bfd_boolean | |
ae17ab41 | 13614 | bfd_elf_lookup_section_flags (struct bfd_link_info *info, |
8b127cbc | 13615 | struct flag_info *flaginfo, |
b9c361e0 | 13616 | asection *section) |
ae17ab41 | 13617 | { |
8b127cbc | 13618 | const bfd_vma sh_flags = elf_section_flags (section); |
ae17ab41 | 13619 | |
8b127cbc | 13620 | if (!flaginfo->flags_initialized) |
ae17ab41 | 13621 | { |
8b127cbc AM |
13622 | bfd *obfd = info->output_bfd; |
13623 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
13624 | struct flag_info_list *tf = flaginfo->flag_list; | |
b9c361e0 JL |
13625 | int with_hex = 0; |
13626 | int without_hex = 0; | |
13627 | ||
8b127cbc | 13628 | for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) |
ae17ab41 | 13629 | { |
b9c361e0 | 13630 | unsigned i; |
8b127cbc | 13631 | flagword (*lookup) (char *); |
ae17ab41 | 13632 | |
8b127cbc AM |
13633 | lookup = bed->elf_backend_lookup_section_flags_hook; |
13634 | if (lookup != NULL) | |
ae17ab41 | 13635 | { |
8b127cbc | 13636 | flagword hexval = (*lookup) ((char *) tf->name); |
b9c361e0 JL |
13637 | |
13638 | if (hexval != 0) | |
13639 | { | |
13640 | if (tf->with == with_flags) | |
13641 | with_hex |= hexval; | |
13642 | else if (tf->with == without_flags) | |
13643 | without_hex |= hexval; | |
13644 | tf->valid = TRUE; | |
13645 | continue; | |
13646 | } | |
ae17ab41 | 13647 | } |
8b127cbc | 13648 | for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) |
ae17ab41 | 13649 | { |
8b127cbc | 13650 | if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) |
b9c361e0 JL |
13651 | { |
13652 | if (tf->with == with_flags) | |
13653 | with_hex |= elf_flags_to_names[i].flag_value; | |
13654 | else if (tf->with == without_flags) | |
13655 | without_hex |= elf_flags_to_names[i].flag_value; | |
13656 | tf->valid = TRUE; | |
13657 | break; | |
13658 | } | |
13659 | } | |
8b127cbc | 13660 | if (!tf->valid) |
b9c361e0 | 13661 | { |
68ffbac6 | 13662 | info->callbacks->einfo |
8b127cbc | 13663 | (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); |
b9c361e0 | 13664 | return FALSE; |
ae17ab41 CM |
13665 | } |
13666 | } | |
8b127cbc AM |
13667 | flaginfo->flags_initialized = TRUE; |
13668 | flaginfo->only_with_flags |= with_hex; | |
13669 | flaginfo->not_with_flags |= without_hex; | |
ae17ab41 | 13670 | } |
ae17ab41 | 13671 | |
8b127cbc | 13672 | if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) |
b9c361e0 JL |
13673 | return FALSE; |
13674 | ||
8b127cbc | 13675 | if ((flaginfo->not_with_flags & sh_flags) != 0) |
b9c361e0 JL |
13676 | return FALSE; |
13677 | ||
13678 | return TRUE; | |
ae17ab41 CM |
13679 | } |
13680 | ||
c152c796 AM |
13681 | struct alloc_got_off_arg { |
13682 | bfd_vma gotoff; | |
10455f89 | 13683 | struct bfd_link_info *info; |
c152c796 AM |
13684 | }; |
13685 | ||
13686 | /* We need a special top-level link routine to convert got reference counts | |
13687 | to real got offsets. */ | |
13688 | ||
13689 | static bfd_boolean | |
13690 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
13691 | { | |
a50b1753 | 13692 | struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; |
10455f89 HPN |
13693 | bfd *obfd = gofarg->info->output_bfd; |
13694 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
c152c796 | 13695 | |
c152c796 AM |
13696 | if (h->got.refcount > 0) |
13697 | { | |
13698 | h->got.offset = gofarg->gotoff; | |
10455f89 | 13699 | gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); |
c152c796 AM |
13700 | } |
13701 | else | |
13702 | h->got.offset = (bfd_vma) -1; | |
13703 | ||
13704 | return TRUE; | |
13705 | } | |
13706 | ||
13707 | /* And an accompanying bit to work out final got entry offsets once | |
13708 | we're done. Should be called from final_link. */ | |
13709 | ||
13710 | bfd_boolean | |
13711 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
13712 | struct bfd_link_info *info) | |
13713 | { | |
13714 | bfd *i; | |
13715 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13716 | bfd_vma gotoff; | |
c152c796 AM |
13717 | struct alloc_got_off_arg gofarg; |
13718 | ||
10455f89 HPN |
13719 | BFD_ASSERT (abfd == info->output_bfd); |
13720 | ||
c152c796 AM |
13721 | if (! is_elf_hash_table (info->hash)) |
13722 | return FALSE; | |
13723 | ||
13724 | /* The GOT offset is relative to the .got section, but the GOT header is | |
13725 | put into the .got.plt section, if the backend uses it. */ | |
13726 | if (bed->want_got_plt) | |
13727 | gotoff = 0; | |
13728 | else | |
13729 | gotoff = bed->got_header_size; | |
13730 | ||
13731 | /* Do the local .got entries first. */ | |
c72f2fb2 | 13732 | for (i = info->input_bfds; i; i = i->link.next) |
c152c796 AM |
13733 | { |
13734 | bfd_signed_vma *local_got; | |
ef53be89 | 13735 | size_t j, locsymcount; |
c152c796 AM |
13736 | Elf_Internal_Shdr *symtab_hdr; |
13737 | ||
13738 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
13739 | continue; | |
13740 | ||
13741 | local_got = elf_local_got_refcounts (i); | |
13742 | if (!local_got) | |
13743 | continue; | |
13744 | ||
13745 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
13746 | if (elf_bad_symtab (i)) | |
13747 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
13748 | else | |
13749 | locsymcount = symtab_hdr->sh_info; | |
13750 | ||
13751 | for (j = 0; j < locsymcount; ++j) | |
13752 | { | |
13753 | if (local_got[j] > 0) | |
13754 | { | |
13755 | local_got[j] = gotoff; | |
10455f89 | 13756 | gotoff += bed->got_elt_size (abfd, info, NULL, i, j); |
c152c796 AM |
13757 | } |
13758 | else | |
13759 | local_got[j] = (bfd_vma) -1; | |
13760 | } | |
13761 | } | |
13762 | ||
13763 | /* Then the global .got entries. .plt refcounts are handled by | |
13764 | adjust_dynamic_symbol */ | |
13765 | gofarg.gotoff = gotoff; | |
10455f89 | 13766 | gofarg.info = info; |
c152c796 AM |
13767 | elf_link_hash_traverse (elf_hash_table (info), |
13768 | elf_gc_allocate_got_offsets, | |
13769 | &gofarg); | |
13770 | return TRUE; | |
13771 | } | |
13772 | ||
13773 | /* Many folk need no more in the way of final link than this, once | |
13774 | got entry reference counting is enabled. */ | |
13775 | ||
13776 | bfd_boolean | |
13777 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
13778 | { | |
13779 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
13780 | return FALSE; | |
13781 | ||
13782 | /* Invoke the regular ELF backend linker to do all the work. */ | |
13783 | return bfd_elf_final_link (abfd, info); | |
13784 | } | |
13785 | ||
13786 | bfd_boolean | |
13787 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
13788 | { | |
a50b1753 | 13789 | struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; |
c152c796 AM |
13790 | |
13791 | if (rcookie->bad_symtab) | |
13792 | rcookie->rel = rcookie->rels; | |
13793 | ||
13794 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
13795 | { | |
13796 | unsigned long r_symndx; | |
13797 | ||
13798 | if (! rcookie->bad_symtab) | |
13799 | if (rcookie->rel->r_offset > offset) | |
13800 | return FALSE; | |
13801 | if (rcookie->rel->r_offset != offset) | |
13802 | continue; | |
13803 | ||
13804 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
2c2fa401 | 13805 | if (r_symndx == STN_UNDEF) |
c152c796 AM |
13806 | return TRUE; |
13807 | ||
13808 | if (r_symndx >= rcookie->locsymcount | |
13809 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
13810 | { | |
13811 | struct elf_link_hash_entry *h; | |
13812 | ||
13813 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
13814 | ||
13815 | while (h->root.type == bfd_link_hash_indirect | |
13816 | || h->root.type == bfd_link_hash_warning) | |
13817 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
13818 | ||
13819 | if ((h->root.type == bfd_link_hash_defined | |
13820 | || h->root.type == bfd_link_hash_defweak) | |
5b69e357 AM |
13821 | && (h->root.u.def.section->owner != rcookie->abfd |
13822 | || h->root.u.def.section->kept_section != NULL | |
13823 | || discarded_section (h->root.u.def.section))) | |
c152c796 | 13824 | return TRUE; |
c152c796 AM |
13825 | } |
13826 | else | |
13827 | { | |
13828 | /* It's not a relocation against a global symbol, | |
13829 | but it could be a relocation against a local | |
13830 | symbol for a discarded section. */ | |
13831 | asection *isec; | |
13832 | Elf_Internal_Sym *isym; | |
13833 | ||
13834 | /* Need to: get the symbol; get the section. */ | |
13835 | isym = &rcookie->locsyms[r_symndx]; | |
cb33740c | 13836 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); |
5b69e357 AM |
13837 | if (isec != NULL |
13838 | && (isec->kept_section != NULL | |
13839 | || discarded_section (isec))) | |
cb33740c | 13840 | return TRUE; |
c152c796 AM |
13841 | } |
13842 | return FALSE; | |
13843 | } | |
13844 | return FALSE; | |
13845 | } | |
13846 | ||
13847 | /* Discard unneeded references to discarded sections. | |
75938853 AM |
13848 | Returns -1 on error, 1 if any section's size was changed, 0 if |
13849 | nothing changed. This function assumes that the relocations are in | |
13850 | sorted order, which is true for all known assemblers. */ | |
c152c796 | 13851 | |
75938853 | 13852 | int |
c152c796 AM |
13853 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) |
13854 | { | |
13855 | struct elf_reloc_cookie cookie; | |
18cd5bce | 13856 | asection *o; |
c152c796 | 13857 | bfd *abfd; |
75938853 | 13858 | int changed = 0; |
c152c796 AM |
13859 | |
13860 | if (info->traditional_format | |
13861 | || !is_elf_hash_table (info->hash)) | |
75938853 | 13862 | return 0; |
c152c796 | 13863 | |
18cd5bce AM |
13864 | o = bfd_get_section_by_name (output_bfd, ".stab"); |
13865 | if (o != NULL) | |
c152c796 | 13866 | { |
18cd5bce | 13867 | asection *i; |
c152c796 | 13868 | |
18cd5bce | 13869 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
8da3dbc5 | 13870 | { |
18cd5bce AM |
13871 | if (i->size == 0 |
13872 | || i->reloc_count == 0 | |
13873 | || i->sec_info_type != SEC_INFO_TYPE_STABS) | |
13874 | continue; | |
c152c796 | 13875 | |
18cd5bce AM |
13876 | abfd = i->owner; |
13877 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
13878 | continue; | |
c152c796 | 13879 | |
18cd5bce | 13880 | if (!init_reloc_cookie_for_section (&cookie, info, i)) |
75938853 | 13881 | return -1; |
c152c796 | 13882 | |
18cd5bce AM |
13883 | if (_bfd_discard_section_stabs (abfd, i, |
13884 | elf_section_data (i)->sec_info, | |
5241d853 RS |
13885 | bfd_elf_reloc_symbol_deleted_p, |
13886 | &cookie)) | |
75938853 | 13887 | changed = 1; |
18cd5bce AM |
13888 | |
13889 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 13890 | } |
18cd5bce AM |
13891 | } |
13892 | ||
2f0c68f2 CM |
13893 | o = NULL; |
13894 | if (info->eh_frame_hdr_type != COMPACT_EH_HDR) | |
13895 | o = bfd_get_section_by_name (output_bfd, ".eh_frame"); | |
18cd5bce AM |
13896 | if (o != NULL) |
13897 | { | |
13898 | asection *i; | |
d7153c4a | 13899 | int eh_changed = 0; |
79a94a2a | 13900 | unsigned int eh_alignment; |
c152c796 | 13901 | |
18cd5bce | 13902 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
c152c796 | 13903 | { |
18cd5bce AM |
13904 | if (i->size == 0) |
13905 | continue; | |
13906 | ||
13907 | abfd = i->owner; | |
13908 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
13909 | continue; | |
13910 | ||
13911 | if (!init_reloc_cookie_for_section (&cookie, info, i)) | |
75938853 | 13912 | return -1; |
18cd5bce AM |
13913 | |
13914 | _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); | |
13915 | if (_bfd_elf_discard_section_eh_frame (abfd, info, i, | |
c152c796 AM |
13916 | bfd_elf_reloc_symbol_deleted_p, |
13917 | &cookie)) | |
d7153c4a AM |
13918 | { |
13919 | eh_changed = 1; | |
13920 | if (i->size != i->rawsize) | |
13921 | changed = 1; | |
13922 | } | |
18cd5bce AM |
13923 | |
13924 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 13925 | } |
9866ffe2 | 13926 | |
79a94a2a | 13927 | eh_alignment = 1 << o->alignment_power; |
9866ffe2 AM |
13928 | /* Skip over zero terminator, and prevent empty sections from |
13929 | adding alignment padding at the end. */ | |
13930 | for (i = o->map_tail.s; i != NULL; i = i->map_tail.s) | |
13931 | if (i->size == 0) | |
13932 | i->flags |= SEC_EXCLUDE; | |
13933 | else if (i->size > 4) | |
13934 | break; | |
13935 | /* The last non-empty eh_frame section doesn't need padding. */ | |
13936 | if (i != NULL) | |
13937 | i = i->map_tail.s; | |
13938 | /* Any prior sections must pad the last FDE out to the output | |
13939 | section alignment. Otherwise we might have zero padding | |
13940 | between sections, which would be seen as a terminator. */ | |
13941 | for (; i != NULL; i = i->map_tail.s) | |
13942 | if (i->size == 4) | |
13943 | /* All but the last zero terminator should have been removed. */ | |
13944 | BFD_FAIL (); | |
13945 | else | |
13946 | { | |
13947 | bfd_size_type size | |
13948 | = (i->size + eh_alignment - 1) & -eh_alignment; | |
13949 | if (i->size != size) | |
af471f82 | 13950 | { |
9866ffe2 AM |
13951 | i->size = size; |
13952 | changed = 1; | |
13953 | eh_changed = 1; | |
af471f82 | 13954 | } |
9866ffe2 | 13955 | } |
d7153c4a AM |
13956 | if (eh_changed) |
13957 | elf_link_hash_traverse (elf_hash_table (info), | |
13958 | _bfd_elf_adjust_eh_frame_global_symbol, NULL); | |
18cd5bce | 13959 | } |
c152c796 | 13960 | |
18cd5bce AM |
13961 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) |
13962 | { | |
13963 | const struct elf_backend_data *bed; | |
57963c05 | 13964 | asection *s; |
c152c796 | 13965 | |
18cd5bce AM |
13966 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) |
13967 | continue; | |
57963c05 AM |
13968 | s = abfd->sections; |
13969 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13970 | continue; | |
18cd5bce AM |
13971 | |
13972 | bed = get_elf_backend_data (abfd); | |
13973 | ||
13974 | if (bed->elf_backend_discard_info != NULL) | |
13975 | { | |
13976 | if (!init_reloc_cookie (&cookie, info, abfd)) | |
75938853 | 13977 | return -1; |
18cd5bce AM |
13978 | |
13979 | if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
75938853 | 13980 | changed = 1; |
18cd5bce AM |
13981 | |
13982 | fini_reloc_cookie (&cookie, abfd); | |
13983 | } | |
c152c796 AM |
13984 | } |
13985 | ||
2f0c68f2 CM |
13986 | if (info->eh_frame_hdr_type == COMPACT_EH_HDR) |
13987 | _bfd_elf_end_eh_frame_parsing (info); | |
13988 | ||
13989 | if (info->eh_frame_hdr_type | |
0e1862bb | 13990 | && !bfd_link_relocatable (info) |
c152c796 | 13991 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) |
75938853 | 13992 | changed = 1; |
c152c796 | 13993 | |
75938853 | 13994 | return changed; |
c152c796 | 13995 | } |
082b7297 | 13996 | |
43e1669b | 13997 | bfd_boolean |
0c511000 | 13998 | _bfd_elf_section_already_linked (bfd *abfd, |
c77ec726 | 13999 | asection *sec, |
c0f00686 | 14000 | struct bfd_link_info *info) |
082b7297 L |
14001 | { |
14002 | flagword flags; | |
c77ec726 | 14003 | const char *name, *key; |
082b7297 L |
14004 | struct bfd_section_already_linked *l; |
14005 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
0c511000 | 14006 | |
c77ec726 AM |
14007 | if (sec->output_section == bfd_abs_section_ptr) |
14008 | return FALSE; | |
0c511000 | 14009 | |
c77ec726 | 14010 | flags = sec->flags; |
0c511000 | 14011 | |
c77ec726 AM |
14012 | /* Return if it isn't a linkonce section. A comdat group section |
14013 | also has SEC_LINK_ONCE set. */ | |
14014 | if ((flags & SEC_LINK_ONCE) == 0) | |
14015 | return FALSE; | |
0c511000 | 14016 | |
c77ec726 AM |
14017 | /* Don't put group member sections on our list of already linked |
14018 | sections. They are handled as a group via their group section. */ | |
14019 | if (elf_sec_group (sec) != NULL) | |
14020 | return FALSE; | |
0c511000 | 14021 | |
c77ec726 AM |
14022 | /* For a SHT_GROUP section, use the group signature as the key. */ |
14023 | name = sec->name; | |
14024 | if ((flags & SEC_GROUP) != 0 | |
14025 | && elf_next_in_group (sec) != NULL | |
14026 | && elf_group_name (elf_next_in_group (sec)) != NULL) | |
14027 | key = elf_group_name (elf_next_in_group (sec)); | |
14028 | else | |
14029 | { | |
14030 | /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ | |
0c511000 | 14031 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
c77ec726 AM |
14032 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
14033 | key++; | |
0c511000 | 14034 | else |
c77ec726 AM |
14035 | /* Must be a user linkonce section that doesn't follow gcc's |
14036 | naming convention. In this case we won't be matching | |
14037 | single member groups. */ | |
14038 | key = name; | |
0c511000 | 14039 | } |
6d2cd210 | 14040 | |
c77ec726 | 14041 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
082b7297 L |
14042 | |
14043 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14044 | { | |
c2370991 | 14045 | /* We may have 2 different types of sections on the list: group |
c77ec726 AM |
14046 | sections with a signature of <key> (<key> is some string), |
14047 | and linkonce sections named .gnu.linkonce.<type>.<key>. | |
14048 | Match like sections. LTO plugin sections are an exception. | |
14049 | They are always named .gnu.linkonce.t.<key> and match either | |
14050 | type of section. */ | |
14051 | if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) | |
14052 | && ((flags & SEC_GROUP) != 0 | |
14053 | || strcmp (name, l->sec->name) == 0)) | |
14054 | || (l->sec->owner->flags & BFD_PLUGIN) != 0) | |
082b7297 L |
14055 | { |
14056 | /* The section has already been linked. See if we should | |
6d2cd210 | 14057 | issue a warning. */ |
c77ec726 AM |
14058 | if (!_bfd_handle_already_linked (sec, l, info)) |
14059 | return FALSE; | |
082b7297 | 14060 | |
c77ec726 | 14061 | if (flags & SEC_GROUP) |
3d7f7666 | 14062 | { |
c77ec726 AM |
14063 | asection *first = elf_next_in_group (sec); |
14064 | asection *s = first; | |
3d7f7666 | 14065 | |
c77ec726 | 14066 | while (s != NULL) |
3d7f7666 | 14067 | { |
c77ec726 AM |
14068 | s->output_section = bfd_abs_section_ptr; |
14069 | /* Record which group discards it. */ | |
14070 | s->kept_section = l->sec; | |
14071 | s = elf_next_in_group (s); | |
14072 | /* These lists are circular. */ | |
14073 | if (s == first) | |
14074 | break; | |
3d7f7666 L |
14075 | } |
14076 | } | |
082b7297 | 14077 | |
43e1669b | 14078 | return TRUE; |
082b7297 L |
14079 | } |
14080 | } | |
14081 | ||
c77ec726 AM |
14082 | /* A single member comdat group section may be discarded by a |
14083 | linkonce section and vice versa. */ | |
14084 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 14085 | { |
c77ec726 | 14086 | asection *first = elf_next_in_group (sec); |
c2370991 | 14087 | |
c77ec726 AM |
14088 | if (first != NULL && elf_next_in_group (first) == first) |
14089 | /* Check this single member group against linkonce sections. */ | |
14090 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14091 | if ((l->sec->flags & SEC_GROUP) == 0 | |
14092 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
14093 | { | |
14094 | first->output_section = bfd_abs_section_ptr; | |
14095 | first->kept_section = l->sec; | |
14096 | sec->output_section = bfd_abs_section_ptr; | |
14097 | break; | |
14098 | } | |
14099 | } | |
14100 | else | |
14101 | /* Check this linkonce section against single member groups. */ | |
14102 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14103 | if (l->sec->flags & SEC_GROUP) | |
6d2cd210 | 14104 | { |
c77ec726 | 14105 | asection *first = elf_next_in_group (l->sec); |
6d2cd210 | 14106 | |
c77ec726 AM |
14107 | if (first != NULL |
14108 | && elf_next_in_group (first) == first | |
14109 | && bfd_elf_match_symbols_in_sections (first, sec, info)) | |
14110 | { | |
14111 | sec->output_section = bfd_abs_section_ptr; | |
14112 | sec->kept_section = first; | |
14113 | break; | |
14114 | } | |
6d2cd210 | 14115 | } |
0c511000 | 14116 | |
c77ec726 AM |
14117 | /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' |
14118 | referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 | |
14119 | specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' | |
14120 | prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its | |
14121 | matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded | |
14122 | but its `.gnu.linkonce.t.F' is discarded means we chose one-only | |
14123 | `.gnu.linkonce.t.F' section from a different bfd not requiring any | |
14124 | `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. | |
14125 | The reverse order cannot happen as there is never a bfd with only the | |
14126 | `.gnu.linkonce.r.F' section. The order of sections in a bfd does not | |
14127 | matter as here were are looking only for cross-bfd sections. */ | |
14128 | ||
14129 | if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) | |
14130 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14131 | if ((l->sec->flags & SEC_GROUP) == 0 | |
14132 | && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) | |
14133 | { | |
14134 | if (abfd != l->sec->owner) | |
14135 | sec->output_section = bfd_abs_section_ptr; | |
14136 | break; | |
14137 | } | |
80c29487 | 14138 | |
082b7297 | 14139 | /* This is the first section with this name. Record it. */ |
c77ec726 | 14140 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
bb6198d2 | 14141 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
c77ec726 | 14142 | return sec->output_section == bfd_abs_section_ptr; |
082b7297 | 14143 | } |
81e1b023 | 14144 | |
a4d8e49b L |
14145 | bfd_boolean |
14146 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
14147 | { | |
14148 | return sym->st_shndx == SHN_COMMON; | |
14149 | } | |
14150 | ||
14151 | unsigned int | |
14152 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
14153 | { | |
14154 | return SHN_COMMON; | |
14155 | } | |
14156 | ||
14157 | asection * | |
14158 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
14159 | { | |
14160 | return bfd_com_section_ptr; | |
14161 | } | |
10455f89 HPN |
14162 | |
14163 | bfd_vma | |
14164 | _bfd_elf_default_got_elt_size (bfd *abfd, | |
14165 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
14166 | struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, | |
14167 | bfd *ibfd ATTRIBUTE_UNUSED, | |
14168 | unsigned long symndx ATTRIBUTE_UNUSED) | |
14169 | { | |
14170 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14171 | return bed->s->arch_size / 8; | |
14172 | } | |
83bac4b0 NC |
14173 | |
14174 | /* Routines to support the creation of dynamic relocs. */ | |
14175 | ||
83bac4b0 NC |
14176 | /* Returns the name of the dynamic reloc section associated with SEC. */ |
14177 | ||
14178 | static const char * | |
14179 | get_dynamic_reloc_section_name (bfd * abfd, | |
14180 | asection * sec, | |
14181 | bfd_boolean is_rela) | |
14182 | { | |
ddcf1fcf BS |
14183 | char *name; |
14184 | const char *old_name = bfd_get_section_name (NULL, sec); | |
14185 | const char *prefix = is_rela ? ".rela" : ".rel"; | |
83bac4b0 | 14186 | |
ddcf1fcf | 14187 | if (old_name == NULL) |
83bac4b0 NC |
14188 | return NULL; |
14189 | ||
ddcf1fcf | 14190 | name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); |
68ffbac6 | 14191 | sprintf (name, "%s%s", prefix, old_name); |
83bac4b0 NC |
14192 | |
14193 | return name; | |
14194 | } | |
14195 | ||
14196 | /* Returns the dynamic reloc section associated with SEC. | |
14197 | If necessary compute the name of the dynamic reloc section based | |
14198 | on SEC's name (looked up in ABFD's string table) and the setting | |
14199 | of IS_RELA. */ | |
14200 | ||
14201 | asection * | |
14202 | _bfd_elf_get_dynamic_reloc_section (bfd * abfd, | |
14203 | asection * sec, | |
14204 | bfd_boolean is_rela) | |
14205 | { | |
14206 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
14207 | ||
14208 | if (reloc_sec == NULL) | |
14209 | { | |
14210 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
14211 | ||
14212 | if (name != NULL) | |
14213 | { | |
3d4d4302 | 14214 | reloc_sec = bfd_get_linker_section (abfd, name); |
83bac4b0 NC |
14215 | |
14216 | if (reloc_sec != NULL) | |
14217 | elf_section_data (sec)->sreloc = reloc_sec; | |
14218 | } | |
14219 | } | |
14220 | ||
14221 | return reloc_sec; | |
14222 | } | |
14223 | ||
14224 | /* Returns the dynamic reloc section associated with SEC. If the | |
14225 | section does not exist it is created and attached to the DYNOBJ | |
14226 | bfd and stored in the SRELOC field of SEC's elf_section_data | |
14227 | structure. | |
f8076f98 | 14228 | |
83bac4b0 NC |
14229 | ALIGNMENT is the alignment for the newly created section and |
14230 | IS_RELA defines whether the name should be .rela.<SEC's name> | |
14231 | or .rel.<SEC's name>. The section name is looked up in the | |
14232 | string table associated with ABFD. */ | |
14233 | ||
14234 | asection * | |
ca4be51c AM |
14235 | _bfd_elf_make_dynamic_reloc_section (asection *sec, |
14236 | bfd *dynobj, | |
14237 | unsigned int alignment, | |
14238 | bfd *abfd, | |
14239 | bfd_boolean is_rela) | |
83bac4b0 NC |
14240 | { |
14241 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
14242 | ||
14243 | if (reloc_sec == NULL) | |
14244 | { | |
14245 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
14246 | ||
14247 | if (name == NULL) | |
14248 | return NULL; | |
14249 | ||
3d4d4302 | 14250 | reloc_sec = bfd_get_linker_section (dynobj, name); |
83bac4b0 NC |
14251 | |
14252 | if (reloc_sec == NULL) | |
14253 | { | |
3d4d4302 AM |
14254 | flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY |
14255 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
83bac4b0 NC |
14256 | if ((sec->flags & SEC_ALLOC) != 0) |
14257 | flags |= SEC_ALLOC | SEC_LOAD; | |
14258 | ||
3d4d4302 | 14259 | reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); |
83bac4b0 NC |
14260 | if (reloc_sec != NULL) |
14261 | { | |
8877b5e5 AM |
14262 | /* _bfd_elf_get_sec_type_attr chooses a section type by |
14263 | name. Override as it may be wrong, eg. for a user | |
14264 | section named "auto" we'll get ".relauto" which is | |
14265 | seen to be a .rela section. */ | |
14266 | elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; | |
83bac4b0 NC |
14267 | if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) |
14268 | reloc_sec = NULL; | |
14269 | } | |
14270 | } | |
14271 | ||
14272 | elf_section_data (sec)->sreloc = reloc_sec; | |
14273 | } | |
14274 | ||
14275 | return reloc_sec; | |
14276 | } | |
1338dd10 | 14277 | |
bffebb6b AM |
14278 | /* Copy the ELF symbol type and other attributes for a linker script |
14279 | assignment from HSRC to HDEST. Generally this should be treated as | |
14280 | if we found a strong non-dynamic definition for HDEST (except that | |
14281 | ld ignores multiple definition errors). */ | |
1338dd10 | 14282 | void |
bffebb6b AM |
14283 | _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, |
14284 | struct bfd_link_hash_entry *hdest, | |
14285 | struct bfd_link_hash_entry *hsrc) | |
1338dd10 | 14286 | { |
bffebb6b AM |
14287 | struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; |
14288 | struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; | |
14289 | Elf_Internal_Sym isym; | |
1338dd10 PB |
14290 | |
14291 | ehdest->type = ehsrc->type; | |
35fc36a8 | 14292 | ehdest->target_internal = ehsrc->target_internal; |
bffebb6b AM |
14293 | |
14294 | isym.st_other = ehsrc->other; | |
b8417128 | 14295 | elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE); |
1338dd10 | 14296 | } |
351f65ca L |
14297 | |
14298 | /* Append a RELA relocation REL to section S in BFD. */ | |
14299 | ||
14300 | void | |
14301 | elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14302 | { | |
14303 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14304 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); | |
14305 | BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); | |
14306 | bed->s->swap_reloca_out (abfd, rel, loc); | |
14307 | } | |
14308 | ||
14309 | /* Append a REL relocation REL to section S in BFD. */ | |
14310 | ||
14311 | void | |
14312 | elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14313 | { | |
14314 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14315 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); | |
14316 | BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); | |
59d6ffb2 | 14317 | bed->s->swap_reloc_out (abfd, rel, loc); |
351f65ca | 14318 | } |
7dba9362 AM |
14319 | |
14320 | /* Define __start, __stop, .startof. or .sizeof. symbol. */ | |
14321 | ||
14322 | struct bfd_link_hash_entry * | |
14323 | bfd_elf_define_start_stop (struct bfd_link_info *info, | |
14324 | const char *symbol, asection *sec) | |
14325 | { | |
487b6440 | 14326 | struct elf_link_hash_entry *h; |
7dba9362 | 14327 | |
487b6440 AM |
14328 | h = elf_link_hash_lookup (elf_hash_table (info), symbol, |
14329 | FALSE, FALSE, TRUE); | |
14330 | if (h != NULL | |
14331 | && (h->root.type == bfd_link_hash_undefined | |
14332 | || h->root.type == bfd_link_hash_undefweak | |
14333 | || (h->ref_regular && !h->def_regular))) | |
7dba9362 | 14334 | { |
487b6440 AM |
14335 | h->root.type = bfd_link_hash_defined; |
14336 | h->root.u.def.section = sec; | |
14337 | h->root.u.def.value = 0; | |
14338 | h->def_regular = 1; | |
14339 | h->def_dynamic = 0; | |
14340 | h->start_stop = 1; | |
14341 | h->u2.start_stop_section = sec; | |
14342 | if (symbol[0] == '.') | |
14343 | { | |
14344 | /* .startof. and .sizeof. symbols are local. */ | |
559192d8 AM |
14345 | const struct elf_backend_data *bed; |
14346 | bed = get_elf_backend_data (info->output_bfd); | |
14347 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
487b6440 AM |
14348 | } |
14349 | else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
14350 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED; | |
14351 | return &h->root; | |
7dba9362 | 14352 | } |
487b6440 | 14353 | return NULL; |
7dba9362 | 14354 | } |