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 | |
8e2a4f11 AM |
580 | if (h->root.type == bfd_link_hash_warning) |
581 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
582 | ||
0f550b3d L |
583 | if (h->versioned == unknown) |
584 | { | |
585 | /* Set versioned if symbol version is unknown. */ | |
586 | char *version = strrchr (name, ELF_VER_CHR); | |
587 | if (version) | |
588 | { | |
589 | if (version > name && version[-1] != ELF_VER_CHR) | |
590 | h->versioned = versioned_hidden; | |
591 | else | |
592 | h->versioned = versioned; | |
593 | } | |
594 | } | |
595 | ||
00cbee0a | 596 | switch (h->root.type) |
77cfaee6 | 597 | { |
00cbee0a L |
598 | case bfd_link_hash_defined: |
599 | case bfd_link_hash_defweak: | |
600 | case bfd_link_hash_common: | |
601 | break; | |
602 | case bfd_link_hash_undefweak: | |
603 | case bfd_link_hash_undefined: | |
604 | /* Since we're defining the symbol, don't let it seem to have not | |
605 | been defined. record_dynamic_symbol and size_dynamic_sections | |
606 | may depend on this. */ | |
4ea42fb7 | 607 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
608 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
609 | bfd_link_repair_undef_list (&htab->root); | |
00cbee0a L |
610 | break; |
611 | case bfd_link_hash_new: | |
40b36307 | 612 | bfd_elf_link_mark_dynamic_symbol (info, h, NULL); |
55255dae | 613 | h->non_elf = 0; |
00cbee0a L |
614 | break; |
615 | case bfd_link_hash_indirect: | |
616 | /* We had a versioned symbol in a dynamic library. We make the | |
a0c8462f | 617 | the versioned symbol point to this one. */ |
00cbee0a L |
618 | bed = get_elf_backend_data (output_bfd); |
619 | hv = h; | |
620 | while (hv->root.type == bfd_link_hash_indirect | |
621 | || hv->root.type == bfd_link_hash_warning) | |
622 | hv = (struct elf_link_hash_entry *) hv->root.u.i.link; | |
623 | /* We don't need to update h->root.u since linker will set them | |
624 | later. */ | |
625 | h->root.type = bfd_link_hash_undefined; | |
626 | hv->root.type = bfd_link_hash_indirect; | |
627 | hv->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
628 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); | |
629 | break; | |
8e2a4f11 AM |
630 | default: |
631 | BFD_FAIL (); | |
c2596ca5 | 632 | return FALSE; |
55255dae | 633 | } |
45d6a902 AM |
634 | |
635 | /* If this symbol is being provided by the linker script, and it is | |
636 | currently defined by a dynamic object, but not by a regular | |
637 | object, then mark it as undefined so that the generic linker will | |
638 | force the correct value. */ | |
639 | if (provide | |
f5385ebf AM |
640 | && h->def_dynamic |
641 | && !h->def_regular) | |
45d6a902 AM |
642 | h->root.type = bfd_link_hash_undefined; |
643 | ||
644 | /* If this symbol is not being provided by the linker script, and it is | |
645 | currently defined by a dynamic object, but not by a regular object, | |
646 | then clear out any version information because the symbol will not be | |
647 | associated with the dynamic object any more. */ | |
648 | if (!provide | |
f5385ebf AM |
649 | && h->def_dynamic |
650 | && !h->def_regular) | |
45d6a902 AM |
651 | h->verinfo.verdef = NULL; |
652 | ||
f5385ebf | 653 | h->def_regular = 1; |
45d6a902 | 654 | |
eb8476a6 | 655 | if (hidden) |
fe21a8fc | 656 | { |
91d6fa6a | 657 | bed = get_elf_backend_data (output_bfd); |
b8297068 AM |
658 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
659 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
fe21a8fc L |
660 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
661 | } | |
662 | ||
6fa3860b PB |
663 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
664 | and executables. */ | |
0e1862bb | 665 | if (!bfd_link_relocatable (info) |
6fa3860b PB |
666 | && h->dynindx != -1 |
667 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
668 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
669 | h->forced_local = 1; | |
670 | ||
f5385ebf AM |
671 | if ((h->def_dynamic |
672 | || h->ref_dynamic | |
6b3b0ab8 L |
673 | || bfd_link_dll (info) |
674 | || elf_hash_table (info)->is_relocatable_executable) | |
45d6a902 AM |
675 | && h->dynindx == -1) |
676 | { | |
c152c796 | 677 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
678 | return FALSE; |
679 | ||
680 | /* If this is a weak defined symbol, and we know a corresponding | |
681 | real symbol from the same dynamic object, make sure the real | |
682 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
683 | if (h->u.weakdef != NULL |
684 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 685 | { |
f6e332e6 | 686 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
687 | return FALSE; |
688 | } | |
689 | } | |
690 | ||
691 | return TRUE; | |
692 | } | |
42751cf3 | 693 | |
8c58d23b AM |
694 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
695 | success, and 2 on a failure caused by attempting to record a symbol | |
696 | in a discarded section, eg. a discarded link-once section symbol. */ | |
697 | ||
698 | int | |
c152c796 AM |
699 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
700 | bfd *input_bfd, | |
701 | long input_indx) | |
8c58d23b AM |
702 | { |
703 | bfd_size_type amt; | |
704 | struct elf_link_local_dynamic_entry *entry; | |
705 | struct elf_link_hash_table *eht; | |
706 | struct elf_strtab_hash *dynstr; | |
ef53be89 | 707 | size_t dynstr_index; |
8c58d23b AM |
708 | char *name; |
709 | Elf_External_Sym_Shndx eshndx; | |
710 | char esym[sizeof (Elf64_External_Sym)]; | |
711 | ||
0eddce27 | 712 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
713 | return 0; |
714 | ||
715 | /* See if the entry exists already. */ | |
716 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
717 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
718 | return 1; | |
719 | ||
720 | amt = sizeof (*entry); | |
a50b1753 | 721 | entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); |
8c58d23b AM |
722 | if (entry == NULL) |
723 | return 0; | |
724 | ||
725 | /* Go find the symbol, so that we can find it's name. */ | |
726 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 727 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
728 | { |
729 | bfd_release (input_bfd, entry); | |
730 | return 0; | |
731 | } | |
732 | ||
733 | if (entry->isym.st_shndx != SHN_UNDEF | |
4fbb74a6 | 734 | && entry->isym.st_shndx < SHN_LORESERVE) |
8c58d23b AM |
735 | { |
736 | asection *s; | |
737 | ||
738 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
739 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
740 | { | |
741 | /* We can still bfd_release here as nothing has done another | |
742 | bfd_alloc. We can't do this later in this function. */ | |
743 | bfd_release (input_bfd, entry); | |
744 | return 2; | |
745 | } | |
746 | } | |
747 | ||
748 | name = (bfd_elf_string_from_elf_section | |
749 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
750 | entry->isym.st_name)); | |
751 | ||
752 | dynstr = elf_hash_table (info)->dynstr; | |
753 | if (dynstr == NULL) | |
754 | { | |
755 | /* Create a strtab to hold the dynamic symbol names. */ | |
756 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
757 | if (dynstr == NULL) | |
758 | return 0; | |
759 | } | |
760 | ||
b34976b6 | 761 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
ef53be89 | 762 | if (dynstr_index == (size_t) -1) |
8c58d23b AM |
763 | return 0; |
764 | entry->isym.st_name = dynstr_index; | |
765 | ||
766 | eht = elf_hash_table (info); | |
767 | ||
768 | entry->next = eht->dynlocal; | |
769 | eht->dynlocal = entry; | |
770 | entry->input_bfd = input_bfd; | |
771 | entry->input_indx = input_indx; | |
772 | eht->dynsymcount++; | |
773 | ||
774 | /* Whatever binding the symbol had before, it's now local. */ | |
775 | entry->isym.st_info | |
776 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
777 | ||
778 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
779 | ||
780 | return 1; | |
781 | } | |
782 | ||
30b30c21 | 783 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 784 | |
30b30c21 | 785 | long |
268b6b39 AM |
786 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
787 | bfd *input_bfd, | |
788 | long input_indx) | |
30b30c21 RH |
789 | { |
790 | struct elf_link_local_dynamic_entry *e; | |
791 | ||
792 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
793 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
794 | return e->dynindx; | |
795 | return -1; | |
796 | } | |
797 | ||
798 | /* This function is used to renumber the dynamic symbols, if some of | |
799 | them are removed because they are marked as local. This is called | |
800 | via elf_link_hash_traverse. */ | |
801 | ||
b34976b6 | 802 | static bfd_boolean |
268b6b39 AM |
803 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
804 | void *data) | |
42751cf3 | 805 | { |
a50b1753 | 806 | size_t *count = (size_t *) data; |
30b30c21 | 807 | |
6fa3860b PB |
808 | if (h->forced_local) |
809 | return TRUE; | |
810 | ||
811 | if (h->dynindx != -1) | |
812 | h->dynindx = ++(*count); | |
813 | ||
814 | return TRUE; | |
815 | } | |
816 | ||
817 | ||
818 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
819 | STB_LOCAL binding. */ | |
820 | ||
821 | static bfd_boolean | |
822 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
823 | void *data) | |
824 | { | |
a50b1753 | 825 | size_t *count = (size_t *) data; |
6fa3860b | 826 | |
6fa3860b PB |
827 | if (!h->forced_local) |
828 | return TRUE; | |
829 | ||
42751cf3 | 830 | if (h->dynindx != -1) |
30b30c21 RH |
831 | h->dynindx = ++(*count); |
832 | ||
b34976b6 | 833 | return TRUE; |
42751cf3 | 834 | } |
30b30c21 | 835 | |
aee6f5b4 AO |
836 | /* Return true if the dynamic symbol for a given section should be |
837 | omitted when creating a shared library. */ | |
838 | bfd_boolean | |
839 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
840 | struct bfd_link_info *info, | |
841 | asection *p) | |
842 | { | |
74541ad4 | 843 | struct elf_link_hash_table *htab; |
ca55926c | 844 | asection *ip; |
74541ad4 | 845 | |
aee6f5b4 AO |
846 | switch (elf_section_data (p)->this_hdr.sh_type) |
847 | { | |
848 | case SHT_PROGBITS: | |
849 | case SHT_NOBITS: | |
850 | /* If sh_type is yet undecided, assume it could be | |
851 | SHT_PROGBITS/SHT_NOBITS. */ | |
852 | case SHT_NULL: | |
74541ad4 AM |
853 | htab = elf_hash_table (info); |
854 | if (p == htab->tls_sec) | |
855 | return FALSE; | |
856 | ||
857 | if (htab->text_index_section != NULL) | |
858 | return p != htab->text_index_section && p != htab->data_index_section; | |
859 | ||
ca55926c | 860 | return (htab->dynobj != NULL |
3d4d4302 | 861 | && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL |
ca55926c | 862 | && ip->output_section == p); |
aee6f5b4 AO |
863 | |
864 | /* There shouldn't be section relative relocations | |
865 | against any other section. */ | |
866 | default: | |
867 | return TRUE; | |
868 | } | |
869 | } | |
870 | ||
062e2358 | 871 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
872 | symbol for each output section, which come first. Next come symbols |
873 | which have been forced to local binding. Then all of the back-end | |
874 | allocated local dynamic syms, followed by the rest of the global | |
875 | symbols. */ | |
30b30c21 | 876 | |
554220db AM |
877 | static unsigned long |
878 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
879 | struct bfd_link_info *info, | |
880 | unsigned long *section_sym_count) | |
30b30c21 RH |
881 | { |
882 | unsigned long dynsymcount = 0; | |
883 | ||
0e1862bb L |
884 | if (bfd_link_pic (info) |
885 | || elf_hash_table (info)->is_relocatable_executable) | |
30b30c21 | 886 | { |
aee6f5b4 | 887 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
888 | asection *p; |
889 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 890 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
891 | && (p->flags & SEC_ALLOC) != 0 |
892 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
893 | elf_section_data (p)->dynindx = ++dynsymcount; | |
74541ad4 AM |
894 | else |
895 | elf_section_data (p)->dynindx = 0; | |
30b30c21 | 896 | } |
554220db | 897 | *section_sym_count = dynsymcount; |
30b30c21 | 898 | |
6fa3860b PB |
899 | elf_link_hash_traverse (elf_hash_table (info), |
900 | elf_link_renumber_local_hash_table_dynsyms, | |
901 | &dynsymcount); | |
902 | ||
30b30c21 RH |
903 | if (elf_hash_table (info)->dynlocal) |
904 | { | |
905 | struct elf_link_local_dynamic_entry *p; | |
906 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
907 | p->dynindx = ++dynsymcount; | |
908 | } | |
90ac2420 | 909 | elf_hash_table (info)->local_dynsymcount = dynsymcount; |
30b30c21 RH |
910 | |
911 | elf_link_hash_traverse (elf_hash_table (info), | |
912 | elf_link_renumber_hash_table_dynsyms, | |
913 | &dynsymcount); | |
914 | ||
d5486c43 L |
915 | /* There is an unused NULL entry at the head of the table which we |
916 | must account for in our count even if the table is empty since it | |
917 | is intended for the mandatory DT_SYMTAB tag (.dynsym section) in | |
918 | .dynamic section. */ | |
919 | dynsymcount++; | |
30b30c21 | 920 | |
ccabcbe5 AM |
921 | elf_hash_table (info)->dynsymcount = dynsymcount; |
922 | return dynsymcount; | |
30b30c21 | 923 | } |
252b5132 | 924 | |
54ac0771 L |
925 | /* Merge st_other field. */ |
926 | ||
927 | static void | |
928 | elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, | |
b8417128 | 929 | const Elf_Internal_Sym *isym, asection *sec, |
cd3416da | 930 | bfd_boolean definition, bfd_boolean dynamic) |
54ac0771 L |
931 | { |
932 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
933 | ||
934 | /* If st_other has a processor-specific meaning, specific | |
cd3416da | 935 | code might be needed here. */ |
54ac0771 L |
936 | if (bed->elf_backend_merge_symbol_attribute) |
937 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
938 | dynamic); | |
939 | ||
cd3416da | 940 | if (!dynamic) |
54ac0771 | 941 | { |
cd3416da AM |
942 | unsigned symvis = ELF_ST_VISIBILITY (isym->st_other); |
943 | unsigned hvis = ELF_ST_VISIBILITY (h->other); | |
54ac0771 | 944 | |
cd3416da AM |
945 | /* Keep the most constraining visibility. Leave the remainder |
946 | of the st_other field to elf_backend_merge_symbol_attribute. */ | |
947 | if (symvis - 1 < hvis - 1) | |
948 | h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); | |
54ac0771 | 949 | } |
b8417128 AM |
950 | else if (definition |
951 | && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT | |
952 | && (sec->flags & SEC_READONLY) == 0) | |
6cabe1ea | 953 | h->protected_def = 1; |
54ac0771 L |
954 | } |
955 | ||
4f3fedcf AM |
956 | /* This function is called when we want to merge a new symbol with an |
957 | existing symbol. It handles the various cases which arise when we | |
958 | find a definition in a dynamic object, or when there is already a | |
959 | definition in a dynamic object. The new symbol is described by | |
960 | NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table | |
961 | entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK | |
962 | if the old symbol was weak. We set POLD_ALIGNMENT to the alignment | |
963 | of an old common symbol. We set OVERRIDE if the old symbol is | |
964 | overriding a new definition. We set TYPE_CHANGE_OK if it is OK for | |
965 | the type to change. We set SIZE_CHANGE_OK if it is OK for the size | |
966 | to change. By OK to change, we mean that we shouldn't warn if the | |
967 | type or size does change. */ | |
45d6a902 | 968 | |
8a56bd02 | 969 | static bfd_boolean |
268b6b39 AM |
970 | _bfd_elf_merge_symbol (bfd *abfd, |
971 | struct bfd_link_info *info, | |
972 | const char *name, | |
973 | Elf_Internal_Sym *sym, | |
974 | asection **psec, | |
975 | bfd_vma *pvalue, | |
4f3fedcf AM |
976 | struct elf_link_hash_entry **sym_hash, |
977 | bfd **poldbfd, | |
37a9e49a | 978 | bfd_boolean *pold_weak, |
af44c138 | 979 | unsigned int *pold_alignment, |
268b6b39 AM |
980 | bfd_boolean *skip, |
981 | bfd_boolean *override, | |
982 | bfd_boolean *type_change_ok, | |
6e33951e L |
983 | bfd_boolean *size_change_ok, |
984 | bfd_boolean *matched) | |
252b5132 | 985 | { |
7479dfd4 | 986 | asection *sec, *oldsec; |
45d6a902 | 987 | struct elf_link_hash_entry *h; |
90c984fc | 988 | struct elf_link_hash_entry *hi; |
45d6a902 AM |
989 | struct elf_link_hash_entry *flip; |
990 | int bind; | |
991 | bfd *oldbfd; | |
992 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
0a36a439 | 993 | bfd_boolean newweak, oldweak, newfunc, oldfunc; |
a4d8e49b | 994 | const struct elf_backend_data *bed; |
6e33951e | 995 | char *new_version; |
45d6a902 AM |
996 | |
997 | *skip = FALSE; | |
998 | *override = FALSE; | |
999 | ||
1000 | sec = *psec; | |
1001 | bind = ELF_ST_BIND (sym->st_info); | |
1002 | ||
1003 | if (! bfd_is_und_section (sec)) | |
1004 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
1005 | else | |
1006 | h = ((struct elf_link_hash_entry *) | |
1007 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
1008 | if (h == NULL) | |
1009 | return FALSE; | |
1010 | *sym_hash = h; | |
252b5132 | 1011 | |
88ba32a0 L |
1012 | bed = get_elf_backend_data (abfd); |
1013 | ||
6e33951e | 1014 | /* NEW_VERSION is the symbol version of the new symbol. */ |
422f1182 | 1015 | if (h->versioned != unversioned) |
6e33951e | 1016 | { |
422f1182 L |
1017 | /* Symbol version is unknown or versioned. */ |
1018 | new_version = strrchr (name, ELF_VER_CHR); | |
1019 | if (new_version) | |
1020 | { | |
1021 | if (h->versioned == unknown) | |
1022 | { | |
1023 | if (new_version > name && new_version[-1] != ELF_VER_CHR) | |
1024 | h->versioned = versioned_hidden; | |
1025 | else | |
1026 | h->versioned = versioned; | |
1027 | } | |
1028 | new_version += 1; | |
1029 | if (new_version[0] == '\0') | |
1030 | new_version = NULL; | |
1031 | } | |
1032 | else | |
1033 | h->versioned = unversioned; | |
6e33951e | 1034 | } |
422f1182 L |
1035 | else |
1036 | new_version = NULL; | |
6e33951e | 1037 | |
90c984fc L |
1038 | /* For merging, we only care about real symbols. But we need to make |
1039 | sure that indirect symbol dynamic flags are updated. */ | |
1040 | hi = h; | |
45d6a902 AM |
1041 | while (h->root.type == bfd_link_hash_indirect |
1042 | || h->root.type == bfd_link_hash_warning) | |
1043 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1044 | ||
6e33951e L |
1045 | if (!*matched) |
1046 | { | |
1047 | if (hi == h || h->root.type == bfd_link_hash_new) | |
1048 | *matched = TRUE; | |
1049 | else | |
1050 | { | |
ae7683d2 | 1051 | /* OLD_HIDDEN is true if the existing symbol is only visible |
6e33951e | 1052 | to the symbol with the same symbol version. NEW_HIDDEN is |
ae7683d2 | 1053 | true if the new symbol is only visible to the symbol with |
6e33951e | 1054 | the same symbol version. */ |
422f1182 L |
1055 | bfd_boolean old_hidden = h->versioned == versioned_hidden; |
1056 | bfd_boolean new_hidden = hi->versioned == versioned_hidden; | |
6e33951e L |
1057 | if (!old_hidden && !new_hidden) |
1058 | /* The new symbol matches the existing symbol if both | |
1059 | aren't hidden. */ | |
1060 | *matched = TRUE; | |
1061 | else | |
1062 | { | |
1063 | /* OLD_VERSION is the symbol version of the existing | |
1064 | symbol. */ | |
422f1182 L |
1065 | char *old_version; |
1066 | ||
1067 | if (h->versioned >= versioned) | |
1068 | old_version = strrchr (h->root.root.string, | |
1069 | ELF_VER_CHR) + 1; | |
1070 | else | |
1071 | old_version = NULL; | |
6e33951e L |
1072 | |
1073 | /* The new symbol matches the existing symbol if they | |
1074 | have the same symbol version. */ | |
1075 | *matched = (old_version == new_version | |
1076 | || (old_version != NULL | |
1077 | && new_version != NULL | |
1078 | && strcmp (old_version, new_version) == 0)); | |
1079 | } | |
1080 | } | |
1081 | } | |
1082 | ||
934bce08 AM |
1083 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
1084 | existing symbol. */ | |
1085 | ||
1086 | oldbfd = NULL; | |
1087 | oldsec = NULL; | |
1088 | switch (h->root.type) | |
1089 | { | |
1090 | default: | |
1091 | break; | |
1092 | ||
1093 | case bfd_link_hash_undefined: | |
1094 | case bfd_link_hash_undefweak: | |
1095 | oldbfd = h->root.u.undef.abfd; | |
1096 | break; | |
1097 | ||
1098 | case bfd_link_hash_defined: | |
1099 | case bfd_link_hash_defweak: | |
1100 | oldbfd = h->root.u.def.section->owner; | |
1101 | oldsec = h->root.u.def.section; | |
1102 | break; | |
1103 | ||
1104 | case bfd_link_hash_common: | |
1105 | oldbfd = h->root.u.c.p->section->owner; | |
1106 | oldsec = h->root.u.c.p->section; | |
1107 | if (pold_alignment) | |
1108 | *pold_alignment = h->root.u.c.p->alignment_power; | |
1109 | break; | |
1110 | } | |
1111 | if (poldbfd && *poldbfd == NULL) | |
1112 | *poldbfd = oldbfd; | |
1113 | ||
1114 | /* Differentiate strong and weak symbols. */ | |
1115 | newweak = bind == STB_WEAK; | |
1116 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1117 | || h->root.type == bfd_link_hash_undefweak); | |
1118 | if (pold_weak) | |
1119 | *pold_weak = oldweak; | |
1120 | ||
1121 | /* This code is for coping with dynamic objects, and is only useful | |
1122 | if we are doing an ELF link. */ | |
1123 | if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
1124 | return TRUE; | |
1125 | ||
40b36307 | 1126 | /* We have to check it for every instance since the first few may be |
ee659f1f | 1127 | references and not all compilers emit symbol type for undefined |
40b36307 L |
1128 | symbols. */ |
1129 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
1130 | ||
ee659f1f AM |
1131 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, |
1132 | respectively, is from a dynamic object. */ | |
1133 | ||
1134 | newdyn = (abfd->flags & DYNAMIC) != 0; | |
1135 | ||
1136 | /* ref_dynamic_nonweak and dynamic_def flags track actual undefined | |
1137 | syms and defined syms in dynamic libraries respectively. | |
1138 | ref_dynamic on the other hand can be set for a symbol defined in | |
1139 | a dynamic library, and def_dynamic may not be set; When the | |
1140 | definition in a dynamic lib is overridden by a definition in the | |
1141 | executable use of the symbol in the dynamic lib becomes a | |
1142 | reference to the executable symbol. */ | |
1143 | if (newdyn) | |
1144 | { | |
1145 | if (bfd_is_und_section (sec)) | |
1146 | { | |
1147 | if (bind != STB_WEAK) | |
1148 | { | |
1149 | h->ref_dynamic_nonweak = 1; | |
1150 | hi->ref_dynamic_nonweak = 1; | |
1151 | } | |
1152 | } | |
1153 | else | |
1154 | { | |
6e33951e L |
1155 | /* Update the existing symbol only if they match. */ |
1156 | if (*matched) | |
1157 | h->dynamic_def = 1; | |
ee659f1f AM |
1158 | hi->dynamic_def = 1; |
1159 | } | |
1160 | } | |
1161 | ||
45d6a902 AM |
1162 | /* If we just created the symbol, mark it as being an ELF symbol. |
1163 | Other than that, there is nothing to do--there is no merge issue | |
1164 | with a newly defined symbol--so we just return. */ | |
1165 | ||
1166 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 1167 | { |
f5385ebf | 1168 | h->non_elf = 0; |
45d6a902 AM |
1169 | return TRUE; |
1170 | } | |
252b5132 | 1171 | |
45d6a902 AM |
1172 | /* In cases involving weak versioned symbols, we may wind up trying |
1173 | to merge a symbol with itself. Catch that here, to avoid the | |
1174 | confusion that results if we try to override a symbol with | |
1175 | itself. The additional tests catch cases like | |
1176 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
1177 | dynamic object, which we do want to handle here. */ | |
1178 | if (abfd == oldbfd | |
895fa45f | 1179 | && (newweak || oldweak) |
45d6a902 | 1180 | && ((abfd->flags & DYNAMIC) == 0 |
f5385ebf | 1181 | || !h->def_regular)) |
45d6a902 AM |
1182 | return TRUE; |
1183 | ||
707bba77 | 1184 | olddyn = FALSE; |
45d6a902 AM |
1185 | if (oldbfd != NULL) |
1186 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 1187 | else if (oldsec != NULL) |
45d6a902 | 1188 | { |
707bba77 | 1189 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 1190 | indices used by MIPS ELF. */ |
707bba77 | 1191 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 1192 | } |
252b5132 | 1193 | |
45d6a902 AM |
1194 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
1195 | respectively, appear to be a definition rather than reference. */ | |
1196 | ||
707bba77 | 1197 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 1198 | |
707bba77 AM |
1199 | olddef = (h->root.type != bfd_link_hash_undefined |
1200 | && h->root.type != bfd_link_hash_undefweak | |
202ac193 | 1201 | && h->root.type != bfd_link_hash_common); |
45d6a902 | 1202 | |
0a36a439 L |
1203 | /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, |
1204 | respectively, appear to be a function. */ | |
1205 | ||
1206 | newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1207 | && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); | |
1208 | ||
1209 | oldfunc = (h->type != STT_NOTYPE | |
1210 | && bed->is_function_type (h->type)); | |
1211 | ||
5b677558 AM |
1212 | /* If creating a default indirect symbol ("foo" or "foo@") from a |
1213 | dynamic versioned definition ("foo@@") skip doing so if there is | |
1214 | an existing regular definition with a different type. We don't | |
1215 | want, for example, a "time" variable in the executable overriding | |
1216 | a "time" function in a shared library. */ | |
580a2b6e | 1217 | if (pold_alignment == NULL |
580a2b6e L |
1218 | && newdyn |
1219 | && newdef | |
1220 | && !olddyn | |
5b677558 AM |
1221 | && (olddef || h->root.type == bfd_link_hash_common) |
1222 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1223 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1224 | && h->type != STT_NOTYPE | |
1225 | && !(newfunc && oldfunc)) | |
580a2b6e L |
1226 | { |
1227 | *skip = TRUE; | |
1228 | return TRUE; | |
1229 | } | |
1230 | ||
4c34aff8 AM |
1231 | /* Check TLS symbols. We don't check undefined symbols introduced |
1232 | by "ld -u" which have no type (and oldbfd NULL), and we don't | |
1233 | check symbols from plugins because they also have no type. */ | |
1234 | if (oldbfd != NULL | |
1235 | && (oldbfd->flags & BFD_PLUGIN) == 0 | |
1236 | && (abfd->flags & BFD_PLUGIN) == 0 | |
1237 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1238 | && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) | |
7479dfd4 L |
1239 | { |
1240 | bfd *ntbfd, *tbfd; | |
1241 | bfd_boolean ntdef, tdef; | |
1242 | asection *ntsec, *tsec; | |
1243 | ||
1244 | if (h->type == STT_TLS) | |
1245 | { | |
3b36f7e6 | 1246 | ntbfd = abfd; |
7479dfd4 L |
1247 | ntsec = sec; |
1248 | ntdef = newdef; | |
1249 | tbfd = oldbfd; | |
1250 | tsec = oldsec; | |
1251 | tdef = olddef; | |
1252 | } | |
1253 | else | |
1254 | { | |
1255 | ntbfd = oldbfd; | |
1256 | ntsec = oldsec; | |
1257 | ntdef = olddef; | |
1258 | tbfd = abfd; | |
1259 | tsec = sec; | |
1260 | tdef = newdef; | |
1261 | } | |
1262 | ||
1263 | if (tdef && ntdef) | |
4eca0228 | 1264 | _bfd_error_handler |
695344c0 | 1265 | /* xgettext:c-format */ |
191c0c42 AM |
1266 | (_("%s: TLS definition in %B section %A " |
1267 | "mismatches non-TLS definition in %B section %A"), | |
7479dfd4 L |
1268 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); |
1269 | else if (!tdef && !ntdef) | |
4eca0228 | 1270 | _bfd_error_handler |
695344c0 | 1271 | /* xgettext:c-format */ |
191c0c42 AM |
1272 | (_("%s: TLS reference in %B " |
1273 | "mismatches non-TLS reference in %B"), | |
7479dfd4 L |
1274 | tbfd, ntbfd, h->root.root.string); |
1275 | else if (tdef) | |
4eca0228 | 1276 | _bfd_error_handler |
695344c0 | 1277 | /* xgettext:c-format */ |
191c0c42 AM |
1278 | (_("%s: TLS definition in %B section %A " |
1279 | "mismatches non-TLS reference in %B"), | |
7479dfd4 L |
1280 | tbfd, tsec, ntbfd, h->root.root.string); |
1281 | else | |
4eca0228 | 1282 | _bfd_error_handler |
695344c0 | 1283 | /* xgettext:c-format */ |
191c0c42 AM |
1284 | (_("%s: TLS reference in %B " |
1285 | "mismatches non-TLS definition in %B section %A"), | |
7479dfd4 L |
1286 | tbfd, ntbfd, ntsec, h->root.root.string); |
1287 | ||
1288 | bfd_set_error (bfd_error_bad_value); | |
1289 | return FALSE; | |
1290 | } | |
1291 | ||
45d6a902 AM |
1292 | /* If the old symbol has non-default visibility, we ignore the new |
1293 | definition from a dynamic object. */ | |
1294 | if (newdyn | |
9c7a29a3 | 1295 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1296 | && !bfd_is_und_section (sec)) |
1297 | { | |
1298 | *skip = TRUE; | |
1299 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1300 | h->ref_dynamic = 1; |
90c984fc | 1301 | hi->ref_dynamic = 1; |
45d6a902 AM |
1302 | /* A protected symbol has external availability. Make sure it is |
1303 | recorded as dynamic. | |
1304 | ||
1305 | FIXME: Should we check type and size for protected symbol? */ | |
1306 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1307 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1308 | else |
1309 | return TRUE; | |
1310 | } | |
1311 | else if (!newdyn | |
9c7a29a3 | 1312 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1313 | && h->def_dynamic) |
45d6a902 AM |
1314 | { |
1315 | /* If the new symbol with non-default visibility comes from a | |
1316 | relocatable file and the old definition comes from a dynamic | |
1317 | object, we remove the old definition. */ | |
6c9b78e6 | 1318 | if (hi->root.type == bfd_link_hash_indirect) |
d2dee3b2 L |
1319 | { |
1320 | /* Handle the case where the old dynamic definition is | |
1321 | default versioned. We need to copy the symbol info from | |
1322 | the symbol with default version to the normal one if it | |
1323 | was referenced before. */ | |
1324 | if (h->ref_regular) | |
1325 | { | |
6c9b78e6 | 1326 | hi->root.type = h->root.type; |
d2dee3b2 | 1327 | h->root.type = bfd_link_hash_indirect; |
6c9b78e6 | 1328 | (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); |
aed81c4e | 1329 | |
6c9b78e6 | 1330 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; |
aed81c4e | 1331 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
d2dee3b2 | 1332 | { |
aed81c4e MR |
1333 | /* If the new symbol is hidden or internal, completely undo |
1334 | any dynamic link state. */ | |
1335 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1336 | h->forced_local = 0; | |
1337 | h->ref_dynamic = 0; | |
d2dee3b2 L |
1338 | } |
1339 | else | |
aed81c4e MR |
1340 | h->ref_dynamic = 1; |
1341 | ||
1342 | h->def_dynamic = 0; | |
aed81c4e MR |
1343 | /* FIXME: Should we check type and size for protected symbol? */ |
1344 | h->size = 0; | |
1345 | h->type = 0; | |
1346 | ||
6c9b78e6 | 1347 | h = hi; |
d2dee3b2 L |
1348 | } |
1349 | else | |
6c9b78e6 | 1350 | h = hi; |
d2dee3b2 | 1351 | } |
1de1a317 | 1352 | |
f5eda473 AM |
1353 | /* If the old symbol was undefined before, then it will still be |
1354 | on the undefs list. If the new symbol is undefined or | |
1355 | common, we can't make it bfd_link_hash_new here, because new | |
1356 | undefined or common symbols will be added to the undefs list | |
1357 | by _bfd_generic_link_add_one_symbol. Symbols may not be | |
1358 | added twice to the undefs list. Also, if the new symbol is | |
1359 | undefweak then we don't want to lose the strong undef. */ | |
1360 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1de1a317 | 1361 | { |
1de1a317 | 1362 | h->root.type = bfd_link_hash_undefined; |
1de1a317 L |
1363 | h->root.u.undef.abfd = abfd; |
1364 | } | |
1365 | else | |
1366 | { | |
1367 | h->root.type = bfd_link_hash_new; | |
1368 | h->root.u.undef.abfd = NULL; | |
1369 | } | |
1370 | ||
f5eda473 | 1371 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
252b5132 | 1372 | { |
f5eda473 AM |
1373 | /* If the new symbol is hidden or internal, completely undo |
1374 | any dynamic link state. */ | |
1375 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1376 | h->forced_local = 0; | |
1377 | h->ref_dynamic = 0; | |
45d6a902 | 1378 | } |
f5eda473 AM |
1379 | else |
1380 | h->ref_dynamic = 1; | |
1381 | h->def_dynamic = 0; | |
45d6a902 AM |
1382 | /* FIXME: Should we check type and size for protected symbol? */ |
1383 | h->size = 0; | |
1384 | h->type = 0; | |
1385 | return TRUE; | |
1386 | } | |
14a793b2 | 1387 | |
15b43f48 AM |
1388 | /* If a new weak symbol definition comes from a regular file and the |
1389 | old symbol comes from a dynamic library, we treat the new one as | |
1390 | strong. Similarly, an old weak symbol definition from a regular | |
1391 | file is treated as strong when the new symbol comes from a dynamic | |
1392 | library. Further, an old weak symbol from a dynamic library is | |
1393 | treated as strong if the new symbol is from a dynamic library. | |
1394 | This reflects the way glibc's ld.so works. | |
1395 | ||
1396 | Do this before setting *type_change_ok or *size_change_ok so that | |
1397 | we warn properly when dynamic library symbols are overridden. */ | |
1398 | ||
1399 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1400 | newweak = FALSE; |
15b43f48 | 1401 | if (olddef && newdyn) |
0f8a2703 AM |
1402 | oldweak = FALSE; |
1403 | ||
d334575b | 1404 | /* Allow changes between different types of function symbol. */ |
0a36a439 | 1405 | if (newfunc && oldfunc) |
fcb93ecf PB |
1406 | *type_change_ok = TRUE; |
1407 | ||
79349b09 AM |
1408 | /* It's OK to change the type if either the existing symbol or the |
1409 | new symbol is weak. A type change is also OK if the old symbol | |
1410 | is undefined and the new symbol is defined. */ | |
252b5132 | 1411 | |
79349b09 AM |
1412 | if (oldweak |
1413 | || newweak | |
1414 | || (newdef | |
1415 | && h->root.type == bfd_link_hash_undefined)) | |
1416 | *type_change_ok = TRUE; | |
1417 | ||
1418 | /* It's OK to change the size if either the existing symbol or the | |
1419 | new symbol is weak, or if the old symbol is undefined. */ | |
1420 | ||
1421 | if (*type_change_ok | |
1422 | || h->root.type == bfd_link_hash_undefined) | |
1423 | *size_change_ok = TRUE; | |
45d6a902 | 1424 | |
45d6a902 AM |
1425 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1426 | symbol, respectively, appears to be a common symbol in a dynamic | |
1427 | object. If a symbol appears in an uninitialized section, and is | |
1428 | not weak, and is not a function, then it may be a common symbol | |
1429 | which was resolved when the dynamic object was created. We want | |
1430 | to treat such symbols specially, because they raise special | |
1431 | considerations when setting the symbol size: if the symbol | |
1432 | appears as a common symbol in a regular object, and the size in | |
1433 | the regular object is larger, we must make sure that we use the | |
1434 | larger size. This problematic case can always be avoided in C, | |
1435 | but it must be handled correctly when using Fortran shared | |
1436 | libraries. | |
1437 | ||
1438 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1439 | likewise for OLDDYNCOMMON and OLDDEF. | |
1440 | ||
1441 | Note that this test is just a heuristic, and that it is quite | |
1442 | possible to have an uninitialized symbol in a shared object which | |
1443 | is really a definition, rather than a common symbol. This could | |
1444 | lead to some minor confusion when the symbol really is a common | |
1445 | symbol in some regular object. However, I think it will be | |
1446 | harmless. */ | |
1447 | ||
1448 | if (newdyn | |
1449 | && newdef | |
79349b09 | 1450 | && !newweak |
45d6a902 AM |
1451 | && (sec->flags & SEC_ALLOC) != 0 |
1452 | && (sec->flags & SEC_LOAD) == 0 | |
1453 | && sym->st_size > 0 | |
0a36a439 | 1454 | && !newfunc) |
45d6a902 AM |
1455 | newdyncommon = TRUE; |
1456 | else | |
1457 | newdyncommon = FALSE; | |
1458 | ||
1459 | if (olddyn | |
1460 | && olddef | |
1461 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1462 | && h->def_dynamic |
45d6a902 AM |
1463 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1464 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1465 | && h->size > 0 | |
0a36a439 | 1466 | && !oldfunc) |
45d6a902 AM |
1467 | olddyncommon = TRUE; |
1468 | else | |
1469 | olddyncommon = FALSE; | |
1470 | ||
a4d8e49b L |
1471 | /* We now know everything about the old and new symbols. We ask the |
1472 | backend to check if we can merge them. */ | |
5d13b3b3 AM |
1473 | if (bed->merge_symbol != NULL) |
1474 | { | |
1475 | if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) | |
1476 | return FALSE; | |
1477 | sec = *psec; | |
1478 | } | |
a4d8e49b | 1479 | |
45d6a902 AM |
1480 | /* If both the old and the new symbols look like common symbols in a |
1481 | dynamic object, set the size of the symbol to the larger of the | |
1482 | two. */ | |
1483 | ||
1484 | if (olddyncommon | |
1485 | && newdyncommon | |
1486 | && sym->st_size != h->size) | |
1487 | { | |
1488 | /* Since we think we have two common symbols, issue a multiple | |
1489 | common warning if desired. Note that we only warn if the | |
1490 | size is different. If the size is the same, we simply let | |
1491 | the old symbol override the new one as normally happens with | |
1492 | symbols defined in dynamic objects. */ | |
1493 | ||
1a72702b AM |
1494 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1495 | bfd_link_hash_common, sym->st_size); | |
45d6a902 AM |
1496 | if (sym->st_size > h->size) |
1497 | h->size = sym->st_size; | |
252b5132 | 1498 | |
45d6a902 | 1499 | *size_change_ok = TRUE; |
252b5132 RH |
1500 | } |
1501 | ||
45d6a902 AM |
1502 | /* If we are looking at a dynamic object, and we have found a |
1503 | definition, we need to see if the symbol was already defined by | |
1504 | some other object. If so, we want to use the existing | |
1505 | definition, and we do not want to report a multiple symbol | |
1506 | definition error; we do this by clobbering *PSEC to be | |
1507 | bfd_und_section_ptr. | |
1508 | ||
1509 | We treat a common symbol as a definition if the symbol in the | |
1510 | shared library is a function, since common symbols always | |
1511 | represent variables; this can cause confusion in principle, but | |
1512 | any such confusion would seem to indicate an erroneous program or | |
1513 | shared library. We also permit a common symbol in a regular | |
202ac193 L |
1514 | object to override a weak symbol in a shared object. A common |
1515 | symbol in executable also overrides a symbol in a shared object. */ | |
45d6a902 AM |
1516 | |
1517 | if (newdyn | |
1518 | && newdef | |
77cfaee6 | 1519 | && (olddef |
45d6a902 | 1520 | || (h->root.type == bfd_link_hash_common |
202ac193 L |
1521 | && (newweak |
1522 | || newfunc | |
1523 | || (!olddyn && bfd_link_executable (info)))))) | |
45d6a902 AM |
1524 | { |
1525 | *override = TRUE; | |
1526 | newdef = FALSE; | |
1527 | newdyncommon = FALSE; | |
252b5132 | 1528 | |
45d6a902 AM |
1529 | *psec = sec = bfd_und_section_ptr; |
1530 | *size_change_ok = TRUE; | |
252b5132 | 1531 | |
45d6a902 AM |
1532 | /* If we get here when the old symbol is a common symbol, then |
1533 | we are explicitly letting it override a weak symbol or | |
1534 | function in a dynamic object, and we don't want to warn about | |
1535 | a type change. If the old symbol is a defined symbol, a type | |
1536 | change warning may still be appropriate. */ | |
252b5132 | 1537 | |
45d6a902 AM |
1538 | if (h->root.type == bfd_link_hash_common) |
1539 | *type_change_ok = TRUE; | |
1540 | } | |
1541 | ||
1542 | /* Handle the special case of an old common symbol merging with a | |
1543 | new symbol which looks like a common symbol in a shared object. | |
1544 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1545 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1546 | right thing. */ | |
45d6a902 AM |
1547 | |
1548 | if (newdyncommon | |
1549 | && h->root.type == bfd_link_hash_common) | |
1550 | { | |
1551 | *override = TRUE; | |
1552 | newdef = FALSE; | |
1553 | newdyncommon = FALSE; | |
1554 | *pvalue = sym->st_size; | |
a4d8e49b | 1555 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1556 | *size_change_ok = TRUE; |
1557 | } | |
1558 | ||
c5e2cead | 1559 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1560 | if (newdef && olddef && newweak) |
54ac0771 | 1561 | { |
35ed3f94 | 1562 | /* Don't skip new non-IR weak syms. */ |
3a5dbfb2 AM |
1563 | if (!(oldbfd != NULL |
1564 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
35ed3f94 | 1565 | && (abfd->flags & BFD_PLUGIN) == 0)) |
57fa7b8c AM |
1566 | { |
1567 | newdef = FALSE; | |
1568 | *skip = TRUE; | |
1569 | } | |
54ac0771 L |
1570 | |
1571 | /* Merge st_other. If the symbol already has a dynamic index, | |
1572 | but visibility says it should not be visible, turn it into a | |
1573 | local symbol. */ | |
b8417128 | 1574 | elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn); |
54ac0771 L |
1575 | if (h->dynindx != -1) |
1576 | switch (ELF_ST_VISIBILITY (h->other)) | |
1577 | { | |
1578 | case STV_INTERNAL: | |
1579 | case STV_HIDDEN: | |
1580 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1581 | break; | |
1582 | } | |
1583 | } | |
c5e2cead | 1584 | |
45d6a902 AM |
1585 | /* If the old symbol is from a dynamic object, and the new symbol is |
1586 | a definition which is not from a dynamic object, then the new | |
1587 | symbol overrides the old symbol. Symbols from regular files | |
1588 | always take precedence over symbols from dynamic objects, even if | |
1589 | they are defined after the dynamic object in the link. | |
1590 | ||
1591 | As above, we again permit a common symbol in a regular object to | |
1592 | override a definition in a shared object if the shared object | |
0f8a2703 | 1593 | symbol is a function or is weak. */ |
45d6a902 AM |
1594 | |
1595 | flip = NULL; | |
77cfaee6 | 1596 | if (!newdyn |
45d6a902 AM |
1597 | && (newdef |
1598 | || (bfd_is_com_section (sec) | |
0a36a439 | 1599 | && (oldweak || oldfunc))) |
45d6a902 AM |
1600 | && olddyn |
1601 | && olddef | |
f5385ebf | 1602 | && h->def_dynamic) |
45d6a902 AM |
1603 | { |
1604 | /* Change the hash table entry to undefined, and let | |
1605 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1606 | new definition. */ | |
1607 | ||
1608 | h->root.type = bfd_link_hash_undefined; | |
1609 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1610 | *size_change_ok = TRUE; | |
1611 | ||
1612 | olddef = FALSE; | |
1613 | olddyncommon = FALSE; | |
1614 | ||
1615 | /* We again permit a type change when a common symbol may be | |
1616 | overriding a function. */ | |
1617 | ||
1618 | if (bfd_is_com_section (sec)) | |
0a36a439 L |
1619 | { |
1620 | if (oldfunc) | |
1621 | { | |
1622 | /* If a common symbol overrides a function, make sure | |
1623 | that it isn't defined dynamically nor has type | |
1624 | function. */ | |
1625 | h->def_dynamic = 0; | |
1626 | h->type = STT_NOTYPE; | |
1627 | } | |
1628 | *type_change_ok = TRUE; | |
1629 | } | |
45d6a902 | 1630 | |
6c9b78e6 AM |
1631 | if (hi->root.type == bfd_link_hash_indirect) |
1632 | flip = hi; | |
45d6a902 AM |
1633 | else |
1634 | /* This union may have been set to be non-NULL when this symbol | |
1635 | was seen in a dynamic object. We must force the union to be | |
1636 | NULL, so that it is correct for a regular symbol. */ | |
1637 | h->verinfo.vertree = NULL; | |
1638 | } | |
1639 | ||
1640 | /* Handle the special case of a new common symbol merging with an | |
1641 | old symbol that looks like it might be a common symbol defined in | |
1642 | a shared object. Note that we have already handled the case in | |
1643 | which a new common symbol should simply override the definition | |
1644 | in the shared library. */ | |
1645 | ||
1646 | if (! newdyn | |
1647 | && bfd_is_com_section (sec) | |
1648 | && olddyncommon) | |
1649 | { | |
1650 | /* It would be best if we could set the hash table entry to a | |
1651 | common symbol, but we don't know what to use for the section | |
1652 | or the alignment. */ | |
1a72702b AM |
1653 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1654 | bfd_link_hash_common, sym->st_size); | |
45d6a902 | 1655 | |
4cc11e76 | 1656 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1657 | larger, pretend that the new symbol has its size. */ |
1658 | ||
1659 | if (h->size > *pvalue) | |
1660 | *pvalue = h->size; | |
1661 | ||
af44c138 L |
1662 | /* We need to remember the alignment required by the symbol |
1663 | in the dynamic object. */ | |
1664 | BFD_ASSERT (pold_alignment); | |
1665 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1666 | |
1667 | olddef = FALSE; | |
1668 | olddyncommon = FALSE; | |
1669 | ||
1670 | h->root.type = bfd_link_hash_undefined; | |
1671 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1672 | ||
1673 | *size_change_ok = TRUE; | |
1674 | *type_change_ok = TRUE; | |
1675 | ||
6c9b78e6 AM |
1676 | if (hi->root.type == bfd_link_hash_indirect) |
1677 | flip = hi; | |
45d6a902 AM |
1678 | else |
1679 | h->verinfo.vertree = NULL; | |
1680 | } | |
1681 | ||
1682 | if (flip != NULL) | |
1683 | { | |
1684 | /* Handle the case where we had a versioned symbol in a dynamic | |
1685 | library and now find a definition in a normal object. In this | |
1686 | case, we make the versioned symbol point to the normal one. */ | |
45d6a902 | 1687 | flip->root.type = h->root.type; |
00cbee0a | 1688 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1689 | h->root.type = bfd_link_hash_indirect; |
1690 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1691 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1692 | if (h->def_dynamic) |
45d6a902 | 1693 | { |
f5385ebf AM |
1694 | h->def_dynamic = 0; |
1695 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1696 | } |
1697 | } | |
1698 | ||
45d6a902 AM |
1699 | return TRUE; |
1700 | } | |
1701 | ||
1702 | /* This function is called to create an indirect symbol from the | |
1703 | default for the symbol with the default version if needed. The | |
4f3fedcf | 1704 | symbol is described by H, NAME, SYM, SEC, and VALUE. We |
0f8a2703 | 1705 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 | 1706 | |
28caa186 | 1707 | static bfd_boolean |
268b6b39 AM |
1708 | _bfd_elf_add_default_symbol (bfd *abfd, |
1709 | struct bfd_link_info *info, | |
1710 | struct elf_link_hash_entry *h, | |
1711 | const char *name, | |
1712 | Elf_Internal_Sym *sym, | |
4f3fedcf AM |
1713 | asection *sec, |
1714 | bfd_vma value, | |
1715 | bfd **poldbfd, | |
e3c9d234 | 1716 | bfd_boolean *dynsym) |
45d6a902 AM |
1717 | { |
1718 | bfd_boolean type_change_ok; | |
1719 | bfd_boolean size_change_ok; | |
1720 | bfd_boolean skip; | |
1721 | char *shortname; | |
1722 | struct elf_link_hash_entry *hi; | |
1723 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1724 | const struct elf_backend_data *bed; |
45d6a902 AM |
1725 | bfd_boolean collect; |
1726 | bfd_boolean dynamic; | |
e3c9d234 | 1727 | bfd_boolean override; |
45d6a902 AM |
1728 | char *p; |
1729 | size_t len, shortlen; | |
ffd65175 | 1730 | asection *tmp_sec; |
6e33951e | 1731 | bfd_boolean matched; |
45d6a902 | 1732 | |
422f1182 L |
1733 | if (h->versioned == unversioned || h->versioned == versioned_hidden) |
1734 | return TRUE; | |
1735 | ||
45d6a902 AM |
1736 | /* If this symbol has a version, and it is the default version, we |
1737 | create an indirect symbol from the default name to the fully | |
1738 | decorated name. This will cause external references which do not | |
1739 | specify a version to be bound to this version of the symbol. */ | |
1740 | p = strchr (name, ELF_VER_CHR); | |
422f1182 L |
1741 | if (h->versioned == unknown) |
1742 | { | |
1743 | if (p == NULL) | |
1744 | { | |
1745 | h->versioned = unversioned; | |
1746 | return TRUE; | |
1747 | } | |
1748 | else | |
1749 | { | |
1750 | if (p[1] != ELF_VER_CHR) | |
1751 | { | |
1752 | h->versioned = versioned_hidden; | |
1753 | return TRUE; | |
1754 | } | |
1755 | else | |
1756 | h->versioned = versioned; | |
1757 | } | |
1758 | } | |
4373f8af L |
1759 | else |
1760 | { | |
1761 | /* PR ld/19073: We may see an unversioned definition after the | |
1762 | default version. */ | |
1763 | if (p == NULL) | |
1764 | return TRUE; | |
1765 | } | |
45d6a902 | 1766 | |
45d6a902 AM |
1767 | bed = get_elf_backend_data (abfd); |
1768 | collect = bed->collect; | |
1769 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1770 | ||
1771 | shortlen = p - name; | |
a50b1753 | 1772 | shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); |
45d6a902 AM |
1773 | if (shortname == NULL) |
1774 | return FALSE; | |
1775 | memcpy (shortname, name, shortlen); | |
1776 | shortname[shortlen] = '\0'; | |
1777 | ||
1778 | /* We are going to create a new symbol. Merge it with any existing | |
1779 | symbol with this name. For the purposes of the merge, act as | |
1780 | though we were defining the symbol we just defined, although we | |
1781 | actually going to define an indirect symbol. */ | |
1782 | type_change_ok = FALSE; | |
1783 | size_change_ok = FALSE; | |
6e33951e | 1784 | matched = TRUE; |
ffd65175 AM |
1785 | tmp_sec = sec; |
1786 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
4f3fedcf | 1787 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 1788 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
1789 | return FALSE; |
1790 | ||
1791 | if (skip) | |
1792 | goto nondefault; | |
1793 | ||
5b677558 AM |
1794 | if (hi->def_regular) |
1795 | { | |
1796 | /* If the undecorated symbol will have a version added by a | |
1797 | script different to H, then don't indirect to/from the | |
1798 | undecorated symbol. This isn't ideal because we may not yet | |
1799 | have seen symbol versions, if given by a script on the | |
1800 | command line rather than via --version-script. */ | |
1801 | if (hi->verinfo.vertree == NULL && info->version_info != NULL) | |
1802 | { | |
1803 | bfd_boolean hide; | |
1804 | ||
1805 | hi->verinfo.vertree | |
1806 | = bfd_find_version_for_sym (info->version_info, | |
1807 | hi->root.root.string, &hide); | |
1808 | if (hi->verinfo.vertree != NULL && hide) | |
1809 | { | |
1810 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
1811 | goto nondefault; | |
1812 | } | |
1813 | } | |
1814 | if (hi->verinfo.vertree != NULL | |
1815 | && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0) | |
1816 | goto nondefault; | |
1817 | } | |
1818 | ||
45d6a902 AM |
1819 | if (! override) |
1820 | { | |
c6e8a9a8 | 1821 | /* Add the default symbol if not performing a relocatable link. */ |
0e1862bb | 1822 | if (! bfd_link_relocatable (info)) |
c6e8a9a8 L |
1823 | { |
1824 | bh = &hi->root; | |
1825 | if (! (_bfd_generic_link_add_one_symbol | |
1826 | (info, abfd, shortname, BSF_INDIRECT, | |
1827 | bfd_ind_section_ptr, | |
1828 | 0, name, FALSE, collect, &bh))) | |
1829 | return FALSE; | |
1830 | hi = (struct elf_link_hash_entry *) bh; | |
1831 | } | |
45d6a902 AM |
1832 | } |
1833 | else | |
1834 | { | |
1835 | /* In this case the symbol named SHORTNAME is overriding the | |
1836 | indirect symbol we want to add. We were planning on making | |
1837 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1838 | is the name without a version. NAME is the fully versioned | |
1839 | name, and it is the default version. | |
1840 | ||
1841 | Overriding means that we already saw a definition for the | |
1842 | symbol SHORTNAME in a regular object, and it is overriding | |
1843 | the symbol defined in the dynamic object. | |
1844 | ||
1845 | When this happens, we actually want to change NAME, the | |
1846 | symbol we just added, to refer to SHORTNAME. This will cause | |
1847 | references to NAME in the shared object to become references | |
1848 | to SHORTNAME in the regular object. This is what we expect | |
1849 | when we override a function in a shared object: that the | |
1850 | references in the shared object will be mapped to the | |
1851 | definition in the regular object. */ | |
1852 | ||
1853 | while (hi->root.type == bfd_link_hash_indirect | |
1854 | || hi->root.type == bfd_link_hash_warning) | |
1855 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1856 | ||
1857 | h->root.type = bfd_link_hash_indirect; | |
1858 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1859 | if (h->def_dynamic) |
45d6a902 | 1860 | { |
f5385ebf AM |
1861 | h->def_dynamic = 0; |
1862 | hi->ref_dynamic = 1; | |
1863 | if (hi->ref_regular | |
1864 | || hi->def_regular) | |
45d6a902 | 1865 | { |
c152c796 | 1866 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1867 | return FALSE; |
1868 | } | |
1869 | } | |
1870 | ||
1871 | /* Now set HI to H, so that the following code will set the | |
1872 | other fields correctly. */ | |
1873 | hi = h; | |
1874 | } | |
1875 | ||
fab4a87f L |
1876 | /* Check if HI is a warning symbol. */ |
1877 | if (hi->root.type == bfd_link_hash_warning) | |
1878 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1879 | ||
45d6a902 AM |
1880 | /* If there is a duplicate definition somewhere, then HI may not |
1881 | point to an indirect symbol. We will have reported an error to | |
1882 | the user in that case. */ | |
1883 | ||
1884 | if (hi->root.type == bfd_link_hash_indirect) | |
1885 | { | |
1886 | struct elf_link_hash_entry *ht; | |
1887 | ||
45d6a902 | 1888 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 1889 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 | 1890 | |
68c88cd4 AM |
1891 | /* A reference to the SHORTNAME symbol from a dynamic library |
1892 | will be satisfied by the versioned symbol at runtime. In | |
1893 | effect, we have a reference to the versioned symbol. */ | |
1894 | ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; | |
1895 | hi->dynamic_def |= ht->dynamic_def; | |
1896 | ||
45d6a902 AM |
1897 | /* See if the new flags lead us to realize that the symbol must |
1898 | be dynamic. */ | |
1899 | if (! *dynsym) | |
1900 | { | |
1901 | if (! dynamic) | |
1902 | { | |
0e1862bb | 1903 | if (! bfd_link_executable (info) |
90c984fc | 1904 | || hi->def_dynamic |
f5385ebf | 1905 | || hi->ref_dynamic) |
45d6a902 AM |
1906 | *dynsym = TRUE; |
1907 | } | |
1908 | else | |
1909 | { | |
f5385ebf | 1910 | if (hi->ref_regular) |
45d6a902 AM |
1911 | *dynsym = TRUE; |
1912 | } | |
1913 | } | |
1914 | } | |
1915 | ||
1916 | /* We also need to define an indirection from the nondefault version | |
1917 | of the symbol. */ | |
1918 | ||
1919 | nondefault: | |
1920 | len = strlen (name); | |
a50b1753 | 1921 | shortname = (char *) bfd_hash_allocate (&info->hash->table, len); |
45d6a902 AM |
1922 | if (shortname == NULL) |
1923 | return FALSE; | |
1924 | memcpy (shortname, name, shortlen); | |
1925 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1926 | ||
1927 | /* Once again, merge with any existing symbol. */ | |
1928 | type_change_ok = FALSE; | |
1929 | size_change_ok = FALSE; | |
ffd65175 AM |
1930 | tmp_sec = sec; |
1931 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
115c6d5c | 1932 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 1933 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
1934 | return FALSE; |
1935 | ||
1936 | if (skip) | |
1937 | return TRUE; | |
1938 | ||
1939 | if (override) | |
1940 | { | |
1941 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1942 | the type of override we do in the case above unless it is | |
4cc11e76 | 1943 | overridden by a versioned definition. */ |
45d6a902 AM |
1944 | if (hi->root.type != bfd_link_hash_defined |
1945 | && hi->root.type != bfd_link_hash_defweak) | |
4eca0228 | 1946 | _bfd_error_handler |
695344c0 | 1947 | /* xgettext:c-format */ |
d003868e AM |
1948 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1949 | abfd, shortname); | |
45d6a902 AM |
1950 | } |
1951 | else | |
1952 | { | |
1953 | bh = &hi->root; | |
1954 | if (! (_bfd_generic_link_add_one_symbol | |
1955 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1956 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1957 | return FALSE; |
1958 | hi = (struct elf_link_hash_entry *) bh; | |
1959 | ||
1960 | /* If there is a duplicate definition somewhere, then HI may not | |
1961 | point to an indirect symbol. We will have reported an error | |
1962 | to the user in that case. */ | |
1963 | ||
1964 | if (hi->root.type == bfd_link_hash_indirect) | |
1965 | { | |
fcfa13d2 | 1966 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
68c88cd4 AM |
1967 | h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; |
1968 | hi->dynamic_def |= h->dynamic_def; | |
45d6a902 AM |
1969 | |
1970 | /* See if the new flags lead us to realize that the symbol | |
1971 | must be dynamic. */ | |
1972 | if (! *dynsym) | |
1973 | { | |
1974 | if (! dynamic) | |
1975 | { | |
0e1862bb | 1976 | if (! bfd_link_executable (info) |
f5385ebf | 1977 | || hi->ref_dynamic) |
45d6a902 AM |
1978 | *dynsym = TRUE; |
1979 | } | |
1980 | else | |
1981 | { | |
f5385ebf | 1982 | if (hi->ref_regular) |
45d6a902 AM |
1983 | *dynsym = TRUE; |
1984 | } | |
1985 | } | |
1986 | } | |
1987 | } | |
1988 | ||
1989 | return TRUE; | |
1990 | } | |
1991 | \f | |
1992 | /* This routine is used to export all defined symbols into the dynamic | |
1993 | symbol table. It is called via elf_link_hash_traverse. */ | |
1994 | ||
28caa186 | 1995 | static bfd_boolean |
268b6b39 | 1996 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1997 | { |
a50b1753 | 1998 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 AM |
1999 | |
2000 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2001 | if (h->root.type == bfd_link_hash_indirect) | |
2002 | return TRUE; | |
2003 | ||
7686d77d AM |
2004 | /* Ignore this if we won't export it. */ |
2005 | if (!eif->info->export_dynamic && !h->dynamic) | |
2006 | return TRUE; | |
45d6a902 AM |
2007 | |
2008 | if (h->dynindx == -1 | |
fd91d419 L |
2009 | && (h->def_regular || h->ref_regular) |
2010 | && ! bfd_hide_sym_by_version (eif->info->version_info, | |
2011 | h->root.root.string)) | |
45d6a902 | 2012 | { |
fd91d419 | 2013 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 | 2014 | { |
fd91d419 L |
2015 | eif->failed = TRUE; |
2016 | return FALSE; | |
45d6a902 AM |
2017 | } |
2018 | } | |
2019 | ||
2020 | return TRUE; | |
2021 | } | |
2022 | \f | |
2023 | /* Look through the symbols which are defined in other shared | |
2024 | libraries and referenced here. Update the list of version | |
2025 | dependencies. This will be put into the .gnu.version_r section. | |
2026 | This function is called via elf_link_hash_traverse. */ | |
2027 | ||
28caa186 | 2028 | static bfd_boolean |
268b6b39 AM |
2029 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
2030 | void *data) | |
45d6a902 | 2031 | { |
a50b1753 | 2032 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; |
45d6a902 AM |
2033 | Elf_Internal_Verneed *t; |
2034 | Elf_Internal_Vernaux *a; | |
2035 | bfd_size_type amt; | |
2036 | ||
45d6a902 AM |
2037 | /* We only care about symbols defined in shared objects with version |
2038 | information. */ | |
f5385ebf AM |
2039 | if (!h->def_dynamic |
2040 | || h->def_regular | |
45d6a902 | 2041 | || h->dynindx == -1 |
7b20f099 AM |
2042 | || h->verinfo.verdef == NULL |
2043 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
2044 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
45d6a902 AM |
2045 | return TRUE; |
2046 | ||
2047 | /* See if we already know about this version. */ | |
28caa186 AM |
2048 | for (t = elf_tdata (rinfo->info->output_bfd)->verref; |
2049 | t != NULL; | |
2050 | t = t->vn_nextref) | |
45d6a902 AM |
2051 | { |
2052 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
2053 | continue; | |
2054 | ||
2055 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2056 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
2057 | return TRUE; | |
2058 | ||
2059 | break; | |
2060 | } | |
2061 | ||
2062 | /* This is a new version. Add it to tree we are building. */ | |
2063 | ||
2064 | if (t == NULL) | |
2065 | { | |
2066 | amt = sizeof *t; | |
a50b1753 | 2067 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); |
45d6a902 AM |
2068 | if (t == NULL) |
2069 | { | |
2070 | rinfo->failed = TRUE; | |
2071 | return FALSE; | |
2072 | } | |
2073 | ||
2074 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
28caa186 AM |
2075 | t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; |
2076 | elf_tdata (rinfo->info->output_bfd)->verref = t; | |
45d6a902 AM |
2077 | } |
2078 | ||
2079 | amt = sizeof *a; | |
a50b1753 | 2080 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); |
14b1c01e AM |
2081 | if (a == NULL) |
2082 | { | |
2083 | rinfo->failed = TRUE; | |
2084 | return FALSE; | |
2085 | } | |
45d6a902 AM |
2086 | |
2087 | /* Note that we are copying a string pointer here, and testing it | |
2088 | above. If bfd_elf_string_from_elf_section is ever changed to | |
2089 | discard the string data when low in memory, this will have to be | |
2090 | fixed. */ | |
2091 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
2092 | ||
2093 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
2094 | a->vna_nextptr = t->vn_auxptr; | |
2095 | ||
2096 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
2097 | ++rinfo->vers; | |
2098 | ||
2099 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
2100 | ||
2101 | t->vn_auxptr = a; | |
2102 | ||
2103 | return TRUE; | |
2104 | } | |
2105 | ||
2106 | /* Figure out appropriate versions for all the symbols. We may not | |
2107 | have the version number script until we have read all of the input | |
2108 | files, so until that point we don't know which symbols should be | |
2109 | local. This function is called via elf_link_hash_traverse. */ | |
2110 | ||
28caa186 | 2111 | static bfd_boolean |
268b6b39 | 2112 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2113 | { |
28caa186 | 2114 | struct elf_info_failed *sinfo; |
45d6a902 | 2115 | struct bfd_link_info *info; |
9c5bfbb7 | 2116 | const struct elf_backend_data *bed; |
45d6a902 AM |
2117 | struct elf_info_failed eif; |
2118 | char *p; | |
45d6a902 | 2119 | |
a50b1753 | 2120 | sinfo = (struct elf_info_failed *) data; |
45d6a902 AM |
2121 | info = sinfo->info; |
2122 | ||
45d6a902 AM |
2123 | /* Fix the symbol flags. */ |
2124 | eif.failed = FALSE; | |
2125 | eif.info = info; | |
2126 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
2127 | { | |
2128 | if (eif.failed) | |
2129 | sinfo->failed = TRUE; | |
2130 | return FALSE; | |
2131 | } | |
2132 | ||
2133 | /* We only need version numbers for symbols defined in regular | |
2134 | objects. */ | |
f5385ebf | 2135 | if (!h->def_regular) |
45d6a902 AM |
2136 | return TRUE; |
2137 | ||
28caa186 | 2138 | bed = get_elf_backend_data (info->output_bfd); |
45d6a902 AM |
2139 | p = strchr (h->root.root.string, ELF_VER_CHR); |
2140 | if (p != NULL && h->verinfo.vertree == NULL) | |
2141 | { | |
2142 | struct bfd_elf_version_tree *t; | |
45d6a902 | 2143 | |
45d6a902 AM |
2144 | ++p; |
2145 | if (*p == ELF_VER_CHR) | |
6e33951e | 2146 | ++p; |
45d6a902 AM |
2147 | |
2148 | /* If there is no version string, we can just return out. */ | |
2149 | if (*p == '\0') | |
6e33951e | 2150 | return TRUE; |
45d6a902 AM |
2151 | |
2152 | /* Look for the version. If we find it, it is no longer weak. */ | |
fd91d419 | 2153 | for (t = sinfo->info->version_info; t != NULL; t = t->next) |
45d6a902 AM |
2154 | { |
2155 | if (strcmp (t->name, p) == 0) | |
2156 | { | |
2157 | size_t len; | |
2158 | char *alc; | |
2159 | struct bfd_elf_version_expr *d; | |
2160 | ||
2161 | len = p - h->root.root.string; | |
a50b1753 | 2162 | alc = (char *) bfd_malloc (len); |
45d6a902 | 2163 | if (alc == NULL) |
14b1c01e AM |
2164 | { |
2165 | sinfo->failed = TRUE; | |
2166 | return FALSE; | |
2167 | } | |
45d6a902 AM |
2168 | memcpy (alc, h->root.root.string, len - 1); |
2169 | alc[len - 1] = '\0'; | |
2170 | if (alc[len - 2] == ELF_VER_CHR) | |
2171 | alc[len - 2] = '\0'; | |
2172 | ||
2173 | h->verinfo.vertree = t; | |
2174 | t->used = TRUE; | |
2175 | d = NULL; | |
2176 | ||
108ba305 JJ |
2177 | if (t->globals.list != NULL) |
2178 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
2179 | |
2180 | /* See if there is anything to force this symbol to | |
2181 | local scope. */ | |
108ba305 | 2182 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 2183 | { |
108ba305 JJ |
2184 | d = (*t->match) (&t->locals, NULL, alc); |
2185 | if (d != NULL | |
2186 | && h->dynindx != -1 | |
108ba305 JJ |
2187 | && ! info->export_dynamic) |
2188 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2189 | } |
2190 | ||
2191 | free (alc); | |
2192 | break; | |
2193 | } | |
2194 | } | |
2195 | ||
2196 | /* If we are building an application, we need to create a | |
2197 | version node for this version. */ | |
0e1862bb | 2198 | if (t == NULL && bfd_link_executable (info)) |
45d6a902 AM |
2199 | { |
2200 | struct bfd_elf_version_tree **pp; | |
2201 | int version_index; | |
2202 | ||
2203 | /* If we aren't going to export this symbol, we don't need | |
2204 | to worry about it. */ | |
2205 | if (h->dynindx == -1) | |
2206 | return TRUE; | |
2207 | ||
ef53be89 AM |
2208 | t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, |
2209 | sizeof *t); | |
45d6a902 AM |
2210 | if (t == NULL) |
2211 | { | |
2212 | sinfo->failed = TRUE; | |
2213 | return FALSE; | |
2214 | } | |
2215 | ||
45d6a902 | 2216 | t->name = p; |
45d6a902 AM |
2217 | t->name_indx = (unsigned int) -1; |
2218 | t->used = TRUE; | |
2219 | ||
2220 | version_index = 1; | |
2221 | /* Don't count anonymous version tag. */ | |
fd91d419 L |
2222 | if (sinfo->info->version_info != NULL |
2223 | && sinfo->info->version_info->vernum == 0) | |
45d6a902 | 2224 | version_index = 0; |
fd91d419 L |
2225 | for (pp = &sinfo->info->version_info; |
2226 | *pp != NULL; | |
2227 | pp = &(*pp)->next) | |
45d6a902 AM |
2228 | ++version_index; |
2229 | t->vernum = version_index; | |
2230 | ||
2231 | *pp = t; | |
2232 | ||
2233 | h->verinfo.vertree = t; | |
2234 | } | |
2235 | else if (t == NULL) | |
2236 | { | |
2237 | /* We could not find the version for a symbol when | |
2238 | generating a shared archive. Return an error. */ | |
4eca0228 | 2239 | _bfd_error_handler |
695344c0 | 2240 | /* xgettext:c-format */ |
c55fe096 | 2241 | (_("%B: version node not found for symbol %s"), |
28caa186 | 2242 | info->output_bfd, h->root.root.string); |
45d6a902 AM |
2243 | bfd_set_error (bfd_error_bad_value); |
2244 | sinfo->failed = TRUE; | |
2245 | return FALSE; | |
2246 | } | |
45d6a902 AM |
2247 | } |
2248 | ||
2249 | /* If we don't have a version for this symbol, see if we can find | |
2250 | something. */ | |
fd91d419 | 2251 | if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL) |
45d6a902 | 2252 | { |
1e8fa21e | 2253 | bfd_boolean hide; |
ae5a3597 | 2254 | |
fd91d419 L |
2255 | h->verinfo.vertree |
2256 | = bfd_find_version_for_sym (sinfo->info->version_info, | |
2257 | h->root.root.string, &hide); | |
1e8fa21e AM |
2258 | if (h->verinfo.vertree != NULL && hide) |
2259 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2260 | } |
2261 | ||
2262 | return TRUE; | |
2263 | } | |
2264 | \f | |
45d6a902 AM |
2265 | /* Read and swap the relocs from the section indicated by SHDR. This |
2266 | may be either a REL or a RELA section. The relocations are | |
2267 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2268 | which should have already been allocated to contain enough space. | |
2269 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2270 | relocations should be stored. | |
2271 | ||
2272 | Returns FALSE if something goes wrong. */ | |
2273 | ||
2274 | static bfd_boolean | |
268b6b39 | 2275 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2276 | asection *sec, |
268b6b39 AM |
2277 | Elf_Internal_Shdr *shdr, |
2278 | void *external_relocs, | |
2279 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2280 | { |
9c5bfbb7 | 2281 | const struct elf_backend_data *bed; |
268b6b39 | 2282 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2283 | const bfd_byte *erela; |
2284 | const bfd_byte *erelaend; | |
2285 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2286 | Elf_Internal_Shdr *symtab_hdr; |
2287 | size_t nsyms; | |
45d6a902 | 2288 | |
45d6a902 AM |
2289 | /* Position ourselves at the start of the section. */ |
2290 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2291 | return FALSE; | |
2292 | ||
2293 | /* Read the relocations. */ | |
2294 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2295 | return FALSE; | |
2296 | ||
243ef1e0 | 2297 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
ce98a316 | 2298 | nsyms = NUM_SHDR_ENTRIES (symtab_hdr); |
243ef1e0 | 2299 | |
45d6a902 AM |
2300 | bed = get_elf_backend_data (abfd); |
2301 | ||
2302 | /* Convert the external relocations to the internal format. */ | |
2303 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2304 | swap_in = bed->s->swap_reloc_in; | |
2305 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2306 | swap_in = bed->s->swap_reloca_in; | |
2307 | else | |
2308 | { | |
2309 | bfd_set_error (bfd_error_wrong_format); | |
2310 | return FALSE; | |
2311 | } | |
2312 | ||
a50b1753 | 2313 | erela = (const bfd_byte *) external_relocs; |
51992aec | 2314 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2315 | irela = internal_relocs; |
2316 | while (erela < erelaend) | |
2317 | { | |
243ef1e0 L |
2318 | bfd_vma r_symndx; |
2319 | ||
45d6a902 | 2320 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2321 | r_symndx = ELF32_R_SYM (irela->r_info); |
2322 | if (bed->s->arch_size == 64) | |
2323 | r_symndx >>= 24; | |
ce98a316 NC |
2324 | if (nsyms > 0) |
2325 | { | |
2326 | if ((size_t) r_symndx >= nsyms) | |
2327 | { | |
4eca0228 | 2328 | _bfd_error_handler |
695344c0 | 2329 | /* xgettext:c-format */ |
ce98a316 NC |
2330 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" |
2331 | " for offset 0x%lx in section `%A'"), | |
2332 | abfd, sec, | |
2333 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
2334 | bfd_set_error (bfd_error_bad_value); | |
2335 | return FALSE; | |
2336 | } | |
2337 | } | |
cf35638d | 2338 | else if (r_symndx != STN_UNDEF) |
243ef1e0 | 2339 | { |
4eca0228 | 2340 | _bfd_error_handler |
695344c0 | 2341 | /* xgettext:c-format */ |
ce98a316 NC |
2342 | (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'" |
2343 | " when the object file has no symbol table"), | |
d003868e AM |
2344 | abfd, sec, |
2345 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
2346 | bfd_set_error (bfd_error_bad_value); |
2347 | return FALSE; | |
2348 | } | |
45d6a902 AM |
2349 | irela += bed->s->int_rels_per_ext_rel; |
2350 | erela += shdr->sh_entsize; | |
2351 | } | |
2352 | ||
2353 | return TRUE; | |
2354 | } | |
2355 | ||
2356 | /* Read and swap the relocs for a section O. They may have been | |
2357 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2358 | not NULL, they are used as buffers to read into. They are known to | |
2359 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2360 | the return value is allocated using either malloc or bfd_alloc, | |
2361 | according to the KEEP_MEMORY argument. If O has two relocation | |
2362 | sections (both REL and RELA relocations), then the REL_HDR | |
2363 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
d4730f92 | 2364 | RELA_HDR relocations. */ |
45d6a902 AM |
2365 | |
2366 | Elf_Internal_Rela * | |
268b6b39 AM |
2367 | _bfd_elf_link_read_relocs (bfd *abfd, |
2368 | asection *o, | |
2369 | void *external_relocs, | |
2370 | Elf_Internal_Rela *internal_relocs, | |
2371 | bfd_boolean keep_memory) | |
45d6a902 | 2372 | { |
268b6b39 | 2373 | void *alloc1 = NULL; |
45d6a902 | 2374 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2375 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2376 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
2377 | Elf_Internal_Rela *internal_rela_relocs; | |
45d6a902 | 2378 | |
d4730f92 BS |
2379 | if (esdo->relocs != NULL) |
2380 | return esdo->relocs; | |
45d6a902 AM |
2381 | |
2382 | if (o->reloc_count == 0) | |
2383 | return NULL; | |
2384 | ||
45d6a902 AM |
2385 | if (internal_relocs == NULL) |
2386 | { | |
2387 | bfd_size_type size; | |
2388 | ||
2389 | size = o->reloc_count; | |
2390 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
2391 | if (keep_memory) | |
a50b1753 | 2392 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); |
45d6a902 | 2393 | else |
a50b1753 | 2394 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
45d6a902 AM |
2395 | if (internal_relocs == NULL) |
2396 | goto error_return; | |
2397 | } | |
2398 | ||
2399 | if (external_relocs == NULL) | |
2400 | { | |
d4730f92 BS |
2401 | bfd_size_type size = 0; |
2402 | ||
2403 | if (esdo->rel.hdr) | |
2404 | size += esdo->rel.hdr->sh_size; | |
2405 | if (esdo->rela.hdr) | |
2406 | size += esdo->rela.hdr->sh_size; | |
45d6a902 | 2407 | |
268b6b39 | 2408 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2409 | if (alloc1 == NULL) |
2410 | goto error_return; | |
2411 | external_relocs = alloc1; | |
2412 | } | |
2413 | ||
d4730f92 BS |
2414 | internal_rela_relocs = internal_relocs; |
2415 | if (esdo->rel.hdr) | |
2416 | { | |
2417 | if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, | |
2418 | external_relocs, | |
2419 | internal_relocs)) | |
2420 | goto error_return; | |
2421 | external_relocs = (((bfd_byte *) external_relocs) | |
2422 | + esdo->rel.hdr->sh_size); | |
2423 | internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) | |
2424 | * bed->s->int_rels_per_ext_rel); | |
2425 | } | |
2426 | ||
2427 | if (esdo->rela.hdr | |
2428 | && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, | |
2429 | external_relocs, | |
2430 | internal_rela_relocs))) | |
45d6a902 AM |
2431 | goto error_return; |
2432 | ||
2433 | /* Cache the results for next time, if we can. */ | |
2434 | if (keep_memory) | |
d4730f92 | 2435 | esdo->relocs = internal_relocs; |
45d6a902 AM |
2436 | |
2437 | if (alloc1 != NULL) | |
2438 | free (alloc1); | |
2439 | ||
2440 | /* Don't free alloc2, since if it was allocated we are passing it | |
2441 | back (under the name of internal_relocs). */ | |
2442 | ||
2443 | return internal_relocs; | |
2444 | ||
2445 | error_return: | |
2446 | if (alloc1 != NULL) | |
2447 | free (alloc1); | |
2448 | if (alloc2 != NULL) | |
4dd07732 AM |
2449 | { |
2450 | if (keep_memory) | |
2451 | bfd_release (abfd, alloc2); | |
2452 | else | |
2453 | free (alloc2); | |
2454 | } | |
45d6a902 AM |
2455 | return NULL; |
2456 | } | |
2457 | ||
2458 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2459 | section header for a section containing relocations for O. */ | |
2460 | ||
28caa186 | 2461 | static bfd_boolean |
9eaff861 AO |
2462 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2463 | struct bfd_elf_section_reloc_data *reldata) | |
45d6a902 | 2464 | { |
9eaff861 | 2465 | Elf_Internal_Shdr *rel_hdr = reldata->hdr; |
45d6a902 AM |
2466 | |
2467 | /* That allows us to calculate the size of the section. */ | |
9eaff861 | 2468 | rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; |
45d6a902 AM |
2469 | |
2470 | /* The contents field must last into write_object_contents, so we | |
2471 | allocate it with bfd_alloc rather than malloc. Also since we | |
2472 | cannot be sure that the contents will actually be filled in, | |
2473 | we zero the allocated space. */ | |
a50b1753 | 2474 | rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2475 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2476 | return FALSE; | |
2477 | ||
d4730f92 | 2478 | if (reldata->hashes == NULL && reldata->count) |
45d6a902 AM |
2479 | { |
2480 | struct elf_link_hash_entry **p; | |
2481 | ||
ca4be51c AM |
2482 | p = ((struct elf_link_hash_entry **) |
2483 | bfd_zmalloc (reldata->count * sizeof (*p))); | |
45d6a902 AM |
2484 | if (p == NULL) |
2485 | return FALSE; | |
2486 | ||
d4730f92 | 2487 | reldata->hashes = p; |
45d6a902 AM |
2488 | } |
2489 | ||
2490 | return TRUE; | |
2491 | } | |
2492 | ||
2493 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2494 | originated from the section given by INPUT_REL_HDR) to the | |
2495 | OUTPUT_BFD. */ | |
2496 | ||
2497 | bfd_boolean | |
268b6b39 AM |
2498 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2499 | asection *input_section, | |
2500 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2501 | Elf_Internal_Rela *internal_relocs, |
2502 | struct elf_link_hash_entry **rel_hash | |
2503 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2504 | { |
2505 | Elf_Internal_Rela *irela; | |
2506 | Elf_Internal_Rela *irelaend; | |
2507 | bfd_byte *erel; | |
d4730f92 | 2508 | struct bfd_elf_section_reloc_data *output_reldata; |
45d6a902 | 2509 | asection *output_section; |
9c5bfbb7 | 2510 | const struct elf_backend_data *bed; |
268b6b39 | 2511 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
d4730f92 | 2512 | struct bfd_elf_section_data *esdo; |
45d6a902 AM |
2513 | |
2514 | output_section = input_section->output_section; | |
45d6a902 | 2515 | |
d4730f92 BS |
2516 | bed = get_elf_backend_data (output_bfd); |
2517 | esdo = elf_section_data (output_section); | |
2518 | if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2519 | { |
d4730f92 BS |
2520 | output_reldata = &esdo->rel; |
2521 | swap_out = bed->s->swap_reloc_out; | |
45d6a902 | 2522 | } |
d4730f92 BS |
2523 | else if (esdo->rela.hdr |
2524 | && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2525 | { |
d4730f92 BS |
2526 | output_reldata = &esdo->rela; |
2527 | swap_out = bed->s->swap_reloca_out; | |
45d6a902 AM |
2528 | } |
2529 | else | |
2530 | { | |
4eca0228 | 2531 | _bfd_error_handler |
695344c0 | 2532 | /* xgettext:c-format */ |
d003868e AM |
2533 | (_("%B: relocation size mismatch in %B section %A"), |
2534 | output_bfd, input_section->owner, input_section); | |
297d8443 | 2535 | bfd_set_error (bfd_error_wrong_format); |
45d6a902 AM |
2536 | return FALSE; |
2537 | } | |
2538 | ||
d4730f92 BS |
2539 | erel = output_reldata->hdr->contents; |
2540 | erel += output_reldata->count * input_rel_hdr->sh_entsize; | |
45d6a902 AM |
2541 | irela = internal_relocs; |
2542 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2543 | * bed->s->int_rels_per_ext_rel); | |
2544 | while (irela < irelaend) | |
2545 | { | |
2546 | (*swap_out) (output_bfd, irela, erel); | |
2547 | irela += bed->s->int_rels_per_ext_rel; | |
2548 | erel += input_rel_hdr->sh_entsize; | |
2549 | } | |
2550 | ||
2551 | /* Bump the counter, so that we know where to add the next set of | |
2552 | relocations. */ | |
d4730f92 | 2553 | output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); |
45d6a902 AM |
2554 | |
2555 | return TRUE; | |
2556 | } | |
2557 | \f | |
508c3946 L |
2558 | /* Make weak undefined symbols in PIE dynamic. */ |
2559 | ||
2560 | bfd_boolean | |
2561 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2562 | struct elf_link_hash_entry *h) | |
2563 | { | |
0e1862bb | 2564 | if (bfd_link_pie (info) |
508c3946 L |
2565 | && h->dynindx == -1 |
2566 | && h->root.type == bfd_link_hash_undefweak) | |
2567 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2568 | ||
2569 | return TRUE; | |
2570 | } | |
2571 | ||
45d6a902 AM |
2572 | /* Fix up the flags for a symbol. This handles various cases which |
2573 | can only be fixed after all the input files are seen. This is | |
2574 | currently called by both adjust_dynamic_symbol and | |
2575 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2576 | the face of future changes. */ | |
2577 | ||
28caa186 | 2578 | static bfd_boolean |
268b6b39 AM |
2579 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2580 | struct elf_info_failed *eif) | |
45d6a902 | 2581 | { |
33774f08 | 2582 | const struct elf_backend_data *bed; |
508c3946 | 2583 | |
45d6a902 AM |
2584 | /* If this symbol was mentioned in a non-ELF file, try to set |
2585 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2586 | permit a non-ELF file to correctly refer to a symbol defined in | |
2587 | an ELF dynamic object. */ | |
f5385ebf | 2588 | if (h->non_elf) |
45d6a902 AM |
2589 | { |
2590 | while (h->root.type == bfd_link_hash_indirect) | |
2591 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2592 | ||
2593 | if (h->root.type != bfd_link_hash_defined | |
2594 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2595 | { |
2596 | h->ref_regular = 1; | |
2597 | h->ref_regular_nonweak = 1; | |
2598 | } | |
45d6a902 AM |
2599 | else |
2600 | { | |
2601 | if (h->root.u.def.section->owner != NULL | |
2602 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2603 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2604 | { |
2605 | h->ref_regular = 1; | |
2606 | h->ref_regular_nonweak = 1; | |
2607 | } | |
45d6a902 | 2608 | else |
f5385ebf | 2609 | h->def_regular = 1; |
45d6a902 AM |
2610 | } |
2611 | ||
2612 | if (h->dynindx == -1 | |
f5385ebf AM |
2613 | && (h->def_dynamic |
2614 | || h->ref_dynamic)) | |
45d6a902 | 2615 | { |
c152c796 | 2616 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2617 | { |
2618 | eif->failed = TRUE; | |
2619 | return FALSE; | |
2620 | } | |
2621 | } | |
2622 | } | |
2623 | else | |
2624 | { | |
f5385ebf | 2625 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2626 | was first seen in a non-ELF file. Fortunately, if the symbol |
2627 | was first seen in an ELF file, we're probably OK unless the | |
2628 | symbol was defined in a non-ELF file. Catch that case here. | |
2629 | FIXME: We're still in trouble if the symbol was first seen in | |
2630 | a dynamic object, and then later in a non-ELF regular object. */ | |
2631 | if ((h->root.type == bfd_link_hash_defined | |
2632 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2633 | && !h->def_regular |
45d6a902 AM |
2634 | && (h->root.u.def.section->owner != NULL |
2635 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2636 | != bfd_target_elf_flavour) | |
2637 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2638 | && !h->def_dynamic))) |
2639 | h->def_regular = 1; | |
45d6a902 AM |
2640 | } |
2641 | ||
508c3946 | 2642 | /* Backend specific symbol fixup. */ |
33774f08 AM |
2643 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2644 | if (bed->elf_backend_fixup_symbol | |
2645 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2646 | return FALSE; | |
508c3946 | 2647 | |
45d6a902 AM |
2648 | /* If this is a final link, and the symbol was defined as a common |
2649 | symbol in a regular object file, and there was no definition in | |
2650 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2651 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2652 | flag will not have been set. */ |
2653 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2654 | && !h->def_regular |
2655 | && h->ref_regular | |
2656 | && !h->def_dynamic | |
96f29d96 | 2657 | && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) |
f5385ebf | 2658 | h->def_regular = 1; |
45d6a902 | 2659 | |
4deb8f71 L |
2660 | /* If a weak undefined symbol has non-default visibility, we also |
2661 | hide it from the dynamic linker. */ | |
2662 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT | |
2663 | && h->root.type == bfd_link_hash_undefweak) | |
2664 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2665 | ||
2666 | /* A hidden versioned symbol in executable should be forced local if | |
2667 | it is is locally defined, not referenced by shared library and not | |
2668 | exported. */ | |
2669 | else if (bfd_link_executable (eif->info) | |
2670 | && h->versioned == versioned_hidden | |
2671 | && !eif->info->export_dynamic | |
2672 | && !h->dynamic | |
2673 | && !h->ref_dynamic | |
2674 | && h->def_regular) | |
2675 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2676 | ||
45d6a902 AM |
2677 | /* If -Bsymbolic was used (which means to bind references to global |
2678 | symbols to the definition within the shared object), and this | |
2679 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2680 | need a PLT entry. Likewise, if the symbol has non-default |
2681 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2682 | will force it local. */ |
4deb8f71 L |
2683 | else if (h->needs_plt |
2684 | && bfd_link_pic (eif->info) | |
2685 | && is_elf_hash_table (eif->info->hash) | |
2686 | && (SYMBOLIC_BIND (eif->info, h) | |
2687 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) | |
2688 | && h->def_regular) | |
45d6a902 | 2689 | { |
45d6a902 AM |
2690 | bfd_boolean force_local; |
2691 | ||
45d6a902 AM |
2692 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2693 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2694 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2695 | } | |
2696 | ||
45d6a902 AM |
2697 | /* If this is a weak defined symbol in a dynamic object, and we know |
2698 | the real definition in the dynamic object, copy interesting flags | |
2699 | over to the real definition. */ | |
f6e332e6 | 2700 | if (h->u.weakdef != NULL) |
45d6a902 | 2701 | { |
45d6a902 AM |
2702 | /* If the real definition is defined by a regular object file, |
2703 | don't do anything special. See the longer description in | |
2704 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
4e6b54a6 | 2705 | if (h->u.weakdef->def_regular) |
f6e332e6 | 2706 | h->u.weakdef = NULL; |
45d6a902 | 2707 | else |
a26587ba | 2708 | { |
4e6b54a6 AM |
2709 | struct elf_link_hash_entry *weakdef = h->u.weakdef; |
2710 | ||
2711 | while (h->root.type == bfd_link_hash_indirect) | |
2712 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2713 | ||
2714 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2715 | || h->root.type == bfd_link_hash_defweak); | |
2716 | BFD_ASSERT (weakdef->def_dynamic); | |
a26587ba RS |
2717 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined |
2718 | || weakdef->root.type == bfd_link_hash_defweak); | |
2719 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h); | |
2720 | } | |
45d6a902 AM |
2721 | } |
2722 | ||
2723 | return TRUE; | |
2724 | } | |
2725 | ||
2726 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2727 | called via elf_link_hash_traverse, and also calls itself | |
2728 | recursively. */ | |
2729 | ||
28caa186 | 2730 | static bfd_boolean |
268b6b39 | 2731 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2732 | { |
a50b1753 | 2733 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 | 2734 | bfd *dynobj; |
9c5bfbb7 | 2735 | const struct elf_backend_data *bed; |
45d6a902 | 2736 | |
0eddce27 | 2737 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2738 | return FALSE; |
2739 | ||
45d6a902 AM |
2740 | /* Ignore indirect symbols. These are added by the versioning code. */ |
2741 | if (h->root.type == bfd_link_hash_indirect) | |
2742 | return TRUE; | |
2743 | ||
2744 | /* Fix the symbol flags. */ | |
2745 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2746 | return FALSE; | |
2747 | ||
2748 | /* If this symbol does not require a PLT entry, and it is not | |
2749 | defined by a dynamic object, or is not referenced by a regular | |
2750 | object, ignore it. We do have to handle a weak defined symbol, | |
2751 | even if no regular object refers to it, if we decided to add it | |
2752 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2753 | about symbols which are defined by one dynamic object and | |
2754 | referenced by another one? */ | |
f5385ebf | 2755 | if (!h->needs_plt |
91e21fb7 | 2756 | && h->type != STT_GNU_IFUNC |
f5385ebf AM |
2757 | && (h->def_regular |
2758 | || !h->def_dynamic | |
2759 | || (!h->ref_regular | |
f6e332e6 | 2760 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 | 2761 | { |
a6aa5195 | 2762 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2763 | return TRUE; |
2764 | } | |
2765 | ||
2766 | /* If we've already adjusted this symbol, don't do it again. This | |
2767 | can happen via a recursive call. */ | |
f5385ebf | 2768 | if (h->dynamic_adjusted) |
45d6a902 AM |
2769 | return TRUE; |
2770 | ||
2771 | /* Don't look at this symbol again. Note that we must set this | |
2772 | after checking the above conditions, because we may look at a | |
2773 | symbol once, decide not to do anything, and then get called | |
2774 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2775 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2776 | |
2777 | /* If this is a weak definition, and we know a real definition, and | |
2778 | the real symbol is not itself defined by a regular object file, | |
2779 | then get a good value for the real definition. We handle the | |
2780 | real symbol first, for the convenience of the backend routine. | |
2781 | ||
2782 | Note that there is a confusing case here. If the real definition | |
2783 | is defined by a regular object file, we don't get the real symbol | |
2784 | from the dynamic object, but we do get the weak symbol. If the | |
2785 | processor backend uses a COPY reloc, then if some routine in the | |
2786 | dynamic object changes the real symbol, we will not see that | |
2787 | change in the corresponding weak symbol. This is the way other | |
2788 | ELF linkers work as well, and seems to be a result of the shared | |
2789 | library model. | |
2790 | ||
2791 | I will clarify this issue. Most SVR4 shared libraries define the | |
2792 | variable _timezone and define timezone as a weak synonym. The | |
2793 | tzset call changes _timezone. If you write | |
2794 | extern int timezone; | |
2795 | int _timezone = 5; | |
2796 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2797 | you might expect that, since timezone is a synonym for _timezone, | |
2798 | the same number will print both times. However, if the processor | |
2799 | backend uses a COPY reloc, then actually timezone will be copied | |
2800 | into your process image, and, since you define _timezone | |
2801 | yourself, _timezone will not. Thus timezone and _timezone will | |
2802 | wind up at different memory locations. The tzset call will set | |
2803 | _timezone, leaving timezone unchanged. */ | |
2804 | ||
f6e332e6 | 2805 | if (h->u.weakdef != NULL) |
45d6a902 | 2806 | { |
ec24dc88 AM |
2807 | /* If we get to this point, there is an implicit reference to |
2808 | H->U.WEAKDEF by a regular object file via the weak symbol H. */ | |
f6e332e6 | 2809 | h->u.weakdef->ref_regular = 1; |
45d6a902 | 2810 | |
ec24dc88 AM |
2811 | /* Ensure that the backend adjust_dynamic_symbol function sees |
2812 | H->U.WEAKDEF before H by recursively calling ourselves. */ | |
f6e332e6 | 2813 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2814 | return FALSE; |
2815 | } | |
2816 | ||
2817 | /* If a symbol has no type and no size and does not require a PLT | |
2818 | entry, then we are probably about to do the wrong thing here: we | |
2819 | are probably going to create a COPY reloc for an empty object. | |
2820 | This case can arise when a shared object is built with assembly | |
2821 | code, and the assembly code fails to set the symbol type. */ | |
2822 | if (h->size == 0 | |
2823 | && h->type == STT_NOTYPE | |
f5385ebf | 2824 | && !h->needs_plt) |
4eca0228 | 2825 | _bfd_error_handler |
45d6a902 AM |
2826 | (_("warning: type and size of dynamic symbol `%s' are not defined"), |
2827 | h->root.root.string); | |
2828 | ||
2829 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2830 | bed = get_elf_backend_data (dynobj); | |
e7c33416 | 2831 | |
45d6a902 AM |
2832 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) |
2833 | { | |
2834 | eif->failed = TRUE; | |
2835 | return FALSE; | |
2836 | } | |
2837 | ||
2838 | return TRUE; | |
2839 | } | |
2840 | ||
027297b7 L |
2841 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
2842 | DYNBSS. */ | |
2843 | ||
2844 | bfd_boolean | |
6cabe1ea AM |
2845 | _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, |
2846 | struct elf_link_hash_entry *h, | |
027297b7 L |
2847 | asection *dynbss) |
2848 | { | |
91ac5911 | 2849 | unsigned int power_of_two; |
027297b7 L |
2850 | bfd_vma mask; |
2851 | asection *sec = h->root.u.def.section; | |
2852 | ||
2853 | /* The section aligment of definition is the maximum alignment | |
91ac5911 L |
2854 | requirement of symbols defined in the section. Since we don't |
2855 | know the symbol alignment requirement, we start with the | |
2856 | maximum alignment and check low bits of the symbol address | |
2857 | for the minimum alignment. */ | |
2858 | power_of_two = bfd_get_section_alignment (sec->owner, sec); | |
2859 | mask = ((bfd_vma) 1 << power_of_two) - 1; | |
2860 | while ((h->root.u.def.value & mask) != 0) | |
2861 | { | |
2862 | mask >>= 1; | |
2863 | --power_of_two; | |
2864 | } | |
027297b7 | 2865 | |
91ac5911 L |
2866 | if (power_of_two > bfd_get_section_alignment (dynbss->owner, |
2867 | dynbss)) | |
027297b7 L |
2868 | { |
2869 | /* Adjust the section alignment if needed. */ | |
2870 | if (! bfd_set_section_alignment (dynbss->owner, dynbss, | |
91ac5911 | 2871 | power_of_two)) |
027297b7 L |
2872 | return FALSE; |
2873 | } | |
2874 | ||
91ac5911 | 2875 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
2876 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
2877 | ||
2878 | /* Define the symbol as being at this point in DYNBSS. */ | |
2879 | h->root.u.def.section = dynbss; | |
2880 | h->root.u.def.value = dynbss->size; | |
2881 | ||
2882 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
2883 | dynbss->size += h->size; | |
2884 | ||
f7483970 L |
2885 | /* No error if extern_protected_data is true. */ |
2886 | if (h->protected_def | |
889c2a67 L |
2887 | && (!info->extern_protected_data |
2888 | || (info->extern_protected_data < 0 | |
2889 | && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) | |
d07a1b05 AM |
2890 | info->callbacks->einfo |
2891 | (_("%P: copy reloc against protected `%T' is dangerous\n"), | |
2892 | h->root.root.string); | |
6cabe1ea | 2893 | |
027297b7 L |
2894 | return TRUE; |
2895 | } | |
2896 | ||
45d6a902 AM |
2897 | /* Adjust all external symbols pointing into SEC_MERGE sections |
2898 | to reflect the object merging within the sections. */ | |
2899 | ||
28caa186 | 2900 | static bfd_boolean |
268b6b39 | 2901 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2902 | { |
2903 | asection *sec; | |
2904 | ||
45d6a902 AM |
2905 | if ((h->root.type == bfd_link_hash_defined |
2906 | || h->root.type == bfd_link_hash_defweak) | |
2907 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
dbaa2011 | 2908 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
45d6a902 | 2909 | { |
a50b1753 | 2910 | bfd *output_bfd = (bfd *) data; |
45d6a902 AM |
2911 | |
2912 | h->root.u.def.value = | |
2913 | _bfd_merged_section_offset (output_bfd, | |
2914 | &h->root.u.def.section, | |
2915 | elf_section_data (sec)->sec_info, | |
753731ee | 2916 | h->root.u.def.value); |
45d6a902 AM |
2917 | } |
2918 | ||
2919 | return TRUE; | |
2920 | } | |
986a241f RH |
2921 | |
2922 | /* Returns false if the symbol referred to by H should be considered | |
2923 | to resolve local to the current module, and true if it should be | |
2924 | considered to bind dynamically. */ | |
2925 | ||
2926 | bfd_boolean | |
268b6b39 AM |
2927 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2928 | struct bfd_link_info *info, | |
89a2ee5a | 2929 | bfd_boolean not_local_protected) |
986a241f RH |
2930 | { |
2931 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
2932 | const struct elf_backend_data *bed; |
2933 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
2934 | |
2935 | if (h == NULL) | |
2936 | return FALSE; | |
2937 | ||
2938 | while (h->root.type == bfd_link_hash_indirect | |
2939 | || h->root.type == bfd_link_hash_warning) | |
2940 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2941 | ||
2942 | /* If it was forced local, then clearly it's not dynamic. */ | |
2943 | if (h->dynindx == -1) | |
2944 | return FALSE; | |
f5385ebf | 2945 | if (h->forced_local) |
986a241f RH |
2946 | return FALSE; |
2947 | ||
2948 | /* Identify the cases where name binding rules say that a | |
2949 | visible symbol resolves locally. */ | |
0e1862bb L |
2950 | binding_stays_local_p = (bfd_link_executable (info) |
2951 | || SYMBOLIC_BIND (info, h)); | |
986a241f RH |
2952 | |
2953 | switch (ELF_ST_VISIBILITY (h->other)) | |
2954 | { | |
2955 | case STV_INTERNAL: | |
2956 | case STV_HIDDEN: | |
2957 | return FALSE; | |
2958 | ||
2959 | case STV_PROTECTED: | |
fcb93ecf PB |
2960 | hash_table = elf_hash_table (info); |
2961 | if (!is_elf_hash_table (hash_table)) | |
2962 | return FALSE; | |
2963 | ||
2964 | bed = get_elf_backend_data (hash_table->dynobj); | |
2965 | ||
986a241f RH |
2966 | /* Proper resolution for function pointer equality may require |
2967 | that these symbols perhaps be resolved dynamically, even though | |
2968 | we should be resolving them to the current module. */ | |
89a2ee5a | 2969 | if (!not_local_protected || !bed->is_function_type (h->type)) |
986a241f RH |
2970 | binding_stays_local_p = TRUE; |
2971 | break; | |
2972 | ||
2973 | default: | |
986a241f RH |
2974 | break; |
2975 | } | |
2976 | ||
aa37626c | 2977 | /* If it isn't defined locally, then clearly it's dynamic. */ |
89a2ee5a | 2978 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
aa37626c L |
2979 | return TRUE; |
2980 | ||
986a241f RH |
2981 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2982 | us that it remains local. */ | |
2983 | return !binding_stays_local_p; | |
2984 | } | |
f6c52c13 AM |
2985 | |
2986 | /* Return true if the symbol referred to by H should be considered | |
2987 | to resolve local to the current module, and false otherwise. Differs | |
2988 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2e76e85a | 2989 | undefined symbols. The two functions are virtually identical except |
89a2ee5a AM |
2990 | for the place where forced_local and dynindx == -1 are tested. If |
2991 | either of those tests are true, _bfd_elf_dynamic_symbol_p will say | |
2992 | the symbol is local, while _bfd_elf_symbol_refs_local_p will say | |
2993 | the symbol is local only for defined symbols. | |
2994 | It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as | |
2995 | !_bfd_elf_symbol_refs_local_p, except that targets differ in their | |
2996 | treatment of undefined weak symbols. For those that do not make | |
2997 | undefined weak symbols dynamic, both functions may return false. */ | |
f6c52c13 AM |
2998 | |
2999 | bfd_boolean | |
268b6b39 AM |
3000 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
3001 | struct bfd_link_info *info, | |
3002 | bfd_boolean local_protected) | |
f6c52c13 | 3003 | { |
fcb93ecf PB |
3004 | const struct elf_backend_data *bed; |
3005 | struct elf_link_hash_table *hash_table; | |
3006 | ||
f6c52c13 AM |
3007 | /* If it's a local sym, of course we resolve locally. */ |
3008 | if (h == NULL) | |
3009 | return TRUE; | |
3010 | ||
d95edcac L |
3011 | /* STV_HIDDEN or STV_INTERNAL ones must be local. */ |
3012 | if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
3013 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
3014 | return TRUE; | |
3015 | ||
7e2294f9 AO |
3016 | /* Common symbols that become definitions don't get the DEF_REGULAR |
3017 | flag set, so test it first, and don't bail out. */ | |
3018 | if (ELF_COMMON_DEF_P (h)) | |
3019 | /* Do nothing. */; | |
f6c52c13 | 3020 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
3021 | resolve locally. The sym is either undefined or dynamic. */ |
3022 | else if (!h->def_regular) | |
f6c52c13 AM |
3023 | return FALSE; |
3024 | ||
3025 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 3026 | if (h->forced_local) |
f6c52c13 AM |
3027 | return TRUE; |
3028 | ||
3029 | /* As do non-dynamic symbols. */ | |
3030 | if (h->dynindx == -1) | |
3031 | return TRUE; | |
3032 | ||
3033 | /* At this point, we know the symbol is defined and dynamic. In an | |
3034 | executable it must resolve locally, likewise when building symbolic | |
3035 | shared libraries. */ | |
0e1862bb | 3036 | if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
3037 | return TRUE; |
3038 | ||
3039 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
3040 | with default visibility might not resolve locally. */ | |
3041 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
3042 | return FALSE; | |
3043 | ||
fcb93ecf PB |
3044 | hash_table = elf_hash_table (info); |
3045 | if (!is_elf_hash_table (hash_table)) | |
3046 | return TRUE; | |
3047 | ||
3048 | bed = get_elf_backend_data (hash_table->dynobj); | |
3049 | ||
f7483970 L |
3050 | /* If extern_protected_data is false, STV_PROTECTED non-function |
3051 | symbols are local. */ | |
889c2a67 L |
3052 | if ((!info->extern_protected_data |
3053 | || (info->extern_protected_data < 0 | |
3054 | && !bed->extern_protected_data)) | |
3055 | && !bed->is_function_type (h->type)) | |
1c16dfa5 L |
3056 | return TRUE; |
3057 | ||
f6c52c13 | 3058 | /* Function pointer equality tests may require that STV_PROTECTED |
2676a7d9 AM |
3059 | symbols be treated as dynamic symbols. If the address of a |
3060 | function not defined in an executable is set to that function's | |
3061 | plt entry in the executable, then the address of the function in | |
3062 | a shared library must also be the plt entry in the executable. */ | |
f6c52c13 AM |
3063 | return local_protected; |
3064 | } | |
e1918d23 AM |
3065 | |
3066 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
3067 | aligned. Returns the first TLS output section. */ | |
3068 | ||
3069 | struct bfd_section * | |
3070 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
3071 | { | |
3072 | struct bfd_section *sec, *tls; | |
3073 | unsigned int align = 0; | |
3074 | ||
3075 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
3076 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
3077 | break; | |
3078 | tls = sec; | |
3079 | ||
3080 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
3081 | if (sec->alignment_power > align) | |
3082 | align = sec->alignment_power; | |
3083 | ||
3084 | elf_hash_table (info)->tls_sec = tls; | |
3085 | ||
3086 | /* Ensure the alignment of the first section is the largest alignment, | |
3087 | so that the tls segment starts aligned. */ | |
3088 | if (tls != NULL) | |
3089 | tls->alignment_power = align; | |
3090 | ||
3091 | return tls; | |
3092 | } | |
0ad989f9 L |
3093 | |
3094 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
3095 | static bfd_boolean | |
3096 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
3097 | Elf_Internal_Sym *sym) | |
3098 | { | |
a4d8e49b L |
3099 | const struct elf_backend_data *bed; |
3100 | ||
0ad989f9 L |
3101 | /* Local symbols do not count, but target specific ones might. */ |
3102 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
3103 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
3104 | return FALSE; | |
3105 | ||
fcb93ecf | 3106 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 3107 | /* Function symbols do not count. */ |
fcb93ecf | 3108 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
3109 | return FALSE; |
3110 | ||
3111 | /* If the section is undefined, then so is the symbol. */ | |
3112 | if (sym->st_shndx == SHN_UNDEF) | |
3113 | return FALSE; | |
3114 | ||
3115 | /* If the symbol is defined in the common section, then | |
3116 | it is a common definition and so does not count. */ | |
a4d8e49b | 3117 | if (bed->common_definition (sym)) |
0ad989f9 L |
3118 | return FALSE; |
3119 | ||
3120 | /* If the symbol is in a target specific section then we | |
3121 | must rely upon the backend to tell us what it is. */ | |
3122 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
3123 | /* FIXME - this function is not coded yet: | |
3124 | ||
3125 | return _bfd_is_global_symbol_definition (abfd, sym); | |
3126 | ||
3127 | Instead for now assume that the definition is not global, | |
3128 | Even if this is wrong, at least the linker will behave | |
3129 | in the same way that it used to do. */ | |
3130 | return FALSE; | |
3131 | ||
3132 | return TRUE; | |
3133 | } | |
3134 | ||
3135 | /* Search the symbol table of the archive element of the archive ABFD | |
3136 | whose archive map contains a mention of SYMDEF, and determine if | |
3137 | the symbol is defined in this element. */ | |
3138 | static bfd_boolean | |
3139 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
3140 | { | |
3141 | Elf_Internal_Shdr * hdr; | |
ef53be89 AM |
3142 | size_t symcount; |
3143 | size_t extsymcount; | |
3144 | size_t extsymoff; | |
0ad989f9 L |
3145 | Elf_Internal_Sym *isymbuf; |
3146 | Elf_Internal_Sym *isym; | |
3147 | Elf_Internal_Sym *isymend; | |
3148 | bfd_boolean result; | |
3149 | ||
3150 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
3151 | if (abfd == NULL) | |
3152 | return FALSE; | |
3153 | ||
3154 | if (! bfd_check_format (abfd, bfd_object)) | |
3155 | return FALSE; | |
3156 | ||
7dc3990e L |
3157 | /* Select the appropriate symbol table. If we don't know if the |
3158 | object file is an IR object, give linker LTO plugin a chance to | |
3159 | get the correct symbol table. */ | |
3160 | if (abfd->plugin_format == bfd_plugin_yes | |
08ce1d72 | 3161 | #if BFD_SUPPORTS_PLUGINS |
7dc3990e L |
3162 | || (abfd->plugin_format == bfd_plugin_unknown |
3163 | && bfd_link_plugin_object_p (abfd)) | |
3164 | #endif | |
3165 | ) | |
3166 | { | |
3167 | /* Use the IR symbol table if the object has been claimed by | |
3168 | plugin. */ | |
3169 | abfd = abfd->plugin_dummy_bfd; | |
3170 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3171 | } | |
3172 | else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
0ad989f9 L |
3173 | hdr = &elf_tdata (abfd)->symtab_hdr; |
3174 | else | |
3175 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3176 | ||
3177 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3178 | ||
3179 | /* The sh_info field of the symtab header tells us where the | |
3180 | external symbols start. We don't care about the local symbols. */ | |
3181 | if (elf_bad_symtab (abfd)) | |
3182 | { | |
3183 | extsymcount = symcount; | |
3184 | extsymoff = 0; | |
3185 | } | |
3186 | else | |
3187 | { | |
3188 | extsymcount = symcount - hdr->sh_info; | |
3189 | extsymoff = hdr->sh_info; | |
3190 | } | |
3191 | ||
3192 | if (extsymcount == 0) | |
3193 | return FALSE; | |
3194 | ||
3195 | /* Read in the symbol table. */ | |
3196 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3197 | NULL, NULL, NULL); | |
3198 | if (isymbuf == NULL) | |
3199 | return FALSE; | |
3200 | ||
3201 | /* Scan the symbol table looking for SYMDEF. */ | |
3202 | result = FALSE; | |
3203 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
3204 | { | |
3205 | const char *name; | |
3206 | ||
3207 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3208 | isym->st_name); | |
3209 | if (name == NULL) | |
3210 | break; | |
3211 | ||
3212 | if (strcmp (name, symdef->name) == 0) | |
3213 | { | |
3214 | result = is_global_data_symbol_definition (abfd, isym); | |
3215 | break; | |
3216 | } | |
3217 | } | |
3218 | ||
3219 | free (isymbuf); | |
3220 | ||
3221 | return result; | |
3222 | } | |
3223 | \f | |
5a580b3a AM |
3224 | /* Add an entry to the .dynamic table. */ |
3225 | ||
3226 | bfd_boolean | |
3227 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
3228 | bfd_vma tag, | |
3229 | bfd_vma val) | |
3230 | { | |
3231 | struct elf_link_hash_table *hash_table; | |
3232 | const struct elf_backend_data *bed; | |
3233 | asection *s; | |
3234 | bfd_size_type newsize; | |
3235 | bfd_byte *newcontents; | |
3236 | Elf_Internal_Dyn dyn; | |
3237 | ||
3238 | hash_table = elf_hash_table (info); | |
3239 | if (! is_elf_hash_table (hash_table)) | |
3240 | return FALSE; | |
3241 | ||
3242 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3243 | s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
5a580b3a AM |
3244 | BFD_ASSERT (s != NULL); |
3245 | ||
eea6121a | 3246 | newsize = s->size + bed->s->sizeof_dyn; |
a50b1753 | 3247 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
5a580b3a AM |
3248 | if (newcontents == NULL) |
3249 | return FALSE; | |
3250 | ||
3251 | dyn.d_tag = tag; | |
3252 | dyn.d_un.d_val = val; | |
eea6121a | 3253 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3254 | |
eea6121a | 3255 | s->size = newsize; |
5a580b3a AM |
3256 | s->contents = newcontents; |
3257 | ||
3258 | return TRUE; | |
3259 | } | |
3260 | ||
3261 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
3262 | otherwise just check whether one already exists. Returns -1 on error, | |
3263 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
3264 | ||
4ad4eba5 | 3265 | static int |
7e9f0867 AM |
3266 | elf_add_dt_needed_tag (bfd *abfd, |
3267 | struct bfd_link_info *info, | |
4ad4eba5 AM |
3268 | const char *soname, |
3269 | bfd_boolean do_it) | |
5a580b3a AM |
3270 | { |
3271 | struct elf_link_hash_table *hash_table; | |
ef53be89 | 3272 | size_t strindex; |
5a580b3a | 3273 | |
7e9f0867 AM |
3274 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3275 | return -1; | |
3276 | ||
5a580b3a | 3277 | hash_table = elf_hash_table (info); |
5a580b3a | 3278 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); |
ef53be89 | 3279 | if (strindex == (size_t) -1) |
5a580b3a AM |
3280 | return -1; |
3281 | ||
02be4619 | 3282 | if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) |
5a580b3a AM |
3283 | { |
3284 | asection *sdyn; | |
3285 | const struct elf_backend_data *bed; | |
3286 | bfd_byte *extdyn; | |
3287 | ||
3288 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3289 | sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
7e9f0867 AM |
3290 | if (sdyn != NULL) |
3291 | for (extdyn = sdyn->contents; | |
3292 | extdyn < sdyn->contents + sdyn->size; | |
3293 | extdyn += bed->s->sizeof_dyn) | |
3294 | { | |
3295 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3296 | |
7e9f0867 AM |
3297 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3298 | if (dyn.d_tag == DT_NEEDED | |
3299 | && dyn.d_un.d_val == strindex) | |
3300 | { | |
3301 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3302 | return 1; | |
3303 | } | |
3304 | } | |
5a580b3a AM |
3305 | } |
3306 | ||
3307 | if (do_it) | |
3308 | { | |
7e9f0867 AM |
3309 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3310 | return -1; | |
3311 | ||
5a580b3a AM |
3312 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3313 | return -1; | |
3314 | } | |
3315 | else | |
3316 | /* We were just checking for existence of the tag. */ | |
3317 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3318 | ||
3319 | return 0; | |
3320 | } | |
3321 | ||
7b15fa7a AM |
3322 | /* Return true if SONAME is on the needed list between NEEDED and STOP |
3323 | (or the end of list if STOP is NULL), and needed by a library that | |
3324 | will be loaded. */ | |
3325 | ||
010e5ae2 | 3326 | static bfd_boolean |
7b15fa7a AM |
3327 | on_needed_list (const char *soname, |
3328 | struct bfd_link_needed_list *needed, | |
3329 | struct bfd_link_needed_list *stop) | |
010e5ae2 | 3330 | { |
7b15fa7a AM |
3331 | struct bfd_link_needed_list *look; |
3332 | for (look = needed; look != stop; look = look->next) | |
3333 | if (strcmp (soname, look->name) == 0 | |
3334 | && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 | |
3335 | /* If needed by a library that itself is not directly | |
3336 | needed, recursively check whether that library is | |
3337 | indirectly needed. Since we add DT_NEEDED entries to | |
3338 | the end of the list, library dependencies appear after | |
3339 | the library. Therefore search prior to the current | |
3340 | LOOK, preventing possible infinite recursion. */ | |
3341 | || on_needed_list (elf_dt_name (look->by), needed, look))) | |
010e5ae2 AM |
3342 | return TRUE; |
3343 | ||
3344 | return FALSE; | |
3345 | } | |
3346 | ||
14160578 | 3347 | /* Sort symbol by value, section, and size. */ |
4ad4eba5 AM |
3348 | static int |
3349 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3350 | { |
3351 | const struct elf_link_hash_entry *h1; | |
3352 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3353 | bfd_signed_vma vdiff; |
5a580b3a AM |
3354 | |
3355 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3356 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3357 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3358 | if (vdiff != 0) | |
3359 | return vdiff > 0 ? 1 : -1; | |
3360 | else | |
3361 | { | |
d3435ae8 | 3362 | int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; |
10b7e05b NC |
3363 | if (sdiff != 0) |
3364 | return sdiff > 0 ? 1 : -1; | |
3365 | } | |
14160578 AM |
3366 | vdiff = h1->size - h2->size; |
3367 | return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1; | |
5a580b3a | 3368 | } |
4ad4eba5 | 3369 | |
5a580b3a AM |
3370 | /* This function is used to adjust offsets into .dynstr for |
3371 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3372 | ||
3373 | static bfd_boolean | |
3374 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3375 | { | |
a50b1753 | 3376 | struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; |
5a580b3a | 3377 | |
5a580b3a AM |
3378 | if (h->dynindx != -1) |
3379 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3380 | return TRUE; | |
3381 | } | |
3382 | ||
3383 | /* Assign string offsets in .dynstr, update all structures referencing | |
3384 | them. */ | |
3385 | ||
4ad4eba5 AM |
3386 | static bfd_boolean |
3387 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3388 | { |
3389 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3390 | struct elf_link_local_dynamic_entry *entry; | |
3391 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3392 | bfd *dynobj = hash_table->dynobj; | |
3393 | asection *sdyn; | |
3394 | bfd_size_type size; | |
3395 | const struct elf_backend_data *bed; | |
3396 | bfd_byte *extdyn; | |
3397 | ||
3398 | _bfd_elf_strtab_finalize (dynstr); | |
3399 | size = _bfd_elf_strtab_size (dynstr); | |
3400 | ||
3401 | bed = get_elf_backend_data (dynobj); | |
3d4d4302 | 3402 | sdyn = bfd_get_linker_section (dynobj, ".dynamic"); |
5a580b3a AM |
3403 | BFD_ASSERT (sdyn != NULL); |
3404 | ||
3405 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3406 | for (extdyn = sdyn->contents; | |
eea6121a | 3407 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3408 | extdyn += bed->s->sizeof_dyn) |
3409 | { | |
3410 | Elf_Internal_Dyn dyn; | |
3411 | ||
3412 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3413 | switch (dyn.d_tag) | |
3414 | { | |
3415 | case DT_STRSZ: | |
3416 | dyn.d_un.d_val = size; | |
3417 | break; | |
3418 | case DT_NEEDED: | |
3419 | case DT_SONAME: | |
3420 | case DT_RPATH: | |
3421 | case DT_RUNPATH: | |
3422 | case DT_FILTER: | |
3423 | case DT_AUXILIARY: | |
7ee314fa AM |
3424 | case DT_AUDIT: |
3425 | case DT_DEPAUDIT: | |
5a580b3a AM |
3426 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); |
3427 | break; | |
3428 | default: | |
3429 | continue; | |
3430 | } | |
3431 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3432 | } | |
3433 | ||
3434 | /* Now update local dynamic symbols. */ | |
3435 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3436 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3437 | entry->isym.st_name); | |
3438 | ||
3439 | /* And the rest of dynamic symbols. */ | |
3440 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3441 | ||
3442 | /* Adjust version definitions. */ | |
3443 | if (elf_tdata (output_bfd)->cverdefs) | |
3444 | { | |
3445 | asection *s; | |
3446 | bfd_byte *p; | |
ef53be89 | 3447 | size_t i; |
5a580b3a AM |
3448 | Elf_Internal_Verdef def; |
3449 | Elf_Internal_Verdaux defaux; | |
3450 | ||
3d4d4302 | 3451 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
3452 | p = s->contents; |
3453 | do | |
3454 | { | |
3455 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3456 | &def); | |
3457 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3458 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3459 | continue; | |
5a580b3a AM |
3460 | for (i = 0; i < def.vd_cnt; ++i) |
3461 | { | |
3462 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3463 | (Elf_External_Verdaux *) p, &defaux); | |
3464 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3465 | defaux.vda_name); | |
3466 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3467 | &defaux, (Elf_External_Verdaux *) p); | |
3468 | p += sizeof (Elf_External_Verdaux); | |
3469 | } | |
3470 | } | |
3471 | while (def.vd_next); | |
3472 | } | |
3473 | ||
3474 | /* Adjust version references. */ | |
3475 | if (elf_tdata (output_bfd)->verref) | |
3476 | { | |
3477 | asection *s; | |
3478 | bfd_byte *p; | |
ef53be89 | 3479 | size_t i; |
5a580b3a AM |
3480 | Elf_Internal_Verneed need; |
3481 | Elf_Internal_Vernaux needaux; | |
3482 | ||
3d4d4302 | 3483 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
3484 | p = s->contents; |
3485 | do | |
3486 | { | |
3487 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3488 | &need); | |
3489 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3490 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3491 | (Elf_External_Verneed *) p); | |
3492 | p += sizeof (Elf_External_Verneed); | |
3493 | for (i = 0; i < need.vn_cnt; ++i) | |
3494 | { | |
3495 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3496 | (Elf_External_Vernaux *) p, &needaux); | |
3497 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3498 | needaux.vna_name); | |
3499 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3500 | &needaux, | |
3501 | (Elf_External_Vernaux *) p); | |
3502 | p += sizeof (Elf_External_Vernaux); | |
3503 | } | |
3504 | } | |
3505 | while (need.vn_next); | |
3506 | } | |
3507 | ||
3508 | return TRUE; | |
3509 | } | |
3510 | \f | |
13285a1b AM |
3511 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
3512 | The default is to only match when the INPUT and OUTPUT are exactly | |
3513 | the same target. */ | |
3514 | ||
3515 | bfd_boolean | |
3516 | _bfd_elf_default_relocs_compatible (const bfd_target *input, | |
3517 | const bfd_target *output) | |
3518 | { | |
3519 | return input == output; | |
3520 | } | |
3521 | ||
3522 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
3523 | This version is used when different targets for the same architecture | |
3524 | are virtually identical. */ | |
3525 | ||
3526 | bfd_boolean | |
3527 | _bfd_elf_relocs_compatible (const bfd_target *input, | |
3528 | const bfd_target *output) | |
3529 | { | |
3530 | const struct elf_backend_data *obed, *ibed; | |
3531 | ||
3532 | if (input == output) | |
3533 | return TRUE; | |
3534 | ||
3535 | ibed = xvec_get_elf_backend_data (input); | |
3536 | obed = xvec_get_elf_backend_data (output); | |
3537 | ||
3538 | if (ibed->arch != obed->arch) | |
3539 | return FALSE; | |
3540 | ||
3541 | /* If both backends are using this function, deem them compatible. */ | |
3542 | return ibed->relocs_compatible == obed->relocs_compatible; | |
3543 | } | |
3544 | ||
e5034e59 AM |
3545 | /* Make a special call to the linker "notice" function to tell it that |
3546 | we are about to handle an as-needed lib, or have finished | |
1b786873 | 3547 | processing the lib. */ |
e5034e59 AM |
3548 | |
3549 | bfd_boolean | |
3550 | _bfd_elf_notice_as_needed (bfd *ibfd, | |
3551 | struct bfd_link_info *info, | |
3552 | enum notice_asneeded_action act) | |
3553 | { | |
46135103 | 3554 | return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); |
e5034e59 AM |
3555 | } |
3556 | ||
d9689752 L |
3557 | /* Check relocations an ELF object file. */ |
3558 | ||
3559 | bfd_boolean | |
3560 | _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) | |
3561 | { | |
3562 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3563 | struct elf_link_hash_table *htab = elf_hash_table (info); | |
3564 | ||
3565 | /* If this object is the same format as the output object, and it is | |
3566 | not a shared library, then let the backend look through the | |
3567 | relocs. | |
3568 | ||
3569 | This is required to build global offset table entries and to | |
3570 | arrange for dynamic relocs. It is not required for the | |
3571 | particular common case of linking non PIC code, even when linking | |
3572 | against shared libraries, but unfortunately there is no way of | |
3573 | knowing whether an object file has been compiled PIC or not. | |
3574 | Looking through the relocs is not particularly time consuming. | |
3575 | The problem is that we must either (1) keep the relocs in memory, | |
3576 | which causes the linker to require additional runtime memory or | |
3577 | (2) read the relocs twice from the input file, which wastes time. | |
3578 | This would be a good case for using mmap. | |
3579 | ||
3580 | I have no idea how to handle linking PIC code into a file of a | |
3581 | different format. It probably can't be done. */ | |
3582 | if ((abfd->flags & DYNAMIC) == 0 | |
3583 | && is_elf_hash_table (htab) | |
3584 | && bed->check_relocs != NULL | |
3585 | && elf_object_id (abfd) == elf_hash_table_id (htab) | |
3586 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
3587 | { | |
3588 | asection *o; | |
3589 | ||
3590 | for (o = abfd->sections; o != NULL; o = o->next) | |
3591 | { | |
3592 | Elf_Internal_Rela *internal_relocs; | |
3593 | bfd_boolean ok; | |
3594 | ||
5ce03cea | 3595 | /* Don't check relocations in excluded sections. */ |
d9689752 | 3596 | if ((o->flags & SEC_RELOC) == 0 |
5ce03cea | 3597 | || (o->flags & SEC_EXCLUDE) != 0 |
d9689752 L |
3598 | || o->reloc_count == 0 |
3599 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
3600 | && (o->flags & SEC_DEBUGGING) != 0) | |
3601 | || bfd_is_abs_section (o->output_section)) | |
3602 | continue; | |
3603 | ||
3604 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
3605 | info->keep_memory); | |
3606 | if (internal_relocs == NULL) | |
3607 | return FALSE; | |
3608 | ||
3609 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); | |
3610 | ||
3611 | if (elf_section_data (o)->relocs != internal_relocs) | |
3612 | free (internal_relocs); | |
3613 | ||
3614 | if (! ok) | |
3615 | return FALSE; | |
3616 | } | |
3617 | } | |
3618 | ||
3619 | return TRUE; | |
3620 | } | |
3621 | ||
4ad4eba5 AM |
3622 | /* Add symbols from an ELF object file to the linker hash table. */ |
3623 | ||
3624 | static bfd_boolean | |
3625 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3626 | { | |
a0c402a5 | 3627 | Elf_Internal_Ehdr *ehdr; |
4ad4eba5 | 3628 | Elf_Internal_Shdr *hdr; |
ef53be89 AM |
3629 | size_t symcount; |
3630 | size_t extsymcount; | |
3631 | size_t extsymoff; | |
4ad4eba5 AM |
3632 | struct elf_link_hash_entry **sym_hash; |
3633 | bfd_boolean dynamic; | |
3634 | Elf_External_Versym *extversym = NULL; | |
3635 | Elf_External_Versym *ever; | |
3636 | struct elf_link_hash_entry *weaks; | |
3637 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
ef53be89 | 3638 | size_t nondeflt_vers_cnt = 0; |
4ad4eba5 AM |
3639 | Elf_Internal_Sym *isymbuf = NULL; |
3640 | Elf_Internal_Sym *isym; | |
3641 | Elf_Internal_Sym *isymend; | |
3642 | const struct elf_backend_data *bed; | |
3643 | bfd_boolean add_needed; | |
66eb6687 | 3644 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3645 | bfd_size_type amt; |
66eb6687 | 3646 | void *alloc_mark = NULL; |
4f87808c AM |
3647 | struct bfd_hash_entry **old_table = NULL; |
3648 | unsigned int old_size = 0; | |
3649 | unsigned int old_count = 0; | |
66eb6687 | 3650 | void *old_tab = NULL; |
66eb6687 AM |
3651 | void *old_ent; |
3652 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3653 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
5b677558 | 3654 | void *old_strtab = NULL; |
66eb6687 | 3655 | size_t tabsize = 0; |
db6a5d5f | 3656 | asection *s; |
29a9f53e | 3657 | bfd_boolean just_syms; |
4ad4eba5 | 3658 | |
66eb6687 | 3659 | htab = elf_hash_table (info); |
4ad4eba5 | 3660 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3661 | |
3662 | if ((abfd->flags & DYNAMIC) == 0) | |
3663 | dynamic = FALSE; | |
3664 | else | |
3665 | { | |
3666 | dynamic = TRUE; | |
3667 | ||
3668 | /* You can't use -r against a dynamic object. Also, there's no | |
3669 | hope of using a dynamic object which does not exactly match | |
3670 | the format of the output file. */ | |
0e1862bb | 3671 | if (bfd_link_relocatable (info) |
66eb6687 | 3672 | || !is_elf_hash_table (htab) |
f13a99db | 3673 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 3674 | { |
0e1862bb | 3675 | if (bfd_link_relocatable (info)) |
9a0789ec NC |
3676 | bfd_set_error (bfd_error_invalid_operation); |
3677 | else | |
3678 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3679 | goto error_return; |
3680 | } | |
3681 | } | |
3682 | ||
a0c402a5 L |
3683 | ehdr = elf_elfheader (abfd); |
3684 | if (info->warn_alternate_em | |
3685 | && bed->elf_machine_code != ehdr->e_machine | |
3686 | && ((bed->elf_machine_alt1 != 0 | |
3687 | && ehdr->e_machine == bed->elf_machine_alt1) | |
3688 | || (bed->elf_machine_alt2 != 0 | |
3689 | && ehdr->e_machine == bed->elf_machine_alt2))) | |
3690 | info->callbacks->einfo | |
695344c0 | 3691 | /* xgettext:c-format */ |
a0c402a5 L |
3692 | (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"), |
3693 | ehdr->e_machine, abfd, bed->elf_machine_code); | |
3694 | ||
4ad4eba5 AM |
3695 | /* As a GNU extension, any input sections which are named |
3696 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3697 | symbol. This differs from .gnu.warning sections, which generate | |
3698 | warnings when they are included in an output file. */ | |
dd98f8d2 | 3699 | /* PR 12761: Also generate this warning when building shared libraries. */ |
db6a5d5f | 3700 | for (s = abfd->sections; s != NULL; s = s->next) |
4ad4eba5 | 3701 | { |
db6a5d5f | 3702 | const char *name; |
4ad4eba5 | 3703 | |
db6a5d5f AM |
3704 | name = bfd_get_section_name (abfd, s); |
3705 | if (CONST_STRNEQ (name, ".gnu.warning.")) | |
4ad4eba5 | 3706 | { |
db6a5d5f AM |
3707 | char *msg; |
3708 | bfd_size_type sz; | |
3709 | ||
3710 | name += sizeof ".gnu.warning." - 1; | |
3711 | ||
3712 | /* If this is a shared object, then look up the symbol | |
3713 | in the hash table. If it is there, and it is already | |
3714 | been defined, then we will not be using the entry | |
3715 | from this shared object, so we don't need to warn. | |
3716 | FIXME: If we see the definition in a regular object | |
3717 | later on, we will warn, but we shouldn't. The only | |
3718 | fix is to keep track of what warnings we are supposed | |
3719 | to emit, and then handle them all at the end of the | |
3720 | link. */ | |
3721 | if (dynamic) | |
4ad4eba5 | 3722 | { |
db6a5d5f AM |
3723 | struct elf_link_hash_entry *h; |
3724 | ||
3725 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); | |
3726 | ||
3727 | /* FIXME: What about bfd_link_hash_common? */ | |
3728 | if (h != NULL | |
3729 | && (h->root.type == bfd_link_hash_defined | |
3730 | || h->root.type == bfd_link_hash_defweak)) | |
3731 | continue; | |
3732 | } | |
4ad4eba5 | 3733 | |
db6a5d5f AM |
3734 | sz = s->size; |
3735 | msg = (char *) bfd_alloc (abfd, sz + 1); | |
3736 | if (msg == NULL) | |
3737 | goto error_return; | |
4ad4eba5 | 3738 | |
db6a5d5f AM |
3739 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
3740 | goto error_return; | |
4ad4eba5 | 3741 | |
db6a5d5f | 3742 | msg[sz] = '\0'; |
4ad4eba5 | 3743 | |
db6a5d5f AM |
3744 | if (! (_bfd_generic_link_add_one_symbol |
3745 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3746 | FALSE, bed->collect, NULL))) | |
3747 | goto error_return; | |
4ad4eba5 | 3748 | |
0e1862bb | 3749 | if (bfd_link_executable (info)) |
db6a5d5f AM |
3750 | { |
3751 | /* Clobber the section size so that the warning does | |
3752 | not get copied into the output file. */ | |
3753 | s->size = 0; | |
11d2f718 | 3754 | |
db6a5d5f AM |
3755 | /* Also set SEC_EXCLUDE, so that symbols defined in |
3756 | the warning section don't get copied to the output. */ | |
3757 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3758 | } |
3759 | } | |
3760 | } | |
3761 | ||
29a9f53e L |
3762 | just_syms = ((s = abfd->sections) != NULL |
3763 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); | |
3764 | ||
4ad4eba5 AM |
3765 | add_needed = TRUE; |
3766 | if (! dynamic) | |
3767 | { | |
3768 | /* If we are creating a shared library, create all the dynamic | |
3769 | sections immediately. We need to attach them to something, | |
3770 | so we attach them to this BFD, provided it is the right | |
bf89386a L |
3771 | format and is not from ld --just-symbols. Always create the |
3772 | dynamic sections for -E/--dynamic-list. FIXME: If there | |
29a9f53e L |
3773 | are no input BFD's of the same format as the output, we can't |
3774 | make a shared library. */ | |
3775 | if (!just_syms | |
bf89386a | 3776 | && (bfd_link_pic (info) |
9c1d7a08 L |
3777 | || (!bfd_link_relocatable (info) |
3778 | && (info->export_dynamic || info->dynamic))) | |
66eb6687 | 3779 | && is_elf_hash_table (htab) |
f13a99db | 3780 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 3781 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
3782 | { |
3783 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3784 | goto error_return; | |
3785 | } | |
3786 | } | |
66eb6687 | 3787 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
3788 | goto error_return; |
3789 | else | |
3790 | { | |
4ad4eba5 | 3791 | const char *soname = NULL; |
7ee314fa | 3792 | char *audit = NULL; |
4ad4eba5 AM |
3793 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; |
3794 | int ret; | |
3795 | ||
3796 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3797 | ld shouldn't allow it. */ |
29a9f53e | 3798 | if (just_syms) |
92fd189d | 3799 | abort (); |
4ad4eba5 AM |
3800 | |
3801 | /* If this dynamic lib was specified on the command line with | |
3802 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3803 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3804 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3805 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3806 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3807 | all. */ | |
3808 | add_needed = (elf_dyn_lib_class (abfd) | |
3809 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3810 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3811 | |
3812 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3813 | if (s != NULL) | |
3814 | { | |
3815 | bfd_byte *dynbuf; | |
3816 | bfd_byte *extdyn; | |
cb33740c | 3817 | unsigned int elfsec; |
4ad4eba5 AM |
3818 | unsigned long shlink; |
3819 | ||
eea6121a | 3820 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
f8703194 L |
3821 | { |
3822 | error_free_dyn: | |
3823 | free (dynbuf); | |
3824 | goto error_return; | |
3825 | } | |
4ad4eba5 AM |
3826 | |
3827 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 3828 | if (elfsec == SHN_BAD) |
4ad4eba5 AM |
3829 | goto error_free_dyn; |
3830 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3831 | ||
3832 | for (extdyn = dynbuf; | |
eea6121a | 3833 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3834 | extdyn += bed->s->sizeof_dyn) |
3835 | { | |
3836 | Elf_Internal_Dyn dyn; | |
3837 | ||
3838 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3839 | if (dyn.d_tag == DT_SONAME) | |
3840 | { | |
3841 | unsigned int tagv = dyn.d_un.d_val; | |
3842 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3843 | if (soname == NULL) | |
3844 | goto error_free_dyn; | |
3845 | } | |
3846 | if (dyn.d_tag == DT_NEEDED) | |
3847 | { | |
3848 | struct bfd_link_needed_list *n, **pn; | |
3849 | char *fnm, *anm; | |
3850 | unsigned int tagv = dyn.d_un.d_val; | |
3851 | ||
3852 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3853 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3854 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3855 | if (n == NULL || fnm == NULL) | |
3856 | goto error_free_dyn; | |
3857 | amt = strlen (fnm) + 1; | |
a50b1753 | 3858 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3859 | if (anm == NULL) |
3860 | goto error_free_dyn; | |
3861 | memcpy (anm, fnm, amt); | |
3862 | n->name = anm; | |
3863 | n->by = abfd; | |
3864 | n->next = NULL; | |
66eb6687 | 3865 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3866 | ; |
3867 | *pn = n; | |
3868 | } | |
3869 | if (dyn.d_tag == DT_RUNPATH) | |
3870 | { | |
3871 | struct bfd_link_needed_list *n, **pn; | |
3872 | char *fnm, *anm; | |
3873 | unsigned int tagv = dyn.d_un.d_val; | |
3874 | ||
3875 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3876 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3877 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3878 | if (n == NULL || fnm == NULL) | |
3879 | goto error_free_dyn; | |
3880 | amt = strlen (fnm) + 1; | |
a50b1753 | 3881 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3882 | if (anm == NULL) |
3883 | goto error_free_dyn; | |
3884 | memcpy (anm, fnm, amt); | |
3885 | n->name = anm; | |
3886 | n->by = abfd; | |
3887 | n->next = NULL; | |
3888 | for (pn = & runpath; | |
3889 | *pn != NULL; | |
3890 | pn = &(*pn)->next) | |
3891 | ; | |
3892 | *pn = n; | |
3893 | } | |
3894 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3895 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3896 | { | |
3897 | struct bfd_link_needed_list *n, **pn; | |
3898 | char *fnm, *anm; | |
3899 | unsigned int tagv = dyn.d_un.d_val; | |
3900 | ||
3901 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3902 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3903 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3904 | if (n == NULL || fnm == NULL) | |
3905 | goto error_free_dyn; | |
3906 | amt = strlen (fnm) + 1; | |
a50b1753 | 3907 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 | 3908 | if (anm == NULL) |
f8703194 | 3909 | goto error_free_dyn; |
4ad4eba5 AM |
3910 | memcpy (anm, fnm, amt); |
3911 | n->name = anm; | |
3912 | n->by = abfd; | |
3913 | n->next = NULL; | |
3914 | for (pn = & rpath; | |
3915 | *pn != NULL; | |
3916 | pn = &(*pn)->next) | |
3917 | ; | |
3918 | *pn = n; | |
3919 | } | |
7ee314fa AM |
3920 | if (dyn.d_tag == DT_AUDIT) |
3921 | { | |
3922 | unsigned int tagv = dyn.d_un.d_val; | |
3923 | audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3924 | } | |
4ad4eba5 AM |
3925 | } |
3926 | ||
3927 | free (dynbuf); | |
3928 | } | |
3929 | ||
3930 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3931 | frees all more recently bfd_alloc'd blocks as well. */ | |
3932 | if (runpath) | |
3933 | rpath = runpath; | |
3934 | ||
3935 | if (rpath) | |
3936 | { | |
3937 | struct bfd_link_needed_list **pn; | |
66eb6687 | 3938 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3939 | ; |
3940 | *pn = rpath; | |
3941 | } | |
3942 | ||
3943 | /* We do not want to include any of the sections in a dynamic | |
3944 | object in the output file. We hack by simply clobbering the | |
3945 | list of sections in the BFD. This could be handled more | |
3946 | cleanly by, say, a new section flag; the existing | |
3947 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3948 | still implies that the section takes up space in the output | |
3949 | file. */ | |
3950 | bfd_section_list_clear (abfd); | |
3951 | ||
4ad4eba5 AM |
3952 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3953 | object. If the object has a DT_SONAME entry, we use it. | |
3954 | Otherwise, if the generic linker stuck something in | |
3955 | elf_dt_name, we use that. Otherwise, we just use the file | |
3956 | name. */ | |
3957 | if (soname == NULL || *soname == '\0') | |
3958 | { | |
3959 | soname = elf_dt_name (abfd); | |
3960 | if (soname == NULL || *soname == '\0') | |
3961 | soname = bfd_get_filename (abfd); | |
3962 | } | |
3963 | ||
3964 | /* Save the SONAME because sometimes the linker emulation code | |
3965 | will need to know it. */ | |
3966 | elf_dt_name (abfd) = soname; | |
3967 | ||
7e9f0867 | 3968 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3969 | if (ret < 0) |
3970 | goto error_return; | |
3971 | ||
3972 | /* If we have already included this dynamic object in the | |
3973 | link, just ignore it. There is no reason to include a | |
3974 | particular dynamic object more than once. */ | |
3975 | if (ret > 0) | |
3976 | return TRUE; | |
7ee314fa AM |
3977 | |
3978 | /* Save the DT_AUDIT entry for the linker emulation code. */ | |
68ffbac6 | 3979 | elf_dt_audit (abfd) = audit; |
4ad4eba5 AM |
3980 | } |
3981 | ||
3982 | /* If this is a dynamic object, we always link against the .dynsym | |
3983 | symbol table, not the .symtab symbol table. The dynamic linker | |
3984 | will only see the .dynsym symbol table, so there is no reason to | |
3985 | look at .symtab for a dynamic object. */ | |
3986 | ||
3987 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3988 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3989 | else | |
3990 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3991 | ||
3992 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3993 | ||
3994 | /* The sh_info field of the symtab header tells us where the | |
3995 | external symbols start. We don't care about the local symbols at | |
3996 | this point. */ | |
3997 | if (elf_bad_symtab (abfd)) | |
3998 | { | |
3999 | extsymcount = symcount; | |
4000 | extsymoff = 0; | |
4001 | } | |
4002 | else | |
4003 | { | |
4004 | extsymcount = symcount - hdr->sh_info; | |
4005 | extsymoff = hdr->sh_info; | |
4006 | } | |
4007 | ||
f45794cb | 4008 | sym_hash = elf_sym_hashes (abfd); |
012b2306 | 4009 | if (extsymcount != 0) |
4ad4eba5 AM |
4010 | { |
4011 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
4012 | NULL, NULL, NULL); | |
4013 | if (isymbuf == NULL) | |
4014 | goto error_return; | |
4015 | ||
4ad4eba5 | 4016 | if (sym_hash == NULL) |
012b2306 AM |
4017 | { |
4018 | /* We store a pointer to the hash table entry for each | |
4019 | external symbol. */ | |
ef53be89 AM |
4020 | amt = extsymcount; |
4021 | amt *= sizeof (struct elf_link_hash_entry *); | |
012b2306 AM |
4022 | sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); |
4023 | if (sym_hash == NULL) | |
4024 | goto error_free_sym; | |
4025 | elf_sym_hashes (abfd) = sym_hash; | |
4026 | } | |
4ad4eba5 AM |
4027 | } |
4028 | ||
4029 | if (dynamic) | |
4030 | { | |
4031 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
4032 | if (!_bfd_elf_slurp_version_tables (abfd, |
4033 | info->default_imported_symver)) | |
4ad4eba5 AM |
4034 | goto error_free_sym; |
4035 | ||
4036 | /* Read in the symbol versions, but don't bother to convert them | |
4037 | to internal format. */ | |
4038 | if (elf_dynversym (abfd) != 0) | |
4039 | { | |
4040 | Elf_Internal_Shdr *versymhdr; | |
4041 | ||
4042 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
a50b1753 | 4043 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
4ad4eba5 AM |
4044 | if (extversym == NULL) |
4045 | goto error_free_sym; | |
4046 | amt = versymhdr->sh_size; | |
4047 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
4048 | || bfd_bread (extversym, amt, abfd) != amt) | |
4049 | goto error_free_vers; | |
4050 | } | |
4051 | } | |
4052 | ||
66eb6687 AM |
4053 | /* If we are loading an as-needed shared lib, save the symbol table |
4054 | state before we start adding symbols. If the lib turns out | |
4055 | to be unneeded, restore the state. */ | |
4056 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4057 | { | |
4058 | unsigned int i; | |
4059 | size_t entsize; | |
4060 | ||
4061 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
4062 | { | |
4063 | struct bfd_hash_entry *p; | |
2de92251 | 4064 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4065 | |
4066 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
4067 | { |
4068 | h = (struct elf_link_hash_entry *) p; | |
4069 | entsize += htab->root.table.entsize; | |
4070 | if (h->root.type == bfd_link_hash_warning) | |
4071 | entsize += htab->root.table.entsize; | |
4072 | } | |
66eb6687 AM |
4073 | } |
4074 | ||
4075 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
f45794cb | 4076 | old_tab = bfd_malloc (tabsize + entsize); |
66eb6687 AM |
4077 | if (old_tab == NULL) |
4078 | goto error_free_vers; | |
4079 | ||
4080 | /* Remember the current objalloc pointer, so that all mem for | |
4081 | symbols added can later be reclaimed. */ | |
4082 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
4083 | if (alloc_mark == NULL) | |
4084 | goto error_free_vers; | |
4085 | ||
5061a885 AM |
4086 | /* Make a special call to the linker "notice" function to |
4087 | tell it that we are about to handle an as-needed lib. */ | |
e5034e59 | 4088 | if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) |
9af2a943 | 4089 | goto error_free_vers; |
5061a885 | 4090 | |
f45794cb AM |
4091 | /* Clone the symbol table. Remember some pointers into the |
4092 | symbol table, and dynamic symbol count. */ | |
4093 | old_ent = (char *) old_tab + tabsize; | |
66eb6687 | 4094 | memcpy (old_tab, htab->root.table.table, tabsize); |
66eb6687 AM |
4095 | old_undefs = htab->root.undefs; |
4096 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
4097 | old_table = htab->root.table.table; |
4098 | old_size = htab->root.table.size; | |
4099 | old_count = htab->root.table.count; | |
5b677558 AM |
4100 | old_strtab = _bfd_elf_strtab_save (htab->dynstr); |
4101 | if (old_strtab == NULL) | |
4102 | goto error_free_vers; | |
66eb6687 AM |
4103 | |
4104 | for (i = 0; i < htab->root.table.size; i++) | |
4105 | { | |
4106 | struct bfd_hash_entry *p; | |
2de92251 | 4107 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4108 | |
4109 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4110 | { | |
4111 | memcpy (old_ent, p, htab->root.table.entsize); | |
4112 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4113 | h = (struct elf_link_hash_entry *) p; |
4114 | if (h->root.type == bfd_link_hash_warning) | |
4115 | { | |
4116 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
4117 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4118 | } | |
66eb6687 AM |
4119 | } |
4120 | } | |
4121 | } | |
4ad4eba5 | 4122 | |
66eb6687 | 4123 | weaks = NULL; |
4ad4eba5 AM |
4124 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
4125 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
4126 | isym < isymend; | |
4127 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
4128 | { | |
4129 | int bind; | |
4130 | bfd_vma value; | |
af44c138 | 4131 | asection *sec, *new_sec; |
4ad4eba5 AM |
4132 | flagword flags; |
4133 | const char *name; | |
4134 | struct elf_link_hash_entry *h; | |
90c984fc | 4135 | struct elf_link_hash_entry *hi; |
4ad4eba5 AM |
4136 | bfd_boolean definition; |
4137 | bfd_boolean size_change_ok; | |
4138 | bfd_boolean type_change_ok; | |
4139 | bfd_boolean new_weakdef; | |
37a9e49a L |
4140 | bfd_boolean new_weak; |
4141 | bfd_boolean old_weak; | |
4ad4eba5 | 4142 | bfd_boolean override; |
a4d8e49b | 4143 | bfd_boolean common; |
97196564 | 4144 | bfd_boolean discarded; |
4ad4eba5 AM |
4145 | unsigned int old_alignment; |
4146 | bfd *old_bfd; | |
6e33951e | 4147 | bfd_boolean matched; |
4ad4eba5 AM |
4148 | |
4149 | override = FALSE; | |
4150 | ||
4151 | flags = BSF_NO_FLAGS; | |
4152 | sec = NULL; | |
4153 | value = isym->st_value; | |
a4d8e49b | 4154 | common = bed->common_definition (isym); |
97196564 | 4155 | discarded = FALSE; |
4ad4eba5 AM |
4156 | |
4157 | bind = ELF_ST_BIND (isym->st_info); | |
3e7a7d11 | 4158 | switch (bind) |
4ad4eba5 | 4159 | { |
3e7a7d11 | 4160 | case STB_LOCAL: |
4ad4eba5 AM |
4161 | /* This should be impossible, since ELF requires that all |
4162 | global symbols follow all local symbols, and that sh_info | |
4163 | point to the first global symbol. Unfortunately, Irix 5 | |
4164 | screws this up. */ | |
4165 | continue; | |
3e7a7d11 NC |
4166 | |
4167 | case STB_GLOBAL: | |
a4d8e49b | 4168 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 | 4169 | flags = BSF_GLOBAL; |
3e7a7d11 NC |
4170 | break; |
4171 | ||
4172 | case STB_WEAK: | |
4173 | flags = BSF_WEAK; | |
4174 | break; | |
4175 | ||
4176 | case STB_GNU_UNIQUE: | |
4177 | flags = BSF_GNU_UNIQUE; | |
4178 | break; | |
4179 | ||
4180 | default: | |
4ad4eba5 | 4181 | /* Leave it up to the processor backend. */ |
3e7a7d11 | 4182 | break; |
4ad4eba5 AM |
4183 | } |
4184 | ||
4185 | if (isym->st_shndx == SHN_UNDEF) | |
4186 | sec = bfd_und_section_ptr; | |
cb33740c AM |
4187 | else if (isym->st_shndx == SHN_ABS) |
4188 | sec = bfd_abs_section_ptr; | |
4189 | else if (isym->st_shndx == SHN_COMMON) | |
4190 | { | |
4191 | sec = bfd_com_section_ptr; | |
4192 | /* What ELF calls the size we call the value. What ELF | |
4193 | calls the value we call the alignment. */ | |
4194 | value = isym->st_size; | |
4195 | } | |
4196 | else | |
4ad4eba5 AM |
4197 | { |
4198 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
4199 | if (sec == NULL) | |
4200 | sec = bfd_abs_section_ptr; | |
dbaa2011 | 4201 | else if (discarded_section (sec)) |
529fcb95 | 4202 | { |
e5d08002 L |
4203 | /* Symbols from discarded section are undefined. We keep |
4204 | its visibility. */ | |
529fcb95 | 4205 | sec = bfd_und_section_ptr; |
97196564 | 4206 | discarded = TRUE; |
529fcb95 PB |
4207 | isym->st_shndx = SHN_UNDEF; |
4208 | } | |
4ad4eba5 AM |
4209 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
4210 | value -= sec->vma; | |
4211 | } | |
4ad4eba5 AM |
4212 | |
4213 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
4214 | isym->st_name); | |
4215 | if (name == NULL) | |
4216 | goto error_free_vers; | |
4217 | ||
4218 | if (isym->st_shndx == SHN_COMMON | |
02d00247 AM |
4219 | && (abfd->flags & BFD_PLUGIN) != 0) |
4220 | { | |
4221 | asection *xc = bfd_get_section_by_name (abfd, "COMMON"); | |
4222 | ||
4223 | if (xc == NULL) | |
4224 | { | |
4225 | flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP | |
4226 | | SEC_EXCLUDE); | |
4227 | xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); | |
4228 | if (xc == NULL) | |
4229 | goto error_free_vers; | |
4230 | } | |
4231 | sec = xc; | |
4232 | } | |
4233 | else if (isym->st_shndx == SHN_COMMON | |
4234 | && ELF_ST_TYPE (isym->st_info) == STT_TLS | |
0e1862bb | 4235 | && !bfd_link_relocatable (info)) |
4ad4eba5 AM |
4236 | { |
4237 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
4238 | ||
4239 | if (tcomm == NULL) | |
4240 | { | |
02d00247 AM |
4241 | flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON |
4242 | | SEC_LINKER_CREATED); | |
4243 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); | |
3496cb2a | 4244 | if (tcomm == NULL) |
4ad4eba5 AM |
4245 | goto error_free_vers; |
4246 | } | |
4247 | sec = tcomm; | |
4248 | } | |
66eb6687 | 4249 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 4250 | { |
66eb6687 AM |
4251 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
4252 | &sec, &value)) | |
4ad4eba5 AM |
4253 | goto error_free_vers; |
4254 | ||
4255 | /* The hook function sets the name to NULL if this symbol | |
4256 | should be skipped for some reason. */ | |
4257 | if (name == NULL) | |
4258 | continue; | |
4259 | } | |
4260 | ||
4261 | /* Sanity check that all possibilities were handled. */ | |
4262 | if (sec == NULL) | |
4263 | { | |
4264 | bfd_set_error (bfd_error_bad_value); | |
4265 | goto error_free_vers; | |
4266 | } | |
4267 | ||
191c0c42 AM |
4268 | /* Silently discard TLS symbols from --just-syms. There's |
4269 | no way to combine a static TLS block with a new TLS block | |
4270 | for this executable. */ | |
4271 | if (ELF_ST_TYPE (isym->st_info) == STT_TLS | |
4272 | && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
4273 | continue; | |
4274 | ||
4ad4eba5 AM |
4275 | if (bfd_is_und_section (sec) |
4276 | || bfd_is_com_section (sec)) | |
4277 | definition = FALSE; | |
4278 | else | |
4279 | definition = TRUE; | |
4280 | ||
4281 | size_change_ok = FALSE; | |
66eb6687 | 4282 | type_change_ok = bed->type_change_ok; |
37a9e49a | 4283 | old_weak = FALSE; |
6e33951e | 4284 | matched = FALSE; |
4ad4eba5 AM |
4285 | old_alignment = 0; |
4286 | old_bfd = NULL; | |
af44c138 | 4287 | new_sec = sec; |
4ad4eba5 | 4288 | |
66eb6687 | 4289 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
4290 | { |
4291 | Elf_Internal_Versym iver; | |
4292 | unsigned int vernum = 0; | |
4293 | bfd_boolean skip; | |
4294 | ||
fc0e6df6 | 4295 | if (ever == NULL) |
4ad4eba5 | 4296 | { |
fc0e6df6 PB |
4297 | if (info->default_imported_symver) |
4298 | /* Use the default symbol version created earlier. */ | |
4299 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
4300 | else | |
4301 | iver.vs_vers = 0; | |
4302 | } | |
4303 | else | |
4304 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
4305 | ||
4306 | vernum = iver.vs_vers & VERSYM_VERSION; | |
4307 | ||
4308 | /* If this is a hidden symbol, or if it is not version | |
4309 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
4310 | However, we do not modify a non-hidden absolute symbol |
4311 | if it is not a function, because it might be the version | |
4312 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 4313 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
4314 | || (vernum > 1 |
4315 | && (!bfd_is_abs_section (sec) | |
4316 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
4317 | { |
4318 | const char *verstr; | |
4319 | size_t namelen, verlen, newlen; | |
4320 | char *newname, *p; | |
4321 | ||
4322 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 4323 | { |
fc0e6df6 PB |
4324 | if (vernum > elf_tdata (abfd)->cverdefs) |
4325 | verstr = NULL; | |
4326 | else if (vernum > 1) | |
4327 | verstr = | |
4328 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
4329 | else | |
4330 | verstr = ""; | |
4ad4eba5 | 4331 | |
fc0e6df6 | 4332 | if (verstr == NULL) |
4ad4eba5 | 4333 | { |
4eca0228 | 4334 | _bfd_error_handler |
695344c0 | 4335 | /* xgettext:c-format */ |
fc0e6df6 PB |
4336 | (_("%B: %s: invalid version %u (max %d)"), |
4337 | abfd, name, vernum, | |
4338 | elf_tdata (abfd)->cverdefs); | |
4339 | bfd_set_error (bfd_error_bad_value); | |
4340 | goto error_free_vers; | |
4ad4eba5 | 4341 | } |
fc0e6df6 PB |
4342 | } |
4343 | else | |
4344 | { | |
4345 | /* We cannot simply test for the number of | |
4346 | entries in the VERNEED section since the | |
4347 | numbers for the needed versions do not start | |
4348 | at 0. */ | |
4349 | Elf_Internal_Verneed *t; | |
4350 | ||
4351 | verstr = NULL; | |
4352 | for (t = elf_tdata (abfd)->verref; | |
4353 | t != NULL; | |
4354 | t = t->vn_nextref) | |
4ad4eba5 | 4355 | { |
fc0e6df6 | 4356 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 4357 | |
fc0e6df6 PB |
4358 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
4359 | { | |
4360 | if (a->vna_other == vernum) | |
4ad4eba5 | 4361 | { |
fc0e6df6 PB |
4362 | verstr = a->vna_nodename; |
4363 | break; | |
4ad4eba5 | 4364 | } |
4ad4eba5 | 4365 | } |
fc0e6df6 PB |
4366 | if (a != NULL) |
4367 | break; | |
4368 | } | |
4369 | if (verstr == NULL) | |
4370 | { | |
4eca0228 | 4371 | _bfd_error_handler |
695344c0 | 4372 | /* xgettext:c-format */ |
fc0e6df6 PB |
4373 | (_("%B: %s: invalid needed version %d"), |
4374 | abfd, name, vernum); | |
4375 | bfd_set_error (bfd_error_bad_value); | |
4376 | goto error_free_vers; | |
4ad4eba5 | 4377 | } |
4ad4eba5 | 4378 | } |
fc0e6df6 PB |
4379 | |
4380 | namelen = strlen (name); | |
4381 | verlen = strlen (verstr); | |
4382 | newlen = namelen + verlen + 2; | |
4383 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4384 | && isym->st_shndx != SHN_UNDEF) | |
4385 | ++newlen; | |
4386 | ||
a50b1753 | 4387 | newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
4388 | if (newname == NULL) |
4389 | goto error_free_vers; | |
4390 | memcpy (newname, name, namelen); | |
4391 | p = newname + namelen; | |
4392 | *p++ = ELF_VER_CHR; | |
4393 | /* If this is a defined non-hidden version symbol, | |
4394 | we add another @ to the name. This indicates the | |
4395 | default version of the symbol. */ | |
4396 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4397 | && isym->st_shndx != SHN_UNDEF) | |
4398 | *p++ = ELF_VER_CHR; | |
4399 | memcpy (p, verstr, verlen + 1); | |
4400 | ||
4401 | name = newname; | |
4ad4eba5 AM |
4402 | } |
4403 | ||
cd3416da AM |
4404 | /* If this symbol has default visibility and the user has |
4405 | requested we not re-export it, then mark it as hidden. */ | |
a0d49154 | 4406 | if (!bfd_is_und_section (sec) |
cd3416da | 4407 | && !dynamic |
ce875075 | 4408 | && abfd->no_export |
cd3416da AM |
4409 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) |
4410 | isym->st_other = (STV_HIDDEN | |
4411 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
4412 | ||
4f3fedcf AM |
4413 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, |
4414 | sym_hash, &old_bfd, &old_weak, | |
4415 | &old_alignment, &skip, &override, | |
6e33951e L |
4416 | &type_change_ok, &size_change_ok, |
4417 | &matched)) | |
4ad4eba5 AM |
4418 | goto error_free_vers; |
4419 | ||
4420 | if (skip) | |
4421 | continue; | |
4422 | ||
6e33951e L |
4423 | /* Override a definition only if the new symbol matches the |
4424 | existing one. */ | |
4425 | if (override && matched) | |
4ad4eba5 AM |
4426 | definition = FALSE; |
4427 | ||
4428 | h = *sym_hash; | |
4429 | while (h->root.type == bfd_link_hash_indirect | |
4430 | || h->root.type == bfd_link_hash_warning) | |
4431 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4432 | ||
4ad4eba5 | 4433 | if (elf_tdata (abfd)->verdef != NULL |
4ad4eba5 AM |
4434 | && vernum > 1 |
4435 | && definition) | |
4436 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4437 | } | |
4438 | ||
4439 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4440 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4441 | (struct bfd_link_hash_entry **) sym_hash))) |
4442 | goto error_free_vers; | |
4443 | ||
a43942db MR |
4444 | if ((flags & BSF_GNU_UNIQUE) |
4445 | && (abfd->flags & DYNAMIC) == 0 | |
4446 | && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour) | |
4447 | elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique; | |
4448 | ||
4ad4eba5 | 4449 | h = *sym_hash; |
90c984fc L |
4450 | /* We need to make sure that indirect symbol dynamic flags are |
4451 | updated. */ | |
4452 | hi = h; | |
4ad4eba5 AM |
4453 | while (h->root.type == bfd_link_hash_indirect |
4454 | || h->root.type == bfd_link_hash_warning) | |
4455 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3e7a7d11 | 4456 | |
97196564 L |
4457 | /* Setting the index to -3 tells elf_link_output_extsym that |
4458 | this symbol is defined in a discarded section. */ | |
4459 | if (discarded) | |
4460 | h->indx = -3; | |
4461 | ||
4ad4eba5 AM |
4462 | *sym_hash = h; |
4463 | ||
37a9e49a | 4464 | new_weak = (flags & BSF_WEAK) != 0; |
4ad4eba5 AM |
4465 | new_weakdef = FALSE; |
4466 | if (dynamic | |
4467 | && definition | |
37a9e49a | 4468 | && new_weak |
fcb93ecf | 4469 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4470 | && is_elf_hash_table (htab) |
f6e332e6 | 4471 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
4472 | { |
4473 | /* Keep a list of all weak defined non function symbols from | |
4474 | a dynamic object, using the weakdef field. Later in this | |
4475 | function we will set the weakdef field to the correct | |
4476 | value. We only put non-function symbols from dynamic | |
4477 | objects on this list, because that happens to be the only | |
4478 | time we need to know the normal symbol corresponding to a | |
4479 | weak symbol, and the information is time consuming to | |
4480 | figure out. If the weakdef field is not already NULL, | |
4481 | then this symbol was already defined by some previous | |
4482 | dynamic object, and we will be using that previous | |
4483 | definition anyhow. */ | |
4484 | ||
f6e332e6 | 4485 | h->u.weakdef = weaks; |
4ad4eba5 AM |
4486 | weaks = h; |
4487 | new_weakdef = TRUE; | |
4488 | } | |
4489 | ||
4490 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4491 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4492 | && h->root.type == bfd_link_hash_common) |
4493 | { | |
4494 | unsigned int align; | |
4495 | ||
a4d8e49b | 4496 | if (common) |
af44c138 L |
4497 | align = bfd_log2 (isym->st_value); |
4498 | else | |
4499 | { | |
4500 | /* The new symbol is a common symbol in a shared object. | |
4501 | We need to get the alignment from the section. */ | |
4502 | align = new_sec->alignment_power; | |
4503 | } | |
595213d4 | 4504 | if (align > old_alignment) |
4ad4eba5 AM |
4505 | h->root.u.c.p->alignment_power = align; |
4506 | else | |
4507 | h->root.u.c.p->alignment_power = old_alignment; | |
4508 | } | |
4509 | ||
66eb6687 | 4510 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4511 | { |
4f3fedcf AM |
4512 | /* Set a flag in the hash table entry indicating the type of |
4513 | reference or definition we just found. A dynamic symbol | |
4514 | is one which is referenced or defined by both a regular | |
4515 | object and a shared object. */ | |
4516 | bfd_boolean dynsym = FALSE; | |
4517 | ||
4518 | /* Plugin symbols aren't normal. Don't set def_regular or | |
4519 | ref_regular for them, or make them dynamic. */ | |
4520 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4521 | ; | |
4522 | else if (! dynamic) | |
4523 | { | |
4524 | if (! definition) | |
4525 | { | |
4526 | h->ref_regular = 1; | |
4527 | if (bind != STB_WEAK) | |
4528 | h->ref_regular_nonweak = 1; | |
4529 | } | |
4530 | else | |
4531 | { | |
4532 | h->def_regular = 1; | |
4533 | if (h->def_dynamic) | |
4534 | { | |
4535 | h->def_dynamic = 0; | |
4536 | h->ref_dynamic = 1; | |
4537 | } | |
4538 | } | |
4539 | ||
4540 | /* If the indirect symbol has been forced local, don't | |
4541 | make the real symbol dynamic. */ | |
4542 | if ((h == hi || !hi->forced_local) | |
0e1862bb | 4543 | && (bfd_link_dll (info) |
4f3fedcf AM |
4544 | || h->def_dynamic |
4545 | || h->ref_dynamic)) | |
4546 | dynsym = TRUE; | |
4547 | } | |
4548 | else | |
4549 | { | |
4550 | if (! definition) | |
4551 | { | |
4552 | h->ref_dynamic = 1; | |
4553 | hi->ref_dynamic = 1; | |
4554 | } | |
4555 | else | |
4556 | { | |
4557 | h->def_dynamic = 1; | |
4558 | hi->def_dynamic = 1; | |
4559 | } | |
4560 | ||
4561 | /* If the indirect symbol has been forced local, don't | |
4562 | make the real symbol dynamic. */ | |
4563 | if ((h == hi || !hi->forced_local) | |
4564 | && (h->def_regular | |
4565 | || h->ref_regular | |
4566 | || (h->u.weakdef != NULL | |
4567 | && ! new_weakdef | |
4568 | && h->u.weakdef->dynindx != -1))) | |
4569 | dynsym = TRUE; | |
4570 | } | |
4571 | ||
4572 | /* Check to see if we need to add an indirect symbol for | |
4573 | the default name. */ | |
4574 | if (definition | |
4575 | || (!override && h->root.type == bfd_link_hash_common)) | |
4576 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4577 | sec, value, &old_bfd, &dynsym)) | |
4578 | goto error_free_vers; | |
4ad4eba5 AM |
4579 | |
4580 | /* Check the alignment when a common symbol is involved. This | |
4581 | can change when a common symbol is overridden by a normal | |
4582 | definition or a common symbol is ignored due to the old | |
4583 | normal definition. We need to make sure the maximum | |
4584 | alignment is maintained. */ | |
a4d8e49b | 4585 | if ((old_alignment || common) |
4ad4eba5 AM |
4586 | && h->root.type != bfd_link_hash_common) |
4587 | { | |
4588 | unsigned int common_align; | |
4589 | unsigned int normal_align; | |
4590 | unsigned int symbol_align; | |
4591 | bfd *normal_bfd; | |
4592 | bfd *common_bfd; | |
4593 | ||
3a81e825 AM |
4594 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
4595 | || h->root.type == bfd_link_hash_defweak); | |
4596 | ||
4ad4eba5 AM |
4597 | symbol_align = ffs (h->root.u.def.value) - 1; |
4598 | if (h->root.u.def.section->owner != NULL | |
0616a280 AM |
4599 | && (h->root.u.def.section->owner->flags |
4600 | & (DYNAMIC | BFD_PLUGIN)) == 0) | |
4ad4eba5 AM |
4601 | { |
4602 | normal_align = h->root.u.def.section->alignment_power; | |
4603 | if (normal_align > symbol_align) | |
4604 | normal_align = symbol_align; | |
4605 | } | |
4606 | else | |
4607 | normal_align = symbol_align; | |
4608 | ||
4609 | if (old_alignment) | |
4610 | { | |
4611 | common_align = old_alignment; | |
4612 | common_bfd = old_bfd; | |
4613 | normal_bfd = abfd; | |
4614 | } | |
4615 | else | |
4616 | { | |
4617 | common_align = bfd_log2 (isym->st_value); | |
4618 | common_bfd = abfd; | |
4619 | normal_bfd = old_bfd; | |
4620 | } | |
4621 | ||
4622 | if (normal_align < common_align) | |
d07676f8 NC |
4623 | { |
4624 | /* PR binutils/2735 */ | |
4625 | if (normal_bfd == NULL) | |
4eca0228 | 4626 | _bfd_error_handler |
695344c0 | 4627 | /* xgettext:c-format */ |
4f3fedcf AM |
4628 | (_("Warning: alignment %u of common symbol `%s' in %B is" |
4629 | " greater than the alignment (%u) of its section %A"), | |
d07676f8 NC |
4630 | common_bfd, h->root.u.def.section, |
4631 | 1 << common_align, name, 1 << normal_align); | |
4632 | else | |
4eca0228 | 4633 | _bfd_error_handler |
695344c0 | 4634 | /* xgettext:c-format */ |
d07676f8 NC |
4635 | (_("Warning: alignment %u of symbol `%s' in %B" |
4636 | " is smaller than %u in %B"), | |
4637 | normal_bfd, common_bfd, | |
4638 | 1 << normal_align, name, 1 << common_align); | |
4639 | } | |
4ad4eba5 AM |
4640 | } |
4641 | ||
83ad0046 | 4642 | /* Remember the symbol size if it isn't undefined. */ |
3a81e825 AM |
4643 | if (isym->st_size != 0 |
4644 | && isym->st_shndx != SHN_UNDEF | |
4ad4eba5 AM |
4645 | && (definition || h->size == 0)) |
4646 | { | |
83ad0046 L |
4647 | if (h->size != 0 |
4648 | && h->size != isym->st_size | |
4649 | && ! size_change_ok) | |
4eca0228 | 4650 | _bfd_error_handler |
695344c0 | 4651 | /* xgettext:c-format */ |
d003868e AM |
4652 | (_("Warning: size of symbol `%s' changed" |
4653 | " from %lu in %B to %lu in %B"), | |
4654 | old_bfd, abfd, | |
4ad4eba5 | 4655 | name, (unsigned long) h->size, |
d003868e | 4656 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
4657 | |
4658 | h->size = isym->st_size; | |
4659 | } | |
4660 | ||
4661 | /* If this is a common symbol, then we always want H->SIZE | |
4662 | to be the size of the common symbol. The code just above | |
4663 | won't fix the size if a common symbol becomes larger. We | |
4664 | don't warn about a size change here, because that is | |
4f3fedcf | 4665 | covered by --warn-common. Allow changes between different |
fcb93ecf | 4666 | function types. */ |
4ad4eba5 AM |
4667 | if (h->root.type == bfd_link_hash_common) |
4668 | h->size = h->root.u.c.size; | |
4669 | ||
4670 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
37a9e49a L |
4671 | && ((definition && !new_weak) |
4672 | || (old_weak && h->root.type == bfd_link_hash_common) | |
4673 | || h->type == STT_NOTYPE)) | |
4ad4eba5 | 4674 | { |
2955ec4c L |
4675 | unsigned int type = ELF_ST_TYPE (isym->st_info); |
4676 | ||
4677 | /* Turn an IFUNC symbol from a DSO into a normal FUNC | |
4678 | symbol. */ | |
4679 | if (type == STT_GNU_IFUNC | |
4680 | && (abfd->flags & DYNAMIC) != 0) | |
4681 | type = STT_FUNC; | |
4ad4eba5 | 4682 | |
2955ec4c L |
4683 | if (h->type != type) |
4684 | { | |
4685 | if (h->type != STT_NOTYPE && ! type_change_ok) | |
695344c0 | 4686 | /* xgettext:c-format */ |
4eca0228 | 4687 | _bfd_error_handler |
2955ec4c L |
4688 | (_("Warning: type of symbol `%s' changed" |
4689 | " from %d to %d in %B"), | |
4690 | abfd, name, h->type, type); | |
4691 | ||
4692 | h->type = type; | |
4693 | } | |
4ad4eba5 AM |
4694 | } |
4695 | ||
54ac0771 | 4696 | /* Merge st_other field. */ |
b8417128 | 4697 | elf_merge_st_other (abfd, h, isym, sec, definition, dynamic); |
4ad4eba5 | 4698 | |
c3df8c14 | 4699 | /* We don't want to make debug symbol dynamic. */ |
0e1862bb L |
4700 | if (definition |
4701 | && (sec->flags & SEC_DEBUGGING) | |
4702 | && !bfd_link_relocatable (info)) | |
c3df8c14 AM |
4703 | dynsym = FALSE; |
4704 | ||
4f3fedcf AM |
4705 | /* Nor should we make plugin symbols dynamic. */ |
4706 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4707 | dynsym = FALSE; | |
4708 | ||
35fc36a8 | 4709 | if (definition) |
35399224 L |
4710 | { |
4711 | h->target_internal = isym->st_target_internal; | |
4712 | h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; | |
4713 | } | |
35fc36a8 | 4714 | |
4ad4eba5 AM |
4715 | if (definition && !dynamic) |
4716 | { | |
4717 | char *p = strchr (name, ELF_VER_CHR); | |
4718 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4719 | { | |
4720 | /* Queue non-default versions so that .symver x, x@FOO | |
4721 | aliases can be checked. */ | |
66eb6687 | 4722 | if (!nondeflt_vers) |
4ad4eba5 | 4723 | { |
66eb6687 AM |
4724 | amt = ((isymend - isym + 1) |
4725 | * sizeof (struct elf_link_hash_entry *)); | |
ca4be51c AM |
4726 | nondeflt_vers |
4727 | = (struct elf_link_hash_entry **) bfd_malloc (amt); | |
14b1c01e AM |
4728 | if (!nondeflt_vers) |
4729 | goto error_free_vers; | |
4ad4eba5 | 4730 | } |
66eb6687 | 4731 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4732 | } |
4733 | } | |
4734 | ||
4735 | if (dynsym && h->dynindx == -1) | |
4736 | { | |
c152c796 | 4737 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4738 | goto error_free_vers; |
f6e332e6 | 4739 | if (h->u.weakdef != NULL |
4ad4eba5 | 4740 | && ! new_weakdef |
f6e332e6 | 4741 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 4742 | { |
66eb6687 | 4743 | if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
4744 | goto error_free_vers; |
4745 | } | |
4746 | } | |
1f599d0e | 4747 | else if (h->dynindx != -1) |
4ad4eba5 AM |
4748 | /* If the symbol already has a dynamic index, but |
4749 | visibility says it should not be visible, turn it into | |
4750 | a local symbol. */ | |
4751 | switch (ELF_ST_VISIBILITY (h->other)) | |
4752 | { | |
4753 | case STV_INTERNAL: | |
4754 | case STV_HIDDEN: | |
4755 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4756 | dynsym = FALSE; | |
4757 | break; | |
4758 | } | |
4759 | ||
aef28989 L |
4760 | /* Don't add DT_NEEDED for references from the dummy bfd nor |
4761 | for unmatched symbol. */ | |
4ad4eba5 | 4762 | if (!add_needed |
aef28989 | 4763 | && matched |
4ad4eba5 | 4764 | && definition |
010e5ae2 | 4765 | && ((dynsym |
ffa9430d | 4766 | && h->ref_regular_nonweak |
4f3fedcf AM |
4767 | && (old_bfd == NULL |
4768 | || (old_bfd->flags & BFD_PLUGIN) == 0)) | |
ffa9430d | 4769 | || (h->ref_dynamic_nonweak |
010e5ae2 | 4770 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 |
7b15fa7a AM |
4771 | && !on_needed_list (elf_dt_name (abfd), |
4772 | htab->needed, NULL)))) | |
4ad4eba5 AM |
4773 | { |
4774 | int ret; | |
4775 | const char *soname = elf_dt_name (abfd); | |
4776 | ||
16e4ecc0 AM |
4777 | info->callbacks->minfo ("%!", soname, old_bfd, |
4778 | h->root.root.string); | |
4779 | ||
4ad4eba5 AM |
4780 | /* A symbol from a library loaded via DT_NEEDED of some |
4781 | other library is referenced by a regular object. | |
e56f61be | 4782 | Add a DT_NEEDED entry for it. Issue an error if |
b918acf9 NC |
4783 | --no-add-needed is used and the reference was not |
4784 | a weak one. */ | |
4f3fedcf | 4785 | if (old_bfd != NULL |
b918acf9 | 4786 | && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) |
e56f61be | 4787 | { |
4eca0228 | 4788 | _bfd_error_handler |
695344c0 | 4789 | /* xgettext:c-format */ |
3cbc5de0 | 4790 | (_("%B: undefined reference to symbol '%s'"), |
4f3fedcf | 4791 | old_bfd, name); |
ff5ac77b | 4792 | bfd_set_error (bfd_error_missing_dso); |
e56f61be L |
4793 | goto error_free_vers; |
4794 | } | |
4795 | ||
a50b1753 | 4796 | elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) |
ca4be51c | 4797 | (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); |
a5db907e | 4798 | |
4ad4eba5 | 4799 | add_needed = TRUE; |
7e9f0867 | 4800 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4801 | if (ret < 0) |
4802 | goto error_free_vers; | |
4803 | ||
4804 | BFD_ASSERT (ret == 0); | |
4805 | } | |
4806 | } | |
4807 | } | |
4808 | ||
66eb6687 AM |
4809 | if (extversym != NULL) |
4810 | { | |
4811 | free (extversym); | |
4812 | extversym = NULL; | |
4813 | } | |
4814 | ||
4815 | if (isymbuf != NULL) | |
4816 | { | |
4817 | free (isymbuf); | |
4818 | isymbuf = NULL; | |
4819 | } | |
4820 | ||
4821 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4822 | { | |
4823 | unsigned int i; | |
4824 | ||
4825 | /* Restore the symbol table. */ | |
f45794cb AM |
4826 | old_ent = (char *) old_tab + tabsize; |
4827 | memset (elf_sym_hashes (abfd), 0, | |
4828 | extsymcount * sizeof (struct elf_link_hash_entry *)); | |
4f87808c AM |
4829 | htab->root.table.table = old_table; |
4830 | htab->root.table.size = old_size; | |
4831 | htab->root.table.count = old_count; | |
66eb6687 | 4832 | memcpy (htab->root.table.table, old_tab, tabsize); |
66eb6687 AM |
4833 | htab->root.undefs = old_undefs; |
4834 | htab->root.undefs_tail = old_undefs_tail; | |
5b677558 AM |
4835 | _bfd_elf_strtab_restore (htab->dynstr, old_strtab); |
4836 | free (old_strtab); | |
4837 | old_strtab = NULL; | |
66eb6687 AM |
4838 | for (i = 0; i < htab->root.table.size; i++) |
4839 | { | |
4840 | struct bfd_hash_entry *p; | |
4841 | struct elf_link_hash_entry *h; | |
3e0882af L |
4842 | bfd_size_type size; |
4843 | unsigned int alignment_power; | |
66eb6687 AM |
4844 | |
4845 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4846 | { | |
4847 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
4848 | if (h->root.type == bfd_link_hash_warning) |
4849 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2de92251 | 4850 | |
3e0882af L |
4851 | /* Preserve the maximum alignment and size for common |
4852 | symbols even if this dynamic lib isn't on DT_NEEDED | |
a4542f1b | 4853 | since it can still be loaded at run time by another |
3e0882af L |
4854 | dynamic lib. */ |
4855 | if (h->root.type == bfd_link_hash_common) | |
4856 | { | |
4857 | size = h->root.u.c.size; | |
4858 | alignment_power = h->root.u.c.p->alignment_power; | |
4859 | } | |
4860 | else | |
4861 | { | |
4862 | size = 0; | |
4863 | alignment_power = 0; | |
4864 | } | |
66eb6687 AM |
4865 | memcpy (p, old_ent, htab->root.table.entsize); |
4866 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4867 | h = (struct elf_link_hash_entry *) p; |
4868 | if (h->root.type == bfd_link_hash_warning) | |
4869 | { | |
4870 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
4871 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
a4542f1b | 4872 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
2de92251 | 4873 | } |
a4542f1b | 4874 | if (h->root.type == bfd_link_hash_common) |
3e0882af L |
4875 | { |
4876 | if (size > h->root.u.c.size) | |
4877 | h->root.u.c.size = size; | |
4878 | if (alignment_power > h->root.u.c.p->alignment_power) | |
4879 | h->root.u.c.p->alignment_power = alignment_power; | |
4880 | } | |
66eb6687 AM |
4881 | } |
4882 | } | |
4883 | ||
5061a885 AM |
4884 | /* Make a special call to the linker "notice" function to |
4885 | tell it that symbols added for crefs may need to be removed. */ | |
e5034e59 | 4886 | if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) |
9af2a943 | 4887 | goto error_free_vers; |
5061a885 | 4888 | |
66eb6687 AM |
4889 | free (old_tab); |
4890 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
4891 | alloc_mark); | |
4892 | if (nondeflt_vers != NULL) | |
4893 | free (nondeflt_vers); | |
4894 | return TRUE; | |
4895 | } | |
2de92251 | 4896 | |
66eb6687 AM |
4897 | if (old_tab != NULL) |
4898 | { | |
e5034e59 | 4899 | if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) |
9af2a943 | 4900 | goto error_free_vers; |
66eb6687 AM |
4901 | free (old_tab); |
4902 | old_tab = NULL; | |
4903 | } | |
4904 | ||
c6e8a9a8 L |
4905 | /* Now that all the symbols from this input file are created, if |
4906 | not performing a relocatable link, handle .symver foo, foo@BAR | |
4907 | such that any relocs against foo become foo@BAR. */ | |
0e1862bb | 4908 | if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) |
4ad4eba5 | 4909 | { |
ef53be89 | 4910 | size_t cnt, symidx; |
4ad4eba5 AM |
4911 | |
4912 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
4913 | { | |
4914 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
4915 | char *shortname, *p; | |
4916 | ||
4917 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
4918 | if (p == NULL | |
4919 | || (h->root.type != bfd_link_hash_defined | |
4920 | && h->root.type != bfd_link_hash_defweak)) | |
4921 | continue; | |
4922 | ||
4923 | amt = p - h->root.root.string; | |
a50b1753 | 4924 | shortname = (char *) bfd_malloc (amt + 1); |
14b1c01e AM |
4925 | if (!shortname) |
4926 | goto error_free_vers; | |
4ad4eba5 AM |
4927 | memcpy (shortname, h->root.root.string, amt); |
4928 | shortname[amt] = '\0'; | |
4929 | ||
4930 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 4931 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
4932 | FALSE, FALSE, FALSE); |
4933 | if (hi != NULL | |
4934 | && hi->root.type == h->root.type | |
4935 | && hi->root.u.def.value == h->root.u.def.value | |
4936 | && hi->root.u.def.section == h->root.u.def.section) | |
4937 | { | |
4938 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4939 | hi->root.type = bfd_link_hash_indirect; | |
4940 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 4941 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
4942 | sym_hash = elf_sym_hashes (abfd); |
4943 | if (sym_hash) | |
4944 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4945 | if (sym_hash[symidx] == hi) | |
4946 | { | |
4947 | sym_hash[symidx] = h; | |
4948 | break; | |
4949 | } | |
4950 | } | |
4951 | free (shortname); | |
4952 | } | |
4953 | free (nondeflt_vers); | |
4954 | nondeflt_vers = NULL; | |
4955 | } | |
4956 | ||
4ad4eba5 AM |
4957 | /* Now set the weakdefs field correctly for all the weak defined |
4958 | symbols we found. The only way to do this is to search all the | |
4959 | symbols. Since we only need the information for non functions in | |
4960 | dynamic objects, that's the only time we actually put anything on | |
4961 | the list WEAKS. We need this information so that if a regular | |
4962 | object refers to a symbol defined weakly in a dynamic object, the | |
4963 | real symbol in the dynamic object is also put in the dynamic | |
4964 | symbols; we also must arrange for both symbols to point to the | |
4965 | same memory location. We could handle the general case of symbol | |
4966 | aliasing, but a general symbol alias can only be generated in | |
4967 | assembler code, handling it correctly would be very time | |
4968 | consuming, and other ELF linkers don't handle general aliasing | |
4969 | either. */ | |
4970 | if (weaks != NULL) | |
4971 | { | |
4972 | struct elf_link_hash_entry **hpp; | |
4973 | struct elf_link_hash_entry **hppend; | |
4974 | struct elf_link_hash_entry **sorted_sym_hash; | |
4975 | struct elf_link_hash_entry *h; | |
4976 | size_t sym_count; | |
4977 | ||
4978 | /* Since we have to search the whole symbol list for each weak | |
4979 | defined symbol, search time for N weak defined symbols will be | |
4980 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
ef53be89 AM |
4981 | amt = extsymcount; |
4982 | amt *= sizeof (struct elf_link_hash_entry *); | |
a50b1753 | 4983 | sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); |
4ad4eba5 AM |
4984 | if (sorted_sym_hash == NULL) |
4985 | goto error_return; | |
4986 | sym_hash = sorted_sym_hash; | |
4987 | hpp = elf_sym_hashes (abfd); | |
4988 | hppend = hpp + extsymcount; | |
4989 | sym_count = 0; | |
4990 | for (; hpp < hppend; hpp++) | |
4991 | { | |
4992 | h = *hpp; | |
4993 | if (h != NULL | |
4994 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 4995 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
4996 | { |
4997 | *sym_hash = h; | |
4998 | sym_hash++; | |
4999 | sym_count++; | |
5000 | } | |
5001 | } | |
5002 | ||
5003 | qsort (sorted_sym_hash, sym_count, | |
5004 | sizeof (struct elf_link_hash_entry *), | |
5005 | elf_sort_symbol); | |
5006 | ||
5007 | while (weaks != NULL) | |
5008 | { | |
5009 | struct elf_link_hash_entry *hlook; | |
5010 | asection *slook; | |
5011 | bfd_vma vlook; | |
ed54588d | 5012 | size_t i, j, idx = 0; |
4ad4eba5 AM |
5013 | |
5014 | hlook = weaks; | |
f6e332e6 AM |
5015 | weaks = hlook->u.weakdef; |
5016 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
5017 | |
5018 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
5019 | || hlook->root.type == bfd_link_hash_defweak | |
5020 | || hlook->root.type == bfd_link_hash_common | |
5021 | || hlook->root.type == bfd_link_hash_indirect); | |
5022 | slook = hlook->root.u.def.section; | |
5023 | vlook = hlook->root.u.def.value; | |
5024 | ||
4ad4eba5 AM |
5025 | i = 0; |
5026 | j = sym_count; | |
14160578 | 5027 | while (i != j) |
4ad4eba5 AM |
5028 | { |
5029 | bfd_signed_vma vdiff; | |
5030 | idx = (i + j) / 2; | |
14160578 | 5031 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5032 | vdiff = vlook - h->root.u.def.value; |
5033 | if (vdiff < 0) | |
5034 | j = idx; | |
5035 | else if (vdiff > 0) | |
5036 | i = idx + 1; | |
5037 | else | |
5038 | { | |
d3435ae8 | 5039 | int sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
5040 | if (sdiff < 0) |
5041 | j = idx; | |
5042 | else if (sdiff > 0) | |
5043 | i = idx + 1; | |
5044 | else | |
14160578 | 5045 | break; |
4ad4eba5 AM |
5046 | } |
5047 | } | |
5048 | ||
5049 | /* We didn't find a value/section match. */ | |
14160578 | 5050 | if (i == j) |
4ad4eba5 AM |
5051 | continue; |
5052 | ||
14160578 AM |
5053 | /* With multiple aliases, or when the weak symbol is already |
5054 | strongly defined, we have multiple matching symbols and | |
5055 | the binary search above may land on any of them. Step | |
5056 | one past the matching symbol(s). */ | |
5057 | while (++idx != j) | |
5058 | { | |
5059 | h = sorted_sym_hash[idx]; | |
5060 | if (h->root.u.def.section != slook | |
5061 | || h->root.u.def.value != vlook) | |
5062 | break; | |
5063 | } | |
5064 | ||
5065 | /* Now look back over the aliases. Since we sorted by size | |
5066 | as well as value and section, we'll choose the one with | |
5067 | the largest size. */ | |
5068 | while (idx-- != i) | |
4ad4eba5 | 5069 | { |
14160578 | 5070 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5071 | |
5072 | /* Stop if value or section doesn't match. */ | |
14160578 AM |
5073 | if (h->root.u.def.section != slook |
5074 | || h->root.u.def.value != vlook) | |
4ad4eba5 AM |
5075 | break; |
5076 | else if (h != hlook) | |
5077 | { | |
f6e332e6 | 5078 | hlook->u.weakdef = h; |
4ad4eba5 AM |
5079 | |
5080 | /* If the weak definition is in the list of dynamic | |
5081 | symbols, make sure the real definition is put | |
5082 | there as well. */ | |
5083 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
5084 | { | |
c152c796 | 5085 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4dd07732 AM |
5086 | { |
5087 | err_free_sym_hash: | |
5088 | free (sorted_sym_hash); | |
5089 | goto error_return; | |
5090 | } | |
4ad4eba5 AM |
5091 | } |
5092 | ||
5093 | /* If the real definition is in the list of dynamic | |
5094 | symbols, make sure the weak definition is put | |
5095 | there as well. If we don't do this, then the | |
5096 | dynamic loader might not merge the entries for the | |
5097 | real definition and the weak definition. */ | |
5098 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
5099 | { | |
c152c796 | 5100 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4dd07732 | 5101 | goto err_free_sym_hash; |
4ad4eba5 AM |
5102 | } |
5103 | break; | |
5104 | } | |
5105 | } | |
5106 | } | |
5107 | ||
5108 | free (sorted_sym_hash); | |
5109 | } | |
5110 | ||
33177bb1 AM |
5111 | if (bed->check_directives |
5112 | && !(*bed->check_directives) (abfd, info)) | |
5113 | return FALSE; | |
85fbca6a | 5114 | |
d9689752 L |
5115 | if (!info->check_relocs_after_open_input |
5116 | && !_bfd_elf_link_check_relocs (abfd, info)) | |
5117 | return FALSE; | |
4ad4eba5 AM |
5118 | |
5119 | /* If this is a non-traditional link, try to optimize the handling | |
5120 | of the .stab/.stabstr sections. */ | |
5121 | if (! dynamic | |
5122 | && ! info->traditional_format | |
66eb6687 | 5123 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
5124 | && (info->strip != strip_all && info->strip != strip_debugger)) |
5125 | { | |
5126 | asection *stabstr; | |
5127 | ||
5128 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
5129 | if (stabstr != NULL) | |
5130 | { | |
5131 | bfd_size_type string_offset = 0; | |
5132 | asection *stab; | |
5133 | ||
5134 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 5135 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
5136 | && (!stab->name[5] || |
5137 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
5138 | && (stab->flags & SEC_MERGE) == 0 | |
5139 | && !bfd_is_abs_section (stab->output_section)) | |
5140 | { | |
5141 | struct bfd_elf_section_data *secdata; | |
5142 | ||
5143 | secdata = elf_section_data (stab); | |
66eb6687 AM |
5144 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
5145 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
5146 | &string_offset)) |
5147 | goto error_return; | |
5148 | if (secdata->sec_info) | |
dbaa2011 | 5149 | stab->sec_info_type = SEC_INFO_TYPE_STABS; |
4ad4eba5 AM |
5150 | } |
5151 | } | |
5152 | } | |
5153 | ||
66eb6687 | 5154 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
5155 | { |
5156 | /* Add this bfd to the loaded list. */ | |
5157 | struct elf_link_loaded_list *n; | |
5158 | ||
ca4be51c | 5159 | n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); |
4ad4eba5 AM |
5160 | if (n == NULL) |
5161 | goto error_return; | |
5162 | n->abfd = abfd; | |
66eb6687 AM |
5163 | n->next = htab->loaded; |
5164 | htab->loaded = n; | |
4ad4eba5 AM |
5165 | } |
5166 | ||
5167 | return TRUE; | |
5168 | ||
5169 | error_free_vers: | |
66eb6687 AM |
5170 | if (old_tab != NULL) |
5171 | free (old_tab); | |
5b677558 AM |
5172 | if (old_strtab != NULL) |
5173 | free (old_strtab); | |
4ad4eba5 AM |
5174 | if (nondeflt_vers != NULL) |
5175 | free (nondeflt_vers); | |
5176 | if (extversym != NULL) | |
5177 | free (extversym); | |
5178 | error_free_sym: | |
5179 | if (isymbuf != NULL) | |
5180 | free (isymbuf); | |
5181 | error_return: | |
5182 | return FALSE; | |
5183 | } | |
5184 | ||
8387904d AM |
5185 | /* Return the linker hash table entry of a symbol that might be |
5186 | satisfied by an archive symbol. Return -1 on error. */ | |
5187 | ||
5188 | struct elf_link_hash_entry * | |
5189 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
5190 | struct bfd_link_info *info, | |
5191 | const char *name) | |
5192 | { | |
5193 | struct elf_link_hash_entry *h; | |
5194 | char *p, *copy; | |
5195 | size_t len, first; | |
5196 | ||
2a41f396 | 5197 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); |
8387904d AM |
5198 | if (h != NULL) |
5199 | return h; | |
5200 | ||
5201 | /* If this is a default version (the name contains @@), look up the | |
5202 | symbol again with only one `@' as well as without the version. | |
5203 | The effect is that references to the symbol with and without the | |
5204 | version will be matched by the default symbol in the archive. */ | |
5205 | ||
5206 | p = strchr (name, ELF_VER_CHR); | |
5207 | if (p == NULL || p[1] != ELF_VER_CHR) | |
5208 | return h; | |
5209 | ||
5210 | /* First check with only one `@'. */ | |
5211 | len = strlen (name); | |
a50b1753 | 5212 | copy = (char *) bfd_alloc (abfd, len); |
8387904d AM |
5213 | if (copy == NULL) |
5214 | return (struct elf_link_hash_entry *) 0 - 1; | |
5215 | ||
5216 | first = p - name + 1; | |
5217 | memcpy (copy, name, first); | |
5218 | memcpy (copy + first, name + first + 1, len - first); | |
5219 | ||
2a41f396 | 5220 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); |
8387904d AM |
5221 | if (h == NULL) |
5222 | { | |
5223 | /* We also need to check references to the symbol without the | |
5224 | version. */ | |
5225 | copy[first - 1] = '\0'; | |
5226 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
2a41f396 | 5227 | FALSE, FALSE, TRUE); |
8387904d AM |
5228 | } |
5229 | ||
5230 | bfd_release (abfd, copy); | |
5231 | return h; | |
5232 | } | |
5233 | ||
0ad989f9 | 5234 | /* Add symbols from an ELF archive file to the linker hash table. We |
13e570f8 AM |
5235 | don't use _bfd_generic_link_add_archive_symbols because we need to |
5236 | handle versioned symbols. | |
0ad989f9 L |
5237 | |
5238 | Fortunately, ELF archive handling is simpler than that done by | |
5239 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
5240 | oddities. In ELF, if we find a symbol in the archive map, and the | |
5241 | symbol is currently undefined, we know that we must pull in that | |
5242 | object file. | |
5243 | ||
5244 | Unfortunately, we do have to make multiple passes over the symbol | |
5245 | table until nothing further is resolved. */ | |
5246 | ||
4ad4eba5 AM |
5247 | static bfd_boolean |
5248 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
5249 | { |
5250 | symindex c; | |
13e570f8 | 5251 | unsigned char *included = NULL; |
0ad989f9 L |
5252 | carsym *symdefs; |
5253 | bfd_boolean loop; | |
5254 | bfd_size_type amt; | |
8387904d AM |
5255 | const struct elf_backend_data *bed; |
5256 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
5257 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
5258 | |
5259 | if (! bfd_has_map (abfd)) | |
5260 | { | |
5261 | /* An empty archive is a special case. */ | |
5262 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
5263 | return TRUE; | |
5264 | bfd_set_error (bfd_error_no_armap); | |
5265 | return FALSE; | |
5266 | } | |
5267 | ||
5268 | /* Keep track of all symbols we know to be already defined, and all | |
5269 | files we know to be already included. This is to speed up the | |
5270 | second and subsequent passes. */ | |
5271 | c = bfd_ardata (abfd)->symdef_count; | |
5272 | if (c == 0) | |
5273 | return TRUE; | |
5274 | amt = c; | |
13e570f8 AM |
5275 | amt *= sizeof (*included); |
5276 | included = (unsigned char *) bfd_zmalloc (amt); | |
5277 | if (included == NULL) | |
5278 | return FALSE; | |
0ad989f9 L |
5279 | |
5280 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
5281 | bed = get_elf_backend_data (abfd); |
5282 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
5283 | |
5284 | do | |
5285 | { | |
5286 | file_ptr last; | |
5287 | symindex i; | |
5288 | carsym *symdef; | |
5289 | carsym *symdefend; | |
5290 | ||
5291 | loop = FALSE; | |
5292 | last = -1; | |
5293 | ||
5294 | symdef = symdefs; | |
5295 | symdefend = symdef + c; | |
5296 | for (i = 0; symdef < symdefend; symdef++, i++) | |
5297 | { | |
5298 | struct elf_link_hash_entry *h; | |
5299 | bfd *element; | |
5300 | struct bfd_link_hash_entry *undefs_tail; | |
5301 | symindex mark; | |
5302 | ||
13e570f8 | 5303 | if (included[i]) |
0ad989f9 L |
5304 | continue; |
5305 | if (symdef->file_offset == last) | |
5306 | { | |
5307 | included[i] = TRUE; | |
5308 | continue; | |
5309 | } | |
5310 | ||
8387904d AM |
5311 | h = archive_symbol_lookup (abfd, info, symdef->name); |
5312 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
5313 | goto error_return; | |
0ad989f9 L |
5314 | |
5315 | if (h == NULL) | |
5316 | continue; | |
5317 | ||
5318 | if (h->root.type == bfd_link_hash_common) | |
5319 | { | |
5320 | /* We currently have a common symbol. The archive map contains | |
5321 | a reference to this symbol, so we may want to include it. We | |
5322 | only want to include it however, if this archive element | |
5323 | contains a definition of the symbol, not just another common | |
5324 | declaration of it. | |
5325 | ||
5326 | Unfortunately some archivers (including GNU ar) will put | |
5327 | declarations of common symbols into their archive maps, as | |
5328 | well as real definitions, so we cannot just go by the archive | |
5329 | map alone. Instead we must read in the element's symbol | |
5330 | table and check that to see what kind of symbol definition | |
5331 | this is. */ | |
5332 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
5333 | continue; | |
5334 | } | |
5335 | else if (h->root.type != bfd_link_hash_undefined) | |
5336 | { | |
5337 | if (h->root.type != bfd_link_hash_undefweak) | |
13e570f8 AM |
5338 | /* Symbol must be defined. Don't check it again. */ |
5339 | included[i] = TRUE; | |
0ad989f9 L |
5340 | continue; |
5341 | } | |
5342 | ||
5343 | /* We need to include this archive member. */ | |
5344 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
5345 | if (element == NULL) | |
5346 | goto error_return; | |
5347 | ||
5348 | if (! bfd_check_format (element, bfd_object)) | |
5349 | goto error_return; | |
5350 | ||
0ad989f9 L |
5351 | undefs_tail = info->hash->undefs_tail; |
5352 | ||
0e144ba7 AM |
5353 | if (!(*info->callbacks |
5354 | ->add_archive_element) (info, element, symdef->name, &element)) | |
b95a0a31 | 5355 | continue; |
0e144ba7 | 5356 | if (!bfd_link_add_symbols (element, info)) |
0ad989f9 L |
5357 | goto error_return; |
5358 | ||
5359 | /* If there are any new undefined symbols, we need to make | |
5360 | another pass through the archive in order to see whether | |
5361 | they can be defined. FIXME: This isn't perfect, because | |
5362 | common symbols wind up on undefs_tail and because an | |
5363 | undefined symbol which is defined later on in this pass | |
5364 | does not require another pass. This isn't a bug, but it | |
5365 | does make the code less efficient than it could be. */ | |
5366 | if (undefs_tail != info->hash->undefs_tail) | |
5367 | loop = TRUE; | |
5368 | ||
5369 | /* Look backward to mark all symbols from this object file | |
5370 | which we have already seen in this pass. */ | |
5371 | mark = i; | |
5372 | do | |
5373 | { | |
5374 | included[mark] = TRUE; | |
5375 | if (mark == 0) | |
5376 | break; | |
5377 | --mark; | |
5378 | } | |
5379 | while (symdefs[mark].file_offset == symdef->file_offset); | |
5380 | ||
5381 | /* We mark subsequent symbols from this object file as we go | |
5382 | on through the loop. */ | |
5383 | last = symdef->file_offset; | |
5384 | } | |
5385 | } | |
5386 | while (loop); | |
5387 | ||
0ad989f9 L |
5388 | free (included); |
5389 | ||
5390 | return TRUE; | |
5391 | ||
5392 | error_return: | |
0ad989f9 L |
5393 | if (included != NULL) |
5394 | free (included); | |
5395 | return FALSE; | |
5396 | } | |
4ad4eba5 AM |
5397 | |
5398 | /* Given an ELF BFD, add symbols to the global hash table as | |
5399 | appropriate. */ | |
5400 | ||
5401 | bfd_boolean | |
5402 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
5403 | { | |
5404 | switch (bfd_get_format (abfd)) | |
5405 | { | |
5406 | case bfd_object: | |
5407 | return elf_link_add_object_symbols (abfd, info); | |
5408 | case bfd_archive: | |
5409 | return elf_link_add_archive_symbols (abfd, info); | |
5410 | default: | |
5411 | bfd_set_error (bfd_error_wrong_format); | |
5412 | return FALSE; | |
5413 | } | |
5414 | } | |
5a580b3a | 5415 | \f |
14b1c01e AM |
5416 | struct hash_codes_info |
5417 | { | |
5418 | unsigned long *hashcodes; | |
5419 | bfd_boolean error; | |
5420 | }; | |
a0c8462f | 5421 | |
5a580b3a AM |
5422 | /* This function will be called though elf_link_hash_traverse to store |
5423 | all hash value of the exported symbols in an array. */ | |
5424 | ||
5425 | static bfd_boolean | |
5426 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5427 | { | |
a50b1753 | 5428 | struct hash_codes_info *inf = (struct hash_codes_info *) data; |
5a580b3a | 5429 | const char *name; |
5a580b3a AM |
5430 | unsigned long ha; |
5431 | char *alc = NULL; | |
5432 | ||
5a580b3a AM |
5433 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5434 | if (h->dynindx == -1) | |
5435 | return TRUE; | |
5436 | ||
5437 | name = h->root.root.string; | |
422f1182 | 5438 | if (h->versioned >= versioned) |
5a580b3a | 5439 | { |
422f1182 L |
5440 | char *p = strchr (name, ELF_VER_CHR); |
5441 | if (p != NULL) | |
14b1c01e | 5442 | { |
422f1182 L |
5443 | alc = (char *) bfd_malloc (p - name + 1); |
5444 | if (alc == NULL) | |
5445 | { | |
5446 | inf->error = TRUE; | |
5447 | return FALSE; | |
5448 | } | |
5449 | memcpy (alc, name, p - name); | |
5450 | alc[p - name] = '\0'; | |
5451 | name = alc; | |
14b1c01e | 5452 | } |
5a580b3a AM |
5453 | } |
5454 | ||
5455 | /* Compute the hash value. */ | |
5456 | ha = bfd_elf_hash (name); | |
5457 | ||
5458 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 5459 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
5460 | |
5461 | /* And store it in the struct so that we can put it in the hash table | |
5462 | later. */ | |
f6e332e6 | 5463 | h->u.elf_hash_value = ha; |
5a580b3a AM |
5464 | |
5465 | if (alc != NULL) | |
5466 | free (alc); | |
5467 | ||
5468 | return TRUE; | |
5469 | } | |
5470 | ||
fdc90cb4 JJ |
5471 | struct collect_gnu_hash_codes |
5472 | { | |
5473 | bfd *output_bfd; | |
5474 | const struct elf_backend_data *bed; | |
5475 | unsigned long int nsyms; | |
5476 | unsigned long int maskbits; | |
5477 | unsigned long int *hashcodes; | |
5478 | unsigned long int *hashval; | |
5479 | unsigned long int *indx; | |
5480 | unsigned long int *counts; | |
5481 | bfd_vma *bitmask; | |
5482 | bfd_byte *contents; | |
5483 | long int min_dynindx; | |
5484 | unsigned long int bucketcount; | |
5485 | unsigned long int symindx; | |
5486 | long int local_indx; | |
5487 | long int shift1, shift2; | |
5488 | unsigned long int mask; | |
14b1c01e | 5489 | bfd_boolean error; |
fdc90cb4 JJ |
5490 | }; |
5491 | ||
5492 | /* This function will be called though elf_link_hash_traverse to store | |
5493 | all hash value of the exported symbols in an array. */ | |
5494 | ||
5495 | static bfd_boolean | |
5496 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5497 | { | |
a50b1753 | 5498 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 | 5499 | const char *name; |
fdc90cb4 JJ |
5500 | unsigned long ha; |
5501 | char *alc = NULL; | |
5502 | ||
fdc90cb4 JJ |
5503 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5504 | if (h->dynindx == -1) | |
5505 | return TRUE; | |
5506 | ||
5507 | /* Ignore also local symbols and undefined symbols. */ | |
5508 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5509 | return TRUE; | |
5510 | ||
5511 | name = h->root.root.string; | |
422f1182 | 5512 | if (h->versioned >= versioned) |
fdc90cb4 | 5513 | { |
422f1182 L |
5514 | char *p = strchr (name, ELF_VER_CHR); |
5515 | if (p != NULL) | |
14b1c01e | 5516 | { |
422f1182 L |
5517 | alc = (char *) bfd_malloc (p - name + 1); |
5518 | if (alc == NULL) | |
5519 | { | |
5520 | s->error = TRUE; | |
5521 | return FALSE; | |
5522 | } | |
5523 | memcpy (alc, name, p - name); | |
5524 | alc[p - name] = '\0'; | |
5525 | name = alc; | |
14b1c01e | 5526 | } |
fdc90cb4 JJ |
5527 | } |
5528 | ||
5529 | /* Compute the hash value. */ | |
5530 | ha = bfd_elf_gnu_hash (name); | |
5531 | ||
5532 | /* Store the found hash value in the array for compute_bucket_count, | |
5533 | and also for .dynsym reordering purposes. */ | |
5534 | s->hashcodes[s->nsyms] = ha; | |
5535 | s->hashval[h->dynindx] = ha; | |
5536 | ++s->nsyms; | |
5537 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5538 | s->min_dynindx = h->dynindx; | |
5539 | ||
5540 | if (alc != NULL) | |
5541 | free (alc); | |
5542 | ||
5543 | return TRUE; | |
5544 | } | |
5545 | ||
5546 | /* This function will be called though elf_link_hash_traverse to do | |
5547 | final dynaminc symbol renumbering. */ | |
5548 | ||
5549 | static bfd_boolean | |
5550 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5551 | { | |
a50b1753 | 5552 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
5553 | unsigned long int bucket; |
5554 | unsigned long int val; | |
5555 | ||
fdc90cb4 JJ |
5556 | /* Ignore indirect symbols. */ |
5557 | if (h->dynindx == -1) | |
5558 | return TRUE; | |
5559 | ||
5560 | /* Ignore also local symbols and undefined symbols. */ | |
5561 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5562 | { | |
5563 | if (h->dynindx >= s->min_dynindx) | |
5564 | h->dynindx = s->local_indx++; | |
5565 | return TRUE; | |
5566 | } | |
5567 | ||
5568 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5569 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5570 | & ((s->maskbits >> s->shift1) - 1); | |
5571 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5572 | s->bitmask[val] | |
5573 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5574 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5575 | if (s->counts[bucket] == 1) | |
5576 | /* Last element terminates the chain. */ | |
5577 | val |= 1; | |
5578 | bfd_put_32 (s->output_bfd, val, | |
5579 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5580 | --s->counts[bucket]; | |
5581 | h->dynindx = s->indx[bucket]++; | |
5582 | return TRUE; | |
5583 | } | |
5584 | ||
5585 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5586 | ||
5587 | bfd_boolean | |
5588 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5589 | { | |
5590 | return !(h->forced_local | |
5591 | || h->root.type == bfd_link_hash_undefined | |
5592 | || h->root.type == bfd_link_hash_undefweak | |
5593 | || ((h->root.type == bfd_link_hash_defined | |
5594 | || h->root.type == bfd_link_hash_defweak) | |
5595 | && h->root.u.def.section->output_section == NULL)); | |
5596 | } | |
5597 | ||
5a580b3a AM |
5598 | /* Array used to determine the number of hash table buckets to use |
5599 | based on the number of symbols there are. If there are fewer than | |
5600 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5601 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5602 | than 32771 buckets. */ | |
5603 | ||
5604 | static const size_t elf_buckets[] = | |
5605 | { | |
5606 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5607 | 16411, 32771, 0 | |
5608 | }; | |
5609 | ||
5610 | /* Compute bucket count for hashing table. We do not use a static set | |
5611 | of possible tables sizes anymore. Instead we determine for all | |
5612 | possible reasonable sizes of the table the outcome (i.e., the | |
5613 | number of collisions etc) and choose the best solution. The | |
5614 | weighting functions are not too simple to allow the table to grow | |
5615 | without bounds. Instead one of the weighting factors is the size. | |
5616 | Therefore the result is always a good payoff between few collisions | |
5617 | (= short chain lengths) and table size. */ | |
5618 | static size_t | |
b20dd2ce | 5619 | compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
d40f3da9 AM |
5620 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, |
5621 | unsigned long int nsyms, | |
5622 | int gnu_hash) | |
5a580b3a | 5623 | { |
5a580b3a | 5624 | size_t best_size = 0; |
5a580b3a | 5625 | unsigned long int i; |
5a580b3a | 5626 | |
5a580b3a AM |
5627 | /* We have a problem here. The following code to optimize the table |
5628 | size requires an integer type with more the 32 bits. If | |
5629 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5630 | #ifdef BFD_HOST_U_64_BIT | |
5631 | if (info->optimize) | |
5632 | { | |
5a580b3a AM |
5633 | size_t minsize; |
5634 | size_t maxsize; | |
5635 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a | 5636 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 5637 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 5638 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 5639 | unsigned long int *counts; |
d40f3da9 | 5640 | bfd_size_type amt; |
0883b6e0 | 5641 | unsigned int no_improvement_count = 0; |
5a580b3a AM |
5642 | |
5643 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5644 | that the hashing table must at least have NSYMS/4 and at most | |
5645 | 2*NSYMS buckets. */ | |
5646 | minsize = nsyms / 4; | |
5647 | if (minsize == 0) | |
5648 | minsize = 1; | |
5649 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5650 | if (gnu_hash) |
5651 | { | |
5652 | if (minsize < 2) | |
5653 | minsize = 2; | |
5654 | if ((best_size & 31) == 0) | |
5655 | ++best_size; | |
5656 | } | |
5a580b3a AM |
5657 | |
5658 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5659 | since the size could be large. */ | |
5660 | amt = maxsize; | |
5661 | amt *= sizeof (unsigned long int); | |
a50b1753 | 5662 | counts = (unsigned long int *) bfd_malloc (amt); |
5a580b3a | 5663 | if (counts == NULL) |
fdc90cb4 | 5664 | return 0; |
5a580b3a AM |
5665 | |
5666 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5667 | minimal chain length. The minor criteria is (of course) the size | |
5668 | of the table. */ | |
5669 | for (i = minsize; i < maxsize; ++i) | |
5670 | { | |
5671 | /* Walk through the array of hashcodes and count the collisions. */ | |
5672 | BFD_HOST_U_64_BIT max; | |
5673 | unsigned long int j; | |
5674 | unsigned long int fact; | |
5675 | ||
fdc90cb4 JJ |
5676 | if (gnu_hash && (i & 31) == 0) |
5677 | continue; | |
5678 | ||
5a580b3a AM |
5679 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5680 | ||
5681 | /* Determine how often each hash bucket is used. */ | |
5682 | for (j = 0; j < nsyms; ++j) | |
5683 | ++counts[hashcodes[j] % i]; | |
5684 | ||
5685 | /* For the weight function we need some information about the | |
5686 | pagesize on the target. This is information need not be 100% | |
5687 | accurate. Since this information is not available (so far) we | |
5688 | define it here to a reasonable default value. If it is crucial | |
5689 | to have a better value some day simply define this value. */ | |
5690 | # ifndef BFD_TARGET_PAGESIZE | |
5691 | # define BFD_TARGET_PAGESIZE (4096) | |
5692 | # endif | |
5693 | ||
fdc90cb4 JJ |
5694 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5695 | and the chains. */ | |
5696 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
5697 | |
5698 | # if 1 | |
5699 | /* Variant 1: optimize for short chains. We add the squares | |
5700 | of all the chain lengths (which favors many small chain | |
5701 | over a few long chains). */ | |
5702 | for (j = 0; j < i; ++j) | |
5703 | max += counts[j] * counts[j]; | |
5704 | ||
5705 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 5706 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5707 | max *= fact * fact; |
5708 | # else | |
5709 | /* Variant 2: Optimize a lot more for small table. Here we | |
5710 | also add squares of the size but we also add penalties for | |
5711 | empty slots (the +1 term). */ | |
5712 | for (j = 0; j < i; ++j) | |
5713 | max += (1 + counts[j]) * (1 + counts[j]); | |
5714 | ||
5715 | /* The overall size of the table is considered, but not as | |
5716 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 5717 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5718 | max *= fact; |
5719 | # endif | |
5720 | ||
5721 | /* Compare with current best results. */ | |
5722 | if (max < best_chlen) | |
5723 | { | |
5724 | best_chlen = max; | |
5725 | best_size = i; | |
ca4be51c | 5726 | no_improvement_count = 0; |
5a580b3a | 5727 | } |
0883b6e0 NC |
5728 | /* PR 11843: Avoid futile long searches for the best bucket size |
5729 | when there are a large number of symbols. */ | |
5730 | else if (++no_improvement_count == 100) | |
5731 | break; | |
5a580b3a AM |
5732 | } |
5733 | ||
5734 | free (counts); | |
5735 | } | |
5736 | else | |
5737 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
5738 | { | |
5739 | /* This is the fallback solution if no 64bit type is available or if we | |
5740 | are not supposed to spend much time on optimizations. We select the | |
5741 | bucket count using a fixed set of numbers. */ | |
5742 | for (i = 0; elf_buckets[i] != 0; i++) | |
5743 | { | |
5744 | best_size = elf_buckets[i]; | |
fdc90cb4 | 5745 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
5746 | break; |
5747 | } | |
fdc90cb4 JJ |
5748 | if (gnu_hash && best_size < 2) |
5749 | best_size = 2; | |
5a580b3a AM |
5750 | } |
5751 | ||
5a580b3a AM |
5752 | return best_size; |
5753 | } | |
5754 | ||
d0bf826b AM |
5755 | /* Size any SHT_GROUP section for ld -r. */ |
5756 | ||
5757 | bfd_boolean | |
5758 | _bfd_elf_size_group_sections (struct bfd_link_info *info) | |
5759 | { | |
5760 | bfd *ibfd; | |
5761 | ||
c72f2fb2 | 5762 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
d0bf826b AM |
5763 | if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour |
5764 | && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) | |
5765 | return FALSE; | |
5766 | return TRUE; | |
5767 | } | |
5768 | ||
04c3a755 NS |
5769 | /* Set a default stack segment size. The value in INFO wins. If it |
5770 | is unset, LEGACY_SYMBOL's value is used, and if that symbol is | |
5771 | undefined it is initialized. */ | |
5772 | ||
5773 | bfd_boolean | |
5774 | bfd_elf_stack_segment_size (bfd *output_bfd, | |
5775 | struct bfd_link_info *info, | |
5776 | const char *legacy_symbol, | |
5777 | bfd_vma default_size) | |
5778 | { | |
5779 | struct elf_link_hash_entry *h = NULL; | |
5780 | ||
5781 | /* Look for legacy symbol. */ | |
5782 | if (legacy_symbol) | |
5783 | h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, | |
5784 | FALSE, FALSE, FALSE); | |
5785 | if (h && (h->root.type == bfd_link_hash_defined | |
5786 | || h->root.type == bfd_link_hash_defweak) | |
5787 | && h->def_regular | |
5788 | && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) | |
5789 | { | |
5790 | /* The symbol has no type if specified on the command line. */ | |
5791 | h->type = STT_OBJECT; | |
5792 | if (info->stacksize) | |
695344c0 | 5793 | /* xgettext:c-format */ |
4eca0228 AM |
5794 | _bfd_error_handler (_("%B: stack size specified and %s set"), |
5795 | output_bfd, legacy_symbol); | |
04c3a755 | 5796 | else if (h->root.u.def.section != bfd_abs_section_ptr) |
695344c0 | 5797 | /* xgettext:c-format */ |
4eca0228 AM |
5798 | _bfd_error_handler (_("%B: %s not absolute"), |
5799 | output_bfd, legacy_symbol); | |
04c3a755 NS |
5800 | else |
5801 | info->stacksize = h->root.u.def.value; | |
5802 | } | |
5803 | ||
5804 | if (!info->stacksize) | |
5805 | /* If the user didn't set a size, or explicitly inhibit the | |
5806 | size, set it now. */ | |
5807 | info->stacksize = default_size; | |
5808 | ||
5809 | /* Provide the legacy symbol, if it is referenced. */ | |
5810 | if (h && (h->root.type == bfd_link_hash_undefined | |
5811 | || h->root.type == bfd_link_hash_undefweak)) | |
5812 | { | |
5813 | struct bfd_link_hash_entry *bh = NULL; | |
5814 | ||
5815 | if (!(_bfd_generic_link_add_one_symbol | |
5816 | (info, output_bfd, legacy_symbol, | |
5817 | BSF_GLOBAL, bfd_abs_section_ptr, | |
5818 | info->stacksize >= 0 ? info->stacksize : 0, | |
5819 | NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh))) | |
5820 | return FALSE; | |
5821 | ||
5822 | h = (struct elf_link_hash_entry *) bh; | |
5823 | h->def_regular = 1; | |
5824 | h->type = STT_OBJECT; | |
5825 | } | |
5826 | ||
5827 | return TRUE; | |
5828 | } | |
5829 | ||
5a580b3a AM |
5830 | /* Set up the sizes and contents of the ELF dynamic sections. This is |
5831 | called by the ELF linker emulation before_allocation routine. We | |
5832 | must set the sizes of the sections before the linker sets the | |
5833 | addresses of the various sections. */ | |
5834 | ||
5835 | bfd_boolean | |
5836 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
5837 | const char *soname, | |
5838 | const char *rpath, | |
5839 | const char *filter_shlib, | |
7ee314fa AM |
5840 | const char *audit, |
5841 | const char *depaudit, | |
5a580b3a AM |
5842 | const char * const *auxiliary_filters, |
5843 | struct bfd_link_info *info, | |
fd91d419 | 5844 | asection **sinterpptr) |
5a580b3a | 5845 | { |
ef53be89 | 5846 | size_t soname_indx; |
5a580b3a AM |
5847 | bfd *dynobj; |
5848 | const struct elf_backend_data *bed; | |
28caa186 | 5849 | struct elf_info_failed asvinfo; |
5a580b3a AM |
5850 | |
5851 | *sinterpptr = NULL; | |
5852 | ||
ef53be89 | 5853 | soname_indx = (size_t) -1; |
5a580b3a AM |
5854 | |
5855 | if (!is_elf_hash_table (info->hash)) | |
5856 | return TRUE; | |
5857 | ||
6bfdb61b | 5858 | bed = get_elf_backend_data (output_bfd); |
04c3a755 NS |
5859 | |
5860 | /* Any syms created from now on start with -1 in | |
5861 | got.refcount/offset and plt.refcount/offset. */ | |
5862 | elf_hash_table (info)->init_got_refcount | |
5863 | = elf_hash_table (info)->init_got_offset; | |
5864 | elf_hash_table (info)->init_plt_refcount | |
5865 | = elf_hash_table (info)->init_plt_offset; | |
5866 | ||
0e1862bb | 5867 | if (bfd_link_relocatable (info) |
04c3a755 NS |
5868 | && !_bfd_elf_size_group_sections (info)) |
5869 | return FALSE; | |
5870 | ||
5871 | /* The backend may have to create some sections regardless of whether | |
5872 | we're dynamic or not. */ | |
5873 | if (bed->elf_backend_always_size_sections | |
5874 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
5875 | return FALSE; | |
5876 | ||
5877 | /* Determine any GNU_STACK segment requirements, after the backend | |
5878 | has had a chance to set a default segment size. */ | |
5a580b3a | 5879 | if (info->execstack) |
12bd6957 | 5880 | elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; |
5a580b3a | 5881 | else if (info->noexecstack) |
12bd6957 | 5882 | elf_stack_flags (output_bfd) = PF_R | PF_W; |
5a580b3a AM |
5883 | else |
5884 | { | |
5885 | bfd *inputobj; | |
5886 | asection *notesec = NULL; | |
5887 | int exec = 0; | |
5888 | ||
5889 | for (inputobj = info->input_bfds; | |
5890 | inputobj; | |
c72f2fb2 | 5891 | inputobj = inputobj->link.next) |
5a580b3a AM |
5892 | { |
5893 | asection *s; | |
5894 | ||
a92c088a L |
5895 | if (inputobj->flags |
5896 | & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) | |
5a580b3a AM |
5897 | continue; |
5898 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
5899 | if (s) | |
5900 | { | |
5901 | if (s->flags & SEC_CODE) | |
5902 | exec = PF_X; | |
5903 | notesec = s; | |
5904 | } | |
6bfdb61b | 5905 | else if (bed->default_execstack) |
5a580b3a AM |
5906 | exec = PF_X; |
5907 | } | |
04c3a755 | 5908 | if (notesec || info->stacksize > 0) |
12bd6957 | 5909 | elf_stack_flags (output_bfd) = PF_R | PF_W | exec; |
0e1862bb | 5910 | if (notesec && exec && bfd_link_relocatable (info) |
04c3a755 NS |
5911 | && notesec->output_section != bfd_abs_section_ptr) |
5912 | notesec->output_section->flags |= SEC_CODE; | |
5a580b3a AM |
5913 | } |
5914 | ||
5a580b3a AM |
5915 | dynobj = elf_hash_table (info)->dynobj; |
5916 | ||
9a2a56cc | 5917 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a AM |
5918 | { |
5919 | struct elf_info_failed eif; | |
5920 | struct elf_link_hash_entry *h; | |
5921 | asection *dynstr; | |
5922 | struct bfd_elf_version_tree *t; | |
5923 | struct bfd_elf_version_expr *d; | |
046183de | 5924 | asection *s; |
5a580b3a AM |
5925 | bfd_boolean all_defined; |
5926 | ||
3d4d4302 | 5927 | *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); |
9b8b325a | 5928 | BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); |
5a580b3a AM |
5929 | |
5930 | if (soname != NULL) | |
5931 | { | |
5932 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5933 | soname, TRUE); | |
ef53be89 | 5934 | if (soname_indx == (size_t) -1 |
5a580b3a AM |
5935 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) |
5936 | return FALSE; | |
5937 | } | |
5938 | ||
5939 | if (info->symbolic) | |
5940 | { | |
5941 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
5942 | return FALSE; | |
5943 | info->flags |= DF_SYMBOLIC; | |
5944 | } | |
5945 | ||
5946 | if (rpath != NULL) | |
5947 | { | |
ef53be89 | 5948 | size_t indx; |
b1b00fcc | 5949 | bfd_vma tag; |
5a580b3a AM |
5950 | |
5951 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
5952 | TRUE); | |
ef53be89 | 5953 | if (indx == (size_t) -1) |
5a580b3a AM |
5954 | return FALSE; |
5955 | ||
b1b00fcc MF |
5956 | tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; |
5957 | if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) | |
5958 | return FALSE; | |
5a580b3a AM |
5959 | } |
5960 | ||
5961 | if (filter_shlib != NULL) | |
5962 | { | |
ef53be89 | 5963 | size_t indx; |
5a580b3a AM |
5964 | |
5965 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5966 | filter_shlib, TRUE); | |
ef53be89 | 5967 | if (indx == (size_t) -1 |
5a580b3a AM |
5968 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) |
5969 | return FALSE; | |
5970 | } | |
5971 | ||
5972 | if (auxiliary_filters != NULL) | |
5973 | { | |
5974 | const char * const *p; | |
5975 | ||
5976 | for (p = auxiliary_filters; *p != NULL; p++) | |
5977 | { | |
ef53be89 | 5978 | size_t indx; |
5a580b3a AM |
5979 | |
5980 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5981 | *p, TRUE); | |
ef53be89 | 5982 | if (indx == (size_t) -1 |
5a580b3a AM |
5983 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) |
5984 | return FALSE; | |
5985 | } | |
5986 | } | |
5987 | ||
7ee314fa AM |
5988 | if (audit != NULL) |
5989 | { | |
ef53be89 | 5990 | size_t indx; |
7ee314fa AM |
5991 | |
5992 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, | |
5993 | TRUE); | |
ef53be89 | 5994 | if (indx == (size_t) -1 |
7ee314fa AM |
5995 | || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) |
5996 | return FALSE; | |
5997 | } | |
5998 | ||
5999 | if (depaudit != NULL) | |
6000 | { | |
ef53be89 | 6001 | size_t indx; |
7ee314fa AM |
6002 | |
6003 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, | |
6004 | TRUE); | |
ef53be89 | 6005 | if (indx == (size_t) -1 |
7ee314fa AM |
6006 | || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) |
6007 | return FALSE; | |
6008 | } | |
6009 | ||
5a580b3a | 6010 | eif.info = info; |
5a580b3a AM |
6011 | eif.failed = FALSE; |
6012 | ||
6013 | /* If we are supposed to export all symbols into the dynamic symbol | |
6014 | table (this is not the normal case), then do so. */ | |
55255dae | 6015 | if (info->export_dynamic |
0e1862bb | 6016 | || (bfd_link_executable (info) && info->dynamic)) |
5a580b3a AM |
6017 | { |
6018 | elf_link_hash_traverse (elf_hash_table (info), | |
6019 | _bfd_elf_export_symbol, | |
6020 | &eif); | |
6021 | if (eif.failed) | |
6022 | return FALSE; | |
6023 | } | |
6024 | ||
6025 | /* Make all global versions with definition. */ | |
fd91d419 | 6026 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6027 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6028 | if (!d->symver && d->literal) |
5a580b3a AM |
6029 | { |
6030 | const char *verstr, *name; | |
6031 | size_t namelen, verlen, newlen; | |
93252b1c | 6032 | char *newname, *p, leading_char; |
5a580b3a AM |
6033 | struct elf_link_hash_entry *newh; |
6034 | ||
93252b1c | 6035 | leading_char = bfd_get_symbol_leading_char (output_bfd); |
ae5a3597 | 6036 | name = d->pattern; |
93252b1c | 6037 | namelen = strlen (name) + (leading_char != '\0'); |
5a580b3a AM |
6038 | verstr = t->name; |
6039 | verlen = strlen (verstr); | |
6040 | newlen = namelen + verlen + 3; | |
6041 | ||
a50b1753 | 6042 | newname = (char *) bfd_malloc (newlen); |
5a580b3a AM |
6043 | if (newname == NULL) |
6044 | return FALSE; | |
93252b1c MF |
6045 | newname[0] = leading_char; |
6046 | memcpy (newname + (leading_char != '\0'), name, namelen); | |
5a580b3a AM |
6047 | |
6048 | /* Check the hidden versioned definition. */ | |
6049 | p = newname + namelen; | |
6050 | *p++ = ELF_VER_CHR; | |
6051 | memcpy (p, verstr, verlen + 1); | |
6052 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6053 | newname, FALSE, FALSE, | |
6054 | FALSE); | |
6055 | if (newh == NULL | |
6056 | || (newh->root.type != bfd_link_hash_defined | |
6057 | && newh->root.type != bfd_link_hash_defweak)) | |
6058 | { | |
6059 | /* Check the default versioned definition. */ | |
6060 | *p++ = ELF_VER_CHR; | |
6061 | memcpy (p, verstr, verlen + 1); | |
6062 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6063 | newname, FALSE, FALSE, | |
6064 | FALSE); | |
6065 | } | |
6066 | free (newname); | |
6067 | ||
6068 | /* Mark this version if there is a definition and it is | |
6069 | not defined in a shared object. */ | |
6070 | if (newh != NULL | |
f5385ebf | 6071 | && !newh->def_dynamic |
5a580b3a AM |
6072 | && (newh->root.type == bfd_link_hash_defined |
6073 | || newh->root.type == bfd_link_hash_defweak)) | |
6074 | d->symver = 1; | |
6075 | } | |
6076 | ||
6077 | /* Attach all the symbols to their version information. */ | |
5a580b3a | 6078 | asvinfo.info = info; |
5a580b3a AM |
6079 | asvinfo.failed = FALSE; |
6080 | ||
6081 | elf_link_hash_traverse (elf_hash_table (info), | |
6082 | _bfd_elf_link_assign_sym_version, | |
6083 | &asvinfo); | |
6084 | if (asvinfo.failed) | |
6085 | return FALSE; | |
6086 | ||
6087 | if (!info->allow_undefined_version) | |
6088 | { | |
6089 | /* Check if all global versions have a definition. */ | |
6090 | all_defined = TRUE; | |
fd91d419 | 6091 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6092 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6093 | if (d->literal && !d->symver && !d->script) |
5a580b3a | 6094 | { |
4eca0228 | 6095 | _bfd_error_handler |
5a580b3a AM |
6096 | (_("%s: undefined version: %s"), |
6097 | d->pattern, t->name); | |
6098 | all_defined = FALSE; | |
6099 | } | |
6100 | ||
6101 | if (!all_defined) | |
6102 | { | |
6103 | bfd_set_error (bfd_error_bad_value); | |
6104 | return FALSE; | |
6105 | } | |
6106 | } | |
6107 | ||
6108 | /* Find all symbols which were defined in a dynamic object and make | |
6109 | the backend pick a reasonable value for them. */ | |
6110 | elf_link_hash_traverse (elf_hash_table (info), | |
6111 | _bfd_elf_adjust_dynamic_symbol, | |
6112 | &eif); | |
6113 | if (eif.failed) | |
6114 | return FALSE; | |
6115 | ||
6116 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 6117 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
6118 | now so that we know the final size of the .dynamic section. */ |
6119 | ||
6120 | /* If there are initialization and/or finalization functions to | |
6121 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
6122 | h = (info->init_function | |
6123 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6124 | info->init_function, FALSE, | |
6125 | FALSE, FALSE) | |
6126 | : NULL); | |
6127 | if (h != NULL | |
f5385ebf AM |
6128 | && (h->ref_regular |
6129 | || h->def_regular)) | |
5a580b3a AM |
6130 | { |
6131 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
6132 | return FALSE; | |
6133 | } | |
6134 | h = (info->fini_function | |
6135 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6136 | info->fini_function, FALSE, | |
6137 | FALSE, FALSE) | |
6138 | : NULL); | |
6139 | if (h != NULL | |
f5385ebf AM |
6140 | && (h->ref_regular |
6141 | || h->def_regular)) | |
5a580b3a AM |
6142 | { |
6143 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
6144 | return FALSE; | |
6145 | } | |
6146 | ||
046183de AM |
6147 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); |
6148 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
6149 | { |
6150 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
0e1862bb | 6151 | if (! bfd_link_executable (info)) |
5a580b3a AM |
6152 | { |
6153 | bfd *sub; | |
6154 | asection *o; | |
6155 | ||
6156 | for (sub = info->input_bfds; sub != NULL; | |
c72f2fb2 | 6157 | sub = sub->link.next) |
3fcd97f1 JJ |
6158 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour) |
6159 | for (o = sub->sections; o != NULL; o = o->next) | |
6160 | if (elf_section_data (o)->this_hdr.sh_type | |
6161 | == SHT_PREINIT_ARRAY) | |
6162 | { | |
4eca0228 | 6163 | _bfd_error_handler |
3fcd97f1 JJ |
6164 | (_("%B: .preinit_array section is not allowed in DSO"), |
6165 | sub); | |
6166 | break; | |
6167 | } | |
5a580b3a AM |
6168 | |
6169 | bfd_set_error (bfd_error_nonrepresentable_section); | |
6170 | return FALSE; | |
6171 | } | |
6172 | ||
6173 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
6174 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
6175 | return FALSE; | |
6176 | } | |
046183de AM |
6177 | s = bfd_get_section_by_name (output_bfd, ".init_array"); |
6178 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
6179 | { |
6180 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
6181 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
6182 | return FALSE; | |
6183 | } | |
046183de AM |
6184 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); |
6185 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
6186 | { |
6187 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
6188 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
6189 | return FALSE; | |
6190 | } | |
6191 | ||
3d4d4302 | 6192 | dynstr = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
6193 | /* If .dynstr is excluded from the link, we don't want any of |
6194 | these tags. Strictly, we should be checking each section | |
6195 | individually; This quick check covers for the case where | |
6196 | someone does a /DISCARD/ : { *(*) }. */ | |
6197 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
6198 | { | |
6199 | bfd_size_type strsize; | |
6200 | ||
6201 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
fdc90cb4 JJ |
6202 | if ((info->emit_hash |
6203 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
6204 | || (info->emit_gnu_hash | |
6205 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
5a580b3a AM |
6206 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) |
6207 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
6208 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
6209 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
6210 | bed->s->sizeof_sym)) | |
6211 | return FALSE; | |
6212 | } | |
6213 | } | |
6214 | ||
de231f20 CM |
6215 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) |
6216 | return FALSE; | |
6217 | ||
5a580b3a AM |
6218 | /* The backend must work out the sizes of all the other dynamic |
6219 | sections. */ | |
9a2a56cc AM |
6220 | if (dynobj != NULL |
6221 | && bed->elf_backend_size_dynamic_sections != NULL | |
5a580b3a AM |
6222 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) |
6223 | return FALSE; | |
6224 | ||
9a2a56cc | 6225 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a | 6226 | { |
554220db | 6227 | unsigned long section_sym_count; |
fd91d419 | 6228 | struct bfd_elf_version_tree *verdefs; |
5a580b3a | 6229 | asection *s; |
5a580b3a AM |
6230 | |
6231 | /* Set up the version definition section. */ | |
3d4d4302 | 6232 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
6233 | BFD_ASSERT (s != NULL); |
6234 | ||
6235 | /* We may have created additional version definitions if we are | |
6236 | just linking a regular application. */ | |
fd91d419 | 6237 | verdefs = info->version_info; |
5a580b3a AM |
6238 | |
6239 | /* Skip anonymous version tag. */ | |
6240 | if (verdefs != NULL && verdefs->vernum == 0) | |
6241 | verdefs = verdefs->next; | |
6242 | ||
3e3b46e5 | 6243 | if (verdefs == NULL && !info->create_default_symver) |
8423293d | 6244 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
6245 | else |
6246 | { | |
6247 | unsigned int cdefs; | |
6248 | bfd_size_type size; | |
6249 | struct bfd_elf_version_tree *t; | |
6250 | bfd_byte *p; | |
6251 | Elf_Internal_Verdef def; | |
6252 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
6253 | struct bfd_link_hash_entry *bh; |
6254 | struct elf_link_hash_entry *h; | |
6255 | const char *name; | |
5a580b3a AM |
6256 | |
6257 | cdefs = 0; | |
6258 | size = 0; | |
6259 | ||
6260 | /* Make space for the base version. */ | |
6261 | size += sizeof (Elf_External_Verdef); | |
6262 | size += sizeof (Elf_External_Verdaux); | |
6263 | ++cdefs; | |
6264 | ||
3e3b46e5 PB |
6265 | /* Make space for the default version. */ |
6266 | if (info->create_default_symver) | |
6267 | { | |
6268 | size += sizeof (Elf_External_Verdef); | |
6269 | ++cdefs; | |
6270 | } | |
6271 | ||
5a580b3a AM |
6272 | for (t = verdefs; t != NULL; t = t->next) |
6273 | { | |
6274 | struct bfd_elf_version_deps *n; | |
6275 | ||
a6cc6b3b RO |
6276 | /* Don't emit base version twice. */ |
6277 | if (t->vernum == 0) | |
6278 | continue; | |
6279 | ||
5a580b3a AM |
6280 | size += sizeof (Elf_External_Verdef); |
6281 | size += sizeof (Elf_External_Verdaux); | |
6282 | ++cdefs; | |
6283 | ||
6284 | for (n = t->deps; n != NULL; n = n->next) | |
6285 | size += sizeof (Elf_External_Verdaux); | |
6286 | } | |
6287 | ||
eea6121a | 6288 | s->size = size; |
a50b1753 | 6289 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
eea6121a | 6290 | if (s->contents == NULL && s->size != 0) |
5a580b3a AM |
6291 | return FALSE; |
6292 | ||
6293 | /* Fill in the version definition section. */ | |
6294 | ||
6295 | p = s->contents; | |
6296 | ||
6297 | def.vd_version = VER_DEF_CURRENT; | |
6298 | def.vd_flags = VER_FLG_BASE; | |
6299 | def.vd_ndx = 1; | |
6300 | def.vd_cnt = 1; | |
3e3b46e5 PB |
6301 | if (info->create_default_symver) |
6302 | { | |
6303 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
6304 | def.vd_next = sizeof (Elf_External_Verdef); | |
6305 | } | |
6306 | else | |
6307 | { | |
6308 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6309 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6310 | + sizeof (Elf_External_Verdaux)); | |
6311 | } | |
5a580b3a | 6312 | |
ef53be89 | 6313 | if (soname_indx != (size_t) -1) |
5a580b3a AM |
6314 | { |
6315 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6316 | soname_indx); | |
6317 | def.vd_hash = bfd_elf_hash (soname); | |
6318 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 6319 | name = soname; |
5a580b3a AM |
6320 | } |
6321 | else | |
6322 | { | |
ef53be89 | 6323 | size_t indx; |
5a580b3a | 6324 | |
06084812 | 6325 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
6326 | def.vd_hash = bfd_elf_hash (name); |
6327 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6328 | name, FALSE); | |
ef53be89 | 6329 | if (indx == (size_t) -1) |
5a580b3a AM |
6330 | return FALSE; |
6331 | defaux.vda_name = indx; | |
6332 | } | |
6333 | defaux.vda_next = 0; | |
6334 | ||
6335 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6336 | (Elf_External_Verdef *) p); | |
6337 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
6338 | if (info->create_default_symver) |
6339 | { | |
6340 | /* Add a symbol representing this version. */ | |
6341 | bh = NULL; | |
6342 | if (! (_bfd_generic_link_add_one_symbol | |
6343 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6344 | 0, NULL, FALSE, | |
6345 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6346 | return FALSE; | |
6347 | h = (struct elf_link_hash_entry *) bh; | |
6348 | h->non_elf = 0; | |
6349 | h->def_regular = 1; | |
6350 | h->type = STT_OBJECT; | |
6351 | h->verinfo.vertree = NULL; | |
6352 | ||
6353 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
6354 | return FALSE; | |
6355 | ||
6356 | /* Create a duplicate of the base version with the same | |
6357 | aux block, but different flags. */ | |
6358 | def.vd_flags = 0; | |
6359 | def.vd_ndx = 2; | |
6360 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6361 | if (verdefs) | |
6362 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6363 | + sizeof (Elf_External_Verdaux)); | |
6364 | else | |
6365 | def.vd_next = 0; | |
6366 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6367 | (Elf_External_Verdef *) p); | |
6368 | p += sizeof (Elf_External_Verdef); | |
6369 | } | |
5a580b3a AM |
6370 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
6371 | (Elf_External_Verdaux *) p); | |
6372 | p += sizeof (Elf_External_Verdaux); | |
6373 | ||
6374 | for (t = verdefs; t != NULL; t = t->next) | |
6375 | { | |
6376 | unsigned int cdeps; | |
6377 | struct bfd_elf_version_deps *n; | |
5a580b3a | 6378 | |
a6cc6b3b RO |
6379 | /* Don't emit the base version twice. */ |
6380 | if (t->vernum == 0) | |
6381 | continue; | |
6382 | ||
5a580b3a AM |
6383 | cdeps = 0; |
6384 | for (n = t->deps; n != NULL; n = n->next) | |
6385 | ++cdeps; | |
6386 | ||
6387 | /* Add a symbol representing this version. */ | |
6388 | bh = NULL; | |
6389 | if (! (_bfd_generic_link_add_one_symbol | |
6390 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6391 | 0, NULL, FALSE, | |
6392 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6393 | return FALSE; | |
6394 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
6395 | h->non_elf = 0; |
6396 | h->def_regular = 1; | |
5a580b3a AM |
6397 | h->type = STT_OBJECT; |
6398 | h->verinfo.vertree = t; | |
6399 | ||
c152c796 | 6400 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
6401 | return FALSE; |
6402 | ||
6403 | def.vd_version = VER_DEF_CURRENT; | |
6404 | def.vd_flags = 0; | |
6405 | if (t->globals.list == NULL | |
6406 | && t->locals.list == NULL | |
6407 | && ! t->used) | |
6408 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 6409 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
6410 | def.vd_cnt = cdeps + 1; |
6411 | def.vd_hash = bfd_elf_hash (t->name); | |
6412 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6413 | def.vd_next = 0; | |
a6cc6b3b RO |
6414 | |
6415 | /* If a basever node is next, it *must* be the last node in | |
6416 | the chain, otherwise Verdef construction breaks. */ | |
6417 | if (t->next != NULL && t->next->vernum == 0) | |
6418 | BFD_ASSERT (t->next->next == NULL); | |
6419 | ||
6420 | if (t->next != NULL && t->next->vernum != 0) | |
5a580b3a AM |
6421 | def.vd_next = (sizeof (Elf_External_Verdef) |
6422 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
6423 | ||
6424 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6425 | (Elf_External_Verdef *) p); | |
6426 | p += sizeof (Elf_External_Verdef); | |
6427 | ||
6428 | defaux.vda_name = h->dynstr_index; | |
6429 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6430 | h->dynstr_index); | |
6431 | defaux.vda_next = 0; | |
6432 | if (t->deps != NULL) | |
6433 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6434 | t->name_indx = defaux.vda_name; | |
6435 | ||
6436 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6437 | (Elf_External_Verdaux *) p); | |
6438 | p += sizeof (Elf_External_Verdaux); | |
6439 | ||
6440 | for (n = t->deps; n != NULL; n = n->next) | |
6441 | { | |
6442 | if (n->version_needed == NULL) | |
6443 | { | |
6444 | /* This can happen if there was an error in the | |
6445 | version script. */ | |
6446 | defaux.vda_name = 0; | |
6447 | } | |
6448 | else | |
6449 | { | |
6450 | defaux.vda_name = n->version_needed->name_indx; | |
6451 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6452 | defaux.vda_name); | |
6453 | } | |
6454 | if (n->next == NULL) | |
6455 | defaux.vda_next = 0; | |
6456 | else | |
6457 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6458 | ||
6459 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6460 | (Elf_External_Verdaux *) p); | |
6461 | p += sizeof (Elf_External_Verdaux); | |
6462 | } | |
6463 | } | |
6464 | ||
6465 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
6466 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
6467 | return FALSE; | |
6468 | ||
6469 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
6470 | } | |
6471 | ||
6472 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
6473 | { | |
6474 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
6475 | return FALSE; | |
6476 | } | |
6477 | else if (info->flags & DF_BIND_NOW) | |
6478 | { | |
6479 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
6480 | return FALSE; | |
6481 | } | |
6482 | ||
6483 | if (info->flags_1) | |
6484 | { | |
0e1862bb | 6485 | if (bfd_link_executable (info)) |
5a580b3a AM |
6486 | info->flags_1 &= ~ (DF_1_INITFIRST |
6487 | | DF_1_NODELETE | |
6488 | | DF_1_NOOPEN); | |
6489 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
6490 | return FALSE; | |
6491 | } | |
6492 | ||
6493 | /* Work out the size of the version reference section. */ | |
6494 | ||
3d4d4302 | 6495 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
6496 | BFD_ASSERT (s != NULL); |
6497 | { | |
6498 | struct elf_find_verdep_info sinfo; | |
6499 | ||
5a580b3a AM |
6500 | sinfo.info = info; |
6501 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
6502 | if (sinfo.vers == 0) | |
6503 | sinfo.vers = 1; | |
6504 | sinfo.failed = FALSE; | |
6505 | ||
6506 | elf_link_hash_traverse (elf_hash_table (info), | |
6507 | _bfd_elf_link_find_version_dependencies, | |
6508 | &sinfo); | |
14b1c01e AM |
6509 | if (sinfo.failed) |
6510 | return FALSE; | |
5a580b3a AM |
6511 | |
6512 | if (elf_tdata (output_bfd)->verref == NULL) | |
8423293d | 6513 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
6514 | else |
6515 | { | |
6516 | Elf_Internal_Verneed *t; | |
6517 | unsigned int size; | |
6518 | unsigned int crefs; | |
6519 | bfd_byte *p; | |
6520 | ||
a6cc6b3b | 6521 | /* Build the version dependency section. */ |
5a580b3a AM |
6522 | size = 0; |
6523 | crefs = 0; | |
6524 | for (t = elf_tdata (output_bfd)->verref; | |
6525 | t != NULL; | |
6526 | t = t->vn_nextref) | |
6527 | { | |
6528 | Elf_Internal_Vernaux *a; | |
6529 | ||
6530 | size += sizeof (Elf_External_Verneed); | |
6531 | ++crefs; | |
6532 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6533 | size += sizeof (Elf_External_Vernaux); | |
6534 | } | |
6535 | ||
eea6121a | 6536 | s->size = size; |
a50b1753 | 6537 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
5a580b3a AM |
6538 | if (s->contents == NULL) |
6539 | return FALSE; | |
6540 | ||
6541 | p = s->contents; | |
6542 | for (t = elf_tdata (output_bfd)->verref; | |
6543 | t != NULL; | |
6544 | t = t->vn_nextref) | |
6545 | { | |
6546 | unsigned int caux; | |
6547 | Elf_Internal_Vernaux *a; | |
ef53be89 | 6548 | size_t indx; |
5a580b3a AM |
6549 | |
6550 | caux = 0; | |
6551 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6552 | ++caux; | |
6553 | ||
6554 | t->vn_version = VER_NEED_CURRENT; | |
6555 | t->vn_cnt = caux; | |
6556 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6557 | elf_dt_name (t->vn_bfd) != NULL | |
6558 | ? elf_dt_name (t->vn_bfd) | |
06084812 | 6559 | : lbasename (t->vn_bfd->filename), |
5a580b3a | 6560 | FALSE); |
ef53be89 | 6561 | if (indx == (size_t) -1) |
5a580b3a AM |
6562 | return FALSE; |
6563 | t->vn_file = indx; | |
6564 | t->vn_aux = sizeof (Elf_External_Verneed); | |
6565 | if (t->vn_nextref == NULL) | |
6566 | t->vn_next = 0; | |
6567 | else | |
6568 | t->vn_next = (sizeof (Elf_External_Verneed) | |
6569 | + caux * sizeof (Elf_External_Vernaux)); | |
6570 | ||
6571 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
6572 | (Elf_External_Verneed *) p); | |
6573 | p += sizeof (Elf_External_Verneed); | |
6574 | ||
6575 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6576 | { | |
6577 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6578 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6579 | a->vna_nodename, FALSE); | |
ef53be89 | 6580 | if (indx == (size_t) -1) |
5a580b3a AM |
6581 | return FALSE; |
6582 | a->vna_name = indx; | |
6583 | if (a->vna_nextptr == NULL) | |
6584 | a->vna_next = 0; | |
6585 | else | |
6586 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6587 | ||
6588 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6589 | (Elf_External_Vernaux *) p); | |
6590 | p += sizeof (Elf_External_Vernaux); | |
6591 | } | |
6592 | } | |
6593 | ||
6594 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
6595 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
6596 | return FALSE; | |
6597 | ||
6598 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6599 | } | |
6600 | } | |
6601 | ||
8423293d AM |
6602 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
6603 | && elf_tdata (output_bfd)->cverdefs == 0) | |
6604 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
6605 | §ion_sym_count) == 0) | |
6606 | { | |
3d4d4302 | 6607 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
8423293d AM |
6608 | s->flags |= SEC_EXCLUDE; |
6609 | } | |
6610 | } | |
6611 | return TRUE; | |
6612 | } | |
6613 | ||
74541ad4 AM |
6614 | /* Find the first non-excluded output section. We'll use its |
6615 | section symbol for some emitted relocs. */ | |
6616 | void | |
6617 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
6618 | { | |
6619 | asection *s; | |
6620 | ||
6621 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6622 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
6623 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6624 | { | |
6625 | elf_hash_table (info)->text_index_section = s; | |
6626 | break; | |
6627 | } | |
6628 | } | |
6629 | ||
6630 | /* Find two non-excluded output sections, one for code, one for data. | |
6631 | We'll use their section symbols for some emitted relocs. */ | |
6632 | void | |
6633 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
6634 | { | |
6635 | asection *s; | |
6636 | ||
266b05cf DJ |
6637 | /* Data first, since setting text_index_section changes |
6638 | _bfd_elf_link_omit_section_dynsym. */ | |
74541ad4 | 6639 | for (s = output_bfd->sections; s != NULL; s = s->next) |
266b05cf | 6640 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) |
74541ad4 AM |
6641 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6642 | { | |
266b05cf | 6643 | elf_hash_table (info)->data_index_section = s; |
74541ad4 AM |
6644 | break; |
6645 | } | |
6646 | ||
6647 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
266b05cf DJ |
6648 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) |
6649 | == (SEC_ALLOC | SEC_READONLY)) | |
74541ad4 AM |
6650 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6651 | { | |
266b05cf | 6652 | elf_hash_table (info)->text_index_section = s; |
74541ad4 AM |
6653 | break; |
6654 | } | |
6655 | ||
6656 | if (elf_hash_table (info)->text_index_section == NULL) | |
6657 | elf_hash_table (info)->text_index_section | |
6658 | = elf_hash_table (info)->data_index_section; | |
6659 | } | |
6660 | ||
8423293d AM |
6661 | bfd_boolean |
6662 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
6663 | { | |
74541ad4 AM |
6664 | const struct elf_backend_data *bed; |
6665 | ||
8423293d AM |
6666 | if (!is_elf_hash_table (info->hash)) |
6667 | return TRUE; | |
6668 | ||
74541ad4 AM |
6669 | bed = get_elf_backend_data (output_bfd); |
6670 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
6671 | ||
8423293d AM |
6672 | if (elf_hash_table (info)->dynamic_sections_created) |
6673 | { | |
6674 | bfd *dynobj; | |
8423293d AM |
6675 | asection *s; |
6676 | bfd_size_type dynsymcount; | |
6677 | unsigned long section_sym_count; | |
8423293d AM |
6678 | unsigned int dtagcount; |
6679 | ||
6680 | dynobj = elf_hash_table (info)->dynobj; | |
6681 | ||
5a580b3a AM |
6682 | /* Assign dynsym indicies. In a shared library we generate a |
6683 | section symbol for each output section, which come first. | |
6684 | Next come all of the back-end allocated local dynamic syms, | |
6685 | followed by the rest of the global symbols. */ | |
6686 | ||
554220db AM |
6687 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, |
6688 | §ion_sym_count); | |
5a580b3a AM |
6689 | |
6690 | /* Work out the size of the symbol version section. */ | |
3d4d4302 | 6691 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
5a580b3a | 6692 | BFD_ASSERT (s != NULL); |
d5486c43 | 6693 | if ((s->flags & SEC_EXCLUDE) == 0) |
5a580b3a | 6694 | { |
eea6121a | 6695 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
a50b1753 | 6696 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
5a580b3a AM |
6697 | if (s->contents == NULL) |
6698 | return FALSE; | |
6699 | ||
6700 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
6701 | return FALSE; | |
6702 | } | |
6703 | ||
6704 | /* Set the size of the .dynsym and .hash sections. We counted | |
6705 | the number of dynamic symbols in elf_link_add_object_symbols. | |
6706 | We will build the contents of .dynsym and .hash when we build | |
6707 | the final symbol table, because until then we do not know the | |
6708 | correct value to give the symbols. We built the .dynstr | |
6709 | section as we went along in elf_link_add_object_symbols. */ | |
cae1fbbb | 6710 | s = elf_hash_table (info)->dynsym; |
5a580b3a | 6711 | BFD_ASSERT (s != NULL); |
eea6121a | 6712 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a | 6713 | |
d5486c43 L |
6714 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
6715 | if (s->contents == NULL) | |
6716 | return FALSE; | |
5a580b3a | 6717 | |
d5486c43 L |
6718 | /* The first entry in .dynsym is a dummy symbol. Clear all the |
6719 | section syms, in case we don't output them all. */ | |
6720 | ++section_sym_count; | |
6721 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a | 6722 | |
fdc90cb4 JJ |
6723 | elf_hash_table (info)->bucketcount = 0; |
6724 | ||
5a580b3a AM |
6725 | /* Compute the size of the hashing table. As a side effect this |
6726 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
6727 | if (info->emit_hash) |
6728 | { | |
6729 | unsigned long int *hashcodes; | |
14b1c01e | 6730 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
6731 | bfd_size_type amt; |
6732 | unsigned long int nsyms; | |
6733 | size_t bucketcount; | |
6734 | size_t hash_entry_size; | |
6735 | ||
6736 | /* Compute the hash values for all exported symbols. At the same | |
6737 | time store the values in an array so that we could use them for | |
6738 | optimizations. */ | |
6739 | amt = dynsymcount * sizeof (unsigned long int); | |
a50b1753 | 6740 | hashcodes = (unsigned long int *) bfd_malloc (amt); |
fdc90cb4 JJ |
6741 | if (hashcodes == NULL) |
6742 | return FALSE; | |
14b1c01e AM |
6743 | hashinf.hashcodes = hashcodes; |
6744 | hashinf.error = FALSE; | |
5a580b3a | 6745 | |
fdc90cb4 JJ |
6746 | /* Put all hash values in HASHCODES. */ |
6747 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
6748 | elf_collect_hash_codes, &hashinf); |
6749 | if (hashinf.error) | |
4dd07732 AM |
6750 | { |
6751 | free (hashcodes); | |
6752 | return FALSE; | |
6753 | } | |
5a580b3a | 6754 | |
14b1c01e | 6755 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
6756 | bucketcount |
6757 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
6758 | free (hashcodes); | |
6759 | ||
6760 | if (bucketcount == 0) | |
6761 | return FALSE; | |
5a580b3a | 6762 | |
fdc90cb4 JJ |
6763 | elf_hash_table (info)->bucketcount = bucketcount; |
6764 | ||
3d4d4302 | 6765 | s = bfd_get_linker_section (dynobj, ".hash"); |
fdc90cb4 JJ |
6766 | BFD_ASSERT (s != NULL); |
6767 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
6768 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
a50b1753 | 6769 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6770 | if (s->contents == NULL) |
6771 | return FALSE; | |
6772 | ||
6773 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
6774 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
6775 | s->contents + hash_entry_size); | |
6776 | } | |
6777 | ||
6778 | if (info->emit_gnu_hash) | |
6779 | { | |
6780 | size_t i, cnt; | |
6781 | unsigned char *contents; | |
6782 | struct collect_gnu_hash_codes cinfo; | |
6783 | bfd_size_type amt; | |
6784 | size_t bucketcount; | |
6785 | ||
6786 | memset (&cinfo, 0, sizeof (cinfo)); | |
6787 | ||
6788 | /* Compute the hash values for all exported symbols. At the same | |
6789 | time store the values in an array so that we could use them for | |
6790 | optimizations. */ | |
6791 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
a50b1753 | 6792 | cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); |
fdc90cb4 JJ |
6793 | if (cinfo.hashcodes == NULL) |
6794 | return FALSE; | |
6795 | ||
6796 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
6797 | cinfo.min_dynindx = -1; | |
6798 | cinfo.output_bfd = output_bfd; | |
6799 | cinfo.bed = bed; | |
6800 | ||
6801 | /* Put all hash values in HASHCODES. */ | |
6802 | elf_link_hash_traverse (elf_hash_table (info), | |
6803 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e | 6804 | if (cinfo.error) |
4dd07732 AM |
6805 | { |
6806 | free (cinfo.hashcodes); | |
6807 | return FALSE; | |
6808 | } | |
fdc90cb4 JJ |
6809 | |
6810 | bucketcount | |
6811 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
6812 | ||
6813 | if (bucketcount == 0) | |
6814 | { | |
6815 | free (cinfo.hashcodes); | |
6816 | return FALSE; | |
6817 | } | |
6818 | ||
3d4d4302 | 6819 | s = bfd_get_linker_section (dynobj, ".gnu.hash"); |
fdc90cb4 JJ |
6820 | BFD_ASSERT (s != NULL); |
6821 | ||
6822 | if (cinfo.nsyms == 0) | |
6823 | { | |
6824 | /* Empty .gnu.hash section is special. */ | |
6825 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
6826 | free (cinfo.hashcodes); | |
6827 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
a50b1753 | 6828 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6829 | if (contents == NULL) |
6830 | return FALSE; | |
6831 | s->contents = contents; | |
6832 | /* 1 empty bucket. */ | |
6833 | bfd_put_32 (output_bfd, 1, contents); | |
6834 | /* SYMIDX above the special symbol 0. */ | |
6835 | bfd_put_32 (output_bfd, 1, contents + 4); | |
6836 | /* Just one word for bitmask. */ | |
6837 | bfd_put_32 (output_bfd, 1, contents + 8); | |
6838 | /* Only hash fn bloom filter. */ | |
6839 | bfd_put_32 (output_bfd, 0, contents + 12); | |
6840 | /* No hashes are valid - empty bitmask. */ | |
6841 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
6842 | /* No hashes in the only bucket. */ | |
6843 | bfd_put_32 (output_bfd, 0, | |
6844 | contents + 16 + bed->s->arch_size / 8); | |
6845 | } | |
6846 | else | |
6847 | { | |
9e6619e2 | 6848 | unsigned long int maskwords, maskbitslog2, x; |
0b33793d | 6849 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 | 6850 | |
9e6619e2 AM |
6851 | x = cinfo.nsyms; |
6852 | maskbitslog2 = 1; | |
6853 | while ((x >>= 1) != 0) | |
6854 | ++maskbitslog2; | |
fdc90cb4 JJ |
6855 | if (maskbitslog2 < 3) |
6856 | maskbitslog2 = 5; | |
6857 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
6858 | maskbitslog2 = maskbitslog2 + 3; | |
6859 | else | |
6860 | maskbitslog2 = maskbitslog2 + 2; | |
6861 | if (bed->s->arch_size == 64) | |
6862 | { | |
6863 | if (maskbitslog2 == 5) | |
6864 | maskbitslog2 = 6; | |
6865 | cinfo.shift1 = 6; | |
6866 | } | |
6867 | else | |
6868 | cinfo.shift1 = 5; | |
6869 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 6870 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
6871 | cinfo.maskbits = 1 << maskbitslog2; |
6872 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
6873 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
6874 | amt += maskwords * sizeof (bfd_vma); | |
a50b1753 | 6875 | cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); |
fdc90cb4 JJ |
6876 | if (cinfo.bitmask == NULL) |
6877 | { | |
6878 | free (cinfo.hashcodes); | |
6879 | return FALSE; | |
6880 | } | |
6881 | ||
a50b1753 | 6882 | cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); |
fdc90cb4 JJ |
6883 | cinfo.indx = cinfo.counts + bucketcount; |
6884 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
6885 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
6886 | ||
6887 | /* Determine how often each hash bucket is used. */ | |
6888 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
6889 | for (i = 0; i < cinfo.nsyms; ++i) | |
6890 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
6891 | ||
6892 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
6893 | if (cinfo.counts[i] != 0) | |
6894 | { | |
6895 | cinfo.indx[i] = cnt; | |
6896 | cnt += cinfo.counts[i]; | |
6897 | } | |
6898 | BFD_ASSERT (cnt == dynsymcount); | |
6899 | cinfo.bucketcount = bucketcount; | |
6900 | cinfo.local_indx = cinfo.min_dynindx; | |
6901 | ||
6902 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
6903 | s->size += cinfo.maskbits / 8; | |
a50b1753 | 6904 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6905 | if (contents == NULL) |
6906 | { | |
6907 | free (cinfo.bitmask); | |
6908 | free (cinfo.hashcodes); | |
6909 | return FALSE; | |
6910 | } | |
6911 | ||
6912 | s->contents = contents; | |
6913 | bfd_put_32 (output_bfd, bucketcount, contents); | |
6914 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
6915 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
6916 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
6917 | contents += 16 + cinfo.maskbits / 8; | |
6918 | ||
6919 | for (i = 0; i < bucketcount; ++i) | |
6920 | { | |
6921 | if (cinfo.counts[i] == 0) | |
6922 | bfd_put_32 (output_bfd, 0, contents); | |
6923 | else | |
6924 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
6925 | contents += 4; | |
6926 | } | |
6927 | ||
6928 | cinfo.contents = contents; | |
6929 | ||
6930 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
6931 | elf_link_hash_traverse (elf_hash_table (info), | |
6932 | elf_renumber_gnu_hash_syms, &cinfo); | |
6933 | ||
6934 | contents = s->contents + 16; | |
6935 | for (i = 0; i < maskwords; ++i) | |
6936 | { | |
6937 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
6938 | contents); | |
6939 | contents += bed->s->arch_size / 8; | |
6940 | } | |
6941 | ||
6942 | free (cinfo.bitmask); | |
6943 | free (cinfo.hashcodes); | |
6944 | } | |
6945 | } | |
5a580b3a | 6946 | |
3d4d4302 | 6947 | s = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
6948 | BFD_ASSERT (s != NULL); |
6949 | ||
4ad4eba5 | 6950 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 6951 | |
eea6121a | 6952 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
6953 | |
6954 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
6955 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
6956 | return FALSE; | |
6957 | } | |
6958 | ||
6959 | return TRUE; | |
6960 | } | |
4d269e42 | 6961 | \f |
4d269e42 AM |
6962 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ |
6963 | ||
6964 | static void | |
6965 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
6966 | asection *sec) | |
6967 | { | |
dbaa2011 AM |
6968 | BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); |
6969 | sec->sec_info_type = SEC_INFO_TYPE_NONE; | |
4d269e42 AM |
6970 | } |
6971 | ||
6972 | /* Finish SHF_MERGE section merging. */ | |
6973 | ||
6974 | bfd_boolean | |
630993ec | 6975 | _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) |
4d269e42 AM |
6976 | { |
6977 | bfd *ibfd; | |
6978 | asection *sec; | |
6979 | ||
6980 | if (!is_elf_hash_table (info->hash)) | |
6981 | return FALSE; | |
6982 | ||
c72f2fb2 | 6983 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
630993ec AM |
6984 | if ((ibfd->flags & DYNAMIC) == 0 |
6985 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
017e6bce AM |
6986 | && (elf_elfheader (ibfd)->e_ident[EI_CLASS] |
6987 | == get_elf_backend_data (obfd)->s->elfclass)) | |
4d269e42 AM |
6988 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) |
6989 | if ((sec->flags & SEC_MERGE) != 0 | |
6990 | && !bfd_is_abs_section (sec->output_section)) | |
6991 | { | |
6992 | struct bfd_elf_section_data *secdata; | |
6993 | ||
6994 | secdata = elf_section_data (sec); | |
630993ec | 6995 | if (! _bfd_add_merge_section (obfd, |
4d269e42 AM |
6996 | &elf_hash_table (info)->merge_info, |
6997 | sec, &secdata->sec_info)) | |
6998 | return FALSE; | |
6999 | else if (secdata->sec_info) | |
dbaa2011 | 7000 | sec->sec_info_type = SEC_INFO_TYPE_MERGE; |
4d269e42 AM |
7001 | } |
7002 | ||
7003 | if (elf_hash_table (info)->merge_info != NULL) | |
630993ec | 7004 | _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, |
4d269e42 AM |
7005 | merge_sections_remove_hook); |
7006 | return TRUE; | |
7007 | } | |
7008 | ||
7009 | /* Create an entry in an ELF linker hash table. */ | |
7010 | ||
7011 | struct bfd_hash_entry * | |
7012 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
7013 | struct bfd_hash_table *table, | |
7014 | const char *string) | |
7015 | { | |
7016 | /* Allocate the structure if it has not already been allocated by a | |
7017 | subclass. */ | |
7018 | if (entry == NULL) | |
7019 | { | |
a50b1753 | 7020 | entry = (struct bfd_hash_entry *) |
ca4be51c | 7021 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); |
4d269e42 AM |
7022 | if (entry == NULL) |
7023 | return entry; | |
7024 | } | |
7025 | ||
7026 | /* Call the allocation method of the superclass. */ | |
7027 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
7028 | if (entry != NULL) | |
7029 | { | |
7030 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
7031 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
7032 | ||
7033 | /* Set local fields. */ | |
7034 | ret->indx = -1; | |
7035 | ret->dynindx = -1; | |
7036 | ret->got = htab->init_got_refcount; | |
7037 | ret->plt = htab->init_plt_refcount; | |
7038 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
7039 | - offsetof (struct elf_link_hash_entry, size))); | |
7040 | /* Assume that we have been called by a non-ELF symbol reader. | |
7041 | This flag is then reset by the code which reads an ELF input | |
7042 | file. This ensures that a symbol created by a non-ELF symbol | |
7043 | reader will have the flag set correctly. */ | |
7044 | ret->non_elf = 1; | |
7045 | } | |
7046 | ||
7047 | return entry; | |
7048 | } | |
7049 | ||
7050 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
7051 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
7052 | ||
7053 | void | |
7054 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
7055 | struct elf_link_hash_entry *dir, | |
7056 | struct elf_link_hash_entry *ind) | |
7057 | { | |
7058 | struct elf_link_hash_table *htab; | |
7059 | ||
7060 | /* Copy down any references that we may have already seen to the | |
6e33951e L |
7061 | symbol which just became indirect if DIR isn't a hidden versioned |
7062 | symbol. */ | |
4d269e42 | 7063 | |
422f1182 | 7064 | if (dir->versioned != versioned_hidden) |
6e33951e L |
7065 | { |
7066 | dir->ref_dynamic |= ind->ref_dynamic; | |
7067 | dir->ref_regular |= ind->ref_regular; | |
7068 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
7069 | dir->non_got_ref |= ind->non_got_ref; | |
7070 | dir->needs_plt |= ind->needs_plt; | |
7071 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
7072 | } | |
4d269e42 AM |
7073 | |
7074 | if (ind->root.type != bfd_link_hash_indirect) | |
7075 | return; | |
7076 | ||
7077 | /* Copy over the global and procedure linkage table refcount entries. | |
7078 | These may have been already set up by a check_relocs routine. */ | |
7079 | htab = elf_hash_table (info); | |
7080 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
7081 | { | |
7082 | if (dir->got.refcount < 0) | |
7083 | dir->got.refcount = 0; | |
7084 | dir->got.refcount += ind->got.refcount; | |
7085 | ind->got.refcount = htab->init_got_refcount.refcount; | |
7086 | } | |
7087 | ||
7088 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
7089 | { | |
7090 | if (dir->plt.refcount < 0) | |
7091 | dir->plt.refcount = 0; | |
7092 | dir->plt.refcount += ind->plt.refcount; | |
7093 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
7094 | } | |
7095 | ||
7096 | if (ind->dynindx != -1) | |
7097 | { | |
7098 | if (dir->dynindx != -1) | |
7099 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
7100 | dir->dynindx = ind->dynindx; | |
7101 | dir->dynstr_index = ind->dynstr_index; | |
7102 | ind->dynindx = -1; | |
7103 | ind->dynstr_index = 0; | |
7104 | } | |
7105 | } | |
7106 | ||
7107 | void | |
7108 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
7109 | struct elf_link_hash_entry *h, | |
7110 | bfd_boolean force_local) | |
7111 | { | |
3aa14d16 L |
7112 | /* STT_GNU_IFUNC symbol must go through PLT. */ |
7113 | if (h->type != STT_GNU_IFUNC) | |
7114 | { | |
7115 | h->plt = elf_hash_table (info)->init_plt_offset; | |
7116 | h->needs_plt = 0; | |
7117 | } | |
4d269e42 AM |
7118 | if (force_local) |
7119 | { | |
7120 | h->forced_local = 1; | |
7121 | if (h->dynindx != -1) | |
7122 | { | |
7123 | h->dynindx = -1; | |
7124 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, | |
7125 | h->dynstr_index); | |
7126 | } | |
7127 | } | |
7128 | } | |
7129 | ||
7bf52ea2 AM |
7130 | /* Initialize an ELF linker hash table. *TABLE has been zeroed by our |
7131 | caller. */ | |
4d269e42 AM |
7132 | |
7133 | bfd_boolean | |
7134 | _bfd_elf_link_hash_table_init | |
7135 | (struct elf_link_hash_table *table, | |
7136 | bfd *abfd, | |
7137 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
7138 | struct bfd_hash_table *, | |
7139 | const char *), | |
4dfe6ac6 NC |
7140 | unsigned int entsize, |
7141 | enum elf_target_id target_id) | |
4d269e42 AM |
7142 | { |
7143 | bfd_boolean ret; | |
7144 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; | |
7145 | ||
4d269e42 AM |
7146 | table->init_got_refcount.refcount = can_refcount - 1; |
7147 | table->init_plt_refcount.refcount = can_refcount - 1; | |
7148 | table->init_got_offset.offset = -(bfd_vma) 1; | |
7149 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
7150 | /* The first dynamic symbol is a dummy. */ | |
7151 | table->dynsymcount = 1; | |
7152 | ||
7153 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
4dfe6ac6 | 7154 | |
4d269e42 | 7155 | table->root.type = bfd_link_elf_hash_table; |
4dfe6ac6 | 7156 | table->hash_table_id = target_id; |
4d269e42 AM |
7157 | |
7158 | return ret; | |
7159 | } | |
7160 | ||
7161 | /* Create an ELF linker hash table. */ | |
7162 | ||
7163 | struct bfd_link_hash_table * | |
7164 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
7165 | { | |
7166 | struct elf_link_hash_table *ret; | |
7167 | bfd_size_type amt = sizeof (struct elf_link_hash_table); | |
7168 | ||
7bf52ea2 | 7169 | ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); |
4d269e42 AM |
7170 | if (ret == NULL) |
7171 | return NULL; | |
7172 | ||
7173 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
4dfe6ac6 NC |
7174 | sizeof (struct elf_link_hash_entry), |
7175 | GENERIC_ELF_DATA)) | |
4d269e42 AM |
7176 | { |
7177 | free (ret); | |
7178 | return NULL; | |
7179 | } | |
d495ab0d | 7180 | ret->root.hash_table_free = _bfd_elf_link_hash_table_free; |
4d269e42 AM |
7181 | |
7182 | return &ret->root; | |
7183 | } | |
7184 | ||
9f7c3e5e AM |
7185 | /* Destroy an ELF linker hash table. */ |
7186 | ||
7187 | void | |
d495ab0d | 7188 | _bfd_elf_link_hash_table_free (bfd *obfd) |
9f7c3e5e | 7189 | { |
d495ab0d AM |
7190 | struct elf_link_hash_table *htab; |
7191 | ||
7192 | htab = (struct elf_link_hash_table *) obfd->link.hash; | |
9f7c3e5e AM |
7193 | if (htab->dynstr != NULL) |
7194 | _bfd_elf_strtab_free (htab->dynstr); | |
7195 | _bfd_merge_sections_free (htab->merge_info); | |
d495ab0d | 7196 | _bfd_generic_link_hash_table_free (obfd); |
9f7c3e5e AM |
7197 | } |
7198 | ||
4d269e42 AM |
7199 | /* This is a hook for the ELF emulation code in the generic linker to |
7200 | tell the backend linker what file name to use for the DT_NEEDED | |
7201 | entry for a dynamic object. */ | |
7202 | ||
7203 | void | |
7204 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
7205 | { | |
7206 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7207 | && bfd_get_format (abfd) == bfd_object) | |
7208 | elf_dt_name (abfd) = name; | |
7209 | } | |
7210 | ||
7211 | int | |
7212 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
7213 | { | |
7214 | int lib_class; | |
7215 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7216 | && bfd_get_format (abfd) == bfd_object) | |
7217 | lib_class = elf_dyn_lib_class (abfd); | |
7218 | else | |
7219 | lib_class = 0; | |
7220 | return lib_class; | |
7221 | } | |
7222 | ||
7223 | void | |
7224 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
7225 | { | |
7226 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7227 | && bfd_get_format (abfd) == bfd_object) | |
7228 | elf_dyn_lib_class (abfd) = lib_class; | |
7229 | } | |
7230 | ||
7231 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
7232 | the linker ELF emulation code. */ | |
7233 | ||
7234 | struct bfd_link_needed_list * | |
7235 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7236 | struct bfd_link_info *info) | |
7237 | { | |
7238 | if (! is_elf_hash_table (info->hash)) | |
7239 | return NULL; | |
7240 | return elf_hash_table (info)->needed; | |
7241 | } | |
7242 | ||
7243 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
7244 | hook for the linker ELF emulation code. */ | |
7245 | ||
7246 | struct bfd_link_needed_list * | |
7247 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7248 | struct bfd_link_info *info) | |
7249 | { | |
7250 | if (! is_elf_hash_table (info->hash)) | |
7251 | return NULL; | |
7252 | return elf_hash_table (info)->runpath; | |
7253 | } | |
7254 | ||
7255 | /* Get the name actually used for a dynamic object for a link. This | |
7256 | is the SONAME entry if there is one. Otherwise, it is the string | |
7257 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
7258 | ||
7259 | const char * | |
7260 | bfd_elf_get_dt_soname (bfd *abfd) | |
7261 | { | |
7262 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7263 | && bfd_get_format (abfd) == bfd_object) | |
7264 | return elf_dt_name (abfd); | |
7265 | return NULL; | |
7266 | } | |
7267 | ||
7268 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
7269 | the ELF linker emulation code. */ | |
7270 | ||
7271 | bfd_boolean | |
7272 | bfd_elf_get_bfd_needed_list (bfd *abfd, | |
7273 | struct bfd_link_needed_list **pneeded) | |
7274 | { | |
7275 | asection *s; | |
7276 | bfd_byte *dynbuf = NULL; | |
cb33740c | 7277 | unsigned int elfsec; |
4d269e42 AM |
7278 | unsigned long shlink; |
7279 | bfd_byte *extdyn, *extdynend; | |
7280 | size_t extdynsize; | |
7281 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
7282 | ||
7283 | *pneeded = NULL; | |
7284 | ||
7285 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
7286 | || bfd_get_format (abfd) != bfd_object) | |
7287 | return TRUE; | |
7288 | ||
7289 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
7290 | if (s == NULL || s->size == 0) | |
7291 | return TRUE; | |
7292 | ||
7293 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
7294 | goto error_return; | |
7295 | ||
7296 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 7297 | if (elfsec == SHN_BAD) |
4d269e42 AM |
7298 | goto error_return; |
7299 | ||
7300 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 7301 | |
4d269e42 AM |
7302 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
7303 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
7304 | ||
7305 | extdyn = dynbuf; | |
7306 | extdynend = extdyn + s->size; | |
7307 | for (; extdyn < extdynend; extdyn += extdynsize) | |
7308 | { | |
7309 | Elf_Internal_Dyn dyn; | |
7310 | ||
7311 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
7312 | ||
7313 | if (dyn.d_tag == DT_NULL) | |
7314 | break; | |
7315 | ||
7316 | if (dyn.d_tag == DT_NEEDED) | |
7317 | { | |
7318 | const char *string; | |
7319 | struct bfd_link_needed_list *l; | |
7320 | unsigned int tagv = dyn.d_un.d_val; | |
7321 | bfd_size_type amt; | |
7322 | ||
7323 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
7324 | if (string == NULL) | |
7325 | goto error_return; | |
7326 | ||
7327 | amt = sizeof *l; | |
a50b1753 | 7328 | l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4d269e42 AM |
7329 | if (l == NULL) |
7330 | goto error_return; | |
7331 | ||
7332 | l->by = abfd; | |
7333 | l->name = string; | |
7334 | l->next = *pneeded; | |
7335 | *pneeded = l; | |
7336 | } | |
7337 | } | |
7338 | ||
7339 | free (dynbuf); | |
7340 | ||
7341 | return TRUE; | |
7342 | ||
7343 | error_return: | |
7344 | if (dynbuf != NULL) | |
7345 | free (dynbuf); | |
7346 | return FALSE; | |
7347 | } | |
7348 | ||
7349 | struct elf_symbuf_symbol | |
7350 | { | |
7351 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
7352 | unsigned char st_info; /* Type and binding attributes */ | |
7353 | unsigned char st_other; /* Visibilty, and target specific */ | |
7354 | }; | |
7355 | ||
7356 | struct elf_symbuf_head | |
7357 | { | |
7358 | struct elf_symbuf_symbol *ssym; | |
ef53be89 | 7359 | size_t count; |
4d269e42 AM |
7360 | unsigned int st_shndx; |
7361 | }; | |
7362 | ||
7363 | struct elf_symbol | |
7364 | { | |
7365 | union | |
7366 | { | |
7367 | Elf_Internal_Sym *isym; | |
7368 | struct elf_symbuf_symbol *ssym; | |
7369 | } u; | |
7370 | const char *name; | |
7371 | }; | |
7372 | ||
7373 | /* Sort references to symbols by ascending section number. */ | |
7374 | ||
7375 | static int | |
7376 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
7377 | { | |
7378 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
7379 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
7380 | ||
7381 | return s1->st_shndx - s2->st_shndx; | |
7382 | } | |
7383 | ||
7384 | static int | |
7385 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
7386 | { | |
7387 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
7388 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
7389 | return strcmp (s1->name, s2->name); | |
7390 | } | |
7391 | ||
7392 | static struct elf_symbuf_head * | |
ef53be89 | 7393 | elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) |
4d269e42 | 7394 | { |
14b1c01e | 7395 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
7396 | struct elf_symbuf_symbol *ssym; |
7397 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
ef53be89 | 7398 | size_t i, shndx_count, total_size; |
4d269e42 | 7399 | |
a50b1753 | 7400 | indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); |
4d269e42 AM |
7401 | if (indbuf == NULL) |
7402 | return NULL; | |
7403 | ||
7404 | for (ind = indbuf, i = 0; i < symcount; i++) | |
7405 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
7406 | *ind++ = &isymbuf[i]; | |
7407 | indbufend = ind; | |
7408 | ||
7409 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
7410 | elf_sort_elf_symbol); | |
7411 | ||
7412 | shndx_count = 0; | |
7413 | if (indbufend > indbuf) | |
7414 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
7415 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
7416 | shndx_count++; | |
7417 | ||
3ae181ee L |
7418 | total_size = ((shndx_count + 1) * sizeof (*ssymbuf) |
7419 | + (indbufend - indbuf) * sizeof (*ssym)); | |
a50b1753 | 7420 | ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); |
4d269e42 AM |
7421 | if (ssymbuf == NULL) |
7422 | { | |
7423 | free (indbuf); | |
7424 | return NULL; | |
7425 | } | |
7426 | ||
3ae181ee | 7427 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); |
4d269e42 AM |
7428 | ssymbuf->ssym = NULL; |
7429 | ssymbuf->count = shndx_count; | |
7430 | ssymbuf->st_shndx = 0; | |
7431 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
7432 | { | |
7433 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
7434 | { | |
7435 | ssymhead++; | |
7436 | ssymhead->ssym = ssym; | |
7437 | ssymhead->count = 0; | |
7438 | ssymhead->st_shndx = (*ind)->st_shndx; | |
7439 | } | |
7440 | ssym->st_name = (*ind)->st_name; | |
7441 | ssym->st_info = (*ind)->st_info; | |
7442 | ssym->st_other = (*ind)->st_other; | |
7443 | ssymhead->count++; | |
7444 | } | |
ef53be89 | 7445 | BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count |
3ae181ee L |
7446 | && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) |
7447 | == total_size)); | |
4d269e42 AM |
7448 | |
7449 | free (indbuf); | |
7450 | return ssymbuf; | |
7451 | } | |
7452 | ||
7453 | /* Check if 2 sections define the same set of local and global | |
7454 | symbols. */ | |
7455 | ||
8f317e31 | 7456 | static bfd_boolean |
4d269e42 AM |
7457 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
7458 | struct bfd_link_info *info) | |
7459 | { | |
7460 | bfd *bfd1, *bfd2; | |
7461 | const struct elf_backend_data *bed1, *bed2; | |
7462 | Elf_Internal_Shdr *hdr1, *hdr2; | |
ef53be89 | 7463 | size_t symcount1, symcount2; |
4d269e42 AM |
7464 | Elf_Internal_Sym *isymbuf1, *isymbuf2; |
7465 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
7466 | Elf_Internal_Sym *isym, *isymend; | |
7467 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
ef53be89 | 7468 | size_t count1, count2, i; |
cb33740c | 7469 | unsigned int shndx1, shndx2; |
4d269e42 AM |
7470 | bfd_boolean result; |
7471 | ||
7472 | bfd1 = sec1->owner; | |
7473 | bfd2 = sec2->owner; | |
7474 | ||
4d269e42 AM |
7475 | /* Both sections have to be in ELF. */ |
7476 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
7477 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
7478 | return FALSE; | |
7479 | ||
7480 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
7481 | return FALSE; | |
7482 | ||
4d269e42 AM |
7483 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
7484 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
cb33740c | 7485 | if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) |
4d269e42 AM |
7486 | return FALSE; |
7487 | ||
7488 | bed1 = get_elf_backend_data (bfd1); | |
7489 | bed2 = get_elf_backend_data (bfd2); | |
7490 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
7491 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
7492 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
7493 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
7494 | ||
7495 | if (symcount1 == 0 || symcount2 == 0) | |
7496 | return FALSE; | |
7497 | ||
7498 | result = FALSE; | |
7499 | isymbuf1 = NULL; | |
7500 | isymbuf2 = NULL; | |
a50b1753 NC |
7501 | ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; |
7502 | ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; | |
4d269e42 AM |
7503 | |
7504 | if (ssymbuf1 == NULL) | |
7505 | { | |
7506 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
7507 | NULL, NULL, NULL); | |
7508 | if (isymbuf1 == NULL) | |
7509 | goto done; | |
7510 | ||
7511 | if (!info->reduce_memory_overheads) | |
7512 | elf_tdata (bfd1)->symbuf = ssymbuf1 | |
7513 | = elf_create_symbuf (symcount1, isymbuf1); | |
7514 | } | |
7515 | ||
7516 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
7517 | { | |
7518 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
7519 | NULL, NULL, NULL); | |
7520 | if (isymbuf2 == NULL) | |
7521 | goto done; | |
7522 | ||
7523 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
7524 | elf_tdata (bfd2)->symbuf = ssymbuf2 | |
7525 | = elf_create_symbuf (symcount2, isymbuf2); | |
7526 | } | |
7527 | ||
7528 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
7529 | { | |
7530 | /* Optimized faster version. */ | |
ef53be89 | 7531 | size_t lo, hi, mid; |
4d269e42 AM |
7532 | struct elf_symbol *symp; |
7533 | struct elf_symbuf_symbol *ssym, *ssymend; | |
7534 | ||
7535 | lo = 0; | |
7536 | hi = ssymbuf1->count; | |
7537 | ssymbuf1++; | |
7538 | count1 = 0; | |
7539 | while (lo < hi) | |
7540 | { | |
7541 | mid = (lo + hi) / 2; | |
cb33740c | 7542 | if (shndx1 < ssymbuf1[mid].st_shndx) |
4d269e42 | 7543 | hi = mid; |
cb33740c | 7544 | else if (shndx1 > ssymbuf1[mid].st_shndx) |
4d269e42 AM |
7545 | lo = mid + 1; |
7546 | else | |
7547 | { | |
7548 | count1 = ssymbuf1[mid].count; | |
7549 | ssymbuf1 += mid; | |
7550 | break; | |
7551 | } | |
7552 | } | |
7553 | ||
7554 | lo = 0; | |
7555 | hi = ssymbuf2->count; | |
7556 | ssymbuf2++; | |
7557 | count2 = 0; | |
7558 | while (lo < hi) | |
7559 | { | |
7560 | mid = (lo + hi) / 2; | |
cb33740c | 7561 | if (shndx2 < ssymbuf2[mid].st_shndx) |
4d269e42 | 7562 | hi = mid; |
cb33740c | 7563 | else if (shndx2 > ssymbuf2[mid].st_shndx) |
4d269e42 AM |
7564 | lo = mid + 1; |
7565 | else | |
7566 | { | |
7567 | count2 = ssymbuf2[mid].count; | |
7568 | ssymbuf2 += mid; | |
7569 | break; | |
7570 | } | |
7571 | } | |
7572 | ||
7573 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7574 | goto done; | |
7575 | ||
ca4be51c AM |
7576 | symtable1 |
7577 | = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); | |
7578 | symtable2 | |
7579 | = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); | |
4d269e42 AM |
7580 | if (symtable1 == NULL || symtable2 == NULL) |
7581 | goto done; | |
7582 | ||
7583 | symp = symtable1; | |
7584 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
7585 | ssym < ssymend; ssym++, symp++) | |
7586 | { | |
7587 | symp->u.ssym = ssym; | |
7588 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
7589 | hdr1->sh_link, | |
7590 | ssym->st_name); | |
7591 | } | |
7592 | ||
7593 | symp = symtable2; | |
7594 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
7595 | ssym < ssymend; ssym++, symp++) | |
7596 | { | |
7597 | symp->u.ssym = ssym; | |
7598 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
7599 | hdr2->sh_link, | |
7600 | ssym->st_name); | |
7601 | } | |
7602 | ||
7603 | /* Sort symbol by name. */ | |
7604 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7605 | elf_sym_name_compare); | |
7606 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7607 | elf_sym_name_compare); | |
7608 | ||
7609 | for (i = 0; i < count1; i++) | |
7610 | /* Two symbols must have the same binding, type and name. */ | |
7611 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
7612 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
7613 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7614 | goto done; | |
7615 | ||
7616 | result = TRUE; | |
7617 | goto done; | |
7618 | } | |
7619 | ||
a50b1753 NC |
7620 | symtable1 = (struct elf_symbol *) |
7621 | bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
7622 | symtable2 = (struct elf_symbol *) | |
7623 | bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
4d269e42 AM |
7624 | if (symtable1 == NULL || symtable2 == NULL) |
7625 | goto done; | |
7626 | ||
7627 | /* Count definitions in the section. */ | |
7628 | count1 = 0; | |
7629 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
cb33740c | 7630 | if (isym->st_shndx == shndx1) |
4d269e42 AM |
7631 | symtable1[count1++].u.isym = isym; |
7632 | ||
7633 | count2 = 0; | |
7634 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
cb33740c | 7635 | if (isym->st_shndx == shndx2) |
4d269e42 AM |
7636 | symtable2[count2++].u.isym = isym; |
7637 | ||
7638 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7639 | goto done; | |
7640 | ||
7641 | for (i = 0; i < count1; i++) | |
7642 | symtable1[i].name | |
7643 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
7644 | symtable1[i].u.isym->st_name); | |
7645 | ||
7646 | for (i = 0; i < count2; i++) | |
7647 | symtable2[i].name | |
7648 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
7649 | symtable2[i].u.isym->st_name); | |
7650 | ||
7651 | /* Sort symbol by name. */ | |
7652 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7653 | elf_sym_name_compare); | |
7654 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7655 | elf_sym_name_compare); | |
7656 | ||
7657 | for (i = 0; i < count1; i++) | |
7658 | /* Two symbols must have the same binding, type and name. */ | |
7659 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
7660 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
7661 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7662 | goto done; | |
7663 | ||
7664 | result = TRUE; | |
7665 | ||
7666 | done: | |
7667 | if (symtable1) | |
7668 | free (symtable1); | |
7669 | if (symtable2) | |
7670 | free (symtable2); | |
7671 | if (isymbuf1) | |
7672 | free (isymbuf1); | |
7673 | if (isymbuf2) | |
7674 | free (isymbuf2); | |
7675 | ||
7676 | return result; | |
7677 | } | |
7678 | ||
7679 | /* Return TRUE if 2 section types are compatible. */ | |
7680 | ||
7681 | bfd_boolean | |
7682 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
7683 | bfd *bbfd, const asection *bsec) | |
7684 | { | |
7685 | if (asec == NULL | |
7686 | || bsec == NULL | |
7687 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
7688 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
7689 | return TRUE; | |
7690 | ||
7691 | return elf_section_type (asec) == elf_section_type (bsec); | |
7692 | } | |
7693 | \f | |
c152c796 AM |
7694 | /* Final phase of ELF linker. */ |
7695 | ||
7696 | /* A structure we use to avoid passing large numbers of arguments. */ | |
7697 | ||
7698 | struct elf_final_link_info | |
7699 | { | |
7700 | /* General link information. */ | |
7701 | struct bfd_link_info *info; | |
7702 | /* Output BFD. */ | |
7703 | bfd *output_bfd; | |
7704 | /* Symbol string table. */ | |
ef10c3ac | 7705 | struct elf_strtab_hash *symstrtab; |
c152c796 AM |
7706 | /* .hash section. */ |
7707 | asection *hash_sec; | |
7708 | /* symbol version section (.gnu.version). */ | |
7709 | asection *symver_sec; | |
7710 | /* Buffer large enough to hold contents of any section. */ | |
7711 | bfd_byte *contents; | |
7712 | /* Buffer large enough to hold external relocs of any section. */ | |
7713 | void *external_relocs; | |
7714 | /* Buffer large enough to hold internal relocs of any section. */ | |
7715 | Elf_Internal_Rela *internal_relocs; | |
7716 | /* Buffer large enough to hold external local symbols of any input | |
7717 | BFD. */ | |
7718 | bfd_byte *external_syms; | |
7719 | /* And a buffer for symbol section indices. */ | |
7720 | Elf_External_Sym_Shndx *locsym_shndx; | |
7721 | /* Buffer large enough to hold internal local symbols of any input | |
7722 | BFD. */ | |
7723 | Elf_Internal_Sym *internal_syms; | |
7724 | /* Array large enough to hold a symbol index for each local symbol | |
7725 | of any input BFD. */ | |
7726 | long *indices; | |
7727 | /* Array large enough to hold a section pointer for each local | |
7728 | symbol of any input BFD. */ | |
7729 | asection **sections; | |
ef10c3ac | 7730 | /* Buffer for SHT_SYMTAB_SHNDX section. */ |
c152c796 | 7731 | Elf_External_Sym_Shndx *symshndxbuf; |
ffbc01cc AM |
7732 | /* Number of STT_FILE syms seen. */ |
7733 | size_t filesym_count; | |
c152c796 AM |
7734 | }; |
7735 | ||
7736 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
7737 | ||
7738 | struct elf_outext_info | |
7739 | { | |
7740 | bfd_boolean failed; | |
7741 | bfd_boolean localsyms; | |
34a79995 | 7742 | bfd_boolean file_sym_done; |
8b127cbc | 7743 | struct elf_final_link_info *flinfo; |
c152c796 AM |
7744 | }; |
7745 | ||
d9352518 DB |
7746 | |
7747 | /* Support for evaluating a complex relocation. | |
7748 | ||
7749 | Complex relocations are generalized, self-describing relocations. The | |
7750 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 7751 | relocations themselves. |
d9352518 DB |
7752 | |
7753 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
7754 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
7755 | information (start bit, end bit, word width, etc) into the addend. This | |
7756 | information is extracted from CGEN-generated operand tables within gas. | |
7757 | ||
7758 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
7759 | internal) representing prefix-notation expressions, including but not | |
7760 | limited to those sorts of expressions normally encoded as addends in the | |
7761 | addend field. The symbol mangling format is: | |
7762 | ||
7763 | <node> := <literal> | |
7764 | | <unary-operator> ':' <node> | |
7765 | | <binary-operator> ':' <node> ':' <node> | |
7766 | ; | |
7767 | ||
7768 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
7769 | | 'S' <digits=N> ':' <N character section name> | |
7770 | | '#' <hexdigits> | |
7771 | ; | |
7772 | ||
7773 | <binary-operator> := as in C | |
7774 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
7775 | ||
7776 | static void | |
a0c8462f AM |
7777 | set_symbol_value (bfd *bfd_with_globals, |
7778 | Elf_Internal_Sym *isymbuf, | |
7779 | size_t locsymcount, | |
7780 | size_t symidx, | |
7781 | bfd_vma val) | |
d9352518 | 7782 | { |
8977835c AM |
7783 | struct elf_link_hash_entry **sym_hashes; |
7784 | struct elf_link_hash_entry *h; | |
7785 | size_t extsymoff = locsymcount; | |
d9352518 | 7786 | |
8977835c | 7787 | if (symidx < locsymcount) |
d9352518 | 7788 | { |
8977835c AM |
7789 | Elf_Internal_Sym *sym; |
7790 | ||
7791 | sym = isymbuf + symidx; | |
7792 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
7793 | { | |
7794 | /* It is a local symbol: move it to the | |
7795 | "absolute" section and give it a value. */ | |
7796 | sym->st_shndx = SHN_ABS; | |
7797 | sym->st_value = val; | |
7798 | return; | |
7799 | } | |
7800 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
7801 | extsymoff = 0; | |
d9352518 | 7802 | } |
8977835c AM |
7803 | |
7804 | /* It is a global symbol: set its link type | |
7805 | to "defined" and give it a value. */ | |
7806 | ||
7807 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
7808 | h = sym_hashes [symidx - extsymoff]; | |
7809 | while (h->root.type == bfd_link_hash_indirect | |
7810 | || h->root.type == bfd_link_hash_warning) | |
7811 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7812 | h->root.type = bfd_link_hash_defined; | |
7813 | h->root.u.def.value = val; | |
7814 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
7815 | } |
7816 | ||
a0c8462f AM |
7817 | static bfd_boolean |
7818 | resolve_symbol (const char *name, | |
7819 | bfd *input_bfd, | |
8b127cbc | 7820 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
7821 | bfd_vma *result, |
7822 | Elf_Internal_Sym *isymbuf, | |
7823 | size_t locsymcount) | |
d9352518 | 7824 | { |
a0c8462f AM |
7825 | Elf_Internal_Sym *sym; |
7826 | struct bfd_link_hash_entry *global_entry; | |
7827 | const char *candidate = NULL; | |
7828 | Elf_Internal_Shdr *symtab_hdr; | |
7829 | size_t i; | |
7830 | ||
d9352518 DB |
7831 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
7832 | ||
7833 | for (i = 0; i < locsymcount; ++ i) | |
7834 | { | |
8977835c | 7835 | sym = isymbuf + i; |
d9352518 DB |
7836 | |
7837 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
7838 | continue; | |
7839 | ||
7840 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
7841 | symtab_hdr->sh_link, | |
7842 | sym->st_name); | |
7843 | #ifdef DEBUG | |
0f02bbd9 AM |
7844 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
7845 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
7846 | #endif |
7847 | if (candidate && strcmp (candidate, name) == 0) | |
7848 | { | |
8b127cbc | 7849 | asection *sec = flinfo->sections [i]; |
d9352518 | 7850 | |
0f02bbd9 AM |
7851 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
7852 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 7853 | #ifdef DEBUG |
0f02bbd9 AM |
7854 | printf ("Found symbol with value %8.8lx\n", |
7855 | (unsigned long) *result); | |
d9352518 DB |
7856 | #endif |
7857 | return TRUE; | |
7858 | } | |
7859 | } | |
7860 | ||
7861 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
8b127cbc | 7862 | global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, |
a0c8462f | 7863 | FALSE, FALSE, TRUE); |
d9352518 DB |
7864 | if (!global_entry) |
7865 | return FALSE; | |
a0c8462f | 7866 | |
d9352518 DB |
7867 | if (global_entry->type == bfd_link_hash_defined |
7868 | || global_entry->type == bfd_link_hash_defweak) | |
7869 | { | |
a0c8462f AM |
7870 | *result = (global_entry->u.def.value |
7871 | + global_entry->u.def.section->output_section->vma | |
7872 | + global_entry->u.def.section->output_offset); | |
d9352518 | 7873 | #ifdef DEBUG |
0f02bbd9 AM |
7874 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
7875 | global_entry->root.string, (unsigned long) *result); | |
d9352518 DB |
7876 | #endif |
7877 | return TRUE; | |
a0c8462f | 7878 | } |
d9352518 | 7879 | |
d9352518 DB |
7880 | return FALSE; |
7881 | } | |
7882 | ||
37b01f6a DG |
7883 | /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in |
7884 | bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section | |
7885 | names like "foo.end" which is the end address of section "foo". */ | |
7886 | ||
d9352518 | 7887 | static bfd_boolean |
a0c8462f AM |
7888 | resolve_section (const char *name, |
7889 | asection *sections, | |
37b01f6a DG |
7890 | bfd_vma *result, |
7891 | bfd * abfd) | |
d9352518 | 7892 | { |
a0c8462f AM |
7893 | asection *curr; |
7894 | unsigned int len; | |
d9352518 | 7895 | |
a0c8462f | 7896 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
7897 | if (strcmp (curr->name, name) == 0) |
7898 | { | |
7899 | *result = curr->vma; | |
7900 | return TRUE; | |
7901 | } | |
7902 | ||
7903 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
37b01f6a | 7904 | /* FIXME: This could be coded more efficiently... */ |
a0c8462f | 7905 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
7906 | { |
7907 | len = strlen (curr->name); | |
a0c8462f | 7908 | if (len > strlen (name)) |
d9352518 DB |
7909 | continue; |
7910 | ||
7911 | if (strncmp (curr->name, name, len) == 0) | |
7912 | { | |
7913 | if (strncmp (".end", name + len, 4) == 0) | |
7914 | { | |
37b01f6a | 7915 | *result = curr->vma + curr->size / bfd_octets_per_byte (abfd); |
d9352518 DB |
7916 | return TRUE; |
7917 | } | |
7918 | ||
7919 | /* Insert more pseudo-section names here, if you like. */ | |
7920 | } | |
7921 | } | |
a0c8462f | 7922 | |
d9352518 DB |
7923 | return FALSE; |
7924 | } | |
7925 | ||
7926 | static void | |
a0c8462f | 7927 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 7928 | { |
695344c0 | 7929 | /* xgettext:c-format */ |
a0c8462f AM |
7930 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
7931 | reftype, name); | |
d9352518 DB |
7932 | } |
7933 | ||
7934 | static bfd_boolean | |
a0c8462f AM |
7935 | eval_symbol (bfd_vma *result, |
7936 | const char **symp, | |
7937 | bfd *input_bfd, | |
8b127cbc | 7938 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
7939 | bfd_vma dot, |
7940 | Elf_Internal_Sym *isymbuf, | |
7941 | size_t locsymcount, | |
7942 | int signed_p) | |
d9352518 | 7943 | { |
4b93929b NC |
7944 | size_t len; |
7945 | size_t symlen; | |
a0c8462f AM |
7946 | bfd_vma a; |
7947 | bfd_vma b; | |
4b93929b | 7948 | char symbuf[4096]; |
0f02bbd9 | 7949 | const char *sym = *symp; |
a0c8462f AM |
7950 | const char *symend; |
7951 | bfd_boolean symbol_is_section = FALSE; | |
d9352518 DB |
7952 | |
7953 | len = strlen (sym); | |
7954 | symend = sym + len; | |
7955 | ||
4b93929b | 7956 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
7957 | { |
7958 | bfd_set_error (bfd_error_invalid_operation); | |
7959 | return FALSE; | |
7960 | } | |
a0c8462f | 7961 | |
d9352518 DB |
7962 | switch (* sym) |
7963 | { | |
7964 | case '.': | |
0f02bbd9 AM |
7965 | *result = dot; |
7966 | *symp = sym + 1; | |
d9352518 DB |
7967 | return TRUE; |
7968 | ||
7969 | case '#': | |
0f02bbd9 AM |
7970 | ++sym; |
7971 | *result = strtoul (sym, (char **) symp, 16); | |
d9352518 DB |
7972 | return TRUE; |
7973 | ||
7974 | case 'S': | |
7975 | symbol_is_section = TRUE; | |
1a0670f3 | 7976 | /* Fall through. */ |
a0c8462f | 7977 | case 's': |
0f02bbd9 AM |
7978 | ++sym; |
7979 | symlen = strtol (sym, (char **) symp, 10); | |
7980 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 7981 | |
4b93929b | 7982 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
7983 | { |
7984 | bfd_set_error (bfd_error_invalid_operation); | |
7985 | return FALSE; | |
7986 | } | |
7987 | ||
7988 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 7989 | symbuf[symlen] = '\0'; |
0f02bbd9 | 7990 | *symp = sym + symlen; |
a0c8462f AM |
7991 | |
7992 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
7993 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
7994 | interpretation here; section means "try section first", not "must be a | |
7995 | section", and likewise with symbol. */ | |
7996 | ||
a0c8462f | 7997 | if (symbol_is_section) |
d9352518 | 7998 | { |
37b01f6a | 7999 | if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) |
8b127cbc | 8000 | && !resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8001 | isymbuf, locsymcount)) |
d9352518 DB |
8002 | { |
8003 | undefined_reference ("section", symbuf); | |
8004 | return FALSE; | |
8005 | } | |
a0c8462f AM |
8006 | } |
8007 | else | |
d9352518 | 8008 | { |
8b127cbc | 8009 | if (!resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8010 | isymbuf, locsymcount) |
8b127cbc | 8011 | && !resolve_section (symbuf, flinfo->output_bfd->sections, |
37b01f6a | 8012 | result, input_bfd)) |
d9352518 DB |
8013 | { |
8014 | undefined_reference ("symbol", symbuf); | |
8015 | return FALSE; | |
8016 | } | |
8017 | } | |
8018 | ||
8019 | return TRUE; | |
a0c8462f | 8020 | |
d9352518 DB |
8021 | /* All that remains are operators. */ |
8022 | ||
8023 | #define UNARY_OP(op) \ | |
8024 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8025 | { \ | |
8026 | sym += strlen (#op); \ | |
a0c8462f AM |
8027 | if (*sym == ':') \ |
8028 | ++sym; \ | |
0f02bbd9 | 8029 | *symp = sym; \ |
8b127cbc | 8030 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8031 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8032 | return FALSE; \ |
8033 | if (signed_p) \ | |
0f02bbd9 | 8034 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
8035 | else \ |
8036 | *result = op a; \ | |
d9352518 DB |
8037 | return TRUE; \ |
8038 | } | |
8039 | ||
8040 | #define BINARY_OP(op) \ | |
8041 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8042 | { \ | |
8043 | sym += strlen (#op); \ | |
a0c8462f AM |
8044 | if (*sym == ':') \ |
8045 | ++sym; \ | |
0f02bbd9 | 8046 | *symp = sym; \ |
8b127cbc | 8047 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8048 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f | 8049 | return FALSE; \ |
0f02bbd9 | 8050 | ++*symp; \ |
8b127cbc | 8051 | if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8052 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8053 | return FALSE; \ |
8054 | if (signed_p) \ | |
0f02bbd9 | 8055 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
8056 | else \ |
8057 | *result = a op b; \ | |
d9352518 DB |
8058 | return TRUE; \ |
8059 | } | |
8060 | ||
8061 | default: | |
8062 | UNARY_OP (0-); | |
8063 | BINARY_OP (<<); | |
8064 | BINARY_OP (>>); | |
8065 | BINARY_OP (==); | |
8066 | BINARY_OP (!=); | |
8067 | BINARY_OP (<=); | |
8068 | BINARY_OP (>=); | |
8069 | BINARY_OP (&&); | |
8070 | BINARY_OP (||); | |
8071 | UNARY_OP (~); | |
8072 | UNARY_OP (!); | |
8073 | BINARY_OP (*); | |
8074 | BINARY_OP (/); | |
8075 | BINARY_OP (%); | |
8076 | BINARY_OP (^); | |
8077 | BINARY_OP (|); | |
8078 | BINARY_OP (&); | |
8079 | BINARY_OP (+); | |
8080 | BINARY_OP (-); | |
8081 | BINARY_OP (<); | |
8082 | BINARY_OP (>); | |
8083 | #undef UNARY_OP | |
8084 | #undef BINARY_OP | |
8085 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
8086 | bfd_set_error (bfd_error_invalid_operation); | |
8087 | return FALSE; | |
8088 | } | |
8089 | } | |
8090 | ||
d9352518 | 8091 | static void |
a0c8462f AM |
8092 | put_value (bfd_vma size, |
8093 | unsigned long chunksz, | |
8094 | bfd *input_bfd, | |
8095 | bfd_vma x, | |
8096 | bfd_byte *location) | |
d9352518 DB |
8097 | { |
8098 | location += (size - chunksz); | |
8099 | ||
41cd1ad1 | 8100 | for (; size; size -= chunksz, location -= chunksz) |
d9352518 DB |
8101 | { |
8102 | switch (chunksz) | |
8103 | { | |
d9352518 DB |
8104 | case 1: |
8105 | bfd_put_8 (input_bfd, x, location); | |
41cd1ad1 | 8106 | x >>= 8; |
d9352518 DB |
8107 | break; |
8108 | case 2: | |
8109 | bfd_put_16 (input_bfd, x, location); | |
41cd1ad1 | 8110 | x >>= 16; |
d9352518 DB |
8111 | break; |
8112 | case 4: | |
8113 | bfd_put_32 (input_bfd, x, location); | |
65164438 NC |
8114 | /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ |
8115 | x >>= 16; | |
8116 | x >>= 16; | |
d9352518 | 8117 | break; |
d9352518 | 8118 | #ifdef BFD64 |
41cd1ad1 | 8119 | case 8: |
d9352518 | 8120 | bfd_put_64 (input_bfd, x, location); |
41cd1ad1 NC |
8121 | /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ |
8122 | x >>= 32; | |
8123 | x >>= 32; | |
8124 | break; | |
d9352518 | 8125 | #endif |
41cd1ad1 NC |
8126 | default: |
8127 | abort (); | |
d9352518 DB |
8128 | break; |
8129 | } | |
8130 | } | |
8131 | } | |
8132 | ||
a0c8462f AM |
8133 | static bfd_vma |
8134 | get_value (bfd_vma size, | |
8135 | unsigned long chunksz, | |
8136 | bfd *input_bfd, | |
8137 | bfd_byte *location) | |
d9352518 | 8138 | { |
9b239e0e | 8139 | int shift; |
d9352518 DB |
8140 | bfd_vma x = 0; |
8141 | ||
9b239e0e NC |
8142 | /* Sanity checks. */ |
8143 | BFD_ASSERT (chunksz <= sizeof (x) | |
8144 | && size >= chunksz | |
8145 | && chunksz != 0 | |
8146 | && (size % chunksz) == 0 | |
8147 | && input_bfd != NULL | |
8148 | && location != NULL); | |
8149 | ||
8150 | if (chunksz == sizeof (x)) | |
8151 | { | |
8152 | BFD_ASSERT (size == chunksz); | |
8153 | ||
8154 | /* Make sure that we do not perform an undefined shift operation. | |
8155 | We know that size == chunksz so there will only be one iteration | |
8156 | of the loop below. */ | |
8157 | shift = 0; | |
8158 | } | |
8159 | else | |
8160 | shift = 8 * chunksz; | |
8161 | ||
a0c8462f | 8162 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
8163 | { |
8164 | switch (chunksz) | |
8165 | { | |
d9352518 | 8166 | case 1: |
9b239e0e | 8167 | x = (x << shift) | bfd_get_8 (input_bfd, location); |
d9352518 DB |
8168 | break; |
8169 | case 2: | |
9b239e0e | 8170 | x = (x << shift) | bfd_get_16 (input_bfd, location); |
d9352518 DB |
8171 | break; |
8172 | case 4: | |
9b239e0e | 8173 | x = (x << shift) | bfd_get_32 (input_bfd, location); |
d9352518 | 8174 | break; |
d9352518 | 8175 | #ifdef BFD64 |
9b239e0e NC |
8176 | case 8: |
8177 | x = (x << shift) | bfd_get_64 (input_bfd, location); | |
d9352518 | 8178 | break; |
9b239e0e NC |
8179 | #endif |
8180 | default: | |
8181 | abort (); | |
d9352518 DB |
8182 | } |
8183 | } | |
8184 | return x; | |
8185 | } | |
8186 | ||
a0c8462f AM |
8187 | static void |
8188 | decode_complex_addend (unsigned long *start, /* in bits */ | |
8189 | unsigned long *oplen, /* in bits */ | |
8190 | unsigned long *len, /* in bits */ | |
8191 | unsigned long *wordsz, /* in bytes */ | |
8192 | unsigned long *chunksz, /* in bytes */ | |
8193 | unsigned long *lsb0_p, | |
8194 | unsigned long *signed_p, | |
8195 | unsigned long *trunc_p, | |
8196 | unsigned long encoded) | |
d9352518 DB |
8197 | { |
8198 | * start = encoded & 0x3F; | |
8199 | * len = (encoded >> 6) & 0x3F; | |
8200 | * oplen = (encoded >> 12) & 0x3F; | |
8201 | * wordsz = (encoded >> 18) & 0xF; | |
8202 | * chunksz = (encoded >> 22) & 0xF; | |
8203 | * lsb0_p = (encoded >> 27) & 1; | |
8204 | * signed_p = (encoded >> 28) & 1; | |
8205 | * trunc_p = (encoded >> 29) & 1; | |
8206 | } | |
8207 | ||
cdfeee4f | 8208 | bfd_reloc_status_type |
0f02bbd9 | 8209 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
cdfeee4f | 8210 | asection *input_section ATTRIBUTE_UNUSED, |
0f02bbd9 AM |
8211 | bfd_byte *contents, |
8212 | Elf_Internal_Rela *rel, | |
8213 | bfd_vma relocation) | |
d9352518 | 8214 | { |
0f02bbd9 AM |
8215 | bfd_vma shift, x, mask; |
8216 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 8217 | bfd_reloc_status_type r; |
d9352518 DB |
8218 | |
8219 | /* Perform this reloc, since it is complex. | |
8220 | (this is not to say that it necessarily refers to a complex | |
8221 | symbol; merely that it is a self-describing CGEN based reloc. | |
8222 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 8223 | word size, etc) encoded within it.). */ |
d9352518 | 8224 | |
a0c8462f AM |
8225 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
8226 | &chunksz, &lsb0_p, &signed_p, | |
8227 | &trunc_p, rel->r_addend); | |
d9352518 DB |
8228 | |
8229 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
8230 | ||
8231 | if (lsb0_p) | |
8232 | shift = (start + 1) - len; | |
8233 | else | |
8234 | shift = (8 * wordsz) - (start + len); | |
8235 | ||
37b01f6a DG |
8236 | x = get_value (wordsz, chunksz, input_bfd, |
8237 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
d9352518 DB |
8238 | |
8239 | #ifdef DEBUG | |
8240 | printf ("Doing complex reloc: " | |
8241 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
8242 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
8243 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
8244 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
9ccb8af9 AM |
8245 | oplen, (unsigned long) x, (unsigned long) mask, |
8246 | (unsigned long) relocation); | |
d9352518 DB |
8247 | #endif |
8248 | ||
cdfeee4f | 8249 | r = bfd_reloc_ok; |
d9352518 | 8250 | if (! trunc_p) |
cdfeee4f AM |
8251 | /* Now do an overflow check. */ |
8252 | r = bfd_check_overflow ((signed_p | |
8253 | ? complain_overflow_signed | |
8254 | : complain_overflow_unsigned), | |
8255 | len, 0, (8 * wordsz), | |
8256 | relocation); | |
a0c8462f | 8257 | |
d9352518 DB |
8258 | /* Do the deed. */ |
8259 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
8260 | ||
8261 | #ifdef DEBUG | |
8262 | printf (" relocation: %8.8lx\n" | |
8263 | " shifted mask: %8.8lx\n" | |
8264 | " shifted/masked reloc: %8.8lx\n" | |
8265 | " result: %8.8lx\n", | |
9ccb8af9 AM |
8266 | (unsigned long) relocation, (unsigned long) (mask << shift), |
8267 | (unsigned long) ((relocation & mask) << shift), (unsigned long) x); | |
d9352518 | 8268 | #endif |
37b01f6a DG |
8269 | put_value (wordsz, chunksz, input_bfd, x, |
8270 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
cdfeee4f | 8271 | return r; |
d9352518 DB |
8272 | } |
8273 | ||
0e287786 AM |
8274 | /* Functions to read r_offset from external (target order) reloc |
8275 | entry. Faster than bfd_getl32 et al, because we let the compiler | |
8276 | know the value is aligned. */ | |
53df40a4 | 8277 | |
0e287786 AM |
8278 | static bfd_vma |
8279 | ext32l_r_offset (const void *p) | |
53df40a4 AM |
8280 | { |
8281 | union aligned32 | |
8282 | { | |
8283 | uint32_t v; | |
8284 | unsigned char c[4]; | |
8285 | }; | |
8286 | const union aligned32 *a | |
0e287786 | 8287 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8288 | |
8289 | uint32_t aval = ( (uint32_t) a->c[0] | |
8290 | | (uint32_t) a->c[1] << 8 | |
8291 | | (uint32_t) a->c[2] << 16 | |
8292 | | (uint32_t) a->c[3] << 24); | |
0e287786 | 8293 | return aval; |
53df40a4 AM |
8294 | } |
8295 | ||
0e287786 AM |
8296 | static bfd_vma |
8297 | ext32b_r_offset (const void *p) | |
53df40a4 AM |
8298 | { |
8299 | union aligned32 | |
8300 | { | |
8301 | uint32_t v; | |
8302 | unsigned char c[4]; | |
8303 | }; | |
8304 | const union aligned32 *a | |
0e287786 | 8305 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8306 | |
8307 | uint32_t aval = ( (uint32_t) a->c[0] << 24 | |
8308 | | (uint32_t) a->c[1] << 16 | |
8309 | | (uint32_t) a->c[2] << 8 | |
8310 | | (uint32_t) a->c[3]); | |
0e287786 | 8311 | return aval; |
53df40a4 AM |
8312 | } |
8313 | ||
8314 | #ifdef BFD_HOST_64_BIT | |
0e287786 AM |
8315 | static bfd_vma |
8316 | ext64l_r_offset (const void *p) | |
53df40a4 AM |
8317 | { |
8318 | union aligned64 | |
8319 | { | |
8320 | uint64_t v; | |
8321 | unsigned char c[8]; | |
8322 | }; | |
8323 | const union aligned64 *a | |
0e287786 | 8324 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8325 | |
8326 | uint64_t aval = ( (uint64_t) a->c[0] | |
8327 | | (uint64_t) a->c[1] << 8 | |
8328 | | (uint64_t) a->c[2] << 16 | |
8329 | | (uint64_t) a->c[3] << 24 | |
8330 | | (uint64_t) a->c[4] << 32 | |
8331 | | (uint64_t) a->c[5] << 40 | |
8332 | | (uint64_t) a->c[6] << 48 | |
8333 | | (uint64_t) a->c[7] << 56); | |
0e287786 | 8334 | return aval; |
53df40a4 AM |
8335 | } |
8336 | ||
0e287786 AM |
8337 | static bfd_vma |
8338 | ext64b_r_offset (const void *p) | |
53df40a4 AM |
8339 | { |
8340 | union aligned64 | |
8341 | { | |
8342 | uint64_t v; | |
8343 | unsigned char c[8]; | |
8344 | }; | |
8345 | const union aligned64 *a | |
0e287786 | 8346 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8347 | |
8348 | uint64_t aval = ( (uint64_t) a->c[0] << 56 | |
8349 | | (uint64_t) a->c[1] << 48 | |
8350 | | (uint64_t) a->c[2] << 40 | |
8351 | | (uint64_t) a->c[3] << 32 | |
8352 | | (uint64_t) a->c[4] << 24 | |
8353 | | (uint64_t) a->c[5] << 16 | |
8354 | | (uint64_t) a->c[6] << 8 | |
8355 | | (uint64_t) a->c[7]); | |
0e287786 | 8356 | return aval; |
53df40a4 AM |
8357 | } |
8358 | #endif | |
8359 | ||
c152c796 AM |
8360 | /* When performing a relocatable link, the input relocations are |
8361 | preserved. But, if they reference global symbols, the indices | |
d4730f92 BS |
8362 | referenced must be updated. Update all the relocations found in |
8363 | RELDATA. */ | |
c152c796 | 8364 | |
bca6d0e3 | 8365 | static bfd_boolean |
c152c796 | 8366 | elf_link_adjust_relocs (bfd *abfd, |
9eaff861 | 8367 | asection *sec, |
28dbcedc AM |
8368 | struct bfd_elf_section_reloc_data *reldata, |
8369 | bfd_boolean sort) | |
c152c796 AM |
8370 | { |
8371 | unsigned int i; | |
8372 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8373 | bfd_byte *erela; | |
8374 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
8375 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8376 | bfd_vma r_type_mask; | |
8377 | int r_sym_shift; | |
d4730f92 BS |
8378 | unsigned int count = reldata->count; |
8379 | struct elf_link_hash_entry **rel_hash = reldata->hashes; | |
c152c796 | 8380 | |
d4730f92 | 8381 | if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) |
c152c796 AM |
8382 | { |
8383 | swap_in = bed->s->swap_reloc_in; | |
8384 | swap_out = bed->s->swap_reloc_out; | |
8385 | } | |
d4730f92 | 8386 | else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) |
c152c796 AM |
8387 | { |
8388 | swap_in = bed->s->swap_reloca_in; | |
8389 | swap_out = bed->s->swap_reloca_out; | |
8390 | } | |
8391 | else | |
8392 | abort (); | |
8393 | ||
8394 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
8395 | abort (); | |
8396 | ||
8397 | if (bed->s->arch_size == 32) | |
8398 | { | |
8399 | r_type_mask = 0xff; | |
8400 | r_sym_shift = 8; | |
8401 | } | |
8402 | else | |
8403 | { | |
8404 | r_type_mask = 0xffffffff; | |
8405 | r_sym_shift = 32; | |
8406 | } | |
8407 | ||
d4730f92 BS |
8408 | erela = reldata->hdr->contents; |
8409 | for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) | |
c152c796 AM |
8410 | { |
8411 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
8412 | unsigned int j; | |
8413 | ||
8414 | if (*rel_hash == NULL) | |
8415 | continue; | |
8416 | ||
8417 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
8418 | ||
8419 | (*swap_in) (abfd, erela, irela); | |
8420 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
8421 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
8422 | | (irela[j].r_info & r_type_mask)); | |
8423 | (*swap_out) (abfd, irela, erela); | |
8424 | } | |
53df40a4 | 8425 | |
9eaff861 AO |
8426 | if (bed->elf_backend_update_relocs) |
8427 | (*bed->elf_backend_update_relocs) (sec, reldata); | |
8428 | ||
0e287786 | 8429 | if (sort && count != 0) |
53df40a4 | 8430 | { |
0e287786 AM |
8431 | bfd_vma (*ext_r_off) (const void *); |
8432 | bfd_vma r_off; | |
8433 | size_t elt_size; | |
8434 | bfd_byte *base, *end, *p, *loc; | |
bca6d0e3 | 8435 | bfd_byte *buf = NULL; |
28dbcedc AM |
8436 | |
8437 | if (bed->s->arch_size == 32) | |
8438 | { | |
8439 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) | |
0e287786 | 8440 | ext_r_off = ext32l_r_offset; |
28dbcedc | 8441 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8442 | ext_r_off = ext32b_r_offset; |
28dbcedc AM |
8443 | else |
8444 | abort (); | |
8445 | } | |
53df40a4 | 8446 | else |
28dbcedc | 8447 | { |
53df40a4 | 8448 | #ifdef BFD_HOST_64_BIT |
28dbcedc | 8449 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) |
0e287786 | 8450 | ext_r_off = ext64l_r_offset; |
28dbcedc | 8451 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8452 | ext_r_off = ext64b_r_offset; |
28dbcedc | 8453 | else |
53df40a4 | 8454 | #endif |
28dbcedc AM |
8455 | abort (); |
8456 | } | |
0e287786 | 8457 | |
bca6d0e3 AM |
8458 | /* Must use a stable sort here. A modified insertion sort, |
8459 | since the relocs are mostly sorted already. */ | |
0e287786 AM |
8460 | elt_size = reldata->hdr->sh_entsize; |
8461 | base = reldata->hdr->contents; | |
8462 | end = base + count * elt_size; | |
bca6d0e3 | 8463 | if (elt_size > sizeof (Elf64_External_Rela)) |
0e287786 AM |
8464 | abort (); |
8465 | ||
8466 | /* Ensure the first element is lowest. This acts as a sentinel, | |
8467 | speeding the main loop below. */ | |
8468 | r_off = (*ext_r_off) (base); | |
8469 | for (p = loc = base; (p += elt_size) < end; ) | |
8470 | { | |
8471 | bfd_vma r_off2 = (*ext_r_off) (p); | |
8472 | if (r_off > r_off2) | |
8473 | { | |
8474 | r_off = r_off2; | |
8475 | loc = p; | |
8476 | } | |
8477 | } | |
8478 | if (loc != base) | |
8479 | { | |
8480 | /* Don't just swap *base and *loc as that changes the order | |
8481 | of the original base[0] and base[1] if they happen to | |
8482 | have the same r_offset. */ | |
bca6d0e3 AM |
8483 | bfd_byte onebuf[sizeof (Elf64_External_Rela)]; |
8484 | memcpy (onebuf, loc, elt_size); | |
0e287786 | 8485 | memmove (base + elt_size, base, loc - base); |
bca6d0e3 | 8486 | memcpy (base, onebuf, elt_size); |
0e287786 AM |
8487 | } |
8488 | ||
b29b8669 | 8489 | for (p = base + elt_size; (p += elt_size) < end; ) |
0e287786 AM |
8490 | { |
8491 | /* base to p is sorted, *p is next to insert. */ | |
8492 | r_off = (*ext_r_off) (p); | |
8493 | /* Search the sorted region for location to insert. */ | |
8494 | loc = p - elt_size; | |
8495 | while (r_off < (*ext_r_off) (loc)) | |
8496 | loc -= elt_size; | |
8497 | loc += elt_size; | |
8498 | if (loc != p) | |
8499 | { | |
bca6d0e3 AM |
8500 | /* Chances are there is a run of relocs to insert here, |
8501 | from one of more input files. Files are not always | |
8502 | linked in order due to the way elf_link_input_bfd is | |
8503 | called. See pr17666. */ | |
8504 | size_t sortlen = p - loc; | |
8505 | bfd_vma r_off2 = (*ext_r_off) (loc); | |
8506 | size_t runlen = elt_size; | |
8507 | size_t buf_size = 96 * 1024; | |
8508 | while (p + runlen < end | |
8509 | && (sortlen <= buf_size | |
8510 | || runlen + elt_size <= buf_size) | |
8511 | && r_off2 > (*ext_r_off) (p + runlen)) | |
8512 | runlen += elt_size; | |
8513 | if (buf == NULL) | |
8514 | { | |
8515 | buf = bfd_malloc (buf_size); | |
8516 | if (buf == NULL) | |
8517 | return FALSE; | |
8518 | } | |
8519 | if (runlen < sortlen) | |
8520 | { | |
8521 | memcpy (buf, p, runlen); | |
8522 | memmove (loc + runlen, loc, sortlen); | |
8523 | memcpy (loc, buf, runlen); | |
8524 | } | |
8525 | else | |
8526 | { | |
8527 | memcpy (buf, loc, sortlen); | |
8528 | memmove (loc, p, runlen); | |
8529 | memcpy (loc + runlen, buf, sortlen); | |
8530 | } | |
b29b8669 | 8531 | p += runlen - elt_size; |
0e287786 AM |
8532 | } |
8533 | } | |
8534 | /* Hashes are no longer valid. */ | |
28dbcedc AM |
8535 | free (reldata->hashes); |
8536 | reldata->hashes = NULL; | |
bca6d0e3 | 8537 | free (buf); |
53df40a4 | 8538 | } |
bca6d0e3 | 8539 | return TRUE; |
c152c796 AM |
8540 | } |
8541 | ||
8542 | struct elf_link_sort_rela | |
8543 | { | |
8544 | union { | |
8545 | bfd_vma offset; | |
8546 | bfd_vma sym_mask; | |
8547 | } u; | |
8548 | enum elf_reloc_type_class type; | |
8549 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
8550 | Elf_Internal_Rela rela[1]; | |
8551 | }; | |
8552 | ||
8553 | static int | |
8554 | elf_link_sort_cmp1 (const void *A, const void *B) | |
8555 | { | |
a50b1753 NC |
8556 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8557 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 AM |
8558 | int relativea, relativeb; |
8559 | ||
8560 | relativea = a->type == reloc_class_relative; | |
8561 | relativeb = b->type == reloc_class_relative; | |
8562 | ||
8563 | if (relativea < relativeb) | |
8564 | return 1; | |
8565 | if (relativea > relativeb) | |
8566 | return -1; | |
8567 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
8568 | return -1; | |
8569 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
8570 | return 1; | |
8571 | if (a->rela->r_offset < b->rela->r_offset) | |
8572 | return -1; | |
8573 | if (a->rela->r_offset > b->rela->r_offset) | |
8574 | return 1; | |
8575 | return 0; | |
8576 | } | |
8577 | ||
8578 | static int | |
8579 | elf_link_sort_cmp2 (const void *A, const void *B) | |
8580 | { | |
a50b1753 NC |
8581 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8582 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 | 8583 | |
7e612e98 | 8584 | if (a->type < b->type) |
c152c796 | 8585 | return -1; |
7e612e98 | 8586 | if (a->type > b->type) |
c152c796 | 8587 | return 1; |
7e612e98 | 8588 | if (a->u.offset < b->u.offset) |
c152c796 | 8589 | return -1; |
7e612e98 | 8590 | if (a->u.offset > b->u.offset) |
c152c796 AM |
8591 | return 1; |
8592 | if (a->rela->r_offset < b->rela->r_offset) | |
8593 | return -1; | |
8594 | if (a->rela->r_offset > b->rela->r_offset) | |
8595 | return 1; | |
8596 | return 0; | |
8597 | } | |
8598 | ||
8599 | static size_t | |
8600 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
8601 | { | |
3410fea8 | 8602 | asection *dynamic_relocs; |
fc66a176 L |
8603 | asection *rela_dyn; |
8604 | asection *rel_dyn; | |
c152c796 AM |
8605 | bfd_size_type count, size; |
8606 | size_t i, ret, sort_elt, ext_size; | |
8607 | bfd_byte *sort, *s_non_relative, *p; | |
8608 | struct elf_link_sort_rela *sq; | |
8609 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8610 | int i2e = bed->s->int_rels_per_ext_rel; | |
c8e44c6d | 8611 | unsigned int opb = bfd_octets_per_byte (abfd); |
c152c796 AM |
8612 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
8613 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8614 | struct bfd_link_order *lo; | |
8615 | bfd_vma r_sym_mask; | |
3410fea8 | 8616 | bfd_boolean use_rela; |
c152c796 | 8617 | |
3410fea8 NC |
8618 | /* Find a dynamic reloc section. */ |
8619 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
8620 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
8621 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
8622 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 8623 | { |
3410fea8 NC |
8624 | bfd_boolean use_rela_initialised = FALSE; |
8625 | ||
8626 | /* This is just here to stop gcc from complaining. | |
c8e44c6d | 8627 | Its initialization checking code is not perfect. */ |
3410fea8 NC |
8628 | use_rela = TRUE; |
8629 | ||
8630 | /* Both sections are present. Examine the sizes | |
8631 | of the indirect sections to help us choose. */ | |
8632 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8633 | if (lo->type == bfd_indirect_link_order) | |
8634 | { | |
8635 | asection *o = lo->u.indirect.section; | |
8636 | ||
8637 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8638 | { | |
8639 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8640 | /* Section size is divisible by both rel and rela sizes. | |
8641 | It is of no help to us. */ | |
8642 | ; | |
8643 | else | |
8644 | { | |
8645 | /* Section size is only divisible by rela. */ | |
8646 | if (use_rela_initialised && (use_rela == FALSE)) | |
8647 | { | |
c8e44c6d AM |
8648 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8649 | "they are in more than one size"), | |
8650 | abfd); | |
3410fea8 NC |
8651 | bfd_set_error (bfd_error_invalid_operation); |
8652 | return 0; | |
8653 | } | |
8654 | else | |
8655 | { | |
8656 | use_rela = TRUE; | |
8657 | use_rela_initialised = TRUE; | |
8658 | } | |
8659 | } | |
8660 | } | |
8661 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
8662 | { | |
8663 | /* Section size is only divisible by rel. */ | |
8664 | if (use_rela_initialised && (use_rela == TRUE)) | |
8665 | { | |
c8e44c6d AM |
8666 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8667 | "they are in more than one size"), | |
8668 | abfd); | |
3410fea8 NC |
8669 | bfd_set_error (bfd_error_invalid_operation); |
8670 | return 0; | |
8671 | } | |
8672 | else | |
8673 | { | |
8674 | use_rela = FALSE; | |
8675 | use_rela_initialised = TRUE; | |
8676 | } | |
8677 | } | |
8678 | else | |
8679 | { | |
c8e44c6d AM |
8680 | /* The section size is not divisible by either - |
8681 | something is wrong. */ | |
8682 | _bfd_error_handler (_("%B: Unable to sort relocs - " | |
8683 | "they are of an unknown size"), abfd); | |
3410fea8 NC |
8684 | bfd_set_error (bfd_error_invalid_operation); |
8685 | return 0; | |
8686 | } | |
8687 | } | |
8688 | ||
8689 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8690 | if (lo->type == bfd_indirect_link_order) | |
8691 | { | |
8692 | asection *o = lo->u.indirect.section; | |
8693 | ||
8694 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8695 | { | |
8696 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8697 | /* Section size is divisible by both rel and rela sizes. | |
8698 | It is of no help to us. */ | |
8699 | ; | |
8700 | else | |
8701 | { | |
8702 | /* Section size is only divisible by rela. */ | |
8703 | if (use_rela_initialised && (use_rela == FALSE)) | |
8704 | { | |
c8e44c6d AM |
8705 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8706 | "they are in more than one size"), | |
8707 | abfd); | |
3410fea8 NC |
8708 | bfd_set_error (bfd_error_invalid_operation); |
8709 | return 0; | |
8710 | } | |
8711 | else | |
8712 | { | |
8713 | use_rela = TRUE; | |
8714 | use_rela_initialised = TRUE; | |
8715 | } | |
8716 | } | |
8717 | } | |
8718 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
8719 | { | |
8720 | /* Section size is only divisible by rel. */ | |
8721 | if (use_rela_initialised && (use_rela == TRUE)) | |
8722 | { | |
c8e44c6d AM |
8723 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8724 | "they are in more than one size"), | |
8725 | abfd); | |
3410fea8 NC |
8726 | bfd_set_error (bfd_error_invalid_operation); |
8727 | return 0; | |
8728 | } | |
8729 | else | |
8730 | { | |
8731 | use_rela = FALSE; | |
8732 | use_rela_initialised = TRUE; | |
8733 | } | |
8734 | } | |
8735 | else | |
8736 | { | |
c8e44c6d AM |
8737 | /* The section size is not divisible by either - |
8738 | something is wrong. */ | |
8739 | _bfd_error_handler (_("%B: Unable to sort relocs - " | |
8740 | "they are of an unknown size"), abfd); | |
3410fea8 NC |
8741 | bfd_set_error (bfd_error_invalid_operation); |
8742 | return 0; | |
8743 | } | |
8744 | } | |
8745 | ||
8746 | if (! use_rela_initialised) | |
8747 | /* Make a guess. */ | |
8748 | use_rela = TRUE; | |
c152c796 | 8749 | } |
fc66a176 L |
8750 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
8751 | use_rela = TRUE; | |
8752 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 8753 | use_rela = FALSE; |
c152c796 | 8754 | else |
fc66a176 | 8755 | return 0; |
3410fea8 NC |
8756 | |
8757 | if (use_rela) | |
c152c796 | 8758 | { |
3410fea8 | 8759 | dynamic_relocs = rela_dyn; |
c152c796 AM |
8760 | ext_size = bed->s->sizeof_rela; |
8761 | swap_in = bed->s->swap_reloca_in; | |
8762 | swap_out = bed->s->swap_reloca_out; | |
8763 | } | |
3410fea8 NC |
8764 | else |
8765 | { | |
8766 | dynamic_relocs = rel_dyn; | |
8767 | ext_size = bed->s->sizeof_rel; | |
8768 | swap_in = bed->s->swap_reloc_in; | |
8769 | swap_out = bed->s->swap_reloc_out; | |
8770 | } | |
c152c796 AM |
8771 | |
8772 | size = 0; | |
3410fea8 | 8773 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 8774 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 8775 | size += lo->u.indirect.section->size; |
c152c796 | 8776 | |
3410fea8 | 8777 | if (size != dynamic_relocs->size) |
c152c796 AM |
8778 | return 0; |
8779 | ||
8780 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
8781 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
8782 | |
8783 | count = dynamic_relocs->size / ext_size; | |
5e486aa1 NC |
8784 | if (count == 0) |
8785 | return 0; | |
a50b1753 | 8786 | sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); |
3410fea8 | 8787 | |
c152c796 AM |
8788 | if (sort == NULL) |
8789 | { | |
8790 | (*info->callbacks->warning) | |
8791 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
8792 | return 0; | |
8793 | } | |
8794 | ||
8795 | if (bed->s->arch_size == 32) | |
8796 | r_sym_mask = ~(bfd_vma) 0xff; | |
8797 | else | |
8798 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
8799 | ||
3410fea8 | 8800 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
8801 | if (lo->type == bfd_indirect_link_order) |
8802 | { | |
8803 | bfd_byte *erel, *erelend; | |
8804 | asection *o = lo->u.indirect.section; | |
8805 | ||
1da212d6 AM |
8806 | if (o->contents == NULL && o->size != 0) |
8807 | { | |
8808 | /* This is a reloc section that is being handled as a normal | |
8809 | section. See bfd_section_from_shdr. We can't combine | |
8810 | relocs in this case. */ | |
8811 | free (sort); | |
8812 | return 0; | |
8813 | } | |
c152c796 | 8814 | erel = o->contents; |
eea6121a | 8815 | erelend = o->contents + o->size; |
c8e44c6d | 8816 | p = sort + o->output_offset * opb / ext_size * sort_elt; |
3410fea8 | 8817 | |
c152c796 AM |
8818 | while (erel < erelend) |
8819 | { | |
8820 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 8821 | |
c152c796 | 8822 | (*swap_in) (abfd, erel, s->rela); |
7e612e98 | 8823 | s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); |
c152c796 AM |
8824 | s->u.sym_mask = r_sym_mask; |
8825 | p += sort_elt; | |
8826 | erel += ext_size; | |
8827 | } | |
8828 | } | |
8829 | ||
8830 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
8831 | ||
8832 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
8833 | { | |
8834 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
8835 | if (s->type != reloc_class_relative) | |
8836 | break; | |
8837 | } | |
8838 | ret = i; | |
8839 | s_non_relative = p; | |
8840 | ||
8841 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
8842 | for (; i < count; i++, p += sort_elt) | |
8843 | { | |
8844 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
8845 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
8846 | sq = sp; | |
8847 | sp->u.offset = sq->rela->r_offset; | |
8848 | } | |
8849 | ||
8850 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
8851 | ||
c8e44c6d AM |
8852 | struct elf_link_hash_table *htab = elf_hash_table (info); |
8853 | if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) | |
8854 | { | |
8855 | /* We have plt relocs in .rela.dyn. */ | |
8856 | sq = (struct elf_link_sort_rela *) sort; | |
8857 | for (i = 0; i < count; i++) | |
8858 | if (sq[count - i - 1].type != reloc_class_plt) | |
8859 | break; | |
8860 | if (i != 0 && htab->srelplt->size == i * ext_size) | |
8861 | { | |
8862 | struct bfd_link_order **plo; | |
8863 | /* Put srelplt link_order last. This is so the output_offset | |
8864 | set in the next loop is correct for DT_JMPREL. */ | |
8865 | for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) | |
8866 | if ((*plo)->type == bfd_indirect_link_order | |
8867 | && (*plo)->u.indirect.section == htab->srelplt) | |
8868 | { | |
8869 | lo = *plo; | |
8870 | *plo = lo->next; | |
8871 | } | |
8872 | else | |
8873 | plo = &(*plo)->next; | |
8874 | *plo = lo; | |
8875 | lo->next = NULL; | |
8876 | dynamic_relocs->map_tail.link_order = lo; | |
8877 | } | |
8878 | } | |
8879 | ||
8880 | p = sort; | |
3410fea8 | 8881 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
8882 | if (lo->type == bfd_indirect_link_order) |
8883 | { | |
8884 | bfd_byte *erel, *erelend; | |
8885 | asection *o = lo->u.indirect.section; | |
8886 | ||
8887 | erel = o->contents; | |
eea6121a | 8888 | erelend = o->contents + o->size; |
c8e44c6d | 8889 | o->output_offset = (p - sort) / sort_elt * ext_size / opb; |
c152c796 AM |
8890 | while (erel < erelend) |
8891 | { | |
8892 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
8893 | (*swap_out) (abfd, s->rela, erel); | |
8894 | p += sort_elt; | |
8895 | erel += ext_size; | |
8896 | } | |
8897 | } | |
8898 | ||
8899 | free (sort); | |
3410fea8 | 8900 | *psec = dynamic_relocs; |
c152c796 AM |
8901 | return ret; |
8902 | } | |
8903 | ||
ef10c3ac | 8904 | /* Add a symbol to the output symbol string table. */ |
c152c796 | 8905 | |
6e0b88f1 | 8906 | static int |
ef10c3ac L |
8907 | elf_link_output_symstrtab (struct elf_final_link_info *flinfo, |
8908 | const char *name, | |
8909 | Elf_Internal_Sym *elfsym, | |
8910 | asection *input_sec, | |
8911 | struct elf_link_hash_entry *h) | |
c152c796 | 8912 | { |
6e0b88f1 | 8913 | int (*output_symbol_hook) |
c152c796 AM |
8914 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, |
8915 | struct elf_link_hash_entry *); | |
ef10c3ac | 8916 | struct elf_link_hash_table *hash_table; |
c152c796 | 8917 | const struct elf_backend_data *bed; |
ef10c3ac | 8918 | bfd_size_type strtabsize; |
c152c796 | 8919 | |
8539e4e8 AM |
8920 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); |
8921 | ||
8b127cbc | 8922 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 AM |
8923 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; |
8924 | if (output_symbol_hook != NULL) | |
8925 | { | |
8b127cbc | 8926 | int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); |
6e0b88f1 AM |
8927 | if (ret != 1) |
8928 | return ret; | |
c152c796 AM |
8929 | } |
8930 | ||
ef10c3ac L |
8931 | if (name == NULL |
8932 | || *name == '\0' | |
8933 | || (input_sec->flags & SEC_EXCLUDE)) | |
8934 | elfsym->st_name = (unsigned long) -1; | |
c152c796 AM |
8935 | else |
8936 | { | |
ef10c3ac L |
8937 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
8938 | to get the final offset for st_name. */ | |
8939 | elfsym->st_name | |
8940 | = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, | |
8941 | name, FALSE); | |
c152c796 | 8942 | if (elfsym->st_name == (unsigned long) -1) |
6e0b88f1 | 8943 | return 0; |
c152c796 AM |
8944 | } |
8945 | ||
ef10c3ac L |
8946 | hash_table = elf_hash_table (flinfo->info); |
8947 | strtabsize = hash_table->strtabsize; | |
8948 | if (strtabsize <= hash_table->strtabcount) | |
c152c796 | 8949 | { |
ef10c3ac L |
8950 | strtabsize += strtabsize; |
8951 | hash_table->strtabsize = strtabsize; | |
8952 | strtabsize *= sizeof (*hash_table->strtab); | |
8953 | hash_table->strtab | |
8954 | = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, | |
8955 | strtabsize); | |
8956 | if (hash_table->strtab == NULL) | |
6e0b88f1 | 8957 | return 0; |
c152c796 | 8958 | } |
ef10c3ac L |
8959 | hash_table->strtab[hash_table->strtabcount].sym = *elfsym; |
8960 | hash_table->strtab[hash_table->strtabcount].dest_index | |
8961 | = hash_table->strtabcount; | |
8962 | hash_table->strtab[hash_table->strtabcount].destshndx_index | |
8963 | = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0; | |
8964 | ||
8965 | bfd_get_symcount (flinfo->output_bfd) += 1; | |
8966 | hash_table->strtabcount += 1; | |
8967 | ||
8968 | return 1; | |
8969 | } | |
8970 | ||
8971 | /* Swap symbols out to the symbol table and flush the output symbols to | |
8972 | the file. */ | |
8973 | ||
8974 | static bfd_boolean | |
8975 | elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) | |
8976 | { | |
8977 | struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); | |
ef53be89 AM |
8978 | bfd_size_type amt; |
8979 | size_t i; | |
ef10c3ac L |
8980 | const struct elf_backend_data *bed; |
8981 | bfd_byte *symbuf; | |
8982 | Elf_Internal_Shdr *hdr; | |
8983 | file_ptr pos; | |
8984 | bfd_boolean ret; | |
8985 | ||
8986 | if (!hash_table->strtabcount) | |
8987 | return TRUE; | |
8988 | ||
8989 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); | |
8990 | ||
8991 | bed = get_elf_backend_data (flinfo->output_bfd); | |
c152c796 | 8992 | |
ef10c3ac L |
8993 | amt = bed->s->sizeof_sym * hash_table->strtabcount; |
8994 | symbuf = (bfd_byte *) bfd_malloc (amt); | |
8995 | if (symbuf == NULL) | |
8996 | return FALSE; | |
1b786873 | 8997 | |
ef10c3ac | 8998 | if (flinfo->symshndxbuf) |
c152c796 | 8999 | { |
ef53be89 AM |
9000 | amt = sizeof (Elf_External_Sym_Shndx); |
9001 | amt *= bfd_get_symcount (flinfo->output_bfd); | |
ef10c3ac L |
9002 | flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); |
9003 | if (flinfo->symshndxbuf == NULL) | |
c152c796 | 9004 | { |
ef10c3ac L |
9005 | free (symbuf); |
9006 | return FALSE; | |
c152c796 | 9007 | } |
c152c796 AM |
9008 | } |
9009 | ||
ef10c3ac L |
9010 | for (i = 0; i < hash_table->strtabcount; i++) |
9011 | { | |
9012 | struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; | |
9013 | if (elfsym->sym.st_name == (unsigned long) -1) | |
9014 | elfsym->sym.st_name = 0; | |
9015 | else | |
9016 | elfsym->sym.st_name | |
9017 | = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, | |
9018 | elfsym->sym.st_name); | |
9019 | bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, | |
9020 | ((bfd_byte *) symbuf | |
9021 | + (elfsym->dest_index | |
9022 | * bed->s->sizeof_sym)), | |
9023 | (flinfo->symshndxbuf | |
9024 | + elfsym->destshndx_index)); | |
9025 | } | |
9026 | ||
9027 | hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; | |
9028 | pos = hdr->sh_offset + hdr->sh_size; | |
9029 | amt = hash_table->strtabcount * bed->s->sizeof_sym; | |
9030 | if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 | |
9031 | && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt) | |
9032 | { | |
9033 | hdr->sh_size += amt; | |
9034 | ret = TRUE; | |
9035 | } | |
9036 | else | |
9037 | ret = FALSE; | |
c152c796 | 9038 | |
ef10c3ac L |
9039 | free (symbuf); |
9040 | ||
9041 | free (hash_table->strtab); | |
9042 | hash_table->strtab = NULL; | |
9043 | ||
9044 | return ret; | |
c152c796 AM |
9045 | } |
9046 | ||
c0d5a53d L |
9047 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
9048 | ||
9049 | static bfd_boolean | |
9050 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
9051 | { | |
4fbb74a6 AM |
9052 | if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) |
9053 | && sym->st_shndx < SHN_LORESERVE) | |
c0d5a53d L |
9054 | { |
9055 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 9056 | beyond 64k. */ |
4eca0228 | 9057 | _bfd_error_handler |
695344c0 | 9058 | /* xgettext:c-format */ |
c0d5a53d | 9059 | (_("%B: Too many sections: %d (>= %d)"), |
4fbb74a6 | 9060 | abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); |
c0d5a53d L |
9061 | bfd_set_error (bfd_error_nonrepresentable_section); |
9062 | return FALSE; | |
9063 | } | |
9064 | return TRUE; | |
9065 | } | |
9066 | ||
c152c796 AM |
9067 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
9068 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
9069 | versioned symbol that would normally require an explicit version. | |
9070 | We also handle the case that a DSO references a hidden symbol | |
9071 | which may be satisfied by a versioned symbol in another DSO. */ | |
9072 | ||
9073 | static bfd_boolean | |
9074 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
9075 | const struct elf_backend_data *bed, | |
9076 | struct elf_link_hash_entry *h) | |
9077 | { | |
9078 | bfd *abfd; | |
9079 | struct elf_link_loaded_list *loaded; | |
9080 | ||
9081 | if (!is_elf_hash_table (info->hash)) | |
9082 | return FALSE; | |
9083 | ||
90c984fc L |
9084 | /* Check indirect symbol. */ |
9085 | while (h->root.type == bfd_link_hash_indirect) | |
9086 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9087 | ||
c152c796 AM |
9088 | switch (h->root.type) |
9089 | { | |
9090 | default: | |
9091 | abfd = NULL; | |
9092 | break; | |
9093 | ||
9094 | case bfd_link_hash_undefined: | |
9095 | case bfd_link_hash_undefweak: | |
9096 | abfd = h->root.u.undef.abfd; | |
f4ab0e2d L |
9097 | if (abfd == NULL |
9098 | || (abfd->flags & DYNAMIC) == 0 | |
e56f61be | 9099 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
9100 | return FALSE; |
9101 | break; | |
9102 | ||
9103 | case bfd_link_hash_defined: | |
9104 | case bfd_link_hash_defweak: | |
9105 | abfd = h->root.u.def.section->owner; | |
9106 | break; | |
9107 | ||
9108 | case bfd_link_hash_common: | |
9109 | abfd = h->root.u.c.p->section->owner; | |
9110 | break; | |
9111 | } | |
9112 | BFD_ASSERT (abfd != NULL); | |
9113 | ||
9114 | for (loaded = elf_hash_table (info)->loaded; | |
9115 | loaded != NULL; | |
9116 | loaded = loaded->next) | |
9117 | { | |
9118 | bfd *input; | |
9119 | Elf_Internal_Shdr *hdr; | |
ef53be89 AM |
9120 | size_t symcount; |
9121 | size_t extsymcount; | |
9122 | size_t extsymoff; | |
c152c796 AM |
9123 | Elf_Internal_Shdr *versymhdr; |
9124 | Elf_Internal_Sym *isym; | |
9125 | Elf_Internal_Sym *isymend; | |
9126 | Elf_Internal_Sym *isymbuf; | |
9127 | Elf_External_Versym *ever; | |
9128 | Elf_External_Versym *extversym; | |
9129 | ||
9130 | input = loaded->abfd; | |
9131 | ||
9132 | /* We check each DSO for a possible hidden versioned definition. */ | |
9133 | if (input == abfd | |
9134 | || (input->flags & DYNAMIC) == 0 | |
9135 | || elf_dynversym (input) == 0) | |
9136 | continue; | |
9137 | ||
9138 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
9139 | ||
9140 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
9141 | if (elf_bad_symtab (input)) | |
9142 | { | |
9143 | extsymcount = symcount; | |
9144 | extsymoff = 0; | |
9145 | } | |
9146 | else | |
9147 | { | |
9148 | extsymcount = symcount - hdr->sh_info; | |
9149 | extsymoff = hdr->sh_info; | |
9150 | } | |
9151 | ||
9152 | if (extsymcount == 0) | |
9153 | continue; | |
9154 | ||
9155 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
9156 | NULL, NULL, NULL); | |
9157 | if (isymbuf == NULL) | |
9158 | return FALSE; | |
9159 | ||
9160 | /* Read in any version definitions. */ | |
9161 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
a50b1753 | 9162 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
c152c796 AM |
9163 | if (extversym == NULL) |
9164 | goto error_ret; | |
9165 | ||
9166 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
9167 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
9168 | != versymhdr->sh_size)) | |
9169 | { | |
9170 | free (extversym); | |
9171 | error_ret: | |
9172 | free (isymbuf); | |
9173 | return FALSE; | |
9174 | } | |
9175 | ||
9176 | ever = extversym + extsymoff; | |
9177 | isymend = isymbuf + extsymcount; | |
9178 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
9179 | { | |
9180 | const char *name; | |
9181 | Elf_Internal_Versym iver; | |
9182 | unsigned short version_index; | |
9183 | ||
9184 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
9185 | || isym->st_shndx == SHN_UNDEF) | |
9186 | continue; | |
9187 | ||
9188 | name = bfd_elf_string_from_elf_section (input, | |
9189 | hdr->sh_link, | |
9190 | isym->st_name); | |
9191 | if (strcmp (name, h->root.root.string) != 0) | |
9192 | continue; | |
9193 | ||
9194 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
9195 | ||
d023c380 L |
9196 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 |
9197 | && !(h->def_regular | |
9198 | && h->forced_local)) | |
c152c796 AM |
9199 | { |
9200 | /* If we have a non-hidden versioned sym, then it should | |
d023c380 L |
9201 | have provided a definition for the undefined sym unless |
9202 | it is defined in a non-shared object and forced local. | |
9203 | */ | |
c152c796 AM |
9204 | abort (); |
9205 | } | |
9206 | ||
9207 | version_index = iver.vs_vers & VERSYM_VERSION; | |
9208 | if (version_index == 1 || version_index == 2) | |
9209 | { | |
9210 | /* This is the base or first version. We can use it. */ | |
9211 | free (extversym); | |
9212 | free (isymbuf); | |
9213 | return TRUE; | |
9214 | } | |
9215 | } | |
9216 | ||
9217 | free (extversym); | |
9218 | free (isymbuf); | |
9219 | } | |
9220 | ||
9221 | return FALSE; | |
9222 | } | |
9223 | ||
b8871f35 L |
9224 | /* Convert ELF common symbol TYPE. */ |
9225 | ||
9226 | static int | |
9227 | elf_link_convert_common_type (struct bfd_link_info *info, int type) | |
9228 | { | |
9229 | /* Commom symbol can only appear in relocatable link. */ | |
9230 | if (!bfd_link_relocatable (info)) | |
9231 | abort (); | |
9232 | switch (info->elf_stt_common) | |
9233 | { | |
9234 | case unchanged: | |
9235 | break; | |
9236 | case elf_stt_common: | |
9237 | type = STT_COMMON; | |
9238 | break; | |
9239 | case no_elf_stt_common: | |
9240 | type = STT_OBJECT; | |
9241 | break; | |
9242 | } | |
9243 | return type; | |
9244 | } | |
9245 | ||
c152c796 AM |
9246 | /* Add an external symbol to the symbol table. This is called from |
9247 | the hash table traversal routine. When generating a shared object, | |
9248 | we go through the symbol table twice. The first time we output | |
9249 | anything that might have been forced to local scope in a version | |
9250 | script. The second time we output the symbols that are still | |
9251 | global symbols. */ | |
9252 | ||
9253 | static bfd_boolean | |
7686d77d | 9254 | elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) |
c152c796 | 9255 | { |
7686d77d | 9256 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; |
a50b1753 | 9257 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
8b127cbc | 9258 | struct elf_final_link_info *flinfo = eoinfo->flinfo; |
c152c796 AM |
9259 | bfd_boolean strip; |
9260 | Elf_Internal_Sym sym; | |
9261 | asection *input_sec; | |
9262 | const struct elf_backend_data *bed; | |
6e0b88f1 AM |
9263 | long indx; |
9264 | int ret; | |
b8871f35 | 9265 | unsigned int type; |
c152c796 AM |
9266 | |
9267 | if (h->root.type == bfd_link_hash_warning) | |
9268 | { | |
9269 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9270 | if (h->root.type == bfd_link_hash_new) | |
9271 | return TRUE; | |
9272 | } | |
9273 | ||
9274 | /* Decide whether to output this symbol in this pass. */ | |
9275 | if (eoinfo->localsyms) | |
9276 | { | |
4deb8f71 | 9277 | if (!h->forced_local) |
c152c796 AM |
9278 | return TRUE; |
9279 | } | |
9280 | else | |
9281 | { | |
4deb8f71 | 9282 | if (h->forced_local) |
c152c796 AM |
9283 | return TRUE; |
9284 | } | |
9285 | ||
8b127cbc | 9286 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 | 9287 | |
12ac1cf5 | 9288 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 9289 | { |
12ac1cf5 NC |
9290 | /* If we have an undefined symbol reference here then it must have |
9291 | come from a shared library that is being linked in. (Undefined | |
98da7939 L |
9292 | references in regular files have already been handled unless |
9293 | they are in unreferenced sections which are removed by garbage | |
9294 | collection). */ | |
12ac1cf5 NC |
9295 | bfd_boolean ignore_undef = FALSE; |
9296 | ||
9297 | /* Some symbols may be special in that the fact that they're | |
9298 | undefined can be safely ignored - let backend determine that. */ | |
9299 | if (bed->elf_backend_ignore_undef_symbol) | |
9300 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
9301 | ||
9302 | /* If we are reporting errors for this situation then do so now. */ | |
89a2ee5a | 9303 | if (!ignore_undef |
12ac1cf5 | 9304 | && h->ref_dynamic |
8b127cbc AM |
9305 | && (!h->ref_regular || flinfo->info->gc_sections) |
9306 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h) | |
9307 | && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
1a72702b AM |
9308 | (*flinfo->info->callbacks->undefined_symbol) |
9309 | (flinfo->info, h->root.root.string, | |
9310 | h->ref_regular ? NULL : h->root.u.undef.abfd, | |
9311 | NULL, 0, | |
9312 | flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR); | |
97196564 L |
9313 | |
9314 | /* Strip a global symbol defined in a discarded section. */ | |
9315 | if (h->indx == -3) | |
9316 | return TRUE; | |
c152c796 AM |
9317 | } |
9318 | ||
9319 | /* We should also warn if a forced local symbol is referenced from | |
9320 | shared libraries. */ | |
0e1862bb | 9321 | if (bfd_link_executable (flinfo->info) |
f5385ebf AM |
9322 | && h->forced_local |
9323 | && h->ref_dynamic | |
371a5866 | 9324 | && h->def_regular |
f5385ebf | 9325 | && !h->dynamic_def |
ee659f1f | 9326 | && h->ref_dynamic_nonweak |
8b127cbc | 9327 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) |
c152c796 | 9328 | { |
17d078c5 AM |
9329 | bfd *def_bfd; |
9330 | const char *msg; | |
90c984fc L |
9331 | struct elf_link_hash_entry *hi = h; |
9332 | ||
9333 | /* Check indirect symbol. */ | |
9334 | while (hi->root.type == bfd_link_hash_indirect) | |
9335 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
17d078c5 AM |
9336 | |
9337 | if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
695344c0 | 9338 | /* xgettext:c-format */ |
17d078c5 AM |
9339 | msg = _("%B: internal symbol `%s' in %B is referenced by DSO"); |
9340 | else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) | |
695344c0 | 9341 | /* xgettext:c-format */ |
17d078c5 AM |
9342 | msg = _("%B: hidden symbol `%s' in %B is referenced by DSO"); |
9343 | else | |
695344c0 | 9344 | /* xgettext:c-format */ |
17d078c5 | 9345 | msg = _("%B: local symbol `%s' in %B is referenced by DSO"); |
8b127cbc | 9346 | def_bfd = flinfo->output_bfd; |
90c984fc L |
9347 | if (hi->root.u.def.section != bfd_abs_section_ptr) |
9348 | def_bfd = hi->root.u.def.section->owner; | |
4eca0228 AM |
9349 | _bfd_error_handler (msg, flinfo->output_bfd, def_bfd, |
9350 | h->root.root.string); | |
17d078c5 | 9351 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9352 | eoinfo->failed = TRUE; |
9353 | return FALSE; | |
9354 | } | |
9355 | ||
9356 | /* We don't want to output symbols that have never been mentioned by | |
9357 | a regular file, or that we have been told to strip. However, if | |
9358 | h->indx is set to -2, the symbol is used by a reloc and we must | |
9359 | output it. */ | |
d983c8c5 | 9360 | strip = FALSE; |
c152c796 | 9361 | if (h->indx == -2) |
d983c8c5 | 9362 | ; |
f5385ebf | 9363 | else if ((h->def_dynamic |
77cfaee6 AM |
9364 | || h->ref_dynamic |
9365 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
9366 | && !h->def_regular |
9367 | && !h->ref_regular) | |
c152c796 | 9368 | strip = TRUE; |
8b127cbc | 9369 | else if (flinfo->info->strip == strip_all) |
c152c796 | 9370 | strip = TRUE; |
8b127cbc AM |
9371 | else if (flinfo->info->strip == strip_some |
9372 | && bfd_hash_lookup (flinfo->info->keep_hash, | |
c152c796 AM |
9373 | h->root.root.string, FALSE, FALSE) == NULL) |
9374 | strip = TRUE; | |
d56d55e7 AM |
9375 | else if ((h->root.type == bfd_link_hash_defined |
9376 | || h->root.type == bfd_link_hash_defweak) | |
8b127cbc | 9377 | && ((flinfo->info->strip_discarded |
dbaa2011 | 9378 | && discarded_section (h->root.u.def.section)) |
ca4be51c AM |
9379 | || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 |
9380 | && h->root.u.def.section->owner != NULL | |
d56d55e7 | 9381 | && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) |
c152c796 | 9382 | strip = TRUE; |
9e2278f5 AM |
9383 | else if ((h->root.type == bfd_link_hash_undefined |
9384 | || h->root.type == bfd_link_hash_undefweak) | |
9385 | && h->root.u.undef.abfd != NULL | |
9386 | && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) | |
9387 | strip = TRUE; | |
c152c796 | 9388 | |
b8871f35 L |
9389 | type = h->type; |
9390 | ||
c152c796 | 9391 | /* If we're stripping it, and it's not a dynamic symbol, there's |
d983c8c5 AM |
9392 | nothing else to do. However, if it is a forced local symbol or |
9393 | an ifunc symbol we need to give the backend finish_dynamic_symbol | |
9394 | function a chance to make it dynamic. */ | |
c152c796 AM |
9395 | if (strip |
9396 | && h->dynindx == -1 | |
b8871f35 | 9397 | && type != STT_GNU_IFUNC |
f5385ebf | 9398 | && !h->forced_local) |
c152c796 AM |
9399 | return TRUE; |
9400 | ||
9401 | sym.st_value = 0; | |
9402 | sym.st_size = h->size; | |
9403 | sym.st_other = h->other; | |
c152c796 AM |
9404 | switch (h->root.type) |
9405 | { | |
9406 | default: | |
9407 | case bfd_link_hash_new: | |
9408 | case bfd_link_hash_warning: | |
9409 | abort (); | |
9410 | return FALSE; | |
9411 | ||
9412 | case bfd_link_hash_undefined: | |
9413 | case bfd_link_hash_undefweak: | |
9414 | input_sec = bfd_und_section_ptr; | |
9415 | sym.st_shndx = SHN_UNDEF; | |
9416 | break; | |
9417 | ||
9418 | case bfd_link_hash_defined: | |
9419 | case bfd_link_hash_defweak: | |
9420 | { | |
9421 | input_sec = h->root.u.def.section; | |
9422 | if (input_sec->output_section != NULL) | |
9423 | { | |
9424 | sym.st_shndx = | |
8b127cbc | 9425 | _bfd_elf_section_from_bfd_section (flinfo->output_bfd, |
c152c796 AM |
9426 | input_sec->output_section); |
9427 | if (sym.st_shndx == SHN_BAD) | |
9428 | { | |
4eca0228 | 9429 | _bfd_error_handler |
695344c0 | 9430 | /* xgettext:c-format */ |
d003868e | 9431 | (_("%B: could not find output section %A for input section %A"), |
8b127cbc | 9432 | flinfo->output_bfd, input_sec->output_section, input_sec); |
17d078c5 | 9433 | bfd_set_error (bfd_error_nonrepresentable_section); |
c152c796 AM |
9434 | eoinfo->failed = TRUE; |
9435 | return FALSE; | |
9436 | } | |
9437 | ||
9438 | /* ELF symbols in relocatable files are section relative, | |
9439 | but in nonrelocatable files they are virtual | |
9440 | addresses. */ | |
9441 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
0e1862bb | 9442 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
9443 | { |
9444 | sym.st_value += input_sec->output_section->vma; | |
9445 | if (h->type == STT_TLS) | |
9446 | { | |
8b127cbc | 9447 | asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; |
430a16a5 NC |
9448 | if (tls_sec != NULL) |
9449 | sym.st_value -= tls_sec->vma; | |
c152c796 AM |
9450 | } |
9451 | } | |
9452 | } | |
9453 | else | |
9454 | { | |
9455 | BFD_ASSERT (input_sec->owner == NULL | |
9456 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
9457 | sym.st_shndx = SHN_UNDEF; | |
9458 | input_sec = bfd_und_section_ptr; | |
9459 | } | |
9460 | } | |
9461 | break; | |
9462 | ||
9463 | case bfd_link_hash_common: | |
9464 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 9465 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
9466 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
9467 | break; | |
9468 | ||
9469 | case bfd_link_hash_indirect: | |
9470 | /* These symbols are created by symbol versioning. They point | |
9471 | to the decorated version of the name. For example, if the | |
9472 | symbol foo@@GNU_1.2 is the default, which should be used when | |
9473 | foo is used with no version, then we add an indirect symbol | |
9474 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
9475 | since the indirected symbol is already in the hash table. */ | |
9476 | return TRUE; | |
9477 | } | |
9478 | ||
b8871f35 L |
9479 | if (type == STT_COMMON || type == STT_OBJECT) |
9480 | switch (h->root.type) | |
9481 | { | |
9482 | case bfd_link_hash_common: | |
9483 | type = elf_link_convert_common_type (flinfo->info, type); | |
9484 | break; | |
9485 | case bfd_link_hash_defined: | |
9486 | case bfd_link_hash_defweak: | |
9487 | if (bed->common_definition (&sym)) | |
9488 | type = elf_link_convert_common_type (flinfo->info, type); | |
9489 | else | |
9490 | type = STT_OBJECT; | |
9491 | break; | |
9492 | case bfd_link_hash_undefined: | |
9493 | case bfd_link_hash_undefweak: | |
9494 | break; | |
9495 | default: | |
9496 | abort (); | |
9497 | } | |
9498 | ||
4deb8f71 | 9499 | if (h->forced_local) |
b8871f35 L |
9500 | { |
9501 | sym.st_info = ELF_ST_INFO (STB_LOCAL, type); | |
9502 | /* Turn off visibility on local symbol. */ | |
9503 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); | |
9504 | } | |
9505 | /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ | |
9506 | else if (h->unique_global && h->def_regular) | |
9507 | sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); | |
9508 | else if (h->root.type == bfd_link_hash_undefweak | |
9509 | || h->root.type == bfd_link_hash_defweak) | |
9510 | sym.st_info = ELF_ST_INFO (STB_WEAK, type); | |
9511 | else | |
9512 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
9513 | sym.st_target_internal = h->target_internal; | |
9514 | ||
c152c796 AM |
9515 | /* Give the processor backend a chance to tweak the symbol value, |
9516 | and also to finish up anything that needs to be done for this | |
9517 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
3aa14d16 | 9518 | forced local syms when non-shared is due to a historical quirk. |
5f35ea9c | 9519 | STT_GNU_IFUNC symbol must go through PLT. */ |
3aa14d16 | 9520 | if ((h->type == STT_GNU_IFUNC |
5f35ea9c | 9521 | && h->def_regular |
0e1862bb | 9522 | && !bfd_link_relocatable (flinfo->info)) |
3aa14d16 L |
9523 | || ((h->dynindx != -1 |
9524 | || h->forced_local) | |
0e1862bb | 9525 | && ((bfd_link_pic (flinfo->info) |
3aa14d16 L |
9526 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
9527 | || h->root.type != bfd_link_hash_undefweak)) | |
9528 | || !h->forced_local) | |
8b127cbc | 9529 | && elf_hash_table (flinfo->info)->dynamic_sections_created)) |
c152c796 AM |
9530 | { |
9531 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8b127cbc | 9532 | (flinfo->output_bfd, flinfo->info, h, &sym))) |
c152c796 AM |
9533 | { |
9534 | eoinfo->failed = TRUE; | |
9535 | return FALSE; | |
9536 | } | |
9537 | } | |
9538 | ||
9539 | /* If we are marking the symbol as undefined, and there are no | |
9540 | non-weak references to this symbol from a regular object, then | |
9541 | mark the symbol as weak undefined; if there are non-weak | |
9542 | references, mark the symbol as strong. We can't do this earlier, | |
9543 | because it might not be marked as undefined until the | |
9544 | finish_dynamic_symbol routine gets through with it. */ | |
9545 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 9546 | && h->ref_regular |
c152c796 AM |
9547 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
9548 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
9549 | { | |
9550 | int bindtype; | |
b8871f35 | 9551 | type = ELF_ST_TYPE (sym.st_info); |
2955ec4c L |
9552 | |
9553 | /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ | |
9554 | if (type == STT_GNU_IFUNC) | |
9555 | type = STT_FUNC; | |
c152c796 | 9556 | |
f5385ebf | 9557 | if (h->ref_regular_nonweak) |
c152c796 AM |
9558 | bindtype = STB_GLOBAL; |
9559 | else | |
9560 | bindtype = STB_WEAK; | |
2955ec4c | 9561 | sym.st_info = ELF_ST_INFO (bindtype, type); |
c152c796 AM |
9562 | } |
9563 | ||
bda987c2 CD |
9564 | /* If this is a symbol defined in a dynamic library, don't use the |
9565 | symbol size from the dynamic library. Relinking an executable | |
9566 | against a new library may introduce gratuitous changes in the | |
9567 | executable's symbols if we keep the size. */ | |
9568 | if (sym.st_shndx == SHN_UNDEF | |
9569 | && !h->def_regular | |
9570 | && h->def_dynamic) | |
9571 | sym.st_size = 0; | |
9572 | ||
c152c796 AM |
9573 | /* If a non-weak symbol with non-default visibility is not defined |
9574 | locally, it is a fatal error. */ | |
0e1862bb | 9575 | if (!bfd_link_relocatable (flinfo->info) |
c152c796 AM |
9576 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT |
9577 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
9578 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 9579 | && !h->def_regular) |
c152c796 | 9580 | { |
17d078c5 AM |
9581 | const char *msg; |
9582 | ||
9583 | if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) | |
695344c0 | 9584 | /* xgettext:c-format */ |
17d078c5 AM |
9585 | msg = _("%B: protected symbol `%s' isn't defined"); |
9586 | else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) | |
695344c0 | 9587 | /* xgettext:c-format */ |
17d078c5 AM |
9588 | msg = _("%B: internal symbol `%s' isn't defined"); |
9589 | else | |
695344c0 | 9590 | /* xgettext:c-format */ |
17d078c5 | 9591 | msg = _("%B: hidden symbol `%s' isn't defined"); |
4eca0228 | 9592 | _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string); |
17d078c5 | 9593 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9594 | eoinfo->failed = TRUE; |
9595 | return FALSE; | |
9596 | } | |
9597 | ||
9598 | /* If this symbol should be put in the .dynsym section, then put it | |
9599 | there now. We already know the symbol index. We also fill in | |
9600 | the entry in the .hash section. */ | |
cae1fbbb | 9601 | if (elf_hash_table (flinfo->info)->dynsym != NULL |
202e2356 | 9602 | && h->dynindx != -1 |
8b127cbc | 9603 | && elf_hash_table (flinfo->info)->dynamic_sections_created) |
c152c796 | 9604 | { |
c152c796 AM |
9605 | bfd_byte *esym; |
9606 | ||
90c984fc L |
9607 | /* Since there is no version information in the dynamic string, |
9608 | if there is no version info in symbol version section, we will | |
1659f720 | 9609 | have a run-time problem if not linking executable, referenced |
4deb8f71 | 9610 | by shared library, or not bound locally. */ |
1659f720 | 9611 | if (h->verinfo.verdef == NULL |
0e1862bb | 9612 | && (!bfd_link_executable (flinfo->info) |
1659f720 L |
9613 | || h->ref_dynamic |
9614 | || !h->def_regular)) | |
90c984fc L |
9615 | { |
9616 | char *p = strrchr (h->root.root.string, ELF_VER_CHR); | |
9617 | ||
9618 | if (p && p [1] != '\0') | |
9619 | { | |
4eca0228 | 9620 | _bfd_error_handler |
695344c0 | 9621 | /* xgettext:c-format */ |
90c984fc L |
9622 | (_("%B: No symbol version section for versioned symbol `%s'"), |
9623 | flinfo->output_bfd, h->root.root.string); | |
9624 | eoinfo->failed = TRUE; | |
9625 | return FALSE; | |
9626 | } | |
9627 | } | |
9628 | ||
c152c796 | 9629 | sym.st_name = h->dynstr_index; |
cae1fbbb L |
9630 | esym = (elf_hash_table (flinfo->info)->dynsym->contents |
9631 | + h->dynindx * bed->s->sizeof_sym); | |
8b127cbc | 9632 | if (!check_dynsym (flinfo->output_bfd, &sym)) |
c0d5a53d L |
9633 | { |
9634 | eoinfo->failed = TRUE; | |
9635 | return FALSE; | |
9636 | } | |
8b127cbc | 9637 | bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); |
c152c796 | 9638 | |
8b127cbc | 9639 | if (flinfo->hash_sec != NULL) |
fdc90cb4 JJ |
9640 | { |
9641 | size_t hash_entry_size; | |
9642 | bfd_byte *bucketpos; | |
9643 | bfd_vma chain; | |
41198d0c L |
9644 | size_t bucketcount; |
9645 | size_t bucket; | |
9646 | ||
8b127cbc | 9647 | bucketcount = elf_hash_table (flinfo->info)->bucketcount; |
41198d0c | 9648 | bucket = h->u.elf_hash_value % bucketcount; |
fdc90cb4 JJ |
9649 | |
9650 | hash_entry_size | |
8b127cbc AM |
9651 | = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; |
9652 | bucketpos = ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 | 9653 | + (bucket + 2) * hash_entry_size); |
8b127cbc AM |
9654 | chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); |
9655 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, | |
9656 | bucketpos); | |
9657 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, | |
9658 | ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 JJ |
9659 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); |
9660 | } | |
c152c796 | 9661 | |
8b127cbc | 9662 | if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) |
c152c796 AM |
9663 | { |
9664 | Elf_Internal_Versym iversym; | |
9665 | Elf_External_Versym *eversym; | |
9666 | ||
f5385ebf | 9667 | if (!h->def_regular) |
c152c796 | 9668 | { |
7b20f099 AM |
9669 | if (h->verinfo.verdef == NULL |
9670 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
9671 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
c152c796 AM |
9672 | iversym.vs_vers = 0; |
9673 | else | |
9674 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
9675 | } | |
9676 | else | |
9677 | { | |
9678 | if (h->verinfo.vertree == NULL) | |
9679 | iversym.vs_vers = 1; | |
9680 | else | |
9681 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
8b127cbc | 9682 | if (flinfo->info->create_default_symver) |
3e3b46e5 | 9683 | iversym.vs_vers++; |
c152c796 AM |
9684 | } |
9685 | ||
422f1182 | 9686 | /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is |
6e33951e | 9687 | defined locally. */ |
422f1182 | 9688 | if (h->versioned == versioned_hidden && h->def_regular) |
c152c796 AM |
9689 | iversym.vs_vers |= VERSYM_HIDDEN; |
9690 | ||
8b127cbc | 9691 | eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; |
c152c796 | 9692 | eversym += h->dynindx; |
8b127cbc | 9693 | _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); |
c152c796 AM |
9694 | } |
9695 | } | |
9696 | ||
d983c8c5 AM |
9697 | /* If the symbol is undefined, and we didn't output it to .dynsym, |
9698 | strip it from .symtab too. Obviously we can't do this for | |
9699 | relocatable output or when needed for --emit-relocs. */ | |
9700 | else if (input_sec == bfd_und_section_ptr | |
9701 | && h->indx != -2 | |
0e1862bb | 9702 | && !bfd_link_relocatable (flinfo->info)) |
d983c8c5 AM |
9703 | return TRUE; |
9704 | /* Also strip others that we couldn't earlier due to dynamic symbol | |
9705 | processing. */ | |
9706 | if (strip) | |
9707 | return TRUE; | |
9708 | if ((input_sec->flags & SEC_EXCLUDE) != 0) | |
c152c796 AM |
9709 | return TRUE; |
9710 | ||
2ec55de3 AM |
9711 | /* Output a FILE symbol so that following locals are not associated |
9712 | with the wrong input file. We need one for forced local symbols | |
9713 | if we've seen more than one FILE symbol or when we have exactly | |
9714 | one FILE symbol but global symbols are present in a file other | |
9715 | than the one with the FILE symbol. We also need one if linker | |
9716 | defined symbols are present. In practice these conditions are | |
9717 | always met, so just emit the FILE symbol unconditionally. */ | |
9718 | if (eoinfo->localsyms | |
9719 | && !eoinfo->file_sym_done | |
9720 | && eoinfo->flinfo->filesym_count != 0) | |
9721 | { | |
9722 | Elf_Internal_Sym fsym; | |
9723 | ||
9724 | memset (&fsym, 0, sizeof (fsym)); | |
9725 | fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
9726 | fsym.st_shndx = SHN_ABS; | |
ef10c3ac L |
9727 | if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, |
9728 | bfd_und_section_ptr, NULL)) | |
2ec55de3 AM |
9729 | return FALSE; |
9730 | ||
9731 | eoinfo->file_sym_done = TRUE; | |
9732 | } | |
9733 | ||
8b127cbc | 9734 | indx = bfd_get_symcount (flinfo->output_bfd); |
ef10c3ac L |
9735 | ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, |
9736 | input_sec, h); | |
6e0b88f1 | 9737 | if (ret == 0) |
c152c796 AM |
9738 | { |
9739 | eoinfo->failed = TRUE; | |
9740 | return FALSE; | |
9741 | } | |
6e0b88f1 AM |
9742 | else if (ret == 1) |
9743 | h->indx = indx; | |
9744 | else if (h->indx == -2) | |
9745 | abort(); | |
c152c796 AM |
9746 | |
9747 | return TRUE; | |
9748 | } | |
9749 | ||
cdd3575c AM |
9750 | /* Return TRUE if special handling is done for relocs in SEC against |
9751 | symbols defined in discarded sections. */ | |
9752 | ||
c152c796 AM |
9753 | static bfd_boolean |
9754 | elf_section_ignore_discarded_relocs (asection *sec) | |
9755 | { | |
9756 | const struct elf_backend_data *bed; | |
9757 | ||
cdd3575c AM |
9758 | switch (sec->sec_info_type) |
9759 | { | |
dbaa2011 AM |
9760 | case SEC_INFO_TYPE_STABS: |
9761 | case SEC_INFO_TYPE_EH_FRAME: | |
2f0c68f2 | 9762 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
cdd3575c AM |
9763 | return TRUE; |
9764 | default: | |
9765 | break; | |
9766 | } | |
c152c796 AM |
9767 | |
9768 | bed = get_elf_backend_data (sec->owner); | |
9769 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
9770 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
9771 | return TRUE; | |
9772 | ||
9773 | return FALSE; | |
9774 | } | |
9775 | ||
9e66c942 AM |
9776 | /* Return a mask saying how ld should treat relocations in SEC against |
9777 | symbols defined in discarded sections. If this function returns | |
9778 | COMPLAIN set, ld will issue a warning message. If this function | |
9779 | returns PRETEND set, and the discarded section was link-once and the | |
9780 | same size as the kept link-once section, ld will pretend that the | |
9781 | symbol was actually defined in the kept section. Otherwise ld will | |
9782 | zero the reloc (at least that is the intent, but some cooperation by | |
9783 | the target dependent code is needed, particularly for REL targets). */ | |
9784 | ||
8a696751 AM |
9785 | unsigned int |
9786 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 9787 | { |
9e66c942 | 9788 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 9789 | return PRETEND; |
cdd3575c AM |
9790 | |
9791 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 9792 | return 0; |
cdd3575c AM |
9793 | |
9794 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 9795 | return 0; |
cdd3575c | 9796 | |
9e66c942 | 9797 | return COMPLAIN | PRETEND; |
cdd3575c AM |
9798 | } |
9799 | ||
3d7f7666 L |
9800 | /* Find a match between a section and a member of a section group. */ |
9801 | ||
9802 | static asection * | |
c0f00686 L |
9803 | match_group_member (asection *sec, asection *group, |
9804 | struct bfd_link_info *info) | |
3d7f7666 L |
9805 | { |
9806 | asection *first = elf_next_in_group (group); | |
9807 | asection *s = first; | |
9808 | ||
9809 | while (s != NULL) | |
9810 | { | |
c0f00686 | 9811 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
9812 | return s; |
9813 | ||
83180ade | 9814 | s = elf_next_in_group (s); |
3d7f7666 L |
9815 | if (s == first) |
9816 | break; | |
9817 | } | |
9818 | ||
9819 | return NULL; | |
9820 | } | |
9821 | ||
01b3c8ab | 9822 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
9823 | to replace it. Return the replacement if it is OK. Otherwise return |
9824 | NULL. */ | |
01b3c8ab L |
9825 | |
9826 | asection * | |
c0f00686 | 9827 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
9828 | { |
9829 | asection *kept; | |
9830 | ||
9831 | kept = sec->kept_section; | |
9832 | if (kept != NULL) | |
9833 | { | |
c2370991 | 9834 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 9835 | kept = match_group_member (sec, kept, info); |
1dd2625f BW |
9836 | if (kept != NULL |
9837 | && ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
9838 | != (kept->rawsize != 0 ? kept->rawsize : kept->size))) | |
01b3c8ab | 9839 | kept = NULL; |
c2370991 | 9840 | sec->kept_section = kept; |
01b3c8ab L |
9841 | } |
9842 | return kept; | |
9843 | } | |
9844 | ||
c152c796 AM |
9845 | /* Link an input file into the linker output file. This function |
9846 | handles all the sections and relocations of the input file at once. | |
9847 | This is so that we only have to read the local symbols once, and | |
9848 | don't have to keep them in memory. */ | |
9849 | ||
9850 | static bfd_boolean | |
8b127cbc | 9851 | elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) |
c152c796 | 9852 | { |
ece5ef60 | 9853 | int (*relocate_section) |
c152c796 AM |
9854 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
9855 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
9856 | bfd *output_bfd; | |
9857 | Elf_Internal_Shdr *symtab_hdr; | |
9858 | size_t locsymcount; | |
9859 | size_t extsymoff; | |
9860 | Elf_Internal_Sym *isymbuf; | |
9861 | Elf_Internal_Sym *isym; | |
9862 | Elf_Internal_Sym *isymend; | |
9863 | long *pindex; | |
9864 | asection **ppsection; | |
9865 | asection *o; | |
9866 | const struct elf_backend_data *bed; | |
c152c796 | 9867 | struct elf_link_hash_entry **sym_hashes; |
310fd250 L |
9868 | bfd_size_type address_size; |
9869 | bfd_vma r_type_mask; | |
9870 | int r_sym_shift; | |
ffbc01cc | 9871 | bfd_boolean have_file_sym = FALSE; |
c152c796 | 9872 | |
8b127cbc | 9873 | output_bfd = flinfo->output_bfd; |
c152c796 AM |
9874 | bed = get_elf_backend_data (output_bfd); |
9875 | relocate_section = bed->elf_backend_relocate_section; | |
9876 | ||
9877 | /* If this is a dynamic object, we don't want to do anything here: | |
9878 | we don't want the local symbols, and we don't want the section | |
9879 | contents. */ | |
9880 | if ((input_bfd->flags & DYNAMIC) != 0) | |
9881 | return TRUE; | |
9882 | ||
c152c796 AM |
9883 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
9884 | if (elf_bad_symtab (input_bfd)) | |
9885 | { | |
9886 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9887 | extsymoff = 0; | |
9888 | } | |
9889 | else | |
9890 | { | |
9891 | locsymcount = symtab_hdr->sh_info; | |
9892 | extsymoff = symtab_hdr->sh_info; | |
9893 | } | |
9894 | ||
9895 | /* Read the local symbols. */ | |
9896 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
9897 | if (isymbuf == NULL && locsymcount != 0) | |
9898 | { | |
9899 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8b127cbc AM |
9900 | flinfo->internal_syms, |
9901 | flinfo->external_syms, | |
9902 | flinfo->locsym_shndx); | |
c152c796 AM |
9903 | if (isymbuf == NULL) |
9904 | return FALSE; | |
9905 | } | |
9906 | ||
9907 | /* Find local symbol sections and adjust values of symbols in | |
9908 | SEC_MERGE sections. Write out those local symbols we know are | |
9909 | going into the output file. */ | |
9910 | isymend = isymbuf + locsymcount; | |
8b127cbc | 9911 | for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; |
c152c796 AM |
9912 | isym < isymend; |
9913 | isym++, pindex++, ppsection++) | |
9914 | { | |
9915 | asection *isec; | |
9916 | const char *name; | |
9917 | Elf_Internal_Sym osym; | |
6e0b88f1 AM |
9918 | long indx; |
9919 | int ret; | |
c152c796 AM |
9920 | |
9921 | *pindex = -1; | |
9922 | ||
9923 | if (elf_bad_symtab (input_bfd)) | |
9924 | { | |
9925 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
9926 | { | |
9927 | *ppsection = NULL; | |
9928 | continue; | |
9929 | } | |
9930 | } | |
9931 | ||
9932 | if (isym->st_shndx == SHN_UNDEF) | |
9933 | isec = bfd_und_section_ptr; | |
c152c796 AM |
9934 | else if (isym->st_shndx == SHN_ABS) |
9935 | isec = bfd_abs_section_ptr; | |
9936 | else if (isym->st_shndx == SHN_COMMON) | |
9937 | isec = bfd_com_section_ptr; | |
9938 | else | |
9939 | { | |
cb33740c AM |
9940 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
9941 | if (isec == NULL) | |
9942 | { | |
9943 | /* Don't attempt to output symbols with st_shnx in the | |
9944 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
9945 | *ppsection = NULL; | |
9946 | continue; | |
9947 | } | |
dbaa2011 | 9948 | else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE |
cb33740c AM |
9949 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) |
9950 | isym->st_value = | |
9951 | _bfd_merged_section_offset (output_bfd, &isec, | |
9952 | elf_section_data (isec)->sec_info, | |
9953 | isym->st_value); | |
c152c796 AM |
9954 | } |
9955 | ||
9956 | *ppsection = isec; | |
9957 | ||
d983c8c5 AM |
9958 | /* Don't output the first, undefined, symbol. In fact, don't |
9959 | output any undefined local symbol. */ | |
9960 | if (isec == bfd_und_section_ptr) | |
c152c796 AM |
9961 | continue; |
9962 | ||
9963 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
9964 | { | |
9965 | /* We never output section symbols. Instead, we use the | |
9966 | section symbol of the corresponding section in the output | |
9967 | file. */ | |
9968 | continue; | |
9969 | } | |
9970 | ||
9971 | /* If we are stripping all symbols, we don't want to output this | |
9972 | one. */ | |
8b127cbc | 9973 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
9974 | continue; |
9975 | ||
9976 | /* If we are discarding all local symbols, we don't want to | |
9977 | output this one. If we are generating a relocatable output | |
9978 | file, then some of the local symbols may be required by | |
9979 | relocs; we output them below as we discover that they are | |
9980 | needed. */ | |
8b127cbc | 9981 | if (flinfo->info->discard == discard_all) |
c152c796 AM |
9982 | continue; |
9983 | ||
9984 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
9985 | discarding, we don't need to keep it. */ |
9986 | if (isym->st_shndx != SHN_UNDEF | |
4fbb74a6 AM |
9987 | && isym->st_shndx < SHN_LORESERVE |
9988 | && bfd_section_removed_from_list (output_bfd, | |
9989 | isec->output_section)) | |
e75a280b L |
9990 | continue; |
9991 | ||
c152c796 AM |
9992 | /* Get the name of the symbol. */ |
9993 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
9994 | isym->st_name); | |
9995 | if (name == NULL) | |
9996 | return FALSE; | |
9997 | ||
9998 | /* See if we are discarding symbols with this name. */ | |
8b127cbc AM |
9999 | if ((flinfo->info->strip == strip_some |
10000 | && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) | |
c152c796 | 10001 | == NULL)) |
8b127cbc | 10002 | || (((flinfo->info->discard == discard_sec_merge |
0e1862bb L |
10003 | && (isec->flags & SEC_MERGE) |
10004 | && !bfd_link_relocatable (flinfo->info)) | |
8b127cbc | 10005 | || flinfo->info->discard == discard_l) |
c152c796 AM |
10006 | && bfd_is_local_label_name (input_bfd, name))) |
10007 | continue; | |
10008 | ||
ffbc01cc AM |
10009 | if (ELF_ST_TYPE (isym->st_info) == STT_FILE) |
10010 | { | |
ce875075 AM |
10011 | if (input_bfd->lto_output) |
10012 | /* -flto puts a temp file name here. This means builds | |
10013 | are not reproducible. Discard the symbol. */ | |
10014 | continue; | |
ffbc01cc AM |
10015 | have_file_sym = TRUE; |
10016 | flinfo->filesym_count += 1; | |
10017 | } | |
10018 | if (!have_file_sym) | |
10019 | { | |
10020 | /* In the absence of debug info, bfd_find_nearest_line uses | |
10021 | FILE symbols to determine the source file for local | |
10022 | function symbols. Provide a FILE symbol here if input | |
10023 | files lack such, so that their symbols won't be | |
10024 | associated with a previous input file. It's not the | |
10025 | source file, but the best we can do. */ | |
10026 | have_file_sym = TRUE; | |
10027 | flinfo->filesym_count += 1; | |
10028 | memset (&osym, 0, sizeof (osym)); | |
10029 | osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10030 | osym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10031 | if (!elf_link_output_symstrtab (flinfo, |
10032 | (input_bfd->lto_output ? NULL | |
10033 | : input_bfd->filename), | |
10034 | &osym, bfd_abs_section_ptr, | |
10035 | NULL)) | |
ffbc01cc AM |
10036 | return FALSE; |
10037 | } | |
10038 | ||
c152c796 AM |
10039 | osym = *isym; |
10040 | ||
10041 | /* Adjust the section index for the output file. */ | |
10042 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10043 | isec->output_section); | |
10044 | if (osym.st_shndx == SHN_BAD) | |
10045 | return FALSE; | |
10046 | ||
c152c796 AM |
10047 | /* ELF symbols in relocatable files are section relative, but |
10048 | in executable files they are virtual addresses. Note that | |
10049 | this code assumes that all ELF sections have an associated | |
10050 | BFD section with a reasonable value for output_offset; below | |
10051 | we assume that they also have a reasonable value for | |
10052 | output_section. Any special sections must be set up to meet | |
10053 | these requirements. */ | |
10054 | osym.st_value += isec->output_offset; | |
0e1862bb | 10055 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10056 | { |
10057 | osym.st_value += isec->output_section->vma; | |
10058 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
10059 | { | |
10060 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
8b127cbc AM |
10061 | BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL); |
10062 | osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; | |
c152c796 AM |
10063 | } |
10064 | } | |
10065 | ||
6e0b88f1 | 10066 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac | 10067 | ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); |
6e0b88f1 | 10068 | if (ret == 0) |
c152c796 | 10069 | return FALSE; |
6e0b88f1 AM |
10070 | else if (ret == 1) |
10071 | *pindex = indx; | |
c152c796 AM |
10072 | } |
10073 | ||
310fd250 L |
10074 | if (bed->s->arch_size == 32) |
10075 | { | |
10076 | r_type_mask = 0xff; | |
10077 | r_sym_shift = 8; | |
10078 | address_size = 4; | |
10079 | } | |
10080 | else | |
10081 | { | |
10082 | r_type_mask = 0xffffffff; | |
10083 | r_sym_shift = 32; | |
10084 | address_size = 8; | |
10085 | } | |
10086 | ||
c152c796 AM |
10087 | /* Relocate the contents of each section. */ |
10088 | sym_hashes = elf_sym_hashes (input_bfd); | |
10089 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
10090 | { | |
10091 | bfd_byte *contents; | |
10092 | ||
10093 | if (! o->linker_mark) | |
10094 | { | |
10095 | /* This section was omitted from the link. */ | |
10096 | continue; | |
10097 | } | |
10098 | ||
0e1862bb | 10099 | if (bfd_link_relocatable (flinfo->info) |
bcacc0f5 AM |
10100 | && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) |
10101 | { | |
10102 | /* Deal with the group signature symbol. */ | |
10103 | struct bfd_elf_section_data *sec_data = elf_section_data (o); | |
10104 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
10105 | asection *osec = o->output_section; | |
10106 | ||
10107 | if (symndx >= locsymcount | |
10108 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10109 | && flinfo->sections[symndx] == NULL)) |
bcacc0f5 AM |
10110 | { |
10111 | struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; | |
10112 | while (h->root.type == bfd_link_hash_indirect | |
10113 | || h->root.type == bfd_link_hash_warning) | |
10114 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10115 | /* Arrange for symbol to be output. */ | |
10116 | h->indx = -2; | |
10117 | elf_section_data (osec)->this_hdr.sh_info = -2; | |
10118 | } | |
10119 | else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) | |
10120 | { | |
10121 | /* We'll use the output section target_index. */ | |
8b127cbc | 10122 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 AM |
10123 | elf_section_data (osec)->this_hdr.sh_info = sec->target_index; |
10124 | } | |
10125 | else | |
10126 | { | |
8b127cbc | 10127 | if (flinfo->indices[symndx] == -1) |
bcacc0f5 AM |
10128 | { |
10129 | /* Otherwise output the local symbol now. */ | |
10130 | Elf_Internal_Sym sym = isymbuf[symndx]; | |
8b127cbc | 10131 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 | 10132 | const char *name; |
6e0b88f1 AM |
10133 | long indx; |
10134 | int ret; | |
bcacc0f5 AM |
10135 | |
10136 | name = bfd_elf_string_from_elf_section (input_bfd, | |
10137 | symtab_hdr->sh_link, | |
10138 | sym.st_name); | |
10139 | if (name == NULL) | |
10140 | return FALSE; | |
10141 | ||
10142 | sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10143 | sec); | |
10144 | if (sym.st_shndx == SHN_BAD) | |
10145 | return FALSE; | |
10146 | ||
10147 | sym.st_value += o->output_offset; | |
10148 | ||
6e0b88f1 | 10149 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10150 | ret = elf_link_output_symstrtab (flinfo, name, &sym, o, |
10151 | NULL); | |
6e0b88f1 | 10152 | if (ret == 0) |
bcacc0f5 | 10153 | return FALSE; |
6e0b88f1 | 10154 | else if (ret == 1) |
8b127cbc | 10155 | flinfo->indices[symndx] = indx; |
6e0b88f1 AM |
10156 | else |
10157 | abort (); | |
bcacc0f5 AM |
10158 | } |
10159 | elf_section_data (osec)->this_hdr.sh_info | |
8b127cbc | 10160 | = flinfo->indices[symndx]; |
bcacc0f5 AM |
10161 | } |
10162 | } | |
10163 | ||
c152c796 | 10164 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 10165 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
10166 | continue; |
10167 | ||
10168 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
10169 | { | |
10170 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
10171 | or somesuch. */ | |
10172 | continue; | |
10173 | } | |
10174 | ||
10175 | /* Get the contents of the section. They have been cached by a | |
10176 | relaxation routine. Note that o is a section in an input | |
10177 | file, so the contents field will not have been set by any of | |
10178 | the routines which work on output files. */ | |
10179 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
53291d1f AM |
10180 | { |
10181 | contents = elf_section_data (o)->this_hdr.contents; | |
10182 | if (bed->caches_rawsize | |
10183 | && o->rawsize != 0 | |
10184 | && o->rawsize < o->size) | |
10185 | { | |
10186 | memcpy (flinfo->contents, contents, o->rawsize); | |
10187 | contents = flinfo->contents; | |
10188 | } | |
10189 | } | |
c152c796 AM |
10190 | else |
10191 | { | |
8b127cbc | 10192 | contents = flinfo->contents; |
4a114e3e | 10193 | if (! bfd_get_full_section_contents (input_bfd, o, &contents)) |
c152c796 AM |
10194 | return FALSE; |
10195 | } | |
10196 | ||
10197 | if ((o->flags & SEC_RELOC) != 0) | |
10198 | { | |
10199 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 10200 | Elf_Internal_Rela *rel, *relend; |
0f02bbd9 | 10201 | int action_discarded; |
ece5ef60 | 10202 | int ret; |
c152c796 AM |
10203 | |
10204 | /* Get the swapped relocs. */ | |
10205 | internal_relocs | |
8b127cbc AM |
10206 | = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, |
10207 | flinfo->internal_relocs, FALSE); | |
c152c796 AM |
10208 | if (internal_relocs == NULL |
10209 | && o->reloc_count > 0) | |
10210 | return FALSE; | |
10211 | ||
310fd250 L |
10212 | /* We need to reverse-copy input .ctors/.dtors sections if |
10213 | they are placed in .init_array/.finit_array for output. */ | |
10214 | if (o->size > address_size | |
10215 | && ((strncmp (o->name, ".ctors", 6) == 0 | |
10216 | && strcmp (o->output_section->name, | |
10217 | ".init_array") == 0) | |
10218 | || (strncmp (o->name, ".dtors", 6) == 0 | |
10219 | && strcmp (o->output_section->name, | |
10220 | ".fini_array") == 0)) | |
10221 | && (o->name[6] == 0 || o->name[6] == '.')) | |
c152c796 | 10222 | { |
310fd250 L |
10223 | if (o->size != o->reloc_count * address_size) |
10224 | { | |
4eca0228 | 10225 | _bfd_error_handler |
695344c0 | 10226 | /* xgettext:c-format */ |
310fd250 L |
10227 | (_("error: %B: size of section %A is not " |
10228 | "multiple of address size"), | |
10229 | input_bfd, o); | |
10230 | bfd_set_error (bfd_error_on_input); | |
10231 | return FALSE; | |
10232 | } | |
10233 | o->flags |= SEC_ELF_REVERSE_COPY; | |
c152c796 AM |
10234 | } |
10235 | ||
0f02bbd9 | 10236 | action_discarded = -1; |
c152c796 | 10237 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
10238 | action_discarded = (*bed->action_discarded) (o); |
10239 | ||
10240 | /* Run through the relocs evaluating complex reloc symbols and | |
10241 | looking for relocs against symbols from discarded sections | |
10242 | or section symbols from removed link-once sections. | |
10243 | Complain about relocs against discarded sections. Zero | |
10244 | relocs against removed link-once sections. */ | |
10245 | ||
10246 | rel = internal_relocs; | |
10247 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
10248 | for ( ; rel < relend; rel++) | |
c152c796 | 10249 | { |
0f02bbd9 AM |
10250 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
10251 | unsigned int s_type; | |
10252 | asection **ps, *sec; | |
10253 | struct elf_link_hash_entry *h = NULL; | |
10254 | const char *sym_name; | |
c152c796 | 10255 | |
0f02bbd9 AM |
10256 | if (r_symndx == STN_UNDEF) |
10257 | continue; | |
c152c796 | 10258 | |
0f02bbd9 AM |
10259 | if (r_symndx >= locsymcount |
10260 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10261 | && flinfo->sections[r_symndx] == NULL)) |
0f02bbd9 AM |
10262 | { |
10263 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 10264 | |
0f02bbd9 AM |
10265 | /* Badly formatted input files can contain relocs that |
10266 | reference non-existant symbols. Check here so that | |
10267 | we do not seg fault. */ | |
10268 | if (h == NULL) | |
c152c796 | 10269 | { |
0f02bbd9 | 10270 | char buffer [32]; |
dce669a1 | 10271 | |
0f02bbd9 | 10272 | sprintf_vma (buffer, rel->r_info); |
4eca0228 | 10273 | _bfd_error_handler |
695344c0 | 10274 | /* xgettext:c-format */ |
0f02bbd9 AM |
10275 | (_("error: %B contains a reloc (0x%s) for section %A " |
10276 | "that references a non-existent global symbol"), | |
10277 | input_bfd, o, buffer); | |
10278 | bfd_set_error (bfd_error_bad_value); | |
10279 | return FALSE; | |
10280 | } | |
3b36f7e6 | 10281 | |
0f02bbd9 AM |
10282 | while (h->root.type == bfd_link_hash_indirect |
10283 | || h->root.type == bfd_link_hash_warning) | |
10284 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 10285 | |
0f02bbd9 | 10286 | s_type = h->type; |
cdd3575c | 10287 | |
9e2dec47 | 10288 | /* If a plugin symbol is referenced from a non-IR file, |
ca4be51c AM |
10289 | mark the symbol as undefined. Note that the |
10290 | linker may attach linker created dynamic sections | |
10291 | to the plugin bfd. Symbols defined in linker | |
10292 | created sections are not plugin symbols. */ | |
9e2dec47 L |
10293 | if (h->root.non_ir_ref |
10294 | && (h->root.type == bfd_link_hash_defined | |
10295 | || h->root.type == bfd_link_hash_defweak) | |
10296 | && (h->root.u.def.section->flags | |
10297 | & SEC_LINKER_CREATED) == 0 | |
10298 | && h->root.u.def.section->owner != NULL | |
10299 | && (h->root.u.def.section->owner->flags | |
10300 | & BFD_PLUGIN) != 0) | |
10301 | { | |
10302 | h->root.type = bfd_link_hash_undefined; | |
10303 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
10304 | } | |
10305 | ||
0f02bbd9 AM |
10306 | ps = NULL; |
10307 | if (h->root.type == bfd_link_hash_defined | |
10308 | || h->root.type == bfd_link_hash_defweak) | |
10309 | ps = &h->root.u.def.section; | |
10310 | ||
10311 | sym_name = h->root.root.string; | |
10312 | } | |
10313 | else | |
10314 | { | |
10315 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
10316 | ||
10317 | s_type = ELF_ST_TYPE (sym->st_info); | |
8b127cbc | 10318 | ps = &flinfo->sections[r_symndx]; |
0f02bbd9 AM |
10319 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, |
10320 | sym, *ps); | |
10321 | } | |
c152c796 | 10322 | |
c301e700 | 10323 | if ((s_type == STT_RELC || s_type == STT_SRELC) |
0e1862bb | 10324 | && !bfd_link_relocatable (flinfo->info)) |
0f02bbd9 AM |
10325 | { |
10326 | bfd_vma val; | |
10327 | bfd_vma dot = (rel->r_offset | |
10328 | + o->output_offset + o->output_section->vma); | |
10329 | #ifdef DEBUG | |
10330 | printf ("Encountered a complex symbol!"); | |
10331 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
9ccb8af9 AM |
10332 | input_bfd->filename, o->name, |
10333 | (long) (rel - internal_relocs)); | |
0f02bbd9 AM |
10334 | printf (" symbol: idx %8.8lx, name %s\n", |
10335 | r_symndx, sym_name); | |
10336 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
10337 | (unsigned long) rel->r_info, | |
10338 | (unsigned long) rel->r_offset); | |
10339 | #endif | |
8b127cbc | 10340 | if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, |
0f02bbd9 AM |
10341 | isymbuf, locsymcount, s_type == STT_SRELC)) |
10342 | return FALSE; | |
10343 | ||
10344 | /* Symbol evaluated OK. Update to absolute value. */ | |
10345 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
10346 | r_symndx, val); | |
10347 | continue; | |
10348 | } | |
10349 | ||
10350 | if (action_discarded != -1 && ps != NULL) | |
10351 | { | |
cdd3575c AM |
10352 | /* Complain if the definition comes from a |
10353 | discarded section. */ | |
dbaa2011 | 10354 | if ((sec = *ps) != NULL && discarded_section (sec)) |
cdd3575c | 10355 | { |
cf35638d | 10356 | BFD_ASSERT (r_symndx != STN_UNDEF); |
0f02bbd9 | 10357 | if (action_discarded & COMPLAIN) |
8b127cbc | 10358 | (*flinfo->info->callbacks->einfo) |
695344c0 | 10359 | /* xgettext:c-format */ |
e1fffbe6 | 10360 | (_("%X`%s' referenced in section `%A' of %B: " |
58ac56d0 | 10361 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 10362 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 10363 | |
87e5235d | 10364 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 10365 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
10366 | really defined in the kept linkonce section. |
10367 | FIXME: This is quite broken. Modifying the | |
10368 | symbol here means we will be changing all later | |
e0ae6d6f | 10369 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 10370 | if (action_discarded & PRETEND) |
87e5235d | 10371 | { |
01b3c8ab L |
10372 | asection *kept; |
10373 | ||
c0f00686 | 10374 | kept = _bfd_elf_check_kept_section (sec, |
8b127cbc | 10375 | flinfo->info); |
01b3c8ab | 10376 | if (kept != NULL) |
87e5235d AM |
10377 | { |
10378 | *ps = kept; | |
10379 | continue; | |
10380 | } | |
10381 | } | |
c152c796 AM |
10382 | } |
10383 | } | |
10384 | } | |
10385 | ||
10386 | /* Relocate the section by invoking a back end routine. | |
10387 | ||
10388 | The back end routine is responsible for adjusting the | |
10389 | section contents as necessary, and (if using Rela relocs | |
10390 | and generating a relocatable output file) adjusting the | |
10391 | reloc addend as necessary. | |
10392 | ||
10393 | The back end routine does not have to worry about setting | |
10394 | the reloc address or the reloc symbol index. | |
10395 | ||
10396 | The back end routine is given a pointer to the swapped in | |
10397 | internal symbols, and can access the hash table entries | |
10398 | for the external symbols via elf_sym_hashes (input_bfd). | |
10399 | ||
10400 | When generating relocatable output, the back end routine | |
10401 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
10402 | output symbol is going to be a section symbol | |
10403 | corresponding to the output section, which will require | |
10404 | the addend to be adjusted. */ | |
10405 | ||
8b127cbc | 10406 | ret = (*relocate_section) (output_bfd, flinfo->info, |
c152c796 AM |
10407 | input_bfd, o, contents, |
10408 | internal_relocs, | |
10409 | isymbuf, | |
8b127cbc | 10410 | flinfo->sections); |
ece5ef60 | 10411 | if (!ret) |
c152c796 AM |
10412 | return FALSE; |
10413 | ||
ece5ef60 | 10414 | if (ret == 2 |
0e1862bb | 10415 | || bfd_link_relocatable (flinfo->info) |
8b127cbc | 10416 | || flinfo->info->emitrelocations) |
c152c796 AM |
10417 | { |
10418 | Elf_Internal_Rela *irela; | |
d4730f92 | 10419 | Elf_Internal_Rela *irelaend, *irelamid; |
c152c796 AM |
10420 | bfd_vma last_offset; |
10421 | struct elf_link_hash_entry **rel_hash; | |
d4730f92 BS |
10422 | struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; |
10423 | Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; | |
c152c796 | 10424 | unsigned int next_erel; |
c152c796 | 10425 | bfd_boolean rela_normal; |
d4730f92 | 10426 | struct bfd_elf_section_data *esdi, *esdo; |
c152c796 | 10427 | |
d4730f92 BS |
10428 | esdi = elf_section_data (o); |
10429 | esdo = elf_section_data (o->output_section); | |
10430 | rela_normal = FALSE; | |
c152c796 AM |
10431 | |
10432 | /* Adjust the reloc addresses and symbol indices. */ | |
10433 | ||
10434 | irela = internal_relocs; | |
10435 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
d4730f92 BS |
10436 | rel_hash = esdo->rel.hashes + esdo->rel.count; |
10437 | /* We start processing the REL relocs, if any. When we reach | |
10438 | IRELAMID in the loop, we switch to the RELA relocs. */ | |
10439 | irelamid = irela; | |
10440 | if (esdi->rel.hdr != NULL) | |
10441 | irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) | |
10442 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10443 | rel_hash_list = rel_hash; |
d4730f92 | 10444 | rela_hash_list = NULL; |
c152c796 | 10445 | last_offset = o->output_offset; |
0e1862bb | 10446 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10447 | last_offset += o->output_section->vma; |
10448 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
10449 | { | |
10450 | unsigned long r_symndx; | |
10451 | asection *sec; | |
10452 | Elf_Internal_Sym sym; | |
10453 | ||
10454 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
10455 | { | |
10456 | rel_hash++; | |
10457 | next_erel = 0; | |
10458 | } | |
10459 | ||
d4730f92 BS |
10460 | if (irela == irelamid) |
10461 | { | |
10462 | rel_hash = esdo->rela.hashes + esdo->rela.count; | |
10463 | rela_hash_list = rel_hash; | |
10464 | rela_normal = bed->rela_normal; | |
10465 | } | |
10466 | ||
c152c796 | 10467 | irela->r_offset = _bfd_elf_section_offset (output_bfd, |
8b127cbc | 10468 | flinfo->info, o, |
c152c796 AM |
10469 | irela->r_offset); |
10470 | if (irela->r_offset >= (bfd_vma) -2) | |
10471 | { | |
10472 | /* This is a reloc for a deleted entry or somesuch. | |
10473 | Turn it into an R_*_NONE reloc, at the same | |
10474 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 10475 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
10476 | being ordered. */ |
10477 | irela->r_offset = last_offset; | |
10478 | irela->r_info = 0; | |
10479 | irela->r_addend = 0; | |
10480 | continue; | |
10481 | } | |
10482 | ||
10483 | irela->r_offset += o->output_offset; | |
10484 | ||
10485 | /* Relocs in an executable have to be virtual addresses. */ | |
0e1862bb | 10486 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10487 | irela->r_offset += o->output_section->vma; |
10488 | ||
10489 | last_offset = irela->r_offset; | |
10490 | ||
10491 | r_symndx = irela->r_info >> r_sym_shift; | |
10492 | if (r_symndx == STN_UNDEF) | |
10493 | continue; | |
10494 | ||
10495 | if (r_symndx >= locsymcount | |
10496 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10497 | && flinfo->sections[r_symndx] == NULL)) |
c152c796 AM |
10498 | { |
10499 | struct elf_link_hash_entry *rh; | |
10500 | unsigned long indx; | |
10501 | ||
10502 | /* This is a reloc against a global symbol. We | |
10503 | have not yet output all the local symbols, so | |
10504 | we do not know the symbol index of any global | |
10505 | symbol. We set the rel_hash entry for this | |
10506 | reloc to point to the global hash table entry | |
10507 | for this symbol. The symbol index is then | |
ee75fd95 | 10508 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
10509 | indx = r_symndx - extsymoff; |
10510 | rh = elf_sym_hashes (input_bfd)[indx]; | |
10511 | while (rh->root.type == bfd_link_hash_indirect | |
10512 | || rh->root.type == bfd_link_hash_warning) | |
10513 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
10514 | ||
10515 | /* Setting the index to -2 tells | |
10516 | elf_link_output_extsym that this symbol is | |
10517 | used by a reloc. */ | |
10518 | BFD_ASSERT (rh->indx < 0); | |
10519 | rh->indx = -2; | |
10520 | ||
10521 | *rel_hash = rh; | |
10522 | ||
10523 | continue; | |
10524 | } | |
10525 | ||
10526 | /* This is a reloc against a local symbol. */ | |
10527 | ||
10528 | *rel_hash = NULL; | |
10529 | sym = isymbuf[r_symndx]; | |
8b127cbc | 10530 | sec = flinfo->sections[r_symndx]; |
c152c796 AM |
10531 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) |
10532 | { | |
10533 | /* I suppose the backend ought to fill in the | |
10534 | section of any STT_SECTION symbol against a | |
6a8d1586 | 10535 | processor specific section. */ |
cf35638d | 10536 | r_symndx = STN_UNDEF; |
6a8d1586 AM |
10537 | if (bfd_is_abs_section (sec)) |
10538 | ; | |
c152c796 AM |
10539 | else if (sec == NULL || sec->owner == NULL) |
10540 | { | |
10541 | bfd_set_error (bfd_error_bad_value); | |
10542 | return FALSE; | |
10543 | } | |
10544 | else | |
10545 | { | |
6a8d1586 AM |
10546 | asection *osec = sec->output_section; |
10547 | ||
10548 | /* If we have discarded a section, the output | |
10549 | section will be the absolute section. In | |
ab96bf03 AM |
10550 | case of discarded SEC_MERGE sections, use |
10551 | the kept section. relocate_section should | |
10552 | have already handled discarded linkonce | |
10553 | sections. */ | |
6a8d1586 AM |
10554 | if (bfd_is_abs_section (osec) |
10555 | && sec->kept_section != NULL | |
10556 | && sec->kept_section->output_section != NULL) | |
10557 | { | |
10558 | osec = sec->kept_section->output_section; | |
10559 | irela->r_addend -= osec->vma; | |
10560 | } | |
10561 | ||
10562 | if (!bfd_is_abs_section (osec)) | |
10563 | { | |
10564 | r_symndx = osec->target_index; | |
cf35638d | 10565 | if (r_symndx == STN_UNDEF) |
74541ad4 | 10566 | { |
051d833a AM |
10567 | irela->r_addend += osec->vma; |
10568 | osec = _bfd_nearby_section (output_bfd, osec, | |
10569 | osec->vma); | |
10570 | irela->r_addend -= osec->vma; | |
10571 | r_symndx = osec->target_index; | |
74541ad4 | 10572 | } |
6a8d1586 | 10573 | } |
c152c796 AM |
10574 | } |
10575 | ||
10576 | /* Adjust the addend according to where the | |
10577 | section winds up in the output section. */ | |
10578 | if (rela_normal) | |
10579 | irela->r_addend += sec->output_offset; | |
10580 | } | |
10581 | else | |
10582 | { | |
8b127cbc | 10583 | if (flinfo->indices[r_symndx] == -1) |
c152c796 AM |
10584 | { |
10585 | unsigned long shlink; | |
10586 | const char *name; | |
10587 | asection *osec; | |
6e0b88f1 | 10588 | long indx; |
c152c796 | 10589 | |
8b127cbc | 10590 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
10591 | { |
10592 | /* You can't do ld -r -s. */ | |
10593 | bfd_set_error (bfd_error_invalid_operation); | |
10594 | return FALSE; | |
10595 | } | |
10596 | ||
10597 | /* This symbol was skipped earlier, but | |
10598 | since it is needed by a reloc, we | |
10599 | must output it now. */ | |
10600 | shlink = symtab_hdr->sh_link; | |
10601 | name = (bfd_elf_string_from_elf_section | |
10602 | (input_bfd, shlink, sym.st_name)); | |
10603 | if (name == NULL) | |
10604 | return FALSE; | |
10605 | ||
10606 | osec = sec->output_section; | |
10607 | sym.st_shndx = | |
10608 | _bfd_elf_section_from_bfd_section (output_bfd, | |
10609 | osec); | |
10610 | if (sym.st_shndx == SHN_BAD) | |
10611 | return FALSE; | |
10612 | ||
10613 | sym.st_value += sec->output_offset; | |
0e1862bb | 10614 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10615 | { |
10616 | sym.st_value += osec->vma; | |
10617 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
10618 | { | |
10619 | /* STT_TLS symbols are relative to PT_TLS | |
10620 | segment base. */ | |
8b127cbc | 10621 | BFD_ASSERT (elf_hash_table (flinfo->info) |
c152c796 | 10622 | ->tls_sec != NULL); |
8b127cbc | 10623 | sym.st_value -= (elf_hash_table (flinfo->info) |
c152c796 AM |
10624 | ->tls_sec->vma); |
10625 | } | |
10626 | } | |
10627 | ||
6e0b88f1 | 10628 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10629 | ret = elf_link_output_symstrtab (flinfo, name, |
10630 | &sym, sec, | |
10631 | NULL); | |
6e0b88f1 | 10632 | if (ret == 0) |
c152c796 | 10633 | return FALSE; |
6e0b88f1 | 10634 | else if (ret == 1) |
8b127cbc | 10635 | flinfo->indices[r_symndx] = indx; |
6e0b88f1 AM |
10636 | else |
10637 | abort (); | |
c152c796 AM |
10638 | } |
10639 | ||
8b127cbc | 10640 | r_symndx = flinfo->indices[r_symndx]; |
c152c796 AM |
10641 | } |
10642 | ||
10643 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
10644 | | (irela->r_info & r_type_mask)); | |
10645 | } | |
10646 | ||
10647 | /* Swap out the relocs. */ | |
d4730f92 BS |
10648 | input_rel_hdr = esdi->rel.hdr; |
10649 | if (input_rel_hdr && input_rel_hdr->sh_size != 0) | |
c152c796 | 10650 | { |
d4730f92 BS |
10651 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
10652 | input_rel_hdr, | |
10653 | internal_relocs, | |
10654 | rel_hash_list)) | |
10655 | return FALSE; | |
c152c796 AM |
10656 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) |
10657 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10658 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
d4730f92 BS |
10659 | } |
10660 | ||
10661 | input_rela_hdr = esdi->rela.hdr; | |
10662 | if (input_rela_hdr && input_rela_hdr->sh_size != 0) | |
10663 | { | |
eac338cf | 10664 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
d4730f92 | 10665 | input_rela_hdr, |
eac338cf | 10666 | internal_relocs, |
d4730f92 | 10667 | rela_hash_list)) |
c152c796 AM |
10668 | return FALSE; |
10669 | } | |
10670 | } | |
10671 | } | |
10672 | ||
10673 | /* Write out the modified section contents. */ | |
10674 | if (bed->elf_backend_write_section | |
8b127cbc | 10675 | && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, |
c7b8f16e | 10676 | contents)) |
c152c796 AM |
10677 | { |
10678 | /* Section written out. */ | |
10679 | } | |
10680 | else switch (o->sec_info_type) | |
10681 | { | |
dbaa2011 | 10682 | case SEC_INFO_TYPE_STABS: |
c152c796 AM |
10683 | if (! (_bfd_write_section_stabs |
10684 | (output_bfd, | |
8b127cbc | 10685 | &elf_hash_table (flinfo->info)->stab_info, |
c152c796 AM |
10686 | o, &elf_section_data (o)->sec_info, contents))) |
10687 | return FALSE; | |
10688 | break; | |
dbaa2011 | 10689 | case SEC_INFO_TYPE_MERGE: |
c152c796 AM |
10690 | if (! _bfd_write_merged_section (output_bfd, o, |
10691 | elf_section_data (o)->sec_info)) | |
10692 | return FALSE; | |
10693 | break; | |
dbaa2011 | 10694 | case SEC_INFO_TYPE_EH_FRAME: |
c152c796 | 10695 | { |
8b127cbc | 10696 | if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, |
c152c796 AM |
10697 | o, contents)) |
10698 | return FALSE; | |
10699 | } | |
10700 | break; | |
2f0c68f2 CM |
10701 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
10702 | { | |
10703 | if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, | |
10704 | flinfo->info, | |
10705 | o, contents)) | |
10706 | return FALSE; | |
10707 | } | |
10708 | break; | |
c152c796 AM |
10709 | default: |
10710 | { | |
310fd250 L |
10711 | if (! (o->flags & SEC_EXCLUDE)) |
10712 | { | |
10713 | file_ptr offset = (file_ptr) o->output_offset; | |
10714 | bfd_size_type todo = o->size; | |
37b01f6a DG |
10715 | |
10716 | offset *= bfd_octets_per_byte (output_bfd); | |
10717 | ||
310fd250 L |
10718 | if ((o->flags & SEC_ELF_REVERSE_COPY)) |
10719 | { | |
10720 | /* Reverse-copy input section to output. */ | |
10721 | do | |
10722 | { | |
10723 | todo -= address_size; | |
10724 | if (! bfd_set_section_contents (output_bfd, | |
10725 | o->output_section, | |
10726 | contents + todo, | |
10727 | offset, | |
10728 | address_size)) | |
10729 | return FALSE; | |
10730 | if (todo == 0) | |
10731 | break; | |
10732 | offset += address_size; | |
10733 | } | |
10734 | while (1); | |
10735 | } | |
10736 | else if (! bfd_set_section_contents (output_bfd, | |
10737 | o->output_section, | |
10738 | contents, | |
10739 | offset, todo)) | |
10740 | return FALSE; | |
10741 | } | |
c152c796 AM |
10742 | } |
10743 | break; | |
10744 | } | |
10745 | } | |
10746 | ||
10747 | return TRUE; | |
10748 | } | |
10749 | ||
10750 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 10751 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
10752 | is used to build constructor and destructor tables when linking |
10753 | with -Ur. */ | |
10754 | ||
10755 | static bfd_boolean | |
10756 | elf_reloc_link_order (bfd *output_bfd, | |
10757 | struct bfd_link_info *info, | |
10758 | asection *output_section, | |
10759 | struct bfd_link_order *link_order) | |
10760 | { | |
10761 | reloc_howto_type *howto; | |
10762 | long indx; | |
10763 | bfd_vma offset; | |
10764 | bfd_vma addend; | |
d4730f92 | 10765 | struct bfd_elf_section_reloc_data *reldata; |
c152c796 AM |
10766 | struct elf_link_hash_entry **rel_hash_ptr; |
10767 | Elf_Internal_Shdr *rel_hdr; | |
10768 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
10769 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
10770 | bfd_byte *erel; | |
10771 | unsigned int i; | |
d4730f92 | 10772 | struct bfd_elf_section_data *esdo = elf_section_data (output_section); |
c152c796 AM |
10773 | |
10774 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
10775 | if (howto == NULL) | |
10776 | { | |
10777 | bfd_set_error (bfd_error_bad_value); | |
10778 | return FALSE; | |
10779 | } | |
10780 | ||
10781 | addend = link_order->u.reloc.p->addend; | |
10782 | ||
d4730f92 BS |
10783 | if (esdo->rel.hdr) |
10784 | reldata = &esdo->rel; | |
10785 | else if (esdo->rela.hdr) | |
10786 | reldata = &esdo->rela; | |
10787 | else | |
10788 | { | |
10789 | reldata = NULL; | |
10790 | BFD_ASSERT (0); | |
10791 | } | |
10792 | ||
c152c796 | 10793 | /* Figure out the symbol index. */ |
d4730f92 | 10794 | rel_hash_ptr = reldata->hashes + reldata->count; |
c152c796 AM |
10795 | if (link_order->type == bfd_section_reloc_link_order) |
10796 | { | |
10797 | indx = link_order->u.reloc.p->u.section->target_index; | |
10798 | BFD_ASSERT (indx != 0); | |
10799 | *rel_hash_ptr = NULL; | |
10800 | } | |
10801 | else | |
10802 | { | |
10803 | struct elf_link_hash_entry *h; | |
10804 | ||
10805 | /* Treat a reloc against a defined symbol as though it were | |
10806 | actually against the section. */ | |
10807 | h = ((struct elf_link_hash_entry *) | |
10808 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
10809 | link_order->u.reloc.p->u.name, | |
10810 | FALSE, FALSE, TRUE)); | |
10811 | if (h != NULL | |
10812 | && (h->root.type == bfd_link_hash_defined | |
10813 | || h->root.type == bfd_link_hash_defweak)) | |
10814 | { | |
10815 | asection *section; | |
10816 | ||
10817 | section = h->root.u.def.section; | |
10818 | indx = section->output_section->target_index; | |
10819 | *rel_hash_ptr = NULL; | |
10820 | /* It seems that we ought to add the symbol value to the | |
10821 | addend here, but in practice it has already been added | |
10822 | because it was passed to constructor_callback. */ | |
10823 | addend += section->output_section->vma + section->output_offset; | |
10824 | } | |
10825 | else if (h != NULL) | |
10826 | { | |
10827 | /* Setting the index to -2 tells elf_link_output_extsym that | |
10828 | this symbol is used by a reloc. */ | |
10829 | h->indx = -2; | |
10830 | *rel_hash_ptr = h; | |
10831 | indx = 0; | |
10832 | } | |
10833 | else | |
10834 | { | |
1a72702b AM |
10835 | (*info->callbacks->unattached_reloc) |
10836 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); | |
c152c796 AM |
10837 | indx = 0; |
10838 | } | |
10839 | } | |
10840 | ||
10841 | /* If this is an inplace reloc, we must write the addend into the | |
10842 | object file. */ | |
10843 | if (howto->partial_inplace && addend != 0) | |
10844 | { | |
10845 | bfd_size_type size; | |
10846 | bfd_reloc_status_type rstat; | |
10847 | bfd_byte *buf; | |
10848 | bfd_boolean ok; | |
10849 | const char *sym_name; | |
10850 | ||
a50b1753 NC |
10851 | size = (bfd_size_type) bfd_get_reloc_size (howto); |
10852 | buf = (bfd_byte *) bfd_zmalloc (size); | |
6346d5ca | 10853 | if (buf == NULL && size != 0) |
c152c796 AM |
10854 | return FALSE; |
10855 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
10856 | switch (rstat) | |
10857 | { | |
10858 | case bfd_reloc_ok: | |
10859 | break; | |
10860 | ||
10861 | default: | |
10862 | case bfd_reloc_outofrange: | |
10863 | abort (); | |
10864 | ||
10865 | case bfd_reloc_overflow: | |
10866 | if (link_order->type == bfd_section_reloc_link_order) | |
10867 | sym_name = bfd_section_name (output_bfd, | |
10868 | link_order->u.reloc.p->u.section); | |
10869 | else | |
10870 | sym_name = link_order->u.reloc.p->u.name; | |
1a72702b AM |
10871 | (*info->callbacks->reloc_overflow) (info, NULL, sym_name, |
10872 | howto->name, addend, NULL, NULL, | |
10873 | (bfd_vma) 0); | |
c152c796 AM |
10874 | break; |
10875 | } | |
37b01f6a | 10876 | |
c152c796 | 10877 | ok = bfd_set_section_contents (output_bfd, output_section, buf, |
37b01f6a DG |
10878 | link_order->offset |
10879 | * bfd_octets_per_byte (output_bfd), | |
10880 | size); | |
c152c796 AM |
10881 | free (buf); |
10882 | if (! ok) | |
10883 | return FALSE; | |
10884 | } | |
10885 | ||
10886 | /* The address of a reloc is relative to the section in a | |
10887 | relocatable file, and is a virtual address in an executable | |
10888 | file. */ | |
10889 | offset = link_order->offset; | |
0e1862bb | 10890 | if (! bfd_link_relocatable (info)) |
c152c796 AM |
10891 | offset += output_section->vma; |
10892 | ||
10893 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
10894 | { | |
10895 | irel[i].r_offset = offset; | |
10896 | irel[i].r_info = 0; | |
10897 | irel[i].r_addend = 0; | |
10898 | } | |
10899 | if (bed->s->arch_size == 32) | |
10900 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
10901 | else | |
10902 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
10903 | ||
d4730f92 | 10904 | rel_hdr = reldata->hdr; |
c152c796 AM |
10905 | erel = rel_hdr->contents; |
10906 | if (rel_hdr->sh_type == SHT_REL) | |
10907 | { | |
d4730f92 | 10908 | erel += reldata->count * bed->s->sizeof_rel; |
c152c796 AM |
10909 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); |
10910 | } | |
10911 | else | |
10912 | { | |
10913 | irel[0].r_addend = addend; | |
d4730f92 | 10914 | erel += reldata->count * bed->s->sizeof_rela; |
c152c796 AM |
10915 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); |
10916 | } | |
10917 | ||
d4730f92 | 10918 | ++reldata->count; |
c152c796 AM |
10919 | |
10920 | return TRUE; | |
10921 | } | |
10922 | ||
0b52efa6 PB |
10923 | |
10924 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
10925 | ||
10926 | static bfd_vma | |
10927 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
10928 | { | |
10929 | Elf_Internal_Shdr **elf_shdrp; | |
10930 | asection *s; | |
10931 | int elfsec; | |
10932 | ||
10933 | s = p->u.indirect.section; | |
10934 | elf_shdrp = elf_elfsections (s->owner); | |
10935 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
10936 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
10937 | /* PR 290: |
10938 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 10939 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
10940 | sh_info fields. Hence we could get the situation |
10941 | where elfsec is 0. */ | |
10942 | if (elfsec == 0) | |
10943 | { | |
10944 | const struct elf_backend_data *bed | |
10945 | = get_elf_backend_data (s->owner); | |
10946 | if (bed->link_order_error_handler) | |
d003868e | 10947 | bed->link_order_error_handler |
695344c0 | 10948 | /* xgettext:c-format */ |
d003868e | 10949 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); |
185d09ad L |
10950 | return 0; |
10951 | } | |
10952 | else | |
10953 | { | |
10954 | s = elf_shdrp[elfsec]->bfd_section; | |
10955 | return s->output_section->vma + s->output_offset; | |
10956 | } | |
0b52efa6 PB |
10957 | } |
10958 | ||
10959 | ||
10960 | /* Compare two sections based on the locations of the sections they are | |
10961 | linked to. Used by elf_fixup_link_order. */ | |
10962 | ||
10963 | static int | |
10964 | compare_link_order (const void * a, const void * b) | |
10965 | { | |
10966 | bfd_vma apos; | |
10967 | bfd_vma bpos; | |
10968 | ||
10969 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
10970 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
10971 | if (apos < bpos) | |
10972 | return -1; | |
10973 | return apos > bpos; | |
10974 | } | |
10975 | ||
10976 | ||
10977 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
10978 | order as their linked sections. Returns false if this could not be done | |
10979 | because an output section includes both ordered and unordered | |
10980 | sections. Ideally we'd do this in the linker proper. */ | |
10981 | ||
10982 | static bfd_boolean | |
10983 | elf_fixup_link_order (bfd *abfd, asection *o) | |
10984 | { | |
10985 | int seen_linkorder; | |
10986 | int seen_other; | |
10987 | int n; | |
10988 | struct bfd_link_order *p; | |
10989 | bfd *sub; | |
10990 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 10991 | unsigned elfsec; |
0b52efa6 | 10992 | struct bfd_link_order **sections; |
d33cdfe3 | 10993 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 10994 | bfd_vma offset; |
3b36f7e6 | 10995 | |
d33cdfe3 L |
10996 | other_sec = NULL; |
10997 | linkorder_sec = NULL; | |
0b52efa6 PB |
10998 | seen_other = 0; |
10999 | seen_linkorder = 0; | |
8423293d | 11000 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 11001 | { |
d33cdfe3 | 11002 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
11003 | { |
11004 | s = p->u.indirect.section; | |
d33cdfe3 L |
11005 | sub = s->owner; |
11006 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
11007 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
11008 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
11009 | && elfsec < elf_numsections (sub) | |
4fbb74a6 AM |
11010 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER |
11011 | && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) | |
d33cdfe3 L |
11012 | { |
11013 | seen_linkorder++; | |
11014 | linkorder_sec = s; | |
11015 | } | |
0b52efa6 | 11016 | else |
d33cdfe3 L |
11017 | { |
11018 | seen_other++; | |
11019 | other_sec = s; | |
11020 | } | |
0b52efa6 PB |
11021 | } |
11022 | else | |
11023 | seen_other++; | |
d33cdfe3 L |
11024 | |
11025 | if (seen_other && seen_linkorder) | |
11026 | { | |
11027 | if (other_sec && linkorder_sec) | |
4eca0228 | 11028 | _bfd_error_handler |
695344c0 | 11029 | /* xgettext:c-format */ |
4eca0228 AM |
11030 | (_("%A has both ordered [`%A' in %B] " |
11031 | "and unordered [`%A' in %B] sections"), | |
11032 | o, linkorder_sec, | |
11033 | linkorder_sec->owner, other_sec, | |
11034 | other_sec->owner); | |
d33cdfe3 | 11035 | else |
4eca0228 AM |
11036 | _bfd_error_handler |
11037 | (_("%A has both ordered and unordered sections"), o); | |
d33cdfe3 L |
11038 | bfd_set_error (bfd_error_bad_value); |
11039 | return FALSE; | |
11040 | } | |
0b52efa6 PB |
11041 | } |
11042 | ||
11043 | if (!seen_linkorder) | |
11044 | return TRUE; | |
11045 | ||
0b52efa6 | 11046 | sections = (struct bfd_link_order **) |
14b1c01e AM |
11047 | bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); |
11048 | if (sections == NULL) | |
11049 | return FALSE; | |
0b52efa6 | 11050 | seen_linkorder = 0; |
3b36f7e6 | 11051 | |
8423293d | 11052 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
11053 | { |
11054 | sections[seen_linkorder++] = p; | |
11055 | } | |
11056 | /* Sort the input sections in the order of their linked section. */ | |
11057 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
11058 | compare_link_order); | |
11059 | ||
11060 | /* Change the offsets of the sections. */ | |
11061 | offset = 0; | |
11062 | for (n = 0; n < seen_linkorder; n++) | |
11063 | { | |
11064 | s = sections[n]->u.indirect.section; | |
461686a3 | 11065 | offset &= ~(bfd_vma) 0 << s->alignment_power; |
37b01f6a | 11066 | s->output_offset = offset / bfd_octets_per_byte (abfd); |
0b52efa6 PB |
11067 | sections[n]->offset = offset; |
11068 | offset += sections[n]->size; | |
11069 | } | |
11070 | ||
4dd07732 | 11071 | free (sections); |
0b52efa6 PB |
11072 | return TRUE; |
11073 | } | |
11074 | ||
76359541 TP |
11075 | /* Generate an import library in INFO->implib_bfd from symbols in ABFD. |
11076 | Returns TRUE upon success, FALSE otherwise. */ | |
11077 | ||
11078 | static bfd_boolean | |
11079 | elf_output_implib (bfd *abfd, struct bfd_link_info *info) | |
11080 | { | |
11081 | bfd_boolean ret = FALSE; | |
11082 | bfd *implib_bfd; | |
11083 | const struct elf_backend_data *bed; | |
11084 | flagword flags; | |
11085 | enum bfd_architecture arch; | |
11086 | unsigned int mach; | |
11087 | asymbol **sympp = NULL; | |
11088 | long symsize; | |
11089 | long symcount; | |
11090 | long src_count; | |
11091 | elf_symbol_type *osymbuf; | |
11092 | ||
11093 | implib_bfd = info->out_implib_bfd; | |
11094 | bed = get_elf_backend_data (abfd); | |
11095 | ||
11096 | if (!bfd_set_format (implib_bfd, bfd_object)) | |
11097 | return FALSE; | |
11098 | ||
11099 | flags = bfd_get_file_flags (abfd); | |
11100 | flags &= ~HAS_RELOC; | |
11101 | if (!bfd_set_start_address (implib_bfd, 0) | |
11102 | || !bfd_set_file_flags (implib_bfd, flags)) | |
11103 | return FALSE; | |
11104 | ||
11105 | /* Copy architecture of output file to import library file. */ | |
11106 | arch = bfd_get_arch (abfd); | |
11107 | mach = bfd_get_mach (abfd); | |
11108 | if (!bfd_set_arch_mach (implib_bfd, arch, mach) | |
11109 | && (abfd->target_defaulted | |
11110 | || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) | |
11111 | return FALSE; | |
11112 | ||
11113 | /* Get symbol table size. */ | |
11114 | symsize = bfd_get_symtab_upper_bound (abfd); | |
11115 | if (symsize < 0) | |
11116 | return FALSE; | |
11117 | ||
11118 | /* Read in the symbol table. */ | |
11119 | sympp = (asymbol **) xmalloc (symsize); | |
11120 | symcount = bfd_canonicalize_symtab (abfd, sympp); | |
11121 | if (symcount < 0) | |
11122 | goto free_sym_buf; | |
11123 | ||
11124 | /* Allow the BFD backend to copy any private header data it | |
11125 | understands from the output BFD to the import library BFD. */ | |
11126 | if (! bfd_copy_private_header_data (abfd, implib_bfd)) | |
11127 | goto free_sym_buf; | |
11128 | ||
11129 | /* Filter symbols to appear in the import library. */ | |
11130 | if (bed->elf_backend_filter_implib_symbols) | |
11131 | symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, | |
11132 | symcount); | |
11133 | else | |
11134 | symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); | |
11135 | if (symcount == 0) | |
11136 | { | |
5df1bc57 | 11137 | bfd_set_error (bfd_error_no_symbols); |
4eca0228 AM |
11138 | _bfd_error_handler (_("%B: no symbol found for import library"), |
11139 | implib_bfd); | |
76359541 TP |
11140 | goto free_sym_buf; |
11141 | } | |
11142 | ||
11143 | ||
11144 | /* Make symbols absolute. */ | |
11145 | osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount, | |
11146 | sizeof (*osymbuf)); | |
11147 | for (src_count = 0; src_count < symcount; src_count++) | |
11148 | { | |
11149 | memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], | |
11150 | sizeof (*osymbuf)); | |
11151 | osymbuf[src_count].symbol.section = bfd_abs_section_ptr; | |
11152 | osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; | |
11153 | osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; | |
11154 | osymbuf[src_count].internal_elf_sym.st_value = | |
11155 | osymbuf[src_count].symbol.value; | |
11156 | sympp[src_count] = &osymbuf[src_count].symbol; | |
11157 | } | |
11158 | ||
11159 | bfd_set_symtab (implib_bfd, sympp, symcount); | |
11160 | ||
11161 | /* Allow the BFD backend to copy any private data it understands | |
11162 | from the output BFD to the import library BFD. This is done last | |
11163 | to permit the routine to look at the filtered symbol table. */ | |
11164 | if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) | |
11165 | goto free_sym_buf; | |
11166 | ||
11167 | if (!bfd_close (implib_bfd)) | |
11168 | goto free_sym_buf; | |
11169 | ||
11170 | ret = TRUE; | |
11171 | ||
11172 | free_sym_buf: | |
11173 | free (sympp); | |
11174 | return ret; | |
11175 | } | |
11176 | ||
9f7c3e5e AM |
11177 | static void |
11178 | elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) | |
11179 | { | |
11180 | asection *o; | |
11181 | ||
11182 | if (flinfo->symstrtab != NULL) | |
ef10c3ac | 11183 | _bfd_elf_strtab_free (flinfo->symstrtab); |
9f7c3e5e AM |
11184 | if (flinfo->contents != NULL) |
11185 | free (flinfo->contents); | |
11186 | if (flinfo->external_relocs != NULL) | |
11187 | free (flinfo->external_relocs); | |
11188 | if (flinfo->internal_relocs != NULL) | |
11189 | free (flinfo->internal_relocs); | |
11190 | if (flinfo->external_syms != NULL) | |
11191 | free (flinfo->external_syms); | |
11192 | if (flinfo->locsym_shndx != NULL) | |
11193 | free (flinfo->locsym_shndx); | |
11194 | if (flinfo->internal_syms != NULL) | |
11195 | free (flinfo->internal_syms); | |
11196 | if (flinfo->indices != NULL) | |
11197 | free (flinfo->indices); | |
11198 | if (flinfo->sections != NULL) | |
11199 | free (flinfo->sections); | |
9f7c3e5e AM |
11200 | if (flinfo->symshndxbuf != NULL) |
11201 | free (flinfo->symshndxbuf); | |
11202 | for (o = obfd->sections; o != NULL; o = o->next) | |
11203 | { | |
11204 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11205 | if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) | |
11206 | free (esdo->rel.hashes); | |
11207 | if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) | |
11208 | free (esdo->rela.hashes); | |
11209 | } | |
11210 | } | |
0b52efa6 | 11211 | |
c152c796 AM |
11212 | /* Do the final step of an ELF link. */ |
11213 | ||
11214 | bfd_boolean | |
11215 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
11216 | { | |
11217 | bfd_boolean dynamic; | |
11218 | bfd_boolean emit_relocs; | |
11219 | bfd *dynobj; | |
8b127cbc | 11220 | struct elf_final_link_info flinfo; |
91d6fa6a NC |
11221 | asection *o; |
11222 | struct bfd_link_order *p; | |
11223 | bfd *sub; | |
c152c796 AM |
11224 | bfd_size_type max_contents_size; |
11225 | bfd_size_type max_external_reloc_size; | |
11226 | bfd_size_type max_internal_reloc_count; | |
11227 | bfd_size_type max_sym_count; | |
11228 | bfd_size_type max_sym_shndx_count; | |
c152c796 AM |
11229 | Elf_Internal_Sym elfsym; |
11230 | unsigned int i; | |
11231 | Elf_Internal_Shdr *symtab_hdr; | |
11232 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
c152c796 AM |
11233 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11234 | struct elf_outext_info eoinfo; | |
11235 | bfd_boolean merged; | |
11236 | size_t relativecount = 0; | |
11237 | asection *reldyn = 0; | |
11238 | bfd_size_type amt; | |
104d59d1 JM |
11239 | asection *attr_section = NULL; |
11240 | bfd_vma attr_size = 0; | |
11241 | const char *std_attrs_section; | |
64f52338 | 11242 | struct elf_link_hash_table *htab = elf_hash_table (info); |
c152c796 | 11243 | |
64f52338 | 11244 | if (!is_elf_hash_table (htab)) |
c152c796 AM |
11245 | return FALSE; |
11246 | ||
0e1862bb | 11247 | if (bfd_link_pic (info)) |
c152c796 AM |
11248 | abfd->flags |= DYNAMIC; |
11249 | ||
64f52338 AM |
11250 | dynamic = htab->dynamic_sections_created; |
11251 | dynobj = htab->dynobj; | |
c152c796 | 11252 | |
0e1862bb | 11253 | emit_relocs = (bfd_link_relocatable (info) |
a4676736 | 11254 | || info->emitrelocations); |
c152c796 | 11255 | |
8b127cbc AM |
11256 | flinfo.info = info; |
11257 | flinfo.output_bfd = abfd; | |
ef10c3ac | 11258 | flinfo.symstrtab = _bfd_elf_strtab_init (); |
8b127cbc | 11259 | if (flinfo.symstrtab == NULL) |
c152c796 AM |
11260 | return FALSE; |
11261 | ||
11262 | if (! dynamic) | |
11263 | { | |
8b127cbc AM |
11264 | flinfo.hash_sec = NULL; |
11265 | flinfo.symver_sec = NULL; | |
c152c796 AM |
11266 | } |
11267 | else | |
11268 | { | |
3d4d4302 | 11269 | flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); |
202e2356 | 11270 | /* Note that dynsym_sec can be NULL (on VMS). */ |
3d4d4302 | 11271 | flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); |
c152c796 AM |
11272 | /* Note that it is OK if symver_sec is NULL. */ |
11273 | } | |
11274 | ||
8b127cbc AM |
11275 | flinfo.contents = NULL; |
11276 | flinfo.external_relocs = NULL; | |
11277 | flinfo.internal_relocs = NULL; | |
11278 | flinfo.external_syms = NULL; | |
11279 | flinfo.locsym_shndx = NULL; | |
11280 | flinfo.internal_syms = NULL; | |
11281 | flinfo.indices = NULL; | |
11282 | flinfo.sections = NULL; | |
8b127cbc | 11283 | flinfo.symshndxbuf = NULL; |
ffbc01cc | 11284 | flinfo.filesym_count = 0; |
c152c796 | 11285 | |
104d59d1 JM |
11286 | /* The object attributes have been merged. Remove the input |
11287 | sections from the link, and set the contents of the output | |
11288 | secton. */ | |
11289 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; | |
11290 | for (o = abfd->sections; o != NULL; o = o->next) | |
11291 | { | |
11292 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) | |
11293 | || strcmp (o->name, ".gnu.attributes") == 0) | |
11294 | { | |
11295 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
11296 | { | |
11297 | asection *input_section; | |
11298 | ||
11299 | if (p->type != bfd_indirect_link_order) | |
11300 | continue; | |
11301 | input_section = p->u.indirect.section; | |
11302 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
11303 | elf_link_input_bfd ignores this section. */ | |
11304 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
11305 | } | |
a0c8462f | 11306 | |
104d59d1 JM |
11307 | attr_size = bfd_elf_obj_attr_size (abfd); |
11308 | if (attr_size) | |
11309 | { | |
11310 | bfd_set_section_size (abfd, o, attr_size); | |
11311 | attr_section = o; | |
11312 | /* Skip this section later on. */ | |
11313 | o->map_head.link_order = NULL; | |
11314 | } | |
11315 | else | |
11316 | o->flags |= SEC_EXCLUDE; | |
11317 | } | |
11318 | } | |
11319 | ||
c152c796 AM |
11320 | /* Count up the number of relocations we will output for each output |
11321 | section, so that we know the sizes of the reloc sections. We | |
11322 | also figure out some maximum sizes. */ | |
11323 | max_contents_size = 0; | |
11324 | max_external_reloc_size = 0; | |
11325 | max_internal_reloc_count = 0; | |
11326 | max_sym_count = 0; | |
11327 | max_sym_shndx_count = 0; | |
11328 | merged = FALSE; | |
11329 | for (o = abfd->sections; o != NULL; o = o->next) | |
11330 | { | |
11331 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11332 | o->reloc_count = 0; | |
11333 | ||
8423293d | 11334 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
11335 | { |
11336 | unsigned int reloc_count = 0; | |
9eaff861 | 11337 | unsigned int additional_reloc_count = 0; |
c152c796 | 11338 | struct bfd_elf_section_data *esdi = NULL; |
c152c796 AM |
11339 | |
11340 | if (p->type == bfd_section_reloc_link_order | |
11341 | || p->type == bfd_symbol_reloc_link_order) | |
11342 | reloc_count = 1; | |
11343 | else if (p->type == bfd_indirect_link_order) | |
11344 | { | |
11345 | asection *sec; | |
11346 | ||
11347 | sec = p->u.indirect.section; | |
c152c796 AM |
11348 | |
11349 | /* Mark all sections which are to be included in the | |
11350 | link. This will normally be every section. We need | |
11351 | to do this so that we can identify any sections which | |
11352 | the linker has decided to not include. */ | |
11353 | sec->linker_mark = TRUE; | |
11354 | ||
11355 | if (sec->flags & SEC_MERGE) | |
11356 | merged = TRUE; | |
11357 | ||
eea6121a AM |
11358 | if (sec->rawsize > max_contents_size) |
11359 | max_contents_size = sec->rawsize; | |
11360 | if (sec->size > max_contents_size) | |
11361 | max_contents_size = sec->size; | |
c152c796 | 11362 | |
c152c796 AM |
11363 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour |
11364 | && (sec->owner->flags & DYNAMIC) == 0) | |
11365 | { | |
11366 | size_t sym_count; | |
11367 | ||
a961cdd5 AM |
11368 | /* We are interested in just local symbols, not all |
11369 | symbols. */ | |
c152c796 AM |
11370 | if (elf_bad_symtab (sec->owner)) |
11371 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
11372 | / bed->s->sizeof_sym); | |
11373 | else | |
11374 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
11375 | ||
11376 | if (sym_count > max_sym_count) | |
11377 | max_sym_count = sym_count; | |
11378 | ||
11379 | if (sym_count > max_sym_shndx_count | |
6a40cf0c | 11380 | && elf_symtab_shndx_list (sec->owner) != NULL) |
c152c796 AM |
11381 | max_sym_shndx_count = sym_count; |
11382 | ||
a961cdd5 AM |
11383 | if (esdo->this_hdr.sh_type == SHT_REL |
11384 | || esdo->this_hdr.sh_type == SHT_RELA) | |
11385 | /* Some backends use reloc_count in relocation sections | |
11386 | to count particular types of relocs. Of course, | |
11387 | reloc sections themselves can't have relocations. */ | |
11388 | ; | |
11389 | else if (emit_relocs) | |
11390 | { | |
11391 | reloc_count = sec->reloc_count; | |
11392 | if (bed->elf_backend_count_additional_relocs) | |
11393 | { | |
11394 | int c; | |
11395 | c = (*bed->elf_backend_count_additional_relocs) (sec); | |
11396 | additional_reloc_count += c; | |
11397 | } | |
11398 | } | |
11399 | else if (bed->elf_backend_count_relocs) | |
11400 | reloc_count = (*bed->elf_backend_count_relocs) (info, sec); | |
11401 | ||
11402 | esdi = elf_section_data (sec); | |
11403 | ||
c152c796 AM |
11404 | if ((sec->flags & SEC_RELOC) != 0) |
11405 | { | |
d4730f92 | 11406 | size_t ext_size = 0; |
c152c796 | 11407 | |
d4730f92 BS |
11408 | if (esdi->rel.hdr != NULL) |
11409 | ext_size = esdi->rel.hdr->sh_size; | |
11410 | if (esdi->rela.hdr != NULL) | |
11411 | ext_size += esdi->rela.hdr->sh_size; | |
7326c758 | 11412 | |
c152c796 AM |
11413 | if (ext_size > max_external_reloc_size) |
11414 | max_external_reloc_size = ext_size; | |
11415 | if (sec->reloc_count > max_internal_reloc_count) | |
11416 | max_internal_reloc_count = sec->reloc_count; | |
11417 | } | |
11418 | } | |
11419 | } | |
11420 | ||
11421 | if (reloc_count == 0) | |
11422 | continue; | |
11423 | ||
9eaff861 | 11424 | reloc_count += additional_reloc_count; |
c152c796 AM |
11425 | o->reloc_count += reloc_count; |
11426 | ||
0e1862bb | 11427 | if (p->type == bfd_indirect_link_order && emit_relocs) |
c152c796 | 11428 | { |
d4730f92 | 11429 | if (esdi->rel.hdr) |
9eaff861 | 11430 | { |
491d01d3 | 11431 | esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); |
9eaff861 AO |
11432 | esdo->rel.count += additional_reloc_count; |
11433 | } | |
d4730f92 | 11434 | if (esdi->rela.hdr) |
9eaff861 | 11435 | { |
491d01d3 | 11436 | esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); |
9eaff861 AO |
11437 | esdo->rela.count += additional_reloc_count; |
11438 | } | |
d4730f92 BS |
11439 | } |
11440 | else | |
11441 | { | |
11442 | if (o->use_rela_p) | |
11443 | esdo->rela.count += reloc_count; | |
2c2b4ed4 | 11444 | else |
d4730f92 | 11445 | esdo->rel.count += reloc_count; |
c152c796 | 11446 | } |
c152c796 AM |
11447 | } |
11448 | ||
9eaff861 | 11449 | if (o->reloc_count > 0) |
c152c796 AM |
11450 | o->flags |= SEC_RELOC; |
11451 | else | |
11452 | { | |
11453 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
11454 | set it (this is probably a bug) and if it is set | |
11455 | assign_section_numbers will create a reloc section. */ | |
11456 | o->flags &=~ SEC_RELOC; | |
11457 | } | |
11458 | ||
11459 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
11460 | zero. This is done in elf_fake_sections as well, but forcing | |
11461 | the VMA to 0 here will ensure that relocs against these | |
11462 | sections are handled correctly. */ | |
11463 | if ((o->flags & SEC_ALLOC) == 0 | |
11464 | && ! o->user_set_vma) | |
11465 | o->vma = 0; | |
11466 | } | |
11467 | ||
0e1862bb | 11468 | if (! bfd_link_relocatable (info) && merged) |
64f52338 | 11469 | elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd); |
c152c796 AM |
11470 | |
11471 | /* Figure out the file positions for everything but the symbol table | |
11472 | and the relocs. We set symcount to force assign_section_numbers | |
11473 | to create a symbol table. */ | |
8539e4e8 | 11474 | bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs; |
c152c796 AM |
11475 | BFD_ASSERT (! abfd->output_has_begun); |
11476 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
11477 | goto error_return; | |
11478 | ||
ee75fd95 | 11479 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
11480 | for (o = abfd->sections; o != NULL; o = o->next) |
11481 | { | |
d4730f92 | 11482 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
c152c796 AM |
11483 | if ((o->flags & SEC_RELOC) != 0) |
11484 | { | |
d4730f92 | 11485 | if (esdo->rel.hdr |
9eaff861 | 11486 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) |
c152c796 AM |
11487 | goto error_return; |
11488 | ||
d4730f92 | 11489 | if (esdo->rela.hdr |
9eaff861 | 11490 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) |
c152c796 AM |
11491 | goto error_return; |
11492 | } | |
11493 | ||
11494 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
11495 | to count upwards while actually outputting the relocations. */ | |
d4730f92 BS |
11496 | esdo->rel.count = 0; |
11497 | esdo->rela.count = 0; | |
0ce398f1 L |
11498 | |
11499 | if (esdo->this_hdr.sh_offset == (file_ptr) -1) | |
11500 | { | |
11501 | /* Cache the section contents so that they can be compressed | |
11502 | later. Use bfd_malloc since it will be freed by | |
11503 | bfd_compress_section_contents. */ | |
11504 | unsigned char *contents = esdo->this_hdr.contents; | |
11505 | if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) | |
11506 | abort (); | |
11507 | contents | |
11508 | = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); | |
11509 | if (contents == NULL) | |
11510 | goto error_return; | |
11511 | esdo->this_hdr.contents = contents; | |
11512 | } | |
c152c796 AM |
11513 | } |
11514 | ||
c152c796 | 11515 | /* We have now assigned file positions for all the sections except |
a485e98e AM |
11516 | .symtab, .strtab, and non-loaded reloc sections. We start the |
11517 | .symtab section at the current file position, and write directly | |
11518 | to it. We build the .strtab section in memory. */ | |
c152c796 AM |
11519 | bfd_get_symcount (abfd) = 0; |
11520 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11521 | /* sh_name is set in prep_headers. */ | |
11522 | symtab_hdr->sh_type = SHT_SYMTAB; | |
11523 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
11524 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
11525 | /* sh_link is set in assign_section_numbers. */ | |
11526 | /* sh_info is set below. */ | |
11527 | /* sh_offset is set just below. */ | |
72de5009 | 11528 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
c152c796 | 11529 | |
ef10c3ac L |
11530 | if (max_sym_count < 20) |
11531 | max_sym_count = 20; | |
64f52338 | 11532 | htab->strtabsize = max_sym_count; |
ef10c3ac | 11533 | amt = max_sym_count * sizeof (struct elf_sym_strtab); |
64f52338 AM |
11534 | htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt); |
11535 | if (htab->strtab == NULL) | |
c152c796 | 11536 | goto error_return; |
ef10c3ac L |
11537 | /* The real buffer will be allocated in elf_link_swap_symbols_out. */ |
11538 | flinfo.symshndxbuf | |
11539 | = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) | |
11540 | ? (Elf_External_Sym_Shndx *) -1 : NULL); | |
c152c796 | 11541 | |
8539e4e8 | 11542 | if (info->strip != strip_all || emit_relocs) |
c152c796 | 11543 | { |
8539e4e8 AM |
11544 | file_ptr off = elf_next_file_pos (abfd); |
11545 | ||
11546 | _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
11547 | ||
11548 | /* Note that at this point elf_next_file_pos (abfd) is | |
11549 | incorrect. We do not yet know the size of the .symtab section. | |
11550 | We correct next_file_pos below, after we do know the size. */ | |
11551 | ||
11552 | /* Start writing out the symbol table. The first symbol is always a | |
11553 | dummy symbol. */ | |
c152c796 AM |
11554 | elfsym.st_value = 0; |
11555 | elfsym.st_size = 0; | |
11556 | elfsym.st_info = 0; | |
11557 | elfsym.st_other = 0; | |
11558 | elfsym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 11559 | elfsym.st_target_internal = 0; |
ef10c3ac L |
11560 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, |
11561 | bfd_und_section_ptr, NULL) != 1) | |
c152c796 | 11562 | goto error_return; |
c152c796 | 11563 | |
8539e4e8 AM |
11564 | /* Output a symbol for each section. We output these even if we are |
11565 | discarding local symbols, since they are used for relocs. These | |
11566 | symbols have no names. We store the index of each one in the | |
11567 | index field of the section, so that we can find it again when | |
11568 | outputting relocs. */ | |
11569 | ||
c152c796 AM |
11570 | elfsym.st_size = 0; |
11571 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
11572 | elfsym.st_other = 0; | |
f0b5bb34 | 11573 | elfsym.st_value = 0; |
35fc36a8 | 11574 | elfsym.st_target_internal = 0; |
c152c796 AM |
11575 | for (i = 1; i < elf_numsections (abfd); i++) |
11576 | { | |
11577 | o = bfd_section_from_elf_index (abfd, i); | |
11578 | if (o != NULL) | |
f0b5bb34 AM |
11579 | { |
11580 | o->target_index = bfd_get_symcount (abfd); | |
11581 | elfsym.st_shndx = i; | |
0e1862bb | 11582 | if (!bfd_link_relocatable (info)) |
f0b5bb34 | 11583 | elfsym.st_value = o->vma; |
ef10c3ac L |
11584 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o, |
11585 | NULL) != 1) | |
f0b5bb34 AM |
11586 | goto error_return; |
11587 | } | |
c152c796 AM |
11588 | } |
11589 | } | |
11590 | ||
11591 | /* Allocate some memory to hold information read in from the input | |
11592 | files. */ | |
11593 | if (max_contents_size != 0) | |
11594 | { | |
8b127cbc AM |
11595 | flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
11596 | if (flinfo.contents == NULL) | |
c152c796 AM |
11597 | goto error_return; |
11598 | } | |
11599 | ||
11600 | if (max_external_reloc_size != 0) | |
11601 | { | |
8b127cbc AM |
11602 | flinfo.external_relocs = bfd_malloc (max_external_reloc_size); |
11603 | if (flinfo.external_relocs == NULL) | |
c152c796 AM |
11604 | goto error_return; |
11605 | } | |
11606 | ||
11607 | if (max_internal_reloc_count != 0) | |
11608 | { | |
11609 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
11610 | amt *= sizeof (Elf_Internal_Rela); | |
8b127cbc AM |
11611 | flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); |
11612 | if (flinfo.internal_relocs == NULL) | |
c152c796 AM |
11613 | goto error_return; |
11614 | } | |
11615 | ||
11616 | if (max_sym_count != 0) | |
11617 | { | |
11618 | amt = max_sym_count * bed->s->sizeof_sym; | |
8b127cbc AM |
11619 | flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); |
11620 | if (flinfo.external_syms == NULL) | |
c152c796 AM |
11621 | goto error_return; |
11622 | ||
11623 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8b127cbc AM |
11624 | flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); |
11625 | if (flinfo.internal_syms == NULL) | |
c152c796 AM |
11626 | goto error_return; |
11627 | ||
11628 | amt = max_sym_count * sizeof (long); | |
8b127cbc AM |
11629 | flinfo.indices = (long int *) bfd_malloc (amt); |
11630 | if (flinfo.indices == NULL) | |
c152c796 AM |
11631 | goto error_return; |
11632 | ||
11633 | amt = max_sym_count * sizeof (asection *); | |
8b127cbc AM |
11634 | flinfo.sections = (asection **) bfd_malloc (amt); |
11635 | if (flinfo.sections == NULL) | |
c152c796 AM |
11636 | goto error_return; |
11637 | } | |
11638 | ||
11639 | if (max_sym_shndx_count != 0) | |
11640 | { | |
11641 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8b127cbc AM |
11642 | flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
11643 | if (flinfo.locsym_shndx == NULL) | |
c152c796 AM |
11644 | goto error_return; |
11645 | } | |
11646 | ||
64f52338 | 11647 | if (htab->tls_sec) |
c152c796 AM |
11648 | { |
11649 | bfd_vma base, end = 0; | |
11650 | asection *sec; | |
11651 | ||
64f52338 | 11652 | for (sec = htab->tls_sec; |
c152c796 AM |
11653 | sec && (sec->flags & SEC_THREAD_LOCAL); |
11654 | sec = sec->next) | |
11655 | { | |
3a800eb9 | 11656 | bfd_size_type size = sec->size; |
c152c796 | 11657 | |
3a800eb9 AM |
11658 | if (size == 0 |
11659 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 11660 | { |
91d6fa6a NC |
11661 | struct bfd_link_order *ord = sec->map_tail.link_order; |
11662 | ||
11663 | if (ord != NULL) | |
11664 | size = ord->offset + ord->size; | |
c152c796 AM |
11665 | } |
11666 | end = sec->vma + size; | |
11667 | } | |
64f52338 | 11668 | base = htab->tls_sec->vma; |
7dc98aea RO |
11669 | /* Only align end of TLS section if static TLS doesn't have special |
11670 | alignment requirements. */ | |
11671 | if (bed->static_tls_alignment == 1) | |
64f52338 AM |
11672 | end = align_power (end, htab->tls_sec->alignment_power); |
11673 | htab->tls_size = end - base; | |
c152c796 AM |
11674 | } |
11675 | ||
0b52efa6 PB |
11676 | /* Reorder SHF_LINK_ORDER sections. */ |
11677 | for (o = abfd->sections; o != NULL; o = o->next) | |
11678 | { | |
11679 | if (!elf_fixup_link_order (abfd, o)) | |
11680 | return FALSE; | |
11681 | } | |
11682 | ||
2f0c68f2 CM |
11683 | if (!_bfd_elf_fixup_eh_frame_hdr (info)) |
11684 | return FALSE; | |
11685 | ||
c152c796 AM |
11686 | /* Since ELF permits relocations to be against local symbols, we |
11687 | must have the local symbols available when we do the relocations. | |
11688 | Since we would rather only read the local symbols once, and we | |
11689 | would rather not keep them in memory, we handle all the | |
11690 | relocations for a single input file at the same time. | |
11691 | ||
11692 | Unfortunately, there is no way to know the total number of local | |
11693 | symbols until we have seen all of them, and the local symbol | |
11694 | indices precede the global symbol indices. This means that when | |
11695 | we are generating relocatable output, and we see a reloc against | |
11696 | a global symbol, we can not know the symbol index until we have | |
11697 | finished examining all the local symbols to see which ones we are | |
11698 | going to output. To deal with this, we keep the relocations in | |
11699 | memory, and don't output them until the end of the link. This is | |
11700 | an unfortunate waste of memory, but I don't see a good way around | |
11701 | it. Fortunately, it only happens when performing a relocatable | |
11702 | link, which is not the common case. FIXME: If keep_memory is set | |
11703 | we could write the relocs out and then read them again; I don't | |
11704 | know how bad the memory loss will be. */ | |
11705 | ||
c72f2fb2 | 11706 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
11707 | sub->output_has_begun = FALSE; |
11708 | for (o = abfd->sections; o != NULL; o = o->next) | |
11709 | { | |
8423293d | 11710 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
11711 | { |
11712 | if (p->type == bfd_indirect_link_order | |
11713 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
11714 | == bfd_target_elf_flavour) | |
11715 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
11716 | { | |
11717 | if (! sub->output_has_begun) | |
11718 | { | |
8b127cbc | 11719 | if (! elf_link_input_bfd (&flinfo, sub)) |
c152c796 AM |
11720 | goto error_return; |
11721 | sub->output_has_begun = TRUE; | |
11722 | } | |
11723 | } | |
11724 | else if (p->type == bfd_section_reloc_link_order | |
11725 | || p->type == bfd_symbol_reloc_link_order) | |
11726 | { | |
11727 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
11728 | goto error_return; | |
11729 | } | |
11730 | else | |
11731 | { | |
11732 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
351f65ca L |
11733 | { |
11734 | if (p->type == bfd_indirect_link_order | |
11735 | && (bfd_get_flavour (sub) | |
11736 | == bfd_target_elf_flavour) | |
11737 | && (elf_elfheader (sub)->e_ident[EI_CLASS] | |
11738 | != bed->s->elfclass)) | |
11739 | { | |
11740 | const char *iclass, *oclass; | |
11741 | ||
aebf9be7 | 11742 | switch (bed->s->elfclass) |
351f65ca | 11743 | { |
aebf9be7 NC |
11744 | case ELFCLASS64: oclass = "ELFCLASS64"; break; |
11745 | case ELFCLASS32: oclass = "ELFCLASS32"; break; | |
11746 | case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; | |
11747 | default: abort (); | |
351f65ca | 11748 | } |
aebf9be7 NC |
11749 | |
11750 | switch (elf_elfheader (sub)->e_ident[EI_CLASS]) | |
351f65ca | 11751 | { |
aebf9be7 NC |
11752 | case ELFCLASS64: iclass = "ELFCLASS64"; break; |
11753 | case ELFCLASS32: iclass = "ELFCLASS32"; break; | |
11754 | case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; | |
11755 | default: abort (); | |
351f65ca L |
11756 | } |
11757 | ||
11758 | bfd_set_error (bfd_error_wrong_format); | |
4eca0228 | 11759 | _bfd_error_handler |
695344c0 | 11760 | /* xgettext:c-format */ |
351f65ca L |
11761 | (_("%B: file class %s incompatible with %s"), |
11762 | sub, iclass, oclass); | |
11763 | } | |
11764 | ||
11765 | goto error_return; | |
11766 | } | |
c152c796 AM |
11767 | } |
11768 | } | |
11769 | } | |
11770 | ||
c0f00686 L |
11771 | /* Free symbol buffer if needed. */ |
11772 | if (!info->reduce_memory_overheads) | |
11773 | { | |
c72f2fb2 | 11774 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
3fcd97f1 JJ |
11775 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
11776 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
11777 | { |
11778 | free (elf_tdata (sub)->symbuf); | |
11779 | elf_tdata (sub)->symbuf = NULL; | |
11780 | } | |
11781 | } | |
11782 | ||
c152c796 AM |
11783 | /* Output any global symbols that got converted to local in a |
11784 | version script or due to symbol visibility. We do this in a | |
11785 | separate step since ELF requires all local symbols to appear | |
11786 | prior to any global symbols. FIXME: We should only do this if | |
11787 | some global symbols were, in fact, converted to become local. | |
11788 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
11789 | eoinfo.failed = FALSE; | |
8b127cbc | 11790 | eoinfo.flinfo = &flinfo; |
c152c796 | 11791 | eoinfo.localsyms = TRUE; |
34a79995 | 11792 | eoinfo.file_sym_done = FALSE; |
7686d77d | 11793 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
11794 | if (eoinfo.failed) |
11795 | return FALSE; | |
11796 | ||
4e617b1e PB |
11797 | /* If backend needs to output some local symbols not present in the hash |
11798 | table, do it now. */ | |
8539e4e8 AM |
11799 | if (bed->elf_backend_output_arch_local_syms |
11800 | && (info->strip != strip_all || emit_relocs)) | |
4e617b1e | 11801 | { |
6e0b88f1 | 11802 | typedef int (*out_sym_func) |
4e617b1e PB |
11803 | (void *, const char *, Elf_Internal_Sym *, asection *, |
11804 | struct elf_link_hash_entry *); | |
11805 | ||
11806 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
ef10c3ac L |
11807 | (abfd, info, &flinfo, |
11808 | (out_sym_func) elf_link_output_symstrtab))) | |
4e617b1e PB |
11809 | return FALSE; |
11810 | } | |
11811 | ||
c152c796 AM |
11812 | /* That wrote out all the local symbols. Finish up the symbol table |
11813 | with the global symbols. Even if we want to strip everything we | |
11814 | can, we still need to deal with those global symbols that got | |
11815 | converted to local in a version script. */ | |
11816 | ||
11817 | /* The sh_info field records the index of the first non local symbol. */ | |
11818 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
11819 | ||
11820 | if (dynamic | |
64f52338 AM |
11821 | && htab->dynsym != NULL |
11822 | && htab->dynsym->output_section != bfd_abs_section_ptr) | |
c152c796 AM |
11823 | { |
11824 | Elf_Internal_Sym sym; | |
64f52338 | 11825 | bfd_byte *dynsym = htab->dynsym->contents; |
90ac2420 | 11826 | |
64f52338 AM |
11827 | o = htab->dynsym->output_section; |
11828 | elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1; | |
c152c796 AM |
11829 | |
11830 | /* Write out the section symbols for the output sections. */ | |
0e1862bb | 11831 | if (bfd_link_pic (info) |
64f52338 | 11832 | || htab->is_relocatable_executable) |
c152c796 AM |
11833 | { |
11834 | asection *s; | |
11835 | ||
11836 | sym.st_size = 0; | |
11837 | sym.st_name = 0; | |
11838 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
11839 | sym.st_other = 0; | |
35fc36a8 | 11840 | sym.st_target_internal = 0; |
c152c796 AM |
11841 | |
11842 | for (s = abfd->sections; s != NULL; s = s->next) | |
11843 | { | |
11844 | int indx; | |
11845 | bfd_byte *dest; | |
11846 | long dynindx; | |
11847 | ||
c152c796 | 11848 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
11849 | if (dynindx <= 0) |
11850 | continue; | |
11851 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
11852 | BFD_ASSERT (indx > 0); |
11853 | sym.st_shndx = indx; | |
c0d5a53d L |
11854 | if (! check_dynsym (abfd, &sym)) |
11855 | return FALSE; | |
c152c796 AM |
11856 | sym.st_value = s->vma; |
11857 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
11858 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
11859 | } | |
c152c796 AM |
11860 | } |
11861 | ||
11862 | /* Write out the local dynsyms. */ | |
64f52338 | 11863 | if (htab->dynlocal) |
c152c796 AM |
11864 | { |
11865 | struct elf_link_local_dynamic_entry *e; | |
64f52338 | 11866 | for (e = htab->dynlocal; e ; e = e->next) |
c152c796 AM |
11867 | { |
11868 | asection *s; | |
11869 | bfd_byte *dest; | |
11870 | ||
935bd1e0 | 11871 | /* Copy the internal symbol and turn off visibility. |
c152c796 AM |
11872 | Note that we saved a word of storage and overwrote |
11873 | the original st_name with the dynstr_index. */ | |
11874 | sym = e->isym; | |
935bd1e0 | 11875 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); |
c152c796 | 11876 | |
cb33740c AM |
11877 | s = bfd_section_from_elf_index (e->input_bfd, |
11878 | e->isym.st_shndx); | |
11879 | if (s != NULL) | |
c152c796 | 11880 | { |
c152c796 AM |
11881 | sym.st_shndx = |
11882 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
11883 | if (! check_dynsym (abfd, &sym)) |
11884 | return FALSE; | |
c152c796 AM |
11885 | sym.st_value = (s->output_section->vma |
11886 | + s->output_offset | |
11887 | + e->isym.st_value); | |
11888 | } | |
11889 | ||
c152c796 AM |
11890 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; |
11891 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
11892 | } | |
11893 | } | |
c152c796 AM |
11894 | } |
11895 | ||
11896 | /* We get the global symbols from the hash table. */ | |
11897 | eoinfo.failed = FALSE; | |
11898 | eoinfo.localsyms = FALSE; | |
8b127cbc | 11899 | eoinfo.flinfo = &flinfo; |
7686d77d | 11900 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
11901 | if (eoinfo.failed) |
11902 | return FALSE; | |
11903 | ||
11904 | /* If backend needs to output some symbols not present in the hash | |
11905 | table, do it now. */ | |
8539e4e8 AM |
11906 | if (bed->elf_backend_output_arch_syms |
11907 | && (info->strip != strip_all || emit_relocs)) | |
c152c796 | 11908 | { |
6e0b88f1 | 11909 | typedef int (*out_sym_func) |
c152c796 AM |
11910 | (void *, const char *, Elf_Internal_Sym *, asection *, |
11911 | struct elf_link_hash_entry *); | |
11912 | ||
11913 | if (! ((*bed->elf_backend_output_arch_syms) | |
ef10c3ac L |
11914 | (abfd, info, &flinfo, |
11915 | (out_sym_func) elf_link_output_symstrtab))) | |
c152c796 AM |
11916 | return FALSE; |
11917 | } | |
11918 | ||
ef10c3ac L |
11919 | /* Finalize the .strtab section. */ |
11920 | _bfd_elf_strtab_finalize (flinfo.symstrtab); | |
11921 | ||
11922 | /* Swap out the .strtab section. */ | |
11923 | if (!elf_link_swap_symbols_out (&flinfo)) | |
c152c796 AM |
11924 | return FALSE; |
11925 | ||
11926 | /* Now we know the size of the symtab section. */ | |
c152c796 AM |
11927 | if (bfd_get_symcount (abfd) > 0) |
11928 | { | |
ee3b52e9 L |
11929 | /* Finish up and write out the symbol string table (.strtab) |
11930 | section. */ | |
11931 | Elf_Internal_Shdr *symstrtab_hdr; | |
8539e4e8 AM |
11932 | file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; |
11933 | ||
6a40cf0c NC |
11934 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
11935 | if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) | |
8539e4e8 AM |
11936 | { |
11937 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
11938 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
11939 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
11940 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
11941 | symtab_shndx_hdr->sh_size = amt; | |
11942 | ||
11943 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
11944 | off, TRUE); | |
11945 | ||
11946 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
11947 | || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) | |
11948 | return FALSE; | |
11949 | } | |
ee3b52e9 L |
11950 | |
11951 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
11952 | /* sh_name was set in prep_headers. */ | |
11953 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 11954 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
ee3b52e9 | 11955 | symstrtab_hdr->sh_addr = 0; |
ef10c3ac | 11956 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); |
ee3b52e9 L |
11957 | symstrtab_hdr->sh_entsize = 0; |
11958 | symstrtab_hdr->sh_link = 0; | |
11959 | symstrtab_hdr->sh_info = 0; | |
11960 | /* sh_offset is set just below. */ | |
11961 | symstrtab_hdr->sh_addralign = 1; | |
11962 | ||
11963 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, | |
11964 | off, TRUE); | |
11965 | elf_next_file_pos (abfd) = off; | |
11966 | ||
c152c796 | 11967 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 11968 | || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) |
c152c796 AM |
11969 | return FALSE; |
11970 | } | |
11971 | ||
76359541 TP |
11972 | if (info->out_implib_bfd && !elf_output_implib (abfd, info)) |
11973 | { | |
4eca0228 AM |
11974 | _bfd_error_handler (_("%B: failed to generate import library"), |
11975 | info->out_implib_bfd); | |
76359541 TP |
11976 | return FALSE; |
11977 | } | |
11978 | ||
c152c796 AM |
11979 | /* Adjust the relocs to have the correct symbol indices. */ |
11980 | for (o = abfd->sections; o != NULL; o = o->next) | |
11981 | { | |
d4730f92 | 11982 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
28dbcedc | 11983 | bfd_boolean sort; |
c152c796 AM |
11984 | if ((o->flags & SEC_RELOC) == 0) |
11985 | continue; | |
11986 | ||
28dbcedc | 11987 | sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); |
bca6d0e3 | 11988 | if (esdo->rel.hdr != NULL |
9eaff861 | 11989 | && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort)) |
bca6d0e3 AM |
11990 | return FALSE; |
11991 | if (esdo->rela.hdr != NULL | |
9eaff861 | 11992 | && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort)) |
bca6d0e3 | 11993 | return FALSE; |
c152c796 AM |
11994 | |
11995 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
11996 | trying to swap the relocs out itself. */ | |
11997 | o->reloc_count = 0; | |
11998 | } | |
11999 | ||
12000 | if (dynamic && info->combreloc && dynobj != NULL) | |
12001 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
12002 | ||
12003 | /* If we are linking against a dynamic object, or generating a | |
12004 | shared library, finish up the dynamic linking information. */ | |
12005 | if (dynamic) | |
12006 | { | |
12007 | bfd_byte *dyncon, *dynconend; | |
12008 | ||
12009 | /* Fix up .dynamic entries. */ | |
3d4d4302 | 12010 | o = bfd_get_linker_section (dynobj, ".dynamic"); |
c152c796 AM |
12011 | BFD_ASSERT (o != NULL); |
12012 | ||
12013 | dyncon = o->contents; | |
eea6121a | 12014 | dynconend = o->contents + o->size; |
c152c796 AM |
12015 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
12016 | { | |
12017 | Elf_Internal_Dyn dyn; | |
12018 | const char *name; | |
12019 | unsigned int type; | |
12020 | ||
12021 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12022 | ||
12023 | switch (dyn.d_tag) | |
12024 | { | |
12025 | default: | |
12026 | continue; | |
12027 | case DT_NULL: | |
12028 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
12029 | { | |
12030 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
12031 | { | |
12032 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
12033 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
12034 | default: continue; | |
12035 | } | |
12036 | dyn.d_un.d_val = relativecount; | |
12037 | relativecount = 0; | |
12038 | break; | |
12039 | } | |
12040 | continue; | |
12041 | ||
12042 | case DT_INIT: | |
12043 | name = info->init_function; | |
12044 | goto get_sym; | |
12045 | case DT_FINI: | |
12046 | name = info->fini_function; | |
12047 | get_sym: | |
12048 | { | |
12049 | struct elf_link_hash_entry *h; | |
12050 | ||
64f52338 | 12051 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
c152c796 AM |
12052 | if (h != NULL |
12053 | && (h->root.type == bfd_link_hash_defined | |
12054 | || h->root.type == bfd_link_hash_defweak)) | |
12055 | { | |
bef26483 | 12056 | dyn.d_un.d_ptr = h->root.u.def.value; |
c152c796 AM |
12057 | o = h->root.u.def.section; |
12058 | if (o->output_section != NULL) | |
bef26483 | 12059 | dyn.d_un.d_ptr += (o->output_section->vma |
c152c796 AM |
12060 | + o->output_offset); |
12061 | else | |
12062 | { | |
12063 | /* The symbol is imported from another shared | |
12064 | library and does not apply to this one. */ | |
bef26483 | 12065 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
12066 | } |
12067 | break; | |
12068 | } | |
12069 | } | |
12070 | continue; | |
12071 | ||
12072 | case DT_PREINIT_ARRAYSZ: | |
12073 | name = ".preinit_array"; | |
4ade44b7 | 12074 | goto get_out_size; |
c152c796 AM |
12075 | case DT_INIT_ARRAYSZ: |
12076 | name = ".init_array"; | |
4ade44b7 | 12077 | goto get_out_size; |
c152c796 AM |
12078 | case DT_FINI_ARRAYSZ: |
12079 | name = ".fini_array"; | |
4ade44b7 | 12080 | get_out_size: |
c152c796 AM |
12081 | o = bfd_get_section_by_name (abfd, name); |
12082 | if (o == NULL) | |
12083 | { | |
4eca0228 | 12084 | _bfd_error_handler |
4ade44b7 | 12085 | (_("could not find section %s"), name); |
c152c796 AM |
12086 | goto error_return; |
12087 | } | |
eea6121a | 12088 | if (o->size == 0) |
4eca0228 | 12089 | _bfd_error_handler |
c152c796 | 12090 | (_("warning: %s section has zero size"), name); |
eea6121a | 12091 | dyn.d_un.d_val = o->size; |
c152c796 AM |
12092 | break; |
12093 | ||
12094 | case DT_PREINIT_ARRAY: | |
12095 | name = ".preinit_array"; | |
4ade44b7 | 12096 | goto get_out_vma; |
c152c796 AM |
12097 | case DT_INIT_ARRAY: |
12098 | name = ".init_array"; | |
4ade44b7 | 12099 | goto get_out_vma; |
c152c796 AM |
12100 | case DT_FINI_ARRAY: |
12101 | name = ".fini_array"; | |
4ade44b7 AM |
12102 | get_out_vma: |
12103 | o = bfd_get_section_by_name (abfd, name); | |
12104 | goto do_vma; | |
c152c796 AM |
12105 | |
12106 | case DT_HASH: | |
12107 | name = ".hash"; | |
12108 | goto get_vma; | |
fdc90cb4 JJ |
12109 | case DT_GNU_HASH: |
12110 | name = ".gnu.hash"; | |
12111 | goto get_vma; | |
c152c796 AM |
12112 | case DT_STRTAB: |
12113 | name = ".dynstr"; | |
12114 | goto get_vma; | |
12115 | case DT_SYMTAB: | |
12116 | name = ".dynsym"; | |
12117 | goto get_vma; | |
12118 | case DT_VERDEF: | |
12119 | name = ".gnu.version_d"; | |
12120 | goto get_vma; | |
12121 | case DT_VERNEED: | |
12122 | name = ".gnu.version_r"; | |
12123 | goto get_vma; | |
12124 | case DT_VERSYM: | |
12125 | name = ".gnu.version"; | |
12126 | get_vma: | |
4ade44b7 AM |
12127 | o = bfd_get_linker_section (dynobj, name); |
12128 | do_vma: | |
c152c796 AM |
12129 | if (o == NULL) |
12130 | { | |
4eca0228 | 12131 | _bfd_error_handler |
4ade44b7 | 12132 | (_("could not find section %s"), name); |
c152c796 AM |
12133 | goto error_return; |
12134 | } | |
894891db NC |
12135 | if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) |
12136 | { | |
4eca0228 | 12137 | _bfd_error_handler |
894891db NC |
12138 | (_("warning: section '%s' is being made into a note"), name); |
12139 | bfd_set_error (bfd_error_nonrepresentable_section); | |
12140 | goto error_return; | |
12141 | } | |
4ade44b7 | 12142 | dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; |
c152c796 AM |
12143 | break; |
12144 | ||
12145 | case DT_REL: | |
12146 | case DT_RELA: | |
12147 | case DT_RELSZ: | |
12148 | case DT_RELASZ: | |
12149 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
12150 | type = SHT_REL; | |
12151 | else | |
12152 | type = SHT_RELA; | |
12153 | dyn.d_un.d_val = 0; | |
bef26483 | 12154 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
12155 | for (i = 1; i < elf_numsections (abfd); i++) |
12156 | { | |
12157 | Elf_Internal_Shdr *hdr; | |
12158 | ||
12159 | hdr = elf_elfsections (abfd)[i]; | |
12160 | if (hdr->sh_type == type | |
12161 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
12162 | { | |
12163 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
12164 | dyn.d_un.d_val += hdr->sh_size; | |
12165 | else | |
12166 | { | |
bef26483 AM |
12167 | if (dyn.d_un.d_ptr == 0 |
12168 | || hdr->sh_addr < dyn.d_un.d_ptr) | |
12169 | dyn.d_un.d_ptr = hdr->sh_addr; | |
c152c796 AM |
12170 | } |
12171 | } | |
12172 | } | |
64f52338 AM |
12173 | if (bed->dtrel_excludes_plt && htab->srelplt != NULL) |
12174 | { | |
12175 | /* Don't count procedure linkage table relocs in the | |
12176 | overall reloc count. */ | |
12177 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
12178 | dyn.d_un.d_val -= htab->srelplt->size; | |
12179 | /* If .rela.plt is the first .rela section, exclude | |
12180 | it from DT_RELA. */ | |
12181 | else if (dyn.d_un.d_ptr == (htab->srelplt->output_section->vma | |
12182 | + htab->srelplt->output_offset)) | |
12183 | dyn.d_un.d_ptr += htab->srelplt->size; | |
12184 | } | |
c152c796 AM |
12185 | break; |
12186 | } | |
12187 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
12188 | } | |
12189 | } | |
12190 | ||
12191 | /* If we have created any dynamic sections, then output them. */ | |
12192 | if (dynobj != NULL) | |
12193 | { | |
12194 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
12195 | goto error_return; | |
12196 | ||
943284cc | 12197 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
0e1862bb | 12198 | if (((info->warn_shared_textrel && bfd_link_pic (info)) |
be7b303d | 12199 | || info->error_textrel) |
3d4d4302 | 12200 | && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) |
943284cc DJ |
12201 | { |
12202 | bfd_byte *dyncon, *dynconend; | |
12203 | ||
943284cc DJ |
12204 | dyncon = o->contents; |
12205 | dynconend = o->contents + o->size; | |
12206 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
12207 | { | |
12208 | Elf_Internal_Dyn dyn; | |
12209 | ||
12210 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12211 | ||
12212 | if (dyn.d_tag == DT_TEXTREL) | |
12213 | { | |
c192a133 AM |
12214 | if (info->error_textrel) |
12215 | info->callbacks->einfo | |
12216 | (_("%P%X: read-only segment has dynamic relocations.\n")); | |
12217 | else | |
12218 | info->callbacks->einfo | |
12219 | (_("%P: warning: creating a DT_TEXTREL in a shared object.\n")); | |
943284cc DJ |
12220 | break; |
12221 | } | |
12222 | } | |
12223 | } | |
12224 | ||
c152c796 AM |
12225 | for (o = dynobj->sections; o != NULL; o = o->next) |
12226 | { | |
12227 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 12228 | || o->size == 0 |
c152c796 AM |
12229 | || o->output_section == bfd_abs_section_ptr) |
12230 | continue; | |
12231 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
12232 | { | |
12233 | /* At this point, we are only interested in sections | |
12234 | created by _bfd_elf_link_create_dynamic_sections. */ | |
12235 | continue; | |
12236 | } | |
64f52338 | 12237 | if (htab->stab_info.stabstr == o) |
3722b82f | 12238 | continue; |
64f52338 | 12239 | if (htab->eh_info.hdr_sec == o) |
eea6121a | 12240 | continue; |
3d4d4302 | 12241 | if (strcmp (o->name, ".dynstr") != 0) |
c152c796 AM |
12242 | { |
12243 | if (! bfd_set_section_contents (abfd, o->output_section, | |
12244 | o->contents, | |
37b01f6a DG |
12245 | (file_ptr) o->output_offset |
12246 | * bfd_octets_per_byte (abfd), | |
eea6121a | 12247 | o->size)) |
c152c796 AM |
12248 | goto error_return; |
12249 | } | |
12250 | else | |
12251 | { | |
12252 | /* The contents of the .dynstr section are actually in a | |
12253 | stringtab. */ | |
8539e4e8 AM |
12254 | file_ptr off; |
12255 | ||
c152c796 AM |
12256 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; |
12257 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
64f52338 | 12258 | || !_bfd_elf_strtab_emit (abfd, htab->dynstr)) |
c152c796 AM |
12259 | goto error_return; |
12260 | } | |
12261 | } | |
12262 | } | |
12263 | ||
0e1862bb | 12264 | if (bfd_link_relocatable (info)) |
c152c796 AM |
12265 | { |
12266 | bfd_boolean failed = FALSE; | |
12267 | ||
12268 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
12269 | if (failed) | |
12270 | goto error_return; | |
12271 | } | |
12272 | ||
12273 | /* If we have optimized stabs strings, output them. */ | |
64f52338 | 12274 | if (htab->stab_info.stabstr != NULL) |
c152c796 | 12275 | { |
64f52338 | 12276 | if (!_bfd_write_stab_strings (abfd, &htab->stab_info)) |
c152c796 AM |
12277 | goto error_return; |
12278 | } | |
12279 | ||
9f7c3e5e AM |
12280 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) |
12281 | goto error_return; | |
c152c796 | 12282 | |
9f7c3e5e | 12283 | elf_final_link_free (abfd, &flinfo); |
c152c796 | 12284 | |
12bd6957 | 12285 | elf_linker (abfd) = TRUE; |
c152c796 | 12286 | |
104d59d1 JM |
12287 | if (attr_section) |
12288 | { | |
a50b1753 | 12289 | bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); |
104d59d1 | 12290 | if (contents == NULL) |
d0f16d5e | 12291 | return FALSE; /* Bail out and fail. */ |
104d59d1 JM |
12292 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
12293 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
12294 | free (contents); | |
12295 | } | |
12296 | ||
c152c796 AM |
12297 | return TRUE; |
12298 | ||
12299 | error_return: | |
9f7c3e5e | 12300 | elf_final_link_free (abfd, &flinfo); |
c152c796 AM |
12301 | return FALSE; |
12302 | } | |
12303 | \f | |
5241d853 RS |
12304 | /* Initialize COOKIE for input bfd ABFD. */ |
12305 | ||
12306 | static bfd_boolean | |
12307 | init_reloc_cookie (struct elf_reloc_cookie *cookie, | |
12308 | struct bfd_link_info *info, bfd *abfd) | |
12309 | { | |
12310 | Elf_Internal_Shdr *symtab_hdr; | |
12311 | const struct elf_backend_data *bed; | |
12312 | ||
12313 | bed = get_elf_backend_data (abfd); | |
12314 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12315 | ||
12316 | cookie->abfd = abfd; | |
12317 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
12318 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
12319 | if (cookie->bad_symtab) | |
12320 | { | |
12321 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
12322 | cookie->extsymoff = 0; | |
12323 | } | |
12324 | else | |
12325 | { | |
12326 | cookie->locsymcount = symtab_hdr->sh_info; | |
12327 | cookie->extsymoff = symtab_hdr->sh_info; | |
12328 | } | |
12329 | ||
12330 | if (bed->s->arch_size == 32) | |
12331 | cookie->r_sym_shift = 8; | |
12332 | else | |
12333 | cookie->r_sym_shift = 32; | |
12334 | ||
12335 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
12336 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
12337 | { | |
12338 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
12339 | cookie->locsymcount, 0, | |
12340 | NULL, NULL, NULL); | |
12341 | if (cookie->locsyms == NULL) | |
12342 | { | |
12343 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
12344 | return FALSE; | |
12345 | } | |
12346 | if (info->keep_memory) | |
12347 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
12348 | } | |
12349 | return TRUE; | |
12350 | } | |
12351 | ||
12352 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
12353 | ||
12354 | static void | |
12355 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
12356 | { | |
12357 | Elf_Internal_Shdr *symtab_hdr; | |
12358 | ||
12359 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12360 | if (cookie->locsyms != NULL | |
12361 | && symtab_hdr->contents != (unsigned char *) cookie->locsyms) | |
12362 | free (cookie->locsyms); | |
12363 | } | |
12364 | ||
12365 | /* Initialize the relocation information in COOKIE for input section SEC | |
12366 | of input bfd ABFD. */ | |
12367 | ||
12368 | static bfd_boolean | |
12369 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12370 | struct bfd_link_info *info, bfd *abfd, | |
12371 | asection *sec) | |
12372 | { | |
12373 | const struct elf_backend_data *bed; | |
12374 | ||
12375 | if (sec->reloc_count == 0) | |
12376 | { | |
12377 | cookie->rels = NULL; | |
12378 | cookie->relend = NULL; | |
12379 | } | |
12380 | else | |
12381 | { | |
12382 | bed = get_elf_backend_data (abfd); | |
12383 | ||
12384 | cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
12385 | info->keep_memory); | |
12386 | if (cookie->rels == NULL) | |
12387 | return FALSE; | |
12388 | cookie->rel = cookie->rels; | |
12389 | cookie->relend = (cookie->rels | |
12390 | + sec->reloc_count * bed->s->int_rels_per_ext_rel); | |
12391 | } | |
12392 | cookie->rel = cookie->rels; | |
12393 | return TRUE; | |
12394 | } | |
12395 | ||
12396 | /* Free the memory allocated by init_reloc_cookie_rels, | |
12397 | if appropriate. */ | |
12398 | ||
12399 | static void | |
12400 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12401 | asection *sec) | |
12402 | { | |
12403 | if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) | |
12404 | free (cookie->rels); | |
12405 | } | |
12406 | ||
12407 | /* Initialize the whole of COOKIE for input section SEC. */ | |
12408 | ||
12409 | static bfd_boolean | |
12410 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12411 | struct bfd_link_info *info, | |
12412 | asection *sec) | |
12413 | { | |
12414 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
12415 | goto error1; | |
12416 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
12417 | goto error2; | |
12418 | return TRUE; | |
12419 | ||
12420 | error2: | |
12421 | fini_reloc_cookie (cookie, sec->owner); | |
12422 | error1: | |
12423 | return FALSE; | |
12424 | } | |
12425 | ||
12426 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
12427 | if appropriate. */ | |
12428 | ||
12429 | static void | |
12430 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12431 | asection *sec) | |
12432 | { | |
12433 | fini_reloc_cookie_rels (cookie, sec); | |
12434 | fini_reloc_cookie (cookie, sec->owner); | |
12435 | } | |
12436 | \f | |
c152c796 AM |
12437 | /* Garbage collect unused sections. */ |
12438 | ||
07adf181 AM |
12439 | /* Default gc_mark_hook. */ |
12440 | ||
12441 | asection * | |
12442 | _bfd_elf_gc_mark_hook (asection *sec, | |
12443 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12444 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
12445 | struct elf_link_hash_entry *h, | |
12446 | Elf_Internal_Sym *sym) | |
12447 | { | |
12448 | if (h != NULL) | |
12449 | { | |
12450 | switch (h->root.type) | |
12451 | { | |
12452 | case bfd_link_hash_defined: | |
12453 | case bfd_link_hash_defweak: | |
12454 | return h->root.u.def.section; | |
12455 | ||
12456 | case bfd_link_hash_common: | |
12457 | return h->root.u.c.p->section; | |
12458 | ||
12459 | default: | |
12460 | break; | |
12461 | } | |
12462 | } | |
12463 | else | |
12464 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
12465 | ||
12466 | return NULL; | |
12467 | } | |
12468 | ||
a6a4679f AM |
12469 | /* For undefined __start_<name> and __stop_<name> symbols, return the |
12470 | first input section matching <name>. Return NULL otherwise. */ | |
12471 | ||
12472 | asection * | |
12473 | _bfd_elf_is_start_stop (const struct bfd_link_info *info, | |
12474 | struct elf_link_hash_entry *h) | |
12475 | { | |
12476 | asection *s; | |
12477 | const char *sec_name; | |
12478 | ||
12479 | if (h->root.type != bfd_link_hash_undefined | |
12480 | && h->root.type != bfd_link_hash_undefweak) | |
12481 | return NULL; | |
12482 | ||
12483 | s = h->root.u.undef.section; | |
12484 | if (s != NULL) | |
12485 | { | |
12486 | if (s == (asection *) 0 - 1) | |
12487 | return NULL; | |
12488 | return s; | |
12489 | } | |
12490 | ||
12491 | sec_name = NULL; | |
12492 | if (strncmp (h->root.root.string, "__start_", 8) == 0) | |
12493 | sec_name = h->root.root.string + 8; | |
12494 | else if (strncmp (h->root.root.string, "__stop_", 7) == 0) | |
12495 | sec_name = h->root.root.string + 7; | |
12496 | ||
12497 | if (sec_name != NULL && *sec_name != '\0') | |
12498 | { | |
12499 | bfd *i; | |
12500 | ||
12501 | for (i = info->input_bfds; i != NULL; i = i->link.next) | |
12502 | { | |
12503 | s = bfd_get_section_by_name (i, sec_name); | |
12504 | if (s != NULL) | |
12505 | { | |
12506 | h->root.u.undef.section = s; | |
12507 | break; | |
12508 | } | |
12509 | } | |
12510 | } | |
12511 | ||
12512 | if (s == NULL) | |
12513 | h->root.u.undef.section = (asection *) 0 - 1; | |
12514 | ||
12515 | return s; | |
12516 | } | |
12517 | ||
5241d853 RS |
12518 | /* COOKIE->rel describes a relocation against section SEC, which is |
12519 | a section we've decided to keep. Return the section that contains | |
12520 | the relocation symbol, or NULL if no section contains it. */ | |
12521 | ||
12522 | asection * | |
12523 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
12524 | elf_gc_mark_hook_fn gc_mark_hook, | |
1cce69b9 AM |
12525 | struct elf_reloc_cookie *cookie, |
12526 | bfd_boolean *start_stop) | |
5241d853 RS |
12527 | { |
12528 | unsigned long r_symndx; | |
12529 | struct elf_link_hash_entry *h; | |
12530 | ||
12531 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
cf35638d | 12532 | if (r_symndx == STN_UNDEF) |
5241d853 RS |
12533 | return NULL; |
12534 | ||
12535 | if (r_symndx >= cookie->locsymcount | |
12536 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
12537 | { | |
12538 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
263ddf68 L |
12539 | if (h == NULL) |
12540 | { | |
12541 | info->callbacks->einfo (_("%F%P: corrupt input: %B\n"), | |
12542 | sec->owner); | |
12543 | return NULL; | |
12544 | } | |
5241d853 RS |
12545 | while (h->root.type == bfd_link_hash_indirect |
12546 | || h->root.type == bfd_link_hash_warning) | |
12547 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1d5316ab | 12548 | h->mark = 1; |
4e6b54a6 AM |
12549 | /* If this symbol is weak and there is a non-weak definition, we |
12550 | keep the non-weak definition because many backends put | |
12551 | dynamic reloc info on the non-weak definition for code | |
12552 | handling copy relocs. */ | |
12553 | if (h->u.weakdef != NULL) | |
12554 | h->u.weakdef->mark = 1; | |
1cce69b9 | 12555 | |
a6a4679f | 12556 | if (start_stop != NULL) |
1cce69b9 AM |
12557 | { |
12558 | /* To work around a glibc bug, mark all XXX input sections | |
12559 | when there is an as yet undefined reference to __start_XXX | |
12560 | or __stop_XXX symbols. The linker will later define such | |
12561 | symbols for orphan input sections that have a name | |
12562 | representable as a C identifier. */ | |
a6a4679f | 12563 | asection *s = _bfd_elf_is_start_stop (info, h); |
1cce69b9 | 12564 | |
a6a4679f | 12565 | if (s != NULL) |
1cce69b9 | 12566 | { |
a6a4679f AM |
12567 | *start_stop = !s->gc_mark; |
12568 | return s; | |
1cce69b9 AM |
12569 | } |
12570 | } | |
12571 | ||
5241d853 RS |
12572 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); |
12573 | } | |
12574 | ||
12575 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
12576 | &cookie->locsyms[r_symndx]); | |
12577 | } | |
12578 | ||
12579 | /* COOKIE->rel describes a relocation against section SEC, which is | |
12580 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 12581 | the relocation symbol. */ |
5241d853 RS |
12582 | |
12583 | bfd_boolean | |
12584 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, | |
12585 | asection *sec, | |
12586 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 12587 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
12588 | { |
12589 | asection *rsec; | |
1cce69b9 | 12590 | bfd_boolean start_stop = FALSE; |
5241d853 | 12591 | |
1cce69b9 AM |
12592 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); |
12593 | while (rsec != NULL) | |
5241d853 | 12594 | { |
1cce69b9 AM |
12595 | if (!rsec->gc_mark) |
12596 | { | |
12597 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour | |
12598 | || (rsec->owner->flags & DYNAMIC) != 0) | |
12599 | rsec->gc_mark = 1; | |
12600 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) | |
12601 | return FALSE; | |
12602 | } | |
12603 | if (!start_stop) | |
12604 | break; | |
199af150 | 12605 | rsec = bfd_get_next_section_by_name (rsec->owner, rsec); |
5241d853 RS |
12606 | } |
12607 | return TRUE; | |
12608 | } | |
12609 | ||
07adf181 AM |
12610 | /* The mark phase of garbage collection. For a given section, mark |
12611 | it and any sections in this section's group, and all the sections | |
12612 | which define symbols to which it refers. */ | |
12613 | ||
ccfa59ea AM |
12614 | bfd_boolean |
12615 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
12616 | asection *sec, | |
6a5bb875 | 12617 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
12618 | { |
12619 | bfd_boolean ret; | |
9d0a14d3 | 12620 | asection *group_sec, *eh_frame; |
c152c796 AM |
12621 | |
12622 | sec->gc_mark = 1; | |
12623 | ||
12624 | /* Mark all the sections in the group. */ | |
12625 | group_sec = elf_section_data (sec)->next_in_group; | |
12626 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 12627 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
12628 | return FALSE; |
12629 | ||
12630 | /* Look through the section relocs. */ | |
12631 | ret = TRUE; | |
9d0a14d3 RS |
12632 | eh_frame = elf_eh_frame_section (sec->owner); |
12633 | if ((sec->flags & SEC_RELOC) != 0 | |
12634 | && sec->reloc_count > 0 | |
12635 | && sec != eh_frame) | |
c152c796 | 12636 | { |
5241d853 | 12637 | struct elf_reloc_cookie cookie; |
c152c796 | 12638 | |
5241d853 RS |
12639 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
12640 | ret = FALSE; | |
c152c796 | 12641 | else |
c152c796 | 12642 | { |
5241d853 | 12643 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 12644 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 RS |
12645 | { |
12646 | ret = FALSE; | |
12647 | break; | |
12648 | } | |
12649 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
12650 | } |
12651 | } | |
9d0a14d3 RS |
12652 | |
12653 | if (ret && eh_frame && elf_fde_list (sec)) | |
12654 | { | |
12655 | struct elf_reloc_cookie cookie; | |
12656 | ||
12657 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
12658 | ret = FALSE; | |
12659 | else | |
12660 | { | |
12661 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
12662 | gc_mark_hook, &cookie)) | |
12663 | ret = FALSE; | |
12664 | fini_reloc_cookie_for_section (&cookie, eh_frame); | |
12665 | } | |
12666 | } | |
12667 | ||
2f0c68f2 CM |
12668 | eh_frame = elf_section_eh_frame_entry (sec); |
12669 | if (ret && eh_frame && !eh_frame->gc_mark) | |
12670 | if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) | |
12671 | ret = FALSE; | |
12672 | ||
c152c796 AM |
12673 | return ret; |
12674 | } | |
12675 | ||
3c758495 TG |
12676 | /* Scan and mark sections in a special or debug section group. */ |
12677 | ||
12678 | static void | |
12679 | _bfd_elf_gc_mark_debug_special_section_group (asection *grp) | |
12680 | { | |
12681 | /* Point to first section of section group. */ | |
12682 | asection *ssec; | |
12683 | /* Used to iterate the section group. */ | |
12684 | asection *msec; | |
12685 | ||
12686 | bfd_boolean is_special_grp = TRUE; | |
12687 | bfd_boolean is_debug_grp = TRUE; | |
12688 | ||
12689 | /* First scan to see if group contains any section other than debug | |
12690 | and special section. */ | |
12691 | ssec = msec = elf_next_in_group (grp); | |
12692 | do | |
12693 | { | |
12694 | if ((msec->flags & SEC_DEBUGGING) == 0) | |
12695 | is_debug_grp = FALSE; | |
12696 | ||
12697 | if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) | |
12698 | is_special_grp = FALSE; | |
12699 | ||
12700 | msec = elf_next_in_group (msec); | |
12701 | } | |
12702 | while (msec != ssec); | |
12703 | ||
12704 | /* If this is a pure debug section group or pure special section group, | |
12705 | keep all sections in this group. */ | |
12706 | if (is_debug_grp || is_special_grp) | |
12707 | { | |
12708 | do | |
12709 | { | |
12710 | msec->gc_mark = 1; | |
12711 | msec = elf_next_in_group (msec); | |
12712 | } | |
12713 | while (msec != ssec); | |
12714 | } | |
12715 | } | |
12716 | ||
7f6ab9f8 AM |
12717 | /* Keep debug and special sections. */ |
12718 | ||
12719 | bfd_boolean | |
12720 | _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, | |
12721 | elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) | |
12722 | { | |
12723 | bfd *ibfd; | |
12724 | ||
c72f2fb2 | 12725 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
7f6ab9f8 AM |
12726 | { |
12727 | asection *isec; | |
12728 | bfd_boolean some_kept; | |
b40bf0a2 | 12729 | bfd_boolean debug_frag_seen; |
7f6ab9f8 AM |
12730 | |
12731 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
12732 | continue; | |
12733 | ||
b40bf0a2 NC |
12734 | /* Ensure all linker created sections are kept, |
12735 | see if any other section is already marked, | |
12736 | and note if we have any fragmented debug sections. */ | |
12737 | debug_frag_seen = some_kept = FALSE; | |
7f6ab9f8 AM |
12738 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
12739 | { | |
12740 | if ((isec->flags & SEC_LINKER_CREATED) != 0) | |
12741 | isec->gc_mark = 1; | |
12742 | else if (isec->gc_mark) | |
12743 | some_kept = TRUE; | |
b40bf0a2 NC |
12744 | |
12745 | if (debug_frag_seen == FALSE | |
12746 | && (isec->flags & SEC_DEBUGGING) | |
12747 | && CONST_STRNEQ (isec->name, ".debug_line.")) | |
12748 | debug_frag_seen = TRUE; | |
7f6ab9f8 AM |
12749 | } |
12750 | ||
12751 | /* If no section in this file will be kept, then we can | |
b40bf0a2 | 12752 | toss out the debug and special sections. */ |
7f6ab9f8 AM |
12753 | if (!some_kept) |
12754 | continue; | |
12755 | ||
12756 | /* Keep debug and special sections like .comment when they are | |
3c758495 TG |
12757 | not part of a group. Also keep section groups that contain |
12758 | just debug sections or special sections. */ | |
7f6ab9f8 | 12759 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
3c758495 TG |
12760 | { |
12761 | if ((isec->flags & SEC_GROUP) != 0) | |
12762 | _bfd_elf_gc_mark_debug_special_section_group (isec); | |
12763 | else if (((isec->flags & SEC_DEBUGGING) != 0 | |
12764 | || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) | |
12765 | && elf_next_in_group (isec) == NULL) | |
12766 | isec->gc_mark = 1; | |
12767 | } | |
b40bf0a2 NC |
12768 | |
12769 | if (! debug_frag_seen) | |
12770 | continue; | |
12771 | ||
12772 | /* Look for CODE sections which are going to be discarded, | |
12773 | and find and discard any fragmented debug sections which | |
12774 | are associated with that code section. */ | |
12775 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
12776 | if ((isec->flags & SEC_CODE) != 0 | |
12777 | && isec->gc_mark == 0) | |
12778 | { | |
12779 | unsigned int ilen; | |
12780 | asection *dsec; | |
12781 | ||
12782 | ilen = strlen (isec->name); | |
12783 | ||
12784 | /* Association is determined by the name of the debug section | |
12785 | containing the name of the code section as a suffix. For | |
12786 | example .debug_line.text.foo is a debug section associated | |
12787 | with .text.foo. */ | |
12788 | for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) | |
12789 | { | |
12790 | unsigned int dlen; | |
12791 | ||
12792 | if (dsec->gc_mark == 0 | |
12793 | || (dsec->flags & SEC_DEBUGGING) == 0) | |
12794 | continue; | |
12795 | ||
12796 | dlen = strlen (dsec->name); | |
12797 | ||
12798 | if (dlen > ilen | |
12799 | && strncmp (dsec->name + (dlen - ilen), | |
12800 | isec->name, ilen) == 0) | |
12801 | { | |
12802 | dsec->gc_mark = 0; | |
b40bf0a2 NC |
12803 | } |
12804 | } | |
12805 | } | |
7f6ab9f8 AM |
12806 | } |
12807 | return TRUE; | |
12808 | } | |
12809 | ||
c152c796 AM |
12810 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ |
12811 | ||
c17d87de NC |
12812 | struct elf_gc_sweep_symbol_info |
12813 | { | |
ccabcbe5 AM |
12814 | struct bfd_link_info *info; |
12815 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
12816 | bfd_boolean); | |
12817 | }; | |
12818 | ||
c152c796 | 12819 | static bfd_boolean |
ccabcbe5 | 12820 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) |
c152c796 | 12821 | { |
1d5316ab AM |
12822 | if (!h->mark |
12823 | && (((h->root.type == bfd_link_hash_defined | |
12824 | || h->root.type == bfd_link_hash_defweak) | |
c4621b33 | 12825 | && !((h->def_regular || ELF_COMMON_DEF_P (h)) |
6673f753 | 12826 | && h->root.u.def.section->gc_mark)) |
1d5316ab AM |
12827 | || h->root.type == bfd_link_hash_undefined |
12828 | || h->root.type == bfd_link_hash_undefweak)) | |
12829 | { | |
12830 | struct elf_gc_sweep_symbol_info *inf; | |
12831 | ||
12832 | inf = (struct elf_gc_sweep_symbol_info *) data; | |
ccabcbe5 | 12833 | (*inf->hide_symbol) (inf->info, h, TRUE); |
1d5316ab AM |
12834 | h->def_regular = 0; |
12835 | h->ref_regular = 0; | |
12836 | h->ref_regular_nonweak = 0; | |
ccabcbe5 | 12837 | } |
c152c796 AM |
12838 | |
12839 | return TRUE; | |
12840 | } | |
12841 | ||
12842 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
12843 | ||
12844 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
12845 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
12846 | ||
12847 | static bfd_boolean | |
ccabcbe5 | 12848 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
12849 | { |
12850 | bfd *sub; | |
ccabcbe5 AM |
12851 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
12852 | gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; | |
12853 | unsigned long section_sym_count; | |
12854 | struct elf_gc_sweep_symbol_info sweep_info; | |
c152c796 | 12855 | |
c72f2fb2 | 12856 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
12857 | { |
12858 | asection *o; | |
12859 | ||
b19a8f85 L |
12860 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
12861 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) | |
c152c796 AM |
12862 | continue; |
12863 | ||
12864 | for (o = sub->sections; o != NULL; o = o->next) | |
12865 | { | |
a33dafc3 L |
12866 | /* When any section in a section group is kept, we keep all |
12867 | sections in the section group. If the first member of | |
12868 | the section group is excluded, we will also exclude the | |
12869 | group section. */ | |
12870 | if (o->flags & SEC_GROUP) | |
12871 | { | |
12872 | asection *first = elf_next_in_group (o); | |
12873 | o->gc_mark = first->gc_mark; | |
12874 | } | |
c152c796 | 12875 | |
1e7eae0d | 12876 | if (o->gc_mark) |
c152c796 AM |
12877 | continue; |
12878 | ||
12879 | /* Skip sweeping sections already excluded. */ | |
12880 | if (o->flags & SEC_EXCLUDE) | |
12881 | continue; | |
12882 | ||
12883 | /* Since this is early in the link process, it is simple | |
12884 | to remove a section from the output. */ | |
12885 | o->flags |= SEC_EXCLUDE; | |
12886 | ||
c55fe096 | 12887 | if (info->print_gc_sections && o->size != 0) |
695344c0 | 12888 | /* xgettext:c-format */ |
c17d87de NC |
12889 | _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name); |
12890 | ||
c152c796 AM |
12891 | /* But we also have to update some of the relocation |
12892 | info we collected before. */ | |
12893 | if (gc_sweep_hook | |
e8aaee2a | 12894 | && (o->flags & SEC_RELOC) != 0 |
9850436d AM |
12895 | && o->reloc_count != 0 |
12896 | && !((info->strip == strip_all || info->strip == strip_debugger) | |
12897 | && (o->flags & SEC_DEBUGGING) != 0) | |
e8aaee2a | 12898 | && !bfd_is_abs_section (o->output_section)) |
c152c796 AM |
12899 | { |
12900 | Elf_Internal_Rela *internal_relocs; | |
12901 | bfd_boolean r; | |
12902 | ||
12903 | internal_relocs | |
12904 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
12905 | info->keep_memory); | |
12906 | if (internal_relocs == NULL) | |
12907 | return FALSE; | |
12908 | ||
12909 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
12910 | ||
12911 | if (elf_section_data (o)->relocs != internal_relocs) | |
12912 | free (internal_relocs); | |
12913 | ||
12914 | if (!r) | |
12915 | return FALSE; | |
12916 | } | |
12917 | } | |
12918 | } | |
12919 | ||
12920 | /* Remove the symbols that were in the swept sections from the dynamic | |
12921 | symbol table. GCFIXME: Anyone know how to get them out of the | |
12922 | static symbol table as well? */ | |
ccabcbe5 AM |
12923 | sweep_info.info = info; |
12924 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
12925 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
12926 | &sweep_info); | |
c152c796 | 12927 | |
ccabcbe5 | 12928 | _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); |
c152c796 AM |
12929 | return TRUE; |
12930 | } | |
12931 | ||
12932 | /* Propagate collected vtable information. This is called through | |
12933 | elf_link_hash_traverse. */ | |
12934 | ||
12935 | static bfd_boolean | |
12936 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
12937 | { | |
c152c796 | 12938 | /* Those that are not vtables. */ |
f6e332e6 | 12939 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
12940 | return TRUE; |
12941 | ||
12942 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 12943 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
12944 | return TRUE; |
12945 | ||
12946 | /* If we've already been done, exit. */ | |
f6e332e6 | 12947 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
12948 | return TRUE; |
12949 | ||
12950 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 12951 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 12952 | |
f6e332e6 | 12953 | if (h->vtable->used == NULL) |
c152c796 AM |
12954 | { |
12955 | /* None of this table's entries were referenced. Re-use the | |
12956 | parent's table. */ | |
f6e332e6 AM |
12957 | h->vtable->used = h->vtable->parent->vtable->used; |
12958 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
12959 | } |
12960 | else | |
12961 | { | |
12962 | size_t n; | |
12963 | bfd_boolean *cu, *pu; | |
12964 | ||
12965 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 12966 | cu = h->vtable->used; |
c152c796 | 12967 | cu[-1] = TRUE; |
f6e332e6 | 12968 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
12969 | if (pu != NULL) |
12970 | { | |
12971 | const struct elf_backend_data *bed; | |
12972 | unsigned int log_file_align; | |
12973 | ||
12974 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
12975 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 12976 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
12977 | while (n--) |
12978 | { | |
12979 | if (*pu) | |
12980 | *cu = TRUE; | |
12981 | pu++; | |
12982 | cu++; | |
12983 | } | |
12984 | } | |
12985 | } | |
12986 | ||
12987 | return TRUE; | |
12988 | } | |
12989 | ||
12990 | static bfd_boolean | |
12991 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
12992 | { | |
12993 | asection *sec; | |
12994 | bfd_vma hstart, hend; | |
12995 | Elf_Internal_Rela *relstart, *relend, *rel; | |
12996 | const struct elf_backend_data *bed; | |
12997 | unsigned int log_file_align; | |
12998 | ||
c152c796 AM |
12999 | /* Take care of both those symbols that do not describe vtables as |
13000 | well as those that are not loaded. */ | |
f6e332e6 | 13001 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
13002 | return TRUE; |
13003 | ||
13004 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
13005 | || h->root.type == bfd_link_hash_defweak); | |
13006 | ||
13007 | sec = h->root.u.def.section; | |
13008 | hstart = h->root.u.def.value; | |
13009 | hend = hstart + h->size; | |
13010 | ||
13011 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
13012 | if (!relstart) | |
13013 | return *(bfd_boolean *) okp = FALSE; | |
13014 | bed = get_elf_backend_data (sec->owner); | |
13015 | log_file_align = bed->s->log_file_align; | |
13016 | ||
13017 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
13018 | ||
13019 | for (rel = relstart; rel < relend; ++rel) | |
13020 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
13021 | { | |
13022 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
13023 | if (h->vtable->used |
13024 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
13025 | { |
13026 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 13027 | if (h->vtable->used[entry]) |
c152c796 AM |
13028 | continue; |
13029 | } | |
13030 | /* Otherwise, kill it. */ | |
13031 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
13032 | } | |
13033 | ||
13034 | return TRUE; | |
13035 | } | |
13036 | ||
87538722 AM |
13037 | /* Mark sections containing dynamically referenced symbols. When |
13038 | building shared libraries, we must assume that any visible symbol is | |
13039 | referenced. */ | |
715df9b8 | 13040 | |
64d03ab5 AM |
13041 | bfd_boolean |
13042 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 13043 | { |
87538722 | 13044 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
d6f6f455 | 13045 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
87538722 | 13046 | |
715df9b8 EB |
13047 | if ((h->root.type == bfd_link_hash_defined |
13048 | || h->root.type == bfd_link_hash_defweak) | |
87538722 | 13049 | && (h->ref_dynamic |
c4621b33 | 13050 | || ((h->def_regular || ELF_COMMON_DEF_P (h)) |
87538722 | 13051 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL |
fd91d419 | 13052 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN |
0e1862bb | 13053 | && (!bfd_link_executable (info) |
b407645f AM |
13054 | || info->export_dynamic |
13055 | || (h->dynamic | |
13056 | && d != NULL | |
13057 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
422f1182 | 13058 | && (h->versioned >= versioned |
54e8959c L |
13059 | || !bfd_hide_sym_by_version (info->version_info, |
13060 | h->root.root.string))))) | |
715df9b8 EB |
13061 | h->root.u.def.section->flags |= SEC_KEEP; |
13062 | ||
13063 | return TRUE; | |
13064 | } | |
3b36f7e6 | 13065 | |
74f0fb50 AM |
13066 | /* Keep all sections containing symbols undefined on the command-line, |
13067 | and the section containing the entry symbol. */ | |
13068 | ||
13069 | void | |
13070 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
13071 | { | |
13072 | struct bfd_sym_chain *sym; | |
13073 | ||
13074 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
13075 | { | |
13076 | struct elf_link_hash_entry *h; | |
13077 | ||
13078 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
13079 | FALSE, FALSE, FALSE); | |
13080 | ||
13081 | if (h != NULL | |
13082 | && (h->root.type == bfd_link_hash_defined | |
13083 | || h->root.type == bfd_link_hash_defweak) | |
f02cb058 AM |
13084 | && !bfd_is_abs_section (h->root.u.def.section) |
13085 | && !bfd_is_und_section (h->root.u.def.section)) | |
74f0fb50 AM |
13086 | h->root.u.def.section->flags |= SEC_KEEP; |
13087 | } | |
13088 | } | |
13089 | ||
2f0c68f2 CM |
13090 | bfd_boolean |
13091 | bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, | |
13092 | struct bfd_link_info *info) | |
13093 | { | |
13094 | bfd *ibfd = info->input_bfds; | |
13095 | ||
13096 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) | |
13097 | { | |
13098 | asection *sec; | |
13099 | struct elf_reloc_cookie cookie; | |
13100 | ||
13101 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13102 | continue; | |
13103 | ||
13104 | if (!init_reloc_cookie (&cookie, info, ibfd)) | |
13105 | return FALSE; | |
13106 | ||
13107 | for (sec = ibfd->sections; sec; sec = sec->next) | |
13108 | { | |
13109 | if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry") | |
13110 | && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) | |
13111 | { | |
13112 | _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); | |
13113 | fini_reloc_cookie_rels (&cookie, sec); | |
13114 | } | |
13115 | } | |
13116 | } | |
13117 | return TRUE; | |
13118 | } | |
13119 | ||
c152c796 AM |
13120 | /* Do mark and sweep of unused sections. */ |
13121 | ||
13122 | bfd_boolean | |
13123 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
13124 | { | |
13125 | bfd_boolean ok = TRUE; | |
13126 | bfd *sub; | |
6a5bb875 | 13127 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 13128 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
da44f4e5 | 13129 | struct elf_link_hash_table *htab; |
c152c796 | 13130 | |
64d03ab5 | 13131 | if (!bed->can_gc_sections |
715df9b8 | 13132 | || !is_elf_hash_table (info->hash)) |
c152c796 | 13133 | { |
4eca0228 | 13134 | _bfd_error_handler(_("Warning: gc-sections option ignored")); |
c152c796 AM |
13135 | return TRUE; |
13136 | } | |
13137 | ||
74f0fb50 | 13138 | bed->gc_keep (info); |
da44f4e5 | 13139 | htab = elf_hash_table (info); |
74f0fb50 | 13140 | |
9d0a14d3 RS |
13141 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
13142 | at the .eh_frame section if we can mark the FDEs individually. */ | |
2f0c68f2 CM |
13143 | for (sub = info->input_bfds; |
13144 | info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; | |
13145 | sub = sub->link.next) | |
9d0a14d3 RS |
13146 | { |
13147 | asection *sec; | |
13148 | struct elf_reloc_cookie cookie; | |
13149 | ||
13150 | sec = bfd_get_section_by_name (sub, ".eh_frame"); | |
9a2a56cc | 13151 | while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) |
9d0a14d3 RS |
13152 | { |
13153 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
9a2a56cc AM |
13154 | if (elf_section_data (sec)->sec_info |
13155 | && (sec->flags & SEC_LINKER_CREATED) == 0) | |
9d0a14d3 RS |
13156 | elf_eh_frame_section (sub) = sec; |
13157 | fini_reloc_cookie_for_section (&cookie, sec); | |
199af150 | 13158 | sec = bfd_get_next_section_by_name (NULL, sec); |
9d0a14d3 RS |
13159 | } |
13160 | } | |
9d0a14d3 | 13161 | |
c152c796 | 13162 | /* Apply transitive closure to the vtable entry usage info. */ |
da44f4e5 | 13163 | elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); |
c152c796 AM |
13164 | if (!ok) |
13165 | return FALSE; | |
13166 | ||
13167 | /* Kill the vtable relocations that were not used. */ | |
da44f4e5 | 13168 | elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok); |
c152c796 AM |
13169 | if (!ok) |
13170 | return FALSE; | |
13171 | ||
715df9b8 | 13172 | /* Mark dynamically referenced symbols. */ |
da44f4e5 AM |
13173 | if (htab->dynamic_sections_created) |
13174 | elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); | |
c152c796 | 13175 | |
715df9b8 | 13176 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 13177 | gc_mark_hook = bed->gc_mark_hook; |
c72f2fb2 | 13178 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13179 | { |
13180 | asection *o; | |
13181 | ||
b19a8f85 L |
13182 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
13183 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) | |
c152c796 AM |
13184 | continue; |
13185 | ||
7f6ab9f8 AM |
13186 | /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). |
13187 | Also treat note sections as a root, if the section is not part | |
13188 | of a group. */ | |
c152c796 | 13189 | for (o = sub->sections; o != NULL; o = o->next) |
7f6ab9f8 AM |
13190 | if (!o->gc_mark |
13191 | && (o->flags & SEC_EXCLUDE) == 0 | |
24007750 | 13192 | && ((o->flags & SEC_KEEP) != 0 |
7f6ab9f8 AM |
13193 | || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE |
13194 | && elf_next_in_group (o) == NULL ))) | |
13195 | { | |
13196 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
13197 | return FALSE; | |
13198 | } | |
c152c796 AM |
13199 | } |
13200 | ||
6a5bb875 | 13201 | /* Allow the backend to mark additional target specific sections. */ |
7f6ab9f8 | 13202 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 13203 | |
c152c796 | 13204 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 13205 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
13206 | } |
13207 | \f | |
13208 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
13209 | ||
13210 | bfd_boolean | |
13211 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
13212 | asection *sec, | |
13213 | struct elf_link_hash_entry *h, | |
13214 | bfd_vma offset) | |
13215 | { | |
13216 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
13217 | struct elf_link_hash_entry **search, *child; | |
ef53be89 | 13218 | size_t extsymcount; |
c152c796 AM |
13219 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
13220 | ||
13221 | /* The sh_info field of the symtab header tells us where the | |
13222 | external symbols start. We don't care about the local symbols at | |
13223 | this point. */ | |
13224 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
13225 | if (!elf_bad_symtab (abfd)) | |
13226 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
13227 | ||
13228 | sym_hashes = elf_sym_hashes (abfd); | |
13229 | sym_hashes_end = sym_hashes + extsymcount; | |
13230 | ||
13231 | /* Hunt down the child symbol, which is in this section at the same | |
13232 | offset as the relocation. */ | |
13233 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
13234 | { | |
13235 | if ((child = *search) != NULL | |
13236 | && (child->root.type == bfd_link_hash_defined | |
13237 | || child->root.type == bfd_link_hash_defweak) | |
13238 | && child->root.u.def.section == sec | |
13239 | && child->root.u.def.value == offset) | |
13240 | goto win; | |
13241 | } | |
13242 | ||
695344c0 NC |
13243 | /* xgettext:c-format */ |
13244 | _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"), | |
4eca0228 | 13245 | abfd, sec, (unsigned long) offset); |
c152c796 AM |
13246 | bfd_set_error (bfd_error_invalid_operation); |
13247 | return FALSE; | |
13248 | ||
13249 | win: | |
f6e332e6 AM |
13250 | if (!child->vtable) |
13251 | { | |
ca4be51c AM |
13252 | child->vtable = ((struct elf_link_virtual_table_entry *) |
13253 | bfd_zalloc (abfd, sizeof (*child->vtable))); | |
f6e332e6 AM |
13254 | if (!child->vtable) |
13255 | return FALSE; | |
13256 | } | |
c152c796 AM |
13257 | if (!h) |
13258 | { | |
13259 | /* This *should* only be the absolute section. It could potentially | |
13260 | be that someone has defined a non-global vtable though, which | |
13261 | would be bad. It isn't worth paging in the local symbols to be | |
13262 | sure though; that case should simply be handled by the assembler. */ | |
13263 | ||
f6e332e6 | 13264 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
13265 | } |
13266 | else | |
f6e332e6 | 13267 | child->vtable->parent = h; |
c152c796 AM |
13268 | |
13269 | return TRUE; | |
13270 | } | |
13271 | ||
13272 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
13273 | ||
13274 | bfd_boolean | |
13275 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
13276 | asection *sec ATTRIBUTE_UNUSED, | |
13277 | struct elf_link_hash_entry *h, | |
13278 | bfd_vma addend) | |
13279 | { | |
13280 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13281 | unsigned int log_file_align = bed->s->log_file_align; | |
13282 | ||
f6e332e6 AM |
13283 | if (!h->vtable) |
13284 | { | |
ca4be51c AM |
13285 | h->vtable = ((struct elf_link_virtual_table_entry *) |
13286 | bfd_zalloc (abfd, sizeof (*h->vtable))); | |
f6e332e6 AM |
13287 | if (!h->vtable) |
13288 | return FALSE; | |
13289 | } | |
13290 | ||
13291 | if (addend >= h->vtable->size) | |
c152c796 AM |
13292 | { |
13293 | size_t size, bytes, file_align; | |
f6e332e6 | 13294 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
13295 | |
13296 | /* While the symbol is undefined, we have to be prepared to handle | |
13297 | a zero size. */ | |
13298 | file_align = 1 << log_file_align; | |
13299 | if (h->root.type == bfd_link_hash_undefined) | |
13300 | size = addend + file_align; | |
13301 | else | |
13302 | { | |
13303 | size = h->size; | |
13304 | if (addend >= size) | |
13305 | { | |
13306 | /* Oops! We've got a reference past the defined end of | |
13307 | the table. This is probably a bug -- shall we warn? */ | |
13308 | size = addend + file_align; | |
13309 | } | |
13310 | } | |
13311 | size = (size + file_align - 1) & -file_align; | |
13312 | ||
13313 | /* Allocate one extra entry for use as a "done" flag for the | |
13314 | consolidation pass. */ | |
13315 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
13316 | ||
13317 | if (ptr) | |
13318 | { | |
a50b1753 | 13319 | ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); |
c152c796 AM |
13320 | |
13321 | if (ptr != NULL) | |
13322 | { | |
13323 | size_t oldbytes; | |
13324 | ||
f6e332e6 | 13325 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
13326 | * sizeof (bfd_boolean)); |
13327 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
13328 | } | |
13329 | } | |
13330 | else | |
a50b1753 | 13331 | ptr = (bfd_boolean *) bfd_zmalloc (bytes); |
c152c796 AM |
13332 | |
13333 | if (ptr == NULL) | |
13334 | return FALSE; | |
13335 | ||
13336 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
13337 | h->vtable->used = ptr + 1; |
13338 | h->vtable->size = size; | |
c152c796 AM |
13339 | } |
13340 | ||
f6e332e6 | 13341 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
13342 | |
13343 | return TRUE; | |
13344 | } | |
13345 | ||
ae17ab41 CM |
13346 | /* Map an ELF section header flag to its corresponding string. */ |
13347 | typedef struct | |
13348 | { | |
13349 | char *flag_name; | |
13350 | flagword flag_value; | |
13351 | } elf_flags_to_name_table; | |
13352 | ||
13353 | static elf_flags_to_name_table elf_flags_to_names [] = | |
13354 | { | |
13355 | { "SHF_WRITE", SHF_WRITE }, | |
13356 | { "SHF_ALLOC", SHF_ALLOC }, | |
13357 | { "SHF_EXECINSTR", SHF_EXECINSTR }, | |
13358 | { "SHF_MERGE", SHF_MERGE }, | |
13359 | { "SHF_STRINGS", SHF_STRINGS }, | |
13360 | { "SHF_INFO_LINK", SHF_INFO_LINK}, | |
13361 | { "SHF_LINK_ORDER", SHF_LINK_ORDER}, | |
13362 | { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, | |
13363 | { "SHF_GROUP", SHF_GROUP }, | |
13364 | { "SHF_TLS", SHF_TLS }, | |
13365 | { "SHF_MASKOS", SHF_MASKOS }, | |
13366 | { "SHF_EXCLUDE", SHF_EXCLUDE }, | |
13367 | }; | |
13368 | ||
b9c361e0 JL |
13369 | /* Returns TRUE if the section is to be included, otherwise FALSE. */ |
13370 | bfd_boolean | |
ae17ab41 | 13371 | bfd_elf_lookup_section_flags (struct bfd_link_info *info, |
8b127cbc | 13372 | struct flag_info *flaginfo, |
b9c361e0 | 13373 | asection *section) |
ae17ab41 | 13374 | { |
8b127cbc | 13375 | const bfd_vma sh_flags = elf_section_flags (section); |
ae17ab41 | 13376 | |
8b127cbc | 13377 | if (!flaginfo->flags_initialized) |
ae17ab41 | 13378 | { |
8b127cbc AM |
13379 | bfd *obfd = info->output_bfd; |
13380 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
13381 | struct flag_info_list *tf = flaginfo->flag_list; | |
b9c361e0 JL |
13382 | int with_hex = 0; |
13383 | int without_hex = 0; | |
13384 | ||
8b127cbc | 13385 | for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) |
ae17ab41 | 13386 | { |
b9c361e0 | 13387 | unsigned i; |
8b127cbc | 13388 | flagword (*lookup) (char *); |
ae17ab41 | 13389 | |
8b127cbc AM |
13390 | lookup = bed->elf_backend_lookup_section_flags_hook; |
13391 | if (lookup != NULL) | |
ae17ab41 | 13392 | { |
8b127cbc | 13393 | flagword hexval = (*lookup) ((char *) tf->name); |
b9c361e0 JL |
13394 | |
13395 | if (hexval != 0) | |
13396 | { | |
13397 | if (tf->with == with_flags) | |
13398 | with_hex |= hexval; | |
13399 | else if (tf->with == without_flags) | |
13400 | without_hex |= hexval; | |
13401 | tf->valid = TRUE; | |
13402 | continue; | |
13403 | } | |
ae17ab41 | 13404 | } |
8b127cbc | 13405 | for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) |
ae17ab41 | 13406 | { |
8b127cbc | 13407 | if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) |
b9c361e0 JL |
13408 | { |
13409 | if (tf->with == with_flags) | |
13410 | with_hex |= elf_flags_to_names[i].flag_value; | |
13411 | else if (tf->with == without_flags) | |
13412 | without_hex |= elf_flags_to_names[i].flag_value; | |
13413 | tf->valid = TRUE; | |
13414 | break; | |
13415 | } | |
13416 | } | |
8b127cbc | 13417 | if (!tf->valid) |
b9c361e0 | 13418 | { |
68ffbac6 | 13419 | info->callbacks->einfo |
8b127cbc | 13420 | (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); |
b9c361e0 | 13421 | return FALSE; |
ae17ab41 CM |
13422 | } |
13423 | } | |
8b127cbc AM |
13424 | flaginfo->flags_initialized = TRUE; |
13425 | flaginfo->only_with_flags |= with_hex; | |
13426 | flaginfo->not_with_flags |= without_hex; | |
ae17ab41 | 13427 | } |
ae17ab41 | 13428 | |
8b127cbc | 13429 | if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) |
b9c361e0 JL |
13430 | return FALSE; |
13431 | ||
8b127cbc | 13432 | if ((flaginfo->not_with_flags & sh_flags) != 0) |
b9c361e0 JL |
13433 | return FALSE; |
13434 | ||
13435 | return TRUE; | |
ae17ab41 CM |
13436 | } |
13437 | ||
c152c796 AM |
13438 | struct alloc_got_off_arg { |
13439 | bfd_vma gotoff; | |
10455f89 | 13440 | struct bfd_link_info *info; |
c152c796 AM |
13441 | }; |
13442 | ||
13443 | /* We need a special top-level link routine to convert got reference counts | |
13444 | to real got offsets. */ | |
13445 | ||
13446 | static bfd_boolean | |
13447 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
13448 | { | |
a50b1753 | 13449 | struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; |
10455f89 HPN |
13450 | bfd *obfd = gofarg->info->output_bfd; |
13451 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
c152c796 | 13452 | |
c152c796 AM |
13453 | if (h->got.refcount > 0) |
13454 | { | |
13455 | h->got.offset = gofarg->gotoff; | |
10455f89 | 13456 | gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); |
c152c796 AM |
13457 | } |
13458 | else | |
13459 | h->got.offset = (bfd_vma) -1; | |
13460 | ||
13461 | return TRUE; | |
13462 | } | |
13463 | ||
13464 | /* And an accompanying bit to work out final got entry offsets once | |
13465 | we're done. Should be called from final_link. */ | |
13466 | ||
13467 | bfd_boolean | |
13468 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
13469 | struct bfd_link_info *info) | |
13470 | { | |
13471 | bfd *i; | |
13472 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13473 | bfd_vma gotoff; | |
c152c796 AM |
13474 | struct alloc_got_off_arg gofarg; |
13475 | ||
10455f89 HPN |
13476 | BFD_ASSERT (abfd == info->output_bfd); |
13477 | ||
c152c796 AM |
13478 | if (! is_elf_hash_table (info->hash)) |
13479 | return FALSE; | |
13480 | ||
13481 | /* The GOT offset is relative to the .got section, but the GOT header is | |
13482 | put into the .got.plt section, if the backend uses it. */ | |
13483 | if (bed->want_got_plt) | |
13484 | gotoff = 0; | |
13485 | else | |
13486 | gotoff = bed->got_header_size; | |
13487 | ||
13488 | /* Do the local .got entries first. */ | |
c72f2fb2 | 13489 | for (i = info->input_bfds; i; i = i->link.next) |
c152c796 AM |
13490 | { |
13491 | bfd_signed_vma *local_got; | |
ef53be89 | 13492 | size_t j, locsymcount; |
c152c796 AM |
13493 | Elf_Internal_Shdr *symtab_hdr; |
13494 | ||
13495 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
13496 | continue; | |
13497 | ||
13498 | local_got = elf_local_got_refcounts (i); | |
13499 | if (!local_got) | |
13500 | continue; | |
13501 | ||
13502 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
13503 | if (elf_bad_symtab (i)) | |
13504 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
13505 | else | |
13506 | locsymcount = symtab_hdr->sh_info; | |
13507 | ||
13508 | for (j = 0; j < locsymcount; ++j) | |
13509 | { | |
13510 | if (local_got[j] > 0) | |
13511 | { | |
13512 | local_got[j] = gotoff; | |
10455f89 | 13513 | gotoff += bed->got_elt_size (abfd, info, NULL, i, j); |
c152c796 AM |
13514 | } |
13515 | else | |
13516 | local_got[j] = (bfd_vma) -1; | |
13517 | } | |
13518 | } | |
13519 | ||
13520 | /* Then the global .got entries. .plt refcounts are handled by | |
13521 | adjust_dynamic_symbol */ | |
13522 | gofarg.gotoff = gotoff; | |
10455f89 | 13523 | gofarg.info = info; |
c152c796 AM |
13524 | elf_link_hash_traverse (elf_hash_table (info), |
13525 | elf_gc_allocate_got_offsets, | |
13526 | &gofarg); | |
13527 | return TRUE; | |
13528 | } | |
13529 | ||
13530 | /* Many folk need no more in the way of final link than this, once | |
13531 | got entry reference counting is enabled. */ | |
13532 | ||
13533 | bfd_boolean | |
13534 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
13535 | { | |
13536 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
13537 | return FALSE; | |
13538 | ||
13539 | /* Invoke the regular ELF backend linker to do all the work. */ | |
13540 | return bfd_elf_final_link (abfd, info); | |
13541 | } | |
13542 | ||
13543 | bfd_boolean | |
13544 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
13545 | { | |
a50b1753 | 13546 | struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; |
c152c796 AM |
13547 | |
13548 | if (rcookie->bad_symtab) | |
13549 | rcookie->rel = rcookie->rels; | |
13550 | ||
13551 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
13552 | { | |
13553 | unsigned long r_symndx; | |
13554 | ||
13555 | if (! rcookie->bad_symtab) | |
13556 | if (rcookie->rel->r_offset > offset) | |
13557 | return FALSE; | |
13558 | if (rcookie->rel->r_offset != offset) | |
13559 | continue; | |
13560 | ||
13561 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
2c2fa401 | 13562 | if (r_symndx == STN_UNDEF) |
c152c796 AM |
13563 | return TRUE; |
13564 | ||
13565 | if (r_symndx >= rcookie->locsymcount | |
13566 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
13567 | { | |
13568 | struct elf_link_hash_entry *h; | |
13569 | ||
13570 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
13571 | ||
13572 | while (h->root.type == bfd_link_hash_indirect | |
13573 | || h->root.type == bfd_link_hash_warning) | |
13574 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
13575 | ||
13576 | if ((h->root.type == bfd_link_hash_defined | |
13577 | || h->root.type == bfd_link_hash_defweak) | |
5b69e357 AM |
13578 | && (h->root.u.def.section->owner != rcookie->abfd |
13579 | || h->root.u.def.section->kept_section != NULL | |
13580 | || discarded_section (h->root.u.def.section))) | |
c152c796 | 13581 | return TRUE; |
c152c796 AM |
13582 | } |
13583 | else | |
13584 | { | |
13585 | /* It's not a relocation against a global symbol, | |
13586 | but it could be a relocation against a local | |
13587 | symbol for a discarded section. */ | |
13588 | asection *isec; | |
13589 | Elf_Internal_Sym *isym; | |
13590 | ||
13591 | /* Need to: get the symbol; get the section. */ | |
13592 | isym = &rcookie->locsyms[r_symndx]; | |
cb33740c | 13593 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); |
5b69e357 AM |
13594 | if (isec != NULL |
13595 | && (isec->kept_section != NULL | |
13596 | || discarded_section (isec))) | |
cb33740c | 13597 | return TRUE; |
c152c796 AM |
13598 | } |
13599 | return FALSE; | |
13600 | } | |
13601 | return FALSE; | |
13602 | } | |
13603 | ||
13604 | /* Discard unneeded references to discarded sections. | |
75938853 AM |
13605 | Returns -1 on error, 1 if any section's size was changed, 0 if |
13606 | nothing changed. This function assumes that the relocations are in | |
13607 | sorted order, which is true for all known assemblers. */ | |
c152c796 | 13608 | |
75938853 | 13609 | int |
c152c796 AM |
13610 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) |
13611 | { | |
13612 | struct elf_reloc_cookie cookie; | |
18cd5bce | 13613 | asection *o; |
c152c796 | 13614 | bfd *abfd; |
75938853 | 13615 | int changed = 0; |
c152c796 AM |
13616 | |
13617 | if (info->traditional_format | |
13618 | || !is_elf_hash_table (info->hash)) | |
75938853 | 13619 | return 0; |
c152c796 | 13620 | |
18cd5bce AM |
13621 | o = bfd_get_section_by_name (output_bfd, ".stab"); |
13622 | if (o != NULL) | |
c152c796 | 13623 | { |
18cd5bce | 13624 | asection *i; |
c152c796 | 13625 | |
18cd5bce | 13626 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
8da3dbc5 | 13627 | { |
18cd5bce AM |
13628 | if (i->size == 0 |
13629 | || i->reloc_count == 0 | |
13630 | || i->sec_info_type != SEC_INFO_TYPE_STABS) | |
13631 | continue; | |
c152c796 | 13632 | |
18cd5bce AM |
13633 | abfd = i->owner; |
13634 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
13635 | continue; | |
c152c796 | 13636 | |
18cd5bce | 13637 | if (!init_reloc_cookie_for_section (&cookie, info, i)) |
75938853 | 13638 | return -1; |
c152c796 | 13639 | |
18cd5bce AM |
13640 | if (_bfd_discard_section_stabs (abfd, i, |
13641 | elf_section_data (i)->sec_info, | |
5241d853 RS |
13642 | bfd_elf_reloc_symbol_deleted_p, |
13643 | &cookie)) | |
75938853 | 13644 | changed = 1; |
18cd5bce AM |
13645 | |
13646 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 13647 | } |
18cd5bce AM |
13648 | } |
13649 | ||
2f0c68f2 CM |
13650 | o = NULL; |
13651 | if (info->eh_frame_hdr_type != COMPACT_EH_HDR) | |
13652 | o = bfd_get_section_by_name (output_bfd, ".eh_frame"); | |
18cd5bce AM |
13653 | if (o != NULL) |
13654 | { | |
13655 | asection *i; | |
c152c796 | 13656 | |
18cd5bce | 13657 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
c152c796 | 13658 | { |
18cd5bce AM |
13659 | if (i->size == 0) |
13660 | continue; | |
13661 | ||
13662 | abfd = i->owner; | |
13663 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
13664 | continue; | |
13665 | ||
13666 | if (!init_reloc_cookie_for_section (&cookie, info, i)) | |
75938853 | 13667 | return -1; |
18cd5bce AM |
13668 | |
13669 | _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); | |
13670 | if (_bfd_elf_discard_section_eh_frame (abfd, info, i, | |
c152c796 AM |
13671 | bfd_elf_reloc_symbol_deleted_p, |
13672 | &cookie)) | |
75938853 | 13673 | changed = 1; |
18cd5bce AM |
13674 | |
13675 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 13676 | } |
18cd5bce | 13677 | } |
c152c796 | 13678 | |
18cd5bce AM |
13679 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) |
13680 | { | |
13681 | const struct elf_backend_data *bed; | |
c152c796 | 13682 | |
18cd5bce AM |
13683 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) |
13684 | continue; | |
13685 | ||
13686 | bed = get_elf_backend_data (abfd); | |
13687 | ||
13688 | if (bed->elf_backend_discard_info != NULL) | |
13689 | { | |
13690 | if (!init_reloc_cookie (&cookie, info, abfd)) | |
75938853 | 13691 | return -1; |
18cd5bce AM |
13692 | |
13693 | if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
75938853 | 13694 | changed = 1; |
18cd5bce AM |
13695 | |
13696 | fini_reloc_cookie (&cookie, abfd); | |
13697 | } | |
c152c796 AM |
13698 | } |
13699 | ||
2f0c68f2 CM |
13700 | if (info->eh_frame_hdr_type == COMPACT_EH_HDR) |
13701 | _bfd_elf_end_eh_frame_parsing (info); | |
13702 | ||
13703 | if (info->eh_frame_hdr_type | |
0e1862bb | 13704 | && !bfd_link_relocatable (info) |
c152c796 | 13705 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) |
75938853 | 13706 | changed = 1; |
c152c796 | 13707 | |
75938853 | 13708 | return changed; |
c152c796 | 13709 | } |
082b7297 | 13710 | |
43e1669b | 13711 | bfd_boolean |
0c511000 | 13712 | _bfd_elf_section_already_linked (bfd *abfd, |
c77ec726 | 13713 | asection *sec, |
c0f00686 | 13714 | struct bfd_link_info *info) |
082b7297 L |
13715 | { |
13716 | flagword flags; | |
c77ec726 | 13717 | const char *name, *key; |
082b7297 L |
13718 | struct bfd_section_already_linked *l; |
13719 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
0c511000 | 13720 | |
c77ec726 AM |
13721 | if (sec->output_section == bfd_abs_section_ptr) |
13722 | return FALSE; | |
0c511000 | 13723 | |
c77ec726 | 13724 | flags = sec->flags; |
0c511000 | 13725 | |
c77ec726 AM |
13726 | /* Return if it isn't a linkonce section. A comdat group section |
13727 | also has SEC_LINK_ONCE set. */ | |
13728 | if ((flags & SEC_LINK_ONCE) == 0) | |
13729 | return FALSE; | |
0c511000 | 13730 | |
c77ec726 AM |
13731 | /* Don't put group member sections on our list of already linked |
13732 | sections. They are handled as a group via their group section. */ | |
13733 | if (elf_sec_group (sec) != NULL) | |
13734 | return FALSE; | |
0c511000 | 13735 | |
c77ec726 AM |
13736 | /* For a SHT_GROUP section, use the group signature as the key. */ |
13737 | name = sec->name; | |
13738 | if ((flags & SEC_GROUP) != 0 | |
13739 | && elf_next_in_group (sec) != NULL | |
13740 | && elf_group_name (elf_next_in_group (sec)) != NULL) | |
13741 | key = elf_group_name (elf_next_in_group (sec)); | |
13742 | else | |
13743 | { | |
13744 | /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ | |
0c511000 | 13745 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
c77ec726 AM |
13746 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
13747 | key++; | |
0c511000 | 13748 | else |
c77ec726 AM |
13749 | /* Must be a user linkonce section that doesn't follow gcc's |
13750 | naming convention. In this case we won't be matching | |
13751 | single member groups. */ | |
13752 | key = name; | |
0c511000 | 13753 | } |
6d2cd210 | 13754 | |
c77ec726 | 13755 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
082b7297 L |
13756 | |
13757 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13758 | { | |
c2370991 | 13759 | /* We may have 2 different types of sections on the list: group |
c77ec726 AM |
13760 | sections with a signature of <key> (<key> is some string), |
13761 | and linkonce sections named .gnu.linkonce.<type>.<key>. | |
13762 | Match like sections. LTO plugin sections are an exception. | |
13763 | They are always named .gnu.linkonce.t.<key> and match either | |
13764 | type of section. */ | |
13765 | if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) | |
13766 | && ((flags & SEC_GROUP) != 0 | |
13767 | || strcmp (name, l->sec->name) == 0)) | |
13768 | || (l->sec->owner->flags & BFD_PLUGIN) != 0) | |
082b7297 L |
13769 | { |
13770 | /* The section has already been linked. See if we should | |
6d2cd210 | 13771 | issue a warning. */ |
c77ec726 AM |
13772 | if (!_bfd_handle_already_linked (sec, l, info)) |
13773 | return FALSE; | |
082b7297 | 13774 | |
c77ec726 | 13775 | if (flags & SEC_GROUP) |
3d7f7666 | 13776 | { |
c77ec726 AM |
13777 | asection *first = elf_next_in_group (sec); |
13778 | asection *s = first; | |
3d7f7666 | 13779 | |
c77ec726 | 13780 | while (s != NULL) |
3d7f7666 | 13781 | { |
c77ec726 AM |
13782 | s->output_section = bfd_abs_section_ptr; |
13783 | /* Record which group discards it. */ | |
13784 | s->kept_section = l->sec; | |
13785 | s = elf_next_in_group (s); | |
13786 | /* These lists are circular. */ | |
13787 | if (s == first) | |
13788 | break; | |
3d7f7666 L |
13789 | } |
13790 | } | |
082b7297 | 13791 | |
43e1669b | 13792 | return TRUE; |
082b7297 L |
13793 | } |
13794 | } | |
13795 | ||
c77ec726 AM |
13796 | /* A single member comdat group section may be discarded by a |
13797 | linkonce section and vice versa. */ | |
13798 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 13799 | { |
c77ec726 | 13800 | asection *first = elf_next_in_group (sec); |
c2370991 | 13801 | |
c77ec726 AM |
13802 | if (first != NULL && elf_next_in_group (first) == first) |
13803 | /* Check this single member group against linkonce sections. */ | |
13804 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13805 | if ((l->sec->flags & SEC_GROUP) == 0 | |
13806 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
13807 | { | |
13808 | first->output_section = bfd_abs_section_ptr; | |
13809 | first->kept_section = l->sec; | |
13810 | sec->output_section = bfd_abs_section_ptr; | |
13811 | break; | |
13812 | } | |
13813 | } | |
13814 | else | |
13815 | /* Check this linkonce section against single member groups. */ | |
13816 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13817 | if (l->sec->flags & SEC_GROUP) | |
6d2cd210 | 13818 | { |
c77ec726 | 13819 | asection *first = elf_next_in_group (l->sec); |
6d2cd210 | 13820 | |
c77ec726 AM |
13821 | if (first != NULL |
13822 | && elf_next_in_group (first) == first | |
13823 | && bfd_elf_match_symbols_in_sections (first, sec, info)) | |
13824 | { | |
13825 | sec->output_section = bfd_abs_section_ptr; | |
13826 | sec->kept_section = first; | |
13827 | break; | |
13828 | } | |
6d2cd210 | 13829 | } |
0c511000 | 13830 | |
c77ec726 AM |
13831 | /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' |
13832 | referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 | |
13833 | specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' | |
13834 | prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its | |
13835 | matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded | |
13836 | but its `.gnu.linkonce.t.F' is discarded means we chose one-only | |
13837 | `.gnu.linkonce.t.F' section from a different bfd not requiring any | |
13838 | `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. | |
13839 | The reverse order cannot happen as there is never a bfd with only the | |
13840 | `.gnu.linkonce.r.F' section. The order of sections in a bfd does not | |
13841 | matter as here were are looking only for cross-bfd sections. */ | |
13842 | ||
13843 | if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) | |
13844 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13845 | if ((l->sec->flags & SEC_GROUP) == 0 | |
13846 | && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) | |
13847 | { | |
13848 | if (abfd != l->sec->owner) | |
13849 | sec->output_section = bfd_abs_section_ptr; | |
13850 | break; | |
13851 | } | |
80c29487 | 13852 | |
082b7297 | 13853 | /* This is the first section with this name. Record it. */ |
c77ec726 | 13854 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
bb6198d2 | 13855 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
c77ec726 | 13856 | return sec->output_section == bfd_abs_section_ptr; |
082b7297 | 13857 | } |
81e1b023 | 13858 | |
a4d8e49b L |
13859 | bfd_boolean |
13860 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
13861 | { | |
13862 | return sym->st_shndx == SHN_COMMON; | |
13863 | } | |
13864 | ||
13865 | unsigned int | |
13866 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
13867 | { | |
13868 | return SHN_COMMON; | |
13869 | } | |
13870 | ||
13871 | asection * | |
13872 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
13873 | { | |
13874 | return bfd_com_section_ptr; | |
13875 | } | |
10455f89 HPN |
13876 | |
13877 | bfd_vma | |
13878 | _bfd_elf_default_got_elt_size (bfd *abfd, | |
13879 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
13880 | struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, | |
13881 | bfd *ibfd ATTRIBUTE_UNUSED, | |
13882 | unsigned long symndx ATTRIBUTE_UNUSED) | |
13883 | { | |
13884 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13885 | return bed->s->arch_size / 8; | |
13886 | } | |
83bac4b0 NC |
13887 | |
13888 | /* Routines to support the creation of dynamic relocs. */ | |
13889 | ||
83bac4b0 NC |
13890 | /* Returns the name of the dynamic reloc section associated with SEC. */ |
13891 | ||
13892 | static const char * | |
13893 | get_dynamic_reloc_section_name (bfd * abfd, | |
13894 | asection * sec, | |
13895 | bfd_boolean is_rela) | |
13896 | { | |
ddcf1fcf BS |
13897 | char *name; |
13898 | const char *old_name = bfd_get_section_name (NULL, sec); | |
13899 | const char *prefix = is_rela ? ".rela" : ".rel"; | |
83bac4b0 | 13900 | |
ddcf1fcf | 13901 | if (old_name == NULL) |
83bac4b0 NC |
13902 | return NULL; |
13903 | ||
ddcf1fcf | 13904 | name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); |
68ffbac6 | 13905 | sprintf (name, "%s%s", prefix, old_name); |
83bac4b0 NC |
13906 | |
13907 | return name; | |
13908 | } | |
13909 | ||
13910 | /* Returns the dynamic reloc section associated with SEC. | |
13911 | If necessary compute the name of the dynamic reloc section based | |
13912 | on SEC's name (looked up in ABFD's string table) and the setting | |
13913 | of IS_RELA. */ | |
13914 | ||
13915 | asection * | |
13916 | _bfd_elf_get_dynamic_reloc_section (bfd * abfd, | |
13917 | asection * sec, | |
13918 | bfd_boolean is_rela) | |
13919 | { | |
13920 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
13921 | ||
13922 | if (reloc_sec == NULL) | |
13923 | { | |
13924 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
13925 | ||
13926 | if (name != NULL) | |
13927 | { | |
3d4d4302 | 13928 | reloc_sec = bfd_get_linker_section (abfd, name); |
83bac4b0 NC |
13929 | |
13930 | if (reloc_sec != NULL) | |
13931 | elf_section_data (sec)->sreloc = reloc_sec; | |
13932 | } | |
13933 | } | |
13934 | ||
13935 | return reloc_sec; | |
13936 | } | |
13937 | ||
13938 | /* Returns the dynamic reloc section associated with SEC. If the | |
13939 | section does not exist it is created and attached to the DYNOBJ | |
13940 | bfd and stored in the SRELOC field of SEC's elf_section_data | |
13941 | structure. | |
f8076f98 | 13942 | |
83bac4b0 NC |
13943 | ALIGNMENT is the alignment for the newly created section and |
13944 | IS_RELA defines whether the name should be .rela.<SEC's name> | |
13945 | or .rel.<SEC's name>. The section name is looked up in the | |
13946 | string table associated with ABFD. */ | |
13947 | ||
13948 | asection * | |
ca4be51c AM |
13949 | _bfd_elf_make_dynamic_reloc_section (asection *sec, |
13950 | bfd *dynobj, | |
13951 | unsigned int alignment, | |
13952 | bfd *abfd, | |
13953 | bfd_boolean is_rela) | |
83bac4b0 NC |
13954 | { |
13955 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
13956 | ||
13957 | if (reloc_sec == NULL) | |
13958 | { | |
13959 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
13960 | ||
13961 | if (name == NULL) | |
13962 | return NULL; | |
13963 | ||
3d4d4302 | 13964 | reloc_sec = bfd_get_linker_section (dynobj, name); |
83bac4b0 NC |
13965 | |
13966 | if (reloc_sec == NULL) | |
13967 | { | |
3d4d4302 AM |
13968 | flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY |
13969 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
83bac4b0 NC |
13970 | if ((sec->flags & SEC_ALLOC) != 0) |
13971 | flags |= SEC_ALLOC | SEC_LOAD; | |
13972 | ||
3d4d4302 | 13973 | reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); |
83bac4b0 NC |
13974 | if (reloc_sec != NULL) |
13975 | { | |
8877b5e5 AM |
13976 | /* _bfd_elf_get_sec_type_attr chooses a section type by |
13977 | name. Override as it may be wrong, eg. for a user | |
13978 | section named "auto" we'll get ".relauto" which is | |
13979 | seen to be a .rela section. */ | |
13980 | elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; | |
83bac4b0 NC |
13981 | if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) |
13982 | reloc_sec = NULL; | |
13983 | } | |
13984 | } | |
13985 | ||
13986 | elf_section_data (sec)->sreloc = reloc_sec; | |
13987 | } | |
13988 | ||
13989 | return reloc_sec; | |
13990 | } | |
1338dd10 | 13991 | |
bffebb6b AM |
13992 | /* Copy the ELF symbol type and other attributes for a linker script |
13993 | assignment from HSRC to HDEST. Generally this should be treated as | |
13994 | if we found a strong non-dynamic definition for HDEST (except that | |
13995 | ld ignores multiple definition errors). */ | |
1338dd10 | 13996 | void |
bffebb6b AM |
13997 | _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, |
13998 | struct bfd_link_hash_entry *hdest, | |
13999 | struct bfd_link_hash_entry *hsrc) | |
1338dd10 | 14000 | { |
bffebb6b AM |
14001 | struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; |
14002 | struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; | |
14003 | Elf_Internal_Sym isym; | |
1338dd10 PB |
14004 | |
14005 | ehdest->type = ehsrc->type; | |
35fc36a8 | 14006 | ehdest->target_internal = ehsrc->target_internal; |
bffebb6b AM |
14007 | |
14008 | isym.st_other = ehsrc->other; | |
b8417128 | 14009 | elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE); |
1338dd10 | 14010 | } |
351f65ca L |
14011 | |
14012 | /* Append a RELA relocation REL to section S in BFD. */ | |
14013 | ||
14014 | void | |
14015 | elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14016 | { | |
14017 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14018 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); | |
14019 | BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); | |
14020 | bed->s->swap_reloca_out (abfd, rel, loc); | |
14021 | } | |
14022 | ||
14023 | /* Append a REL relocation REL to section S in BFD. */ | |
14024 | ||
14025 | void | |
14026 | elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14027 | { | |
14028 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14029 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); | |
14030 | BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); | |
59d6ffb2 | 14031 | bed->s->swap_reloc_out (abfd, rel, loc); |
351f65ca | 14032 | } |