Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
6f2750fe | 2 | Copyright (C) 1995-2016 Free Software Foundation, Inc. |
252b5132 | 3 | |
8fdd7217 | 4 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 5 | |
8fdd7217 NC |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 8 | the Free Software Foundation; either version 3 of the License, or |
8fdd7217 | 9 | (at your option) any later version. |
252b5132 | 10 | |
8fdd7217 NC |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
252b5132 | 15 | |
8fdd7217 NC |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
19 | MA 02110-1301, USA. */ | |
252b5132 | 20 | |
252b5132 | 21 | #include "sysdep.h" |
3db64b00 | 22 | #include "bfd.h" |
53df40a4 | 23 | #include "bfd_stdint.h" |
252b5132 RH |
24 | #include "bfdlink.h" |
25 | #include "libbfd.h" | |
26 | #define ARCH_SIZE 0 | |
27 | #include "elf-bfd.h" | |
4ad4eba5 | 28 | #include "safe-ctype.h" |
ccf2f652 | 29 | #include "libiberty.h" |
66eb6687 | 30 | #include "objalloc.h" |
08ce1d72 | 31 | #if BFD_SUPPORTS_PLUGINS |
7d0b9ebc | 32 | #include "plugin-api.h" |
7dc3990e L |
33 | #include "plugin.h" |
34 | #endif | |
252b5132 | 35 | |
28caa186 AM |
36 | /* This struct is used to pass information to routines called via |
37 | elf_link_hash_traverse which must return failure. */ | |
38 | ||
39 | struct elf_info_failed | |
40 | { | |
41 | struct bfd_link_info *info; | |
28caa186 AM |
42 | bfd_boolean failed; |
43 | }; | |
44 | ||
45 | /* This structure is used to pass information to | |
46 | _bfd_elf_link_find_version_dependencies. */ | |
47 | ||
48 | struct elf_find_verdep_info | |
49 | { | |
50 | /* General link information. */ | |
51 | struct bfd_link_info *info; | |
52 | /* The number of dependencies. */ | |
53 | unsigned int vers; | |
54 | /* Whether we had a failure. */ | |
55 | bfd_boolean failed; | |
56 | }; | |
57 | ||
58 | static bfd_boolean _bfd_elf_fix_symbol_flags | |
59 | (struct elf_link_hash_entry *, struct elf_info_failed *); | |
60 | ||
2f0c68f2 CM |
61 | asection * |
62 | _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie, | |
63 | unsigned long r_symndx, | |
64 | bfd_boolean discard) | |
65 | { | |
66 | if (r_symndx >= cookie->locsymcount | |
67 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
68 | { | |
69 | struct elf_link_hash_entry *h; | |
70 | ||
71 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
72 | ||
73 | while (h->root.type == bfd_link_hash_indirect | |
74 | || h->root.type == bfd_link_hash_warning) | |
75 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
76 | ||
77 | if ((h->root.type == bfd_link_hash_defined | |
78 | || h->root.type == bfd_link_hash_defweak) | |
79 | && discarded_section (h->root.u.def.section)) | |
80 | return h->root.u.def.section; | |
81 | else | |
82 | return NULL; | |
83 | } | |
84 | else | |
85 | { | |
86 | /* It's not a relocation against a global symbol, | |
87 | but it could be a relocation against a local | |
88 | symbol for a discarded section. */ | |
89 | asection *isec; | |
90 | Elf_Internal_Sym *isym; | |
91 | ||
92 | /* Need to: get the symbol; get the section. */ | |
93 | isym = &cookie->locsyms[r_symndx]; | |
94 | isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx); | |
95 | if (isec != NULL | |
96 | && discard ? discarded_section (isec) : 1) | |
97 | return isec; | |
98 | } | |
99 | return NULL; | |
100 | } | |
101 | ||
d98685ac AM |
102 | /* Define a symbol in a dynamic linkage section. */ |
103 | ||
104 | struct elf_link_hash_entry * | |
105 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
106 | struct bfd_link_info *info, | |
107 | asection *sec, | |
108 | const char *name) | |
109 | { | |
110 | struct elf_link_hash_entry *h; | |
111 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 112 | const struct elf_backend_data *bed; |
d98685ac AM |
113 | |
114 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
115 | if (h != NULL) | |
116 | { | |
117 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
118 | This is a symptom of a larger problem: Absolute symbols | |
119 | defined in shared libraries can't be overridden, because we | |
120 | lose the link to the bfd which is via the symbol section. */ | |
121 | h->root.type = bfd_link_hash_new; | |
122 | } | |
123 | ||
124 | bh = &h->root; | |
cf18fda4 | 125 | bed = get_elf_backend_data (abfd); |
d98685ac | 126 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, |
cf18fda4 | 127 | sec, 0, NULL, FALSE, bed->collect, |
d98685ac AM |
128 | &bh)) |
129 | return NULL; | |
130 | h = (struct elf_link_hash_entry *) bh; | |
131 | h->def_regular = 1; | |
e28df02b | 132 | h->non_elf = 0; |
12b2843a | 133 | h->root.linker_def = 1; |
d98685ac | 134 | h->type = STT_OBJECT; |
00b7642b AM |
135 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
136 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
d98685ac | 137 | |
ccabcbe5 | 138 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
d98685ac AM |
139 | return h; |
140 | } | |
141 | ||
b34976b6 | 142 | bfd_boolean |
268b6b39 | 143 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
144 | { |
145 | flagword flags; | |
aad5d350 | 146 | asection *s; |
252b5132 | 147 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 148 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 149 | struct elf_link_hash_table *htab = elf_hash_table (info); |
252b5132 RH |
150 | |
151 | /* This function may be called more than once. */ | |
ce558b89 | 152 | if (htab->sgot != NULL) |
b34976b6 | 153 | return TRUE; |
252b5132 | 154 | |
e5a52504 | 155 | flags = bed->dynamic_sec_flags; |
252b5132 | 156 | |
14b2f831 AM |
157 | s = bfd_make_section_anyway_with_flags (abfd, |
158 | (bed->rela_plts_and_copies_p | |
159 | ? ".rela.got" : ".rel.got"), | |
160 | (bed->dynamic_sec_flags | |
161 | | SEC_READONLY)); | |
6de2ae4a L |
162 | if (s == NULL |
163 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
164 | return FALSE; | |
165 | htab->srelgot = s; | |
252b5132 | 166 | |
14b2f831 | 167 | s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); |
64e77c6d L |
168 | if (s == NULL |
169 | || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
170 | return FALSE; | |
171 | htab->sgot = s; | |
172 | ||
252b5132 RH |
173 | if (bed->want_got_plt) |
174 | { | |
14b2f831 | 175 | s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); |
252b5132 | 176 | if (s == NULL |
6de2ae4a L |
177 | || !bfd_set_section_alignment (abfd, s, |
178 | bed->s->log_file_align)) | |
b34976b6 | 179 | return FALSE; |
6de2ae4a | 180 | htab->sgotplt = s; |
252b5132 RH |
181 | } |
182 | ||
64e77c6d L |
183 | /* The first bit of the global offset table is the header. */ |
184 | s->size += bed->got_header_size; | |
185 | ||
2517a57f AM |
186 | if (bed->want_got_sym) |
187 | { | |
188 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
189 | (or .got.plt) section. We don't do this in the linker script | |
190 | because we don't want to define the symbol if we are not creating | |
191 | a global offset table. */ | |
6de2ae4a L |
192 | h = _bfd_elf_define_linkage_sym (abfd, info, s, |
193 | "_GLOBAL_OFFSET_TABLE_"); | |
2517a57f | 194 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
195 | if (h == NULL) |
196 | return FALSE; | |
2517a57f | 197 | } |
252b5132 | 198 | |
b34976b6 | 199 | return TRUE; |
252b5132 RH |
200 | } |
201 | \f | |
7e9f0867 AM |
202 | /* Create a strtab to hold the dynamic symbol names. */ |
203 | static bfd_boolean | |
204 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
205 | { | |
206 | struct elf_link_hash_table *hash_table; | |
207 | ||
208 | hash_table = elf_hash_table (info); | |
209 | if (hash_table->dynobj == NULL) | |
6cd255ca L |
210 | { |
211 | /* We may not set dynobj, an input file holding linker created | |
212 | dynamic sections to abfd, which may be a dynamic object with | |
213 | its own dynamic sections. We need to find a normal input file | |
214 | to hold linker created sections if possible. */ | |
215 | if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0) | |
216 | { | |
217 | bfd *ibfd; | |
218 | for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next) | |
6645479e L |
219 | if ((ibfd->flags |
220 | & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0) | |
6cd255ca L |
221 | { |
222 | abfd = ibfd; | |
223 | break; | |
224 | } | |
225 | } | |
226 | hash_table->dynobj = abfd; | |
227 | } | |
7e9f0867 AM |
228 | |
229 | if (hash_table->dynstr == NULL) | |
230 | { | |
231 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
232 | if (hash_table->dynstr == NULL) | |
233 | return FALSE; | |
234 | } | |
235 | return TRUE; | |
236 | } | |
237 | ||
45d6a902 AM |
238 | /* Create some sections which will be filled in with dynamic linking |
239 | information. ABFD is an input file which requires dynamic sections | |
240 | to be created. The dynamic sections take up virtual memory space | |
241 | when the final executable is run, so we need to create them before | |
242 | addresses are assigned to the output sections. We work out the | |
243 | actual contents and size of these sections later. */ | |
252b5132 | 244 | |
b34976b6 | 245 | bfd_boolean |
268b6b39 | 246 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 247 | { |
45d6a902 | 248 | flagword flags; |
91d6fa6a | 249 | asection *s; |
9c5bfbb7 | 250 | const struct elf_backend_data *bed; |
9637f6ef | 251 | struct elf_link_hash_entry *h; |
252b5132 | 252 | |
0eddce27 | 253 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
254 | return FALSE; |
255 | ||
256 | if (elf_hash_table (info)->dynamic_sections_created) | |
257 | return TRUE; | |
258 | ||
7e9f0867 AM |
259 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
260 | return FALSE; | |
45d6a902 | 261 | |
7e9f0867 | 262 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
263 | bed = get_elf_backend_data (abfd); |
264 | ||
265 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
266 | |
267 | /* A dynamically linked executable has a .interp section, but a | |
268 | shared library does not. */ | |
9b8b325a | 269 | if (bfd_link_executable (info) && !info->nointerp) |
252b5132 | 270 | { |
14b2f831 AM |
271 | s = bfd_make_section_anyway_with_flags (abfd, ".interp", |
272 | flags | SEC_READONLY); | |
3496cb2a | 273 | if (s == NULL) |
45d6a902 AM |
274 | return FALSE; |
275 | } | |
bb0deeff | 276 | |
45d6a902 AM |
277 | /* Create sections to hold version informations. These are removed |
278 | if they are not needed. */ | |
14b2f831 AM |
279 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d", |
280 | flags | SEC_READONLY); | |
45d6a902 | 281 | if (s == NULL |
45d6a902 AM |
282 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
283 | return FALSE; | |
284 | ||
14b2f831 AM |
285 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version", |
286 | flags | SEC_READONLY); | |
45d6a902 | 287 | if (s == NULL |
45d6a902 AM |
288 | || ! bfd_set_section_alignment (abfd, s, 1)) |
289 | return FALSE; | |
290 | ||
14b2f831 AM |
291 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r", |
292 | flags | SEC_READONLY); | |
45d6a902 | 293 | if (s == NULL |
45d6a902 AM |
294 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
295 | return FALSE; | |
296 | ||
14b2f831 AM |
297 | s = bfd_make_section_anyway_with_flags (abfd, ".dynsym", |
298 | flags | SEC_READONLY); | |
45d6a902 | 299 | if (s == NULL |
45d6a902 AM |
300 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
301 | return FALSE; | |
cae1fbbb | 302 | elf_hash_table (info)->dynsym = s; |
45d6a902 | 303 | |
14b2f831 AM |
304 | s = bfd_make_section_anyway_with_flags (abfd, ".dynstr", |
305 | flags | SEC_READONLY); | |
3496cb2a | 306 | if (s == NULL) |
45d6a902 AM |
307 | return FALSE; |
308 | ||
14b2f831 | 309 | s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 310 | if (s == NULL |
45d6a902 AM |
311 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
312 | return FALSE; | |
313 | ||
314 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
315 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
316 | only want to define it if we are, in fact, creating a .dynamic | |
317 | section. We don't want to define it if there is no .dynamic | |
318 | section, since on some ELF platforms the start up code examines it | |
319 | to decide how to initialize the process. */ | |
9637f6ef L |
320 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"); |
321 | elf_hash_table (info)->hdynamic = h; | |
322 | if (h == NULL) | |
45d6a902 AM |
323 | return FALSE; |
324 | ||
fdc90cb4 JJ |
325 | if (info->emit_hash) |
326 | { | |
14b2f831 AM |
327 | s = bfd_make_section_anyway_with_flags (abfd, ".hash", |
328 | flags | SEC_READONLY); | |
fdc90cb4 JJ |
329 | if (s == NULL |
330 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
331 | return FALSE; | |
332 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
333 | } | |
334 | ||
335 | if (info->emit_gnu_hash) | |
336 | { | |
14b2f831 AM |
337 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash", |
338 | flags | SEC_READONLY); | |
fdc90cb4 JJ |
339 | if (s == NULL |
340 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
341 | return FALSE; | |
342 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: | |
343 | 4 32-bit words followed by variable count of 64-bit words, then | |
344 | variable count of 32-bit words. */ | |
345 | if (bed->s->arch_size == 64) | |
346 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
347 | else | |
348 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
349 | } | |
45d6a902 AM |
350 | |
351 | /* Let the backend create the rest of the sections. This lets the | |
352 | backend set the right flags. The backend will normally create | |
353 | the .got and .plt sections. */ | |
894891db NC |
354 | if (bed->elf_backend_create_dynamic_sections == NULL |
355 | || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
45d6a902 AM |
356 | return FALSE; |
357 | ||
358 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
359 | ||
360 | return TRUE; | |
361 | } | |
362 | ||
363 | /* Create dynamic sections when linking against a dynamic object. */ | |
364 | ||
365 | bfd_boolean | |
268b6b39 | 366 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
367 | { |
368 | flagword flags, pltflags; | |
7325306f | 369 | struct elf_link_hash_entry *h; |
45d6a902 | 370 | asection *s; |
9c5bfbb7 | 371 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 372 | struct elf_link_hash_table *htab = elf_hash_table (info); |
45d6a902 | 373 | |
252b5132 RH |
374 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
375 | .rel[a].bss sections. */ | |
e5a52504 | 376 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
377 | |
378 | pltflags = flags; | |
252b5132 | 379 | if (bed->plt_not_loaded) |
6df4d94c MM |
380 | /* We do not clear SEC_ALLOC here because we still want the OS to |
381 | allocate space for the section; it's just that there's nothing | |
382 | to read in from the object file. */ | |
5d1634d7 | 383 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
384 | else |
385 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
386 | if (bed->plt_readonly) |
387 | pltflags |= SEC_READONLY; | |
388 | ||
14b2f831 | 389 | s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags); |
252b5132 | 390 | if (s == NULL |
252b5132 | 391 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 392 | return FALSE; |
6de2ae4a | 393 | htab->splt = s; |
252b5132 | 394 | |
d98685ac AM |
395 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
396 | .plt section. */ | |
7325306f RS |
397 | if (bed->want_plt_sym) |
398 | { | |
399 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
400 | "_PROCEDURE_LINKAGE_TABLE_"); | |
401 | elf_hash_table (info)->hplt = h; | |
402 | if (h == NULL) | |
403 | return FALSE; | |
404 | } | |
252b5132 | 405 | |
14b2f831 AM |
406 | s = bfd_make_section_anyway_with_flags (abfd, |
407 | (bed->rela_plts_and_copies_p | |
408 | ? ".rela.plt" : ".rel.plt"), | |
409 | flags | SEC_READONLY); | |
252b5132 | 410 | if (s == NULL |
45d6a902 | 411 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 412 | return FALSE; |
6de2ae4a | 413 | htab->srelplt = s; |
252b5132 RH |
414 | |
415 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 416 | return FALSE; |
252b5132 | 417 | |
3018b441 RH |
418 | if (bed->want_dynbss) |
419 | { | |
420 | /* The .dynbss section is a place to put symbols which are defined | |
421 | by dynamic objects, are referenced by regular objects, and are | |
422 | not functions. We must allocate space for them in the process | |
423 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
424 | initialize them at run time. The linker script puts the .dynbss | |
425 | section into the .bss section of the final image. */ | |
14b2f831 AM |
426 | s = bfd_make_section_anyway_with_flags (abfd, ".dynbss", |
427 | (SEC_ALLOC | SEC_LINKER_CREATED)); | |
3496cb2a | 428 | if (s == NULL) |
b34976b6 | 429 | return FALSE; |
252b5132 | 430 | |
3018b441 | 431 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
432 | normally needed. We need to create it here, though, so that the |
433 | linker will map it to an output section. We can't just create it | |
434 | only if we need it, because we will not know whether we need it | |
435 | until we have seen all the input files, and the first time the | |
436 | main linker code calls BFD after examining all the input files | |
437 | (size_dynamic_sections) the input sections have already been | |
438 | mapped to the output sections. If the section turns out not to | |
439 | be needed, we can discard it later. We will never need this | |
440 | section when generating a shared object, since they do not use | |
441 | copy relocs. */ | |
0e1862bb | 442 | if (! bfd_link_pic (info)) |
3018b441 | 443 | { |
14b2f831 AM |
444 | s = bfd_make_section_anyway_with_flags (abfd, |
445 | (bed->rela_plts_and_copies_p | |
446 | ? ".rela.bss" : ".rel.bss"), | |
447 | flags | SEC_READONLY); | |
3018b441 | 448 | if (s == NULL |
45d6a902 | 449 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 450 | return FALSE; |
3018b441 | 451 | } |
252b5132 RH |
452 | } |
453 | ||
b34976b6 | 454 | return TRUE; |
252b5132 RH |
455 | } |
456 | \f | |
252b5132 RH |
457 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
458 | read the input files, since we need to have a list of all of them | |
459 | before we can determine the final sizes of the output sections. | |
460 | Note that we may actually call this function even though we are not | |
461 | going to output any dynamic symbols; in some cases we know that a | |
462 | symbol should be in the dynamic symbol table, but only if there is | |
463 | one. */ | |
464 | ||
b34976b6 | 465 | bfd_boolean |
c152c796 AM |
466 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
467 | struct elf_link_hash_entry *h) | |
252b5132 RH |
468 | { |
469 | if (h->dynindx == -1) | |
470 | { | |
2b0f7ef9 | 471 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 472 | char *p; |
252b5132 | 473 | const char *name; |
ef53be89 | 474 | size_t indx; |
252b5132 | 475 | |
7a13edea NC |
476 | /* XXX: The ABI draft says the linker must turn hidden and |
477 | internal symbols into STB_LOCAL symbols when producing the | |
478 | DSO. However, if ld.so honors st_other in the dynamic table, | |
479 | this would not be necessary. */ | |
480 | switch (ELF_ST_VISIBILITY (h->other)) | |
481 | { | |
482 | case STV_INTERNAL: | |
483 | case STV_HIDDEN: | |
9d6eee78 L |
484 | if (h->root.type != bfd_link_hash_undefined |
485 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 486 | { |
f5385ebf | 487 | h->forced_local = 1; |
67687978 PB |
488 | if (!elf_hash_table (info)->is_relocatable_executable) |
489 | return TRUE; | |
7a13edea | 490 | } |
0444bdd4 | 491 | |
7a13edea NC |
492 | default: |
493 | break; | |
494 | } | |
495 | ||
252b5132 RH |
496 | h->dynindx = elf_hash_table (info)->dynsymcount; |
497 | ++elf_hash_table (info)->dynsymcount; | |
498 | ||
499 | dynstr = elf_hash_table (info)->dynstr; | |
500 | if (dynstr == NULL) | |
501 | { | |
502 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 503 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 504 | if (dynstr == NULL) |
b34976b6 | 505 | return FALSE; |
252b5132 RH |
506 | } |
507 | ||
508 | /* We don't put any version information in the dynamic string | |
aad5d350 | 509 | table. */ |
252b5132 RH |
510 | name = h->root.root.string; |
511 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
512 | if (p != NULL) |
513 | /* We know that the p points into writable memory. In fact, | |
514 | there are only a few symbols that have read-only names, being | |
515 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
516 | by the backends. Most symbols will have names pointing into | |
517 | an ELF string table read from a file, or to objalloc memory. */ | |
518 | *p = 0; | |
519 | ||
520 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
521 | ||
522 | if (p != NULL) | |
523 | *p = ELF_VER_CHR; | |
252b5132 | 524 | |
ef53be89 | 525 | if (indx == (size_t) -1) |
b34976b6 | 526 | return FALSE; |
252b5132 RH |
527 | h->dynstr_index = indx; |
528 | } | |
529 | ||
b34976b6 | 530 | return TRUE; |
252b5132 | 531 | } |
45d6a902 | 532 | \f |
55255dae L |
533 | /* Mark a symbol dynamic. */ |
534 | ||
28caa186 | 535 | static void |
55255dae | 536 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, |
40b36307 L |
537 | struct elf_link_hash_entry *h, |
538 | Elf_Internal_Sym *sym) | |
55255dae | 539 | { |
40b36307 | 540 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
55255dae | 541 | |
40b36307 | 542 | /* It may be called more than once on the same H. */ |
0e1862bb | 543 | if(h->dynamic || bfd_link_relocatable (info)) |
55255dae L |
544 | return; |
545 | ||
40b36307 L |
546 | if ((info->dynamic_data |
547 | && (h->type == STT_OBJECT | |
b8871f35 | 548 | || h->type == STT_COMMON |
40b36307 | 549 | || (sym != NULL |
b8871f35 L |
550 | && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT |
551 | || ELF_ST_TYPE (sym->st_info) == STT_COMMON)))) | |
a0c8462f | 552 | || (d != NULL |
40b36307 L |
553 | && h->root.type == bfd_link_hash_new |
554 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
55255dae L |
555 | h->dynamic = 1; |
556 | } | |
557 | ||
45d6a902 AM |
558 | /* Record an assignment to a symbol made by a linker script. We need |
559 | this in case some dynamic object refers to this symbol. */ | |
560 | ||
561 | bfd_boolean | |
fe21a8fc L |
562 | bfd_elf_record_link_assignment (bfd *output_bfd, |
563 | struct bfd_link_info *info, | |
268b6b39 | 564 | const char *name, |
fe21a8fc L |
565 | bfd_boolean provide, |
566 | bfd_boolean hidden) | |
45d6a902 | 567 | { |
00cbee0a | 568 | struct elf_link_hash_entry *h, *hv; |
4ea42fb7 | 569 | struct elf_link_hash_table *htab; |
00cbee0a | 570 | const struct elf_backend_data *bed; |
45d6a902 | 571 | |
0eddce27 | 572 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
573 | return TRUE; |
574 | ||
4ea42fb7 AM |
575 | htab = elf_hash_table (info); |
576 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 577 | if (h == NULL) |
4ea42fb7 | 578 | return provide; |
45d6a902 | 579 | |
0f550b3d L |
580 | if (h->versioned == unknown) |
581 | { | |
582 | /* Set versioned if symbol version is unknown. */ | |
583 | char *version = strrchr (name, ELF_VER_CHR); | |
584 | if (version) | |
585 | { | |
586 | if (version > name && version[-1] != ELF_VER_CHR) | |
587 | h->versioned = versioned_hidden; | |
588 | else | |
589 | h->versioned = versioned; | |
590 | } | |
591 | } | |
592 | ||
00cbee0a | 593 | switch (h->root.type) |
77cfaee6 | 594 | { |
00cbee0a L |
595 | case bfd_link_hash_defined: |
596 | case bfd_link_hash_defweak: | |
597 | case bfd_link_hash_common: | |
598 | break; | |
599 | case bfd_link_hash_undefweak: | |
600 | case bfd_link_hash_undefined: | |
601 | /* Since we're defining the symbol, don't let it seem to have not | |
602 | been defined. record_dynamic_symbol and size_dynamic_sections | |
603 | may depend on this. */ | |
4ea42fb7 | 604 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
605 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
606 | bfd_link_repair_undef_list (&htab->root); | |
00cbee0a L |
607 | break; |
608 | case bfd_link_hash_new: | |
40b36307 | 609 | bfd_elf_link_mark_dynamic_symbol (info, h, NULL); |
55255dae | 610 | h->non_elf = 0; |
00cbee0a L |
611 | break; |
612 | case bfd_link_hash_indirect: | |
613 | /* We had a versioned symbol in a dynamic library. We make the | |
a0c8462f | 614 | the versioned symbol point to this one. */ |
00cbee0a L |
615 | bed = get_elf_backend_data (output_bfd); |
616 | hv = h; | |
617 | while (hv->root.type == bfd_link_hash_indirect | |
618 | || hv->root.type == bfd_link_hash_warning) | |
619 | hv = (struct elf_link_hash_entry *) hv->root.u.i.link; | |
620 | /* We don't need to update h->root.u since linker will set them | |
621 | later. */ | |
622 | h->root.type = bfd_link_hash_undefined; | |
623 | hv->root.type = bfd_link_hash_indirect; | |
624 | hv->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
625 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); | |
626 | break; | |
627 | case bfd_link_hash_warning: | |
628 | abort (); | |
629 | break; | |
55255dae | 630 | } |
45d6a902 AM |
631 | |
632 | /* If this symbol is being provided by the linker script, and it is | |
633 | currently defined by a dynamic object, but not by a regular | |
634 | object, then mark it as undefined so that the generic linker will | |
635 | force the correct value. */ | |
636 | if (provide | |
f5385ebf AM |
637 | && h->def_dynamic |
638 | && !h->def_regular) | |
45d6a902 AM |
639 | h->root.type = bfd_link_hash_undefined; |
640 | ||
641 | /* If this symbol is not being provided by the linker script, and it is | |
642 | currently defined by a dynamic object, but not by a regular object, | |
643 | then clear out any version information because the symbol will not be | |
644 | associated with the dynamic object any more. */ | |
645 | if (!provide | |
f5385ebf AM |
646 | && h->def_dynamic |
647 | && !h->def_regular) | |
45d6a902 AM |
648 | h->verinfo.verdef = NULL; |
649 | ||
f5385ebf | 650 | h->def_regular = 1; |
45d6a902 | 651 | |
eb8476a6 | 652 | if (hidden) |
fe21a8fc | 653 | { |
91d6fa6a | 654 | bed = get_elf_backend_data (output_bfd); |
b8297068 AM |
655 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
656 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
fe21a8fc L |
657 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
658 | } | |
659 | ||
6fa3860b PB |
660 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
661 | and executables. */ | |
0e1862bb | 662 | if (!bfd_link_relocatable (info) |
6fa3860b PB |
663 | && h->dynindx != -1 |
664 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
665 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
666 | h->forced_local = 1; | |
667 | ||
f5385ebf AM |
668 | if ((h->def_dynamic |
669 | || h->ref_dynamic | |
6b3b0ab8 L |
670 | || bfd_link_dll (info) |
671 | || elf_hash_table (info)->is_relocatable_executable) | |
45d6a902 AM |
672 | && h->dynindx == -1) |
673 | { | |
c152c796 | 674 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
675 | return FALSE; |
676 | ||
677 | /* If this is a weak defined symbol, and we know a corresponding | |
678 | real symbol from the same dynamic object, make sure the real | |
679 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
680 | if (h->u.weakdef != NULL |
681 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 682 | { |
f6e332e6 | 683 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
684 | return FALSE; |
685 | } | |
686 | } | |
687 | ||
688 | return TRUE; | |
689 | } | |
42751cf3 | 690 | |
8c58d23b AM |
691 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
692 | success, and 2 on a failure caused by attempting to record a symbol | |
693 | in a discarded section, eg. a discarded link-once section symbol. */ | |
694 | ||
695 | int | |
c152c796 AM |
696 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
697 | bfd *input_bfd, | |
698 | long input_indx) | |
8c58d23b AM |
699 | { |
700 | bfd_size_type amt; | |
701 | struct elf_link_local_dynamic_entry *entry; | |
702 | struct elf_link_hash_table *eht; | |
703 | struct elf_strtab_hash *dynstr; | |
ef53be89 | 704 | size_t dynstr_index; |
8c58d23b AM |
705 | char *name; |
706 | Elf_External_Sym_Shndx eshndx; | |
707 | char esym[sizeof (Elf64_External_Sym)]; | |
708 | ||
0eddce27 | 709 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
710 | return 0; |
711 | ||
712 | /* See if the entry exists already. */ | |
713 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
714 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
715 | return 1; | |
716 | ||
717 | amt = sizeof (*entry); | |
a50b1753 | 718 | entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); |
8c58d23b AM |
719 | if (entry == NULL) |
720 | return 0; | |
721 | ||
722 | /* Go find the symbol, so that we can find it's name. */ | |
723 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 724 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
725 | { |
726 | bfd_release (input_bfd, entry); | |
727 | return 0; | |
728 | } | |
729 | ||
730 | if (entry->isym.st_shndx != SHN_UNDEF | |
4fbb74a6 | 731 | && entry->isym.st_shndx < SHN_LORESERVE) |
8c58d23b AM |
732 | { |
733 | asection *s; | |
734 | ||
735 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
736 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
737 | { | |
738 | /* We can still bfd_release here as nothing has done another | |
739 | bfd_alloc. We can't do this later in this function. */ | |
740 | bfd_release (input_bfd, entry); | |
741 | return 2; | |
742 | } | |
743 | } | |
744 | ||
745 | name = (bfd_elf_string_from_elf_section | |
746 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
747 | entry->isym.st_name)); | |
748 | ||
749 | dynstr = elf_hash_table (info)->dynstr; | |
750 | if (dynstr == NULL) | |
751 | { | |
752 | /* Create a strtab to hold the dynamic symbol names. */ | |
753 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
754 | if (dynstr == NULL) | |
755 | return 0; | |
756 | } | |
757 | ||
b34976b6 | 758 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
ef53be89 | 759 | if (dynstr_index == (size_t) -1) |
8c58d23b AM |
760 | return 0; |
761 | entry->isym.st_name = dynstr_index; | |
762 | ||
763 | eht = elf_hash_table (info); | |
764 | ||
765 | entry->next = eht->dynlocal; | |
766 | eht->dynlocal = entry; | |
767 | entry->input_bfd = input_bfd; | |
768 | entry->input_indx = input_indx; | |
769 | eht->dynsymcount++; | |
770 | ||
771 | /* Whatever binding the symbol had before, it's now local. */ | |
772 | entry->isym.st_info | |
773 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
774 | ||
775 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
776 | ||
777 | return 1; | |
778 | } | |
779 | ||
30b30c21 | 780 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 781 | |
30b30c21 | 782 | long |
268b6b39 AM |
783 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
784 | bfd *input_bfd, | |
785 | long input_indx) | |
30b30c21 RH |
786 | { |
787 | struct elf_link_local_dynamic_entry *e; | |
788 | ||
789 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
790 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
791 | return e->dynindx; | |
792 | return -1; | |
793 | } | |
794 | ||
795 | /* This function is used to renumber the dynamic symbols, if some of | |
796 | them are removed because they are marked as local. This is called | |
797 | via elf_link_hash_traverse. */ | |
798 | ||
b34976b6 | 799 | static bfd_boolean |
268b6b39 AM |
800 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
801 | void *data) | |
42751cf3 | 802 | { |
a50b1753 | 803 | size_t *count = (size_t *) data; |
30b30c21 | 804 | |
6fa3860b PB |
805 | if (h->forced_local) |
806 | return TRUE; | |
807 | ||
808 | if (h->dynindx != -1) | |
809 | h->dynindx = ++(*count); | |
810 | ||
811 | return TRUE; | |
812 | } | |
813 | ||
814 | ||
815 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
816 | STB_LOCAL binding. */ | |
817 | ||
818 | static bfd_boolean | |
819 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
820 | void *data) | |
821 | { | |
a50b1753 | 822 | size_t *count = (size_t *) data; |
6fa3860b | 823 | |
6fa3860b PB |
824 | if (!h->forced_local) |
825 | return TRUE; | |
826 | ||
42751cf3 | 827 | if (h->dynindx != -1) |
30b30c21 RH |
828 | h->dynindx = ++(*count); |
829 | ||
b34976b6 | 830 | return TRUE; |
42751cf3 | 831 | } |
30b30c21 | 832 | |
aee6f5b4 AO |
833 | /* Return true if the dynamic symbol for a given section should be |
834 | omitted when creating a shared library. */ | |
835 | bfd_boolean | |
836 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
837 | struct bfd_link_info *info, | |
838 | asection *p) | |
839 | { | |
74541ad4 | 840 | struct elf_link_hash_table *htab; |
ca55926c | 841 | asection *ip; |
74541ad4 | 842 | |
aee6f5b4 AO |
843 | switch (elf_section_data (p)->this_hdr.sh_type) |
844 | { | |
845 | case SHT_PROGBITS: | |
846 | case SHT_NOBITS: | |
847 | /* If sh_type is yet undecided, assume it could be | |
848 | SHT_PROGBITS/SHT_NOBITS. */ | |
849 | case SHT_NULL: | |
74541ad4 AM |
850 | htab = elf_hash_table (info); |
851 | if (p == htab->tls_sec) | |
852 | return FALSE; | |
853 | ||
854 | if (htab->text_index_section != NULL) | |
855 | return p != htab->text_index_section && p != htab->data_index_section; | |
856 | ||
ca55926c | 857 | return (htab->dynobj != NULL |
3d4d4302 | 858 | && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL |
ca55926c | 859 | && ip->output_section == p); |
aee6f5b4 AO |
860 | |
861 | /* There shouldn't be section relative relocations | |
862 | against any other section. */ | |
863 | default: | |
864 | return TRUE; | |
865 | } | |
866 | } | |
867 | ||
062e2358 | 868 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
869 | symbol for each output section, which come first. Next come symbols |
870 | which have been forced to local binding. Then all of the back-end | |
871 | allocated local dynamic syms, followed by the rest of the global | |
872 | symbols. */ | |
30b30c21 | 873 | |
554220db AM |
874 | static unsigned long |
875 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
876 | struct bfd_link_info *info, | |
877 | unsigned long *section_sym_count) | |
30b30c21 RH |
878 | { |
879 | unsigned long dynsymcount = 0; | |
880 | ||
0e1862bb L |
881 | if (bfd_link_pic (info) |
882 | || elf_hash_table (info)->is_relocatable_executable) | |
30b30c21 | 883 | { |
aee6f5b4 | 884 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
885 | asection *p; |
886 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 887 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
888 | && (p->flags & SEC_ALLOC) != 0 |
889 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
890 | elf_section_data (p)->dynindx = ++dynsymcount; | |
74541ad4 AM |
891 | else |
892 | elf_section_data (p)->dynindx = 0; | |
30b30c21 | 893 | } |
554220db | 894 | *section_sym_count = dynsymcount; |
30b30c21 | 895 | |
6fa3860b PB |
896 | elf_link_hash_traverse (elf_hash_table (info), |
897 | elf_link_renumber_local_hash_table_dynsyms, | |
898 | &dynsymcount); | |
899 | ||
30b30c21 RH |
900 | if (elf_hash_table (info)->dynlocal) |
901 | { | |
902 | struct elf_link_local_dynamic_entry *p; | |
903 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
904 | p->dynindx = ++dynsymcount; | |
905 | } | |
90ac2420 | 906 | elf_hash_table (info)->local_dynsymcount = dynsymcount; |
30b30c21 RH |
907 | |
908 | elf_link_hash_traverse (elf_hash_table (info), | |
909 | elf_link_renumber_hash_table_dynsyms, | |
910 | &dynsymcount); | |
911 | ||
d5486c43 L |
912 | /* There is an unused NULL entry at the head of the table which we |
913 | must account for in our count even if the table is empty since it | |
914 | is intended for the mandatory DT_SYMTAB tag (.dynsym section) in | |
915 | .dynamic section. */ | |
916 | dynsymcount++; | |
30b30c21 | 917 | |
ccabcbe5 AM |
918 | elf_hash_table (info)->dynsymcount = dynsymcount; |
919 | return dynsymcount; | |
30b30c21 | 920 | } |
252b5132 | 921 | |
54ac0771 L |
922 | /* Merge st_other field. */ |
923 | ||
924 | static void | |
925 | elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, | |
b8417128 | 926 | const Elf_Internal_Sym *isym, asection *sec, |
cd3416da | 927 | bfd_boolean definition, bfd_boolean dynamic) |
54ac0771 L |
928 | { |
929 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
930 | ||
931 | /* If st_other has a processor-specific meaning, specific | |
cd3416da | 932 | code might be needed here. */ |
54ac0771 L |
933 | if (bed->elf_backend_merge_symbol_attribute) |
934 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
935 | dynamic); | |
936 | ||
cd3416da | 937 | if (!dynamic) |
54ac0771 | 938 | { |
cd3416da AM |
939 | unsigned symvis = ELF_ST_VISIBILITY (isym->st_other); |
940 | unsigned hvis = ELF_ST_VISIBILITY (h->other); | |
54ac0771 | 941 | |
cd3416da AM |
942 | /* Keep the most constraining visibility. Leave the remainder |
943 | of the st_other field to elf_backend_merge_symbol_attribute. */ | |
944 | if (symvis - 1 < hvis - 1) | |
945 | h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); | |
54ac0771 | 946 | } |
b8417128 AM |
947 | else if (definition |
948 | && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT | |
949 | && (sec->flags & SEC_READONLY) == 0) | |
6cabe1ea | 950 | h->protected_def = 1; |
54ac0771 L |
951 | } |
952 | ||
4f3fedcf AM |
953 | /* This function is called when we want to merge a new symbol with an |
954 | existing symbol. It handles the various cases which arise when we | |
955 | find a definition in a dynamic object, or when there is already a | |
956 | definition in a dynamic object. The new symbol is described by | |
957 | NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table | |
958 | entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK | |
959 | if the old symbol was weak. We set POLD_ALIGNMENT to the alignment | |
960 | of an old common symbol. We set OVERRIDE if the old symbol is | |
961 | overriding a new definition. We set TYPE_CHANGE_OK if it is OK for | |
962 | the type to change. We set SIZE_CHANGE_OK if it is OK for the size | |
963 | to change. By OK to change, we mean that we shouldn't warn if the | |
964 | type or size does change. */ | |
45d6a902 | 965 | |
8a56bd02 | 966 | static bfd_boolean |
268b6b39 AM |
967 | _bfd_elf_merge_symbol (bfd *abfd, |
968 | struct bfd_link_info *info, | |
969 | const char *name, | |
970 | Elf_Internal_Sym *sym, | |
971 | asection **psec, | |
972 | bfd_vma *pvalue, | |
4f3fedcf AM |
973 | struct elf_link_hash_entry **sym_hash, |
974 | bfd **poldbfd, | |
37a9e49a | 975 | bfd_boolean *pold_weak, |
af44c138 | 976 | unsigned int *pold_alignment, |
268b6b39 AM |
977 | bfd_boolean *skip, |
978 | bfd_boolean *override, | |
979 | bfd_boolean *type_change_ok, | |
6e33951e L |
980 | bfd_boolean *size_change_ok, |
981 | bfd_boolean *matched) | |
252b5132 | 982 | { |
7479dfd4 | 983 | asection *sec, *oldsec; |
45d6a902 | 984 | struct elf_link_hash_entry *h; |
90c984fc | 985 | struct elf_link_hash_entry *hi; |
45d6a902 AM |
986 | struct elf_link_hash_entry *flip; |
987 | int bind; | |
988 | bfd *oldbfd; | |
989 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
0a36a439 | 990 | bfd_boolean newweak, oldweak, newfunc, oldfunc; |
a4d8e49b | 991 | const struct elf_backend_data *bed; |
6e33951e | 992 | char *new_version; |
45d6a902 AM |
993 | |
994 | *skip = FALSE; | |
995 | *override = FALSE; | |
996 | ||
997 | sec = *psec; | |
998 | bind = ELF_ST_BIND (sym->st_info); | |
999 | ||
1000 | if (! bfd_is_und_section (sec)) | |
1001 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
1002 | else | |
1003 | h = ((struct elf_link_hash_entry *) | |
1004 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
1005 | if (h == NULL) | |
1006 | return FALSE; | |
1007 | *sym_hash = h; | |
252b5132 | 1008 | |
88ba32a0 L |
1009 | bed = get_elf_backend_data (abfd); |
1010 | ||
6e33951e | 1011 | /* NEW_VERSION is the symbol version of the new symbol. */ |
422f1182 | 1012 | if (h->versioned != unversioned) |
6e33951e | 1013 | { |
422f1182 L |
1014 | /* Symbol version is unknown or versioned. */ |
1015 | new_version = strrchr (name, ELF_VER_CHR); | |
1016 | if (new_version) | |
1017 | { | |
1018 | if (h->versioned == unknown) | |
1019 | { | |
1020 | if (new_version > name && new_version[-1] != ELF_VER_CHR) | |
1021 | h->versioned = versioned_hidden; | |
1022 | else | |
1023 | h->versioned = versioned; | |
1024 | } | |
1025 | new_version += 1; | |
1026 | if (new_version[0] == '\0') | |
1027 | new_version = NULL; | |
1028 | } | |
1029 | else | |
1030 | h->versioned = unversioned; | |
6e33951e | 1031 | } |
422f1182 L |
1032 | else |
1033 | new_version = NULL; | |
6e33951e | 1034 | |
90c984fc L |
1035 | /* For merging, we only care about real symbols. But we need to make |
1036 | sure that indirect symbol dynamic flags are updated. */ | |
1037 | hi = h; | |
45d6a902 AM |
1038 | while (h->root.type == bfd_link_hash_indirect |
1039 | || h->root.type == bfd_link_hash_warning) | |
1040 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1041 | ||
6e33951e L |
1042 | if (!*matched) |
1043 | { | |
1044 | if (hi == h || h->root.type == bfd_link_hash_new) | |
1045 | *matched = TRUE; | |
1046 | else | |
1047 | { | |
ae7683d2 | 1048 | /* OLD_HIDDEN is true if the existing symbol is only visible |
6e33951e | 1049 | to the symbol with the same symbol version. NEW_HIDDEN is |
ae7683d2 | 1050 | true if the new symbol is only visible to the symbol with |
6e33951e | 1051 | the same symbol version. */ |
422f1182 L |
1052 | bfd_boolean old_hidden = h->versioned == versioned_hidden; |
1053 | bfd_boolean new_hidden = hi->versioned == versioned_hidden; | |
6e33951e L |
1054 | if (!old_hidden && !new_hidden) |
1055 | /* The new symbol matches the existing symbol if both | |
1056 | aren't hidden. */ | |
1057 | *matched = TRUE; | |
1058 | else | |
1059 | { | |
1060 | /* OLD_VERSION is the symbol version of the existing | |
1061 | symbol. */ | |
422f1182 L |
1062 | char *old_version; |
1063 | ||
1064 | if (h->versioned >= versioned) | |
1065 | old_version = strrchr (h->root.root.string, | |
1066 | ELF_VER_CHR) + 1; | |
1067 | else | |
1068 | old_version = NULL; | |
6e33951e L |
1069 | |
1070 | /* The new symbol matches the existing symbol if they | |
1071 | have the same symbol version. */ | |
1072 | *matched = (old_version == new_version | |
1073 | || (old_version != NULL | |
1074 | && new_version != NULL | |
1075 | && strcmp (old_version, new_version) == 0)); | |
1076 | } | |
1077 | } | |
1078 | } | |
1079 | ||
934bce08 AM |
1080 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
1081 | existing symbol. */ | |
1082 | ||
1083 | oldbfd = NULL; | |
1084 | oldsec = NULL; | |
1085 | switch (h->root.type) | |
1086 | { | |
1087 | default: | |
1088 | break; | |
1089 | ||
1090 | case bfd_link_hash_undefined: | |
1091 | case bfd_link_hash_undefweak: | |
1092 | oldbfd = h->root.u.undef.abfd; | |
1093 | break; | |
1094 | ||
1095 | case bfd_link_hash_defined: | |
1096 | case bfd_link_hash_defweak: | |
1097 | oldbfd = h->root.u.def.section->owner; | |
1098 | oldsec = h->root.u.def.section; | |
1099 | break; | |
1100 | ||
1101 | case bfd_link_hash_common: | |
1102 | oldbfd = h->root.u.c.p->section->owner; | |
1103 | oldsec = h->root.u.c.p->section; | |
1104 | if (pold_alignment) | |
1105 | *pold_alignment = h->root.u.c.p->alignment_power; | |
1106 | break; | |
1107 | } | |
1108 | if (poldbfd && *poldbfd == NULL) | |
1109 | *poldbfd = oldbfd; | |
1110 | ||
1111 | /* Differentiate strong and weak symbols. */ | |
1112 | newweak = bind == STB_WEAK; | |
1113 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1114 | || h->root.type == bfd_link_hash_undefweak); | |
1115 | if (pold_weak) | |
1116 | *pold_weak = oldweak; | |
1117 | ||
1118 | /* This code is for coping with dynamic objects, and is only useful | |
1119 | if we are doing an ELF link. */ | |
1120 | if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
1121 | return TRUE; | |
1122 | ||
40b36307 | 1123 | /* We have to check it for every instance since the first few may be |
ee659f1f | 1124 | references and not all compilers emit symbol type for undefined |
40b36307 L |
1125 | symbols. */ |
1126 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
1127 | ||
ee659f1f AM |
1128 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, |
1129 | respectively, is from a dynamic object. */ | |
1130 | ||
1131 | newdyn = (abfd->flags & DYNAMIC) != 0; | |
1132 | ||
1133 | /* ref_dynamic_nonweak and dynamic_def flags track actual undefined | |
1134 | syms and defined syms in dynamic libraries respectively. | |
1135 | ref_dynamic on the other hand can be set for a symbol defined in | |
1136 | a dynamic library, and def_dynamic may not be set; When the | |
1137 | definition in a dynamic lib is overridden by a definition in the | |
1138 | executable use of the symbol in the dynamic lib becomes a | |
1139 | reference to the executable symbol. */ | |
1140 | if (newdyn) | |
1141 | { | |
1142 | if (bfd_is_und_section (sec)) | |
1143 | { | |
1144 | if (bind != STB_WEAK) | |
1145 | { | |
1146 | h->ref_dynamic_nonweak = 1; | |
1147 | hi->ref_dynamic_nonweak = 1; | |
1148 | } | |
1149 | } | |
1150 | else | |
1151 | { | |
6e33951e L |
1152 | /* Update the existing symbol only if they match. */ |
1153 | if (*matched) | |
1154 | h->dynamic_def = 1; | |
ee659f1f AM |
1155 | hi->dynamic_def = 1; |
1156 | } | |
1157 | } | |
1158 | ||
45d6a902 AM |
1159 | /* If we just created the symbol, mark it as being an ELF symbol. |
1160 | Other than that, there is nothing to do--there is no merge issue | |
1161 | with a newly defined symbol--so we just return. */ | |
1162 | ||
1163 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 1164 | { |
f5385ebf | 1165 | h->non_elf = 0; |
45d6a902 AM |
1166 | return TRUE; |
1167 | } | |
252b5132 | 1168 | |
45d6a902 AM |
1169 | /* In cases involving weak versioned symbols, we may wind up trying |
1170 | to merge a symbol with itself. Catch that here, to avoid the | |
1171 | confusion that results if we try to override a symbol with | |
1172 | itself. The additional tests catch cases like | |
1173 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
1174 | dynamic object, which we do want to handle here. */ | |
1175 | if (abfd == oldbfd | |
895fa45f | 1176 | && (newweak || oldweak) |
45d6a902 | 1177 | && ((abfd->flags & DYNAMIC) == 0 |
f5385ebf | 1178 | || !h->def_regular)) |
45d6a902 AM |
1179 | return TRUE; |
1180 | ||
707bba77 | 1181 | olddyn = FALSE; |
45d6a902 AM |
1182 | if (oldbfd != NULL) |
1183 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 1184 | else if (oldsec != NULL) |
45d6a902 | 1185 | { |
707bba77 | 1186 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 1187 | indices used by MIPS ELF. */ |
707bba77 | 1188 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 1189 | } |
252b5132 | 1190 | |
45d6a902 AM |
1191 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
1192 | respectively, appear to be a definition rather than reference. */ | |
1193 | ||
707bba77 | 1194 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 1195 | |
707bba77 AM |
1196 | olddef = (h->root.type != bfd_link_hash_undefined |
1197 | && h->root.type != bfd_link_hash_undefweak | |
202ac193 | 1198 | && h->root.type != bfd_link_hash_common); |
45d6a902 | 1199 | |
0a36a439 L |
1200 | /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, |
1201 | respectively, appear to be a function. */ | |
1202 | ||
1203 | newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1204 | && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); | |
1205 | ||
1206 | oldfunc = (h->type != STT_NOTYPE | |
1207 | && bed->is_function_type (h->type)); | |
1208 | ||
5b677558 AM |
1209 | /* If creating a default indirect symbol ("foo" or "foo@") from a |
1210 | dynamic versioned definition ("foo@@") skip doing so if there is | |
1211 | an existing regular definition with a different type. We don't | |
1212 | want, for example, a "time" variable in the executable overriding | |
1213 | a "time" function in a shared library. */ | |
580a2b6e | 1214 | if (pold_alignment == NULL |
580a2b6e L |
1215 | && newdyn |
1216 | && newdef | |
1217 | && !olddyn | |
5b677558 AM |
1218 | && (olddef || h->root.type == bfd_link_hash_common) |
1219 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1220 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1221 | && h->type != STT_NOTYPE | |
1222 | && !(newfunc && oldfunc)) | |
580a2b6e L |
1223 | { |
1224 | *skip = TRUE; | |
1225 | return TRUE; | |
1226 | } | |
1227 | ||
4c34aff8 AM |
1228 | /* Check TLS symbols. We don't check undefined symbols introduced |
1229 | by "ld -u" which have no type (and oldbfd NULL), and we don't | |
1230 | check symbols from plugins because they also have no type. */ | |
1231 | if (oldbfd != NULL | |
1232 | && (oldbfd->flags & BFD_PLUGIN) == 0 | |
1233 | && (abfd->flags & BFD_PLUGIN) == 0 | |
1234 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1235 | && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) | |
7479dfd4 L |
1236 | { |
1237 | bfd *ntbfd, *tbfd; | |
1238 | bfd_boolean ntdef, tdef; | |
1239 | asection *ntsec, *tsec; | |
1240 | ||
1241 | if (h->type == STT_TLS) | |
1242 | { | |
3b36f7e6 | 1243 | ntbfd = abfd; |
7479dfd4 L |
1244 | ntsec = sec; |
1245 | ntdef = newdef; | |
1246 | tbfd = oldbfd; | |
1247 | tsec = oldsec; | |
1248 | tdef = olddef; | |
1249 | } | |
1250 | else | |
1251 | { | |
1252 | ntbfd = oldbfd; | |
1253 | ntsec = oldsec; | |
1254 | ntdef = olddef; | |
1255 | tbfd = abfd; | |
1256 | tsec = sec; | |
1257 | tdef = newdef; | |
1258 | } | |
1259 | ||
1260 | if (tdef && ntdef) | |
4eca0228 | 1261 | _bfd_error_handler |
695344c0 | 1262 | /* xgettext:c-format */ |
191c0c42 AM |
1263 | (_("%s: TLS definition in %B section %A " |
1264 | "mismatches non-TLS definition in %B section %A"), | |
7479dfd4 L |
1265 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); |
1266 | else if (!tdef && !ntdef) | |
4eca0228 | 1267 | _bfd_error_handler |
695344c0 | 1268 | /* xgettext:c-format */ |
191c0c42 AM |
1269 | (_("%s: TLS reference in %B " |
1270 | "mismatches non-TLS reference in %B"), | |
7479dfd4 L |
1271 | tbfd, ntbfd, h->root.root.string); |
1272 | else if (tdef) | |
4eca0228 | 1273 | _bfd_error_handler |
695344c0 | 1274 | /* xgettext:c-format */ |
191c0c42 AM |
1275 | (_("%s: TLS definition in %B section %A " |
1276 | "mismatches non-TLS reference in %B"), | |
7479dfd4 L |
1277 | tbfd, tsec, ntbfd, h->root.root.string); |
1278 | else | |
4eca0228 | 1279 | _bfd_error_handler |
695344c0 | 1280 | /* xgettext:c-format */ |
191c0c42 AM |
1281 | (_("%s: TLS reference in %B " |
1282 | "mismatches non-TLS definition in %B section %A"), | |
7479dfd4 L |
1283 | tbfd, ntbfd, ntsec, h->root.root.string); |
1284 | ||
1285 | bfd_set_error (bfd_error_bad_value); | |
1286 | return FALSE; | |
1287 | } | |
1288 | ||
45d6a902 AM |
1289 | /* If the old symbol has non-default visibility, we ignore the new |
1290 | definition from a dynamic object. */ | |
1291 | if (newdyn | |
9c7a29a3 | 1292 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1293 | && !bfd_is_und_section (sec)) |
1294 | { | |
1295 | *skip = TRUE; | |
1296 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1297 | h->ref_dynamic = 1; |
90c984fc | 1298 | hi->ref_dynamic = 1; |
45d6a902 AM |
1299 | /* A protected symbol has external availability. Make sure it is |
1300 | recorded as dynamic. | |
1301 | ||
1302 | FIXME: Should we check type and size for protected symbol? */ | |
1303 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1304 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1305 | else |
1306 | return TRUE; | |
1307 | } | |
1308 | else if (!newdyn | |
9c7a29a3 | 1309 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1310 | && h->def_dynamic) |
45d6a902 AM |
1311 | { |
1312 | /* If the new symbol with non-default visibility comes from a | |
1313 | relocatable file and the old definition comes from a dynamic | |
1314 | object, we remove the old definition. */ | |
6c9b78e6 | 1315 | if (hi->root.type == bfd_link_hash_indirect) |
d2dee3b2 L |
1316 | { |
1317 | /* Handle the case where the old dynamic definition is | |
1318 | default versioned. We need to copy the symbol info from | |
1319 | the symbol with default version to the normal one if it | |
1320 | was referenced before. */ | |
1321 | if (h->ref_regular) | |
1322 | { | |
6c9b78e6 | 1323 | hi->root.type = h->root.type; |
d2dee3b2 | 1324 | h->root.type = bfd_link_hash_indirect; |
6c9b78e6 | 1325 | (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); |
aed81c4e | 1326 | |
6c9b78e6 | 1327 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; |
aed81c4e | 1328 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
d2dee3b2 | 1329 | { |
aed81c4e MR |
1330 | /* If the new symbol is hidden or internal, completely undo |
1331 | any dynamic link state. */ | |
1332 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1333 | h->forced_local = 0; | |
1334 | h->ref_dynamic = 0; | |
d2dee3b2 L |
1335 | } |
1336 | else | |
aed81c4e MR |
1337 | h->ref_dynamic = 1; |
1338 | ||
1339 | h->def_dynamic = 0; | |
aed81c4e MR |
1340 | /* FIXME: Should we check type and size for protected symbol? */ |
1341 | h->size = 0; | |
1342 | h->type = 0; | |
1343 | ||
6c9b78e6 | 1344 | h = hi; |
d2dee3b2 L |
1345 | } |
1346 | else | |
6c9b78e6 | 1347 | h = hi; |
d2dee3b2 | 1348 | } |
1de1a317 | 1349 | |
f5eda473 AM |
1350 | /* If the old symbol was undefined before, then it will still be |
1351 | on the undefs list. If the new symbol is undefined or | |
1352 | common, we can't make it bfd_link_hash_new here, because new | |
1353 | undefined or common symbols will be added to the undefs list | |
1354 | by _bfd_generic_link_add_one_symbol. Symbols may not be | |
1355 | added twice to the undefs list. Also, if the new symbol is | |
1356 | undefweak then we don't want to lose the strong undef. */ | |
1357 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1de1a317 | 1358 | { |
1de1a317 | 1359 | h->root.type = bfd_link_hash_undefined; |
1de1a317 L |
1360 | h->root.u.undef.abfd = abfd; |
1361 | } | |
1362 | else | |
1363 | { | |
1364 | h->root.type = bfd_link_hash_new; | |
1365 | h->root.u.undef.abfd = NULL; | |
1366 | } | |
1367 | ||
f5eda473 | 1368 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
252b5132 | 1369 | { |
f5eda473 AM |
1370 | /* If the new symbol is hidden or internal, completely undo |
1371 | any dynamic link state. */ | |
1372 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1373 | h->forced_local = 0; | |
1374 | h->ref_dynamic = 0; | |
45d6a902 | 1375 | } |
f5eda473 AM |
1376 | else |
1377 | h->ref_dynamic = 1; | |
1378 | h->def_dynamic = 0; | |
45d6a902 AM |
1379 | /* FIXME: Should we check type and size for protected symbol? */ |
1380 | h->size = 0; | |
1381 | h->type = 0; | |
1382 | return TRUE; | |
1383 | } | |
14a793b2 | 1384 | |
15b43f48 AM |
1385 | /* If a new weak symbol definition comes from a regular file and the |
1386 | old symbol comes from a dynamic library, we treat the new one as | |
1387 | strong. Similarly, an old weak symbol definition from a regular | |
1388 | file is treated as strong when the new symbol comes from a dynamic | |
1389 | library. Further, an old weak symbol from a dynamic library is | |
1390 | treated as strong if the new symbol is from a dynamic library. | |
1391 | This reflects the way glibc's ld.so works. | |
1392 | ||
1393 | Do this before setting *type_change_ok or *size_change_ok so that | |
1394 | we warn properly when dynamic library symbols are overridden. */ | |
1395 | ||
1396 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1397 | newweak = FALSE; |
15b43f48 | 1398 | if (olddef && newdyn) |
0f8a2703 AM |
1399 | oldweak = FALSE; |
1400 | ||
d334575b | 1401 | /* Allow changes between different types of function symbol. */ |
0a36a439 | 1402 | if (newfunc && oldfunc) |
fcb93ecf PB |
1403 | *type_change_ok = TRUE; |
1404 | ||
79349b09 AM |
1405 | /* It's OK to change the type if either the existing symbol or the |
1406 | new symbol is weak. A type change is also OK if the old symbol | |
1407 | is undefined and the new symbol is defined. */ | |
252b5132 | 1408 | |
79349b09 AM |
1409 | if (oldweak |
1410 | || newweak | |
1411 | || (newdef | |
1412 | && h->root.type == bfd_link_hash_undefined)) | |
1413 | *type_change_ok = TRUE; | |
1414 | ||
1415 | /* It's OK to change the size if either the existing symbol or the | |
1416 | new symbol is weak, or if the old symbol is undefined. */ | |
1417 | ||
1418 | if (*type_change_ok | |
1419 | || h->root.type == bfd_link_hash_undefined) | |
1420 | *size_change_ok = TRUE; | |
45d6a902 | 1421 | |
45d6a902 AM |
1422 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1423 | symbol, respectively, appears to be a common symbol in a dynamic | |
1424 | object. If a symbol appears in an uninitialized section, and is | |
1425 | not weak, and is not a function, then it may be a common symbol | |
1426 | which was resolved when the dynamic object was created. We want | |
1427 | to treat such symbols specially, because they raise special | |
1428 | considerations when setting the symbol size: if the symbol | |
1429 | appears as a common symbol in a regular object, and the size in | |
1430 | the regular object is larger, we must make sure that we use the | |
1431 | larger size. This problematic case can always be avoided in C, | |
1432 | but it must be handled correctly when using Fortran shared | |
1433 | libraries. | |
1434 | ||
1435 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1436 | likewise for OLDDYNCOMMON and OLDDEF. | |
1437 | ||
1438 | Note that this test is just a heuristic, and that it is quite | |
1439 | possible to have an uninitialized symbol in a shared object which | |
1440 | is really a definition, rather than a common symbol. This could | |
1441 | lead to some minor confusion when the symbol really is a common | |
1442 | symbol in some regular object. However, I think it will be | |
1443 | harmless. */ | |
1444 | ||
1445 | if (newdyn | |
1446 | && newdef | |
79349b09 | 1447 | && !newweak |
45d6a902 AM |
1448 | && (sec->flags & SEC_ALLOC) != 0 |
1449 | && (sec->flags & SEC_LOAD) == 0 | |
1450 | && sym->st_size > 0 | |
0a36a439 | 1451 | && !newfunc) |
45d6a902 AM |
1452 | newdyncommon = TRUE; |
1453 | else | |
1454 | newdyncommon = FALSE; | |
1455 | ||
1456 | if (olddyn | |
1457 | && olddef | |
1458 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1459 | && h->def_dynamic |
45d6a902 AM |
1460 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1461 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1462 | && h->size > 0 | |
0a36a439 | 1463 | && !oldfunc) |
45d6a902 AM |
1464 | olddyncommon = TRUE; |
1465 | else | |
1466 | olddyncommon = FALSE; | |
1467 | ||
a4d8e49b L |
1468 | /* We now know everything about the old and new symbols. We ask the |
1469 | backend to check if we can merge them. */ | |
5d13b3b3 AM |
1470 | if (bed->merge_symbol != NULL) |
1471 | { | |
1472 | if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) | |
1473 | return FALSE; | |
1474 | sec = *psec; | |
1475 | } | |
a4d8e49b | 1476 | |
45d6a902 AM |
1477 | /* If both the old and the new symbols look like common symbols in a |
1478 | dynamic object, set the size of the symbol to the larger of the | |
1479 | two. */ | |
1480 | ||
1481 | if (olddyncommon | |
1482 | && newdyncommon | |
1483 | && sym->st_size != h->size) | |
1484 | { | |
1485 | /* Since we think we have two common symbols, issue a multiple | |
1486 | common warning if desired. Note that we only warn if the | |
1487 | size is different. If the size is the same, we simply let | |
1488 | the old symbol override the new one as normally happens with | |
1489 | symbols defined in dynamic objects. */ | |
1490 | ||
1a72702b AM |
1491 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1492 | bfd_link_hash_common, sym->st_size); | |
45d6a902 AM |
1493 | if (sym->st_size > h->size) |
1494 | h->size = sym->st_size; | |
252b5132 | 1495 | |
45d6a902 | 1496 | *size_change_ok = TRUE; |
252b5132 RH |
1497 | } |
1498 | ||
45d6a902 AM |
1499 | /* If we are looking at a dynamic object, and we have found a |
1500 | definition, we need to see if the symbol was already defined by | |
1501 | some other object. If so, we want to use the existing | |
1502 | definition, and we do not want to report a multiple symbol | |
1503 | definition error; we do this by clobbering *PSEC to be | |
1504 | bfd_und_section_ptr. | |
1505 | ||
1506 | We treat a common symbol as a definition if the symbol in the | |
1507 | shared library is a function, since common symbols always | |
1508 | represent variables; this can cause confusion in principle, but | |
1509 | any such confusion would seem to indicate an erroneous program or | |
1510 | shared library. We also permit a common symbol in a regular | |
202ac193 L |
1511 | object to override a weak symbol in a shared object. A common |
1512 | symbol in executable also overrides a symbol in a shared object. */ | |
45d6a902 AM |
1513 | |
1514 | if (newdyn | |
1515 | && newdef | |
77cfaee6 | 1516 | && (olddef |
45d6a902 | 1517 | || (h->root.type == bfd_link_hash_common |
202ac193 L |
1518 | && (newweak |
1519 | || newfunc | |
1520 | || (!olddyn && bfd_link_executable (info)))))) | |
45d6a902 AM |
1521 | { |
1522 | *override = TRUE; | |
1523 | newdef = FALSE; | |
1524 | newdyncommon = FALSE; | |
252b5132 | 1525 | |
45d6a902 AM |
1526 | *psec = sec = bfd_und_section_ptr; |
1527 | *size_change_ok = TRUE; | |
252b5132 | 1528 | |
45d6a902 AM |
1529 | /* If we get here when the old symbol is a common symbol, then |
1530 | we are explicitly letting it override a weak symbol or | |
1531 | function in a dynamic object, and we don't want to warn about | |
1532 | a type change. If the old symbol is a defined symbol, a type | |
1533 | change warning may still be appropriate. */ | |
252b5132 | 1534 | |
45d6a902 AM |
1535 | if (h->root.type == bfd_link_hash_common) |
1536 | *type_change_ok = TRUE; | |
1537 | } | |
1538 | ||
1539 | /* Handle the special case of an old common symbol merging with a | |
1540 | new symbol which looks like a common symbol in a shared object. | |
1541 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1542 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1543 | right thing. */ | |
45d6a902 AM |
1544 | |
1545 | if (newdyncommon | |
1546 | && h->root.type == bfd_link_hash_common) | |
1547 | { | |
1548 | *override = TRUE; | |
1549 | newdef = FALSE; | |
1550 | newdyncommon = FALSE; | |
1551 | *pvalue = sym->st_size; | |
a4d8e49b | 1552 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1553 | *size_change_ok = TRUE; |
1554 | } | |
1555 | ||
c5e2cead | 1556 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1557 | if (newdef && olddef && newweak) |
54ac0771 | 1558 | { |
35ed3f94 | 1559 | /* Don't skip new non-IR weak syms. */ |
3a5dbfb2 AM |
1560 | if (!(oldbfd != NULL |
1561 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
35ed3f94 | 1562 | && (abfd->flags & BFD_PLUGIN) == 0)) |
57fa7b8c AM |
1563 | { |
1564 | newdef = FALSE; | |
1565 | *skip = TRUE; | |
1566 | } | |
54ac0771 L |
1567 | |
1568 | /* Merge st_other. If the symbol already has a dynamic index, | |
1569 | but visibility says it should not be visible, turn it into a | |
1570 | local symbol. */ | |
b8417128 | 1571 | elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn); |
54ac0771 L |
1572 | if (h->dynindx != -1) |
1573 | switch (ELF_ST_VISIBILITY (h->other)) | |
1574 | { | |
1575 | case STV_INTERNAL: | |
1576 | case STV_HIDDEN: | |
1577 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1578 | break; | |
1579 | } | |
1580 | } | |
c5e2cead | 1581 | |
45d6a902 AM |
1582 | /* If the old symbol is from a dynamic object, and the new symbol is |
1583 | a definition which is not from a dynamic object, then the new | |
1584 | symbol overrides the old symbol. Symbols from regular files | |
1585 | always take precedence over symbols from dynamic objects, even if | |
1586 | they are defined after the dynamic object in the link. | |
1587 | ||
1588 | As above, we again permit a common symbol in a regular object to | |
1589 | override a definition in a shared object if the shared object | |
0f8a2703 | 1590 | symbol is a function or is weak. */ |
45d6a902 AM |
1591 | |
1592 | flip = NULL; | |
77cfaee6 | 1593 | if (!newdyn |
45d6a902 AM |
1594 | && (newdef |
1595 | || (bfd_is_com_section (sec) | |
0a36a439 | 1596 | && (oldweak || oldfunc))) |
45d6a902 AM |
1597 | && olddyn |
1598 | && olddef | |
f5385ebf | 1599 | && h->def_dynamic) |
45d6a902 AM |
1600 | { |
1601 | /* Change the hash table entry to undefined, and let | |
1602 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1603 | new definition. */ | |
1604 | ||
1605 | h->root.type = bfd_link_hash_undefined; | |
1606 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1607 | *size_change_ok = TRUE; | |
1608 | ||
1609 | olddef = FALSE; | |
1610 | olddyncommon = FALSE; | |
1611 | ||
1612 | /* We again permit a type change when a common symbol may be | |
1613 | overriding a function. */ | |
1614 | ||
1615 | if (bfd_is_com_section (sec)) | |
0a36a439 L |
1616 | { |
1617 | if (oldfunc) | |
1618 | { | |
1619 | /* If a common symbol overrides a function, make sure | |
1620 | that it isn't defined dynamically nor has type | |
1621 | function. */ | |
1622 | h->def_dynamic = 0; | |
1623 | h->type = STT_NOTYPE; | |
1624 | } | |
1625 | *type_change_ok = TRUE; | |
1626 | } | |
45d6a902 | 1627 | |
6c9b78e6 AM |
1628 | if (hi->root.type == bfd_link_hash_indirect) |
1629 | flip = hi; | |
45d6a902 AM |
1630 | else |
1631 | /* This union may have been set to be non-NULL when this symbol | |
1632 | was seen in a dynamic object. We must force the union to be | |
1633 | NULL, so that it is correct for a regular symbol. */ | |
1634 | h->verinfo.vertree = NULL; | |
1635 | } | |
1636 | ||
1637 | /* Handle the special case of a new common symbol merging with an | |
1638 | old symbol that looks like it might be a common symbol defined in | |
1639 | a shared object. Note that we have already handled the case in | |
1640 | which a new common symbol should simply override the definition | |
1641 | in the shared library. */ | |
1642 | ||
1643 | if (! newdyn | |
1644 | && bfd_is_com_section (sec) | |
1645 | && olddyncommon) | |
1646 | { | |
1647 | /* It would be best if we could set the hash table entry to a | |
1648 | common symbol, but we don't know what to use for the section | |
1649 | or the alignment. */ | |
1a72702b AM |
1650 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1651 | bfd_link_hash_common, sym->st_size); | |
45d6a902 | 1652 | |
4cc11e76 | 1653 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1654 | larger, pretend that the new symbol has its size. */ |
1655 | ||
1656 | if (h->size > *pvalue) | |
1657 | *pvalue = h->size; | |
1658 | ||
af44c138 L |
1659 | /* We need to remember the alignment required by the symbol |
1660 | in the dynamic object. */ | |
1661 | BFD_ASSERT (pold_alignment); | |
1662 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1663 | |
1664 | olddef = FALSE; | |
1665 | olddyncommon = FALSE; | |
1666 | ||
1667 | h->root.type = bfd_link_hash_undefined; | |
1668 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1669 | ||
1670 | *size_change_ok = TRUE; | |
1671 | *type_change_ok = TRUE; | |
1672 | ||
6c9b78e6 AM |
1673 | if (hi->root.type == bfd_link_hash_indirect) |
1674 | flip = hi; | |
45d6a902 AM |
1675 | else |
1676 | h->verinfo.vertree = NULL; | |
1677 | } | |
1678 | ||
1679 | if (flip != NULL) | |
1680 | { | |
1681 | /* Handle the case where we had a versioned symbol in a dynamic | |
1682 | library and now find a definition in a normal object. In this | |
1683 | case, we make the versioned symbol point to the normal one. */ | |
45d6a902 | 1684 | flip->root.type = h->root.type; |
00cbee0a | 1685 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1686 | h->root.type = bfd_link_hash_indirect; |
1687 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1688 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1689 | if (h->def_dynamic) |
45d6a902 | 1690 | { |
f5385ebf AM |
1691 | h->def_dynamic = 0; |
1692 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1693 | } |
1694 | } | |
1695 | ||
45d6a902 AM |
1696 | return TRUE; |
1697 | } | |
1698 | ||
1699 | /* This function is called to create an indirect symbol from the | |
1700 | default for the symbol with the default version if needed. The | |
4f3fedcf | 1701 | symbol is described by H, NAME, SYM, SEC, and VALUE. We |
0f8a2703 | 1702 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 | 1703 | |
28caa186 | 1704 | static bfd_boolean |
268b6b39 AM |
1705 | _bfd_elf_add_default_symbol (bfd *abfd, |
1706 | struct bfd_link_info *info, | |
1707 | struct elf_link_hash_entry *h, | |
1708 | const char *name, | |
1709 | Elf_Internal_Sym *sym, | |
4f3fedcf AM |
1710 | asection *sec, |
1711 | bfd_vma value, | |
1712 | bfd **poldbfd, | |
e3c9d234 | 1713 | bfd_boolean *dynsym) |
45d6a902 AM |
1714 | { |
1715 | bfd_boolean type_change_ok; | |
1716 | bfd_boolean size_change_ok; | |
1717 | bfd_boolean skip; | |
1718 | char *shortname; | |
1719 | struct elf_link_hash_entry *hi; | |
1720 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1721 | const struct elf_backend_data *bed; |
45d6a902 AM |
1722 | bfd_boolean collect; |
1723 | bfd_boolean dynamic; | |
e3c9d234 | 1724 | bfd_boolean override; |
45d6a902 AM |
1725 | char *p; |
1726 | size_t len, shortlen; | |
ffd65175 | 1727 | asection *tmp_sec; |
6e33951e | 1728 | bfd_boolean matched; |
45d6a902 | 1729 | |
422f1182 L |
1730 | if (h->versioned == unversioned || h->versioned == versioned_hidden) |
1731 | return TRUE; | |
1732 | ||
45d6a902 AM |
1733 | /* If this symbol has a version, and it is the default version, we |
1734 | create an indirect symbol from the default name to the fully | |
1735 | decorated name. This will cause external references which do not | |
1736 | specify a version to be bound to this version of the symbol. */ | |
1737 | p = strchr (name, ELF_VER_CHR); | |
422f1182 L |
1738 | if (h->versioned == unknown) |
1739 | { | |
1740 | if (p == NULL) | |
1741 | { | |
1742 | h->versioned = unversioned; | |
1743 | return TRUE; | |
1744 | } | |
1745 | else | |
1746 | { | |
1747 | if (p[1] != ELF_VER_CHR) | |
1748 | { | |
1749 | h->versioned = versioned_hidden; | |
1750 | return TRUE; | |
1751 | } | |
1752 | else | |
1753 | h->versioned = versioned; | |
1754 | } | |
1755 | } | |
4373f8af L |
1756 | else |
1757 | { | |
1758 | /* PR ld/19073: We may see an unversioned definition after the | |
1759 | default version. */ | |
1760 | if (p == NULL) | |
1761 | return TRUE; | |
1762 | } | |
45d6a902 | 1763 | |
45d6a902 AM |
1764 | bed = get_elf_backend_data (abfd); |
1765 | collect = bed->collect; | |
1766 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1767 | ||
1768 | shortlen = p - name; | |
a50b1753 | 1769 | shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); |
45d6a902 AM |
1770 | if (shortname == NULL) |
1771 | return FALSE; | |
1772 | memcpy (shortname, name, shortlen); | |
1773 | shortname[shortlen] = '\0'; | |
1774 | ||
1775 | /* We are going to create a new symbol. Merge it with any existing | |
1776 | symbol with this name. For the purposes of the merge, act as | |
1777 | though we were defining the symbol we just defined, although we | |
1778 | actually going to define an indirect symbol. */ | |
1779 | type_change_ok = FALSE; | |
1780 | size_change_ok = FALSE; | |
6e33951e | 1781 | matched = TRUE; |
ffd65175 AM |
1782 | tmp_sec = sec; |
1783 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
4f3fedcf | 1784 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 1785 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
1786 | return FALSE; |
1787 | ||
1788 | if (skip) | |
1789 | goto nondefault; | |
1790 | ||
5b677558 AM |
1791 | if (hi->def_regular) |
1792 | { | |
1793 | /* If the undecorated symbol will have a version added by a | |
1794 | script different to H, then don't indirect to/from the | |
1795 | undecorated symbol. This isn't ideal because we may not yet | |
1796 | have seen symbol versions, if given by a script on the | |
1797 | command line rather than via --version-script. */ | |
1798 | if (hi->verinfo.vertree == NULL && info->version_info != NULL) | |
1799 | { | |
1800 | bfd_boolean hide; | |
1801 | ||
1802 | hi->verinfo.vertree | |
1803 | = bfd_find_version_for_sym (info->version_info, | |
1804 | hi->root.root.string, &hide); | |
1805 | if (hi->verinfo.vertree != NULL && hide) | |
1806 | { | |
1807 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
1808 | goto nondefault; | |
1809 | } | |
1810 | } | |
1811 | if (hi->verinfo.vertree != NULL | |
1812 | && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0) | |
1813 | goto nondefault; | |
1814 | } | |
1815 | ||
45d6a902 AM |
1816 | if (! override) |
1817 | { | |
c6e8a9a8 | 1818 | /* Add the default symbol if not performing a relocatable link. */ |
0e1862bb | 1819 | if (! bfd_link_relocatable (info)) |
c6e8a9a8 L |
1820 | { |
1821 | bh = &hi->root; | |
1822 | if (! (_bfd_generic_link_add_one_symbol | |
1823 | (info, abfd, shortname, BSF_INDIRECT, | |
1824 | bfd_ind_section_ptr, | |
1825 | 0, name, FALSE, collect, &bh))) | |
1826 | return FALSE; | |
1827 | hi = (struct elf_link_hash_entry *) bh; | |
1828 | } | |
45d6a902 AM |
1829 | } |
1830 | else | |
1831 | { | |
1832 | /* In this case the symbol named SHORTNAME is overriding the | |
1833 | indirect symbol we want to add. We were planning on making | |
1834 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1835 | is the name without a version. NAME is the fully versioned | |
1836 | name, and it is the default version. | |
1837 | ||
1838 | Overriding means that we already saw a definition for the | |
1839 | symbol SHORTNAME in a regular object, and it is overriding | |
1840 | the symbol defined in the dynamic object. | |
1841 | ||
1842 | When this happens, we actually want to change NAME, the | |
1843 | symbol we just added, to refer to SHORTNAME. This will cause | |
1844 | references to NAME in the shared object to become references | |
1845 | to SHORTNAME in the regular object. This is what we expect | |
1846 | when we override a function in a shared object: that the | |
1847 | references in the shared object will be mapped to the | |
1848 | definition in the regular object. */ | |
1849 | ||
1850 | while (hi->root.type == bfd_link_hash_indirect | |
1851 | || hi->root.type == bfd_link_hash_warning) | |
1852 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1853 | ||
1854 | h->root.type = bfd_link_hash_indirect; | |
1855 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1856 | if (h->def_dynamic) |
45d6a902 | 1857 | { |
f5385ebf AM |
1858 | h->def_dynamic = 0; |
1859 | hi->ref_dynamic = 1; | |
1860 | if (hi->ref_regular | |
1861 | || hi->def_regular) | |
45d6a902 | 1862 | { |
c152c796 | 1863 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1864 | return FALSE; |
1865 | } | |
1866 | } | |
1867 | ||
1868 | /* Now set HI to H, so that the following code will set the | |
1869 | other fields correctly. */ | |
1870 | hi = h; | |
1871 | } | |
1872 | ||
fab4a87f L |
1873 | /* Check if HI is a warning symbol. */ |
1874 | if (hi->root.type == bfd_link_hash_warning) | |
1875 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1876 | ||
45d6a902 AM |
1877 | /* If there is a duplicate definition somewhere, then HI may not |
1878 | point to an indirect symbol. We will have reported an error to | |
1879 | the user in that case. */ | |
1880 | ||
1881 | if (hi->root.type == bfd_link_hash_indirect) | |
1882 | { | |
1883 | struct elf_link_hash_entry *ht; | |
1884 | ||
45d6a902 | 1885 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 1886 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 | 1887 | |
68c88cd4 AM |
1888 | /* A reference to the SHORTNAME symbol from a dynamic library |
1889 | will be satisfied by the versioned symbol at runtime. In | |
1890 | effect, we have a reference to the versioned symbol. */ | |
1891 | ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; | |
1892 | hi->dynamic_def |= ht->dynamic_def; | |
1893 | ||
45d6a902 AM |
1894 | /* See if the new flags lead us to realize that the symbol must |
1895 | be dynamic. */ | |
1896 | if (! *dynsym) | |
1897 | { | |
1898 | if (! dynamic) | |
1899 | { | |
0e1862bb | 1900 | if (! bfd_link_executable (info) |
90c984fc | 1901 | || hi->def_dynamic |
f5385ebf | 1902 | || hi->ref_dynamic) |
45d6a902 AM |
1903 | *dynsym = TRUE; |
1904 | } | |
1905 | else | |
1906 | { | |
f5385ebf | 1907 | if (hi->ref_regular) |
45d6a902 AM |
1908 | *dynsym = TRUE; |
1909 | } | |
1910 | } | |
1911 | } | |
1912 | ||
1913 | /* We also need to define an indirection from the nondefault version | |
1914 | of the symbol. */ | |
1915 | ||
1916 | nondefault: | |
1917 | len = strlen (name); | |
a50b1753 | 1918 | shortname = (char *) bfd_hash_allocate (&info->hash->table, len); |
45d6a902 AM |
1919 | if (shortname == NULL) |
1920 | return FALSE; | |
1921 | memcpy (shortname, name, shortlen); | |
1922 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1923 | ||
1924 | /* Once again, merge with any existing symbol. */ | |
1925 | type_change_ok = FALSE; | |
1926 | size_change_ok = FALSE; | |
ffd65175 AM |
1927 | tmp_sec = sec; |
1928 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
115c6d5c | 1929 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 1930 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
1931 | return FALSE; |
1932 | ||
1933 | if (skip) | |
1934 | return TRUE; | |
1935 | ||
1936 | if (override) | |
1937 | { | |
1938 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1939 | the type of override we do in the case above unless it is | |
4cc11e76 | 1940 | overridden by a versioned definition. */ |
45d6a902 AM |
1941 | if (hi->root.type != bfd_link_hash_defined |
1942 | && hi->root.type != bfd_link_hash_defweak) | |
4eca0228 | 1943 | _bfd_error_handler |
695344c0 | 1944 | /* xgettext:c-format */ |
d003868e AM |
1945 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1946 | abfd, shortname); | |
45d6a902 AM |
1947 | } |
1948 | else | |
1949 | { | |
1950 | bh = &hi->root; | |
1951 | if (! (_bfd_generic_link_add_one_symbol | |
1952 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1953 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1954 | return FALSE; |
1955 | hi = (struct elf_link_hash_entry *) bh; | |
1956 | ||
1957 | /* If there is a duplicate definition somewhere, then HI may not | |
1958 | point to an indirect symbol. We will have reported an error | |
1959 | to the user in that case. */ | |
1960 | ||
1961 | if (hi->root.type == bfd_link_hash_indirect) | |
1962 | { | |
fcfa13d2 | 1963 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
68c88cd4 AM |
1964 | h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; |
1965 | hi->dynamic_def |= h->dynamic_def; | |
45d6a902 AM |
1966 | |
1967 | /* See if the new flags lead us to realize that the symbol | |
1968 | must be dynamic. */ | |
1969 | if (! *dynsym) | |
1970 | { | |
1971 | if (! dynamic) | |
1972 | { | |
0e1862bb | 1973 | if (! bfd_link_executable (info) |
f5385ebf | 1974 | || hi->ref_dynamic) |
45d6a902 AM |
1975 | *dynsym = TRUE; |
1976 | } | |
1977 | else | |
1978 | { | |
f5385ebf | 1979 | if (hi->ref_regular) |
45d6a902 AM |
1980 | *dynsym = TRUE; |
1981 | } | |
1982 | } | |
1983 | } | |
1984 | } | |
1985 | ||
1986 | return TRUE; | |
1987 | } | |
1988 | \f | |
1989 | /* This routine is used to export all defined symbols into the dynamic | |
1990 | symbol table. It is called via elf_link_hash_traverse. */ | |
1991 | ||
28caa186 | 1992 | static bfd_boolean |
268b6b39 | 1993 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1994 | { |
a50b1753 | 1995 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 AM |
1996 | |
1997 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
1998 | if (h->root.type == bfd_link_hash_indirect) | |
1999 | return TRUE; | |
2000 | ||
7686d77d AM |
2001 | /* Ignore this if we won't export it. */ |
2002 | if (!eif->info->export_dynamic && !h->dynamic) | |
2003 | return TRUE; | |
45d6a902 AM |
2004 | |
2005 | if (h->dynindx == -1 | |
fd91d419 L |
2006 | && (h->def_regular || h->ref_regular) |
2007 | && ! bfd_hide_sym_by_version (eif->info->version_info, | |
2008 | h->root.root.string)) | |
45d6a902 | 2009 | { |
fd91d419 | 2010 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 | 2011 | { |
fd91d419 L |
2012 | eif->failed = TRUE; |
2013 | return FALSE; | |
45d6a902 AM |
2014 | } |
2015 | } | |
2016 | ||
2017 | return TRUE; | |
2018 | } | |
2019 | \f | |
2020 | /* Look through the symbols which are defined in other shared | |
2021 | libraries and referenced here. Update the list of version | |
2022 | dependencies. This will be put into the .gnu.version_r section. | |
2023 | This function is called via elf_link_hash_traverse. */ | |
2024 | ||
28caa186 | 2025 | static bfd_boolean |
268b6b39 AM |
2026 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
2027 | void *data) | |
45d6a902 | 2028 | { |
a50b1753 | 2029 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; |
45d6a902 AM |
2030 | Elf_Internal_Verneed *t; |
2031 | Elf_Internal_Vernaux *a; | |
2032 | bfd_size_type amt; | |
2033 | ||
45d6a902 AM |
2034 | /* We only care about symbols defined in shared objects with version |
2035 | information. */ | |
f5385ebf AM |
2036 | if (!h->def_dynamic |
2037 | || h->def_regular | |
45d6a902 | 2038 | || h->dynindx == -1 |
7b20f099 AM |
2039 | || h->verinfo.verdef == NULL |
2040 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
2041 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
45d6a902 AM |
2042 | return TRUE; |
2043 | ||
2044 | /* See if we already know about this version. */ | |
28caa186 AM |
2045 | for (t = elf_tdata (rinfo->info->output_bfd)->verref; |
2046 | t != NULL; | |
2047 | t = t->vn_nextref) | |
45d6a902 AM |
2048 | { |
2049 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
2050 | continue; | |
2051 | ||
2052 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2053 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
2054 | return TRUE; | |
2055 | ||
2056 | break; | |
2057 | } | |
2058 | ||
2059 | /* This is a new version. Add it to tree we are building. */ | |
2060 | ||
2061 | if (t == NULL) | |
2062 | { | |
2063 | amt = sizeof *t; | |
a50b1753 | 2064 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); |
45d6a902 AM |
2065 | if (t == NULL) |
2066 | { | |
2067 | rinfo->failed = TRUE; | |
2068 | return FALSE; | |
2069 | } | |
2070 | ||
2071 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
28caa186 AM |
2072 | t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; |
2073 | elf_tdata (rinfo->info->output_bfd)->verref = t; | |
45d6a902 AM |
2074 | } |
2075 | ||
2076 | amt = sizeof *a; | |
a50b1753 | 2077 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); |
14b1c01e AM |
2078 | if (a == NULL) |
2079 | { | |
2080 | rinfo->failed = TRUE; | |
2081 | return FALSE; | |
2082 | } | |
45d6a902 AM |
2083 | |
2084 | /* Note that we are copying a string pointer here, and testing it | |
2085 | above. If bfd_elf_string_from_elf_section is ever changed to | |
2086 | discard the string data when low in memory, this will have to be | |
2087 | fixed. */ | |
2088 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
2089 | ||
2090 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
2091 | a->vna_nextptr = t->vn_auxptr; | |
2092 | ||
2093 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
2094 | ++rinfo->vers; | |
2095 | ||
2096 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
2097 | ||
2098 | t->vn_auxptr = a; | |
2099 | ||
2100 | return TRUE; | |
2101 | } | |
2102 | ||
2103 | /* Figure out appropriate versions for all the symbols. We may not | |
2104 | have the version number script until we have read all of the input | |
2105 | files, so until that point we don't know which symbols should be | |
2106 | local. This function is called via elf_link_hash_traverse. */ | |
2107 | ||
28caa186 | 2108 | static bfd_boolean |
268b6b39 | 2109 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2110 | { |
28caa186 | 2111 | struct elf_info_failed *sinfo; |
45d6a902 | 2112 | struct bfd_link_info *info; |
9c5bfbb7 | 2113 | const struct elf_backend_data *bed; |
45d6a902 AM |
2114 | struct elf_info_failed eif; |
2115 | char *p; | |
45d6a902 | 2116 | |
a50b1753 | 2117 | sinfo = (struct elf_info_failed *) data; |
45d6a902 AM |
2118 | info = sinfo->info; |
2119 | ||
45d6a902 AM |
2120 | /* Fix the symbol flags. */ |
2121 | eif.failed = FALSE; | |
2122 | eif.info = info; | |
2123 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
2124 | { | |
2125 | if (eif.failed) | |
2126 | sinfo->failed = TRUE; | |
2127 | return FALSE; | |
2128 | } | |
2129 | ||
2130 | /* We only need version numbers for symbols defined in regular | |
2131 | objects. */ | |
f5385ebf | 2132 | if (!h->def_regular) |
45d6a902 AM |
2133 | return TRUE; |
2134 | ||
28caa186 | 2135 | bed = get_elf_backend_data (info->output_bfd); |
45d6a902 AM |
2136 | p = strchr (h->root.root.string, ELF_VER_CHR); |
2137 | if (p != NULL && h->verinfo.vertree == NULL) | |
2138 | { | |
2139 | struct bfd_elf_version_tree *t; | |
45d6a902 | 2140 | |
45d6a902 AM |
2141 | ++p; |
2142 | if (*p == ELF_VER_CHR) | |
6e33951e | 2143 | ++p; |
45d6a902 AM |
2144 | |
2145 | /* If there is no version string, we can just return out. */ | |
2146 | if (*p == '\0') | |
6e33951e | 2147 | return TRUE; |
45d6a902 AM |
2148 | |
2149 | /* Look for the version. If we find it, it is no longer weak. */ | |
fd91d419 | 2150 | for (t = sinfo->info->version_info; t != NULL; t = t->next) |
45d6a902 AM |
2151 | { |
2152 | if (strcmp (t->name, p) == 0) | |
2153 | { | |
2154 | size_t len; | |
2155 | char *alc; | |
2156 | struct bfd_elf_version_expr *d; | |
2157 | ||
2158 | len = p - h->root.root.string; | |
a50b1753 | 2159 | alc = (char *) bfd_malloc (len); |
45d6a902 | 2160 | if (alc == NULL) |
14b1c01e AM |
2161 | { |
2162 | sinfo->failed = TRUE; | |
2163 | return FALSE; | |
2164 | } | |
45d6a902 AM |
2165 | memcpy (alc, h->root.root.string, len - 1); |
2166 | alc[len - 1] = '\0'; | |
2167 | if (alc[len - 2] == ELF_VER_CHR) | |
2168 | alc[len - 2] = '\0'; | |
2169 | ||
2170 | h->verinfo.vertree = t; | |
2171 | t->used = TRUE; | |
2172 | d = NULL; | |
2173 | ||
108ba305 JJ |
2174 | if (t->globals.list != NULL) |
2175 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
2176 | |
2177 | /* See if there is anything to force this symbol to | |
2178 | local scope. */ | |
108ba305 | 2179 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 2180 | { |
108ba305 JJ |
2181 | d = (*t->match) (&t->locals, NULL, alc); |
2182 | if (d != NULL | |
2183 | && h->dynindx != -1 | |
108ba305 JJ |
2184 | && ! info->export_dynamic) |
2185 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2186 | } |
2187 | ||
2188 | free (alc); | |
2189 | break; | |
2190 | } | |
2191 | } | |
2192 | ||
2193 | /* If we are building an application, we need to create a | |
2194 | version node for this version. */ | |
0e1862bb | 2195 | if (t == NULL && bfd_link_executable (info)) |
45d6a902 AM |
2196 | { |
2197 | struct bfd_elf_version_tree **pp; | |
2198 | int version_index; | |
2199 | ||
2200 | /* If we aren't going to export this symbol, we don't need | |
2201 | to worry about it. */ | |
2202 | if (h->dynindx == -1) | |
2203 | return TRUE; | |
2204 | ||
ef53be89 AM |
2205 | t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, |
2206 | sizeof *t); | |
45d6a902 AM |
2207 | if (t == NULL) |
2208 | { | |
2209 | sinfo->failed = TRUE; | |
2210 | return FALSE; | |
2211 | } | |
2212 | ||
45d6a902 | 2213 | t->name = p; |
45d6a902 AM |
2214 | t->name_indx = (unsigned int) -1; |
2215 | t->used = TRUE; | |
2216 | ||
2217 | version_index = 1; | |
2218 | /* Don't count anonymous version tag. */ | |
fd91d419 L |
2219 | if (sinfo->info->version_info != NULL |
2220 | && sinfo->info->version_info->vernum == 0) | |
45d6a902 | 2221 | version_index = 0; |
fd91d419 L |
2222 | for (pp = &sinfo->info->version_info; |
2223 | *pp != NULL; | |
2224 | pp = &(*pp)->next) | |
45d6a902 AM |
2225 | ++version_index; |
2226 | t->vernum = version_index; | |
2227 | ||
2228 | *pp = t; | |
2229 | ||
2230 | h->verinfo.vertree = t; | |
2231 | } | |
2232 | else if (t == NULL) | |
2233 | { | |
2234 | /* We could not find the version for a symbol when | |
2235 | generating a shared archive. Return an error. */ | |
4eca0228 | 2236 | _bfd_error_handler |
695344c0 | 2237 | /* xgettext:c-format */ |
c55fe096 | 2238 | (_("%B: version node not found for symbol %s"), |
28caa186 | 2239 | info->output_bfd, h->root.root.string); |
45d6a902 AM |
2240 | bfd_set_error (bfd_error_bad_value); |
2241 | sinfo->failed = TRUE; | |
2242 | return FALSE; | |
2243 | } | |
45d6a902 AM |
2244 | } |
2245 | ||
2246 | /* If we don't have a version for this symbol, see if we can find | |
2247 | something. */ | |
fd91d419 | 2248 | if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL) |
45d6a902 | 2249 | { |
1e8fa21e | 2250 | bfd_boolean hide; |
ae5a3597 | 2251 | |
fd91d419 L |
2252 | h->verinfo.vertree |
2253 | = bfd_find_version_for_sym (sinfo->info->version_info, | |
2254 | h->root.root.string, &hide); | |
1e8fa21e AM |
2255 | if (h->verinfo.vertree != NULL && hide) |
2256 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2257 | } |
2258 | ||
2259 | return TRUE; | |
2260 | } | |
2261 | \f | |
45d6a902 AM |
2262 | /* Read and swap the relocs from the section indicated by SHDR. This |
2263 | may be either a REL or a RELA section. The relocations are | |
2264 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2265 | which should have already been allocated to contain enough space. | |
2266 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2267 | relocations should be stored. | |
2268 | ||
2269 | Returns FALSE if something goes wrong. */ | |
2270 | ||
2271 | static bfd_boolean | |
268b6b39 | 2272 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2273 | asection *sec, |
268b6b39 AM |
2274 | Elf_Internal_Shdr *shdr, |
2275 | void *external_relocs, | |
2276 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2277 | { |
9c5bfbb7 | 2278 | const struct elf_backend_data *bed; |
268b6b39 | 2279 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2280 | const bfd_byte *erela; |
2281 | const bfd_byte *erelaend; | |
2282 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2283 | Elf_Internal_Shdr *symtab_hdr; |
2284 | size_t nsyms; | |
45d6a902 | 2285 | |
45d6a902 AM |
2286 | /* Position ourselves at the start of the section. */ |
2287 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2288 | return FALSE; | |
2289 | ||
2290 | /* Read the relocations. */ | |
2291 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2292 | return FALSE; | |
2293 | ||
243ef1e0 | 2294 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
ce98a316 | 2295 | nsyms = NUM_SHDR_ENTRIES (symtab_hdr); |
243ef1e0 | 2296 | |
45d6a902 AM |
2297 | bed = get_elf_backend_data (abfd); |
2298 | ||
2299 | /* Convert the external relocations to the internal format. */ | |
2300 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2301 | swap_in = bed->s->swap_reloc_in; | |
2302 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2303 | swap_in = bed->s->swap_reloca_in; | |
2304 | else | |
2305 | { | |
2306 | bfd_set_error (bfd_error_wrong_format); | |
2307 | return FALSE; | |
2308 | } | |
2309 | ||
a50b1753 | 2310 | erela = (const bfd_byte *) external_relocs; |
51992aec | 2311 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2312 | irela = internal_relocs; |
2313 | while (erela < erelaend) | |
2314 | { | |
243ef1e0 L |
2315 | bfd_vma r_symndx; |
2316 | ||
45d6a902 | 2317 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2318 | r_symndx = ELF32_R_SYM (irela->r_info); |
2319 | if (bed->s->arch_size == 64) | |
2320 | r_symndx >>= 24; | |
ce98a316 NC |
2321 | if (nsyms > 0) |
2322 | { | |
2323 | if ((size_t) r_symndx >= nsyms) | |
2324 | { | |
4eca0228 | 2325 | _bfd_error_handler |
695344c0 | 2326 | /* xgettext:c-format */ |
ce98a316 NC |
2327 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" |
2328 | " for offset 0x%lx in section `%A'"), | |
2329 | abfd, sec, | |
2330 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
2331 | bfd_set_error (bfd_error_bad_value); | |
2332 | return FALSE; | |
2333 | } | |
2334 | } | |
cf35638d | 2335 | else if (r_symndx != STN_UNDEF) |
243ef1e0 | 2336 | { |
4eca0228 | 2337 | _bfd_error_handler |
695344c0 | 2338 | /* xgettext:c-format */ |
ce98a316 NC |
2339 | (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'" |
2340 | " when the object file has no symbol table"), | |
d003868e AM |
2341 | abfd, sec, |
2342 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
2343 | bfd_set_error (bfd_error_bad_value); |
2344 | return FALSE; | |
2345 | } | |
45d6a902 AM |
2346 | irela += bed->s->int_rels_per_ext_rel; |
2347 | erela += shdr->sh_entsize; | |
2348 | } | |
2349 | ||
2350 | return TRUE; | |
2351 | } | |
2352 | ||
2353 | /* Read and swap the relocs for a section O. They may have been | |
2354 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2355 | not NULL, they are used as buffers to read into. They are known to | |
2356 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2357 | the return value is allocated using either malloc or bfd_alloc, | |
2358 | according to the KEEP_MEMORY argument. If O has two relocation | |
2359 | sections (both REL and RELA relocations), then the REL_HDR | |
2360 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
d4730f92 | 2361 | RELA_HDR relocations. */ |
45d6a902 AM |
2362 | |
2363 | Elf_Internal_Rela * | |
268b6b39 AM |
2364 | _bfd_elf_link_read_relocs (bfd *abfd, |
2365 | asection *o, | |
2366 | void *external_relocs, | |
2367 | Elf_Internal_Rela *internal_relocs, | |
2368 | bfd_boolean keep_memory) | |
45d6a902 | 2369 | { |
268b6b39 | 2370 | void *alloc1 = NULL; |
45d6a902 | 2371 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2372 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2373 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
2374 | Elf_Internal_Rela *internal_rela_relocs; | |
45d6a902 | 2375 | |
d4730f92 BS |
2376 | if (esdo->relocs != NULL) |
2377 | return esdo->relocs; | |
45d6a902 AM |
2378 | |
2379 | if (o->reloc_count == 0) | |
2380 | return NULL; | |
2381 | ||
45d6a902 AM |
2382 | if (internal_relocs == NULL) |
2383 | { | |
2384 | bfd_size_type size; | |
2385 | ||
2386 | size = o->reloc_count; | |
2387 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
2388 | if (keep_memory) | |
a50b1753 | 2389 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); |
45d6a902 | 2390 | else |
a50b1753 | 2391 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
45d6a902 AM |
2392 | if (internal_relocs == NULL) |
2393 | goto error_return; | |
2394 | } | |
2395 | ||
2396 | if (external_relocs == NULL) | |
2397 | { | |
d4730f92 BS |
2398 | bfd_size_type size = 0; |
2399 | ||
2400 | if (esdo->rel.hdr) | |
2401 | size += esdo->rel.hdr->sh_size; | |
2402 | if (esdo->rela.hdr) | |
2403 | size += esdo->rela.hdr->sh_size; | |
45d6a902 | 2404 | |
268b6b39 | 2405 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2406 | if (alloc1 == NULL) |
2407 | goto error_return; | |
2408 | external_relocs = alloc1; | |
2409 | } | |
2410 | ||
d4730f92 BS |
2411 | internal_rela_relocs = internal_relocs; |
2412 | if (esdo->rel.hdr) | |
2413 | { | |
2414 | if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, | |
2415 | external_relocs, | |
2416 | internal_relocs)) | |
2417 | goto error_return; | |
2418 | external_relocs = (((bfd_byte *) external_relocs) | |
2419 | + esdo->rel.hdr->sh_size); | |
2420 | internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) | |
2421 | * bed->s->int_rels_per_ext_rel); | |
2422 | } | |
2423 | ||
2424 | if (esdo->rela.hdr | |
2425 | && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, | |
2426 | external_relocs, | |
2427 | internal_rela_relocs))) | |
45d6a902 AM |
2428 | goto error_return; |
2429 | ||
2430 | /* Cache the results for next time, if we can. */ | |
2431 | if (keep_memory) | |
d4730f92 | 2432 | esdo->relocs = internal_relocs; |
45d6a902 AM |
2433 | |
2434 | if (alloc1 != NULL) | |
2435 | free (alloc1); | |
2436 | ||
2437 | /* Don't free alloc2, since if it was allocated we are passing it | |
2438 | back (under the name of internal_relocs). */ | |
2439 | ||
2440 | return internal_relocs; | |
2441 | ||
2442 | error_return: | |
2443 | if (alloc1 != NULL) | |
2444 | free (alloc1); | |
2445 | if (alloc2 != NULL) | |
4dd07732 AM |
2446 | { |
2447 | if (keep_memory) | |
2448 | bfd_release (abfd, alloc2); | |
2449 | else | |
2450 | free (alloc2); | |
2451 | } | |
45d6a902 AM |
2452 | return NULL; |
2453 | } | |
2454 | ||
2455 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2456 | section header for a section containing relocations for O. */ | |
2457 | ||
28caa186 | 2458 | static bfd_boolean |
9eaff861 AO |
2459 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2460 | struct bfd_elf_section_reloc_data *reldata) | |
45d6a902 | 2461 | { |
9eaff861 | 2462 | Elf_Internal_Shdr *rel_hdr = reldata->hdr; |
45d6a902 AM |
2463 | |
2464 | /* That allows us to calculate the size of the section. */ | |
9eaff861 | 2465 | rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; |
45d6a902 AM |
2466 | |
2467 | /* The contents field must last into write_object_contents, so we | |
2468 | allocate it with bfd_alloc rather than malloc. Also since we | |
2469 | cannot be sure that the contents will actually be filled in, | |
2470 | we zero the allocated space. */ | |
a50b1753 | 2471 | rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2472 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2473 | return FALSE; | |
2474 | ||
d4730f92 | 2475 | if (reldata->hashes == NULL && reldata->count) |
45d6a902 AM |
2476 | { |
2477 | struct elf_link_hash_entry **p; | |
2478 | ||
ca4be51c AM |
2479 | p = ((struct elf_link_hash_entry **) |
2480 | bfd_zmalloc (reldata->count * sizeof (*p))); | |
45d6a902 AM |
2481 | if (p == NULL) |
2482 | return FALSE; | |
2483 | ||
d4730f92 | 2484 | reldata->hashes = p; |
45d6a902 AM |
2485 | } |
2486 | ||
2487 | return TRUE; | |
2488 | } | |
2489 | ||
2490 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2491 | originated from the section given by INPUT_REL_HDR) to the | |
2492 | OUTPUT_BFD. */ | |
2493 | ||
2494 | bfd_boolean | |
268b6b39 AM |
2495 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2496 | asection *input_section, | |
2497 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2498 | Elf_Internal_Rela *internal_relocs, |
2499 | struct elf_link_hash_entry **rel_hash | |
2500 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2501 | { |
2502 | Elf_Internal_Rela *irela; | |
2503 | Elf_Internal_Rela *irelaend; | |
2504 | bfd_byte *erel; | |
d4730f92 | 2505 | struct bfd_elf_section_reloc_data *output_reldata; |
45d6a902 | 2506 | asection *output_section; |
9c5bfbb7 | 2507 | const struct elf_backend_data *bed; |
268b6b39 | 2508 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
d4730f92 | 2509 | struct bfd_elf_section_data *esdo; |
45d6a902 AM |
2510 | |
2511 | output_section = input_section->output_section; | |
45d6a902 | 2512 | |
d4730f92 BS |
2513 | bed = get_elf_backend_data (output_bfd); |
2514 | esdo = elf_section_data (output_section); | |
2515 | if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2516 | { |
d4730f92 BS |
2517 | output_reldata = &esdo->rel; |
2518 | swap_out = bed->s->swap_reloc_out; | |
45d6a902 | 2519 | } |
d4730f92 BS |
2520 | else if (esdo->rela.hdr |
2521 | && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2522 | { |
d4730f92 BS |
2523 | output_reldata = &esdo->rela; |
2524 | swap_out = bed->s->swap_reloca_out; | |
45d6a902 AM |
2525 | } |
2526 | else | |
2527 | { | |
4eca0228 | 2528 | _bfd_error_handler |
695344c0 | 2529 | /* xgettext:c-format */ |
d003868e AM |
2530 | (_("%B: relocation size mismatch in %B section %A"), |
2531 | output_bfd, input_section->owner, input_section); | |
297d8443 | 2532 | bfd_set_error (bfd_error_wrong_format); |
45d6a902 AM |
2533 | return FALSE; |
2534 | } | |
2535 | ||
d4730f92 BS |
2536 | erel = output_reldata->hdr->contents; |
2537 | erel += output_reldata->count * input_rel_hdr->sh_entsize; | |
45d6a902 AM |
2538 | irela = internal_relocs; |
2539 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2540 | * bed->s->int_rels_per_ext_rel); | |
2541 | while (irela < irelaend) | |
2542 | { | |
2543 | (*swap_out) (output_bfd, irela, erel); | |
2544 | irela += bed->s->int_rels_per_ext_rel; | |
2545 | erel += input_rel_hdr->sh_entsize; | |
2546 | } | |
2547 | ||
2548 | /* Bump the counter, so that we know where to add the next set of | |
2549 | relocations. */ | |
d4730f92 | 2550 | output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); |
45d6a902 AM |
2551 | |
2552 | return TRUE; | |
2553 | } | |
2554 | \f | |
508c3946 L |
2555 | /* Make weak undefined symbols in PIE dynamic. */ |
2556 | ||
2557 | bfd_boolean | |
2558 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2559 | struct elf_link_hash_entry *h) | |
2560 | { | |
0e1862bb | 2561 | if (bfd_link_pie (info) |
508c3946 L |
2562 | && h->dynindx == -1 |
2563 | && h->root.type == bfd_link_hash_undefweak) | |
2564 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2565 | ||
2566 | return TRUE; | |
2567 | } | |
2568 | ||
45d6a902 AM |
2569 | /* Fix up the flags for a symbol. This handles various cases which |
2570 | can only be fixed after all the input files are seen. This is | |
2571 | currently called by both adjust_dynamic_symbol and | |
2572 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2573 | the face of future changes. */ | |
2574 | ||
28caa186 | 2575 | static bfd_boolean |
268b6b39 AM |
2576 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2577 | struct elf_info_failed *eif) | |
45d6a902 | 2578 | { |
33774f08 | 2579 | const struct elf_backend_data *bed; |
508c3946 | 2580 | |
45d6a902 AM |
2581 | /* If this symbol was mentioned in a non-ELF file, try to set |
2582 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2583 | permit a non-ELF file to correctly refer to a symbol defined in | |
2584 | an ELF dynamic object. */ | |
f5385ebf | 2585 | if (h->non_elf) |
45d6a902 AM |
2586 | { |
2587 | while (h->root.type == bfd_link_hash_indirect) | |
2588 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2589 | ||
2590 | if (h->root.type != bfd_link_hash_defined | |
2591 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2592 | { |
2593 | h->ref_regular = 1; | |
2594 | h->ref_regular_nonweak = 1; | |
2595 | } | |
45d6a902 AM |
2596 | else |
2597 | { | |
2598 | if (h->root.u.def.section->owner != NULL | |
2599 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2600 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2601 | { |
2602 | h->ref_regular = 1; | |
2603 | h->ref_regular_nonweak = 1; | |
2604 | } | |
45d6a902 | 2605 | else |
f5385ebf | 2606 | h->def_regular = 1; |
45d6a902 AM |
2607 | } |
2608 | ||
2609 | if (h->dynindx == -1 | |
f5385ebf AM |
2610 | && (h->def_dynamic |
2611 | || h->ref_dynamic)) | |
45d6a902 | 2612 | { |
c152c796 | 2613 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2614 | { |
2615 | eif->failed = TRUE; | |
2616 | return FALSE; | |
2617 | } | |
2618 | } | |
2619 | } | |
2620 | else | |
2621 | { | |
f5385ebf | 2622 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2623 | was first seen in a non-ELF file. Fortunately, if the symbol |
2624 | was first seen in an ELF file, we're probably OK unless the | |
2625 | symbol was defined in a non-ELF file. Catch that case here. | |
2626 | FIXME: We're still in trouble if the symbol was first seen in | |
2627 | a dynamic object, and then later in a non-ELF regular object. */ | |
2628 | if ((h->root.type == bfd_link_hash_defined | |
2629 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2630 | && !h->def_regular |
45d6a902 AM |
2631 | && (h->root.u.def.section->owner != NULL |
2632 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2633 | != bfd_target_elf_flavour) | |
2634 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2635 | && !h->def_dynamic))) |
2636 | h->def_regular = 1; | |
45d6a902 AM |
2637 | } |
2638 | ||
508c3946 | 2639 | /* Backend specific symbol fixup. */ |
33774f08 AM |
2640 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2641 | if (bed->elf_backend_fixup_symbol | |
2642 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2643 | return FALSE; | |
508c3946 | 2644 | |
45d6a902 AM |
2645 | /* If this is a final link, and the symbol was defined as a common |
2646 | symbol in a regular object file, and there was no definition in | |
2647 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2648 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2649 | flag will not have been set. */ |
2650 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2651 | && !h->def_regular |
2652 | && h->ref_regular | |
2653 | && !h->def_dynamic | |
96f29d96 | 2654 | && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) |
f5385ebf | 2655 | h->def_regular = 1; |
45d6a902 AM |
2656 | |
2657 | /* If -Bsymbolic was used (which means to bind references to global | |
2658 | symbols to the definition within the shared object), and this | |
2659 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2660 | need a PLT entry. Likewise, if the symbol has non-default |
2661 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2662 | will force it local. */ |
f5385ebf | 2663 | if (h->needs_plt |
0e1862bb | 2664 | && bfd_link_pic (eif->info) |
0eddce27 | 2665 | && is_elf_hash_table (eif->info->hash) |
55255dae | 2666 | && (SYMBOLIC_BIND (eif->info, h) |
c1be741f | 2667 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2668 | && h->def_regular) |
45d6a902 | 2669 | { |
45d6a902 AM |
2670 | bfd_boolean force_local; |
2671 | ||
45d6a902 AM |
2672 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2673 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2674 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2675 | } | |
2676 | ||
2677 | /* If a weak undefined symbol has non-default visibility, we also | |
2678 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2679 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 | 2680 | && h->root.type == bfd_link_hash_undefweak) |
33774f08 | 2681 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
45d6a902 AM |
2682 | |
2683 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2684 | the real definition in the dynamic object, copy interesting flags | |
2685 | over to the real definition. */ | |
f6e332e6 | 2686 | if (h->u.weakdef != NULL) |
45d6a902 | 2687 | { |
45d6a902 AM |
2688 | /* If the real definition is defined by a regular object file, |
2689 | don't do anything special. See the longer description in | |
2690 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
4e6b54a6 | 2691 | if (h->u.weakdef->def_regular) |
f6e332e6 | 2692 | h->u.weakdef = NULL; |
45d6a902 | 2693 | else |
a26587ba | 2694 | { |
4e6b54a6 AM |
2695 | struct elf_link_hash_entry *weakdef = h->u.weakdef; |
2696 | ||
2697 | while (h->root.type == bfd_link_hash_indirect) | |
2698 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2699 | ||
2700 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2701 | || h->root.type == bfd_link_hash_defweak); | |
2702 | BFD_ASSERT (weakdef->def_dynamic); | |
a26587ba RS |
2703 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined |
2704 | || weakdef->root.type == bfd_link_hash_defweak); | |
2705 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h); | |
2706 | } | |
45d6a902 AM |
2707 | } |
2708 | ||
2709 | return TRUE; | |
2710 | } | |
2711 | ||
2712 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2713 | called via elf_link_hash_traverse, and also calls itself | |
2714 | recursively. */ | |
2715 | ||
28caa186 | 2716 | static bfd_boolean |
268b6b39 | 2717 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2718 | { |
a50b1753 | 2719 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 | 2720 | bfd *dynobj; |
9c5bfbb7 | 2721 | const struct elf_backend_data *bed; |
45d6a902 | 2722 | |
0eddce27 | 2723 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2724 | return FALSE; |
2725 | ||
45d6a902 AM |
2726 | /* Ignore indirect symbols. These are added by the versioning code. */ |
2727 | if (h->root.type == bfd_link_hash_indirect) | |
2728 | return TRUE; | |
2729 | ||
2730 | /* Fix the symbol flags. */ | |
2731 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2732 | return FALSE; | |
2733 | ||
2734 | /* If this symbol does not require a PLT entry, and it is not | |
2735 | defined by a dynamic object, or is not referenced by a regular | |
2736 | object, ignore it. We do have to handle a weak defined symbol, | |
2737 | even if no regular object refers to it, if we decided to add it | |
2738 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2739 | about symbols which are defined by one dynamic object and | |
2740 | referenced by another one? */ | |
f5385ebf | 2741 | if (!h->needs_plt |
91e21fb7 | 2742 | && h->type != STT_GNU_IFUNC |
f5385ebf AM |
2743 | && (h->def_regular |
2744 | || !h->def_dynamic | |
2745 | || (!h->ref_regular | |
f6e332e6 | 2746 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 | 2747 | { |
a6aa5195 | 2748 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2749 | return TRUE; |
2750 | } | |
2751 | ||
2752 | /* If we've already adjusted this symbol, don't do it again. This | |
2753 | can happen via a recursive call. */ | |
f5385ebf | 2754 | if (h->dynamic_adjusted) |
45d6a902 AM |
2755 | return TRUE; |
2756 | ||
2757 | /* Don't look at this symbol again. Note that we must set this | |
2758 | after checking the above conditions, because we may look at a | |
2759 | symbol once, decide not to do anything, and then get called | |
2760 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2761 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2762 | |
2763 | /* If this is a weak definition, and we know a real definition, and | |
2764 | the real symbol is not itself defined by a regular object file, | |
2765 | then get a good value for the real definition. We handle the | |
2766 | real symbol first, for the convenience of the backend routine. | |
2767 | ||
2768 | Note that there is a confusing case here. If the real definition | |
2769 | is defined by a regular object file, we don't get the real symbol | |
2770 | from the dynamic object, but we do get the weak symbol. If the | |
2771 | processor backend uses a COPY reloc, then if some routine in the | |
2772 | dynamic object changes the real symbol, we will not see that | |
2773 | change in the corresponding weak symbol. This is the way other | |
2774 | ELF linkers work as well, and seems to be a result of the shared | |
2775 | library model. | |
2776 | ||
2777 | I will clarify this issue. Most SVR4 shared libraries define the | |
2778 | variable _timezone and define timezone as a weak synonym. The | |
2779 | tzset call changes _timezone. If you write | |
2780 | extern int timezone; | |
2781 | int _timezone = 5; | |
2782 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2783 | you might expect that, since timezone is a synonym for _timezone, | |
2784 | the same number will print both times. However, if the processor | |
2785 | backend uses a COPY reloc, then actually timezone will be copied | |
2786 | into your process image, and, since you define _timezone | |
2787 | yourself, _timezone will not. Thus timezone and _timezone will | |
2788 | wind up at different memory locations. The tzset call will set | |
2789 | _timezone, leaving timezone unchanged. */ | |
2790 | ||
f6e332e6 | 2791 | if (h->u.weakdef != NULL) |
45d6a902 | 2792 | { |
ec24dc88 AM |
2793 | /* If we get to this point, there is an implicit reference to |
2794 | H->U.WEAKDEF by a regular object file via the weak symbol H. */ | |
f6e332e6 | 2795 | h->u.weakdef->ref_regular = 1; |
45d6a902 | 2796 | |
ec24dc88 AM |
2797 | /* Ensure that the backend adjust_dynamic_symbol function sees |
2798 | H->U.WEAKDEF before H by recursively calling ourselves. */ | |
f6e332e6 | 2799 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2800 | return FALSE; |
2801 | } | |
2802 | ||
2803 | /* If a symbol has no type and no size and does not require a PLT | |
2804 | entry, then we are probably about to do the wrong thing here: we | |
2805 | are probably going to create a COPY reloc for an empty object. | |
2806 | This case can arise when a shared object is built with assembly | |
2807 | code, and the assembly code fails to set the symbol type. */ | |
2808 | if (h->size == 0 | |
2809 | && h->type == STT_NOTYPE | |
f5385ebf | 2810 | && !h->needs_plt) |
4eca0228 | 2811 | _bfd_error_handler |
45d6a902 AM |
2812 | (_("warning: type and size of dynamic symbol `%s' are not defined"), |
2813 | h->root.root.string); | |
2814 | ||
2815 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2816 | bed = get_elf_backend_data (dynobj); | |
e7c33416 | 2817 | |
45d6a902 AM |
2818 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) |
2819 | { | |
2820 | eif->failed = TRUE; | |
2821 | return FALSE; | |
2822 | } | |
2823 | ||
2824 | return TRUE; | |
2825 | } | |
2826 | ||
027297b7 L |
2827 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
2828 | DYNBSS. */ | |
2829 | ||
2830 | bfd_boolean | |
6cabe1ea AM |
2831 | _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, |
2832 | struct elf_link_hash_entry *h, | |
027297b7 L |
2833 | asection *dynbss) |
2834 | { | |
91ac5911 | 2835 | unsigned int power_of_two; |
027297b7 L |
2836 | bfd_vma mask; |
2837 | asection *sec = h->root.u.def.section; | |
2838 | ||
2839 | /* The section aligment of definition is the maximum alignment | |
91ac5911 L |
2840 | requirement of symbols defined in the section. Since we don't |
2841 | know the symbol alignment requirement, we start with the | |
2842 | maximum alignment and check low bits of the symbol address | |
2843 | for the minimum alignment. */ | |
2844 | power_of_two = bfd_get_section_alignment (sec->owner, sec); | |
2845 | mask = ((bfd_vma) 1 << power_of_two) - 1; | |
2846 | while ((h->root.u.def.value & mask) != 0) | |
2847 | { | |
2848 | mask >>= 1; | |
2849 | --power_of_two; | |
2850 | } | |
027297b7 | 2851 | |
91ac5911 L |
2852 | if (power_of_two > bfd_get_section_alignment (dynbss->owner, |
2853 | dynbss)) | |
027297b7 L |
2854 | { |
2855 | /* Adjust the section alignment if needed. */ | |
2856 | if (! bfd_set_section_alignment (dynbss->owner, dynbss, | |
91ac5911 | 2857 | power_of_two)) |
027297b7 L |
2858 | return FALSE; |
2859 | } | |
2860 | ||
91ac5911 | 2861 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
2862 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
2863 | ||
2864 | /* Define the symbol as being at this point in DYNBSS. */ | |
2865 | h->root.u.def.section = dynbss; | |
2866 | h->root.u.def.value = dynbss->size; | |
2867 | ||
2868 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
2869 | dynbss->size += h->size; | |
2870 | ||
f7483970 L |
2871 | /* No error if extern_protected_data is true. */ |
2872 | if (h->protected_def | |
889c2a67 L |
2873 | && (!info->extern_protected_data |
2874 | || (info->extern_protected_data < 0 | |
2875 | && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) | |
d07a1b05 AM |
2876 | info->callbacks->einfo |
2877 | (_("%P: copy reloc against protected `%T' is dangerous\n"), | |
2878 | h->root.root.string); | |
6cabe1ea | 2879 | |
027297b7 L |
2880 | return TRUE; |
2881 | } | |
2882 | ||
45d6a902 AM |
2883 | /* Adjust all external symbols pointing into SEC_MERGE sections |
2884 | to reflect the object merging within the sections. */ | |
2885 | ||
28caa186 | 2886 | static bfd_boolean |
268b6b39 | 2887 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2888 | { |
2889 | asection *sec; | |
2890 | ||
45d6a902 AM |
2891 | if ((h->root.type == bfd_link_hash_defined |
2892 | || h->root.type == bfd_link_hash_defweak) | |
2893 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
dbaa2011 | 2894 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
45d6a902 | 2895 | { |
a50b1753 | 2896 | bfd *output_bfd = (bfd *) data; |
45d6a902 AM |
2897 | |
2898 | h->root.u.def.value = | |
2899 | _bfd_merged_section_offset (output_bfd, | |
2900 | &h->root.u.def.section, | |
2901 | elf_section_data (sec)->sec_info, | |
753731ee | 2902 | h->root.u.def.value); |
45d6a902 AM |
2903 | } |
2904 | ||
2905 | return TRUE; | |
2906 | } | |
986a241f RH |
2907 | |
2908 | /* Returns false if the symbol referred to by H should be considered | |
2909 | to resolve local to the current module, and true if it should be | |
2910 | considered to bind dynamically. */ | |
2911 | ||
2912 | bfd_boolean | |
268b6b39 AM |
2913 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2914 | struct bfd_link_info *info, | |
89a2ee5a | 2915 | bfd_boolean not_local_protected) |
986a241f RH |
2916 | { |
2917 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
2918 | const struct elf_backend_data *bed; |
2919 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
2920 | |
2921 | if (h == NULL) | |
2922 | return FALSE; | |
2923 | ||
2924 | while (h->root.type == bfd_link_hash_indirect | |
2925 | || h->root.type == bfd_link_hash_warning) | |
2926 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2927 | ||
2928 | /* If it was forced local, then clearly it's not dynamic. */ | |
2929 | if (h->dynindx == -1) | |
2930 | return FALSE; | |
f5385ebf | 2931 | if (h->forced_local) |
986a241f RH |
2932 | return FALSE; |
2933 | ||
2934 | /* Identify the cases where name binding rules say that a | |
2935 | visible symbol resolves locally. */ | |
0e1862bb L |
2936 | binding_stays_local_p = (bfd_link_executable (info) |
2937 | || SYMBOLIC_BIND (info, h)); | |
986a241f RH |
2938 | |
2939 | switch (ELF_ST_VISIBILITY (h->other)) | |
2940 | { | |
2941 | case STV_INTERNAL: | |
2942 | case STV_HIDDEN: | |
2943 | return FALSE; | |
2944 | ||
2945 | case STV_PROTECTED: | |
fcb93ecf PB |
2946 | hash_table = elf_hash_table (info); |
2947 | if (!is_elf_hash_table (hash_table)) | |
2948 | return FALSE; | |
2949 | ||
2950 | bed = get_elf_backend_data (hash_table->dynobj); | |
2951 | ||
986a241f RH |
2952 | /* Proper resolution for function pointer equality may require |
2953 | that these symbols perhaps be resolved dynamically, even though | |
2954 | we should be resolving them to the current module. */ | |
89a2ee5a | 2955 | if (!not_local_protected || !bed->is_function_type (h->type)) |
986a241f RH |
2956 | binding_stays_local_p = TRUE; |
2957 | break; | |
2958 | ||
2959 | default: | |
986a241f RH |
2960 | break; |
2961 | } | |
2962 | ||
aa37626c | 2963 | /* If it isn't defined locally, then clearly it's dynamic. */ |
89a2ee5a | 2964 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
aa37626c L |
2965 | return TRUE; |
2966 | ||
986a241f RH |
2967 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2968 | us that it remains local. */ | |
2969 | return !binding_stays_local_p; | |
2970 | } | |
f6c52c13 AM |
2971 | |
2972 | /* Return true if the symbol referred to by H should be considered | |
2973 | to resolve local to the current module, and false otherwise. Differs | |
2974 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2e76e85a | 2975 | undefined symbols. The two functions are virtually identical except |
89a2ee5a AM |
2976 | for the place where forced_local and dynindx == -1 are tested. If |
2977 | either of those tests are true, _bfd_elf_dynamic_symbol_p will say | |
2978 | the symbol is local, while _bfd_elf_symbol_refs_local_p will say | |
2979 | the symbol is local only for defined symbols. | |
2980 | It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as | |
2981 | !_bfd_elf_symbol_refs_local_p, except that targets differ in their | |
2982 | treatment of undefined weak symbols. For those that do not make | |
2983 | undefined weak symbols dynamic, both functions may return false. */ | |
f6c52c13 AM |
2984 | |
2985 | bfd_boolean | |
268b6b39 AM |
2986 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2987 | struct bfd_link_info *info, | |
2988 | bfd_boolean local_protected) | |
f6c52c13 | 2989 | { |
fcb93ecf PB |
2990 | const struct elf_backend_data *bed; |
2991 | struct elf_link_hash_table *hash_table; | |
2992 | ||
f6c52c13 AM |
2993 | /* If it's a local sym, of course we resolve locally. */ |
2994 | if (h == NULL) | |
2995 | return TRUE; | |
2996 | ||
d95edcac L |
2997 | /* STV_HIDDEN or STV_INTERNAL ones must be local. */ |
2998 | if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
2999 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
3000 | return TRUE; | |
3001 | ||
7e2294f9 AO |
3002 | /* Common symbols that become definitions don't get the DEF_REGULAR |
3003 | flag set, so test it first, and don't bail out. */ | |
3004 | if (ELF_COMMON_DEF_P (h)) | |
3005 | /* Do nothing. */; | |
f6c52c13 | 3006 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
3007 | resolve locally. The sym is either undefined or dynamic. */ |
3008 | else if (!h->def_regular) | |
f6c52c13 AM |
3009 | return FALSE; |
3010 | ||
3011 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 3012 | if (h->forced_local) |
f6c52c13 AM |
3013 | return TRUE; |
3014 | ||
3015 | /* As do non-dynamic symbols. */ | |
3016 | if (h->dynindx == -1) | |
3017 | return TRUE; | |
3018 | ||
3019 | /* At this point, we know the symbol is defined and dynamic. In an | |
3020 | executable it must resolve locally, likewise when building symbolic | |
3021 | shared libraries. */ | |
0e1862bb | 3022 | if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
3023 | return TRUE; |
3024 | ||
3025 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
3026 | with default visibility might not resolve locally. */ | |
3027 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
3028 | return FALSE; | |
3029 | ||
fcb93ecf PB |
3030 | hash_table = elf_hash_table (info); |
3031 | if (!is_elf_hash_table (hash_table)) | |
3032 | return TRUE; | |
3033 | ||
3034 | bed = get_elf_backend_data (hash_table->dynobj); | |
3035 | ||
f7483970 L |
3036 | /* If extern_protected_data is false, STV_PROTECTED non-function |
3037 | symbols are local. */ | |
889c2a67 L |
3038 | if ((!info->extern_protected_data |
3039 | || (info->extern_protected_data < 0 | |
3040 | && !bed->extern_protected_data)) | |
3041 | && !bed->is_function_type (h->type)) | |
1c16dfa5 L |
3042 | return TRUE; |
3043 | ||
f6c52c13 | 3044 | /* Function pointer equality tests may require that STV_PROTECTED |
2676a7d9 AM |
3045 | symbols be treated as dynamic symbols. If the address of a |
3046 | function not defined in an executable is set to that function's | |
3047 | plt entry in the executable, then the address of the function in | |
3048 | a shared library must also be the plt entry in the executable. */ | |
f6c52c13 AM |
3049 | return local_protected; |
3050 | } | |
e1918d23 AM |
3051 | |
3052 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
3053 | aligned. Returns the first TLS output section. */ | |
3054 | ||
3055 | struct bfd_section * | |
3056 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
3057 | { | |
3058 | struct bfd_section *sec, *tls; | |
3059 | unsigned int align = 0; | |
3060 | ||
3061 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
3062 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
3063 | break; | |
3064 | tls = sec; | |
3065 | ||
3066 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
3067 | if (sec->alignment_power > align) | |
3068 | align = sec->alignment_power; | |
3069 | ||
3070 | elf_hash_table (info)->tls_sec = tls; | |
3071 | ||
3072 | /* Ensure the alignment of the first section is the largest alignment, | |
3073 | so that the tls segment starts aligned. */ | |
3074 | if (tls != NULL) | |
3075 | tls->alignment_power = align; | |
3076 | ||
3077 | return tls; | |
3078 | } | |
0ad989f9 L |
3079 | |
3080 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
3081 | static bfd_boolean | |
3082 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
3083 | Elf_Internal_Sym *sym) | |
3084 | { | |
a4d8e49b L |
3085 | const struct elf_backend_data *bed; |
3086 | ||
0ad989f9 L |
3087 | /* Local symbols do not count, but target specific ones might. */ |
3088 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
3089 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
3090 | return FALSE; | |
3091 | ||
fcb93ecf | 3092 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 3093 | /* Function symbols do not count. */ |
fcb93ecf | 3094 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
3095 | return FALSE; |
3096 | ||
3097 | /* If the section is undefined, then so is the symbol. */ | |
3098 | if (sym->st_shndx == SHN_UNDEF) | |
3099 | return FALSE; | |
3100 | ||
3101 | /* If the symbol is defined in the common section, then | |
3102 | it is a common definition and so does not count. */ | |
a4d8e49b | 3103 | if (bed->common_definition (sym)) |
0ad989f9 L |
3104 | return FALSE; |
3105 | ||
3106 | /* If the symbol is in a target specific section then we | |
3107 | must rely upon the backend to tell us what it is. */ | |
3108 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
3109 | /* FIXME - this function is not coded yet: | |
3110 | ||
3111 | return _bfd_is_global_symbol_definition (abfd, sym); | |
3112 | ||
3113 | Instead for now assume that the definition is not global, | |
3114 | Even if this is wrong, at least the linker will behave | |
3115 | in the same way that it used to do. */ | |
3116 | return FALSE; | |
3117 | ||
3118 | return TRUE; | |
3119 | } | |
3120 | ||
3121 | /* Search the symbol table of the archive element of the archive ABFD | |
3122 | whose archive map contains a mention of SYMDEF, and determine if | |
3123 | the symbol is defined in this element. */ | |
3124 | static bfd_boolean | |
3125 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
3126 | { | |
3127 | Elf_Internal_Shdr * hdr; | |
ef53be89 AM |
3128 | size_t symcount; |
3129 | size_t extsymcount; | |
3130 | size_t extsymoff; | |
0ad989f9 L |
3131 | Elf_Internal_Sym *isymbuf; |
3132 | Elf_Internal_Sym *isym; | |
3133 | Elf_Internal_Sym *isymend; | |
3134 | bfd_boolean result; | |
3135 | ||
3136 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
3137 | if (abfd == NULL) | |
3138 | return FALSE; | |
3139 | ||
3140 | if (! bfd_check_format (abfd, bfd_object)) | |
3141 | return FALSE; | |
3142 | ||
7dc3990e L |
3143 | /* Select the appropriate symbol table. If we don't know if the |
3144 | object file is an IR object, give linker LTO plugin a chance to | |
3145 | get the correct symbol table. */ | |
3146 | if (abfd->plugin_format == bfd_plugin_yes | |
08ce1d72 | 3147 | #if BFD_SUPPORTS_PLUGINS |
7dc3990e L |
3148 | || (abfd->plugin_format == bfd_plugin_unknown |
3149 | && bfd_link_plugin_object_p (abfd)) | |
3150 | #endif | |
3151 | ) | |
3152 | { | |
3153 | /* Use the IR symbol table if the object has been claimed by | |
3154 | plugin. */ | |
3155 | abfd = abfd->plugin_dummy_bfd; | |
3156 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3157 | } | |
3158 | else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
0ad989f9 L |
3159 | hdr = &elf_tdata (abfd)->symtab_hdr; |
3160 | else | |
3161 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3162 | ||
3163 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3164 | ||
3165 | /* The sh_info field of the symtab header tells us where the | |
3166 | external symbols start. We don't care about the local symbols. */ | |
3167 | if (elf_bad_symtab (abfd)) | |
3168 | { | |
3169 | extsymcount = symcount; | |
3170 | extsymoff = 0; | |
3171 | } | |
3172 | else | |
3173 | { | |
3174 | extsymcount = symcount - hdr->sh_info; | |
3175 | extsymoff = hdr->sh_info; | |
3176 | } | |
3177 | ||
3178 | if (extsymcount == 0) | |
3179 | return FALSE; | |
3180 | ||
3181 | /* Read in the symbol table. */ | |
3182 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3183 | NULL, NULL, NULL); | |
3184 | if (isymbuf == NULL) | |
3185 | return FALSE; | |
3186 | ||
3187 | /* Scan the symbol table looking for SYMDEF. */ | |
3188 | result = FALSE; | |
3189 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
3190 | { | |
3191 | const char *name; | |
3192 | ||
3193 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3194 | isym->st_name); | |
3195 | if (name == NULL) | |
3196 | break; | |
3197 | ||
3198 | if (strcmp (name, symdef->name) == 0) | |
3199 | { | |
3200 | result = is_global_data_symbol_definition (abfd, isym); | |
3201 | break; | |
3202 | } | |
3203 | } | |
3204 | ||
3205 | free (isymbuf); | |
3206 | ||
3207 | return result; | |
3208 | } | |
3209 | \f | |
5a580b3a AM |
3210 | /* Add an entry to the .dynamic table. */ |
3211 | ||
3212 | bfd_boolean | |
3213 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
3214 | bfd_vma tag, | |
3215 | bfd_vma val) | |
3216 | { | |
3217 | struct elf_link_hash_table *hash_table; | |
3218 | const struct elf_backend_data *bed; | |
3219 | asection *s; | |
3220 | bfd_size_type newsize; | |
3221 | bfd_byte *newcontents; | |
3222 | Elf_Internal_Dyn dyn; | |
3223 | ||
3224 | hash_table = elf_hash_table (info); | |
3225 | if (! is_elf_hash_table (hash_table)) | |
3226 | return FALSE; | |
3227 | ||
3228 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3229 | s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
5a580b3a AM |
3230 | BFD_ASSERT (s != NULL); |
3231 | ||
eea6121a | 3232 | newsize = s->size + bed->s->sizeof_dyn; |
a50b1753 | 3233 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
5a580b3a AM |
3234 | if (newcontents == NULL) |
3235 | return FALSE; | |
3236 | ||
3237 | dyn.d_tag = tag; | |
3238 | dyn.d_un.d_val = val; | |
eea6121a | 3239 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3240 | |
eea6121a | 3241 | s->size = newsize; |
5a580b3a AM |
3242 | s->contents = newcontents; |
3243 | ||
3244 | return TRUE; | |
3245 | } | |
3246 | ||
3247 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
3248 | otherwise just check whether one already exists. Returns -1 on error, | |
3249 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
3250 | ||
4ad4eba5 | 3251 | static int |
7e9f0867 AM |
3252 | elf_add_dt_needed_tag (bfd *abfd, |
3253 | struct bfd_link_info *info, | |
4ad4eba5 AM |
3254 | const char *soname, |
3255 | bfd_boolean do_it) | |
5a580b3a AM |
3256 | { |
3257 | struct elf_link_hash_table *hash_table; | |
ef53be89 | 3258 | size_t strindex; |
5a580b3a | 3259 | |
7e9f0867 AM |
3260 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3261 | return -1; | |
3262 | ||
5a580b3a | 3263 | hash_table = elf_hash_table (info); |
5a580b3a | 3264 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); |
ef53be89 | 3265 | if (strindex == (size_t) -1) |
5a580b3a AM |
3266 | return -1; |
3267 | ||
02be4619 | 3268 | if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) |
5a580b3a AM |
3269 | { |
3270 | asection *sdyn; | |
3271 | const struct elf_backend_data *bed; | |
3272 | bfd_byte *extdyn; | |
3273 | ||
3274 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3275 | sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
7e9f0867 AM |
3276 | if (sdyn != NULL) |
3277 | for (extdyn = sdyn->contents; | |
3278 | extdyn < sdyn->contents + sdyn->size; | |
3279 | extdyn += bed->s->sizeof_dyn) | |
3280 | { | |
3281 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3282 | |
7e9f0867 AM |
3283 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3284 | if (dyn.d_tag == DT_NEEDED | |
3285 | && dyn.d_un.d_val == strindex) | |
3286 | { | |
3287 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3288 | return 1; | |
3289 | } | |
3290 | } | |
5a580b3a AM |
3291 | } |
3292 | ||
3293 | if (do_it) | |
3294 | { | |
7e9f0867 AM |
3295 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3296 | return -1; | |
3297 | ||
5a580b3a AM |
3298 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3299 | return -1; | |
3300 | } | |
3301 | else | |
3302 | /* We were just checking for existence of the tag. */ | |
3303 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3304 | ||
3305 | return 0; | |
3306 | } | |
3307 | ||
7b15fa7a AM |
3308 | /* Return true if SONAME is on the needed list between NEEDED and STOP |
3309 | (or the end of list if STOP is NULL), and needed by a library that | |
3310 | will be loaded. */ | |
3311 | ||
010e5ae2 | 3312 | static bfd_boolean |
7b15fa7a AM |
3313 | on_needed_list (const char *soname, |
3314 | struct bfd_link_needed_list *needed, | |
3315 | struct bfd_link_needed_list *stop) | |
010e5ae2 | 3316 | { |
7b15fa7a AM |
3317 | struct bfd_link_needed_list *look; |
3318 | for (look = needed; look != stop; look = look->next) | |
3319 | if (strcmp (soname, look->name) == 0 | |
3320 | && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 | |
3321 | /* If needed by a library that itself is not directly | |
3322 | needed, recursively check whether that library is | |
3323 | indirectly needed. Since we add DT_NEEDED entries to | |
3324 | the end of the list, library dependencies appear after | |
3325 | the library. Therefore search prior to the current | |
3326 | LOOK, preventing possible infinite recursion. */ | |
3327 | || on_needed_list (elf_dt_name (look->by), needed, look))) | |
010e5ae2 AM |
3328 | return TRUE; |
3329 | ||
3330 | return FALSE; | |
3331 | } | |
3332 | ||
14160578 | 3333 | /* Sort symbol by value, section, and size. */ |
4ad4eba5 AM |
3334 | static int |
3335 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3336 | { |
3337 | const struct elf_link_hash_entry *h1; | |
3338 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3339 | bfd_signed_vma vdiff; |
5a580b3a AM |
3340 | |
3341 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3342 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3343 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3344 | if (vdiff != 0) | |
3345 | return vdiff > 0 ? 1 : -1; | |
3346 | else | |
3347 | { | |
d3435ae8 | 3348 | int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; |
10b7e05b NC |
3349 | if (sdiff != 0) |
3350 | return sdiff > 0 ? 1 : -1; | |
3351 | } | |
14160578 AM |
3352 | vdiff = h1->size - h2->size; |
3353 | return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1; | |
5a580b3a | 3354 | } |
4ad4eba5 | 3355 | |
5a580b3a AM |
3356 | /* This function is used to adjust offsets into .dynstr for |
3357 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3358 | ||
3359 | static bfd_boolean | |
3360 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3361 | { | |
a50b1753 | 3362 | struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; |
5a580b3a | 3363 | |
5a580b3a AM |
3364 | if (h->dynindx != -1) |
3365 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3366 | return TRUE; | |
3367 | } | |
3368 | ||
3369 | /* Assign string offsets in .dynstr, update all structures referencing | |
3370 | them. */ | |
3371 | ||
4ad4eba5 AM |
3372 | static bfd_boolean |
3373 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3374 | { |
3375 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3376 | struct elf_link_local_dynamic_entry *entry; | |
3377 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3378 | bfd *dynobj = hash_table->dynobj; | |
3379 | asection *sdyn; | |
3380 | bfd_size_type size; | |
3381 | const struct elf_backend_data *bed; | |
3382 | bfd_byte *extdyn; | |
3383 | ||
3384 | _bfd_elf_strtab_finalize (dynstr); | |
3385 | size = _bfd_elf_strtab_size (dynstr); | |
3386 | ||
3387 | bed = get_elf_backend_data (dynobj); | |
3d4d4302 | 3388 | sdyn = bfd_get_linker_section (dynobj, ".dynamic"); |
5a580b3a AM |
3389 | BFD_ASSERT (sdyn != NULL); |
3390 | ||
3391 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3392 | for (extdyn = sdyn->contents; | |
eea6121a | 3393 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3394 | extdyn += bed->s->sizeof_dyn) |
3395 | { | |
3396 | Elf_Internal_Dyn dyn; | |
3397 | ||
3398 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3399 | switch (dyn.d_tag) | |
3400 | { | |
3401 | case DT_STRSZ: | |
3402 | dyn.d_un.d_val = size; | |
3403 | break; | |
3404 | case DT_NEEDED: | |
3405 | case DT_SONAME: | |
3406 | case DT_RPATH: | |
3407 | case DT_RUNPATH: | |
3408 | case DT_FILTER: | |
3409 | case DT_AUXILIARY: | |
7ee314fa AM |
3410 | case DT_AUDIT: |
3411 | case DT_DEPAUDIT: | |
5a580b3a AM |
3412 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); |
3413 | break; | |
3414 | default: | |
3415 | continue; | |
3416 | } | |
3417 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3418 | } | |
3419 | ||
3420 | /* Now update local dynamic symbols. */ | |
3421 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3422 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3423 | entry->isym.st_name); | |
3424 | ||
3425 | /* And the rest of dynamic symbols. */ | |
3426 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3427 | ||
3428 | /* Adjust version definitions. */ | |
3429 | if (elf_tdata (output_bfd)->cverdefs) | |
3430 | { | |
3431 | asection *s; | |
3432 | bfd_byte *p; | |
ef53be89 | 3433 | size_t i; |
5a580b3a AM |
3434 | Elf_Internal_Verdef def; |
3435 | Elf_Internal_Verdaux defaux; | |
3436 | ||
3d4d4302 | 3437 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
3438 | p = s->contents; |
3439 | do | |
3440 | { | |
3441 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3442 | &def); | |
3443 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3444 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3445 | continue; | |
5a580b3a AM |
3446 | for (i = 0; i < def.vd_cnt; ++i) |
3447 | { | |
3448 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3449 | (Elf_External_Verdaux *) p, &defaux); | |
3450 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3451 | defaux.vda_name); | |
3452 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3453 | &defaux, (Elf_External_Verdaux *) p); | |
3454 | p += sizeof (Elf_External_Verdaux); | |
3455 | } | |
3456 | } | |
3457 | while (def.vd_next); | |
3458 | } | |
3459 | ||
3460 | /* Adjust version references. */ | |
3461 | if (elf_tdata (output_bfd)->verref) | |
3462 | { | |
3463 | asection *s; | |
3464 | bfd_byte *p; | |
ef53be89 | 3465 | size_t i; |
5a580b3a AM |
3466 | Elf_Internal_Verneed need; |
3467 | Elf_Internal_Vernaux needaux; | |
3468 | ||
3d4d4302 | 3469 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
3470 | p = s->contents; |
3471 | do | |
3472 | { | |
3473 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3474 | &need); | |
3475 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3476 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3477 | (Elf_External_Verneed *) p); | |
3478 | p += sizeof (Elf_External_Verneed); | |
3479 | for (i = 0; i < need.vn_cnt; ++i) | |
3480 | { | |
3481 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3482 | (Elf_External_Vernaux *) p, &needaux); | |
3483 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3484 | needaux.vna_name); | |
3485 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3486 | &needaux, | |
3487 | (Elf_External_Vernaux *) p); | |
3488 | p += sizeof (Elf_External_Vernaux); | |
3489 | } | |
3490 | } | |
3491 | while (need.vn_next); | |
3492 | } | |
3493 | ||
3494 | return TRUE; | |
3495 | } | |
3496 | \f | |
13285a1b AM |
3497 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
3498 | The default is to only match when the INPUT and OUTPUT are exactly | |
3499 | the same target. */ | |
3500 | ||
3501 | bfd_boolean | |
3502 | _bfd_elf_default_relocs_compatible (const bfd_target *input, | |
3503 | const bfd_target *output) | |
3504 | { | |
3505 | return input == output; | |
3506 | } | |
3507 | ||
3508 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
3509 | This version is used when different targets for the same architecture | |
3510 | are virtually identical. */ | |
3511 | ||
3512 | bfd_boolean | |
3513 | _bfd_elf_relocs_compatible (const bfd_target *input, | |
3514 | const bfd_target *output) | |
3515 | { | |
3516 | const struct elf_backend_data *obed, *ibed; | |
3517 | ||
3518 | if (input == output) | |
3519 | return TRUE; | |
3520 | ||
3521 | ibed = xvec_get_elf_backend_data (input); | |
3522 | obed = xvec_get_elf_backend_data (output); | |
3523 | ||
3524 | if (ibed->arch != obed->arch) | |
3525 | return FALSE; | |
3526 | ||
3527 | /* If both backends are using this function, deem them compatible. */ | |
3528 | return ibed->relocs_compatible == obed->relocs_compatible; | |
3529 | } | |
3530 | ||
e5034e59 AM |
3531 | /* Make a special call to the linker "notice" function to tell it that |
3532 | we are about to handle an as-needed lib, or have finished | |
1b786873 | 3533 | processing the lib. */ |
e5034e59 AM |
3534 | |
3535 | bfd_boolean | |
3536 | _bfd_elf_notice_as_needed (bfd *ibfd, | |
3537 | struct bfd_link_info *info, | |
3538 | enum notice_asneeded_action act) | |
3539 | { | |
46135103 | 3540 | return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); |
e5034e59 AM |
3541 | } |
3542 | ||
d9689752 L |
3543 | /* Check relocations an ELF object file. */ |
3544 | ||
3545 | bfd_boolean | |
3546 | _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) | |
3547 | { | |
3548 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3549 | struct elf_link_hash_table *htab = elf_hash_table (info); | |
3550 | ||
3551 | /* If this object is the same format as the output object, and it is | |
3552 | not a shared library, then let the backend look through the | |
3553 | relocs. | |
3554 | ||
3555 | This is required to build global offset table entries and to | |
3556 | arrange for dynamic relocs. It is not required for the | |
3557 | particular common case of linking non PIC code, even when linking | |
3558 | against shared libraries, but unfortunately there is no way of | |
3559 | knowing whether an object file has been compiled PIC or not. | |
3560 | Looking through the relocs is not particularly time consuming. | |
3561 | The problem is that we must either (1) keep the relocs in memory, | |
3562 | which causes the linker to require additional runtime memory or | |
3563 | (2) read the relocs twice from the input file, which wastes time. | |
3564 | This would be a good case for using mmap. | |
3565 | ||
3566 | I have no idea how to handle linking PIC code into a file of a | |
3567 | different format. It probably can't be done. */ | |
3568 | if ((abfd->flags & DYNAMIC) == 0 | |
3569 | && is_elf_hash_table (htab) | |
3570 | && bed->check_relocs != NULL | |
3571 | && elf_object_id (abfd) == elf_hash_table_id (htab) | |
3572 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
3573 | { | |
3574 | asection *o; | |
3575 | ||
3576 | for (o = abfd->sections; o != NULL; o = o->next) | |
3577 | { | |
3578 | Elf_Internal_Rela *internal_relocs; | |
3579 | bfd_boolean ok; | |
3580 | ||
5ce03cea | 3581 | /* Don't check relocations in excluded sections. */ |
d9689752 | 3582 | if ((o->flags & SEC_RELOC) == 0 |
5ce03cea | 3583 | || (o->flags & SEC_EXCLUDE) != 0 |
d9689752 L |
3584 | || o->reloc_count == 0 |
3585 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
3586 | && (o->flags & SEC_DEBUGGING) != 0) | |
3587 | || bfd_is_abs_section (o->output_section)) | |
3588 | continue; | |
3589 | ||
3590 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
3591 | info->keep_memory); | |
3592 | if (internal_relocs == NULL) | |
3593 | return FALSE; | |
3594 | ||
3595 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); | |
3596 | ||
3597 | if (elf_section_data (o)->relocs != internal_relocs) | |
3598 | free (internal_relocs); | |
3599 | ||
3600 | if (! ok) | |
3601 | return FALSE; | |
3602 | } | |
3603 | } | |
3604 | ||
3605 | return TRUE; | |
3606 | } | |
3607 | ||
4ad4eba5 AM |
3608 | /* Add symbols from an ELF object file to the linker hash table. */ |
3609 | ||
3610 | static bfd_boolean | |
3611 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3612 | { | |
a0c402a5 | 3613 | Elf_Internal_Ehdr *ehdr; |
4ad4eba5 | 3614 | Elf_Internal_Shdr *hdr; |
ef53be89 AM |
3615 | size_t symcount; |
3616 | size_t extsymcount; | |
3617 | size_t extsymoff; | |
4ad4eba5 AM |
3618 | struct elf_link_hash_entry **sym_hash; |
3619 | bfd_boolean dynamic; | |
3620 | Elf_External_Versym *extversym = NULL; | |
3621 | Elf_External_Versym *ever; | |
3622 | struct elf_link_hash_entry *weaks; | |
3623 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
ef53be89 | 3624 | size_t nondeflt_vers_cnt = 0; |
4ad4eba5 AM |
3625 | Elf_Internal_Sym *isymbuf = NULL; |
3626 | Elf_Internal_Sym *isym; | |
3627 | Elf_Internal_Sym *isymend; | |
3628 | const struct elf_backend_data *bed; | |
3629 | bfd_boolean add_needed; | |
66eb6687 | 3630 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3631 | bfd_size_type amt; |
66eb6687 | 3632 | void *alloc_mark = NULL; |
4f87808c AM |
3633 | struct bfd_hash_entry **old_table = NULL; |
3634 | unsigned int old_size = 0; | |
3635 | unsigned int old_count = 0; | |
66eb6687 | 3636 | void *old_tab = NULL; |
66eb6687 AM |
3637 | void *old_ent; |
3638 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3639 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
5b677558 | 3640 | void *old_strtab = NULL; |
66eb6687 | 3641 | size_t tabsize = 0; |
db6a5d5f | 3642 | asection *s; |
29a9f53e | 3643 | bfd_boolean just_syms; |
4ad4eba5 | 3644 | |
66eb6687 | 3645 | htab = elf_hash_table (info); |
4ad4eba5 | 3646 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3647 | |
3648 | if ((abfd->flags & DYNAMIC) == 0) | |
3649 | dynamic = FALSE; | |
3650 | else | |
3651 | { | |
3652 | dynamic = TRUE; | |
3653 | ||
3654 | /* You can't use -r against a dynamic object. Also, there's no | |
3655 | hope of using a dynamic object which does not exactly match | |
3656 | the format of the output file. */ | |
0e1862bb | 3657 | if (bfd_link_relocatable (info) |
66eb6687 | 3658 | || !is_elf_hash_table (htab) |
f13a99db | 3659 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 3660 | { |
0e1862bb | 3661 | if (bfd_link_relocatable (info)) |
9a0789ec NC |
3662 | bfd_set_error (bfd_error_invalid_operation); |
3663 | else | |
3664 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3665 | goto error_return; |
3666 | } | |
3667 | } | |
3668 | ||
a0c402a5 L |
3669 | ehdr = elf_elfheader (abfd); |
3670 | if (info->warn_alternate_em | |
3671 | && bed->elf_machine_code != ehdr->e_machine | |
3672 | && ((bed->elf_machine_alt1 != 0 | |
3673 | && ehdr->e_machine == bed->elf_machine_alt1) | |
3674 | || (bed->elf_machine_alt2 != 0 | |
3675 | && ehdr->e_machine == bed->elf_machine_alt2))) | |
3676 | info->callbacks->einfo | |
695344c0 | 3677 | /* xgettext:c-format */ |
a0c402a5 L |
3678 | (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"), |
3679 | ehdr->e_machine, abfd, bed->elf_machine_code); | |
3680 | ||
4ad4eba5 AM |
3681 | /* As a GNU extension, any input sections which are named |
3682 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3683 | symbol. This differs from .gnu.warning sections, which generate | |
3684 | warnings when they are included in an output file. */ | |
dd98f8d2 | 3685 | /* PR 12761: Also generate this warning when building shared libraries. */ |
db6a5d5f | 3686 | for (s = abfd->sections; s != NULL; s = s->next) |
4ad4eba5 | 3687 | { |
db6a5d5f | 3688 | const char *name; |
4ad4eba5 | 3689 | |
db6a5d5f AM |
3690 | name = bfd_get_section_name (abfd, s); |
3691 | if (CONST_STRNEQ (name, ".gnu.warning.")) | |
4ad4eba5 | 3692 | { |
db6a5d5f AM |
3693 | char *msg; |
3694 | bfd_size_type sz; | |
3695 | ||
3696 | name += sizeof ".gnu.warning." - 1; | |
3697 | ||
3698 | /* If this is a shared object, then look up the symbol | |
3699 | in the hash table. If it is there, and it is already | |
3700 | been defined, then we will not be using the entry | |
3701 | from this shared object, so we don't need to warn. | |
3702 | FIXME: If we see the definition in a regular object | |
3703 | later on, we will warn, but we shouldn't. The only | |
3704 | fix is to keep track of what warnings we are supposed | |
3705 | to emit, and then handle them all at the end of the | |
3706 | link. */ | |
3707 | if (dynamic) | |
4ad4eba5 | 3708 | { |
db6a5d5f AM |
3709 | struct elf_link_hash_entry *h; |
3710 | ||
3711 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); | |
3712 | ||
3713 | /* FIXME: What about bfd_link_hash_common? */ | |
3714 | if (h != NULL | |
3715 | && (h->root.type == bfd_link_hash_defined | |
3716 | || h->root.type == bfd_link_hash_defweak)) | |
3717 | continue; | |
3718 | } | |
4ad4eba5 | 3719 | |
db6a5d5f AM |
3720 | sz = s->size; |
3721 | msg = (char *) bfd_alloc (abfd, sz + 1); | |
3722 | if (msg == NULL) | |
3723 | goto error_return; | |
4ad4eba5 | 3724 | |
db6a5d5f AM |
3725 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
3726 | goto error_return; | |
4ad4eba5 | 3727 | |
db6a5d5f | 3728 | msg[sz] = '\0'; |
4ad4eba5 | 3729 | |
db6a5d5f AM |
3730 | if (! (_bfd_generic_link_add_one_symbol |
3731 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3732 | FALSE, bed->collect, NULL))) | |
3733 | goto error_return; | |
4ad4eba5 | 3734 | |
0e1862bb | 3735 | if (bfd_link_executable (info)) |
db6a5d5f AM |
3736 | { |
3737 | /* Clobber the section size so that the warning does | |
3738 | not get copied into the output file. */ | |
3739 | s->size = 0; | |
11d2f718 | 3740 | |
db6a5d5f AM |
3741 | /* Also set SEC_EXCLUDE, so that symbols defined in |
3742 | the warning section don't get copied to the output. */ | |
3743 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3744 | } |
3745 | } | |
3746 | } | |
3747 | ||
29a9f53e L |
3748 | just_syms = ((s = abfd->sections) != NULL |
3749 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); | |
3750 | ||
4ad4eba5 AM |
3751 | add_needed = TRUE; |
3752 | if (! dynamic) | |
3753 | { | |
3754 | /* If we are creating a shared library, create all the dynamic | |
3755 | sections immediately. We need to attach them to something, | |
3756 | so we attach them to this BFD, provided it is the right | |
bf89386a L |
3757 | format and is not from ld --just-symbols. Always create the |
3758 | dynamic sections for -E/--dynamic-list. FIXME: If there | |
29a9f53e L |
3759 | are no input BFD's of the same format as the output, we can't |
3760 | make a shared library. */ | |
3761 | if (!just_syms | |
bf89386a | 3762 | && (bfd_link_pic (info) |
9c1d7a08 L |
3763 | || (!bfd_link_relocatable (info) |
3764 | && (info->export_dynamic || info->dynamic))) | |
66eb6687 | 3765 | && is_elf_hash_table (htab) |
f13a99db | 3766 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 3767 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
3768 | { |
3769 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3770 | goto error_return; | |
3771 | } | |
3772 | } | |
66eb6687 | 3773 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
3774 | goto error_return; |
3775 | else | |
3776 | { | |
4ad4eba5 | 3777 | const char *soname = NULL; |
7ee314fa | 3778 | char *audit = NULL; |
4ad4eba5 AM |
3779 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; |
3780 | int ret; | |
3781 | ||
3782 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3783 | ld shouldn't allow it. */ |
29a9f53e | 3784 | if (just_syms) |
92fd189d | 3785 | abort (); |
4ad4eba5 AM |
3786 | |
3787 | /* If this dynamic lib was specified on the command line with | |
3788 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3789 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3790 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3791 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3792 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3793 | all. */ | |
3794 | add_needed = (elf_dyn_lib_class (abfd) | |
3795 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3796 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3797 | |
3798 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3799 | if (s != NULL) | |
3800 | { | |
3801 | bfd_byte *dynbuf; | |
3802 | bfd_byte *extdyn; | |
cb33740c | 3803 | unsigned int elfsec; |
4ad4eba5 AM |
3804 | unsigned long shlink; |
3805 | ||
eea6121a | 3806 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
f8703194 L |
3807 | { |
3808 | error_free_dyn: | |
3809 | free (dynbuf); | |
3810 | goto error_return; | |
3811 | } | |
4ad4eba5 AM |
3812 | |
3813 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 3814 | if (elfsec == SHN_BAD) |
4ad4eba5 AM |
3815 | goto error_free_dyn; |
3816 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3817 | ||
3818 | for (extdyn = dynbuf; | |
eea6121a | 3819 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3820 | extdyn += bed->s->sizeof_dyn) |
3821 | { | |
3822 | Elf_Internal_Dyn dyn; | |
3823 | ||
3824 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3825 | if (dyn.d_tag == DT_SONAME) | |
3826 | { | |
3827 | unsigned int tagv = dyn.d_un.d_val; | |
3828 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3829 | if (soname == NULL) | |
3830 | goto error_free_dyn; | |
3831 | } | |
3832 | if (dyn.d_tag == DT_NEEDED) | |
3833 | { | |
3834 | struct bfd_link_needed_list *n, **pn; | |
3835 | char *fnm, *anm; | |
3836 | unsigned int tagv = dyn.d_un.d_val; | |
3837 | ||
3838 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3839 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3840 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3841 | if (n == NULL || fnm == NULL) | |
3842 | goto error_free_dyn; | |
3843 | amt = strlen (fnm) + 1; | |
a50b1753 | 3844 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3845 | if (anm == NULL) |
3846 | goto error_free_dyn; | |
3847 | memcpy (anm, fnm, amt); | |
3848 | n->name = anm; | |
3849 | n->by = abfd; | |
3850 | n->next = NULL; | |
66eb6687 | 3851 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3852 | ; |
3853 | *pn = n; | |
3854 | } | |
3855 | if (dyn.d_tag == DT_RUNPATH) | |
3856 | { | |
3857 | struct bfd_link_needed_list *n, **pn; | |
3858 | char *fnm, *anm; | |
3859 | unsigned int tagv = dyn.d_un.d_val; | |
3860 | ||
3861 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3862 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3863 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3864 | if (n == NULL || fnm == NULL) | |
3865 | goto error_free_dyn; | |
3866 | amt = strlen (fnm) + 1; | |
a50b1753 | 3867 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3868 | if (anm == NULL) |
3869 | goto error_free_dyn; | |
3870 | memcpy (anm, fnm, amt); | |
3871 | n->name = anm; | |
3872 | n->by = abfd; | |
3873 | n->next = NULL; | |
3874 | for (pn = & runpath; | |
3875 | *pn != NULL; | |
3876 | pn = &(*pn)->next) | |
3877 | ; | |
3878 | *pn = n; | |
3879 | } | |
3880 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3881 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3882 | { | |
3883 | struct bfd_link_needed_list *n, **pn; | |
3884 | char *fnm, *anm; | |
3885 | unsigned int tagv = dyn.d_un.d_val; | |
3886 | ||
3887 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3888 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3889 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3890 | if (n == NULL || fnm == NULL) | |
3891 | goto error_free_dyn; | |
3892 | amt = strlen (fnm) + 1; | |
a50b1753 | 3893 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 | 3894 | if (anm == NULL) |
f8703194 | 3895 | goto error_free_dyn; |
4ad4eba5 AM |
3896 | memcpy (anm, fnm, amt); |
3897 | n->name = anm; | |
3898 | n->by = abfd; | |
3899 | n->next = NULL; | |
3900 | for (pn = & rpath; | |
3901 | *pn != NULL; | |
3902 | pn = &(*pn)->next) | |
3903 | ; | |
3904 | *pn = n; | |
3905 | } | |
7ee314fa AM |
3906 | if (dyn.d_tag == DT_AUDIT) |
3907 | { | |
3908 | unsigned int tagv = dyn.d_un.d_val; | |
3909 | audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3910 | } | |
4ad4eba5 AM |
3911 | } |
3912 | ||
3913 | free (dynbuf); | |
3914 | } | |
3915 | ||
3916 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3917 | frees all more recently bfd_alloc'd blocks as well. */ | |
3918 | if (runpath) | |
3919 | rpath = runpath; | |
3920 | ||
3921 | if (rpath) | |
3922 | { | |
3923 | struct bfd_link_needed_list **pn; | |
66eb6687 | 3924 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3925 | ; |
3926 | *pn = rpath; | |
3927 | } | |
3928 | ||
3929 | /* We do not want to include any of the sections in a dynamic | |
3930 | object in the output file. We hack by simply clobbering the | |
3931 | list of sections in the BFD. This could be handled more | |
3932 | cleanly by, say, a new section flag; the existing | |
3933 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3934 | still implies that the section takes up space in the output | |
3935 | file. */ | |
3936 | bfd_section_list_clear (abfd); | |
3937 | ||
4ad4eba5 AM |
3938 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3939 | object. If the object has a DT_SONAME entry, we use it. | |
3940 | Otherwise, if the generic linker stuck something in | |
3941 | elf_dt_name, we use that. Otherwise, we just use the file | |
3942 | name. */ | |
3943 | if (soname == NULL || *soname == '\0') | |
3944 | { | |
3945 | soname = elf_dt_name (abfd); | |
3946 | if (soname == NULL || *soname == '\0') | |
3947 | soname = bfd_get_filename (abfd); | |
3948 | } | |
3949 | ||
3950 | /* Save the SONAME because sometimes the linker emulation code | |
3951 | will need to know it. */ | |
3952 | elf_dt_name (abfd) = soname; | |
3953 | ||
7e9f0867 | 3954 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3955 | if (ret < 0) |
3956 | goto error_return; | |
3957 | ||
3958 | /* If we have already included this dynamic object in the | |
3959 | link, just ignore it. There is no reason to include a | |
3960 | particular dynamic object more than once. */ | |
3961 | if (ret > 0) | |
3962 | return TRUE; | |
7ee314fa AM |
3963 | |
3964 | /* Save the DT_AUDIT entry for the linker emulation code. */ | |
68ffbac6 | 3965 | elf_dt_audit (abfd) = audit; |
4ad4eba5 AM |
3966 | } |
3967 | ||
3968 | /* If this is a dynamic object, we always link against the .dynsym | |
3969 | symbol table, not the .symtab symbol table. The dynamic linker | |
3970 | will only see the .dynsym symbol table, so there is no reason to | |
3971 | look at .symtab for a dynamic object. */ | |
3972 | ||
3973 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3974 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3975 | else | |
3976 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3977 | ||
3978 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3979 | ||
3980 | /* The sh_info field of the symtab header tells us where the | |
3981 | external symbols start. We don't care about the local symbols at | |
3982 | this point. */ | |
3983 | if (elf_bad_symtab (abfd)) | |
3984 | { | |
3985 | extsymcount = symcount; | |
3986 | extsymoff = 0; | |
3987 | } | |
3988 | else | |
3989 | { | |
3990 | extsymcount = symcount - hdr->sh_info; | |
3991 | extsymoff = hdr->sh_info; | |
3992 | } | |
3993 | ||
f45794cb | 3994 | sym_hash = elf_sym_hashes (abfd); |
012b2306 | 3995 | if (extsymcount != 0) |
4ad4eba5 AM |
3996 | { |
3997 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3998 | NULL, NULL, NULL); | |
3999 | if (isymbuf == NULL) | |
4000 | goto error_return; | |
4001 | ||
4ad4eba5 | 4002 | if (sym_hash == NULL) |
012b2306 AM |
4003 | { |
4004 | /* We store a pointer to the hash table entry for each | |
4005 | external symbol. */ | |
ef53be89 AM |
4006 | amt = extsymcount; |
4007 | amt *= sizeof (struct elf_link_hash_entry *); | |
012b2306 AM |
4008 | sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); |
4009 | if (sym_hash == NULL) | |
4010 | goto error_free_sym; | |
4011 | elf_sym_hashes (abfd) = sym_hash; | |
4012 | } | |
4ad4eba5 AM |
4013 | } |
4014 | ||
4015 | if (dynamic) | |
4016 | { | |
4017 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
4018 | if (!_bfd_elf_slurp_version_tables (abfd, |
4019 | info->default_imported_symver)) | |
4ad4eba5 AM |
4020 | goto error_free_sym; |
4021 | ||
4022 | /* Read in the symbol versions, but don't bother to convert them | |
4023 | to internal format. */ | |
4024 | if (elf_dynversym (abfd) != 0) | |
4025 | { | |
4026 | Elf_Internal_Shdr *versymhdr; | |
4027 | ||
4028 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
a50b1753 | 4029 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
4ad4eba5 AM |
4030 | if (extversym == NULL) |
4031 | goto error_free_sym; | |
4032 | amt = versymhdr->sh_size; | |
4033 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
4034 | || bfd_bread (extversym, amt, abfd) != amt) | |
4035 | goto error_free_vers; | |
4036 | } | |
4037 | } | |
4038 | ||
66eb6687 AM |
4039 | /* If we are loading an as-needed shared lib, save the symbol table |
4040 | state before we start adding symbols. If the lib turns out | |
4041 | to be unneeded, restore the state. */ | |
4042 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4043 | { | |
4044 | unsigned int i; | |
4045 | size_t entsize; | |
4046 | ||
4047 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
4048 | { | |
4049 | struct bfd_hash_entry *p; | |
2de92251 | 4050 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4051 | |
4052 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
4053 | { |
4054 | h = (struct elf_link_hash_entry *) p; | |
4055 | entsize += htab->root.table.entsize; | |
4056 | if (h->root.type == bfd_link_hash_warning) | |
4057 | entsize += htab->root.table.entsize; | |
4058 | } | |
66eb6687 AM |
4059 | } |
4060 | ||
4061 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
f45794cb | 4062 | old_tab = bfd_malloc (tabsize + entsize); |
66eb6687 AM |
4063 | if (old_tab == NULL) |
4064 | goto error_free_vers; | |
4065 | ||
4066 | /* Remember the current objalloc pointer, so that all mem for | |
4067 | symbols added can later be reclaimed. */ | |
4068 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
4069 | if (alloc_mark == NULL) | |
4070 | goto error_free_vers; | |
4071 | ||
5061a885 AM |
4072 | /* Make a special call to the linker "notice" function to |
4073 | tell it that we are about to handle an as-needed lib. */ | |
e5034e59 | 4074 | if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) |
9af2a943 | 4075 | goto error_free_vers; |
5061a885 | 4076 | |
f45794cb AM |
4077 | /* Clone the symbol table. Remember some pointers into the |
4078 | symbol table, and dynamic symbol count. */ | |
4079 | old_ent = (char *) old_tab + tabsize; | |
66eb6687 | 4080 | memcpy (old_tab, htab->root.table.table, tabsize); |
66eb6687 AM |
4081 | old_undefs = htab->root.undefs; |
4082 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
4083 | old_table = htab->root.table.table; |
4084 | old_size = htab->root.table.size; | |
4085 | old_count = htab->root.table.count; | |
5b677558 AM |
4086 | old_strtab = _bfd_elf_strtab_save (htab->dynstr); |
4087 | if (old_strtab == NULL) | |
4088 | goto error_free_vers; | |
66eb6687 AM |
4089 | |
4090 | for (i = 0; i < htab->root.table.size; i++) | |
4091 | { | |
4092 | struct bfd_hash_entry *p; | |
2de92251 | 4093 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4094 | |
4095 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4096 | { | |
4097 | memcpy (old_ent, p, htab->root.table.entsize); | |
4098 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4099 | h = (struct elf_link_hash_entry *) p; |
4100 | if (h->root.type == bfd_link_hash_warning) | |
4101 | { | |
4102 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
4103 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4104 | } | |
66eb6687 AM |
4105 | } |
4106 | } | |
4107 | } | |
4ad4eba5 | 4108 | |
66eb6687 | 4109 | weaks = NULL; |
4ad4eba5 AM |
4110 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
4111 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
4112 | isym < isymend; | |
4113 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
4114 | { | |
4115 | int bind; | |
4116 | bfd_vma value; | |
af44c138 | 4117 | asection *sec, *new_sec; |
4ad4eba5 AM |
4118 | flagword flags; |
4119 | const char *name; | |
4120 | struct elf_link_hash_entry *h; | |
90c984fc | 4121 | struct elf_link_hash_entry *hi; |
4ad4eba5 AM |
4122 | bfd_boolean definition; |
4123 | bfd_boolean size_change_ok; | |
4124 | bfd_boolean type_change_ok; | |
4125 | bfd_boolean new_weakdef; | |
37a9e49a L |
4126 | bfd_boolean new_weak; |
4127 | bfd_boolean old_weak; | |
4ad4eba5 | 4128 | bfd_boolean override; |
a4d8e49b | 4129 | bfd_boolean common; |
97196564 | 4130 | bfd_boolean discarded; |
4ad4eba5 AM |
4131 | unsigned int old_alignment; |
4132 | bfd *old_bfd; | |
6e33951e | 4133 | bfd_boolean matched; |
4ad4eba5 AM |
4134 | |
4135 | override = FALSE; | |
4136 | ||
4137 | flags = BSF_NO_FLAGS; | |
4138 | sec = NULL; | |
4139 | value = isym->st_value; | |
a4d8e49b | 4140 | common = bed->common_definition (isym); |
97196564 | 4141 | discarded = FALSE; |
4ad4eba5 AM |
4142 | |
4143 | bind = ELF_ST_BIND (isym->st_info); | |
3e7a7d11 | 4144 | switch (bind) |
4ad4eba5 | 4145 | { |
3e7a7d11 | 4146 | case STB_LOCAL: |
4ad4eba5 AM |
4147 | /* This should be impossible, since ELF requires that all |
4148 | global symbols follow all local symbols, and that sh_info | |
4149 | point to the first global symbol. Unfortunately, Irix 5 | |
4150 | screws this up. */ | |
4151 | continue; | |
3e7a7d11 NC |
4152 | |
4153 | case STB_GLOBAL: | |
a4d8e49b | 4154 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 | 4155 | flags = BSF_GLOBAL; |
3e7a7d11 NC |
4156 | break; |
4157 | ||
4158 | case STB_WEAK: | |
4159 | flags = BSF_WEAK; | |
4160 | break; | |
4161 | ||
4162 | case STB_GNU_UNIQUE: | |
4163 | flags = BSF_GNU_UNIQUE; | |
4164 | break; | |
4165 | ||
4166 | default: | |
4ad4eba5 | 4167 | /* Leave it up to the processor backend. */ |
3e7a7d11 | 4168 | break; |
4ad4eba5 AM |
4169 | } |
4170 | ||
4171 | if (isym->st_shndx == SHN_UNDEF) | |
4172 | sec = bfd_und_section_ptr; | |
cb33740c AM |
4173 | else if (isym->st_shndx == SHN_ABS) |
4174 | sec = bfd_abs_section_ptr; | |
4175 | else if (isym->st_shndx == SHN_COMMON) | |
4176 | { | |
4177 | sec = bfd_com_section_ptr; | |
4178 | /* What ELF calls the size we call the value. What ELF | |
4179 | calls the value we call the alignment. */ | |
4180 | value = isym->st_size; | |
4181 | } | |
4182 | else | |
4ad4eba5 AM |
4183 | { |
4184 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
4185 | if (sec == NULL) | |
4186 | sec = bfd_abs_section_ptr; | |
dbaa2011 | 4187 | else if (discarded_section (sec)) |
529fcb95 | 4188 | { |
e5d08002 L |
4189 | /* Symbols from discarded section are undefined. We keep |
4190 | its visibility. */ | |
529fcb95 | 4191 | sec = bfd_und_section_ptr; |
97196564 | 4192 | discarded = TRUE; |
529fcb95 PB |
4193 | isym->st_shndx = SHN_UNDEF; |
4194 | } | |
4ad4eba5 AM |
4195 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
4196 | value -= sec->vma; | |
4197 | } | |
4ad4eba5 AM |
4198 | |
4199 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
4200 | isym->st_name); | |
4201 | if (name == NULL) | |
4202 | goto error_free_vers; | |
4203 | ||
4204 | if (isym->st_shndx == SHN_COMMON | |
02d00247 AM |
4205 | && (abfd->flags & BFD_PLUGIN) != 0) |
4206 | { | |
4207 | asection *xc = bfd_get_section_by_name (abfd, "COMMON"); | |
4208 | ||
4209 | if (xc == NULL) | |
4210 | { | |
4211 | flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP | |
4212 | | SEC_EXCLUDE); | |
4213 | xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); | |
4214 | if (xc == NULL) | |
4215 | goto error_free_vers; | |
4216 | } | |
4217 | sec = xc; | |
4218 | } | |
4219 | else if (isym->st_shndx == SHN_COMMON | |
4220 | && ELF_ST_TYPE (isym->st_info) == STT_TLS | |
0e1862bb | 4221 | && !bfd_link_relocatable (info)) |
4ad4eba5 AM |
4222 | { |
4223 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
4224 | ||
4225 | if (tcomm == NULL) | |
4226 | { | |
02d00247 AM |
4227 | flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON |
4228 | | SEC_LINKER_CREATED); | |
4229 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); | |
3496cb2a | 4230 | if (tcomm == NULL) |
4ad4eba5 AM |
4231 | goto error_free_vers; |
4232 | } | |
4233 | sec = tcomm; | |
4234 | } | |
66eb6687 | 4235 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 4236 | { |
66eb6687 AM |
4237 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
4238 | &sec, &value)) | |
4ad4eba5 AM |
4239 | goto error_free_vers; |
4240 | ||
4241 | /* The hook function sets the name to NULL if this symbol | |
4242 | should be skipped for some reason. */ | |
4243 | if (name == NULL) | |
4244 | continue; | |
4245 | } | |
4246 | ||
4247 | /* Sanity check that all possibilities were handled. */ | |
4248 | if (sec == NULL) | |
4249 | { | |
4250 | bfd_set_error (bfd_error_bad_value); | |
4251 | goto error_free_vers; | |
4252 | } | |
4253 | ||
191c0c42 AM |
4254 | /* Silently discard TLS symbols from --just-syms. There's |
4255 | no way to combine a static TLS block with a new TLS block | |
4256 | for this executable. */ | |
4257 | if (ELF_ST_TYPE (isym->st_info) == STT_TLS | |
4258 | && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
4259 | continue; | |
4260 | ||
4ad4eba5 AM |
4261 | if (bfd_is_und_section (sec) |
4262 | || bfd_is_com_section (sec)) | |
4263 | definition = FALSE; | |
4264 | else | |
4265 | definition = TRUE; | |
4266 | ||
4267 | size_change_ok = FALSE; | |
66eb6687 | 4268 | type_change_ok = bed->type_change_ok; |
37a9e49a | 4269 | old_weak = FALSE; |
6e33951e | 4270 | matched = FALSE; |
4ad4eba5 AM |
4271 | old_alignment = 0; |
4272 | old_bfd = NULL; | |
af44c138 | 4273 | new_sec = sec; |
4ad4eba5 | 4274 | |
66eb6687 | 4275 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
4276 | { |
4277 | Elf_Internal_Versym iver; | |
4278 | unsigned int vernum = 0; | |
4279 | bfd_boolean skip; | |
4280 | ||
fc0e6df6 | 4281 | if (ever == NULL) |
4ad4eba5 | 4282 | { |
fc0e6df6 PB |
4283 | if (info->default_imported_symver) |
4284 | /* Use the default symbol version created earlier. */ | |
4285 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
4286 | else | |
4287 | iver.vs_vers = 0; | |
4288 | } | |
4289 | else | |
4290 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
4291 | ||
4292 | vernum = iver.vs_vers & VERSYM_VERSION; | |
4293 | ||
4294 | /* If this is a hidden symbol, or if it is not version | |
4295 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
4296 | However, we do not modify a non-hidden absolute symbol |
4297 | if it is not a function, because it might be the version | |
4298 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 4299 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
4300 | || (vernum > 1 |
4301 | && (!bfd_is_abs_section (sec) | |
4302 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
4303 | { |
4304 | const char *verstr; | |
4305 | size_t namelen, verlen, newlen; | |
4306 | char *newname, *p; | |
4307 | ||
4308 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 4309 | { |
fc0e6df6 PB |
4310 | if (vernum > elf_tdata (abfd)->cverdefs) |
4311 | verstr = NULL; | |
4312 | else if (vernum > 1) | |
4313 | verstr = | |
4314 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
4315 | else | |
4316 | verstr = ""; | |
4ad4eba5 | 4317 | |
fc0e6df6 | 4318 | if (verstr == NULL) |
4ad4eba5 | 4319 | { |
4eca0228 | 4320 | _bfd_error_handler |
695344c0 | 4321 | /* xgettext:c-format */ |
fc0e6df6 PB |
4322 | (_("%B: %s: invalid version %u (max %d)"), |
4323 | abfd, name, vernum, | |
4324 | elf_tdata (abfd)->cverdefs); | |
4325 | bfd_set_error (bfd_error_bad_value); | |
4326 | goto error_free_vers; | |
4ad4eba5 | 4327 | } |
fc0e6df6 PB |
4328 | } |
4329 | else | |
4330 | { | |
4331 | /* We cannot simply test for the number of | |
4332 | entries in the VERNEED section since the | |
4333 | numbers for the needed versions do not start | |
4334 | at 0. */ | |
4335 | Elf_Internal_Verneed *t; | |
4336 | ||
4337 | verstr = NULL; | |
4338 | for (t = elf_tdata (abfd)->verref; | |
4339 | t != NULL; | |
4340 | t = t->vn_nextref) | |
4ad4eba5 | 4341 | { |
fc0e6df6 | 4342 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 4343 | |
fc0e6df6 PB |
4344 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
4345 | { | |
4346 | if (a->vna_other == vernum) | |
4ad4eba5 | 4347 | { |
fc0e6df6 PB |
4348 | verstr = a->vna_nodename; |
4349 | break; | |
4ad4eba5 | 4350 | } |
4ad4eba5 | 4351 | } |
fc0e6df6 PB |
4352 | if (a != NULL) |
4353 | break; | |
4354 | } | |
4355 | if (verstr == NULL) | |
4356 | { | |
4eca0228 | 4357 | _bfd_error_handler |
695344c0 | 4358 | /* xgettext:c-format */ |
fc0e6df6 PB |
4359 | (_("%B: %s: invalid needed version %d"), |
4360 | abfd, name, vernum); | |
4361 | bfd_set_error (bfd_error_bad_value); | |
4362 | goto error_free_vers; | |
4ad4eba5 | 4363 | } |
4ad4eba5 | 4364 | } |
fc0e6df6 PB |
4365 | |
4366 | namelen = strlen (name); | |
4367 | verlen = strlen (verstr); | |
4368 | newlen = namelen + verlen + 2; | |
4369 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4370 | && isym->st_shndx != SHN_UNDEF) | |
4371 | ++newlen; | |
4372 | ||
a50b1753 | 4373 | newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
4374 | if (newname == NULL) |
4375 | goto error_free_vers; | |
4376 | memcpy (newname, name, namelen); | |
4377 | p = newname + namelen; | |
4378 | *p++ = ELF_VER_CHR; | |
4379 | /* If this is a defined non-hidden version symbol, | |
4380 | we add another @ to the name. This indicates the | |
4381 | default version of the symbol. */ | |
4382 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4383 | && isym->st_shndx != SHN_UNDEF) | |
4384 | *p++ = ELF_VER_CHR; | |
4385 | memcpy (p, verstr, verlen + 1); | |
4386 | ||
4387 | name = newname; | |
4ad4eba5 AM |
4388 | } |
4389 | ||
cd3416da AM |
4390 | /* If this symbol has default visibility and the user has |
4391 | requested we not re-export it, then mark it as hidden. */ | |
a0d49154 | 4392 | if (!bfd_is_und_section (sec) |
cd3416da | 4393 | && !dynamic |
ce875075 | 4394 | && abfd->no_export |
cd3416da AM |
4395 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) |
4396 | isym->st_other = (STV_HIDDEN | |
4397 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
4398 | ||
4f3fedcf AM |
4399 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, |
4400 | sym_hash, &old_bfd, &old_weak, | |
4401 | &old_alignment, &skip, &override, | |
6e33951e L |
4402 | &type_change_ok, &size_change_ok, |
4403 | &matched)) | |
4ad4eba5 AM |
4404 | goto error_free_vers; |
4405 | ||
4406 | if (skip) | |
4407 | continue; | |
4408 | ||
6e33951e L |
4409 | /* Override a definition only if the new symbol matches the |
4410 | existing one. */ | |
4411 | if (override && matched) | |
4ad4eba5 AM |
4412 | definition = FALSE; |
4413 | ||
4414 | h = *sym_hash; | |
4415 | while (h->root.type == bfd_link_hash_indirect | |
4416 | || h->root.type == bfd_link_hash_warning) | |
4417 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4418 | ||
4ad4eba5 | 4419 | if (elf_tdata (abfd)->verdef != NULL |
4ad4eba5 AM |
4420 | && vernum > 1 |
4421 | && definition) | |
4422 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4423 | } | |
4424 | ||
4425 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4426 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4427 | (struct bfd_link_hash_entry **) sym_hash))) |
4428 | goto error_free_vers; | |
4429 | ||
a43942db MR |
4430 | if ((flags & BSF_GNU_UNIQUE) |
4431 | && (abfd->flags & DYNAMIC) == 0 | |
4432 | && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour) | |
4433 | elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique; | |
4434 | ||
4ad4eba5 | 4435 | h = *sym_hash; |
90c984fc L |
4436 | /* We need to make sure that indirect symbol dynamic flags are |
4437 | updated. */ | |
4438 | hi = h; | |
4ad4eba5 AM |
4439 | while (h->root.type == bfd_link_hash_indirect |
4440 | || h->root.type == bfd_link_hash_warning) | |
4441 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3e7a7d11 | 4442 | |
97196564 L |
4443 | /* Setting the index to -3 tells elf_link_output_extsym that |
4444 | this symbol is defined in a discarded section. */ | |
4445 | if (discarded) | |
4446 | h->indx = -3; | |
4447 | ||
4ad4eba5 AM |
4448 | *sym_hash = h; |
4449 | ||
37a9e49a | 4450 | new_weak = (flags & BSF_WEAK) != 0; |
4ad4eba5 AM |
4451 | new_weakdef = FALSE; |
4452 | if (dynamic | |
4453 | && definition | |
37a9e49a | 4454 | && new_weak |
fcb93ecf | 4455 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4456 | && is_elf_hash_table (htab) |
f6e332e6 | 4457 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
4458 | { |
4459 | /* Keep a list of all weak defined non function symbols from | |
4460 | a dynamic object, using the weakdef field. Later in this | |
4461 | function we will set the weakdef field to the correct | |
4462 | value. We only put non-function symbols from dynamic | |
4463 | objects on this list, because that happens to be the only | |
4464 | time we need to know the normal symbol corresponding to a | |
4465 | weak symbol, and the information is time consuming to | |
4466 | figure out. If the weakdef field is not already NULL, | |
4467 | then this symbol was already defined by some previous | |
4468 | dynamic object, and we will be using that previous | |
4469 | definition anyhow. */ | |
4470 | ||
f6e332e6 | 4471 | h->u.weakdef = weaks; |
4ad4eba5 AM |
4472 | weaks = h; |
4473 | new_weakdef = TRUE; | |
4474 | } | |
4475 | ||
4476 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4477 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4478 | && h->root.type == bfd_link_hash_common) |
4479 | { | |
4480 | unsigned int align; | |
4481 | ||
a4d8e49b | 4482 | if (common) |
af44c138 L |
4483 | align = bfd_log2 (isym->st_value); |
4484 | else | |
4485 | { | |
4486 | /* The new symbol is a common symbol in a shared object. | |
4487 | We need to get the alignment from the section. */ | |
4488 | align = new_sec->alignment_power; | |
4489 | } | |
595213d4 | 4490 | if (align > old_alignment) |
4ad4eba5 AM |
4491 | h->root.u.c.p->alignment_power = align; |
4492 | else | |
4493 | h->root.u.c.p->alignment_power = old_alignment; | |
4494 | } | |
4495 | ||
66eb6687 | 4496 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4497 | { |
4f3fedcf AM |
4498 | /* Set a flag in the hash table entry indicating the type of |
4499 | reference or definition we just found. A dynamic symbol | |
4500 | is one which is referenced or defined by both a regular | |
4501 | object and a shared object. */ | |
4502 | bfd_boolean dynsym = FALSE; | |
4503 | ||
4504 | /* Plugin symbols aren't normal. Don't set def_regular or | |
4505 | ref_regular for them, or make them dynamic. */ | |
4506 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4507 | ; | |
4508 | else if (! dynamic) | |
4509 | { | |
4510 | if (! definition) | |
4511 | { | |
4512 | h->ref_regular = 1; | |
4513 | if (bind != STB_WEAK) | |
4514 | h->ref_regular_nonweak = 1; | |
4515 | } | |
4516 | else | |
4517 | { | |
4518 | h->def_regular = 1; | |
4519 | if (h->def_dynamic) | |
4520 | { | |
4521 | h->def_dynamic = 0; | |
4522 | h->ref_dynamic = 1; | |
4523 | } | |
4524 | } | |
4525 | ||
4526 | /* If the indirect symbol has been forced local, don't | |
4527 | make the real symbol dynamic. */ | |
4528 | if ((h == hi || !hi->forced_local) | |
0e1862bb | 4529 | && (bfd_link_dll (info) |
4f3fedcf AM |
4530 | || h->def_dynamic |
4531 | || h->ref_dynamic)) | |
4532 | dynsym = TRUE; | |
4533 | } | |
4534 | else | |
4535 | { | |
4536 | if (! definition) | |
4537 | { | |
4538 | h->ref_dynamic = 1; | |
4539 | hi->ref_dynamic = 1; | |
4540 | } | |
4541 | else | |
4542 | { | |
4543 | h->def_dynamic = 1; | |
4544 | hi->def_dynamic = 1; | |
4545 | } | |
4546 | ||
4547 | /* If the indirect symbol has been forced local, don't | |
4548 | make the real symbol dynamic. */ | |
4549 | if ((h == hi || !hi->forced_local) | |
4550 | && (h->def_regular | |
4551 | || h->ref_regular | |
4552 | || (h->u.weakdef != NULL | |
4553 | && ! new_weakdef | |
4554 | && h->u.weakdef->dynindx != -1))) | |
4555 | dynsym = TRUE; | |
4556 | } | |
4557 | ||
4558 | /* Check to see if we need to add an indirect symbol for | |
4559 | the default name. */ | |
4560 | if (definition | |
4561 | || (!override && h->root.type == bfd_link_hash_common)) | |
4562 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4563 | sec, value, &old_bfd, &dynsym)) | |
4564 | goto error_free_vers; | |
4ad4eba5 AM |
4565 | |
4566 | /* Check the alignment when a common symbol is involved. This | |
4567 | can change when a common symbol is overridden by a normal | |
4568 | definition or a common symbol is ignored due to the old | |
4569 | normal definition. We need to make sure the maximum | |
4570 | alignment is maintained. */ | |
a4d8e49b | 4571 | if ((old_alignment || common) |
4ad4eba5 AM |
4572 | && h->root.type != bfd_link_hash_common) |
4573 | { | |
4574 | unsigned int common_align; | |
4575 | unsigned int normal_align; | |
4576 | unsigned int symbol_align; | |
4577 | bfd *normal_bfd; | |
4578 | bfd *common_bfd; | |
4579 | ||
3a81e825 AM |
4580 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
4581 | || h->root.type == bfd_link_hash_defweak); | |
4582 | ||
4ad4eba5 AM |
4583 | symbol_align = ffs (h->root.u.def.value) - 1; |
4584 | if (h->root.u.def.section->owner != NULL | |
0616a280 AM |
4585 | && (h->root.u.def.section->owner->flags |
4586 | & (DYNAMIC | BFD_PLUGIN)) == 0) | |
4ad4eba5 AM |
4587 | { |
4588 | normal_align = h->root.u.def.section->alignment_power; | |
4589 | if (normal_align > symbol_align) | |
4590 | normal_align = symbol_align; | |
4591 | } | |
4592 | else | |
4593 | normal_align = symbol_align; | |
4594 | ||
4595 | if (old_alignment) | |
4596 | { | |
4597 | common_align = old_alignment; | |
4598 | common_bfd = old_bfd; | |
4599 | normal_bfd = abfd; | |
4600 | } | |
4601 | else | |
4602 | { | |
4603 | common_align = bfd_log2 (isym->st_value); | |
4604 | common_bfd = abfd; | |
4605 | normal_bfd = old_bfd; | |
4606 | } | |
4607 | ||
4608 | if (normal_align < common_align) | |
d07676f8 NC |
4609 | { |
4610 | /* PR binutils/2735 */ | |
4611 | if (normal_bfd == NULL) | |
4eca0228 | 4612 | _bfd_error_handler |
695344c0 | 4613 | /* xgettext:c-format */ |
4f3fedcf AM |
4614 | (_("Warning: alignment %u of common symbol `%s' in %B is" |
4615 | " greater than the alignment (%u) of its section %A"), | |
d07676f8 NC |
4616 | common_bfd, h->root.u.def.section, |
4617 | 1 << common_align, name, 1 << normal_align); | |
4618 | else | |
4eca0228 | 4619 | _bfd_error_handler |
695344c0 | 4620 | /* xgettext:c-format */ |
d07676f8 NC |
4621 | (_("Warning: alignment %u of symbol `%s' in %B" |
4622 | " is smaller than %u in %B"), | |
4623 | normal_bfd, common_bfd, | |
4624 | 1 << normal_align, name, 1 << common_align); | |
4625 | } | |
4ad4eba5 AM |
4626 | } |
4627 | ||
83ad0046 | 4628 | /* Remember the symbol size if it isn't undefined. */ |
3a81e825 AM |
4629 | if (isym->st_size != 0 |
4630 | && isym->st_shndx != SHN_UNDEF | |
4ad4eba5 AM |
4631 | && (definition || h->size == 0)) |
4632 | { | |
83ad0046 L |
4633 | if (h->size != 0 |
4634 | && h->size != isym->st_size | |
4635 | && ! size_change_ok) | |
4eca0228 | 4636 | _bfd_error_handler |
695344c0 | 4637 | /* xgettext:c-format */ |
d003868e AM |
4638 | (_("Warning: size of symbol `%s' changed" |
4639 | " from %lu in %B to %lu in %B"), | |
4640 | old_bfd, abfd, | |
4ad4eba5 | 4641 | name, (unsigned long) h->size, |
d003868e | 4642 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
4643 | |
4644 | h->size = isym->st_size; | |
4645 | } | |
4646 | ||
4647 | /* If this is a common symbol, then we always want H->SIZE | |
4648 | to be the size of the common symbol. The code just above | |
4649 | won't fix the size if a common symbol becomes larger. We | |
4650 | don't warn about a size change here, because that is | |
4f3fedcf | 4651 | covered by --warn-common. Allow changes between different |
fcb93ecf | 4652 | function types. */ |
4ad4eba5 AM |
4653 | if (h->root.type == bfd_link_hash_common) |
4654 | h->size = h->root.u.c.size; | |
4655 | ||
4656 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
37a9e49a L |
4657 | && ((definition && !new_weak) |
4658 | || (old_weak && h->root.type == bfd_link_hash_common) | |
4659 | || h->type == STT_NOTYPE)) | |
4ad4eba5 | 4660 | { |
2955ec4c L |
4661 | unsigned int type = ELF_ST_TYPE (isym->st_info); |
4662 | ||
4663 | /* Turn an IFUNC symbol from a DSO into a normal FUNC | |
4664 | symbol. */ | |
4665 | if (type == STT_GNU_IFUNC | |
4666 | && (abfd->flags & DYNAMIC) != 0) | |
4667 | type = STT_FUNC; | |
4ad4eba5 | 4668 | |
2955ec4c L |
4669 | if (h->type != type) |
4670 | { | |
4671 | if (h->type != STT_NOTYPE && ! type_change_ok) | |
695344c0 | 4672 | /* xgettext:c-format */ |
4eca0228 | 4673 | _bfd_error_handler |
2955ec4c L |
4674 | (_("Warning: type of symbol `%s' changed" |
4675 | " from %d to %d in %B"), | |
4676 | abfd, name, h->type, type); | |
4677 | ||
4678 | h->type = type; | |
4679 | } | |
4ad4eba5 AM |
4680 | } |
4681 | ||
54ac0771 | 4682 | /* Merge st_other field. */ |
b8417128 | 4683 | elf_merge_st_other (abfd, h, isym, sec, definition, dynamic); |
4ad4eba5 | 4684 | |
c3df8c14 | 4685 | /* We don't want to make debug symbol dynamic. */ |
0e1862bb L |
4686 | if (definition |
4687 | && (sec->flags & SEC_DEBUGGING) | |
4688 | && !bfd_link_relocatable (info)) | |
c3df8c14 AM |
4689 | dynsym = FALSE; |
4690 | ||
4f3fedcf AM |
4691 | /* Nor should we make plugin symbols dynamic. */ |
4692 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4693 | dynsym = FALSE; | |
4694 | ||
35fc36a8 | 4695 | if (definition) |
35399224 L |
4696 | { |
4697 | h->target_internal = isym->st_target_internal; | |
4698 | h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; | |
4699 | } | |
35fc36a8 | 4700 | |
4ad4eba5 AM |
4701 | if (definition && !dynamic) |
4702 | { | |
4703 | char *p = strchr (name, ELF_VER_CHR); | |
4704 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4705 | { | |
4706 | /* Queue non-default versions so that .symver x, x@FOO | |
4707 | aliases can be checked. */ | |
66eb6687 | 4708 | if (!nondeflt_vers) |
4ad4eba5 | 4709 | { |
66eb6687 AM |
4710 | amt = ((isymend - isym + 1) |
4711 | * sizeof (struct elf_link_hash_entry *)); | |
ca4be51c AM |
4712 | nondeflt_vers |
4713 | = (struct elf_link_hash_entry **) bfd_malloc (amt); | |
14b1c01e AM |
4714 | if (!nondeflt_vers) |
4715 | goto error_free_vers; | |
4ad4eba5 | 4716 | } |
66eb6687 | 4717 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4718 | } |
4719 | } | |
4720 | ||
4721 | if (dynsym && h->dynindx == -1) | |
4722 | { | |
c152c796 | 4723 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4724 | goto error_free_vers; |
f6e332e6 | 4725 | if (h->u.weakdef != NULL |
4ad4eba5 | 4726 | && ! new_weakdef |
f6e332e6 | 4727 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 4728 | { |
66eb6687 | 4729 | if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
4730 | goto error_free_vers; |
4731 | } | |
4732 | } | |
1f599d0e | 4733 | else if (h->dynindx != -1) |
4ad4eba5 AM |
4734 | /* If the symbol already has a dynamic index, but |
4735 | visibility says it should not be visible, turn it into | |
4736 | a local symbol. */ | |
4737 | switch (ELF_ST_VISIBILITY (h->other)) | |
4738 | { | |
4739 | case STV_INTERNAL: | |
4740 | case STV_HIDDEN: | |
4741 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4742 | dynsym = FALSE; | |
4743 | break; | |
4744 | } | |
4745 | ||
aef28989 L |
4746 | /* Don't add DT_NEEDED for references from the dummy bfd nor |
4747 | for unmatched symbol. */ | |
4ad4eba5 | 4748 | if (!add_needed |
aef28989 | 4749 | && matched |
4ad4eba5 | 4750 | && definition |
010e5ae2 | 4751 | && ((dynsym |
ffa9430d | 4752 | && h->ref_regular_nonweak |
4f3fedcf AM |
4753 | && (old_bfd == NULL |
4754 | || (old_bfd->flags & BFD_PLUGIN) == 0)) | |
ffa9430d | 4755 | || (h->ref_dynamic_nonweak |
010e5ae2 | 4756 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 |
7b15fa7a AM |
4757 | && !on_needed_list (elf_dt_name (abfd), |
4758 | htab->needed, NULL)))) | |
4ad4eba5 AM |
4759 | { |
4760 | int ret; | |
4761 | const char *soname = elf_dt_name (abfd); | |
4762 | ||
16e4ecc0 AM |
4763 | info->callbacks->minfo ("%!", soname, old_bfd, |
4764 | h->root.root.string); | |
4765 | ||
4ad4eba5 AM |
4766 | /* A symbol from a library loaded via DT_NEEDED of some |
4767 | other library is referenced by a regular object. | |
e56f61be | 4768 | Add a DT_NEEDED entry for it. Issue an error if |
b918acf9 NC |
4769 | --no-add-needed is used and the reference was not |
4770 | a weak one. */ | |
4f3fedcf | 4771 | if (old_bfd != NULL |
b918acf9 | 4772 | && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) |
e56f61be | 4773 | { |
4eca0228 | 4774 | _bfd_error_handler |
695344c0 | 4775 | /* xgettext:c-format */ |
3cbc5de0 | 4776 | (_("%B: undefined reference to symbol '%s'"), |
4f3fedcf | 4777 | old_bfd, name); |
ff5ac77b | 4778 | bfd_set_error (bfd_error_missing_dso); |
e56f61be L |
4779 | goto error_free_vers; |
4780 | } | |
4781 | ||
a50b1753 | 4782 | elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) |
ca4be51c | 4783 | (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); |
a5db907e | 4784 | |
4ad4eba5 | 4785 | add_needed = TRUE; |
7e9f0867 | 4786 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4787 | if (ret < 0) |
4788 | goto error_free_vers; | |
4789 | ||
4790 | BFD_ASSERT (ret == 0); | |
4791 | } | |
4792 | } | |
4793 | } | |
4794 | ||
66eb6687 AM |
4795 | if (extversym != NULL) |
4796 | { | |
4797 | free (extversym); | |
4798 | extversym = NULL; | |
4799 | } | |
4800 | ||
4801 | if (isymbuf != NULL) | |
4802 | { | |
4803 | free (isymbuf); | |
4804 | isymbuf = NULL; | |
4805 | } | |
4806 | ||
4807 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4808 | { | |
4809 | unsigned int i; | |
4810 | ||
4811 | /* Restore the symbol table. */ | |
f45794cb AM |
4812 | old_ent = (char *) old_tab + tabsize; |
4813 | memset (elf_sym_hashes (abfd), 0, | |
4814 | extsymcount * sizeof (struct elf_link_hash_entry *)); | |
4f87808c AM |
4815 | htab->root.table.table = old_table; |
4816 | htab->root.table.size = old_size; | |
4817 | htab->root.table.count = old_count; | |
66eb6687 | 4818 | memcpy (htab->root.table.table, old_tab, tabsize); |
66eb6687 AM |
4819 | htab->root.undefs = old_undefs; |
4820 | htab->root.undefs_tail = old_undefs_tail; | |
5b677558 AM |
4821 | _bfd_elf_strtab_restore (htab->dynstr, old_strtab); |
4822 | free (old_strtab); | |
4823 | old_strtab = NULL; | |
66eb6687 AM |
4824 | for (i = 0; i < htab->root.table.size; i++) |
4825 | { | |
4826 | struct bfd_hash_entry *p; | |
4827 | struct elf_link_hash_entry *h; | |
3e0882af L |
4828 | bfd_size_type size; |
4829 | unsigned int alignment_power; | |
66eb6687 AM |
4830 | |
4831 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4832 | { | |
4833 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
4834 | if (h->root.type == bfd_link_hash_warning) |
4835 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2de92251 | 4836 | |
3e0882af L |
4837 | /* Preserve the maximum alignment and size for common |
4838 | symbols even if this dynamic lib isn't on DT_NEEDED | |
a4542f1b | 4839 | since it can still be loaded at run time by another |
3e0882af L |
4840 | dynamic lib. */ |
4841 | if (h->root.type == bfd_link_hash_common) | |
4842 | { | |
4843 | size = h->root.u.c.size; | |
4844 | alignment_power = h->root.u.c.p->alignment_power; | |
4845 | } | |
4846 | else | |
4847 | { | |
4848 | size = 0; | |
4849 | alignment_power = 0; | |
4850 | } | |
66eb6687 AM |
4851 | memcpy (p, old_ent, htab->root.table.entsize); |
4852 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4853 | h = (struct elf_link_hash_entry *) p; |
4854 | if (h->root.type == bfd_link_hash_warning) | |
4855 | { | |
4856 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
4857 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
a4542f1b | 4858 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
2de92251 | 4859 | } |
a4542f1b | 4860 | if (h->root.type == bfd_link_hash_common) |
3e0882af L |
4861 | { |
4862 | if (size > h->root.u.c.size) | |
4863 | h->root.u.c.size = size; | |
4864 | if (alignment_power > h->root.u.c.p->alignment_power) | |
4865 | h->root.u.c.p->alignment_power = alignment_power; | |
4866 | } | |
66eb6687 AM |
4867 | } |
4868 | } | |
4869 | ||
5061a885 AM |
4870 | /* Make a special call to the linker "notice" function to |
4871 | tell it that symbols added for crefs may need to be removed. */ | |
e5034e59 | 4872 | if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) |
9af2a943 | 4873 | goto error_free_vers; |
5061a885 | 4874 | |
66eb6687 AM |
4875 | free (old_tab); |
4876 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
4877 | alloc_mark); | |
4878 | if (nondeflt_vers != NULL) | |
4879 | free (nondeflt_vers); | |
4880 | return TRUE; | |
4881 | } | |
2de92251 | 4882 | |
66eb6687 AM |
4883 | if (old_tab != NULL) |
4884 | { | |
e5034e59 | 4885 | if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) |
9af2a943 | 4886 | goto error_free_vers; |
66eb6687 AM |
4887 | free (old_tab); |
4888 | old_tab = NULL; | |
4889 | } | |
4890 | ||
c6e8a9a8 L |
4891 | /* Now that all the symbols from this input file are created, if |
4892 | not performing a relocatable link, handle .symver foo, foo@BAR | |
4893 | such that any relocs against foo become foo@BAR. */ | |
0e1862bb | 4894 | if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) |
4ad4eba5 | 4895 | { |
ef53be89 | 4896 | size_t cnt, symidx; |
4ad4eba5 AM |
4897 | |
4898 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
4899 | { | |
4900 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
4901 | char *shortname, *p; | |
4902 | ||
4903 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
4904 | if (p == NULL | |
4905 | || (h->root.type != bfd_link_hash_defined | |
4906 | && h->root.type != bfd_link_hash_defweak)) | |
4907 | continue; | |
4908 | ||
4909 | amt = p - h->root.root.string; | |
a50b1753 | 4910 | shortname = (char *) bfd_malloc (amt + 1); |
14b1c01e AM |
4911 | if (!shortname) |
4912 | goto error_free_vers; | |
4ad4eba5 AM |
4913 | memcpy (shortname, h->root.root.string, amt); |
4914 | shortname[amt] = '\0'; | |
4915 | ||
4916 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 4917 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
4918 | FALSE, FALSE, FALSE); |
4919 | if (hi != NULL | |
4920 | && hi->root.type == h->root.type | |
4921 | && hi->root.u.def.value == h->root.u.def.value | |
4922 | && hi->root.u.def.section == h->root.u.def.section) | |
4923 | { | |
4924 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4925 | hi->root.type = bfd_link_hash_indirect; | |
4926 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 4927 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
4928 | sym_hash = elf_sym_hashes (abfd); |
4929 | if (sym_hash) | |
4930 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4931 | if (sym_hash[symidx] == hi) | |
4932 | { | |
4933 | sym_hash[symidx] = h; | |
4934 | break; | |
4935 | } | |
4936 | } | |
4937 | free (shortname); | |
4938 | } | |
4939 | free (nondeflt_vers); | |
4940 | nondeflt_vers = NULL; | |
4941 | } | |
4942 | ||
4ad4eba5 AM |
4943 | /* Now set the weakdefs field correctly for all the weak defined |
4944 | symbols we found. The only way to do this is to search all the | |
4945 | symbols. Since we only need the information for non functions in | |
4946 | dynamic objects, that's the only time we actually put anything on | |
4947 | the list WEAKS. We need this information so that if a regular | |
4948 | object refers to a symbol defined weakly in a dynamic object, the | |
4949 | real symbol in the dynamic object is also put in the dynamic | |
4950 | symbols; we also must arrange for both symbols to point to the | |
4951 | same memory location. We could handle the general case of symbol | |
4952 | aliasing, but a general symbol alias can only be generated in | |
4953 | assembler code, handling it correctly would be very time | |
4954 | consuming, and other ELF linkers don't handle general aliasing | |
4955 | either. */ | |
4956 | if (weaks != NULL) | |
4957 | { | |
4958 | struct elf_link_hash_entry **hpp; | |
4959 | struct elf_link_hash_entry **hppend; | |
4960 | struct elf_link_hash_entry **sorted_sym_hash; | |
4961 | struct elf_link_hash_entry *h; | |
4962 | size_t sym_count; | |
4963 | ||
4964 | /* Since we have to search the whole symbol list for each weak | |
4965 | defined symbol, search time for N weak defined symbols will be | |
4966 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
ef53be89 AM |
4967 | amt = extsymcount; |
4968 | amt *= sizeof (struct elf_link_hash_entry *); | |
a50b1753 | 4969 | sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); |
4ad4eba5 AM |
4970 | if (sorted_sym_hash == NULL) |
4971 | goto error_return; | |
4972 | sym_hash = sorted_sym_hash; | |
4973 | hpp = elf_sym_hashes (abfd); | |
4974 | hppend = hpp + extsymcount; | |
4975 | sym_count = 0; | |
4976 | for (; hpp < hppend; hpp++) | |
4977 | { | |
4978 | h = *hpp; | |
4979 | if (h != NULL | |
4980 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 4981 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
4982 | { |
4983 | *sym_hash = h; | |
4984 | sym_hash++; | |
4985 | sym_count++; | |
4986 | } | |
4987 | } | |
4988 | ||
4989 | qsort (sorted_sym_hash, sym_count, | |
4990 | sizeof (struct elf_link_hash_entry *), | |
4991 | elf_sort_symbol); | |
4992 | ||
4993 | while (weaks != NULL) | |
4994 | { | |
4995 | struct elf_link_hash_entry *hlook; | |
4996 | asection *slook; | |
4997 | bfd_vma vlook; | |
ed54588d | 4998 | size_t i, j, idx = 0; |
4ad4eba5 AM |
4999 | |
5000 | hlook = weaks; | |
f6e332e6 AM |
5001 | weaks = hlook->u.weakdef; |
5002 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
5003 | |
5004 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
5005 | || hlook->root.type == bfd_link_hash_defweak | |
5006 | || hlook->root.type == bfd_link_hash_common | |
5007 | || hlook->root.type == bfd_link_hash_indirect); | |
5008 | slook = hlook->root.u.def.section; | |
5009 | vlook = hlook->root.u.def.value; | |
5010 | ||
4ad4eba5 AM |
5011 | i = 0; |
5012 | j = sym_count; | |
14160578 | 5013 | while (i != j) |
4ad4eba5 AM |
5014 | { |
5015 | bfd_signed_vma vdiff; | |
5016 | idx = (i + j) / 2; | |
14160578 | 5017 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5018 | vdiff = vlook - h->root.u.def.value; |
5019 | if (vdiff < 0) | |
5020 | j = idx; | |
5021 | else if (vdiff > 0) | |
5022 | i = idx + 1; | |
5023 | else | |
5024 | { | |
d3435ae8 | 5025 | int sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
5026 | if (sdiff < 0) |
5027 | j = idx; | |
5028 | else if (sdiff > 0) | |
5029 | i = idx + 1; | |
5030 | else | |
14160578 | 5031 | break; |
4ad4eba5 AM |
5032 | } |
5033 | } | |
5034 | ||
5035 | /* We didn't find a value/section match. */ | |
14160578 | 5036 | if (i == j) |
4ad4eba5 AM |
5037 | continue; |
5038 | ||
14160578 AM |
5039 | /* With multiple aliases, or when the weak symbol is already |
5040 | strongly defined, we have multiple matching symbols and | |
5041 | the binary search above may land on any of them. Step | |
5042 | one past the matching symbol(s). */ | |
5043 | while (++idx != j) | |
5044 | { | |
5045 | h = sorted_sym_hash[idx]; | |
5046 | if (h->root.u.def.section != slook | |
5047 | || h->root.u.def.value != vlook) | |
5048 | break; | |
5049 | } | |
5050 | ||
5051 | /* Now look back over the aliases. Since we sorted by size | |
5052 | as well as value and section, we'll choose the one with | |
5053 | the largest size. */ | |
5054 | while (idx-- != i) | |
4ad4eba5 | 5055 | { |
14160578 | 5056 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5057 | |
5058 | /* Stop if value or section doesn't match. */ | |
14160578 AM |
5059 | if (h->root.u.def.section != slook |
5060 | || h->root.u.def.value != vlook) | |
4ad4eba5 AM |
5061 | break; |
5062 | else if (h != hlook) | |
5063 | { | |
f6e332e6 | 5064 | hlook->u.weakdef = h; |
4ad4eba5 AM |
5065 | |
5066 | /* If the weak definition is in the list of dynamic | |
5067 | symbols, make sure the real definition is put | |
5068 | there as well. */ | |
5069 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
5070 | { | |
c152c796 | 5071 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4dd07732 AM |
5072 | { |
5073 | err_free_sym_hash: | |
5074 | free (sorted_sym_hash); | |
5075 | goto error_return; | |
5076 | } | |
4ad4eba5 AM |
5077 | } |
5078 | ||
5079 | /* If the real definition is in the list of dynamic | |
5080 | symbols, make sure the weak definition is put | |
5081 | there as well. If we don't do this, then the | |
5082 | dynamic loader might not merge the entries for the | |
5083 | real definition and the weak definition. */ | |
5084 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
5085 | { | |
c152c796 | 5086 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4dd07732 | 5087 | goto err_free_sym_hash; |
4ad4eba5 AM |
5088 | } |
5089 | break; | |
5090 | } | |
5091 | } | |
5092 | } | |
5093 | ||
5094 | free (sorted_sym_hash); | |
5095 | } | |
5096 | ||
33177bb1 AM |
5097 | if (bed->check_directives |
5098 | && !(*bed->check_directives) (abfd, info)) | |
5099 | return FALSE; | |
85fbca6a | 5100 | |
d9689752 L |
5101 | if (!info->check_relocs_after_open_input |
5102 | && !_bfd_elf_link_check_relocs (abfd, info)) | |
5103 | return FALSE; | |
4ad4eba5 AM |
5104 | |
5105 | /* If this is a non-traditional link, try to optimize the handling | |
5106 | of the .stab/.stabstr sections. */ | |
5107 | if (! dynamic | |
5108 | && ! info->traditional_format | |
66eb6687 | 5109 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
5110 | && (info->strip != strip_all && info->strip != strip_debugger)) |
5111 | { | |
5112 | asection *stabstr; | |
5113 | ||
5114 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
5115 | if (stabstr != NULL) | |
5116 | { | |
5117 | bfd_size_type string_offset = 0; | |
5118 | asection *stab; | |
5119 | ||
5120 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 5121 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
5122 | && (!stab->name[5] || |
5123 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
5124 | && (stab->flags & SEC_MERGE) == 0 | |
5125 | && !bfd_is_abs_section (stab->output_section)) | |
5126 | { | |
5127 | struct bfd_elf_section_data *secdata; | |
5128 | ||
5129 | secdata = elf_section_data (stab); | |
66eb6687 AM |
5130 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
5131 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
5132 | &string_offset)) |
5133 | goto error_return; | |
5134 | if (secdata->sec_info) | |
dbaa2011 | 5135 | stab->sec_info_type = SEC_INFO_TYPE_STABS; |
4ad4eba5 AM |
5136 | } |
5137 | } | |
5138 | } | |
5139 | ||
66eb6687 | 5140 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
5141 | { |
5142 | /* Add this bfd to the loaded list. */ | |
5143 | struct elf_link_loaded_list *n; | |
5144 | ||
ca4be51c | 5145 | n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); |
4ad4eba5 AM |
5146 | if (n == NULL) |
5147 | goto error_return; | |
5148 | n->abfd = abfd; | |
66eb6687 AM |
5149 | n->next = htab->loaded; |
5150 | htab->loaded = n; | |
4ad4eba5 AM |
5151 | } |
5152 | ||
5153 | return TRUE; | |
5154 | ||
5155 | error_free_vers: | |
66eb6687 AM |
5156 | if (old_tab != NULL) |
5157 | free (old_tab); | |
5b677558 AM |
5158 | if (old_strtab != NULL) |
5159 | free (old_strtab); | |
4ad4eba5 AM |
5160 | if (nondeflt_vers != NULL) |
5161 | free (nondeflt_vers); | |
5162 | if (extversym != NULL) | |
5163 | free (extversym); | |
5164 | error_free_sym: | |
5165 | if (isymbuf != NULL) | |
5166 | free (isymbuf); | |
5167 | error_return: | |
5168 | return FALSE; | |
5169 | } | |
5170 | ||
8387904d AM |
5171 | /* Return the linker hash table entry of a symbol that might be |
5172 | satisfied by an archive symbol. Return -1 on error. */ | |
5173 | ||
5174 | struct elf_link_hash_entry * | |
5175 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
5176 | struct bfd_link_info *info, | |
5177 | const char *name) | |
5178 | { | |
5179 | struct elf_link_hash_entry *h; | |
5180 | char *p, *copy; | |
5181 | size_t len, first; | |
5182 | ||
2a41f396 | 5183 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); |
8387904d AM |
5184 | if (h != NULL) |
5185 | return h; | |
5186 | ||
5187 | /* If this is a default version (the name contains @@), look up the | |
5188 | symbol again with only one `@' as well as without the version. | |
5189 | The effect is that references to the symbol with and without the | |
5190 | version will be matched by the default symbol in the archive. */ | |
5191 | ||
5192 | p = strchr (name, ELF_VER_CHR); | |
5193 | if (p == NULL || p[1] != ELF_VER_CHR) | |
5194 | return h; | |
5195 | ||
5196 | /* First check with only one `@'. */ | |
5197 | len = strlen (name); | |
a50b1753 | 5198 | copy = (char *) bfd_alloc (abfd, len); |
8387904d AM |
5199 | if (copy == NULL) |
5200 | return (struct elf_link_hash_entry *) 0 - 1; | |
5201 | ||
5202 | first = p - name + 1; | |
5203 | memcpy (copy, name, first); | |
5204 | memcpy (copy + first, name + first + 1, len - first); | |
5205 | ||
2a41f396 | 5206 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); |
8387904d AM |
5207 | if (h == NULL) |
5208 | { | |
5209 | /* We also need to check references to the symbol without the | |
5210 | version. */ | |
5211 | copy[first - 1] = '\0'; | |
5212 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
2a41f396 | 5213 | FALSE, FALSE, TRUE); |
8387904d AM |
5214 | } |
5215 | ||
5216 | bfd_release (abfd, copy); | |
5217 | return h; | |
5218 | } | |
5219 | ||
0ad989f9 | 5220 | /* Add symbols from an ELF archive file to the linker hash table. We |
13e570f8 AM |
5221 | don't use _bfd_generic_link_add_archive_symbols because we need to |
5222 | handle versioned symbols. | |
0ad989f9 L |
5223 | |
5224 | Fortunately, ELF archive handling is simpler than that done by | |
5225 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
5226 | oddities. In ELF, if we find a symbol in the archive map, and the | |
5227 | symbol is currently undefined, we know that we must pull in that | |
5228 | object file. | |
5229 | ||
5230 | Unfortunately, we do have to make multiple passes over the symbol | |
5231 | table until nothing further is resolved. */ | |
5232 | ||
4ad4eba5 AM |
5233 | static bfd_boolean |
5234 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
5235 | { |
5236 | symindex c; | |
13e570f8 | 5237 | unsigned char *included = NULL; |
0ad989f9 L |
5238 | carsym *symdefs; |
5239 | bfd_boolean loop; | |
5240 | bfd_size_type amt; | |
8387904d AM |
5241 | const struct elf_backend_data *bed; |
5242 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
5243 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
5244 | |
5245 | if (! bfd_has_map (abfd)) | |
5246 | { | |
5247 | /* An empty archive is a special case. */ | |
5248 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
5249 | return TRUE; | |
5250 | bfd_set_error (bfd_error_no_armap); | |
5251 | return FALSE; | |
5252 | } | |
5253 | ||
5254 | /* Keep track of all symbols we know to be already defined, and all | |
5255 | files we know to be already included. This is to speed up the | |
5256 | second and subsequent passes. */ | |
5257 | c = bfd_ardata (abfd)->symdef_count; | |
5258 | if (c == 0) | |
5259 | return TRUE; | |
5260 | amt = c; | |
13e570f8 AM |
5261 | amt *= sizeof (*included); |
5262 | included = (unsigned char *) bfd_zmalloc (amt); | |
5263 | if (included == NULL) | |
5264 | return FALSE; | |
0ad989f9 L |
5265 | |
5266 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
5267 | bed = get_elf_backend_data (abfd); |
5268 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
5269 | |
5270 | do | |
5271 | { | |
5272 | file_ptr last; | |
5273 | symindex i; | |
5274 | carsym *symdef; | |
5275 | carsym *symdefend; | |
5276 | ||
5277 | loop = FALSE; | |
5278 | last = -1; | |
5279 | ||
5280 | symdef = symdefs; | |
5281 | symdefend = symdef + c; | |
5282 | for (i = 0; symdef < symdefend; symdef++, i++) | |
5283 | { | |
5284 | struct elf_link_hash_entry *h; | |
5285 | bfd *element; | |
5286 | struct bfd_link_hash_entry *undefs_tail; | |
5287 | symindex mark; | |
5288 | ||
13e570f8 | 5289 | if (included[i]) |
0ad989f9 L |
5290 | continue; |
5291 | if (symdef->file_offset == last) | |
5292 | { | |
5293 | included[i] = TRUE; | |
5294 | continue; | |
5295 | } | |
5296 | ||
8387904d AM |
5297 | h = archive_symbol_lookup (abfd, info, symdef->name); |
5298 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
5299 | goto error_return; | |
0ad989f9 L |
5300 | |
5301 | if (h == NULL) | |
5302 | continue; | |
5303 | ||
5304 | if (h->root.type == bfd_link_hash_common) | |
5305 | { | |
5306 | /* We currently have a common symbol. The archive map contains | |
5307 | a reference to this symbol, so we may want to include it. We | |
5308 | only want to include it however, if this archive element | |
5309 | contains a definition of the symbol, not just another common | |
5310 | declaration of it. | |
5311 | ||
5312 | Unfortunately some archivers (including GNU ar) will put | |
5313 | declarations of common symbols into their archive maps, as | |
5314 | well as real definitions, so we cannot just go by the archive | |
5315 | map alone. Instead we must read in the element's symbol | |
5316 | table and check that to see what kind of symbol definition | |
5317 | this is. */ | |
5318 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
5319 | continue; | |
5320 | } | |
5321 | else if (h->root.type != bfd_link_hash_undefined) | |
5322 | { | |
5323 | if (h->root.type != bfd_link_hash_undefweak) | |
13e570f8 AM |
5324 | /* Symbol must be defined. Don't check it again. */ |
5325 | included[i] = TRUE; | |
0ad989f9 L |
5326 | continue; |
5327 | } | |
5328 | ||
5329 | /* We need to include this archive member. */ | |
5330 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
5331 | if (element == NULL) | |
5332 | goto error_return; | |
5333 | ||
5334 | if (! bfd_check_format (element, bfd_object)) | |
5335 | goto error_return; | |
5336 | ||
0ad989f9 L |
5337 | undefs_tail = info->hash->undefs_tail; |
5338 | ||
0e144ba7 AM |
5339 | if (!(*info->callbacks |
5340 | ->add_archive_element) (info, element, symdef->name, &element)) | |
b95a0a31 | 5341 | continue; |
0e144ba7 | 5342 | if (!bfd_link_add_symbols (element, info)) |
0ad989f9 L |
5343 | goto error_return; |
5344 | ||
5345 | /* If there are any new undefined symbols, we need to make | |
5346 | another pass through the archive in order to see whether | |
5347 | they can be defined. FIXME: This isn't perfect, because | |
5348 | common symbols wind up on undefs_tail and because an | |
5349 | undefined symbol which is defined later on in this pass | |
5350 | does not require another pass. This isn't a bug, but it | |
5351 | does make the code less efficient than it could be. */ | |
5352 | if (undefs_tail != info->hash->undefs_tail) | |
5353 | loop = TRUE; | |
5354 | ||
5355 | /* Look backward to mark all symbols from this object file | |
5356 | which we have already seen in this pass. */ | |
5357 | mark = i; | |
5358 | do | |
5359 | { | |
5360 | included[mark] = TRUE; | |
5361 | if (mark == 0) | |
5362 | break; | |
5363 | --mark; | |
5364 | } | |
5365 | while (symdefs[mark].file_offset == symdef->file_offset); | |
5366 | ||
5367 | /* We mark subsequent symbols from this object file as we go | |
5368 | on through the loop. */ | |
5369 | last = symdef->file_offset; | |
5370 | } | |
5371 | } | |
5372 | while (loop); | |
5373 | ||
0ad989f9 L |
5374 | free (included); |
5375 | ||
5376 | return TRUE; | |
5377 | ||
5378 | error_return: | |
0ad989f9 L |
5379 | if (included != NULL) |
5380 | free (included); | |
5381 | return FALSE; | |
5382 | } | |
4ad4eba5 AM |
5383 | |
5384 | /* Given an ELF BFD, add symbols to the global hash table as | |
5385 | appropriate. */ | |
5386 | ||
5387 | bfd_boolean | |
5388 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
5389 | { | |
5390 | switch (bfd_get_format (abfd)) | |
5391 | { | |
5392 | case bfd_object: | |
5393 | return elf_link_add_object_symbols (abfd, info); | |
5394 | case bfd_archive: | |
5395 | return elf_link_add_archive_symbols (abfd, info); | |
5396 | default: | |
5397 | bfd_set_error (bfd_error_wrong_format); | |
5398 | return FALSE; | |
5399 | } | |
5400 | } | |
5a580b3a | 5401 | \f |
14b1c01e AM |
5402 | struct hash_codes_info |
5403 | { | |
5404 | unsigned long *hashcodes; | |
5405 | bfd_boolean error; | |
5406 | }; | |
a0c8462f | 5407 | |
5a580b3a AM |
5408 | /* This function will be called though elf_link_hash_traverse to store |
5409 | all hash value of the exported symbols in an array. */ | |
5410 | ||
5411 | static bfd_boolean | |
5412 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5413 | { | |
a50b1753 | 5414 | struct hash_codes_info *inf = (struct hash_codes_info *) data; |
5a580b3a | 5415 | const char *name; |
5a580b3a AM |
5416 | unsigned long ha; |
5417 | char *alc = NULL; | |
5418 | ||
5a580b3a AM |
5419 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5420 | if (h->dynindx == -1) | |
5421 | return TRUE; | |
5422 | ||
5423 | name = h->root.root.string; | |
422f1182 | 5424 | if (h->versioned >= versioned) |
5a580b3a | 5425 | { |
422f1182 L |
5426 | char *p = strchr (name, ELF_VER_CHR); |
5427 | if (p != NULL) | |
14b1c01e | 5428 | { |
422f1182 L |
5429 | alc = (char *) bfd_malloc (p - name + 1); |
5430 | if (alc == NULL) | |
5431 | { | |
5432 | inf->error = TRUE; | |
5433 | return FALSE; | |
5434 | } | |
5435 | memcpy (alc, name, p - name); | |
5436 | alc[p - name] = '\0'; | |
5437 | name = alc; | |
14b1c01e | 5438 | } |
5a580b3a AM |
5439 | } |
5440 | ||
5441 | /* Compute the hash value. */ | |
5442 | ha = bfd_elf_hash (name); | |
5443 | ||
5444 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 5445 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
5446 | |
5447 | /* And store it in the struct so that we can put it in the hash table | |
5448 | later. */ | |
f6e332e6 | 5449 | h->u.elf_hash_value = ha; |
5a580b3a AM |
5450 | |
5451 | if (alc != NULL) | |
5452 | free (alc); | |
5453 | ||
5454 | return TRUE; | |
5455 | } | |
5456 | ||
fdc90cb4 JJ |
5457 | struct collect_gnu_hash_codes |
5458 | { | |
5459 | bfd *output_bfd; | |
5460 | const struct elf_backend_data *bed; | |
5461 | unsigned long int nsyms; | |
5462 | unsigned long int maskbits; | |
5463 | unsigned long int *hashcodes; | |
5464 | unsigned long int *hashval; | |
5465 | unsigned long int *indx; | |
5466 | unsigned long int *counts; | |
5467 | bfd_vma *bitmask; | |
5468 | bfd_byte *contents; | |
5469 | long int min_dynindx; | |
5470 | unsigned long int bucketcount; | |
5471 | unsigned long int symindx; | |
5472 | long int local_indx; | |
5473 | long int shift1, shift2; | |
5474 | unsigned long int mask; | |
14b1c01e | 5475 | bfd_boolean error; |
fdc90cb4 JJ |
5476 | }; |
5477 | ||
5478 | /* This function will be called though elf_link_hash_traverse to store | |
5479 | all hash value of the exported symbols in an array. */ | |
5480 | ||
5481 | static bfd_boolean | |
5482 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5483 | { | |
a50b1753 | 5484 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 | 5485 | const char *name; |
fdc90cb4 JJ |
5486 | unsigned long ha; |
5487 | char *alc = NULL; | |
5488 | ||
fdc90cb4 JJ |
5489 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5490 | if (h->dynindx == -1) | |
5491 | return TRUE; | |
5492 | ||
5493 | /* Ignore also local symbols and undefined symbols. */ | |
5494 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5495 | return TRUE; | |
5496 | ||
5497 | name = h->root.root.string; | |
422f1182 | 5498 | if (h->versioned >= versioned) |
fdc90cb4 | 5499 | { |
422f1182 L |
5500 | char *p = strchr (name, ELF_VER_CHR); |
5501 | if (p != NULL) | |
14b1c01e | 5502 | { |
422f1182 L |
5503 | alc = (char *) bfd_malloc (p - name + 1); |
5504 | if (alc == NULL) | |
5505 | { | |
5506 | s->error = TRUE; | |
5507 | return FALSE; | |
5508 | } | |
5509 | memcpy (alc, name, p - name); | |
5510 | alc[p - name] = '\0'; | |
5511 | name = alc; | |
14b1c01e | 5512 | } |
fdc90cb4 JJ |
5513 | } |
5514 | ||
5515 | /* Compute the hash value. */ | |
5516 | ha = bfd_elf_gnu_hash (name); | |
5517 | ||
5518 | /* Store the found hash value in the array for compute_bucket_count, | |
5519 | and also for .dynsym reordering purposes. */ | |
5520 | s->hashcodes[s->nsyms] = ha; | |
5521 | s->hashval[h->dynindx] = ha; | |
5522 | ++s->nsyms; | |
5523 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5524 | s->min_dynindx = h->dynindx; | |
5525 | ||
5526 | if (alc != NULL) | |
5527 | free (alc); | |
5528 | ||
5529 | return TRUE; | |
5530 | } | |
5531 | ||
5532 | /* This function will be called though elf_link_hash_traverse to do | |
5533 | final dynaminc symbol renumbering. */ | |
5534 | ||
5535 | static bfd_boolean | |
5536 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5537 | { | |
a50b1753 | 5538 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
5539 | unsigned long int bucket; |
5540 | unsigned long int val; | |
5541 | ||
fdc90cb4 JJ |
5542 | /* Ignore indirect symbols. */ |
5543 | if (h->dynindx == -1) | |
5544 | return TRUE; | |
5545 | ||
5546 | /* Ignore also local symbols and undefined symbols. */ | |
5547 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5548 | { | |
5549 | if (h->dynindx >= s->min_dynindx) | |
5550 | h->dynindx = s->local_indx++; | |
5551 | return TRUE; | |
5552 | } | |
5553 | ||
5554 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5555 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5556 | & ((s->maskbits >> s->shift1) - 1); | |
5557 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5558 | s->bitmask[val] | |
5559 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5560 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5561 | if (s->counts[bucket] == 1) | |
5562 | /* Last element terminates the chain. */ | |
5563 | val |= 1; | |
5564 | bfd_put_32 (s->output_bfd, val, | |
5565 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5566 | --s->counts[bucket]; | |
5567 | h->dynindx = s->indx[bucket]++; | |
5568 | return TRUE; | |
5569 | } | |
5570 | ||
5571 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5572 | ||
5573 | bfd_boolean | |
5574 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5575 | { | |
5576 | return !(h->forced_local | |
5577 | || h->root.type == bfd_link_hash_undefined | |
5578 | || h->root.type == bfd_link_hash_undefweak | |
5579 | || ((h->root.type == bfd_link_hash_defined | |
5580 | || h->root.type == bfd_link_hash_defweak) | |
5581 | && h->root.u.def.section->output_section == NULL)); | |
5582 | } | |
5583 | ||
5a580b3a AM |
5584 | /* Array used to determine the number of hash table buckets to use |
5585 | based on the number of symbols there are. If there are fewer than | |
5586 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5587 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5588 | than 32771 buckets. */ | |
5589 | ||
5590 | static const size_t elf_buckets[] = | |
5591 | { | |
5592 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5593 | 16411, 32771, 0 | |
5594 | }; | |
5595 | ||
5596 | /* Compute bucket count for hashing table. We do not use a static set | |
5597 | of possible tables sizes anymore. Instead we determine for all | |
5598 | possible reasonable sizes of the table the outcome (i.e., the | |
5599 | number of collisions etc) and choose the best solution. The | |
5600 | weighting functions are not too simple to allow the table to grow | |
5601 | without bounds. Instead one of the weighting factors is the size. | |
5602 | Therefore the result is always a good payoff between few collisions | |
5603 | (= short chain lengths) and table size. */ | |
5604 | static size_t | |
b20dd2ce | 5605 | compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
d40f3da9 AM |
5606 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, |
5607 | unsigned long int nsyms, | |
5608 | int gnu_hash) | |
5a580b3a | 5609 | { |
5a580b3a | 5610 | size_t best_size = 0; |
5a580b3a | 5611 | unsigned long int i; |
5a580b3a | 5612 | |
5a580b3a AM |
5613 | /* We have a problem here. The following code to optimize the table |
5614 | size requires an integer type with more the 32 bits. If | |
5615 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5616 | #ifdef BFD_HOST_U_64_BIT | |
5617 | if (info->optimize) | |
5618 | { | |
5a580b3a AM |
5619 | size_t minsize; |
5620 | size_t maxsize; | |
5621 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a | 5622 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 5623 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 5624 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 5625 | unsigned long int *counts; |
d40f3da9 | 5626 | bfd_size_type amt; |
0883b6e0 | 5627 | unsigned int no_improvement_count = 0; |
5a580b3a AM |
5628 | |
5629 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5630 | that the hashing table must at least have NSYMS/4 and at most | |
5631 | 2*NSYMS buckets. */ | |
5632 | minsize = nsyms / 4; | |
5633 | if (minsize == 0) | |
5634 | minsize = 1; | |
5635 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5636 | if (gnu_hash) |
5637 | { | |
5638 | if (minsize < 2) | |
5639 | minsize = 2; | |
5640 | if ((best_size & 31) == 0) | |
5641 | ++best_size; | |
5642 | } | |
5a580b3a AM |
5643 | |
5644 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5645 | since the size could be large. */ | |
5646 | amt = maxsize; | |
5647 | amt *= sizeof (unsigned long int); | |
a50b1753 | 5648 | counts = (unsigned long int *) bfd_malloc (amt); |
5a580b3a | 5649 | if (counts == NULL) |
fdc90cb4 | 5650 | return 0; |
5a580b3a AM |
5651 | |
5652 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5653 | minimal chain length. The minor criteria is (of course) the size | |
5654 | of the table. */ | |
5655 | for (i = minsize; i < maxsize; ++i) | |
5656 | { | |
5657 | /* Walk through the array of hashcodes and count the collisions. */ | |
5658 | BFD_HOST_U_64_BIT max; | |
5659 | unsigned long int j; | |
5660 | unsigned long int fact; | |
5661 | ||
fdc90cb4 JJ |
5662 | if (gnu_hash && (i & 31) == 0) |
5663 | continue; | |
5664 | ||
5a580b3a AM |
5665 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5666 | ||
5667 | /* Determine how often each hash bucket is used. */ | |
5668 | for (j = 0; j < nsyms; ++j) | |
5669 | ++counts[hashcodes[j] % i]; | |
5670 | ||
5671 | /* For the weight function we need some information about the | |
5672 | pagesize on the target. This is information need not be 100% | |
5673 | accurate. Since this information is not available (so far) we | |
5674 | define it here to a reasonable default value. If it is crucial | |
5675 | to have a better value some day simply define this value. */ | |
5676 | # ifndef BFD_TARGET_PAGESIZE | |
5677 | # define BFD_TARGET_PAGESIZE (4096) | |
5678 | # endif | |
5679 | ||
fdc90cb4 JJ |
5680 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5681 | and the chains. */ | |
5682 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
5683 | |
5684 | # if 1 | |
5685 | /* Variant 1: optimize for short chains. We add the squares | |
5686 | of all the chain lengths (which favors many small chain | |
5687 | over a few long chains). */ | |
5688 | for (j = 0; j < i; ++j) | |
5689 | max += counts[j] * counts[j]; | |
5690 | ||
5691 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 5692 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5693 | max *= fact * fact; |
5694 | # else | |
5695 | /* Variant 2: Optimize a lot more for small table. Here we | |
5696 | also add squares of the size but we also add penalties for | |
5697 | empty slots (the +1 term). */ | |
5698 | for (j = 0; j < i; ++j) | |
5699 | max += (1 + counts[j]) * (1 + counts[j]); | |
5700 | ||
5701 | /* The overall size of the table is considered, but not as | |
5702 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 5703 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5704 | max *= fact; |
5705 | # endif | |
5706 | ||
5707 | /* Compare with current best results. */ | |
5708 | if (max < best_chlen) | |
5709 | { | |
5710 | best_chlen = max; | |
5711 | best_size = i; | |
ca4be51c | 5712 | no_improvement_count = 0; |
5a580b3a | 5713 | } |
0883b6e0 NC |
5714 | /* PR 11843: Avoid futile long searches for the best bucket size |
5715 | when there are a large number of symbols. */ | |
5716 | else if (++no_improvement_count == 100) | |
5717 | break; | |
5a580b3a AM |
5718 | } |
5719 | ||
5720 | free (counts); | |
5721 | } | |
5722 | else | |
5723 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
5724 | { | |
5725 | /* This is the fallback solution if no 64bit type is available or if we | |
5726 | are not supposed to spend much time on optimizations. We select the | |
5727 | bucket count using a fixed set of numbers. */ | |
5728 | for (i = 0; elf_buckets[i] != 0; i++) | |
5729 | { | |
5730 | best_size = elf_buckets[i]; | |
fdc90cb4 | 5731 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
5732 | break; |
5733 | } | |
fdc90cb4 JJ |
5734 | if (gnu_hash && best_size < 2) |
5735 | best_size = 2; | |
5a580b3a AM |
5736 | } |
5737 | ||
5a580b3a AM |
5738 | return best_size; |
5739 | } | |
5740 | ||
d0bf826b AM |
5741 | /* Size any SHT_GROUP section for ld -r. */ |
5742 | ||
5743 | bfd_boolean | |
5744 | _bfd_elf_size_group_sections (struct bfd_link_info *info) | |
5745 | { | |
5746 | bfd *ibfd; | |
5747 | ||
c72f2fb2 | 5748 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
d0bf826b AM |
5749 | if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour |
5750 | && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) | |
5751 | return FALSE; | |
5752 | return TRUE; | |
5753 | } | |
5754 | ||
04c3a755 NS |
5755 | /* Set a default stack segment size. The value in INFO wins. If it |
5756 | is unset, LEGACY_SYMBOL's value is used, and if that symbol is | |
5757 | undefined it is initialized. */ | |
5758 | ||
5759 | bfd_boolean | |
5760 | bfd_elf_stack_segment_size (bfd *output_bfd, | |
5761 | struct bfd_link_info *info, | |
5762 | const char *legacy_symbol, | |
5763 | bfd_vma default_size) | |
5764 | { | |
5765 | struct elf_link_hash_entry *h = NULL; | |
5766 | ||
5767 | /* Look for legacy symbol. */ | |
5768 | if (legacy_symbol) | |
5769 | h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, | |
5770 | FALSE, FALSE, FALSE); | |
5771 | if (h && (h->root.type == bfd_link_hash_defined | |
5772 | || h->root.type == bfd_link_hash_defweak) | |
5773 | && h->def_regular | |
5774 | && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) | |
5775 | { | |
5776 | /* The symbol has no type if specified on the command line. */ | |
5777 | h->type = STT_OBJECT; | |
5778 | if (info->stacksize) | |
695344c0 | 5779 | /* xgettext:c-format */ |
4eca0228 AM |
5780 | _bfd_error_handler (_("%B: stack size specified and %s set"), |
5781 | output_bfd, legacy_symbol); | |
04c3a755 | 5782 | else if (h->root.u.def.section != bfd_abs_section_ptr) |
695344c0 | 5783 | /* xgettext:c-format */ |
4eca0228 AM |
5784 | _bfd_error_handler (_("%B: %s not absolute"), |
5785 | output_bfd, legacy_symbol); | |
04c3a755 NS |
5786 | else |
5787 | info->stacksize = h->root.u.def.value; | |
5788 | } | |
5789 | ||
5790 | if (!info->stacksize) | |
5791 | /* If the user didn't set a size, or explicitly inhibit the | |
5792 | size, set it now. */ | |
5793 | info->stacksize = default_size; | |
5794 | ||
5795 | /* Provide the legacy symbol, if it is referenced. */ | |
5796 | if (h && (h->root.type == bfd_link_hash_undefined | |
5797 | || h->root.type == bfd_link_hash_undefweak)) | |
5798 | { | |
5799 | struct bfd_link_hash_entry *bh = NULL; | |
5800 | ||
5801 | if (!(_bfd_generic_link_add_one_symbol | |
5802 | (info, output_bfd, legacy_symbol, | |
5803 | BSF_GLOBAL, bfd_abs_section_ptr, | |
5804 | info->stacksize >= 0 ? info->stacksize : 0, | |
5805 | NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh))) | |
5806 | return FALSE; | |
5807 | ||
5808 | h = (struct elf_link_hash_entry *) bh; | |
5809 | h->def_regular = 1; | |
5810 | h->type = STT_OBJECT; | |
5811 | } | |
5812 | ||
5813 | return TRUE; | |
5814 | } | |
5815 | ||
5a580b3a AM |
5816 | /* Set up the sizes and contents of the ELF dynamic sections. This is |
5817 | called by the ELF linker emulation before_allocation routine. We | |
5818 | must set the sizes of the sections before the linker sets the | |
5819 | addresses of the various sections. */ | |
5820 | ||
5821 | bfd_boolean | |
5822 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
5823 | const char *soname, | |
5824 | const char *rpath, | |
5825 | const char *filter_shlib, | |
7ee314fa AM |
5826 | const char *audit, |
5827 | const char *depaudit, | |
5a580b3a AM |
5828 | const char * const *auxiliary_filters, |
5829 | struct bfd_link_info *info, | |
fd91d419 | 5830 | asection **sinterpptr) |
5a580b3a | 5831 | { |
ef53be89 | 5832 | size_t soname_indx; |
5a580b3a AM |
5833 | bfd *dynobj; |
5834 | const struct elf_backend_data *bed; | |
28caa186 | 5835 | struct elf_info_failed asvinfo; |
5a580b3a AM |
5836 | |
5837 | *sinterpptr = NULL; | |
5838 | ||
ef53be89 | 5839 | soname_indx = (size_t) -1; |
5a580b3a AM |
5840 | |
5841 | if (!is_elf_hash_table (info->hash)) | |
5842 | return TRUE; | |
5843 | ||
6bfdb61b | 5844 | bed = get_elf_backend_data (output_bfd); |
04c3a755 NS |
5845 | |
5846 | /* Any syms created from now on start with -1 in | |
5847 | got.refcount/offset and plt.refcount/offset. */ | |
5848 | elf_hash_table (info)->init_got_refcount | |
5849 | = elf_hash_table (info)->init_got_offset; | |
5850 | elf_hash_table (info)->init_plt_refcount | |
5851 | = elf_hash_table (info)->init_plt_offset; | |
5852 | ||
0e1862bb | 5853 | if (bfd_link_relocatable (info) |
04c3a755 NS |
5854 | && !_bfd_elf_size_group_sections (info)) |
5855 | return FALSE; | |
5856 | ||
5857 | /* The backend may have to create some sections regardless of whether | |
5858 | we're dynamic or not. */ | |
5859 | if (bed->elf_backend_always_size_sections | |
5860 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
5861 | return FALSE; | |
5862 | ||
5863 | /* Determine any GNU_STACK segment requirements, after the backend | |
5864 | has had a chance to set a default segment size. */ | |
5a580b3a | 5865 | if (info->execstack) |
12bd6957 | 5866 | elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; |
5a580b3a | 5867 | else if (info->noexecstack) |
12bd6957 | 5868 | elf_stack_flags (output_bfd) = PF_R | PF_W; |
5a580b3a AM |
5869 | else |
5870 | { | |
5871 | bfd *inputobj; | |
5872 | asection *notesec = NULL; | |
5873 | int exec = 0; | |
5874 | ||
5875 | for (inputobj = info->input_bfds; | |
5876 | inputobj; | |
c72f2fb2 | 5877 | inputobj = inputobj->link.next) |
5a580b3a AM |
5878 | { |
5879 | asection *s; | |
5880 | ||
a92c088a L |
5881 | if (inputobj->flags |
5882 | & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) | |
5a580b3a AM |
5883 | continue; |
5884 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
5885 | if (s) | |
5886 | { | |
5887 | if (s->flags & SEC_CODE) | |
5888 | exec = PF_X; | |
5889 | notesec = s; | |
5890 | } | |
6bfdb61b | 5891 | else if (bed->default_execstack) |
5a580b3a AM |
5892 | exec = PF_X; |
5893 | } | |
04c3a755 | 5894 | if (notesec || info->stacksize > 0) |
12bd6957 | 5895 | elf_stack_flags (output_bfd) = PF_R | PF_W | exec; |
0e1862bb | 5896 | if (notesec && exec && bfd_link_relocatable (info) |
04c3a755 NS |
5897 | && notesec->output_section != bfd_abs_section_ptr) |
5898 | notesec->output_section->flags |= SEC_CODE; | |
5a580b3a AM |
5899 | } |
5900 | ||
5a580b3a AM |
5901 | dynobj = elf_hash_table (info)->dynobj; |
5902 | ||
9a2a56cc | 5903 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a AM |
5904 | { |
5905 | struct elf_info_failed eif; | |
5906 | struct elf_link_hash_entry *h; | |
5907 | asection *dynstr; | |
5908 | struct bfd_elf_version_tree *t; | |
5909 | struct bfd_elf_version_expr *d; | |
046183de | 5910 | asection *s; |
5a580b3a AM |
5911 | bfd_boolean all_defined; |
5912 | ||
3d4d4302 | 5913 | *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); |
9b8b325a | 5914 | BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); |
5a580b3a AM |
5915 | |
5916 | if (soname != NULL) | |
5917 | { | |
5918 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5919 | soname, TRUE); | |
ef53be89 | 5920 | if (soname_indx == (size_t) -1 |
5a580b3a AM |
5921 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) |
5922 | return FALSE; | |
5923 | } | |
5924 | ||
5925 | if (info->symbolic) | |
5926 | { | |
5927 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
5928 | return FALSE; | |
5929 | info->flags |= DF_SYMBOLIC; | |
5930 | } | |
5931 | ||
5932 | if (rpath != NULL) | |
5933 | { | |
ef53be89 | 5934 | size_t indx; |
b1b00fcc | 5935 | bfd_vma tag; |
5a580b3a AM |
5936 | |
5937 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
5938 | TRUE); | |
ef53be89 | 5939 | if (indx == (size_t) -1) |
5a580b3a AM |
5940 | return FALSE; |
5941 | ||
b1b00fcc MF |
5942 | tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; |
5943 | if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) | |
5944 | return FALSE; | |
5a580b3a AM |
5945 | } |
5946 | ||
5947 | if (filter_shlib != NULL) | |
5948 | { | |
ef53be89 | 5949 | size_t indx; |
5a580b3a AM |
5950 | |
5951 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5952 | filter_shlib, TRUE); | |
ef53be89 | 5953 | if (indx == (size_t) -1 |
5a580b3a AM |
5954 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) |
5955 | return FALSE; | |
5956 | } | |
5957 | ||
5958 | if (auxiliary_filters != NULL) | |
5959 | { | |
5960 | const char * const *p; | |
5961 | ||
5962 | for (p = auxiliary_filters; *p != NULL; p++) | |
5963 | { | |
ef53be89 | 5964 | size_t indx; |
5a580b3a AM |
5965 | |
5966 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5967 | *p, TRUE); | |
ef53be89 | 5968 | if (indx == (size_t) -1 |
5a580b3a AM |
5969 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) |
5970 | return FALSE; | |
5971 | } | |
5972 | } | |
5973 | ||
7ee314fa AM |
5974 | if (audit != NULL) |
5975 | { | |
ef53be89 | 5976 | size_t indx; |
7ee314fa AM |
5977 | |
5978 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, | |
5979 | TRUE); | |
ef53be89 | 5980 | if (indx == (size_t) -1 |
7ee314fa AM |
5981 | || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) |
5982 | return FALSE; | |
5983 | } | |
5984 | ||
5985 | if (depaudit != NULL) | |
5986 | { | |
ef53be89 | 5987 | size_t indx; |
7ee314fa AM |
5988 | |
5989 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, | |
5990 | TRUE); | |
ef53be89 | 5991 | if (indx == (size_t) -1 |
7ee314fa AM |
5992 | || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) |
5993 | return FALSE; | |
5994 | } | |
5995 | ||
5a580b3a | 5996 | eif.info = info; |
5a580b3a AM |
5997 | eif.failed = FALSE; |
5998 | ||
5999 | /* If we are supposed to export all symbols into the dynamic symbol | |
6000 | table (this is not the normal case), then do so. */ | |
55255dae | 6001 | if (info->export_dynamic |
0e1862bb | 6002 | || (bfd_link_executable (info) && info->dynamic)) |
5a580b3a AM |
6003 | { |
6004 | elf_link_hash_traverse (elf_hash_table (info), | |
6005 | _bfd_elf_export_symbol, | |
6006 | &eif); | |
6007 | if (eif.failed) | |
6008 | return FALSE; | |
6009 | } | |
6010 | ||
6011 | /* Make all global versions with definition. */ | |
fd91d419 | 6012 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6013 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6014 | if (!d->symver && d->literal) |
5a580b3a AM |
6015 | { |
6016 | const char *verstr, *name; | |
6017 | size_t namelen, verlen, newlen; | |
93252b1c | 6018 | char *newname, *p, leading_char; |
5a580b3a AM |
6019 | struct elf_link_hash_entry *newh; |
6020 | ||
93252b1c | 6021 | leading_char = bfd_get_symbol_leading_char (output_bfd); |
ae5a3597 | 6022 | name = d->pattern; |
93252b1c | 6023 | namelen = strlen (name) + (leading_char != '\0'); |
5a580b3a AM |
6024 | verstr = t->name; |
6025 | verlen = strlen (verstr); | |
6026 | newlen = namelen + verlen + 3; | |
6027 | ||
a50b1753 | 6028 | newname = (char *) bfd_malloc (newlen); |
5a580b3a AM |
6029 | if (newname == NULL) |
6030 | return FALSE; | |
93252b1c MF |
6031 | newname[0] = leading_char; |
6032 | memcpy (newname + (leading_char != '\0'), name, namelen); | |
5a580b3a AM |
6033 | |
6034 | /* Check the hidden versioned definition. */ | |
6035 | p = newname + namelen; | |
6036 | *p++ = ELF_VER_CHR; | |
6037 | memcpy (p, verstr, verlen + 1); | |
6038 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6039 | newname, FALSE, FALSE, | |
6040 | FALSE); | |
6041 | if (newh == NULL | |
6042 | || (newh->root.type != bfd_link_hash_defined | |
6043 | && newh->root.type != bfd_link_hash_defweak)) | |
6044 | { | |
6045 | /* Check the default versioned definition. */ | |
6046 | *p++ = ELF_VER_CHR; | |
6047 | memcpy (p, verstr, verlen + 1); | |
6048 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6049 | newname, FALSE, FALSE, | |
6050 | FALSE); | |
6051 | } | |
6052 | free (newname); | |
6053 | ||
6054 | /* Mark this version if there is a definition and it is | |
6055 | not defined in a shared object. */ | |
6056 | if (newh != NULL | |
f5385ebf | 6057 | && !newh->def_dynamic |
5a580b3a AM |
6058 | && (newh->root.type == bfd_link_hash_defined |
6059 | || newh->root.type == bfd_link_hash_defweak)) | |
6060 | d->symver = 1; | |
6061 | } | |
6062 | ||
6063 | /* Attach all the symbols to their version information. */ | |
5a580b3a | 6064 | asvinfo.info = info; |
5a580b3a AM |
6065 | asvinfo.failed = FALSE; |
6066 | ||
6067 | elf_link_hash_traverse (elf_hash_table (info), | |
6068 | _bfd_elf_link_assign_sym_version, | |
6069 | &asvinfo); | |
6070 | if (asvinfo.failed) | |
6071 | return FALSE; | |
6072 | ||
6073 | if (!info->allow_undefined_version) | |
6074 | { | |
6075 | /* Check if all global versions have a definition. */ | |
6076 | all_defined = TRUE; | |
fd91d419 | 6077 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6078 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6079 | if (d->literal && !d->symver && !d->script) |
5a580b3a | 6080 | { |
4eca0228 | 6081 | _bfd_error_handler |
5a580b3a AM |
6082 | (_("%s: undefined version: %s"), |
6083 | d->pattern, t->name); | |
6084 | all_defined = FALSE; | |
6085 | } | |
6086 | ||
6087 | if (!all_defined) | |
6088 | { | |
6089 | bfd_set_error (bfd_error_bad_value); | |
6090 | return FALSE; | |
6091 | } | |
6092 | } | |
6093 | ||
6094 | /* Find all symbols which were defined in a dynamic object and make | |
6095 | the backend pick a reasonable value for them. */ | |
6096 | elf_link_hash_traverse (elf_hash_table (info), | |
6097 | _bfd_elf_adjust_dynamic_symbol, | |
6098 | &eif); | |
6099 | if (eif.failed) | |
6100 | return FALSE; | |
6101 | ||
6102 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 6103 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
6104 | now so that we know the final size of the .dynamic section. */ |
6105 | ||
6106 | /* If there are initialization and/or finalization functions to | |
6107 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
6108 | h = (info->init_function | |
6109 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6110 | info->init_function, FALSE, | |
6111 | FALSE, FALSE) | |
6112 | : NULL); | |
6113 | if (h != NULL | |
f5385ebf AM |
6114 | && (h->ref_regular |
6115 | || h->def_regular)) | |
5a580b3a AM |
6116 | { |
6117 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
6118 | return FALSE; | |
6119 | } | |
6120 | h = (info->fini_function | |
6121 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6122 | info->fini_function, FALSE, | |
6123 | FALSE, FALSE) | |
6124 | : NULL); | |
6125 | if (h != NULL | |
f5385ebf AM |
6126 | && (h->ref_regular |
6127 | || h->def_regular)) | |
5a580b3a AM |
6128 | { |
6129 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
6130 | return FALSE; | |
6131 | } | |
6132 | ||
046183de AM |
6133 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); |
6134 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
6135 | { |
6136 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
0e1862bb | 6137 | if (! bfd_link_executable (info)) |
5a580b3a AM |
6138 | { |
6139 | bfd *sub; | |
6140 | asection *o; | |
6141 | ||
6142 | for (sub = info->input_bfds; sub != NULL; | |
c72f2fb2 | 6143 | sub = sub->link.next) |
3fcd97f1 JJ |
6144 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour) |
6145 | for (o = sub->sections; o != NULL; o = o->next) | |
6146 | if (elf_section_data (o)->this_hdr.sh_type | |
6147 | == SHT_PREINIT_ARRAY) | |
6148 | { | |
4eca0228 | 6149 | _bfd_error_handler |
3fcd97f1 JJ |
6150 | (_("%B: .preinit_array section is not allowed in DSO"), |
6151 | sub); | |
6152 | break; | |
6153 | } | |
5a580b3a AM |
6154 | |
6155 | bfd_set_error (bfd_error_nonrepresentable_section); | |
6156 | return FALSE; | |
6157 | } | |
6158 | ||
6159 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
6160 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
6161 | return FALSE; | |
6162 | } | |
046183de AM |
6163 | s = bfd_get_section_by_name (output_bfd, ".init_array"); |
6164 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
6165 | { |
6166 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
6167 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
6168 | return FALSE; | |
6169 | } | |
046183de AM |
6170 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); |
6171 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
6172 | { |
6173 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
6174 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
6175 | return FALSE; | |
6176 | } | |
6177 | ||
3d4d4302 | 6178 | dynstr = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
6179 | /* If .dynstr is excluded from the link, we don't want any of |
6180 | these tags. Strictly, we should be checking each section | |
6181 | individually; This quick check covers for the case where | |
6182 | someone does a /DISCARD/ : { *(*) }. */ | |
6183 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
6184 | { | |
6185 | bfd_size_type strsize; | |
6186 | ||
6187 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
fdc90cb4 JJ |
6188 | if ((info->emit_hash |
6189 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
6190 | || (info->emit_gnu_hash | |
6191 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
5a580b3a AM |
6192 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) |
6193 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
6194 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
6195 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
6196 | bed->s->sizeof_sym)) | |
6197 | return FALSE; | |
6198 | } | |
6199 | } | |
6200 | ||
de231f20 CM |
6201 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) |
6202 | return FALSE; | |
6203 | ||
5a580b3a AM |
6204 | /* The backend must work out the sizes of all the other dynamic |
6205 | sections. */ | |
9a2a56cc AM |
6206 | if (dynobj != NULL |
6207 | && bed->elf_backend_size_dynamic_sections != NULL | |
5a580b3a AM |
6208 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) |
6209 | return FALSE; | |
6210 | ||
9a2a56cc | 6211 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a | 6212 | { |
554220db | 6213 | unsigned long section_sym_count; |
fd91d419 | 6214 | struct bfd_elf_version_tree *verdefs; |
5a580b3a | 6215 | asection *s; |
5a580b3a AM |
6216 | |
6217 | /* Set up the version definition section. */ | |
3d4d4302 | 6218 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
6219 | BFD_ASSERT (s != NULL); |
6220 | ||
6221 | /* We may have created additional version definitions if we are | |
6222 | just linking a regular application. */ | |
fd91d419 | 6223 | verdefs = info->version_info; |
5a580b3a AM |
6224 | |
6225 | /* Skip anonymous version tag. */ | |
6226 | if (verdefs != NULL && verdefs->vernum == 0) | |
6227 | verdefs = verdefs->next; | |
6228 | ||
3e3b46e5 | 6229 | if (verdefs == NULL && !info->create_default_symver) |
8423293d | 6230 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
6231 | else |
6232 | { | |
6233 | unsigned int cdefs; | |
6234 | bfd_size_type size; | |
6235 | struct bfd_elf_version_tree *t; | |
6236 | bfd_byte *p; | |
6237 | Elf_Internal_Verdef def; | |
6238 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
6239 | struct bfd_link_hash_entry *bh; |
6240 | struct elf_link_hash_entry *h; | |
6241 | const char *name; | |
5a580b3a AM |
6242 | |
6243 | cdefs = 0; | |
6244 | size = 0; | |
6245 | ||
6246 | /* Make space for the base version. */ | |
6247 | size += sizeof (Elf_External_Verdef); | |
6248 | size += sizeof (Elf_External_Verdaux); | |
6249 | ++cdefs; | |
6250 | ||
3e3b46e5 PB |
6251 | /* Make space for the default version. */ |
6252 | if (info->create_default_symver) | |
6253 | { | |
6254 | size += sizeof (Elf_External_Verdef); | |
6255 | ++cdefs; | |
6256 | } | |
6257 | ||
5a580b3a AM |
6258 | for (t = verdefs; t != NULL; t = t->next) |
6259 | { | |
6260 | struct bfd_elf_version_deps *n; | |
6261 | ||
a6cc6b3b RO |
6262 | /* Don't emit base version twice. */ |
6263 | if (t->vernum == 0) | |
6264 | continue; | |
6265 | ||
5a580b3a AM |
6266 | size += sizeof (Elf_External_Verdef); |
6267 | size += sizeof (Elf_External_Verdaux); | |
6268 | ++cdefs; | |
6269 | ||
6270 | for (n = t->deps; n != NULL; n = n->next) | |
6271 | size += sizeof (Elf_External_Verdaux); | |
6272 | } | |
6273 | ||
eea6121a | 6274 | s->size = size; |
a50b1753 | 6275 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
eea6121a | 6276 | if (s->contents == NULL && s->size != 0) |
5a580b3a AM |
6277 | return FALSE; |
6278 | ||
6279 | /* Fill in the version definition section. */ | |
6280 | ||
6281 | p = s->contents; | |
6282 | ||
6283 | def.vd_version = VER_DEF_CURRENT; | |
6284 | def.vd_flags = VER_FLG_BASE; | |
6285 | def.vd_ndx = 1; | |
6286 | def.vd_cnt = 1; | |
3e3b46e5 PB |
6287 | if (info->create_default_symver) |
6288 | { | |
6289 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
6290 | def.vd_next = sizeof (Elf_External_Verdef); | |
6291 | } | |
6292 | else | |
6293 | { | |
6294 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6295 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6296 | + sizeof (Elf_External_Verdaux)); | |
6297 | } | |
5a580b3a | 6298 | |
ef53be89 | 6299 | if (soname_indx != (size_t) -1) |
5a580b3a AM |
6300 | { |
6301 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6302 | soname_indx); | |
6303 | def.vd_hash = bfd_elf_hash (soname); | |
6304 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 6305 | name = soname; |
5a580b3a AM |
6306 | } |
6307 | else | |
6308 | { | |
ef53be89 | 6309 | size_t indx; |
5a580b3a | 6310 | |
06084812 | 6311 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
6312 | def.vd_hash = bfd_elf_hash (name); |
6313 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6314 | name, FALSE); | |
ef53be89 | 6315 | if (indx == (size_t) -1) |
5a580b3a AM |
6316 | return FALSE; |
6317 | defaux.vda_name = indx; | |
6318 | } | |
6319 | defaux.vda_next = 0; | |
6320 | ||
6321 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6322 | (Elf_External_Verdef *) p); | |
6323 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
6324 | if (info->create_default_symver) |
6325 | { | |
6326 | /* Add a symbol representing this version. */ | |
6327 | bh = NULL; | |
6328 | if (! (_bfd_generic_link_add_one_symbol | |
6329 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6330 | 0, NULL, FALSE, | |
6331 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6332 | return FALSE; | |
6333 | h = (struct elf_link_hash_entry *) bh; | |
6334 | h->non_elf = 0; | |
6335 | h->def_regular = 1; | |
6336 | h->type = STT_OBJECT; | |
6337 | h->verinfo.vertree = NULL; | |
6338 | ||
6339 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
6340 | return FALSE; | |
6341 | ||
6342 | /* Create a duplicate of the base version with the same | |
6343 | aux block, but different flags. */ | |
6344 | def.vd_flags = 0; | |
6345 | def.vd_ndx = 2; | |
6346 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6347 | if (verdefs) | |
6348 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6349 | + sizeof (Elf_External_Verdaux)); | |
6350 | else | |
6351 | def.vd_next = 0; | |
6352 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6353 | (Elf_External_Verdef *) p); | |
6354 | p += sizeof (Elf_External_Verdef); | |
6355 | } | |
5a580b3a AM |
6356 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
6357 | (Elf_External_Verdaux *) p); | |
6358 | p += sizeof (Elf_External_Verdaux); | |
6359 | ||
6360 | for (t = verdefs; t != NULL; t = t->next) | |
6361 | { | |
6362 | unsigned int cdeps; | |
6363 | struct bfd_elf_version_deps *n; | |
5a580b3a | 6364 | |
a6cc6b3b RO |
6365 | /* Don't emit the base version twice. */ |
6366 | if (t->vernum == 0) | |
6367 | continue; | |
6368 | ||
5a580b3a AM |
6369 | cdeps = 0; |
6370 | for (n = t->deps; n != NULL; n = n->next) | |
6371 | ++cdeps; | |
6372 | ||
6373 | /* Add a symbol representing this version. */ | |
6374 | bh = NULL; | |
6375 | if (! (_bfd_generic_link_add_one_symbol | |
6376 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6377 | 0, NULL, FALSE, | |
6378 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6379 | return FALSE; | |
6380 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
6381 | h->non_elf = 0; |
6382 | h->def_regular = 1; | |
5a580b3a AM |
6383 | h->type = STT_OBJECT; |
6384 | h->verinfo.vertree = t; | |
6385 | ||
c152c796 | 6386 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
6387 | return FALSE; |
6388 | ||
6389 | def.vd_version = VER_DEF_CURRENT; | |
6390 | def.vd_flags = 0; | |
6391 | if (t->globals.list == NULL | |
6392 | && t->locals.list == NULL | |
6393 | && ! t->used) | |
6394 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 6395 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
6396 | def.vd_cnt = cdeps + 1; |
6397 | def.vd_hash = bfd_elf_hash (t->name); | |
6398 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6399 | def.vd_next = 0; | |
a6cc6b3b RO |
6400 | |
6401 | /* If a basever node is next, it *must* be the last node in | |
6402 | the chain, otherwise Verdef construction breaks. */ | |
6403 | if (t->next != NULL && t->next->vernum == 0) | |
6404 | BFD_ASSERT (t->next->next == NULL); | |
6405 | ||
6406 | if (t->next != NULL && t->next->vernum != 0) | |
5a580b3a AM |
6407 | def.vd_next = (sizeof (Elf_External_Verdef) |
6408 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
6409 | ||
6410 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6411 | (Elf_External_Verdef *) p); | |
6412 | p += sizeof (Elf_External_Verdef); | |
6413 | ||
6414 | defaux.vda_name = h->dynstr_index; | |
6415 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6416 | h->dynstr_index); | |
6417 | defaux.vda_next = 0; | |
6418 | if (t->deps != NULL) | |
6419 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6420 | t->name_indx = defaux.vda_name; | |
6421 | ||
6422 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6423 | (Elf_External_Verdaux *) p); | |
6424 | p += sizeof (Elf_External_Verdaux); | |
6425 | ||
6426 | for (n = t->deps; n != NULL; n = n->next) | |
6427 | { | |
6428 | if (n->version_needed == NULL) | |
6429 | { | |
6430 | /* This can happen if there was an error in the | |
6431 | version script. */ | |
6432 | defaux.vda_name = 0; | |
6433 | } | |
6434 | else | |
6435 | { | |
6436 | defaux.vda_name = n->version_needed->name_indx; | |
6437 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6438 | defaux.vda_name); | |
6439 | } | |
6440 | if (n->next == NULL) | |
6441 | defaux.vda_next = 0; | |
6442 | else | |
6443 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6444 | ||
6445 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6446 | (Elf_External_Verdaux *) p); | |
6447 | p += sizeof (Elf_External_Verdaux); | |
6448 | } | |
6449 | } | |
6450 | ||
6451 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
6452 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
6453 | return FALSE; | |
6454 | ||
6455 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
6456 | } | |
6457 | ||
6458 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
6459 | { | |
6460 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
6461 | return FALSE; | |
6462 | } | |
6463 | else if (info->flags & DF_BIND_NOW) | |
6464 | { | |
6465 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
6466 | return FALSE; | |
6467 | } | |
6468 | ||
6469 | if (info->flags_1) | |
6470 | { | |
0e1862bb | 6471 | if (bfd_link_executable (info)) |
5a580b3a AM |
6472 | info->flags_1 &= ~ (DF_1_INITFIRST |
6473 | | DF_1_NODELETE | |
6474 | | DF_1_NOOPEN); | |
6475 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
6476 | return FALSE; | |
6477 | } | |
6478 | ||
6479 | /* Work out the size of the version reference section. */ | |
6480 | ||
3d4d4302 | 6481 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
6482 | BFD_ASSERT (s != NULL); |
6483 | { | |
6484 | struct elf_find_verdep_info sinfo; | |
6485 | ||
5a580b3a AM |
6486 | sinfo.info = info; |
6487 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
6488 | if (sinfo.vers == 0) | |
6489 | sinfo.vers = 1; | |
6490 | sinfo.failed = FALSE; | |
6491 | ||
6492 | elf_link_hash_traverse (elf_hash_table (info), | |
6493 | _bfd_elf_link_find_version_dependencies, | |
6494 | &sinfo); | |
14b1c01e AM |
6495 | if (sinfo.failed) |
6496 | return FALSE; | |
5a580b3a AM |
6497 | |
6498 | if (elf_tdata (output_bfd)->verref == NULL) | |
8423293d | 6499 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
6500 | else |
6501 | { | |
6502 | Elf_Internal_Verneed *t; | |
6503 | unsigned int size; | |
6504 | unsigned int crefs; | |
6505 | bfd_byte *p; | |
6506 | ||
a6cc6b3b | 6507 | /* Build the version dependency section. */ |
5a580b3a AM |
6508 | size = 0; |
6509 | crefs = 0; | |
6510 | for (t = elf_tdata (output_bfd)->verref; | |
6511 | t != NULL; | |
6512 | t = t->vn_nextref) | |
6513 | { | |
6514 | Elf_Internal_Vernaux *a; | |
6515 | ||
6516 | size += sizeof (Elf_External_Verneed); | |
6517 | ++crefs; | |
6518 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6519 | size += sizeof (Elf_External_Vernaux); | |
6520 | } | |
6521 | ||
eea6121a | 6522 | s->size = size; |
a50b1753 | 6523 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
5a580b3a AM |
6524 | if (s->contents == NULL) |
6525 | return FALSE; | |
6526 | ||
6527 | p = s->contents; | |
6528 | for (t = elf_tdata (output_bfd)->verref; | |
6529 | t != NULL; | |
6530 | t = t->vn_nextref) | |
6531 | { | |
6532 | unsigned int caux; | |
6533 | Elf_Internal_Vernaux *a; | |
ef53be89 | 6534 | size_t indx; |
5a580b3a AM |
6535 | |
6536 | caux = 0; | |
6537 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6538 | ++caux; | |
6539 | ||
6540 | t->vn_version = VER_NEED_CURRENT; | |
6541 | t->vn_cnt = caux; | |
6542 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6543 | elf_dt_name (t->vn_bfd) != NULL | |
6544 | ? elf_dt_name (t->vn_bfd) | |
06084812 | 6545 | : lbasename (t->vn_bfd->filename), |
5a580b3a | 6546 | FALSE); |
ef53be89 | 6547 | if (indx == (size_t) -1) |
5a580b3a AM |
6548 | return FALSE; |
6549 | t->vn_file = indx; | |
6550 | t->vn_aux = sizeof (Elf_External_Verneed); | |
6551 | if (t->vn_nextref == NULL) | |
6552 | t->vn_next = 0; | |
6553 | else | |
6554 | t->vn_next = (sizeof (Elf_External_Verneed) | |
6555 | + caux * sizeof (Elf_External_Vernaux)); | |
6556 | ||
6557 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
6558 | (Elf_External_Verneed *) p); | |
6559 | p += sizeof (Elf_External_Verneed); | |
6560 | ||
6561 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6562 | { | |
6563 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6564 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6565 | a->vna_nodename, FALSE); | |
ef53be89 | 6566 | if (indx == (size_t) -1) |
5a580b3a AM |
6567 | return FALSE; |
6568 | a->vna_name = indx; | |
6569 | if (a->vna_nextptr == NULL) | |
6570 | a->vna_next = 0; | |
6571 | else | |
6572 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6573 | ||
6574 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6575 | (Elf_External_Vernaux *) p); | |
6576 | p += sizeof (Elf_External_Vernaux); | |
6577 | } | |
6578 | } | |
6579 | ||
6580 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
6581 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
6582 | return FALSE; | |
6583 | ||
6584 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6585 | } | |
6586 | } | |
6587 | ||
8423293d AM |
6588 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
6589 | && elf_tdata (output_bfd)->cverdefs == 0) | |
6590 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
6591 | §ion_sym_count) == 0) | |
6592 | { | |
3d4d4302 | 6593 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
8423293d AM |
6594 | s->flags |= SEC_EXCLUDE; |
6595 | } | |
6596 | } | |
6597 | return TRUE; | |
6598 | } | |
6599 | ||
74541ad4 AM |
6600 | /* Find the first non-excluded output section. We'll use its |
6601 | section symbol for some emitted relocs. */ | |
6602 | void | |
6603 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
6604 | { | |
6605 | asection *s; | |
6606 | ||
6607 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6608 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
6609 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6610 | { | |
6611 | elf_hash_table (info)->text_index_section = s; | |
6612 | break; | |
6613 | } | |
6614 | } | |
6615 | ||
6616 | /* Find two non-excluded output sections, one for code, one for data. | |
6617 | We'll use their section symbols for some emitted relocs. */ | |
6618 | void | |
6619 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
6620 | { | |
6621 | asection *s; | |
6622 | ||
266b05cf DJ |
6623 | /* Data first, since setting text_index_section changes |
6624 | _bfd_elf_link_omit_section_dynsym. */ | |
74541ad4 | 6625 | for (s = output_bfd->sections; s != NULL; s = s->next) |
266b05cf | 6626 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) |
74541ad4 AM |
6627 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6628 | { | |
266b05cf | 6629 | elf_hash_table (info)->data_index_section = s; |
74541ad4 AM |
6630 | break; |
6631 | } | |
6632 | ||
6633 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
266b05cf DJ |
6634 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) |
6635 | == (SEC_ALLOC | SEC_READONLY)) | |
74541ad4 AM |
6636 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6637 | { | |
266b05cf | 6638 | elf_hash_table (info)->text_index_section = s; |
74541ad4 AM |
6639 | break; |
6640 | } | |
6641 | ||
6642 | if (elf_hash_table (info)->text_index_section == NULL) | |
6643 | elf_hash_table (info)->text_index_section | |
6644 | = elf_hash_table (info)->data_index_section; | |
6645 | } | |
6646 | ||
8423293d AM |
6647 | bfd_boolean |
6648 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
6649 | { | |
74541ad4 AM |
6650 | const struct elf_backend_data *bed; |
6651 | ||
8423293d AM |
6652 | if (!is_elf_hash_table (info->hash)) |
6653 | return TRUE; | |
6654 | ||
74541ad4 AM |
6655 | bed = get_elf_backend_data (output_bfd); |
6656 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
6657 | ||
8423293d AM |
6658 | if (elf_hash_table (info)->dynamic_sections_created) |
6659 | { | |
6660 | bfd *dynobj; | |
8423293d AM |
6661 | asection *s; |
6662 | bfd_size_type dynsymcount; | |
6663 | unsigned long section_sym_count; | |
8423293d AM |
6664 | unsigned int dtagcount; |
6665 | ||
6666 | dynobj = elf_hash_table (info)->dynobj; | |
6667 | ||
5a580b3a AM |
6668 | /* Assign dynsym indicies. In a shared library we generate a |
6669 | section symbol for each output section, which come first. | |
6670 | Next come all of the back-end allocated local dynamic syms, | |
6671 | followed by the rest of the global symbols. */ | |
6672 | ||
554220db AM |
6673 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, |
6674 | §ion_sym_count); | |
5a580b3a AM |
6675 | |
6676 | /* Work out the size of the symbol version section. */ | |
3d4d4302 | 6677 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
5a580b3a | 6678 | BFD_ASSERT (s != NULL); |
d5486c43 | 6679 | if ((s->flags & SEC_EXCLUDE) == 0) |
5a580b3a | 6680 | { |
eea6121a | 6681 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
a50b1753 | 6682 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
5a580b3a AM |
6683 | if (s->contents == NULL) |
6684 | return FALSE; | |
6685 | ||
6686 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
6687 | return FALSE; | |
6688 | } | |
6689 | ||
6690 | /* Set the size of the .dynsym and .hash sections. We counted | |
6691 | the number of dynamic symbols in elf_link_add_object_symbols. | |
6692 | We will build the contents of .dynsym and .hash when we build | |
6693 | the final symbol table, because until then we do not know the | |
6694 | correct value to give the symbols. We built the .dynstr | |
6695 | section as we went along in elf_link_add_object_symbols. */ | |
cae1fbbb | 6696 | s = elf_hash_table (info)->dynsym; |
5a580b3a | 6697 | BFD_ASSERT (s != NULL); |
eea6121a | 6698 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a | 6699 | |
d5486c43 L |
6700 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
6701 | if (s->contents == NULL) | |
6702 | return FALSE; | |
5a580b3a | 6703 | |
d5486c43 L |
6704 | /* The first entry in .dynsym is a dummy symbol. Clear all the |
6705 | section syms, in case we don't output them all. */ | |
6706 | ++section_sym_count; | |
6707 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a | 6708 | |
fdc90cb4 JJ |
6709 | elf_hash_table (info)->bucketcount = 0; |
6710 | ||
5a580b3a AM |
6711 | /* Compute the size of the hashing table. As a side effect this |
6712 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
6713 | if (info->emit_hash) |
6714 | { | |
6715 | unsigned long int *hashcodes; | |
14b1c01e | 6716 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
6717 | bfd_size_type amt; |
6718 | unsigned long int nsyms; | |
6719 | size_t bucketcount; | |
6720 | size_t hash_entry_size; | |
6721 | ||
6722 | /* Compute the hash values for all exported symbols. At the same | |
6723 | time store the values in an array so that we could use them for | |
6724 | optimizations. */ | |
6725 | amt = dynsymcount * sizeof (unsigned long int); | |
a50b1753 | 6726 | hashcodes = (unsigned long int *) bfd_malloc (amt); |
fdc90cb4 JJ |
6727 | if (hashcodes == NULL) |
6728 | return FALSE; | |
14b1c01e AM |
6729 | hashinf.hashcodes = hashcodes; |
6730 | hashinf.error = FALSE; | |
5a580b3a | 6731 | |
fdc90cb4 JJ |
6732 | /* Put all hash values in HASHCODES. */ |
6733 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
6734 | elf_collect_hash_codes, &hashinf); |
6735 | if (hashinf.error) | |
4dd07732 AM |
6736 | { |
6737 | free (hashcodes); | |
6738 | return FALSE; | |
6739 | } | |
5a580b3a | 6740 | |
14b1c01e | 6741 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
6742 | bucketcount |
6743 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
6744 | free (hashcodes); | |
6745 | ||
6746 | if (bucketcount == 0) | |
6747 | return FALSE; | |
5a580b3a | 6748 | |
fdc90cb4 JJ |
6749 | elf_hash_table (info)->bucketcount = bucketcount; |
6750 | ||
3d4d4302 | 6751 | s = bfd_get_linker_section (dynobj, ".hash"); |
fdc90cb4 JJ |
6752 | BFD_ASSERT (s != NULL); |
6753 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
6754 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
a50b1753 | 6755 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6756 | if (s->contents == NULL) |
6757 | return FALSE; | |
6758 | ||
6759 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
6760 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
6761 | s->contents + hash_entry_size); | |
6762 | } | |
6763 | ||
6764 | if (info->emit_gnu_hash) | |
6765 | { | |
6766 | size_t i, cnt; | |
6767 | unsigned char *contents; | |
6768 | struct collect_gnu_hash_codes cinfo; | |
6769 | bfd_size_type amt; | |
6770 | size_t bucketcount; | |
6771 | ||
6772 | memset (&cinfo, 0, sizeof (cinfo)); | |
6773 | ||
6774 | /* Compute the hash values for all exported symbols. At the same | |
6775 | time store the values in an array so that we could use them for | |
6776 | optimizations. */ | |
6777 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
a50b1753 | 6778 | cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); |
fdc90cb4 JJ |
6779 | if (cinfo.hashcodes == NULL) |
6780 | return FALSE; | |
6781 | ||
6782 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
6783 | cinfo.min_dynindx = -1; | |
6784 | cinfo.output_bfd = output_bfd; | |
6785 | cinfo.bed = bed; | |
6786 | ||
6787 | /* Put all hash values in HASHCODES. */ | |
6788 | elf_link_hash_traverse (elf_hash_table (info), | |
6789 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e | 6790 | if (cinfo.error) |
4dd07732 AM |
6791 | { |
6792 | free (cinfo.hashcodes); | |
6793 | return FALSE; | |
6794 | } | |
fdc90cb4 JJ |
6795 | |
6796 | bucketcount | |
6797 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
6798 | ||
6799 | if (bucketcount == 0) | |
6800 | { | |
6801 | free (cinfo.hashcodes); | |
6802 | return FALSE; | |
6803 | } | |
6804 | ||
3d4d4302 | 6805 | s = bfd_get_linker_section (dynobj, ".gnu.hash"); |
fdc90cb4 JJ |
6806 | BFD_ASSERT (s != NULL); |
6807 | ||
6808 | if (cinfo.nsyms == 0) | |
6809 | { | |
6810 | /* Empty .gnu.hash section is special. */ | |
6811 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
6812 | free (cinfo.hashcodes); | |
6813 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
a50b1753 | 6814 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6815 | if (contents == NULL) |
6816 | return FALSE; | |
6817 | s->contents = contents; | |
6818 | /* 1 empty bucket. */ | |
6819 | bfd_put_32 (output_bfd, 1, contents); | |
6820 | /* SYMIDX above the special symbol 0. */ | |
6821 | bfd_put_32 (output_bfd, 1, contents + 4); | |
6822 | /* Just one word for bitmask. */ | |
6823 | bfd_put_32 (output_bfd, 1, contents + 8); | |
6824 | /* Only hash fn bloom filter. */ | |
6825 | bfd_put_32 (output_bfd, 0, contents + 12); | |
6826 | /* No hashes are valid - empty bitmask. */ | |
6827 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
6828 | /* No hashes in the only bucket. */ | |
6829 | bfd_put_32 (output_bfd, 0, | |
6830 | contents + 16 + bed->s->arch_size / 8); | |
6831 | } | |
6832 | else | |
6833 | { | |
9e6619e2 | 6834 | unsigned long int maskwords, maskbitslog2, x; |
0b33793d | 6835 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 | 6836 | |
9e6619e2 AM |
6837 | x = cinfo.nsyms; |
6838 | maskbitslog2 = 1; | |
6839 | while ((x >>= 1) != 0) | |
6840 | ++maskbitslog2; | |
fdc90cb4 JJ |
6841 | if (maskbitslog2 < 3) |
6842 | maskbitslog2 = 5; | |
6843 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
6844 | maskbitslog2 = maskbitslog2 + 3; | |
6845 | else | |
6846 | maskbitslog2 = maskbitslog2 + 2; | |
6847 | if (bed->s->arch_size == 64) | |
6848 | { | |
6849 | if (maskbitslog2 == 5) | |
6850 | maskbitslog2 = 6; | |
6851 | cinfo.shift1 = 6; | |
6852 | } | |
6853 | else | |
6854 | cinfo.shift1 = 5; | |
6855 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 6856 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
6857 | cinfo.maskbits = 1 << maskbitslog2; |
6858 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
6859 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
6860 | amt += maskwords * sizeof (bfd_vma); | |
a50b1753 | 6861 | cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); |
fdc90cb4 JJ |
6862 | if (cinfo.bitmask == NULL) |
6863 | { | |
6864 | free (cinfo.hashcodes); | |
6865 | return FALSE; | |
6866 | } | |
6867 | ||
a50b1753 | 6868 | cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); |
fdc90cb4 JJ |
6869 | cinfo.indx = cinfo.counts + bucketcount; |
6870 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
6871 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
6872 | ||
6873 | /* Determine how often each hash bucket is used. */ | |
6874 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
6875 | for (i = 0; i < cinfo.nsyms; ++i) | |
6876 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
6877 | ||
6878 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
6879 | if (cinfo.counts[i] != 0) | |
6880 | { | |
6881 | cinfo.indx[i] = cnt; | |
6882 | cnt += cinfo.counts[i]; | |
6883 | } | |
6884 | BFD_ASSERT (cnt == dynsymcount); | |
6885 | cinfo.bucketcount = bucketcount; | |
6886 | cinfo.local_indx = cinfo.min_dynindx; | |
6887 | ||
6888 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
6889 | s->size += cinfo.maskbits / 8; | |
a50b1753 | 6890 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6891 | if (contents == NULL) |
6892 | { | |
6893 | free (cinfo.bitmask); | |
6894 | free (cinfo.hashcodes); | |
6895 | return FALSE; | |
6896 | } | |
6897 | ||
6898 | s->contents = contents; | |
6899 | bfd_put_32 (output_bfd, bucketcount, contents); | |
6900 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
6901 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
6902 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
6903 | contents += 16 + cinfo.maskbits / 8; | |
6904 | ||
6905 | for (i = 0; i < bucketcount; ++i) | |
6906 | { | |
6907 | if (cinfo.counts[i] == 0) | |
6908 | bfd_put_32 (output_bfd, 0, contents); | |
6909 | else | |
6910 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
6911 | contents += 4; | |
6912 | } | |
6913 | ||
6914 | cinfo.contents = contents; | |
6915 | ||
6916 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
6917 | elf_link_hash_traverse (elf_hash_table (info), | |
6918 | elf_renumber_gnu_hash_syms, &cinfo); | |
6919 | ||
6920 | contents = s->contents + 16; | |
6921 | for (i = 0; i < maskwords; ++i) | |
6922 | { | |
6923 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
6924 | contents); | |
6925 | contents += bed->s->arch_size / 8; | |
6926 | } | |
6927 | ||
6928 | free (cinfo.bitmask); | |
6929 | free (cinfo.hashcodes); | |
6930 | } | |
6931 | } | |
5a580b3a | 6932 | |
3d4d4302 | 6933 | s = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
6934 | BFD_ASSERT (s != NULL); |
6935 | ||
4ad4eba5 | 6936 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 6937 | |
eea6121a | 6938 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
6939 | |
6940 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
6941 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
6942 | return FALSE; | |
6943 | } | |
6944 | ||
6945 | return TRUE; | |
6946 | } | |
4d269e42 | 6947 | \f |
4d269e42 AM |
6948 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ |
6949 | ||
6950 | static void | |
6951 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
6952 | asection *sec) | |
6953 | { | |
dbaa2011 AM |
6954 | BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); |
6955 | sec->sec_info_type = SEC_INFO_TYPE_NONE; | |
4d269e42 AM |
6956 | } |
6957 | ||
6958 | /* Finish SHF_MERGE section merging. */ | |
6959 | ||
6960 | bfd_boolean | |
630993ec | 6961 | _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) |
4d269e42 AM |
6962 | { |
6963 | bfd *ibfd; | |
6964 | asection *sec; | |
6965 | ||
6966 | if (!is_elf_hash_table (info->hash)) | |
6967 | return FALSE; | |
6968 | ||
c72f2fb2 | 6969 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
630993ec AM |
6970 | if ((ibfd->flags & DYNAMIC) == 0 |
6971 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
017e6bce AM |
6972 | && (elf_elfheader (ibfd)->e_ident[EI_CLASS] |
6973 | == get_elf_backend_data (obfd)->s->elfclass)) | |
4d269e42 AM |
6974 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) |
6975 | if ((sec->flags & SEC_MERGE) != 0 | |
6976 | && !bfd_is_abs_section (sec->output_section)) | |
6977 | { | |
6978 | struct bfd_elf_section_data *secdata; | |
6979 | ||
6980 | secdata = elf_section_data (sec); | |
630993ec | 6981 | if (! _bfd_add_merge_section (obfd, |
4d269e42 AM |
6982 | &elf_hash_table (info)->merge_info, |
6983 | sec, &secdata->sec_info)) | |
6984 | return FALSE; | |
6985 | else if (secdata->sec_info) | |
dbaa2011 | 6986 | sec->sec_info_type = SEC_INFO_TYPE_MERGE; |
4d269e42 AM |
6987 | } |
6988 | ||
6989 | if (elf_hash_table (info)->merge_info != NULL) | |
630993ec | 6990 | _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, |
4d269e42 AM |
6991 | merge_sections_remove_hook); |
6992 | return TRUE; | |
6993 | } | |
6994 | ||
6995 | /* Create an entry in an ELF linker hash table. */ | |
6996 | ||
6997 | struct bfd_hash_entry * | |
6998 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
6999 | struct bfd_hash_table *table, | |
7000 | const char *string) | |
7001 | { | |
7002 | /* Allocate the structure if it has not already been allocated by a | |
7003 | subclass. */ | |
7004 | if (entry == NULL) | |
7005 | { | |
a50b1753 | 7006 | entry = (struct bfd_hash_entry *) |
ca4be51c | 7007 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); |
4d269e42 AM |
7008 | if (entry == NULL) |
7009 | return entry; | |
7010 | } | |
7011 | ||
7012 | /* Call the allocation method of the superclass. */ | |
7013 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
7014 | if (entry != NULL) | |
7015 | { | |
7016 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
7017 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
7018 | ||
7019 | /* Set local fields. */ | |
7020 | ret->indx = -1; | |
7021 | ret->dynindx = -1; | |
7022 | ret->got = htab->init_got_refcount; | |
7023 | ret->plt = htab->init_plt_refcount; | |
7024 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
7025 | - offsetof (struct elf_link_hash_entry, size))); | |
7026 | /* Assume that we have been called by a non-ELF symbol reader. | |
7027 | This flag is then reset by the code which reads an ELF input | |
7028 | file. This ensures that a symbol created by a non-ELF symbol | |
7029 | reader will have the flag set correctly. */ | |
7030 | ret->non_elf = 1; | |
7031 | } | |
7032 | ||
7033 | return entry; | |
7034 | } | |
7035 | ||
7036 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
7037 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
7038 | ||
7039 | void | |
7040 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
7041 | struct elf_link_hash_entry *dir, | |
7042 | struct elf_link_hash_entry *ind) | |
7043 | { | |
7044 | struct elf_link_hash_table *htab; | |
7045 | ||
7046 | /* Copy down any references that we may have already seen to the | |
6e33951e L |
7047 | symbol which just became indirect if DIR isn't a hidden versioned |
7048 | symbol. */ | |
4d269e42 | 7049 | |
422f1182 | 7050 | if (dir->versioned != versioned_hidden) |
6e33951e L |
7051 | { |
7052 | dir->ref_dynamic |= ind->ref_dynamic; | |
7053 | dir->ref_regular |= ind->ref_regular; | |
7054 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
7055 | dir->non_got_ref |= ind->non_got_ref; | |
7056 | dir->needs_plt |= ind->needs_plt; | |
7057 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
7058 | } | |
4d269e42 AM |
7059 | |
7060 | if (ind->root.type != bfd_link_hash_indirect) | |
7061 | return; | |
7062 | ||
7063 | /* Copy over the global and procedure linkage table refcount entries. | |
7064 | These may have been already set up by a check_relocs routine. */ | |
7065 | htab = elf_hash_table (info); | |
7066 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
7067 | { | |
7068 | if (dir->got.refcount < 0) | |
7069 | dir->got.refcount = 0; | |
7070 | dir->got.refcount += ind->got.refcount; | |
7071 | ind->got.refcount = htab->init_got_refcount.refcount; | |
7072 | } | |
7073 | ||
7074 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
7075 | { | |
7076 | if (dir->plt.refcount < 0) | |
7077 | dir->plt.refcount = 0; | |
7078 | dir->plt.refcount += ind->plt.refcount; | |
7079 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
7080 | } | |
7081 | ||
7082 | if (ind->dynindx != -1) | |
7083 | { | |
7084 | if (dir->dynindx != -1) | |
7085 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
7086 | dir->dynindx = ind->dynindx; | |
7087 | dir->dynstr_index = ind->dynstr_index; | |
7088 | ind->dynindx = -1; | |
7089 | ind->dynstr_index = 0; | |
7090 | } | |
7091 | } | |
7092 | ||
7093 | void | |
7094 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
7095 | struct elf_link_hash_entry *h, | |
7096 | bfd_boolean force_local) | |
7097 | { | |
3aa14d16 L |
7098 | /* STT_GNU_IFUNC symbol must go through PLT. */ |
7099 | if (h->type != STT_GNU_IFUNC) | |
7100 | { | |
7101 | h->plt = elf_hash_table (info)->init_plt_offset; | |
7102 | h->needs_plt = 0; | |
7103 | } | |
4d269e42 AM |
7104 | if (force_local) |
7105 | { | |
7106 | h->forced_local = 1; | |
7107 | if (h->dynindx != -1) | |
7108 | { | |
7109 | h->dynindx = -1; | |
7110 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, | |
7111 | h->dynstr_index); | |
7112 | } | |
7113 | } | |
7114 | } | |
7115 | ||
7bf52ea2 AM |
7116 | /* Initialize an ELF linker hash table. *TABLE has been zeroed by our |
7117 | caller. */ | |
4d269e42 AM |
7118 | |
7119 | bfd_boolean | |
7120 | _bfd_elf_link_hash_table_init | |
7121 | (struct elf_link_hash_table *table, | |
7122 | bfd *abfd, | |
7123 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
7124 | struct bfd_hash_table *, | |
7125 | const char *), | |
4dfe6ac6 NC |
7126 | unsigned int entsize, |
7127 | enum elf_target_id target_id) | |
4d269e42 AM |
7128 | { |
7129 | bfd_boolean ret; | |
7130 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; | |
7131 | ||
4d269e42 AM |
7132 | table->init_got_refcount.refcount = can_refcount - 1; |
7133 | table->init_plt_refcount.refcount = can_refcount - 1; | |
7134 | table->init_got_offset.offset = -(bfd_vma) 1; | |
7135 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
7136 | /* The first dynamic symbol is a dummy. */ | |
7137 | table->dynsymcount = 1; | |
7138 | ||
7139 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
4dfe6ac6 | 7140 | |
4d269e42 | 7141 | table->root.type = bfd_link_elf_hash_table; |
4dfe6ac6 | 7142 | table->hash_table_id = target_id; |
4d269e42 AM |
7143 | |
7144 | return ret; | |
7145 | } | |
7146 | ||
7147 | /* Create an ELF linker hash table. */ | |
7148 | ||
7149 | struct bfd_link_hash_table * | |
7150 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
7151 | { | |
7152 | struct elf_link_hash_table *ret; | |
7153 | bfd_size_type amt = sizeof (struct elf_link_hash_table); | |
7154 | ||
7bf52ea2 | 7155 | ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); |
4d269e42 AM |
7156 | if (ret == NULL) |
7157 | return NULL; | |
7158 | ||
7159 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
4dfe6ac6 NC |
7160 | sizeof (struct elf_link_hash_entry), |
7161 | GENERIC_ELF_DATA)) | |
4d269e42 AM |
7162 | { |
7163 | free (ret); | |
7164 | return NULL; | |
7165 | } | |
d495ab0d | 7166 | ret->root.hash_table_free = _bfd_elf_link_hash_table_free; |
4d269e42 AM |
7167 | |
7168 | return &ret->root; | |
7169 | } | |
7170 | ||
9f7c3e5e AM |
7171 | /* Destroy an ELF linker hash table. */ |
7172 | ||
7173 | void | |
d495ab0d | 7174 | _bfd_elf_link_hash_table_free (bfd *obfd) |
9f7c3e5e | 7175 | { |
d495ab0d AM |
7176 | struct elf_link_hash_table *htab; |
7177 | ||
7178 | htab = (struct elf_link_hash_table *) obfd->link.hash; | |
9f7c3e5e AM |
7179 | if (htab->dynstr != NULL) |
7180 | _bfd_elf_strtab_free (htab->dynstr); | |
7181 | _bfd_merge_sections_free (htab->merge_info); | |
d495ab0d | 7182 | _bfd_generic_link_hash_table_free (obfd); |
9f7c3e5e AM |
7183 | } |
7184 | ||
4d269e42 AM |
7185 | /* This is a hook for the ELF emulation code in the generic linker to |
7186 | tell the backend linker what file name to use for the DT_NEEDED | |
7187 | entry for a dynamic object. */ | |
7188 | ||
7189 | void | |
7190 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
7191 | { | |
7192 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7193 | && bfd_get_format (abfd) == bfd_object) | |
7194 | elf_dt_name (abfd) = name; | |
7195 | } | |
7196 | ||
7197 | int | |
7198 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
7199 | { | |
7200 | int lib_class; | |
7201 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7202 | && bfd_get_format (abfd) == bfd_object) | |
7203 | lib_class = elf_dyn_lib_class (abfd); | |
7204 | else | |
7205 | lib_class = 0; | |
7206 | return lib_class; | |
7207 | } | |
7208 | ||
7209 | void | |
7210 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
7211 | { | |
7212 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7213 | && bfd_get_format (abfd) == bfd_object) | |
7214 | elf_dyn_lib_class (abfd) = lib_class; | |
7215 | } | |
7216 | ||
7217 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
7218 | the linker ELF emulation code. */ | |
7219 | ||
7220 | struct bfd_link_needed_list * | |
7221 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7222 | struct bfd_link_info *info) | |
7223 | { | |
7224 | if (! is_elf_hash_table (info->hash)) | |
7225 | return NULL; | |
7226 | return elf_hash_table (info)->needed; | |
7227 | } | |
7228 | ||
7229 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
7230 | hook for the linker ELF emulation code. */ | |
7231 | ||
7232 | struct bfd_link_needed_list * | |
7233 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7234 | struct bfd_link_info *info) | |
7235 | { | |
7236 | if (! is_elf_hash_table (info->hash)) | |
7237 | return NULL; | |
7238 | return elf_hash_table (info)->runpath; | |
7239 | } | |
7240 | ||
7241 | /* Get the name actually used for a dynamic object for a link. This | |
7242 | is the SONAME entry if there is one. Otherwise, it is the string | |
7243 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
7244 | ||
7245 | const char * | |
7246 | bfd_elf_get_dt_soname (bfd *abfd) | |
7247 | { | |
7248 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7249 | && bfd_get_format (abfd) == bfd_object) | |
7250 | return elf_dt_name (abfd); | |
7251 | return NULL; | |
7252 | } | |
7253 | ||
7254 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
7255 | the ELF linker emulation code. */ | |
7256 | ||
7257 | bfd_boolean | |
7258 | bfd_elf_get_bfd_needed_list (bfd *abfd, | |
7259 | struct bfd_link_needed_list **pneeded) | |
7260 | { | |
7261 | asection *s; | |
7262 | bfd_byte *dynbuf = NULL; | |
cb33740c | 7263 | unsigned int elfsec; |
4d269e42 AM |
7264 | unsigned long shlink; |
7265 | bfd_byte *extdyn, *extdynend; | |
7266 | size_t extdynsize; | |
7267 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
7268 | ||
7269 | *pneeded = NULL; | |
7270 | ||
7271 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
7272 | || bfd_get_format (abfd) != bfd_object) | |
7273 | return TRUE; | |
7274 | ||
7275 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
7276 | if (s == NULL || s->size == 0) | |
7277 | return TRUE; | |
7278 | ||
7279 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
7280 | goto error_return; | |
7281 | ||
7282 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 7283 | if (elfsec == SHN_BAD) |
4d269e42 AM |
7284 | goto error_return; |
7285 | ||
7286 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 7287 | |
4d269e42 AM |
7288 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
7289 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
7290 | ||
7291 | extdyn = dynbuf; | |
7292 | extdynend = extdyn + s->size; | |
7293 | for (; extdyn < extdynend; extdyn += extdynsize) | |
7294 | { | |
7295 | Elf_Internal_Dyn dyn; | |
7296 | ||
7297 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
7298 | ||
7299 | if (dyn.d_tag == DT_NULL) | |
7300 | break; | |
7301 | ||
7302 | if (dyn.d_tag == DT_NEEDED) | |
7303 | { | |
7304 | const char *string; | |
7305 | struct bfd_link_needed_list *l; | |
7306 | unsigned int tagv = dyn.d_un.d_val; | |
7307 | bfd_size_type amt; | |
7308 | ||
7309 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
7310 | if (string == NULL) | |
7311 | goto error_return; | |
7312 | ||
7313 | amt = sizeof *l; | |
a50b1753 | 7314 | l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4d269e42 AM |
7315 | if (l == NULL) |
7316 | goto error_return; | |
7317 | ||
7318 | l->by = abfd; | |
7319 | l->name = string; | |
7320 | l->next = *pneeded; | |
7321 | *pneeded = l; | |
7322 | } | |
7323 | } | |
7324 | ||
7325 | free (dynbuf); | |
7326 | ||
7327 | return TRUE; | |
7328 | ||
7329 | error_return: | |
7330 | if (dynbuf != NULL) | |
7331 | free (dynbuf); | |
7332 | return FALSE; | |
7333 | } | |
7334 | ||
7335 | struct elf_symbuf_symbol | |
7336 | { | |
7337 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
7338 | unsigned char st_info; /* Type and binding attributes */ | |
7339 | unsigned char st_other; /* Visibilty, and target specific */ | |
7340 | }; | |
7341 | ||
7342 | struct elf_symbuf_head | |
7343 | { | |
7344 | struct elf_symbuf_symbol *ssym; | |
ef53be89 | 7345 | size_t count; |
4d269e42 AM |
7346 | unsigned int st_shndx; |
7347 | }; | |
7348 | ||
7349 | struct elf_symbol | |
7350 | { | |
7351 | union | |
7352 | { | |
7353 | Elf_Internal_Sym *isym; | |
7354 | struct elf_symbuf_symbol *ssym; | |
7355 | } u; | |
7356 | const char *name; | |
7357 | }; | |
7358 | ||
7359 | /* Sort references to symbols by ascending section number. */ | |
7360 | ||
7361 | static int | |
7362 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
7363 | { | |
7364 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
7365 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
7366 | ||
7367 | return s1->st_shndx - s2->st_shndx; | |
7368 | } | |
7369 | ||
7370 | static int | |
7371 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
7372 | { | |
7373 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
7374 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
7375 | return strcmp (s1->name, s2->name); | |
7376 | } | |
7377 | ||
7378 | static struct elf_symbuf_head * | |
ef53be89 | 7379 | elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) |
4d269e42 | 7380 | { |
14b1c01e | 7381 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
7382 | struct elf_symbuf_symbol *ssym; |
7383 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
ef53be89 | 7384 | size_t i, shndx_count, total_size; |
4d269e42 | 7385 | |
a50b1753 | 7386 | indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); |
4d269e42 AM |
7387 | if (indbuf == NULL) |
7388 | return NULL; | |
7389 | ||
7390 | for (ind = indbuf, i = 0; i < symcount; i++) | |
7391 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
7392 | *ind++ = &isymbuf[i]; | |
7393 | indbufend = ind; | |
7394 | ||
7395 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
7396 | elf_sort_elf_symbol); | |
7397 | ||
7398 | shndx_count = 0; | |
7399 | if (indbufend > indbuf) | |
7400 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
7401 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
7402 | shndx_count++; | |
7403 | ||
3ae181ee L |
7404 | total_size = ((shndx_count + 1) * sizeof (*ssymbuf) |
7405 | + (indbufend - indbuf) * sizeof (*ssym)); | |
a50b1753 | 7406 | ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); |
4d269e42 AM |
7407 | if (ssymbuf == NULL) |
7408 | { | |
7409 | free (indbuf); | |
7410 | return NULL; | |
7411 | } | |
7412 | ||
3ae181ee | 7413 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); |
4d269e42 AM |
7414 | ssymbuf->ssym = NULL; |
7415 | ssymbuf->count = shndx_count; | |
7416 | ssymbuf->st_shndx = 0; | |
7417 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
7418 | { | |
7419 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
7420 | { | |
7421 | ssymhead++; | |
7422 | ssymhead->ssym = ssym; | |
7423 | ssymhead->count = 0; | |
7424 | ssymhead->st_shndx = (*ind)->st_shndx; | |
7425 | } | |
7426 | ssym->st_name = (*ind)->st_name; | |
7427 | ssym->st_info = (*ind)->st_info; | |
7428 | ssym->st_other = (*ind)->st_other; | |
7429 | ssymhead->count++; | |
7430 | } | |
ef53be89 | 7431 | BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count |
3ae181ee L |
7432 | && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) |
7433 | == total_size)); | |
4d269e42 AM |
7434 | |
7435 | free (indbuf); | |
7436 | return ssymbuf; | |
7437 | } | |
7438 | ||
7439 | /* Check if 2 sections define the same set of local and global | |
7440 | symbols. */ | |
7441 | ||
8f317e31 | 7442 | static bfd_boolean |
4d269e42 AM |
7443 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
7444 | struct bfd_link_info *info) | |
7445 | { | |
7446 | bfd *bfd1, *bfd2; | |
7447 | const struct elf_backend_data *bed1, *bed2; | |
7448 | Elf_Internal_Shdr *hdr1, *hdr2; | |
ef53be89 | 7449 | size_t symcount1, symcount2; |
4d269e42 AM |
7450 | Elf_Internal_Sym *isymbuf1, *isymbuf2; |
7451 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
7452 | Elf_Internal_Sym *isym, *isymend; | |
7453 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
ef53be89 | 7454 | size_t count1, count2, i; |
cb33740c | 7455 | unsigned int shndx1, shndx2; |
4d269e42 AM |
7456 | bfd_boolean result; |
7457 | ||
7458 | bfd1 = sec1->owner; | |
7459 | bfd2 = sec2->owner; | |
7460 | ||
4d269e42 AM |
7461 | /* Both sections have to be in ELF. */ |
7462 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
7463 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
7464 | return FALSE; | |
7465 | ||
7466 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
7467 | return FALSE; | |
7468 | ||
4d269e42 AM |
7469 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
7470 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
cb33740c | 7471 | if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) |
4d269e42 AM |
7472 | return FALSE; |
7473 | ||
7474 | bed1 = get_elf_backend_data (bfd1); | |
7475 | bed2 = get_elf_backend_data (bfd2); | |
7476 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
7477 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
7478 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
7479 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
7480 | ||
7481 | if (symcount1 == 0 || symcount2 == 0) | |
7482 | return FALSE; | |
7483 | ||
7484 | result = FALSE; | |
7485 | isymbuf1 = NULL; | |
7486 | isymbuf2 = NULL; | |
a50b1753 NC |
7487 | ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; |
7488 | ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; | |
4d269e42 AM |
7489 | |
7490 | if (ssymbuf1 == NULL) | |
7491 | { | |
7492 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
7493 | NULL, NULL, NULL); | |
7494 | if (isymbuf1 == NULL) | |
7495 | goto done; | |
7496 | ||
7497 | if (!info->reduce_memory_overheads) | |
7498 | elf_tdata (bfd1)->symbuf = ssymbuf1 | |
7499 | = elf_create_symbuf (symcount1, isymbuf1); | |
7500 | } | |
7501 | ||
7502 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
7503 | { | |
7504 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
7505 | NULL, NULL, NULL); | |
7506 | if (isymbuf2 == NULL) | |
7507 | goto done; | |
7508 | ||
7509 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
7510 | elf_tdata (bfd2)->symbuf = ssymbuf2 | |
7511 | = elf_create_symbuf (symcount2, isymbuf2); | |
7512 | } | |
7513 | ||
7514 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
7515 | { | |
7516 | /* Optimized faster version. */ | |
ef53be89 | 7517 | size_t lo, hi, mid; |
4d269e42 AM |
7518 | struct elf_symbol *symp; |
7519 | struct elf_symbuf_symbol *ssym, *ssymend; | |
7520 | ||
7521 | lo = 0; | |
7522 | hi = ssymbuf1->count; | |
7523 | ssymbuf1++; | |
7524 | count1 = 0; | |
7525 | while (lo < hi) | |
7526 | { | |
7527 | mid = (lo + hi) / 2; | |
cb33740c | 7528 | if (shndx1 < ssymbuf1[mid].st_shndx) |
4d269e42 | 7529 | hi = mid; |
cb33740c | 7530 | else if (shndx1 > ssymbuf1[mid].st_shndx) |
4d269e42 AM |
7531 | lo = mid + 1; |
7532 | else | |
7533 | { | |
7534 | count1 = ssymbuf1[mid].count; | |
7535 | ssymbuf1 += mid; | |
7536 | break; | |
7537 | } | |
7538 | } | |
7539 | ||
7540 | lo = 0; | |
7541 | hi = ssymbuf2->count; | |
7542 | ssymbuf2++; | |
7543 | count2 = 0; | |
7544 | while (lo < hi) | |
7545 | { | |
7546 | mid = (lo + hi) / 2; | |
cb33740c | 7547 | if (shndx2 < ssymbuf2[mid].st_shndx) |
4d269e42 | 7548 | hi = mid; |
cb33740c | 7549 | else if (shndx2 > ssymbuf2[mid].st_shndx) |
4d269e42 AM |
7550 | lo = mid + 1; |
7551 | else | |
7552 | { | |
7553 | count2 = ssymbuf2[mid].count; | |
7554 | ssymbuf2 += mid; | |
7555 | break; | |
7556 | } | |
7557 | } | |
7558 | ||
7559 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7560 | goto done; | |
7561 | ||
ca4be51c AM |
7562 | symtable1 |
7563 | = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); | |
7564 | symtable2 | |
7565 | = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); | |
4d269e42 AM |
7566 | if (symtable1 == NULL || symtable2 == NULL) |
7567 | goto done; | |
7568 | ||
7569 | symp = symtable1; | |
7570 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
7571 | ssym < ssymend; ssym++, symp++) | |
7572 | { | |
7573 | symp->u.ssym = ssym; | |
7574 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
7575 | hdr1->sh_link, | |
7576 | ssym->st_name); | |
7577 | } | |
7578 | ||
7579 | symp = symtable2; | |
7580 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
7581 | ssym < ssymend; ssym++, symp++) | |
7582 | { | |
7583 | symp->u.ssym = ssym; | |
7584 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
7585 | hdr2->sh_link, | |
7586 | ssym->st_name); | |
7587 | } | |
7588 | ||
7589 | /* Sort symbol by name. */ | |
7590 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7591 | elf_sym_name_compare); | |
7592 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7593 | elf_sym_name_compare); | |
7594 | ||
7595 | for (i = 0; i < count1; i++) | |
7596 | /* Two symbols must have the same binding, type and name. */ | |
7597 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
7598 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
7599 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7600 | goto done; | |
7601 | ||
7602 | result = TRUE; | |
7603 | goto done; | |
7604 | } | |
7605 | ||
a50b1753 NC |
7606 | symtable1 = (struct elf_symbol *) |
7607 | bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
7608 | symtable2 = (struct elf_symbol *) | |
7609 | bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
4d269e42 AM |
7610 | if (symtable1 == NULL || symtable2 == NULL) |
7611 | goto done; | |
7612 | ||
7613 | /* Count definitions in the section. */ | |
7614 | count1 = 0; | |
7615 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
cb33740c | 7616 | if (isym->st_shndx == shndx1) |
4d269e42 AM |
7617 | symtable1[count1++].u.isym = isym; |
7618 | ||
7619 | count2 = 0; | |
7620 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
cb33740c | 7621 | if (isym->st_shndx == shndx2) |
4d269e42 AM |
7622 | symtable2[count2++].u.isym = isym; |
7623 | ||
7624 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7625 | goto done; | |
7626 | ||
7627 | for (i = 0; i < count1; i++) | |
7628 | symtable1[i].name | |
7629 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
7630 | symtable1[i].u.isym->st_name); | |
7631 | ||
7632 | for (i = 0; i < count2; i++) | |
7633 | symtable2[i].name | |
7634 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
7635 | symtable2[i].u.isym->st_name); | |
7636 | ||
7637 | /* Sort symbol by name. */ | |
7638 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7639 | elf_sym_name_compare); | |
7640 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7641 | elf_sym_name_compare); | |
7642 | ||
7643 | for (i = 0; i < count1; i++) | |
7644 | /* Two symbols must have the same binding, type and name. */ | |
7645 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
7646 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
7647 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7648 | goto done; | |
7649 | ||
7650 | result = TRUE; | |
7651 | ||
7652 | done: | |
7653 | if (symtable1) | |
7654 | free (symtable1); | |
7655 | if (symtable2) | |
7656 | free (symtable2); | |
7657 | if (isymbuf1) | |
7658 | free (isymbuf1); | |
7659 | if (isymbuf2) | |
7660 | free (isymbuf2); | |
7661 | ||
7662 | return result; | |
7663 | } | |
7664 | ||
7665 | /* Return TRUE if 2 section types are compatible. */ | |
7666 | ||
7667 | bfd_boolean | |
7668 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
7669 | bfd *bbfd, const asection *bsec) | |
7670 | { | |
7671 | if (asec == NULL | |
7672 | || bsec == NULL | |
7673 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
7674 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
7675 | return TRUE; | |
7676 | ||
7677 | return elf_section_type (asec) == elf_section_type (bsec); | |
7678 | } | |
7679 | \f | |
c152c796 AM |
7680 | /* Final phase of ELF linker. */ |
7681 | ||
7682 | /* A structure we use to avoid passing large numbers of arguments. */ | |
7683 | ||
7684 | struct elf_final_link_info | |
7685 | { | |
7686 | /* General link information. */ | |
7687 | struct bfd_link_info *info; | |
7688 | /* Output BFD. */ | |
7689 | bfd *output_bfd; | |
7690 | /* Symbol string table. */ | |
ef10c3ac | 7691 | struct elf_strtab_hash *symstrtab; |
c152c796 AM |
7692 | /* .hash section. */ |
7693 | asection *hash_sec; | |
7694 | /* symbol version section (.gnu.version). */ | |
7695 | asection *symver_sec; | |
7696 | /* Buffer large enough to hold contents of any section. */ | |
7697 | bfd_byte *contents; | |
7698 | /* Buffer large enough to hold external relocs of any section. */ | |
7699 | void *external_relocs; | |
7700 | /* Buffer large enough to hold internal relocs of any section. */ | |
7701 | Elf_Internal_Rela *internal_relocs; | |
7702 | /* Buffer large enough to hold external local symbols of any input | |
7703 | BFD. */ | |
7704 | bfd_byte *external_syms; | |
7705 | /* And a buffer for symbol section indices. */ | |
7706 | Elf_External_Sym_Shndx *locsym_shndx; | |
7707 | /* Buffer large enough to hold internal local symbols of any input | |
7708 | BFD. */ | |
7709 | Elf_Internal_Sym *internal_syms; | |
7710 | /* Array large enough to hold a symbol index for each local symbol | |
7711 | of any input BFD. */ | |
7712 | long *indices; | |
7713 | /* Array large enough to hold a section pointer for each local | |
7714 | symbol of any input BFD. */ | |
7715 | asection **sections; | |
ef10c3ac | 7716 | /* Buffer for SHT_SYMTAB_SHNDX section. */ |
c152c796 | 7717 | Elf_External_Sym_Shndx *symshndxbuf; |
ffbc01cc AM |
7718 | /* Number of STT_FILE syms seen. */ |
7719 | size_t filesym_count; | |
c152c796 AM |
7720 | }; |
7721 | ||
7722 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
7723 | ||
7724 | struct elf_outext_info | |
7725 | { | |
7726 | bfd_boolean failed; | |
7727 | bfd_boolean localsyms; | |
34a79995 | 7728 | bfd_boolean file_sym_done; |
8b127cbc | 7729 | struct elf_final_link_info *flinfo; |
c152c796 AM |
7730 | }; |
7731 | ||
d9352518 DB |
7732 | |
7733 | /* Support for evaluating a complex relocation. | |
7734 | ||
7735 | Complex relocations are generalized, self-describing relocations. The | |
7736 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 7737 | relocations themselves. |
d9352518 DB |
7738 | |
7739 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
7740 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
7741 | information (start bit, end bit, word width, etc) into the addend. This | |
7742 | information is extracted from CGEN-generated operand tables within gas. | |
7743 | ||
7744 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
7745 | internal) representing prefix-notation expressions, including but not | |
7746 | limited to those sorts of expressions normally encoded as addends in the | |
7747 | addend field. The symbol mangling format is: | |
7748 | ||
7749 | <node> := <literal> | |
7750 | | <unary-operator> ':' <node> | |
7751 | | <binary-operator> ':' <node> ':' <node> | |
7752 | ; | |
7753 | ||
7754 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
7755 | | 'S' <digits=N> ':' <N character section name> | |
7756 | | '#' <hexdigits> | |
7757 | ; | |
7758 | ||
7759 | <binary-operator> := as in C | |
7760 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
7761 | ||
7762 | static void | |
a0c8462f AM |
7763 | set_symbol_value (bfd *bfd_with_globals, |
7764 | Elf_Internal_Sym *isymbuf, | |
7765 | size_t locsymcount, | |
7766 | size_t symidx, | |
7767 | bfd_vma val) | |
d9352518 | 7768 | { |
8977835c AM |
7769 | struct elf_link_hash_entry **sym_hashes; |
7770 | struct elf_link_hash_entry *h; | |
7771 | size_t extsymoff = locsymcount; | |
d9352518 | 7772 | |
8977835c | 7773 | if (symidx < locsymcount) |
d9352518 | 7774 | { |
8977835c AM |
7775 | Elf_Internal_Sym *sym; |
7776 | ||
7777 | sym = isymbuf + symidx; | |
7778 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
7779 | { | |
7780 | /* It is a local symbol: move it to the | |
7781 | "absolute" section and give it a value. */ | |
7782 | sym->st_shndx = SHN_ABS; | |
7783 | sym->st_value = val; | |
7784 | return; | |
7785 | } | |
7786 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
7787 | extsymoff = 0; | |
d9352518 | 7788 | } |
8977835c AM |
7789 | |
7790 | /* It is a global symbol: set its link type | |
7791 | to "defined" and give it a value. */ | |
7792 | ||
7793 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
7794 | h = sym_hashes [symidx - extsymoff]; | |
7795 | while (h->root.type == bfd_link_hash_indirect | |
7796 | || h->root.type == bfd_link_hash_warning) | |
7797 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7798 | h->root.type = bfd_link_hash_defined; | |
7799 | h->root.u.def.value = val; | |
7800 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
7801 | } |
7802 | ||
a0c8462f AM |
7803 | static bfd_boolean |
7804 | resolve_symbol (const char *name, | |
7805 | bfd *input_bfd, | |
8b127cbc | 7806 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
7807 | bfd_vma *result, |
7808 | Elf_Internal_Sym *isymbuf, | |
7809 | size_t locsymcount) | |
d9352518 | 7810 | { |
a0c8462f AM |
7811 | Elf_Internal_Sym *sym; |
7812 | struct bfd_link_hash_entry *global_entry; | |
7813 | const char *candidate = NULL; | |
7814 | Elf_Internal_Shdr *symtab_hdr; | |
7815 | size_t i; | |
7816 | ||
d9352518 DB |
7817 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
7818 | ||
7819 | for (i = 0; i < locsymcount; ++ i) | |
7820 | { | |
8977835c | 7821 | sym = isymbuf + i; |
d9352518 DB |
7822 | |
7823 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
7824 | continue; | |
7825 | ||
7826 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
7827 | symtab_hdr->sh_link, | |
7828 | sym->st_name); | |
7829 | #ifdef DEBUG | |
0f02bbd9 AM |
7830 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
7831 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
7832 | #endif |
7833 | if (candidate && strcmp (candidate, name) == 0) | |
7834 | { | |
8b127cbc | 7835 | asection *sec = flinfo->sections [i]; |
d9352518 | 7836 | |
0f02bbd9 AM |
7837 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
7838 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 7839 | #ifdef DEBUG |
0f02bbd9 AM |
7840 | printf ("Found symbol with value %8.8lx\n", |
7841 | (unsigned long) *result); | |
d9352518 DB |
7842 | #endif |
7843 | return TRUE; | |
7844 | } | |
7845 | } | |
7846 | ||
7847 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
8b127cbc | 7848 | global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, |
a0c8462f | 7849 | FALSE, FALSE, TRUE); |
d9352518 DB |
7850 | if (!global_entry) |
7851 | return FALSE; | |
a0c8462f | 7852 | |
d9352518 DB |
7853 | if (global_entry->type == bfd_link_hash_defined |
7854 | || global_entry->type == bfd_link_hash_defweak) | |
7855 | { | |
a0c8462f AM |
7856 | *result = (global_entry->u.def.value |
7857 | + global_entry->u.def.section->output_section->vma | |
7858 | + global_entry->u.def.section->output_offset); | |
d9352518 | 7859 | #ifdef DEBUG |
0f02bbd9 AM |
7860 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
7861 | global_entry->root.string, (unsigned long) *result); | |
d9352518 DB |
7862 | #endif |
7863 | return TRUE; | |
a0c8462f | 7864 | } |
d9352518 | 7865 | |
d9352518 DB |
7866 | return FALSE; |
7867 | } | |
7868 | ||
37b01f6a DG |
7869 | /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in |
7870 | bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section | |
7871 | names like "foo.end" which is the end address of section "foo". */ | |
7872 | ||
d9352518 | 7873 | static bfd_boolean |
a0c8462f AM |
7874 | resolve_section (const char *name, |
7875 | asection *sections, | |
37b01f6a DG |
7876 | bfd_vma *result, |
7877 | bfd * abfd) | |
d9352518 | 7878 | { |
a0c8462f AM |
7879 | asection *curr; |
7880 | unsigned int len; | |
d9352518 | 7881 | |
a0c8462f | 7882 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
7883 | if (strcmp (curr->name, name) == 0) |
7884 | { | |
7885 | *result = curr->vma; | |
7886 | return TRUE; | |
7887 | } | |
7888 | ||
7889 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
37b01f6a | 7890 | /* FIXME: This could be coded more efficiently... */ |
a0c8462f | 7891 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
7892 | { |
7893 | len = strlen (curr->name); | |
a0c8462f | 7894 | if (len > strlen (name)) |
d9352518 DB |
7895 | continue; |
7896 | ||
7897 | if (strncmp (curr->name, name, len) == 0) | |
7898 | { | |
7899 | if (strncmp (".end", name + len, 4) == 0) | |
7900 | { | |
37b01f6a | 7901 | *result = curr->vma + curr->size / bfd_octets_per_byte (abfd); |
d9352518 DB |
7902 | return TRUE; |
7903 | } | |
7904 | ||
7905 | /* Insert more pseudo-section names here, if you like. */ | |
7906 | } | |
7907 | } | |
a0c8462f | 7908 | |
d9352518 DB |
7909 | return FALSE; |
7910 | } | |
7911 | ||
7912 | static void | |
a0c8462f | 7913 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 7914 | { |
695344c0 | 7915 | /* xgettext:c-format */ |
a0c8462f AM |
7916 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
7917 | reftype, name); | |
d9352518 DB |
7918 | } |
7919 | ||
7920 | static bfd_boolean | |
a0c8462f AM |
7921 | eval_symbol (bfd_vma *result, |
7922 | const char **symp, | |
7923 | bfd *input_bfd, | |
8b127cbc | 7924 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
7925 | bfd_vma dot, |
7926 | Elf_Internal_Sym *isymbuf, | |
7927 | size_t locsymcount, | |
7928 | int signed_p) | |
d9352518 | 7929 | { |
4b93929b NC |
7930 | size_t len; |
7931 | size_t symlen; | |
a0c8462f AM |
7932 | bfd_vma a; |
7933 | bfd_vma b; | |
4b93929b | 7934 | char symbuf[4096]; |
0f02bbd9 | 7935 | const char *sym = *symp; |
a0c8462f AM |
7936 | const char *symend; |
7937 | bfd_boolean symbol_is_section = FALSE; | |
d9352518 DB |
7938 | |
7939 | len = strlen (sym); | |
7940 | symend = sym + len; | |
7941 | ||
4b93929b | 7942 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
7943 | { |
7944 | bfd_set_error (bfd_error_invalid_operation); | |
7945 | return FALSE; | |
7946 | } | |
a0c8462f | 7947 | |
d9352518 DB |
7948 | switch (* sym) |
7949 | { | |
7950 | case '.': | |
0f02bbd9 AM |
7951 | *result = dot; |
7952 | *symp = sym + 1; | |
d9352518 DB |
7953 | return TRUE; |
7954 | ||
7955 | case '#': | |
0f02bbd9 AM |
7956 | ++sym; |
7957 | *result = strtoul (sym, (char **) symp, 16); | |
d9352518 DB |
7958 | return TRUE; |
7959 | ||
7960 | case 'S': | |
7961 | symbol_is_section = TRUE; | |
1a0670f3 | 7962 | /* Fall through. */ |
a0c8462f | 7963 | case 's': |
0f02bbd9 AM |
7964 | ++sym; |
7965 | symlen = strtol (sym, (char **) symp, 10); | |
7966 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 7967 | |
4b93929b | 7968 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
7969 | { |
7970 | bfd_set_error (bfd_error_invalid_operation); | |
7971 | return FALSE; | |
7972 | } | |
7973 | ||
7974 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 7975 | symbuf[symlen] = '\0'; |
0f02bbd9 | 7976 | *symp = sym + symlen; |
a0c8462f AM |
7977 | |
7978 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
7979 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
7980 | interpretation here; section means "try section first", not "must be a | |
7981 | section", and likewise with symbol. */ | |
7982 | ||
a0c8462f | 7983 | if (symbol_is_section) |
d9352518 | 7984 | { |
37b01f6a | 7985 | if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) |
8b127cbc | 7986 | && !resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 7987 | isymbuf, locsymcount)) |
d9352518 DB |
7988 | { |
7989 | undefined_reference ("section", symbuf); | |
7990 | return FALSE; | |
7991 | } | |
a0c8462f AM |
7992 | } |
7993 | else | |
d9352518 | 7994 | { |
8b127cbc | 7995 | if (!resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 7996 | isymbuf, locsymcount) |
8b127cbc | 7997 | && !resolve_section (symbuf, flinfo->output_bfd->sections, |
37b01f6a | 7998 | result, input_bfd)) |
d9352518 DB |
7999 | { |
8000 | undefined_reference ("symbol", symbuf); | |
8001 | return FALSE; | |
8002 | } | |
8003 | } | |
8004 | ||
8005 | return TRUE; | |
a0c8462f | 8006 | |
d9352518 DB |
8007 | /* All that remains are operators. */ |
8008 | ||
8009 | #define UNARY_OP(op) \ | |
8010 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8011 | { \ | |
8012 | sym += strlen (#op); \ | |
a0c8462f AM |
8013 | if (*sym == ':') \ |
8014 | ++sym; \ | |
0f02bbd9 | 8015 | *symp = sym; \ |
8b127cbc | 8016 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8017 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8018 | return FALSE; \ |
8019 | if (signed_p) \ | |
0f02bbd9 | 8020 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
8021 | else \ |
8022 | *result = op a; \ | |
d9352518 DB |
8023 | return TRUE; \ |
8024 | } | |
8025 | ||
8026 | #define BINARY_OP(op) \ | |
8027 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8028 | { \ | |
8029 | sym += strlen (#op); \ | |
a0c8462f AM |
8030 | if (*sym == ':') \ |
8031 | ++sym; \ | |
0f02bbd9 | 8032 | *symp = sym; \ |
8b127cbc | 8033 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8034 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f | 8035 | return FALSE; \ |
0f02bbd9 | 8036 | ++*symp; \ |
8b127cbc | 8037 | if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8038 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8039 | return FALSE; \ |
8040 | if (signed_p) \ | |
0f02bbd9 | 8041 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
8042 | else \ |
8043 | *result = a op b; \ | |
d9352518 DB |
8044 | return TRUE; \ |
8045 | } | |
8046 | ||
8047 | default: | |
8048 | UNARY_OP (0-); | |
8049 | BINARY_OP (<<); | |
8050 | BINARY_OP (>>); | |
8051 | BINARY_OP (==); | |
8052 | BINARY_OP (!=); | |
8053 | BINARY_OP (<=); | |
8054 | BINARY_OP (>=); | |
8055 | BINARY_OP (&&); | |
8056 | BINARY_OP (||); | |
8057 | UNARY_OP (~); | |
8058 | UNARY_OP (!); | |
8059 | BINARY_OP (*); | |
8060 | BINARY_OP (/); | |
8061 | BINARY_OP (%); | |
8062 | BINARY_OP (^); | |
8063 | BINARY_OP (|); | |
8064 | BINARY_OP (&); | |
8065 | BINARY_OP (+); | |
8066 | BINARY_OP (-); | |
8067 | BINARY_OP (<); | |
8068 | BINARY_OP (>); | |
8069 | #undef UNARY_OP | |
8070 | #undef BINARY_OP | |
8071 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
8072 | bfd_set_error (bfd_error_invalid_operation); | |
8073 | return FALSE; | |
8074 | } | |
8075 | } | |
8076 | ||
d9352518 | 8077 | static void |
a0c8462f AM |
8078 | put_value (bfd_vma size, |
8079 | unsigned long chunksz, | |
8080 | bfd *input_bfd, | |
8081 | bfd_vma x, | |
8082 | bfd_byte *location) | |
d9352518 DB |
8083 | { |
8084 | location += (size - chunksz); | |
8085 | ||
41cd1ad1 | 8086 | for (; size; size -= chunksz, location -= chunksz) |
d9352518 DB |
8087 | { |
8088 | switch (chunksz) | |
8089 | { | |
d9352518 DB |
8090 | case 1: |
8091 | bfd_put_8 (input_bfd, x, location); | |
41cd1ad1 | 8092 | x >>= 8; |
d9352518 DB |
8093 | break; |
8094 | case 2: | |
8095 | bfd_put_16 (input_bfd, x, location); | |
41cd1ad1 | 8096 | x >>= 16; |
d9352518 DB |
8097 | break; |
8098 | case 4: | |
8099 | bfd_put_32 (input_bfd, x, location); | |
65164438 NC |
8100 | /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ |
8101 | x >>= 16; | |
8102 | x >>= 16; | |
d9352518 | 8103 | break; |
d9352518 | 8104 | #ifdef BFD64 |
41cd1ad1 | 8105 | case 8: |
d9352518 | 8106 | bfd_put_64 (input_bfd, x, location); |
41cd1ad1 NC |
8107 | /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ |
8108 | x >>= 32; | |
8109 | x >>= 32; | |
8110 | break; | |
d9352518 | 8111 | #endif |
41cd1ad1 NC |
8112 | default: |
8113 | abort (); | |
d9352518 DB |
8114 | break; |
8115 | } | |
8116 | } | |
8117 | } | |
8118 | ||
a0c8462f AM |
8119 | static bfd_vma |
8120 | get_value (bfd_vma size, | |
8121 | unsigned long chunksz, | |
8122 | bfd *input_bfd, | |
8123 | bfd_byte *location) | |
d9352518 | 8124 | { |
9b239e0e | 8125 | int shift; |
d9352518 DB |
8126 | bfd_vma x = 0; |
8127 | ||
9b239e0e NC |
8128 | /* Sanity checks. */ |
8129 | BFD_ASSERT (chunksz <= sizeof (x) | |
8130 | && size >= chunksz | |
8131 | && chunksz != 0 | |
8132 | && (size % chunksz) == 0 | |
8133 | && input_bfd != NULL | |
8134 | && location != NULL); | |
8135 | ||
8136 | if (chunksz == sizeof (x)) | |
8137 | { | |
8138 | BFD_ASSERT (size == chunksz); | |
8139 | ||
8140 | /* Make sure that we do not perform an undefined shift operation. | |
8141 | We know that size == chunksz so there will only be one iteration | |
8142 | of the loop below. */ | |
8143 | shift = 0; | |
8144 | } | |
8145 | else | |
8146 | shift = 8 * chunksz; | |
8147 | ||
a0c8462f | 8148 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
8149 | { |
8150 | switch (chunksz) | |
8151 | { | |
d9352518 | 8152 | case 1: |
9b239e0e | 8153 | x = (x << shift) | bfd_get_8 (input_bfd, location); |
d9352518 DB |
8154 | break; |
8155 | case 2: | |
9b239e0e | 8156 | x = (x << shift) | bfd_get_16 (input_bfd, location); |
d9352518 DB |
8157 | break; |
8158 | case 4: | |
9b239e0e | 8159 | x = (x << shift) | bfd_get_32 (input_bfd, location); |
d9352518 | 8160 | break; |
d9352518 | 8161 | #ifdef BFD64 |
9b239e0e NC |
8162 | case 8: |
8163 | x = (x << shift) | bfd_get_64 (input_bfd, location); | |
d9352518 | 8164 | break; |
9b239e0e NC |
8165 | #endif |
8166 | default: | |
8167 | abort (); | |
d9352518 DB |
8168 | } |
8169 | } | |
8170 | return x; | |
8171 | } | |
8172 | ||
a0c8462f AM |
8173 | static void |
8174 | decode_complex_addend (unsigned long *start, /* in bits */ | |
8175 | unsigned long *oplen, /* in bits */ | |
8176 | unsigned long *len, /* in bits */ | |
8177 | unsigned long *wordsz, /* in bytes */ | |
8178 | unsigned long *chunksz, /* in bytes */ | |
8179 | unsigned long *lsb0_p, | |
8180 | unsigned long *signed_p, | |
8181 | unsigned long *trunc_p, | |
8182 | unsigned long encoded) | |
d9352518 DB |
8183 | { |
8184 | * start = encoded & 0x3F; | |
8185 | * len = (encoded >> 6) & 0x3F; | |
8186 | * oplen = (encoded >> 12) & 0x3F; | |
8187 | * wordsz = (encoded >> 18) & 0xF; | |
8188 | * chunksz = (encoded >> 22) & 0xF; | |
8189 | * lsb0_p = (encoded >> 27) & 1; | |
8190 | * signed_p = (encoded >> 28) & 1; | |
8191 | * trunc_p = (encoded >> 29) & 1; | |
8192 | } | |
8193 | ||
cdfeee4f | 8194 | bfd_reloc_status_type |
0f02bbd9 | 8195 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
cdfeee4f | 8196 | asection *input_section ATTRIBUTE_UNUSED, |
0f02bbd9 AM |
8197 | bfd_byte *contents, |
8198 | Elf_Internal_Rela *rel, | |
8199 | bfd_vma relocation) | |
d9352518 | 8200 | { |
0f02bbd9 AM |
8201 | bfd_vma shift, x, mask; |
8202 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 8203 | bfd_reloc_status_type r; |
d9352518 DB |
8204 | |
8205 | /* Perform this reloc, since it is complex. | |
8206 | (this is not to say that it necessarily refers to a complex | |
8207 | symbol; merely that it is a self-describing CGEN based reloc. | |
8208 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 8209 | word size, etc) encoded within it.). */ |
d9352518 | 8210 | |
a0c8462f AM |
8211 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
8212 | &chunksz, &lsb0_p, &signed_p, | |
8213 | &trunc_p, rel->r_addend); | |
d9352518 DB |
8214 | |
8215 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
8216 | ||
8217 | if (lsb0_p) | |
8218 | shift = (start + 1) - len; | |
8219 | else | |
8220 | shift = (8 * wordsz) - (start + len); | |
8221 | ||
37b01f6a DG |
8222 | x = get_value (wordsz, chunksz, input_bfd, |
8223 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
d9352518 DB |
8224 | |
8225 | #ifdef DEBUG | |
8226 | printf ("Doing complex reloc: " | |
8227 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
8228 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
8229 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
8230 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
9ccb8af9 AM |
8231 | oplen, (unsigned long) x, (unsigned long) mask, |
8232 | (unsigned long) relocation); | |
d9352518 DB |
8233 | #endif |
8234 | ||
cdfeee4f | 8235 | r = bfd_reloc_ok; |
d9352518 | 8236 | if (! trunc_p) |
cdfeee4f AM |
8237 | /* Now do an overflow check. */ |
8238 | r = bfd_check_overflow ((signed_p | |
8239 | ? complain_overflow_signed | |
8240 | : complain_overflow_unsigned), | |
8241 | len, 0, (8 * wordsz), | |
8242 | relocation); | |
a0c8462f | 8243 | |
d9352518 DB |
8244 | /* Do the deed. */ |
8245 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
8246 | ||
8247 | #ifdef DEBUG | |
8248 | printf (" relocation: %8.8lx\n" | |
8249 | " shifted mask: %8.8lx\n" | |
8250 | " shifted/masked reloc: %8.8lx\n" | |
8251 | " result: %8.8lx\n", | |
9ccb8af9 AM |
8252 | (unsigned long) relocation, (unsigned long) (mask << shift), |
8253 | (unsigned long) ((relocation & mask) << shift), (unsigned long) x); | |
d9352518 | 8254 | #endif |
37b01f6a DG |
8255 | put_value (wordsz, chunksz, input_bfd, x, |
8256 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
cdfeee4f | 8257 | return r; |
d9352518 DB |
8258 | } |
8259 | ||
0e287786 AM |
8260 | /* Functions to read r_offset from external (target order) reloc |
8261 | entry. Faster than bfd_getl32 et al, because we let the compiler | |
8262 | know the value is aligned. */ | |
53df40a4 | 8263 | |
0e287786 AM |
8264 | static bfd_vma |
8265 | ext32l_r_offset (const void *p) | |
53df40a4 AM |
8266 | { |
8267 | union aligned32 | |
8268 | { | |
8269 | uint32_t v; | |
8270 | unsigned char c[4]; | |
8271 | }; | |
8272 | const union aligned32 *a | |
0e287786 | 8273 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8274 | |
8275 | uint32_t aval = ( (uint32_t) a->c[0] | |
8276 | | (uint32_t) a->c[1] << 8 | |
8277 | | (uint32_t) a->c[2] << 16 | |
8278 | | (uint32_t) a->c[3] << 24); | |
0e287786 | 8279 | return aval; |
53df40a4 AM |
8280 | } |
8281 | ||
0e287786 AM |
8282 | static bfd_vma |
8283 | ext32b_r_offset (const void *p) | |
53df40a4 AM |
8284 | { |
8285 | union aligned32 | |
8286 | { | |
8287 | uint32_t v; | |
8288 | unsigned char c[4]; | |
8289 | }; | |
8290 | const union aligned32 *a | |
0e287786 | 8291 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8292 | |
8293 | uint32_t aval = ( (uint32_t) a->c[0] << 24 | |
8294 | | (uint32_t) a->c[1] << 16 | |
8295 | | (uint32_t) a->c[2] << 8 | |
8296 | | (uint32_t) a->c[3]); | |
0e287786 | 8297 | return aval; |
53df40a4 AM |
8298 | } |
8299 | ||
8300 | #ifdef BFD_HOST_64_BIT | |
0e287786 AM |
8301 | static bfd_vma |
8302 | ext64l_r_offset (const void *p) | |
53df40a4 AM |
8303 | { |
8304 | union aligned64 | |
8305 | { | |
8306 | uint64_t v; | |
8307 | unsigned char c[8]; | |
8308 | }; | |
8309 | const union aligned64 *a | |
0e287786 | 8310 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8311 | |
8312 | uint64_t aval = ( (uint64_t) a->c[0] | |
8313 | | (uint64_t) a->c[1] << 8 | |
8314 | | (uint64_t) a->c[2] << 16 | |
8315 | | (uint64_t) a->c[3] << 24 | |
8316 | | (uint64_t) a->c[4] << 32 | |
8317 | | (uint64_t) a->c[5] << 40 | |
8318 | | (uint64_t) a->c[6] << 48 | |
8319 | | (uint64_t) a->c[7] << 56); | |
0e287786 | 8320 | return aval; |
53df40a4 AM |
8321 | } |
8322 | ||
0e287786 AM |
8323 | static bfd_vma |
8324 | ext64b_r_offset (const void *p) | |
53df40a4 AM |
8325 | { |
8326 | union aligned64 | |
8327 | { | |
8328 | uint64_t v; | |
8329 | unsigned char c[8]; | |
8330 | }; | |
8331 | const union aligned64 *a | |
0e287786 | 8332 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8333 | |
8334 | uint64_t aval = ( (uint64_t) a->c[0] << 56 | |
8335 | | (uint64_t) a->c[1] << 48 | |
8336 | | (uint64_t) a->c[2] << 40 | |
8337 | | (uint64_t) a->c[3] << 32 | |
8338 | | (uint64_t) a->c[4] << 24 | |
8339 | | (uint64_t) a->c[5] << 16 | |
8340 | | (uint64_t) a->c[6] << 8 | |
8341 | | (uint64_t) a->c[7]); | |
0e287786 | 8342 | return aval; |
53df40a4 AM |
8343 | } |
8344 | #endif | |
8345 | ||
c152c796 AM |
8346 | /* When performing a relocatable link, the input relocations are |
8347 | preserved. But, if they reference global symbols, the indices | |
d4730f92 BS |
8348 | referenced must be updated. Update all the relocations found in |
8349 | RELDATA. */ | |
c152c796 | 8350 | |
bca6d0e3 | 8351 | static bfd_boolean |
c152c796 | 8352 | elf_link_adjust_relocs (bfd *abfd, |
9eaff861 | 8353 | asection *sec, |
28dbcedc AM |
8354 | struct bfd_elf_section_reloc_data *reldata, |
8355 | bfd_boolean sort) | |
c152c796 AM |
8356 | { |
8357 | unsigned int i; | |
8358 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8359 | bfd_byte *erela; | |
8360 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
8361 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8362 | bfd_vma r_type_mask; | |
8363 | int r_sym_shift; | |
d4730f92 BS |
8364 | unsigned int count = reldata->count; |
8365 | struct elf_link_hash_entry **rel_hash = reldata->hashes; | |
c152c796 | 8366 | |
d4730f92 | 8367 | if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) |
c152c796 AM |
8368 | { |
8369 | swap_in = bed->s->swap_reloc_in; | |
8370 | swap_out = bed->s->swap_reloc_out; | |
8371 | } | |
d4730f92 | 8372 | else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) |
c152c796 AM |
8373 | { |
8374 | swap_in = bed->s->swap_reloca_in; | |
8375 | swap_out = bed->s->swap_reloca_out; | |
8376 | } | |
8377 | else | |
8378 | abort (); | |
8379 | ||
8380 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
8381 | abort (); | |
8382 | ||
8383 | if (bed->s->arch_size == 32) | |
8384 | { | |
8385 | r_type_mask = 0xff; | |
8386 | r_sym_shift = 8; | |
8387 | } | |
8388 | else | |
8389 | { | |
8390 | r_type_mask = 0xffffffff; | |
8391 | r_sym_shift = 32; | |
8392 | } | |
8393 | ||
d4730f92 BS |
8394 | erela = reldata->hdr->contents; |
8395 | for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) | |
c152c796 AM |
8396 | { |
8397 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
8398 | unsigned int j; | |
8399 | ||
8400 | if (*rel_hash == NULL) | |
8401 | continue; | |
8402 | ||
8403 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
8404 | ||
8405 | (*swap_in) (abfd, erela, irela); | |
8406 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
8407 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
8408 | | (irela[j].r_info & r_type_mask)); | |
8409 | (*swap_out) (abfd, irela, erela); | |
8410 | } | |
53df40a4 | 8411 | |
9eaff861 AO |
8412 | if (bed->elf_backend_update_relocs) |
8413 | (*bed->elf_backend_update_relocs) (sec, reldata); | |
8414 | ||
0e287786 | 8415 | if (sort && count != 0) |
53df40a4 | 8416 | { |
0e287786 AM |
8417 | bfd_vma (*ext_r_off) (const void *); |
8418 | bfd_vma r_off; | |
8419 | size_t elt_size; | |
8420 | bfd_byte *base, *end, *p, *loc; | |
bca6d0e3 | 8421 | bfd_byte *buf = NULL; |
28dbcedc AM |
8422 | |
8423 | if (bed->s->arch_size == 32) | |
8424 | { | |
8425 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) | |
0e287786 | 8426 | ext_r_off = ext32l_r_offset; |
28dbcedc | 8427 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8428 | ext_r_off = ext32b_r_offset; |
28dbcedc AM |
8429 | else |
8430 | abort (); | |
8431 | } | |
53df40a4 | 8432 | else |
28dbcedc | 8433 | { |
53df40a4 | 8434 | #ifdef BFD_HOST_64_BIT |
28dbcedc | 8435 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) |
0e287786 | 8436 | ext_r_off = ext64l_r_offset; |
28dbcedc | 8437 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8438 | ext_r_off = ext64b_r_offset; |
28dbcedc | 8439 | else |
53df40a4 | 8440 | #endif |
28dbcedc AM |
8441 | abort (); |
8442 | } | |
0e287786 | 8443 | |
bca6d0e3 AM |
8444 | /* Must use a stable sort here. A modified insertion sort, |
8445 | since the relocs are mostly sorted already. */ | |
0e287786 AM |
8446 | elt_size = reldata->hdr->sh_entsize; |
8447 | base = reldata->hdr->contents; | |
8448 | end = base + count * elt_size; | |
bca6d0e3 | 8449 | if (elt_size > sizeof (Elf64_External_Rela)) |
0e287786 AM |
8450 | abort (); |
8451 | ||
8452 | /* Ensure the first element is lowest. This acts as a sentinel, | |
8453 | speeding the main loop below. */ | |
8454 | r_off = (*ext_r_off) (base); | |
8455 | for (p = loc = base; (p += elt_size) < end; ) | |
8456 | { | |
8457 | bfd_vma r_off2 = (*ext_r_off) (p); | |
8458 | if (r_off > r_off2) | |
8459 | { | |
8460 | r_off = r_off2; | |
8461 | loc = p; | |
8462 | } | |
8463 | } | |
8464 | if (loc != base) | |
8465 | { | |
8466 | /* Don't just swap *base and *loc as that changes the order | |
8467 | of the original base[0] and base[1] if they happen to | |
8468 | have the same r_offset. */ | |
bca6d0e3 AM |
8469 | bfd_byte onebuf[sizeof (Elf64_External_Rela)]; |
8470 | memcpy (onebuf, loc, elt_size); | |
0e287786 | 8471 | memmove (base + elt_size, base, loc - base); |
bca6d0e3 | 8472 | memcpy (base, onebuf, elt_size); |
0e287786 AM |
8473 | } |
8474 | ||
b29b8669 | 8475 | for (p = base + elt_size; (p += elt_size) < end; ) |
0e287786 AM |
8476 | { |
8477 | /* base to p is sorted, *p is next to insert. */ | |
8478 | r_off = (*ext_r_off) (p); | |
8479 | /* Search the sorted region for location to insert. */ | |
8480 | loc = p - elt_size; | |
8481 | while (r_off < (*ext_r_off) (loc)) | |
8482 | loc -= elt_size; | |
8483 | loc += elt_size; | |
8484 | if (loc != p) | |
8485 | { | |
bca6d0e3 AM |
8486 | /* Chances are there is a run of relocs to insert here, |
8487 | from one of more input files. Files are not always | |
8488 | linked in order due to the way elf_link_input_bfd is | |
8489 | called. See pr17666. */ | |
8490 | size_t sortlen = p - loc; | |
8491 | bfd_vma r_off2 = (*ext_r_off) (loc); | |
8492 | size_t runlen = elt_size; | |
8493 | size_t buf_size = 96 * 1024; | |
8494 | while (p + runlen < end | |
8495 | && (sortlen <= buf_size | |
8496 | || runlen + elt_size <= buf_size) | |
8497 | && r_off2 > (*ext_r_off) (p + runlen)) | |
8498 | runlen += elt_size; | |
8499 | if (buf == NULL) | |
8500 | { | |
8501 | buf = bfd_malloc (buf_size); | |
8502 | if (buf == NULL) | |
8503 | return FALSE; | |
8504 | } | |
8505 | if (runlen < sortlen) | |
8506 | { | |
8507 | memcpy (buf, p, runlen); | |
8508 | memmove (loc + runlen, loc, sortlen); | |
8509 | memcpy (loc, buf, runlen); | |
8510 | } | |
8511 | else | |
8512 | { | |
8513 | memcpy (buf, loc, sortlen); | |
8514 | memmove (loc, p, runlen); | |
8515 | memcpy (loc + runlen, buf, sortlen); | |
8516 | } | |
b29b8669 | 8517 | p += runlen - elt_size; |
0e287786 AM |
8518 | } |
8519 | } | |
8520 | /* Hashes are no longer valid. */ | |
28dbcedc AM |
8521 | free (reldata->hashes); |
8522 | reldata->hashes = NULL; | |
bca6d0e3 | 8523 | free (buf); |
53df40a4 | 8524 | } |
bca6d0e3 | 8525 | return TRUE; |
c152c796 AM |
8526 | } |
8527 | ||
8528 | struct elf_link_sort_rela | |
8529 | { | |
8530 | union { | |
8531 | bfd_vma offset; | |
8532 | bfd_vma sym_mask; | |
8533 | } u; | |
8534 | enum elf_reloc_type_class type; | |
8535 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
8536 | Elf_Internal_Rela rela[1]; | |
8537 | }; | |
8538 | ||
8539 | static int | |
8540 | elf_link_sort_cmp1 (const void *A, const void *B) | |
8541 | { | |
a50b1753 NC |
8542 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8543 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 AM |
8544 | int relativea, relativeb; |
8545 | ||
8546 | relativea = a->type == reloc_class_relative; | |
8547 | relativeb = b->type == reloc_class_relative; | |
8548 | ||
8549 | if (relativea < relativeb) | |
8550 | return 1; | |
8551 | if (relativea > relativeb) | |
8552 | return -1; | |
8553 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
8554 | return -1; | |
8555 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
8556 | return 1; | |
8557 | if (a->rela->r_offset < b->rela->r_offset) | |
8558 | return -1; | |
8559 | if (a->rela->r_offset > b->rela->r_offset) | |
8560 | return 1; | |
8561 | return 0; | |
8562 | } | |
8563 | ||
8564 | static int | |
8565 | elf_link_sort_cmp2 (const void *A, const void *B) | |
8566 | { | |
a50b1753 NC |
8567 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8568 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 | 8569 | |
7e612e98 | 8570 | if (a->type < b->type) |
c152c796 | 8571 | return -1; |
7e612e98 | 8572 | if (a->type > b->type) |
c152c796 | 8573 | return 1; |
7e612e98 | 8574 | if (a->u.offset < b->u.offset) |
c152c796 | 8575 | return -1; |
7e612e98 | 8576 | if (a->u.offset > b->u.offset) |
c152c796 AM |
8577 | return 1; |
8578 | if (a->rela->r_offset < b->rela->r_offset) | |
8579 | return -1; | |
8580 | if (a->rela->r_offset > b->rela->r_offset) | |
8581 | return 1; | |
8582 | return 0; | |
8583 | } | |
8584 | ||
8585 | static size_t | |
8586 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
8587 | { | |
3410fea8 | 8588 | asection *dynamic_relocs; |
fc66a176 L |
8589 | asection *rela_dyn; |
8590 | asection *rel_dyn; | |
c152c796 AM |
8591 | bfd_size_type count, size; |
8592 | size_t i, ret, sort_elt, ext_size; | |
8593 | bfd_byte *sort, *s_non_relative, *p; | |
8594 | struct elf_link_sort_rela *sq; | |
8595 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8596 | int i2e = bed->s->int_rels_per_ext_rel; | |
c8e44c6d | 8597 | unsigned int opb = bfd_octets_per_byte (abfd); |
c152c796 AM |
8598 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
8599 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8600 | struct bfd_link_order *lo; | |
8601 | bfd_vma r_sym_mask; | |
3410fea8 | 8602 | bfd_boolean use_rela; |
c152c796 | 8603 | |
3410fea8 NC |
8604 | /* Find a dynamic reloc section. */ |
8605 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
8606 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
8607 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
8608 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 8609 | { |
3410fea8 NC |
8610 | bfd_boolean use_rela_initialised = FALSE; |
8611 | ||
8612 | /* This is just here to stop gcc from complaining. | |
c8e44c6d | 8613 | Its initialization checking code is not perfect. */ |
3410fea8 NC |
8614 | use_rela = TRUE; |
8615 | ||
8616 | /* Both sections are present. Examine the sizes | |
8617 | of the indirect sections to help us choose. */ | |
8618 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8619 | if (lo->type == bfd_indirect_link_order) | |
8620 | { | |
8621 | asection *o = lo->u.indirect.section; | |
8622 | ||
8623 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8624 | { | |
8625 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8626 | /* Section size is divisible by both rel and rela sizes. | |
8627 | It is of no help to us. */ | |
8628 | ; | |
8629 | else | |
8630 | { | |
8631 | /* Section size is only divisible by rela. */ | |
8632 | if (use_rela_initialised && (use_rela == FALSE)) | |
8633 | { | |
c8e44c6d AM |
8634 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8635 | "they are in more than one size"), | |
8636 | abfd); | |
3410fea8 NC |
8637 | bfd_set_error (bfd_error_invalid_operation); |
8638 | return 0; | |
8639 | } | |
8640 | else | |
8641 | { | |
8642 | use_rela = TRUE; | |
8643 | use_rela_initialised = TRUE; | |
8644 | } | |
8645 | } | |
8646 | } | |
8647 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
8648 | { | |
8649 | /* Section size is only divisible by rel. */ | |
8650 | if (use_rela_initialised && (use_rela == TRUE)) | |
8651 | { | |
c8e44c6d AM |
8652 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8653 | "they are in more than one size"), | |
8654 | abfd); | |
3410fea8 NC |
8655 | bfd_set_error (bfd_error_invalid_operation); |
8656 | return 0; | |
8657 | } | |
8658 | else | |
8659 | { | |
8660 | use_rela = FALSE; | |
8661 | use_rela_initialised = TRUE; | |
8662 | } | |
8663 | } | |
8664 | else | |
8665 | { | |
c8e44c6d AM |
8666 | /* The section size is not divisible by either - |
8667 | something is wrong. */ | |
8668 | _bfd_error_handler (_("%B: Unable to sort relocs - " | |
8669 | "they are of an unknown size"), abfd); | |
3410fea8 NC |
8670 | bfd_set_error (bfd_error_invalid_operation); |
8671 | return 0; | |
8672 | } | |
8673 | } | |
8674 | ||
8675 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8676 | if (lo->type == bfd_indirect_link_order) | |
8677 | { | |
8678 | asection *o = lo->u.indirect.section; | |
8679 | ||
8680 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8681 | { | |
8682 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8683 | /* Section size is divisible by both rel and rela sizes. | |
8684 | It is of no help to us. */ | |
8685 | ; | |
8686 | else | |
8687 | { | |
8688 | /* Section size is only divisible by rela. */ | |
8689 | if (use_rela_initialised && (use_rela == FALSE)) | |
8690 | { | |
c8e44c6d AM |
8691 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8692 | "they are in more than one size"), | |
8693 | abfd); | |
3410fea8 NC |
8694 | bfd_set_error (bfd_error_invalid_operation); |
8695 | return 0; | |
8696 | } | |
8697 | else | |
8698 | { | |
8699 | use_rela = TRUE; | |
8700 | use_rela_initialised = TRUE; | |
8701 | } | |
8702 | } | |
8703 | } | |
8704 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
8705 | { | |
8706 | /* Section size is only divisible by rel. */ | |
8707 | if (use_rela_initialised && (use_rela == TRUE)) | |
8708 | { | |
c8e44c6d AM |
8709 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8710 | "they are in more than one size"), | |
8711 | abfd); | |
3410fea8 NC |
8712 | bfd_set_error (bfd_error_invalid_operation); |
8713 | return 0; | |
8714 | } | |
8715 | else | |
8716 | { | |
8717 | use_rela = FALSE; | |
8718 | use_rela_initialised = TRUE; | |
8719 | } | |
8720 | } | |
8721 | else | |
8722 | { | |
c8e44c6d AM |
8723 | /* The section size is not divisible by either - |
8724 | something is wrong. */ | |
8725 | _bfd_error_handler (_("%B: Unable to sort relocs - " | |
8726 | "they are of an unknown size"), abfd); | |
3410fea8 NC |
8727 | bfd_set_error (bfd_error_invalid_operation); |
8728 | return 0; | |
8729 | } | |
8730 | } | |
8731 | ||
8732 | if (! use_rela_initialised) | |
8733 | /* Make a guess. */ | |
8734 | use_rela = TRUE; | |
c152c796 | 8735 | } |
fc66a176 L |
8736 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
8737 | use_rela = TRUE; | |
8738 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 8739 | use_rela = FALSE; |
c152c796 | 8740 | else |
fc66a176 | 8741 | return 0; |
3410fea8 NC |
8742 | |
8743 | if (use_rela) | |
c152c796 | 8744 | { |
3410fea8 | 8745 | dynamic_relocs = rela_dyn; |
c152c796 AM |
8746 | ext_size = bed->s->sizeof_rela; |
8747 | swap_in = bed->s->swap_reloca_in; | |
8748 | swap_out = bed->s->swap_reloca_out; | |
8749 | } | |
3410fea8 NC |
8750 | else |
8751 | { | |
8752 | dynamic_relocs = rel_dyn; | |
8753 | ext_size = bed->s->sizeof_rel; | |
8754 | swap_in = bed->s->swap_reloc_in; | |
8755 | swap_out = bed->s->swap_reloc_out; | |
8756 | } | |
c152c796 AM |
8757 | |
8758 | size = 0; | |
3410fea8 | 8759 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 8760 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 8761 | size += lo->u.indirect.section->size; |
c152c796 | 8762 | |
3410fea8 | 8763 | if (size != dynamic_relocs->size) |
c152c796 AM |
8764 | return 0; |
8765 | ||
8766 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
8767 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
8768 | |
8769 | count = dynamic_relocs->size / ext_size; | |
5e486aa1 NC |
8770 | if (count == 0) |
8771 | return 0; | |
a50b1753 | 8772 | sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); |
3410fea8 | 8773 | |
c152c796 AM |
8774 | if (sort == NULL) |
8775 | { | |
8776 | (*info->callbacks->warning) | |
8777 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
8778 | return 0; | |
8779 | } | |
8780 | ||
8781 | if (bed->s->arch_size == 32) | |
8782 | r_sym_mask = ~(bfd_vma) 0xff; | |
8783 | else | |
8784 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
8785 | ||
3410fea8 | 8786 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
8787 | if (lo->type == bfd_indirect_link_order) |
8788 | { | |
8789 | bfd_byte *erel, *erelend; | |
8790 | asection *o = lo->u.indirect.section; | |
8791 | ||
1da212d6 AM |
8792 | if (o->contents == NULL && o->size != 0) |
8793 | { | |
8794 | /* This is a reloc section that is being handled as a normal | |
8795 | section. See bfd_section_from_shdr. We can't combine | |
8796 | relocs in this case. */ | |
8797 | free (sort); | |
8798 | return 0; | |
8799 | } | |
c152c796 | 8800 | erel = o->contents; |
eea6121a | 8801 | erelend = o->contents + o->size; |
c8e44c6d | 8802 | p = sort + o->output_offset * opb / ext_size * sort_elt; |
3410fea8 | 8803 | |
c152c796 AM |
8804 | while (erel < erelend) |
8805 | { | |
8806 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 8807 | |
c152c796 | 8808 | (*swap_in) (abfd, erel, s->rela); |
7e612e98 | 8809 | s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); |
c152c796 AM |
8810 | s->u.sym_mask = r_sym_mask; |
8811 | p += sort_elt; | |
8812 | erel += ext_size; | |
8813 | } | |
8814 | } | |
8815 | ||
8816 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
8817 | ||
8818 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
8819 | { | |
8820 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
8821 | if (s->type != reloc_class_relative) | |
8822 | break; | |
8823 | } | |
8824 | ret = i; | |
8825 | s_non_relative = p; | |
8826 | ||
8827 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
8828 | for (; i < count; i++, p += sort_elt) | |
8829 | { | |
8830 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
8831 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
8832 | sq = sp; | |
8833 | sp->u.offset = sq->rela->r_offset; | |
8834 | } | |
8835 | ||
8836 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
8837 | ||
c8e44c6d AM |
8838 | struct elf_link_hash_table *htab = elf_hash_table (info); |
8839 | if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) | |
8840 | { | |
8841 | /* We have plt relocs in .rela.dyn. */ | |
8842 | sq = (struct elf_link_sort_rela *) sort; | |
8843 | for (i = 0; i < count; i++) | |
8844 | if (sq[count - i - 1].type != reloc_class_plt) | |
8845 | break; | |
8846 | if (i != 0 && htab->srelplt->size == i * ext_size) | |
8847 | { | |
8848 | struct bfd_link_order **plo; | |
8849 | /* Put srelplt link_order last. This is so the output_offset | |
8850 | set in the next loop is correct for DT_JMPREL. */ | |
8851 | for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) | |
8852 | if ((*plo)->type == bfd_indirect_link_order | |
8853 | && (*plo)->u.indirect.section == htab->srelplt) | |
8854 | { | |
8855 | lo = *plo; | |
8856 | *plo = lo->next; | |
8857 | } | |
8858 | else | |
8859 | plo = &(*plo)->next; | |
8860 | *plo = lo; | |
8861 | lo->next = NULL; | |
8862 | dynamic_relocs->map_tail.link_order = lo; | |
8863 | } | |
8864 | } | |
8865 | ||
8866 | p = sort; | |
3410fea8 | 8867 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
8868 | if (lo->type == bfd_indirect_link_order) |
8869 | { | |
8870 | bfd_byte *erel, *erelend; | |
8871 | asection *o = lo->u.indirect.section; | |
8872 | ||
8873 | erel = o->contents; | |
eea6121a | 8874 | erelend = o->contents + o->size; |
c8e44c6d | 8875 | o->output_offset = (p - sort) / sort_elt * ext_size / opb; |
c152c796 AM |
8876 | while (erel < erelend) |
8877 | { | |
8878 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
8879 | (*swap_out) (abfd, s->rela, erel); | |
8880 | p += sort_elt; | |
8881 | erel += ext_size; | |
8882 | } | |
8883 | } | |
8884 | ||
8885 | free (sort); | |
3410fea8 | 8886 | *psec = dynamic_relocs; |
c152c796 AM |
8887 | return ret; |
8888 | } | |
8889 | ||
ef10c3ac | 8890 | /* Add a symbol to the output symbol string table. */ |
c152c796 | 8891 | |
6e0b88f1 | 8892 | static int |
ef10c3ac L |
8893 | elf_link_output_symstrtab (struct elf_final_link_info *flinfo, |
8894 | const char *name, | |
8895 | Elf_Internal_Sym *elfsym, | |
8896 | asection *input_sec, | |
8897 | struct elf_link_hash_entry *h) | |
c152c796 | 8898 | { |
6e0b88f1 | 8899 | int (*output_symbol_hook) |
c152c796 AM |
8900 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, |
8901 | struct elf_link_hash_entry *); | |
ef10c3ac | 8902 | struct elf_link_hash_table *hash_table; |
c152c796 | 8903 | const struct elf_backend_data *bed; |
ef10c3ac | 8904 | bfd_size_type strtabsize; |
c152c796 | 8905 | |
8539e4e8 AM |
8906 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); |
8907 | ||
8b127cbc | 8908 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 AM |
8909 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; |
8910 | if (output_symbol_hook != NULL) | |
8911 | { | |
8b127cbc | 8912 | int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); |
6e0b88f1 AM |
8913 | if (ret != 1) |
8914 | return ret; | |
c152c796 AM |
8915 | } |
8916 | ||
ef10c3ac L |
8917 | if (name == NULL |
8918 | || *name == '\0' | |
8919 | || (input_sec->flags & SEC_EXCLUDE)) | |
8920 | elfsym->st_name = (unsigned long) -1; | |
c152c796 AM |
8921 | else |
8922 | { | |
ef10c3ac L |
8923 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
8924 | to get the final offset for st_name. */ | |
8925 | elfsym->st_name | |
8926 | = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, | |
8927 | name, FALSE); | |
c152c796 | 8928 | if (elfsym->st_name == (unsigned long) -1) |
6e0b88f1 | 8929 | return 0; |
c152c796 AM |
8930 | } |
8931 | ||
ef10c3ac L |
8932 | hash_table = elf_hash_table (flinfo->info); |
8933 | strtabsize = hash_table->strtabsize; | |
8934 | if (strtabsize <= hash_table->strtabcount) | |
c152c796 | 8935 | { |
ef10c3ac L |
8936 | strtabsize += strtabsize; |
8937 | hash_table->strtabsize = strtabsize; | |
8938 | strtabsize *= sizeof (*hash_table->strtab); | |
8939 | hash_table->strtab | |
8940 | = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, | |
8941 | strtabsize); | |
8942 | if (hash_table->strtab == NULL) | |
6e0b88f1 | 8943 | return 0; |
c152c796 | 8944 | } |
ef10c3ac L |
8945 | hash_table->strtab[hash_table->strtabcount].sym = *elfsym; |
8946 | hash_table->strtab[hash_table->strtabcount].dest_index | |
8947 | = hash_table->strtabcount; | |
8948 | hash_table->strtab[hash_table->strtabcount].destshndx_index | |
8949 | = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0; | |
8950 | ||
8951 | bfd_get_symcount (flinfo->output_bfd) += 1; | |
8952 | hash_table->strtabcount += 1; | |
8953 | ||
8954 | return 1; | |
8955 | } | |
8956 | ||
8957 | /* Swap symbols out to the symbol table and flush the output symbols to | |
8958 | the file. */ | |
8959 | ||
8960 | static bfd_boolean | |
8961 | elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) | |
8962 | { | |
8963 | struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); | |
ef53be89 AM |
8964 | bfd_size_type amt; |
8965 | size_t i; | |
ef10c3ac L |
8966 | const struct elf_backend_data *bed; |
8967 | bfd_byte *symbuf; | |
8968 | Elf_Internal_Shdr *hdr; | |
8969 | file_ptr pos; | |
8970 | bfd_boolean ret; | |
8971 | ||
8972 | if (!hash_table->strtabcount) | |
8973 | return TRUE; | |
8974 | ||
8975 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); | |
8976 | ||
8977 | bed = get_elf_backend_data (flinfo->output_bfd); | |
c152c796 | 8978 | |
ef10c3ac L |
8979 | amt = bed->s->sizeof_sym * hash_table->strtabcount; |
8980 | symbuf = (bfd_byte *) bfd_malloc (amt); | |
8981 | if (symbuf == NULL) | |
8982 | return FALSE; | |
1b786873 | 8983 | |
ef10c3ac | 8984 | if (flinfo->symshndxbuf) |
c152c796 | 8985 | { |
ef53be89 AM |
8986 | amt = sizeof (Elf_External_Sym_Shndx); |
8987 | amt *= bfd_get_symcount (flinfo->output_bfd); | |
ef10c3ac L |
8988 | flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); |
8989 | if (flinfo->symshndxbuf == NULL) | |
c152c796 | 8990 | { |
ef10c3ac L |
8991 | free (symbuf); |
8992 | return FALSE; | |
c152c796 | 8993 | } |
c152c796 AM |
8994 | } |
8995 | ||
ef10c3ac L |
8996 | for (i = 0; i < hash_table->strtabcount; i++) |
8997 | { | |
8998 | struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; | |
8999 | if (elfsym->sym.st_name == (unsigned long) -1) | |
9000 | elfsym->sym.st_name = 0; | |
9001 | else | |
9002 | elfsym->sym.st_name | |
9003 | = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, | |
9004 | elfsym->sym.st_name); | |
9005 | bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, | |
9006 | ((bfd_byte *) symbuf | |
9007 | + (elfsym->dest_index | |
9008 | * bed->s->sizeof_sym)), | |
9009 | (flinfo->symshndxbuf | |
9010 | + elfsym->destshndx_index)); | |
9011 | } | |
9012 | ||
9013 | hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; | |
9014 | pos = hdr->sh_offset + hdr->sh_size; | |
9015 | amt = hash_table->strtabcount * bed->s->sizeof_sym; | |
9016 | if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 | |
9017 | && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt) | |
9018 | { | |
9019 | hdr->sh_size += amt; | |
9020 | ret = TRUE; | |
9021 | } | |
9022 | else | |
9023 | ret = FALSE; | |
c152c796 | 9024 | |
ef10c3ac L |
9025 | free (symbuf); |
9026 | ||
9027 | free (hash_table->strtab); | |
9028 | hash_table->strtab = NULL; | |
9029 | ||
9030 | return ret; | |
c152c796 AM |
9031 | } |
9032 | ||
c0d5a53d L |
9033 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
9034 | ||
9035 | static bfd_boolean | |
9036 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
9037 | { | |
4fbb74a6 AM |
9038 | if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) |
9039 | && sym->st_shndx < SHN_LORESERVE) | |
c0d5a53d L |
9040 | { |
9041 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 9042 | beyond 64k. */ |
4eca0228 | 9043 | _bfd_error_handler |
695344c0 | 9044 | /* xgettext:c-format */ |
c0d5a53d | 9045 | (_("%B: Too many sections: %d (>= %d)"), |
4fbb74a6 | 9046 | abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); |
c0d5a53d L |
9047 | bfd_set_error (bfd_error_nonrepresentable_section); |
9048 | return FALSE; | |
9049 | } | |
9050 | return TRUE; | |
9051 | } | |
9052 | ||
c152c796 AM |
9053 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
9054 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
9055 | versioned symbol that would normally require an explicit version. | |
9056 | We also handle the case that a DSO references a hidden symbol | |
9057 | which may be satisfied by a versioned symbol in another DSO. */ | |
9058 | ||
9059 | static bfd_boolean | |
9060 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
9061 | const struct elf_backend_data *bed, | |
9062 | struct elf_link_hash_entry *h) | |
9063 | { | |
9064 | bfd *abfd; | |
9065 | struct elf_link_loaded_list *loaded; | |
9066 | ||
9067 | if (!is_elf_hash_table (info->hash)) | |
9068 | return FALSE; | |
9069 | ||
90c984fc L |
9070 | /* Check indirect symbol. */ |
9071 | while (h->root.type == bfd_link_hash_indirect) | |
9072 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9073 | ||
c152c796 AM |
9074 | switch (h->root.type) |
9075 | { | |
9076 | default: | |
9077 | abfd = NULL; | |
9078 | break; | |
9079 | ||
9080 | case bfd_link_hash_undefined: | |
9081 | case bfd_link_hash_undefweak: | |
9082 | abfd = h->root.u.undef.abfd; | |
f4ab0e2d L |
9083 | if (abfd == NULL |
9084 | || (abfd->flags & DYNAMIC) == 0 | |
e56f61be | 9085 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
9086 | return FALSE; |
9087 | break; | |
9088 | ||
9089 | case bfd_link_hash_defined: | |
9090 | case bfd_link_hash_defweak: | |
9091 | abfd = h->root.u.def.section->owner; | |
9092 | break; | |
9093 | ||
9094 | case bfd_link_hash_common: | |
9095 | abfd = h->root.u.c.p->section->owner; | |
9096 | break; | |
9097 | } | |
9098 | BFD_ASSERT (abfd != NULL); | |
9099 | ||
9100 | for (loaded = elf_hash_table (info)->loaded; | |
9101 | loaded != NULL; | |
9102 | loaded = loaded->next) | |
9103 | { | |
9104 | bfd *input; | |
9105 | Elf_Internal_Shdr *hdr; | |
ef53be89 AM |
9106 | size_t symcount; |
9107 | size_t extsymcount; | |
9108 | size_t extsymoff; | |
c152c796 AM |
9109 | Elf_Internal_Shdr *versymhdr; |
9110 | Elf_Internal_Sym *isym; | |
9111 | Elf_Internal_Sym *isymend; | |
9112 | Elf_Internal_Sym *isymbuf; | |
9113 | Elf_External_Versym *ever; | |
9114 | Elf_External_Versym *extversym; | |
9115 | ||
9116 | input = loaded->abfd; | |
9117 | ||
9118 | /* We check each DSO for a possible hidden versioned definition. */ | |
9119 | if (input == abfd | |
9120 | || (input->flags & DYNAMIC) == 0 | |
9121 | || elf_dynversym (input) == 0) | |
9122 | continue; | |
9123 | ||
9124 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
9125 | ||
9126 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
9127 | if (elf_bad_symtab (input)) | |
9128 | { | |
9129 | extsymcount = symcount; | |
9130 | extsymoff = 0; | |
9131 | } | |
9132 | else | |
9133 | { | |
9134 | extsymcount = symcount - hdr->sh_info; | |
9135 | extsymoff = hdr->sh_info; | |
9136 | } | |
9137 | ||
9138 | if (extsymcount == 0) | |
9139 | continue; | |
9140 | ||
9141 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
9142 | NULL, NULL, NULL); | |
9143 | if (isymbuf == NULL) | |
9144 | return FALSE; | |
9145 | ||
9146 | /* Read in any version definitions. */ | |
9147 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
a50b1753 | 9148 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
c152c796 AM |
9149 | if (extversym == NULL) |
9150 | goto error_ret; | |
9151 | ||
9152 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
9153 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
9154 | != versymhdr->sh_size)) | |
9155 | { | |
9156 | free (extversym); | |
9157 | error_ret: | |
9158 | free (isymbuf); | |
9159 | return FALSE; | |
9160 | } | |
9161 | ||
9162 | ever = extversym + extsymoff; | |
9163 | isymend = isymbuf + extsymcount; | |
9164 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
9165 | { | |
9166 | const char *name; | |
9167 | Elf_Internal_Versym iver; | |
9168 | unsigned short version_index; | |
9169 | ||
9170 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
9171 | || isym->st_shndx == SHN_UNDEF) | |
9172 | continue; | |
9173 | ||
9174 | name = bfd_elf_string_from_elf_section (input, | |
9175 | hdr->sh_link, | |
9176 | isym->st_name); | |
9177 | if (strcmp (name, h->root.root.string) != 0) | |
9178 | continue; | |
9179 | ||
9180 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
9181 | ||
d023c380 L |
9182 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 |
9183 | && !(h->def_regular | |
9184 | && h->forced_local)) | |
c152c796 AM |
9185 | { |
9186 | /* If we have a non-hidden versioned sym, then it should | |
d023c380 L |
9187 | have provided a definition for the undefined sym unless |
9188 | it is defined in a non-shared object and forced local. | |
9189 | */ | |
c152c796 AM |
9190 | abort (); |
9191 | } | |
9192 | ||
9193 | version_index = iver.vs_vers & VERSYM_VERSION; | |
9194 | if (version_index == 1 || version_index == 2) | |
9195 | { | |
9196 | /* This is the base or first version. We can use it. */ | |
9197 | free (extversym); | |
9198 | free (isymbuf); | |
9199 | return TRUE; | |
9200 | } | |
9201 | } | |
9202 | ||
9203 | free (extversym); | |
9204 | free (isymbuf); | |
9205 | } | |
9206 | ||
9207 | return FALSE; | |
9208 | } | |
9209 | ||
b8871f35 L |
9210 | /* Convert ELF common symbol TYPE. */ |
9211 | ||
9212 | static int | |
9213 | elf_link_convert_common_type (struct bfd_link_info *info, int type) | |
9214 | { | |
9215 | /* Commom symbol can only appear in relocatable link. */ | |
9216 | if (!bfd_link_relocatable (info)) | |
9217 | abort (); | |
9218 | switch (info->elf_stt_common) | |
9219 | { | |
9220 | case unchanged: | |
9221 | break; | |
9222 | case elf_stt_common: | |
9223 | type = STT_COMMON; | |
9224 | break; | |
9225 | case no_elf_stt_common: | |
9226 | type = STT_OBJECT; | |
9227 | break; | |
9228 | } | |
9229 | return type; | |
9230 | } | |
9231 | ||
c152c796 AM |
9232 | /* Add an external symbol to the symbol table. This is called from |
9233 | the hash table traversal routine. When generating a shared object, | |
9234 | we go through the symbol table twice. The first time we output | |
9235 | anything that might have been forced to local scope in a version | |
9236 | script. The second time we output the symbols that are still | |
9237 | global symbols. */ | |
9238 | ||
9239 | static bfd_boolean | |
7686d77d | 9240 | elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) |
c152c796 | 9241 | { |
7686d77d | 9242 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; |
a50b1753 | 9243 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
8b127cbc | 9244 | struct elf_final_link_info *flinfo = eoinfo->flinfo; |
c152c796 AM |
9245 | bfd_boolean strip; |
9246 | Elf_Internal_Sym sym; | |
9247 | asection *input_sec; | |
9248 | const struct elf_backend_data *bed; | |
6e0b88f1 AM |
9249 | long indx; |
9250 | int ret; | |
b8871f35 | 9251 | unsigned int type; |
6e33951e L |
9252 | /* A symbol is bound locally if it is forced local or it is locally |
9253 | defined, hidden versioned, not referenced by shared library and | |
9254 | not exported when linking executable. */ | |
9255 | bfd_boolean local_bind = (h->forced_local | |
0e1862bb | 9256 | || (bfd_link_executable (flinfo->info) |
6e33951e L |
9257 | && !flinfo->info->export_dynamic |
9258 | && !h->dynamic | |
9259 | && !h->ref_dynamic | |
9260 | && h->def_regular | |
422f1182 | 9261 | && h->versioned == versioned_hidden)); |
c152c796 AM |
9262 | |
9263 | if (h->root.type == bfd_link_hash_warning) | |
9264 | { | |
9265 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9266 | if (h->root.type == bfd_link_hash_new) | |
9267 | return TRUE; | |
9268 | } | |
9269 | ||
9270 | /* Decide whether to output this symbol in this pass. */ | |
9271 | if (eoinfo->localsyms) | |
9272 | { | |
6e33951e | 9273 | if (!local_bind) |
c152c796 AM |
9274 | return TRUE; |
9275 | } | |
9276 | else | |
9277 | { | |
6e33951e | 9278 | if (local_bind) |
c152c796 AM |
9279 | return TRUE; |
9280 | } | |
9281 | ||
8b127cbc | 9282 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 | 9283 | |
12ac1cf5 | 9284 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 9285 | { |
12ac1cf5 NC |
9286 | /* If we have an undefined symbol reference here then it must have |
9287 | come from a shared library that is being linked in. (Undefined | |
98da7939 L |
9288 | references in regular files have already been handled unless |
9289 | they are in unreferenced sections which are removed by garbage | |
9290 | collection). */ | |
12ac1cf5 NC |
9291 | bfd_boolean ignore_undef = FALSE; |
9292 | ||
9293 | /* Some symbols may be special in that the fact that they're | |
9294 | undefined can be safely ignored - let backend determine that. */ | |
9295 | if (bed->elf_backend_ignore_undef_symbol) | |
9296 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
9297 | ||
9298 | /* If we are reporting errors for this situation then do so now. */ | |
89a2ee5a | 9299 | if (!ignore_undef |
12ac1cf5 | 9300 | && h->ref_dynamic |
8b127cbc AM |
9301 | && (!h->ref_regular || flinfo->info->gc_sections) |
9302 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h) | |
9303 | && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
1a72702b AM |
9304 | (*flinfo->info->callbacks->undefined_symbol) |
9305 | (flinfo->info, h->root.root.string, | |
9306 | h->ref_regular ? NULL : h->root.u.undef.abfd, | |
9307 | NULL, 0, | |
9308 | flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR); | |
97196564 L |
9309 | |
9310 | /* Strip a global symbol defined in a discarded section. */ | |
9311 | if (h->indx == -3) | |
9312 | return TRUE; | |
c152c796 AM |
9313 | } |
9314 | ||
9315 | /* We should also warn if a forced local symbol is referenced from | |
9316 | shared libraries. */ | |
0e1862bb | 9317 | if (bfd_link_executable (flinfo->info) |
f5385ebf AM |
9318 | && h->forced_local |
9319 | && h->ref_dynamic | |
371a5866 | 9320 | && h->def_regular |
f5385ebf | 9321 | && !h->dynamic_def |
ee659f1f | 9322 | && h->ref_dynamic_nonweak |
8b127cbc | 9323 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) |
c152c796 | 9324 | { |
17d078c5 AM |
9325 | bfd *def_bfd; |
9326 | const char *msg; | |
90c984fc L |
9327 | struct elf_link_hash_entry *hi = h; |
9328 | ||
9329 | /* Check indirect symbol. */ | |
9330 | while (hi->root.type == bfd_link_hash_indirect) | |
9331 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
17d078c5 AM |
9332 | |
9333 | if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
695344c0 | 9334 | /* xgettext:c-format */ |
17d078c5 AM |
9335 | msg = _("%B: internal symbol `%s' in %B is referenced by DSO"); |
9336 | else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) | |
695344c0 | 9337 | /* xgettext:c-format */ |
17d078c5 AM |
9338 | msg = _("%B: hidden symbol `%s' in %B is referenced by DSO"); |
9339 | else | |
695344c0 | 9340 | /* xgettext:c-format */ |
17d078c5 | 9341 | msg = _("%B: local symbol `%s' in %B is referenced by DSO"); |
8b127cbc | 9342 | def_bfd = flinfo->output_bfd; |
90c984fc L |
9343 | if (hi->root.u.def.section != bfd_abs_section_ptr) |
9344 | def_bfd = hi->root.u.def.section->owner; | |
4eca0228 AM |
9345 | _bfd_error_handler (msg, flinfo->output_bfd, def_bfd, |
9346 | h->root.root.string); | |
17d078c5 | 9347 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9348 | eoinfo->failed = TRUE; |
9349 | return FALSE; | |
9350 | } | |
9351 | ||
9352 | /* We don't want to output symbols that have never been mentioned by | |
9353 | a regular file, or that we have been told to strip. However, if | |
9354 | h->indx is set to -2, the symbol is used by a reloc and we must | |
9355 | output it. */ | |
d983c8c5 | 9356 | strip = FALSE; |
c152c796 | 9357 | if (h->indx == -2) |
d983c8c5 | 9358 | ; |
f5385ebf | 9359 | else if ((h->def_dynamic |
77cfaee6 AM |
9360 | || h->ref_dynamic |
9361 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
9362 | && !h->def_regular |
9363 | && !h->ref_regular) | |
c152c796 | 9364 | strip = TRUE; |
8b127cbc | 9365 | else if (flinfo->info->strip == strip_all) |
c152c796 | 9366 | strip = TRUE; |
8b127cbc AM |
9367 | else if (flinfo->info->strip == strip_some |
9368 | && bfd_hash_lookup (flinfo->info->keep_hash, | |
c152c796 AM |
9369 | h->root.root.string, FALSE, FALSE) == NULL) |
9370 | strip = TRUE; | |
d56d55e7 AM |
9371 | else if ((h->root.type == bfd_link_hash_defined |
9372 | || h->root.type == bfd_link_hash_defweak) | |
8b127cbc | 9373 | && ((flinfo->info->strip_discarded |
dbaa2011 | 9374 | && discarded_section (h->root.u.def.section)) |
ca4be51c AM |
9375 | || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 |
9376 | && h->root.u.def.section->owner != NULL | |
d56d55e7 | 9377 | && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) |
c152c796 | 9378 | strip = TRUE; |
9e2278f5 AM |
9379 | else if ((h->root.type == bfd_link_hash_undefined |
9380 | || h->root.type == bfd_link_hash_undefweak) | |
9381 | && h->root.u.undef.abfd != NULL | |
9382 | && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) | |
9383 | strip = TRUE; | |
c152c796 | 9384 | |
b8871f35 L |
9385 | type = h->type; |
9386 | ||
c152c796 | 9387 | /* If we're stripping it, and it's not a dynamic symbol, there's |
d983c8c5 AM |
9388 | nothing else to do. However, if it is a forced local symbol or |
9389 | an ifunc symbol we need to give the backend finish_dynamic_symbol | |
9390 | function a chance to make it dynamic. */ | |
c152c796 AM |
9391 | if (strip |
9392 | && h->dynindx == -1 | |
b8871f35 | 9393 | && type != STT_GNU_IFUNC |
f5385ebf | 9394 | && !h->forced_local) |
c152c796 AM |
9395 | return TRUE; |
9396 | ||
9397 | sym.st_value = 0; | |
9398 | sym.st_size = h->size; | |
9399 | sym.st_other = h->other; | |
c152c796 AM |
9400 | switch (h->root.type) |
9401 | { | |
9402 | default: | |
9403 | case bfd_link_hash_new: | |
9404 | case bfd_link_hash_warning: | |
9405 | abort (); | |
9406 | return FALSE; | |
9407 | ||
9408 | case bfd_link_hash_undefined: | |
9409 | case bfd_link_hash_undefweak: | |
9410 | input_sec = bfd_und_section_ptr; | |
9411 | sym.st_shndx = SHN_UNDEF; | |
9412 | break; | |
9413 | ||
9414 | case bfd_link_hash_defined: | |
9415 | case bfd_link_hash_defweak: | |
9416 | { | |
9417 | input_sec = h->root.u.def.section; | |
9418 | if (input_sec->output_section != NULL) | |
9419 | { | |
9420 | sym.st_shndx = | |
8b127cbc | 9421 | _bfd_elf_section_from_bfd_section (flinfo->output_bfd, |
c152c796 AM |
9422 | input_sec->output_section); |
9423 | if (sym.st_shndx == SHN_BAD) | |
9424 | { | |
4eca0228 | 9425 | _bfd_error_handler |
695344c0 | 9426 | /* xgettext:c-format */ |
d003868e | 9427 | (_("%B: could not find output section %A for input section %A"), |
8b127cbc | 9428 | flinfo->output_bfd, input_sec->output_section, input_sec); |
17d078c5 | 9429 | bfd_set_error (bfd_error_nonrepresentable_section); |
c152c796 AM |
9430 | eoinfo->failed = TRUE; |
9431 | return FALSE; | |
9432 | } | |
9433 | ||
9434 | /* ELF symbols in relocatable files are section relative, | |
9435 | but in nonrelocatable files they are virtual | |
9436 | addresses. */ | |
9437 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
0e1862bb | 9438 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
9439 | { |
9440 | sym.st_value += input_sec->output_section->vma; | |
9441 | if (h->type == STT_TLS) | |
9442 | { | |
8b127cbc | 9443 | asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; |
430a16a5 NC |
9444 | if (tls_sec != NULL) |
9445 | sym.st_value -= tls_sec->vma; | |
c152c796 AM |
9446 | } |
9447 | } | |
9448 | } | |
9449 | else | |
9450 | { | |
9451 | BFD_ASSERT (input_sec->owner == NULL | |
9452 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
9453 | sym.st_shndx = SHN_UNDEF; | |
9454 | input_sec = bfd_und_section_ptr; | |
9455 | } | |
9456 | } | |
9457 | break; | |
9458 | ||
9459 | case bfd_link_hash_common: | |
9460 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 9461 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
9462 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
9463 | break; | |
9464 | ||
9465 | case bfd_link_hash_indirect: | |
9466 | /* These symbols are created by symbol versioning. They point | |
9467 | to the decorated version of the name. For example, if the | |
9468 | symbol foo@@GNU_1.2 is the default, which should be used when | |
9469 | foo is used with no version, then we add an indirect symbol | |
9470 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
9471 | since the indirected symbol is already in the hash table. */ | |
9472 | return TRUE; | |
9473 | } | |
9474 | ||
b8871f35 L |
9475 | if (type == STT_COMMON || type == STT_OBJECT) |
9476 | switch (h->root.type) | |
9477 | { | |
9478 | case bfd_link_hash_common: | |
9479 | type = elf_link_convert_common_type (flinfo->info, type); | |
9480 | break; | |
9481 | case bfd_link_hash_defined: | |
9482 | case bfd_link_hash_defweak: | |
9483 | if (bed->common_definition (&sym)) | |
9484 | type = elf_link_convert_common_type (flinfo->info, type); | |
9485 | else | |
9486 | type = STT_OBJECT; | |
9487 | break; | |
9488 | case bfd_link_hash_undefined: | |
9489 | case bfd_link_hash_undefweak: | |
9490 | break; | |
9491 | default: | |
9492 | abort (); | |
9493 | } | |
9494 | ||
9495 | if (local_bind) | |
9496 | { | |
9497 | sym.st_info = ELF_ST_INFO (STB_LOCAL, type); | |
9498 | /* Turn off visibility on local symbol. */ | |
9499 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); | |
9500 | } | |
9501 | /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ | |
9502 | else if (h->unique_global && h->def_regular) | |
9503 | sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); | |
9504 | else if (h->root.type == bfd_link_hash_undefweak | |
9505 | || h->root.type == bfd_link_hash_defweak) | |
9506 | sym.st_info = ELF_ST_INFO (STB_WEAK, type); | |
9507 | else | |
9508 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
9509 | sym.st_target_internal = h->target_internal; | |
9510 | ||
c152c796 AM |
9511 | /* Give the processor backend a chance to tweak the symbol value, |
9512 | and also to finish up anything that needs to be done for this | |
9513 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
3aa14d16 | 9514 | forced local syms when non-shared is due to a historical quirk. |
5f35ea9c | 9515 | STT_GNU_IFUNC symbol must go through PLT. */ |
3aa14d16 | 9516 | if ((h->type == STT_GNU_IFUNC |
5f35ea9c | 9517 | && h->def_regular |
0e1862bb | 9518 | && !bfd_link_relocatable (flinfo->info)) |
3aa14d16 L |
9519 | || ((h->dynindx != -1 |
9520 | || h->forced_local) | |
0e1862bb | 9521 | && ((bfd_link_pic (flinfo->info) |
3aa14d16 L |
9522 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
9523 | || h->root.type != bfd_link_hash_undefweak)) | |
9524 | || !h->forced_local) | |
8b127cbc | 9525 | && elf_hash_table (flinfo->info)->dynamic_sections_created)) |
c152c796 AM |
9526 | { |
9527 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8b127cbc | 9528 | (flinfo->output_bfd, flinfo->info, h, &sym))) |
c152c796 AM |
9529 | { |
9530 | eoinfo->failed = TRUE; | |
9531 | return FALSE; | |
9532 | } | |
9533 | } | |
9534 | ||
9535 | /* If we are marking the symbol as undefined, and there are no | |
9536 | non-weak references to this symbol from a regular object, then | |
9537 | mark the symbol as weak undefined; if there are non-weak | |
9538 | references, mark the symbol as strong. We can't do this earlier, | |
9539 | because it might not be marked as undefined until the | |
9540 | finish_dynamic_symbol routine gets through with it. */ | |
9541 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 9542 | && h->ref_regular |
c152c796 AM |
9543 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
9544 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
9545 | { | |
9546 | int bindtype; | |
b8871f35 | 9547 | type = ELF_ST_TYPE (sym.st_info); |
2955ec4c L |
9548 | |
9549 | /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ | |
9550 | if (type == STT_GNU_IFUNC) | |
9551 | type = STT_FUNC; | |
c152c796 | 9552 | |
f5385ebf | 9553 | if (h->ref_regular_nonweak) |
c152c796 AM |
9554 | bindtype = STB_GLOBAL; |
9555 | else | |
9556 | bindtype = STB_WEAK; | |
2955ec4c | 9557 | sym.st_info = ELF_ST_INFO (bindtype, type); |
c152c796 AM |
9558 | } |
9559 | ||
bda987c2 CD |
9560 | /* If this is a symbol defined in a dynamic library, don't use the |
9561 | symbol size from the dynamic library. Relinking an executable | |
9562 | against a new library may introduce gratuitous changes in the | |
9563 | executable's symbols if we keep the size. */ | |
9564 | if (sym.st_shndx == SHN_UNDEF | |
9565 | && !h->def_regular | |
9566 | && h->def_dynamic) | |
9567 | sym.st_size = 0; | |
9568 | ||
c152c796 AM |
9569 | /* If a non-weak symbol with non-default visibility is not defined |
9570 | locally, it is a fatal error. */ | |
0e1862bb | 9571 | if (!bfd_link_relocatable (flinfo->info) |
c152c796 AM |
9572 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT |
9573 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
9574 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 9575 | && !h->def_regular) |
c152c796 | 9576 | { |
17d078c5 AM |
9577 | const char *msg; |
9578 | ||
9579 | if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) | |
695344c0 | 9580 | /* xgettext:c-format */ |
17d078c5 AM |
9581 | msg = _("%B: protected symbol `%s' isn't defined"); |
9582 | else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) | |
695344c0 | 9583 | /* xgettext:c-format */ |
17d078c5 AM |
9584 | msg = _("%B: internal symbol `%s' isn't defined"); |
9585 | else | |
695344c0 | 9586 | /* xgettext:c-format */ |
17d078c5 | 9587 | msg = _("%B: hidden symbol `%s' isn't defined"); |
4eca0228 | 9588 | _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string); |
17d078c5 | 9589 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9590 | eoinfo->failed = TRUE; |
9591 | return FALSE; | |
9592 | } | |
9593 | ||
9594 | /* If this symbol should be put in the .dynsym section, then put it | |
9595 | there now. We already know the symbol index. We also fill in | |
9596 | the entry in the .hash section. */ | |
cae1fbbb | 9597 | if (elf_hash_table (flinfo->info)->dynsym != NULL |
202e2356 | 9598 | && h->dynindx != -1 |
8b127cbc | 9599 | && elf_hash_table (flinfo->info)->dynamic_sections_created) |
c152c796 | 9600 | { |
c152c796 AM |
9601 | bfd_byte *esym; |
9602 | ||
90c984fc L |
9603 | /* Since there is no version information in the dynamic string, |
9604 | if there is no version info in symbol version section, we will | |
1659f720 | 9605 | have a run-time problem if not linking executable, referenced |
6e33951e L |
9606 | by shared library, not locally defined, or not bound locally. |
9607 | */ | |
1659f720 | 9608 | if (h->verinfo.verdef == NULL |
6e33951e | 9609 | && !local_bind |
0e1862bb | 9610 | && (!bfd_link_executable (flinfo->info) |
1659f720 L |
9611 | || h->ref_dynamic |
9612 | || !h->def_regular)) | |
90c984fc L |
9613 | { |
9614 | char *p = strrchr (h->root.root.string, ELF_VER_CHR); | |
9615 | ||
9616 | if (p && p [1] != '\0') | |
9617 | { | |
4eca0228 | 9618 | _bfd_error_handler |
695344c0 | 9619 | /* xgettext:c-format */ |
90c984fc L |
9620 | (_("%B: No symbol version section for versioned symbol `%s'"), |
9621 | flinfo->output_bfd, h->root.root.string); | |
9622 | eoinfo->failed = TRUE; | |
9623 | return FALSE; | |
9624 | } | |
9625 | } | |
9626 | ||
c152c796 | 9627 | sym.st_name = h->dynstr_index; |
cae1fbbb L |
9628 | esym = (elf_hash_table (flinfo->info)->dynsym->contents |
9629 | + h->dynindx * bed->s->sizeof_sym); | |
8b127cbc | 9630 | if (!check_dynsym (flinfo->output_bfd, &sym)) |
c0d5a53d L |
9631 | { |
9632 | eoinfo->failed = TRUE; | |
9633 | return FALSE; | |
9634 | } | |
8b127cbc | 9635 | bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); |
c152c796 | 9636 | |
8b127cbc | 9637 | if (flinfo->hash_sec != NULL) |
fdc90cb4 JJ |
9638 | { |
9639 | size_t hash_entry_size; | |
9640 | bfd_byte *bucketpos; | |
9641 | bfd_vma chain; | |
41198d0c L |
9642 | size_t bucketcount; |
9643 | size_t bucket; | |
9644 | ||
8b127cbc | 9645 | bucketcount = elf_hash_table (flinfo->info)->bucketcount; |
41198d0c | 9646 | bucket = h->u.elf_hash_value % bucketcount; |
fdc90cb4 JJ |
9647 | |
9648 | hash_entry_size | |
8b127cbc AM |
9649 | = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; |
9650 | bucketpos = ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 | 9651 | + (bucket + 2) * hash_entry_size); |
8b127cbc AM |
9652 | chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); |
9653 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, | |
9654 | bucketpos); | |
9655 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, | |
9656 | ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 JJ |
9657 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); |
9658 | } | |
c152c796 | 9659 | |
8b127cbc | 9660 | if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) |
c152c796 AM |
9661 | { |
9662 | Elf_Internal_Versym iversym; | |
9663 | Elf_External_Versym *eversym; | |
9664 | ||
f5385ebf | 9665 | if (!h->def_regular) |
c152c796 | 9666 | { |
7b20f099 AM |
9667 | if (h->verinfo.verdef == NULL |
9668 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
9669 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
c152c796 AM |
9670 | iversym.vs_vers = 0; |
9671 | else | |
9672 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
9673 | } | |
9674 | else | |
9675 | { | |
9676 | if (h->verinfo.vertree == NULL) | |
9677 | iversym.vs_vers = 1; | |
9678 | else | |
9679 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
8b127cbc | 9680 | if (flinfo->info->create_default_symver) |
3e3b46e5 | 9681 | iversym.vs_vers++; |
c152c796 AM |
9682 | } |
9683 | ||
422f1182 | 9684 | /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is |
6e33951e | 9685 | defined locally. */ |
422f1182 | 9686 | if (h->versioned == versioned_hidden && h->def_regular) |
c152c796 AM |
9687 | iversym.vs_vers |= VERSYM_HIDDEN; |
9688 | ||
8b127cbc | 9689 | eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; |
c152c796 | 9690 | eversym += h->dynindx; |
8b127cbc | 9691 | _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); |
c152c796 AM |
9692 | } |
9693 | } | |
9694 | ||
d983c8c5 AM |
9695 | /* If the symbol is undefined, and we didn't output it to .dynsym, |
9696 | strip it from .symtab too. Obviously we can't do this for | |
9697 | relocatable output or when needed for --emit-relocs. */ | |
9698 | else if (input_sec == bfd_und_section_ptr | |
9699 | && h->indx != -2 | |
0e1862bb | 9700 | && !bfd_link_relocatable (flinfo->info)) |
d983c8c5 AM |
9701 | return TRUE; |
9702 | /* Also strip others that we couldn't earlier due to dynamic symbol | |
9703 | processing. */ | |
9704 | if (strip) | |
9705 | return TRUE; | |
9706 | if ((input_sec->flags & SEC_EXCLUDE) != 0) | |
c152c796 AM |
9707 | return TRUE; |
9708 | ||
2ec55de3 AM |
9709 | /* Output a FILE symbol so that following locals are not associated |
9710 | with the wrong input file. We need one for forced local symbols | |
9711 | if we've seen more than one FILE symbol or when we have exactly | |
9712 | one FILE symbol but global symbols are present in a file other | |
9713 | than the one with the FILE symbol. We also need one if linker | |
9714 | defined symbols are present. In practice these conditions are | |
9715 | always met, so just emit the FILE symbol unconditionally. */ | |
9716 | if (eoinfo->localsyms | |
9717 | && !eoinfo->file_sym_done | |
9718 | && eoinfo->flinfo->filesym_count != 0) | |
9719 | { | |
9720 | Elf_Internal_Sym fsym; | |
9721 | ||
9722 | memset (&fsym, 0, sizeof (fsym)); | |
9723 | fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
9724 | fsym.st_shndx = SHN_ABS; | |
ef10c3ac L |
9725 | if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, |
9726 | bfd_und_section_ptr, NULL)) | |
2ec55de3 AM |
9727 | return FALSE; |
9728 | ||
9729 | eoinfo->file_sym_done = TRUE; | |
9730 | } | |
9731 | ||
8b127cbc | 9732 | indx = bfd_get_symcount (flinfo->output_bfd); |
ef10c3ac L |
9733 | ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, |
9734 | input_sec, h); | |
6e0b88f1 | 9735 | if (ret == 0) |
c152c796 AM |
9736 | { |
9737 | eoinfo->failed = TRUE; | |
9738 | return FALSE; | |
9739 | } | |
6e0b88f1 AM |
9740 | else if (ret == 1) |
9741 | h->indx = indx; | |
9742 | else if (h->indx == -2) | |
9743 | abort(); | |
c152c796 AM |
9744 | |
9745 | return TRUE; | |
9746 | } | |
9747 | ||
cdd3575c AM |
9748 | /* Return TRUE if special handling is done for relocs in SEC against |
9749 | symbols defined in discarded sections. */ | |
9750 | ||
c152c796 AM |
9751 | static bfd_boolean |
9752 | elf_section_ignore_discarded_relocs (asection *sec) | |
9753 | { | |
9754 | const struct elf_backend_data *bed; | |
9755 | ||
cdd3575c AM |
9756 | switch (sec->sec_info_type) |
9757 | { | |
dbaa2011 AM |
9758 | case SEC_INFO_TYPE_STABS: |
9759 | case SEC_INFO_TYPE_EH_FRAME: | |
2f0c68f2 | 9760 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
cdd3575c AM |
9761 | return TRUE; |
9762 | default: | |
9763 | break; | |
9764 | } | |
c152c796 AM |
9765 | |
9766 | bed = get_elf_backend_data (sec->owner); | |
9767 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
9768 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
9769 | return TRUE; | |
9770 | ||
9771 | return FALSE; | |
9772 | } | |
9773 | ||
9e66c942 AM |
9774 | /* Return a mask saying how ld should treat relocations in SEC against |
9775 | symbols defined in discarded sections. If this function returns | |
9776 | COMPLAIN set, ld will issue a warning message. If this function | |
9777 | returns PRETEND set, and the discarded section was link-once and the | |
9778 | same size as the kept link-once section, ld will pretend that the | |
9779 | symbol was actually defined in the kept section. Otherwise ld will | |
9780 | zero the reloc (at least that is the intent, but some cooperation by | |
9781 | the target dependent code is needed, particularly for REL targets). */ | |
9782 | ||
8a696751 AM |
9783 | unsigned int |
9784 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 9785 | { |
9e66c942 | 9786 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 9787 | return PRETEND; |
cdd3575c AM |
9788 | |
9789 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 9790 | return 0; |
cdd3575c AM |
9791 | |
9792 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 9793 | return 0; |
cdd3575c | 9794 | |
9e66c942 | 9795 | return COMPLAIN | PRETEND; |
cdd3575c AM |
9796 | } |
9797 | ||
3d7f7666 L |
9798 | /* Find a match between a section and a member of a section group. */ |
9799 | ||
9800 | static asection * | |
c0f00686 L |
9801 | match_group_member (asection *sec, asection *group, |
9802 | struct bfd_link_info *info) | |
3d7f7666 L |
9803 | { |
9804 | asection *first = elf_next_in_group (group); | |
9805 | asection *s = first; | |
9806 | ||
9807 | while (s != NULL) | |
9808 | { | |
c0f00686 | 9809 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
9810 | return s; |
9811 | ||
83180ade | 9812 | s = elf_next_in_group (s); |
3d7f7666 L |
9813 | if (s == first) |
9814 | break; | |
9815 | } | |
9816 | ||
9817 | return NULL; | |
9818 | } | |
9819 | ||
01b3c8ab | 9820 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
9821 | to replace it. Return the replacement if it is OK. Otherwise return |
9822 | NULL. */ | |
01b3c8ab L |
9823 | |
9824 | asection * | |
c0f00686 | 9825 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
9826 | { |
9827 | asection *kept; | |
9828 | ||
9829 | kept = sec->kept_section; | |
9830 | if (kept != NULL) | |
9831 | { | |
c2370991 | 9832 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 9833 | kept = match_group_member (sec, kept, info); |
1dd2625f BW |
9834 | if (kept != NULL |
9835 | && ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
9836 | != (kept->rawsize != 0 ? kept->rawsize : kept->size))) | |
01b3c8ab | 9837 | kept = NULL; |
c2370991 | 9838 | sec->kept_section = kept; |
01b3c8ab L |
9839 | } |
9840 | return kept; | |
9841 | } | |
9842 | ||
c152c796 AM |
9843 | /* Link an input file into the linker output file. This function |
9844 | handles all the sections and relocations of the input file at once. | |
9845 | This is so that we only have to read the local symbols once, and | |
9846 | don't have to keep them in memory. */ | |
9847 | ||
9848 | static bfd_boolean | |
8b127cbc | 9849 | elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) |
c152c796 | 9850 | { |
ece5ef60 | 9851 | int (*relocate_section) |
c152c796 AM |
9852 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
9853 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
9854 | bfd *output_bfd; | |
9855 | Elf_Internal_Shdr *symtab_hdr; | |
9856 | size_t locsymcount; | |
9857 | size_t extsymoff; | |
9858 | Elf_Internal_Sym *isymbuf; | |
9859 | Elf_Internal_Sym *isym; | |
9860 | Elf_Internal_Sym *isymend; | |
9861 | long *pindex; | |
9862 | asection **ppsection; | |
9863 | asection *o; | |
9864 | const struct elf_backend_data *bed; | |
c152c796 | 9865 | struct elf_link_hash_entry **sym_hashes; |
310fd250 L |
9866 | bfd_size_type address_size; |
9867 | bfd_vma r_type_mask; | |
9868 | int r_sym_shift; | |
ffbc01cc | 9869 | bfd_boolean have_file_sym = FALSE; |
c152c796 | 9870 | |
8b127cbc | 9871 | output_bfd = flinfo->output_bfd; |
c152c796 AM |
9872 | bed = get_elf_backend_data (output_bfd); |
9873 | relocate_section = bed->elf_backend_relocate_section; | |
9874 | ||
9875 | /* If this is a dynamic object, we don't want to do anything here: | |
9876 | we don't want the local symbols, and we don't want the section | |
9877 | contents. */ | |
9878 | if ((input_bfd->flags & DYNAMIC) != 0) | |
9879 | return TRUE; | |
9880 | ||
c152c796 AM |
9881 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
9882 | if (elf_bad_symtab (input_bfd)) | |
9883 | { | |
9884 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9885 | extsymoff = 0; | |
9886 | } | |
9887 | else | |
9888 | { | |
9889 | locsymcount = symtab_hdr->sh_info; | |
9890 | extsymoff = symtab_hdr->sh_info; | |
9891 | } | |
9892 | ||
9893 | /* Read the local symbols. */ | |
9894 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
9895 | if (isymbuf == NULL && locsymcount != 0) | |
9896 | { | |
9897 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8b127cbc AM |
9898 | flinfo->internal_syms, |
9899 | flinfo->external_syms, | |
9900 | flinfo->locsym_shndx); | |
c152c796 AM |
9901 | if (isymbuf == NULL) |
9902 | return FALSE; | |
9903 | } | |
9904 | ||
9905 | /* Find local symbol sections and adjust values of symbols in | |
9906 | SEC_MERGE sections. Write out those local symbols we know are | |
9907 | going into the output file. */ | |
9908 | isymend = isymbuf + locsymcount; | |
8b127cbc | 9909 | for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; |
c152c796 AM |
9910 | isym < isymend; |
9911 | isym++, pindex++, ppsection++) | |
9912 | { | |
9913 | asection *isec; | |
9914 | const char *name; | |
9915 | Elf_Internal_Sym osym; | |
6e0b88f1 AM |
9916 | long indx; |
9917 | int ret; | |
c152c796 AM |
9918 | |
9919 | *pindex = -1; | |
9920 | ||
9921 | if (elf_bad_symtab (input_bfd)) | |
9922 | { | |
9923 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
9924 | { | |
9925 | *ppsection = NULL; | |
9926 | continue; | |
9927 | } | |
9928 | } | |
9929 | ||
9930 | if (isym->st_shndx == SHN_UNDEF) | |
9931 | isec = bfd_und_section_ptr; | |
c152c796 AM |
9932 | else if (isym->st_shndx == SHN_ABS) |
9933 | isec = bfd_abs_section_ptr; | |
9934 | else if (isym->st_shndx == SHN_COMMON) | |
9935 | isec = bfd_com_section_ptr; | |
9936 | else | |
9937 | { | |
cb33740c AM |
9938 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
9939 | if (isec == NULL) | |
9940 | { | |
9941 | /* Don't attempt to output symbols with st_shnx in the | |
9942 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
9943 | *ppsection = NULL; | |
9944 | continue; | |
9945 | } | |
dbaa2011 | 9946 | else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE |
cb33740c AM |
9947 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) |
9948 | isym->st_value = | |
9949 | _bfd_merged_section_offset (output_bfd, &isec, | |
9950 | elf_section_data (isec)->sec_info, | |
9951 | isym->st_value); | |
c152c796 AM |
9952 | } |
9953 | ||
9954 | *ppsection = isec; | |
9955 | ||
d983c8c5 AM |
9956 | /* Don't output the first, undefined, symbol. In fact, don't |
9957 | output any undefined local symbol. */ | |
9958 | if (isec == bfd_und_section_ptr) | |
c152c796 AM |
9959 | continue; |
9960 | ||
9961 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
9962 | { | |
9963 | /* We never output section symbols. Instead, we use the | |
9964 | section symbol of the corresponding section in the output | |
9965 | file. */ | |
9966 | continue; | |
9967 | } | |
9968 | ||
9969 | /* If we are stripping all symbols, we don't want to output this | |
9970 | one. */ | |
8b127cbc | 9971 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
9972 | continue; |
9973 | ||
9974 | /* If we are discarding all local symbols, we don't want to | |
9975 | output this one. If we are generating a relocatable output | |
9976 | file, then some of the local symbols may be required by | |
9977 | relocs; we output them below as we discover that they are | |
9978 | needed. */ | |
8b127cbc | 9979 | if (flinfo->info->discard == discard_all) |
c152c796 AM |
9980 | continue; |
9981 | ||
9982 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
9983 | discarding, we don't need to keep it. */ |
9984 | if (isym->st_shndx != SHN_UNDEF | |
4fbb74a6 AM |
9985 | && isym->st_shndx < SHN_LORESERVE |
9986 | && bfd_section_removed_from_list (output_bfd, | |
9987 | isec->output_section)) | |
e75a280b L |
9988 | continue; |
9989 | ||
c152c796 AM |
9990 | /* Get the name of the symbol. */ |
9991 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
9992 | isym->st_name); | |
9993 | if (name == NULL) | |
9994 | return FALSE; | |
9995 | ||
9996 | /* See if we are discarding symbols with this name. */ | |
8b127cbc AM |
9997 | if ((flinfo->info->strip == strip_some |
9998 | && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) | |
c152c796 | 9999 | == NULL)) |
8b127cbc | 10000 | || (((flinfo->info->discard == discard_sec_merge |
0e1862bb L |
10001 | && (isec->flags & SEC_MERGE) |
10002 | && !bfd_link_relocatable (flinfo->info)) | |
8b127cbc | 10003 | || flinfo->info->discard == discard_l) |
c152c796 AM |
10004 | && bfd_is_local_label_name (input_bfd, name))) |
10005 | continue; | |
10006 | ||
ffbc01cc AM |
10007 | if (ELF_ST_TYPE (isym->st_info) == STT_FILE) |
10008 | { | |
ce875075 AM |
10009 | if (input_bfd->lto_output) |
10010 | /* -flto puts a temp file name here. This means builds | |
10011 | are not reproducible. Discard the symbol. */ | |
10012 | continue; | |
ffbc01cc AM |
10013 | have_file_sym = TRUE; |
10014 | flinfo->filesym_count += 1; | |
10015 | } | |
10016 | if (!have_file_sym) | |
10017 | { | |
10018 | /* In the absence of debug info, bfd_find_nearest_line uses | |
10019 | FILE symbols to determine the source file for local | |
10020 | function symbols. Provide a FILE symbol here if input | |
10021 | files lack such, so that their symbols won't be | |
10022 | associated with a previous input file. It's not the | |
10023 | source file, but the best we can do. */ | |
10024 | have_file_sym = TRUE; | |
10025 | flinfo->filesym_count += 1; | |
10026 | memset (&osym, 0, sizeof (osym)); | |
10027 | osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10028 | osym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10029 | if (!elf_link_output_symstrtab (flinfo, |
10030 | (input_bfd->lto_output ? NULL | |
10031 | : input_bfd->filename), | |
10032 | &osym, bfd_abs_section_ptr, | |
10033 | NULL)) | |
ffbc01cc AM |
10034 | return FALSE; |
10035 | } | |
10036 | ||
c152c796 AM |
10037 | osym = *isym; |
10038 | ||
10039 | /* Adjust the section index for the output file. */ | |
10040 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10041 | isec->output_section); | |
10042 | if (osym.st_shndx == SHN_BAD) | |
10043 | return FALSE; | |
10044 | ||
c152c796 AM |
10045 | /* ELF symbols in relocatable files are section relative, but |
10046 | in executable files they are virtual addresses. Note that | |
10047 | this code assumes that all ELF sections have an associated | |
10048 | BFD section with a reasonable value for output_offset; below | |
10049 | we assume that they also have a reasonable value for | |
10050 | output_section. Any special sections must be set up to meet | |
10051 | these requirements. */ | |
10052 | osym.st_value += isec->output_offset; | |
0e1862bb | 10053 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10054 | { |
10055 | osym.st_value += isec->output_section->vma; | |
10056 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
10057 | { | |
10058 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
8b127cbc AM |
10059 | BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL); |
10060 | osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; | |
c152c796 AM |
10061 | } |
10062 | } | |
10063 | ||
6e0b88f1 | 10064 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac | 10065 | ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); |
6e0b88f1 | 10066 | if (ret == 0) |
c152c796 | 10067 | return FALSE; |
6e0b88f1 AM |
10068 | else if (ret == 1) |
10069 | *pindex = indx; | |
c152c796 AM |
10070 | } |
10071 | ||
310fd250 L |
10072 | if (bed->s->arch_size == 32) |
10073 | { | |
10074 | r_type_mask = 0xff; | |
10075 | r_sym_shift = 8; | |
10076 | address_size = 4; | |
10077 | } | |
10078 | else | |
10079 | { | |
10080 | r_type_mask = 0xffffffff; | |
10081 | r_sym_shift = 32; | |
10082 | address_size = 8; | |
10083 | } | |
10084 | ||
c152c796 AM |
10085 | /* Relocate the contents of each section. */ |
10086 | sym_hashes = elf_sym_hashes (input_bfd); | |
10087 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
10088 | { | |
10089 | bfd_byte *contents; | |
10090 | ||
10091 | if (! o->linker_mark) | |
10092 | { | |
10093 | /* This section was omitted from the link. */ | |
10094 | continue; | |
10095 | } | |
10096 | ||
0e1862bb | 10097 | if (bfd_link_relocatable (flinfo->info) |
bcacc0f5 AM |
10098 | && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) |
10099 | { | |
10100 | /* Deal with the group signature symbol. */ | |
10101 | struct bfd_elf_section_data *sec_data = elf_section_data (o); | |
10102 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
10103 | asection *osec = o->output_section; | |
10104 | ||
10105 | if (symndx >= locsymcount | |
10106 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10107 | && flinfo->sections[symndx] == NULL)) |
bcacc0f5 AM |
10108 | { |
10109 | struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; | |
10110 | while (h->root.type == bfd_link_hash_indirect | |
10111 | || h->root.type == bfd_link_hash_warning) | |
10112 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10113 | /* Arrange for symbol to be output. */ | |
10114 | h->indx = -2; | |
10115 | elf_section_data (osec)->this_hdr.sh_info = -2; | |
10116 | } | |
10117 | else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) | |
10118 | { | |
10119 | /* We'll use the output section target_index. */ | |
8b127cbc | 10120 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 AM |
10121 | elf_section_data (osec)->this_hdr.sh_info = sec->target_index; |
10122 | } | |
10123 | else | |
10124 | { | |
8b127cbc | 10125 | if (flinfo->indices[symndx] == -1) |
bcacc0f5 AM |
10126 | { |
10127 | /* Otherwise output the local symbol now. */ | |
10128 | Elf_Internal_Sym sym = isymbuf[symndx]; | |
8b127cbc | 10129 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 | 10130 | const char *name; |
6e0b88f1 AM |
10131 | long indx; |
10132 | int ret; | |
bcacc0f5 AM |
10133 | |
10134 | name = bfd_elf_string_from_elf_section (input_bfd, | |
10135 | symtab_hdr->sh_link, | |
10136 | sym.st_name); | |
10137 | if (name == NULL) | |
10138 | return FALSE; | |
10139 | ||
10140 | sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10141 | sec); | |
10142 | if (sym.st_shndx == SHN_BAD) | |
10143 | return FALSE; | |
10144 | ||
10145 | sym.st_value += o->output_offset; | |
10146 | ||
6e0b88f1 | 10147 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10148 | ret = elf_link_output_symstrtab (flinfo, name, &sym, o, |
10149 | NULL); | |
6e0b88f1 | 10150 | if (ret == 0) |
bcacc0f5 | 10151 | return FALSE; |
6e0b88f1 | 10152 | else if (ret == 1) |
8b127cbc | 10153 | flinfo->indices[symndx] = indx; |
6e0b88f1 AM |
10154 | else |
10155 | abort (); | |
bcacc0f5 AM |
10156 | } |
10157 | elf_section_data (osec)->this_hdr.sh_info | |
8b127cbc | 10158 | = flinfo->indices[symndx]; |
bcacc0f5 AM |
10159 | } |
10160 | } | |
10161 | ||
c152c796 | 10162 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 10163 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
10164 | continue; |
10165 | ||
10166 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
10167 | { | |
10168 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
10169 | or somesuch. */ | |
10170 | continue; | |
10171 | } | |
10172 | ||
10173 | /* Get the contents of the section. They have been cached by a | |
10174 | relaxation routine. Note that o is a section in an input | |
10175 | file, so the contents field will not have been set by any of | |
10176 | the routines which work on output files. */ | |
10177 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
53291d1f AM |
10178 | { |
10179 | contents = elf_section_data (o)->this_hdr.contents; | |
10180 | if (bed->caches_rawsize | |
10181 | && o->rawsize != 0 | |
10182 | && o->rawsize < o->size) | |
10183 | { | |
10184 | memcpy (flinfo->contents, contents, o->rawsize); | |
10185 | contents = flinfo->contents; | |
10186 | } | |
10187 | } | |
c152c796 AM |
10188 | else |
10189 | { | |
8b127cbc | 10190 | contents = flinfo->contents; |
4a114e3e | 10191 | if (! bfd_get_full_section_contents (input_bfd, o, &contents)) |
c152c796 AM |
10192 | return FALSE; |
10193 | } | |
10194 | ||
10195 | if ((o->flags & SEC_RELOC) != 0) | |
10196 | { | |
10197 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 10198 | Elf_Internal_Rela *rel, *relend; |
0f02bbd9 | 10199 | int action_discarded; |
ece5ef60 | 10200 | int ret; |
c152c796 AM |
10201 | |
10202 | /* Get the swapped relocs. */ | |
10203 | internal_relocs | |
8b127cbc AM |
10204 | = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, |
10205 | flinfo->internal_relocs, FALSE); | |
c152c796 AM |
10206 | if (internal_relocs == NULL |
10207 | && o->reloc_count > 0) | |
10208 | return FALSE; | |
10209 | ||
310fd250 L |
10210 | /* We need to reverse-copy input .ctors/.dtors sections if |
10211 | they are placed in .init_array/.finit_array for output. */ | |
10212 | if (o->size > address_size | |
10213 | && ((strncmp (o->name, ".ctors", 6) == 0 | |
10214 | && strcmp (o->output_section->name, | |
10215 | ".init_array") == 0) | |
10216 | || (strncmp (o->name, ".dtors", 6) == 0 | |
10217 | && strcmp (o->output_section->name, | |
10218 | ".fini_array") == 0)) | |
10219 | && (o->name[6] == 0 || o->name[6] == '.')) | |
c152c796 | 10220 | { |
310fd250 L |
10221 | if (o->size != o->reloc_count * address_size) |
10222 | { | |
4eca0228 | 10223 | _bfd_error_handler |
695344c0 | 10224 | /* xgettext:c-format */ |
310fd250 L |
10225 | (_("error: %B: size of section %A is not " |
10226 | "multiple of address size"), | |
10227 | input_bfd, o); | |
10228 | bfd_set_error (bfd_error_on_input); | |
10229 | return FALSE; | |
10230 | } | |
10231 | o->flags |= SEC_ELF_REVERSE_COPY; | |
c152c796 AM |
10232 | } |
10233 | ||
0f02bbd9 | 10234 | action_discarded = -1; |
c152c796 | 10235 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
10236 | action_discarded = (*bed->action_discarded) (o); |
10237 | ||
10238 | /* Run through the relocs evaluating complex reloc symbols and | |
10239 | looking for relocs against symbols from discarded sections | |
10240 | or section symbols from removed link-once sections. | |
10241 | Complain about relocs against discarded sections. Zero | |
10242 | relocs against removed link-once sections. */ | |
10243 | ||
10244 | rel = internal_relocs; | |
10245 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
10246 | for ( ; rel < relend; rel++) | |
c152c796 | 10247 | { |
0f02bbd9 AM |
10248 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
10249 | unsigned int s_type; | |
10250 | asection **ps, *sec; | |
10251 | struct elf_link_hash_entry *h = NULL; | |
10252 | const char *sym_name; | |
c152c796 | 10253 | |
0f02bbd9 AM |
10254 | if (r_symndx == STN_UNDEF) |
10255 | continue; | |
c152c796 | 10256 | |
0f02bbd9 AM |
10257 | if (r_symndx >= locsymcount |
10258 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10259 | && flinfo->sections[r_symndx] == NULL)) |
0f02bbd9 AM |
10260 | { |
10261 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 10262 | |
0f02bbd9 AM |
10263 | /* Badly formatted input files can contain relocs that |
10264 | reference non-existant symbols. Check here so that | |
10265 | we do not seg fault. */ | |
10266 | if (h == NULL) | |
c152c796 | 10267 | { |
0f02bbd9 | 10268 | char buffer [32]; |
dce669a1 | 10269 | |
0f02bbd9 | 10270 | sprintf_vma (buffer, rel->r_info); |
4eca0228 | 10271 | _bfd_error_handler |
695344c0 | 10272 | /* xgettext:c-format */ |
0f02bbd9 AM |
10273 | (_("error: %B contains a reloc (0x%s) for section %A " |
10274 | "that references a non-existent global symbol"), | |
10275 | input_bfd, o, buffer); | |
10276 | bfd_set_error (bfd_error_bad_value); | |
10277 | return FALSE; | |
10278 | } | |
3b36f7e6 | 10279 | |
0f02bbd9 AM |
10280 | while (h->root.type == bfd_link_hash_indirect |
10281 | || h->root.type == bfd_link_hash_warning) | |
10282 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 10283 | |
0f02bbd9 | 10284 | s_type = h->type; |
cdd3575c | 10285 | |
9e2dec47 | 10286 | /* If a plugin symbol is referenced from a non-IR file, |
ca4be51c AM |
10287 | mark the symbol as undefined. Note that the |
10288 | linker may attach linker created dynamic sections | |
10289 | to the plugin bfd. Symbols defined in linker | |
10290 | created sections are not plugin symbols. */ | |
9e2dec47 L |
10291 | if (h->root.non_ir_ref |
10292 | && (h->root.type == bfd_link_hash_defined | |
10293 | || h->root.type == bfd_link_hash_defweak) | |
10294 | && (h->root.u.def.section->flags | |
10295 | & SEC_LINKER_CREATED) == 0 | |
10296 | && h->root.u.def.section->owner != NULL | |
10297 | && (h->root.u.def.section->owner->flags | |
10298 | & BFD_PLUGIN) != 0) | |
10299 | { | |
10300 | h->root.type = bfd_link_hash_undefined; | |
10301 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
10302 | } | |
10303 | ||
0f02bbd9 AM |
10304 | ps = NULL; |
10305 | if (h->root.type == bfd_link_hash_defined | |
10306 | || h->root.type == bfd_link_hash_defweak) | |
10307 | ps = &h->root.u.def.section; | |
10308 | ||
10309 | sym_name = h->root.root.string; | |
10310 | } | |
10311 | else | |
10312 | { | |
10313 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
10314 | ||
10315 | s_type = ELF_ST_TYPE (sym->st_info); | |
8b127cbc | 10316 | ps = &flinfo->sections[r_symndx]; |
0f02bbd9 AM |
10317 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, |
10318 | sym, *ps); | |
10319 | } | |
c152c796 | 10320 | |
c301e700 | 10321 | if ((s_type == STT_RELC || s_type == STT_SRELC) |
0e1862bb | 10322 | && !bfd_link_relocatable (flinfo->info)) |
0f02bbd9 AM |
10323 | { |
10324 | bfd_vma val; | |
10325 | bfd_vma dot = (rel->r_offset | |
10326 | + o->output_offset + o->output_section->vma); | |
10327 | #ifdef DEBUG | |
10328 | printf ("Encountered a complex symbol!"); | |
10329 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
9ccb8af9 AM |
10330 | input_bfd->filename, o->name, |
10331 | (long) (rel - internal_relocs)); | |
0f02bbd9 AM |
10332 | printf (" symbol: idx %8.8lx, name %s\n", |
10333 | r_symndx, sym_name); | |
10334 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
10335 | (unsigned long) rel->r_info, | |
10336 | (unsigned long) rel->r_offset); | |
10337 | #endif | |
8b127cbc | 10338 | if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, |
0f02bbd9 AM |
10339 | isymbuf, locsymcount, s_type == STT_SRELC)) |
10340 | return FALSE; | |
10341 | ||
10342 | /* Symbol evaluated OK. Update to absolute value. */ | |
10343 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
10344 | r_symndx, val); | |
10345 | continue; | |
10346 | } | |
10347 | ||
10348 | if (action_discarded != -1 && ps != NULL) | |
10349 | { | |
cdd3575c AM |
10350 | /* Complain if the definition comes from a |
10351 | discarded section. */ | |
dbaa2011 | 10352 | if ((sec = *ps) != NULL && discarded_section (sec)) |
cdd3575c | 10353 | { |
cf35638d | 10354 | BFD_ASSERT (r_symndx != STN_UNDEF); |
0f02bbd9 | 10355 | if (action_discarded & COMPLAIN) |
8b127cbc | 10356 | (*flinfo->info->callbacks->einfo) |
695344c0 | 10357 | /* xgettext:c-format */ |
e1fffbe6 | 10358 | (_("%X`%s' referenced in section `%A' of %B: " |
58ac56d0 | 10359 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 10360 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 10361 | |
87e5235d | 10362 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 10363 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
10364 | really defined in the kept linkonce section. |
10365 | FIXME: This is quite broken. Modifying the | |
10366 | symbol here means we will be changing all later | |
e0ae6d6f | 10367 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 10368 | if (action_discarded & PRETEND) |
87e5235d | 10369 | { |
01b3c8ab L |
10370 | asection *kept; |
10371 | ||
c0f00686 | 10372 | kept = _bfd_elf_check_kept_section (sec, |
8b127cbc | 10373 | flinfo->info); |
01b3c8ab | 10374 | if (kept != NULL) |
87e5235d AM |
10375 | { |
10376 | *ps = kept; | |
10377 | continue; | |
10378 | } | |
10379 | } | |
c152c796 AM |
10380 | } |
10381 | } | |
10382 | } | |
10383 | ||
10384 | /* Relocate the section by invoking a back end routine. | |
10385 | ||
10386 | The back end routine is responsible for adjusting the | |
10387 | section contents as necessary, and (if using Rela relocs | |
10388 | and generating a relocatable output file) adjusting the | |
10389 | reloc addend as necessary. | |
10390 | ||
10391 | The back end routine does not have to worry about setting | |
10392 | the reloc address or the reloc symbol index. | |
10393 | ||
10394 | The back end routine is given a pointer to the swapped in | |
10395 | internal symbols, and can access the hash table entries | |
10396 | for the external symbols via elf_sym_hashes (input_bfd). | |
10397 | ||
10398 | When generating relocatable output, the back end routine | |
10399 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
10400 | output symbol is going to be a section symbol | |
10401 | corresponding to the output section, which will require | |
10402 | the addend to be adjusted. */ | |
10403 | ||
8b127cbc | 10404 | ret = (*relocate_section) (output_bfd, flinfo->info, |
c152c796 AM |
10405 | input_bfd, o, contents, |
10406 | internal_relocs, | |
10407 | isymbuf, | |
8b127cbc | 10408 | flinfo->sections); |
ece5ef60 | 10409 | if (!ret) |
c152c796 AM |
10410 | return FALSE; |
10411 | ||
ece5ef60 | 10412 | if (ret == 2 |
0e1862bb | 10413 | || bfd_link_relocatable (flinfo->info) |
8b127cbc | 10414 | || flinfo->info->emitrelocations) |
c152c796 AM |
10415 | { |
10416 | Elf_Internal_Rela *irela; | |
d4730f92 | 10417 | Elf_Internal_Rela *irelaend, *irelamid; |
c152c796 AM |
10418 | bfd_vma last_offset; |
10419 | struct elf_link_hash_entry **rel_hash; | |
d4730f92 BS |
10420 | struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; |
10421 | Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; | |
c152c796 | 10422 | unsigned int next_erel; |
c152c796 | 10423 | bfd_boolean rela_normal; |
d4730f92 | 10424 | struct bfd_elf_section_data *esdi, *esdo; |
c152c796 | 10425 | |
d4730f92 BS |
10426 | esdi = elf_section_data (o); |
10427 | esdo = elf_section_data (o->output_section); | |
10428 | rela_normal = FALSE; | |
c152c796 AM |
10429 | |
10430 | /* Adjust the reloc addresses and symbol indices. */ | |
10431 | ||
10432 | irela = internal_relocs; | |
10433 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
d4730f92 BS |
10434 | rel_hash = esdo->rel.hashes + esdo->rel.count; |
10435 | /* We start processing the REL relocs, if any. When we reach | |
10436 | IRELAMID in the loop, we switch to the RELA relocs. */ | |
10437 | irelamid = irela; | |
10438 | if (esdi->rel.hdr != NULL) | |
10439 | irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) | |
10440 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10441 | rel_hash_list = rel_hash; |
d4730f92 | 10442 | rela_hash_list = NULL; |
c152c796 | 10443 | last_offset = o->output_offset; |
0e1862bb | 10444 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10445 | last_offset += o->output_section->vma; |
10446 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
10447 | { | |
10448 | unsigned long r_symndx; | |
10449 | asection *sec; | |
10450 | Elf_Internal_Sym sym; | |
10451 | ||
10452 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
10453 | { | |
10454 | rel_hash++; | |
10455 | next_erel = 0; | |
10456 | } | |
10457 | ||
d4730f92 BS |
10458 | if (irela == irelamid) |
10459 | { | |
10460 | rel_hash = esdo->rela.hashes + esdo->rela.count; | |
10461 | rela_hash_list = rel_hash; | |
10462 | rela_normal = bed->rela_normal; | |
10463 | } | |
10464 | ||
c152c796 | 10465 | irela->r_offset = _bfd_elf_section_offset (output_bfd, |
8b127cbc | 10466 | flinfo->info, o, |
c152c796 AM |
10467 | irela->r_offset); |
10468 | if (irela->r_offset >= (bfd_vma) -2) | |
10469 | { | |
10470 | /* This is a reloc for a deleted entry or somesuch. | |
10471 | Turn it into an R_*_NONE reloc, at the same | |
10472 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 10473 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
10474 | being ordered. */ |
10475 | irela->r_offset = last_offset; | |
10476 | irela->r_info = 0; | |
10477 | irela->r_addend = 0; | |
10478 | continue; | |
10479 | } | |
10480 | ||
10481 | irela->r_offset += o->output_offset; | |
10482 | ||
10483 | /* Relocs in an executable have to be virtual addresses. */ | |
0e1862bb | 10484 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10485 | irela->r_offset += o->output_section->vma; |
10486 | ||
10487 | last_offset = irela->r_offset; | |
10488 | ||
10489 | r_symndx = irela->r_info >> r_sym_shift; | |
10490 | if (r_symndx == STN_UNDEF) | |
10491 | continue; | |
10492 | ||
10493 | if (r_symndx >= locsymcount | |
10494 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10495 | && flinfo->sections[r_symndx] == NULL)) |
c152c796 AM |
10496 | { |
10497 | struct elf_link_hash_entry *rh; | |
10498 | unsigned long indx; | |
10499 | ||
10500 | /* This is a reloc against a global symbol. We | |
10501 | have not yet output all the local symbols, so | |
10502 | we do not know the symbol index of any global | |
10503 | symbol. We set the rel_hash entry for this | |
10504 | reloc to point to the global hash table entry | |
10505 | for this symbol. The symbol index is then | |
ee75fd95 | 10506 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
10507 | indx = r_symndx - extsymoff; |
10508 | rh = elf_sym_hashes (input_bfd)[indx]; | |
10509 | while (rh->root.type == bfd_link_hash_indirect | |
10510 | || rh->root.type == bfd_link_hash_warning) | |
10511 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
10512 | ||
10513 | /* Setting the index to -2 tells | |
10514 | elf_link_output_extsym that this symbol is | |
10515 | used by a reloc. */ | |
10516 | BFD_ASSERT (rh->indx < 0); | |
10517 | rh->indx = -2; | |
10518 | ||
10519 | *rel_hash = rh; | |
10520 | ||
10521 | continue; | |
10522 | } | |
10523 | ||
10524 | /* This is a reloc against a local symbol. */ | |
10525 | ||
10526 | *rel_hash = NULL; | |
10527 | sym = isymbuf[r_symndx]; | |
8b127cbc | 10528 | sec = flinfo->sections[r_symndx]; |
c152c796 AM |
10529 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) |
10530 | { | |
10531 | /* I suppose the backend ought to fill in the | |
10532 | section of any STT_SECTION symbol against a | |
6a8d1586 | 10533 | processor specific section. */ |
cf35638d | 10534 | r_symndx = STN_UNDEF; |
6a8d1586 AM |
10535 | if (bfd_is_abs_section (sec)) |
10536 | ; | |
c152c796 AM |
10537 | else if (sec == NULL || sec->owner == NULL) |
10538 | { | |
10539 | bfd_set_error (bfd_error_bad_value); | |
10540 | return FALSE; | |
10541 | } | |
10542 | else | |
10543 | { | |
6a8d1586 AM |
10544 | asection *osec = sec->output_section; |
10545 | ||
10546 | /* If we have discarded a section, the output | |
10547 | section will be the absolute section. In | |
ab96bf03 AM |
10548 | case of discarded SEC_MERGE sections, use |
10549 | the kept section. relocate_section should | |
10550 | have already handled discarded linkonce | |
10551 | sections. */ | |
6a8d1586 AM |
10552 | if (bfd_is_abs_section (osec) |
10553 | && sec->kept_section != NULL | |
10554 | && sec->kept_section->output_section != NULL) | |
10555 | { | |
10556 | osec = sec->kept_section->output_section; | |
10557 | irela->r_addend -= osec->vma; | |
10558 | } | |
10559 | ||
10560 | if (!bfd_is_abs_section (osec)) | |
10561 | { | |
10562 | r_symndx = osec->target_index; | |
cf35638d | 10563 | if (r_symndx == STN_UNDEF) |
74541ad4 | 10564 | { |
051d833a AM |
10565 | irela->r_addend += osec->vma; |
10566 | osec = _bfd_nearby_section (output_bfd, osec, | |
10567 | osec->vma); | |
10568 | irela->r_addend -= osec->vma; | |
10569 | r_symndx = osec->target_index; | |
74541ad4 | 10570 | } |
6a8d1586 | 10571 | } |
c152c796 AM |
10572 | } |
10573 | ||
10574 | /* Adjust the addend according to where the | |
10575 | section winds up in the output section. */ | |
10576 | if (rela_normal) | |
10577 | irela->r_addend += sec->output_offset; | |
10578 | } | |
10579 | else | |
10580 | { | |
8b127cbc | 10581 | if (flinfo->indices[r_symndx] == -1) |
c152c796 AM |
10582 | { |
10583 | unsigned long shlink; | |
10584 | const char *name; | |
10585 | asection *osec; | |
6e0b88f1 | 10586 | long indx; |
c152c796 | 10587 | |
8b127cbc | 10588 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
10589 | { |
10590 | /* You can't do ld -r -s. */ | |
10591 | bfd_set_error (bfd_error_invalid_operation); | |
10592 | return FALSE; | |
10593 | } | |
10594 | ||
10595 | /* This symbol was skipped earlier, but | |
10596 | since it is needed by a reloc, we | |
10597 | must output it now. */ | |
10598 | shlink = symtab_hdr->sh_link; | |
10599 | name = (bfd_elf_string_from_elf_section | |
10600 | (input_bfd, shlink, sym.st_name)); | |
10601 | if (name == NULL) | |
10602 | return FALSE; | |
10603 | ||
10604 | osec = sec->output_section; | |
10605 | sym.st_shndx = | |
10606 | _bfd_elf_section_from_bfd_section (output_bfd, | |
10607 | osec); | |
10608 | if (sym.st_shndx == SHN_BAD) | |
10609 | return FALSE; | |
10610 | ||
10611 | sym.st_value += sec->output_offset; | |
0e1862bb | 10612 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10613 | { |
10614 | sym.st_value += osec->vma; | |
10615 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
10616 | { | |
10617 | /* STT_TLS symbols are relative to PT_TLS | |
10618 | segment base. */ | |
8b127cbc | 10619 | BFD_ASSERT (elf_hash_table (flinfo->info) |
c152c796 | 10620 | ->tls_sec != NULL); |
8b127cbc | 10621 | sym.st_value -= (elf_hash_table (flinfo->info) |
c152c796 AM |
10622 | ->tls_sec->vma); |
10623 | } | |
10624 | } | |
10625 | ||
6e0b88f1 | 10626 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10627 | ret = elf_link_output_symstrtab (flinfo, name, |
10628 | &sym, sec, | |
10629 | NULL); | |
6e0b88f1 | 10630 | if (ret == 0) |
c152c796 | 10631 | return FALSE; |
6e0b88f1 | 10632 | else if (ret == 1) |
8b127cbc | 10633 | flinfo->indices[r_symndx] = indx; |
6e0b88f1 AM |
10634 | else |
10635 | abort (); | |
c152c796 AM |
10636 | } |
10637 | ||
8b127cbc | 10638 | r_symndx = flinfo->indices[r_symndx]; |
c152c796 AM |
10639 | } |
10640 | ||
10641 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
10642 | | (irela->r_info & r_type_mask)); | |
10643 | } | |
10644 | ||
10645 | /* Swap out the relocs. */ | |
d4730f92 BS |
10646 | input_rel_hdr = esdi->rel.hdr; |
10647 | if (input_rel_hdr && input_rel_hdr->sh_size != 0) | |
c152c796 | 10648 | { |
d4730f92 BS |
10649 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
10650 | input_rel_hdr, | |
10651 | internal_relocs, | |
10652 | rel_hash_list)) | |
10653 | return FALSE; | |
c152c796 AM |
10654 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) |
10655 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10656 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
d4730f92 BS |
10657 | } |
10658 | ||
10659 | input_rela_hdr = esdi->rela.hdr; | |
10660 | if (input_rela_hdr && input_rela_hdr->sh_size != 0) | |
10661 | { | |
eac338cf | 10662 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
d4730f92 | 10663 | input_rela_hdr, |
eac338cf | 10664 | internal_relocs, |
d4730f92 | 10665 | rela_hash_list)) |
c152c796 AM |
10666 | return FALSE; |
10667 | } | |
10668 | } | |
10669 | } | |
10670 | ||
10671 | /* Write out the modified section contents. */ | |
10672 | if (bed->elf_backend_write_section | |
8b127cbc | 10673 | && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, |
c7b8f16e | 10674 | contents)) |
c152c796 AM |
10675 | { |
10676 | /* Section written out. */ | |
10677 | } | |
10678 | else switch (o->sec_info_type) | |
10679 | { | |
dbaa2011 | 10680 | case SEC_INFO_TYPE_STABS: |
c152c796 AM |
10681 | if (! (_bfd_write_section_stabs |
10682 | (output_bfd, | |
8b127cbc | 10683 | &elf_hash_table (flinfo->info)->stab_info, |
c152c796 AM |
10684 | o, &elf_section_data (o)->sec_info, contents))) |
10685 | return FALSE; | |
10686 | break; | |
dbaa2011 | 10687 | case SEC_INFO_TYPE_MERGE: |
c152c796 AM |
10688 | if (! _bfd_write_merged_section (output_bfd, o, |
10689 | elf_section_data (o)->sec_info)) | |
10690 | return FALSE; | |
10691 | break; | |
dbaa2011 | 10692 | case SEC_INFO_TYPE_EH_FRAME: |
c152c796 | 10693 | { |
8b127cbc | 10694 | if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, |
c152c796 AM |
10695 | o, contents)) |
10696 | return FALSE; | |
10697 | } | |
10698 | break; | |
2f0c68f2 CM |
10699 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
10700 | { | |
10701 | if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, | |
10702 | flinfo->info, | |
10703 | o, contents)) | |
10704 | return FALSE; | |
10705 | } | |
10706 | break; | |
c152c796 AM |
10707 | default: |
10708 | { | |
310fd250 L |
10709 | if (! (o->flags & SEC_EXCLUDE)) |
10710 | { | |
10711 | file_ptr offset = (file_ptr) o->output_offset; | |
10712 | bfd_size_type todo = o->size; | |
37b01f6a DG |
10713 | |
10714 | offset *= bfd_octets_per_byte (output_bfd); | |
10715 | ||
310fd250 L |
10716 | if ((o->flags & SEC_ELF_REVERSE_COPY)) |
10717 | { | |
10718 | /* Reverse-copy input section to output. */ | |
10719 | do | |
10720 | { | |
10721 | todo -= address_size; | |
10722 | if (! bfd_set_section_contents (output_bfd, | |
10723 | o->output_section, | |
10724 | contents + todo, | |
10725 | offset, | |
10726 | address_size)) | |
10727 | return FALSE; | |
10728 | if (todo == 0) | |
10729 | break; | |
10730 | offset += address_size; | |
10731 | } | |
10732 | while (1); | |
10733 | } | |
10734 | else if (! bfd_set_section_contents (output_bfd, | |
10735 | o->output_section, | |
10736 | contents, | |
10737 | offset, todo)) | |
10738 | return FALSE; | |
10739 | } | |
c152c796 AM |
10740 | } |
10741 | break; | |
10742 | } | |
10743 | } | |
10744 | ||
10745 | return TRUE; | |
10746 | } | |
10747 | ||
10748 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 10749 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
10750 | is used to build constructor and destructor tables when linking |
10751 | with -Ur. */ | |
10752 | ||
10753 | static bfd_boolean | |
10754 | elf_reloc_link_order (bfd *output_bfd, | |
10755 | struct bfd_link_info *info, | |
10756 | asection *output_section, | |
10757 | struct bfd_link_order *link_order) | |
10758 | { | |
10759 | reloc_howto_type *howto; | |
10760 | long indx; | |
10761 | bfd_vma offset; | |
10762 | bfd_vma addend; | |
d4730f92 | 10763 | struct bfd_elf_section_reloc_data *reldata; |
c152c796 AM |
10764 | struct elf_link_hash_entry **rel_hash_ptr; |
10765 | Elf_Internal_Shdr *rel_hdr; | |
10766 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
10767 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
10768 | bfd_byte *erel; | |
10769 | unsigned int i; | |
d4730f92 | 10770 | struct bfd_elf_section_data *esdo = elf_section_data (output_section); |
c152c796 AM |
10771 | |
10772 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
10773 | if (howto == NULL) | |
10774 | { | |
10775 | bfd_set_error (bfd_error_bad_value); | |
10776 | return FALSE; | |
10777 | } | |
10778 | ||
10779 | addend = link_order->u.reloc.p->addend; | |
10780 | ||
d4730f92 BS |
10781 | if (esdo->rel.hdr) |
10782 | reldata = &esdo->rel; | |
10783 | else if (esdo->rela.hdr) | |
10784 | reldata = &esdo->rela; | |
10785 | else | |
10786 | { | |
10787 | reldata = NULL; | |
10788 | BFD_ASSERT (0); | |
10789 | } | |
10790 | ||
c152c796 | 10791 | /* Figure out the symbol index. */ |
d4730f92 | 10792 | rel_hash_ptr = reldata->hashes + reldata->count; |
c152c796 AM |
10793 | if (link_order->type == bfd_section_reloc_link_order) |
10794 | { | |
10795 | indx = link_order->u.reloc.p->u.section->target_index; | |
10796 | BFD_ASSERT (indx != 0); | |
10797 | *rel_hash_ptr = NULL; | |
10798 | } | |
10799 | else | |
10800 | { | |
10801 | struct elf_link_hash_entry *h; | |
10802 | ||
10803 | /* Treat a reloc against a defined symbol as though it were | |
10804 | actually against the section. */ | |
10805 | h = ((struct elf_link_hash_entry *) | |
10806 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
10807 | link_order->u.reloc.p->u.name, | |
10808 | FALSE, FALSE, TRUE)); | |
10809 | if (h != NULL | |
10810 | && (h->root.type == bfd_link_hash_defined | |
10811 | || h->root.type == bfd_link_hash_defweak)) | |
10812 | { | |
10813 | asection *section; | |
10814 | ||
10815 | section = h->root.u.def.section; | |
10816 | indx = section->output_section->target_index; | |
10817 | *rel_hash_ptr = NULL; | |
10818 | /* It seems that we ought to add the symbol value to the | |
10819 | addend here, but in practice it has already been added | |
10820 | because it was passed to constructor_callback. */ | |
10821 | addend += section->output_section->vma + section->output_offset; | |
10822 | } | |
10823 | else if (h != NULL) | |
10824 | { | |
10825 | /* Setting the index to -2 tells elf_link_output_extsym that | |
10826 | this symbol is used by a reloc. */ | |
10827 | h->indx = -2; | |
10828 | *rel_hash_ptr = h; | |
10829 | indx = 0; | |
10830 | } | |
10831 | else | |
10832 | { | |
1a72702b AM |
10833 | (*info->callbacks->unattached_reloc) |
10834 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); | |
c152c796 AM |
10835 | indx = 0; |
10836 | } | |
10837 | } | |
10838 | ||
10839 | /* If this is an inplace reloc, we must write the addend into the | |
10840 | object file. */ | |
10841 | if (howto->partial_inplace && addend != 0) | |
10842 | { | |
10843 | bfd_size_type size; | |
10844 | bfd_reloc_status_type rstat; | |
10845 | bfd_byte *buf; | |
10846 | bfd_boolean ok; | |
10847 | const char *sym_name; | |
10848 | ||
a50b1753 NC |
10849 | size = (bfd_size_type) bfd_get_reloc_size (howto); |
10850 | buf = (bfd_byte *) bfd_zmalloc (size); | |
6346d5ca | 10851 | if (buf == NULL && size != 0) |
c152c796 AM |
10852 | return FALSE; |
10853 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
10854 | switch (rstat) | |
10855 | { | |
10856 | case bfd_reloc_ok: | |
10857 | break; | |
10858 | ||
10859 | default: | |
10860 | case bfd_reloc_outofrange: | |
10861 | abort (); | |
10862 | ||
10863 | case bfd_reloc_overflow: | |
10864 | if (link_order->type == bfd_section_reloc_link_order) | |
10865 | sym_name = bfd_section_name (output_bfd, | |
10866 | link_order->u.reloc.p->u.section); | |
10867 | else | |
10868 | sym_name = link_order->u.reloc.p->u.name; | |
1a72702b AM |
10869 | (*info->callbacks->reloc_overflow) (info, NULL, sym_name, |
10870 | howto->name, addend, NULL, NULL, | |
10871 | (bfd_vma) 0); | |
c152c796 AM |
10872 | break; |
10873 | } | |
37b01f6a | 10874 | |
c152c796 | 10875 | ok = bfd_set_section_contents (output_bfd, output_section, buf, |
37b01f6a DG |
10876 | link_order->offset |
10877 | * bfd_octets_per_byte (output_bfd), | |
10878 | size); | |
c152c796 AM |
10879 | free (buf); |
10880 | if (! ok) | |
10881 | return FALSE; | |
10882 | } | |
10883 | ||
10884 | /* The address of a reloc is relative to the section in a | |
10885 | relocatable file, and is a virtual address in an executable | |
10886 | file. */ | |
10887 | offset = link_order->offset; | |
0e1862bb | 10888 | if (! bfd_link_relocatable (info)) |
c152c796 AM |
10889 | offset += output_section->vma; |
10890 | ||
10891 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
10892 | { | |
10893 | irel[i].r_offset = offset; | |
10894 | irel[i].r_info = 0; | |
10895 | irel[i].r_addend = 0; | |
10896 | } | |
10897 | if (bed->s->arch_size == 32) | |
10898 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
10899 | else | |
10900 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
10901 | ||
d4730f92 | 10902 | rel_hdr = reldata->hdr; |
c152c796 AM |
10903 | erel = rel_hdr->contents; |
10904 | if (rel_hdr->sh_type == SHT_REL) | |
10905 | { | |
d4730f92 | 10906 | erel += reldata->count * bed->s->sizeof_rel; |
c152c796 AM |
10907 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); |
10908 | } | |
10909 | else | |
10910 | { | |
10911 | irel[0].r_addend = addend; | |
d4730f92 | 10912 | erel += reldata->count * bed->s->sizeof_rela; |
c152c796 AM |
10913 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); |
10914 | } | |
10915 | ||
d4730f92 | 10916 | ++reldata->count; |
c152c796 AM |
10917 | |
10918 | return TRUE; | |
10919 | } | |
10920 | ||
0b52efa6 PB |
10921 | |
10922 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
10923 | ||
10924 | static bfd_vma | |
10925 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
10926 | { | |
10927 | Elf_Internal_Shdr **elf_shdrp; | |
10928 | asection *s; | |
10929 | int elfsec; | |
10930 | ||
10931 | s = p->u.indirect.section; | |
10932 | elf_shdrp = elf_elfsections (s->owner); | |
10933 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
10934 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
10935 | /* PR 290: |
10936 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 10937 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
10938 | sh_info fields. Hence we could get the situation |
10939 | where elfsec is 0. */ | |
10940 | if (elfsec == 0) | |
10941 | { | |
10942 | const struct elf_backend_data *bed | |
10943 | = get_elf_backend_data (s->owner); | |
10944 | if (bed->link_order_error_handler) | |
d003868e | 10945 | bed->link_order_error_handler |
695344c0 | 10946 | /* xgettext:c-format */ |
d003868e | 10947 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); |
185d09ad L |
10948 | return 0; |
10949 | } | |
10950 | else | |
10951 | { | |
10952 | s = elf_shdrp[elfsec]->bfd_section; | |
10953 | return s->output_section->vma + s->output_offset; | |
10954 | } | |
0b52efa6 PB |
10955 | } |
10956 | ||
10957 | ||
10958 | /* Compare two sections based on the locations of the sections they are | |
10959 | linked to. Used by elf_fixup_link_order. */ | |
10960 | ||
10961 | static int | |
10962 | compare_link_order (const void * a, const void * b) | |
10963 | { | |
10964 | bfd_vma apos; | |
10965 | bfd_vma bpos; | |
10966 | ||
10967 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
10968 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
10969 | if (apos < bpos) | |
10970 | return -1; | |
10971 | return apos > bpos; | |
10972 | } | |
10973 | ||
10974 | ||
10975 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
10976 | order as their linked sections. Returns false if this could not be done | |
10977 | because an output section includes both ordered and unordered | |
10978 | sections. Ideally we'd do this in the linker proper. */ | |
10979 | ||
10980 | static bfd_boolean | |
10981 | elf_fixup_link_order (bfd *abfd, asection *o) | |
10982 | { | |
10983 | int seen_linkorder; | |
10984 | int seen_other; | |
10985 | int n; | |
10986 | struct bfd_link_order *p; | |
10987 | bfd *sub; | |
10988 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 10989 | unsigned elfsec; |
0b52efa6 | 10990 | struct bfd_link_order **sections; |
d33cdfe3 | 10991 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 10992 | bfd_vma offset; |
3b36f7e6 | 10993 | |
d33cdfe3 L |
10994 | other_sec = NULL; |
10995 | linkorder_sec = NULL; | |
0b52efa6 PB |
10996 | seen_other = 0; |
10997 | seen_linkorder = 0; | |
8423293d | 10998 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 10999 | { |
d33cdfe3 | 11000 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
11001 | { |
11002 | s = p->u.indirect.section; | |
d33cdfe3 L |
11003 | sub = s->owner; |
11004 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
11005 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
11006 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
11007 | && elfsec < elf_numsections (sub) | |
4fbb74a6 AM |
11008 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER |
11009 | && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) | |
d33cdfe3 L |
11010 | { |
11011 | seen_linkorder++; | |
11012 | linkorder_sec = s; | |
11013 | } | |
0b52efa6 | 11014 | else |
d33cdfe3 L |
11015 | { |
11016 | seen_other++; | |
11017 | other_sec = s; | |
11018 | } | |
0b52efa6 PB |
11019 | } |
11020 | else | |
11021 | seen_other++; | |
d33cdfe3 L |
11022 | |
11023 | if (seen_other && seen_linkorder) | |
11024 | { | |
11025 | if (other_sec && linkorder_sec) | |
4eca0228 | 11026 | _bfd_error_handler |
695344c0 | 11027 | /* xgettext:c-format */ |
4eca0228 AM |
11028 | (_("%A has both ordered [`%A' in %B] " |
11029 | "and unordered [`%A' in %B] sections"), | |
11030 | o, linkorder_sec, | |
11031 | linkorder_sec->owner, other_sec, | |
11032 | other_sec->owner); | |
d33cdfe3 | 11033 | else |
4eca0228 AM |
11034 | _bfd_error_handler |
11035 | (_("%A has both ordered and unordered sections"), o); | |
d33cdfe3 L |
11036 | bfd_set_error (bfd_error_bad_value); |
11037 | return FALSE; | |
11038 | } | |
0b52efa6 PB |
11039 | } |
11040 | ||
11041 | if (!seen_linkorder) | |
11042 | return TRUE; | |
11043 | ||
0b52efa6 | 11044 | sections = (struct bfd_link_order **) |
14b1c01e AM |
11045 | bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); |
11046 | if (sections == NULL) | |
11047 | return FALSE; | |
0b52efa6 | 11048 | seen_linkorder = 0; |
3b36f7e6 | 11049 | |
8423293d | 11050 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
11051 | { |
11052 | sections[seen_linkorder++] = p; | |
11053 | } | |
11054 | /* Sort the input sections in the order of their linked section. */ | |
11055 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
11056 | compare_link_order); | |
11057 | ||
11058 | /* Change the offsets of the sections. */ | |
11059 | offset = 0; | |
11060 | for (n = 0; n < seen_linkorder; n++) | |
11061 | { | |
11062 | s = sections[n]->u.indirect.section; | |
461686a3 | 11063 | offset &= ~(bfd_vma) 0 << s->alignment_power; |
37b01f6a | 11064 | s->output_offset = offset / bfd_octets_per_byte (abfd); |
0b52efa6 PB |
11065 | sections[n]->offset = offset; |
11066 | offset += sections[n]->size; | |
11067 | } | |
11068 | ||
4dd07732 | 11069 | free (sections); |
0b52efa6 PB |
11070 | return TRUE; |
11071 | } | |
11072 | ||
76359541 TP |
11073 | /* Generate an import library in INFO->implib_bfd from symbols in ABFD. |
11074 | Returns TRUE upon success, FALSE otherwise. */ | |
11075 | ||
11076 | static bfd_boolean | |
11077 | elf_output_implib (bfd *abfd, struct bfd_link_info *info) | |
11078 | { | |
11079 | bfd_boolean ret = FALSE; | |
11080 | bfd *implib_bfd; | |
11081 | const struct elf_backend_data *bed; | |
11082 | flagword flags; | |
11083 | enum bfd_architecture arch; | |
11084 | unsigned int mach; | |
11085 | asymbol **sympp = NULL; | |
11086 | long symsize; | |
11087 | long symcount; | |
11088 | long src_count; | |
11089 | elf_symbol_type *osymbuf; | |
11090 | ||
11091 | implib_bfd = info->out_implib_bfd; | |
11092 | bed = get_elf_backend_data (abfd); | |
11093 | ||
11094 | if (!bfd_set_format (implib_bfd, bfd_object)) | |
11095 | return FALSE; | |
11096 | ||
11097 | flags = bfd_get_file_flags (abfd); | |
11098 | flags &= ~HAS_RELOC; | |
11099 | if (!bfd_set_start_address (implib_bfd, 0) | |
11100 | || !bfd_set_file_flags (implib_bfd, flags)) | |
11101 | return FALSE; | |
11102 | ||
11103 | /* Copy architecture of output file to import library file. */ | |
11104 | arch = bfd_get_arch (abfd); | |
11105 | mach = bfd_get_mach (abfd); | |
11106 | if (!bfd_set_arch_mach (implib_bfd, arch, mach) | |
11107 | && (abfd->target_defaulted | |
11108 | || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) | |
11109 | return FALSE; | |
11110 | ||
11111 | /* Get symbol table size. */ | |
11112 | symsize = bfd_get_symtab_upper_bound (abfd); | |
11113 | if (symsize < 0) | |
11114 | return FALSE; | |
11115 | ||
11116 | /* Read in the symbol table. */ | |
11117 | sympp = (asymbol **) xmalloc (symsize); | |
11118 | symcount = bfd_canonicalize_symtab (abfd, sympp); | |
11119 | if (symcount < 0) | |
11120 | goto free_sym_buf; | |
11121 | ||
11122 | /* Allow the BFD backend to copy any private header data it | |
11123 | understands from the output BFD to the import library BFD. */ | |
11124 | if (! bfd_copy_private_header_data (abfd, implib_bfd)) | |
11125 | goto free_sym_buf; | |
11126 | ||
11127 | /* Filter symbols to appear in the import library. */ | |
11128 | if (bed->elf_backend_filter_implib_symbols) | |
11129 | symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, | |
11130 | symcount); | |
11131 | else | |
11132 | symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); | |
11133 | if (symcount == 0) | |
11134 | { | |
5df1bc57 | 11135 | bfd_set_error (bfd_error_no_symbols); |
4eca0228 AM |
11136 | _bfd_error_handler (_("%B: no symbol found for import library"), |
11137 | implib_bfd); | |
76359541 TP |
11138 | goto free_sym_buf; |
11139 | } | |
11140 | ||
11141 | ||
11142 | /* Make symbols absolute. */ | |
11143 | osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount, | |
11144 | sizeof (*osymbuf)); | |
11145 | for (src_count = 0; src_count < symcount; src_count++) | |
11146 | { | |
11147 | memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], | |
11148 | sizeof (*osymbuf)); | |
11149 | osymbuf[src_count].symbol.section = bfd_abs_section_ptr; | |
11150 | osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; | |
11151 | osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; | |
11152 | osymbuf[src_count].internal_elf_sym.st_value = | |
11153 | osymbuf[src_count].symbol.value; | |
11154 | sympp[src_count] = &osymbuf[src_count].symbol; | |
11155 | } | |
11156 | ||
11157 | bfd_set_symtab (implib_bfd, sympp, symcount); | |
11158 | ||
11159 | /* Allow the BFD backend to copy any private data it understands | |
11160 | from the output BFD to the import library BFD. This is done last | |
11161 | to permit the routine to look at the filtered symbol table. */ | |
11162 | if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) | |
11163 | goto free_sym_buf; | |
11164 | ||
11165 | if (!bfd_close (implib_bfd)) | |
11166 | goto free_sym_buf; | |
11167 | ||
11168 | ret = TRUE; | |
11169 | ||
11170 | free_sym_buf: | |
11171 | free (sympp); | |
11172 | return ret; | |
11173 | } | |
11174 | ||
9f7c3e5e AM |
11175 | static void |
11176 | elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) | |
11177 | { | |
11178 | asection *o; | |
11179 | ||
11180 | if (flinfo->symstrtab != NULL) | |
ef10c3ac | 11181 | _bfd_elf_strtab_free (flinfo->symstrtab); |
9f7c3e5e AM |
11182 | if (flinfo->contents != NULL) |
11183 | free (flinfo->contents); | |
11184 | if (flinfo->external_relocs != NULL) | |
11185 | free (flinfo->external_relocs); | |
11186 | if (flinfo->internal_relocs != NULL) | |
11187 | free (flinfo->internal_relocs); | |
11188 | if (flinfo->external_syms != NULL) | |
11189 | free (flinfo->external_syms); | |
11190 | if (flinfo->locsym_shndx != NULL) | |
11191 | free (flinfo->locsym_shndx); | |
11192 | if (flinfo->internal_syms != NULL) | |
11193 | free (flinfo->internal_syms); | |
11194 | if (flinfo->indices != NULL) | |
11195 | free (flinfo->indices); | |
11196 | if (flinfo->sections != NULL) | |
11197 | free (flinfo->sections); | |
9f7c3e5e AM |
11198 | if (flinfo->symshndxbuf != NULL) |
11199 | free (flinfo->symshndxbuf); | |
11200 | for (o = obfd->sections; o != NULL; o = o->next) | |
11201 | { | |
11202 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11203 | if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) | |
11204 | free (esdo->rel.hashes); | |
11205 | if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) | |
11206 | free (esdo->rela.hashes); | |
11207 | } | |
11208 | } | |
0b52efa6 | 11209 | |
c152c796 AM |
11210 | /* Do the final step of an ELF link. */ |
11211 | ||
11212 | bfd_boolean | |
11213 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
11214 | { | |
11215 | bfd_boolean dynamic; | |
11216 | bfd_boolean emit_relocs; | |
11217 | bfd *dynobj; | |
8b127cbc | 11218 | struct elf_final_link_info flinfo; |
91d6fa6a NC |
11219 | asection *o; |
11220 | struct bfd_link_order *p; | |
11221 | bfd *sub; | |
c152c796 AM |
11222 | bfd_size_type max_contents_size; |
11223 | bfd_size_type max_external_reloc_size; | |
11224 | bfd_size_type max_internal_reloc_count; | |
11225 | bfd_size_type max_sym_count; | |
11226 | bfd_size_type max_sym_shndx_count; | |
c152c796 AM |
11227 | Elf_Internal_Sym elfsym; |
11228 | unsigned int i; | |
11229 | Elf_Internal_Shdr *symtab_hdr; | |
11230 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
c152c796 AM |
11231 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11232 | struct elf_outext_info eoinfo; | |
11233 | bfd_boolean merged; | |
11234 | size_t relativecount = 0; | |
11235 | asection *reldyn = 0; | |
11236 | bfd_size_type amt; | |
104d59d1 JM |
11237 | asection *attr_section = NULL; |
11238 | bfd_vma attr_size = 0; | |
11239 | const char *std_attrs_section; | |
64f52338 | 11240 | struct elf_link_hash_table *htab = elf_hash_table (info); |
c152c796 | 11241 | |
64f52338 | 11242 | if (!is_elf_hash_table (htab)) |
c152c796 AM |
11243 | return FALSE; |
11244 | ||
0e1862bb | 11245 | if (bfd_link_pic (info)) |
c152c796 AM |
11246 | abfd->flags |= DYNAMIC; |
11247 | ||
64f52338 AM |
11248 | dynamic = htab->dynamic_sections_created; |
11249 | dynobj = htab->dynobj; | |
c152c796 | 11250 | |
0e1862bb | 11251 | emit_relocs = (bfd_link_relocatable (info) |
a4676736 | 11252 | || info->emitrelocations); |
c152c796 | 11253 | |
8b127cbc AM |
11254 | flinfo.info = info; |
11255 | flinfo.output_bfd = abfd; | |
ef10c3ac | 11256 | flinfo.symstrtab = _bfd_elf_strtab_init (); |
8b127cbc | 11257 | if (flinfo.symstrtab == NULL) |
c152c796 AM |
11258 | return FALSE; |
11259 | ||
11260 | if (! dynamic) | |
11261 | { | |
8b127cbc AM |
11262 | flinfo.hash_sec = NULL; |
11263 | flinfo.symver_sec = NULL; | |
c152c796 AM |
11264 | } |
11265 | else | |
11266 | { | |
3d4d4302 | 11267 | flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); |
202e2356 | 11268 | /* Note that dynsym_sec can be NULL (on VMS). */ |
3d4d4302 | 11269 | flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); |
c152c796 AM |
11270 | /* Note that it is OK if symver_sec is NULL. */ |
11271 | } | |
11272 | ||
8b127cbc AM |
11273 | flinfo.contents = NULL; |
11274 | flinfo.external_relocs = NULL; | |
11275 | flinfo.internal_relocs = NULL; | |
11276 | flinfo.external_syms = NULL; | |
11277 | flinfo.locsym_shndx = NULL; | |
11278 | flinfo.internal_syms = NULL; | |
11279 | flinfo.indices = NULL; | |
11280 | flinfo.sections = NULL; | |
8b127cbc | 11281 | flinfo.symshndxbuf = NULL; |
ffbc01cc | 11282 | flinfo.filesym_count = 0; |
c152c796 | 11283 | |
104d59d1 JM |
11284 | /* The object attributes have been merged. Remove the input |
11285 | sections from the link, and set the contents of the output | |
11286 | secton. */ | |
11287 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; | |
11288 | for (o = abfd->sections; o != NULL; o = o->next) | |
11289 | { | |
11290 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) | |
11291 | || strcmp (o->name, ".gnu.attributes") == 0) | |
11292 | { | |
11293 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
11294 | { | |
11295 | asection *input_section; | |
11296 | ||
11297 | if (p->type != bfd_indirect_link_order) | |
11298 | continue; | |
11299 | input_section = p->u.indirect.section; | |
11300 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
11301 | elf_link_input_bfd ignores this section. */ | |
11302 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
11303 | } | |
a0c8462f | 11304 | |
104d59d1 JM |
11305 | attr_size = bfd_elf_obj_attr_size (abfd); |
11306 | if (attr_size) | |
11307 | { | |
11308 | bfd_set_section_size (abfd, o, attr_size); | |
11309 | attr_section = o; | |
11310 | /* Skip this section later on. */ | |
11311 | o->map_head.link_order = NULL; | |
11312 | } | |
11313 | else | |
11314 | o->flags |= SEC_EXCLUDE; | |
11315 | } | |
11316 | } | |
11317 | ||
c152c796 AM |
11318 | /* Count up the number of relocations we will output for each output |
11319 | section, so that we know the sizes of the reloc sections. We | |
11320 | also figure out some maximum sizes. */ | |
11321 | max_contents_size = 0; | |
11322 | max_external_reloc_size = 0; | |
11323 | max_internal_reloc_count = 0; | |
11324 | max_sym_count = 0; | |
11325 | max_sym_shndx_count = 0; | |
11326 | merged = FALSE; | |
11327 | for (o = abfd->sections; o != NULL; o = o->next) | |
11328 | { | |
11329 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11330 | o->reloc_count = 0; | |
11331 | ||
8423293d | 11332 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
11333 | { |
11334 | unsigned int reloc_count = 0; | |
9eaff861 | 11335 | unsigned int additional_reloc_count = 0; |
c152c796 | 11336 | struct bfd_elf_section_data *esdi = NULL; |
c152c796 AM |
11337 | |
11338 | if (p->type == bfd_section_reloc_link_order | |
11339 | || p->type == bfd_symbol_reloc_link_order) | |
11340 | reloc_count = 1; | |
11341 | else if (p->type == bfd_indirect_link_order) | |
11342 | { | |
11343 | asection *sec; | |
11344 | ||
11345 | sec = p->u.indirect.section; | |
11346 | esdi = elf_section_data (sec); | |
11347 | ||
11348 | /* Mark all sections which are to be included in the | |
11349 | link. This will normally be every section. We need | |
11350 | to do this so that we can identify any sections which | |
11351 | the linker has decided to not include. */ | |
11352 | sec->linker_mark = TRUE; | |
11353 | ||
11354 | if (sec->flags & SEC_MERGE) | |
11355 | merged = TRUE; | |
11356 | ||
aed64b35 L |
11357 | if (esdo->this_hdr.sh_type == SHT_REL |
11358 | || esdo->this_hdr.sh_type == SHT_RELA) | |
11359 | /* Some backends use reloc_count in relocation sections | |
11360 | to count particular types of relocs. Of course, | |
11361 | reloc sections themselves can't have relocations. */ | |
11362 | reloc_count = 0; | |
0e1862bb | 11363 | else if (emit_relocs) |
491d01d3 YU |
11364 | { |
11365 | reloc_count = sec->reloc_count; | |
11366 | if (bed->elf_backend_count_additional_relocs) | |
11367 | { | |
11368 | int c; | |
11369 | c = (*bed->elf_backend_count_additional_relocs) (sec); | |
11370 | additional_reloc_count += c; | |
11371 | } | |
11372 | } | |
c152c796 | 11373 | else if (bed->elf_backend_count_relocs) |
58217f29 | 11374 | reloc_count = (*bed->elf_backend_count_relocs) (info, sec); |
c152c796 | 11375 | |
eea6121a AM |
11376 | if (sec->rawsize > max_contents_size) |
11377 | max_contents_size = sec->rawsize; | |
11378 | if (sec->size > max_contents_size) | |
11379 | max_contents_size = sec->size; | |
c152c796 AM |
11380 | |
11381 | /* We are interested in just local symbols, not all | |
11382 | symbols. */ | |
11383 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
11384 | && (sec->owner->flags & DYNAMIC) == 0) | |
11385 | { | |
11386 | size_t sym_count; | |
11387 | ||
11388 | if (elf_bad_symtab (sec->owner)) | |
11389 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
11390 | / bed->s->sizeof_sym); | |
11391 | else | |
11392 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
11393 | ||
11394 | if (sym_count > max_sym_count) | |
11395 | max_sym_count = sym_count; | |
11396 | ||
11397 | if (sym_count > max_sym_shndx_count | |
6a40cf0c | 11398 | && elf_symtab_shndx_list (sec->owner) != NULL) |
c152c796 AM |
11399 | max_sym_shndx_count = sym_count; |
11400 | ||
11401 | if ((sec->flags & SEC_RELOC) != 0) | |
11402 | { | |
d4730f92 | 11403 | size_t ext_size = 0; |
c152c796 | 11404 | |
d4730f92 BS |
11405 | if (esdi->rel.hdr != NULL) |
11406 | ext_size = esdi->rel.hdr->sh_size; | |
11407 | if (esdi->rela.hdr != NULL) | |
11408 | ext_size += esdi->rela.hdr->sh_size; | |
7326c758 | 11409 | |
c152c796 AM |
11410 | if (ext_size > max_external_reloc_size) |
11411 | max_external_reloc_size = ext_size; | |
11412 | if (sec->reloc_count > max_internal_reloc_count) | |
11413 | max_internal_reloc_count = sec->reloc_count; | |
11414 | } | |
11415 | } | |
11416 | } | |
11417 | ||
11418 | if (reloc_count == 0) | |
11419 | continue; | |
11420 | ||
9eaff861 | 11421 | reloc_count += additional_reloc_count; |
c152c796 AM |
11422 | o->reloc_count += reloc_count; |
11423 | ||
0e1862bb | 11424 | if (p->type == bfd_indirect_link_order && emit_relocs) |
c152c796 | 11425 | { |
d4730f92 | 11426 | if (esdi->rel.hdr) |
9eaff861 | 11427 | { |
491d01d3 | 11428 | esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); |
9eaff861 AO |
11429 | esdo->rel.count += additional_reloc_count; |
11430 | } | |
d4730f92 | 11431 | if (esdi->rela.hdr) |
9eaff861 | 11432 | { |
491d01d3 | 11433 | esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); |
9eaff861 AO |
11434 | esdo->rela.count += additional_reloc_count; |
11435 | } | |
d4730f92 BS |
11436 | } |
11437 | else | |
11438 | { | |
11439 | if (o->use_rela_p) | |
11440 | esdo->rela.count += reloc_count; | |
2c2b4ed4 | 11441 | else |
d4730f92 | 11442 | esdo->rel.count += reloc_count; |
c152c796 | 11443 | } |
c152c796 AM |
11444 | } |
11445 | ||
9eaff861 | 11446 | if (o->reloc_count > 0) |
c152c796 AM |
11447 | o->flags |= SEC_RELOC; |
11448 | else | |
11449 | { | |
11450 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
11451 | set it (this is probably a bug) and if it is set | |
11452 | assign_section_numbers will create a reloc section. */ | |
11453 | o->flags &=~ SEC_RELOC; | |
11454 | } | |
11455 | ||
11456 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
11457 | zero. This is done in elf_fake_sections as well, but forcing | |
11458 | the VMA to 0 here will ensure that relocs against these | |
11459 | sections are handled correctly. */ | |
11460 | if ((o->flags & SEC_ALLOC) == 0 | |
11461 | && ! o->user_set_vma) | |
11462 | o->vma = 0; | |
11463 | } | |
11464 | ||
0e1862bb | 11465 | if (! bfd_link_relocatable (info) && merged) |
64f52338 | 11466 | elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd); |
c152c796 AM |
11467 | |
11468 | /* Figure out the file positions for everything but the symbol table | |
11469 | and the relocs. We set symcount to force assign_section_numbers | |
11470 | to create a symbol table. */ | |
8539e4e8 | 11471 | bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs; |
c152c796 AM |
11472 | BFD_ASSERT (! abfd->output_has_begun); |
11473 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
11474 | goto error_return; | |
11475 | ||
ee75fd95 | 11476 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
11477 | for (o = abfd->sections; o != NULL; o = o->next) |
11478 | { | |
d4730f92 | 11479 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
c152c796 AM |
11480 | if ((o->flags & SEC_RELOC) != 0) |
11481 | { | |
d4730f92 | 11482 | if (esdo->rel.hdr |
9eaff861 | 11483 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) |
c152c796 AM |
11484 | goto error_return; |
11485 | ||
d4730f92 | 11486 | if (esdo->rela.hdr |
9eaff861 | 11487 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) |
c152c796 AM |
11488 | goto error_return; |
11489 | } | |
11490 | ||
11491 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
11492 | to count upwards while actually outputting the relocations. */ | |
d4730f92 BS |
11493 | esdo->rel.count = 0; |
11494 | esdo->rela.count = 0; | |
0ce398f1 L |
11495 | |
11496 | if (esdo->this_hdr.sh_offset == (file_ptr) -1) | |
11497 | { | |
11498 | /* Cache the section contents so that they can be compressed | |
11499 | later. Use bfd_malloc since it will be freed by | |
11500 | bfd_compress_section_contents. */ | |
11501 | unsigned char *contents = esdo->this_hdr.contents; | |
11502 | if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) | |
11503 | abort (); | |
11504 | contents | |
11505 | = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); | |
11506 | if (contents == NULL) | |
11507 | goto error_return; | |
11508 | esdo->this_hdr.contents = contents; | |
11509 | } | |
c152c796 AM |
11510 | } |
11511 | ||
c152c796 | 11512 | /* We have now assigned file positions for all the sections except |
a485e98e AM |
11513 | .symtab, .strtab, and non-loaded reloc sections. We start the |
11514 | .symtab section at the current file position, and write directly | |
11515 | to it. We build the .strtab section in memory. */ | |
c152c796 AM |
11516 | bfd_get_symcount (abfd) = 0; |
11517 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11518 | /* sh_name is set in prep_headers. */ | |
11519 | symtab_hdr->sh_type = SHT_SYMTAB; | |
11520 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
11521 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
11522 | /* sh_link is set in assign_section_numbers. */ | |
11523 | /* sh_info is set below. */ | |
11524 | /* sh_offset is set just below. */ | |
72de5009 | 11525 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
c152c796 | 11526 | |
ef10c3ac L |
11527 | if (max_sym_count < 20) |
11528 | max_sym_count = 20; | |
64f52338 | 11529 | htab->strtabsize = max_sym_count; |
ef10c3ac | 11530 | amt = max_sym_count * sizeof (struct elf_sym_strtab); |
64f52338 AM |
11531 | htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt); |
11532 | if (htab->strtab == NULL) | |
c152c796 | 11533 | goto error_return; |
ef10c3ac L |
11534 | /* The real buffer will be allocated in elf_link_swap_symbols_out. */ |
11535 | flinfo.symshndxbuf | |
11536 | = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) | |
11537 | ? (Elf_External_Sym_Shndx *) -1 : NULL); | |
c152c796 | 11538 | |
8539e4e8 | 11539 | if (info->strip != strip_all || emit_relocs) |
c152c796 | 11540 | { |
8539e4e8 AM |
11541 | file_ptr off = elf_next_file_pos (abfd); |
11542 | ||
11543 | _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
11544 | ||
11545 | /* Note that at this point elf_next_file_pos (abfd) is | |
11546 | incorrect. We do not yet know the size of the .symtab section. | |
11547 | We correct next_file_pos below, after we do know the size. */ | |
11548 | ||
11549 | /* Start writing out the symbol table. The first symbol is always a | |
11550 | dummy symbol. */ | |
c152c796 AM |
11551 | elfsym.st_value = 0; |
11552 | elfsym.st_size = 0; | |
11553 | elfsym.st_info = 0; | |
11554 | elfsym.st_other = 0; | |
11555 | elfsym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 11556 | elfsym.st_target_internal = 0; |
ef10c3ac L |
11557 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, |
11558 | bfd_und_section_ptr, NULL) != 1) | |
c152c796 | 11559 | goto error_return; |
c152c796 | 11560 | |
8539e4e8 AM |
11561 | /* Output a symbol for each section. We output these even if we are |
11562 | discarding local symbols, since they are used for relocs. These | |
11563 | symbols have no names. We store the index of each one in the | |
11564 | index field of the section, so that we can find it again when | |
11565 | outputting relocs. */ | |
11566 | ||
c152c796 AM |
11567 | elfsym.st_size = 0; |
11568 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
11569 | elfsym.st_other = 0; | |
f0b5bb34 | 11570 | elfsym.st_value = 0; |
35fc36a8 | 11571 | elfsym.st_target_internal = 0; |
c152c796 AM |
11572 | for (i = 1; i < elf_numsections (abfd); i++) |
11573 | { | |
11574 | o = bfd_section_from_elf_index (abfd, i); | |
11575 | if (o != NULL) | |
f0b5bb34 AM |
11576 | { |
11577 | o->target_index = bfd_get_symcount (abfd); | |
11578 | elfsym.st_shndx = i; | |
0e1862bb | 11579 | if (!bfd_link_relocatable (info)) |
f0b5bb34 | 11580 | elfsym.st_value = o->vma; |
ef10c3ac L |
11581 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o, |
11582 | NULL) != 1) | |
f0b5bb34 AM |
11583 | goto error_return; |
11584 | } | |
c152c796 AM |
11585 | } |
11586 | } | |
11587 | ||
11588 | /* Allocate some memory to hold information read in from the input | |
11589 | files. */ | |
11590 | if (max_contents_size != 0) | |
11591 | { | |
8b127cbc AM |
11592 | flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
11593 | if (flinfo.contents == NULL) | |
c152c796 AM |
11594 | goto error_return; |
11595 | } | |
11596 | ||
11597 | if (max_external_reloc_size != 0) | |
11598 | { | |
8b127cbc AM |
11599 | flinfo.external_relocs = bfd_malloc (max_external_reloc_size); |
11600 | if (flinfo.external_relocs == NULL) | |
c152c796 AM |
11601 | goto error_return; |
11602 | } | |
11603 | ||
11604 | if (max_internal_reloc_count != 0) | |
11605 | { | |
11606 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
11607 | amt *= sizeof (Elf_Internal_Rela); | |
8b127cbc AM |
11608 | flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); |
11609 | if (flinfo.internal_relocs == NULL) | |
c152c796 AM |
11610 | goto error_return; |
11611 | } | |
11612 | ||
11613 | if (max_sym_count != 0) | |
11614 | { | |
11615 | amt = max_sym_count * bed->s->sizeof_sym; | |
8b127cbc AM |
11616 | flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); |
11617 | if (flinfo.external_syms == NULL) | |
c152c796 AM |
11618 | goto error_return; |
11619 | ||
11620 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8b127cbc AM |
11621 | flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); |
11622 | if (flinfo.internal_syms == NULL) | |
c152c796 AM |
11623 | goto error_return; |
11624 | ||
11625 | amt = max_sym_count * sizeof (long); | |
8b127cbc AM |
11626 | flinfo.indices = (long int *) bfd_malloc (amt); |
11627 | if (flinfo.indices == NULL) | |
c152c796 AM |
11628 | goto error_return; |
11629 | ||
11630 | amt = max_sym_count * sizeof (asection *); | |
8b127cbc AM |
11631 | flinfo.sections = (asection **) bfd_malloc (amt); |
11632 | if (flinfo.sections == NULL) | |
c152c796 AM |
11633 | goto error_return; |
11634 | } | |
11635 | ||
11636 | if (max_sym_shndx_count != 0) | |
11637 | { | |
11638 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8b127cbc AM |
11639 | flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
11640 | if (flinfo.locsym_shndx == NULL) | |
c152c796 AM |
11641 | goto error_return; |
11642 | } | |
11643 | ||
64f52338 | 11644 | if (htab->tls_sec) |
c152c796 AM |
11645 | { |
11646 | bfd_vma base, end = 0; | |
11647 | asection *sec; | |
11648 | ||
64f52338 | 11649 | for (sec = htab->tls_sec; |
c152c796 AM |
11650 | sec && (sec->flags & SEC_THREAD_LOCAL); |
11651 | sec = sec->next) | |
11652 | { | |
3a800eb9 | 11653 | bfd_size_type size = sec->size; |
c152c796 | 11654 | |
3a800eb9 AM |
11655 | if (size == 0 |
11656 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 11657 | { |
91d6fa6a NC |
11658 | struct bfd_link_order *ord = sec->map_tail.link_order; |
11659 | ||
11660 | if (ord != NULL) | |
11661 | size = ord->offset + ord->size; | |
c152c796 AM |
11662 | } |
11663 | end = sec->vma + size; | |
11664 | } | |
64f52338 | 11665 | base = htab->tls_sec->vma; |
7dc98aea RO |
11666 | /* Only align end of TLS section if static TLS doesn't have special |
11667 | alignment requirements. */ | |
11668 | if (bed->static_tls_alignment == 1) | |
64f52338 AM |
11669 | end = align_power (end, htab->tls_sec->alignment_power); |
11670 | htab->tls_size = end - base; | |
c152c796 AM |
11671 | } |
11672 | ||
0b52efa6 PB |
11673 | /* Reorder SHF_LINK_ORDER sections. */ |
11674 | for (o = abfd->sections; o != NULL; o = o->next) | |
11675 | { | |
11676 | if (!elf_fixup_link_order (abfd, o)) | |
11677 | return FALSE; | |
11678 | } | |
11679 | ||
2f0c68f2 CM |
11680 | if (!_bfd_elf_fixup_eh_frame_hdr (info)) |
11681 | return FALSE; | |
11682 | ||
c152c796 AM |
11683 | /* Since ELF permits relocations to be against local symbols, we |
11684 | must have the local symbols available when we do the relocations. | |
11685 | Since we would rather only read the local symbols once, and we | |
11686 | would rather not keep them in memory, we handle all the | |
11687 | relocations for a single input file at the same time. | |
11688 | ||
11689 | Unfortunately, there is no way to know the total number of local | |
11690 | symbols until we have seen all of them, and the local symbol | |
11691 | indices precede the global symbol indices. This means that when | |
11692 | we are generating relocatable output, and we see a reloc against | |
11693 | a global symbol, we can not know the symbol index until we have | |
11694 | finished examining all the local symbols to see which ones we are | |
11695 | going to output. To deal with this, we keep the relocations in | |
11696 | memory, and don't output them until the end of the link. This is | |
11697 | an unfortunate waste of memory, but I don't see a good way around | |
11698 | it. Fortunately, it only happens when performing a relocatable | |
11699 | link, which is not the common case. FIXME: If keep_memory is set | |
11700 | we could write the relocs out and then read them again; I don't | |
11701 | know how bad the memory loss will be. */ | |
11702 | ||
c72f2fb2 | 11703 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
11704 | sub->output_has_begun = FALSE; |
11705 | for (o = abfd->sections; o != NULL; o = o->next) | |
11706 | { | |
8423293d | 11707 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
11708 | { |
11709 | if (p->type == bfd_indirect_link_order | |
11710 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
11711 | == bfd_target_elf_flavour) | |
11712 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
11713 | { | |
11714 | if (! sub->output_has_begun) | |
11715 | { | |
8b127cbc | 11716 | if (! elf_link_input_bfd (&flinfo, sub)) |
c152c796 AM |
11717 | goto error_return; |
11718 | sub->output_has_begun = TRUE; | |
11719 | } | |
11720 | } | |
11721 | else if (p->type == bfd_section_reloc_link_order | |
11722 | || p->type == bfd_symbol_reloc_link_order) | |
11723 | { | |
11724 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
11725 | goto error_return; | |
11726 | } | |
11727 | else | |
11728 | { | |
11729 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
351f65ca L |
11730 | { |
11731 | if (p->type == bfd_indirect_link_order | |
11732 | && (bfd_get_flavour (sub) | |
11733 | == bfd_target_elf_flavour) | |
11734 | && (elf_elfheader (sub)->e_ident[EI_CLASS] | |
11735 | != bed->s->elfclass)) | |
11736 | { | |
11737 | const char *iclass, *oclass; | |
11738 | ||
aebf9be7 | 11739 | switch (bed->s->elfclass) |
351f65ca | 11740 | { |
aebf9be7 NC |
11741 | case ELFCLASS64: oclass = "ELFCLASS64"; break; |
11742 | case ELFCLASS32: oclass = "ELFCLASS32"; break; | |
11743 | case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; | |
11744 | default: abort (); | |
351f65ca | 11745 | } |
aebf9be7 NC |
11746 | |
11747 | switch (elf_elfheader (sub)->e_ident[EI_CLASS]) | |
351f65ca | 11748 | { |
aebf9be7 NC |
11749 | case ELFCLASS64: iclass = "ELFCLASS64"; break; |
11750 | case ELFCLASS32: iclass = "ELFCLASS32"; break; | |
11751 | case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; | |
11752 | default: abort (); | |
351f65ca L |
11753 | } |
11754 | ||
11755 | bfd_set_error (bfd_error_wrong_format); | |
4eca0228 | 11756 | _bfd_error_handler |
695344c0 | 11757 | /* xgettext:c-format */ |
351f65ca L |
11758 | (_("%B: file class %s incompatible with %s"), |
11759 | sub, iclass, oclass); | |
11760 | } | |
11761 | ||
11762 | goto error_return; | |
11763 | } | |
c152c796 AM |
11764 | } |
11765 | } | |
11766 | } | |
11767 | ||
c0f00686 L |
11768 | /* Free symbol buffer if needed. */ |
11769 | if (!info->reduce_memory_overheads) | |
11770 | { | |
c72f2fb2 | 11771 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
3fcd97f1 JJ |
11772 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
11773 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
11774 | { |
11775 | free (elf_tdata (sub)->symbuf); | |
11776 | elf_tdata (sub)->symbuf = NULL; | |
11777 | } | |
11778 | } | |
11779 | ||
c152c796 AM |
11780 | /* Output any global symbols that got converted to local in a |
11781 | version script or due to symbol visibility. We do this in a | |
11782 | separate step since ELF requires all local symbols to appear | |
11783 | prior to any global symbols. FIXME: We should only do this if | |
11784 | some global symbols were, in fact, converted to become local. | |
11785 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
11786 | eoinfo.failed = FALSE; | |
8b127cbc | 11787 | eoinfo.flinfo = &flinfo; |
c152c796 | 11788 | eoinfo.localsyms = TRUE; |
34a79995 | 11789 | eoinfo.file_sym_done = FALSE; |
7686d77d | 11790 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
11791 | if (eoinfo.failed) |
11792 | return FALSE; | |
11793 | ||
4e617b1e PB |
11794 | /* If backend needs to output some local symbols not present in the hash |
11795 | table, do it now. */ | |
8539e4e8 AM |
11796 | if (bed->elf_backend_output_arch_local_syms |
11797 | && (info->strip != strip_all || emit_relocs)) | |
4e617b1e | 11798 | { |
6e0b88f1 | 11799 | typedef int (*out_sym_func) |
4e617b1e PB |
11800 | (void *, const char *, Elf_Internal_Sym *, asection *, |
11801 | struct elf_link_hash_entry *); | |
11802 | ||
11803 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
ef10c3ac L |
11804 | (abfd, info, &flinfo, |
11805 | (out_sym_func) elf_link_output_symstrtab))) | |
4e617b1e PB |
11806 | return FALSE; |
11807 | } | |
11808 | ||
c152c796 AM |
11809 | /* That wrote out all the local symbols. Finish up the symbol table |
11810 | with the global symbols. Even if we want to strip everything we | |
11811 | can, we still need to deal with those global symbols that got | |
11812 | converted to local in a version script. */ | |
11813 | ||
11814 | /* The sh_info field records the index of the first non local symbol. */ | |
11815 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
11816 | ||
11817 | if (dynamic | |
64f52338 AM |
11818 | && htab->dynsym != NULL |
11819 | && htab->dynsym->output_section != bfd_abs_section_ptr) | |
c152c796 AM |
11820 | { |
11821 | Elf_Internal_Sym sym; | |
64f52338 | 11822 | bfd_byte *dynsym = htab->dynsym->contents; |
90ac2420 | 11823 | |
64f52338 AM |
11824 | o = htab->dynsym->output_section; |
11825 | elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1; | |
c152c796 AM |
11826 | |
11827 | /* Write out the section symbols for the output sections. */ | |
0e1862bb | 11828 | if (bfd_link_pic (info) |
64f52338 | 11829 | || htab->is_relocatable_executable) |
c152c796 AM |
11830 | { |
11831 | asection *s; | |
11832 | ||
11833 | sym.st_size = 0; | |
11834 | sym.st_name = 0; | |
11835 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
11836 | sym.st_other = 0; | |
35fc36a8 | 11837 | sym.st_target_internal = 0; |
c152c796 AM |
11838 | |
11839 | for (s = abfd->sections; s != NULL; s = s->next) | |
11840 | { | |
11841 | int indx; | |
11842 | bfd_byte *dest; | |
11843 | long dynindx; | |
11844 | ||
c152c796 | 11845 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
11846 | if (dynindx <= 0) |
11847 | continue; | |
11848 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
11849 | BFD_ASSERT (indx > 0); |
11850 | sym.st_shndx = indx; | |
c0d5a53d L |
11851 | if (! check_dynsym (abfd, &sym)) |
11852 | return FALSE; | |
c152c796 AM |
11853 | sym.st_value = s->vma; |
11854 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
11855 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
11856 | } | |
c152c796 AM |
11857 | } |
11858 | ||
11859 | /* Write out the local dynsyms. */ | |
64f52338 | 11860 | if (htab->dynlocal) |
c152c796 AM |
11861 | { |
11862 | struct elf_link_local_dynamic_entry *e; | |
64f52338 | 11863 | for (e = htab->dynlocal; e ; e = e->next) |
c152c796 AM |
11864 | { |
11865 | asection *s; | |
11866 | bfd_byte *dest; | |
11867 | ||
935bd1e0 | 11868 | /* Copy the internal symbol and turn off visibility. |
c152c796 AM |
11869 | Note that we saved a word of storage and overwrote |
11870 | the original st_name with the dynstr_index. */ | |
11871 | sym = e->isym; | |
935bd1e0 | 11872 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); |
c152c796 | 11873 | |
cb33740c AM |
11874 | s = bfd_section_from_elf_index (e->input_bfd, |
11875 | e->isym.st_shndx); | |
11876 | if (s != NULL) | |
c152c796 | 11877 | { |
c152c796 AM |
11878 | sym.st_shndx = |
11879 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
11880 | if (! check_dynsym (abfd, &sym)) |
11881 | return FALSE; | |
c152c796 AM |
11882 | sym.st_value = (s->output_section->vma |
11883 | + s->output_offset | |
11884 | + e->isym.st_value); | |
11885 | } | |
11886 | ||
c152c796 AM |
11887 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; |
11888 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
11889 | } | |
11890 | } | |
c152c796 AM |
11891 | } |
11892 | ||
11893 | /* We get the global symbols from the hash table. */ | |
11894 | eoinfo.failed = FALSE; | |
11895 | eoinfo.localsyms = FALSE; | |
8b127cbc | 11896 | eoinfo.flinfo = &flinfo; |
7686d77d | 11897 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
11898 | if (eoinfo.failed) |
11899 | return FALSE; | |
11900 | ||
11901 | /* If backend needs to output some symbols not present in the hash | |
11902 | table, do it now. */ | |
8539e4e8 AM |
11903 | if (bed->elf_backend_output_arch_syms |
11904 | && (info->strip != strip_all || emit_relocs)) | |
c152c796 | 11905 | { |
6e0b88f1 | 11906 | typedef int (*out_sym_func) |
c152c796 AM |
11907 | (void *, const char *, Elf_Internal_Sym *, asection *, |
11908 | struct elf_link_hash_entry *); | |
11909 | ||
11910 | if (! ((*bed->elf_backend_output_arch_syms) | |
ef10c3ac L |
11911 | (abfd, info, &flinfo, |
11912 | (out_sym_func) elf_link_output_symstrtab))) | |
c152c796 AM |
11913 | return FALSE; |
11914 | } | |
11915 | ||
ef10c3ac L |
11916 | /* Finalize the .strtab section. */ |
11917 | _bfd_elf_strtab_finalize (flinfo.symstrtab); | |
11918 | ||
11919 | /* Swap out the .strtab section. */ | |
11920 | if (!elf_link_swap_symbols_out (&flinfo)) | |
c152c796 AM |
11921 | return FALSE; |
11922 | ||
11923 | /* Now we know the size of the symtab section. */ | |
c152c796 AM |
11924 | if (bfd_get_symcount (abfd) > 0) |
11925 | { | |
ee3b52e9 L |
11926 | /* Finish up and write out the symbol string table (.strtab) |
11927 | section. */ | |
11928 | Elf_Internal_Shdr *symstrtab_hdr; | |
8539e4e8 AM |
11929 | file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; |
11930 | ||
6a40cf0c NC |
11931 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
11932 | if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) | |
8539e4e8 AM |
11933 | { |
11934 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
11935 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
11936 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
11937 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
11938 | symtab_shndx_hdr->sh_size = amt; | |
11939 | ||
11940 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
11941 | off, TRUE); | |
11942 | ||
11943 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
11944 | || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) | |
11945 | return FALSE; | |
11946 | } | |
ee3b52e9 L |
11947 | |
11948 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
11949 | /* sh_name was set in prep_headers. */ | |
11950 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 11951 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
ee3b52e9 | 11952 | symstrtab_hdr->sh_addr = 0; |
ef10c3ac | 11953 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); |
ee3b52e9 L |
11954 | symstrtab_hdr->sh_entsize = 0; |
11955 | symstrtab_hdr->sh_link = 0; | |
11956 | symstrtab_hdr->sh_info = 0; | |
11957 | /* sh_offset is set just below. */ | |
11958 | symstrtab_hdr->sh_addralign = 1; | |
11959 | ||
11960 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, | |
11961 | off, TRUE); | |
11962 | elf_next_file_pos (abfd) = off; | |
11963 | ||
c152c796 | 11964 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 11965 | || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) |
c152c796 AM |
11966 | return FALSE; |
11967 | } | |
11968 | ||
76359541 TP |
11969 | if (info->out_implib_bfd && !elf_output_implib (abfd, info)) |
11970 | { | |
4eca0228 AM |
11971 | _bfd_error_handler (_("%B: failed to generate import library"), |
11972 | info->out_implib_bfd); | |
76359541 TP |
11973 | return FALSE; |
11974 | } | |
11975 | ||
c152c796 AM |
11976 | /* Adjust the relocs to have the correct symbol indices. */ |
11977 | for (o = abfd->sections; o != NULL; o = o->next) | |
11978 | { | |
d4730f92 | 11979 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
28dbcedc | 11980 | bfd_boolean sort; |
c152c796 AM |
11981 | if ((o->flags & SEC_RELOC) == 0) |
11982 | continue; | |
11983 | ||
28dbcedc | 11984 | sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); |
bca6d0e3 | 11985 | if (esdo->rel.hdr != NULL |
9eaff861 | 11986 | && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort)) |
bca6d0e3 AM |
11987 | return FALSE; |
11988 | if (esdo->rela.hdr != NULL | |
9eaff861 | 11989 | && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort)) |
bca6d0e3 | 11990 | return FALSE; |
c152c796 AM |
11991 | |
11992 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
11993 | trying to swap the relocs out itself. */ | |
11994 | o->reloc_count = 0; | |
11995 | } | |
11996 | ||
11997 | if (dynamic && info->combreloc && dynobj != NULL) | |
11998 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
11999 | ||
12000 | /* If we are linking against a dynamic object, or generating a | |
12001 | shared library, finish up the dynamic linking information. */ | |
12002 | if (dynamic) | |
12003 | { | |
12004 | bfd_byte *dyncon, *dynconend; | |
12005 | ||
12006 | /* Fix up .dynamic entries. */ | |
3d4d4302 | 12007 | o = bfd_get_linker_section (dynobj, ".dynamic"); |
c152c796 AM |
12008 | BFD_ASSERT (o != NULL); |
12009 | ||
12010 | dyncon = o->contents; | |
eea6121a | 12011 | dynconend = o->contents + o->size; |
c152c796 AM |
12012 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
12013 | { | |
12014 | Elf_Internal_Dyn dyn; | |
12015 | const char *name; | |
12016 | unsigned int type; | |
12017 | ||
12018 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12019 | ||
12020 | switch (dyn.d_tag) | |
12021 | { | |
12022 | default: | |
12023 | continue; | |
12024 | case DT_NULL: | |
12025 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
12026 | { | |
12027 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
12028 | { | |
12029 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
12030 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
12031 | default: continue; | |
12032 | } | |
12033 | dyn.d_un.d_val = relativecount; | |
12034 | relativecount = 0; | |
12035 | break; | |
12036 | } | |
12037 | continue; | |
12038 | ||
12039 | case DT_INIT: | |
12040 | name = info->init_function; | |
12041 | goto get_sym; | |
12042 | case DT_FINI: | |
12043 | name = info->fini_function; | |
12044 | get_sym: | |
12045 | { | |
12046 | struct elf_link_hash_entry *h; | |
12047 | ||
64f52338 | 12048 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
c152c796 AM |
12049 | if (h != NULL |
12050 | && (h->root.type == bfd_link_hash_defined | |
12051 | || h->root.type == bfd_link_hash_defweak)) | |
12052 | { | |
bef26483 | 12053 | dyn.d_un.d_ptr = h->root.u.def.value; |
c152c796 AM |
12054 | o = h->root.u.def.section; |
12055 | if (o->output_section != NULL) | |
bef26483 | 12056 | dyn.d_un.d_ptr += (o->output_section->vma |
c152c796 AM |
12057 | + o->output_offset); |
12058 | else | |
12059 | { | |
12060 | /* The symbol is imported from another shared | |
12061 | library and does not apply to this one. */ | |
bef26483 | 12062 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
12063 | } |
12064 | break; | |
12065 | } | |
12066 | } | |
12067 | continue; | |
12068 | ||
12069 | case DT_PREINIT_ARRAYSZ: | |
12070 | name = ".preinit_array"; | |
4ade44b7 | 12071 | goto get_out_size; |
c152c796 AM |
12072 | case DT_INIT_ARRAYSZ: |
12073 | name = ".init_array"; | |
4ade44b7 | 12074 | goto get_out_size; |
c152c796 AM |
12075 | case DT_FINI_ARRAYSZ: |
12076 | name = ".fini_array"; | |
4ade44b7 | 12077 | get_out_size: |
c152c796 AM |
12078 | o = bfd_get_section_by_name (abfd, name); |
12079 | if (o == NULL) | |
12080 | { | |
4eca0228 | 12081 | _bfd_error_handler |
4ade44b7 | 12082 | (_("could not find section %s"), name); |
c152c796 AM |
12083 | goto error_return; |
12084 | } | |
eea6121a | 12085 | if (o->size == 0) |
4eca0228 | 12086 | _bfd_error_handler |
c152c796 | 12087 | (_("warning: %s section has zero size"), name); |
eea6121a | 12088 | dyn.d_un.d_val = o->size; |
c152c796 AM |
12089 | break; |
12090 | ||
12091 | case DT_PREINIT_ARRAY: | |
12092 | name = ".preinit_array"; | |
4ade44b7 | 12093 | goto get_out_vma; |
c152c796 AM |
12094 | case DT_INIT_ARRAY: |
12095 | name = ".init_array"; | |
4ade44b7 | 12096 | goto get_out_vma; |
c152c796 AM |
12097 | case DT_FINI_ARRAY: |
12098 | name = ".fini_array"; | |
4ade44b7 AM |
12099 | get_out_vma: |
12100 | o = bfd_get_section_by_name (abfd, name); | |
12101 | goto do_vma; | |
c152c796 AM |
12102 | |
12103 | case DT_HASH: | |
12104 | name = ".hash"; | |
12105 | goto get_vma; | |
fdc90cb4 JJ |
12106 | case DT_GNU_HASH: |
12107 | name = ".gnu.hash"; | |
12108 | goto get_vma; | |
c152c796 AM |
12109 | case DT_STRTAB: |
12110 | name = ".dynstr"; | |
12111 | goto get_vma; | |
12112 | case DT_SYMTAB: | |
12113 | name = ".dynsym"; | |
12114 | goto get_vma; | |
12115 | case DT_VERDEF: | |
12116 | name = ".gnu.version_d"; | |
12117 | goto get_vma; | |
12118 | case DT_VERNEED: | |
12119 | name = ".gnu.version_r"; | |
12120 | goto get_vma; | |
12121 | case DT_VERSYM: | |
12122 | name = ".gnu.version"; | |
12123 | get_vma: | |
4ade44b7 AM |
12124 | o = bfd_get_linker_section (dynobj, name); |
12125 | do_vma: | |
c152c796 AM |
12126 | if (o == NULL) |
12127 | { | |
4eca0228 | 12128 | _bfd_error_handler |
4ade44b7 | 12129 | (_("could not find section %s"), name); |
c152c796 AM |
12130 | goto error_return; |
12131 | } | |
894891db NC |
12132 | if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) |
12133 | { | |
4eca0228 | 12134 | _bfd_error_handler |
894891db NC |
12135 | (_("warning: section '%s' is being made into a note"), name); |
12136 | bfd_set_error (bfd_error_nonrepresentable_section); | |
12137 | goto error_return; | |
12138 | } | |
4ade44b7 | 12139 | dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; |
c152c796 AM |
12140 | break; |
12141 | ||
12142 | case DT_REL: | |
12143 | case DT_RELA: | |
12144 | case DT_RELSZ: | |
12145 | case DT_RELASZ: | |
12146 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
12147 | type = SHT_REL; | |
12148 | else | |
12149 | type = SHT_RELA; | |
12150 | dyn.d_un.d_val = 0; | |
bef26483 | 12151 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
12152 | for (i = 1; i < elf_numsections (abfd); i++) |
12153 | { | |
12154 | Elf_Internal_Shdr *hdr; | |
12155 | ||
12156 | hdr = elf_elfsections (abfd)[i]; | |
12157 | if (hdr->sh_type == type | |
12158 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
12159 | { | |
12160 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
12161 | dyn.d_un.d_val += hdr->sh_size; | |
12162 | else | |
12163 | { | |
bef26483 AM |
12164 | if (dyn.d_un.d_ptr == 0 |
12165 | || hdr->sh_addr < dyn.d_un.d_ptr) | |
12166 | dyn.d_un.d_ptr = hdr->sh_addr; | |
c152c796 AM |
12167 | } |
12168 | } | |
12169 | } | |
64f52338 AM |
12170 | if (bed->dtrel_excludes_plt && htab->srelplt != NULL) |
12171 | { | |
12172 | /* Don't count procedure linkage table relocs in the | |
12173 | overall reloc count. */ | |
12174 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
12175 | dyn.d_un.d_val -= htab->srelplt->size; | |
12176 | /* If .rela.plt is the first .rela section, exclude | |
12177 | it from DT_RELA. */ | |
12178 | else if (dyn.d_un.d_ptr == (htab->srelplt->output_section->vma | |
12179 | + htab->srelplt->output_offset)) | |
12180 | dyn.d_un.d_ptr += htab->srelplt->size; | |
12181 | } | |
c152c796 AM |
12182 | break; |
12183 | } | |
12184 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
12185 | } | |
12186 | } | |
12187 | ||
12188 | /* If we have created any dynamic sections, then output them. */ | |
12189 | if (dynobj != NULL) | |
12190 | { | |
12191 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
12192 | goto error_return; | |
12193 | ||
943284cc | 12194 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
0e1862bb | 12195 | if (((info->warn_shared_textrel && bfd_link_pic (info)) |
be7b303d | 12196 | || info->error_textrel) |
3d4d4302 | 12197 | && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) |
943284cc DJ |
12198 | { |
12199 | bfd_byte *dyncon, *dynconend; | |
12200 | ||
943284cc DJ |
12201 | dyncon = o->contents; |
12202 | dynconend = o->contents + o->size; | |
12203 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
12204 | { | |
12205 | Elf_Internal_Dyn dyn; | |
12206 | ||
12207 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12208 | ||
12209 | if (dyn.d_tag == DT_TEXTREL) | |
12210 | { | |
c192a133 AM |
12211 | if (info->error_textrel) |
12212 | info->callbacks->einfo | |
12213 | (_("%P%X: read-only segment has dynamic relocations.\n")); | |
12214 | else | |
12215 | info->callbacks->einfo | |
12216 | (_("%P: warning: creating a DT_TEXTREL in a shared object.\n")); | |
943284cc DJ |
12217 | break; |
12218 | } | |
12219 | } | |
12220 | } | |
12221 | ||
c152c796 AM |
12222 | for (o = dynobj->sections; o != NULL; o = o->next) |
12223 | { | |
12224 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 12225 | || o->size == 0 |
c152c796 AM |
12226 | || o->output_section == bfd_abs_section_ptr) |
12227 | continue; | |
12228 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
12229 | { | |
12230 | /* At this point, we are only interested in sections | |
12231 | created by _bfd_elf_link_create_dynamic_sections. */ | |
12232 | continue; | |
12233 | } | |
64f52338 | 12234 | if (htab->stab_info.stabstr == o) |
3722b82f | 12235 | continue; |
64f52338 | 12236 | if (htab->eh_info.hdr_sec == o) |
eea6121a | 12237 | continue; |
3d4d4302 | 12238 | if (strcmp (o->name, ".dynstr") != 0) |
c152c796 AM |
12239 | { |
12240 | if (! bfd_set_section_contents (abfd, o->output_section, | |
12241 | o->contents, | |
37b01f6a DG |
12242 | (file_ptr) o->output_offset |
12243 | * bfd_octets_per_byte (abfd), | |
eea6121a | 12244 | o->size)) |
c152c796 AM |
12245 | goto error_return; |
12246 | } | |
12247 | else | |
12248 | { | |
12249 | /* The contents of the .dynstr section are actually in a | |
12250 | stringtab. */ | |
8539e4e8 AM |
12251 | file_ptr off; |
12252 | ||
c152c796 AM |
12253 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; |
12254 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
64f52338 | 12255 | || !_bfd_elf_strtab_emit (abfd, htab->dynstr)) |
c152c796 AM |
12256 | goto error_return; |
12257 | } | |
12258 | } | |
12259 | } | |
12260 | ||
0e1862bb | 12261 | if (bfd_link_relocatable (info)) |
c152c796 AM |
12262 | { |
12263 | bfd_boolean failed = FALSE; | |
12264 | ||
12265 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
12266 | if (failed) | |
12267 | goto error_return; | |
12268 | } | |
12269 | ||
12270 | /* If we have optimized stabs strings, output them. */ | |
64f52338 | 12271 | if (htab->stab_info.stabstr != NULL) |
c152c796 | 12272 | { |
64f52338 | 12273 | if (!_bfd_write_stab_strings (abfd, &htab->stab_info)) |
c152c796 AM |
12274 | goto error_return; |
12275 | } | |
12276 | ||
9f7c3e5e AM |
12277 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) |
12278 | goto error_return; | |
c152c796 | 12279 | |
9f7c3e5e | 12280 | elf_final_link_free (abfd, &flinfo); |
c152c796 | 12281 | |
12bd6957 | 12282 | elf_linker (abfd) = TRUE; |
c152c796 | 12283 | |
104d59d1 JM |
12284 | if (attr_section) |
12285 | { | |
a50b1753 | 12286 | bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); |
104d59d1 | 12287 | if (contents == NULL) |
d0f16d5e | 12288 | return FALSE; /* Bail out and fail. */ |
104d59d1 JM |
12289 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
12290 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
12291 | free (contents); | |
12292 | } | |
12293 | ||
c152c796 AM |
12294 | return TRUE; |
12295 | ||
12296 | error_return: | |
9f7c3e5e | 12297 | elf_final_link_free (abfd, &flinfo); |
c152c796 AM |
12298 | return FALSE; |
12299 | } | |
12300 | \f | |
5241d853 RS |
12301 | /* Initialize COOKIE for input bfd ABFD. */ |
12302 | ||
12303 | static bfd_boolean | |
12304 | init_reloc_cookie (struct elf_reloc_cookie *cookie, | |
12305 | struct bfd_link_info *info, bfd *abfd) | |
12306 | { | |
12307 | Elf_Internal_Shdr *symtab_hdr; | |
12308 | const struct elf_backend_data *bed; | |
12309 | ||
12310 | bed = get_elf_backend_data (abfd); | |
12311 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12312 | ||
12313 | cookie->abfd = abfd; | |
12314 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
12315 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
12316 | if (cookie->bad_symtab) | |
12317 | { | |
12318 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
12319 | cookie->extsymoff = 0; | |
12320 | } | |
12321 | else | |
12322 | { | |
12323 | cookie->locsymcount = symtab_hdr->sh_info; | |
12324 | cookie->extsymoff = symtab_hdr->sh_info; | |
12325 | } | |
12326 | ||
12327 | if (bed->s->arch_size == 32) | |
12328 | cookie->r_sym_shift = 8; | |
12329 | else | |
12330 | cookie->r_sym_shift = 32; | |
12331 | ||
12332 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
12333 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
12334 | { | |
12335 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
12336 | cookie->locsymcount, 0, | |
12337 | NULL, NULL, NULL); | |
12338 | if (cookie->locsyms == NULL) | |
12339 | { | |
12340 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
12341 | return FALSE; | |
12342 | } | |
12343 | if (info->keep_memory) | |
12344 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
12345 | } | |
12346 | return TRUE; | |
12347 | } | |
12348 | ||
12349 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
12350 | ||
12351 | static void | |
12352 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
12353 | { | |
12354 | Elf_Internal_Shdr *symtab_hdr; | |
12355 | ||
12356 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12357 | if (cookie->locsyms != NULL | |
12358 | && symtab_hdr->contents != (unsigned char *) cookie->locsyms) | |
12359 | free (cookie->locsyms); | |
12360 | } | |
12361 | ||
12362 | /* Initialize the relocation information in COOKIE for input section SEC | |
12363 | of input bfd ABFD. */ | |
12364 | ||
12365 | static bfd_boolean | |
12366 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12367 | struct bfd_link_info *info, bfd *abfd, | |
12368 | asection *sec) | |
12369 | { | |
12370 | const struct elf_backend_data *bed; | |
12371 | ||
12372 | if (sec->reloc_count == 0) | |
12373 | { | |
12374 | cookie->rels = NULL; | |
12375 | cookie->relend = NULL; | |
12376 | } | |
12377 | else | |
12378 | { | |
12379 | bed = get_elf_backend_data (abfd); | |
12380 | ||
12381 | cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
12382 | info->keep_memory); | |
12383 | if (cookie->rels == NULL) | |
12384 | return FALSE; | |
12385 | cookie->rel = cookie->rels; | |
12386 | cookie->relend = (cookie->rels | |
12387 | + sec->reloc_count * bed->s->int_rels_per_ext_rel); | |
12388 | } | |
12389 | cookie->rel = cookie->rels; | |
12390 | return TRUE; | |
12391 | } | |
12392 | ||
12393 | /* Free the memory allocated by init_reloc_cookie_rels, | |
12394 | if appropriate. */ | |
12395 | ||
12396 | static void | |
12397 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12398 | asection *sec) | |
12399 | { | |
12400 | if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) | |
12401 | free (cookie->rels); | |
12402 | } | |
12403 | ||
12404 | /* Initialize the whole of COOKIE for input section SEC. */ | |
12405 | ||
12406 | static bfd_boolean | |
12407 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12408 | struct bfd_link_info *info, | |
12409 | asection *sec) | |
12410 | { | |
12411 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
12412 | goto error1; | |
12413 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
12414 | goto error2; | |
12415 | return TRUE; | |
12416 | ||
12417 | error2: | |
12418 | fini_reloc_cookie (cookie, sec->owner); | |
12419 | error1: | |
12420 | return FALSE; | |
12421 | } | |
12422 | ||
12423 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
12424 | if appropriate. */ | |
12425 | ||
12426 | static void | |
12427 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12428 | asection *sec) | |
12429 | { | |
12430 | fini_reloc_cookie_rels (cookie, sec); | |
12431 | fini_reloc_cookie (cookie, sec->owner); | |
12432 | } | |
12433 | \f | |
c152c796 AM |
12434 | /* Garbage collect unused sections. */ |
12435 | ||
07adf181 AM |
12436 | /* Default gc_mark_hook. */ |
12437 | ||
12438 | asection * | |
12439 | _bfd_elf_gc_mark_hook (asection *sec, | |
12440 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12441 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
12442 | struct elf_link_hash_entry *h, | |
12443 | Elf_Internal_Sym *sym) | |
12444 | { | |
12445 | if (h != NULL) | |
12446 | { | |
12447 | switch (h->root.type) | |
12448 | { | |
12449 | case bfd_link_hash_defined: | |
12450 | case bfd_link_hash_defweak: | |
12451 | return h->root.u.def.section; | |
12452 | ||
12453 | case bfd_link_hash_common: | |
12454 | return h->root.u.c.p->section; | |
12455 | ||
12456 | default: | |
12457 | break; | |
12458 | } | |
12459 | } | |
12460 | else | |
12461 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
12462 | ||
12463 | return NULL; | |
12464 | } | |
12465 | ||
a6a4679f AM |
12466 | /* For undefined __start_<name> and __stop_<name> symbols, return the |
12467 | first input section matching <name>. Return NULL otherwise. */ | |
12468 | ||
12469 | asection * | |
12470 | _bfd_elf_is_start_stop (const struct bfd_link_info *info, | |
12471 | struct elf_link_hash_entry *h) | |
12472 | { | |
12473 | asection *s; | |
12474 | const char *sec_name; | |
12475 | ||
12476 | if (h->root.type != bfd_link_hash_undefined | |
12477 | && h->root.type != bfd_link_hash_undefweak) | |
12478 | return NULL; | |
12479 | ||
12480 | s = h->root.u.undef.section; | |
12481 | if (s != NULL) | |
12482 | { | |
12483 | if (s == (asection *) 0 - 1) | |
12484 | return NULL; | |
12485 | return s; | |
12486 | } | |
12487 | ||
12488 | sec_name = NULL; | |
12489 | if (strncmp (h->root.root.string, "__start_", 8) == 0) | |
12490 | sec_name = h->root.root.string + 8; | |
12491 | else if (strncmp (h->root.root.string, "__stop_", 7) == 0) | |
12492 | sec_name = h->root.root.string + 7; | |
12493 | ||
12494 | if (sec_name != NULL && *sec_name != '\0') | |
12495 | { | |
12496 | bfd *i; | |
12497 | ||
12498 | for (i = info->input_bfds; i != NULL; i = i->link.next) | |
12499 | { | |
12500 | s = bfd_get_section_by_name (i, sec_name); | |
12501 | if (s != NULL) | |
12502 | { | |
12503 | h->root.u.undef.section = s; | |
12504 | break; | |
12505 | } | |
12506 | } | |
12507 | } | |
12508 | ||
12509 | if (s == NULL) | |
12510 | h->root.u.undef.section = (asection *) 0 - 1; | |
12511 | ||
12512 | return s; | |
12513 | } | |
12514 | ||
5241d853 RS |
12515 | /* COOKIE->rel describes a relocation against section SEC, which is |
12516 | a section we've decided to keep. Return the section that contains | |
12517 | the relocation symbol, or NULL if no section contains it. */ | |
12518 | ||
12519 | asection * | |
12520 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
12521 | elf_gc_mark_hook_fn gc_mark_hook, | |
1cce69b9 AM |
12522 | struct elf_reloc_cookie *cookie, |
12523 | bfd_boolean *start_stop) | |
5241d853 RS |
12524 | { |
12525 | unsigned long r_symndx; | |
12526 | struct elf_link_hash_entry *h; | |
12527 | ||
12528 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
cf35638d | 12529 | if (r_symndx == STN_UNDEF) |
5241d853 RS |
12530 | return NULL; |
12531 | ||
12532 | if (r_symndx >= cookie->locsymcount | |
12533 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
12534 | { | |
12535 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
263ddf68 L |
12536 | if (h == NULL) |
12537 | { | |
12538 | info->callbacks->einfo (_("%F%P: corrupt input: %B\n"), | |
12539 | sec->owner); | |
12540 | return NULL; | |
12541 | } | |
5241d853 RS |
12542 | while (h->root.type == bfd_link_hash_indirect |
12543 | || h->root.type == bfd_link_hash_warning) | |
12544 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1d5316ab | 12545 | h->mark = 1; |
4e6b54a6 AM |
12546 | /* If this symbol is weak and there is a non-weak definition, we |
12547 | keep the non-weak definition because many backends put | |
12548 | dynamic reloc info on the non-weak definition for code | |
12549 | handling copy relocs. */ | |
12550 | if (h->u.weakdef != NULL) | |
12551 | h->u.weakdef->mark = 1; | |
1cce69b9 | 12552 | |
a6a4679f | 12553 | if (start_stop != NULL) |
1cce69b9 AM |
12554 | { |
12555 | /* To work around a glibc bug, mark all XXX input sections | |
12556 | when there is an as yet undefined reference to __start_XXX | |
12557 | or __stop_XXX symbols. The linker will later define such | |
12558 | symbols for orphan input sections that have a name | |
12559 | representable as a C identifier. */ | |
a6a4679f | 12560 | asection *s = _bfd_elf_is_start_stop (info, h); |
1cce69b9 | 12561 | |
a6a4679f | 12562 | if (s != NULL) |
1cce69b9 | 12563 | { |
a6a4679f AM |
12564 | *start_stop = !s->gc_mark; |
12565 | return s; | |
1cce69b9 AM |
12566 | } |
12567 | } | |
12568 | ||
5241d853 RS |
12569 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); |
12570 | } | |
12571 | ||
12572 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
12573 | &cookie->locsyms[r_symndx]); | |
12574 | } | |
12575 | ||
12576 | /* COOKIE->rel describes a relocation against section SEC, which is | |
12577 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 12578 | the relocation symbol. */ |
5241d853 RS |
12579 | |
12580 | bfd_boolean | |
12581 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, | |
12582 | asection *sec, | |
12583 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 12584 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
12585 | { |
12586 | asection *rsec; | |
1cce69b9 | 12587 | bfd_boolean start_stop = FALSE; |
5241d853 | 12588 | |
1cce69b9 AM |
12589 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); |
12590 | while (rsec != NULL) | |
5241d853 | 12591 | { |
1cce69b9 AM |
12592 | if (!rsec->gc_mark) |
12593 | { | |
12594 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour | |
12595 | || (rsec->owner->flags & DYNAMIC) != 0) | |
12596 | rsec->gc_mark = 1; | |
12597 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) | |
12598 | return FALSE; | |
12599 | } | |
12600 | if (!start_stop) | |
12601 | break; | |
199af150 | 12602 | rsec = bfd_get_next_section_by_name (rsec->owner, rsec); |
5241d853 RS |
12603 | } |
12604 | return TRUE; | |
12605 | } | |
12606 | ||
07adf181 AM |
12607 | /* The mark phase of garbage collection. For a given section, mark |
12608 | it and any sections in this section's group, and all the sections | |
12609 | which define symbols to which it refers. */ | |
12610 | ||
ccfa59ea AM |
12611 | bfd_boolean |
12612 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
12613 | asection *sec, | |
6a5bb875 | 12614 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
12615 | { |
12616 | bfd_boolean ret; | |
9d0a14d3 | 12617 | asection *group_sec, *eh_frame; |
c152c796 AM |
12618 | |
12619 | sec->gc_mark = 1; | |
12620 | ||
12621 | /* Mark all the sections in the group. */ | |
12622 | group_sec = elf_section_data (sec)->next_in_group; | |
12623 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 12624 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
12625 | return FALSE; |
12626 | ||
12627 | /* Look through the section relocs. */ | |
12628 | ret = TRUE; | |
9d0a14d3 RS |
12629 | eh_frame = elf_eh_frame_section (sec->owner); |
12630 | if ((sec->flags & SEC_RELOC) != 0 | |
12631 | && sec->reloc_count > 0 | |
12632 | && sec != eh_frame) | |
c152c796 | 12633 | { |
5241d853 | 12634 | struct elf_reloc_cookie cookie; |
c152c796 | 12635 | |
5241d853 RS |
12636 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
12637 | ret = FALSE; | |
c152c796 | 12638 | else |
c152c796 | 12639 | { |
5241d853 | 12640 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 12641 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 RS |
12642 | { |
12643 | ret = FALSE; | |
12644 | break; | |
12645 | } | |
12646 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
12647 | } |
12648 | } | |
9d0a14d3 RS |
12649 | |
12650 | if (ret && eh_frame && elf_fde_list (sec)) | |
12651 | { | |
12652 | struct elf_reloc_cookie cookie; | |
12653 | ||
12654 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
12655 | ret = FALSE; | |
12656 | else | |
12657 | { | |
12658 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
12659 | gc_mark_hook, &cookie)) | |
12660 | ret = FALSE; | |
12661 | fini_reloc_cookie_for_section (&cookie, eh_frame); | |
12662 | } | |
12663 | } | |
12664 | ||
2f0c68f2 CM |
12665 | eh_frame = elf_section_eh_frame_entry (sec); |
12666 | if (ret && eh_frame && !eh_frame->gc_mark) | |
12667 | if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) | |
12668 | ret = FALSE; | |
12669 | ||
c152c796 AM |
12670 | return ret; |
12671 | } | |
12672 | ||
3c758495 TG |
12673 | /* Scan and mark sections in a special or debug section group. */ |
12674 | ||
12675 | static void | |
12676 | _bfd_elf_gc_mark_debug_special_section_group (asection *grp) | |
12677 | { | |
12678 | /* Point to first section of section group. */ | |
12679 | asection *ssec; | |
12680 | /* Used to iterate the section group. */ | |
12681 | asection *msec; | |
12682 | ||
12683 | bfd_boolean is_special_grp = TRUE; | |
12684 | bfd_boolean is_debug_grp = TRUE; | |
12685 | ||
12686 | /* First scan to see if group contains any section other than debug | |
12687 | and special section. */ | |
12688 | ssec = msec = elf_next_in_group (grp); | |
12689 | do | |
12690 | { | |
12691 | if ((msec->flags & SEC_DEBUGGING) == 0) | |
12692 | is_debug_grp = FALSE; | |
12693 | ||
12694 | if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) | |
12695 | is_special_grp = FALSE; | |
12696 | ||
12697 | msec = elf_next_in_group (msec); | |
12698 | } | |
12699 | while (msec != ssec); | |
12700 | ||
12701 | /* If this is a pure debug section group or pure special section group, | |
12702 | keep all sections in this group. */ | |
12703 | if (is_debug_grp || is_special_grp) | |
12704 | { | |
12705 | do | |
12706 | { | |
12707 | msec->gc_mark = 1; | |
12708 | msec = elf_next_in_group (msec); | |
12709 | } | |
12710 | while (msec != ssec); | |
12711 | } | |
12712 | } | |
12713 | ||
7f6ab9f8 AM |
12714 | /* Keep debug and special sections. */ |
12715 | ||
12716 | bfd_boolean | |
12717 | _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, | |
12718 | elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) | |
12719 | { | |
12720 | bfd *ibfd; | |
12721 | ||
c72f2fb2 | 12722 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
7f6ab9f8 AM |
12723 | { |
12724 | asection *isec; | |
12725 | bfd_boolean some_kept; | |
b40bf0a2 | 12726 | bfd_boolean debug_frag_seen; |
7f6ab9f8 AM |
12727 | |
12728 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
12729 | continue; | |
12730 | ||
b40bf0a2 NC |
12731 | /* Ensure all linker created sections are kept, |
12732 | see if any other section is already marked, | |
12733 | and note if we have any fragmented debug sections. */ | |
12734 | debug_frag_seen = some_kept = FALSE; | |
7f6ab9f8 AM |
12735 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
12736 | { | |
12737 | if ((isec->flags & SEC_LINKER_CREATED) != 0) | |
12738 | isec->gc_mark = 1; | |
12739 | else if (isec->gc_mark) | |
12740 | some_kept = TRUE; | |
b40bf0a2 NC |
12741 | |
12742 | if (debug_frag_seen == FALSE | |
12743 | && (isec->flags & SEC_DEBUGGING) | |
12744 | && CONST_STRNEQ (isec->name, ".debug_line.")) | |
12745 | debug_frag_seen = TRUE; | |
7f6ab9f8 AM |
12746 | } |
12747 | ||
12748 | /* If no section in this file will be kept, then we can | |
b40bf0a2 | 12749 | toss out the debug and special sections. */ |
7f6ab9f8 AM |
12750 | if (!some_kept) |
12751 | continue; | |
12752 | ||
12753 | /* Keep debug and special sections like .comment when they are | |
3c758495 TG |
12754 | not part of a group. Also keep section groups that contain |
12755 | just debug sections or special sections. */ | |
7f6ab9f8 | 12756 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
3c758495 TG |
12757 | { |
12758 | if ((isec->flags & SEC_GROUP) != 0) | |
12759 | _bfd_elf_gc_mark_debug_special_section_group (isec); | |
12760 | else if (((isec->flags & SEC_DEBUGGING) != 0 | |
12761 | || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) | |
12762 | && elf_next_in_group (isec) == NULL) | |
12763 | isec->gc_mark = 1; | |
12764 | } | |
b40bf0a2 NC |
12765 | |
12766 | if (! debug_frag_seen) | |
12767 | continue; | |
12768 | ||
12769 | /* Look for CODE sections which are going to be discarded, | |
12770 | and find and discard any fragmented debug sections which | |
12771 | are associated with that code section. */ | |
12772 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
12773 | if ((isec->flags & SEC_CODE) != 0 | |
12774 | && isec->gc_mark == 0) | |
12775 | { | |
12776 | unsigned int ilen; | |
12777 | asection *dsec; | |
12778 | ||
12779 | ilen = strlen (isec->name); | |
12780 | ||
12781 | /* Association is determined by the name of the debug section | |
12782 | containing the name of the code section as a suffix. For | |
12783 | example .debug_line.text.foo is a debug section associated | |
12784 | with .text.foo. */ | |
12785 | for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) | |
12786 | { | |
12787 | unsigned int dlen; | |
12788 | ||
12789 | if (dsec->gc_mark == 0 | |
12790 | || (dsec->flags & SEC_DEBUGGING) == 0) | |
12791 | continue; | |
12792 | ||
12793 | dlen = strlen (dsec->name); | |
12794 | ||
12795 | if (dlen > ilen | |
12796 | && strncmp (dsec->name + (dlen - ilen), | |
12797 | isec->name, ilen) == 0) | |
12798 | { | |
12799 | dsec->gc_mark = 0; | |
b40bf0a2 NC |
12800 | } |
12801 | } | |
12802 | } | |
7f6ab9f8 AM |
12803 | } |
12804 | return TRUE; | |
12805 | } | |
12806 | ||
c152c796 AM |
12807 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ |
12808 | ||
c17d87de NC |
12809 | struct elf_gc_sweep_symbol_info |
12810 | { | |
ccabcbe5 AM |
12811 | struct bfd_link_info *info; |
12812 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
12813 | bfd_boolean); | |
12814 | }; | |
12815 | ||
c152c796 | 12816 | static bfd_boolean |
ccabcbe5 | 12817 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) |
c152c796 | 12818 | { |
1d5316ab AM |
12819 | if (!h->mark |
12820 | && (((h->root.type == bfd_link_hash_defined | |
12821 | || h->root.type == bfd_link_hash_defweak) | |
c4621b33 | 12822 | && !((h->def_regular || ELF_COMMON_DEF_P (h)) |
6673f753 | 12823 | && h->root.u.def.section->gc_mark)) |
1d5316ab AM |
12824 | || h->root.type == bfd_link_hash_undefined |
12825 | || h->root.type == bfd_link_hash_undefweak)) | |
12826 | { | |
12827 | struct elf_gc_sweep_symbol_info *inf; | |
12828 | ||
12829 | inf = (struct elf_gc_sweep_symbol_info *) data; | |
ccabcbe5 | 12830 | (*inf->hide_symbol) (inf->info, h, TRUE); |
1d5316ab AM |
12831 | h->def_regular = 0; |
12832 | h->ref_regular = 0; | |
12833 | h->ref_regular_nonweak = 0; | |
ccabcbe5 | 12834 | } |
c152c796 AM |
12835 | |
12836 | return TRUE; | |
12837 | } | |
12838 | ||
12839 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
12840 | ||
12841 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
12842 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
12843 | ||
12844 | static bfd_boolean | |
ccabcbe5 | 12845 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
12846 | { |
12847 | bfd *sub; | |
ccabcbe5 AM |
12848 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
12849 | gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; | |
12850 | unsigned long section_sym_count; | |
12851 | struct elf_gc_sweep_symbol_info sweep_info; | |
c152c796 | 12852 | |
c72f2fb2 | 12853 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
12854 | { |
12855 | asection *o; | |
12856 | ||
b19a8f85 L |
12857 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
12858 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) | |
c152c796 AM |
12859 | continue; |
12860 | ||
12861 | for (o = sub->sections; o != NULL; o = o->next) | |
12862 | { | |
a33dafc3 L |
12863 | /* When any section in a section group is kept, we keep all |
12864 | sections in the section group. If the first member of | |
12865 | the section group is excluded, we will also exclude the | |
12866 | group section. */ | |
12867 | if (o->flags & SEC_GROUP) | |
12868 | { | |
12869 | asection *first = elf_next_in_group (o); | |
12870 | o->gc_mark = first->gc_mark; | |
12871 | } | |
c152c796 | 12872 | |
1e7eae0d | 12873 | if (o->gc_mark) |
c152c796 AM |
12874 | continue; |
12875 | ||
12876 | /* Skip sweeping sections already excluded. */ | |
12877 | if (o->flags & SEC_EXCLUDE) | |
12878 | continue; | |
12879 | ||
12880 | /* Since this is early in the link process, it is simple | |
12881 | to remove a section from the output. */ | |
12882 | o->flags |= SEC_EXCLUDE; | |
12883 | ||
c55fe096 | 12884 | if (info->print_gc_sections && o->size != 0) |
695344c0 | 12885 | /* xgettext:c-format */ |
c17d87de NC |
12886 | _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name); |
12887 | ||
c152c796 AM |
12888 | /* But we also have to update some of the relocation |
12889 | info we collected before. */ | |
12890 | if (gc_sweep_hook | |
e8aaee2a | 12891 | && (o->flags & SEC_RELOC) != 0 |
9850436d AM |
12892 | && o->reloc_count != 0 |
12893 | && !((info->strip == strip_all || info->strip == strip_debugger) | |
12894 | && (o->flags & SEC_DEBUGGING) != 0) | |
e8aaee2a | 12895 | && !bfd_is_abs_section (o->output_section)) |
c152c796 AM |
12896 | { |
12897 | Elf_Internal_Rela *internal_relocs; | |
12898 | bfd_boolean r; | |
12899 | ||
12900 | internal_relocs | |
12901 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
12902 | info->keep_memory); | |
12903 | if (internal_relocs == NULL) | |
12904 | return FALSE; | |
12905 | ||
12906 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
12907 | ||
12908 | if (elf_section_data (o)->relocs != internal_relocs) | |
12909 | free (internal_relocs); | |
12910 | ||
12911 | if (!r) | |
12912 | return FALSE; | |
12913 | } | |
12914 | } | |
12915 | } | |
12916 | ||
12917 | /* Remove the symbols that were in the swept sections from the dynamic | |
12918 | symbol table. GCFIXME: Anyone know how to get them out of the | |
12919 | static symbol table as well? */ | |
ccabcbe5 AM |
12920 | sweep_info.info = info; |
12921 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
12922 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
12923 | &sweep_info); | |
c152c796 | 12924 | |
ccabcbe5 | 12925 | _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); |
c152c796 AM |
12926 | return TRUE; |
12927 | } | |
12928 | ||
12929 | /* Propagate collected vtable information. This is called through | |
12930 | elf_link_hash_traverse. */ | |
12931 | ||
12932 | static bfd_boolean | |
12933 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
12934 | { | |
c152c796 | 12935 | /* Those that are not vtables. */ |
f6e332e6 | 12936 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
12937 | return TRUE; |
12938 | ||
12939 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 12940 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
12941 | return TRUE; |
12942 | ||
12943 | /* If we've already been done, exit. */ | |
f6e332e6 | 12944 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
12945 | return TRUE; |
12946 | ||
12947 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 12948 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 12949 | |
f6e332e6 | 12950 | if (h->vtable->used == NULL) |
c152c796 AM |
12951 | { |
12952 | /* None of this table's entries were referenced. Re-use the | |
12953 | parent's table. */ | |
f6e332e6 AM |
12954 | h->vtable->used = h->vtable->parent->vtable->used; |
12955 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
12956 | } |
12957 | else | |
12958 | { | |
12959 | size_t n; | |
12960 | bfd_boolean *cu, *pu; | |
12961 | ||
12962 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 12963 | cu = h->vtable->used; |
c152c796 | 12964 | cu[-1] = TRUE; |
f6e332e6 | 12965 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
12966 | if (pu != NULL) |
12967 | { | |
12968 | const struct elf_backend_data *bed; | |
12969 | unsigned int log_file_align; | |
12970 | ||
12971 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
12972 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 12973 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
12974 | while (n--) |
12975 | { | |
12976 | if (*pu) | |
12977 | *cu = TRUE; | |
12978 | pu++; | |
12979 | cu++; | |
12980 | } | |
12981 | } | |
12982 | } | |
12983 | ||
12984 | return TRUE; | |
12985 | } | |
12986 | ||
12987 | static bfd_boolean | |
12988 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
12989 | { | |
12990 | asection *sec; | |
12991 | bfd_vma hstart, hend; | |
12992 | Elf_Internal_Rela *relstart, *relend, *rel; | |
12993 | const struct elf_backend_data *bed; | |
12994 | unsigned int log_file_align; | |
12995 | ||
c152c796 AM |
12996 | /* Take care of both those symbols that do not describe vtables as |
12997 | well as those that are not loaded. */ | |
f6e332e6 | 12998 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
12999 | return TRUE; |
13000 | ||
13001 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
13002 | || h->root.type == bfd_link_hash_defweak); | |
13003 | ||
13004 | sec = h->root.u.def.section; | |
13005 | hstart = h->root.u.def.value; | |
13006 | hend = hstart + h->size; | |
13007 | ||
13008 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
13009 | if (!relstart) | |
13010 | return *(bfd_boolean *) okp = FALSE; | |
13011 | bed = get_elf_backend_data (sec->owner); | |
13012 | log_file_align = bed->s->log_file_align; | |
13013 | ||
13014 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
13015 | ||
13016 | for (rel = relstart; rel < relend; ++rel) | |
13017 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
13018 | { | |
13019 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
13020 | if (h->vtable->used |
13021 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
13022 | { |
13023 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 13024 | if (h->vtable->used[entry]) |
c152c796 AM |
13025 | continue; |
13026 | } | |
13027 | /* Otherwise, kill it. */ | |
13028 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
13029 | } | |
13030 | ||
13031 | return TRUE; | |
13032 | } | |
13033 | ||
87538722 AM |
13034 | /* Mark sections containing dynamically referenced symbols. When |
13035 | building shared libraries, we must assume that any visible symbol is | |
13036 | referenced. */ | |
715df9b8 | 13037 | |
64d03ab5 AM |
13038 | bfd_boolean |
13039 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 13040 | { |
87538722 | 13041 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
d6f6f455 | 13042 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
87538722 | 13043 | |
715df9b8 EB |
13044 | if ((h->root.type == bfd_link_hash_defined |
13045 | || h->root.type == bfd_link_hash_defweak) | |
87538722 | 13046 | && (h->ref_dynamic |
c4621b33 | 13047 | || ((h->def_regular || ELF_COMMON_DEF_P (h)) |
87538722 | 13048 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL |
fd91d419 | 13049 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN |
0e1862bb | 13050 | && (!bfd_link_executable (info) |
b407645f AM |
13051 | || info->export_dynamic |
13052 | || (h->dynamic | |
13053 | && d != NULL | |
13054 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
422f1182 | 13055 | && (h->versioned >= versioned |
54e8959c L |
13056 | || !bfd_hide_sym_by_version (info->version_info, |
13057 | h->root.root.string))))) | |
715df9b8 EB |
13058 | h->root.u.def.section->flags |= SEC_KEEP; |
13059 | ||
13060 | return TRUE; | |
13061 | } | |
3b36f7e6 | 13062 | |
74f0fb50 AM |
13063 | /* Keep all sections containing symbols undefined on the command-line, |
13064 | and the section containing the entry symbol. */ | |
13065 | ||
13066 | void | |
13067 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
13068 | { | |
13069 | struct bfd_sym_chain *sym; | |
13070 | ||
13071 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
13072 | { | |
13073 | struct elf_link_hash_entry *h; | |
13074 | ||
13075 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
13076 | FALSE, FALSE, FALSE); | |
13077 | ||
13078 | if (h != NULL | |
13079 | && (h->root.type == bfd_link_hash_defined | |
13080 | || h->root.type == bfd_link_hash_defweak) | |
f02cb058 AM |
13081 | && !bfd_is_abs_section (h->root.u.def.section) |
13082 | && !bfd_is_und_section (h->root.u.def.section)) | |
74f0fb50 AM |
13083 | h->root.u.def.section->flags |= SEC_KEEP; |
13084 | } | |
13085 | } | |
13086 | ||
2f0c68f2 CM |
13087 | bfd_boolean |
13088 | bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, | |
13089 | struct bfd_link_info *info) | |
13090 | { | |
13091 | bfd *ibfd = info->input_bfds; | |
13092 | ||
13093 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) | |
13094 | { | |
13095 | asection *sec; | |
13096 | struct elf_reloc_cookie cookie; | |
13097 | ||
13098 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13099 | continue; | |
13100 | ||
13101 | if (!init_reloc_cookie (&cookie, info, ibfd)) | |
13102 | return FALSE; | |
13103 | ||
13104 | for (sec = ibfd->sections; sec; sec = sec->next) | |
13105 | { | |
13106 | if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry") | |
13107 | && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) | |
13108 | { | |
13109 | _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); | |
13110 | fini_reloc_cookie_rels (&cookie, sec); | |
13111 | } | |
13112 | } | |
13113 | } | |
13114 | return TRUE; | |
13115 | } | |
13116 | ||
c152c796 AM |
13117 | /* Do mark and sweep of unused sections. */ |
13118 | ||
13119 | bfd_boolean | |
13120 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
13121 | { | |
13122 | bfd_boolean ok = TRUE; | |
13123 | bfd *sub; | |
6a5bb875 | 13124 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 13125 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
da44f4e5 | 13126 | struct elf_link_hash_table *htab; |
c152c796 | 13127 | |
64d03ab5 | 13128 | if (!bed->can_gc_sections |
715df9b8 | 13129 | || !is_elf_hash_table (info->hash)) |
c152c796 | 13130 | { |
4eca0228 | 13131 | _bfd_error_handler(_("Warning: gc-sections option ignored")); |
c152c796 AM |
13132 | return TRUE; |
13133 | } | |
13134 | ||
74f0fb50 | 13135 | bed->gc_keep (info); |
da44f4e5 | 13136 | htab = elf_hash_table (info); |
74f0fb50 | 13137 | |
9d0a14d3 RS |
13138 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
13139 | at the .eh_frame section if we can mark the FDEs individually. */ | |
2f0c68f2 CM |
13140 | for (sub = info->input_bfds; |
13141 | info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; | |
13142 | sub = sub->link.next) | |
9d0a14d3 RS |
13143 | { |
13144 | asection *sec; | |
13145 | struct elf_reloc_cookie cookie; | |
13146 | ||
13147 | sec = bfd_get_section_by_name (sub, ".eh_frame"); | |
9a2a56cc | 13148 | while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) |
9d0a14d3 RS |
13149 | { |
13150 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
9a2a56cc AM |
13151 | if (elf_section_data (sec)->sec_info |
13152 | && (sec->flags & SEC_LINKER_CREATED) == 0) | |
9d0a14d3 RS |
13153 | elf_eh_frame_section (sub) = sec; |
13154 | fini_reloc_cookie_for_section (&cookie, sec); | |
199af150 | 13155 | sec = bfd_get_next_section_by_name (NULL, sec); |
9d0a14d3 RS |
13156 | } |
13157 | } | |
9d0a14d3 | 13158 | |
c152c796 | 13159 | /* Apply transitive closure to the vtable entry usage info. */ |
da44f4e5 | 13160 | elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); |
c152c796 AM |
13161 | if (!ok) |
13162 | return FALSE; | |
13163 | ||
13164 | /* Kill the vtable relocations that were not used. */ | |
da44f4e5 | 13165 | elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok); |
c152c796 AM |
13166 | if (!ok) |
13167 | return FALSE; | |
13168 | ||
715df9b8 | 13169 | /* Mark dynamically referenced symbols. */ |
da44f4e5 AM |
13170 | if (htab->dynamic_sections_created) |
13171 | elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); | |
c152c796 | 13172 | |
715df9b8 | 13173 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 13174 | gc_mark_hook = bed->gc_mark_hook; |
c72f2fb2 | 13175 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13176 | { |
13177 | asection *o; | |
13178 | ||
b19a8f85 L |
13179 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
13180 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) | |
c152c796 AM |
13181 | continue; |
13182 | ||
7f6ab9f8 AM |
13183 | /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). |
13184 | Also treat note sections as a root, if the section is not part | |
13185 | of a group. */ | |
c152c796 | 13186 | for (o = sub->sections; o != NULL; o = o->next) |
7f6ab9f8 AM |
13187 | if (!o->gc_mark |
13188 | && (o->flags & SEC_EXCLUDE) == 0 | |
24007750 | 13189 | && ((o->flags & SEC_KEEP) != 0 |
7f6ab9f8 AM |
13190 | || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE |
13191 | && elf_next_in_group (o) == NULL ))) | |
13192 | { | |
13193 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
13194 | return FALSE; | |
13195 | } | |
c152c796 AM |
13196 | } |
13197 | ||
6a5bb875 | 13198 | /* Allow the backend to mark additional target specific sections. */ |
7f6ab9f8 | 13199 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 13200 | |
c152c796 | 13201 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 13202 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
13203 | } |
13204 | \f | |
13205 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
13206 | ||
13207 | bfd_boolean | |
13208 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
13209 | asection *sec, | |
13210 | struct elf_link_hash_entry *h, | |
13211 | bfd_vma offset) | |
13212 | { | |
13213 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
13214 | struct elf_link_hash_entry **search, *child; | |
ef53be89 | 13215 | size_t extsymcount; |
c152c796 AM |
13216 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
13217 | ||
13218 | /* The sh_info field of the symtab header tells us where the | |
13219 | external symbols start. We don't care about the local symbols at | |
13220 | this point. */ | |
13221 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
13222 | if (!elf_bad_symtab (abfd)) | |
13223 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
13224 | ||
13225 | sym_hashes = elf_sym_hashes (abfd); | |
13226 | sym_hashes_end = sym_hashes + extsymcount; | |
13227 | ||
13228 | /* Hunt down the child symbol, which is in this section at the same | |
13229 | offset as the relocation. */ | |
13230 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
13231 | { | |
13232 | if ((child = *search) != NULL | |
13233 | && (child->root.type == bfd_link_hash_defined | |
13234 | || child->root.type == bfd_link_hash_defweak) | |
13235 | && child->root.u.def.section == sec | |
13236 | && child->root.u.def.value == offset) | |
13237 | goto win; | |
13238 | } | |
13239 | ||
695344c0 NC |
13240 | /* xgettext:c-format */ |
13241 | _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"), | |
4eca0228 | 13242 | abfd, sec, (unsigned long) offset); |
c152c796 AM |
13243 | bfd_set_error (bfd_error_invalid_operation); |
13244 | return FALSE; | |
13245 | ||
13246 | win: | |
f6e332e6 AM |
13247 | if (!child->vtable) |
13248 | { | |
ca4be51c AM |
13249 | child->vtable = ((struct elf_link_virtual_table_entry *) |
13250 | bfd_zalloc (abfd, sizeof (*child->vtable))); | |
f6e332e6 AM |
13251 | if (!child->vtable) |
13252 | return FALSE; | |
13253 | } | |
c152c796 AM |
13254 | if (!h) |
13255 | { | |
13256 | /* This *should* only be the absolute section. It could potentially | |
13257 | be that someone has defined a non-global vtable though, which | |
13258 | would be bad. It isn't worth paging in the local symbols to be | |
13259 | sure though; that case should simply be handled by the assembler. */ | |
13260 | ||
f6e332e6 | 13261 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
13262 | } |
13263 | else | |
f6e332e6 | 13264 | child->vtable->parent = h; |
c152c796 AM |
13265 | |
13266 | return TRUE; | |
13267 | } | |
13268 | ||
13269 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
13270 | ||
13271 | bfd_boolean | |
13272 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
13273 | asection *sec ATTRIBUTE_UNUSED, | |
13274 | struct elf_link_hash_entry *h, | |
13275 | bfd_vma addend) | |
13276 | { | |
13277 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13278 | unsigned int log_file_align = bed->s->log_file_align; | |
13279 | ||
f6e332e6 AM |
13280 | if (!h->vtable) |
13281 | { | |
ca4be51c AM |
13282 | h->vtable = ((struct elf_link_virtual_table_entry *) |
13283 | bfd_zalloc (abfd, sizeof (*h->vtable))); | |
f6e332e6 AM |
13284 | if (!h->vtable) |
13285 | return FALSE; | |
13286 | } | |
13287 | ||
13288 | if (addend >= h->vtable->size) | |
c152c796 AM |
13289 | { |
13290 | size_t size, bytes, file_align; | |
f6e332e6 | 13291 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
13292 | |
13293 | /* While the symbol is undefined, we have to be prepared to handle | |
13294 | a zero size. */ | |
13295 | file_align = 1 << log_file_align; | |
13296 | if (h->root.type == bfd_link_hash_undefined) | |
13297 | size = addend + file_align; | |
13298 | else | |
13299 | { | |
13300 | size = h->size; | |
13301 | if (addend >= size) | |
13302 | { | |
13303 | /* Oops! We've got a reference past the defined end of | |
13304 | the table. This is probably a bug -- shall we warn? */ | |
13305 | size = addend + file_align; | |
13306 | } | |
13307 | } | |
13308 | size = (size + file_align - 1) & -file_align; | |
13309 | ||
13310 | /* Allocate one extra entry for use as a "done" flag for the | |
13311 | consolidation pass. */ | |
13312 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
13313 | ||
13314 | if (ptr) | |
13315 | { | |
a50b1753 | 13316 | ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); |
c152c796 AM |
13317 | |
13318 | if (ptr != NULL) | |
13319 | { | |
13320 | size_t oldbytes; | |
13321 | ||
f6e332e6 | 13322 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
13323 | * sizeof (bfd_boolean)); |
13324 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
13325 | } | |
13326 | } | |
13327 | else | |
a50b1753 | 13328 | ptr = (bfd_boolean *) bfd_zmalloc (bytes); |
c152c796 AM |
13329 | |
13330 | if (ptr == NULL) | |
13331 | return FALSE; | |
13332 | ||
13333 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
13334 | h->vtable->used = ptr + 1; |
13335 | h->vtable->size = size; | |
c152c796 AM |
13336 | } |
13337 | ||
f6e332e6 | 13338 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
13339 | |
13340 | return TRUE; | |
13341 | } | |
13342 | ||
ae17ab41 CM |
13343 | /* Map an ELF section header flag to its corresponding string. */ |
13344 | typedef struct | |
13345 | { | |
13346 | char *flag_name; | |
13347 | flagword flag_value; | |
13348 | } elf_flags_to_name_table; | |
13349 | ||
13350 | static elf_flags_to_name_table elf_flags_to_names [] = | |
13351 | { | |
13352 | { "SHF_WRITE", SHF_WRITE }, | |
13353 | { "SHF_ALLOC", SHF_ALLOC }, | |
13354 | { "SHF_EXECINSTR", SHF_EXECINSTR }, | |
13355 | { "SHF_MERGE", SHF_MERGE }, | |
13356 | { "SHF_STRINGS", SHF_STRINGS }, | |
13357 | { "SHF_INFO_LINK", SHF_INFO_LINK}, | |
13358 | { "SHF_LINK_ORDER", SHF_LINK_ORDER}, | |
13359 | { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, | |
13360 | { "SHF_GROUP", SHF_GROUP }, | |
13361 | { "SHF_TLS", SHF_TLS }, | |
13362 | { "SHF_MASKOS", SHF_MASKOS }, | |
13363 | { "SHF_EXCLUDE", SHF_EXCLUDE }, | |
13364 | }; | |
13365 | ||
b9c361e0 JL |
13366 | /* Returns TRUE if the section is to be included, otherwise FALSE. */ |
13367 | bfd_boolean | |
ae17ab41 | 13368 | bfd_elf_lookup_section_flags (struct bfd_link_info *info, |
8b127cbc | 13369 | struct flag_info *flaginfo, |
b9c361e0 | 13370 | asection *section) |
ae17ab41 | 13371 | { |
8b127cbc | 13372 | const bfd_vma sh_flags = elf_section_flags (section); |
ae17ab41 | 13373 | |
8b127cbc | 13374 | if (!flaginfo->flags_initialized) |
ae17ab41 | 13375 | { |
8b127cbc AM |
13376 | bfd *obfd = info->output_bfd; |
13377 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
13378 | struct flag_info_list *tf = flaginfo->flag_list; | |
b9c361e0 JL |
13379 | int with_hex = 0; |
13380 | int without_hex = 0; | |
13381 | ||
8b127cbc | 13382 | for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) |
ae17ab41 | 13383 | { |
b9c361e0 | 13384 | unsigned i; |
8b127cbc | 13385 | flagword (*lookup) (char *); |
ae17ab41 | 13386 | |
8b127cbc AM |
13387 | lookup = bed->elf_backend_lookup_section_flags_hook; |
13388 | if (lookup != NULL) | |
ae17ab41 | 13389 | { |
8b127cbc | 13390 | flagword hexval = (*lookup) ((char *) tf->name); |
b9c361e0 JL |
13391 | |
13392 | if (hexval != 0) | |
13393 | { | |
13394 | if (tf->with == with_flags) | |
13395 | with_hex |= hexval; | |
13396 | else if (tf->with == without_flags) | |
13397 | without_hex |= hexval; | |
13398 | tf->valid = TRUE; | |
13399 | continue; | |
13400 | } | |
ae17ab41 | 13401 | } |
8b127cbc | 13402 | for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) |
ae17ab41 | 13403 | { |
8b127cbc | 13404 | if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) |
b9c361e0 JL |
13405 | { |
13406 | if (tf->with == with_flags) | |
13407 | with_hex |= elf_flags_to_names[i].flag_value; | |
13408 | else if (tf->with == without_flags) | |
13409 | without_hex |= elf_flags_to_names[i].flag_value; | |
13410 | tf->valid = TRUE; | |
13411 | break; | |
13412 | } | |
13413 | } | |
8b127cbc | 13414 | if (!tf->valid) |
b9c361e0 | 13415 | { |
68ffbac6 | 13416 | info->callbacks->einfo |
8b127cbc | 13417 | (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); |
b9c361e0 | 13418 | return FALSE; |
ae17ab41 CM |
13419 | } |
13420 | } | |
8b127cbc AM |
13421 | flaginfo->flags_initialized = TRUE; |
13422 | flaginfo->only_with_flags |= with_hex; | |
13423 | flaginfo->not_with_flags |= without_hex; | |
ae17ab41 | 13424 | } |
ae17ab41 | 13425 | |
8b127cbc | 13426 | if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) |
b9c361e0 JL |
13427 | return FALSE; |
13428 | ||
8b127cbc | 13429 | if ((flaginfo->not_with_flags & sh_flags) != 0) |
b9c361e0 JL |
13430 | return FALSE; |
13431 | ||
13432 | return TRUE; | |
ae17ab41 CM |
13433 | } |
13434 | ||
c152c796 AM |
13435 | struct alloc_got_off_arg { |
13436 | bfd_vma gotoff; | |
10455f89 | 13437 | struct bfd_link_info *info; |
c152c796 AM |
13438 | }; |
13439 | ||
13440 | /* We need a special top-level link routine to convert got reference counts | |
13441 | to real got offsets. */ | |
13442 | ||
13443 | static bfd_boolean | |
13444 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
13445 | { | |
a50b1753 | 13446 | struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; |
10455f89 HPN |
13447 | bfd *obfd = gofarg->info->output_bfd; |
13448 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
c152c796 | 13449 | |
c152c796 AM |
13450 | if (h->got.refcount > 0) |
13451 | { | |
13452 | h->got.offset = gofarg->gotoff; | |
10455f89 | 13453 | gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); |
c152c796 AM |
13454 | } |
13455 | else | |
13456 | h->got.offset = (bfd_vma) -1; | |
13457 | ||
13458 | return TRUE; | |
13459 | } | |
13460 | ||
13461 | /* And an accompanying bit to work out final got entry offsets once | |
13462 | we're done. Should be called from final_link. */ | |
13463 | ||
13464 | bfd_boolean | |
13465 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
13466 | struct bfd_link_info *info) | |
13467 | { | |
13468 | bfd *i; | |
13469 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13470 | bfd_vma gotoff; | |
c152c796 AM |
13471 | struct alloc_got_off_arg gofarg; |
13472 | ||
10455f89 HPN |
13473 | BFD_ASSERT (abfd == info->output_bfd); |
13474 | ||
c152c796 AM |
13475 | if (! is_elf_hash_table (info->hash)) |
13476 | return FALSE; | |
13477 | ||
13478 | /* The GOT offset is relative to the .got section, but the GOT header is | |
13479 | put into the .got.plt section, if the backend uses it. */ | |
13480 | if (bed->want_got_plt) | |
13481 | gotoff = 0; | |
13482 | else | |
13483 | gotoff = bed->got_header_size; | |
13484 | ||
13485 | /* Do the local .got entries first. */ | |
c72f2fb2 | 13486 | for (i = info->input_bfds; i; i = i->link.next) |
c152c796 AM |
13487 | { |
13488 | bfd_signed_vma *local_got; | |
ef53be89 | 13489 | size_t j, locsymcount; |
c152c796 AM |
13490 | Elf_Internal_Shdr *symtab_hdr; |
13491 | ||
13492 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
13493 | continue; | |
13494 | ||
13495 | local_got = elf_local_got_refcounts (i); | |
13496 | if (!local_got) | |
13497 | continue; | |
13498 | ||
13499 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
13500 | if (elf_bad_symtab (i)) | |
13501 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
13502 | else | |
13503 | locsymcount = symtab_hdr->sh_info; | |
13504 | ||
13505 | for (j = 0; j < locsymcount; ++j) | |
13506 | { | |
13507 | if (local_got[j] > 0) | |
13508 | { | |
13509 | local_got[j] = gotoff; | |
10455f89 | 13510 | gotoff += bed->got_elt_size (abfd, info, NULL, i, j); |
c152c796 AM |
13511 | } |
13512 | else | |
13513 | local_got[j] = (bfd_vma) -1; | |
13514 | } | |
13515 | } | |
13516 | ||
13517 | /* Then the global .got entries. .plt refcounts are handled by | |
13518 | adjust_dynamic_symbol */ | |
13519 | gofarg.gotoff = gotoff; | |
10455f89 | 13520 | gofarg.info = info; |
c152c796 AM |
13521 | elf_link_hash_traverse (elf_hash_table (info), |
13522 | elf_gc_allocate_got_offsets, | |
13523 | &gofarg); | |
13524 | return TRUE; | |
13525 | } | |
13526 | ||
13527 | /* Many folk need no more in the way of final link than this, once | |
13528 | got entry reference counting is enabled. */ | |
13529 | ||
13530 | bfd_boolean | |
13531 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
13532 | { | |
13533 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
13534 | return FALSE; | |
13535 | ||
13536 | /* Invoke the regular ELF backend linker to do all the work. */ | |
13537 | return bfd_elf_final_link (abfd, info); | |
13538 | } | |
13539 | ||
13540 | bfd_boolean | |
13541 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
13542 | { | |
a50b1753 | 13543 | struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; |
c152c796 AM |
13544 | |
13545 | if (rcookie->bad_symtab) | |
13546 | rcookie->rel = rcookie->rels; | |
13547 | ||
13548 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
13549 | { | |
13550 | unsigned long r_symndx; | |
13551 | ||
13552 | if (! rcookie->bad_symtab) | |
13553 | if (rcookie->rel->r_offset > offset) | |
13554 | return FALSE; | |
13555 | if (rcookie->rel->r_offset != offset) | |
13556 | continue; | |
13557 | ||
13558 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
2c2fa401 | 13559 | if (r_symndx == STN_UNDEF) |
c152c796 AM |
13560 | return TRUE; |
13561 | ||
13562 | if (r_symndx >= rcookie->locsymcount | |
13563 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
13564 | { | |
13565 | struct elf_link_hash_entry *h; | |
13566 | ||
13567 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
13568 | ||
13569 | while (h->root.type == bfd_link_hash_indirect | |
13570 | || h->root.type == bfd_link_hash_warning) | |
13571 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
13572 | ||
13573 | if ((h->root.type == bfd_link_hash_defined | |
13574 | || h->root.type == bfd_link_hash_defweak) | |
5b69e357 AM |
13575 | && (h->root.u.def.section->owner != rcookie->abfd |
13576 | || h->root.u.def.section->kept_section != NULL | |
13577 | || discarded_section (h->root.u.def.section))) | |
c152c796 | 13578 | return TRUE; |
c152c796 AM |
13579 | } |
13580 | else | |
13581 | { | |
13582 | /* It's not a relocation against a global symbol, | |
13583 | but it could be a relocation against a local | |
13584 | symbol for a discarded section. */ | |
13585 | asection *isec; | |
13586 | Elf_Internal_Sym *isym; | |
13587 | ||
13588 | /* Need to: get the symbol; get the section. */ | |
13589 | isym = &rcookie->locsyms[r_symndx]; | |
cb33740c | 13590 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); |
5b69e357 AM |
13591 | if (isec != NULL |
13592 | && (isec->kept_section != NULL | |
13593 | || discarded_section (isec))) | |
cb33740c | 13594 | return TRUE; |
c152c796 AM |
13595 | } |
13596 | return FALSE; | |
13597 | } | |
13598 | return FALSE; | |
13599 | } | |
13600 | ||
13601 | /* Discard unneeded references to discarded sections. | |
75938853 AM |
13602 | Returns -1 on error, 1 if any section's size was changed, 0 if |
13603 | nothing changed. This function assumes that the relocations are in | |
13604 | sorted order, which is true for all known assemblers. */ | |
c152c796 | 13605 | |
75938853 | 13606 | int |
c152c796 AM |
13607 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) |
13608 | { | |
13609 | struct elf_reloc_cookie cookie; | |
18cd5bce | 13610 | asection *o; |
c152c796 | 13611 | bfd *abfd; |
75938853 | 13612 | int changed = 0; |
c152c796 AM |
13613 | |
13614 | if (info->traditional_format | |
13615 | || !is_elf_hash_table (info->hash)) | |
75938853 | 13616 | return 0; |
c152c796 | 13617 | |
18cd5bce AM |
13618 | o = bfd_get_section_by_name (output_bfd, ".stab"); |
13619 | if (o != NULL) | |
c152c796 | 13620 | { |
18cd5bce | 13621 | asection *i; |
c152c796 | 13622 | |
18cd5bce | 13623 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
8da3dbc5 | 13624 | { |
18cd5bce AM |
13625 | if (i->size == 0 |
13626 | || i->reloc_count == 0 | |
13627 | || i->sec_info_type != SEC_INFO_TYPE_STABS) | |
13628 | continue; | |
c152c796 | 13629 | |
18cd5bce AM |
13630 | abfd = i->owner; |
13631 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
13632 | continue; | |
c152c796 | 13633 | |
18cd5bce | 13634 | if (!init_reloc_cookie_for_section (&cookie, info, i)) |
75938853 | 13635 | return -1; |
c152c796 | 13636 | |
18cd5bce AM |
13637 | if (_bfd_discard_section_stabs (abfd, i, |
13638 | elf_section_data (i)->sec_info, | |
5241d853 RS |
13639 | bfd_elf_reloc_symbol_deleted_p, |
13640 | &cookie)) | |
75938853 | 13641 | changed = 1; |
18cd5bce AM |
13642 | |
13643 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 13644 | } |
18cd5bce AM |
13645 | } |
13646 | ||
2f0c68f2 CM |
13647 | o = NULL; |
13648 | if (info->eh_frame_hdr_type != COMPACT_EH_HDR) | |
13649 | o = bfd_get_section_by_name (output_bfd, ".eh_frame"); | |
18cd5bce AM |
13650 | if (o != NULL) |
13651 | { | |
13652 | asection *i; | |
c152c796 | 13653 | |
18cd5bce | 13654 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
c152c796 | 13655 | { |
18cd5bce AM |
13656 | if (i->size == 0) |
13657 | continue; | |
13658 | ||
13659 | abfd = i->owner; | |
13660 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
13661 | continue; | |
13662 | ||
13663 | if (!init_reloc_cookie_for_section (&cookie, info, i)) | |
75938853 | 13664 | return -1; |
18cd5bce AM |
13665 | |
13666 | _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); | |
13667 | if (_bfd_elf_discard_section_eh_frame (abfd, info, i, | |
c152c796 AM |
13668 | bfd_elf_reloc_symbol_deleted_p, |
13669 | &cookie)) | |
75938853 | 13670 | changed = 1; |
18cd5bce AM |
13671 | |
13672 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 13673 | } |
18cd5bce | 13674 | } |
c152c796 | 13675 | |
18cd5bce AM |
13676 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) |
13677 | { | |
13678 | const struct elf_backend_data *bed; | |
c152c796 | 13679 | |
18cd5bce AM |
13680 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) |
13681 | continue; | |
13682 | ||
13683 | bed = get_elf_backend_data (abfd); | |
13684 | ||
13685 | if (bed->elf_backend_discard_info != NULL) | |
13686 | { | |
13687 | if (!init_reloc_cookie (&cookie, info, abfd)) | |
75938853 | 13688 | return -1; |
18cd5bce AM |
13689 | |
13690 | if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
75938853 | 13691 | changed = 1; |
18cd5bce AM |
13692 | |
13693 | fini_reloc_cookie (&cookie, abfd); | |
13694 | } | |
c152c796 AM |
13695 | } |
13696 | ||
2f0c68f2 CM |
13697 | if (info->eh_frame_hdr_type == COMPACT_EH_HDR) |
13698 | _bfd_elf_end_eh_frame_parsing (info); | |
13699 | ||
13700 | if (info->eh_frame_hdr_type | |
0e1862bb | 13701 | && !bfd_link_relocatable (info) |
c152c796 | 13702 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) |
75938853 | 13703 | changed = 1; |
c152c796 | 13704 | |
75938853 | 13705 | return changed; |
c152c796 | 13706 | } |
082b7297 | 13707 | |
43e1669b | 13708 | bfd_boolean |
0c511000 | 13709 | _bfd_elf_section_already_linked (bfd *abfd, |
c77ec726 | 13710 | asection *sec, |
c0f00686 | 13711 | struct bfd_link_info *info) |
082b7297 L |
13712 | { |
13713 | flagword flags; | |
c77ec726 | 13714 | const char *name, *key; |
082b7297 L |
13715 | struct bfd_section_already_linked *l; |
13716 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
0c511000 | 13717 | |
c77ec726 AM |
13718 | if (sec->output_section == bfd_abs_section_ptr) |
13719 | return FALSE; | |
0c511000 | 13720 | |
c77ec726 | 13721 | flags = sec->flags; |
0c511000 | 13722 | |
c77ec726 AM |
13723 | /* Return if it isn't a linkonce section. A comdat group section |
13724 | also has SEC_LINK_ONCE set. */ | |
13725 | if ((flags & SEC_LINK_ONCE) == 0) | |
13726 | return FALSE; | |
0c511000 | 13727 | |
c77ec726 AM |
13728 | /* Don't put group member sections on our list of already linked |
13729 | sections. They are handled as a group via their group section. */ | |
13730 | if (elf_sec_group (sec) != NULL) | |
13731 | return FALSE; | |
0c511000 | 13732 | |
c77ec726 AM |
13733 | /* For a SHT_GROUP section, use the group signature as the key. */ |
13734 | name = sec->name; | |
13735 | if ((flags & SEC_GROUP) != 0 | |
13736 | && elf_next_in_group (sec) != NULL | |
13737 | && elf_group_name (elf_next_in_group (sec)) != NULL) | |
13738 | key = elf_group_name (elf_next_in_group (sec)); | |
13739 | else | |
13740 | { | |
13741 | /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ | |
0c511000 | 13742 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
c77ec726 AM |
13743 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
13744 | key++; | |
0c511000 | 13745 | else |
c77ec726 AM |
13746 | /* Must be a user linkonce section that doesn't follow gcc's |
13747 | naming convention. In this case we won't be matching | |
13748 | single member groups. */ | |
13749 | key = name; | |
0c511000 | 13750 | } |
6d2cd210 | 13751 | |
c77ec726 | 13752 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
082b7297 L |
13753 | |
13754 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13755 | { | |
c2370991 | 13756 | /* We may have 2 different types of sections on the list: group |
c77ec726 AM |
13757 | sections with a signature of <key> (<key> is some string), |
13758 | and linkonce sections named .gnu.linkonce.<type>.<key>. | |
13759 | Match like sections. LTO plugin sections are an exception. | |
13760 | They are always named .gnu.linkonce.t.<key> and match either | |
13761 | type of section. */ | |
13762 | if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) | |
13763 | && ((flags & SEC_GROUP) != 0 | |
13764 | || strcmp (name, l->sec->name) == 0)) | |
13765 | || (l->sec->owner->flags & BFD_PLUGIN) != 0) | |
082b7297 L |
13766 | { |
13767 | /* The section has already been linked. See if we should | |
6d2cd210 | 13768 | issue a warning. */ |
c77ec726 AM |
13769 | if (!_bfd_handle_already_linked (sec, l, info)) |
13770 | return FALSE; | |
082b7297 | 13771 | |
c77ec726 | 13772 | if (flags & SEC_GROUP) |
3d7f7666 | 13773 | { |
c77ec726 AM |
13774 | asection *first = elf_next_in_group (sec); |
13775 | asection *s = first; | |
3d7f7666 | 13776 | |
c77ec726 | 13777 | while (s != NULL) |
3d7f7666 | 13778 | { |
c77ec726 AM |
13779 | s->output_section = bfd_abs_section_ptr; |
13780 | /* Record which group discards it. */ | |
13781 | s->kept_section = l->sec; | |
13782 | s = elf_next_in_group (s); | |
13783 | /* These lists are circular. */ | |
13784 | if (s == first) | |
13785 | break; | |
3d7f7666 L |
13786 | } |
13787 | } | |
082b7297 | 13788 | |
43e1669b | 13789 | return TRUE; |
082b7297 L |
13790 | } |
13791 | } | |
13792 | ||
c77ec726 AM |
13793 | /* A single member comdat group section may be discarded by a |
13794 | linkonce section and vice versa. */ | |
13795 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 13796 | { |
c77ec726 | 13797 | asection *first = elf_next_in_group (sec); |
c2370991 | 13798 | |
c77ec726 AM |
13799 | if (first != NULL && elf_next_in_group (first) == first) |
13800 | /* Check this single member group against linkonce sections. */ | |
13801 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13802 | if ((l->sec->flags & SEC_GROUP) == 0 | |
13803 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
13804 | { | |
13805 | first->output_section = bfd_abs_section_ptr; | |
13806 | first->kept_section = l->sec; | |
13807 | sec->output_section = bfd_abs_section_ptr; | |
13808 | break; | |
13809 | } | |
13810 | } | |
13811 | else | |
13812 | /* Check this linkonce section against single member groups. */ | |
13813 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13814 | if (l->sec->flags & SEC_GROUP) | |
6d2cd210 | 13815 | { |
c77ec726 | 13816 | asection *first = elf_next_in_group (l->sec); |
6d2cd210 | 13817 | |
c77ec726 AM |
13818 | if (first != NULL |
13819 | && elf_next_in_group (first) == first | |
13820 | && bfd_elf_match_symbols_in_sections (first, sec, info)) | |
13821 | { | |
13822 | sec->output_section = bfd_abs_section_ptr; | |
13823 | sec->kept_section = first; | |
13824 | break; | |
13825 | } | |
6d2cd210 | 13826 | } |
0c511000 | 13827 | |
c77ec726 AM |
13828 | /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' |
13829 | referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 | |
13830 | specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' | |
13831 | prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its | |
13832 | matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded | |
13833 | but its `.gnu.linkonce.t.F' is discarded means we chose one-only | |
13834 | `.gnu.linkonce.t.F' section from a different bfd not requiring any | |
13835 | `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. | |
13836 | The reverse order cannot happen as there is never a bfd with only the | |
13837 | `.gnu.linkonce.r.F' section. The order of sections in a bfd does not | |
13838 | matter as here were are looking only for cross-bfd sections. */ | |
13839 | ||
13840 | if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) | |
13841 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13842 | if ((l->sec->flags & SEC_GROUP) == 0 | |
13843 | && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) | |
13844 | { | |
13845 | if (abfd != l->sec->owner) | |
13846 | sec->output_section = bfd_abs_section_ptr; | |
13847 | break; | |
13848 | } | |
80c29487 | 13849 | |
082b7297 | 13850 | /* This is the first section with this name. Record it. */ |
c77ec726 | 13851 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
bb6198d2 | 13852 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
c77ec726 | 13853 | return sec->output_section == bfd_abs_section_ptr; |
082b7297 | 13854 | } |
81e1b023 | 13855 | |
a4d8e49b L |
13856 | bfd_boolean |
13857 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
13858 | { | |
13859 | return sym->st_shndx == SHN_COMMON; | |
13860 | } | |
13861 | ||
13862 | unsigned int | |
13863 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
13864 | { | |
13865 | return SHN_COMMON; | |
13866 | } | |
13867 | ||
13868 | asection * | |
13869 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
13870 | { | |
13871 | return bfd_com_section_ptr; | |
13872 | } | |
10455f89 HPN |
13873 | |
13874 | bfd_vma | |
13875 | _bfd_elf_default_got_elt_size (bfd *abfd, | |
13876 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
13877 | struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, | |
13878 | bfd *ibfd ATTRIBUTE_UNUSED, | |
13879 | unsigned long symndx ATTRIBUTE_UNUSED) | |
13880 | { | |
13881 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13882 | return bed->s->arch_size / 8; | |
13883 | } | |
83bac4b0 NC |
13884 | |
13885 | /* Routines to support the creation of dynamic relocs. */ | |
13886 | ||
83bac4b0 NC |
13887 | /* Returns the name of the dynamic reloc section associated with SEC. */ |
13888 | ||
13889 | static const char * | |
13890 | get_dynamic_reloc_section_name (bfd * abfd, | |
13891 | asection * sec, | |
13892 | bfd_boolean is_rela) | |
13893 | { | |
ddcf1fcf BS |
13894 | char *name; |
13895 | const char *old_name = bfd_get_section_name (NULL, sec); | |
13896 | const char *prefix = is_rela ? ".rela" : ".rel"; | |
83bac4b0 | 13897 | |
ddcf1fcf | 13898 | if (old_name == NULL) |
83bac4b0 NC |
13899 | return NULL; |
13900 | ||
ddcf1fcf | 13901 | name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); |
68ffbac6 | 13902 | sprintf (name, "%s%s", prefix, old_name); |
83bac4b0 NC |
13903 | |
13904 | return name; | |
13905 | } | |
13906 | ||
13907 | /* Returns the dynamic reloc section associated with SEC. | |
13908 | If necessary compute the name of the dynamic reloc section based | |
13909 | on SEC's name (looked up in ABFD's string table) and the setting | |
13910 | of IS_RELA. */ | |
13911 | ||
13912 | asection * | |
13913 | _bfd_elf_get_dynamic_reloc_section (bfd * abfd, | |
13914 | asection * sec, | |
13915 | bfd_boolean is_rela) | |
13916 | { | |
13917 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
13918 | ||
13919 | if (reloc_sec == NULL) | |
13920 | { | |
13921 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
13922 | ||
13923 | if (name != NULL) | |
13924 | { | |
3d4d4302 | 13925 | reloc_sec = bfd_get_linker_section (abfd, name); |
83bac4b0 NC |
13926 | |
13927 | if (reloc_sec != NULL) | |
13928 | elf_section_data (sec)->sreloc = reloc_sec; | |
13929 | } | |
13930 | } | |
13931 | ||
13932 | return reloc_sec; | |
13933 | } | |
13934 | ||
13935 | /* Returns the dynamic reloc section associated with SEC. If the | |
13936 | section does not exist it is created and attached to the DYNOBJ | |
13937 | bfd and stored in the SRELOC field of SEC's elf_section_data | |
13938 | structure. | |
f8076f98 | 13939 | |
83bac4b0 NC |
13940 | ALIGNMENT is the alignment for the newly created section and |
13941 | IS_RELA defines whether the name should be .rela.<SEC's name> | |
13942 | or .rel.<SEC's name>. The section name is looked up in the | |
13943 | string table associated with ABFD. */ | |
13944 | ||
13945 | asection * | |
ca4be51c AM |
13946 | _bfd_elf_make_dynamic_reloc_section (asection *sec, |
13947 | bfd *dynobj, | |
13948 | unsigned int alignment, | |
13949 | bfd *abfd, | |
13950 | bfd_boolean is_rela) | |
83bac4b0 NC |
13951 | { |
13952 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
13953 | ||
13954 | if (reloc_sec == NULL) | |
13955 | { | |
13956 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
13957 | ||
13958 | if (name == NULL) | |
13959 | return NULL; | |
13960 | ||
3d4d4302 | 13961 | reloc_sec = bfd_get_linker_section (dynobj, name); |
83bac4b0 NC |
13962 | |
13963 | if (reloc_sec == NULL) | |
13964 | { | |
3d4d4302 AM |
13965 | flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY |
13966 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
83bac4b0 NC |
13967 | if ((sec->flags & SEC_ALLOC) != 0) |
13968 | flags |= SEC_ALLOC | SEC_LOAD; | |
13969 | ||
3d4d4302 | 13970 | reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); |
83bac4b0 NC |
13971 | if (reloc_sec != NULL) |
13972 | { | |
8877b5e5 AM |
13973 | /* _bfd_elf_get_sec_type_attr chooses a section type by |
13974 | name. Override as it may be wrong, eg. for a user | |
13975 | section named "auto" we'll get ".relauto" which is | |
13976 | seen to be a .rela section. */ | |
13977 | elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; | |
83bac4b0 NC |
13978 | if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) |
13979 | reloc_sec = NULL; | |
13980 | } | |
13981 | } | |
13982 | ||
13983 | elf_section_data (sec)->sreloc = reloc_sec; | |
13984 | } | |
13985 | ||
13986 | return reloc_sec; | |
13987 | } | |
1338dd10 | 13988 | |
bffebb6b AM |
13989 | /* Copy the ELF symbol type and other attributes for a linker script |
13990 | assignment from HSRC to HDEST. Generally this should be treated as | |
13991 | if we found a strong non-dynamic definition for HDEST (except that | |
13992 | ld ignores multiple definition errors). */ | |
1338dd10 | 13993 | void |
bffebb6b AM |
13994 | _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, |
13995 | struct bfd_link_hash_entry *hdest, | |
13996 | struct bfd_link_hash_entry *hsrc) | |
1338dd10 | 13997 | { |
bffebb6b AM |
13998 | struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; |
13999 | struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; | |
14000 | Elf_Internal_Sym isym; | |
1338dd10 PB |
14001 | |
14002 | ehdest->type = ehsrc->type; | |
35fc36a8 | 14003 | ehdest->target_internal = ehsrc->target_internal; |
bffebb6b AM |
14004 | |
14005 | isym.st_other = ehsrc->other; | |
b8417128 | 14006 | elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE); |
1338dd10 | 14007 | } |
351f65ca L |
14008 | |
14009 | /* Append a RELA relocation REL to section S in BFD. */ | |
14010 | ||
14011 | void | |
14012 | elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14013 | { | |
14014 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14015 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); | |
14016 | BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); | |
14017 | bed->s->swap_reloca_out (abfd, rel, loc); | |
14018 | } | |
14019 | ||
14020 | /* Append a REL relocation REL to section S in BFD. */ | |
14021 | ||
14022 | void | |
14023 | elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14024 | { | |
14025 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14026 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); | |
14027 | BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); | |
59d6ffb2 | 14028 | bed->s->swap_reloc_out (abfd, rel, loc); |
351f65ca | 14029 | } |