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. */ | |
3d4d4302 AM |
152 | s = bfd_get_linker_section (abfd, ".got"); |
153 | if (s != NULL) | |
b34976b6 | 154 | return TRUE; |
252b5132 | 155 | |
e5a52504 | 156 | flags = bed->dynamic_sec_flags; |
252b5132 | 157 | |
14b2f831 AM |
158 | s = bfd_make_section_anyway_with_flags (abfd, |
159 | (bed->rela_plts_and_copies_p | |
160 | ? ".rela.got" : ".rel.got"), | |
161 | (bed->dynamic_sec_flags | |
162 | | SEC_READONLY)); | |
6de2ae4a L |
163 | if (s == NULL |
164 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
165 | return FALSE; | |
166 | htab->srelgot = s; | |
252b5132 | 167 | |
14b2f831 | 168 | s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); |
64e77c6d L |
169 | if (s == NULL |
170 | || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
171 | return FALSE; | |
172 | htab->sgot = s; | |
173 | ||
252b5132 RH |
174 | if (bed->want_got_plt) |
175 | { | |
14b2f831 | 176 | s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); |
252b5132 | 177 | if (s == NULL |
6de2ae4a L |
178 | || !bfd_set_section_alignment (abfd, s, |
179 | bed->s->log_file_align)) | |
b34976b6 | 180 | return FALSE; |
6de2ae4a | 181 | htab->sgotplt = s; |
252b5132 RH |
182 | } |
183 | ||
64e77c6d L |
184 | /* The first bit of the global offset table is the header. */ |
185 | s->size += bed->got_header_size; | |
186 | ||
2517a57f AM |
187 | if (bed->want_got_sym) |
188 | { | |
189 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
190 | (or .got.plt) section. We don't do this in the linker script | |
191 | because we don't want to define the symbol if we are not creating | |
192 | a global offset table. */ | |
6de2ae4a L |
193 | h = _bfd_elf_define_linkage_sym (abfd, info, s, |
194 | "_GLOBAL_OFFSET_TABLE_"); | |
2517a57f | 195 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
196 | if (h == NULL) |
197 | return FALSE; | |
2517a57f | 198 | } |
252b5132 | 199 | |
b34976b6 | 200 | return TRUE; |
252b5132 RH |
201 | } |
202 | \f | |
7e9f0867 AM |
203 | /* Create a strtab to hold the dynamic symbol names. */ |
204 | static bfd_boolean | |
205 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
206 | { | |
207 | struct elf_link_hash_table *hash_table; | |
208 | ||
209 | hash_table = elf_hash_table (info); | |
210 | if (hash_table->dynobj == NULL) | |
6cd255ca L |
211 | { |
212 | /* We may not set dynobj, an input file holding linker created | |
213 | dynamic sections to abfd, which may be a dynamic object with | |
214 | its own dynamic sections. We need to find a normal input file | |
215 | to hold linker created sections if possible. */ | |
216 | if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0) | |
217 | { | |
218 | bfd *ibfd; | |
219 | for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next) | |
6645479e L |
220 | if ((ibfd->flags |
221 | & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0) | |
6cd255ca L |
222 | { |
223 | abfd = ibfd; | |
224 | break; | |
225 | } | |
226 | } | |
227 | hash_table->dynobj = abfd; | |
228 | } | |
7e9f0867 AM |
229 | |
230 | if (hash_table->dynstr == NULL) | |
231 | { | |
232 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
233 | if (hash_table->dynstr == NULL) | |
234 | return FALSE; | |
235 | } | |
236 | return TRUE; | |
237 | } | |
238 | ||
45d6a902 AM |
239 | /* Create some sections which will be filled in with dynamic linking |
240 | information. ABFD is an input file which requires dynamic sections | |
241 | to be created. The dynamic sections take up virtual memory space | |
242 | when the final executable is run, so we need to create them before | |
243 | addresses are assigned to the output sections. We work out the | |
244 | actual contents and size of these sections later. */ | |
252b5132 | 245 | |
b34976b6 | 246 | bfd_boolean |
268b6b39 | 247 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 248 | { |
45d6a902 | 249 | flagword flags; |
91d6fa6a | 250 | asection *s; |
9c5bfbb7 | 251 | const struct elf_backend_data *bed; |
9637f6ef | 252 | struct elf_link_hash_entry *h; |
252b5132 | 253 | |
0eddce27 | 254 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
255 | return FALSE; |
256 | ||
257 | if (elf_hash_table (info)->dynamic_sections_created) | |
258 | return TRUE; | |
259 | ||
7e9f0867 AM |
260 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
261 | return FALSE; | |
45d6a902 | 262 | |
7e9f0867 | 263 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
264 | bed = get_elf_backend_data (abfd); |
265 | ||
266 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
267 | |
268 | /* A dynamically linked executable has a .interp section, but a | |
269 | shared library does not. */ | |
9b8b325a | 270 | if (bfd_link_executable (info) && !info->nointerp) |
252b5132 | 271 | { |
14b2f831 AM |
272 | s = bfd_make_section_anyway_with_flags (abfd, ".interp", |
273 | flags | SEC_READONLY); | |
3496cb2a | 274 | if (s == NULL) |
45d6a902 AM |
275 | return FALSE; |
276 | } | |
bb0deeff | 277 | |
45d6a902 AM |
278 | /* Create sections to hold version informations. These are removed |
279 | if they are not needed. */ | |
14b2f831 AM |
280 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d", |
281 | flags | SEC_READONLY); | |
45d6a902 | 282 | if (s == NULL |
45d6a902 AM |
283 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
284 | return FALSE; | |
285 | ||
14b2f831 AM |
286 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version", |
287 | flags | SEC_READONLY); | |
45d6a902 | 288 | if (s == NULL |
45d6a902 AM |
289 | || ! bfd_set_section_alignment (abfd, s, 1)) |
290 | return FALSE; | |
291 | ||
14b2f831 AM |
292 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r", |
293 | flags | SEC_READONLY); | |
45d6a902 | 294 | if (s == NULL |
45d6a902 AM |
295 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
296 | return FALSE; | |
297 | ||
14b2f831 AM |
298 | s = bfd_make_section_anyway_with_flags (abfd, ".dynsym", |
299 | flags | SEC_READONLY); | |
45d6a902 | 300 | if (s == NULL |
45d6a902 AM |
301 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
302 | return FALSE; | |
cae1fbbb | 303 | elf_hash_table (info)->dynsym = s; |
45d6a902 | 304 | |
14b2f831 AM |
305 | s = bfd_make_section_anyway_with_flags (abfd, ".dynstr", |
306 | flags | SEC_READONLY); | |
3496cb2a | 307 | if (s == NULL) |
45d6a902 AM |
308 | return FALSE; |
309 | ||
14b2f831 | 310 | s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 311 | if (s == NULL |
45d6a902 AM |
312 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
313 | return FALSE; | |
314 | ||
315 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
316 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
317 | only want to define it if we are, in fact, creating a .dynamic | |
318 | section. We don't want to define it if there is no .dynamic | |
319 | section, since on some ELF platforms the start up code examines it | |
320 | to decide how to initialize the process. */ | |
9637f6ef L |
321 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"); |
322 | elf_hash_table (info)->hdynamic = h; | |
323 | if (h == NULL) | |
45d6a902 AM |
324 | return FALSE; |
325 | ||
fdc90cb4 JJ |
326 | if (info->emit_hash) |
327 | { | |
14b2f831 AM |
328 | s = bfd_make_section_anyway_with_flags (abfd, ".hash", |
329 | flags | SEC_READONLY); | |
fdc90cb4 JJ |
330 | if (s == NULL |
331 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
332 | return FALSE; | |
333 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
334 | } | |
335 | ||
336 | if (info->emit_gnu_hash) | |
337 | { | |
14b2f831 AM |
338 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash", |
339 | flags | SEC_READONLY); | |
fdc90cb4 JJ |
340 | if (s == NULL |
341 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
342 | return FALSE; | |
343 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: | |
344 | 4 32-bit words followed by variable count of 64-bit words, then | |
345 | variable count of 32-bit words. */ | |
346 | if (bed->s->arch_size == 64) | |
347 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
348 | else | |
349 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
350 | } | |
45d6a902 AM |
351 | |
352 | /* Let the backend create the rest of the sections. This lets the | |
353 | backend set the right flags. The backend will normally create | |
354 | the .got and .plt sections. */ | |
894891db NC |
355 | if (bed->elf_backend_create_dynamic_sections == NULL |
356 | || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
45d6a902 AM |
357 | return FALSE; |
358 | ||
359 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
360 | ||
361 | return TRUE; | |
362 | } | |
363 | ||
364 | /* Create dynamic sections when linking against a dynamic object. */ | |
365 | ||
366 | bfd_boolean | |
268b6b39 | 367 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
368 | { |
369 | flagword flags, pltflags; | |
7325306f | 370 | struct elf_link_hash_entry *h; |
45d6a902 | 371 | asection *s; |
9c5bfbb7 | 372 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 373 | struct elf_link_hash_table *htab = elf_hash_table (info); |
45d6a902 | 374 | |
252b5132 RH |
375 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
376 | .rel[a].bss sections. */ | |
e5a52504 | 377 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
378 | |
379 | pltflags = flags; | |
252b5132 | 380 | if (bed->plt_not_loaded) |
6df4d94c MM |
381 | /* We do not clear SEC_ALLOC here because we still want the OS to |
382 | allocate space for the section; it's just that there's nothing | |
383 | to read in from the object file. */ | |
5d1634d7 | 384 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
385 | else |
386 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
387 | if (bed->plt_readonly) |
388 | pltflags |= SEC_READONLY; | |
389 | ||
14b2f831 | 390 | s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags); |
252b5132 | 391 | if (s == NULL |
252b5132 | 392 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 393 | return FALSE; |
6de2ae4a | 394 | htab->splt = s; |
252b5132 | 395 | |
d98685ac AM |
396 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
397 | .plt section. */ | |
7325306f RS |
398 | if (bed->want_plt_sym) |
399 | { | |
400 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
401 | "_PROCEDURE_LINKAGE_TABLE_"); | |
402 | elf_hash_table (info)->hplt = h; | |
403 | if (h == NULL) | |
404 | return FALSE; | |
405 | } | |
252b5132 | 406 | |
14b2f831 AM |
407 | s = bfd_make_section_anyway_with_flags (abfd, |
408 | (bed->rela_plts_and_copies_p | |
409 | ? ".rela.plt" : ".rel.plt"), | |
410 | flags | SEC_READONLY); | |
252b5132 | 411 | if (s == NULL |
45d6a902 | 412 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 413 | return FALSE; |
6de2ae4a | 414 | htab->srelplt = s; |
252b5132 RH |
415 | |
416 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 417 | return FALSE; |
252b5132 | 418 | |
3018b441 RH |
419 | if (bed->want_dynbss) |
420 | { | |
421 | /* The .dynbss section is a place to put symbols which are defined | |
422 | by dynamic objects, are referenced by regular objects, and are | |
423 | not functions. We must allocate space for them in the process | |
424 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
425 | initialize them at run time. The linker script puts the .dynbss | |
426 | section into the .bss section of the final image. */ | |
14b2f831 AM |
427 | s = bfd_make_section_anyway_with_flags (abfd, ".dynbss", |
428 | (SEC_ALLOC | SEC_LINKER_CREATED)); | |
3496cb2a | 429 | if (s == NULL) |
b34976b6 | 430 | return FALSE; |
252b5132 | 431 | |
3018b441 | 432 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
433 | normally needed. We need to create it here, though, so that the |
434 | linker will map it to an output section. We can't just create it | |
435 | only if we need it, because we will not know whether we need it | |
436 | until we have seen all the input files, and the first time the | |
437 | main linker code calls BFD after examining all the input files | |
438 | (size_dynamic_sections) the input sections have already been | |
439 | mapped to the output sections. If the section turns out not to | |
440 | be needed, we can discard it later. We will never need this | |
441 | section when generating a shared object, since they do not use | |
442 | copy relocs. */ | |
0e1862bb | 443 | if (! bfd_link_pic (info)) |
3018b441 | 444 | { |
14b2f831 AM |
445 | s = bfd_make_section_anyway_with_flags (abfd, |
446 | (bed->rela_plts_and_copies_p | |
447 | ? ".rela.bss" : ".rel.bss"), | |
448 | flags | SEC_READONLY); | |
3018b441 | 449 | if (s == NULL |
45d6a902 | 450 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 451 | return FALSE; |
3018b441 | 452 | } |
252b5132 RH |
453 | } |
454 | ||
b34976b6 | 455 | return TRUE; |
252b5132 RH |
456 | } |
457 | \f | |
252b5132 RH |
458 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
459 | read the input files, since we need to have a list of all of them | |
460 | before we can determine the final sizes of the output sections. | |
461 | Note that we may actually call this function even though we are not | |
462 | going to output any dynamic symbols; in some cases we know that a | |
463 | symbol should be in the dynamic symbol table, but only if there is | |
464 | one. */ | |
465 | ||
b34976b6 | 466 | bfd_boolean |
c152c796 AM |
467 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
468 | struct elf_link_hash_entry *h) | |
252b5132 RH |
469 | { |
470 | if (h->dynindx == -1) | |
471 | { | |
2b0f7ef9 | 472 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 473 | char *p; |
252b5132 | 474 | const char *name; |
ef53be89 | 475 | size_t indx; |
252b5132 | 476 | |
7a13edea NC |
477 | /* XXX: The ABI draft says the linker must turn hidden and |
478 | internal symbols into STB_LOCAL symbols when producing the | |
479 | DSO. However, if ld.so honors st_other in the dynamic table, | |
480 | this would not be necessary. */ | |
481 | switch (ELF_ST_VISIBILITY (h->other)) | |
482 | { | |
483 | case STV_INTERNAL: | |
484 | case STV_HIDDEN: | |
9d6eee78 L |
485 | if (h->root.type != bfd_link_hash_undefined |
486 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 487 | { |
f5385ebf | 488 | h->forced_local = 1; |
67687978 PB |
489 | if (!elf_hash_table (info)->is_relocatable_executable) |
490 | return TRUE; | |
7a13edea | 491 | } |
0444bdd4 | 492 | |
7a13edea NC |
493 | default: |
494 | break; | |
495 | } | |
496 | ||
252b5132 RH |
497 | h->dynindx = elf_hash_table (info)->dynsymcount; |
498 | ++elf_hash_table (info)->dynsymcount; | |
499 | ||
500 | dynstr = elf_hash_table (info)->dynstr; | |
501 | if (dynstr == NULL) | |
502 | { | |
503 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 504 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 505 | if (dynstr == NULL) |
b34976b6 | 506 | return FALSE; |
252b5132 RH |
507 | } |
508 | ||
509 | /* We don't put any version information in the dynamic string | |
aad5d350 | 510 | table. */ |
252b5132 RH |
511 | name = h->root.root.string; |
512 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
513 | if (p != NULL) |
514 | /* We know that the p points into writable memory. In fact, | |
515 | there are only a few symbols that have read-only names, being | |
516 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
517 | by the backends. Most symbols will have names pointing into | |
518 | an ELF string table read from a file, or to objalloc memory. */ | |
519 | *p = 0; | |
520 | ||
521 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
522 | ||
523 | if (p != NULL) | |
524 | *p = ELF_VER_CHR; | |
252b5132 | 525 | |
ef53be89 | 526 | if (indx == (size_t) -1) |
b34976b6 | 527 | return FALSE; |
252b5132 RH |
528 | h->dynstr_index = indx; |
529 | } | |
530 | ||
b34976b6 | 531 | return TRUE; |
252b5132 | 532 | } |
45d6a902 | 533 | \f |
55255dae L |
534 | /* Mark a symbol dynamic. */ |
535 | ||
28caa186 | 536 | static void |
55255dae | 537 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, |
40b36307 L |
538 | struct elf_link_hash_entry *h, |
539 | Elf_Internal_Sym *sym) | |
55255dae | 540 | { |
40b36307 | 541 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
55255dae | 542 | |
40b36307 | 543 | /* It may be called more than once on the same H. */ |
0e1862bb | 544 | if(h->dynamic || bfd_link_relocatable (info)) |
55255dae L |
545 | return; |
546 | ||
40b36307 L |
547 | if ((info->dynamic_data |
548 | && (h->type == STT_OBJECT | |
b8871f35 | 549 | || h->type == STT_COMMON |
40b36307 | 550 | || (sym != NULL |
b8871f35 L |
551 | && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT |
552 | || ELF_ST_TYPE (sym->st_info) == STT_COMMON)))) | |
a0c8462f | 553 | || (d != NULL |
40b36307 L |
554 | && h->root.type == bfd_link_hash_new |
555 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
55255dae L |
556 | h->dynamic = 1; |
557 | } | |
558 | ||
45d6a902 AM |
559 | /* Record an assignment to a symbol made by a linker script. We need |
560 | this in case some dynamic object refers to this symbol. */ | |
561 | ||
562 | bfd_boolean | |
fe21a8fc L |
563 | bfd_elf_record_link_assignment (bfd *output_bfd, |
564 | struct bfd_link_info *info, | |
268b6b39 | 565 | const char *name, |
fe21a8fc L |
566 | bfd_boolean provide, |
567 | bfd_boolean hidden) | |
45d6a902 | 568 | { |
00cbee0a | 569 | struct elf_link_hash_entry *h, *hv; |
4ea42fb7 | 570 | struct elf_link_hash_table *htab; |
00cbee0a | 571 | const struct elf_backend_data *bed; |
45d6a902 | 572 | |
0eddce27 | 573 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
574 | return TRUE; |
575 | ||
4ea42fb7 AM |
576 | htab = elf_hash_table (info); |
577 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 578 | if (h == NULL) |
4ea42fb7 | 579 | return provide; |
45d6a902 | 580 | |
0f550b3d L |
581 | if (h->versioned == unknown) |
582 | { | |
583 | /* Set versioned if symbol version is unknown. */ | |
584 | char *version = strrchr (name, ELF_VER_CHR); | |
585 | if (version) | |
586 | { | |
587 | if (version > name && version[-1] != ELF_VER_CHR) | |
588 | h->versioned = versioned_hidden; | |
589 | else | |
590 | h->versioned = versioned; | |
591 | } | |
592 | } | |
593 | ||
00cbee0a | 594 | switch (h->root.type) |
77cfaee6 | 595 | { |
00cbee0a L |
596 | case bfd_link_hash_defined: |
597 | case bfd_link_hash_defweak: | |
598 | case bfd_link_hash_common: | |
599 | break; | |
600 | case bfd_link_hash_undefweak: | |
601 | case bfd_link_hash_undefined: | |
602 | /* Since we're defining the symbol, don't let it seem to have not | |
603 | been defined. record_dynamic_symbol and size_dynamic_sections | |
604 | may depend on this. */ | |
4ea42fb7 | 605 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
606 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
607 | bfd_link_repair_undef_list (&htab->root); | |
00cbee0a L |
608 | break; |
609 | case bfd_link_hash_new: | |
40b36307 | 610 | bfd_elf_link_mark_dynamic_symbol (info, h, NULL); |
55255dae | 611 | h->non_elf = 0; |
00cbee0a L |
612 | break; |
613 | case bfd_link_hash_indirect: | |
614 | /* We had a versioned symbol in a dynamic library. We make the | |
a0c8462f | 615 | the versioned symbol point to this one. */ |
00cbee0a L |
616 | bed = get_elf_backend_data (output_bfd); |
617 | hv = h; | |
618 | while (hv->root.type == bfd_link_hash_indirect | |
619 | || hv->root.type == bfd_link_hash_warning) | |
620 | hv = (struct elf_link_hash_entry *) hv->root.u.i.link; | |
621 | /* We don't need to update h->root.u since linker will set them | |
622 | later. */ | |
623 | h->root.type = bfd_link_hash_undefined; | |
624 | hv->root.type = bfd_link_hash_indirect; | |
625 | hv->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
626 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); | |
627 | break; | |
628 | case bfd_link_hash_warning: | |
629 | abort (); | |
630 | break; | |
55255dae | 631 | } |
45d6a902 AM |
632 | |
633 | /* If this symbol is being provided by the linker script, and it is | |
634 | currently defined by a dynamic object, but not by a regular | |
635 | object, then mark it as undefined so that the generic linker will | |
636 | force the correct value. */ | |
637 | if (provide | |
f5385ebf AM |
638 | && h->def_dynamic |
639 | && !h->def_regular) | |
45d6a902 AM |
640 | h->root.type = bfd_link_hash_undefined; |
641 | ||
642 | /* If this symbol is not being provided by the linker script, and it is | |
643 | currently defined by a dynamic object, but not by a regular object, | |
644 | then clear out any version information because the symbol will not be | |
645 | associated with the dynamic object any more. */ | |
646 | if (!provide | |
f5385ebf AM |
647 | && h->def_dynamic |
648 | && !h->def_regular) | |
45d6a902 AM |
649 | h->verinfo.verdef = NULL; |
650 | ||
f5385ebf | 651 | h->def_regular = 1; |
45d6a902 | 652 | |
eb8476a6 | 653 | if (hidden) |
fe21a8fc | 654 | { |
91d6fa6a | 655 | bed = get_elf_backend_data (output_bfd); |
b8297068 AM |
656 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
657 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
fe21a8fc L |
658 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
659 | } | |
660 | ||
6fa3860b PB |
661 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
662 | and executables. */ | |
0e1862bb | 663 | if (!bfd_link_relocatable (info) |
6fa3860b PB |
664 | && h->dynindx != -1 |
665 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
666 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
667 | h->forced_local = 1; | |
668 | ||
f5385ebf AM |
669 | if ((h->def_dynamic |
670 | || h->ref_dynamic | |
6b3b0ab8 L |
671 | || bfd_link_dll (info) |
672 | || elf_hash_table (info)->is_relocatable_executable) | |
45d6a902 AM |
673 | && h->dynindx == -1) |
674 | { | |
c152c796 | 675 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
676 | return FALSE; |
677 | ||
678 | /* If this is a weak defined symbol, and we know a corresponding | |
679 | real symbol from the same dynamic object, make sure the real | |
680 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
681 | if (h->u.weakdef != NULL |
682 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 683 | { |
f6e332e6 | 684 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
685 | return FALSE; |
686 | } | |
687 | } | |
688 | ||
689 | return TRUE; | |
690 | } | |
42751cf3 | 691 | |
8c58d23b AM |
692 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
693 | success, and 2 on a failure caused by attempting to record a symbol | |
694 | in a discarded section, eg. a discarded link-once section symbol. */ | |
695 | ||
696 | int | |
c152c796 AM |
697 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
698 | bfd *input_bfd, | |
699 | long input_indx) | |
8c58d23b AM |
700 | { |
701 | bfd_size_type amt; | |
702 | struct elf_link_local_dynamic_entry *entry; | |
703 | struct elf_link_hash_table *eht; | |
704 | struct elf_strtab_hash *dynstr; | |
ef53be89 | 705 | size_t dynstr_index; |
8c58d23b AM |
706 | char *name; |
707 | Elf_External_Sym_Shndx eshndx; | |
708 | char esym[sizeof (Elf64_External_Sym)]; | |
709 | ||
0eddce27 | 710 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
711 | return 0; |
712 | ||
713 | /* See if the entry exists already. */ | |
714 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
715 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
716 | return 1; | |
717 | ||
718 | amt = sizeof (*entry); | |
a50b1753 | 719 | entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); |
8c58d23b AM |
720 | if (entry == NULL) |
721 | return 0; | |
722 | ||
723 | /* Go find the symbol, so that we can find it's name. */ | |
724 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 725 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
726 | { |
727 | bfd_release (input_bfd, entry); | |
728 | return 0; | |
729 | } | |
730 | ||
731 | if (entry->isym.st_shndx != SHN_UNDEF | |
4fbb74a6 | 732 | && entry->isym.st_shndx < SHN_LORESERVE) |
8c58d23b AM |
733 | { |
734 | asection *s; | |
735 | ||
736 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
737 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
738 | { | |
739 | /* We can still bfd_release here as nothing has done another | |
740 | bfd_alloc. We can't do this later in this function. */ | |
741 | bfd_release (input_bfd, entry); | |
742 | return 2; | |
743 | } | |
744 | } | |
745 | ||
746 | name = (bfd_elf_string_from_elf_section | |
747 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
748 | entry->isym.st_name)); | |
749 | ||
750 | dynstr = elf_hash_table (info)->dynstr; | |
751 | if (dynstr == NULL) | |
752 | { | |
753 | /* Create a strtab to hold the dynamic symbol names. */ | |
754 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
755 | if (dynstr == NULL) | |
756 | return 0; | |
757 | } | |
758 | ||
b34976b6 | 759 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
ef53be89 | 760 | if (dynstr_index == (size_t) -1) |
8c58d23b AM |
761 | return 0; |
762 | entry->isym.st_name = dynstr_index; | |
763 | ||
764 | eht = elf_hash_table (info); | |
765 | ||
766 | entry->next = eht->dynlocal; | |
767 | eht->dynlocal = entry; | |
768 | entry->input_bfd = input_bfd; | |
769 | entry->input_indx = input_indx; | |
770 | eht->dynsymcount++; | |
771 | ||
772 | /* Whatever binding the symbol had before, it's now local. */ | |
773 | entry->isym.st_info | |
774 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
775 | ||
776 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
777 | ||
778 | return 1; | |
779 | } | |
780 | ||
30b30c21 | 781 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 782 | |
30b30c21 | 783 | long |
268b6b39 AM |
784 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
785 | bfd *input_bfd, | |
786 | long input_indx) | |
30b30c21 RH |
787 | { |
788 | struct elf_link_local_dynamic_entry *e; | |
789 | ||
790 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
791 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
792 | return e->dynindx; | |
793 | return -1; | |
794 | } | |
795 | ||
796 | /* This function is used to renumber the dynamic symbols, if some of | |
797 | them are removed because they are marked as local. This is called | |
798 | via elf_link_hash_traverse. */ | |
799 | ||
b34976b6 | 800 | static bfd_boolean |
268b6b39 AM |
801 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
802 | void *data) | |
42751cf3 | 803 | { |
a50b1753 | 804 | size_t *count = (size_t *) data; |
30b30c21 | 805 | |
6fa3860b PB |
806 | if (h->forced_local) |
807 | return TRUE; | |
808 | ||
809 | if (h->dynindx != -1) | |
810 | h->dynindx = ++(*count); | |
811 | ||
812 | return TRUE; | |
813 | } | |
814 | ||
815 | ||
816 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
817 | STB_LOCAL binding. */ | |
818 | ||
819 | static bfd_boolean | |
820 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
821 | void *data) | |
822 | { | |
a50b1753 | 823 | size_t *count = (size_t *) data; |
6fa3860b | 824 | |
6fa3860b PB |
825 | if (!h->forced_local) |
826 | return TRUE; | |
827 | ||
42751cf3 | 828 | if (h->dynindx != -1) |
30b30c21 RH |
829 | h->dynindx = ++(*count); |
830 | ||
b34976b6 | 831 | return TRUE; |
42751cf3 | 832 | } |
30b30c21 | 833 | |
aee6f5b4 AO |
834 | /* Return true if the dynamic symbol for a given section should be |
835 | omitted when creating a shared library. */ | |
836 | bfd_boolean | |
837 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
838 | struct bfd_link_info *info, | |
839 | asection *p) | |
840 | { | |
74541ad4 | 841 | struct elf_link_hash_table *htab; |
ca55926c | 842 | asection *ip; |
74541ad4 | 843 | |
aee6f5b4 AO |
844 | switch (elf_section_data (p)->this_hdr.sh_type) |
845 | { | |
846 | case SHT_PROGBITS: | |
847 | case SHT_NOBITS: | |
848 | /* If sh_type is yet undecided, assume it could be | |
849 | SHT_PROGBITS/SHT_NOBITS. */ | |
850 | case SHT_NULL: | |
74541ad4 AM |
851 | htab = elf_hash_table (info); |
852 | if (p == htab->tls_sec) | |
853 | return FALSE; | |
854 | ||
855 | if (htab->text_index_section != NULL) | |
856 | return p != htab->text_index_section && p != htab->data_index_section; | |
857 | ||
ca55926c | 858 | return (htab->dynobj != NULL |
3d4d4302 | 859 | && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL |
ca55926c | 860 | && ip->output_section == p); |
aee6f5b4 AO |
861 | |
862 | /* There shouldn't be section relative relocations | |
863 | against any other section. */ | |
864 | default: | |
865 | return TRUE; | |
866 | } | |
867 | } | |
868 | ||
062e2358 | 869 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
870 | symbol for each output section, which come first. Next come symbols |
871 | which have been forced to local binding. Then all of the back-end | |
872 | allocated local dynamic syms, followed by the rest of the global | |
873 | symbols. */ | |
30b30c21 | 874 | |
554220db AM |
875 | static unsigned long |
876 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
877 | struct bfd_link_info *info, | |
878 | unsigned long *section_sym_count) | |
30b30c21 RH |
879 | { |
880 | unsigned long dynsymcount = 0; | |
881 | ||
0e1862bb L |
882 | if (bfd_link_pic (info) |
883 | || elf_hash_table (info)->is_relocatable_executable) | |
30b30c21 | 884 | { |
aee6f5b4 | 885 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
886 | asection *p; |
887 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 888 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
889 | && (p->flags & SEC_ALLOC) != 0 |
890 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
891 | elf_section_data (p)->dynindx = ++dynsymcount; | |
74541ad4 AM |
892 | else |
893 | elf_section_data (p)->dynindx = 0; | |
30b30c21 | 894 | } |
554220db | 895 | *section_sym_count = dynsymcount; |
30b30c21 | 896 | |
6fa3860b PB |
897 | elf_link_hash_traverse (elf_hash_table (info), |
898 | elf_link_renumber_local_hash_table_dynsyms, | |
899 | &dynsymcount); | |
900 | ||
30b30c21 RH |
901 | if (elf_hash_table (info)->dynlocal) |
902 | { | |
903 | struct elf_link_local_dynamic_entry *p; | |
904 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
905 | p->dynindx = ++dynsymcount; | |
906 | } | |
90ac2420 | 907 | elf_hash_table (info)->local_dynsymcount = dynsymcount; |
30b30c21 RH |
908 | |
909 | elf_link_hash_traverse (elf_hash_table (info), | |
910 | elf_link_renumber_hash_table_dynsyms, | |
911 | &dynsymcount); | |
912 | ||
d5486c43 L |
913 | /* There is an unused NULL entry at the head of the table which we |
914 | must account for in our count even if the table is empty since it | |
915 | is intended for the mandatory DT_SYMTAB tag (.dynsym section) in | |
916 | .dynamic section. */ | |
917 | dynsymcount++; | |
30b30c21 | 918 | |
ccabcbe5 AM |
919 | elf_hash_table (info)->dynsymcount = dynsymcount; |
920 | return dynsymcount; | |
30b30c21 | 921 | } |
252b5132 | 922 | |
54ac0771 L |
923 | /* Merge st_other field. */ |
924 | ||
925 | static void | |
926 | elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, | |
b8417128 | 927 | const Elf_Internal_Sym *isym, asection *sec, |
cd3416da | 928 | bfd_boolean definition, bfd_boolean dynamic) |
54ac0771 L |
929 | { |
930 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
931 | ||
932 | /* If st_other has a processor-specific meaning, specific | |
cd3416da | 933 | code might be needed here. */ |
54ac0771 L |
934 | if (bed->elf_backend_merge_symbol_attribute) |
935 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
936 | dynamic); | |
937 | ||
cd3416da | 938 | if (!dynamic) |
54ac0771 | 939 | { |
cd3416da AM |
940 | unsigned symvis = ELF_ST_VISIBILITY (isym->st_other); |
941 | unsigned hvis = ELF_ST_VISIBILITY (h->other); | |
54ac0771 | 942 | |
cd3416da AM |
943 | /* Keep the most constraining visibility. Leave the remainder |
944 | of the st_other field to elf_backend_merge_symbol_attribute. */ | |
945 | if (symvis - 1 < hvis - 1) | |
946 | h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); | |
54ac0771 | 947 | } |
b8417128 AM |
948 | else if (definition |
949 | && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT | |
950 | && (sec->flags & SEC_READONLY) == 0) | |
6cabe1ea | 951 | h->protected_def = 1; |
54ac0771 L |
952 | } |
953 | ||
4f3fedcf AM |
954 | /* This function is called when we want to merge a new symbol with an |
955 | existing symbol. It handles the various cases which arise when we | |
956 | find a definition in a dynamic object, or when there is already a | |
957 | definition in a dynamic object. The new symbol is described by | |
958 | NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table | |
959 | entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK | |
960 | if the old symbol was weak. We set POLD_ALIGNMENT to the alignment | |
961 | of an old common symbol. We set OVERRIDE if the old symbol is | |
962 | overriding a new definition. We set TYPE_CHANGE_OK if it is OK for | |
963 | the type to change. We set SIZE_CHANGE_OK if it is OK for the size | |
964 | to change. By OK to change, we mean that we shouldn't warn if the | |
965 | type or size does change. */ | |
45d6a902 | 966 | |
8a56bd02 | 967 | static bfd_boolean |
268b6b39 AM |
968 | _bfd_elf_merge_symbol (bfd *abfd, |
969 | struct bfd_link_info *info, | |
970 | const char *name, | |
971 | Elf_Internal_Sym *sym, | |
972 | asection **psec, | |
973 | bfd_vma *pvalue, | |
4f3fedcf AM |
974 | struct elf_link_hash_entry **sym_hash, |
975 | bfd **poldbfd, | |
37a9e49a | 976 | bfd_boolean *pold_weak, |
af44c138 | 977 | unsigned int *pold_alignment, |
268b6b39 AM |
978 | bfd_boolean *skip, |
979 | bfd_boolean *override, | |
980 | bfd_boolean *type_change_ok, | |
6e33951e L |
981 | bfd_boolean *size_change_ok, |
982 | bfd_boolean *matched) | |
252b5132 | 983 | { |
7479dfd4 | 984 | asection *sec, *oldsec; |
45d6a902 | 985 | struct elf_link_hash_entry *h; |
90c984fc | 986 | struct elf_link_hash_entry *hi; |
45d6a902 AM |
987 | struct elf_link_hash_entry *flip; |
988 | int bind; | |
989 | bfd *oldbfd; | |
990 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
0a36a439 | 991 | bfd_boolean newweak, oldweak, newfunc, oldfunc; |
a4d8e49b | 992 | const struct elf_backend_data *bed; |
6e33951e | 993 | char *new_version; |
45d6a902 AM |
994 | |
995 | *skip = FALSE; | |
996 | *override = FALSE; | |
997 | ||
998 | sec = *psec; | |
999 | bind = ELF_ST_BIND (sym->st_info); | |
1000 | ||
1001 | if (! bfd_is_und_section (sec)) | |
1002 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
1003 | else | |
1004 | h = ((struct elf_link_hash_entry *) | |
1005 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
1006 | if (h == NULL) | |
1007 | return FALSE; | |
1008 | *sym_hash = h; | |
252b5132 | 1009 | |
88ba32a0 L |
1010 | bed = get_elf_backend_data (abfd); |
1011 | ||
6e33951e | 1012 | /* NEW_VERSION is the symbol version of the new symbol. */ |
422f1182 | 1013 | if (h->versioned != unversioned) |
6e33951e | 1014 | { |
422f1182 L |
1015 | /* Symbol version is unknown or versioned. */ |
1016 | new_version = strrchr (name, ELF_VER_CHR); | |
1017 | if (new_version) | |
1018 | { | |
1019 | if (h->versioned == unknown) | |
1020 | { | |
1021 | if (new_version > name && new_version[-1] != ELF_VER_CHR) | |
1022 | h->versioned = versioned_hidden; | |
1023 | else | |
1024 | h->versioned = versioned; | |
1025 | } | |
1026 | new_version += 1; | |
1027 | if (new_version[0] == '\0') | |
1028 | new_version = NULL; | |
1029 | } | |
1030 | else | |
1031 | h->versioned = unversioned; | |
6e33951e | 1032 | } |
422f1182 L |
1033 | else |
1034 | new_version = NULL; | |
6e33951e | 1035 | |
90c984fc L |
1036 | /* For merging, we only care about real symbols. But we need to make |
1037 | sure that indirect symbol dynamic flags are updated. */ | |
1038 | hi = h; | |
45d6a902 AM |
1039 | while (h->root.type == bfd_link_hash_indirect |
1040 | || h->root.type == bfd_link_hash_warning) | |
1041 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1042 | ||
6e33951e L |
1043 | if (!*matched) |
1044 | { | |
1045 | if (hi == h || h->root.type == bfd_link_hash_new) | |
1046 | *matched = TRUE; | |
1047 | else | |
1048 | { | |
ae7683d2 | 1049 | /* OLD_HIDDEN is true if the existing symbol is only visible |
6e33951e | 1050 | to the symbol with the same symbol version. NEW_HIDDEN is |
ae7683d2 | 1051 | true if the new symbol is only visible to the symbol with |
6e33951e | 1052 | the same symbol version. */ |
422f1182 L |
1053 | bfd_boolean old_hidden = h->versioned == versioned_hidden; |
1054 | bfd_boolean new_hidden = hi->versioned == versioned_hidden; | |
6e33951e L |
1055 | if (!old_hidden && !new_hidden) |
1056 | /* The new symbol matches the existing symbol if both | |
1057 | aren't hidden. */ | |
1058 | *matched = TRUE; | |
1059 | else | |
1060 | { | |
1061 | /* OLD_VERSION is the symbol version of the existing | |
1062 | symbol. */ | |
422f1182 L |
1063 | char *old_version; |
1064 | ||
1065 | if (h->versioned >= versioned) | |
1066 | old_version = strrchr (h->root.root.string, | |
1067 | ELF_VER_CHR) + 1; | |
1068 | else | |
1069 | old_version = NULL; | |
6e33951e L |
1070 | |
1071 | /* The new symbol matches the existing symbol if they | |
1072 | have the same symbol version. */ | |
1073 | *matched = (old_version == new_version | |
1074 | || (old_version != NULL | |
1075 | && new_version != NULL | |
1076 | && strcmp (old_version, new_version) == 0)); | |
1077 | } | |
1078 | } | |
1079 | } | |
1080 | ||
934bce08 AM |
1081 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
1082 | existing symbol. */ | |
1083 | ||
1084 | oldbfd = NULL; | |
1085 | oldsec = NULL; | |
1086 | switch (h->root.type) | |
1087 | { | |
1088 | default: | |
1089 | break; | |
1090 | ||
1091 | case bfd_link_hash_undefined: | |
1092 | case bfd_link_hash_undefweak: | |
1093 | oldbfd = h->root.u.undef.abfd; | |
1094 | break; | |
1095 | ||
1096 | case bfd_link_hash_defined: | |
1097 | case bfd_link_hash_defweak: | |
1098 | oldbfd = h->root.u.def.section->owner; | |
1099 | oldsec = h->root.u.def.section; | |
1100 | break; | |
1101 | ||
1102 | case bfd_link_hash_common: | |
1103 | oldbfd = h->root.u.c.p->section->owner; | |
1104 | oldsec = h->root.u.c.p->section; | |
1105 | if (pold_alignment) | |
1106 | *pold_alignment = h->root.u.c.p->alignment_power; | |
1107 | break; | |
1108 | } | |
1109 | if (poldbfd && *poldbfd == NULL) | |
1110 | *poldbfd = oldbfd; | |
1111 | ||
1112 | /* Differentiate strong and weak symbols. */ | |
1113 | newweak = bind == STB_WEAK; | |
1114 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1115 | || h->root.type == bfd_link_hash_undefweak); | |
1116 | if (pold_weak) | |
1117 | *pold_weak = oldweak; | |
1118 | ||
1119 | /* This code is for coping with dynamic objects, and is only useful | |
1120 | if we are doing an ELF link. */ | |
1121 | if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
1122 | return TRUE; | |
1123 | ||
40b36307 | 1124 | /* We have to check it for every instance since the first few may be |
ee659f1f | 1125 | references and not all compilers emit symbol type for undefined |
40b36307 L |
1126 | symbols. */ |
1127 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
1128 | ||
ee659f1f AM |
1129 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, |
1130 | respectively, is from a dynamic object. */ | |
1131 | ||
1132 | newdyn = (abfd->flags & DYNAMIC) != 0; | |
1133 | ||
1134 | /* ref_dynamic_nonweak and dynamic_def flags track actual undefined | |
1135 | syms and defined syms in dynamic libraries respectively. | |
1136 | ref_dynamic on the other hand can be set for a symbol defined in | |
1137 | a dynamic library, and def_dynamic may not be set; When the | |
1138 | definition in a dynamic lib is overridden by a definition in the | |
1139 | executable use of the symbol in the dynamic lib becomes a | |
1140 | reference to the executable symbol. */ | |
1141 | if (newdyn) | |
1142 | { | |
1143 | if (bfd_is_und_section (sec)) | |
1144 | { | |
1145 | if (bind != STB_WEAK) | |
1146 | { | |
1147 | h->ref_dynamic_nonweak = 1; | |
1148 | hi->ref_dynamic_nonweak = 1; | |
1149 | } | |
1150 | } | |
1151 | else | |
1152 | { | |
6e33951e L |
1153 | /* Update the existing symbol only if they match. */ |
1154 | if (*matched) | |
1155 | h->dynamic_def = 1; | |
ee659f1f AM |
1156 | hi->dynamic_def = 1; |
1157 | } | |
1158 | } | |
1159 | ||
45d6a902 AM |
1160 | /* If we just created the symbol, mark it as being an ELF symbol. |
1161 | Other than that, there is nothing to do--there is no merge issue | |
1162 | with a newly defined symbol--so we just return. */ | |
1163 | ||
1164 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 1165 | { |
f5385ebf | 1166 | h->non_elf = 0; |
45d6a902 AM |
1167 | return TRUE; |
1168 | } | |
252b5132 | 1169 | |
45d6a902 AM |
1170 | /* In cases involving weak versioned symbols, we may wind up trying |
1171 | to merge a symbol with itself. Catch that here, to avoid the | |
1172 | confusion that results if we try to override a symbol with | |
1173 | itself. The additional tests catch cases like | |
1174 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
1175 | dynamic object, which we do want to handle here. */ | |
1176 | if (abfd == oldbfd | |
895fa45f | 1177 | && (newweak || oldweak) |
45d6a902 | 1178 | && ((abfd->flags & DYNAMIC) == 0 |
f5385ebf | 1179 | || !h->def_regular)) |
45d6a902 AM |
1180 | return TRUE; |
1181 | ||
707bba77 | 1182 | olddyn = FALSE; |
45d6a902 AM |
1183 | if (oldbfd != NULL) |
1184 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 1185 | else if (oldsec != NULL) |
45d6a902 | 1186 | { |
707bba77 | 1187 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 1188 | indices used by MIPS ELF. */ |
707bba77 | 1189 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 1190 | } |
252b5132 | 1191 | |
45d6a902 AM |
1192 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
1193 | respectively, appear to be a definition rather than reference. */ | |
1194 | ||
707bba77 | 1195 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 1196 | |
707bba77 AM |
1197 | olddef = (h->root.type != bfd_link_hash_undefined |
1198 | && h->root.type != bfd_link_hash_undefweak | |
202ac193 | 1199 | && h->root.type != bfd_link_hash_common); |
45d6a902 | 1200 | |
0a36a439 L |
1201 | /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, |
1202 | respectively, appear to be a function. */ | |
1203 | ||
1204 | newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1205 | && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); | |
1206 | ||
1207 | oldfunc = (h->type != STT_NOTYPE | |
1208 | && bed->is_function_type (h->type)); | |
1209 | ||
5b677558 AM |
1210 | /* If creating a default indirect symbol ("foo" or "foo@") from a |
1211 | dynamic versioned definition ("foo@@") skip doing so if there is | |
1212 | an existing regular definition with a different type. We don't | |
1213 | want, for example, a "time" variable in the executable overriding | |
1214 | a "time" function in a shared library. */ | |
580a2b6e | 1215 | if (pold_alignment == NULL |
580a2b6e L |
1216 | && newdyn |
1217 | && newdef | |
1218 | && !olddyn | |
5b677558 AM |
1219 | && (olddef || h->root.type == bfd_link_hash_common) |
1220 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1221 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1222 | && h->type != STT_NOTYPE | |
1223 | && !(newfunc && oldfunc)) | |
580a2b6e L |
1224 | { |
1225 | *skip = TRUE; | |
1226 | return TRUE; | |
1227 | } | |
1228 | ||
4c34aff8 AM |
1229 | /* Check TLS symbols. We don't check undefined symbols introduced |
1230 | by "ld -u" which have no type (and oldbfd NULL), and we don't | |
1231 | check symbols from plugins because they also have no type. */ | |
1232 | if (oldbfd != NULL | |
1233 | && (oldbfd->flags & BFD_PLUGIN) == 0 | |
1234 | && (abfd->flags & BFD_PLUGIN) == 0 | |
1235 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1236 | && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) | |
7479dfd4 L |
1237 | { |
1238 | bfd *ntbfd, *tbfd; | |
1239 | bfd_boolean ntdef, tdef; | |
1240 | asection *ntsec, *tsec; | |
1241 | ||
1242 | if (h->type == STT_TLS) | |
1243 | { | |
3b36f7e6 | 1244 | ntbfd = abfd; |
7479dfd4 L |
1245 | ntsec = sec; |
1246 | ntdef = newdef; | |
1247 | tbfd = oldbfd; | |
1248 | tsec = oldsec; | |
1249 | tdef = olddef; | |
1250 | } | |
1251 | else | |
1252 | { | |
1253 | ntbfd = oldbfd; | |
1254 | ntsec = oldsec; | |
1255 | ntdef = olddef; | |
1256 | tbfd = abfd; | |
1257 | tsec = sec; | |
1258 | tdef = newdef; | |
1259 | } | |
1260 | ||
1261 | if (tdef && ntdef) | |
1262 | (*_bfd_error_handler) | |
191c0c42 AM |
1263 | (_("%s: TLS definition in %B section %A " |
1264 | "mismatches non-TLS definition in %B section %A"), | |
7479dfd4 L |
1265 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); |
1266 | else if (!tdef && !ntdef) | |
1267 | (*_bfd_error_handler) | |
191c0c42 AM |
1268 | (_("%s: TLS reference in %B " |
1269 | "mismatches non-TLS reference in %B"), | |
7479dfd4 L |
1270 | tbfd, ntbfd, h->root.root.string); |
1271 | else if (tdef) | |
1272 | (*_bfd_error_handler) | |
191c0c42 AM |
1273 | (_("%s: TLS definition in %B section %A " |
1274 | "mismatches non-TLS reference in %B"), | |
7479dfd4 L |
1275 | tbfd, tsec, ntbfd, h->root.root.string); |
1276 | else | |
1277 | (*_bfd_error_handler) | |
191c0c42 AM |
1278 | (_("%s: TLS reference in %B " |
1279 | "mismatches non-TLS definition in %B section %A"), | |
7479dfd4 L |
1280 | tbfd, ntbfd, ntsec, h->root.root.string); |
1281 | ||
1282 | bfd_set_error (bfd_error_bad_value); | |
1283 | return FALSE; | |
1284 | } | |
1285 | ||
45d6a902 AM |
1286 | /* If the old symbol has non-default visibility, we ignore the new |
1287 | definition from a dynamic object. */ | |
1288 | if (newdyn | |
9c7a29a3 | 1289 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1290 | && !bfd_is_und_section (sec)) |
1291 | { | |
1292 | *skip = TRUE; | |
1293 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1294 | h->ref_dynamic = 1; |
90c984fc | 1295 | hi->ref_dynamic = 1; |
45d6a902 AM |
1296 | /* A protected symbol has external availability. Make sure it is |
1297 | recorded as dynamic. | |
1298 | ||
1299 | FIXME: Should we check type and size for protected symbol? */ | |
1300 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1301 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1302 | else |
1303 | return TRUE; | |
1304 | } | |
1305 | else if (!newdyn | |
9c7a29a3 | 1306 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1307 | && h->def_dynamic) |
45d6a902 AM |
1308 | { |
1309 | /* If the new symbol with non-default visibility comes from a | |
1310 | relocatable file and the old definition comes from a dynamic | |
1311 | object, we remove the old definition. */ | |
6c9b78e6 | 1312 | if (hi->root.type == bfd_link_hash_indirect) |
d2dee3b2 L |
1313 | { |
1314 | /* Handle the case where the old dynamic definition is | |
1315 | default versioned. We need to copy the symbol info from | |
1316 | the symbol with default version to the normal one if it | |
1317 | was referenced before. */ | |
1318 | if (h->ref_regular) | |
1319 | { | |
6c9b78e6 | 1320 | hi->root.type = h->root.type; |
d2dee3b2 | 1321 | h->root.type = bfd_link_hash_indirect; |
6c9b78e6 | 1322 | (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); |
aed81c4e | 1323 | |
6c9b78e6 | 1324 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; |
aed81c4e | 1325 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
d2dee3b2 | 1326 | { |
aed81c4e MR |
1327 | /* If the new symbol is hidden or internal, completely undo |
1328 | any dynamic link state. */ | |
1329 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1330 | h->forced_local = 0; | |
1331 | h->ref_dynamic = 0; | |
d2dee3b2 L |
1332 | } |
1333 | else | |
aed81c4e MR |
1334 | h->ref_dynamic = 1; |
1335 | ||
1336 | h->def_dynamic = 0; | |
aed81c4e MR |
1337 | /* FIXME: Should we check type and size for protected symbol? */ |
1338 | h->size = 0; | |
1339 | h->type = 0; | |
1340 | ||
6c9b78e6 | 1341 | h = hi; |
d2dee3b2 L |
1342 | } |
1343 | else | |
6c9b78e6 | 1344 | h = hi; |
d2dee3b2 | 1345 | } |
1de1a317 | 1346 | |
f5eda473 AM |
1347 | /* If the old symbol was undefined before, then it will still be |
1348 | on the undefs list. If the new symbol is undefined or | |
1349 | common, we can't make it bfd_link_hash_new here, because new | |
1350 | undefined or common symbols will be added to the undefs list | |
1351 | by _bfd_generic_link_add_one_symbol. Symbols may not be | |
1352 | added twice to the undefs list. Also, if the new symbol is | |
1353 | undefweak then we don't want to lose the strong undef. */ | |
1354 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1de1a317 | 1355 | { |
1de1a317 | 1356 | h->root.type = bfd_link_hash_undefined; |
1de1a317 L |
1357 | h->root.u.undef.abfd = abfd; |
1358 | } | |
1359 | else | |
1360 | { | |
1361 | h->root.type = bfd_link_hash_new; | |
1362 | h->root.u.undef.abfd = NULL; | |
1363 | } | |
1364 | ||
f5eda473 | 1365 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
252b5132 | 1366 | { |
f5eda473 AM |
1367 | /* If the new symbol is hidden or internal, completely undo |
1368 | any dynamic link state. */ | |
1369 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1370 | h->forced_local = 0; | |
1371 | h->ref_dynamic = 0; | |
45d6a902 | 1372 | } |
f5eda473 AM |
1373 | else |
1374 | h->ref_dynamic = 1; | |
1375 | h->def_dynamic = 0; | |
45d6a902 AM |
1376 | /* FIXME: Should we check type and size for protected symbol? */ |
1377 | h->size = 0; | |
1378 | h->type = 0; | |
1379 | return TRUE; | |
1380 | } | |
14a793b2 | 1381 | |
15b43f48 AM |
1382 | /* If a new weak symbol definition comes from a regular file and the |
1383 | old symbol comes from a dynamic library, we treat the new one as | |
1384 | strong. Similarly, an old weak symbol definition from a regular | |
1385 | file is treated as strong when the new symbol comes from a dynamic | |
1386 | library. Further, an old weak symbol from a dynamic library is | |
1387 | treated as strong if the new symbol is from a dynamic library. | |
1388 | This reflects the way glibc's ld.so works. | |
1389 | ||
1390 | Do this before setting *type_change_ok or *size_change_ok so that | |
1391 | we warn properly when dynamic library symbols are overridden. */ | |
1392 | ||
1393 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1394 | newweak = FALSE; |
15b43f48 | 1395 | if (olddef && newdyn) |
0f8a2703 AM |
1396 | oldweak = FALSE; |
1397 | ||
d334575b | 1398 | /* Allow changes between different types of function symbol. */ |
0a36a439 | 1399 | if (newfunc && oldfunc) |
fcb93ecf PB |
1400 | *type_change_ok = TRUE; |
1401 | ||
79349b09 AM |
1402 | /* It's OK to change the type if either the existing symbol or the |
1403 | new symbol is weak. A type change is also OK if the old symbol | |
1404 | is undefined and the new symbol is defined. */ | |
252b5132 | 1405 | |
79349b09 AM |
1406 | if (oldweak |
1407 | || newweak | |
1408 | || (newdef | |
1409 | && h->root.type == bfd_link_hash_undefined)) | |
1410 | *type_change_ok = TRUE; | |
1411 | ||
1412 | /* It's OK to change the size if either the existing symbol or the | |
1413 | new symbol is weak, or if the old symbol is undefined. */ | |
1414 | ||
1415 | if (*type_change_ok | |
1416 | || h->root.type == bfd_link_hash_undefined) | |
1417 | *size_change_ok = TRUE; | |
45d6a902 | 1418 | |
45d6a902 AM |
1419 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1420 | symbol, respectively, appears to be a common symbol in a dynamic | |
1421 | object. If a symbol appears in an uninitialized section, and is | |
1422 | not weak, and is not a function, then it may be a common symbol | |
1423 | which was resolved when the dynamic object was created. We want | |
1424 | to treat such symbols specially, because they raise special | |
1425 | considerations when setting the symbol size: if the symbol | |
1426 | appears as a common symbol in a regular object, and the size in | |
1427 | the regular object is larger, we must make sure that we use the | |
1428 | larger size. This problematic case can always be avoided in C, | |
1429 | but it must be handled correctly when using Fortran shared | |
1430 | libraries. | |
1431 | ||
1432 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1433 | likewise for OLDDYNCOMMON and OLDDEF. | |
1434 | ||
1435 | Note that this test is just a heuristic, and that it is quite | |
1436 | possible to have an uninitialized symbol in a shared object which | |
1437 | is really a definition, rather than a common symbol. This could | |
1438 | lead to some minor confusion when the symbol really is a common | |
1439 | symbol in some regular object. However, I think it will be | |
1440 | harmless. */ | |
1441 | ||
1442 | if (newdyn | |
1443 | && newdef | |
79349b09 | 1444 | && !newweak |
45d6a902 AM |
1445 | && (sec->flags & SEC_ALLOC) != 0 |
1446 | && (sec->flags & SEC_LOAD) == 0 | |
1447 | && sym->st_size > 0 | |
0a36a439 | 1448 | && !newfunc) |
45d6a902 AM |
1449 | newdyncommon = TRUE; |
1450 | else | |
1451 | newdyncommon = FALSE; | |
1452 | ||
1453 | if (olddyn | |
1454 | && olddef | |
1455 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1456 | && h->def_dynamic |
45d6a902 AM |
1457 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1458 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1459 | && h->size > 0 | |
0a36a439 | 1460 | && !oldfunc) |
45d6a902 AM |
1461 | olddyncommon = TRUE; |
1462 | else | |
1463 | olddyncommon = FALSE; | |
1464 | ||
a4d8e49b L |
1465 | /* We now know everything about the old and new symbols. We ask the |
1466 | backend to check if we can merge them. */ | |
5d13b3b3 AM |
1467 | if (bed->merge_symbol != NULL) |
1468 | { | |
1469 | if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) | |
1470 | return FALSE; | |
1471 | sec = *psec; | |
1472 | } | |
a4d8e49b | 1473 | |
45d6a902 AM |
1474 | /* If both the old and the new symbols look like common symbols in a |
1475 | dynamic object, set the size of the symbol to the larger of the | |
1476 | two. */ | |
1477 | ||
1478 | if (olddyncommon | |
1479 | && newdyncommon | |
1480 | && sym->st_size != h->size) | |
1481 | { | |
1482 | /* Since we think we have two common symbols, issue a multiple | |
1483 | common warning if desired. Note that we only warn if the | |
1484 | size is different. If the size is the same, we simply let | |
1485 | the old symbol override the new one as normally happens with | |
1486 | symbols defined in dynamic objects. */ | |
1487 | ||
1a72702b AM |
1488 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1489 | bfd_link_hash_common, sym->st_size); | |
45d6a902 AM |
1490 | if (sym->st_size > h->size) |
1491 | h->size = sym->st_size; | |
252b5132 | 1492 | |
45d6a902 | 1493 | *size_change_ok = TRUE; |
252b5132 RH |
1494 | } |
1495 | ||
45d6a902 AM |
1496 | /* If we are looking at a dynamic object, and we have found a |
1497 | definition, we need to see if the symbol was already defined by | |
1498 | some other object. If so, we want to use the existing | |
1499 | definition, and we do not want to report a multiple symbol | |
1500 | definition error; we do this by clobbering *PSEC to be | |
1501 | bfd_und_section_ptr. | |
1502 | ||
1503 | We treat a common symbol as a definition if the symbol in the | |
1504 | shared library is a function, since common symbols always | |
1505 | represent variables; this can cause confusion in principle, but | |
1506 | any such confusion would seem to indicate an erroneous program or | |
1507 | shared library. We also permit a common symbol in a regular | |
202ac193 L |
1508 | object to override a weak symbol in a shared object. A common |
1509 | symbol in executable also overrides a symbol in a shared object. */ | |
45d6a902 AM |
1510 | |
1511 | if (newdyn | |
1512 | && newdef | |
77cfaee6 | 1513 | && (olddef |
45d6a902 | 1514 | || (h->root.type == bfd_link_hash_common |
202ac193 L |
1515 | && (newweak |
1516 | || newfunc | |
1517 | || (!olddyn && bfd_link_executable (info)))))) | |
45d6a902 AM |
1518 | { |
1519 | *override = TRUE; | |
1520 | newdef = FALSE; | |
1521 | newdyncommon = FALSE; | |
252b5132 | 1522 | |
45d6a902 AM |
1523 | *psec = sec = bfd_und_section_ptr; |
1524 | *size_change_ok = TRUE; | |
252b5132 | 1525 | |
45d6a902 AM |
1526 | /* If we get here when the old symbol is a common symbol, then |
1527 | we are explicitly letting it override a weak symbol or | |
1528 | function in a dynamic object, and we don't want to warn about | |
1529 | a type change. If the old symbol is a defined symbol, a type | |
1530 | change warning may still be appropriate. */ | |
252b5132 | 1531 | |
45d6a902 AM |
1532 | if (h->root.type == bfd_link_hash_common) |
1533 | *type_change_ok = TRUE; | |
1534 | } | |
1535 | ||
1536 | /* Handle the special case of an old common symbol merging with a | |
1537 | new symbol which looks like a common symbol in a shared object. | |
1538 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1539 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1540 | right thing. */ | |
45d6a902 AM |
1541 | |
1542 | if (newdyncommon | |
1543 | && h->root.type == bfd_link_hash_common) | |
1544 | { | |
1545 | *override = TRUE; | |
1546 | newdef = FALSE; | |
1547 | newdyncommon = FALSE; | |
1548 | *pvalue = sym->st_size; | |
a4d8e49b | 1549 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1550 | *size_change_ok = TRUE; |
1551 | } | |
1552 | ||
c5e2cead | 1553 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1554 | if (newdef && olddef && newweak) |
54ac0771 | 1555 | { |
35ed3f94 | 1556 | /* Don't skip new non-IR weak syms. */ |
3a5dbfb2 AM |
1557 | if (!(oldbfd != NULL |
1558 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
35ed3f94 | 1559 | && (abfd->flags & BFD_PLUGIN) == 0)) |
57fa7b8c AM |
1560 | { |
1561 | newdef = FALSE; | |
1562 | *skip = TRUE; | |
1563 | } | |
54ac0771 L |
1564 | |
1565 | /* Merge st_other. If the symbol already has a dynamic index, | |
1566 | but visibility says it should not be visible, turn it into a | |
1567 | local symbol. */ | |
b8417128 | 1568 | elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn); |
54ac0771 L |
1569 | if (h->dynindx != -1) |
1570 | switch (ELF_ST_VISIBILITY (h->other)) | |
1571 | { | |
1572 | case STV_INTERNAL: | |
1573 | case STV_HIDDEN: | |
1574 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1575 | break; | |
1576 | } | |
1577 | } | |
c5e2cead | 1578 | |
45d6a902 AM |
1579 | /* If the old symbol is from a dynamic object, and the new symbol is |
1580 | a definition which is not from a dynamic object, then the new | |
1581 | symbol overrides the old symbol. Symbols from regular files | |
1582 | always take precedence over symbols from dynamic objects, even if | |
1583 | they are defined after the dynamic object in the link. | |
1584 | ||
1585 | As above, we again permit a common symbol in a regular object to | |
1586 | override a definition in a shared object if the shared object | |
0f8a2703 | 1587 | symbol is a function or is weak. */ |
45d6a902 AM |
1588 | |
1589 | flip = NULL; | |
77cfaee6 | 1590 | if (!newdyn |
45d6a902 AM |
1591 | && (newdef |
1592 | || (bfd_is_com_section (sec) | |
0a36a439 | 1593 | && (oldweak || oldfunc))) |
45d6a902 AM |
1594 | && olddyn |
1595 | && olddef | |
f5385ebf | 1596 | && h->def_dynamic) |
45d6a902 AM |
1597 | { |
1598 | /* Change the hash table entry to undefined, and let | |
1599 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1600 | new definition. */ | |
1601 | ||
1602 | h->root.type = bfd_link_hash_undefined; | |
1603 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1604 | *size_change_ok = TRUE; | |
1605 | ||
1606 | olddef = FALSE; | |
1607 | olddyncommon = FALSE; | |
1608 | ||
1609 | /* We again permit a type change when a common symbol may be | |
1610 | overriding a function. */ | |
1611 | ||
1612 | if (bfd_is_com_section (sec)) | |
0a36a439 L |
1613 | { |
1614 | if (oldfunc) | |
1615 | { | |
1616 | /* If a common symbol overrides a function, make sure | |
1617 | that it isn't defined dynamically nor has type | |
1618 | function. */ | |
1619 | h->def_dynamic = 0; | |
1620 | h->type = STT_NOTYPE; | |
1621 | } | |
1622 | *type_change_ok = TRUE; | |
1623 | } | |
45d6a902 | 1624 | |
6c9b78e6 AM |
1625 | if (hi->root.type == bfd_link_hash_indirect) |
1626 | flip = hi; | |
45d6a902 AM |
1627 | else |
1628 | /* This union may have been set to be non-NULL when this symbol | |
1629 | was seen in a dynamic object. We must force the union to be | |
1630 | NULL, so that it is correct for a regular symbol. */ | |
1631 | h->verinfo.vertree = NULL; | |
1632 | } | |
1633 | ||
1634 | /* Handle the special case of a new common symbol merging with an | |
1635 | old symbol that looks like it might be a common symbol defined in | |
1636 | a shared object. Note that we have already handled the case in | |
1637 | which a new common symbol should simply override the definition | |
1638 | in the shared library. */ | |
1639 | ||
1640 | if (! newdyn | |
1641 | && bfd_is_com_section (sec) | |
1642 | && olddyncommon) | |
1643 | { | |
1644 | /* It would be best if we could set the hash table entry to a | |
1645 | common symbol, but we don't know what to use for the section | |
1646 | or the alignment. */ | |
1a72702b AM |
1647 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1648 | bfd_link_hash_common, sym->st_size); | |
45d6a902 | 1649 | |
4cc11e76 | 1650 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1651 | larger, pretend that the new symbol has its size. */ |
1652 | ||
1653 | if (h->size > *pvalue) | |
1654 | *pvalue = h->size; | |
1655 | ||
af44c138 L |
1656 | /* We need to remember the alignment required by the symbol |
1657 | in the dynamic object. */ | |
1658 | BFD_ASSERT (pold_alignment); | |
1659 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1660 | |
1661 | olddef = FALSE; | |
1662 | olddyncommon = FALSE; | |
1663 | ||
1664 | h->root.type = bfd_link_hash_undefined; | |
1665 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1666 | ||
1667 | *size_change_ok = TRUE; | |
1668 | *type_change_ok = TRUE; | |
1669 | ||
6c9b78e6 AM |
1670 | if (hi->root.type == bfd_link_hash_indirect) |
1671 | flip = hi; | |
45d6a902 AM |
1672 | else |
1673 | h->verinfo.vertree = NULL; | |
1674 | } | |
1675 | ||
1676 | if (flip != NULL) | |
1677 | { | |
1678 | /* Handle the case where we had a versioned symbol in a dynamic | |
1679 | library and now find a definition in a normal object. In this | |
1680 | case, we make the versioned symbol point to the normal one. */ | |
45d6a902 | 1681 | flip->root.type = h->root.type; |
00cbee0a | 1682 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1683 | h->root.type = bfd_link_hash_indirect; |
1684 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1685 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1686 | if (h->def_dynamic) |
45d6a902 | 1687 | { |
f5385ebf AM |
1688 | h->def_dynamic = 0; |
1689 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1690 | } |
1691 | } | |
1692 | ||
45d6a902 AM |
1693 | return TRUE; |
1694 | } | |
1695 | ||
1696 | /* This function is called to create an indirect symbol from the | |
1697 | default for the symbol with the default version if needed. The | |
4f3fedcf | 1698 | symbol is described by H, NAME, SYM, SEC, and VALUE. We |
0f8a2703 | 1699 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 | 1700 | |
28caa186 | 1701 | static bfd_boolean |
268b6b39 AM |
1702 | _bfd_elf_add_default_symbol (bfd *abfd, |
1703 | struct bfd_link_info *info, | |
1704 | struct elf_link_hash_entry *h, | |
1705 | const char *name, | |
1706 | Elf_Internal_Sym *sym, | |
4f3fedcf AM |
1707 | asection *sec, |
1708 | bfd_vma value, | |
1709 | bfd **poldbfd, | |
e3c9d234 | 1710 | bfd_boolean *dynsym) |
45d6a902 AM |
1711 | { |
1712 | bfd_boolean type_change_ok; | |
1713 | bfd_boolean size_change_ok; | |
1714 | bfd_boolean skip; | |
1715 | char *shortname; | |
1716 | struct elf_link_hash_entry *hi; | |
1717 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1718 | const struct elf_backend_data *bed; |
45d6a902 AM |
1719 | bfd_boolean collect; |
1720 | bfd_boolean dynamic; | |
e3c9d234 | 1721 | bfd_boolean override; |
45d6a902 AM |
1722 | char *p; |
1723 | size_t len, shortlen; | |
ffd65175 | 1724 | asection *tmp_sec; |
6e33951e | 1725 | bfd_boolean matched; |
45d6a902 | 1726 | |
422f1182 L |
1727 | if (h->versioned == unversioned || h->versioned == versioned_hidden) |
1728 | return TRUE; | |
1729 | ||
45d6a902 AM |
1730 | /* If this symbol has a version, and it is the default version, we |
1731 | create an indirect symbol from the default name to the fully | |
1732 | decorated name. This will cause external references which do not | |
1733 | specify a version to be bound to this version of the symbol. */ | |
1734 | p = strchr (name, ELF_VER_CHR); | |
422f1182 L |
1735 | if (h->versioned == unknown) |
1736 | { | |
1737 | if (p == NULL) | |
1738 | { | |
1739 | h->versioned = unversioned; | |
1740 | return TRUE; | |
1741 | } | |
1742 | else | |
1743 | { | |
1744 | if (p[1] != ELF_VER_CHR) | |
1745 | { | |
1746 | h->versioned = versioned_hidden; | |
1747 | return TRUE; | |
1748 | } | |
1749 | else | |
1750 | h->versioned = versioned; | |
1751 | } | |
1752 | } | |
4373f8af L |
1753 | else |
1754 | { | |
1755 | /* PR ld/19073: We may see an unversioned definition after the | |
1756 | default version. */ | |
1757 | if (p == NULL) | |
1758 | return TRUE; | |
1759 | } | |
45d6a902 | 1760 | |
45d6a902 AM |
1761 | bed = get_elf_backend_data (abfd); |
1762 | collect = bed->collect; | |
1763 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1764 | ||
1765 | shortlen = p - name; | |
a50b1753 | 1766 | shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); |
45d6a902 AM |
1767 | if (shortname == NULL) |
1768 | return FALSE; | |
1769 | memcpy (shortname, name, shortlen); | |
1770 | shortname[shortlen] = '\0'; | |
1771 | ||
1772 | /* We are going to create a new symbol. Merge it with any existing | |
1773 | symbol with this name. For the purposes of the merge, act as | |
1774 | though we were defining the symbol we just defined, although we | |
1775 | actually going to define an indirect symbol. */ | |
1776 | type_change_ok = FALSE; | |
1777 | size_change_ok = FALSE; | |
6e33951e | 1778 | matched = TRUE; |
ffd65175 AM |
1779 | tmp_sec = sec; |
1780 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
4f3fedcf | 1781 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 1782 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
1783 | return FALSE; |
1784 | ||
1785 | if (skip) | |
1786 | goto nondefault; | |
1787 | ||
5b677558 AM |
1788 | if (hi->def_regular) |
1789 | { | |
1790 | /* If the undecorated symbol will have a version added by a | |
1791 | script different to H, then don't indirect to/from the | |
1792 | undecorated symbol. This isn't ideal because we may not yet | |
1793 | have seen symbol versions, if given by a script on the | |
1794 | command line rather than via --version-script. */ | |
1795 | if (hi->verinfo.vertree == NULL && info->version_info != NULL) | |
1796 | { | |
1797 | bfd_boolean hide; | |
1798 | ||
1799 | hi->verinfo.vertree | |
1800 | = bfd_find_version_for_sym (info->version_info, | |
1801 | hi->root.root.string, &hide); | |
1802 | if (hi->verinfo.vertree != NULL && hide) | |
1803 | { | |
1804 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
1805 | goto nondefault; | |
1806 | } | |
1807 | } | |
1808 | if (hi->verinfo.vertree != NULL | |
1809 | && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0) | |
1810 | goto nondefault; | |
1811 | } | |
1812 | ||
45d6a902 AM |
1813 | if (! override) |
1814 | { | |
c6e8a9a8 | 1815 | /* Add the default symbol if not performing a relocatable link. */ |
0e1862bb | 1816 | if (! bfd_link_relocatable (info)) |
c6e8a9a8 L |
1817 | { |
1818 | bh = &hi->root; | |
1819 | if (! (_bfd_generic_link_add_one_symbol | |
1820 | (info, abfd, shortname, BSF_INDIRECT, | |
1821 | bfd_ind_section_ptr, | |
1822 | 0, name, FALSE, collect, &bh))) | |
1823 | return FALSE; | |
1824 | hi = (struct elf_link_hash_entry *) bh; | |
1825 | } | |
45d6a902 AM |
1826 | } |
1827 | else | |
1828 | { | |
1829 | /* In this case the symbol named SHORTNAME is overriding the | |
1830 | indirect symbol we want to add. We were planning on making | |
1831 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1832 | is the name without a version. NAME is the fully versioned | |
1833 | name, and it is the default version. | |
1834 | ||
1835 | Overriding means that we already saw a definition for the | |
1836 | symbol SHORTNAME in a regular object, and it is overriding | |
1837 | the symbol defined in the dynamic object. | |
1838 | ||
1839 | When this happens, we actually want to change NAME, the | |
1840 | symbol we just added, to refer to SHORTNAME. This will cause | |
1841 | references to NAME in the shared object to become references | |
1842 | to SHORTNAME in the regular object. This is what we expect | |
1843 | when we override a function in a shared object: that the | |
1844 | references in the shared object will be mapped to the | |
1845 | definition in the regular object. */ | |
1846 | ||
1847 | while (hi->root.type == bfd_link_hash_indirect | |
1848 | || hi->root.type == bfd_link_hash_warning) | |
1849 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1850 | ||
1851 | h->root.type = bfd_link_hash_indirect; | |
1852 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1853 | if (h->def_dynamic) |
45d6a902 | 1854 | { |
f5385ebf AM |
1855 | h->def_dynamic = 0; |
1856 | hi->ref_dynamic = 1; | |
1857 | if (hi->ref_regular | |
1858 | || hi->def_regular) | |
45d6a902 | 1859 | { |
c152c796 | 1860 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1861 | return FALSE; |
1862 | } | |
1863 | } | |
1864 | ||
1865 | /* Now set HI to H, so that the following code will set the | |
1866 | other fields correctly. */ | |
1867 | hi = h; | |
1868 | } | |
1869 | ||
fab4a87f L |
1870 | /* Check if HI is a warning symbol. */ |
1871 | if (hi->root.type == bfd_link_hash_warning) | |
1872 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1873 | ||
45d6a902 AM |
1874 | /* If there is a duplicate definition somewhere, then HI may not |
1875 | point to an indirect symbol. We will have reported an error to | |
1876 | the user in that case. */ | |
1877 | ||
1878 | if (hi->root.type == bfd_link_hash_indirect) | |
1879 | { | |
1880 | struct elf_link_hash_entry *ht; | |
1881 | ||
45d6a902 | 1882 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 1883 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 | 1884 | |
68c88cd4 AM |
1885 | /* A reference to the SHORTNAME symbol from a dynamic library |
1886 | will be satisfied by the versioned symbol at runtime. In | |
1887 | effect, we have a reference to the versioned symbol. */ | |
1888 | ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; | |
1889 | hi->dynamic_def |= ht->dynamic_def; | |
1890 | ||
45d6a902 AM |
1891 | /* See if the new flags lead us to realize that the symbol must |
1892 | be dynamic. */ | |
1893 | if (! *dynsym) | |
1894 | { | |
1895 | if (! dynamic) | |
1896 | { | |
0e1862bb | 1897 | if (! bfd_link_executable (info) |
90c984fc | 1898 | || hi->def_dynamic |
f5385ebf | 1899 | || hi->ref_dynamic) |
45d6a902 AM |
1900 | *dynsym = TRUE; |
1901 | } | |
1902 | else | |
1903 | { | |
f5385ebf | 1904 | if (hi->ref_regular) |
45d6a902 AM |
1905 | *dynsym = TRUE; |
1906 | } | |
1907 | } | |
1908 | } | |
1909 | ||
1910 | /* We also need to define an indirection from the nondefault version | |
1911 | of the symbol. */ | |
1912 | ||
1913 | nondefault: | |
1914 | len = strlen (name); | |
a50b1753 | 1915 | shortname = (char *) bfd_hash_allocate (&info->hash->table, len); |
45d6a902 AM |
1916 | if (shortname == NULL) |
1917 | return FALSE; | |
1918 | memcpy (shortname, name, shortlen); | |
1919 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1920 | ||
1921 | /* Once again, merge with any existing symbol. */ | |
1922 | type_change_ok = FALSE; | |
1923 | size_change_ok = FALSE; | |
ffd65175 AM |
1924 | tmp_sec = sec; |
1925 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
115c6d5c | 1926 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 1927 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
1928 | return FALSE; |
1929 | ||
1930 | if (skip) | |
1931 | return TRUE; | |
1932 | ||
1933 | if (override) | |
1934 | { | |
1935 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1936 | the type of override we do in the case above unless it is | |
4cc11e76 | 1937 | overridden by a versioned definition. */ |
45d6a902 AM |
1938 | if (hi->root.type != bfd_link_hash_defined |
1939 | && hi->root.type != bfd_link_hash_defweak) | |
1940 | (*_bfd_error_handler) | |
d003868e AM |
1941 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1942 | abfd, shortname); | |
45d6a902 AM |
1943 | } |
1944 | else | |
1945 | { | |
1946 | bh = &hi->root; | |
1947 | if (! (_bfd_generic_link_add_one_symbol | |
1948 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1949 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1950 | return FALSE; |
1951 | hi = (struct elf_link_hash_entry *) bh; | |
1952 | ||
1953 | /* If there is a duplicate definition somewhere, then HI may not | |
1954 | point to an indirect symbol. We will have reported an error | |
1955 | to the user in that case. */ | |
1956 | ||
1957 | if (hi->root.type == bfd_link_hash_indirect) | |
1958 | { | |
fcfa13d2 | 1959 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
68c88cd4 AM |
1960 | h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; |
1961 | hi->dynamic_def |= h->dynamic_def; | |
45d6a902 AM |
1962 | |
1963 | /* See if the new flags lead us to realize that the symbol | |
1964 | must be dynamic. */ | |
1965 | if (! *dynsym) | |
1966 | { | |
1967 | if (! dynamic) | |
1968 | { | |
0e1862bb | 1969 | if (! bfd_link_executable (info) |
f5385ebf | 1970 | || hi->ref_dynamic) |
45d6a902 AM |
1971 | *dynsym = TRUE; |
1972 | } | |
1973 | else | |
1974 | { | |
f5385ebf | 1975 | if (hi->ref_regular) |
45d6a902 AM |
1976 | *dynsym = TRUE; |
1977 | } | |
1978 | } | |
1979 | } | |
1980 | } | |
1981 | ||
1982 | return TRUE; | |
1983 | } | |
1984 | \f | |
1985 | /* This routine is used to export all defined symbols into the dynamic | |
1986 | symbol table. It is called via elf_link_hash_traverse. */ | |
1987 | ||
28caa186 | 1988 | static bfd_boolean |
268b6b39 | 1989 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1990 | { |
a50b1753 | 1991 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 AM |
1992 | |
1993 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
1994 | if (h->root.type == bfd_link_hash_indirect) | |
1995 | return TRUE; | |
1996 | ||
7686d77d AM |
1997 | /* Ignore this if we won't export it. */ |
1998 | if (!eif->info->export_dynamic && !h->dynamic) | |
1999 | return TRUE; | |
45d6a902 AM |
2000 | |
2001 | if (h->dynindx == -1 | |
fd91d419 L |
2002 | && (h->def_regular || h->ref_regular) |
2003 | && ! bfd_hide_sym_by_version (eif->info->version_info, | |
2004 | h->root.root.string)) | |
45d6a902 | 2005 | { |
fd91d419 | 2006 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 | 2007 | { |
fd91d419 L |
2008 | eif->failed = TRUE; |
2009 | return FALSE; | |
45d6a902 AM |
2010 | } |
2011 | } | |
2012 | ||
2013 | return TRUE; | |
2014 | } | |
2015 | \f | |
2016 | /* Look through the symbols which are defined in other shared | |
2017 | libraries and referenced here. Update the list of version | |
2018 | dependencies. This will be put into the .gnu.version_r section. | |
2019 | This function is called via elf_link_hash_traverse. */ | |
2020 | ||
28caa186 | 2021 | static bfd_boolean |
268b6b39 AM |
2022 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
2023 | void *data) | |
45d6a902 | 2024 | { |
a50b1753 | 2025 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; |
45d6a902 AM |
2026 | Elf_Internal_Verneed *t; |
2027 | Elf_Internal_Vernaux *a; | |
2028 | bfd_size_type amt; | |
2029 | ||
45d6a902 AM |
2030 | /* We only care about symbols defined in shared objects with version |
2031 | information. */ | |
f5385ebf AM |
2032 | if (!h->def_dynamic |
2033 | || h->def_regular | |
45d6a902 | 2034 | || h->dynindx == -1 |
7b20f099 AM |
2035 | || h->verinfo.verdef == NULL |
2036 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
2037 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
45d6a902 AM |
2038 | return TRUE; |
2039 | ||
2040 | /* See if we already know about this version. */ | |
28caa186 AM |
2041 | for (t = elf_tdata (rinfo->info->output_bfd)->verref; |
2042 | t != NULL; | |
2043 | t = t->vn_nextref) | |
45d6a902 AM |
2044 | { |
2045 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
2046 | continue; | |
2047 | ||
2048 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2049 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
2050 | return TRUE; | |
2051 | ||
2052 | break; | |
2053 | } | |
2054 | ||
2055 | /* This is a new version. Add it to tree we are building. */ | |
2056 | ||
2057 | if (t == NULL) | |
2058 | { | |
2059 | amt = sizeof *t; | |
a50b1753 | 2060 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); |
45d6a902 AM |
2061 | if (t == NULL) |
2062 | { | |
2063 | rinfo->failed = TRUE; | |
2064 | return FALSE; | |
2065 | } | |
2066 | ||
2067 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
28caa186 AM |
2068 | t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; |
2069 | elf_tdata (rinfo->info->output_bfd)->verref = t; | |
45d6a902 AM |
2070 | } |
2071 | ||
2072 | amt = sizeof *a; | |
a50b1753 | 2073 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); |
14b1c01e AM |
2074 | if (a == NULL) |
2075 | { | |
2076 | rinfo->failed = TRUE; | |
2077 | return FALSE; | |
2078 | } | |
45d6a902 AM |
2079 | |
2080 | /* Note that we are copying a string pointer here, and testing it | |
2081 | above. If bfd_elf_string_from_elf_section is ever changed to | |
2082 | discard the string data when low in memory, this will have to be | |
2083 | fixed. */ | |
2084 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
2085 | ||
2086 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
2087 | a->vna_nextptr = t->vn_auxptr; | |
2088 | ||
2089 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
2090 | ++rinfo->vers; | |
2091 | ||
2092 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
2093 | ||
2094 | t->vn_auxptr = a; | |
2095 | ||
2096 | return TRUE; | |
2097 | } | |
2098 | ||
2099 | /* Figure out appropriate versions for all the symbols. We may not | |
2100 | have the version number script until we have read all of the input | |
2101 | files, so until that point we don't know which symbols should be | |
2102 | local. This function is called via elf_link_hash_traverse. */ | |
2103 | ||
28caa186 | 2104 | static bfd_boolean |
268b6b39 | 2105 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2106 | { |
28caa186 | 2107 | struct elf_info_failed *sinfo; |
45d6a902 | 2108 | struct bfd_link_info *info; |
9c5bfbb7 | 2109 | const struct elf_backend_data *bed; |
45d6a902 AM |
2110 | struct elf_info_failed eif; |
2111 | char *p; | |
45d6a902 | 2112 | |
a50b1753 | 2113 | sinfo = (struct elf_info_failed *) data; |
45d6a902 AM |
2114 | info = sinfo->info; |
2115 | ||
45d6a902 AM |
2116 | /* Fix the symbol flags. */ |
2117 | eif.failed = FALSE; | |
2118 | eif.info = info; | |
2119 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
2120 | { | |
2121 | if (eif.failed) | |
2122 | sinfo->failed = TRUE; | |
2123 | return FALSE; | |
2124 | } | |
2125 | ||
2126 | /* We only need version numbers for symbols defined in regular | |
2127 | objects. */ | |
f5385ebf | 2128 | if (!h->def_regular) |
45d6a902 AM |
2129 | return TRUE; |
2130 | ||
28caa186 | 2131 | bed = get_elf_backend_data (info->output_bfd); |
45d6a902 AM |
2132 | p = strchr (h->root.root.string, ELF_VER_CHR); |
2133 | if (p != NULL && h->verinfo.vertree == NULL) | |
2134 | { | |
2135 | struct bfd_elf_version_tree *t; | |
45d6a902 | 2136 | |
45d6a902 AM |
2137 | ++p; |
2138 | if (*p == ELF_VER_CHR) | |
6e33951e | 2139 | ++p; |
45d6a902 AM |
2140 | |
2141 | /* If there is no version string, we can just return out. */ | |
2142 | if (*p == '\0') | |
6e33951e | 2143 | return TRUE; |
45d6a902 AM |
2144 | |
2145 | /* Look for the version. If we find it, it is no longer weak. */ | |
fd91d419 | 2146 | for (t = sinfo->info->version_info; t != NULL; t = t->next) |
45d6a902 AM |
2147 | { |
2148 | if (strcmp (t->name, p) == 0) | |
2149 | { | |
2150 | size_t len; | |
2151 | char *alc; | |
2152 | struct bfd_elf_version_expr *d; | |
2153 | ||
2154 | len = p - h->root.root.string; | |
a50b1753 | 2155 | alc = (char *) bfd_malloc (len); |
45d6a902 | 2156 | if (alc == NULL) |
14b1c01e AM |
2157 | { |
2158 | sinfo->failed = TRUE; | |
2159 | return FALSE; | |
2160 | } | |
45d6a902 AM |
2161 | memcpy (alc, h->root.root.string, len - 1); |
2162 | alc[len - 1] = '\0'; | |
2163 | if (alc[len - 2] == ELF_VER_CHR) | |
2164 | alc[len - 2] = '\0'; | |
2165 | ||
2166 | h->verinfo.vertree = t; | |
2167 | t->used = TRUE; | |
2168 | d = NULL; | |
2169 | ||
108ba305 JJ |
2170 | if (t->globals.list != NULL) |
2171 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
2172 | |
2173 | /* See if there is anything to force this symbol to | |
2174 | local scope. */ | |
108ba305 | 2175 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 2176 | { |
108ba305 JJ |
2177 | d = (*t->match) (&t->locals, NULL, alc); |
2178 | if (d != NULL | |
2179 | && h->dynindx != -1 | |
108ba305 JJ |
2180 | && ! info->export_dynamic) |
2181 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2182 | } |
2183 | ||
2184 | free (alc); | |
2185 | break; | |
2186 | } | |
2187 | } | |
2188 | ||
2189 | /* If we are building an application, we need to create a | |
2190 | version node for this version. */ | |
0e1862bb | 2191 | if (t == NULL && bfd_link_executable (info)) |
45d6a902 AM |
2192 | { |
2193 | struct bfd_elf_version_tree **pp; | |
2194 | int version_index; | |
2195 | ||
2196 | /* If we aren't going to export this symbol, we don't need | |
2197 | to worry about it. */ | |
2198 | if (h->dynindx == -1) | |
2199 | return TRUE; | |
2200 | ||
ef53be89 AM |
2201 | t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, |
2202 | sizeof *t); | |
45d6a902 AM |
2203 | if (t == NULL) |
2204 | { | |
2205 | sinfo->failed = TRUE; | |
2206 | return FALSE; | |
2207 | } | |
2208 | ||
45d6a902 | 2209 | t->name = p; |
45d6a902 AM |
2210 | t->name_indx = (unsigned int) -1; |
2211 | t->used = TRUE; | |
2212 | ||
2213 | version_index = 1; | |
2214 | /* Don't count anonymous version tag. */ | |
fd91d419 L |
2215 | if (sinfo->info->version_info != NULL |
2216 | && sinfo->info->version_info->vernum == 0) | |
45d6a902 | 2217 | version_index = 0; |
fd91d419 L |
2218 | for (pp = &sinfo->info->version_info; |
2219 | *pp != NULL; | |
2220 | pp = &(*pp)->next) | |
45d6a902 AM |
2221 | ++version_index; |
2222 | t->vernum = version_index; | |
2223 | ||
2224 | *pp = t; | |
2225 | ||
2226 | h->verinfo.vertree = t; | |
2227 | } | |
2228 | else if (t == NULL) | |
2229 | { | |
2230 | /* We could not find the version for a symbol when | |
2231 | generating a shared archive. Return an error. */ | |
2232 | (*_bfd_error_handler) | |
c55fe096 | 2233 | (_("%B: version node not found for symbol %s"), |
28caa186 | 2234 | info->output_bfd, h->root.root.string); |
45d6a902 AM |
2235 | bfd_set_error (bfd_error_bad_value); |
2236 | sinfo->failed = TRUE; | |
2237 | return FALSE; | |
2238 | } | |
45d6a902 AM |
2239 | } |
2240 | ||
2241 | /* If we don't have a version for this symbol, see if we can find | |
2242 | something. */ | |
fd91d419 | 2243 | if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL) |
45d6a902 | 2244 | { |
1e8fa21e | 2245 | bfd_boolean hide; |
ae5a3597 | 2246 | |
fd91d419 L |
2247 | h->verinfo.vertree |
2248 | = bfd_find_version_for_sym (sinfo->info->version_info, | |
2249 | h->root.root.string, &hide); | |
1e8fa21e AM |
2250 | if (h->verinfo.vertree != NULL && hide) |
2251 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2252 | } |
2253 | ||
2254 | return TRUE; | |
2255 | } | |
2256 | \f | |
45d6a902 AM |
2257 | /* Read and swap the relocs from the section indicated by SHDR. This |
2258 | may be either a REL or a RELA section. The relocations are | |
2259 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2260 | which should have already been allocated to contain enough space. | |
2261 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2262 | relocations should be stored. | |
2263 | ||
2264 | Returns FALSE if something goes wrong. */ | |
2265 | ||
2266 | static bfd_boolean | |
268b6b39 | 2267 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2268 | asection *sec, |
268b6b39 AM |
2269 | Elf_Internal_Shdr *shdr, |
2270 | void *external_relocs, | |
2271 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2272 | { |
9c5bfbb7 | 2273 | const struct elf_backend_data *bed; |
268b6b39 | 2274 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2275 | const bfd_byte *erela; |
2276 | const bfd_byte *erelaend; | |
2277 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2278 | Elf_Internal_Shdr *symtab_hdr; |
2279 | size_t nsyms; | |
45d6a902 | 2280 | |
45d6a902 AM |
2281 | /* Position ourselves at the start of the section. */ |
2282 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2283 | return FALSE; | |
2284 | ||
2285 | /* Read the relocations. */ | |
2286 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2287 | return FALSE; | |
2288 | ||
243ef1e0 | 2289 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
ce98a316 | 2290 | nsyms = NUM_SHDR_ENTRIES (symtab_hdr); |
243ef1e0 | 2291 | |
45d6a902 AM |
2292 | bed = get_elf_backend_data (abfd); |
2293 | ||
2294 | /* Convert the external relocations to the internal format. */ | |
2295 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2296 | swap_in = bed->s->swap_reloc_in; | |
2297 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2298 | swap_in = bed->s->swap_reloca_in; | |
2299 | else | |
2300 | { | |
2301 | bfd_set_error (bfd_error_wrong_format); | |
2302 | return FALSE; | |
2303 | } | |
2304 | ||
a50b1753 | 2305 | erela = (const bfd_byte *) external_relocs; |
51992aec | 2306 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2307 | irela = internal_relocs; |
2308 | while (erela < erelaend) | |
2309 | { | |
243ef1e0 L |
2310 | bfd_vma r_symndx; |
2311 | ||
45d6a902 | 2312 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2313 | r_symndx = ELF32_R_SYM (irela->r_info); |
2314 | if (bed->s->arch_size == 64) | |
2315 | r_symndx >>= 24; | |
ce98a316 NC |
2316 | if (nsyms > 0) |
2317 | { | |
2318 | if ((size_t) r_symndx >= nsyms) | |
2319 | { | |
2320 | (*_bfd_error_handler) | |
2321 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" | |
2322 | " for offset 0x%lx in section `%A'"), | |
2323 | abfd, sec, | |
2324 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
2325 | bfd_set_error (bfd_error_bad_value); | |
2326 | return FALSE; | |
2327 | } | |
2328 | } | |
cf35638d | 2329 | else if (r_symndx != STN_UNDEF) |
243ef1e0 L |
2330 | { |
2331 | (*_bfd_error_handler) | |
ce98a316 NC |
2332 | (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'" |
2333 | " when the object file has no symbol table"), | |
d003868e AM |
2334 | abfd, sec, |
2335 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
2336 | bfd_set_error (bfd_error_bad_value); |
2337 | return FALSE; | |
2338 | } | |
45d6a902 AM |
2339 | irela += bed->s->int_rels_per_ext_rel; |
2340 | erela += shdr->sh_entsize; | |
2341 | } | |
2342 | ||
2343 | return TRUE; | |
2344 | } | |
2345 | ||
2346 | /* Read and swap the relocs for a section O. They may have been | |
2347 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2348 | not NULL, they are used as buffers to read into. They are known to | |
2349 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2350 | the return value is allocated using either malloc or bfd_alloc, | |
2351 | according to the KEEP_MEMORY argument. If O has two relocation | |
2352 | sections (both REL and RELA relocations), then the REL_HDR | |
2353 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
d4730f92 | 2354 | RELA_HDR relocations. */ |
45d6a902 AM |
2355 | |
2356 | Elf_Internal_Rela * | |
268b6b39 AM |
2357 | _bfd_elf_link_read_relocs (bfd *abfd, |
2358 | asection *o, | |
2359 | void *external_relocs, | |
2360 | Elf_Internal_Rela *internal_relocs, | |
2361 | bfd_boolean keep_memory) | |
45d6a902 | 2362 | { |
268b6b39 | 2363 | void *alloc1 = NULL; |
45d6a902 | 2364 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2365 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2366 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
2367 | Elf_Internal_Rela *internal_rela_relocs; | |
45d6a902 | 2368 | |
d4730f92 BS |
2369 | if (esdo->relocs != NULL) |
2370 | return esdo->relocs; | |
45d6a902 AM |
2371 | |
2372 | if (o->reloc_count == 0) | |
2373 | return NULL; | |
2374 | ||
45d6a902 AM |
2375 | if (internal_relocs == NULL) |
2376 | { | |
2377 | bfd_size_type size; | |
2378 | ||
2379 | size = o->reloc_count; | |
2380 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
2381 | if (keep_memory) | |
a50b1753 | 2382 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); |
45d6a902 | 2383 | else |
a50b1753 | 2384 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
45d6a902 AM |
2385 | if (internal_relocs == NULL) |
2386 | goto error_return; | |
2387 | } | |
2388 | ||
2389 | if (external_relocs == NULL) | |
2390 | { | |
d4730f92 BS |
2391 | bfd_size_type size = 0; |
2392 | ||
2393 | if (esdo->rel.hdr) | |
2394 | size += esdo->rel.hdr->sh_size; | |
2395 | if (esdo->rela.hdr) | |
2396 | size += esdo->rela.hdr->sh_size; | |
45d6a902 | 2397 | |
268b6b39 | 2398 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2399 | if (alloc1 == NULL) |
2400 | goto error_return; | |
2401 | external_relocs = alloc1; | |
2402 | } | |
2403 | ||
d4730f92 BS |
2404 | internal_rela_relocs = internal_relocs; |
2405 | if (esdo->rel.hdr) | |
2406 | { | |
2407 | if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, | |
2408 | external_relocs, | |
2409 | internal_relocs)) | |
2410 | goto error_return; | |
2411 | external_relocs = (((bfd_byte *) external_relocs) | |
2412 | + esdo->rel.hdr->sh_size); | |
2413 | internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) | |
2414 | * bed->s->int_rels_per_ext_rel); | |
2415 | } | |
2416 | ||
2417 | if (esdo->rela.hdr | |
2418 | && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, | |
2419 | external_relocs, | |
2420 | internal_rela_relocs))) | |
45d6a902 AM |
2421 | goto error_return; |
2422 | ||
2423 | /* Cache the results for next time, if we can. */ | |
2424 | if (keep_memory) | |
d4730f92 | 2425 | esdo->relocs = internal_relocs; |
45d6a902 AM |
2426 | |
2427 | if (alloc1 != NULL) | |
2428 | free (alloc1); | |
2429 | ||
2430 | /* Don't free alloc2, since if it was allocated we are passing it | |
2431 | back (under the name of internal_relocs). */ | |
2432 | ||
2433 | return internal_relocs; | |
2434 | ||
2435 | error_return: | |
2436 | if (alloc1 != NULL) | |
2437 | free (alloc1); | |
2438 | if (alloc2 != NULL) | |
4dd07732 AM |
2439 | { |
2440 | if (keep_memory) | |
2441 | bfd_release (abfd, alloc2); | |
2442 | else | |
2443 | free (alloc2); | |
2444 | } | |
45d6a902 AM |
2445 | return NULL; |
2446 | } | |
2447 | ||
2448 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2449 | section header for a section containing relocations for O. */ | |
2450 | ||
28caa186 | 2451 | static bfd_boolean |
5025eb7c AO |
2452 | _bfd_elf_link_size_reloc_section (bfd *abfd, struct bfd_link_info *info, |
2453 | asection *o, bfd_boolean rela) | |
45d6a902 | 2454 | { |
5025eb7c AO |
2455 | struct bfd_elf_section_data *esdo; |
2456 | const struct elf_backend_data *bed; | |
2457 | struct bfd_elf_section_reloc_data *reldata; | |
2458 | Elf_Internal_Shdr *rel_hdr; | |
2459 | unsigned int count; | |
2460 | ||
2461 | esdo = elf_section_data (o); | |
2462 | reldata = rela ? &esdo->rela : &esdo->rel; | |
2463 | rel_hdr = reldata->hdr; | |
45d6a902 AM |
2464 | |
2465 | /* That allows us to calculate the size of the section. */ | |
5025eb7c AO |
2466 | bed = get_elf_backend_data (abfd); |
2467 | count = (*bed->elf_backend_count_output_relocs) (info, o, rela); | |
2468 | rel_hdr->sh_size = count * rel_hdr->sh_entsize; | |
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 | { | |
2531 | (*_bfd_error_handler) | |
d003868e AM |
2532 | (_("%B: relocation size mismatch in %B section %A"), |
2533 | output_bfd, input_section->owner, input_section); | |
297d8443 | 2534 | bfd_set_error (bfd_error_wrong_format); |
45d6a902 AM |
2535 | return FALSE; |
2536 | } | |
2537 | ||
d4730f92 BS |
2538 | erel = output_reldata->hdr->contents; |
2539 | erel += output_reldata->count * input_rel_hdr->sh_entsize; | |
45d6a902 AM |
2540 | irela = internal_relocs; |
2541 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2542 | * bed->s->int_rels_per_ext_rel); | |
2543 | while (irela < irelaend) | |
2544 | { | |
2545 | (*swap_out) (output_bfd, irela, erel); | |
2546 | irela += bed->s->int_rels_per_ext_rel; | |
2547 | erel += input_rel_hdr->sh_entsize; | |
2548 | } | |
2549 | ||
2550 | /* Bump the counter, so that we know where to add the next set of | |
2551 | relocations. */ | |
d4730f92 | 2552 | output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); |
45d6a902 AM |
2553 | |
2554 | return TRUE; | |
2555 | } | |
5025eb7c AO |
2556 | |
2557 | unsigned int | |
2558 | _bfd_elf_default_count_output_relocs (struct bfd_link_info * info ATTRIBUTE_UNUSED, | |
2559 | asection * o, | |
2560 | bfd_boolean rela) | |
2561 | { | |
2562 | struct bfd_elf_section_data *esdo; | |
2563 | struct bfd_elf_section_reloc_data *reldata; | |
2564 | ||
2565 | esdo = elf_section_data (o); | |
2566 | reldata = rela ? &esdo->rela : &esdo->rel; | |
2567 | ||
2568 | return reldata->count; | |
2569 | } | |
45d6a902 | 2570 | \f |
508c3946 L |
2571 | /* Make weak undefined symbols in PIE dynamic. */ |
2572 | ||
2573 | bfd_boolean | |
2574 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2575 | struct elf_link_hash_entry *h) | |
2576 | { | |
0e1862bb | 2577 | if (bfd_link_pie (info) |
508c3946 L |
2578 | && h->dynindx == -1 |
2579 | && h->root.type == bfd_link_hash_undefweak) | |
2580 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2581 | ||
2582 | return TRUE; | |
2583 | } | |
2584 | ||
45d6a902 AM |
2585 | /* Fix up the flags for a symbol. This handles various cases which |
2586 | can only be fixed after all the input files are seen. This is | |
2587 | currently called by both adjust_dynamic_symbol and | |
2588 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2589 | the face of future changes. */ | |
2590 | ||
28caa186 | 2591 | static bfd_boolean |
268b6b39 AM |
2592 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2593 | struct elf_info_failed *eif) | |
45d6a902 | 2594 | { |
33774f08 | 2595 | const struct elf_backend_data *bed; |
508c3946 | 2596 | |
45d6a902 AM |
2597 | /* If this symbol was mentioned in a non-ELF file, try to set |
2598 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2599 | permit a non-ELF file to correctly refer to a symbol defined in | |
2600 | an ELF dynamic object. */ | |
f5385ebf | 2601 | if (h->non_elf) |
45d6a902 AM |
2602 | { |
2603 | while (h->root.type == bfd_link_hash_indirect) | |
2604 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2605 | ||
2606 | if (h->root.type != bfd_link_hash_defined | |
2607 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2608 | { |
2609 | h->ref_regular = 1; | |
2610 | h->ref_regular_nonweak = 1; | |
2611 | } | |
45d6a902 AM |
2612 | else |
2613 | { | |
2614 | if (h->root.u.def.section->owner != NULL | |
2615 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2616 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2617 | { |
2618 | h->ref_regular = 1; | |
2619 | h->ref_regular_nonweak = 1; | |
2620 | } | |
45d6a902 | 2621 | else |
f5385ebf | 2622 | h->def_regular = 1; |
45d6a902 AM |
2623 | } |
2624 | ||
2625 | if (h->dynindx == -1 | |
f5385ebf AM |
2626 | && (h->def_dynamic |
2627 | || h->ref_dynamic)) | |
45d6a902 | 2628 | { |
c152c796 | 2629 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2630 | { |
2631 | eif->failed = TRUE; | |
2632 | return FALSE; | |
2633 | } | |
2634 | } | |
2635 | } | |
2636 | else | |
2637 | { | |
f5385ebf | 2638 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2639 | was first seen in a non-ELF file. Fortunately, if the symbol |
2640 | was first seen in an ELF file, we're probably OK unless the | |
2641 | symbol was defined in a non-ELF file. Catch that case here. | |
2642 | FIXME: We're still in trouble if the symbol was first seen in | |
2643 | a dynamic object, and then later in a non-ELF regular object. */ | |
2644 | if ((h->root.type == bfd_link_hash_defined | |
2645 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2646 | && !h->def_regular |
45d6a902 AM |
2647 | && (h->root.u.def.section->owner != NULL |
2648 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2649 | != bfd_target_elf_flavour) | |
2650 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2651 | && !h->def_dynamic))) |
2652 | h->def_regular = 1; | |
45d6a902 AM |
2653 | } |
2654 | ||
508c3946 | 2655 | /* Backend specific symbol fixup. */ |
33774f08 AM |
2656 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2657 | if (bed->elf_backend_fixup_symbol | |
2658 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2659 | return FALSE; | |
508c3946 | 2660 | |
45d6a902 AM |
2661 | /* If this is a final link, and the symbol was defined as a common |
2662 | symbol in a regular object file, and there was no definition in | |
2663 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2664 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2665 | flag will not have been set. */ |
2666 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2667 | && !h->def_regular |
2668 | && h->ref_regular | |
2669 | && !h->def_dynamic | |
96f29d96 | 2670 | && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) |
f5385ebf | 2671 | h->def_regular = 1; |
45d6a902 AM |
2672 | |
2673 | /* If -Bsymbolic was used (which means to bind references to global | |
2674 | symbols to the definition within the shared object), and this | |
2675 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2676 | need a PLT entry. Likewise, if the symbol has non-default |
2677 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2678 | will force it local. */ |
f5385ebf | 2679 | if (h->needs_plt |
0e1862bb | 2680 | && bfd_link_pic (eif->info) |
0eddce27 | 2681 | && is_elf_hash_table (eif->info->hash) |
55255dae | 2682 | && (SYMBOLIC_BIND (eif->info, h) |
c1be741f | 2683 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2684 | && h->def_regular) |
45d6a902 | 2685 | { |
45d6a902 AM |
2686 | bfd_boolean force_local; |
2687 | ||
45d6a902 AM |
2688 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2689 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2690 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2691 | } | |
2692 | ||
2693 | /* If a weak undefined symbol has non-default visibility, we also | |
2694 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2695 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 | 2696 | && h->root.type == bfd_link_hash_undefweak) |
33774f08 | 2697 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
45d6a902 AM |
2698 | |
2699 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2700 | the real definition in the dynamic object, copy interesting flags | |
2701 | over to the real definition. */ | |
f6e332e6 | 2702 | if (h->u.weakdef != NULL) |
45d6a902 | 2703 | { |
45d6a902 AM |
2704 | /* If the real definition is defined by a regular object file, |
2705 | don't do anything special. See the longer description in | |
2706 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
4e6b54a6 | 2707 | if (h->u.weakdef->def_regular) |
f6e332e6 | 2708 | h->u.weakdef = NULL; |
45d6a902 | 2709 | else |
a26587ba | 2710 | { |
4e6b54a6 AM |
2711 | struct elf_link_hash_entry *weakdef = h->u.weakdef; |
2712 | ||
2713 | while (h->root.type == bfd_link_hash_indirect) | |
2714 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2715 | ||
2716 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2717 | || h->root.type == bfd_link_hash_defweak); | |
2718 | BFD_ASSERT (weakdef->def_dynamic); | |
a26587ba RS |
2719 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined |
2720 | || weakdef->root.type == bfd_link_hash_defweak); | |
2721 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h); | |
2722 | } | |
45d6a902 AM |
2723 | } |
2724 | ||
2725 | return TRUE; | |
2726 | } | |
2727 | ||
2728 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2729 | called via elf_link_hash_traverse, and also calls itself | |
2730 | recursively. */ | |
2731 | ||
28caa186 | 2732 | static bfd_boolean |
268b6b39 | 2733 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2734 | { |
a50b1753 | 2735 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 | 2736 | bfd *dynobj; |
9c5bfbb7 | 2737 | const struct elf_backend_data *bed; |
45d6a902 | 2738 | |
0eddce27 | 2739 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2740 | return FALSE; |
2741 | ||
45d6a902 AM |
2742 | /* Ignore indirect symbols. These are added by the versioning code. */ |
2743 | if (h->root.type == bfd_link_hash_indirect) | |
2744 | return TRUE; | |
2745 | ||
2746 | /* Fix the symbol flags. */ | |
2747 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2748 | return FALSE; | |
2749 | ||
2750 | /* If this symbol does not require a PLT entry, and it is not | |
2751 | defined by a dynamic object, or is not referenced by a regular | |
2752 | object, ignore it. We do have to handle a weak defined symbol, | |
2753 | even if no regular object refers to it, if we decided to add it | |
2754 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2755 | about symbols which are defined by one dynamic object and | |
2756 | referenced by another one? */ | |
f5385ebf | 2757 | if (!h->needs_plt |
91e21fb7 | 2758 | && h->type != STT_GNU_IFUNC |
f5385ebf AM |
2759 | && (h->def_regular |
2760 | || !h->def_dynamic | |
2761 | || (!h->ref_regular | |
f6e332e6 | 2762 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 | 2763 | { |
a6aa5195 | 2764 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2765 | return TRUE; |
2766 | } | |
2767 | ||
2768 | /* If we've already adjusted this symbol, don't do it again. This | |
2769 | can happen via a recursive call. */ | |
f5385ebf | 2770 | if (h->dynamic_adjusted) |
45d6a902 AM |
2771 | return TRUE; |
2772 | ||
2773 | /* Don't look at this symbol again. Note that we must set this | |
2774 | after checking the above conditions, because we may look at a | |
2775 | symbol once, decide not to do anything, and then get called | |
2776 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2777 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2778 | |
2779 | /* If this is a weak definition, and we know a real definition, and | |
2780 | the real symbol is not itself defined by a regular object file, | |
2781 | then get a good value for the real definition. We handle the | |
2782 | real symbol first, for the convenience of the backend routine. | |
2783 | ||
2784 | Note that there is a confusing case here. If the real definition | |
2785 | is defined by a regular object file, we don't get the real symbol | |
2786 | from the dynamic object, but we do get the weak symbol. If the | |
2787 | processor backend uses a COPY reloc, then if some routine in the | |
2788 | dynamic object changes the real symbol, we will not see that | |
2789 | change in the corresponding weak symbol. This is the way other | |
2790 | ELF linkers work as well, and seems to be a result of the shared | |
2791 | library model. | |
2792 | ||
2793 | I will clarify this issue. Most SVR4 shared libraries define the | |
2794 | variable _timezone and define timezone as a weak synonym. The | |
2795 | tzset call changes _timezone. If you write | |
2796 | extern int timezone; | |
2797 | int _timezone = 5; | |
2798 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2799 | you might expect that, since timezone is a synonym for _timezone, | |
2800 | the same number will print both times. However, if the processor | |
2801 | backend uses a COPY reloc, then actually timezone will be copied | |
2802 | into your process image, and, since you define _timezone | |
2803 | yourself, _timezone will not. Thus timezone and _timezone will | |
2804 | wind up at different memory locations. The tzset call will set | |
2805 | _timezone, leaving timezone unchanged. */ | |
2806 | ||
f6e332e6 | 2807 | if (h->u.weakdef != NULL) |
45d6a902 | 2808 | { |
ec24dc88 AM |
2809 | /* If we get to this point, there is an implicit reference to |
2810 | H->U.WEAKDEF by a regular object file via the weak symbol H. */ | |
f6e332e6 | 2811 | h->u.weakdef->ref_regular = 1; |
45d6a902 | 2812 | |
ec24dc88 AM |
2813 | /* Ensure that the backend adjust_dynamic_symbol function sees |
2814 | H->U.WEAKDEF before H by recursively calling ourselves. */ | |
f6e332e6 | 2815 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2816 | return FALSE; |
2817 | } | |
2818 | ||
2819 | /* If a symbol has no type and no size and does not require a PLT | |
2820 | entry, then we are probably about to do the wrong thing here: we | |
2821 | are probably going to create a COPY reloc for an empty object. | |
2822 | This case can arise when a shared object is built with assembly | |
2823 | code, and the assembly code fails to set the symbol type. */ | |
2824 | if (h->size == 0 | |
2825 | && h->type == STT_NOTYPE | |
f5385ebf | 2826 | && !h->needs_plt) |
45d6a902 AM |
2827 | (*_bfd_error_handler) |
2828 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2829 | h->root.root.string); | |
2830 | ||
2831 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2832 | bed = get_elf_backend_data (dynobj); | |
e7c33416 | 2833 | |
45d6a902 AM |
2834 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) |
2835 | { | |
2836 | eif->failed = TRUE; | |
2837 | return FALSE; | |
2838 | } | |
2839 | ||
2840 | return TRUE; | |
2841 | } | |
2842 | ||
027297b7 L |
2843 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
2844 | DYNBSS. */ | |
2845 | ||
2846 | bfd_boolean | |
6cabe1ea AM |
2847 | _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, |
2848 | struct elf_link_hash_entry *h, | |
027297b7 L |
2849 | asection *dynbss) |
2850 | { | |
91ac5911 | 2851 | unsigned int power_of_two; |
027297b7 L |
2852 | bfd_vma mask; |
2853 | asection *sec = h->root.u.def.section; | |
2854 | ||
2855 | /* The section aligment of definition is the maximum alignment | |
91ac5911 L |
2856 | requirement of symbols defined in the section. Since we don't |
2857 | know the symbol alignment requirement, we start with the | |
2858 | maximum alignment and check low bits of the symbol address | |
2859 | for the minimum alignment. */ | |
2860 | power_of_two = bfd_get_section_alignment (sec->owner, sec); | |
2861 | mask = ((bfd_vma) 1 << power_of_two) - 1; | |
2862 | while ((h->root.u.def.value & mask) != 0) | |
2863 | { | |
2864 | mask >>= 1; | |
2865 | --power_of_two; | |
2866 | } | |
027297b7 | 2867 | |
91ac5911 L |
2868 | if (power_of_two > bfd_get_section_alignment (dynbss->owner, |
2869 | dynbss)) | |
027297b7 L |
2870 | { |
2871 | /* Adjust the section alignment if needed. */ | |
2872 | if (! bfd_set_section_alignment (dynbss->owner, dynbss, | |
91ac5911 | 2873 | power_of_two)) |
027297b7 L |
2874 | return FALSE; |
2875 | } | |
2876 | ||
91ac5911 | 2877 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
2878 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
2879 | ||
2880 | /* Define the symbol as being at this point in DYNBSS. */ | |
2881 | h->root.u.def.section = dynbss; | |
2882 | h->root.u.def.value = dynbss->size; | |
2883 | ||
2884 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
2885 | dynbss->size += h->size; | |
2886 | ||
f7483970 L |
2887 | /* No error if extern_protected_data is true. */ |
2888 | if (h->protected_def | |
889c2a67 L |
2889 | && (!info->extern_protected_data |
2890 | || (info->extern_protected_data < 0 | |
2891 | && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) | |
d07a1b05 AM |
2892 | info->callbacks->einfo |
2893 | (_("%P: copy reloc against protected `%T' is dangerous\n"), | |
2894 | h->root.root.string); | |
6cabe1ea | 2895 | |
027297b7 L |
2896 | return TRUE; |
2897 | } | |
2898 | ||
45d6a902 AM |
2899 | /* Adjust all external symbols pointing into SEC_MERGE sections |
2900 | to reflect the object merging within the sections. */ | |
2901 | ||
28caa186 | 2902 | static bfd_boolean |
268b6b39 | 2903 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2904 | { |
2905 | asection *sec; | |
2906 | ||
45d6a902 AM |
2907 | if ((h->root.type == bfd_link_hash_defined |
2908 | || h->root.type == bfd_link_hash_defweak) | |
2909 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
dbaa2011 | 2910 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
45d6a902 | 2911 | { |
a50b1753 | 2912 | bfd *output_bfd = (bfd *) data; |
45d6a902 AM |
2913 | |
2914 | h->root.u.def.value = | |
2915 | _bfd_merged_section_offset (output_bfd, | |
2916 | &h->root.u.def.section, | |
2917 | elf_section_data (sec)->sec_info, | |
753731ee | 2918 | h->root.u.def.value); |
45d6a902 AM |
2919 | } |
2920 | ||
2921 | return TRUE; | |
2922 | } | |
986a241f RH |
2923 | |
2924 | /* Returns false if the symbol referred to by H should be considered | |
2925 | to resolve local to the current module, and true if it should be | |
2926 | considered to bind dynamically. */ | |
2927 | ||
2928 | bfd_boolean | |
268b6b39 AM |
2929 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2930 | struct bfd_link_info *info, | |
89a2ee5a | 2931 | bfd_boolean not_local_protected) |
986a241f RH |
2932 | { |
2933 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
2934 | const struct elf_backend_data *bed; |
2935 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
2936 | |
2937 | if (h == NULL) | |
2938 | return FALSE; | |
2939 | ||
2940 | while (h->root.type == bfd_link_hash_indirect | |
2941 | || h->root.type == bfd_link_hash_warning) | |
2942 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2943 | ||
2944 | /* If it was forced local, then clearly it's not dynamic. */ | |
2945 | if (h->dynindx == -1) | |
2946 | return FALSE; | |
f5385ebf | 2947 | if (h->forced_local) |
986a241f RH |
2948 | return FALSE; |
2949 | ||
2950 | /* Identify the cases where name binding rules say that a | |
2951 | visible symbol resolves locally. */ | |
0e1862bb L |
2952 | binding_stays_local_p = (bfd_link_executable (info) |
2953 | || SYMBOLIC_BIND (info, h)); | |
986a241f RH |
2954 | |
2955 | switch (ELF_ST_VISIBILITY (h->other)) | |
2956 | { | |
2957 | case STV_INTERNAL: | |
2958 | case STV_HIDDEN: | |
2959 | return FALSE; | |
2960 | ||
2961 | case STV_PROTECTED: | |
fcb93ecf PB |
2962 | hash_table = elf_hash_table (info); |
2963 | if (!is_elf_hash_table (hash_table)) | |
2964 | return FALSE; | |
2965 | ||
2966 | bed = get_elf_backend_data (hash_table->dynobj); | |
2967 | ||
986a241f RH |
2968 | /* Proper resolution for function pointer equality may require |
2969 | that these symbols perhaps be resolved dynamically, even though | |
2970 | we should be resolving them to the current module. */ | |
89a2ee5a | 2971 | if (!not_local_protected || !bed->is_function_type (h->type)) |
986a241f RH |
2972 | binding_stays_local_p = TRUE; |
2973 | break; | |
2974 | ||
2975 | default: | |
986a241f RH |
2976 | break; |
2977 | } | |
2978 | ||
aa37626c | 2979 | /* If it isn't defined locally, then clearly it's dynamic. */ |
89a2ee5a | 2980 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
aa37626c L |
2981 | return TRUE; |
2982 | ||
986a241f RH |
2983 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2984 | us that it remains local. */ | |
2985 | return !binding_stays_local_p; | |
2986 | } | |
f6c52c13 AM |
2987 | |
2988 | /* Return true if the symbol referred to by H should be considered | |
2989 | to resolve local to the current module, and false otherwise. Differs | |
2990 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2e76e85a | 2991 | undefined symbols. The two functions are virtually identical except |
89a2ee5a AM |
2992 | for the place where forced_local and dynindx == -1 are tested. If |
2993 | either of those tests are true, _bfd_elf_dynamic_symbol_p will say | |
2994 | the symbol is local, while _bfd_elf_symbol_refs_local_p will say | |
2995 | the symbol is local only for defined symbols. | |
2996 | It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as | |
2997 | !_bfd_elf_symbol_refs_local_p, except that targets differ in their | |
2998 | treatment of undefined weak symbols. For those that do not make | |
2999 | undefined weak symbols dynamic, both functions may return false. */ | |
f6c52c13 AM |
3000 | |
3001 | bfd_boolean | |
268b6b39 AM |
3002 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
3003 | struct bfd_link_info *info, | |
3004 | bfd_boolean local_protected) | |
f6c52c13 | 3005 | { |
fcb93ecf PB |
3006 | const struct elf_backend_data *bed; |
3007 | struct elf_link_hash_table *hash_table; | |
3008 | ||
f6c52c13 AM |
3009 | /* If it's a local sym, of course we resolve locally. */ |
3010 | if (h == NULL) | |
3011 | return TRUE; | |
3012 | ||
d95edcac L |
3013 | /* STV_HIDDEN or STV_INTERNAL ones must be local. */ |
3014 | if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
3015 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
3016 | return TRUE; | |
3017 | ||
7e2294f9 AO |
3018 | /* Common symbols that become definitions don't get the DEF_REGULAR |
3019 | flag set, so test it first, and don't bail out. */ | |
3020 | if (ELF_COMMON_DEF_P (h)) | |
3021 | /* Do nothing. */; | |
f6c52c13 | 3022 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
3023 | resolve locally. The sym is either undefined or dynamic. */ |
3024 | else if (!h->def_regular) | |
f6c52c13 AM |
3025 | return FALSE; |
3026 | ||
3027 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 3028 | if (h->forced_local) |
f6c52c13 AM |
3029 | return TRUE; |
3030 | ||
3031 | /* As do non-dynamic symbols. */ | |
3032 | if (h->dynindx == -1) | |
3033 | return TRUE; | |
3034 | ||
3035 | /* At this point, we know the symbol is defined and dynamic. In an | |
3036 | executable it must resolve locally, likewise when building symbolic | |
3037 | shared libraries. */ | |
0e1862bb | 3038 | if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
3039 | return TRUE; |
3040 | ||
3041 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
3042 | with default visibility might not resolve locally. */ | |
3043 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
3044 | return FALSE; | |
3045 | ||
fcb93ecf PB |
3046 | hash_table = elf_hash_table (info); |
3047 | if (!is_elf_hash_table (hash_table)) | |
3048 | return TRUE; | |
3049 | ||
3050 | bed = get_elf_backend_data (hash_table->dynobj); | |
3051 | ||
f7483970 L |
3052 | /* If extern_protected_data is false, STV_PROTECTED non-function |
3053 | symbols are local. */ | |
889c2a67 L |
3054 | if ((!info->extern_protected_data |
3055 | || (info->extern_protected_data < 0 | |
3056 | && !bed->extern_protected_data)) | |
3057 | && !bed->is_function_type (h->type)) | |
1c16dfa5 L |
3058 | return TRUE; |
3059 | ||
f6c52c13 | 3060 | /* Function pointer equality tests may require that STV_PROTECTED |
2676a7d9 AM |
3061 | symbols be treated as dynamic symbols. If the address of a |
3062 | function not defined in an executable is set to that function's | |
3063 | plt entry in the executable, then the address of the function in | |
3064 | a shared library must also be the plt entry in the executable. */ | |
f6c52c13 AM |
3065 | return local_protected; |
3066 | } | |
e1918d23 AM |
3067 | |
3068 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
3069 | aligned. Returns the first TLS output section. */ | |
3070 | ||
3071 | struct bfd_section * | |
3072 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
3073 | { | |
3074 | struct bfd_section *sec, *tls; | |
3075 | unsigned int align = 0; | |
3076 | ||
3077 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
3078 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
3079 | break; | |
3080 | tls = sec; | |
3081 | ||
3082 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
3083 | if (sec->alignment_power > align) | |
3084 | align = sec->alignment_power; | |
3085 | ||
3086 | elf_hash_table (info)->tls_sec = tls; | |
3087 | ||
3088 | /* Ensure the alignment of the first section is the largest alignment, | |
3089 | so that the tls segment starts aligned. */ | |
3090 | if (tls != NULL) | |
3091 | tls->alignment_power = align; | |
3092 | ||
3093 | return tls; | |
3094 | } | |
0ad989f9 L |
3095 | |
3096 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
3097 | static bfd_boolean | |
3098 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
3099 | Elf_Internal_Sym *sym) | |
3100 | { | |
a4d8e49b L |
3101 | const struct elf_backend_data *bed; |
3102 | ||
0ad989f9 L |
3103 | /* Local symbols do not count, but target specific ones might. */ |
3104 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
3105 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
3106 | return FALSE; | |
3107 | ||
fcb93ecf | 3108 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 3109 | /* Function symbols do not count. */ |
fcb93ecf | 3110 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
3111 | return FALSE; |
3112 | ||
3113 | /* If the section is undefined, then so is the symbol. */ | |
3114 | if (sym->st_shndx == SHN_UNDEF) | |
3115 | return FALSE; | |
3116 | ||
3117 | /* If the symbol is defined in the common section, then | |
3118 | it is a common definition and so does not count. */ | |
a4d8e49b | 3119 | if (bed->common_definition (sym)) |
0ad989f9 L |
3120 | return FALSE; |
3121 | ||
3122 | /* If the symbol is in a target specific section then we | |
3123 | must rely upon the backend to tell us what it is. */ | |
3124 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
3125 | /* FIXME - this function is not coded yet: | |
3126 | ||
3127 | return _bfd_is_global_symbol_definition (abfd, sym); | |
3128 | ||
3129 | Instead for now assume that the definition is not global, | |
3130 | Even if this is wrong, at least the linker will behave | |
3131 | in the same way that it used to do. */ | |
3132 | return FALSE; | |
3133 | ||
3134 | return TRUE; | |
3135 | } | |
3136 | ||
3137 | /* Search the symbol table of the archive element of the archive ABFD | |
3138 | whose archive map contains a mention of SYMDEF, and determine if | |
3139 | the symbol is defined in this element. */ | |
3140 | static bfd_boolean | |
3141 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
3142 | { | |
3143 | Elf_Internal_Shdr * hdr; | |
ef53be89 AM |
3144 | size_t symcount; |
3145 | size_t extsymcount; | |
3146 | size_t extsymoff; | |
0ad989f9 L |
3147 | Elf_Internal_Sym *isymbuf; |
3148 | Elf_Internal_Sym *isym; | |
3149 | Elf_Internal_Sym *isymend; | |
3150 | bfd_boolean result; | |
3151 | ||
3152 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
3153 | if (abfd == NULL) | |
3154 | return FALSE; | |
3155 | ||
3156 | if (! bfd_check_format (abfd, bfd_object)) | |
3157 | return FALSE; | |
3158 | ||
7dc3990e L |
3159 | /* Select the appropriate symbol table. If we don't know if the |
3160 | object file is an IR object, give linker LTO plugin a chance to | |
3161 | get the correct symbol table. */ | |
3162 | if (abfd->plugin_format == bfd_plugin_yes | |
08ce1d72 | 3163 | #if BFD_SUPPORTS_PLUGINS |
7dc3990e L |
3164 | || (abfd->plugin_format == bfd_plugin_unknown |
3165 | && bfd_link_plugin_object_p (abfd)) | |
3166 | #endif | |
3167 | ) | |
3168 | { | |
3169 | /* Use the IR symbol table if the object has been claimed by | |
3170 | plugin. */ | |
3171 | abfd = abfd->plugin_dummy_bfd; | |
3172 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3173 | } | |
3174 | else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
0ad989f9 L |
3175 | hdr = &elf_tdata (abfd)->symtab_hdr; |
3176 | else | |
3177 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3178 | ||
3179 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3180 | ||
3181 | /* The sh_info field of the symtab header tells us where the | |
3182 | external symbols start. We don't care about the local symbols. */ | |
3183 | if (elf_bad_symtab (abfd)) | |
3184 | { | |
3185 | extsymcount = symcount; | |
3186 | extsymoff = 0; | |
3187 | } | |
3188 | else | |
3189 | { | |
3190 | extsymcount = symcount - hdr->sh_info; | |
3191 | extsymoff = hdr->sh_info; | |
3192 | } | |
3193 | ||
3194 | if (extsymcount == 0) | |
3195 | return FALSE; | |
3196 | ||
3197 | /* Read in the symbol table. */ | |
3198 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3199 | NULL, NULL, NULL); | |
3200 | if (isymbuf == NULL) | |
3201 | return FALSE; | |
3202 | ||
3203 | /* Scan the symbol table looking for SYMDEF. */ | |
3204 | result = FALSE; | |
3205 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
3206 | { | |
3207 | const char *name; | |
3208 | ||
3209 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3210 | isym->st_name); | |
3211 | if (name == NULL) | |
3212 | break; | |
3213 | ||
3214 | if (strcmp (name, symdef->name) == 0) | |
3215 | { | |
3216 | result = is_global_data_symbol_definition (abfd, isym); | |
3217 | break; | |
3218 | } | |
3219 | } | |
3220 | ||
3221 | free (isymbuf); | |
3222 | ||
3223 | return result; | |
3224 | } | |
3225 | \f | |
5a580b3a AM |
3226 | /* Add an entry to the .dynamic table. */ |
3227 | ||
3228 | bfd_boolean | |
3229 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
3230 | bfd_vma tag, | |
3231 | bfd_vma val) | |
3232 | { | |
3233 | struct elf_link_hash_table *hash_table; | |
3234 | const struct elf_backend_data *bed; | |
3235 | asection *s; | |
3236 | bfd_size_type newsize; | |
3237 | bfd_byte *newcontents; | |
3238 | Elf_Internal_Dyn dyn; | |
3239 | ||
3240 | hash_table = elf_hash_table (info); | |
3241 | if (! is_elf_hash_table (hash_table)) | |
3242 | return FALSE; | |
3243 | ||
3244 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3245 | s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
5a580b3a AM |
3246 | BFD_ASSERT (s != NULL); |
3247 | ||
eea6121a | 3248 | newsize = s->size + bed->s->sizeof_dyn; |
a50b1753 | 3249 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
5a580b3a AM |
3250 | if (newcontents == NULL) |
3251 | return FALSE; | |
3252 | ||
3253 | dyn.d_tag = tag; | |
3254 | dyn.d_un.d_val = val; | |
eea6121a | 3255 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3256 | |
eea6121a | 3257 | s->size = newsize; |
5a580b3a AM |
3258 | s->contents = newcontents; |
3259 | ||
3260 | return TRUE; | |
3261 | } | |
3262 | ||
3263 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
3264 | otherwise just check whether one already exists. Returns -1 on error, | |
3265 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
3266 | ||
4ad4eba5 | 3267 | static int |
7e9f0867 AM |
3268 | elf_add_dt_needed_tag (bfd *abfd, |
3269 | struct bfd_link_info *info, | |
4ad4eba5 AM |
3270 | const char *soname, |
3271 | bfd_boolean do_it) | |
5a580b3a AM |
3272 | { |
3273 | struct elf_link_hash_table *hash_table; | |
ef53be89 | 3274 | size_t strindex; |
5a580b3a | 3275 | |
7e9f0867 AM |
3276 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3277 | return -1; | |
3278 | ||
5a580b3a | 3279 | hash_table = elf_hash_table (info); |
5a580b3a | 3280 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); |
ef53be89 | 3281 | if (strindex == (size_t) -1) |
5a580b3a AM |
3282 | return -1; |
3283 | ||
02be4619 | 3284 | if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) |
5a580b3a AM |
3285 | { |
3286 | asection *sdyn; | |
3287 | const struct elf_backend_data *bed; | |
3288 | bfd_byte *extdyn; | |
3289 | ||
3290 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3291 | sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
7e9f0867 AM |
3292 | if (sdyn != NULL) |
3293 | for (extdyn = sdyn->contents; | |
3294 | extdyn < sdyn->contents + sdyn->size; | |
3295 | extdyn += bed->s->sizeof_dyn) | |
3296 | { | |
3297 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3298 | |
7e9f0867 AM |
3299 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3300 | if (dyn.d_tag == DT_NEEDED | |
3301 | && dyn.d_un.d_val == strindex) | |
3302 | { | |
3303 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3304 | return 1; | |
3305 | } | |
3306 | } | |
5a580b3a AM |
3307 | } |
3308 | ||
3309 | if (do_it) | |
3310 | { | |
7e9f0867 AM |
3311 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3312 | return -1; | |
3313 | ||
5a580b3a AM |
3314 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3315 | return -1; | |
3316 | } | |
3317 | else | |
3318 | /* We were just checking for existence of the tag. */ | |
3319 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3320 | ||
3321 | return 0; | |
3322 | } | |
3323 | ||
7b15fa7a AM |
3324 | /* Return true if SONAME is on the needed list between NEEDED and STOP |
3325 | (or the end of list if STOP is NULL), and needed by a library that | |
3326 | will be loaded. */ | |
3327 | ||
010e5ae2 | 3328 | static bfd_boolean |
7b15fa7a AM |
3329 | on_needed_list (const char *soname, |
3330 | struct bfd_link_needed_list *needed, | |
3331 | struct bfd_link_needed_list *stop) | |
010e5ae2 | 3332 | { |
7b15fa7a AM |
3333 | struct bfd_link_needed_list *look; |
3334 | for (look = needed; look != stop; look = look->next) | |
3335 | if (strcmp (soname, look->name) == 0 | |
3336 | && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 | |
3337 | /* If needed by a library that itself is not directly | |
3338 | needed, recursively check whether that library is | |
3339 | indirectly needed. Since we add DT_NEEDED entries to | |
3340 | the end of the list, library dependencies appear after | |
3341 | the library. Therefore search prior to the current | |
3342 | LOOK, preventing possible infinite recursion. */ | |
3343 | || on_needed_list (elf_dt_name (look->by), needed, look))) | |
010e5ae2 AM |
3344 | return TRUE; |
3345 | ||
3346 | return FALSE; | |
3347 | } | |
3348 | ||
14160578 | 3349 | /* Sort symbol by value, section, and size. */ |
4ad4eba5 AM |
3350 | static int |
3351 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3352 | { |
3353 | const struct elf_link_hash_entry *h1; | |
3354 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3355 | bfd_signed_vma vdiff; |
5a580b3a AM |
3356 | |
3357 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3358 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3359 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3360 | if (vdiff != 0) | |
3361 | return vdiff > 0 ? 1 : -1; | |
3362 | else | |
3363 | { | |
d3435ae8 | 3364 | int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; |
10b7e05b NC |
3365 | if (sdiff != 0) |
3366 | return sdiff > 0 ? 1 : -1; | |
3367 | } | |
14160578 AM |
3368 | vdiff = h1->size - h2->size; |
3369 | return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1; | |
5a580b3a | 3370 | } |
4ad4eba5 | 3371 | |
5a580b3a AM |
3372 | /* This function is used to adjust offsets into .dynstr for |
3373 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3374 | ||
3375 | static bfd_boolean | |
3376 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3377 | { | |
a50b1753 | 3378 | struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; |
5a580b3a | 3379 | |
5a580b3a AM |
3380 | if (h->dynindx != -1) |
3381 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3382 | return TRUE; | |
3383 | } | |
3384 | ||
3385 | /* Assign string offsets in .dynstr, update all structures referencing | |
3386 | them. */ | |
3387 | ||
4ad4eba5 AM |
3388 | static bfd_boolean |
3389 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3390 | { |
3391 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3392 | struct elf_link_local_dynamic_entry *entry; | |
3393 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3394 | bfd *dynobj = hash_table->dynobj; | |
3395 | asection *sdyn; | |
3396 | bfd_size_type size; | |
3397 | const struct elf_backend_data *bed; | |
3398 | bfd_byte *extdyn; | |
3399 | ||
3400 | _bfd_elf_strtab_finalize (dynstr); | |
3401 | size = _bfd_elf_strtab_size (dynstr); | |
3402 | ||
3403 | bed = get_elf_backend_data (dynobj); | |
3d4d4302 | 3404 | sdyn = bfd_get_linker_section (dynobj, ".dynamic"); |
5a580b3a AM |
3405 | BFD_ASSERT (sdyn != NULL); |
3406 | ||
3407 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3408 | for (extdyn = sdyn->contents; | |
eea6121a | 3409 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3410 | extdyn += bed->s->sizeof_dyn) |
3411 | { | |
3412 | Elf_Internal_Dyn dyn; | |
3413 | ||
3414 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3415 | switch (dyn.d_tag) | |
3416 | { | |
3417 | case DT_STRSZ: | |
3418 | dyn.d_un.d_val = size; | |
3419 | break; | |
3420 | case DT_NEEDED: | |
3421 | case DT_SONAME: | |
3422 | case DT_RPATH: | |
3423 | case DT_RUNPATH: | |
3424 | case DT_FILTER: | |
3425 | case DT_AUXILIARY: | |
7ee314fa AM |
3426 | case DT_AUDIT: |
3427 | case DT_DEPAUDIT: | |
5a580b3a AM |
3428 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); |
3429 | break; | |
3430 | default: | |
3431 | continue; | |
3432 | } | |
3433 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3434 | } | |
3435 | ||
3436 | /* Now update local dynamic symbols. */ | |
3437 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3438 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3439 | entry->isym.st_name); | |
3440 | ||
3441 | /* And the rest of dynamic symbols. */ | |
3442 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3443 | ||
3444 | /* Adjust version definitions. */ | |
3445 | if (elf_tdata (output_bfd)->cverdefs) | |
3446 | { | |
3447 | asection *s; | |
3448 | bfd_byte *p; | |
ef53be89 | 3449 | size_t i; |
5a580b3a AM |
3450 | Elf_Internal_Verdef def; |
3451 | Elf_Internal_Verdaux defaux; | |
3452 | ||
3d4d4302 | 3453 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
3454 | p = s->contents; |
3455 | do | |
3456 | { | |
3457 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3458 | &def); | |
3459 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3460 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3461 | continue; | |
5a580b3a AM |
3462 | for (i = 0; i < def.vd_cnt; ++i) |
3463 | { | |
3464 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3465 | (Elf_External_Verdaux *) p, &defaux); | |
3466 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3467 | defaux.vda_name); | |
3468 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3469 | &defaux, (Elf_External_Verdaux *) p); | |
3470 | p += sizeof (Elf_External_Verdaux); | |
3471 | } | |
3472 | } | |
3473 | while (def.vd_next); | |
3474 | } | |
3475 | ||
3476 | /* Adjust version references. */ | |
3477 | if (elf_tdata (output_bfd)->verref) | |
3478 | { | |
3479 | asection *s; | |
3480 | bfd_byte *p; | |
ef53be89 | 3481 | size_t i; |
5a580b3a AM |
3482 | Elf_Internal_Verneed need; |
3483 | Elf_Internal_Vernaux needaux; | |
3484 | ||
3d4d4302 | 3485 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
3486 | p = s->contents; |
3487 | do | |
3488 | { | |
3489 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3490 | &need); | |
3491 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3492 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3493 | (Elf_External_Verneed *) p); | |
3494 | p += sizeof (Elf_External_Verneed); | |
3495 | for (i = 0; i < need.vn_cnt; ++i) | |
3496 | { | |
3497 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3498 | (Elf_External_Vernaux *) p, &needaux); | |
3499 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3500 | needaux.vna_name); | |
3501 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3502 | &needaux, | |
3503 | (Elf_External_Vernaux *) p); | |
3504 | p += sizeof (Elf_External_Vernaux); | |
3505 | } | |
3506 | } | |
3507 | while (need.vn_next); | |
3508 | } | |
3509 | ||
3510 | return TRUE; | |
3511 | } | |
3512 | \f | |
13285a1b AM |
3513 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
3514 | The default is to only match when the INPUT and OUTPUT are exactly | |
3515 | the same target. */ | |
3516 | ||
3517 | bfd_boolean | |
3518 | _bfd_elf_default_relocs_compatible (const bfd_target *input, | |
3519 | const bfd_target *output) | |
3520 | { | |
3521 | return input == output; | |
3522 | } | |
3523 | ||
3524 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
3525 | This version is used when different targets for the same architecture | |
3526 | are virtually identical. */ | |
3527 | ||
3528 | bfd_boolean | |
3529 | _bfd_elf_relocs_compatible (const bfd_target *input, | |
3530 | const bfd_target *output) | |
3531 | { | |
3532 | const struct elf_backend_data *obed, *ibed; | |
3533 | ||
3534 | if (input == output) | |
3535 | return TRUE; | |
3536 | ||
3537 | ibed = xvec_get_elf_backend_data (input); | |
3538 | obed = xvec_get_elf_backend_data (output); | |
3539 | ||
3540 | if (ibed->arch != obed->arch) | |
3541 | return FALSE; | |
3542 | ||
3543 | /* If both backends are using this function, deem them compatible. */ | |
3544 | return ibed->relocs_compatible == obed->relocs_compatible; | |
3545 | } | |
3546 | ||
e5034e59 AM |
3547 | /* Make a special call to the linker "notice" function to tell it that |
3548 | we are about to handle an as-needed lib, or have finished | |
1b786873 | 3549 | processing the lib. */ |
e5034e59 AM |
3550 | |
3551 | bfd_boolean | |
3552 | _bfd_elf_notice_as_needed (bfd *ibfd, | |
3553 | struct bfd_link_info *info, | |
3554 | enum notice_asneeded_action act) | |
3555 | { | |
46135103 | 3556 | return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); |
e5034e59 AM |
3557 | } |
3558 | ||
d9689752 L |
3559 | /* Check relocations an ELF object file. */ |
3560 | ||
3561 | bfd_boolean | |
3562 | _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) | |
3563 | { | |
3564 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3565 | struct elf_link_hash_table *htab = elf_hash_table (info); | |
3566 | ||
3567 | /* If this object is the same format as the output object, and it is | |
3568 | not a shared library, then let the backend look through the | |
3569 | relocs. | |
3570 | ||
3571 | This is required to build global offset table entries and to | |
3572 | arrange for dynamic relocs. It is not required for the | |
3573 | particular common case of linking non PIC code, even when linking | |
3574 | against shared libraries, but unfortunately there is no way of | |
3575 | knowing whether an object file has been compiled PIC or not. | |
3576 | Looking through the relocs is not particularly time consuming. | |
3577 | The problem is that we must either (1) keep the relocs in memory, | |
3578 | which causes the linker to require additional runtime memory or | |
3579 | (2) read the relocs twice from the input file, which wastes time. | |
3580 | This would be a good case for using mmap. | |
3581 | ||
3582 | I have no idea how to handle linking PIC code into a file of a | |
3583 | different format. It probably can't be done. */ | |
3584 | if ((abfd->flags & DYNAMIC) == 0 | |
3585 | && is_elf_hash_table (htab) | |
3586 | && bed->check_relocs != NULL | |
3587 | && elf_object_id (abfd) == elf_hash_table_id (htab) | |
3588 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
3589 | { | |
3590 | asection *o; | |
3591 | ||
3592 | for (o = abfd->sections; o != NULL; o = o->next) | |
3593 | { | |
3594 | Elf_Internal_Rela *internal_relocs; | |
3595 | bfd_boolean ok; | |
3596 | ||
5ce03cea | 3597 | /* Don't check relocations in excluded sections. */ |
d9689752 | 3598 | if ((o->flags & SEC_RELOC) == 0 |
5ce03cea | 3599 | || (o->flags & SEC_EXCLUDE) != 0 |
d9689752 L |
3600 | || o->reloc_count == 0 |
3601 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
3602 | && (o->flags & SEC_DEBUGGING) != 0) | |
3603 | || bfd_is_abs_section (o->output_section)) | |
3604 | continue; | |
3605 | ||
3606 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
3607 | info->keep_memory); | |
3608 | if (internal_relocs == NULL) | |
3609 | return FALSE; | |
3610 | ||
3611 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); | |
3612 | ||
3613 | if (elf_section_data (o)->relocs != internal_relocs) | |
3614 | free (internal_relocs); | |
3615 | ||
3616 | if (! ok) | |
3617 | return FALSE; | |
3618 | } | |
3619 | } | |
3620 | ||
3621 | return TRUE; | |
3622 | } | |
3623 | ||
4ad4eba5 AM |
3624 | /* Add symbols from an ELF object file to the linker hash table. */ |
3625 | ||
3626 | static bfd_boolean | |
3627 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3628 | { | |
a0c402a5 | 3629 | Elf_Internal_Ehdr *ehdr; |
4ad4eba5 | 3630 | Elf_Internal_Shdr *hdr; |
ef53be89 AM |
3631 | size_t symcount; |
3632 | size_t extsymcount; | |
3633 | size_t extsymoff; | |
4ad4eba5 AM |
3634 | struct elf_link_hash_entry **sym_hash; |
3635 | bfd_boolean dynamic; | |
3636 | Elf_External_Versym *extversym = NULL; | |
3637 | Elf_External_Versym *ever; | |
3638 | struct elf_link_hash_entry *weaks; | |
3639 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
ef53be89 | 3640 | size_t nondeflt_vers_cnt = 0; |
4ad4eba5 AM |
3641 | Elf_Internal_Sym *isymbuf = NULL; |
3642 | Elf_Internal_Sym *isym; | |
3643 | Elf_Internal_Sym *isymend; | |
3644 | const struct elf_backend_data *bed; | |
3645 | bfd_boolean add_needed; | |
66eb6687 | 3646 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3647 | bfd_size_type amt; |
66eb6687 | 3648 | void *alloc_mark = NULL; |
4f87808c AM |
3649 | struct bfd_hash_entry **old_table = NULL; |
3650 | unsigned int old_size = 0; | |
3651 | unsigned int old_count = 0; | |
66eb6687 | 3652 | void *old_tab = NULL; |
66eb6687 AM |
3653 | void *old_ent; |
3654 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3655 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
5b677558 | 3656 | void *old_strtab = NULL; |
66eb6687 | 3657 | size_t tabsize = 0; |
db6a5d5f | 3658 | asection *s; |
29a9f53e | 3659 | bfd_boolean just_syms; |
4ad4eba5 | 3660 | |
66eb6687 | 3661 | htab = elf_hash_table (info); |
4ad4eba5 | 3662 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3663 | |
3664 | if ((abfd->flags & DYNAMIC) == 0) | |
3665 | dynamic = FALSE; | |
3666 | else | |
3667 | { | |
3668 | dynamic = TRUE; | |
3669 | ||
3670 | /* You can't use -r against a dynamic object. Also, there's no | |
3671 | hope of using a dynamic object which does not exactly match | |
3672 | the format of the output file. */ | |
0e1862bb | 3673 | if (bfd_link_relocatable (info) |
66eb6687 | 3674 | || !is_elf_hash_table (htab) |
f13a99db | 3675 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 3676 | { |
0e1862bb | 3677 | if (bfd_link_relocatable (info)) |
9a0789ec NC |
3678 | bfd_set_error (bfd_error_invalid_operation); |
3679 | else | |
3680 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3681 | goto error_return; |
3682 | } | |
3683 | } | |
3684 | ||
a0c402a5 L |
3685 | ehdr = elf_elfheader (abfd); |
3686 | if (info->warn_alternate_em | |
3687 | && bed->elf_machine_code != ehdr->e_machine | |
3688 | && ((bed->elf_machine_alt1 != 0 | |
3689 | && ehdr->e_machine == bed->elf_machine_alt1) | |
3690 | || (bed->elf_machine_alt2 != 0 | |
3691 | && ehdr->e_machine == bed->elf_machine_alt2))) | |
3692 | info->callbacks->einfo | |
3693 | (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"), | |
3694 | ehdr->e_machine, abfd, bed->elf_machine_code); | |
3695 | ||
4ad4eba5 AM |
3696 | /* As a GNU extension, any input sections which are named |
3697 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3698 | symbol. This differs from .gnu.warning sections, which generate | |
3699 | warnings when they are included in an output file. */ | |
dd98f8d2 | 3700 | /* PR 12761: Also generate this warning when building shared libraries. */ |
db6a5d5f | 3701 | for (s = abfd->sections; s != NULL; s = s->next) |
4ad4eba5 | 3702 | { |
db6a5d5f | 3703 | const char *name; |
4ad4eba5 | 3704 | |
db6a5d5f AM |
3705 | name = bfd_get_section_name (abfd, s); |
3706 | if (CONST_STRNEQ (name, ".gnu.warning.")) | |
4ad4eba5 | 3707 | { |
db6a5d5f AM |
3708 | char *msg; |
3709 | bfd_size_type sz; | |
3710 | ||
3711 | name += sizeof ".gnu.warning." - 1; | |
3712 | ||
3713 | /* If this is a shared object, then look up the symbol | |
3714 | in the hash table. If it is there, and it is already | |
3715 | been defined, then we will not be using the entry | |
3716 | from this shared object, so we don't need to warn. | |
3717 | FIXME: If we see the definition in a regular object | |
3718 | later on, we will warn, but we shouldn't. The only | |
3719 | fix is to keep track of what warnings we are supposed | |
3720 | to emit, and then handle them all at the end of the | |
3721 | link. */ | |
3722 | if (dynamic) | |
4ad4eba5 | 3723 | { |
db6a5d5f AM |
3724 | struct elf_link_hash_entry *h; |
3725 | ||
3726 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); | |
3727 | ||
3728 | /* FIXME: What about bfd_link_hash_common? */ | |
3729 | if (h != NULL | |
3730 | && (h->root.type == bfd_link_hash_defined | |
3731 | || h->root.type == bfd_link_hash_defweak)) | |
3732 | continue; | |
3733 | } | |
4ad4eba5 | 3734 | |
db6a5d5f AM |
3735 | sz = s->size; |
3736 | msg = (char *) bfd_alloc (abfd, sz + 1); | |
3737 | if (msg == NULL) | |
3738 | goto error_return; | |
4ad4eba5 | 3739 | |
db6a5d5f AM |
3740 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
3741 | goto error_return; | |
4ad4eba5 | 3742 | |
db6a5d5f | 3743 | msg[sz] = '\0'; |
4ad4eba5 | 3744 | |
db6a5d5f AM |
3745 | if (! (_bfd_generic_link_add_one_symbol |
3746 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3747 | FALSE, bed->collect, NULL))) | |
3748 | goto error_return; | |
4ad4eba5 | 3749 | |
0e1862bb | 3750 | if (bfd_link_executable (info)) |
db6a5d5f AM |
3751 | { |
3752 | /* Clobber the section size so that the warning does | |
3753 | not get copied into the output file. */ | |
3754 | s->size = 0; | |
11d2f718 | 3755 | |
db6a5d5f AM |
3756 | /* Also set SEC_EXCLUDE, so that symbols defined in |
3757 | the warning section don't get copied to the output. */ | |
3758 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3759 | } |
3760 | } | |
3761 | } | |
3762 | ||
29a9f53e L |
3763 | just_syms = ((s = abfd->sections) != NULL |
3764 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); | |
3765 | ||
4ad4eba5 AM |
3766 | add_needed = TRUE; |
3767 | if (! dynamic) | |
3768 | { | |
3769 | /* If we are creating a shared library, create all the dynamic | |
3770 | sections immediately. We need to attach them to something, | |
3771 | so we attach them to this BFD, provided it is the right | |
bf89386a L |
3772 | format and is not from ld --just-symbols. Always create the |
3773 | dynamic sections for -E/--dynamic-list. FIXME: If there | |
29a9f53e L |
3774 | are no input BFD's of the same format as the output, we can't |
3775 | make a shared library. */ | |
3776 | if (!just_syms | |
bf89386a | 3777 | && (bfd_link_pic (info) |
9c1d7a08 L |
3778 | || (!bfd_link_relocatable (info) |
3779 | && (info->export_dynamic || info->dynamic))) | |
66eb6687 | 3780 | && is_elf_hash_table (htab) |
f13a99db | 3781 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 3782 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
3783 | { |
3784 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3785 | goto error_return; | |
3786 | } | |
3787 | } | |
66eb6687 | 3788 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
3789 | goto error_return; |
3790 | else | |
3791 | { | |
4ad4eba5 | 3792 | const char *soname = NULL; |
7ee314fa | 3793 | char *audit = NULL; |
4ad4eba5 AM |
3794 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; |
3795 | int ret; | |
3796 | ||
3797 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3798 | ld shouldn't allow it. */ |
29a9f53e | 3799 | if (just_syms) |
92fd189d | 3800 | abort (); |
4ad4eba5 AM |
3801 | |
3802 | /* If this dynamic lib was specified on the command line with | |
3803 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3804 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3805 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3806 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3807 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3808 | all. */ | |
3809 | add_needed = (elf_dyn_lib_class (abfd) | |
3810 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3811 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3812 | |
3813 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3814 | if (s != NULL) | |
3815 | { | |
3816 | bfd_byte *dynbuf; | |
3817 | bfd_byte *extdyn; | |
cb33740c | 3818 | unsigned int elfsec; |
4ad4eba5 AM |
3819 | unsigned long shlink; |
3820 | ||
eea6121a | 3821 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
f8703194 L |
3822 | { |
3823 | error_free_dyn: | |
3824 | free (dynbuf); | |
3825 | goto error_return; | |
3826 | } | |
4ad4eba5 AM |
3827 | |
3828 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 3829 | if (elfsec == SHN_BAD) |
4ad4eba5 AM |
3830 | goto error_free_dyn; |
3831 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3832 | ||
3833 | for (extdyn = dynbuf; | |
eea6121a | 3834 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3835 | extdyn += bed->s->sizeof_dyn) |
3836 | { | |
3837 | Elf_Internal_Dyn dyn; | |
3838 | ||
3839 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3840 | if (dyn.d_tag == DT_SONAME) | |
3841 | { | |
3842 | unsigned int tagv = dyn.d_un.d_val; | |
3843 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3844 | if (soname == NULL) | |
3845 | goto error_free_dyn; | |
3846 | } | |
3847 | if (dyn.d_tag == DT_NEEDED) | |
3848 | { | |
3849 | struct bfd_link_needed_list *n, **pn; | |
3850 | char *fnm, *anm; | |
3851 | unsigned int tagv = dyn.d_un.d_val; | |
3852 | ||
3853 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3854 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3855 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3856 | if (n == NULL || fnm == NULL) | |
3857 | goto error_free_dyn; | |
3858 | amt = strlen (fnm) + 1; | |
a50b1753 | 3859 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3860 | if (anm == NULL) |
3861 | goto error_free_dyn; | |
3862 | memcpy (anm, fnm, amt); | |
3863 | n->name = anm; | |
3864 | n->by = abfd; | |
3865 | n->next = NULL; | |
66eb6687 | 3866 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3867 | ; |
3868 | *pn = n; | |
3869 | } | |
3870 | if (dyn.d_tag == DT_RUNPATH) | |
3871 | { | |
3872 | struct bfd_link_needed_list *n, **pn; | |
3873 | char *fnm, *anm; | |
3874 | unsigned int tagv = dyn.d_un.d_val; | |
3875 | ||
3876 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3877 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3878 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3879 | if (n == NULL || fnm == NULL) | |
3880 | goto error_free_dyn; | |
3881 | amt = strlen (fnm) + 1; | |
a50b1753 | 3882 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3883 | if (anm == NULL) |
3884 | goto error_free_dyn; | |
3885 | memcpy (anm, fnm, amt); | |
3886 | n->name = anm; | |
3887 | n->by = abfd; | |
3888 | n->next = NULL; | |
3889 | for (pn = & runpath; | |
3890 | *pn != NULL; | |
3891 | pn = &(*pn)->next) | |
3892 | ; | |
3893 | *pn = n; | |
3894 | } | |
3895 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3896 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3897 | { | |
3898 | struct bfd_link_needed_list *n, **pn; | |
3899 | char *fnm, *anm; | |
3900 | unsigned int tagv = dyn.d_un.d_val; | |
3901 | ||
3902 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3903 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3904 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3905 | if (n == NULL || fnm == NULL) | |
3906 | goto error_free_dyn; | |
3907 | amt = strlen (fnm) + 1; | |
a50b1753 | 3908 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 | 3909 | if (anm == NULL) |
f8703194 | 3910 | goto error_free_dyn; |
4ad4eba5 AM |
3911 | memcpy (anm, fnm, amt); |
3912 | n->name = anm; | |
3913 | n->by = abfd; | |
3914 | n->next = NULL; | |
3915 | for (pn = & rpath; | |
3916 | *pn != NULL; | |
3917 | pn = &(*pn)->next) | |
3918 | ; | |
3919 | *pn = n; | |
3920 | } | |
7ee314fa AM |
3921 | if (dyn.d_tag == DT_AUDIT) |
3922 | { | |
3923 | unsigned int tagv = dyn.d_un.d_val; | |
3924 | audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3925 | } | |
4ad4eba5 AM |
3926 | } |
3927 | ||
3928 | free (dynbuf); | |
3929 | } | |
3930 | ||
3931 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3932 | frees all more recently bfd_alloc'd blocks as well. */ | |
3933 | if (runpath) | |
3934 | rpath = runpath; | |
3935 | ||
3936 | if (rpath) | |
3937 | { | |
3938 | struct bfd_link_needed_list **pn; | |
66eb6687 | 3939 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3940 | ; |
3941 | *pn = rpath; | |
3942 | } | |
3943 | ||
3944 | /* We do not want to include any of the sections in a dynamic | |
3945 | object in the output file. We hack by simply clobbering the | |
3946 | list of sections in the BFD. This could be handled more | |
3947 | cleanly by, say, a new section flag; the existing | |
3948 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3949 | still implies that the section takes up space in the output | |
3950 | file. */ | |
3951 | bfd_section_list_clear (abfd); | |
3952 | ||
4ad4eba5 AM |
3953 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3954 | object. If the object has a DT_SONAME entry, we use it. | |
3955 | Otherwise, if the generic linker stuck something in | |
3956 | elf_dt_name, we use that. Otherwise, we just use the file | |
3957 | name. */ | |
3958 | if (soname == NULL || *soname == '\0') | |
3959 | { | |
3960 | soname = elf_dt_name (abfd); | |
3961 | if (soname == NULL || *soname == '\0') | |
3962 | soname = bfd_get_filename (abfd); | |
3963 | } | |
3964 | ||
3965 | /* Save the SONAME because sometimes the linker emulation code | |
3966 | will need to know it. */ | |
3967 | elf_dt_name (abfd) = soname; | |
3968 | ||
7e9f0867 | 3969 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3970 | if (ret < 0) |
3971 | goto error_return; | |
3972 | ||
3973 | /* If we have already included this dynamic object in the | |
3974 | link, just ignore it. There is no reason to include a | |
3975 | particular dynamic object more than once. */ | |
3976 | if (ret > 0) | |
3977 | return TRUE; | |
7ee314fa AM |
3978 | |
3979 | /* Save the DT_AUDIT entry for the linker emulation code. */ | |
68ffbac6 | 3980 | elf_dt_audit (abfd) = audit; |
4ad4eba5 AM |
3981 | } |
3982 | ||
3983 | /* If this is a dynamic object, we always link against the .dynsym | |
3984 | symbol table, not the .symtab symbol table. The dynamic linker | |
3985 | will only see the .dynsym symbol table, so there is no reason to | |
3986 | look at .symtab for a dynamic object. */ | |
3987 | ||
3988 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3989 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3990 | else | |
3991 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3992 | ||
3993 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3994 | ||
3995 | /* The sh_info field of the symtab header tells us where the | |
3996 | external symbols start. We don't care about the local symbols at | |
3997 | this point. */ | |
3998 | if (elf_bad_symtab (abfd)) | |
3999 | { | |
4000 | extsymcount = symcount; | |
4001 | extsymoff = 0; | |
4002 | } | |
4003 | else | |
4004 | { | |
4005 | extsymcount = symcount - hdr->sh_info; | |
4006 | extsymoff = hdr->sh_info; | |
4007 | } | |
4008 | ||
f45794cb | 4009 | sym_hash = elf_sym_hashes (abfd); |
012b2306 | 4010 | if (extsymcount != 0) |
4ad4eba5 AM |
4011 | { |
4012 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
4013 | NULL, NULL, NULL); | |
4014 | if (isymbuf == NULL) | |
4015 | goto error_return; | |
4016 | ||
4ad4eba5 | 4017 | if (sym_hash == NULL) |
012b2306 AM |
4018 | { |
4019 | /* We store a pointer to the hash table entry for each | |
4020 | external symbol. */ | |
ef53be89 AM |
4021 | amt = extsymcount; |
4022 | amt *= sizeof (struct elf_link_hash_entry *); | |
012b2306 AM |
4023 | sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); |
4024 | if (sym_hash == NULL) | |
4025 | goto error_free_sym; | |
4026 | elf_sym_hashes (abfd) = sym_hash; | |
4027 | } | |
4ad4eba5 AM |
4028 | } |
4029 | ||
4030 | if (dynamic) | |
4031 | { | |
4032 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
4033 | if (!_bfd_elf_slurp_version_tables (abfd, |
4034 | info->default_imported_symver)) | |
4ad4eba5 AM |
4035 | goto error_free_sym; |
4036 | ||
4037 | /* Read in the symbol versions, but don't bother to convert them | |
4038 | to internal format. */ | |
4039 | if (elf_dynversym (abfd) != 0) | |
4040 | { | |
4041 | Elf_Internal_Shdr *versymhdr; | |
4042 | ||
4043 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
a50b1753 | 4044 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
4ad4eba5 AM |
4045 | if (extversym == NULL) |
4046 | goto error_free_sym; | |
4047 | amt = versymhdr->sh_size; | |
4048 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
4049 | || bfd_bread (extversym, amt, abfd) != amt) | |
4050 | goto error_free_vers; | |
4051 | } | |
4052 | } | |
4053 | ||
66eb6687 AM |
4054 | /* If we are loading an as-needed shared lib, save the symbol table |
4055 | state before we start adding symbols. If the lib turns out | |
4056 | to be unneeded, restore the state. */ | |
4057 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4058 | { | |
4059 | unsigned int i; | |
4060 | size_t entsize; | |
4061 | ||
4062 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
4063 | { | |
4064 | struct bfd_hash_entry *p; | |
2de92251 | 4065 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4066 | |
4067 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
4068 | { |
4069 | h = (struct elf_link_hash_entry *) p; | |
4070 | entsize += htab->root.table.entsize; | |
4071 | if (h->root.type == bfd_link_hash_warning) | |
4072 | entsize += htab->root.table.entsize; | |
4073 | } | |
66eb6687 AM |
4074 | } |
4075 | ||
4076 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
f45794cb | 4077 | old_tab = bfd_malloc (tabsize + entsize); |
66eb6687 AM |
4078 | if (old_tab == NULL) |
4079 | goto error_free_vers; | |
4080 | ||
4081 | /* Remember the current objalloc pointer, so that all mem for | |
4082 | symbols added can later be reclaimed. */ | |
4083 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
4084 | if (alloc_mark == NULL) | |
4085 | goto error_free_vers; | |
4086 | ||
5061a885 AM |
4087 | /* Make a special call to the linker "notice" function to |
4088 | tell it that we are about to handle an as-needed lib. */ | |
e5034e59 | 4089 | if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) |
9af2a943 | 4090 | goto error_free_vers; |
5061a885 | 4091 | |
f45794cb AM |
4092 | /* Clone the symbol table. Remember some pointers into the |
4093 | symbol table, and dynamic symbol count. */ | |
4094 | old_ent = (char *) old_tab + tabsize; | |
66eb6687 | 4095 | memcpy (old_tab, htab->root.table.table, tabsize); |
66eb6687 AM |
4096 | old_undefs = htab->root.undefs; |
4097 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
4098 | old_table = htab->root.table.table; |
4099 | old_size = htab->root.table.size; | |
4100 | old_count = htab->root.table.count; | |
5b677558 AM |
4101 | old_strtab = _bfd_elf_strtab_save (htab->dynstr); |
4102 | if (old_strtab == NULL) | |
4103 | goto error_free_vers; | |
66eb6687 AM |
4104 | |
4105 | for (i = 0; i < htab->root.table.size; i++) | |
4106 | { | |
4107 | struct bfd_hash_entry *p; | |
2de92251 | 4108 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4109 | |
4110 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4111 | { | |
4112 | memcpy (old_ent, p, htab->root.table.entsize); | |
4113 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4114 | h = (struct elf_link_hash_entry *) p; |
4115 | if (h->root.type == bfd_link_hash_warning) | |
4116 | { | |
4117 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
4118 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4119 | } | |
66eb6687 AM |
4120 | } |
4121 | } | |
4122 | } | |
4ad4eba5 | 4123 | |
66eb6687 | 4124 | weaks = NULL; |
4ad4eba5 AM |
4125 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
4126 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
4127 | isym < isymend; | |
4128 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
4129 | { | |
4130 | int bind; | |
4131 | bfd_vma value; | |
af44c138 | 4132 | asection *sec, *new_sec; |
4ad4eba5 AM |
4133 | flagword flags; |
4134 | const char *name; | |
4135 | struct elf_link_hash_entry *h; | |
90c984fc | 4136 | struct elf_link_hash_entry *hi; |
4ad4eba5 AM |
4137 | bfd_boolean definition; |
4138 | bfd_boolean size_change_ok; | |
4139 | bfd_boolean type_change_ok; | |
4140 | bfd_boolean new_weakdef; | |
37a9e49a L |
4141 | bfd_boolean new_weak; |
4142 | bfd_boolean old_weak; | |
4ad4eba5 | 4143 | bfd_boolean override; |
a4d8e49b | 4144 | bfd_boolean common; |
97196564 | 4145 | bfd_boolean discarded; |
4ad4eba5 AM |
4146 | unsigned int old_alignment; |
4147 | bfd *old_bfd; | |
6e33951e | 4148 | bfd_boolean matched; |
4ad4eba5 AM |
4149 | |
4150 | override = FALSE; | |
4151 | ||
4152 | flags = BSF_NO_FLAGS; | |
4153 | sec = NULL; | |
4154 | value = isym->st_value; | |
a4d8e49b | 4155 | common = bed->common_definition (isym); |
97196564 | 4156 | discarded = FALSE; |
4ad4eba5 AM |
4157 | |
4158 | bind = ELF_ST_BIND (isym->st_info); | |
3e7a7d11 | 4159 | switch (bind) |
4ad4eba5 | 4160 | { |
3e7a7d11 | 4161 | case STB_LOCAL: |
4ad4eba5 AM |
4162 | /* This should be impossible, since ELF requires that all |
4163 | global symbols follow all local symbols, and that sh_info | |
4164 | point to the first global symbol. Unfortunately, Irix 5 | |
4165 | screws this up. */ | |
4166 | continue; | |
3e7a7d11 NC |
4167 | |
4168 | case STB_GLOBAL: | |
a4d8e49b | 4169 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 | 4170 | flags = BSF_GLOBAL; |
3e7a7d11 NC |
4171 | break; |
4172 | ||
4173 | case STB_WEAK: | |
4174 | flags = BSF_WEAK; | |
4175 | break; | |
4176 | ||
4177 | case STB_GNU_UNIQUE: | |
4178 | flags = BSF_GNU_UNIQUE; | |
4179 | break; | |
4180 | ||
4181 | default: | |
4ad4eba5 | 4182 | /* Leave it up to the processor backend. */ |
3e7a7d11 | 4183 | break; |
4ad4eba5 AM |
4184 | } |
4185 | ||
4186 | if (isym->st_shndx == SHN_UNDEF) | |
4187 | sec = bfd_und_section_ptr; | |
cb33740c AM |
4188 | else if (isym->st_shndx == SHN_ABS) |
4189 | sec = bfd_abs_section_ptr; | |
4190 | else if (isym->st_shndx == SHN_COMMON) | |
4191 | { | |
4192 | sec = bfd_com_section_ptr; | |
4193 | /* What ELF calls the size we call the value. What ELF | |
4194 | calls the value we call the alignment. */ | |
4195 | value = isym->st_size; | |
4196 | } | |
4197 | else | |
4ad4eba5 AM |
4198 | { |
4199 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
4200 | if (sec == NULL) | |
4201 | sec = bfd_abs_section_ptr; | |
dbaa2011 | 4202 | else if (discarded_section (sec)) |
529fcb95 | 4203 | { |
e5d08002 L |
4204 | /* Symbols from discarded section are undefined. We keep |
4205 | its visibility. */ | |
529fcb95 | 4206 | sec = bfd_und_section_ptr; |
97196564 | 4207 | discarded = TRUE; |
529fcb95 PB |
4208 | isym->st_shndx = SHN_UNDEF; |
4209 | } | |
4ad4eba5 AM |
4210 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
4211 | value -= sec->vma; | |
4212 | } | |
4ad4eba5 AM |
4213 | |
4214 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
4215 | isym->st_name); | |
4216 | if (name == NULL) | |
4217 | goto error_free_vers; | |
4218 | ||
4219 | if (isym->st_shndx == SHN_COMMON | |
02d00247 AM |
4220 | && (abfd->flags & BFD_PLUGIN) != 0) |
4221 | { | |
4222 | asection *xc = bfd_get_section_by_name (abfd, "COMMON"); | |
4223 | ||
4224 | if (xc == NULL) | |
4225 | { | |
4226 | flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP | |
4227 | | SEC_EXCLUDE); | |
4228 | xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); | |
4229 | if (xc == NULL) | |
4230 | goto error_free_vers; | |
4231 | } | |
4232 | sec = xc; | |
4233 | } | |
4234 | else if (isym->st_shndx == SHN_COMMON | |
4235 | && ELF_ST_TYPE (isym->st_info) == STT_TLS | |
0e1862bb | 4236 | && !bfd_link_relocatable (info)) |
4ad4eba5 AM |
4237 | { |
4238 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
4239 | ||
4240 | if (tcomm == NULL) | |
4241 | { | |
02d00247 AM |
4242 | flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON |
4243 | | SEC_LINKER_CREATED); | |
4244 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); | |
3496cb2a | 4245 | if (tcomm == NULL) |
4ad4eba5 AM |
4246 | goto error_free_vers; |
4247 | } | |
4248 | sec = tcomm; | |
4249 | } | |
66eb6687 | 4250 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 4251 | { |
66eb6687 AM |
4252 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
4253 | &sec, &value)) | |
4ad4eba5 AM |
4254 | goto error_free_vers; |
4255 | ||
4256 | /* The hook function sets the name to NULL if this symbol | |
4257 | should be skipped for some reason. */ | |
4258 | if (name == NULL) | |
4259 | continue; | |
4260 | } | |
4261 | ||
4262 | /* Sanity check that all possibilities were handled. */ | |
4263 | if (sec == NULL) | |
4264 | { | |
4265 | bfd_set_error (bfd_error_bad_value); | |
4266 | goto error_free_vers; | |
4267 | } | |
4268 | ||
191c0c42 AM |
4269 | /* Silently discard TLS symbols from --just-syms. There's |
4270 | no way to combine a static TLS block with a new TLS block | |
4271 | for this executable. */ | |
4272 | if (ELF_ST_TYPE (isym->st_info) == STT_TLS | |
4273 | && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
4274 | continue; | |
4275 | ||
4ad4eba5 AM |
4276 | if (bfd_is_und_section (sec) |
4277 | || bfd_is_com_section (sec)) | |
4278 | definition = FALSE; | |
4279 | else | |
4280 | definition = TRUE; | |
4281 | ||
4282 | size_change_ok = FALSE; | |
66eb6687 | 4283 | type_change_ok = bed->type_change_ok; |
37a9e49a | 4284 | old_weak = FALSE; |
6e33951e | 4285 | matched = FALSE; |
4ad4eba5 AM |
4286 | old_alignment = 0; |
4287 | old_bfd = NULL; | |
af44c138 | 4288 | new_sec = sec; |
4ad4eba5 | 4289 | |
66eb6687 | 4290 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
4291 | { |
4292 | Elf_Internal_Versym iver; | |
4293 | unsigned int vernum = 0; | |
4294 | bfd_boolean skip; | |
4295 | ||
fc0e6df6 | 4296 | if (ever == NULL) |
4ad4eba5 | 4297 | { |
fc0e6df6 PB |
4298 | if (info->default_imported_symver) |
4299 | /* Use the default symbol version created earlier. */ | |
4300 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
4301 | else | |
4302 | iver.vs_vers = 0; | |
4303 | } | |
4304 | else | |
4305 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
4306 | ||
4307 | vernum = iver.vs_vers & VERSYM_VERSION; | |
4308 | ||
4309 | /* If this is a hidden symbol, or if it is not version | |
4310 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
4311 | However, we do not modify a non-hidden absolute symbol |
4312 | if it is not a function, because it might be the version | |
4313 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 4314 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
4315 | || (vernum > 1 |
4316 | && (!bfd_is_abs_section (sec) | |
4317 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
4318 | { |
4319 | const char *verstr; | |
4320 | size_t namelen, verlen, newlen; | |
4321 | char *newname, *p; | |
4322 | ||
4323 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 4324 | { |
fc0e6df6 PB |
4325 | if (vernum > elf_tdata (abfd)->cverdefs) |
4326 | verstr = NULL; | |
4327 | else if (vernum > 1) | |
4328 | verstr = | |
4329 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
4330 | else | |
4331 | verstr = ""; | |
4ad4eba5 | 4332 | |
fc0e6df6 | 4333 | if (verstr == NULL) |
4ad4eba5 | 4334 | { |
fc0e6df6 PB |
4335 | (*_bfd_error_handler) |
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 | { | |
4371 | (*_bfd_error_handler) | |
4372 | (_("%B: %s: invalid needed version %d"), | |
4373 | abfd, name, vernum); | |
4374 | bfd_set_error (bfd_error_bad_value); | |
4375 | goto error_free_vers; | |
4ad4eba5 | 4376 | } |
4ad4eba5 | 4377 | } |
fc0e6df6 PB |
4378 | |
4379 | namelen = strlen (name); | |
4380 | verlen = strlen (verstr); | |
4381 | newlen = namelen + verlen + 2; | |
4382 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4383 | && isym->st_shndx != SHN_UNDEF) | |
4384 | ++newlen; | |
4385 | ||
a50b1753 | 4386 | newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
4387 | if (newname == NULL) |
4388 | goto error_free_vers; | |
4389 | memcpy (newname, name, namelen); | |
4390 | p = newname + namelen; | |
4391 | *p++ = ELF_VER_CHR; | |
4392 | /* If this is a defined non-hidden version symbol, | |
4393 | we add another @ to the name. This indicates the | |
4394 | default version of the symbol. */ | |
4395 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4396 | && isym->st_shndx != SHN_UNDEF) | |
4397 | *p++ = ELF_VER_CHR; | |
4398 | memcpy (p, verstr, verlen + 1); | |
4399 | ||
4400 | name = newname; | |
4ad4eba5 AM |
4401 | } |
4402 | ||
cd3416da AM |
4403 | /* If this symbol has default visibility and the user has |
4404 | requested we not re-export it, then mark it as hidden. */ | |
a0d49154 | 4405 | if (!bfd_is_und_section (sec) |
cd3416da | 4406 | && !dynamic |
ce875075 | 4407 | && abfd->no_export |
cd3416da AM |
4408 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) |
4409 | isym->st_other = (STV_HIDDEN | |
4410 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
4411 | ||
4f3fedcf AM |
4412 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, |
4413 | sym_hash, &old_bfd, &old_weak, | |
4414 | &old_alignment, &skip, &override, | |
6e33951e L |
4415 | &type_change_ok, &size_change_ok, |
4416 | &matched)) | |
4ad4eba5 AM |
4417 | goto error_free_vers; |
4418 | ||
4419 | if (skip) | |
4420 | continue; | |
4421 | ||
6e33951e L |
4422 | /* Override a definition only if the new symbol matches the |
4423 | existing one. */ | |
4424 | if (override && matched) | |
4ad4eba5 AM |
4425 | definition = FALSE; |
4426 | ||
4427 | h = *sym_hash; | |
4428 | while (h->root.type == bfd_link_hash_indirect | |
4429 | || h->root.type == bfd_link_hash_warning) | |
4430 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4431 | ||
4ad4eba5 | 4432 | if (elf_tdata (abfd)->verdef != NULL |
4ad4eba5 AM |
4433 | && vernum > 1 |
4434 | && definition) | |
4435 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4436 | } | |
4437 | ||
4438 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4439 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4440 | (struct bfd_link_hash_entry **) sym_hash))) |
4441 | goto error_free_vers; | |
4442 | ||
a43942db MR |
4443 | if ((flags & BSF_GNU_UNIQUE) |
4444 | && (abfd->flags & DYNAMIC) == 0 | |
4445 | && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour) | |
4446 | elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique; | |
4447 | ||
4ad4eba5 | 4448 | h = *sym_hash; |
90c984fc L |
4449 | /* We need to make sure that indirect symbol dynamic flags are |
4450 | updated. */ | |
4451 | hi = h; | |
4ad4eba5 AM |
4452 | while (h->root.type == bfd_link_hash_indirect |
4453 | || h->root.type == bfd_link_hash_warning) | |
4454 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3e7a7d11 | 4455 | |
97196564 L |
4456 | /* Setting the index to -3 tells elf_link_output_extsym that |
4457 | this symbol is defined in a discarded section. */ | |
4458 | if (discarded) | |
4459 | h->indx = -3; | |
4460 | ||
4ad4eba5 AM |
4461 | *sym_hash = h; |
4462 | ||
37a9e49a | 4463 | new_weak = (flags & BSF_WEAK) != 0; |
4ad4eba5 AM |
4464 | new_weakdef = FALSE; |
4465 | if (dynamic | |
4466 | && definition | |
37a9e49a | 4467 | && new_weak |
fcb93ecf | 4468 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4469 | && is_elf_hash_table (htab) |
f6e332e6 | 4470 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
4471 | { |
4472 | /* Keep a list of all weak defined non function symbols from | |
4473 | a dynamic object, using the weakdef field. Later in this | |
4474 | function we will set the weakdef field to the correct | |
4475 | value. We only put non-function symbols from dynamic | |
4476 | objects on this list, because that happens to be the only | |
4477 | time we need to know the normal symbol corresponding to a | |
4478 | weak symbol, and the information is time consuming to | |
4479 | figure out. If the weakdef field is not already NULL, | |
4480 | then this symbol was already defined by some previous | |
4481 | dynamic object, and we will be using that previous | |
4482 | definition anyhow. */ | |
4483 | ||
f6e332e6 | 4484 | h->u.weakdef = weaks; |
4ad4eba5 AM |
4485 | weaks = h; |
4486 | new_weakdef = TRUE; | |
4487 | } | |
4488 | ||
4489 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4490 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4491 | && h->root.type == bfd_link_hash_common) |
4492 | { | |
4493 | unsigned int align; | |
4494 | ||
a4d8e49b | 4495 | if (common) |
af44c138 L |
4496 | align = bfd_log2 (isym->st_value); |
4497 | else | |
4498 | { | |
4499 | /* The new symbol is a common symbol in a shared object. | |
4500 | We need to get the alignment from the section. */ | |
4501 | align = new_sec->alignment_power; | |
4502 | } | |
595213d4 | 4503 | if (align > old_alignment) |
4ad4eba5 AM |
4504 | h->root.u.c.p->alignment_power = align; |
4505 | else | |
4506 | h->root.u.c.p->alignment_power = old_alignment; | |
4507 | } | |
4508 | ||
66eb6687 | 4509 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4510 | { |
4f3fedcf AM |
4511 | /* Set a flag in the hash table entry indicating the type of |
4512 | reference or definition we just found. A dynamic symbol | |
4513 | is one which is referenced or defined by both a regular | |
4514 | object and a shared object. */ | |
4515 | bfd_boolean dynsym = FALSE; | |
4516 | ||
4517 | /* Plugin symbols aren't normal. Don't set def_regular or | |
4518 | ref_regular for them, or make them dynamic. */ | |
4519 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4520 | ; | |
4521 | else if (! dynamic) | |
4522 | { | |
4523 | if (! definition) | |
4524 | { | |
4525 | h->ref_regular = 1; | |
4526 | if (bind != STB_WEAK) | |
4527 | h->ref_regular_nonweak = 1; | |
4528 | } | |
4529 | else | |
4530 | { | |
4531 | h->def_regular = 1; | |
4532 | if (h->def_dynamic) | |
4533 | { | |
4534 | h->def_dynamic = 0; | |
4535 | h->ref_dynamic = 1; | |
4536 | } | |
4537 | } | |
4538 | ||
4539 | /* If the indirect symbol has been forced local, don't | |
4540 | make the real symbol dynamic. */ | |
4541 | if ((h == hi || !hi->forced_local) | |
0e1862bb | 4542 | && (bfd_link_dll (info) |
4f3fedcf AM |
4543 | || h->def_dynamic |
4544 | || h->ref_dynamic)) | |
4545 | dynsym = TRUE; | |
4546 | } | |
4547 | else | |
4548 | { | |
4549 | if (! definition) | |
4550 | { | |
4551 | h->ref_dynamic = 1; | |
4552 | hi->ref_dynamic = 1; | |
4553 | } | |
4554 | else | |
4555 | { | |
4556 | h->def_dynamic = 1; | |
4557 | hi->def_dynamic = 1; | |
4558 | } | |
4559 | ||
4560 | /* If the indirect symbol has been forced local, don't | |
4561 | make the real symbol dynamic. */ | |
4562 | if ((h == hi || !hi->forced_local) | |
4563 | && (h->def_regular | |
4564 | || h->ref_regular | |
4565 | || (h->u.weakdef != NULL | |
4566 | && ! new_weakdef | |
4567 | && h->u.weakdef->dynindx != -1))) | |
4568 | dynsym = TRUE; | |
4569 | } | |
4570 | ||
4571 | /* Check to see if we need to add an indirect symbol for | |
4572 | the default name. */ | |
4573 | if (definition | |
4574 | || (!override && h->root.type == bfd_link_hash_common)) | |
4575 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4576 | sec, value, &old_bfd, &dynsym)) | |
4577 | goto error_free_vers; | |
4ad4eba5 AM |
4578 | |
4579 | /* Check the alignment when a common symbol is involved. This | |
4580 | can change when a common symbol is overridden by a normal | |
4581 | definition or a common symbol is ignored due to the old | |
4582 | normal definition. We need to make sure the maximum | |
4583 | alignment is maintained. */ | |
a4d8e49b | 4584 | if ((old_alignment || common) |
4ad4eba5 AM |
4585 | && h->root.type != bfd_link_hash_common) |
4586 | { | |
4587 | unsigned int common_align; | |
4588 | unsigned int normal_align; | |
4589 | unsigned int symbol_align; | |
4590 | bfd *normal_bfd; | |
4591 | bfd *common_bfd; | |
4592 | ||
3a81e825 AM |
4593 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
4594 | || h->root.type == bfd_link_hash_defweak); | |
4595 | ||
4ad4eba5 AM |
4596 | symbol_align = ffs (h->root.u.def.value) - 1; |
4597 | if (h->root.u.def.section->owner != NULL | |
0616a280 AM |
4598 | && (h->root.u.def.section->owner->flags |
4599 | & (DYNAMIC | BFD_PLUGIN)) == 0) | |
4ad4eba5 AM |
4600 | { |
4601 | normal_align = h->root.u.def.section->alignment_power; | |
4602 | if (normal_align > symbol_align) | |
4603 | normal_align = symbol_align; | |
4604 | } | |
4605 | else | |
4606 | normal_align = symbol_align; | |
4607 | ||
4608 | if (old_alignment) | |
4609 | { | |
4610 | common_align = old_alignment; | |
4611 | common_bfd = old_bfd; | |
4612 | normal_bfd = abfd; | |
4613 | } | |
4614 | else | |
4615 | { | |
4616 | common_align = bfd_log2 (isym->st_value); | |
4617 | common_bfd = abfd; | |
4618 | normal_bfd = old_bfd; | |
4619 | } | |
4620 | ||
4621 | if (normal_align < common_align) | |
d07676f8 NC |
4622 | { |
4623 | /* PR binutils/2735 */ | |
4624 | if (normal_bfd == NULL) | |
4625 | (*_bfd_error_handler) | |
4f3fedcf AM |
4626 | (_("Warning: alignment %u of common symbol `%s' in %B is" |
4627 | " greater than the alignment (%u) of its section %A"), | |
d07676f8 NC |
4628 | common_bfd, h->root.u.def.section, |
4629 | 1 << common_align, name, 1 << normal_align); | |
4630 | else | |
4631 | (*_bfd_error_handler) | |
4632 | (_("Warning: alignment %u of symbol `%s' in %B" | |
4633 | " is smaller than %u in %B"), | |
4634 | normal_bfd, common_bfd, | |
4635 | 1 << normal_align, name, 1 << common_align); | |
4636 | } | |
4ad4eba5 AM |
4637 | } |
4638 | ||
83ad0046 | 4639 | /* Remember the symbol size if it isn't undefined. */ |
3a81e825 AM |
4640 | if (isym->st_size != 0 |
4641 | && isym->st_shndx != SHN_UNDEF | |
4ad4eba5 AM |
4642 | && (definition || h->size == 0)) |
4643 | { | |
83ad0046 L |
4644 | if (h->size != 0 |
4645 | && h->size != isym->st_size | |
4646 | && ! size_change_ok) | |
4ad4eba5 | 4647 | (*_bfd_error_handler) |
d003868e AM |
4648 | (_("Warning: size of symbol `%s' changed" |
4649 | " from %lu in %B to %lu in %B"), | |
4650 | old_bfd, abfd, | |
4ad4eba5 | 4651 | name, (unsigned long) h->size, |
d003868e | 4652 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
4653 | |
4654 | h->size = isym->st_size; | |
4655 | } | |
4656 | ||
4657 | /* If this is a common symbol, then we always want H->SIZE | |
4658 | to be the size of the common symbol. The code just above | |
4659 | won't fix the size if a common symbol becomes larger. We | |
4660 | don't warn about a size change here, because that is | |
4f3fedcf | 4661 | covered by --warn-common. Allow changes between different |
fcb93ecf | 4662 | function types. */ |
4ad4eba5 AM |
4663 | if (h->root.type == bfd_link_hash_common) |
4664 | h->size = h->root.u.c.size; | |
4665 | ||
4666 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
37a9e49a L |
4667 | && ((definition && !new_weak) |
4668 | || (old_weak && h->root.type == bfd_link_hash_common) | |
4669 | || h->type == STT_NOTYPE)) | |
4ad4eba5 | 4670 | { |
2955ec4c L |
4671 | unsigned int type = ELF_ST_TYPE (isym->st_info); |
4672 | ||
4673 | /* Turn an IFUNC symbol from a DSO into a normal FUNC | |
4674 | symbol. */ | |
4675 | if (type == STT_GNU_IFUNC | |
4676 | && (abfd->flags & DYNAMIC) != 0) | |
4677 | type = STT_FUNC; | |
4ad4eba5 | 4678 | |
2955ec4c L |
4679 | if (h->type != type) |
4680 | { | |
4681 | if (h->type != STT_NOTYPE && ! type_change_ok) | |
4682 | (*_bfd_error_handler) | |
4683 | (_("Warning: type of symbol `%s' changed" | |
4684 | " from %d to %d in %B"), | |
4685 | abfd, name, h->type, type); | |
4686 | ||
4687 | h->type = type; | |
4688 | } | |
4ad4eba5 AM |
4689 | } |
4690 | ||
54ac0771 | 4691 | /* Merge st_other field. */ |
b8417128 | 4692 | elf_merge_st_other (abfd, h, isym, sec, definition, dynamic); |
4ad4eba5 | 4693 | |
c3df8c14 | 4694 | /* We don't want to make debug symbol dynamic. */ |
0e1862bb L |
4695 | if (definition |
4696 | && (sec->flags & SEC_DEBUGGING) | |
4697 | && !bfd_link_relocatable (info)) | |
c3df8c14 AM |
4698 | dynsym = FALSE; |
4699 | ||
4f3fedcf AM |
4700 | /* Nor should we make plugin symbols dynamic. */ |
4701 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4702 | dynsym = FALSE; | |
4703 | ||
35fc36a8 | 4704 | if (definition) |
35399224 L |
4705 | { |
4706 | h->target_internal = isym->st_target_internal; | |
4707 | h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; | |
4708 | } | |
35fc36a8 | 4709 | |
4ad4eba5 AM |
4710 | if (definition && !dynamic) |
4711 | { | |
4712 | char *p = strchr (name, ELF_VER_CHR); | |
4713 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4714 | { | |
4715 | /* Queue non-default versions so that .symver x, x@FOO | |
4716 | aliases can be checked. */ | |
66eb6687 | 4717 | if (!nondeflt_vers) |
4ad4eba5 | 4718 | { |
66eb6687 AM |
4719 | amt = ((isymend - isym + 1) |
4720 | * sizeof (struct elf_link_hash_entry *)); | |
ca4be51c AM |
4721 | nondeflt_vers |
4722 | = (struct elf_link_hash_entry **) bfd_malloc (amt); | |
14b1c01e AM |
4723 | if (!nondeflt_vers) |
4724 | goto error_free_vers; | |
4ad4eba5 | 4725 | } |
66eb6687 | 4726 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4727 | } |
4728 | } | |
4729 | ||
4730 | if (dynsym && h->dynindx == -1) | |
4731 | { | |
c152c796 | 4732 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4733 | goto error_free_vers; |
f6e332e6 | 4734 | if (h->u.weakdef != NULL |
4ad4eba5 | 4735 | && ! new_weakdef |
f6e332e6 | 4736 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 4737 | { |
66eb6687 | 4738 | if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
4739 | goto error_free_vers; |
4740 | } | |
4741 | } | |
1f599d0e | 4742 | else if (h->dynindx != -1) |
4ad4eba5 AM |
4743 | /* If the symbol already has a dynamic index, but |
4744 | visibility says it should not be visible, turn it into | |
4745 | a local symbol. */ | |
4746 | switch (ELF_ST_VISIBILITY (h->other)) | |
4747 | { | |
4748 | case STV_INTERNAL: | |
4749 | case STV_HIDDEN: | |
4750 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4751 | dynsym = FALSE; | |
4752 | break; | |
4753 | } | |
4754 | ||
aef28989 L |
4755 | /* Don't add DT_NEEDED for references from the dummy bfd nor |
4756 | for unmatched symbol. */ | |
4ad4eba5 | 4757 | if (!add_needed |
aef28989 | 4758 | && matched |
4ad4eba5 | 4759 | && definition |
010e5ae2 | 4760 | && ((dynsym |
ffa9430d | 4761 | && h->ref_regular_nonweak |
4f3fedcf AM |
4762 | && (old_bfd == NULL |
4763 | || (old_bfd->flags & BFD_PLUGIN) == 0)) | |
ffa9430d | 4764 | || (h->ref_dynamic_nonweak |
010e5ae2 | 4765 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 |
7b15fa7a AM |
4766 | && !on_needed_list (elf_dt_name (abfd), |
4767 | htab->needed, NULL)))) | |
4ad4eba5 AM |
4768 | { |
4769 | int ret; | |
4770 | const char *soname = elf_dt_name (abfd); | |
4771 | ||
16e4ecc0 AM |
4772 | info->callbacks->minfo ("%!", soname, old_bfd, |
4773 | h->root.root.string); | |
4774 | ||
4ad4eba5 AM |
4775 | /* A symbol from a library loaded via DT_NEEDED of some |
4776 | other library is referenced by a regular object. | |
e56f61be | 4777 | Add a DT_NEEDED entry for it. Issue an error if |
b918acf9 NC |
4778 | --no-add-needed is used and the reference was not |
4779 | a weak one. */ | |
4f3fedcf | 4780 | if (old_bfd != NULL |
b918acf9 | 4781 | && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) |
e56f61be L |
4782 | { |
4783 | (*_bfd_error_handler) | |
3cbc5de0 | 4784 | (_("%B: undefined reference to symbol '%s'"), |
4f3fedcf | 4785 | old_bfd, name); |
ff5ac77b | 4786 | bfd_set_error (bfd_error_missing_dso); |
e56f61be L |
4787 | goto error_free_vers; |
4788 | } | |
4789 | ||
a50b1753 | 4790 | elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) |
ca4be51c | 4791 | (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); |
a5db907e | 4792 | |
4ad4eba5 | 4793 | add_needed = TRUE; |
7e9f0867 | 4794 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4795 | if (ret < 0) |
4796 | goto error_free_vers; | |
4797 | ||
4798 | BFD_ASSERT (ret == 0); | |
4799 | } | |
4800 | } | |
4801 | } | |
4802 | ||
66eb6687 AM |
4803 | if (extversym != NULL) |
4804 | { | |
4805 | free (extversym); | |
4806 | extversym = NULL; | |
4807 | } | |
4808 | ||
4809 | if (isymbuf != NULL) | |
4810 | { | |
4811 | free (isymbuf); | |
4812 | isymbuf = NULL; | |
4813 | } | |
4814 | ||
4815 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4816 | { | |
4817 | unsigned int i; | |
4818 | ||
4819 | /* Restore the symbol table. */ | |
f45794cb AM |
4820 | old_ent = (char *) old_tab + tabsize; |
4821 | memset (elf_sym_hashes (abfd), 0, | |
4822 | extsymcount * sizeof (struct elf_link_hash_entry *)); | |
4f87808c AM |
4823 | htab->root.table.table = old_table; |
4824 | htab->root.table.size = old_size; | |
4825 | htab->root.table.count = old_count; | |
66eb6687 | 4826 | memcpy (htab->root.table.table, old_tab, tabsize); |
66eb6687 AM |
4827 | htab->root.undefs = old_undefs; |
4828 | htab->root.undefs_tail = old_undefs_tail; | |
5b677558 AM |
4829 | _bfd_elf_strtab_restore (htab->dynstr, old_strtab); |
4830 | free (old_strtab); | |
4831 | old_strtab = NULL; | |
66eb6687 AM |
4832 | for (i = 0; i < htab->root.table.size; i++) |
4833 | { | |
4834 | struct bfd_hash_entry *p; | |
4835 | struct elf_link_hash_entry *h; | |
3e0882af L |
4836 | bfd_size_type size; |
4837 | unsigned int alignment_power; | |
66eb6687 AM |
4838 | |
4839 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4840 | { | |
4841 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
4842 | if (h->root.type == bfd_link_hash_warning) |
4843 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2de92251 | 4844 | |
3e0882af L |
4845 | /* Preserve the maximum alignment and size for common |
4846 | symbols even if this dynamic lib isn't on DT_NEEDED | |
a4542f1b | 4847 | since it can still be loaded at run time by another |
3e0882af L |
4848 | dynamic lib. */ |
4849 | if (h->root.type == bfd_link_hash_common) | |
4850 | { | |
4851 | size = h->root.u.c.size; | |
4852 | alignment_power = h->root.u.c.p->alignment_power; | |
4853 | } | |
4854 | else | |
4855 | { | |
4856 | size = 0; | |
4857 | alignment_power = 0; | |
4858 | } | |
66eb6687 AM |
4859 | memcpy (p, old_ent, htab->root.table.entsize); |
4860 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4861 | h = (struct elf_link_hash_entry *) p; |
4862 | if (h->root.type == bfd_link_hash_warning) | |
4863 | { | |
4864 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
4865 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
a4542f1b | 4866 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
2de92251 | 4867 | } |
a4542f1b | 4868 | if (h->root.type == bfd_link_hash_common) |
3e0882af L |
4869 | { |
4870 | if (size > h->root.u.c.size) | |
4871 | h->root.u.c.size = size; | |
4872 | if (alignment_power > h->root.u.c.p->alignment_power) | |
4873 | h->root.u.c.p->alignment_power = alignment_power; | |
4874 | } | |
66eb6687 AM |
4875 | } |
4876 | } | |
4877 | ||
5061a885 AM |
4878 | /* Make a special call to the linker "notice" function to |
4879 | tell it that symbols added for crefs may need to be removed. */ | |
e5034e59 | 4880 | if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) |
9af2a943 | 4881 | goto error_free_vers; |
5061a885 | 4882 | |
66eb6687 AM |
4883 | free (old_tab); |
4884 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
4885 | alloc_mark); | |
4886 | if (nondeflt_vers != NULL) | |
4887 | free (nondeflt_vers); | |
4888 | return TRUE; | |
4889 | } | |
2de92251 | 4890 | |
66eb6687 AM |
4891 | if (old_tab != NULL) |
4892 | { | |
e5034e59 | 4893 | if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) |
9af2a943 | 4894 | goto error_free_vers; |
66eb6687 AM |
4895 | free (old_tab); |
4896 | old_tab = NULL; | |
4897 | } | |
4898 | ||
c6e8a9a8 L |
4899 | /* Now that all the symbols from this input file are created, if |
4900 | not performing a relocatable link, handle .symver foo, foo@BAR | |
4901 | such that any relocs against foo become foo@BAR. */ | |
0e1862bb | 4902 | if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) |
4ad4eba5 | 4903 | { |
ef53be89 | 4904 | size_t cnt, symidx; |
4ad4eba5 AM |
4905 | |
4906 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
4907 | { | |
4908 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
4909 | char *shortname, *p; | |
4910 | ||
4911 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
4912 | if (p == NULL | |
4913 | || (h->root.type != bfd_link_hash_defined | |
4914 | && h->root.type != bfd_link_hash_defweak)) | |
4915 | continue; | |
4916 | ||
4917 | amt = p - h->root.root.string; | |
a50b1753 | 4918 | shortname = (char *) bfd_malloc (amt + 1); |
14b1c01e AM |
4919 | if (!shortname) |
4920 | goto error_free_vers; | |
4ad4eba5 AM |
4921 | memcpy (shortname, h->root.root.string, amt); |
4922 | shortname[amt] = '\0'; | |
4923 | ||
4924 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 4925 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
4926 | FALSE, FALSE, FALSE); |
4927 | if (hi != NULL | |
4928 | && hi->root.type == h->root.type | |
4929 | && hi->root.u.def.value == h->root.u.def.value | |
4930 | && hi->root.u.def.section == h->root.u.def.section) | |
4931 | { | |
4932 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4933 | hi->root.type = bfd_link_hash_indirect; | |
4934 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 4935 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
4936 | sym_hash = elf_sym_hashes (abfd); |
4937 | if (sym_hash) | |
4938 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4939 | if (sym_hash[symidx] == hi) | |
4940 | { | |
4941 | sym_hash[symidx] = h; | |
4942 | break; | |
4943 | } | |
4944 | } | |
4945 | free (shortname); | |
4946 | } | |
4947 | free (nondeflt_vers); | |
4948 | nondeflt_vers = NULL; | |
4949 | } | |
4950 | ||
4ad4eba5 AM |
4951 | /* Now set the weakdefs field correctly for all the weak defined |
4952 | symbols we found. The only way to do this is to search all the | |
4953 | symbols. Since we only need the information for non functions in | |
4954 | dynamic objects, that's the only time we actually put anything on | |
4955 | the list WEAKS. We need this information so that if a regular | |
4956 | object refers to a symbol defined weakly in a dynamic object, the | |
4957 | real symbol in the dynamic object is also put in the dynamic | |
4958 | symbols; we also must arrange for both symbols to point to the | |
4959 | same memory location. We could handle the general case of symbol | |
4960 | aliasing, but a general symbol alias can only be generated in | |
4961 | assembler code, handling it correctly would be very time | |
4962 | consuming, and other ELF linkers don't handle general aliasing | |
4963 | either. */ | |
4964 | if (weaks != NULL) | |
4965 | { | |
4966 | struct elf_link_hash_entry **hpp; | |
4967 | struct elf_link_hash_entry **hppend; | |
4968 | struct elf_link_hash_entry **sorted_sym_hash; | |
4969 | struct elf_link_hash_entry *h; | |
4970 | size_t sym_count; | |
4971 | ||
4972 | /* Since we have to search the whole symbol list for each weak | |
4973 | defined symbol, search time for N weak defined symbols will be | |
4974 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
ef53be89 AM |
4975 | amt = extsymcount; |
4976 | amt *= sizeof (struct elf_link_hash_entry *); | |
a50b1753 | 4977 | sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); |
4ad4eba5 AM |
4978 | if (sorted_sym_hash == NULL) |
4979 | goto error_return; | |
4980 | sym_hash = sorted_sym_hash; | |
4981 | hpp = elf_sym_hashes (abfd); | |
4982 | hppend = hpp + extsymcount; | |
4983 | sym_count = 0; | |
4984 | for (; hpp < hppend; hpp++) | |
4985 | { | |
4986 | h = *hpp; | |
4987 | if (h != NULL | |
4988 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 4989 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
4990 | { |
4991 | *sym_hash = h; | |
4992 | sym_hash++; | |
4993 | sym_count++; | |
4994 | } | |
4995 | } | |
4996 | ||
4997 | qsort (sorted_sym_hash, sym_count, | |
4998 | sizeof (struct elf_link_hash_entry *), | |
4999 | elf_sort_symbol); | |
5000 | ||
5001 | while (weaks != NULL) | |
5002 | { | |
5003 | struct elf_link_hash_entry *hlook; | |
5004 | asection *slook; | |
5005 | bfd_vma vlook; | |
ed54588d | 5006 | size_t i, j, idx = 0; |
4ad4eba5 AM |
5007 | |
5008 | hlook = weaks; | |
f6e332e6 AM |
5009 | weaks = hlook->u.weakdef; |
5010 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
5011 | |
5012 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
5013 | || hlook->root.type == bfd_link_hash_defweak | |
5014 | || hlook->root.type == bfd_link_hash_common | |
5015 | || hlook->root.type == bfd_link_hash_indirect); | |
5016 | slook = hlook->root.u.def.section; | |
5017 | vlook = hlook->root.u.def.value; | |
5018 | ||
4ad4eba5 AM |
5019 | i = 0; |
5020 | j = sym_count; | |
14160578 | 5021 | while (i != j) |
4ad4eba5 AM |
5022 | { |
5023 | bfd_signed_vma vdiff; | |
5024 | idx = (i + j) / 2; | |
14160578 | 5025 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5026 | vdiff = vlook - h->root.u.def.value; |
5027 | if (vdiff < 0) | |
5028 | j = idx; | |
5029 | else if (vdiff > 0) | |
5030 | i = idx + 1; | |
5031 | else | |
5032 | { | |
d3435ae8 | 5033 | int sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
5034 | if (sdiff < 0) |
5035 | j = idx; | |
5036 | else if (sdiff > 0) | |
5037 | i = idx + 1; | |
5038 | else | |
14160578 | 5039 | break; |
4ad4eba5 AM |
5040 | } |
5041 | } | |
5042 | ||
5043 | /* We didn't find a value/section match. */ | |
14160578 | 5044 | if (i == j) |
4ad4eba5 AM |
5045 | continue; |
5046 | ||
14160578 AM |
5047 | /* With multiple aliases, or when the weak symbol is already |
5048 | strongly defined, we have multiple matching symbols and | |
5049 | the binary search above may land on any of them. Step | |
5050 | one past the matching symbol(s). */ | |
5051 | while (++idx != j) | |
5052 | { | |
5053 | h = sorted_sym_hash[idx]; | |
5054 | if (h->root.u.def.section != slook | |
5055 | || h->root.u.def.value != vlook) | |
5056 | break; | |
5057 | } | |
5058 | ||
5059 | /* Now look back over the aliases. Since we sorted by size | |
5060 | as well as value and section, we'll choose the one with | |
5061 | the largest size. */ | |
5062 | while (idx-- != i) | |
4ad4eba5 | 5063 | { |
14160578 | 5064 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5065 | |
5066 | /* Stop if value or section doesn't match. */ | |
14160578 AM |
5067 | if (h->root.u.def.section != slook |
5068 | || h->root.u.def.value != vlook) | |
4ad4eba5 AM |
5069 | break; |
5070 | else if (h != hlook) | |
5071 | { | |
f6e332e6 | 5072 | hlook->u.weakdef = h; |
4ad4eba5 AM |
5073 | |
5074 | /* If the weak definition is in the list of dynamic | |
5075 | symbols, make sure the real definition is put | |
5076 | there as well. */ | |
5077 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
5078 | { | |
c152c796 | 5079 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4dd07732 AM |
5080 | { |
5081 | err_free_sym_hash: | |
5082 | free (sorted_sym_hash); | |
5083 | goto error_return; | |
5084 | } | |
4ad4eba5 AM |
5085 | } |
5086 | ||
5087 | /* If the real definition is in the list of dynamic | |
5088 | symbols, make sure the weak definition is put | |
5089 | there as well. If we don't do this, then the | |
5090 | dynamic loader might not merge the entries for the | |
5091 | real definition and the weak definition. */ | |
5092 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
5093 | { | |
c152c796 | 5094 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4dd07732 | 5095 | goto err_free_sym_hash; |
4ad4eba5 AM |
5096 | } |
5097 | break; | |
5098 | } | |
5099 | } | |
5100 | } | |
5101 | ||
5102 | free (sorted_sym_hash); | |
5103 | } | |
5104 | ||
33177bb1 AM |
5105 | if (bed->check_directives |
5106 | && !(*bed->check_directives) (abfd, info)) | |
5107 | return FALSE; | |
85fbca6a | 5108 | |
d9689752 L |
5109 | if (!info->check_relocs_after_open_input |
5110 | && !_bfd_elf_link_check_relocs (abfd, info)) | |
5111 | return FALSE; | |
4ad4eba5 AM |
5112 | |
5113 | /* If this is a non-traditional link, try to optimize the handling | |
5114 | of the .stab/.stabstr sections. */ | |
5115 | if (! dynamic | |
5116 | && ! info->traditional_format | |
66eb6687 | 5117 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
5118 | && (info->strip != strip_all && info->strip != strip_debugger)) |
5119 | { | |
5120 | asection *stabstr; | |
5121 | ||
5122 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
5123 | if (stabstr != NULL) | |
5124 | { | |
5125 | bfd_size_type string_offset = 0; | |
5126 | asection *stab; | |
5127 | ||
5128 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 5129 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
5130 | && (!stab->name[5] || |
5131 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
5132 | && (stab->flags & SEC_MERGE) == 0 | |
5133 | && !bfd_is_abs_section (stab->output_section)) | |
5134 | { | |
5135 | struct bfd_elf_section_data *secdata; | |
5136 | ||
5137 | secdata = elf_section_data (stab); | |
66eb6687 AM |
5138 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
5139 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
5140 | &string_offset)) |
5141 | goto error_return; | |
5142 | if (secdata->sec_info) | |
dbaa2011 | 5143 | stab->sec_info_type = SEC_INFO_TYPE_STABS; |
4ad4eba5 AM |
5144 | } |
5145 | } | |
5146 | } | |
5147 | ||
66eb6687 | 5148 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
5149 | { |
5150 | /* Add this bfd to the loaded list. */ | |
5151 | struct elf_link_loaded_list *n; | |
5152 | ||
ca4be51c | 5153 | n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); |
4ad4eba5 AM |
5154 | if (n == NULL) |
5155 | goto error_return; | |
5156 | n->abfd = abfd; | |
66eb6687 AM |
5157 | n->next = htab->loaded; |
5158 | htab->loaded = n; | |
4ad4eba5 AM |
5159 | } |
5160 | ||
5161 | return TRUE; | |
5162 | ||
5163 | error_free_vers: | |
66eb6687 AM |
5164 | if (old_tab != NULL) |
5165 | free (old_tab); | |
5b677558 AM |
5166 | if (old_strtab != NULL) |
5167 | free (old_strtab); | |
4ad4eba5 AM |
5168 | if (nondeflt_vers != NULL) |
5169 | free (nondeflt_vers); | |
5170 | if (extversym != NULL) | |
5171 | free (extversym); | |
5172 | error_free_sym: | |
5173 | if (isymbuf != NULL) | |
5174 | free (isymbuf); | |
5175 | error_return: | |
5176 | return FALSE; | |
5177 | } | |
5178 | ||
8387904d AM |
5179 | /* Return the linker hash table entry of a symbol that might be |
5180 | satisfied by an archive symbol. Return -1 on error. */ | |
5181 | ||
5182 | struct elf_link_hash_entry * | |
5183 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
5184 | struct bfd_link_info *info, | |
5185 | const char *name) | |
5186 | { | |
5187 | struct elf_link_hash_entry *h; | |
5188 | char *p, *copy; | |
5189 | size_t len, first; | |
5190 | ||
2a41f396 | 5191 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); |
8387904d AM |
5192 | if (h != NULL) |
5193 | return h; | |
5194 | ||
5195 | /* If this is a default version (the name contains @@), look up the | |
5196 | symbol again with only one `@' as well as without the version. | |
5197 | The effect is that references to the symbol with and without the | |
5198 | version will be matched by the default symbol in the archive. */ | |
5199 | ||
5200 | p = strchr (name, ELF_VER_CHR); | |
5201 | if (p == NULL || p[1] != ELF_VER_CHR) | |
5202 | return h; | |
5203 | ||
5204 | /* First check with only one `@'. */ | |
5205 | len = strlen (name); | |
a50b1753 | 5206 | copy = (char *) bfd_alloc (abfd, len); |
8387904d AM |
5207 | if (copy == NULL) |
5208 | return (struct elf_link_hash_entry *) 0 - 1; | |
5209 | ||
5210 | first = p - name + 1; | |
5211 | memcpy (copy, name, first); | |
5212 | memcpy (copy + first, name + first + 1, len - first); | |
5213 | ||
2a41f396 | 5214 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); |
8387904d AM |
5215 | if (h == NULL) |
5216 | { | |
5217 | /* We also need to check references to the symbol without the | |
5218 | version. */ | |
5219 | copy[first - 1] = '\0'; | |
5220 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
2a41f396 | 5221 | FALSE, FALSE, TRUE); |
8387904d AM |
5222 | } |
5223 | ||
5224 | bfd_release (abfd, copy); | |
5225 | return h; | |
5226 | } | |
5227 | ||
0ad989f9 | 5228 | /* Add symbols from an ELF archive file to the linker hash table. We |
13e570f8 AM |
5229 | don't use _bfd_generic_link_add_archive_symbols because we need to |
5230 | handle versioned symbols. | |
0ad989f9 L |
5231 | |
5232 | Fortunately, ELF archive handling is simpler than that done by | |
5233 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
5234 | oddities. In ELF, if we find a symbol in the archive map, and the | |
5235 | symbol is currently undefined, we know that we must pull in that | |
5236 | object file. | |
5237 | ||
5238 | Unfortunately, we do have to make multiple passes over the symbol | |
5239 | table until nothing further is resolved. */ | |
5240 | ||
4ad4eba5 AM |
5241 | static bfd_boolean |
5242 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
5243 | { |
5244 | symindex c; | |
13e570f8 | 5245 | unsigned char *included = NULL; |
0ad989f9 L |
5246 | carsym *symdefs; |
5247 | bfd_boolean loop; | |
5248 | bfd_size_type amt; | |
8387904d AM |
5249 | const struct elf_backend_data *bed; |
5250 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
5251 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
5252 | |
5253 | if (! bfd_has_map (abfd)) | |
5254 | { | |
5255 | /* An empty archive is a special case. */ | |
5256 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
5257 | return TRUE; | |
5258 | bfd_set_error (bfd_error_no_armap); | |
5259 | return FALSE; | |
5260 | } | |
5261 | ||
5262 | /* Keep track of all symbols we know to be already defined, and all | |
5263 | files we know to be already included. This is to speed up the | |
5264 | second and subsequent passes. */ | |
5265 | c = bfd_ardata (abfd)->symdef_count; | |
5266 | if (c == 0) | |
5267 | return TRUE; | |
5268 | amt = c; | |
13e570f8 AM |
5269 | amt *= sizeof (*included); |
5270 | included = (unsigned char *) bfd_zmalloc (amt); | |
5271 | if (included == NULL) | |
5272 | return FALSE; | |
0ad989f9 L |
5273 | |
5274 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
5275 | bed = get_elf_backend_data (abfd); |
5276 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
5277 | |
5278 | do | |
5279 | { | |
5280 | file_ptr last; | |
5281 | symindex i; | |
5282 | carsym *symdef; | |
5283 | carsym *symdefend; | |
5284 | ||
5285 | loop = FALSE; | |
5286 | last = -1; | |
5287 | ||
5288 | symdef = symdefs; | |
5289 | symdefend = symdef + c; | |
5290 | for (i = 0; symdef < symdefend; symdef++, i++) | |
5291 | { | |
5292 | struct elf_link_hash_entry *h; | |
5293 | bfd *element; | |
5294 | struct bfd_link_hash_entry *undefs_tail; | |
5295 | symindex mark; | |
5296 | ||
13e570f8 | 5297 | if (included[i]) |
0ad989f9 L |
5298 | continue; |
5299 | if (symdef->file_offset == last) | |
5300 | { | |
5301 | included[i] = TRUE; | |
5302 | continue; | |
5303 | } | |
5304 | ||
8387904d AM |
5305 | h = archive_symbol_lookup (abfd, info, symdef->name); |
5306 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
5307 | goto error_return; | |
0ad989f9 L |
5308 | |
5309 | if (h == NULL) | |
5310 | continue; | |
5311 | ||
5312 | if (h->root.type == bfd_link_hash_common) | |
5313 | { | |
5314 | /* We currently have a common symbol. The archive map contains | |
5315 | a reference to this symbol, so we may want to include it. We | |
5316 | only want to include it however, if this archive element | |
5317 | contains a definition of the symbol, not just another common | |
5318 | declaration of it. | |
5319 | ||
5320 | Unfortunately some archivers (including GNU ar) will put | |
5321 | declarations of common symbols into their archive maps, as | |
5322 | well as real definitions, so we cannot just go by the archive | |
5323 | map alone. Instead we must read in the element's symbol | |
5324 | table and check that to see what kind of symbol definition | |
5325 | this is. */ | |
5326 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
5327 | continue; | |
5328 | } | |
5329 | else if (h->root.type != bfd_link_hash_undefined) | |
5330 | { | |
5331 | if (h->root.type != bfd_link_hash_undefweak) | |
13e570f8 AM |
5332 | /* Symbol must be defined. Don't check it again. */ |
5333 | included[i] = TRUE; | |
0ad989f9 L |
5334 | continue; |
5335 | } | |
5336 | ||
5337 | /* We need to include this archive member. */ | |
5338 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
5339 | if (element == NULL) | |
5340 | goto error_return; | |
5341 | ||
5342 | if (! bfd_check_format (element, bfd_object)) | |
5343 | goto error_return; | |
5344 | ||
0ad989f9 L |
5345 | undefs_tail = info->hash->undefs_tail; |
5346 | ||
0e144ba7 AM |
5347 | if (!(*info->callbacks |
5348 | ->add_archive_element) (info, element, symdef->name, &element)) | |
b95a0a31 | 5349 | continue; |
0e144ba7 | 5350 | if (!bfd_link_add_symbols (element, info)) |
0ad989f9 L |
5351 | goto error_return; |
5352 | ||
5353 | /* If there are any new undefined symbols, we need to make | |
5354 | another pass through the archive in order to see whether | |
5355 | they can be defined. FIXME: This isn't perfect, because | |
5356 | common symbols wind up on undefs_tail and because an | |
5357 | undefined symbol which is defined later on in this pass | |
5358 | does not require another pass. This isn't a bug, but it | |
5359 | does make the code less efficient than it could be. */ | |
5360 | if (undefs_tail != info->hash->undefs_tail) | |
5361 | loop = TRUE; | |
5362 | ||
5363 | /* Look backward to mark all symbols from this object file | |
5364 | which we have already seen in this pass. */ | |
5365 | mark = i; | |
5366 | do | |
5367 | { | |
5368 | included[mark] = TRUE; | |
5369 | if (mark == 0) | |
5370 | break; | |
5371 | --mark; | |
5372 | } | |
5373 | while (symdefs[mark].file_offset == symdef->file_offset); | |
5374 | ||
5375 | /* We mark subsequent symbols from this object file as we go | |
5376 | on through the loop. */ | |
5377 | last = symdef->file_offset; | |
5378 | } | |
5379 | } | |
5380 | while (loop); | |
5381 | ||
0ad989f9 L |
5382 | free (included); |
5383 | ||
5384 | return TRUE; | |
5385 | ||
5386 | error_return: | |
0ad989f9 L |
5387 | if (included != NULL) |
5388 | free (included); | |
5389 | return FALSE; | |
5390 | } | |
4ad4eba5 AM |
5391 | |
5392 | /* Given an ELF BFD, add symbols to the global hash table as | |
5393 | appropriate. */ | |
5394 | ||
5395 | bfd_boolean | |
5396 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
5397 | { | |
5398 | switch (bfd_get_format (abfd)) | |
5399 | { | |
5400 | case bfd_object: | |
5401 | return elf_link_add_object_symbols (abfd, info); | |
5402 | case bfd_archive: | |
5403 | return elf_link_add_archive_symbols (abfd, info); | |
5404 | default: | |
5405 | bfd_set_error (bfd_error_wrong_format); | |
5406 | return FALSE; | |
5407 | } | |
5408 | } | |
5a580b3a | 5409 | \f |
14b1c01e AM |
5410 | struct hash_codes_info |
5411 | { | |
5412 | unsigned long *hashcodes; | |
5413 | bfd_boolean error; | |
5414 | }; | |
a0c8462f | 5415 | |
5a580b3a AM |
5416 | /* This function will be called though elf_link_hash_traverse to store |
5417 | all hash value of the exported symbols in an array. */ | |
5418 | ||
5419 | static bfd_boolean | |
5420 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5421 | { | |
a50b1753 | 5422 | struct hash_codes_info *inf = (struct hash_codes_info *) data; |
5a580b3a | 5423 | const char *name; |
5a580b3a AM |
5424 | unsigned long ha; |
5425 | char *alc = NULL; | |
5426 | ||
5a580b3a AM |
5427 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5428 | if (h->dynindx == -1) | |
5429 | return TRUE; | |
5430 | ||
5431 | name = h->root.root.string; | |
422f1182 | 5432 | if (h->versioned >= versioned) |
5a580b3a | 5433 | { |
422f1182 L |
5434 | char *p = strchr (name, ELF_VER_CHR); |
5435 | if (p != NULL) | |
14b1c01e | 5436 | { |
422f1182 L |
5437 | alc = (char *) bfd_malloc (p - name + 1); |
5438 | if (alc == NULL) | |
5439 | { | |
5440 | inf->error = TRUE; | |
5441 | return FALSE; | |
5442 | } | |
5443 | memcpy (alc, name, p - name); | |
5444 | alc[p - name] = '\0'; | |
5445 | name = alc; | |
14b1c01e | 5446 | } |
5a580b3a AM |
5447 | } |
5448 | ||
5449 | /* Compute the hash value. */ | |
5450 | ha = bfd_elf_hash (name); | |
5451 | ||
5452 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 5453 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
5454 | |
5455 | /* And store it in the struct so that we can put it in the hash table | |
5456 | later. */ | |
f6e332e6 | 5457 | h->u.elf_hash_value = ha; |
5a580b3a AM |
5458 | |
5459 | if (alc != NULL) | |
5460 | free (alc); | |
5461 | ||
5462 | return TRUE; | |
5463 | } | |
5464 | ||
fdc90cb4 JJ |
5465 | struct collect_gnu_hash_codes |
5466 | { | |
5467 | bfd *output_bfd; | |
5468 | const struct elf_backend_data *bed; | |
5469 | unsigned long int nsyms; | |
5470 | unsigned long int maskbits; | |
5471 | unsigned long int *hashcodes; | |
5472 | unsigned long int *hashval; | |
5473 | unsigned long int *indx; | |
5474 | unsigned long int *counts; | |
5475 | bfd_vma *bitmask; | |
5476 | bfd_byte *contents; | |
5477 | long int min_dynindx; | |
5478 | unsigned long int bucketcount; | |
5479 | unsigned long int symindx; | |
5480 | long int local_indx; | |
5481 | long int shift1, shift2; | |
5482 | unsigned long int mask; | |
14b1c01e | 5483 | bfd_boolean error; |
fdc90cb4 JJ |
5484 | }; |
5485 | ||
5486 | /* This function will be called though elf_link_hash_traverse to store | |
5487 | all hash value of the exported symbols in an array. */ | |
5488 | ||
5489 | static bfd_boolean | |
5490 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5491 | { | |
a50b1753 | 5492 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 | 5493 | const char *name; |
fdc90cb4 JJ |
5494 | unsigned long ha; |
5495 | char *alc = NULL; | |
5496 | ||
fdc90cb4 JJ |
5497 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5498 | if (h->dynindx == -1) | |
5499 | return TRUE; | |
5500 | ||
5501 | /* Ignore also local symbols and undefined symbols. */ | |
5502 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5503 | return TRUE; | |
5504 | ||
5505 | name = h->root.root.string; | |
422f1182 | 5506 | if (h->versioned >= versioned) |
fdc90cb4 | 5507 | { |
422f1182 L |
5508 | char *p = strchr (name, ELF_VER_CHR); |
5509 | if (p != NULL) | |
14b1c01e | 5510 | { |
422f1182 L |
5511 | alc = (char *) bfd_malloc (p - name + 1); |
5512 | if (alc == NULL) | |
5513 | { | |
5514 | s->error = TRUE; | |
5515 | return FALSE; | |
5516 | } | |
5517 | memcpy (alc, name, p - name); | |
5518 | alc[p - name] = '\0'; | |
5519 | name = alc; | |
14b1c01e | 5520 | } |
fdc90cb4 JJ |
5521 | } |
5522 | ||
5523 | /* Compute the hash value. */ | |
5524 | ha = bfd_elf_gnu_hash (name); | |
5525 | ||
5526 | /* Store the found hash value in the array for compute_bucket_count, | |
5527 | and also for .dynsym reordering purposes. */ | |
5528 | s->hashcodes[s->nsyms] = ha; | |
5529 | s->hashval[h->dynindx] = ha; | |
5530 | ++s->nsyms; | |
5531 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5532 | s->min_dynindx = h->dynindx; | |
5533 | ||
5534 | if (alc != NULL) | |
5535 | free (alc); | |
5536 | ||
5537 | return TRUE; | |
5538 | } | |
5539 | ||
5540 | /* This function will be called though elf_link_hash_traverse to do | |
5541 | final dynaminc symbol renumbering. */ | |
5542 | ||
5543 | static bfd_boolean | |
5544 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5545 | { | |
a50b1753 | 5546 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
5547 | unsigned long int bucket; |
5548 | unsigned long int val; | |
5549 | ||
fdc90cb4 JJ |
5550 | /* Ignore indirect symbols. */ |
5551 | if (h->dynindx == -1) | |
5552 | return TRUE; | |
5553 | ||
5554 | /* Ignore also local symbols and undefined symbols. */ | |
5555 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5556 | { | |
5557 | if (h->dynindx >= s->min_dynindx) | |
5558 | h->dynindx = s->local_indx++; | |
5559 | return TRUE; | |
5560 | } | |
5561 | ||
5562 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5563 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5564 | & ((s->maskbits >> s->shift1) - 1); | |
5565 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5566 | s->bitmask[val] | |
5567 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5568 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5569 | if (s->counts[bucket] == 1) | |
5570 | /* Last element terminates the chain. */ | |
5571 | val |= 1; | |
5572 | bfd_put_32 (s->output_bfd, val, | |
5573 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5574 | --s->counts[bucket]; | |
5575 | h->dynindx = s->indx[bucket]++; | |
5576 | return TRUE; | |
5577 | } | |
5578 | ||
5579 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5580 | ||
5581 | bfd_boolean | |
5582 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5583 | { | |
5584 | return !(h->forced_local | |
5585 | || h->root.type == bfd_link_hash_undefined | |
5586 | || h->root.type == bfd_link_hash_undefweak | |
5587 | || ((h->root.type == bfd_link_hash_defined | |
5588 | || h->root.type == bfd_link_hash_defweak) | |
5589 | && h->root.u.def.section->output_section == NULL)); | |
5590 | } | |
5591 | ||
5a580b3a AM |
5592 | /* Array used to determine the number of hash table buckets to use |
5593 | based on the number of symbols there are. If there are fewer than | |
5594 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5595 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5596 | than 32771 buckets. */ | |
5597 | ||
5598 | static const size_t elf_buckets[] = | |
5599 | { | |
5600 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5601 | 16411, 32771, 0 | |
5602 | }; | |
5603 | ||
5604 | /* Compute bucket count for hashing table. We do not use a static set | |
5605 | of possible tables sizes anymore. Instead we determine for all | |
5606 | possible reasonable sizes of the table the outcome (i.e., the | |
5607 | number of collisions etc) and choose the best solution. The | |
5608 | weighting functions are not too simple to allow the table to grow | |
5609 | without bounds. Instead one of the weighting factors is the size. | |
5610 | Therefore the result is always a good payoff between few collisions | |
5611 | (= short chain lengths) and table size. */ | |
5612 | static size_t | |
b20dd2ce | 5613 | compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
d40f3da9 AM |
5614 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, |
5615 | unsigned long int nsyms, | |
5616 | int gnu_hash) | |
5a580b3a | 5617 | { |
5a580b3a | 5618 | size_t best_size = 0; |
5a580b3a | 5619 | unsigned long int i; |
5a580b3a | 5620 | |
5a580b3a AM |
5621 | /* We have a problem here. The following code to optimize the table |
5622 | size requires an integer type with more the 32 bits. If | |
5623 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5624 | #ifdef BFD_HOST_U_64_BIT | |
5625 | if (info->optimize) | |
5626 | { | |
5a580b3a AM |
5627 | size_t minsize; |
5628 | size_t maxsize; | |
5629 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a | 5630 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 5631 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 5632 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 5633 | unsigned long int *counts; |
d40f3da9 | 5634 | bfd_size_type amt; |
0883b6e0 | 5635 | unsigned int no_improvement_count = 0; |
5a580b3a AM |
5636 | |
5637 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5638 | that the hashing table must at least have NSYMS/4 and at most | |
5639 | 2*NSYMS buckets. */ | |
5640 | minsize = nsyms / 4; | |
5641 | if (minsize == 0) | |
5642 | minsize = 1; | |
5643 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5644 | if (gnu_hash) |
5645 | { | |
5646 | if (minsize < 2) | |
5647 | minsize = 2; | |
5648 | if ((best_size & 31) == 0) | |
5649 | ++best_size; | |
5650 | } | |
5a580b3a AM |
5651 | |
5652 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5653 | since the size could be large. */ | |
5654 | amt = maxsize; | |
5655 | amt *= sizeof (unsigned long int); | |
a50b1753 | 5656 | counts = (unsigned long int *) bfd_malloc (amt); |
5a580b3a | 5657 | if (counts == NULL) |
fdc90cb4 | 5658 | return 0; |
5a580b3a AM |
5659 | |
5660 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5661 | minimal chain length. The minor criteria is (of course) the size | |
5662 | of the table. */ | |
5663 | for (i = minsize; i < maxsize; ++i) | |
5664 | { | |
5665 | /* Walk through the array of hashcodes and count the collisions. */ | |
5666 | BFD_HOST_U_64_BIT max; | |
5667 | unsigned long int j; | |
5668 | unsigned long int fact; | |
5669 | ||
fdc90cb4 JJ |
5670 | if (gnu_hash && (i & 31) == 0) |
5671 | continue; | |
5672 | ||
5a580b3a AM |
5673 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5674 | ||
5675 | /* Determine how often each hash bucket is used. */ | |
5676 | for (j = 0; j < nsyms; ++j) | |
5677 | ++counts[hashcodes[j] % i]; | |
5678 | ||
5679 | /* For the weight function we need some information about the | |
5680 | pagesize on the target. This is information need not be 100% | |
5681 | accurate. Since this information is not available (so far) we | |
5682 | define it here to a reasonable default value. If it is crucial | |
5683 | to have a better value some day simply define this value. */ | |
5684 | # ifndef BFD_TARGET_PAGESIZE | |
5685 | # define BFD_TARGET_PAGESIZE (4096) | |
5686 | # endif | |
5687 | ||
fdc90cb4 JJ |
5688 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5689 | and the chains. */ | |
5690 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
5691 | |
5692 | # if 1 | |
5693 | /* Variant 1: optimize for short chains. We add the squares | |
5694 | of all the chain lengths (which favors many small chain | |
5695 | over a few long chains). */ | |
5696 | for (j = 0; j < i; ++j) | |
5697 | max += counts[j] * counts[j]; | |
5698 | ||
5699 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 5700 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5701 | max *= fact * fact; |
5702 | # else | |
5703 | /* Variant 2: Optimize a lot more for small table. Here we | |
5704 | also add squares of the size but we also add penalties for | |
5705 | empty slots (the +1 term). */ | |
5706 | for (j = 0; j < i; ++j) | |
5707 | max += (1 + counts[j]) * (1 + counts[j]); | |
5708 | ||
5709 | /* The overall size of the table is considered, but not as | |
5710 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 5711 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5712 | max *= fact; |
5713 | # endif | |
5714 | ||
5715 | /* Compare with current best results. */ | |
5716 | if (max < best_chlen) | |
5717 | { | |
5718 | best_chlen = max; | |
5719 | best_size = i; | |
ca4be51c | 5720 | no_improvement_count = 0; |
5a580b3a | 5721 | } |
0883b6e0 NC |
5722 | /* PR 11843: Avoid futile long searches for the best bucket size |
5723 | when there are a large number of symbols. */ | |
5724 | else if (++no_improvement_count == 100) | |
5725 | break; | |
5a580b3a AM |
5726 | } |
5727 | ||
5728 | free (counts); | |
5729 | } | |
5730 | else | |
5731 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
5732 | { | |
5733 | /* This is the fallback solution if no 64bit type is available or if we | |
5734 | are not supposed to spend much time on optimizations. We select the | |
5735 | bucket count using a fixed set of numbers. */ | |
5736 | for (i = 0; elf_buckets[i] != 0; i++) | |
5737 | { | |
5738 | best_size = elf_buckets[i]; | |
fdc90cb4 | 5739 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
5740 | break; |
5741 | } | |
fdc90cb4 JJ |
5742 | if (gnu_hash && best_size < 2) |
5743 | best_size = 2; | |
5a580b3a AM |
5744 | } |
5745 | ||
5a580b3a AM |
5746 | return best_size; |
5747 | } | |
5748 | ||
d0bf826b AM |
5749 | /* Size any SHT_GROUP section for ld -r. */ |
5750 | ||
5751 | bfd_boolean | |
5752 | _bfd_elf_size_group_sections (struct bfd_link_info *info) | |
5753 | { | |
5754 | bfd *ibfd; | |
5755 | ||
c72f2fb2 | 5756 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
d0bf826b AM |
5757 | if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour |
5758 | && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) | |
5759 | return FALSE; | |
5760 | return TRUE; | |
5761 | } | |
5762 | ||
04c3a755 NS |
5763 | /* Set a default stack segment size. The value in INFO wins. If it |
5764 | is unset, LEGACY_SYMBOL's value is used, and if that symbol is | |
5765 | undefined it is initialized. */ | |
5766 | ||
5767 | bfd_boolean | |
5768 | bfd_elf_stack_segment_size (bfd *output_bfd, | |
5769 | struct bfd_link_info *info, | |
5770 | const char *legacy_symbol, | |
5771 | bfd_vma default_size) | |
5772 | { | |
5773 | struct elf_link_hash_entry *h = NULL; | |
5774 | ||
5775 | /* Look for legacy symbol. */ | |
5776 | if (legacy_symbol) | |
5777 | h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, | |
5778 | FALSE, FALSE, FALSE); | |
5779 | if (h && (h->root.type == bfd_link_hash_defined | |
5780 | || h->root.type == bfd_link_hash_defweak) | |
5781 | && h->def_regular | |
5782 | && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) | |
5783 | { | |
5784 | /* The symbol has no type if specified on the command line. */ | |
5785 | h->type = STT_OBJECT; | |
5786 | if (info->stacksize) | |
5787 | (*_bfd_error_handler) (_("%B: stack size specified and %s set"), | |
5788 | output_bfd, legacy_symbol); | |
5789 | else if (h->root.u.def.section != bfd_abs_section_ptr) | |
5790 | (*_bfd_error_handler) (_("%B: %s not absolute"), | |
5791 | output_bfd, legacy_symbol); | |
5792 | else | |
5793 | info->stacksize = h->root.u.def.value; | |
5794 | } | |
5795 | ||
5796 | if (!info->stacksize) | |
5797 | /* If the user didn't set a size, or explicitly inhibit the | |
5798 | size, set it now. */ | |
5799 | info->stacksize = default_size; | |
5800 | ||
5801 | /* Provide the legacy symbol, if it is referenced. */ | |
5802 | if (h && (h->root.type == bfd_link_hash_undefined | |
5803 | || h->root.type == bfd_link_hash_undefweak)) | |
5804 | { | |
5805 | struct bfd_link_hash_entry *bh = NULL; | |
5806 | ||
5807 | if (!(_bfd_generic_link_add_one_symbol | |
5808 | (info, output_bfd, legacy_symbol, | |
5809 | BSF_GLOBAL, bfd_abs_section_ptr, | |
5810 | info->stacksize >= 0 ? info->stacksize : 0, | |
5811 | NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh))) | |
5812 | return FALSE; | |
5813 | ||
5814 | h = (struct elf_link_hash_entry *) bh; | |
5815 | h->def_regular = 1; | |
5816 | h->type = STT_OBJECT; | |
5817 | } | |
5818 | ||
5819 | return TRUE; | |
5820 | } | |
5821 | ||
5a580b3a AM |
5822 | /* Set up the sizes and contents of the ELF dynamic sections. This is |
5823 | called by the ELF linker emulation before_allocation routine. We | |
5824 | must set the sizes of the sections before the linker sets the | |
5825 | addresses of the various sections. */ | |
5826 | ||
5827 | bfd_boolean | |
5828 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
5829 | const char *soname, | |
5830 | const char *rpath, | |
5831 | const char *filter_shlib, | |
7ee314fa AM |
5832 | const char *audit, |
5833 | const char *depaudit, | |
5a580b3a AM |
5834 | const char * const *auxiliary_filters, |
5835 | struct bfd_link_info *info, | |
fd91d419 | 5836 | asection **sinterpptr) |
5a580b3a | 5837 | { |
ef53be89 | 5838 | size_t soname_indx; |
5a580b3a AM |
5839 | bfd *dynobj; |
5840 | const struct elf_backend_data *bed; | |
28caa186 | 5841 | struct elf_info_failed asvinfo; |
5a580b3a AM |
5842 | |
5843 | *sinterpptr = NULL; | |
5844 | ||
ef53be89 | 5845 | soname_indx = (size_t) -1; |
5a580b3a AM |
5846 | |
5847 | if (!is_elf_hash_table (info->hash)) | |
5848 | return TRUE; | |
5849 | ||
6bfdb61b | 5850 | bed = get_elf_backend_data (output_bfd); |
04c3a755 NS |
5851 | |
5852 | /* Any syms created from now on start with -1 in | |
5853 | got.refcount/offset and plt.refcount/offset. */ | |
5854 | elf_hash_table (info)->init_got_refcount | |
5855 | = elf_hash_table (info)->init_got_offset; | |
5856 | elf_hash_table (info)->init_plt_refcount | |
5857 | = elf_hash_table (info)->init_plt_offset; | |
5858 | ||
0e1862bb | 5859 | if (bfd_link_relocatable (info) |
04c3a755 NS |
5860 | && !_bfd_elf_size_group_sections (info)) |
5861 | return FALSE; | |
5862 | ||
5863 | /* The backend may have to create some sections regardless of whether | |
5864 | we're dynamic or not. */ | |
5865 | if (bed->elf_backend_always_size_sections | |
5866 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
5867 | return FALSE; | |
5868 | ||
5869 | /* Determine any GNU_STACK segment requirements, after the backend | |
5870 | has had a chance to set a default segment size. */ | |
5a580b3a | 5871 | if (info->execstack) |
12bd6957 | 5872 | elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; |
5a580b3a | 5873 | else if (info->noexecstack) |
12bd6957 | 5874 | elf_stack_flags (output_bfd) = PF_R | PF_W; |
5a580b3a AM |
5875 | else |
5876 | { | |
5877 | bfd *inputobj; | |
5878 | asection *notesec = NULL; | |
5879 | int exec = 0; | |
5880 | ||
5881 | for (inputobj = info->input_bfds; | |
5882 | inputobj; | |
c72f2fb2 | 5883 | inputobj = inputobj->link.next) |
5a580b3a AM |
5884 | { |
5885 | asection *s; | |
5886 | ||
a92c088a L |
5887 | if (inputobj->flags |
5888 | & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) | |
5a580b3a AM |
5889 | continue; |
5890 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
5891 | if (s) | |
5892 | { | |
5893 | if (s->flags & SEC_CODE) | |
5894 | exec = PF_X; | |
5895 | notesec = s; | |
5896 | } | |
6bfdb61b | 5897 | else if (bed->default_execstack) |
5a580b3a AM |
5898 | exec = PF_X; |
5899 | } | |
04c3a755 | 5900 | if (notesec || info->stacksize > 0) |
12bd6957 | 5901 | elf_stack_flags (output_bfd) = PF_R | PF_W | exec; |
0e1862bb | 5902 | if (notesec && exec && bfd_link_relocatable (info) |
04c3a755 NS |
5903 | && notesec->output_section != bfd_abs_section_ptr) |
5904 | notesec->output_section->flags |= SEC_CODE; | |
5a580b3a AM |
5905 | } |
5906 | ||
5a580b3a AM |
5907 | dynobj = elf_hash_table (info)->dynobj; |
5908 | ||
9a2a56cc | 5909 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a AM |
5910 | { |
5911 | struct elf_info_failed eif; | |
5912 | struct elf_link_hash_entry *h; | |
5913 | asection *dynstr; | |
5914 | struct bfd_elf_version_tree *t; | |
5915 | struct bfd_elf_version_expr *d; | |
046183de | 5916 | asection *s; |
5a580b3a AM |
5917 | bfd_boolean all_defined; |
5918 | ||
3d4d4302 | 5919 | *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); |
9b8b325a | 5920 | BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); |
5a580b3a AM |
5921 | |
5922 | if (soname != NULL) | |
5923 | { | |
5924 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5925 | soname, TRUE); | |
ef53be89 | 5926 | if (soname_indx == (size_t) -1 |
5a580b3a AM |
5927 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) |
5928 | return FALSE; | |
5929 | } | |
5930 | ||
5931 | if (info->symbolic) | |
5932 | { | |
5933 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
5934 | return FALSE; | |
5935 | info->flags |= DF_SYMBOLIC; | |
5936 | } | |
5937 | ||
5938 | if (rpath != NULL) | |
5939 | { | |
ef53be89 | 5940 | size_t indx; |
b1b00fcc | 5941 | bfd_vma tag; |
5a580b3a AM |
5942 | |
5943 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
5944 | TRUE); | |
ef53be89 | 5945 | if (indx == (size_t) -1) |
5a580b3a AM |
5946 | return FALSE; |
5947 | ||
b1b00fcc MF |
5948 | tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; |
5949 | if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) | |
5950 | return FALSE; | |
5a580b3a AM |
5951 | } |
5952 | ||
5953 | if (filter_shlib != NULL) | |
5954 | { | |
ef53be89 | 5955 | size_t indx; |
5a580b3a AM |
5956 | |
5957 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5958 | filter_shlib, TRUE); | |
ef53be89 | 5959 | if (indx == (size_t) -1 |
5a580b3a AM |
5960 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) |
5961 | return FALSE; | |
5962 | } | |
5963 | ||
5964 | if (auxiliary_filters != NULL) | |
5965 | { | |
5966 | const char * const *p; | |
5967 | ||
5968 | for (p = auxiliary_filters; *p != NULL; p++) | |
5969 | { | |
ef53be89 | 5970 | size_t indx; |
5a580b3a AM |
5971 | |
5972 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5973 | *p, TRUE); | |
ef53be89 | 5974 | if (indx == (size_t) -1 |
5a580b3a AM |
5975 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) |
5976 | return FALSE; | |
5977 | } | |
5978 | } | |
5979 | ||
7ee314fa AM |
5980 | if (audit != NULL) |
5981 | { | |
ef53be89 | 5982 | size_t indx; |
7ee314fa AM |
5983 | |
5984 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, | |
5985 | TRUE); | |
ef53be89 | 5986 | if (indx == (size_t) -1 |
7ee314fa AM |
5987 | || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) |
5988 | return FALSE; | |
5989 | } | |
5990 | ||
5991 | if (depaudit != NULL) | |
5992 | { | |
ef53be89 | 5993 | size_t indx; |
7ee314fa AM |
5994 | |
5995 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, | |
5996 | TRUE); | |
ef53be89 | 5997 | if (indx == (size_t) -1 |
7ee314fa AM |
5998 | || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) |
5999 | return FALSE; | |
6000 | } | |
6001 | ||
5a580b3a | 6002 | eif.info = info; |
5a580b3a AM |
6003 | eif.failed = FALSE; |
6004 | ||
6005 | /* If we are supposed to export all symbols into the dynamic symbol | |
6006 | table (this is not the normal case), then do so. */ | |
55255dae | 6007 | if (info->export_dynamic |
0e1862bb | 6008 | || (bfd_link_executable (info) && info->dynamic)) |
5a580b3a AM |
6009 | { |
6010 | elf_link_hash_traverse (elf_hash_table (info), | |
6011 | _bfd_elf_export_symbol, | |
6012 | &eif); | |
6013 | if (eif.failed) | |
6014 | return FALSE; | |
6015 | } | |
6016 | ||
6017 | /* Make all global versions with definition. */ | |
fd91d419 | 6018 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6019 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6020 | if (!d->symver && d->literal) |
5a580b3a AM |
6021 | { |
6022 | const char *verstr, *name; | |
6023 | size_t namelen, verlen, newlen; | |
93252b1c | 6024 | char *newname, *p, leading_char; |
5a580b3a AM |
6025 | struct elf_link_hash_entry *newh; |
6026 | ||
93252b1c | 6027 | leading_char = bfd_get_symbol_leading_char (output_bfd); |
ae5a3597 | 6028 | name = d->pattern; |
93252b1c | 6029 | namelen = strlen (name) + (leading_char != '\0'); |
5a580b3a AM |
6030 | verstr = t->name; |
6031 | verlen = strlen (verstr); | |
6032 | newlen = namelen + verlen + 3; | |
6033 | ||
a50b1753 | 6034 | newname = (char *) bfd_malloc (newlen); |
5a580b3a AM |
6035 | if (newname == NULL) |
6036 | return FALSE; | |
93252b1c MF |
6037 | newname[0] = leading_char; |
6038 | memcpy (newname + (leading_char != '\0'), name, namelen); | |
5a580b3a AM |
6039 | |
6040 | /* Check the hidden versioned definition. */ | |
6041 | p = newname + namelen; | |
6042 | *p++ = ELF_VER_CHR; | |
6043 | memcpy (p, verstr, verlen + 1); | |
6044 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6045 | newname, FALSE, FALSE, | |
6046 | FALSE); | |
6047 | if (newh == NULL | |
6048 | || (newh->root.type != bfd_link_hash_defined | |
6049 | && newh->root.type != bfd_link_hash_defweak)) | |
6050 | { | |
6051 | /* Check the default versioned definition. */ | |
6052 | *p++ = ELF_VER_CHR; | |
6053 | memcpy (p, verstr, verlen + 1); | |
6054 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6055 | newname, FALSE, FALSE, | |
6056 | FALSE); | |
6057 | } | |
6058 | free (newname); | |
6059 | ||
6060 | /* Mark this version if there is a definition and it is | |
6061 | not defined in a shared object. */ | |
6062 | if (newh != NULL | |
f5385ebf | 6063 | && !newh->def_dynamic |
5a580b3a AM |
6064 | && (newh->root.type == bfd_link_hash_defined |
6065 | || newh->root.type == bfd_link_hash_defweak)) | |
6066 | d->symver = 1; | |
6067 | } | |
6068 | ||
6069 | /* Attach all the symbols to their version information. */ | |
5a580b3a | 6070 | asvinfo.info = info; |
5a580b3a AM |
6071 | asvinfo.failed = FALSE; |
6072 | ||
6073 | elf_link_hash_traverse (elf_hash_table (info), | |
6074 | _bfd_elf_link_assign_sym_version, | |
6075 | &asvinfo); | |
6076 | if (asvinfo.failed) | |
6077 | return FALSE; | |
6078 | ||
6079 | if (!info->allow_undefined_version) | |
6080 | { | |
6081 | /* Check if all global versions have a definition. */ | |
6082 | all_defined = TRUE; | |
fd91d419 | 6083 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6084 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6085 | if (d->literal && !d->symver && !d->script) |
5a580b3a AM |
6086 | { |
6087 | (*_bfd_error_handler) | |
6088 | (_("%s: undefined version: %s"), | |
6089 | d->pattern, t->name); | |
6090 | all_defined = FALSE; | |
6091 | } | |
6092 | ||
6093 | if (!all_defined) | |
6094 | { | |
6095 | bfd_set_error (bfd_error_bad_value); | |
6096 | return FALSE; | |
6097 | } | |
6098 | } | |
6099 | ||
6100 | /* Find all symbols which were defined in a dynamic object and make | |
6101 | the backend pick a reasonable value for them. */ | |
6102 | elf_link_hash_traverse (elf_hash_table (info), | |
6103 | _bfd_elf_adjust_dynamic_symbol, | |
6104 | &eif); | |
6105 | if (eif.failed) | |
6106 | return FALSE; | |
6107 | ||
6108 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 6109 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
6110 | now so that we know the final size of the .dynamic section. */ |
6111 | ||
6112 | /* If there are initialization and/or finalization functions to | |
6113 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
6114 | h = (info->init_function | |
6115 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6116 | info->init_function, FALSE, | |
6117 | FALSE, FALSE) | |
6118 | : NULL); | |
6119 | if (h != NULL | |
f5385ebf AM |
6120 | && (h->ref_regular |
6121 | || h->def_regular)) | |
5a580b3a AM |
6122 | { |
6123 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
6124 | return FALSE; | |
6125 | } | |
6126 | h = (info->fini_function | |
6127 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6128 | info->fini_function, FALSE, | |
6129 | FALSE, FALSE) | |
6130 | : NULL); | |
6131 | if (h != NULL | |
f5385ebf AM |
6132 | && (h->ref_regular |
6133 | || h->def_regular)) | |
5a580b3a AM |
6134 | { |
6135 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
6136 | return FALSE; | |
6137 | } | |
6138 | ||
046183de AM |
6139 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); |
6140 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
6141 | { |
6142 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
0e1862bb | 6143 | if (! bfd_link_executable (info)) |
5a580b3a AM |
6144 | { |
6145 | bfd *sub; | |
6146 | asection *o; | |
6147 | ||
6148 | for (sub = info->input_bfds; sub != NULL; | |
c72f2fb2 | 6149 | sub = sub->link.next) |
3fcd97f1 JJ |
6150 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour) |
6151 | for (o = sub->sections; o != NULL; o = o->next) | |
6152 | if (elf_section_data (o)->this_hdr.sh_type | |
6153 | == SHT_PREINIT_ARRAY) | |
6154 | { | |
6155 | (*_bfd_error_handler) | |
6156 | (_("%B: .preinit_array section is not allowed in DSO"), | |
6157 | sub); | |
6158 | break; | |
6159 | } | |
5a580b3a AM |
6160 | |
6161 | bfd_set_error (bfd_error_nonrepresentable_section); | |
6162 | return FALSE; | |
6163 | } | |
6164 | ||
6165 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
6166 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
6167 | return FALSE; | |
6168 | } | |
046183de AM |
6169 | s = bfd_get_section_by_name (output_bfd, ".init_array"); |
6170 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
6171 | { |
6172 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
6173 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
6174 | return FALSE; | |
6175 | } | |
046183de AM |
6176 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); |
6177 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
6178 | { |
6179 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
6180 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
6181 | return FALSE; | |
6182 | } | |
6183 | ||
3d4d4302 | 6184 | dynstr = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
6185 | /* If .dynstr is excluded from the link, we don't want any of |
6186 | these tags. Strictly, we should be checking each section | |
6187 | individually; This quick check covers for the case where | |
6188 | someone does a /DISCARD/ : { *(*) }. */ | |
6189 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
6190 | { | |
6191 | bfd_size_type strsize; | |
6192 | ||
6193 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
fdc90cb4 JJ |
6194 | if ((info->emit_hash |
6195 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
6196 | || (info->emit_gnu_hash | |
6197 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
5a580b3a AM |
6198 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) |
6199 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
6200 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
6201 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
6202 | bed->s->sizeof_sym)) | |
6203 | return FALSE; | |
6204 | } | |
6205 | } | |
6206 | ||
de231f20 CM |
6207 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) |
6208 | return FALSE; | |
6209 | ||
5a580b3a AM |
6210 | /* The backend must work out the sizes of all the other dynamic |
6211 | sections. */ | |
9a2a56cc AM |
6212 | if (dynobj != NULL |
6213 | && bed->elf_backend_size_dynamic_sections != NULL | |
5a580b3a AM |
6214 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) |
6215 | return FALSE; | |
6216 | ||
9a2a56cc | 6217 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a | 6218 | { |
554220db | 6219 | unsigned long section_sym_count; |
fd91d419 | 6220 | struct bfd_elf_version_tree *verdefs; |
5a580b3a | 6221 | asection *s; |
5a580b3a AM |
6222 | |
6223 | /* Set up the version definition section. */ | |
3d4d4302 | 6224 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
6225 | BFD_ASSERT (s != NULL); |
6226 | ||
6227 | /* We may have created additional version definitions if we are | |
6228 | just linking a regular application. */ | |
fd91d419 | 6229 | verdefs = info->version_info; |
5a580b3a AM |
6230 | |
6231 | /* Skip anonymous version tag. */ | |
6232 | if (verdefs != NULL && verdefs->vernum == 0) | |
6233 | verdefs = verdefs->next; | |
6234 | ||
3e3b46e5 | 6235 | if (verdefs == NULL && !info->create_default_symver) |
8423293d | 6236 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
6237 | else |
6238 | { | |
6239 | unsigned int cdefs; | |
6240 | bfd_size_type size; | |
6241 | struct bfd_elf_version_tree *t; | |
6242 | bfd_byte *p; | |
6243 | Elf_Internal_Verdef def; | |
6244 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
6245 | struct bfd_link_hash_entry *bh; |
6246 | struct elf_link_hash_entry *h; | |
6247 | const char *name; | |
5a580b3a AM |
6248 | |
6249 | cdefs = 0; | |
6250 | size = 0; | |
6251 | ||
6252 | /* Make space for the base version. */ | |
6253 | size += sizeof (Elf_External_Verdef); | |
6254 | size += sizeof (Elf_External_Verdaux); | |
6255 | ++cdefs; | |
6256 | ||
3e3b46e5 PB |
6257 | /* Make space for the default version. */ |
6258 | if (info->create_default_symver) | |
6259 | { | |
6260 | size += sizeof (Elf_External_Verdef); | |
6261 | ++cdefs; | |
6262 | } | |
6263 | ||
5a580b3a AM |
6264 | for (t = verdefs; t != NULL; t = t->next) |
6265 | { | |
6266 | struct bfd_elf_version_deps *n; | |
6267 | ||
a6cc6b3b RO |
6268 | /* Don't emit base version twice. */ |
6269 | if (t->vernum == 0) | |
6270 | continue; | |
6271 | ||
5a580b3a AM |
6272 | size += sizeof (Elf_External_Verdef); |
6273 | size += sizeof (Elf_External_Verdaux); | |
6274 | ++cdefs; | |
6275 | ||
6276 | for (n = t->deps; n != NULL; n = n->next) | |
6277 | size += sizeof (Elf_External_Verdaux); | |
6278 | } | |
6279 | ||
eea6121a | 6280 | s->size = size; |
a50b1753 | 6281 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
eea6121a | 6282 | if (s->contents == NULL && s->size != 0) |
5a580b3a AM |
6283 | return FALSE; |
6284 | ||
6285 | /* Fill in the version definition section. */ | |
6286 | ||
6287 | p = s->contents; | |
6288 | ||
6289 | def.vd_version = VER_DEF_CURRENT; | |
6290 | def.vd_flags = VER_FLG_BASE; | |
6291 | def.vd_ndx = 1; | |
6292 | def.vd_cnt = 1; | |
3e3b46e5 PB |
6293 | if (info->create_default_symver) |
6294 | { | |
6295 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
6296 | def.vd_next = sizeof (Elf_External_Verdef); | |
6297 | } | |
6298 | else | |
6299 | { | |
6300 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6301 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6302 | + sizeof (Elf_External_Verdaux)); | |
6303 | } | |
5a580b3a | 6304 | |
ef53be89 | 6305 | if (soname_indx != (size_t) -1) |
5a580b3a AM |
6306 | { |
6307 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6308 | soname_indx); | |
6309 | def.vd_hash = bfd_elf_hash (soname); | |
6310 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 6311 | name = soname; |
5a580b3a AM |
6312 | } |
6313 | else | |
6314 | { | |
ef53be89 | 6315 | size_t indx; |
5a580b3a | 6316 | |
06084812 | 6317 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
6318 | def.vd_hash = bfd_elf_hash (name); |
6319 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6320 | name, FALSE); | |
ef53be89 | 6321 | if (indx == (size_t) -1) |
5a580b3a AM |
6322 | return FALSE; |
6323 | defaux.vda_name = indx; | |
6324 | } | |
6325 | defaux.vda_next = 0; | |
6326 | ||
6327 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6328 | (Elf_External_Verdef *) p); | |
6329 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
6330 | if (info->create_default_symver) |
6331 | { | |
6332 | /* Add a symbol representing this version. */ | |
6333 | bh = NULL; | |
6334 | if (! (_bfd_generic_link_add_one_symbol | |
6335 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6336 | 0, NULL, FALSE, | |
6337 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6338 | return FALSE; | |
6339 | h = (struct elf_link_hash_entry *) bh; | |
6340 | h->non_elf = 0; | |
6341 | h->def_regular = 1; | |
6342 | h->type = STT_OBJECT; | |
6343 | h->verinfo.vertree = NULL; | |
6344 | ||
6345 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
6346 | return FALSE; | |
6347 | ||
6348 | /* Create a duplicate of the base version with the same | |
6349 | aux block, but different flags. */ | |
6350 | def.vd_flags = 0; | |
6351 | def.vd_ndx = 2; | |
6352 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6353 | if (verdefs) | |
6354 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6355 | + sizeof (Elf_External_Verdaux)); | |
6356 | else | |
6357 | def.vd_next = 0; | |
6358 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6359 | (Elf_External_Verdef *) p); | |
6360 | p += sizeof (Elf_External_Verdef); | |
6361 | } | |
5a580b3a AM |
6362 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
6363 | (Elf_External_Verdaux *) p); | |
6364 | p += sizeof (Elf_External_Verdaux); | |
6365 | ||
6366 | for (t = verdefs; t != NULL; t = t->next) | |
6367 | { | |
6368 | unsigned int cdeps; | |
6369 | struct bfd_elf_version_deps *n; | |
5a580b3a | 6370 | |
a6cc6b3b RO |
6371 | /* Don't emit the base version twice. */ |
6372 | if (t->vernum == 0) | |
6373 | continue; | |
6374 | ||
5a580b3a AM |
6375 | cdeps = 0; |
6376 | for (n = t->deps; n != NULL; n = n->next) | |
6377 | ++cdeps; | |
6378 | ||
6379 | /* Add a symbol representing this version. */ | |
6380 | bh = NULL; | |
6381 | if (! (_bfd_generic_link_add_one_symbol | |
6382 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6383 | 0, NULL, FALSE, | |
6384 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6385 | return FALSE; | |
6386 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
6387 | h->non_elf = 0; |
6388 | h->def_regular = 1; | |
5a580b3a AM |
6389 | h->type = STT_OBJECT; |
6390 | h->verinfo.vertree = t; | |
6391 | ||
c152c796 | 6392 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
6393 | return FALSE; |
6394 | ||
6395 | def.vd_version = VER_DEF_CURRENT; | |
6396 | def.vd_flags = 0; | |
6397 | if (t->globals.list == NULL | |
6398 | && t->locals.list == NULL | |
6399 | && ! t->used) | |
6400 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 6401 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
6402 | def.vd_cnt = cdeps + 1; |
6403 | def.vd_hash = bfd_elf_hash (t->name); | |
6404 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6405 | def.vd_next = 0; | |
a6cc6b3b RO |
6406 | |
6407 | /* If a basever node is next, it *must* be the last node in | |
6408 | the chain, otherwise Verdef construction breaks. */ | |
6409 | if (t->next != NULL && t->next->vernum == 0) | |
6410 | BFD_ASSERT (t->next->next == NULL); | |
6411 | ||
6412 | if (t->next != NULL && t->next->vernum != 0) | |
5a580b3a AM |
6413 | def.vd_next = (sizeof (Elf_External_Verdef) |
6414 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
6415 | ||
6416 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6417 | (Elf_External_Verdef *) p); | |
6418 | p += sizeof (Elf_External_Verdef); | |
6419 | ||
6420 | defaux.vda_name = h->dynstr_index; | |
6421 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6422 | h->dynstr_index); | |
6423 | defaux.vda_next = 0; | |
6424 | if (t->deps != NULL) | |
6425 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6426 | t->name_indx = defaux.vda_name; | |
6427 | ||
6428 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6429 | (Elf_External_Verdaux *) p); | |
6430 | p += sizeof (Elf_External_Verdaux); | |
6431 | ||
6432 | for (n = t->deps; n != NULL; n = n->next) | |
6433 | { | |
6434 | if (n->version_needed == NULL) | |
6435 | { | |
6436 | /* This can happen if there was an error in the | |
6437 | version script. */ | |
6438 | defaux.vda_name = 0; | |
6439 | } | |
6440 | else | |
6441 | { | |
6442 | defaux.vda_name = n->version_needed->name_indx; | |
6443 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6444 | defaux.vda_name); | |
6445 | } | |
6446 | if (n->next == NULL) | |
6447 | defaux.vda_next = 0; | |
6448 | else | |
6449 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6450 | ||
6451 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6452 | (Elf_External_Verdaux *) p); | |
6453 | p += sizeof (Elf_External_Verdaux); | |
6454 | } | |
6455 | } | |
6456 | ||
6457 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
6458 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
6459 | return FALSE; | |
6460 | ||
6461 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
6462 | } | |
6463 | ||
6464 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
6465 | { | |
6466 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
6467 | return FALSE; | |
6468 | } | |
6469 | else if (info->flags & DF_BIND_NOW) | |
6470 | { | |
6471 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
6472 | return FALSE; | |
6473 | } | |
6474 | ||
6475 | if (info->flags_1) | |
6476 | { | |
0e1862bb | 6477 | if (bfd_link_executable (info)) |
5a580b3a AM |
6478 | info->flags_1 &= ~ (DF_1_INITFIRST |
6479 | | DF_1_NODELETE | |
6480 | | DF_1_NOOPEN); | |
6481 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
6482 | return FALSE; | |
6483 | } | |
6484 | ||
6485 | /* Work out the size of the version reference section. */ | |
6486 | ||
3d4d4302 | 6487 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
6488 | BFD_ASSERT (s != NULL); |
6489 | { | |
6490 | struct elf_find_verdep_info sinfo; | |
6491 | ||
5a580b3a AM |
6492 | sinfo.info = info; |
6493 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
6494 | if (sinfo.vers == 0) | |
6495 | sinfo.vers = 1; | |
6496 | sinfo.failed = FALSE; | |
6497 | ||
6498 | elf_link_hash_traverse (elf_hash_table (info), | |
6499 | _bfd_elf_link_find_version_dependencies, | |
6500 | &sinfo); | |
14b1c01e AM |
6501 | if (sinfo.failed) |
6502 | return FALSE; | |
5a580b3a AM |
6503 | |
6504 | if (elf_tdata (output_bfd)->verref == NULL) | |
8423293d | 6505 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
6506 | else |
6507 | { | |
6508 | Elf_Internal_Verneed *t; | |
6509 | unsigned int size; | |
6510 | unsigned int crefs; | |
6511 | bfd_byte *p; | |
6512 | ||
a6cc6b3b | 6513 | /* Build the version dependency section. */ |
5a580b3a AM |
6514 | size = 0; |
6515 | crefs = 0; | |
6516 | for (t = elf_tdata (output_bfd)->verref; | |
6517 | t != NULL; | |
6518 | t = t->vn_nextref) | |
6519 | { | |
6520 | Elf_Internal_Vernaux *a; | |
6521 | ||
6522 | size += sizeof (Elf_External_Verneed); | |
6523 | ++crefs; | |
6524 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6525 | size += sizeof (Elf_External_Vernaux); | |
6526 | } | |
6527 | ||
eea6121a | 6528 | s->size = size; |
a50b1753 | 6529 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
5a580b3a AM |
6530 | if (s->contents == NULL) |
6531 | return FALSE; | |
6532 | ||
6533 | p = s->contents; | |
6534 | for (t = elf_tdata (output_bfd)->verref; | |
6535 | t != NULL; | |
6536 | t = t->vn_nextref) | |
6537 | { | |
6538 | unsigned int caux; | |
6539 | Elf_Internal_Vernaux *a; | |
ef53be89 | 6540 | size_t indx; |
5a580b3a AM |
6541 | |
6542 | caux = 0; | |
6543 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6544 | ++caux; | |
6545 | ||
6546 | t->vn_version = VER_NEED_CURRENT; | |
6547 | t->vn_cnt = caux; | |
6548 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6549 | elf_dt_name (t->vn_bfd) != NULL | |
6550 | ? elf_dt_name (t->vn_bfd) | |
06084812 | 6551 | : lbasename (t->vn_bfd->filename), |
5a580b3a | 6552 | FALSE); |
ef53be89 | 6553 | if (indx == (size_t) -1) |
5a580b3a AM |
6554 | return FALSE; |
6555 | t->vn_file = indx; | |
6556 | t->vn_aux = sizeof (Elf_External_Verneed); | |
6557 | if (t->vn_nextref == NULL) | |
6558 | t->vn_next = 0; | |
6559 | else | |
6560 | t->vn_next = (sizeof (Elf_External_Verneed) | |
6561 | + caux * sizeof (Elf_External_Vernaux)); | |
6562 | ||
6563 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
6564 | (Elf_External_Verneed *) p); | |
6565 | p += sizeof (Elf_External_Verneed); | |
6566 | ||
6567 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6568 | { | |
6569 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6570 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6571 | a->vna_nodename, FALSE); | |
ef53be89 | 6572 | if (indx == (size_t) -1) |
5a580b3a AM |
6573 | return FALSE; |
6574 | a->vna_name = indx; | |
6575 | if (a->vna_nextptr == NULL) | |
6576 | a->vna_next = 0; | |
6577 | else | |
6578 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6579 | ||
6580 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6581 | (Elf_External_Vernaux *) p); | |
6582 | p += sizeof (Elf_External_Vernaux); | |
6583 | } | |
6584 | } | |
6585 | ||
6586 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
6587 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
6588 | return FALSE; | |
6589 | ||
6590 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6591 | } | |
6592 | } | |
6593 | ||
8423293d AM |
6594 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
6595 | && elf_tdata (output_bfd)->cverdefs == 0) | |
6596 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
6597 | §ion_sym_count) == 0) | |
6598 | { | |
3d4d4302 | 6599 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
8423293d AM |
6600 | s->flags |= SEC_EXCLUDE; |
6601 | } | |
6602 | } | |
6603 | return TRUE; | |
6604 | } | |
6605 | ||
74541ad4 AM |
6606 | /* Find the first non-excluded output section. We'll use its |
6607 | section symbol for some emitted relocs. */ | |
6608 | void | |
6609 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
6610 | { | |
6611 | asection *s; | |
6612 | ||
6613 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6614 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
6615 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6616 | { | |
6617 | elf_hash_table (info)->text_index_section = s; | |
6618 | break; | |
6619 | } | |
6620 | } | |
6621 | ||
6622 | /* Find two non-excluded output sections, one for code, one for data. | |
6623 | We'll use their section symbols for some emitted relocs. */ | |
6624 | void | |
6625 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
6626 | { | |
6627 | asection *s; | |
6628 | ||
266b05cf DJ |
6629 | /* Data first, since setting text_index_section changes |
6630 | _bfd_elf_link_omit_section_dynsym. */ | |
74541ad4 | 6631 | for (s = output_bfd->sections; s != NULL; s = s->next) |
266b05cf | 6632 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) |
74541ad4 AM |
6633 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6634 | { | |
266b05cf | 6635 | elf_hash_table (info)->data_index_section = s; |
74541ad4 AM |
6636 | break; |
6637 | } | |
6638 | ||
6639 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
266b05cf DJ |
6640 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) |
6641 | == (SEC_ALLOC | SEC_READONLY)) | |
74541ad4 AM |
6642 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6643 | { | |
266b05cf | 6644 | elf_hash_table (info)->text_index_section = s; |
74541ad4 AM |
6645 | break; |
6646 | } | |
6647 | ||
6648 | if (elf_hash_table (info)->text_index_section == NULL) | |
6649 | elf_hash_table (info)->text_index_section | |
6650 | = elf_hash_table (info)->data_index_section; | |
6651 | } | |
6652 | ||
8423293d AM |
6653 | bfd_boolean |
6654 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
6655 | { | |
74541ad4 AM |
6656 | const struct elf_backend_data *bed; |
6657 | ||
8423293d AM |
6658 | if (!is_elf_hash_table (info->hash)) |
6659 | return TRUE; | |
6660 | ||
74541ad4 AM |
6661 | bed = get_elf_backend_data (output_bfd); |
6662 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
6663 | ||
8423293d AM |
6664 | if (elf_hash_table (info)->dynamic_sections_created) |
6665 | { | |
6666 | bfd *dynobj; | |
8423293d AM |
6667 | asection *s; |
6668 | bfd_size_type dynsymcount; | |
6669 | unsigned long section_sym_count; | |
8423293d AM |
6670 | unsigned int dtagcount; |
6671 | ||
6672 | dynobj = elf_hash_table (info)->dynobj; | |
6673 | ||
5a580b3a AM |
6674 | /* Assign dynsym indicies. In a shared library we generate a |
6675 | section symbol for each output section, which come first. | |
6676 | Next come all of the back-end allocated local dynamic syms, | |
6677 | followed by the rest of the global symbols. */ | |
6678 | ||
554220db AM |
6679 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, |
6680 | §ion_sym_count); | |
5a580b3a AM |
6681 | |
6682 | /* Work out the size of the symbol version section. */ | |
3d4d4302 | 6683 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
5a580b3a | 6684 | BFD_ASSERT (s != NULL); |
d5486c43 | 6685 | if ((s->flags & SEC_EXCLUDE) == 0) |
5a580b3a | 6686 | { |
eea6121a | 6687 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
a50b1753 | 6688 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
5a580b3a AM |
6689 | if (s->contents == NULL) |
6690 | return FALSE; | |
6691 | ||
6692 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
6693 | return FALSE; | |
6694 | } | |
6695 | ||
6696 | /* Set the size of the .dynsym and .hash sections. We counted | |
6697 | the number of dynamic symbols in elf_link_add_object_symbols. | |
6698 | We will build the contents of .dynsym and .hash when we build | |
6699 | the final symbol table, because until then we do not know the | |
6700 | correct value to give the symbols. We built the .dynstr | |
6701 | section as we went along in elf_link_add_object_symbols. */ | |
cae1fbbb | 6702 | s = elf_hash_table (info)->dynsym; |
5a580b3a | 6703 | BFD_ASSERT (s != NULL); |
eea6121a | 6704 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a | 6705 | |
d5486c43 L |
6706 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
6707 | if (s->contents == NULL) | |
6708 | return FALSE; | |
5a580b3a | 6709 | |
d5486c43 L |
6710 | /* The first entry in .dynsym is a dummy symbol. Clear all the |
6711 | section syms, in case we don't output them all. */ | |
6712 | ++section_sym_count; | |
6713 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a | 6714 | |
fdc90cb4 JJ |
6715 | elf_hash_table (info)->bucketcount = 0; |
6716 | ||
5a580b3a AM |
6717 | /* Compute the size of the hashing table. As a side effect this |
6718 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
6719 | if (info->emit_hash) |
6720 | { | |
6721 | unsigned long int *hashcodes; | |
14b1c01e | 6722 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
6723 | bfd_size_type amt; |
6724 | unsigned long int nsyms; | |
6725 | size_t bucketcount; | |
6726 | size_t hash_entry_size; | |
6727 | ||
6728 | /* Compute the hash values for all exported symbols. At the same | |
6729 | time store the values in an array so that we could use them for | |
6730 | optimizations. */ | |
6731 | amt = dynsymcount * sizeof (unsigned long int); | |
a50b1753 | 6732 | hashcodes = (unsigned long int *) bfd_malloc (amt); |
fdc90cb4 JJ |
6733 | if (hashcodes == NULL) |
6734 | return FALSE; | |
14b1c01e AM |
6735 | hashinf.hashcodes = hashcodes; |
6736 | hashinf.error = FALSE; | |
5a580b3a | 6737 | |
fdc90cb4 JJ |
6738 | /* Put all hash values in HASHCODES. */ |
6739 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
6740 | elf_collect_hash_codes, &hashinf); |
6741 | if (hashinf.error) | |
4dd07732 AM |
6742 | { |
6743 | free (hashcodes); | |
6744 | return FALSE; | |
6745 | } | |
5a580b3a | 6746 | |
14b1c01e | 6747 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
6748 | bucketcount |
6749 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
6750 | free (hashcodes); | |
6751 | ||
6752 | if (bucketcount == 0) | |
6753 | return FALSE; | |
5a580b3a | 6754 | |
fdc90cb4 JJ |
6755 | elf_hash_table (info)->bucketcount = bucketcount; |
6756 | ||
3d4d4302 | 6757 | s = bfd_get_linker_section (dynobj, ".hash"); |
fdc90cb4 JJ |
6758 | BFD_ASSERT (s != NULL); |
6759 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
6760 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
a50b1753 | 6761 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6762 | if (s->contents == NULL) |
6763 | return FALSE; | |
6764 | ||
6765 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
6766 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
6767 | s->contents + hash_entry_size); | |
6768 | } | |
6769 | ||
6770 | if (info->emit_gnu_hash) | |
6771 | { | |
6772 | size_t i, cnt; | |
6773 | unsigned char *contents; | |
6774 | struct collect_gnu_hash_codes cinfo; | |
6775 | bfd_size_type amt; | |
6776 | size_t bucketcount; | |
6777 | ||
6778 | memset (&cinfo, 0, sizeof (cinfo)); | |
6779 | ||
6780 | /* Compute the hash values for all exported symbols. At the same | |
6781 | time store the values in an array so that we could use them for | |
6782 | optimizations. */ | |
6783 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
a50b1753 | 6784 | cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); |
fdc90cb4 JJ |
6785 | if (cinfo.hashcodes == NULL) |
6786 | return FALSE; | |
6787 | ||
6788 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
6789 | cinfo.min_dynindx = -1; | |
6790 | cinfo.output_bfd = output_bfd; | |
6791 | cinfo.bed = bed; | |
6792 | ||
6793 | /* Put all hash values in HASHCODES. */ | |
6794 | elf_link_hash_traverse (elf_hash_table (info), | |
6795 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e | 6796 | if (cinfo.error) |
4dd07732 AM |
6797 | { |
6798 | free (cinfo.hashcodes); | |
6799 | return FALSE; | |
6800 | } | |
fdc90cb4 JJ |
6801 | |
6802 | bucketcount | |
6803 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
6804 | ||
6805 | if (bucketcount == 0) | |
6806 | { | |
6807 | free (cinfo.hashcodes); | |
6808 | return FALSE; | |
6809 | } | |
6810 | ||
3d4d4302 | 6811 | s = bfd_get_linker_section (dynobj, ".gnu.hash"); |
fdc90cb4 JJ |
6812 | BFD_ASSERT (s != NULL); |
6813 | ||
6814 | if (cinfo.nsyms == 0) | |
6815 | { | |
6816 | /* Empty .gnu.hash section is special. */ | |
6817 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
6818 | free (cinfo.hashcodes); | |
6819 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
a50b1753 | 6820 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6821 | if (contents == NULL) |
6822 | return FALSE; | |
6823 | s->contents = contents; | |
6824 | /* 1 empty bucket. */ | |
6825 | bfd_put_32 (output_bfd, 1, contents); | |
6826 | /* SYMIDX above the special symbol 0. */ | |
6827 | bfd_put_32 (output_bfd, 1, contents + 4); | |
6828 | /* Just one word for bitmask. */ | |
6829 | bfd_put_32 (output_bfd, 1, contents + 8); | |
6830 | /* Only hash fn bloom filter. */ | |
6831 | bfd_put_32 (output_bfd, 0, contents + 12); | |
6832 | /* No hashes are valid - empty bitmask. */ | |
6833 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
6834 | /* No hashes in the only bucket. */ | |
6835 | bfd_put_32 (output_bfd, 0, | |
6836 | contents + 16 + bed->s->arch_size / 8); | |
6837 | } | |
6838 | else | |
6839 | { | |
9e6619e2 | 6840 | unsigned long int maskwords, maskbitslog2, x; |
0b33793d | 6841 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 | 6842 | |
9e6619e2 AM |
6843 | x = cinfo.nsyms; |
6844 | maskbitslog2 = 1; | |
6845 | while ((x >>= 1) != 0) | |
6846 | ++maskbitslog2; | |
fdc90cb4 JJ |
6847 | if (maskbitslog2 < 3) |
6848 | maskbitslog2 = 5; | |
6849 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
6850 | maskbitslog2 = maskbitslog2 + 3; | |
6851 | else | |
6852 | maskbitslog2 = maskbitslog2 + 2; | |
6853 | if (bed->s->arch_size == 64) | |
6854 | { | |
6855 | if (maskbitslog2 == 5) | |
6856 | maskbitslog2 = 6; | |
6857 | cinfo.shift1 = 6; | |
6858 | } | |
6859 | else | |
6860 | cinfo.shift1 = 5; | |
6861 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 6862 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
6863 | cinfo.maskbits = 1 << maskbitslog2; |
6864 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
6865 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
6866 | amt += maskwords * sizeof (bfd_vma); | |
a50b1753 | 6867 | cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); |
fdc90cb4 JJ |
6868 | if (cinfo.bitmask == NULL) |
6869 | { | |
6870 | free (cinfo.hashcodes); | |
6871 | return FALSE; | |
6872 | } | |
6873 | ||
a50b1753 | 6874 | cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); |
fdc90cb4 JJ |
6875 | cinfo.indx = cinfo.counts + bucketcount; |
6876 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
6877 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
6878 | ||
6879 | /* Determine how often each hash bucket is used. */ | |
6880 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
6881 | for (i = 0; i < cinfo.nsyms; ++i) | |
6882 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
6883 | ||
6884 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
6885 | if (cinfo.counts[i] != 0) | |
6886 | { | |
6887 | cinfo.indx[i] = cnt; | |
6888 | cnt += cinfo.counts[i]; | |
6889 | } | |
6890 | BFD_ASSERT (cnt == dynsymcount); | |
6891 | cinfo.bucketcount = bucketcount; | |
6892 | cinfo.local_indx = cinfo.min_dynindx; | |
6893 | ||
6894 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
6895 | s->size += cinfo.maskbits / 8; | |
a50b1753 | 6896 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6897 | if (contents == NULL) |
6898 | { | |
6899 | free (cinfo.bitmask); | |
6900 | free (cinfo.hashcodes); | |
6901 | return FALSE; | |
6902 | } | |
6903 | ||
6904 | s->contents = contents; | |
6905 | bfd_put_32 (output_bfd, bucketcount, contents); | |
6906 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
6907 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
6908 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
6909 | contents += 16 + cinfo.maskbits / 8; | |
6910 | ||
6911 | for (i = 0; i < bucketcount; ++i) | |
6912 | { | |
6913 | if (cinfo.counts[i] == 0) | |
6914 | bfd_put_32 (output_bfd, 0, contents); | |
6915 | else | |
6916 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
6917 | contents += 4; | |
6918 | } | |
6919 | ||
6920 | cinfo.contents = contents; | |
6921 | ||
6922 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
6923 | elf_link_hash_traverse (elf_hash_table (info), | |
6924 | elf_renumber_gnu_hash_syms, &cinfo); | |
6925 | ||
6926 | contents = s->contents + 16; | |
6927 | for (i = 0; i < maskwords; ++i) | |
6928 | { | |
6929 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
6930 | contents); | |
6931 | contents += bed->s->arch_size / 8; | |
6932 | } | |
6933 | ||
6934 | free (cinfo.bitmask); | |
6935 | free (cinfo.hashcodes); | |
6936 | } | |
6937 | } | |
5a580b3a | 6938 | |
3d4d4302 | 6939 | s = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
6940 | BFD_ASSERT (s != NULL); |
6941 | ||
4ad4eba5 | 6942 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 6943 | |
eea6121a | 6944 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
6945 | |
6946 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
6947 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
6948 | return FALSE; | |
6949 | } | |
6950 | ||
6951 | return TRUE; | |
6952 | } | |
4d269e42 | 6953 | \f |
4d269e42 AM |
6954 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ |
6955 | ||
6956 | static void | |
6957 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
6958 | asection *sec) | |
6959 | { | |
dbaa2011 AM |
6960 | BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); |
6961 | sec->sec_info_type = SEC_INFO_TYPE_NONE; | |
4d269e42 AM |
6962 | } |
6963 | ||
6964 | /* Finish SHF_MERGE section merging. */ | |
6965 | ||
6966 | bfd_boolean | |
630993ec | 6967 | _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) |
4d269e42 AM |
6968 | { |
6969 | bfd *ibfd; | |
6970 | asection *sec; | |
6971 | ||
6972 | if (!is_elf_hash_table (info->hash)) | |
6973 | return FALSE; | |
6974 | ||
c72f2fb2 | 6975 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
630993ec AM |
6976 | if ((ibfd->flags & DYNAMIC) == 0 |
6977 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
017e6bce AM |
6978 | && (elf_elfheader (ibfd)->e_ident[EI_CLASS] |
6979 | == get_elf_backend_data (obfd)->s->elfclass)) | |
4d269e42 AM |
6980 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) |
6981 | if ((sec->flags & SEC_MERGE) != 0 | |
6982 | && !bfd_is_abs_section (sec->output_section)) | |
6983 | { | |
6984 | struct bfd_elf_section_data *secdata; | |
6985 | ||
6986 | secdata = elf_section_data (sec); | |
630993ec | 6987 | if (! _bfd_add_merge_section (obfd, |
4d269e42 AM |
6988 | &elf_hash_table (info)->merge_info, |
6989 | sec, &secdata->sec_info)) | |
6990 | return FALSE; | |
6991 | else if (secdata->sec_info) | |
dbaa2011 | 6992 | sec->sec_info_type = SEC_INFO_TYPE_MERGE; |
4d269e42 AM |
6993 | } |
6994 | ||
6995 | if (elf_hash_table (info)->merge_info != NULL) | |
630993ec | 6996 | _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, |
4d269e42 AM |
6997 | merge_sections_remove_hook); |
6998 | return TRUE; | |
6999 | } | |
7000 | ||
7001 | /* Create an entry in an ELF linker hash table. */ | |
7002 | ||
7003 | struct bfd_hash_entry * | |
7004 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
7005 | struct bfd_hash_table *table, | |
7006 | const char *string) | |
7007 | { | |
7008 | /* Allocate the structure if it has not already been allocated by a | |
7009 | subclass. */ | |
7010 | if (entry == NULL) | |
7011 | { | |
a50b1753 | 7012 | entry = (struct bfd_hash_entry *) |
ca4be51c | 7013 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); |
4d269e42 AM |
7014 | if (entry == NULL) |
7015 | return entry; | |
7016 | } | |
7017 | ||
7018 | /* Call the allocation method of the superclass. */ | |
7019 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
7020 | if (entry != NULL) | |
7021 | { | |
7022 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
7023 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
7024 | ||
7025 | /* Set local fields. */ | |
7026 | ret->indx = -1; | |
7027 | ret->dynindx = -1; | |
7028 | ret->got = htab->init_got_refcount; | |
7029 | ret->plt = htab->init_plt_refcount; | |
7030 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
7031 | - offsetof (struct elf_link_hash_entry, size))); | |
7032 | /* Assume that we have been called by a non-ELF symbol reader. | |
7033 | This flag is then reset by the code which reads an ELF input | |
7034 | file. This ensures that a symbol created by a non-ELF symbol | |
7035 | reader will have the flag set correctly. */ | |
7036 | ret->non_elf = 1; | |
7037 | } | |
7038 | ||
7039 | return entry; | |
7040 | } | |
7041 | ||
7042 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
7043 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
7044 | ||
7045 | void | |
7046 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
7047 | struct elf_link_hash_entry *dir, | |
7048 | struct elf_link_hash_entry *ind) | |
7049 | { | |
7050 | struct elf_link_hash_table *htab; | |
7051 | ||
7052 | /* Copy down any references that we may have already seen to the | |
6e33951e L |
7053 | symbol which just became indirect if DIR isn't a hidden versioned |
7054 | symbol. */ | |
4d269e42 | 7055 | |
422f1182 | 7056 | if (dir->versioned != versioned_hidden) |
6e33951e L |
7057 | { |
7058 | dir->ref_dynamic |= ind->ref_dynamic; | |
7059 | dir->ref_regular |= ind->ref_regular; | |
7060 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
7061 | dir->non_got_ref |= ind->non_got_ref; | |
7062 | dir->needs_plt |= ind->needs_plt; | |
7063 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
7064 | } | |
4d269e42 AM |
7065 | |
7066 | if (ind->root.type != bfd_link_hash_indirect) | |
7067 | return; | |
7068 | ||
7069 | /* Copy over the global and procedure linkage table refcount entries. | |
7070 | These may have been already set up by a check_relocs routine. */ | |
7071 | htab = elf_hash_table (info); | |
7072 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
7073 | { | |
7074 | if (dir->got.refcount < 0) | |
7075 | dir->got.refcount = 0; | |
7076 | dir->got.refcount += ind->got.refcount; | |
7077 | ind->got.refcount = htab->init_got_refcount.refcount; | |
7078 | } | |
7079 | ||
7080 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
7081 | { | |
7082 | if (dir->plt.refcount < 0) | |
7083 | dir->plt.refcount = 0; | |
7084 | dir->plt.refcount += ind->plt.refcount; | |
7085 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
7086 | } | |
7087 | ||
7088 | if (ind->dynindx != -1) | |
7089 | { | |
7090 | if (dir->dynindx != -1) | |
7091 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
7092 | dir->dynindx = ind->dynindx; | |
7093 | dir->dynstr_index = ind->dynstr_index; | |
7094 | ind->dynindx = -1; | |
7095 | ind->dynstr_index = 0; | |
7096 | } | |
7097 | } | |
7098 | ||
7099 | void | |
7100 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
7101 | struct elf_link_hash_entry *h, | |
7102 | bfd_boolean force_local) | |
7103 | { | |
3aa14d16 L |
7104 | /* STT_GNU_IFUNC symbol must go through PLT. */ |
7105 | if (h->type != STT_GNU_IFUNC) | |
7106 | { | |
7107 | h->plt = elf_hash_table (info)->init_plt_offset; | |
7108 | h->needs_plt = 0; | |
7109 | } | |
4d269e42 AM |
7110 | if (force_local) |
7111 | { | |
7112 | h->forced_local = 1; | |
7113 | if (h->dynindx != -1) | |
7114 | { | |
7115 | h->dynindx = -1; | |
7116 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, | |
7117 | h->dynstr_index); | |
7118 | } | |
7119 | } | |
7120 | } | |
7121 | ||
7bf52ea2 AM |
7122 | /* Initialize an ELF linker hash table. *TABLE has been zeroed by our |
7123 | caller. */ | |
4d269e42 AM |
7124 | |
7125 | bfd_boolean | |
7126 | _bfd_elf_link_hash_table_init | |
7127 | (struct elf_link_hash_table *table, | |
7128 | bfd *abfd, | |
7129 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
7130 | struct bfd_hash_table *, | |
7131 | const char *), | |
4dfe6ac6 NC |
7132 | unsigned int entsize, |
7133 | enum elf_target_id target_id) | |
4d269e42 AM |
7134 | { |
7135 | bfd_boolean ret; | |
7136 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; | |
7137 | ||
4d269e42 AM |
7138 | table->init_got_refcount.refcount = can_refcount - 1; |
7139 | table->init_plt_refcount.refcount = can_refcount - 1; | |
7140 | table->init_got_offset.offset = -(bfd_vma) 1; | |
7141 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
7142 | /* The first dynamic symbol is a dummy. */ | |
7143 | table->dynsymcount = 1; | |
7144 | ||
7145 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
4dfe6ac6 | 7146 | |
4d269e42 | 7147 | table->root.type = bfd_link_elf_hash_table; |
4dfe6ac6 | 7148 | table->hash_table_id = target_id; |
4d269e42 AM |
7149 | |
7150 | return ret; | |
7151 | } | |
7152 | ||
7153 | /* Create an ELF linker hash table. */ | |
7154 | ||
7155 | struct bfd_link_hash_table * | |
7156 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
7157 | { | |
7158 | struct elf_link_hash_table *ret; | |
7159 | bfd_size_type amt = sizeof (struct elf_link_hash_table); | |
7160 | ||
7bf52ea2 | 7161 | ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); |
4d269e42 AM |
7162 | if (ret == NULL) |
7163 | return NULL; | |
7164 | ||
7165 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
4dfe6ac6 NC |
7166 | sizeof (struct elf_link_hash_entry), |
7167 | GENERIC_ELF_DATA)) | |
4d269e42 AM |
7168 | { |
7169 | free (ret); | |
7170 | return NULL; | |
7171 | } | |
d495ab0d | 7172 | ret->root.hash_table_free = _bfd_elf_link_hash_table_free; |
4d269e42 AM |
7173 | |
7174 | return &ret->root; | |
7175 | } | |
7176 | ||
9f7c3e5e AM |
7177 | /* Destroy an ELF linker hash table. */ |
7178 | ||
7179 | void | |
d495ab0d | 7180 | _bfd_elf_link_hash_table_free (bfd *obfd) |
9f7c3e5e | 7181 | { |
d495ab0d AM |
7182 | struct elf_link_hash_table *htab; |
7183 | ||
7184 | htab = (struct elf_link_hash_table *) obfd->link.hash; | |
9f7c3e5e AM |
7185 | if (htab->dynstr != NULL) |
7186 | _bfd_elf_strtab_free (htab->dynstr); | |
7187 | _bfd_merge_sections_free (htab->merge_info); | |
d495ab0d | 7188 | _bfd_generic_link_hash_table_free (obfd); |
9f7c3e5e AM |
7189 | } |
7190 | ||
4d269e42 AM |
7191 | /* This is a hook for the ELF emulation code in the generic linker to |
7192 | tell the backend linker what file name to use for the DT_NEEDED | |
7193 | entry for a dynamic object. */ | |
7194 | ||
7195 | void | |
7196 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
7197 | { | |
7198 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7199 | && bfd_get_format (abfd) == bfd_object) | |
7200 | elf_dt_name (abfd) = name; | |
7201 | } | |
7202 | ||
7203 | int | |
7204 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
7205 | { | |
7206 | int lib_class; | |
7207 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7208 | && bfd_get_format (abfd) == bfd_object) | |
7209 | lib_class = elf_dyn_lib_class (abfd); | |
7210 | else | |
7211 | lib_class = 0; | |
7212 | return lib_class; | |
7213 | } | |
7214 | ||
7215 | void | |
7216 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
7217 | { | |
7218 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7219 | && bfd_get_format (abfd) == bfd_object) | |
7220 | elf_dyn_lib_class (abfd) = lib_class; | |
7221 | } | |
7222 | ||
7223 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
7224 | the linker ELF emulation code. */ | |
7225 | ||
7226 | struct bfd_link_needed_list * | |
7227 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7228 | struct bfd_link_info *info) | |
7229 | { | |
7230 | if (! is_elf_hash_table (info->hash)) | |
7231 | return NULL; | |
7232 | return elf_hash_table (info)->needed; | |
7233 | } | |
7234 | ||
7235 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
7236 | hook for the linker ELF emulation code. */ | |
7237 | ||
7238 | struct bfd_link_needed_list * | |
7239 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7240 | struct bfd_link_info *info) | |
7241 | { | |
7242 | if (! is_elf_hash_table (info->hash)) | |
7243 | return NULL; | |
7244 | return elf_hash_table (info)->runpath; | |
7245 | } | |
7246 | ||
7247 | /* Get the name actually used for a dynamic object for a link. This | |
7248 | is the SONAME entry if there is one. Otherwise, it is the string | |
7249 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
7250 | ||
7251 | const char * | |
7252 | bfd_elf_get_dt_soname (bfd *abfd) | |
7253 | { | |
7254 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7255 | && bfd_get_format (abfd) == bfd_object) | |
7256 | return elf_dt_name (abfd); | |
7257 | return NULL; | |
7258 | } | |
7259 | ||
7260 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
7261 | the ELF linker emulation code. */ | |
7262 | ||
7263 | bfd_boolean | |
7264 | bfd_elf_get_bfd_needed_list (bfd *abfd, | |
7265 | struct bfd_link_needed_list **pneeded) | |
7266 | { | |
7267 | asection *s; | |
7268 | bfd_byte *dynbuf = NULL; | |
cb33740c | 7269 | unsigned int elfsec; |
4d269e42 AM |
7270 | unsigned long shlink; |
7271 | bfd_byte *extdyn, *extdynend; | |
7272 | size_t extdynsize; | |
7273 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
7274 | ||
7275 | *pneeded = NULL; | |
7276 | ||
7277 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
7278 | || bfd_get_format (abfd) != bfd_object) | |
7279 | return TRUE; | |
7280 | ||
7281 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
7282 | if (s == NULL || s->size == 0) | |
7283 | return TRUE; | |
7284 | ||
7285 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
7286 | goto error_return; | |
7287 | ||
7288 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 7289 | if (elfsec == SHN_BAD) |
4d269e42 AM |
7290 | goto error_return; |
7291 | ||
7292 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 7293 | |
4d269e42 AM |
7294 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
7295 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
7296 | ||
7297 | extdyn = dynbuf; | |
7298 | extdynend = extdyn + s->size; | |
7299 | for (; extdyn < extdynend; extdyn += extdynsize) | |
7300 | { | |
7301 | Elf_Internal_Dyn dyn; | |
7302 | ||
7303 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
7304 | ||
7305 | if (dyn.d_tag == DT_NULL) | |
7306 | break; | |
7307 | ||
7308 | if (dyn.d_tag == DT_NEEDED) | |
7309 | { | |
7310 | const char *string; | |
7311 | struct bfd_link_needed_list *l; | |
7312 | unsigned int tagv = dyn.d_un.d_val; | |
7313 | bfd_size_type amt; | |
7314 | ||
7315 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
7316 | if (string == NULL) | |
7317 | goto error_return; | |
7318 | ||
7319 | amt = sizeof *l; | |
a50b1753 | 7320 | l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4d269e42 AM |
7321 | if (l == NULL) |
7322 | goto error_return; | |
7323 | ||
7324 | l->by = abfd; | |
7325 | l->name = string; | |
7326 | l->next = *pneeded; | |
7327 | *pneeded = l; | |
7328 | } | |
7329 | } | |
7330 | ||
7331 | free (dynbuf); | |
7332 | ||
7333 | return TRUE; | |
7334 | ||
7335 | error_return: | |
7336 | if (dynbuf != NULL) | |
7337 | free (dynbuf); | |
7338 | return FALSE; | |
7339 | } | |
7340 | ||
7341 | struct elf_symbuf_symbol | |
7342 | { | |
7343 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
7344 | unsigned char st_info; /* Type and binding attributes */ | |
7345 | unsigned char st_other; /* Visibilty, and target specific */ | |
7346 | }; | |
7347 | ||
7348 | struct elf_symbuf_head | |
7349 | { | |
7350 | struct elf_symbuf_symbol *ssym; | |
ef53be89 | 7351 | size_t count; |
4d269e42 AM |
7352 | unsigned int st_shndx; |
7353 | }; | |
7354 | ||
7355 | struct elf_symbol | |
7356 | { | |
7357 | union | |
7358 | { | |
7359 | Elf_Internal_Sym *isym; | |
7360 | struct elf_symbuf_symbol *ssym; | |
7361 | } u; | |
7362 | const char *name; | |
7363 | }; | |
7364 | ||
7365 | /* Sort references to symbols by ascending section number. */ | |
7366 | ||
7367 | static int | |
7368 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
7369 | { | |
7370 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
7371 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
7372 | ||
7373 | return s1->st_shndx - s2->st_shndx; | |
7374 | } | |
7375 | ||
7376 | static int | |
7377 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
7378 | { | |
7379 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
7380 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
7381 | return strcmp (s1->name, s2->name); | |
7382 | } | |
7383 | ||
7384 | static struct elf_symbuf_head * | |
ef53be89 | 7385 | elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) |
4d269e42 | 7386 | { |
14b1c01e | 7387 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
7388 | struct elf_symbuf_symbol *ssym; |
7389 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
ef53be89 | 7390 | size_t i, shndx_count, total_size; |
4d269e42 | 7391 | |
a50b1753 | 7392 | indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); |
4d269e42 AM |
7393 | if (indbuf == NULL) |
7394 | return NULL; | |
7395 | ||
7396 | for (ind = indbuf, i = 0; i < symcount; i++) | |
7397 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
7398 | *ind++ = &isymbuf[i]; | |
7399 | indbufend = ind; | |
7400 | ||
7401 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
7402 | elf_sort_elf_symbol); | |
7403 | ||
7404 | shndx_count = 0; | |
7405 | if (indbufend > indbuf) | |
7406 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
7407 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
7408 | shndx_count++; | |
7409 | ||
3ae181ee L |
7410 | total_size = ((shndx_count + 1) * sizeof (*ssymbuf) |
7411 | + (indbufend - indbuf) * sizeof (*ssym)); | |
a50b1753 | 7412 | ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); |
4d269e42 AM |
7413 | if (ssymbuf == NULL) |
7414 | { | |
7415 | free (indbuf); | |
7416 | return NULL; | |
7417 | } | |
7418 | ||
3ae181ee | 7419 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); |
4d269e42 AM |
7420 | ssymbuf->ssym = NULL; |
7421 | ssymbuf->count = shndx_count; | |
7422 | ssymbuf->st_shndx = 0; | |
7423 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
7424 | { | |
7425 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
7426 | { | |
7427 | ssymhead++; | |
7428 | ssymhead->ssym = ssym; | |
7429 | ssymhead->count = 0; | |
7430 | ssymhead->st_shndx = (*ind)->st_shndx; | |
7431 | } | |
7432 | ssym->st_name = (*ind)->st_name; | |
7433 | ssym->st_info = (*ind)->st_info; | |
7434 | ssym->st_other = (*ind)->st_other; | |
7435 | ssymhead->count++; | |
7436 | } | |
ef53be89 | 7437 | BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count |
3ae181ee L |
7438 | && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) |
7439 | == total_size)); | |
4d269e42 AM |
7440 | |
7441 | free (indbuf); | |
7442 | return ssymbuf; | |
7443 | } | |
7444 | ||
7445 | /* Check if 2 sections define the same set of local and global | |
7446 | symbols. */ | |
7447 | ||
8f317e31 | 7448 | static bfd_boolean |
4d269e42 AM |
7449 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
7450 | struct bfd_link_info *info) | |
7451 | { | |
7452 | bfd *bfd1, *bfd2; | |
7453 | const struct elf_backend_data *bed1, *bed2; | |
7454 | Elf_Internal_Shdr *hdr1, *hdr2; | |
ef53be89 | 7455 | size_t symcount1, symcount2; |
4d269e42 AM |
7456 | Elf_Internal_Sym *isymbuf1, *isymbuf2; |
7457 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
7458 | Elf_Internal_Sym *isym, *isymend; | |
7459 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
ef53be89 | 7460 | size_t count1, count2, i; |
cb33740c | 7461 | unsigned int shndx1, shndx2; |
4d269e42 AM |
7462 | bfd_boolean result; |
7463 | ||
7464 | bfd1 = sec1->owner; | |
7465 | bfd2 = sec2->owner; | |
7466 | ||
4d269e42 AM |
7467 | /* Both sections have to be in ELF. */ |
7468 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
7469 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
7470 | return FALSE; | |
7471 | ||
7472 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
7473 | return FALSE; | |
7474 | ||
4d269e42 AM |
7475 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
7476 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
cb33740c | 7477 | if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) |
4d269e42 AM |
7478 | return FALSE; |
7479 | ||
7480 | bed1 = get_elf_backend_data (bfd1); | |
7481 | bed2 = get_elf_backend_data (bfd2); | |
7482 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
7483 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
7484 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
7485 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
7486 | ||
7487 | if (symcount1 == 0 || symcount2 == 0) | |
7488 | return FALSE; | |
7489 | ||
7490 | result = FALSE; | |
7491 | isymbuf1 = NULL; | |
7492 | isymbuf2 = NULL; | |
a50b1753 NC |
7493 | ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; |
7494 | ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; | |
4d269e42 AM |
7495 | |
7496 | if (ssymbuf1 == NULL) | |
7497 | { | |
7498 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
7499 | NULL, NULL, NULL); | |
7500 | if (isymbuf1 == NULL) | |
7501 | goto done; | |
7502 | ||
7503 | if (!info->reduce_memory_overheads) | |
7504 | elf_tdata (bfd1)->symbuf = ssymbuf1 | |
7505 | = elf_create_symbuf (symcount1, isymbuf1); | |
7506 | } | |
7507 | ||
7508 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
7509 | { | |
7510 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
7511 | NULL, NULL, NULL); | |
7512 | if (isymbuf2 == NULL) | |
7513 | goto done; | |
7514 | ||
7515 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
7516 | elf_tdata (bfd2)->symbuf = ssymbuf2 | |
7517 | = elf_create_symbuf (symcount2, isymbuf2); | |
7518 | } | |
7519 | ||
7520 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
7521 | { | |
7522 | /* Optimized faster version. */ | |
ef53be89 | 7523 | size_t lo, hi, mid; |
4d269e42 AM |
7524 | struct elf_symbol *symp; |
7525 | struct elf_symbuf_symbol *ssym, *ssymend; | |
7526 | ||
7527 | lo = 0; | |
7528 | hi = ssymbuf1->count; | |
7529 | ssymbuf1++; | |
7530 | count1 = 0; | |
7531 | while (lo < hi) | |
7532 | { | |
7533 | mid = (lo + hi) / 2; | |
cb33740c | 7534 | if (shndx1 < ssymbuf1[mid].st_shndx) |
4d269e42 | 7535 | hi = mid; |
cb33740c | 7536 | else if (shndx1 > ssymbuf1[mid].st_shndx) |
4d269e42 AM |
7537 | lo = mid + 1; |
7538 | else | |
7539 | { | |
7540 | count1 = ssymbuf1[mid].count; | |
7541 | ssymbuf1 += mid; | |
7542 | break; | |
7543 | } | |
7544 | } | |
7545 | ||
7546 | lo = 0; | |
7547 | hi = ssymbuf2->count; | |
7548 | ssymbuf2++; | |
7549 | count2 = 0; | |
7550 | while (lo < hi) | |
7551 | { | |
7552 | mid = (lo + hi) / 2; | |
cb33740c | 7553 | if (shndx2 < ssymbuf2[mid].st_shndx) |
4d269e42 | 7554 | hi = mid; |
cb33740c | 7555 | else if (shndx2 > ssymbuf2[mid].st_shndx) |
4d269e42 AM |
7556 | lo = mid + 1; |
7557 | else | |
7558 | { | |
7559 | count2 = ssymbuf2[mid].count; | |
7560 | ssymbuf2 += mid; | |
7561 | break; | |
7562 | } | |
7563 | } | |
7564 | ||
7565 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7566 | goto done; | |
7567 | ||
ca4be51c AM |
7568 | symtable1 |
7569 | = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); | |
7570 | symtable2 | |
7571 | = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); | |
4d269e42 AM |
7572 | if (symtable1 == NULL || symtable2 == NULL) |
7573 | goto done; | |
7574 | ||
7575 | symp = symtable1; | |
7576 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
7577 | ssym < ssymend; ssym++, symp++) | |
7578 | { | |
7579 | symp->u.ssym = ssym; | |
7580 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
7581 | hdr1->sh_link, | |
7582 | ssym->st_name); | |
7583 | } | |
7584 | ||
7585 | symp = symtable2; | |
7586 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
7587 | ssym < ssymend; ssym++, symp++) | |
7588 | { | |
7589 | symp->u.ssym = ssym; | |
7590 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
7591 | hdr2->sh_link, | |
7592 | ssym->st_name); | |
7593 | } | |
7594 | ||
7595 | /* Sort symbol by name. */ | |
7596 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7597 | elf_sym_name_compare); | |
7598 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7599 | elf_sym_name_compare); | |
7600 | ||
7601 | for (i = 0; i < count1; i++) | |
7602 | /* Two symbols must have the same binding, type and name. */ | |
7603 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
7604 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
7605 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7606 | goto done; | |
7607 | ||
7608 | result = TRUE; | |
7609 | goto done; | |
7610 | } | |
7611 | ||
a50b1753 NC |
7612 | symtable1 = (struct elf_symbol *) |
7613 | bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
7614 | symtable2 = (struct elf_symbol *) | |
7615 | bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
4d269e42 AM |
7616 | if (symtable1 == NULL || symtable2 == NULL) |
7617 | goto done; | |
7618 | ||
7619 | /* Count definitions in the section. */ | |
7620 | count1 = 0; | |
7621 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
cb33740c | 7622 | if (isym->st_shndx == shndx1) |
4d269e42 AM |
7623 | symtable1[count1++].u.isym = isym; |
7624 | ||
7625 | count2 = 0; | |
7626 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
cb33740c | 7627 | if (isym->st_shndx == shndx2) |
4d269e42 AM |
7628 | symtable2[count2++].u.isym = isym; |
7629 | ||
7630 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7631 | goto done; | |
7632 | ||
7633 | for (i = 0; i < count1; i++) | |
7634 | symtable1[i].name | |
7635 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
7636 | symtable1[i].u.isym->st_name); | |
7637 | ||
7638 | for (i = 0; i < count2; i++) | |
7639 | symtable2[i].name | |
7640 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
7641 | symtable2[i].u.isym->st_name); | |
7642 | ||
7643 | /* Sort symbol by name. */ | |
7644 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7645 | elf_sym_name_compare); | |
7646 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7647 | elf_sym_name_compare); | |
7648 | ||
7649 | for (i = 0; i < count1; i++) | |
7650 | /* Two symbols must have the same binding, type and name. */ | |
7651 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
7652 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
7653 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7654 | goto done; | |
7655 | ||
7656 | result = TRUE; | |
7657 | ||
7658 | done: | |
7659 | if (symtable1) | |
7660 | free (symtable1); | |
7661 | if (symtable2) | |
7662 | free (symtable2); | |
7663 | if (isymbuf1) | |
7664 | free (isymbuf1); | |
7665 | if (isymbuf2) | |
7666 | free (isymbuf2); | |
7667 | ||
7668 | return result; | |
7669 | } | |
7670 | ||
7671 | /* Return TRUE if 2 section types are compatible. */ | |
7672 | ||
7673 | bfd_boolean | |
7674 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
7675 | bfd *bbfd, const asection *bsec) | |
7676 | { | |
7677 | if (asec == NULL | |
7678 | || bsec == NULL | |
7679 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
7680 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
7681 | return TRUE; | |
7682 | ||
7683 | return elf_section_type (asec) == elf_section_type (bsec); | |
7684 | } | |
7685 | \f | |
c152c796 AM |
7686 | /* Final phase of ELF linker. */ |
7687 | ||
7688 | /* A structure we use to avoid passing large numbers of arguments. */ | |
7689 | ||
7690 | struct elf_final_link_info | |
7691 | { | |
7692 | /* General link information. */ | |
7693 | struct bfd_link_info *info; | |
7694 | /* Output BFD. */ | |
7695 | bfd *output_bfd; | |
7696 | /* Symbol string table. */ | |
ef10c3ac | 7697 | struct elf_strtab_hash *symstrtab; |
c152c796 AM |
7698 | /* .hash section. */ |
7699 | asection *hash_sec; | |
7700 | /* symbol version section (.gnu.version). */ | |
7701 | asection *symver_sec; | |
7702 | /* Buffer large enough to hold contents of any section. */ | |
7703 | bfd_byte *contents; | |
7704 | /* Buffer large enough to hold external relocs of any section. */ | |
7705 | void *external_relocs; | |
7706 | /* Buffer large enough to hold internal relocs of any section. */ | |
7707 | Elf_Internal_Rela *internal_relocs; | |
7708 | /* Buffer large enough to hold external local symbols of any input | |
7709 | BFD. */ | |
7710 | bfd_byte *external_syms; | |
7711 | /* And a buffer for symbol section indices. */ | |
7712 | Elf_External_Sym_Shndx *locsym_shndx; | |
7713 | /* Buffer large enough to hold internal local symbols of any input | |
7714 | BFD. */ | |
7715 | Elf_Internal_Sym *internal_syms; | |
7716 | /* Array large enough to hold a symbol index for each local symbol | |
7717 | of any input BFD. */ | |
7718 | long *indices; | |
7719 | /* Array large enough to hold a section pointer for each local | |
7720 | symbol of any input BFD. */ | |
7721 | asection **sections; | |
ef10c3ac | 7722 | /* Buffer for SHT_SYMTAB_SHNDX section. */ |
c152c796 | 7723 | Elf_External_Sym_Shndx *symshndxbuf; |
ffbc01cc AM |
7724 | /* Number of STT_FILE syms seen. */ |
7725 | size_t filesym_count; | |
c152c796 AM |
7726 | }; |
7727 | ||
7728 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
7729 | ||
7730 | struct elf_outext_info | |
7731 | { | |
7732 | bfd_boolean failed; | |
7733 | bfd_boolean localsyms; | |
34a79995 | 7734 | bfd_boolean file_sym_done; |
8b127cbc | 7735 | struct elf_final_link_info *flinfo; |
c152c796 AM |
7736 | }; |
7737 | ||
d9352518 DB |
7738 | |
7739 | /* Support for evaluating a complex relocation. | |
7740 | ||
7741 | Complex relocations are generalized, self-describing relocations. The | |
7742 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 7743 | relocations themselves. |
d9352518 DB |
7744 | |
7745 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
7746 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
7747 | information (start bit, end bit, word width, etc) into the addend. This | |
7748 | information is extracted from CGEN-generated operand tables within gas. | |
7749 | ||
7750 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
7751 | internal) representing prefix-notation expressions, including but not | |
7752 | limited to those sorts of expressions normally encoded as addends in the | |
7753 | addend field. The symbol mangling format is: | |
7754 | ||
7755 | <node> := <literal> | |
7756 | | <unary-operator> ':' <node> | |
7757 | | <binary-operator> ':' <node> ':' <node> | |
7758 | ; | |
7759 | ||
7760 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
7761 | | 'S' <digits=N> ':' <N character section name> | |
7762 | | '#' <hexdigits> | |
7763 | ; | |
7764 | ||
7765 | <binary-operator> := as in C | |
7766 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
7767 | ||
7768 | static void | |
a0c8462f AM |
7769 | set_symbol_value (bfd *bfd_with_globals, |
7770 | Elf_Internal_Sym *isymbuf, | |
7771 | size_t locsymcount, | |
7772 | size_t symidx, | |
7773 | bfd_vma val) | |
d9352518 | 7774 | { |
8977835c AM |
7775 | struct elf_link_hash_entry **sym_hashes; |
7776 | struct elf_link_hash_entry *h; | |
7777 | size_t extsymoff = locsymcount; | |
d9352518 | 7778 | |
8977835c | 7779 | if (symidx < locsymcount) |
d9352518 | 7780 | { |
8977835c AM |
7781 | Elf_Internal_Sym *sym; |
7782 | ||
7783 | sym = isymbuf + symidx; | |
7784 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
7785 | { | |
7786 | /* It is a local symbol: move it to the | |
7787 | "absolute" section and give it a value. */ | |
7788 | sym->st_shndx = SHN_ABS; | |
7789 | sym->st_value = val; | |
7790 | return; | |
7791 | } | |
7792 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
7793 | extsymoff = 0; | |
d9352518 | 7794 | } |
8977835c AM |
7795 | |
7796 | /* It is a global symbol: set its link type | |
7797 | to "defined" and give it a value. */ | |
7798 | ||
7799 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
7800 | h = sym_hashes [symidx - extsymoff]; | |
7801 | while (h->root.type == bfd_link_hash_indirect | |
7802 | || h->root.type == bfd_link_hash_warning) | |
7803 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7804 | h->root.type = bfd_link_hash_defined; | |
7805 | h->root.u.def.value = val; | |
7806 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
7807 | } |
7808 | ||
a0c8462f AM |
7809 | static bfd_boolean |
7810 | resolve_symbol (const char *name, | |
7811 | bfd *input_bfd, | |
8b127cbc | 7812 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
7813 | bfd_vma *result, |
7814 | Elf_Internal_Sym *isymbuf, | |
7815 | size_t locsymcount) | |
d9352518 | 7816 | { |
a0c8462f AM |
7817 | Elf_Internal_Sym *sym; |
7818 | struct bfd_link_hash_entry *global_entry; | |
7819 | const char *candidate = NULL; | |
7820 | Elf_Internal_Shdr *symtab_hdr; | |
7821 | size_t i; | |
7822 | ||
d9352518 DB |
7823 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
7824 | ||
7825 | for (i = 0; i < locsymcount; ++ i) | |
7826 | { | |
8977835c | 7827 | sym = isymbuf + i; |
d9352518 DB |
7828 | |
7829 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
7830 | continue; | |
7831 | ||
7832 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
7833 | symtab_hdr->sh_link, | |
7834 | sym->st_name); | |
7835 | #ifdef DEBUG | |
0f02bbd9 AM |
7836 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
7837 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
7838 | #endif |
7839 | if (candidate && strcmp (candidate, name) == 0) | |
7840 | { | |
8b127cbc | 7841 | asection *sec = flinfo->sections [i]; |
d9352518 | 7842 | |
0f02bbd9 AM |
7843 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
7844 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 7845 | #ifdef DEBUG |
0f02bbd9 AM |
7846 | printf ("Found symbol with value %8.8lx\n", |
7847 | (unsigned long) *result); | |
d9352518 DB |
7848 | #endif |
7849 | return TRUE; | |
7850 | } | |
7851 | } | |
7852 | ||
7853 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
8b127cbc | 7854 | global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, |
a0c8462f | 7855 | FALSE, FALSE, TRUE); |
d9352518 DB |
7856 | if (!global_entry) |
7857 | return FALSE; | |
a0c8462f | 7858 | |
d9352518 DB |
7859 | if (global_entry->type == bfd_link_hash_defined |
7860 | || global_entry->type == bfd_link_hash_defweak) | |
7861 | { | |
a0c8462f AM |
7862 | *result = (global_entry->u.def.value |
7863 | + global_entry->u.def.section->output_section->vma | |
7864 | + global_entry->u.def.section->output_offset); | |
d9352518 | 7865 | #ifdef DEBUG |
0f02bbd9 AM |
7866 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
7867 | global_entry->root.string, (unsigned long) *result); | |
d9352518 DB |
7868 | #endif |
7869 | return TRUE; | |
a0c8462f | 7870 | } |
d9352518 | 7871 | |
d9352518 DB |
7872 | return FALSE; |
7873 | } | |
7874 | ||
37b01f6a DG |
7875 | /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in |
7876 | bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section | |
7877 | names like "foo.end" which is the end address of section "foo". */ | |
7878 | ||
d9352518 | 7879 | static bfd_boolean |
a0c8462f AM |
7880 | resolve_section (const char *name, |
7881 | asection *sections, | |
37b01f6a DG |
7882 | bfd_vma *result, |
7883 | bfd * abfd) | |
d9352518 | 7884 | { |
a0c8462f AM |
7885 | asection *curr; |
7886 | unsigned int len; | |
d9352518 | 7887 | |
a0c8462f | 7888 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
7889 | if (strcmp (curr->name, name) == 0) |
7890 | { | |
7891 | *result = curr->vma; | |
7892 | return TRUE; | |
7893 | } | |
7894 | ||
7895 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
37b01f6a | 7896 | /* FIXME: This could be coded more efficiently... */ |
a0c8462f | 7897 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
7898 | { |
7899 | len = strlen (curr->name); | |
a0c8462f | 7900 | if (len > strlen (name)) |
d9352518 DB |
7901 | continue; |
7902 | ||
7903 | if (strncmp (curr->name, name, len) == 0) | |
7904 | { | |
7905 | if (strncmp (".end", name + len, 4) == 0) | |
7906 | { | |
37b01f6a | 7907 | *result = curr->vma + curr->size / bfd_octets_per_byte (abfd); |
d9352518 DB |
7908 | return TRUE; |
7909 | } | |
7910 | ||
7911 | /* Insert more pseudo-section names here, if you like. */ | |
7912 | } | |
7913 | } | |
a0c8462f | 7914 | |
d9352518 DB |
7915 | return FALSE; |
7916 | } | |
7917 | ||
7918 | static void | |
a0c8462f | 7919 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 7920 | { |
a0c8462f AM |
7921 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
7922 | reftype, name); | |
d9352518 DB |
7923 | } |
7924 | ||
7925 | static bfd_boolean | |
a0c8462f AM |
7926 | eval_symbol (bfd_vma *result, |
7927 | const char **symp, | |
7928 | bfd *input_bfd, | |
8b127cbc | 7929 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
7930 | bfd_vma dot, |
7931 | Elf_Internal_Sym *isymbuf, | |
7932 | size_t locsymcount, | |
7933 | int signed_p) | |
d9352518 | 7934 | { |
4b93929b NC |
7935 | size_t len; |
7936 | size_t symlen; | |
a0c8462f AM |
7937 | bfd_vma a; |
7938 | bfd_vma b; | |
4b93929b | 7939 | char symbuf[4096]; |
0f02bbd9 | 7940 | const char *sym = *symp; |
a0c8462f AM |
7941 | const char *symend; |
7942 | bfd_boolean symbol_is_section = FALSE; | |
d9352518 DB |
7943 | |
7944 | len = strlen (sym); | |
7945 | symend = sym + len; | |
7946 | ||
4b93929b | 7947 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
7948 | { |
7949 | bfd_set_error (bfd_error_invalid_operation); | |
7950 | return FALSE; | |
7951 | } | |
a0c8462f | 7952 | |
d9352518 DB |
7953 | switch (* sym) |
7954 | { | |
7955 | case '.': | |
0f02bbd9 AM |
7956 | *result = dot; |
7957 | *symp = sym + 1; | |
d9352518 DB |
7958 | return TRUE; |
7959 | ||
7960 | case '#': | |
0f02bbd9 AM |
7961 | ++sym; |
7962 | *result = strtoul (sym, (char **) symp, 16); | |
d9352518 DB |
7963 | return TRUE; |
7964 | ||
7965 | case 'S': | |
7966 | symbol_is_section = TRUE; | |
a0c8462f | 7967 | case 's': |
0f02bbd9 AM |
7968 | ++sym; |
7969 | symlen = strtol (sym, (char **) symp, 10); | |
7970 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 7971 | |
4b93929b | 7972 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
7973 | { |
7974 | bfd_set_error (bfd_error_invalid_operation); | |
7975 | return FALSE; | |
7976 | } | |
7977 | ||
7978 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 7979 | symbuf[symlen] = '\0'; |
0f02bbd9 | 7980 | *symp = sym + symlen; |
a0c8462f AM |
7981 | |
7982 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
7983 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
7984 | interpretation here; section means "try section first", not "must be a | |
7985 | section", and likewise with symbol. */ | |
7986 | ||
a0c8462f | 7987 | if (symbol_is_section) |
d9352518 | 7988 | { |
37b01f6a | 7989 | if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) |
8b127cbc | 7990 | && !resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 7991 | isymbuf, locsymcount)) |
d9352518 DB |
7992 | { |
7993 | undefined_reference ("section", symbuf); | |
7994 | return FALSE; | |
7995 | } | |
a0c8462f AM |
7996 | } |
7997 | else | |
d9352518 | 7998 | { |
8b127cbc | 7999 | if (!resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8000 | isymbuf, locsymcount) |
8b127cbc | 8001 | && !resolve_section (symbuf, flinfo->output_bfd->sections, |
37b01f6a | 8002 | result, input_bfd)) |
d9352518 DB |
8003 | { |
8004 | undefined_reference ("symbol", symbuf); | |
8005 | return FALSE; | |
8006 | } | |
8007 | } | |
8008 | ||
8009 | return TRUE; | |
a0c8462f | 8010 | |
d9352518 DB |
8011 | /* All that remains are operators. */ |
8012 | ||
8013 | #define UNARY_OP(op) \ | |
8014 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8015 | { \ | |
8016 | sym += strlen (#op); \ | |
a0c8462f AM |
8017 | if (*sym == ':') \ |
8018 | ++sym; \ | |
0f02bbd9 | 8019 | *symp = sym; \ |
8b127cbc | 8020 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8021 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8022 | return FALSE; \ |
8023 | if (signed_p) \ | |
0f02bbd9 | 8024 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
8025 | else \ |
8026 | *result = op a; \ | |
d9352518 DB |
8027 | return TRUE; \ |
8028 | } | |
8029 | ||
8030 | #define BINARY_OP(op) \ | |
8031 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8032 | { \ | |
8033 | sym += strlen (#op); \ | |
a0c8462f AM |
8034 | if (*sym == ':') \ |
8035 | ++sym; \ | |
0f02bbd9 | 8036 | *symp = sym; \ |
8b127cbc | 8037 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8038 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f | 8039 | return FALSE; \ |
0f02bbd9 | 8040 | ++*symp; \ |
8b127cbc | 8041 | if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8042 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8043 | return FALSE; \ |
8044 | if (signed_p) \ | |
0f02bbd9 | 8045 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
8046 | else \ |
8047 | *result = a op b; \ | |
d9352518 DB |
8048 | return TRUE; \ |
8049 | } | |
8050 | ||
8051 | default: | |
8052 | UNARY_OP (0-); | |
8053 | BINARY_OP (<<); | |
8054 | BINARY_OP (>>); | |
8055 | BINARY_OP (==); | |
8056 | BINARY_OP (!=); | |
8057 | BINARY_OP (<=); | |
8058 | BINARY_OP (>=); | |
8059 | BINARY_OP (&&); | |
8060 | BINARY_OP (||); | |
8061 | UNARY_OP (~); | |
8062 | UNARY_OP (!); | |
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 | BINARY_OP (<); | |
8072 | BINARY_OP (>); | |
8073 | #undef UNARY_OP | |
8074 | #undef BINARY_OP | |
8075 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
8076 | bfd_set_error (bfd_error_invalid_operation); | |
8077 | return FALSE; | |
8078 | } | |
8079 | } | |
8080 | ||
d9352518 | 8081 | static void |
a0c8462f AM |
8082 | put_value (bfd_vma size, |
8083 | unsigned long chunksz, | |
8084 | bfd *input_bfd, | |
8085 | bfd_vma x, | |
8086 | bfd_byte *location) | |
d9352518 DB |
8087 | { |
8088 | location += (size - chunksz); | |
8089 | ||
41cd1ad1 | 8090 | for (; size; size -= chunksz, location -= chunksz) |
d9352518 DB |
8091 | { |
8092 | switch (chunksz) | |
8093 | { | |
d9352518 DB |
8094 | case 1: |
8095 | bfd_put_8 (input_bfd, x, location); | |
41cd1ad1 | 8096 | x >>= 8; |
d9352518 DB |
8097 | break; |
8098 | case 2: | |
8099 | bfd_put_16 (input_bfd, x, location); | |
41cd1ad1 | 8100 | x >>= 16; |
d9352518 DB |
8101 | break; |
8102 | case 4: | |
8103 | bfd_put_32 (input_bfd, x, location); | |
65164438 NC |
8104 | /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ |
8105 | x >>= 16; | |
8106 | x >>= 16; | |
d9352518 | 8107 | break; |
d9352518 | 8108 | #ifdef BFD64 |
41cd1ad1 | 8109 | case 8: |
d9352518 | 8110 | bfd_put_64 (input_bfd, x, location); |
41cd1ad1 NC |
8111 | /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ |
8112 | x >>= 32; | |
8113 | x >>= 32; | |
8114 | break; | |
d9352518 | 8115 | #endif |
41cd1ad1 NC |
8116 | default: |
8117 | abort (); | |
d9352518 DB |
8118 | break; |
8119 | } | |
8120 | } | |
8121 | } | |
8122 | ||
a0c8462f AM |
8123 | static bfd_vma |
8124 | get_value (bfd_vma size, | |
8125 | unsigned long chunksz, | |
8126 | bfd *input_bfd, | |
8127 | bfd_byte *location) | |
d9352518 | 8128 | { |
9b239e0e | 8129 | int shift; |
d9352518 DB |
8130 | bfd_vma x = 0; |
8131 | ||
9b239e0e NC |
8132 | /* Sanity checks. */ |
8133 | BFD_ASSERT (chunksz <= sizeof (x) | |
8134 | && size >= chunksz | |
8135 | && chunksz != 0 | |
8136 | && (size % chunksz) == 0 | |
8137 | && input_bfd != NULL | |
8138 | && location != NULL); | |
8139 | ||
8140 | if (chunksz == sizeof (x)) | |
8141 | { | |
8142 | BFD_ASSERT (size == chunksz); | |
8143 | ||
8144 | /* Make sure that we do not perform an undefined shift operation. | |
8145 | We know that size == chunksz so there will only be one iteration | |
8146 | of the loop below. */ | |
8147 | shift = 0; | |
8148 | } | |
8149 | else | |
8150 | shift = 8 * chunksz; | |
8151 | ||
a0c8462f | 8152 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
8153 | { |
8154 | switch (chunksz) | |
8155 | { | |
d9352518 | 8156 | case 1: |
9b239e0e | 8157 | x = (x << shift) | bfd_get_8 (input_bfd, location); |
d9352518 DB |
8158 | break; |
8159 | case 2: | |
9b239e0e | 8160 | x = (x << shift) | bfd_get_16 (input_bfd, location); |
d9352518 DB |
8161 | break; |
8162 | case 4: | |
9b239e0e | 8163 | x = (x << shift) | bfd_get_32 (input_bfd, location); |
d9352518 | 8164 | break; |
d9352518 | 8165 | #ifdef BFD64 |
9b239e0e NC |
8166 | case 8: |
8167 | x = (x << shift) | bfd_get_64 (input_bfd, location); | |
d9352518 | 8168 | break; |
9b239e0e NC |
8169 | #endif |
8170 | default: | |
8171 | abort (); | |
d9352518 DB |
8172 | } |
8173 | } | |
8174 | return x; | |
8175 | } | |
8176 | ||
a0c8462f AM |
8177 | static void |
8178 | decode_complex_addend (unsigned long *start, /* in bits */ | |
8179 | unsigned long *oplen, /* in bits */ | |
8180 | unsigned long *len, /* in bits */ | |
8181 | unsigned long *wordsz, /* in bytes */ | |
8182 | unsigned long *chunksz, /* in bytes */ | |
8183 | unsigned long *lsb0_p, | |
8184 | unsigned long *signed_p, | |
8185 | unsigned long *trunc_p, | |
8186 | unsigned long encoded) | |
d9352518 DB |
8187 | { |
8188 | * start = encoded & 0x3F; | |
8189 | * len = (encoded >> 6) & 0x3F; | |
8190 | * oplen = (encoded >> 12) & 0x3F; | |
8191 | * wordsz = (encoded >> 18) & 0xF; | |
8192 | * chunksz = (encoded >> 22) & 0xF; | |
8193 | * lsb0_p = (encoded >> 27) & 1; | |
8194 | * signed_p = (encoded >> 28) & 1; | |
8195 | * trunc_p = (encoded >> 29) & 1; | |
8196 | } | |
8197 | ||
cdfeee4f | 8198 | bfd_reloc_status_type |
0f02bbd9 | 8199 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
cdfeee4f | 8200 | asection *input_section ATTRIBUTE_UNUSED, |
0f02bbd9 AM |
8201 | bfd_byte *contents, |
8202 | Elf_Internal_Rela *rel, | |
8203 | bfd_vma relocation) | |
d9352518 | 8204 | { |
0f02bbd9 AM |
8205 | bfd_vma shift, x, mask; |
8206 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 8207 | bfd_reloc_status_type r; |
d9352518 DB |
8208 | |
8209 | /* Perform this reloc, since it is complex. | |
8210 | (this is not to say that it necessarily refers to a complex | |
8211 | symbol; merely that it is a self-describing CGEN based reloc. | |
8212 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 8213 | word size, etc) encoded within it.). */ |
d9352518 | 8214 | |
a0c8462f AM |
8215 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
8216 | &chunksz, &lsb0_p, &signed_p, | |
8217 | &trunc_p, rel->r_addend); | |
d9352518 DB |
8218 | |
8219 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
8220 | ||
8221 | if (lsb0_p) | |
8222 | shift = (start + 1) - len; | |
8223 | else | |
8224 | shift = (8 * wordsz) - (start + len); | |
8225 | ||
37b01f6a DG |
8226 | x = get_value (wordsz, chunksz, input_bfd, |
8227 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
d9352518 DB |
8228 | |
8229 | #ifdef DEBUG | |
8230 | printf ("Doing complex reloc: " | |
8231 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
8232 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
8233 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
8234 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
9ccb8af9 AM |
8235 | oplen, (unsigned long) x, (unsigned long) mask, |
8236 | (unsigned long) relocation); | |
d9352518 DB |
8237 | #endif |
8238 | ||
cdfeee4f | 8239 | r = bfd_reloc_ok; |
d9352518 | 8240 | if (! trunc_p) |
cdfeee4f AM |
8241 | /* Now do an overflow check. */ |
8242 | r = bfd_check_overflow ((signed_p | |
8243 | ? complain_overflow_signed | |
8244 | : complain_overflow_unsigned), | |
8245 | len, 0, (8 * wordsz), | |
8246 | relocation); | |
a0c8462f | 8247 | |
d9352518 DB |
8248 | /* Do the deed. */ |
8249 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
8250 | ||
8251 | #ifdef DEBUG | |
8252 | printf (" relocation: %8.8lx\n" | |
8253 | " shifted mask: %8.8lx\n" | |
8254 | " shifted/masked reloc: %8.8lx\n" | |
8255 | " result: %8.8lx\n", | |
9ccb8af9 AM |
8256 | (unsigned long) relocation, (unsigned long) (mask << shift), |
8257 | (unsigned long) ((relocation & mask) << shift), (unsigned long) x); | |
d9352518 | 8258 | #endif |
37b01f6a DG |
8259 | put_value (wordsz, chunksz, input_bfd, x, |
8260 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
cdfeee4f | 8261 | return r; |
d9352518 DB |
8262 | } |
8263 | ||
0e287786 AM |
8264 | /* Functions to read r_offset from external (target order) reloc |
8265 | entry. Faster than bfd_getl32 et al, because we let the compiler | |
8266 | know the value is aligned. */ | |
53df40a4 | 8267 | |
0e287786 AM |
8268 | static bfd_vma |
8269 | ext32l_r_offset (const void *p) | |
53df40a4 AM |
8270 | { |
8271 | union aligned32 | |
8272 | { | |
8273 | uint32_t v; | |
8274 | unsigned char c[4]; | |
8275 | }; | |
8276 | const union aligned32 *a | |
0e287786 | 8277 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8278 | |
8279 | uint32_t aval = ( (uint32_t) a->c[0] | |
8280 | | (uint32_t) a->c[1] << 8 | |
8281 | | (uint32_t) a->c[2] << 16 | |
8282 | | (uint32_t) a->c[3] << 24); | |
0e287786 | 8283 | return aval; |
53df40a4 AM |
8284 | } |
8285 | ||
0e287786 AM |
8286 | static bfd_vma |
8287 | ext32b_r_offset (const void *p) | |
53df40a4 AM |
8288 | { |
8289 | union aligned32 | |
8290 | { | |
8291 | uint32_t v; | |
8292 | unsigned char c[4]; | |
8293 | }; | |
8294 | const union aligned32 *a | |
0e287786 | 8295 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8296 | |
8297 | uint32_t aval = ( (uint32_t) a->c[0] << 24 | |
8298 | | (uint32_t) a->c[1] << 16 | |
8299 | | (uint32_t) a->c[2] << 8 | |
8300 | | (uint32_t) a->c[3]); | |
0e287786 | 8301 | return aval; |
53df40a4 AM |
8302 | } |
8303 | ||
8304 | #ifdef BFD_HOST_64_BIT | |
0e287786 AM |
8305 | static bfd_vma |
8306 | ext64l_r_offset (const void *p) | |
53df40a4 AM |
8307 | { |
8308 | union aligned64 | |
8309 | { | |
8310 | uint64_t v; | |
8311 | unsigned char c[8]; | |
8312 | }; | |
8313 | const union aligned64 *a | |
0e287786 | 8314 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8315 | |
8316 | uint64_t aval = ( (uint64_t) a->c[0] | |
8317 | | (uint64_t) a->c[1] << 8 | |
8318 | | (uint64_t) a->c[2] << 16 | |
8319 | | (uint64_t) a->c[3] << 24 | |
8320 | | (uint64_t) a->c[4] << 32 | |
8321 | | (uint64_t) a->c[5] << 40 | |
8322 | | (uint64_t) a->c[6] << 48 | |
8323 | | (uint64_t) a->c[7] << 56); | |
0e287786 | 8324 | return aval; |
53df40a4 AM |
8325 | } |
8326 | ||
0e287786 AM |
8327 | static bfd_vma |
8328 | ext64b_r_offset (const void *p) | |
53df40a4 AM |
8329 | { |
8330 | union aligned64 | |
8331 | { | |
8332 | uint64_t v; | |
8333 | unsigned char c[8]; | |
8334 | }; | |
8335 | const union aligned64 *a | |
0e287786 | 8336 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8337 | |
8338 | uint64_t aval = ( (uint64_t) a->c[0] << 56 | |
8339 | | (uint64_t) a->c[1] << 48 | |
8340 | | (uint64_t) a->c[2] << 40 | |
8341 | | (uint64_t) a->c[3] << 32 | |
8342 | | (uint64_t) a->c[4] << 24 | |
8343 | | (uint64_t) a->c[5] << 16 | |
8344 | | (uint64_t) a->c[6] << 8 | |
8345 | | (uint64_t) a->c[7]); | |
0e287786 | 8346 | return aval; |
53df40a4 AM |
8347 | } |
8348 | #endif | |
8349 | ||
c152c796 AM |
8350 | /* When performing a relocatable link, the input relocations are |
8351 | preserved. But, if they reference global symbols, the indices | |
d4730f92 BS |
8352 | referenced must be updated. Update all the relocations found in |
8353 | RELDATA. */ | |
c152c796 | 8354 | |
bca6d0e3 | 8355 | static bfd_boolean |
c152c796 | 8356 | elf_link_adjust_relocs (bfd *abfd, |
28dbcedc AM |
8357 | struct bfd_elf_section_reloc_data *reldata, |
8358 | bfd_boolean sort) | |
c152c796 AM |
8359 | { |
8360 | unsigned int i; | |
8361 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8362 | bfd_byte *erela; | |
8363 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
8364 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8365 | bfd_vma r_type_mask; | |
8366 | int r_sym_shift; | |
d4730f92 BS |
8367 | unsigned int count = reldata->count; |
8368 | struct elf_link_hash_entry **rel_hash = reldata->hashes; | |
c152c796 | 8369 | |
d4730f92 | 8370 | if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) |
c152c796 AM |
8371 | { |
8372 | swap_in = bed->s->swap_reloc_in; | |
8373 | swap_out = bed->s->swap_reloc_out; | |
8374 | } | |
d4730f92 | 8375 | else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) |
c152c796 AM |
8376 | { |
8377 | swap_in = bed->s->swap_reloca_in; | |
8378 | swap_out = bed->s->swap_reloca_out; | |
8379 | } | |
8380 | else | |
8381 | abort (); | |
8382 | ||
8383 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
8384 | abort (); | |
8385 | ||
8386 | if (bed->s->arch_size == 32) | |
8387 | { | |
8388 | r_type_mask = 0xff; | |
8389 | r_sym_shift = 8; | |
8390 | } | |
8391 | else | |
8392 | { | |
8393 | r_type_mask = 0xffffffff; | |
8394 | r_sym_shift = 32; | |
8395 | } | |
8396 | ||
d4730f92 BS |
8397 | erela = reldata->hdr->contents; |
8398 | for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) | |
c152c796 AM |
8399 | { |
8400 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
8401 | unsigned int j; | |
8402 | ||
8403 | if (*rel_hash == NULL) | |
8404 | continue; | |
8405 | ||
8406 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
8407 | ||
8408 | (*swap_in) (abfd, erela, irela); | |
8409 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
8410 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
8411 | | (irela[j].r_info & r_type_mask)); | |
8412 | (*swap_out) (abfd, irela, erela); | |
8413 | } | |
53df40a4 | 8414 | |
0e287786 | 8415 | if (sort && count != 0) |
53df40a4 | 8416 | { |
0e287786 AM |
8417 | bfd_vma (*ext_r_off) (const void *); |
8418 | bfd_vma r_off; | |
8419 | size_t elt_size; | |
8420 | bfd_byte *base, *end, *p, *loc; | |
bca6d0e3 | 8421 | bfd_byte *buf = NULL; |
28dbcedc AM |
8422 | |
8423 | if (bed->s->arch_size == 32) | |
8424 | { | |
8425 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) | |
0e287786 | 8426 | ext_r_off = ext32l_r_offset; |
28dbcedc | 8427 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8428 | ext_r_off = ext32b_r_offset; |
28dbcedc AM |
8429 | else |
8430 | abort (); | |
8431 | } | |
53df40a4 | 8432 | else |
28dbcedc | 8433 | { |
53df40a4 | 8434 | #ifdef BFD_HOST_64_BIT |
28dbcedc | 8435 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) |
0e287786 | 8436 | ext_r_off = ext64l_r_offset; |
28dbcedc | 8437 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8438 | ext_r_off = ext64b_r_offset; |
28dbcedc | 8439 | else |
53df40a4 | 8440 | #endif |
28dbcedc AM |
8441 | abort (); |
8442 | } | |
0e287786 | 8443 | |
bca6d0e3 AM |
8444 | /* Must use a stable sort here. A modified insertion sort, |
8445 | since the relocs are mostly sorted already. */ | |
0e287786 AM |
8446 | elt_size = reldata->hdr->sh_entsize; |
8447 | base = reldata->hdr->contents; | |
8448 | end = base + count * elt_size; | |
bca6d0e3 | 8449 | if (elt_size > sizeof (Elf64_External_Rela)) |
0e287786 AM |
8450 | abort (); |
8451 | ||
8452 | /* Ensure the first element is lowest. This acts as a sentinel, | |
8453 | speeding the main loop below. */ | |
8454 | r_off = (*ext_r_off) (base); | |
8455 | for (p = loc = base; (p += elt_size) < end; ) | |
8456 | { | |
8457 | bfd_vma r_off2 = (*ext_r_off) (p); | |
8458 | if (r_off > r_off2) | |
8459 | { | |
8460 | r_off = r_off2; | |
8461 | loc = p; | |
8462 | } | |
8463 | } | |
8464 | if (loc != base) | |
8465 | { | |
8466 | /* Don't just swap *base and *loc as that changes the order | |
8467 | of the original base[0] and base[1] if they happen to | |
8468 | have the same r_offset. */ | |
bca6d0e3 AM |
8469 | bfd_byte onebuf[sizeof (Elf64_External_Rela)]; |
8470 | memcpy (onebuf, loc, elt_size); | |
0e287786 | 8471 | memmove (base + elt_size, base, loc - base); |
bca6d0e3 | 8472 | memcpy (base, onebuf, elt_size); |
0e287786 AM |
8473 | } |
8474 | ||
b29b8669 | 8475 | for (p = base + elt_size; (p += elt_size) < end; ) |
0e287786 AM |
8476 | { |
8477 | /* base to p is sorted, *p is next to insert. */ | |
8478 | r_off = (*ext_r_off) (p); | |
8479 | /* Search the sorted region for location to insert. */ | |
8480 | loc = p - elt_size; | |
8481 | while (r_off < (*ext_r_off) (loc)) | |
8482 | loc -= elt_size; | |
8483 | loc += elt_size; | |
8484 | if (loc != p) | |
8485 | { | |
bca6d0e3 AM |
8486 | /* Chances are there is a run of relocs to insert here, |
8487 | from one of more input files. Files are not always | |
8488 | linked in order due to the way elf_link_input_bfd is | |
8489 | called. See pr17666. */ | |
8490 | size_t sortlen = p - loc; | |
8491 | bfd_vma r_off2 = (*ext_r_off) (loc); | |
8492 | size_t runlen = elt_size; | |
8493 | size_t buf_size = 96 * 1024; | |
8494 | while (p + runlen < end | |
8495 | && (sortlen <= buf_size | |
8496 | || runlen + elt_size <= buf_size) | |
8497 | && r_off2 > (*ext_r_off) (p + runlen)) | |
8498 | runlen += elt_size; | |
8499 | if (buf == NULL) | |
8500 | { | |
8501 | buf = bfd_malloc (buf_size); | |
8502 | if (buf == NULL) | |
8503 | return FALSE; | |
8504 | } | |
8505 | if (runlen < sortlen) | |
8506 | { | |
8507 | memcpy (buf, p, runlen); | |
8508 | memmove (loc + runlen, loc, sortlen); | |
8509 | memcpy (loc, buf, runlen); | |
8510 | } | |
8511 | else | |
8512 | { | |
8513 | memcpy (buf, loc, sortlen); | |
8514 | memmove (loc, p, runlen); | |
8515 | memcpy (loc + runlen, buf, sortlen); | |
8516 | } | |
b29b8669 | 8517 | p += runlen - elt_size; |
0e287786 AM |
8518 | } |
8519 | } | |
8520 | /* Hashes are no longer valid. */ | |
28dbcedc AM |
8521 | free (reldata->hashes); |
8522 | reldata->hashes = NULL; | |
bca6d0e3 | 8523 | free (buf); |
53df40a4 | 8524 | } |
bca6d0e3 | 8525 | return TRUE; |
c152c796 AM |
8526 | } |
8527 | ||
8528 | struct elf_link_sort_rela | |
8529 | { | |
8530 | union { | |
8531 | bfd_vma offset; | |
8532 | bfd_vma sym_mask; | |
8533 | } u; | |
8534 | enum elf_reloc_type_class type; | |
8535 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
8536 | Elf_Internal_Rela rela[1]; | |
8537 | }; | |
8538 | ||
8539 | static int | |
8540 | elf_link_sort_cmp1 (const void *A, const void *B) | |
8541 | { | |
a50b1753 NC |
8542 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8543 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 AM |
8544 | int relativea, relativeb; |
8545 | ||
8546 | relativea = a->type == reloc_class_relative; | |
8547 | relativeb = b->type == reloc_class_relative; | |
8548 | ||
8549 | if (relativea < relativeb) | |
8550 | return 1; | |
8551 | if (relativea > relativeb) | |
8552 | return -1; | |
8553 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
8554 | return -1; | |
8555 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
8556 | return 1; | |
8557 | if (a->rela->r_offset < b->rela->r_offset) | |
8558 | return -1; | |
8559 | if (a->rela->r_offset > b->rela->r_offset) | |
8560 | return 1; | |
8561 | return 0; | |
8562 | } | |
8563 | ||
8564 | static int | |
8565 | elf_link_sort_cmp2 (const void *A, const void *B) | |
8566 | { | |
a50b1753 NC |
8567 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8568 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 | 8569 | |
7e612e98 | 8570 | if (a->type < b->type) |
c152c796 | 8571 | return -1; |
7e612e98 | 8572 | if (a->type > b->type) |
c152c796 | 8573 | return 1; |
7e612e98 | 8574 | if (a->u.offset < b->u.offset) |
c152c796 | 8575 | return -1; |
7e612e98 | 8576 | if (a->u.offset > b->u.offset) |
c152c796 AM |
8577 | return 1; |
8578 | if (a->rela->r_offset < b->rela->r_offset) | |
8579 | return -1; | |
8580 | if (a->rela->r_offset > b->rela->r_offset) | |
8581 | return 1; | |
8582 | return 0; | |
8583 | } | |
8584 | ||
8585 | static size_t | |
8586 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
8587 | { | |
3410fea8 | 8588 | asection *dynamic_relocs; |
fc66a176 L |
8589 | asection *rela_dyn; |
8590 | asection *rel_dyn; | |
c152c796 AM |
8591 | bfd_size_type count, size; |
8592 | size_t i, ret, sort_elt, ext_size; | |
8593 | bfd_byte *sort, *s_non_relative, *p; | |
8594 | struct elf_link_sort_rela *sq; | |
8595 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8596 | int i2e = bed->s->int_rels_per_ext_rel; | |
c8e44c6d | 8597 | unsigned int opb = bfd_octets_per_byte (abfd); |
c152c796 AM |
8598 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
8599 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8600 | struct bfd_link_order *lo; | |
8601 | bfd_vma r_sym_mask; | |
3410fea8 | 8602 | bfd_boolean use_rela; |
c152c796 | 8603 | |
3410fea8 NC |
8604 | /* Find a dynamic reloc section. */ |
8605 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
8606 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
8607 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
8608 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 8609 | { |
3410fea8 NC |
8610 | bfd_boolean use_rela_initialised = FALSE; |
8611 | ||
8612 | /* This is just here to stop gcc from complaining. | |
c8e44c6d | 8613 | Its initialization checking code is not perfect. */ |
3410fea8 NC |
8614 | use_rela = TRUE; |
8615 | ||
8616 | /* Both sections are present. Examine the sizes | |
8617 | of the indirect sections to help us choose. */ | |
8618 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8619 | if (lo->type == bfd_indirect_link_order) | |
8620 | { | |
8621 | asection *o = lo->u.indirect.section; | |
8622 | ||
8623 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8624 | { | |
8625 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8626 | /* Section size is divisible by both rel and rela sizes. | |
8627 | It is of no help to us. */ | |
8628 | ; | |
8629 | else | |
8630 | { | |
8631 | /* Section size is only divisible by rela. */ | |
8632 | if (use_rela_initialised && (use_rela == FALSE)) | |
8633 | { | |
c8e44c6d AM |
8634 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8635 | "they are in more than one size"), | |
8636 | abfd); | |
3410fea8 NC |
8637 | bfd_set_error (bfd_error_invalid_operation); |
8638 | return 0; | |
8639 | } | |
8640 | else | |
8641 | { | |
8642 | use_rela = TRUE; | |
8643 | use_rela_initialised = TRUE; | |
8644 | } | |
8645 | } | |
8646 | } | |
8647 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
8648 | { | |
8649 | /* Section size is only divisible by rel. */ | |
8650 | if (use_rela_initialised && (use_rela == TRUE)) | |
8651 | { | |
c8e44c6d AM |
8652 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8653 | "they are in more than one size"), | |
8654 | abfd); | |
3410fea8 NC |
8655 | bfd_set_error (bfd_error_invalid_operation); |
8656 | return 0; | |
8657 | } | |
8658 | else | |
8659 | { | |
8660 | use_rela = FALSE; | |
8661 | use_rela_initialised = TRUE; | |
8662 | } | |
8663 | } | |
8664 | else | |
8665 | { | |
c8e44c6d AM |
8666 | /* The section size is not divisible by either - |
8667 | something is wrong. */ | |
8668 | _bfd_error_handler (_("%B: Unable to sort relocs - " | |
8669 | "they are of an unknown size"), abfd); | |
3410fea8 NC |
8670 | bfd_set_error (bfd_error_invalid_operation); |
8671 | return 0; | |
8672 | } | |
8673 | } | |
8674 | ||
8675 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8676 | if (lo->type == bfd_indirect_link_order) | |
8677 | { | |
8678 | asection *o = lo->u.indirect.section; | |
8679 | ||
8680 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8681 | { | |
8682 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8683 | /* Section size is divisible by both rel and rela sizes. | |
8684 | It is of no help to us. */ | |
8685 | ; | |
8686 | else | |
8687 | { | |
8688 | /* Section size is only divisible by rela. */ | |
8689 | if (use_rela_initialised && (use_rela == FALSE)) | |
8690 | { | |
c8e44c6d AM |
8691 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8692 | "they are in more than one size"), | |
8693 | abfd); | |
3410fea8 NC |
8694 | bfd_set_error (bfd_error_invalid_operation); |
8695 | return 0; | |
8696 | } | |
8697 | else | |
8698 | { | |
8699 | use_rela = TRUE; | |
8700 | use_rela_initialised = TRUE; | |
8701 | } | |
8702 | } | |
8703 | } | |
8704 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
8705 | { | |
8706 | /* Section size is only divisible by rel. */ | |
8707 | if (use_rela_initialised && (use_rela == TRUE)) | |
8708 | { | |
c8e44c6d AM |
8709 | _bfd_error_handler (_("%B: Unable to sort relocs - " |
8710 | "they are in more than one size"), | |
8711 | abfd); | |
3410fea8 NC |
8712 | bfd_set_error (bfd_error_invalid_operation); |
8713 | return 0; | |
8714 | } | |
8715 | else | |
8716 | { | |
8717 | use_rela = FALSE; | |
8718 | use_rela_initialised = TRUE; | |
8719 | } | |
8720 | } | |
8721 | else | |
8722 | { | |
c8e44c6d AM |
8723 | /* The section size is not divisible by either - |
8724 | something is wrong. */ | |
8725 | _bfd_error_handler (_("%B: Unable to sort relocs - " | |
8726 | "they are of an unknown size"), abfd); | |
3410fea8 NC |
8727 | bfd_set_error (bfd_error_invalid_operation); |
8728 | return 0; | |
8729 | } | |
8730 | } | |
8731 | ||
8732 | if (! use_rela_initialised) | |
8733 | /* Make a guess. */ | |
8734 | use_rela = TRUE; | |
c152c796 | 8735 | } |
fc66a176 L |
8736 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
8737 | use_rela = TRUE; | |
8738 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 8739 | use_rela = FALSE; |
c152c796 | 8740 | else |
fc66a176 | 8741 | return 0; |
3410fea8 NC |
8742 | |
8743 | if (use_rela) | |
c152c796 | 8744 | { |
3410fea8 | 8745 | dynamic_relocs = rela_dyn; |
c152c796 AM |
8746 | ext_size = bed->s->sizeof_rela; |
8747 | swap_in = bed->s->swap_reloca_in; | |
8748 | swap_out = bed->s->swap_reloca_out; | |
8749 | } | |
3410fea8 NC |
8750 | else |
8751 | { | |
8752 | dynamic_relocs = rel_dyn; | |
8753 | ext_size = bed->s->sizeof_rel; | |
8754 | swap_in = bed->s->swap_reloc_in; | |
8755 | swap_out = bed->s->swap_reloc_out; | |
8756 | } | |
c152c796 AM |
8757 | |
8758 | size = 0; | |
3410fea8 | 8759 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 8760 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 8761 | size += lo->u.indirect.section->size; |
c152c796 | 8762 | |
3410fea8 | 8763 | if (size != dynamic_relocs->size) |
c152c796 AM |
8764 | return 0; |
8765 | ||
8766 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
8767 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
8768 | |
8769 | count = dynamic_relocs->size / ext_size; | |
5e486aa1 NC |
8770 | if (count == 0) |
8771 | return 0; | |
a50b1753 | 8772 | sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); |
3410fea8 | 8773 | |
c152c796 AM |
8774 | if (sort == NULL) |
8775 | { | |
8776 | (*info->callbacks->warning) | |
8777 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
8778 | return 0; | |
8779 | } | |
8780 | ||
8781 | if (bed->s->arch_size == 32) | |
8782 | r_sym_mask = ~(bfd_vma) 0xff; | |
8783 | else | |
8784 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
8785 | ||
3410fea8 | 8786 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
8787 | if (lo->type == bfd_indirect_link_order) |
8788 | { | |
8789 | bfd_byte *erel, *erelend; | |
8790 | asection *o = lo->u.indirect.section; | |
8791 | ||
1da212d6 AM |
8792 | if (o->contents == NULL && o->size != 0) |
8793 | { | |
8794 | /* This is a reloc section that is being handled as a normal | |
8795 | section. See bfd_section_from_shdr. We can't combine | |
8796 | relocs in this case. */ | |
8797 | free (sort); | |
8798 | return 0; | |
8799 | } | |
c152c796 | 8800 | erel = o->contents; |
eea6121a | 8801 | erelend = o->contents + o->size; |
c8e44c6d | 8802 | p = sort + o->output_offset * opb / ext_size * sort_elt; |
3410fea8 | 8803 | |
c152c796 AM |
8804 | while (erel < erelend) |
8805 | { | |
8806 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 8807 | |
c152c796 | 8808 | (*swap_in) (abfd, erel, s->rela); |
7e612e98 | 8809 | s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); |
c152c796 AM |
8810 | s->u.sym_mask = r_sym_mask; |
8811 | p += sort_elt; | |
8812 | erel += ext_size; | |
8813 | } | |
8814 | } | |
8815 | ||
8816 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
8817 | ||
8818 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
8819 | { | |
8820 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
8821 | if (s->type != reloc_class_relative) | |
8822 | break; | |
8823 | } | |
8824 | ret = i; | |
8825 | s_non_relative = p; | |
8826 | ||
8827 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
8828 | for (; i < count; i++, p += sort_elt) | |
8829 | { | |
8830 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
8831 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
8832 | sq = sp; | |
8833 | sp->u.offset = sq->rela->r_offset; | |
8834 | } | |
8835 | ||
8836 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
8837 | ||
c8e44c6d AM |
8838 | struct elf_link_hash_table *htab = elf_hash_table (info); |
8839 | if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) | |
8840 | { | |
8841 | /* We have plt relocs in .rela.dyn. */ | |
8842 | sq = (struct elf_link_sort_rela *) sort; | |
8843 | for (i = 0; i < count; i++) | |
8844 | if (sq[count - i - 1].type != reloc_class_plt) | |
8845 | break; | |
8846 | if (i != 0 && htab->srelplt->size == i * ext_size) | |
8847 | { | |
8848 | struct bfd_link_order **plo; | |
8849 | /* Put srelplt link_order last. This is so the output_offset | |
8850 | set in the next loop is correct for DT_JMPREL. */ | |
8851 | for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) | |
8852 | if ((*plo)->type == bfd_indirect_link_order | |
8853 | && (*plo)->u.indirect.section == htab->srelplt) | |
8854 | { | |
8855 | lo = *plo; | |
8856 | *plo = lo->next; | |
8857 | } | |
8858 | else | |
8859 | plo = &(*plo)->next; | |
8860 | *plo = lo; | |
8861 | lo->next = NULL; | |
8862 | dynamic_relocs->map_tail.link_order = lo; | |
8863 | } | |
8864 | } | |
8865 | ||
8866 | p = sort; | |
3410fea8 | 8867 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
8868 | if (lo->type == bfd_indirect_link_order) |
8869 | { | |
8870 | bfd_byte *erel, *erelend; | |
8871 | asection *o = lo->u.indirect.section; | |
8872 | ||
8873 | erel = o->contents; | |
eea6121a | 8874 | erelend = o->contents + o->size; |
c8e44c6d | 8875 | o->output_offset = (p - sort) / sort_elt * ext_size / opb; |
c152c796 AM |
8876 | while (erel < erelend) |
8877 | { | |
8878 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
8879 | (*swap_out) (abfd, s->rela, erel); | |
8880 | p += sort_elt; | |
8881 | erel += ext_size; | |
8882 | } | |
8883 | } | |
8884 | ||
8885 | free (sort); | |
3410fea8 | 8886 | *psec = dynamic_relocs; |
c152c796 AM |
8887 | return ret; |
8888 | } | |
8889 | ||
ef10c3ac | 8890 | /* Add a symbol to the output symbol string table. */ |
c152c796 | 8891 | |
6e0b88f1 | 8892 | static int |
ef10c3ac L |
8893 | elf_link_output_symstrtab (struct elf_final_link_info *flinfo, |
8894 | const char *name, | |
8895 | Elf_Internal_Sym *elfsym, | |
8896 | asection *input_sec, | |
8897 | struct elf_link_hash_entry *h) | |
c152c796 | 8898 | { |
6e0b88f1 | 8899 | int (*output_symbol_hook) |
c152c796 AM |
8900 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, |
8901 | struct elf_link_hash_entry *); | |
ef10c3ac | 8902 | struct elf_link_hash_table *hash_table; |
c152c796 | 8903 | const struct elf_backend_data *bed; |
ef10c3ac | 8904 | bfd_size_type strtabsize; |
c152c796 | 8905 | |
8539e4e8 AM |
8906 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); |
8907 | ||
8b127cbc | 8908 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 AM |
8909 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; |
8910 | if (output_symbol_hook != NULL) | |
8911 | { | |
8b127cbc | 8912 | int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); |
6e0b88f1 AM |
8913 | if (ret != 1) |
8914 | return ret; | |
c152c796 AM |
8915 | } |
8916 | ||
ef10c3ac L |
8917 | if (name == NULL |
8918 | || *name == '\0' | |
8919 | || (input_sec->flags & SEC_EXCLUDE)) | |
8920 | elfsym->st_name = (unsigned long) -1; | |
c152c796 AM |
8921 | else |
8922 | { | |
ef10c3ac L |
8923 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
8924 | to get the final offset for st_name. */ | |
8925 | elfsym->st_name | |
8926 | = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, | |
8927 | name, FALSE); | |
c152c796 | 8928 | if (elfsym->st_name == (unsigned long) -1) |
6e0b88f1 | 8929 | return 0; |
c152c796 AM |
8930 | } |
8931 | ||
ef10c3ac L |
8932 | hash_table = elf_hash_table (flinfo->info); |
8933 | strtabsize = hash_table->strtabsize; | |
8934 | if (strtabsize <= hash_table->strtabcount) | |
c152c796 | 8935 | { |
ef10c3ac L |
8936 | strtabsize += strtabsize; |
8937 | hash_table->strtabsize = strtabsize; | |
8938 | strtabsize *= sizeof (*hash_table->strtab); | |
8939 | hash_table->strtab | |
8940 | = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, | |
8941 | strtabsize); | |
8942 | if (hash_table->strtab == NULL) | |
6e0b88f1 | 8943 | return 0; |
c152c796 | 8944 | } |
ef10c3ac L |
8945 | hash_table->strtab[hash_table->strtabcount].sym = *elfsym; |
8946 | hash_table->strtab[hash_table->strtabcount].dest_index | |
8947 | = hash_table->strtabcount; | |
8948 | hash_table->strtab[hash_table->strtabcount].destshndx_index | |
8949 | = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0; | |
8950 | ||
8951 | bfd_get_symcount (flinfo->output_bfd) += 1; | |
8952 | hash_table->strtabcount += 1; | |
8953 | ||
8954 | return 1; | |
8955 | } | |
8956 | ||
8957 | /* Swap symbols out to the symbol table and flush the output symbols to | |
8958 | the file. */ | |
8959 | ||
8960 | static bfd_boolean | |
8961 | elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) | |
8962 | { | |
8963 | struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); | |
ef53be89 AM |
8964 | bfd_size_type amt; |
8965 | size_t i; | |
ef10c3ac L |
8966 | const struct elf_backend_data *bed; |
8967 | bfd_byte *symbuf; | |
8968 | Elf_Internal_Shdr *hdr; | |
8969 | file_ptr pos; | |
8970 | bfd_boolean ret; | |
8971 | ||
8972 | if (!hash_table->strtabcount) | |
8973 | return TRUE; | |
8974 | ||
8975 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); | |
8976 | ||
8977 | bed = get_elf_backend_data (flinfo->output_bfd); | |
c152c796 | 8978 | |
ef10c3ac L |
8979 | amt = bed->s->sizeof_sym * hash_table->strtabcount; |
8980 | symbuf = (bfd_byte *) bfd_malloc (amt); | |
8981 | if (symbuf == NULL) | |
8982 | return FALSE; | |
1b786873 | 8983 | |
ef10c3ac | 8984 | if (flinfo->symshndxbuf) |
c152c796 | 8985 | { |
ef53be89 AM |
8986 | amt = sizeof (Elf_External_Sym_Shndx); |
8987 | amt *= bfd_get_symcount (flinfo->output_bfd); | |
ef10c3ac L |
8988 | flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); |
8989 | if (flinfo->symshndxbuf == NULL) | |
c152c796 | 8990 | { |
ef10c3ac L |
8991 | free (symbuf); |
8992 | return FALSE; | |
c152c796 | 8993 | } |
c152c796 AM |
8994 | } |
8995 | ||
ef10c3ac L |
8996 | for (i = 0; i < hash_table->strtabcount; i++) |
8997 | { | |
8998 | struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; | |
8999 | if (elfsym->sym.st_name == (unsigned long) -1) | |
9000 | elfsym->sym.st_name = 0; | |
9001 | else | |
9002 | elfsym->sym.st_name | |
9003 | = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, | |
9004 | elfsym->sym.st_name); | |
9005 | bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, | |
9006 | ((bfd_byte *) symbuf | |
9007 | + (elfsym->dest_index | |
9008 | * bed->s->sizeof_sym)), | |
9009 | (flinfo->symshndxbuf | |
9010 | + elfsym->destshndx_index)); | |
9011 | } | |
9012 | ||
9013 | hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; | |
9014 | pos = hdr->sh_offset + hdr->sh_size; | |
9015 | amt = hash_table->strtabcount * bed->s->sizeof_sym; | |
9016 | if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 | |
9017 | && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt) | |
9018 | { | |
9019 | hdr->sh_size += amt; | |
9020 | ret = TRUE; | |
9021 | } | |
9022 | else | |
9023 | ret = FALSE; | |
c152c796 | 9024 | |
ef10c3ac L |
9025 | free (symbuf); |
9026 | ||
9027 | free (hash_table->strtab); | |
9028 | hash_table->strtab = NULL; | |
9029 | ||
9030 | return ret; | |
c152c796 AM |
9031 | } |
9032 | ||
c0d5a53d L |
9033 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
9034 | ||
9035 | static bfd_boolean | |
9036 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
9037 | { | |
4fbb74a6 AM |
9038 | if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) |
9039 | && sym->st_shndx < SHN_LORESERVE) | |
c0d5a53d L |
9040 | { |
9041 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 9042 | beyond 64k. */ |
c0d5a53d L |
9043 | (*_bfd_error_handler) |
9044 | (_("%B: Too many sections: %d (>= %d)"), | |
4fbb74a6 | 9045 | abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); |
c0d5a53d L |
9046 | bfd_set_error (bfd_error_nonrepresentable_section); |
9047 | return FALSE; | |
9048 | } | |
9049 | return TRUE; | |
9050 | } | |
9051 | ||
c152c796 AM |
9052 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
9053 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
9054 | versioned symbol that would normally require an explicit version. | |
9055 | We also handle the case that a DSO references a hidden symbol | |
9056 | which may be satisfied by a versioned symbol in another DSO. */ | |
9057 | ||
9058 | static bfd_boolean | |
9059 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
9060 | const struct elf_backend_data *bed, | |
9061 | struct elf_link_hash_entry *h) | |
9062 | { | |
9063 | bfd *abfd; | |
9064 | struct elf_link_loaded_list *loaded; | |
9065 | ||
9066 | if (!is_elf_hash_table (info->hash)) | |
9067 | return FALSE; | |
9068 | ||
90c984fc L |
9069 | /* Check indirect symbol. */ |
9070 | while (h->root.type == bfd_link_hash_indirect) | |
9071 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9072 | ||
c152c796 AM |
9073 | switch (h->root.type) |
9074 | { | |
9075 | default: | |
9076 | abfd = NULL; | |
9077 | break; | |
9078 | ||
9079 | case bfd_link_hash_undefined: | |
9080 | case bfd_link_hash_undefweak: | |
9081 | abfd = h->root.u.undef.abfd; | |
f4ab0e2d L |
9082 | if (abfd == NULL |
9083 | || (abfd->flags & DYNAMIC) == 0 | |
e56f61be | 9084 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
9085 | return FALSE; |
9086 | break; | |
9087 | ||
9088 | case bfd_link_hash_defined: | |
9089 | case bfd_link_hash_defweak: | |
9090 | abfd = h->root.u.def.section->owner; | |
9091 | break; | |
9092 | ||
9093 | case bfd_link_hash_common: | |
9094 | abfd = h->root.u.c.p->section->owner; | |
9095 | break; | |
9096 | } | |
9097 | BFD_ASSERT (abfd != NULL); | |
9098 | ||
9099 | for (loaded = elf_hash_table (info)->loaded; | |
9100 | loaded != NULL; | |
9101 | loaded = loaded->next) | |
9102 | { | |
9103 | bfd *input; | |
9104 | Elf_Internal_Shdr *hdr; | |
ef53be89 AM |
9105 | size_t symcount; |
9106 | size_t extsymcount; | |
9107 | size_t extsymoff; | |
c152c796 AM |
9108 | Elf_Internal_Shdr *versymhdr; |
9109 | Elf_Internal_Sym *isym; | |
9110 | Elf_Internal_Sym *isymend; | |
9111 | Elf_Internal_Sym *isymbuf; | |
9112 | Elf_External_Versym *ever; | |
9113 | Elf_External_Versym *extversym; | |
9114 | ||
9115 | input = loaded->abfd; | |
9116 | ||
9117 | /* We check each DSO for a possible hidden versioned definition. */ | |
9118 | if (input == abfd | |
9119 | || (input->flags & DYNAMIC) == 0 | |
9120 | || elf_dynversym (input) == 0) | |
9121 | continue; | |
9122 | ||
9123 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
9124 | ||
9125 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
9126 | if (elf_bad_symtab (input)) | |
9127 | { | |
9128 | extsymcount = symcount; | |
9129 | extsymoff = 0; | |
9130 | } | |
9131 | else | |
9132 | { | |
9133 | extsymcount = symcount - hdr->sh_info; | |
9134 | extsymoff = hdr->sh_info; | |
9135 | } | |
9136 | ||
9137 | if (extsymcount == 0) | |
9138 | continue; | |
9139 | ||
9140 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
9141 | NULL, NULL, NULL); | |
9142 | if (isymbuf == NULL) | |
9143 | return FALSE; | |
9144 | ||
9145 | /* Read in any version definitions. */ | |
9146 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
a50b1753 | 9147 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
c152c796 AM |
9148 | if (extversym == NULL) |
9149 | goto error_ret; | |
9150 | ||
9151 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
9152 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
9153 | != versymhdr->sh_size)) | |
9154 | { | |
9155 | free (extversym); | |
9156 | error_ret: | |
9157 | free (isymbuf); | |
9158 | return FALSE; | |
9159 | } | |
9160 | ||
9161 | ever = extversym + extsymoff; | |
9162 | isymend = isymbuf + extsymcount; | |
9163 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
9164 | { | |
9165 | const char *name; | |
9166 | Elf_Internal_Versym iver; | |
9167 | unsigned short version_index; | |
9168 | ||
9169 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
9170 | || isym->st_shndx == SHN_UNDEF) | |
9171 | continue; | |
9172 | ||
9173 | name = bfd_elf_string_from_elf_section (input, | |
9174 | hdr->sh_link, | |
9175 | isym->st_name); | |
9176 | if (strcmp (name, h->root.root.string) != 0) | |
9177 | continue; | |
9178 | ||
9179 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
9180 | ||
d023c380 L |
9181 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 |
9182 | && !(h->def_regular | |
9183 | && h->forced_local)) | |
c152c796 AM |
9184 | { |
9185 | /* If we have a non-hidden versioned sym, then it should | |
d023c380 L |
9186 | have provided a definition for the undefined sym unless |
9187 | it is defined in a non-shared object and forced local. | |
9188 | */ | |
c152c796 AM |
9189 | abort (); |
9190 | } | |
9191 | ||
9192 | version_index = iver.vs_vers & VERSYM_VERSION; | |
9193 | if (version_index == 1 || version_index == 2) | |
9194 | { | |
9195 | /* This is the base or first version. We can use it. */ | |
9196 | free (extversym); | |
9197 | free (isymbuf); | |
9198 | return TRUE; | |
9199 | } | |
9200 | } | |
9201 | ||
9202 | free (extversym); | |
9203 | free (isymbuf); | |
9204 | } | |
9205 | ||
9206 | return FALSE; | |
9207 | } | |
9208 | ||
b8871f35 L |
9209 | /* Convert ELF common symbol TYPE. */ |
9210 | ||
9211 | static int | |
9212 | elf_link_convert_common_type (struct bfd_link_info *info, int type) | |
9213 | { | |
9214 | /* Commom symbol can only appear in relocatable link. */ | |
9215 | if (!bfd_link_relocatable (info)) | |
9216 | abort (); | |
9217 | switch (info->elf_stt_common) | |
9218 | { | |
9219 | case unchanged: | |
9220 | break; | |
9221 | case elf_stt_common: | |
9222 | type = STT_COMMON; | |
9223 | break; | |
9224 | case no_elf_stt_common: | |
9225 | type = STT_OBJECT; | |
9226 | break; | |
9227 | } | |
9228 | return type; | |
9229 | } | |
9230 | ||
c152c796 AM |
9231 | /* Add an external symbol to the symbol table. This is called from |
9232 | the hash table traversal routine. When generating a shared object, | |
9233 | we go through the symbol table twice. The first time we output | |
9234 | anything that might have been forced to local scope in a version | |
9235 | script. The second time we output the symbols that are still | |
9236 | global symbols. */ | |
9237 | ||
9238 | static bfd_boolean | |
7686d77d | 9239 | elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) |
c152c796 | 9240 | { |
7686d77d | 9241 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; |
a50b1753 | 9242 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
8b127cbc | 9243 | struct elf_final_link_info *flinfo = eoinfo->flinfo; |
c152c796 AM |
9244 | bfd_boolean strip; |
9245 | Elf_Internal_Sym sym; | |
9246 | asection *input_sec; | |
9247 | const struct elf_backend_data *bed; | |
6e0b88f1 AM |
9248 | long indx; |
9249 | int ret; | |
b8871f35 | 9250 | unsigned int type; |
6e33951e L |
9251 | /* A symbol is bound locally if it is forced local or it is locally |
9252 | defined, hidden versioned, not referenced by shared library and | |
9253 | not exported when linking executable. */ | |
9254 | bfd_boolean local_bind = (h->forced_local | |
0e1862bb | 9255 | || (bfd_link_executable (flinfo->info) |
6e33951e L |
9256 | && !flinfo->info->export_dynamic |
9257 | && !h->dynamic | |
9258 | && !h->ref_dynamic | |
9259 | && h->def_regular | |
422f1182 | 9260 | && h->versioned == versioned_hidden)); |
c152c796 AM |
9261 | |
9262 | if (h->root.type == bfd_link_hash_warning) | |
9263 | { | |
9264 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9265 | if (h->root.type == bfd_link_hash_new) | |
9266 | return TRUE; | |
9267 | } | |
9268 | ||
9269 | /* Decide whether to output this symbol in this pass. */ | |
9270 | if (eoinfo->localsyms) | |
9271 | { | |
6e33951e | 9272 | if (!local_bind) |
c152c796 AM |
9273 | return TRUE; |
9274 | } | |
9275 | else | |
9276 | { | |
6e33951e | 9277 | if (local_bind) |
c152c796 AM |
9278 | return TRUE; |
9279 | } | |
9280 | ||
8b127cbc | 9281 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 | 9282 | |
12ac1cf5 | 9283 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 9284 | { |
12ac1cf5 NC |
9285 | /* If we have an undefined symbol reference here then it must have |
9286 | come from a shared library that is being linked in. (Undefined | |
98da7939 L |
9287 | references in regular files have already been handled unless |
9288 | they are in unreferenced sections which are removed by garbage | |
9289 | collection). */ | |
12ac1cf5 NC |
9290 | bfd_boolean ignore_undef = FALSE; |
9291 | ||
9292 | /* Some symbols may be special in that the fact that they're | |
9293 | undefined can be safely ignored - let backend determine that. */ | |
9294 | if (bed->elf_backend_ignore_undef_symbol) | |
9295 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
9296 | ||
9297 | /* If we are reporting errors for this situation then do so now. */ | |
89a2ee5a | 9298 | if (!ignore_undef |
12ac1cf5 | 9299 | && h->ref_dynamic |
8b127cbc AM |
9300 | && (!h->ref_regular || flinfo->info->gc_sections) |
9301 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h) | |
9302 | && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
1a72702b AM |
9303 | (*flinfo->info->callbacks->undefined_symbol) |
9304 | (flinfo->info, h->root.root.string, | |
9305 | h->ref_regular ? NULL : h->root.u.undef.abfd, | |
9306 | NULL, 0, | |
9307 | flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR); | |
97196564 L |
9308 | |
9309 | /* Strip a global symbol defined in a discarded section. */ | |
9310 | if (h->indx == -3) | |
9311 | return TRUE; | |
c152c796 AM |
9312 | } |
9313 | ||
9314 | /* We should also warn if a forced local symbol is referenced from | |
9315 | shared libraries. */ | |
0e1862bb | 9316 | if (bfd_link_executable (flinfo->info) |
f5385ebf AM |
9317 | && h->forced_local |
9318 | && h->ref_dynamic | |
371a5866 | 9319 | && h->def_regular |
f5385ebf | 9320 | && !h->dynamic_def |
ee659f1f | 9321 | && h->ref_dynamic_nonweak |
8b127cbc | 9322 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) |
c152c796 | 9323 | { |
17d078c5 AM |
9324 | bfd *def_bfd; |
9325 | const char *msg; | |
90c984fc L |
9326 | struct elf_link_hash_entry *hi = h; |
9327 | ||
9328 | /* Check indirect symbol. */ | |
9329 | while (hi->root.type == bfd_link_hash_indirect) | |
9330 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
17d078c5 AM |
9331 | |
9332 | if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
9333 | msg = _("%B: internal symbol `%s' in %B is referenced by DSO"); | |
9334 | else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) | |
9335 | msg = _("%B: hidden symbol `%s' in %B is referenced by DSO"); | |
9336 | else | |
9337 | msg = _("%B: local symbol `%s' in %B is referenced by DSO"); | |
8b127cbc | 9338 | def_bfd = flinfo->output_bfd; |
90c984fc L |
9339 | if (hi->root.u.def.section != bfd_abs_section_ptr) |
9340 | def_bfd = hi->root.u.def.section->owner; | |
8b127cbc | 9341 | (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd, |
17d078c5 AM |
9342 | h->root.root.string); |
9343 | bfd_set_error (bfd_error_bad_value); | |
c152c796 AM |
9344 | eoinfo->failed = TRUE; |
9345 | return FALSE; | |
9346 | } | |
9347 | ||
9348 | /* We don't want to output symbols that have never been mentioned by | |
9349 | a regular file, or that we have been told to strip. However, if | |
9350 | h->indx is set to -2, the symbol is used by a reloc and we must | |
9351 | output it. */ | |
d983c8c5 | 9352 | strip = FALSE; |
c152c796 | 9353 | if (h->indx == -2) |
d983c8c5 | 9354 | ; |
f5385ebf | 9355 | else if ((h->def_dynamic |
77cfaee6 AM |
9356 | || h->ref_dynamic |
9357 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
9358 | && !h->def_regular |
9359 | && !h->ref_regular) | |
c152c796 | 9360 | strip = TRUE; |
8b127cbc | 9361 | else if (flinfo->info->strip == strip_all) |
c152c796 | 9362 | strip = TRUE; |
8b127cbc AM |
9363 | else if (flinfo->info->strip == strip_some |
9364 | && bfd_hash_lookup (flinfo->info->keep_hash, | |
c152c796 AM |
9365 | h->root.root.string, FALSE, FALSE) == NULL) |
9366 | strip = TRUE; | |
d56d55e7 AM |
9367 | else if ((h->root.type == bfd_link_hash_defined |
9368 | || h->root.type == bfd_link_hash_defweak) | |
8b127cbc | 9369 | && ((flinfo->info->strip_discarded |
dbaa2011 | 9370 | && discarded_section (h->root.u.def.section)) |
ca4be51c AM |
9371 | || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 |
9372 | && h->root.u.def.section->owner != NULL | |
d56d55e7 | 9373 | && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) |
c152c796 | 9374 | strip = TRUE; |
9e2278f5 AM |
9375 | else if ((h->root.type == bfd_link_hash_undefined |
9376 | || h->root.type == bfd_link_hash_undefweak) | |
9377 | && h->root.u.undef.abfd != NULL | |
9378 | && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) | |
9379 | strip = TRUE; | |
c152c796 | 9380 | |
b8871f35 L |
9381 | type = h->type; |
9382 | ||
c152c796 | 9383 | /* If we're stripping it, and it's not a dynamic symbol, there's |
d983c8c5 AM |
9384 | nothing else to do. However, if it is a forced local symbol or |
9385 | an ifunc symbol we need to give the backend finish_dynamic_symbol | |
9386 | function a chance to make it dynamic. */ | |
c152c796 AM |
9387 | if (strip |
9388 | && h->dynindx == -1 | |
b8871f35 | 9389 | && type != STT_GNU_IFUNC |
f5385ebf | 9390 | && !h->forced_local) |
c152c796 AM |
9391 | return TRUE; |
9392 | ||
9393 | sym.st_value = 0; | |
9394 | sym.st_size = h->size; | |
9395 | sym.st_other = h->other; | |
c152c796 AM |
9396 | switch (h->root.type) |
9397 | { | |
9398 | default: | |
9399 | case bfd_link_hash_new: | |
9400 | case bfd_link_hash_warning: | |
9401 | abort (); | |
9402 | return FALSE; | |
9403 | ||
9404 | case bfd_link_hash_undefined: | |
9405 | case bfd_link_hash_undefweak: | |
9406 | input_sec = bfd_und_section_ptr; | |
9407 | sym.st_shndx = SHN_UNDEF; | |
9408 | break; | |
9409 | ||
9410 | case bfd_link_hash_defined: | |
9411 | case bfd_link_hash_defweak: | |
9412 | { | |
9413 | input_sec = h->root.u.def.section; | |
9414 | if (input_sec->output_section != NULL) | |
9415 | { | |
9416 | sym.st_shndx = | |
8b127cbc | 9417 | _bfd_elf_section_from_bfd_section (flinfo->output_bfd, |
c152c796 AM |
9418 | input_sec->output_section); |
9419 | if (sym.st_shndx == SHN_BAD) | |
9420 | { | |
9421 | (*_bfd_error_handler) | |
d003868e | 9422 | (_("%B: could not find output section %A for input section %A"), |
8b127cbc | 9423 | flinfo->output_bfd, input_sec->output_section, input_sec); |
17d078c5 | 9424 | bfd_set_error (bfd_error_nonrepresentable_section); |
c152c796 AM |
9425 | eoinfo->failed = TRUE; |
9426 | return FALSE; | |
9427 | } | |
9428 | ||
9429 | /* ELF symbols in relocatable files are section relative, | |
9430 | but in nonrelocatable files they are virtual | |
9431 | addresses. */ | |
9432 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
0e1862bb | 9433 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
9434 | { |
9435 | sym.st_value += input_sec->output_section->vma; | |
9436 | if (h->type == STT_TLS) | |
9437 | { | |
8b127cbc | 9438 | asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; |
430a16a5 NC |
9439 | if (tls_sec != NULL) |
9440 | sym.st_value -= tls_sec->vma; | |
c152c796 AM |
9441 | } |
9442 | } | |
9443 | } | |
9444 | else | |
9445 | { | |
9446 | BFD_ASSERT (input_sec->owner == NULL | |
9447 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
9448 | sym.st_shndx = SHN_UNDEF; | |
9449 | input_sec = bfd_und_section_ptr; | |
9450 | } | |
9451 | } | |
9452 | break; | |
9453 | ||
9454 | case bfd_link_hash_common: | |
9455 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 9456 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
9457 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
9458 | break; | |
9459 | ||
9460 | case bfd_link_hash_indirect: | |
9461 | /* These symbols are created by symbol versioning. They point | |
9462 | to the decorated version of the name. For example, if the | |
9463 | symbol foo@@GNU_1.2 is the default, which should be used when | |
9464 | foo is used with no version, then we add an indirect symbol | |
9465 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
9466 | since the indirected symbol is already in the hash table. */ | |
9467 | return TRUE; | |
9468 | } | |
9469 | ||
b8871f35 L |
9470 | if (type == STT_COMMON || type == STT_OBJECT) |
9471 | switch (h->root.type) | |
9472 | { | |
9473 | case bfd_link_hash_common: | |
9474 | type = elf_link_convert_common_type (flinfo->info, type); | |
9475 | break; | |
9476 | case bfd_link_hash_defined: | |
9477 | case bfd_link_hash_defweak: | |
9478 | if (bed->common_definition (&sym)) | |
9479 | type = elf_link_convert_common_type (flinfo->info, type); | |
9480 | else | |
9481 | type = STT_OBJECT; | |
9482 | break; | |
9483 | case bfd_link_hash_undefined: | |
9484 | case bfd_link_hash_undefweak: | |
9485 | break; | |
9486 | default: | |
9487 | abort (); | |
9488 | } | |
9489 | ||
9490 | if (local_bind) | |
9491 | { | |
9492 | sym.st_info = ELF_ST_INFO (STB_LOCAL, type); | |
9493 | /* Turn off visibility on local symbol. */ | |
9494 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); | |
9495 | } | |
9496 | /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ | |
9497 | else if (h->unique_global && h->def_regular) | |
9498 | sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); | |
9499 | else if (h->root.type == bfd_link_hash_undefweak | |
9500 | || h->root.type == bfd_link_hash_defweak) | |
9501 | sym.st_info = ELF_ST_INFO (STB_WEAK, type); | |
9502 | else | |
9503 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
9504 | sym.st_target_internal = h->target_internal; | |
9505 | ||
c152c796 AM |
9506 | /* Give the processor backend a chance to tweak the symbol value, |
9507 | and also to finish up anything that needs to be done for this | |
9508 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
3aa14d16 | 9509 | forced local syms when non-shared is due to a historical quirk. |
5f35ea9c | 9510 | STT_GNU_IFUNC symbol must go through PLT. */ |
3aa14d16 | 9511 | if ((h->type == STT_GNU_IFUNC |
5f35ea9c | 9512 | && h->def_regular |
0e1862bb | 9513 | && !bfd_link_relocatable (flinfo->info)) |
3aa14d16 L |
9514 | || ((h->dynindx != -1 |
9515 | || h->forced_local) | |
0e1862bb | 9516 | && ((bfd_link_pic (flinfo->info) |
3aa14d16 L |
9517 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
9518 | || h->root.type != bfd_link_hash_undefweak)) | |
9519 | || !h->forced_local) | |
8b127cbc | 9520 | && elf_hash_table (flinfo->info)->dynamic_sections_created)) |
c152c796 AM |
9521 | { |
9522 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8b127cbc | 9523 | (flinfo->output_bfd, flinfo->info, h, &sym))) |
c152c796 AM |
9524 | { |
9525 | eoinfo->failed = TRUE; | |
9526 | return FALSE; | |
9527 | } | |
9528 | } | |
9529 | ||
9530 | /* If we are marking the symbol as undefined, and there are no | |
9531 | non-weak references to this symbol from a regular object, then | |
9532 | mark the symbol as weak undefined; if there are non-weak | |
9533 | references, mark the symbol as strong. We can't do this earlier, | |
9534 | because it might not be marked as undefined until the | |
9535 | finish_dynamic_symbol routine gets through with it. */ | |
9536 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 9537 | && h->ref_regular |
c152c796 AM |
9538 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
9539 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
9540 | { | |
9541 | int bindtype; | |
b8871f35 | 9542 | type = ELF_ST_TYPE (sym.st_info); |
2955ec4c L |
9543 | |
9544 | /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ | |
9545 | if (type == STT_GNU_IFUNC) | |
9546 | type = STT_FUNC; | |
c152c796 | 9547 | |
f5385ebf | 9548 | if (h->ref_regular_nonweak) |
c152c796 AM |
9549 | bindtype = STB_GLOBAL; |
9550 | else | |
9551 | bindtype = STB_WEAK; | |
2955ec4c | 9552 | sym.st_info = ELF_ST_INFO (bindtype, type); |
c152c796 AM |
9553 | } |
9554 | ||
bda987c2 CD |
9555 | /* If this is a symbol defined in a dynamic library, don't use the |
9556 | symbol size from the dynamic library. Relinking an executable | |
9557 | against a new library may introduce gratuitous changes in the | |
9558 | executable's symbols if we keep the size. */ | |
9559 | if (sym.st_shndx == SHN_UNDEF | |
9560 | && !h->def_regular | |
9561 | && h->def_dynamic) | |
9562 | sym.st_size = 0; | |
9563 | ||
c152c796 AM |
9564 | /* If a non-weak symbol with non-default visibility is not defined |
9565 | locally, it is a fatal error. */ | |
0e1862bb | 9566 | if (!bfd_link_relocatable (flinfo->info) |
c152c796 AM |
9567 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT |
9568 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
9569 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 9570 | && !h->def_regular) |
c152c796 | 9571 | { |
17d078c5 AM |
9572 | const char *msg; |
9573 | ||
9574 | if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) | |
9575 | msg = _("%B: protected symbol `%s' isn't defined"); | |
9576 | else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) | |
9577 | msg = _("%B: internal symbol `%s' isn't defined"); | |
9578 | else | |
9579 | msg = _("%B: hidden symbol `%s' isn't defined"); | |
8b127cbc | 9580 | (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string); |
17d078c5 | 9581 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9582 | eoinfo->failed = TRUE; |
9583 | return FALSE; | |
9584 | } | |
9585 | ||
9586 | /* If this symbol should be put in the .dynsym section, then put it | |
9587 | there now. We already know the symbol index. We also fill in | |
9588 | the entry in the .hash section. */ | |
cae1fbbb | 9589 | if (elf_hash_table (flinfo->info)->dynsym != NULL |
202e2356 | 9590 | && h->dynindx != -1 |
8b127cbc | 9591 | && elf_hash_table (flinfo->info)->dynamic_sections_created) |
c152c796 | 9592 | { |
c152c796 AM |
9593 | bfd_byte *esym; |
9594 | ||
90c984fc L |
9595 | /* Since there is no version information in the dynamic string, |
9596 | if there is no version info in symbol version section, we will | |
1659f720 | 9597 | have a run-time problem if not linking executable, referenced |
6e33951e L |
9598 | by shared library, not locally defined, or not bound locally. |
9599 | */ | |
1659f720 | 9600 | if (h->verinfo.verdef == NULL |
6e33951e | 9601 | && !local_bind |
0e1862bb | 9602 | && (!bfd_link_executable (flinfo->info) |
1659f720 L |
9603 | || h->ref_dynamic |
9604 | || !h->def_regular)) | |
90c984fc L |
9605 | { |
9606 | char *p = strrchr (h->root.root.string, ELF_VER_CHR); | |
9607 | ||
9608 | if (p && p [1] != '\0') | |
9609 | { | |
9610 | (*_bfd_error_handler) | |
9611 | (_("%B: No symbol version section for versioned symbol `%s'"), | |
9612 | flinfo->output_bfd, h->root.root.string); | |
9613 | eoinfo->failed = TRUE; | |
9614 | return FALSE; | |
9615 | } | |
9616 | } | |
9617 | ||
c152c796 | 9618 | sym.st_name = h->dynstr_index; |
cae1fbbb L |
9619 | esym = (elf_hash_table (flinfo->info)->dynsym->contents |
9620 | + h->dynindx * bed->s->sizeof_sym); | |
8b127cbc | 9621 | if (!check_dynsym (flinfo->output_bfd, &sym)) |
c0d5a53d L |
9622 | { |
9623 | eoinfo->failed = TRUE; | |
9624 | return FALSE; | |
9625 | } | |
8b127cbc | 9626 | bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); |
c152c796 | 9627 | |
8b127cbc | 9628 | if (flinfo->hash_sec != NULL) |
fdc90cb4 JJ |
9629 | { |
9630 | size_t hash_entry_size; | |
9631 | bfd_byte *bucketpos; | |
9632 | bfd_vma chain; | |
41198d0c L |
9633 | size_t bucketcount; |
9634 | size_t bucket; | |
9635 | ||
8b127cbc | 9636 | bucketcount = elf_hash_table (flinfo->info)->bucketcount; |
41198d0c | 9637 | bucket = h->u.elf_hash_value % bucketcount; |
fdc90cb4 JJ |
9638 | |
9639 | hash_entry_size | |
8b127cbc AM |
9640 | = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; |
9641 | bucketpos = ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 | 9642 | + (bucket + 2) * hash_entry_size); |
8b127cbc AM |
9643 | chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); |
9644 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, | |
9645 | bucketpos); | |
9646 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, | |
9647 | ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 JJ |
9648 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); |
9649 | } | |
c152c796 | 9650 | |
8b127cbc | 9651 | if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) |
c152c796 AM |
9652 | { |
9653 | Elf_Internal_Versym iversym; | |
9654 | Elf_External_Versym *eversym; | |
9655 | ||
f5385ebf | 9656 | if (!h->def_regular) |
c152c796 | 9657 | { |
7b20f099 AM |
9658 | if (h->verinfo.verdef == NULL |
9659 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
9660 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
c152c796 AM |
9661 | iversym.vs_vers = 0; |
9662 | else | |
9663 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
9664 | } | |
9665 | else | |
9666 | { | |
9667 | if (h->verinfo.vertree == NULL) | |
9668 | iversym.vs_vers = 1; | |
9669 | else | |
9670 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
8b127cbc | 9671 | if (flinfo->info->create_default_symver) |
3e3b46e5 | 9672 | iversym.vs_vers++; |
c152c796 AM |
9673 | } |
9674 | ||
422f1182 | 9675 | /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is |
6e33951e | 9676 | defined locally. */ |
422f1182 | 9677 | if (h->versioned == versioned_hidden && h->def_regular) |
c152c796 AM |
9678 | iversym.vs_vers |= VERSYM_HIDDEN; |
9679 | ||
8b127cbc | 9680 | eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; |
c152c796 | 9681 | eversym += h->dynindx; |
8b127cbc | 9682 | _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); |
c152c796 AM |
9683 | } |
9684 | } | |
9685 | ||
d983c8c5 AM |
9686 | /* If the symbol is undefined, and we didn't output it to .dynsym, |
9687 | strip it from .symtab too. Obviously we can't do this for | |
9688 | relocatable output or when needed for --emit-relocs. */ | |
9689 | else if (input_sec == bfd_und_section_ptr | |
9690 | && h->indx != -2 | |
0e1862bb | 9691 | && !bfd_link_relocatable (flinfo->info)) |
d983c8c5 AM |
9692 | return TRUE; |
9693 | /* Also strip others that we couldn't earlier due to dynamic symbol | |
9694 | processing. */ | |
9695 | if (strip) | |
9696 | return TRUE; | |
9697 | if ((input_sec->flags & SEC_EXCLUDE) != 0) | |
c152c796 AM |
9698 | return TRUE; |
9699 | ||
2ec55de3 AM |
9700 | /* Output a FILE symbol so that following locals are not associated |
9701 | with the wrong input file. We need one for forced local symbols | |
9702 | if we've seen more than one FILE symbol or when we have exactly | |
9703 | one FILE symbol but global symbols are present in a file other | |
9704 | than the one with the FILE symbol. We also need one if linker | |
9705 | defined symbols are present. In practice these conditions are | |
9706 | always met, so just emit the FILE symbol unconditionally. */ | |
9707 | if (eoinfo->localsyms | |
9708 | && !eoinfo->file_sym_done | |
9709 | && eoinfo->flinfo->filesym_count != 0) | |
9710 | { | |
9711 | Elf_Internal_Sym fsym; | |
9712 | ||
9713 | memset (&fsym, 0, sizeof (fsym)); | |
9714 | fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
9715 | fsym.st_shndx = SHN_ABS; | |
ef10c3ac L |
9716 | if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, |
9717 | bfd_und_section_ptr, NULL)) | |
2ec55de3 AM |
9718 | return FALSE; |
9719 | ||
9720 | eoinfo->file_sym_done = TRUE; | |
9721 | } | |
9722 | ||
8b127cbc | 9723 | indx = bfd_get_symcount (flinfo->output_bfd); |
ef10c3ac L |
9724 | ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, |
9725 | input_sec, h); | |
6e0b88f1 | 9726 | if (ret == 0) |
c152c796 AM |
9727 | { |
9728 | eoinfo->failed = TRUE; | |
9729 | return FALSE; | |
9730 | } | |
6e0b88f1 AM |
9731 | else if (ret == 1) |
9732 | h->indx = indx; | |
9733 | else if (h->indx == -2) | |
9734 | abort(); | |
c152c796 AM |
9735 | |
9736 | return TRUE; | |
9737 | } | |
9738 | ||
cdd3575c AM |
9739 | /* Return TRUE if special handling is done for relocs in SEC against |
9740 | symbols defined in discarded sections. */ | |
9741 | ||
c152c796 AM |
9742 | static bfd_boolean |
9743 | elf_section_ignore_discarded_relocs (asection *sec) | |
9744 | { | |
9745 | const struct elf_backend_data *bed; | |
9746 | ||
cdd3575c AM |
9747 | switch (sec->sec_info_type) |
9748 | { | |
dbaa2011 AM |
9749 | case SEC_INFO_TYPE_STABS: |
9750 | case SEC_INFO_TYPE_EH_FRAME: | |
2f0c68f2 | 9751 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
cdd3575c AM |
9752 | return TRUE; |
9753 | default: | |
9754 | break; | |
9755 | } | |
c152c796 AM |
9756 | |
9757 | bed = get_elf_backend_data (sec->owner); | |
9758 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
9759 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
9760 | return TRUE; | |
9761 | ||
9762 | return FALSE; | |
9763 | } | |
9764 | ||
9e66c942 AM |
9765 | /* Return a mask saying how ld should treat relocations in SEC against |
9766 | symbols defined in discarded sections. If this function returns | |
9767 | COMPLAIN set, ld will issue a warning message. If this function | |
9768 | returns PRETEND set, and the discarded section was link-once and the | |
9769 | same size as the kept link-once section, ld will pretend that the | |
9770 | symbol was actually defined in the kept section. Otherwise ld will | |
9771 | zero the reloc (at least that is the intent, but some cooperation by | |
9772 | the target dependent code is needed, particularly for REL targets). */ | |
9773 | ||
8a696751 AM |
9774 | unsigned int |
9775 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 9776 | { |
9e66c942 | 9777 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 9778 | return PRETEND; |
cdd3575c AM |
9779 | |
9780 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 9781 | return 0; |
cdd3575c AM |
9782 | |
9783 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 9784 | return 0; |
cdd3575c | 9785 | |
9e66c942 | 9786 | return COMPLAIN | PRETEND; |
cdd3575c AM |
9787 | } |
9788 | ||
3d7f7666 L |
9789 | /* Find a match between a section and a member of a section group. */ |
9790 | ||
9791 | static asection * | |
c0f00686 L |
9792 | match_group_member (asection *sec, asection *group, |
9793 | struct bfd_link_info *info) | |
3d7f7666 L |
9794 | { |
9795 | asection *first = elf_next_in_group (group); | |
9796 | asection *s = first; | |
9797 | ||
9798 | while (s != NULL) | |
9799 | { | |
c0f00686 | 9800 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
9801 | return s; |
9802 | ||
83180ade | 9803 | s = elf_next_in_group (s); |
3d7f7666 L |
9804 | if (s == first) |
9805 | break; | |
9806 | } | |
9807 | ||
9808 | return NULL; | |
9809 | } | |
9810 | ||
01b3c8ab | 9811 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
9812 | to replace it. Return the replacement if it is OK. Otherwise return |
9813 | NULL. */ | |
01b3c8ab L |
9814 | |
9815 | asection * | |
c0f00686 | 9816 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
9817 | { |
9818 | asection *kept; | |
9819 | ||
9820 | kept = sec->kept_section; | |
9821 | if (kept != NULL) | |
9822 | { | |
c2370991 | 9823 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 9824 | kept = match_group_member (sec, kept, info); |
1dd2625f BW |
9825 | if (kept != NULL |
9826 | && ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
9827 | != (kept->rawsize != 0 ? kept->rawsize : kept->size))) | |
01b3c8ab | 9828 | kept = NULL; |
c2370991 | 9829 | sec->kept_section = kept; |
01b3c8ab L |
9830 | } |
9831 | return kept; | |
9832 | } | |
9833 | ||
c152c796 AM |
9834 | /* Link an input file into the linker output file. This function |
9835 | handles all the sections and relocations of the input file at once. | |
9836 | This is so that we only have to read the local symbols once, and | |
9837 | don't have to keep them in memory. */ | |
9838 | ||
9839 | static bfd_boolean | |
8b127cbc | 9840 | elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) |
c152c796 | 9841 | { |
ece5ef60 | 9842 | int (*relocate_section) |
c152c796 AM |
9843 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
9844 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
9845 | bfd *output_bfd; | |
9846 | Elf_Internal_Shdr *symtab_hdr; | |
9847 | size_t locsymcount; | |
9848 | size_t extsymoff; | |
9849 | Elf_Internal_Sym *isymbuf; | |
9850 | Elf_Internal_Sym *isym; | |
9851 | Elf_Internal_Sym *isymend; | |
9852 | long *pindex; | |
9853 | asection **ppsection; | |
9854 | asection *o; | |
9855 | const struct elf_backend_data *bed; | |
c152c796 | 9856 | struct elf_link_hash_entry **sym_hashes; |
310fd250 L |
9857 | bfd_size_type address_size; |
9858 | bfd_vma r_type_mask; | |
9859 | int r_sym_shift; | |
ffbc01cc | 9860 | bfd_boolean have_file_sym = FALSE; |
c152c796 | 9861 | |
8b127cbc | 9862 | output_bfd = flinfo->output_bfd; |
c152c796 AM |
9863 | bed = get_elf_backend_data (output_bfd); |
9864 | relocate_section = bed->elf_backend_relocate_section; | |
9865 | ||
9866 | /* If this is a dynamic object, we don't want to do anything here: | |
9867 | we don't want the local symbols, and we don't want the section | |
9868 | contents. */ | |
9869 | if ((input_bfd->flags & DYNAMIC) != 0) | |
9870 | return TRUE; | |
9871 | ||
c152c796 AM |
9872 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
9873 | if (elf_bad_symtab (input_bfd)) | |
9874 | { | |
9875 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9876 | extsymoff = 0; | |
9877 | } | |
9878 | else | |
9879 | { | |
9880 | locsymcount = symtab_hdr->sh_info; | |
9881 | extsymoff = symtab_hdr->sh_info; | |
9882 | } | |
9883 | ||
9884 | /* Read the local symbols. */ | |
9885 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
9886 | if (isymbuf == NULL && locsymcount != 0) | |
9887 | { | |
9888 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8b127cbc AM |
9889 | flinfo->internal_syms, |
9890 | flinfo->external_syms, | |
9891 | flinfo->locsym_shndx); | |
c152c796 AM |
9892 | if (isymbuf == NULL) |
9893 | return FALSE; | |
9894 | } | |
9895 | ||
9896 | /* Find local symbol sections and adjust values of symbols in | |
9897 | SEC_MERGE sections. Write out those local symbols we know are | |
9898 | going into the output file. */ | |
9899 | isymend = isymbuf + locsymcount; | |
8b127cbc | 9900 | for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; |
c152c796 AM |
9901 | isym < isymend; |
9902 | isym++, pindex++, ppsection++) | |
9903 | { | |
9904 | asection *isec; | |
9905 | const char *name; | |
9906 | Elf_Internal_Sym osym; | |
6e0b88f1 AM |
9907 | long indx; |
9908 | int ret; | |
c152c796 AM |
9909 | |
9910 | *pindex = -1; | |
9911 | ||
9912 | if (elf_bad_symtab (input_bfd)) | |
9913 | { | |
9914 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
9915 | { | |
9916 | *ppsection = NULL; | |
9917 | continue; | |
9918 | } | |
9919 | } | |
9920 | ||
9921 | if (isym->st_shndx == SHN_UNDEF) | |
9922 | isec = bfd_und_section_ptr; | |
c152c796 AM |
9923 | else if (isym->st_shndx == SHN_ABS) |
9924 | isec = bfd_abs_section_ptr; | |
9925 | else if (isym->st_shndx == SHN_COMMON) | |
9926 | isec = bfd_com_section_ptr; | |
9927 | else | |
9928 | { | |
cb33740c AM |
9929 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
9930 | if (isec == NULL) | |
9931 | { | |
9932 | /* Don't attempt to output symbols with st_shnx in the | |
9933 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
9934 | *ppsection = NULL; | |
9935 | continue; | |
9936 | } | |
dbaa2011 | 9937 | else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE |
cb33740c AM |
9938 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) |
9939 | isym->st_value = | |
9940 | _bfd_merged_section_offset (output_bfd, &isec, | |
9941 | elf_section_data (isec)->sec_info, | |
9942 | isym->st_value); | |
c152c796 AM |
9943 | } |
9944 | ||
9945 | *ppsection = isec; | |
9946 | ||
d983c8c5 AM |
9947 | /* Don't output the first, undefined, symbol. In fact, don't |
9948 | output any undefined local symbol. */ | |
9949 | if (isec == bfd_und_section_ptr) | |
c152c796 AM |
9950 | continue; |
9951 | ||
9952 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
9953 | { | |
9954 | /* We never output section symbols. Instead, we use the | |
9955 | section symbol of the corresponding section in the output | |
9956 | file. */ | |
9957 | continue; | |
9958 | } | |
9959 | ||
9960 | /* If we are stripping all symbols, we don't want to output this | |
9961 | one. */ | |
8b127cbc | 9962 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
9963 | continue; |
9964 | ||
9965 | /* If we are discarding all local symbols, we don't want to | |
9966 | output this one. If we are generating a relocatable output | |
9967 | file, then some of the local symbols may be required by | |
9968 | relocs; we output them below as we discover that they are | |
9969 | needed. */ | |
8b127cbc | 9970 | if (flinfo->info->discard == discard_all) |
c152c796 AM |
9971 | continue; |
9972 | ||
9973 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
9974 | discarding, we don't need to keep it. */ |
9975 | if (isym->st_shndx != SHN_UNDEF | |
4fbb74a6 AM |
9976 | && isym->st_shndx < SHN_LORESERVE |
9977 | && bfd_section_removed_from_list (output_bfd, | |
9978 | isec->output_section)) | |
e75a280b L |
9979 | continue; |
9980 | ||
c152c796 AM |
9981 | /* Get the name of the symbol. */ |
9982 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
9983 | isym->st_name); | |
9984 | if (name == NULL) | |
9985 | return FALSE; | |
9986 | ||
9987 | /* See if we are discarding symbols with this name. */ | |
8b127cbc AM |
9988 | if ((flinfo->info->strip == strip_some |
9989 | && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) | |
c152c796 | 9990 | == NULL)) |
8b127cbc | 9991 | || (((flinfo->info->discard == discard_sec_merge |
0e1862bb L |
9992 | && (isec->flags & SEC_MERGE) |
9993 | && !bfd_link_relocatable (flinfo->info)) | |
8b127cbc | 9994 | || flinfo->info->discard == discard_l) |
c152c796 AM |
9995 | && bfd_is_local_label_name (input_bfd, name))) |
9996 | continue; | |
9997 | ||
ffbc01cc AM |
9998 | if (ELF_ST_TYPE (isym->st_info) == STT_FILE) |
9999 | { | |
ce875075 AM |
10000 | if (input_bfd->lto_output) |
10001 | /* -flto puts a temp file name here. This means builds | |
10002 | are not reproducible. Discard the symbol. */ | |
10003 | continue; | |
ffbc01cc AM |
10004 | have_file_sym = TRUE; |
10005 | flinfo->filesym_count += 1; | |
10006 | } | |
10007 | if (!have_file_sym) | |
10008 | { | |
10009 | /* In the absence of debug info, bfd_find_nearest_line uses | |
10010 | FILE symbols to determine the source file for local | |
10011 | function symbols. Provide a FILE symbol here if input | |
10012 | files lack such, so that their symbols won't be | |
10013 | associated with a previous input file. It's not the | |
10014 | source file, but the best we can do. */ | |
10015 | have_file_sym = TRUE; | |
10016 | flinfo->filesym_count += 1; | |
10017 | memset (&osym, 0, sizeof (osym)); | |
10018 | osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10019 | osym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10020 | if (!elf_link_output_symstrtab (flinfo, |
10021 | (input_bfd->lto_output ? NULL | |
10022 | : input_bfd->filename), | |
10023 | &osym, bfd_abs_section_ptr, | |
10024 | NULL)) | |
ffbc01cc AM |
10025 | return FALSE; |
10026 | } | |
10027 | ||
c152c796 AM |
10028 | osym = *isym; |
10029 | ||
10030 | /* Adjust the section index for the output file. */ | |
10031 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10032 | isec->output_section); | |
10033 | if (osym.st_shndx == SHN_BAD) | |
10034 | return FALSE; | |
10035 | ||
c152c796 AM |
10036 | /* ELF symbols in relocatable files are section relative, but |
10037 | in executable files they are virtual addresses. Note that | |
10038 | this code assumes that all ELF sections have an associated | |
10039 | BFD section with a reasonable value for output_offset; below | |
10040 | we assume that they also have a reasonable value for | |
10041 | output_section. Any special sections must be set up to meet | |
10042 | these requirements. */ | |
10043 | osym.st_value += isec->output_offset; | |
0e1862bb | 10044 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10045 | { |
10046 | osym.st_value += isec->output_section->vma; | |
10047 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
10048 | { | |
10049 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
8b127cbc AM |
10050 | BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL); |
10051 | osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; | |
c152c796 AM |
10052 | } |
10053 | } | |
10054 | ||
6e0b88f1 | 10055 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac | 10056 | ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); |
6e0b88f1 | 10057 | if (ret == 0) |
c152c796 | 10058 | return FALSE; |
6e0b88f1 AM |
10059 | else if (ret == 1) |
10060 | *pindex = indx; | |
c152c796 AM |
10061 | } |
10062 | ||
310fd250 L |
10063 | if (bed->s->arch_size == 32) |
10064 | { | |
10065 | r_type_mask = 0xff; | |
10066 | r_sym_shift = 8; | |
10067 | address_size = 4; | |
10068 | } | |
10069 | else | |
10070 | { | |
10071 | r_type_mask = 0xffffffff; | |
10072 | r_sym_shift = 32; | |
10073 | address_size = 8; | |
10074 | } | |
10075 | ||
c152c796 AM |
10076 | /* Relocate the contents of each section. */ |
10077 | sym_hashes = elf_sym_hashes (input_bfd); | |
10078 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
10079 | { | |
10080 | bfd_byte *contents; | |
10081 | ||
10082 | if (! o->linker_mark) | |
10083 | { | |
10084 | /* This section was omitted from the link. */ | |
10085 | continue; | |
10086 | } | |
10087 | ||
0e1862bb | 10088 | if (bfd_link_relocatable (flinfo->info) |
bcacc0f5 AM |
10089 | && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) |
10090 | { | |
10091 | /* Deal with the group signature symbol. */ | |
10092 | struct bfd_elf_section_data *sec_data = elf_section_data (o); | |
10093 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
10094 | asection *osec = o->output_section; | |
10095 | ||
10096 | if (symndx >= locsymcount | |
10097 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10098 | && flinfo->sections[symndx] == NULL)) |
bcacc0f5 AM |
10099 | { |
10100 | struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; | |
10101 | while (h->root.type == bfd_link_hash_indirect | |
10102 | || h->root.type == bfd_link_hash_warning) | |
10103 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10104 | /* Arrange for symbol to be output. */ | |
10105 | h->indx = -2; | |
10106 | elf_section_data (osec)->this_hdr.sh_info = -2; | |
10107 | } | |
10108 | else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) | |
10109 | { | |
10110 | /* We'll use the output section target_index. */ | |
8b127cbc | 10111 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 AM |
10112 | elf_section_data (osec)->this_hdr.sh_info = sec->target_index; |
10113 | } | |
10114 | else | |
10115 | { | |
8b127cbc | 10116 | if (flinfo->indices[symndx] == -1) |
bcacc0f5 AM |
10117 | { |
10118 | /* Otherwise output the local symbol now. */ | |
10119 | Elf_Internal_Sym sym = isymbuf[symndx]; | |
8b127cbc | 10120 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 | 10121 | const char *name; |
6e0b88f1 AM |
10122 | long indx; |
10123 | int ret; | |
bcacc0f5 AM |
10124 | |
10125 | name = bfd_elf_string_from_elf_section (input_bfd, | |
10126 | symtab_hdr->sh_link, | |
10127 | sym.st_name); | |
10128 | if (name == NULL) | |
10129 | return FALSE; | |
10130 | ||
10131 | sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10132 | sec); | |
10133 | if (sym.st_shndx == SHN_BAD) | |
10134 | return FALSE; | |
10135 | ||
10136 | sym.st_value += o->output_offset; | |
10137 | ||
6e0b88f1 | 10138 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10139 | ret = elf_link_output_symstrtab (flinfo, name, &sym, o, |
10140 | NULL); | |
6e0b88f1 | 10141 | if (ret == 0) |
bcacc0f5 | 10142 | return FALSE; |
6e0b88f1 | 10143 | else if (ret == 1) |
8b127cbc | 10144 | flinfo->indices[symndx] = indx; |
6e0b88f1 AM |
10145 | else |
10146 | abort (); | |
bcacc0f5 AM |
10147 | } |
10148 | elf_section_data (osec)->this_hdr.sh_info | |
8b127cbc | 10149 | = flinfo->indices[symndx]; |
bcacc0f5 AM |
10150 | } |
10151 | } | |
10152 | ||
c152c796 | 10153 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 10154 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
10155 | continue; |
10156 | ||
10157 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
10158 | { | |
10159 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
10160 | or somesuch. */ | |
10161 | continue; | |
10162 | } | |
10163 | ||
10164 | /* Get the contents of the section. They have been cached by a | |
10165 | relaxation routine. Note that o is a section in an input | |
10166 | file, so the contents field will not have been set by any of | |
10167 | the routines which work on output files. */ | |
10168 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
53291d1f AM |
10169 | { |
10170 | contents = elf_section_data (o)->this_hdr.contents; | |
10171 | if (bed->caches_rawsize | |
10172 | && o->rawsize != 0 | |
10173 | && o->rawsize < o->size) | |
10174 | { | |
10175 | memcpy (flinfo->contents, contents, o->rawsize); | |
10176 | contents = flinfo->contents; | |
10177 | } | |
10178 | } | |
c152c796 AM |
10179 | else |
10180 | { | |
8b127cbc | 10181 | contents = flinfo->contents; |
4a114e3e | 10182 | if (! bfd_get_full_section_contents (input_bfd, o, &contents)) |
c152c796 AM |
10183 | return FALSE; |
10184 | } | |
10185 | ||
10186 | if ((o->flags & SEC_RELOC) != 0) | |
10187 | { | |
10188 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 10189 | Elf_Internal_Rela *rel, *relend; |
0f02bbd9 | 10190 | int action_discarded; |
ece5ef60 | 10191 | int ret; |
c152c796 AM |
10192 | |
10193 | /* Get the swapped relocs. */ | |
10194 | internal_relocs | |
8b127cbc AM |
10195 | = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, |
10196 | flinfo->internal_relocs, FALSE); | |
c152c796 AM |
10197 | if (internal_relocs == NULL |
10198 | && o->reloc_count > 0) | |
10199 | return FALSE; | |
10200 | ||
310fd250 L |
10201 | /* We need to reverse-copy input .ctors/.dtors sections if |
10202 | they are placed in .init_array/.finit_array for output. */ | |
10203 | if (o->size > address_size | |
10204 | && ((strncmp (o->name, ".ctors", 6) == 0 | |
10205 | && strcmp (o->output_section->name, | |
10206 | ".init_array") == 0) | |
10207 | || (strncmp (o->name, ".dtors", 6) == 0 | |
10208 | && strcmp (o->output_section->name, | |
10209 | ".fini_array") == 0)) | |
10210 | && (o->name[6] == 0 || o->name[6] == '.')) | |
c152c796 | 10211 | { |
310fd250 L |
10212 | if (o->size != o->reloc_count * address_size) |
10213 | { | |
10214 | (*_bfd_error_handler) | |
10215 | (_("error: %B: size of section %A is not " | |
10216 | "multiple of address size"), | |
10217 | input_bfd, o); | |
10218 | bfd_set_error (bfd_error_on_input); | |
10219 | return FALSE; | |
10220 | } | |
10221 | o->flags |= SEC_ELF_REVERSE_COPY; | |
c152c796 AM |
10222 | } |
10223 | ||
0f02bbd9 | 10224 | action_discarded = -1; |
c152c796 | 10225 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
10226 | action_discarded = (*bed->action_discarded) (o); |
10227 | ||
10228 | /* Run through the relocs evaluating complex reloc symbols and | |
10229 | looking for relocs against symbols from discarded sections | |
10230 | or section symbols from removed link-once sections. | |
10231 | Complain about relocs against discarded sections. Zero | |
10232 | relocs against removed link-once sections. */ | |
10233 | ||
10234 | rel = internal_relocs; | |
10235 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
10236 | for ( ; rel < relend; rel++) | |
c152c796 | 10237 | { |
0f02bbd9 AM |
10238 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
10239 | unsigned int s_type; | |
10240 | asection **ps, *sec; | |
10241 | struct elf_link_hash_entry *h = NULL; | |
10242 | const char *sym_name; | |
c152c796 | 10243 | |
0f02bbd9 AM |
10244 | if (r_symndx == STN_UNDEF) |
10245 | continue; | |
c152c796 | 10246 | |
0f02bbd9 AM |
10247 | if (r_symndx >= locsymcount |
10248 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10249 | && flinfo->sections[r_symndx] == NULL)) |
0f02bbd9 AM |
10250 | { |
10251 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 10252 | |
0f02bbd9 AM |
10253 | /* Badly formatted input files can contain relocs that |
10254 | reference non-existant symbols. Check here so that | |
10255 | we do not seg fault. */ | |
10256 | if (h == NULL) | |
c152c796 | 10257 | { |
0f02bbd9 | 10258 | char buffer [32]; |
dce669a1 | 10259 | |
0f02bbd9 AM |
10260 | sprintf_vma (buffer, rel->r_info); |
10261 | (*_bfd_error_handler) | |
10262 | (_("error: %B contains a reloc (0x%s) for section %A " | |
10263 | "that references a non-existent global symbol"), | |
10264 | input_bfd, o, buffer); | |
10265 | bfd_set_error (bfd_error_bad_value); | |
10266 | return FALSE; | |
10267 | } | |
3b36f7e6 | 10268 | |
0f02bbd9 AM |
10269 | while (h->root.type == bfd_link_hash_indirect |
10270 | || h->root.type == bfd_link_hash_warning) | |
10271 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 10272 | |
0f02bbd9 | 10273 | s_type = h->type; |
cdd3575c | 10274 | |
9e2dec47 | 10275 | /* If a plugin symbol is referenced from a non-IR file, |
ca4be51c AM |
10276 | mark the symbol as undefined. Note that the |
10277 | linker may attach linker created dynamic sections | |
10278 | to the plugin bfd. Symbols defined in linker | |
10279 | created sections are not plugin symbols. */ | |
9e2dec47 L |
10280 | if (h->root.non_ir_ref |
10281 | && (h->root.type == bfd_link_hash_defined | |
10282 | || h->root.type == bfd_link_hash_defweak) | |
10283 | && (h->root.u.def.section->flags | |
10284 | & SEC_LINKER_CREATED) == 0 | |
10285 | && h->root.u.def.section->owner != NULL | |
10286 | && (h->root.u.def.section->owner->flags | |
10287 | & BFD_PLUGIN) != 0) | |
10288 | { | |
10289 | h->root.type = bfd_link_hash_undefined; | |
10290 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
10291 | } | |
10292 | ||
0f02bbd9 AM |
10293 | ps = NULL; |
10294 | if (h->root.type == bfd_link_hash_defined | |
10295 | || h->root.type == bfd_link_hash_defweak) | |
10296 | ps = &h->root.u.def.section; | |
10297 | ||
10298 | sym_name = h->root.root.string; | |
10299 | } | |
10300 | else | |
10301 | { | |
10302 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
10303 | ||
10304 | s_type = ELF_ST_TYPE (sym->st_info); | |
8b127cbc | 10305 | ps = &flinfo->sections[r_symndx]; |
0f02bbd9 AM |
10306 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, |
10307 | sym, *ps); | |
10308 | } | |
c152c796 | 10309 | |
c301e700 | 10310 | if ((s_type == STT_RELC || s_type == STT_SRELC) |
0e1862bb | 10311 | && !bfd_link_relocatable (flinfo->info)) |
0f02bbd9 AM |
10312 | { |
10313 | bfd_vma val; | |
10314 | bfd_vma dot = (rel->r_offset | |
10315 | + o->output_offset + o->output_section->vma); | |
10316 | #ifdef DEBUG | |
10317 | printf ("Encountered a complex symbol!"); | |
10318 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
9ccb8af9 AM |
10319 | input_bfd->filename, o->name, |
10320 | (long) (rel - internal_relocs)); | |
0f02bbd9 AM |
10321 | printf (" symbol: idx %8.8lx, name %s\n", |
10322 | r_symndx, sym_name); | |
10323 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
10324 | (unsigned long) rel->r_info, | |
10325 | (unsigned long) rel->r_offset); | |
10326 | #endif | |
8b127cbc | 10327 | if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, |
0f02bbd9 AM |
10328 | isymbuf, locsymcount, s_type == STT_SRELC)) |
10329 | return FALSE; | |
10330 | ||
10331 | /* Symbol evaluated OK. Update to absolute value. */ | |
10332 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
10333 | r_symndx, val); | |
10334 | continue; | |
10335 | } | |
10336 | ||
10337 | if (action_discarded != -1 && ps != NULL) | |
10338 | { | |
cdd3575c AM |
10339 | /* Complain if the definition comes from a |
10340 | discarded section. */ | |
dbaa2011 | 10341 | if ((sec = *ps) != NULL && discarded_section (sec)) |
cdd3575c | 10342 | { |
cf35638d | 10343 | BFD_ASSERT (r_symndx != STN_UNDEF); |
0f02bbd9 | 10344 | if (action_discarded & COMPLAIN) |
8b127cbc | 10345 | (*flinfo->info->callbacks->einfo) |
e1fffbe6 | 10346 | (_("%X`%s' referenced in section `%A' of %B: " |
58ac56d0 | 10347 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 10348 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 10349 | |
87e5235d | 10350 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 10351 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
10352 | really defined in the kept linkonce section. |
10353 | FIXME: This is quite broken. Modifying the | |
10354 | symbol here means we will be changing all later | |
e0ae6d6f | 10355 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 10356 | if (action_discarded & PRETEND) |
87e5235d | 10357 | { |
01b3c8ab L |
10358 | asection *kept; |
10359 | ||
c0f00686 | 10360 | kept = _bfd_elf_check_kept_section (sec, |
8b127cbc | 10361 | flinfo->info); |
01b3c8ab | 10362 | if (kept != NULL) |
87e5235d AM |
10363 | { |
10364 | *ps = kept; | |
10365 | continue; | |
10366 | } | |
10367 | } | |
c152c796 AM |
10368 | } |
10369 | } | |
10370 | } | |
10371 | ||
10372 | /* Relocate the section by invoking a back end routine. | |
10373 | ||
10374 | The back end routine is responsible for adjusting the | |
10375 | section contents as necessary, and (if using Rela relocs | |
10376 | and generating a relocatable output file) adjusting the | |
10377 | reloc addend as necessary. | |
10378 | ||
10379 | The back end routine does not have to worry about setting | |
10380 | the reloc address or the reloc symbol index. | |
10381 | ||
10382 | The back end routine is given a pointer to the swapped in | |
10383 | internal symbols, and can access the hash table entries | |
10384 | for the external symbols via elf_sym_hashes (input_bfd). | |
10385 | ||
10386 | When generating relocatable output, the back end routine | |
10387 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
10388 | output symbol is going to be a section symbol | |
10389 | corresponding to the output section, which will require | |
10390 | the addend to be adjusted. */ | |
10391 | ||
8b127cbc | 10392 | ret = (*relocate_section) (output_bfd, flinfo->info, |
c152c796 AM |
10393 | input_bfd, o, contents, |
10394 | internal_relocs, | |
10395 | isymbuf, | |
8b127cbc | 10396 | flinfo->sections); |
ece5ef60 | 10397 | if (!ret) |
c152c796 AM |
10398 | return FALSE; |
10399 | ||
ece5ef60 | 10400 | if (ret == 2 |
0e1862bb | 10401 | || bfd_link_relocatable (flinfo->info) |
8b127cbc | 10402 | || flinfo->info->emitrelocations) |
c152c796 AM |
10403 | { |
10404 | Elf_Internal_Rela *irela; | |
d4730f92 | 10405 | Elf_Internal_Rela *irelaend, *irelamid; |
c152c796 AM |
10406 | bfd_vma last_offset; |
10407 | struct elf_link_hash_entry **rel_hash; | |
d4730f92 BS |
10408 | struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; |
10409 | Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; | |
c152c796 | 10410 | unsigned int next_erel; |
c152c796 | 10411 | bfd_boolean rela_normal; |
d4730f92 | 10412 | struct bfd_elf_section_data *esdi, *esdo; |
c152c796 | 10413 | |
d4730f92 BS |
10414 | esdi = elf_section_data (o); |
10415 | esdo = elf_section_data (o->output_section); | |
10416 | rela_normal = FALSE; | |
c152c796 AM |
10417 | |
10418 | /* Adjust the reloc addresses and symbol indices. */ | |
10419 | ||
10420 | irela = internal_relocs; | |
10421 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
d4730f92 BS |
10422 | rel_hash = esdo->rel.hashes + esdo->rel.count; |
10423 | /* We start processing the REL relocs, if any. When we reach | |
10424 | IRELAMID in the loop, we switch to the RELA relocs. */ | |
10425 | irelamid = irela; | |
10426 | if (esdi->rel.hdr != NULL) | |
10427 | irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) | |
10428 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10429 | rel_hash_list = rel_hash; |
d4730f92 | 10430 | rela_hash_list = NULL; |
c152c796 | 10431 | last_offset = o->output_offset; |
0e1862bb | 10432 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10433 | last_offset += o->output_section->vma; |
10434 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
10435 | { | |
10436 | unsigned long r_symndx; | |
10437 | asection *sec; | |
10438 | Elf_Internal_Sym sym; | |
10439 | ||
10440 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
10441 | { | |
10442 | rel_hash++; | |
10443 | next_erel = 0; | |
10444 | } | |
10445 | ||
d4730f92 BS |
10446 | if (irela == irelamid) |
10447 | { | |
10448 | rel_hash = esdo->rela.hashes + esdo->rela.count; | |
10449 | rela_hash_list = rel_hash; | |
10450 | rela_normal = bed->rela_normal; | |
10451 | } | |
10452 | ||
c152c796 | 10453 | irela->r_offset = _bfd_elf_section_offset (output_bfd, |
8b127cbc | 10454 | flinfo->info, o, |
c152c796 AM |
10455 | irela->r_offset); |
10456 | if (irela->r_offset >= (bfd_vma) -2) | |
10457 | { | |
10458 | /* This is a reloc for a deleted entry or somesuch. | |
10459 | Turn it into an R_*_NONE reloc, at the same | |
10460 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 10461 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
10462 | being ordered. */ |
10463 | irela->r_offset = last_offset; | |
10464 | irela->r_info = 0; | |
10465 | irela->r_addend = 0; | |
10466 | continue; | |
10467 | } | |
10468 | ||
10469 | irela->r_offset += o->output_offset; | |
10470 | ||
10471 | /* Relocs in an executable have to be virtual addresses. */ | |
0e1862bb | 10472 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10473 | irela->r_offset += o->output_section->vma; |
10474 | ||
10475 | last_offset = irela->r_offset; | |
10476 | ||
10477 | r_symndx = irela->r_info >> r_sym_shift; | |
10478 | if (r_symndx == STN_UNDEF) | |
10479 | continue; | |
10480 | ||
10481 | if (r_symndx >= locsymcount | |
10482 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10483 | && flinfo->sections[r_symndx] == NULL)) |
c152c796 AM |
10484 | { |
10485 | struct elf_link_hash_entry *rh; | |
10486 | unsigned long indx; | |
10487 | ||
10488 | /* This is a reloc against a global symbol. We | |
10489 | have not yet output all the local symbols, so | |
10490 | we do not know the symbol index of any global | |
10491 | symbol. We set the rel_hash entry for this | |
10492 | reloc to point to the global hash table entry | |
10493 | for this symbol. The symbol index is then | |
ee75fd95 | 10494 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
10495 | indx = r_symndx - extsymoff; |
10496 | rh = elf_sym_hashes (input_bfd)[indx]; | |
10497 | while (rh->root.type == bfd_link_hash_indirect | |
10498 | || rh->root.type == bfd_link_hash_warning) | |
10499 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
10500 | ||
10501 | /* Setting the index to -2 tells | |
10502 | elf_link_output_extsym that this symbol is | |
10503 | used by a reloc. */ | |
10504 | BFD_ASSERT (rh->indx < 0); | |
10505 | rh->indx = -2; | |
10506 | ||
10507 | *rel_hash = rh; | |
10508 | ||
10509 | continue; | |
10510 | } | |
10511 | ||
10512 | /* This is a reloc against a local symbol. */ | |
10513 | ||
10514 | *rel_hash = NULL; | |
10515 | sym = isymbuf[r_symndx]; | |
8b127cbc | 10516 | sec = flinfo->sections[r_symndx]; |
c152c796 AM |
10517 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) |
10518 | { | |
10519 | /* I suppose the backend ought to fill in the | |
10520 | section of any STT_SECTION symbol against a | |
6a8d1586 | 10521 | processor specific section. */ |
cf35638d | 10522 | r_symndx = STN_UNDEF; |
6a8d1586 AM |
10523 | if (bfd_is_abs_section (sec)) |
10524 | ; | |
c152c796 AM |
10525 | else if (sec == NULL || sec->owner == NULL) |
10526 | { | |
10527 | bfd_set_error (bfd_error_bad_value); | |
10528 | return FALSE; | |
10529 | } | |
10530 | else | |
10531 | { | |
6a8d1586 AM |
10532 | asection *osec = sec->output_section; |
10533 | ||
10534 | /* If we have discarded a section, the output | |
10535 | section will be the absolute section. In | |
ab96bf03 AM |
10536 | case of discarded SEC_MERGE sections, use |
10537 | the kept section. relocate_section should | |
10538 | have already handled discarded linkonce | |
10539 | sections. */ | |
6a8d1586 AM |
10540 | if (bfd_is_abs_section (osec) |
10541 | && sec->kept_section != NULL | |
10542 | && sec->kept_section->output_section != NULL) | |
10543 | { | |
10544 | osec = sec->kept_section->output_section; | |
10545 | irela->r_addend -= osec->vma; | |
10546 | } | |
10547 | ||
10548 | if (!bfd_is_abs_section (osec)) | |
10549 | { | |
10550 | r_symndx = osec->target_index; | |
cf35638d | 10551 | if (r_symndx == STN_UNDEF) |
74541ad4 | 10552 | { |
051d833a AM |
10553 | irela->r_addend += osec->vma; |
10554 | osec = _bfd_nearby_section (output_bfd, osec, | |
10555 | osec->vma); | |
10556 | irela->r_addend -= osec->vma; | |
10557 | r_symndx = osec->target_index; | |
74541ad4 | 10558 | } |
6a8d1586 | 10559 | } |
c152c796 AM |
10560 | } |
10561 | ||
10562 | /* Adjust the addend according to where the | |
10563 | section winds up in the output section. */ | |
10564 | if (rela_normal) | |
10565 | irela->r_addend += sec->output_offset; | |
10566 | } | |
10567 | else | |
10568 | { | |
8b127cbc | 10569 | if (flinfo->indices[r_symndx] == -1) |
c152c796 AM |
10570 | { |
10571 | unsigned long shlink; | |
10572 | const char *name; | |
10573 | asection *osec; | |
6e0b88f1 | 10574 | long indx; |
c152c796 | 10575 | |
8b127cbc | 10576 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
10577 | { |
10578 | /* You can't do ld -r -s. */ | |
10579 | bfd_set_error (bfd_error_invalid_operation); | |
10580 | return FALSE; | |
10581 | } | |
10582 | ||
10583 | /* This symbol was skipped earlier, but | |
10584 | since it is needed by a reloc, we | |
10585 | must output it now. */ | |
10586 | shlink = symtab_hdr->sh_link; | |
10587 | name = (bfd_elf_string_from_elf_section | |
10588 | (input_bfd, shlink, sym.st_name)); | |
10589 | if (name == NULL) | |
10590 | return FALSE; | |
10591 | ||
10592 | osec = sec->output_section; | |
10593 | sym.st_shndx = | |
10594 | _bfd_elf_section_from_bfd_section (output_bfd, | |
10595 | osec); | |
10596 | if (sym.st_shndx == SHN_BAD) | |
10597 | return FALSE; | |
10598 | ||
10599 | sym.st_value += sec->output_offset; | |
0e1862bb | 10600 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10601 | { |
10602 | sym.st_value += osec->vma; | |
10603 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
10604 | { | |
10605 | /* STT_TLS symbols are relative to PT_TLS | |
10606 | segment base. */ | |
8b127cbc | 10607 | BFD_ASSERT (elf_hash_table (flinfo->info) |
c152c796 | 10608 | ->tls_sec != NULL); |
8b127cbc | 10609 | sym.st_value -= (elf_hash_table (flinfo->info) |
c152c796 AM |
10610 | ->tls_sec->vma); |
10611 | } | |
10612 | } | |
10613 | ||
6e0b88f1 | 10614 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10615 | ret = elf_link_output_symstrtab (flinfo, name, |
10616 | &sym, sec, | |
10617 | NULL); | |
6e0b88f1 | 10618 | if (ret == 0) |
c152c796 | 10619 | return FALSE; |
6e0b88f1 | 10620 | else if (ret == 1) |
8b127cbc | 10621 | flinfo->indices[r_symndx] = indx; |
6e0b88f1 AM |
10622 | else |
10623 | abort (); | |
c152c796 AM |
10624 | } |
10625 | ||
8b127cbc | 10626 | r_symndx = flinfo->indices[r_symndx]; |
c152c796 AM |
10627 | } |
10628 | ||
10629 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
10630 | | (irela->r_info & r_type_mask)); | |
10631 | } | |
10632 | ||
10633 | /* Swap out the relocs. */ | |
d4730f92 BS |
10634 | input_rel_hdr = esdi->rel.hdr; |
10635 | if (input_rel_hdr && input_rel_hdr->sh_size != 0) | |
c152c796 | 10636 | { |
d4730f92 BS |
10637 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
10638 | input_rel_hdr, | |
10639 | internal_relocs, | |
10640 | rel_hash_list)) | |
10641 | return FALSE; | |
c152c796 AM |
10642 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) |
10643 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10644 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
d4730f92 BS |
10645 | } |
10646 | ||
10647 | input_rela_hdr = esdi->rela.hdr; | |
10648 | if (input_rela_hdr && input_rela_hdr->sh_size != 0) | |
10649 | { | |
eac338cf | 10650 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
d4730f92 | 10651 | input_rela_hdr, |
eac338cf | 10652 | internal_relocs, |
d4730f92 | 10653 | rela_hash_list)) |
c152c796 AM |
10654 | return FALSE; |
10655 | } | |
10656 | } | |
10657 | } | |
10658 | ||
10659 | /* Write out the modified section contents. */ | |
10660 | if (bed->elf_backend_write_section | |
8b127cbc | 10661 | && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, |
c7b8f16e | 10662 | contents)) |
c152c796 AM |
10663 | { |
10664 | /* Section written out. */ | |
10665 | } | |
10666 | else switch (o->sec_info_type) | |
10667 | { | |
dbaa2011 | 10668 | case SEC_INFO_TYPE_STABS: |
c152c796 AM |
10669 | if (! (_bfd_write_section_stabs |
10670 | (output_bfd, | |
8b127cbc | 10671 | &elf_hash_table (flinfo->info)->stab_info, |
c152c796 AM |
10672 | o, &elf_section_data (o)->sec_info, contents))) |
10673 | return FALSE; | |
10674 | break; | |
dbaa2011 | 10675 | case SEC_INFO_TYPE_MERGE: |
c152c796 AM |
10676 | if (! _bfd_write_merged_section (output_bfd, o, |
10677 | elf_section_data (o)->sec_info)) | |
10678 | return FALSE; | |
10679 | break; | |
dbaa2011 | 10680 | case SEC_INFO_TYPE_EH_FRAME: |
c152c796 | 10681 | { |
8b127cbc | 10682 | if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, |
c152c796 AM |
10683 | o, contents)) |
10684 | return FALSE; | |
10685 | } | |
10686 | break; | |
2f0c68f2 CM |
10687 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
10688 | { | |
10689 | if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, | |
10690 | flinfo->info, | |
10691 | o, contents)) | |
10692 | return FALSE; | |
10693 | } | |
10694 | break; | |
c152c796 AM |
10695 | default: |
10696 | { | |
310fd250 L |
10697 | if (! (o->flags & SEC_EXCLUDE)) |
10698 | { | |
10699 | file_ptr offset = (file_ptr) o->output_offset; | |
10700 | bfd_size_type todo = o->size; | |
37b01f6a DG |
10701 | |
10702 | offset *= bfd_octets_per_byte (output_bfd); | |
10703 | ||
310fd250 L |
10704 | if ((o->flags & SEC_ELF_REVERSE_COPY)) |
10705 | { | |
10706 | /* Reverse-copy input section to output. */ | |
10707 | do | |
10708 | { | |
10709 | todo -= address_size; | |
10710 | if (! bfd_set_section_contents (output_bfd, | |
10711 | o->output_section, | |
10712 | contents + todo, | |
10713 | offset, | |
10714 | address_size)) | |
10715 | return FALSE; | |
10716 | if (todo == 0) | |
10717 | break; | |
10718 | offset += address_size; | |
10719 | } | |
10720 | while (1); | |
10721 | } | |
10722 | else if (! bfd_set_section_contents (output_bfd, | |
10723 | o->output_section, | |
10724 | contents, | |
10725 | offset, todo)) | |
10726 | return FALSE; | |
10727 | } | |
c152c796 AM |
10728 | } |
10729 | break; | |
10730 | } | |
10731 | } | |
10732 | ||
10733 | return TRUE; | |
10734 | } | |
10735 | ||
10736 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 10737 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
10738 | is used to build constructor and destructor tables when linking |
10739 | with -Ur. */ | |
10740 | ||
10741 | static bfd_boolean | |
10742 | elf_reloc_link_order (bfd *output_bfd, | |
10743 | struct bfd_link_info *info, | |
10744 | asection *output_section, | |
10745 | struct bfd_link_order *link_order) | |
10746 | { | |
10747 | reloc_howto_type *howto; | |
10748 | long indx; | |
10749 | bfd_vma offset; | |
10750 | bfd_vma addend; | |
d4730f92 | 10751 | struct bfd_elf_section_reloc_data *reldata; |
c152c796 AM |
10752 | struct elf_link_hash_entry **rel_hash_ptr; |
10753 | Elf_Internal_Shdr *rel_hdr; | |
10754 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
10755 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
10756 | bfd_byte *erel; | |
10757 | unsigned int i; | |
d4730f92 | 10758 | struct bfd_elf_section_data *esdo = elf_section_data (output_section); |
c152c796 AM |
10759 | |
10760 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
10761 | if (howto == NULL) | |
10762 | { | |
10763 | bfd_set_error (bfd_error_bad_value); | |
10764 | return FALSE; | |
10765 | } | |
10766 | ||
10767 | addend = link_order->u.reloc.p->addend; | |
10768 | ||
d4730f92 BS |
10769 | if (esdo->rel.hdr) |
10770 | reldata = &esdo->rel; | |
10771 | else if (esdo->rela.hdr) | |
10772 | reldata = &esdo->rela; | |
10773 | else | |
10774 | { | |
10775 | reldata = NULL; | |
10776 | BFD_ASSERT (0); | |
10777 | } | |
10778 | ||
c152c796 | 10779 | /* Figure out the symbol index. */ |
d4730f92 | 10780 | rel_hash_ptr = reldata->hashes + reldata->count; |
c152c796 AM |
10781 | if (link_order->type == bfd_section_reloc_link_order) |
10782 | { | |
10783 | indx = link_order->u.reloc.p->u.section->target_index; | |
10784 | BFD_ASSERT (indx != 0); | |
10785 | *rel_hash_ptr = NULL; | |
10786 | } | |
10787 | else | |
10788 | { | |
10789 | struct elf_link_hash_entry *h; | |
10790 | ||
10791 | /* Treat a reloc against a defined symbol as though it were | |
10792 | actually against the section. */ | |
10793 | h = ((struct elf_link_hash_entry *) | |
10794 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
10795 | link_order->u.reloc.p->u.name, | |
10796 | FALSE, FALSE, TRUE)); | |
10797 | if (h != NULL | |
10798 | && (h->root.type == bfd_link_hash_defined | |
10799 | || h->root.type == bfd_link_hash_defweak)) | |
10800 | { | |
10801 | asection *section; | |
10802 | ||
10803 | section = h->root.u.def.section; | |
10804 | indx = section->output_section->target_index; | |
10805 | *rel_hash_ptr = NULL; | |
10806 | /* It seems that we ought to add the symbol value to the | |
10807 | addend here, but in practice it has already been added | |
10808 | because it was passed to constructor_callback. */ | |
10809 | addend += section->output_section->vma + section->output_offset; | |
10810 | } | |
10811 | else if (h != NULL) | |
10812 | { | |
10813 | /* Setting the index to -2 tells elf_link_output_extsym that | |
10814 | this symbol is used by a reloc. */ | |
10815 | h->indx = -2; | |
10816 | *rel_hash_ptr = h; | |
10817 | indx = 0; | |
10818 | } | |
10819 | else | |
10820 | { | |
1a72702b AM |
10821 | (*info->callbacks->unattached_reloc) |
10822 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); | |
c152c796 AM |
10823 | indx = 0; |
10824 | } | |
10825 | } | |
10826 | ||
10827 | /* If this is an inplace reloc, we must write the addend into the | |
10828 | object file. */ | |
10829 | if (howto->partial_inplace && addend != 0) | |
10830 | { | |
10831 | bfd_size_type size; | |
10832 | bfd_reloc_status_type rstat; | |
10833 | bfd_byte *buf; | |
10834 | bfd_boolean ok; | |
10835 | const char *sym_name; | |
10836 | ||
a50b1753 NC |
10837 | size = (bfd_size_type) bfd_get_reloc_size (howto); |
10838 | buf = (bfd_byte *) bfd_zmalloc (size); | |
6346d5ca | 10839 | if (buf == NULL && size != 0) |
c152c796 AM |
10840 | return FALSE; |
10841 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
10842 | switch (rstat) | |
10843 | { | |
10844 | case bfd_reloc_ok: | |
10845 | break; | |
10846 | ||
10847 | default: | |
10848 | case bfd_reloc_outofrange: | |
10849 | abort (); | |
10850 | ||
10851 | case bfd_reloc_overflow: | |
10852 | if (link_order->type == bfd_section_reloc_link_order) | |
10853 | sym_name = bfd_section_name (output_bfd, | |
10854 | link_order->u.reloc.p->u.section); | |
10855 | else | |
10856 | sym_name = link_order->u.reloc.p->u.name; | |
1a72702b AM |
10857 | (*info->callbacks->reloc_overflow) (info, NULL, sym_name, |
10858 | howto->name, addend, NULL, NULL, | |
10859 | (bfd_vma) 0); | |
c152c796 AM |
10860 | break; |
10861 | } | |
37b01f6a | 10862 | |
c152c796 | 10863 | ok = bfd_set_section_contents (output_bfd, output_section, buf, |
37b01f6a DG |
10864 | link_order->offset |
10865 | * bfd_octets_per_byte (output_bfd), | |
10866 | size); | |
c152c796 AM |
10867 | free (buf); |
10868 | if (! ok) | |
10869 | return FALSE; | |
10870 | } | |
10871 | ||
10872 | /* The address of a reloc is relative to the section in a | |
10873 | relocatable file, and is a virtual address in an executable | |
10874 | file. */ | |
10875 | offset = link_order->offset; | |
0e1862bb | 10876 | if (! bfd_link_relocatable (info)) |
c152c796 AM |
10877 | offset += output_section->vma; |
10878 | ||
10879 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
10880 | { | |
10881 | irel[i].r_offset = offset; | |
10882 | irel[i].r_info = 0; | |
10883 | irel[i].r_addend = 0; | |
10884 | } | |
10885 | if (bed->s->arch_size == 32) | |
10886 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
10887 | else | |
10888 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
10889 | ||
d4730f92 | 10890 | rel_hdr = reldata->hdr; |
c152c796 AM |
10891 | erel = rel_hdr->contents; |
10892 | if (rel_hdr->sh_type == SHT_REL) | |
10893 | { | |
d4730f92 | 10894 | erel += reldata->count * bed->s->sizeof_rel; |
c152c796 AM |
10895 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); |
10896 | } | |
10897 | else | |
10898 | { | |
10899 | irel[0].r_addend = addend; | |
d4730f92 | 10900 | erel += reldata->count * bed->s->sizeof_rela; |
c152c796 AM |
10901 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); |
10902 | } | |
10903 | ||
d4730f92 | 10904 | ++reldata->count; |
c152c796 AM |
10905 | |
10906 | return TRUE; | |
10907 | } | |
10908 | ||
0b52efa6 PB |
10909 | |
10910 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
10911 | ||
10912 | static bfd_vma | |
10913 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
10914 | { | |
10915 | Elf_Internal_Shdr **elf_shdrp; | |
10916 | asection *s; | |
10917 | int elfsec; | |
10918 | ||
10919 | s = p->u.indirect.section; | |
10920 | elf_shdrp = elf_elfsections (s->owner); | |
10921 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
10922 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
10923 | /* PR 290: |
10924 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 10925 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
10926 | sh_info fields. Hence we could get the situation |
10927 | where elfsec is 0. */ | |
10928 | if (elfsec == 0) | |
10929 | { | |
10930 | const struct elf_backend_data *bed | |
10931 | = get_elf_backend_data (s->owner); | |
10932 | if (bed->link_order_error_handler) | |
d003868e AM |
10933 | bed->link_order_error_handler |
10934 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); | |
185d09ad L |
10935 | return 0; |
10936 | } | |
10937 | else | |
10938 | { | |
10939 | s = elf_shdrp[elfsec]->bfd_section; | |
10940 | return s->output_section->vma + s->output_offset; | |
10941 | } | |
0b52efa6 PB |
10942 | } |
10943 | ||
10944 | ||
10945 | /* Compare two sections based on the locations of the sections they are | |
10946 | linked to. Used by elf_fixup_link_order. */ | |
10947 | ||
10948 | static int | |
10949 | compare_link_order (const void * a, const void * b) | |
10950 | { | |
10951 | bfd_vma apos; | |
10952 | bfd_vma bpos; | |
10953 | ||
10954 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
10955 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
10956 | if (apos < bpos) | |
10957 | return -1; | |
10958 | return apos > bpos; | |
10959 | } | |
10960 | ||
10961 | ||
10962 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
10963 | order as their linked sections. Returns false if this could not be done | |
10964 | because an output section includes both ordered and unordered | |
10965 | sections. Ideally we'd do this in the linker proper. */ | |
10966 | ||
10967 | static bfd_boolean | |
10968 | elf_fixup_link_order (bfd *abfd, asection *o) | |
10969 | { | |
10970 | int seen_linkorder; | |
10971 | int seen_other; | |
10972 | int n; | |
10973 | struct bfd_link_order *p; | |
10974 | bfd *sub; | |
10975 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 10976 | unsigned elfsec; |
0b52efa6 | 10977 | struct bfd_link_order **sections; |
d33cdfe3 | 10978 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 10979 | bfd_vma offset; |
3b36f7e6 | 10980 | |
d33cdfe3 L |
10981 | other_sec = NULL; |
10982 | linkorder_sec = NULL; | |
0b52efa6 PB |
10983 | seen_other = 0; |
10984 | seen_linkorder = 0; | |
8423293d | 10985 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 10986 | { |
d33cdfe3 | 10987 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
10988 | { |
10989 | s = p->u.indirect.section; | |
d33cdfe3 L |
10990 | sub = s->owner; |
10991 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
10992 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
10993 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
10994 | && elfsec < elf_numsections (sub) | |
4fbb74a6 AM |
10995 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER |
10996 | && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) | |
d33cdfe3 L |
10997 | { |
10998 | seen_linkorder++; | |
10999 | linkorder_sec = s; | |
11000 | } | |
0b52efa6 | 11001 | else |
d33cdfe3 L |
11002 | { |
11003 | seen_other++; | |
11004 | other_sec = s; | |
11005 | } | |
0b52efa6 PB |
11006 | } |
11007 | else | |
11008 | seen_other++; | |
d33cdfe3 L |
11009 | |
11010 | if (seen_other && seen_linkorder) | |
11011 | { | |
11012 | if (other_sec && linkorder_sec) | |
11013 | (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"), | |
11014 | o, linkorder_sec, | |
11015 | linkorder_sec->owner, other_sec, | |
11016 | other_sec->owner); | |
11017 | else | |
11018 | (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), | |
11019 | o); | |
11020 | bfd_set_error (bfd_error_bad_value); | |
11021 | return FALSE; | |
11022 | } | |
0b52efa6 PB |
11023 | } |
11024 | ||
11025 | if (!seen_linkorder) | |
11026 | return TRUE; | |
11027 | ||
0b52efa6 | 11028 | sections = (struct bfd_link_order **) |
14b1c01e AM |
11029 | bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); |
11030 | if (sections == NULL) | |
11031 | return FALSE; | |
0b52efa6 | 11032 | seen_linkorder = 0; |
3b36f7e6 | 11033 | |
8423293d | 11034 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
11035 | { |
11036 | sections[seen_linkorder++] = p; | |
11037 | } | |
11038 | /* Sort the input sections in the order of their linked section. */ | |
11039 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
11040 | compare_link_order); | |
11041 | ||
11042 | /* Change the offsets of the sections. */ | |
11043 | offset = 0; | |
11044 | for (n = 0; n < seen_linkorder; n++) | |
11045 | { | |
11046 | s = sections[n]->u.indirect.section; | |
461686a3 | 11047 | offset &= ~(bfd_vma) 0 << s->alignment_power; |
37b01f6a | 11048 | s->output_offset = offset / bfd_octets_per_byte (abfd); |
0b52efa6 PB |
11049 | sections[n]->offset = offset; |
11050 | offset += sections[n]->size; | |
11051 | } | |
11052 | ||
4dd07732 | 11053 | free (sections); |
0b52efa6 PB |
11054 | return TRUE; |
11055 | } | |
11056 | ||
76359541 TP |
11057 | /* Generate an import library in INFO->implib_bfd from symbols in ABFD. |
11058 | Returns TRUE upon success, FALSE otherwise. */ | |
11059 | ||
11060 | static bfd_boolean | |
11061 | elf_output_implib (bfd *abfd, struct bfd_link_info *info) | |
11062 | { | |
11063 | bfd_boolean ret = FALSE; | |
11064 | bfd *implib_bfd; | |
11065 | const struct elf_backend_data *bed; | |
11066 | flagword flags; | |
11067 | enum bfd_architecture arch; | |
11068 | unsigned int mach; | |
11069 | asymbol **sympp = NULL; | |
11070 | long symsize; | |
11071 | long symcount; | |
11072 | long src_count; | |
11073 | elf_symbol_type *osymbuf; | |
11074 | ||
11075 | implib_bfd = info->out_implib_bfd; | |
11076 | bed = get_elf_backend_data (abfd); | |
11077 | ||
11078 | if (!bfd_set_format (implib_bfd, bfd_object)) | |
11079 | return FALSE; | |
11080 | ||
11081 | flags = bfd_get_file_flags (abfd); | |
11082 | flags &= ~HAS_RELOC; | |
11083 | if (!bfd_set_start_address (implib_bfd, 0) | |
11084 | || !bfd_set_file_flags (implib_bfd, flags)) | |
11085 | return FALSE; | |
11086 | ||
11087 | /* Copy architecture of output file to import library file. */ | |
11088 | arch = bfd_get_arch (abfd); | |
11089 | mach = bfd_get_mach (abfd); | |
11090 | if (!bfd_set_arch_mach (implib_bfd, arch, mach) | |
11091 | && (abfd->target_defaulted | |
11092 | || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) | |
11093 | return FALSE; | |
11094 | ||
11095 | /* Get symbol table size. */ | |
11096 | symsize = bfd_get_symtab_upper_bound (abfd); | |
11097 | if (symsize < 0) | |
11098 | return FALSE; | |
11099 | ||
11100 | /* Read in the symbol table. */ | |
11101 | sympp = (asymbol **) xmalloc (symsize); | |
11102 | symcount = bfd_canonicalize_symtab (abfd, sympp); | |
11103 | if (symcount < 0) | |
11104 | goto free_sym_buf; | |
11105 | ||
11106 | /* Allow the BFD backend to copy any private header data it | |
11107 | understands from the output BFD to the import library BFD. */ | |
11108 | if (! bfd_copy_private_header_data (abfd, implib_bfd)) | |
11109 | goto free_sym_buf; | |
11110 | ||
11111 | /* Filter symbols to appear in the import library. */ | |
11112 | if (bed->elf_backend_filter_implib_symbols) | |
11113 | symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, | |
11114 | symcount); | |
11115 | else | |
11116 | symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); | |
11117 | if (symcount == 0) | |
11118 | { | |
5df1bc57 | 11119 | bfd_set_error (bfd_error_no_symbols); |
76359541 TP |
11120 | (*_bfd_error_handler) (_("%B: no symbol found for import library"), |
11121 | implib_bfd); | |
11122 | goto free_sym_buf; | |
11123 | } | |
11124 | ||
11125 | ||
11126 | /* Make symbols absolute. */ | |
11127 | osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount, | |
11128 | sizeof (*osymbuf)); | |
11129 | for (src_count = 0; src_count < symcount; src_count++) | |
11130 | { | |
11131 | memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], | |
11132 | sizeof (*osymbuf)); | |
11133 | osymbuf[src_count].symbol.section = bfd_abs_section_ptr; | |
11134 | osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; | |
11135 | osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; | |
11136 | osymbuf[src_count].internal_elf_sym.st_value = | |
11137 | osymbuf[src_count].symbol.value; | |
11138 | sympp[src_count] = &osymbuf[src_count].symbol; | |
11139 | } | |
11140 | ||
11141 | bfd_set_symtab (implib_bfd, sympp, symcount); | |
11142 | ||
11143 | /* Allow the BFD backend to copy any private data it understands | |
11144 | from the output BFD to the import library BFD. This is done last | |
11145 | to permit the routine to look at the filtered symbol table. */ | |
11146 | if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) | |
11147 | goto free_sym_buf; | |
11148 | ||
11149 | if (!bfd_close (implib_bfd)) | |
11150 | goto free_sym_buf; | |
11151 | ||
11152 | ret = TRUE; | |
11153 | ||
11154 | free_sym_buf: | |
11155 | free (sympp); | |
11156 | return ret; | |
11157 | } | |
11158 | ||
9f7c3e5e AM |
11159 | static void |
11160 | elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) | |
11161 | { | |
11162 | asection *o; | |
11163 | ||
11164 | if (flinfo->symstrtab != NULL) | |
ef10c3ac | 11165 | _bfd_elf_strtab_free (flinfo->symstrtab); |
9f7c3e5e AM |
11166 | if (flinfo->contents != NULL) |
11167 | free (flinfo->contents); | |
11168 | if (flinfo->external_relocs != NULL) | |
11169 | free (flinfo->external_relocs); | |
11170 | if (flinfo->internal_relocs != NULL) | |
11171 | free (flinfo->internal_relocs); | |
11172 | if (flinfo->external_syms != NULL) | |
11173 | free (flinfo->external_syms); | |
11174 | if (flinfo->locsym_shndx != NULL) | |
11175 | free (flinfo->locsym_shndx); | |
11176 | if (flinfo->internal_syms != NULL) | |
11177 | free (flinfo->internal_syms); | |
11178 | if (flinfo->indices != NULL) | |
11179 | free (flinfo->indices); | |
11180 | if (flinfo->sections != NULL) | |
11181 | free (flinfo->sections); | |
9f7c3e5e AM |
11182 | if (flinfo->symshndxbuf != NULL) |
11183 | free (flinfo->symshndxbuf); | |
11184 | for (o = obfd->sections; o != NULL; o = o->next) | |
11185 | { | |
11186 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11187 | if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) | |
11188 | free (esdo->rel.hashes); | |
11189 | if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) | |
11190 | free (esdo->rela.hashes); | |
11191 | } | |
11192 | } | |
0b52efa6 | 11193 | |
c152c796 AM |
11194 | /* Do the final step of an ELF link. */ |
11195 | ||
11196 | bfd_boolean | |
11197 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
11198 | { | |
11199 | bfd_boolean dynamic; | |
11200 | bfd_boolean emit_relocs; | |
11201 | bfd *dynobj; | |
8b127cbc | 11202 | struct elf_final_link_info flinfo; |
91d6fa6a NC |
11203 | asection *o; |
11204 | struct bfd_link_order *p; | |
11205 | bfd *sub; | |
c152c796 AM |
11206 | bfd_size_type max_contents_size; |
11207 | bfd_size_type max_external_reloc_size; | |
11208 | bfd_size_type max_internal_reloc_count; | |
11209 | bfd_size_type max_sym_count; | |
11210 | bfd_size_type max_sym_shndx_count; | |
c152c796 AM |
11211 | Elf_Internal_Sym elfsym; |
11212 | unsigned int i; | |
11213 | Elf_Internal_Shdr *symtab_hdr; | |
11214 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
c152c796 AM |
11215 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11216 | struct elf_outext_info eoinfo; | |
11217 | bfd_boolean merged; | |
11218 | size_t relativecount = 0; | |
11219 | asection *reldyn = 0; | |
11220 | bfd_size_type amt; | |
104d59d1 JM |
11221 | asection *attr_section = NULL; |
11222 | bfd_vma attr_size = 0; | |
11223 | const char *std_attrs_section; | |
c152c796 AM |
11224 | |
11225 | if (! is_elf_hash_table (info->hash)) | |
11226 | return FALSE; | |
11227 | ||
0e1862bb | 11228 | if (bfd_link_pic (info)) |
c152c796 AM |
11229 | abfd->flags |= DYNAMIC; |
11230 | ||
11231 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
11232 | dynobj = elf_hash_table (info)->dynobj; | |
11233 | ||
0e1862bb | 11234 | emit_relocs = (bfd_link_relocatable (info) |
a4676736 | 11235 | || info->emitrelocations); |
c152c796 | 11236 | |
8b127cbc AM |
11237 | flinfo.info = info; |
11238 | flinfo.output_bfd = abfd; | |
ef10c3ac | 11239 | flinfo.symstrtab = _bfd_elf_strtab_init (); |
8b127cbc | 11240 | if (flinfo.symstrtab == NULL) |
c152c796 AM |
11241 | return FALSE; |
11242 | ||
11243 | if (! dynamic) | |
11244 | { | |
8b127cbc AM |
11245 | flinfo.hash_sec = NULL; |
11246 | flinfo.symver_sec = NULL; | |
c152c796 AM |
11247 | } |
11248 | else | |
11249 | { | |
3d4d4302 | 11250 | flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); |
202e2356 | 11251 | /* Note that dynsym_sec can be NULL (on VMS). */ |
3d4d4302 | 11252 | flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); |
c152c796 AM |
11253 | /* Note that it is OK if symver_sec is NULL. */ |
11254 | } | |
11255 | ||
8b127cbc AM |
11256 | flinfo.contents = NULL; |
11257 | flinfo.external_relocs = NULL; | |
11258 | flinfo.internal_relocs = NULL; | |
11259 | flinfo.external_syms = NULL; | |
11260 | flinfo.locsym_shndx = NULL; | |
11261 | flinfo.internal_syms = NULL; | |
11262 | flinfo.indices = NULL; | |
11263 | flinfo.sections = NULL; | |
8b127cbc | 11264 | flinfo.symshndxbuf = NULL; |
ffbc01cc | 11265 | flinfo.filesym_count = 0; |
c152c796 | 11266 | |
104d59d1 JM |
11267 | /* The object attributes have been merged. Remove the input |
11268 | sections from the link, and set the contents of the output | |
11269 | secton. */ | |
11270 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; | |
11271 | for (o = abfd->sections; o != NULL; o = o->next) | |
11272 | { | |
11273 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) | |
11274 | || strcmp (o->name, ".gnu.attributes") == 0) | |
11275 | { | |
11276 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
11277 | { | |
11278 | asection *input_section; | |
11279 | ||
11280 | if (p->type != bfd_indirect_link_order) | |
11281 | continue; | |
11282 | input_section = p->u.indirect.section; | |
11283 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
11284 | elf_link_input_bfd ignores this section. */ | |
11285 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
11286 | } | |
a0c8462f | 11287 | |
104d59d1 JM |
11288 | attr_size = bfd_elf_obj_attr_size (abfd); |
11289 | if (attr_size) | |
11290 | { | |
11291 | bfd_set_section_size (abfd, o, attr_size); | |
11292 | attr_section = o; | |
11293 | /* Skip this section later on. */ | |
11294 | o->map_head.link_order = NULL; | |
11295 | } | |
11296 | else | |
11297 | o->flags |= SEC_EXCLUDE; | |
11298 | } | |
11299 | } | |
11300 | ||
c152c796 AM |
11301 | /* Count up the number of relocations we will output for each output |
11302 | section, so that we know the sizes of the reloc sections. We | |
11303 | also figure out some maximum sizes. */ | |
11304 | max_contents_size = 0; | |
11305 | max_external_reloc_size = 0; | |
11306 | max_internal_reloc_count = 0; | |
11307 | max_sym_count = 0; | |
11308 | max_sym_shndx_count = 0; | |
11309 | merged = FALSE; | |
11310 | for (o = abfd->sections; o != NULL; o = o->next) | |
11311 | { | |
11312 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
5025eb7c | 11313 | unsigned int additional_reloc_count = 0; |
c152c796 AM |
11314 | o->reloc_count = 0; |
11315 | ||
8423293d | 11316 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
11317 | { |
11318 | unsigned int reloc_count = 0; | |
11319 | struct bfd_elf_section_data *esdi = NULL; | |
c152c796 AM |
11320 | |
11321 | if (p->type == bfd_section_reloc_link_order | |
11322 | || p->type == bfd_symbol_reloc_link_order) | |
11323 | reloc_count = 1; | |
11324 | else if (p->type == bfd_indirect_link_order) | |
11325 | { | |
11326 | asection *sec; | |
11327 | ||
11328 | sec = p->u.indirect.section; | |
11329 | esdi = elf_section_data (sec); | |
11330 | ||
11331 | /* Mark all sections which are to be included in the | |
11332 | link. This will normally be every section. We need | |
11333 | to do this so that we can identify any sections which | |
11334 | the linker has decided to not include. */ | |
11335 | sec->linker_mark = TRUE; | |
11336 | ||
11337 | if (sec->flags & SEC_MERGE) | |
11338 | merged = TRUE; | |
11339 | ||
aed64b35 L |
11340 | if (esdo->this_hdr.sh_type == SHT_REL |
11341 | || esdo->this_hdr.sh_type == SHT_RELA) | |
11342 | /* Some backends use reloc_count in relocation sections | |
11343 | to count particular types of relocs. Of course, | |
11344 | reloc sections themselves can't have relocations. */ | |
11345 | reloc_count = 0; | |
0e1862bb | 11346 | else if (emit_relocs) |
491d01d3 YU |
11347 | { |
11348 | reloc_count = sec->reloc_count; | |
11349 | if (bed->elf_backend_count_additional_relocs) | |
11350 | { | |
11351 | int c; | |
11352 | c = (*bed->elf_backend_count_additional_relocs) (sec); | |
11353 | additional_reloc_count += c; | |
11354 | } | |
11355 | } | |
c152c796 | 11356 | else if (bed->elf_backend_count_relocs) |
58217f29 | 11357 | reloc_count = (*bed->elf_backend_count_relocs) (info, sec); |
c152c796 | 11358 | |
eea6121a AM |
11359 | if (sec->rawsize > max_contents_size) |
11360 | max_contents_size = sec->rawsize; | |
11361 | if (sec->size > max_contents_size) | |
11362 | max_contents_size = sec->size; | |
c152c796 AM |
11363 | |
11364 | /* We are interested in just local symbols, not all | |
11365 | symbols. */ | |
11366 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
11367 | && (sec->owner->flags & DYNAMIC) == 0) | |
11368 | { | |
11369 | size_t sym_count; | |
11370 | ||
11371 | if (elf_bad_symtab (sec->owner)) | |
11372 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
11373 | / bed->s->sizeof_sym); | |
11374 | else | |
11375 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
11376 | ||
11377 | if (sym_count > max_sym_count) | |
11378 | max_sym_count = sym_count; | |
11379 | ||
11380 | if (sym_count > max_sym_shndx_count | |
6a40cf0c | 11381 | && elf_symtab_shndx_list (sec->owner) != NULL) |
c152c796 AM |
11382 | max_sym_shndx_count = sym_count; |
11383 | ||
11384 | if ((sec->flags & SEC_RELOC) != 0) | |
11385 | { | |
d4730f92 | 11386 | size_t ext_size = 0; |
c152c796 | 11387 | |
d4730f92 BS |
11388 | if (esdi->rel.hdr != NULL) |
11389 | ext_size = esdi->rel.hdr->sh_size; | |
11390 | if (esdi->rela.hdr != NULL) | |
11391 | ext_size += esdi->rela.hdr->sh_size; | |
7326c758 | 11392 | |
c152c796 AM |
11393 | if (ext_size > max_external_reloc_size) |
11394 | max_external_reloc_size = ext_size; | |
11395 | if (sec->reloc_count > max_internal_reloc_count) | |
11396 | max_internal_reloc_count = sec->reloc_count; | |
11397 | } | |
11398 | } | |
11399 | } | |
11400 | ||
11401 | if (reloc_count == 0) | |
11402 | continue; | |
11403 | ||
11404 | o->reloc_count += reloc_count; | |
11405 | ||
0e1862bb | 11406 | if (p->type == bfd_indirect_link_order && emit_relocs) |
c152c796 | 11407 | { |
d4730f92 | 11408 | if (esdi->rel.hdr) |
491d01d3 | 11409 | esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); |
d4730f92 | 11410 | if (esdi->rela.hdr) |
491d01d3 | 11411 | esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); |
d4730f92 BS |
11412 | } |
11413 | else | |
11414 | { | |
11415 | if (o->use_rela_p) | |
11416 | esdo->rela.count += reloc_count; | |
2c2b4ed4 | 11417 | else |
d4730f92 | 11418 | esdo->rel.count += reloc_count; |
c152c796 | 11419 | } |
c152c796 AM |
11420 | } |
11421 | ||
5025eb7c | 11422 | if (o->reloc_count > 0 || additional_reloc_count > 0) |
c152c796 AM |
11423 | o->flags |= SEC_RELOC; |
11424 | else | |
11425 | { | |
11426 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
11427 | set it (this is probably a bug) and if it is set | |
11428 | assign_section_numbers will create a reloc section. */ | |
11429 | o->flags &=~ SEC_RELOC; | |
11430 | } | |
11431 | ||
11432 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
11433 | zero. This is done in elf_fake_sections as well, but forcing | |
11434 | the VMA to 0 here will ensure that relocs against these | |
11435 | sections are handled correctly. */ | |
11436 | if ((o->flags & SEC_ALLOC) == 0 | |
11437 | && ! o->user_set_vma) | |
11438 | o->vma = 0; | |
11439 | } | |
11440 | ||
0e1862bb | 11441 | if (! bfd_link_relocatable (info) && merged) |
c152c796 AM |
11442 | elf_link_hash_traverse (elf_hash_table (info), |
11443 | _bfd_elf_link_sec_merge_syms, abfd); | |
11444 | ||
11445 | /* Figure out the file positions for everything but the symbol table | |
11446 | and the relocs. We set symcount to force assign_section_numbers | |
11447 | to create a symbol table. */ | |
8539e4e8 | 11448 | bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs; |
c152c796 AM |
11449 | BFD_ASSERT (! abfd->output_has_begun); |
11450 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
11451 | goto error_return; | |
11452 | ||
ee75fd95 | 11453 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
11454 | for (o = abfd->sections; o != NULL; o = o->next) |
11455 | { | |
d4730f92 | 11456 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
c152c796 AM |
11457 | if ((o->flags & SEC_RELOC) != 0) |
11458 | { | |
d4730f92 | 11459 | if (esdo->rel.hdr |
5025eb7c | 11460 | && !(_bfd_elf_link_size_reloc_section (abfd, info, o, FALSE))) |
c152c796 AM |
11461 | goto error_return; |
11462 | ||
d4730f92 | 11463 | if (esdo->rela.hdr |
5025eb7c | 11464 | && !(_bfd_elf_link_size_reloc_section (abfd, info, o, TRUE))) |
c152c796 AM |
11465 | goto error_return; |
11466 | } | |
11467 | ||
11468 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
11469 | to count upwards while actually outputting the relocations. */ | |
d4730f92 BS |
11470 | esdo->rel.count = 0; |
11471 | esdo->rela.count = 0; | |
0ce398f1 L |
11472 | |
11473 | if (esdo->this_hdr.sh_offset == (file_ptr) -1) | |
11474 | { | |
11475 | /* Cache the section contents so that they can be compressed | |
11476 | later. Use bfd_malloc since it will be freed by | |
11477 | bfd_compress_section_contents. */ | |
11478 | unsigned char *contents = esdo->this_hdr.contents; | |
11479 | if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) | |
11480 | abort (); | |
11481 | contents | |
11482 | = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); | |
11483 | if (contents == NULL) | |
11484 | goto error_return; | |
11485 | esdo->this_hdr.contents = contents; | |
11486 | } | |
c152c796 AM |
11487 | } |
11488 | ||
c152c796 | 11489 | /* We have now assigned file positions for all the sections except |
a485e98e AM |
11490 | .symtab, .strtab, and non-loaded reloc sections. We start the |
11491 | .symtab section at the current file position, and write directly | |
11492 | to it. We build the .strtab section in memory. */ | |
c152c796 AM |
11493 | bfd_get_symcount (abfd) = 0; |
11494 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11495 | /* sh_name is set in prep_headers. */ | |
11496 | symtab_hdr->sh_type = SHT_SYMTAB; | |
11497 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
11498 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
11499 | /* sh_link is set in assign_section_numbers. */ | |
11500 | /* sh_info is set below. */ | |
11501 | /* sh_offset is set just below. */ | |
72de5009 | 11502 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
c152c796 | 11503 | |
ef10c3ac L |
11504 | if (max_sym_count < 20) |
11505 | max_sym_count = 20; | |
11506 | elf_hash_table (info)->strtabsize = max_sym_count; | |
11507 | amt = max_sym_count * sizeof (struct elf_sym_strtab); | |
11508 | elf_hash_table (info)->strtab | |
11509 | = (struct elf_sym_strtab *) bfd_malloc (amt); | |
11510 | if (elf_hash_table (info)->strtab == NULL) | |
c152c796 | 11511 | goto error_return; |
ef10c3ac L |
11512 | /* The real buffer will be allocated in elf_link_swap_symbols_out. */ |
11513 | flinfo.symshndxbuf | |
11514 | = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) | |
11515 | ? (Elf_External_Sym_Shndx *) -1 : NULL); | |
c152c796 | 11516 | |
8539e4e8 | 11517 | if (info->strip != strip_all || emit_relocs) |
c152c796 | 11518 | { |
8539e4e8 AM |
11519 | file_ptr off = elf_next_file_pos (abfd); |
11520 | ||
11521 | _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
11522 | ||
11523 | /* Note that at this point elf_next_file_pos (abfd) is | |
11524 | incorrect. We do not yet know the size of the .symtab section. | |
11525 | We correct next_file_pos below, after we do know the size. */ | |
11526 | ||
11527 | /* Start writing out the symbol table. The first symbol is always a | |
11528 | dummy symbol. */ | |
c152c796 AM |
11529 | elfsym.st_value = 0; |
11530 | elfsym.st_size = 0; | |
11531 | elfsym.st_info = 0; | |
11532 | elfsym.st_other = 0; | |
11533 | elfsym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 11534 | elfsym.st_target_internal = 0; |
ef10c3ac L |
11535 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, |
11536 | bfd_und_section_ptr, NULL) != 1) | |
c152c796 | 11537 | goto error_return; |
c152c796 | 11538 | |
8539e4e8 AM |
11539 | /* Output a symbol for each section. We output these even if we are |
11540 | discarding local symbols, since they are used for relocs. These | |
11541 | symbols have no names. We store the index of each one in the | |
11542 | index field of the section, so that we can find it again when | |
11543 | outputting relocs. */ | |
11544 | ||
c152c796 AM |
11545 | elfsym.st_size = 0; |
11546 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
11547 | elfsym.st_other = 0; | |
f0b5bb34 | 11548 | elfsym.st_value = 0; |
35fc36a8 | 11549 | elfsym.st_target_internal = 0; |
c152c796 AM |
11550 | for (i = 1; i < elf_numsections (abfd); i++) |
11551 | { | |
11552 | o = bfd_section_from_elf_index (abfd, i); | |
11553 | if (o != NULL) | |
f0b5bb34 AM |
11554 | { |
11555 | o->target_index = bfd_get_symcount (abfd); | |
11556 | elfsym.st_shndx = i; | |
0e1862bb | 11557 | if (!bfd_link_relocatable (info)) |
f0b5bb34 | 11558 | elfsym.st_value = o->vma; |
ef10c3ac L |
11559 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o, |
11560 | NULL) != 1) | |
f0b5bb34 AM |
11561 | goto error_return; |
11562 | } | |
c152c796 AM |
11563 | } |
11564 | } | |
11565 | ||
11566 | /* Allocate some memory to hold information read in from the input | |
11567 | files. */ | |
11568 | if (max_contents_size != 0) | |
11569 | { | |
8b127cbc AM |
11570 | flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
11571 | if (flinfo.contents == NULL) | |
c152c796 AM |
11572 | goto error_return; |
11573 | } | |
11574 | ||
11575 | if (max_external_reloc_size != 0) | |
11576 | { | |
8b127cbc AM |
11577 | flinfo.external_relocs = bfd_malloc (max_external_reloc_size); |
11578 | if (flinfo.external_relocs == NULL) | |
c152c796 AM |
11579 | goto error_return; |
11580 | } | |
11581 | ||
11582 | if (max_internal_reloc_count != 0) | |
11583 | { | |
11584 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
11585 | amt *= sizeof (Elf_Internal_Rela); | |
8b127cbc AM |
11586 | flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); |
11587 | if (flinfo.internal_relocs == NULL) | |
c152c796 AM |
11588 | goto error_return; |
11589 | } | |
11590 | ||
11591 | if (max_sym_count != 0) | |
11592 | { | |
11593 | amt = max_sym_count * bed->s->sizeof_sym; | |
8b127cbc AM |
11594 | flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); |
11595 | if (flinfo.external_syms == NULL) | |
c152c796 AM |
11596 | goto error_return; |
11597 | ||
11598 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8b127cbc AM |
11599 | flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); |
11600 | if (flinfo.internal_syms == NULL) | |
c152c796 AM |
11601 | goto error_return; |
11602 | ||
11603 | amt = max_sym_count * sizeof (long); | |
8b127cbc AM |
11604 | flinfo.indices = (long int *) bfd_malloc (amt); |
11605 | if (flinfo.indices == NULL) | |
c152c796 AM |
11606 | goto error_return; |
11607 | ||
11608 | amt = max_sym_count * sizeof (asection *); | |
8b127cbc AM |
11609 | flinfo.sections = (asection **) bfd_malloc (amt); |
11610 | if (flinfo.sections == NULL) | |
c152c796 AM |
11611 | goto error_return; |
11612 | } | |
11613 | ||
11614 | if (max_sym_shndx_count != 0) | |
11615 | { | |
11616 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8b127cbc AM |
11617 | flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
11618 | if (flinfo.locsym_shndx == NULL) | |
c152c796 AM |
11619 | goto error_return; |
11620 | } | |
11621 | ||
11622 | if (elf_hash_table (info)->tls_sec) | |
11623 | { | |
11624 | bfd_vma base, end = 0; | |
11625 | asection *sec; | |
11626 | ||
11627 | for (sec = elf_hash_table (info)->tls_sec; | |
11628 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
11629 | sec = sec->next) | |
11630 | { | |
3a800eb9 | 11631 | bfd_size_type size = sec->size; |
c152c796 | 11632 | |
3a800eb9 AM |
11633 | if (size == 0 |
11634 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 11635 | { |
91d6fa6a NC |
11636 | struct bfd_link_order *ord = sec->map_tail.link_order; |
11637 | ||
11638 | if (ord != NULL) | |
11639 | size = ord->offset + ord->size; | |
c152c796 AM |
11640 | } |
11641 | end = sec->vma + size; | |
11642 | } | |
11643 | base = elf_hash_table (info)->tls_sec->vma; | |
7dc98aea RO |
11644 | /* Only align end of TLS section if static TLS doesn't have special |
11645 | alignment requirements. */ | |
11646 | if (bed->static_tls_alignment == 1) | |
11647 | end = align_power (end, | |
11648 | elf_hash_table (info)->tls_sec->alignment_power); | |
c152c796 AM |
11649 | elf_hash_table (info)->tls_size = end - base; |
11650 | } | |
11651 | ||
0b52efa6 PB |
11652 | /* Reorder SHF_LINK_ORDER sections. */ |
11653 | for (o = abfd->sections; o != NULL; o = o->next) | |
11654 | { | |
11655 | if (!elf_fixup_link_order (abfd, o)) | |
11656 | return FALSE; | |
11657 | } | |
11658 | ||
2f0c68f2 CM |
11659 | if (!_bfd_elf_fixup_eh_frame_hdr (info)) |
11660 | return FALSE; | |
11661 | ||
c152c796 AM |
11662 | /* Since ELF permits relocations to be against local symbols, we |
11663 | must have the local symbols available when we do the relocations. | |
11664 | Since we would rather only read the local symbols once, and we | |
11665 | would rather not keep them in memory, we handle all the | |
11666 | relocations for a single input file at the same time. | |
11667 | ||
11668 | Unfortunately, there is no way to know the total number of local | |
11669 | symbols until we have seen all of them, and the local symbol | |
11670 | indices precede the global symbol indices. This means that when | |
11671 | we are generating relocatable output, and we see a reloc against | |
11672 | a global symbol, we can not know the symbol index until we have | |
11673 | finished examining all the local symbols to see which ones we are | |
11674 | going to output. To deal with this, we keep the relocations in | |
11675 | memory, and don't output them until the end of the link. This is | |
11676 | an unfortunate waste of memory, but I don't see a good way around | |
11677 | it. Fortunately, it only happens when performing a relocatable | |
11678 | link, which is not the common case. FIXME: If keep_memory is set | |
11679 | we could write the relocs out and then read them again; I don't | |
11680 | know how bad the memory loss will be. */ | |
11681 | ||
c72f2fb2 | 11682 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
11683 | sub->output_has_begun = FALSE; |
11684 | for (o = abfd->sections; o != NULL; o = o->next) | |
11685 | { | |
8423293d | 11686 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
11687 | { |
11688 | if (p->type == bfd_indirect_link_order | |
11689 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
11690 | == bfd_target_elf_flavour) | |
11691 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
11692 | { | |
11693 | if (! sub->output_has_begun) | |
11694 | { | |
8b127cbc | 11695 | if (! elf_link_input_bfd (&flinfo, sub)) |
c152c796 AM |
11696 | goto error_return; |
11697 | sub->output_has_begun = TRUE; | |
11698 | } | |
11699 | } | |
11700 | else if (p->type == bfd_section_reloc_link_order | |
11701 | || p->type == bfd_symbol_reloc_link_order) | |
11702 | { | |
11703 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
11704 | goto error_return; | |
11705 | } | |
11706 | else | |
11707 | { | |
11708 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
351f65ca L |
11709 | { |
11710 | if (p->type == bfd_indirect_link_order | |
11711 | && (bfd_get_flavour (sub) | |
11712 | == bfd_target_elf_flavour) | |
11713 | && (elf_elfheader (sub)->e_ident[EI_CLASS] | |
11714 | != bed->s->elfclass)) | |
11715 | { | |
11716 | const char *iclass, *oclass; | |
11717 | ||
aebf9be7 | 11718 | switch (bed->s->elfclass) |
351f65ca | 11719 | { |
aebf9be7 NC |
11720 | case ELFCLASS64: oclass = "ELFCLASS64"; break; |
11721 | case ELFCLASS32: oclass = "ELFCLASS32"; break; | |
11722 | case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; | |
11723 | default: abort (); | |
351f65ca | 11724 | } |
aebf9be7 NC |
11725 | |
11726 | switch (elf_elfheader (sub)->e_ident[EI_CLASS]) | |
351f65ca | 11727 | { |
aebf9be7 NC |
11728 | case ELFCLASS64: iclass = "ELFCLASS64"; break; |
11729 | case ELFCLASS32: iclass = "ELFCLASS32"; break; | |
11730 | case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; | |
11731 | default: abort (); | |
351f65ca L |
11732 | } |
11733 | ||
11734 | bfd_set_error (bfd_error_wrong_format); | |
11735 | (*_bfd_error_handler) | |
11736 | (_("%B: file class %s incompatible with %s"), | |
11737 | sub, iclass, oclass); | |
11738 | } | |
11739 | ||
11740 | goto error_return; | |
11741 | } | |
c152c796 AM |
11742 | } |
11743 | } | |
11744 | } | |
11745 | ||
c0f00686 L |
11746 | /* Free symbol buffer if needed. */ |
11747 | if (!info->reduce_memory_overheads) | |
11748 | { | |
c72f2fb2 | 11749 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
3fcd97f1 JJ |
11750 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
11751 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
11752 | { |
11753 | free (elf_tdata (sub)->symbuf); | |
11754 | elf_tdata (sub)->symbuf = NULL; | |
11755 | } | |
11756 | } | |
11757 | ||
c152c796 AM |
11758 | /* Output any global symbols that got converted to local in a |
11759 | version script or due to symbol visibility. We do this in a | |
11760 | separate step since ELF requires all local symbols to appear | |
11761 | prior to any global symbols. FIXME: We should only do this if | |
11762 | some global symbols were, in fact, converted to become local. | |
11763 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
11764 | eoinfo.failed = FALSE; | |
8b127cbc | 11765 | eoinfo.flinfo = &flinfo; |
c152c796 | 11766 | eoinfo.localsyms = TRUE; |
34a79995 | 11767 | eoinfo.file_sym_done = FALSE; |
7686d77d | 11768 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
11769 | if (eoinfo.failed) |
11770 | return FALSE; | |
11771 | ||
4e617b1e PB |
11772 | /* If backend needs to output some local symbols not present in the hash |
11773 | table, do it now. */ | |
8539e4e8 AM |
11774 | if (bed->elf_backend_output_arch_local_syms |
11775 | && (info->strip != strip_all || emit_relocs)) | |
4e617b1e | 11776 | { |
6e0b88f1 | 11777 | typedef int (*out_sym_func) |
4e617b1e PB |
11778 | (void *, const char *, Elf_Internal_Sym *, asection *, |
11779 | struct elf_link_hash_entry *); | |
11780 | ||
11781 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
ef10c3ac L |
11782 | (abfd, info, &flinfo, |
11783 | (out_sym_func) elf_link_output_symstrtab))) | |
4e617b1e PB |
11784 | return FALSE; |
11785 | } | |
11786 | ||
c152c796 AM |
11787 | /* That wrote out all the local symbols. Finish up the symbol table |
11788 | with the global symbols. Even if we want to strip everything we | |
11789 | can, we still need to deal with those global symbols that got | |
11790 | converted to local in a version script. */ | |
11791 | ||
11792 | /* The sh_info field records the index of the first non local symbol. */ | |
11793 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
11794 | ||
11795 | if (dynamic | |
cae1fbbb L |
11796 | && elf_hash_table (info)->dynsym != NULL |
11797 | && (elf_hash_table (info)->dynsym->output_section | |
11798 | != bfd_abs_section_ptr)) | |
c152c796 AM |
11799 | { |
11800 | Elf_Internal_Sym sym; | |
cae1fbbb | 11801 | bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents; |
90ac2420 AM |
11802 | |
11803 | o = elf_hash_table (info)->dynsym->output_section; | |
11804 | elf_section_data (o)->this_hdr.sh_info | |
11805 | = elf_hash_table (info)->local_dynsymcount + 1; | |
c152c796 AM |
11806 | |
11807 | /* Write out the section symbols for the output sections. */ | |
0e1862bb L |
11808 | if (bfd_link_pic (info) |
11809 | || elf_hash_table (info)->is_relocatable_executable) | |
c152c796 AM |
11810 | { |
11811 | asection *s; | |
11812 | ||
11813 | sym.st_size = 0; | |
11814 | sym.st_name = 0; | |
11815 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
11816 | sym.st_other = 0; | |
35fc36a8 | 11817 | sym.st_target_internal = 0; |
c152c796 AM |
11818 | |
11819 | for (s = abfd->sections; s != NULL; s = s->next) | |
11820 | { | |
11821 | int indx; | |
11822 | bfd_byte *dest; | |
11823 | long dynindx; | |
11824 | ||
c152c796 | 11825 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
11826 | if (dynindx <= 0) |
11827 | continue; | |
11828 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
11829 | BFD_ASSERT (indx > 0); |
11830 | sym.st_shndx = indx; | |
c0d5a53d L |
11831 | if (! check_dynsym (abfd, &sym)) |
11832 | return FALSE; | |
c152c796 AM |
11833 | sym.st_value = s->vma; |
11834 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
11835 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
11836 | } | |
c152c796 AM |
11837 | } |
11838 | ||
11839 | /* Write out the local dynsyms. */ | |
11840 | if (elf_hash_table (info)->dynlocal) | |
11841 | { | |
11842 | struct elf_link_local_dynamic_entry *e; | |
11843 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
11844 | { | |
11845 | asection *s; | |
11846 | bfd_byte *dest; | |
11847 | ||
935bd1e0 | 11848 | /* Copy the internal symbol and turn off visibility. |
c152c796 AM |
11849 | Note that we saved a word of storage and overwrote |
11850 | the original st_name with the dynstr_index. */ | |
11851 | sym = e->isym; | |
935bd1e0 | 11852 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); |
c152c796 | 11853 | |
cb33740c AM |
11854 | s = bfd_section_from_elf_index (e->input_bfd, |
11855 | e->isym.st_shndx); | |
11856 | if (s != NULL) | |
c152c796 | 11857 | { |
c152c796 AM |
11858 | sym.st_shndx = |
11859 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
11860 | if (! check_dynsym (abfd, &sym)) |
11861 | return FALSE; | |
c152c796 AM |
11862 | sym.st_value = (s->output_section->vma |
11863 | + s->output_offset | |
11864 | + e->isym.st_value); | |
11865 | } | |
11866 | ||
c152c796 AM |
11867 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; |
11868 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
11869 | } | |
11870 | } | |
c152c796 AM |
11871 | } |
11872 | ||
11873 | /* We get the global symbols from the hash table. */ | |
11874 | eoinfo.failed = FALSE; | |
11875 | eoinfo.localsyms = FALSE; | |
8b127cbc | 11876 | eoinfo.flinfo = &flinfo; |
7686d77d | 11877 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
11878 | if (eoinfo.failed) |
11879 | return FALSE; | |
11880 | ||
11881 | /* If backend needs to output some symbols not present in the hash | |
11882 | table, do it now. */ | |
8539e4e8 AM |
11883 | if (bed->elf_backend_output_arch_syms |
11884 | && (info->strip != strip_all || emit_relocs)) | |
c152c796 | 11885 | { |
6e0b88f1 | 11886 | typedef int (*out_sym_func) |
c152c796 AM |
11887 | (void *, const char *, Elf_Internal_Sym *, asection *, |
11888 | struct elf_link_hash_entry *); | |
11889 | ||
11890 | if (! ((*bed->elf_backend_output_arch_syms) | |
ef10c3ac L |
11891 | (abfd, info, &flinfo, |
11892 | (out_sym_func) elf_link_output_symstrtab))) | |
c152c796 AM |
11893 | return FALSE; |
11894 | } | |
11895 | ||
ef10c3ac L |
11896 | /* Finalize the .strtab section. */ |
11897 | _bfd_elf_strtab_finalize (flinfo.symstrtab); | |
11898 | ||
11899 | /* Swap out the .strtab section. */ | |
11900 | if (!elf_link_swap_symbols_out (&flinfo)) | |
c152c796 AM |
11901 | return FALSE; |
11902 | ||
11903 | /* Now we know the size of the symtab section. */ | |
c152c796 AM |
11904 | if (bfd_get_symcount (abfd) > 0) |
11905 | { | |
ee3b52e9 L |
11906 | /* Finish up and write out the symbol string table (.strtab) |
11907 | section. */ | |
11908 | Elf_Internal_Shdr *symstrtab_hdr; | |
8539e4e8 AM |
11909 | file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; |
11910 | ||
6a40cf0c NC |
11911 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
11912 | if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) | |
8539e4e8 AM |
11913 | { |
11914 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
11915 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
11916 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
11917 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
11918 | symtab_shndx_hdr->sh_size = amt; | |
11919 | ||
11920 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
11921 | off, TRUE); | |
11922 | ||
11923 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
11924 | || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) | |
11925 | return FALSE; | |
11926 | } | |
ee3b52e9 L |
11927 | |
11928 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
11929 | /* sh_name was set in prep_headers. */ | |
11930 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 11931 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
ee3b52e9 | 11932 | symstrtab_hdr->sh_addr = 0; |
ef10c3ac | 11933 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); |
ee3b52e9 L |
11934 | symstrtab_hdr->sh_entsize = 0; |
11935 | symstrtab_hdr->sh_link = 0; | |
11936 | symstrtab_hdr->sh_info = 0; | |
11937 | /* sh_offset is set just below. */ | |
11938 | symstrtab_hdr->sh_addralign = 1; | |
11939 | ||
11940 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, | |
11941 | off, TRUE); | |
11942 | elf_next_file_pos (abfd) = off; | |
11943 | ||
c152c796 | 11944 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 11945 | || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) |
c152c796 AM |
11946 | return FALSE; |
11947 | } | |
11948 | ||
76359541 TP |
11949 | if (info->out_implib_bfd && !elf_output_implib (abfd, info)) |
11950 | { | |
11951 | (*_bfd_error_handler) (_("%B: failed to generate import library"), | |
11952 | info->out_implib_bfd); | |
11953 | return FALSE; | |
11954 | } | |
11955 | ||
c152c796 AM |
11956 | /* Adjust the relocs to have the correct symbol indices. */ |
11957 | for (o = abfd->sections; o != NULL; o = o->next) | |
11958 | { | |
d4730f92 | 11959 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
28dbcedc | 11960 | bfd_boolean sort; |
c152c796 AM |
11961 | if ((o->flags & SEC_RELOC) == 0) |
11962 | continue; | |
11963 | ||
28dbcedc | 11964 | sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); |
bca6d0e3 AM |
11965 | if (esdo->rel.hdr != NULL |
11966 | && !elf_link_adjust_relocs (abfd, &esdo->rel, sort)) | |
11967 | return FALSE; | |
11968 | if (esdo->rela.hdr != NULL | |
11969 | && !elf_link_adjust_relocs (abfd, &esdo->rela, sort)) | |
11970 | return FALSE; | |
c152c796 AM |
11971 | |
11972 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
11973 | trying to swap the relocs out itself. */ | |
11974 | o->reloc_count = 0; | |
11975 | } | |
11976 | ||
11977 | if (dynamic && info->combreloc && dynobj != NULL) | |
11978 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
11979 | ||
11980 | /* If we are linking against a dynamic object, or generating a | |
11981 | shared library, finish up the dynamic linking information. */ | |
11982 | if (dynamic) | |
11983 | { | |
11984 | bfd_byte *dyncon, *dynconend; | |
11985 | ||
11986 | /* Fix up .dynamic entries. */ | |
3d4d4302 | 11987 | o = bfd_get_linker_section (dynobj, ".dynamic"); |
c152c796 AM |
11988 | BFD_ASSERT (o != NULL); |
11989 | ||
11990 | dyncon = o->contents; | |
eea6121a | 11991 | dynconend = o->contents + o->size; |
c152c796 AM |
11992 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
11993 | { | |
11994 | Elf_Internal_Dyn dyn; | |
11995 | const char *name; | |
11996 | unsigned int type; | |
11997 | ||
11998 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
11999 | ||
12000 | switch (dyn.d_tag) | |
12001 | { | |
12002 | default: | |
12003 | continue; | |
12004 | case DT_NULL: | |
12005 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
12006 | { | |
12007 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
12008 | { | |
12009 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
12010 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
12011 | default: continue; | |
12012 | } | |
12013 | dyn.d_un.d_val = relativecount; | |
12014 | relativecount = 0; | |
12015 | break; | |
12016 | } | |
12017 | continue; | |
12018 | ||
12019 | case DT_INIT: | |
12020 | name = info->init_function; | |
12021 | goto get_sym; | |
12022 | case DT_FINI: | |
12023 | name = info->fini_function; | |
12024 | get_sym: | |
12025 | { | |
12026 | struct elf_link_hash_entry *h; | |
12027 | ||
12028 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
12029 | FALSE, FALSE, TRUE); | |
12030 | if (h != NULL | |
12031 | && (h->root.type == bfd_link_hash_defined | |
12032 | || h->root.type == bfd_link_hash_defweak)) | |
12033 | { | |
bef26483 | 12034 | dyn.d_un.d_ptr = h->root.u.def.value; |
c152c796 AM |
12035 | o = h->root.u.def.section; |
12036 | if (o->output_section != NULL) | |
bef26483 | 12037 | dyn.d_un.d_ptr += (o->output_section->vma |
c152c796 AM |
12038 | + o->output_offset); |
12039 | else | |
12040 | { | |
12041 | /* The symbol is imported from another shared | |
12042 | library and does not apply to this one. */ | |
bef26483 | 12043 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
12044 | } |
12045 | break; | |
12046 | } | |
12047 | } | |
12048 | continue; | |
12049 | ||
12050 | case DT_PREINIT_ARRAYSZ: | |
12051 | name = ".preinit_array"; | |
4ade44b7 | 12052 | goto get_out_size; |
c152c796 AM |
12053 | case DT_INIT_ARRAYSZ: |
12054 | name = ".init_array"; | |
4ade44b7 | 12055 | goto get_out_size; |
c152c796 AM |
12056 | case DT_FINI_ARRAYSZ: |
12057 | name = ".fini_array"; | |
4ade44b7 | 12058 | get_out_size: |
c152c796 AM |
12059 | o = bfd_get_section_by_name (abfd, name); |
12060 | if (o == NULL) | |
12061 | { | |
12062 | (*_bfd_error_handler) | |
4ade44b7 | 12063 | (_("could not find section %s"), name); |
c152c796 AM |
12064 | goto error_return; |
12065 | } | |
eea6121a | 12066 | if (o->size == 0) |
c152c796 AM |
12067 | (*_bfd_error_handler) |
12068 | (_("warning: %s section has zero size"), name); | |
eea6121a | 12069 | dyn.d_un.d_val = o->size; |
c152c796 AM |
12070 | break; |
12071 | ||
12072 | case DT_PREINIT_ARRAY: | |
12073 | name = ".preinit_array"; | |
4ade44b7 | 12074 | goto get_out_vma; |
c152c796 AM |
12075 | case DT_INIT_ARRAY: |
12076 | name = ".init_array"; | |
4ade44b7 | 12077 | goto get_out_vma; |
c152c796 AM |
12078 | case DT_FINI_ARRAY: |
12079 | name = ".fini_array"; | |
4ade44b7 AM |
12080 | get_out_vma: |
12081 | o = bfd_get_section_by_name (abfd, name); | |
12082 | goto do_vma; | |
c152c796 AM |
12083 | |
12084 | case DT_HASH: | |
12085 | name = ".hash"; | |
12086 | goto get_vma; | |
fdc90cb4 JJ |
12087 | case DT_GNU_HASH: |
12088 | name = ".gnu.hash"; | |
12089 | goto get_vma; | |
c152c796 AM |
12090 | case DT_STRTAB: |
12091 | name = ".dynstr"; | |
12092 | goto get_vma; | |
12093 | case DT_SYMTAB: | |
12094 | name = ".dynsym"; | |
12095 | goto get_vma; | |
12096 | case DT_VERDEF: | |
12097 | name = ".gnu.version_d"; | |
12098 | goto get_vma; | |
12099 | case DT_VERNEED: | |
12100 | name = ".gnu.version_r"; | |
12101 | goto get_vma; | |
12102 | case DT_VERSYM: | |
12103 | name = ".gnu.version"; | |
12104 | get_vma: | |
4ade44b7 AM |
12105 | o = bfd_get_linker_section (dynobj, name); |
12106 | do_vma: | |
c152c796 AM |
12107 | if (o == NULL) |
12108 | { | |
12109 | (*_bfd_error_handler) | |
4ade44b7 | 12110 | (_("could not find section %s"), name); |
c152c796 AM |
12111 | goto error_return; |
12112 | } | |
894891db NC |
12113 | if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) |
12114 | { | |
12115 | (*_bfd_error_handler) | |
12116 | (_("warning: section '%s' is being made into a note"), name); | |
12117 | bfd_set_error (bfd_error_nonrepresentable_section); | |
12118 | goto error_return; | |
12119 | } | |
4ade44b7 | 12120 | dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; |
c152c796 AM |
12121 | break; |
12122 | ||
12123 | case DT_REL: | |
12124 | case DT_RELA: | |
12125 | case DT_RELSZ: | |
12126 | case DT_RELASZ: | |
12127 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
12128 | type = SHT_REL; | |
12129 | else | |
12130 | type = SHT_RELA; | |
12131 | dyn.d_un.d_val = 0; | |
bef26483 | 12132 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
12133 | for (i = 1; i < elf_numsections (abfd); i++) |
12134 | { | |
12135 | Elf_Internal_Shdr *hdr; | |
12136 | ||
12137 | hdr = elf_elfsections (abfd)[i]; | |
12138 | if (hdr->sh_type == type | |
12139 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
12140 | { | |
12141 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
12142 | dyn.d_un.d_val += hdr->sh_size; | |
12143 | else | |
12144 | { | |
bef26483 AM |
12145 | if (dyn.d_un.d_ptr == 0 |
12146 | || hdr->sh_addr < dyn.d_un.d_ptr) | |
12147 | dyn.d_un.d_ptr = hdr->sh_addr; | |
c152c796 AM |
12148 | } |
12149 | } | |
12150 | } | |
12151 | break; | |
12152 | } | |
12153 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
12154 | } | |
12155 | } | |
12156 | ||
12157 | /* If we have created any dynamic sections, then output them. */ | |
12158 | if (dynobj != NULL) | |
12159 | { | |
12160 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
12161 | goto error_return; | |
12162 | ||
943284cc | 12163 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
0e1862bb | 12164 | if (((info->warn_shared_textrel && bfd_link_pic (info)) |
be7b303d | 12165 | || info->error_textrel) |
3d4d4302 | 12166 | && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) |
943284cc DJ |
12167 | { |
12168 | bfd_byte *dyncon, *dynconend; | |
12169 | ||
943284cc DJ |
12170 | dyncon = o->contents; |
12171 | dynconend = o->contents + o->size; | |
12172 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
12173 | { | |
12174 | Elf_Internal_Dyn dyn; | |
12175 | ||
12176 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12177 | ||
12178 | if (dyn.d_tag == DT_TEXTREL) | |
12179 | { | |
c192a133 AM |
12180 | if (info->error_textrel) |
12181 | info->callbacks->einfo | |
12182 | (_("%P%X: read-only segment has dynamic relocations.\n")); | |
12183 | else | |
12184 | info->callbacks->einfo | |
12185 | (_("%P: warning: creating a DT_TEXTREL in a shared object.\n")); | |
943284cc DJ |
12186 | break; |
12187 | } | |
12188 | } | |
12189 | } | |
12190 | ||
c152c796 AM |
12191 | for (o = dynobj->sections; o != NULL; o = o->next) |
12192 | { | |
12193 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 12194 | || o->size == 0 |
c152c796 AM |
12195 | || o->output_section == bfd_abs_section_ptr) |
12196 | continue; | |
12197 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
12198 | { | |
12199 | /* At this point, we are only interested in sections | |
12200 | created by _bfd_elf_link_create_dynamic_sections. */ | |
12201 | continue; | |
12202 | } | |
3722b82f AM |
12203 | if (elf_hash_table (info)->stab_info.stabstr == o) |
12204 | continue; | |
eea6121a AM |
12205 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
12206 | continue; | |
3d4d4302 | 12207 | if (strcmp (o->name, ".dynstr") != 0) |
c152c796 AM |
12208 | { |
12209 | if (! bfd_set_section_contents (abfd, o->output_section, | |
12210 | o->contents, | |
37b01f6a DG |
12211 | (file_ptr) o->output_offset |
12212 | * bfd_octets_per_byte (abfd), | |
eea6121a | 12213 | o->size)) |
c152c796 AM |
12214 | goto error_return; |
12215 | } | |
12216 | else | |
12217 | { | |
12218 | /* The contents of the .dynstr section are actually in a | |
12219 | stringtab. */ | |
8539e4e8 AM |
12220 | file_ptr off; |
12221 | ||
c152c796 AM |
12222 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; |
12223 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
12224 | || ! _bfd_elf_strtab_emit (abfd, | |
12225 | elf_hash_table (info)->dynstr)) | |
12226 | goto error_return; | |
12227 | } | |
12228 | } | |
12229 | } | |
12230 | ||
0e1862bb | 12231 | if (bfd_link_relocatable (info)) |
c152c796 AM |
12232 | { |
12233 | bfd_boolean failed = FALSE; | |
12234 | ||
12235 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
12236 | if (failed) | |
12237 | goto error_return; | |
12238 | } | |
12239 | ||
12240 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 12241 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
12242 | { |
12243 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
12244 | goto error_return; | |
12245 | } | |
12246 | ||
9f7c3e5e AM |
12247 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) |
12248 | goto error_return; | |
c152c796 | 12249 | |
9f7c3e5e | 12250 | elf_final_link_free (abfd, &flinfo); |
c152c796 | 12251 | |
12bd6957 | 12252 | elf_linker (abfd) = TRUE; |
c152c796 | 12253 | |
104d59d1 JM |
12254 | if (attr_section) |
12255 | { | |
a50b1753 | 12256 | bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); |
104d59d1 | 12257 | if (contents == NULL) |
d0f16d5e | 12258 | return FALSE; /* Bail out and fail. */ |
104d59d1 JM |
12259 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
12260 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
12261 | free (contents); | |
12262 | } | |
12263 | ||
c152c796 AM |
12264 | return TRUE; |
12265 | ||
12266 | error_return: | |
9f7c3e5e | 12267 | elf_final_link_free (abfd, &flinfo); |
c152c796 AM |
12268 | return FALSE; |
12269 | } | |
12270 | \f | |
5241d853 RS |
12271 | /* Initialize COOKIE for input bfd ABFD. */ |
12272 | ||
12273 | static bfd_boolean | |
12274 | init_reloc_cookie (struct elf_reloc_cookie *cookie, | |
12275 | struct bfd_link_info *info, bfd *abfd) | |
12276 | { | |
12277 | Elf_Internal_Shdr *symtab_hdr; | |
12278 | const struct elf_backend_data *bed; | |
12279 | ||
12280 | bed = get_elf_backend_data (abfd); | |
12281 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12282 | ||
12283 | cookie->abfd = abfd; | |
12284 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
12285 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
12286 | if (cookie->bad_symtab) | |
12287 | { | |
12288 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
12289 | cookie->extsymoff = 0; | |
12290 | } | |
12291 | else | |
12292 | { | |
12293 | cookie->locsymcount = symtab_hdr->sh_info; | |
12294 | cookie->extsymoff = symtab_hdr->sh_info; | |
12295 | } | |
12296 | ||
12297 | if (bed->s->arch_size == 32) | |
12298 | cookie->r_sym_shift = 8; | |
12299 | else | |
12300 | cookie->r_sym_shift = 32; | |
12301 | ||
12302 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
12303 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
12304 | { | |
12305 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
12306 | cookie->locsymcount, 0, | |
12307 | NULL, NULL, NULL); | |
12308 | if (cookie->locsyms == NULL) | |
12309 | { | |
12310 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
12311 | return FALSE; | |
12312 | } | |
12313 | if (info->keep_memory) | |
12314 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
12315 | } | |
12316 | return TRUE; | |
12317 | } | |
12318 | ||
12319 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
12320 | ||
12321 | static void | |
12322 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
12323 | { | |
12324 | Elf_Internal_Shdr *symtab_hdr; | |
12325 | ||
12326 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12327 | if (cookie->locsyms != NULL | |
12328 | && symtab_hdr->contents != (unsigned char *) cookie->locsyms) | |
12329 | free (cookie->locsyms); | |
12330 | } | |
12331 | ||
12332 | /* Initialize the relocation information in COOKIE for input section SEC | |
12333 | of input bfd ABFD. */ | |
12334 | ||
12335 | static bfd_boolean | |
12336 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12337 | struct bfd_link_info *info, bfd *abfd, | |
12338 | asection *sec) | |
12339 | { | |
12340 | const struct elf_backend_data *bed; | |
12341 | ||
12342 | if (sec->reloc_count == 0) | |
12343 | { | |
12344 | cookie->rels = NULL; | |
12345 | cookie->relend = NULL; | |
12346 | } | |
12347 | else | |
12348 | { | |
12349 | bed = get_elf_backend_data (abfd); | |
12350 | ||
12351 | cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
12352 | info->keep_memory); | |
12353 | if (cookie->rels == NULL) | |
12354 | return FALSE; | |
12355 | cookie->rel = cookie->rels; | |
12356 | cookie->relend = (cookie->rels | |
12357 | + sec->reloc_count * bed->s->int_rels_per_ext_rel); | |
12358 | } | |
12359 | cookie->rel = cookie->rels; | |
12360 | return TRUE; | |
12361 | } | |
12362 | ||
12363 | /* Free the memory allocated by init_reloc_cookie_rels, | |
12364 | if appropriate. */ | |
12365 | ||
12366 | static void | |
12367 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12368 | asection *sec) | |
12369 | { | |
12370 | if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) | |
12371 | free (cookie->rels); | |
12372 | } | |
12373 | ||
12374 | /* Initialize the whole of COOKIE for input section SEC. */ | |
12375 | ||
12376 | static bfd_boolean | |
12377 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12378 | struct bfd_link_info *info, | |
12379 | asection *sec) | |
12380 | { | |
12381 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
12382 | goto error1; | |
12383 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
12384 | goto error2; | |
12385 | return TRUE; | |
12386 | ||
12387 | error2: | |
12388 | fini_reloc_cookie (cookie, sec->owner); | |
12389 | error1: | |
12390 | return FALSE; | |
12391 | } | |
12392 | ||
12393 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
12394 | if appropriate. */ | |
12395 | ||
12396 | static void | |
12397 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12398 | asection *sec) | |
12399 | { | |
12400 | fini_reloc_cookie_rels (cookie, sec); | |
12401 | fini_reloc_cookie (cookie, sec->owner); | |
12402 | } | |
12403 | \f | |
c152c796 AM |
12404 | /* Garbage collect unused sections. */ |
12405 | ||
07adf181 AM |
12406 | /* Default gc_mark_hook. */ |
12407 | ||
12408 | asection * | |
12409 | _bfd_elf_gc_mark_hook (asection *sec, | |
12410 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12411 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
12412 | struct elf_link_hash_entry *h, | |
12413 | Elf_Internal_Sym *sym) | |
12414 | { | |
12415 | if (h != NULL) | |
12416 | { | |
12417 | switch (h->root.type) | |
12418 | { | |
12419 | case bfd_link_hash_defined: | |
12420 | case bfd_link_hash_defweak: | |
12421 | return h->root.u.def.section; | |
12422 | ||
12423 | case bfd_link_hash_common: | |
12424 | return h->root.u.c.p->section; | |
12425 | ||
12426 | default: | |
12427 | break; | |
12428 | } | |
12429 | } | |
12430 | else | |
12431 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
12432 | ||
12433 | return NULL; | |
12434 | } | |
12435 | ||
a6a4679f AM |
12436 | /* For undefined __start_<name> and __stop_<name> symbols, return the |
12437 | first input section matching <name>. Return NULL otherwise. */ | |
12438 | ||
12439 | asection * | |
12440 | _bfd_elf_is_start_stop (const struct bfd_link_info *info, | |
12441 | struct elf_link_hash_entry *h) | |
12442 | { | |
12443 | asection *s; | |
12444 | const char *sec_name; | |
12445 | ||
12446 | if (h->root.type != bfd_link_hash_undefined | |
12447 | && h->root.type != bfd_link_hash_undefweak) | |
12448 | return NULL; | |
12449 | ||
12450 | s = h->root.u.undef.section; | |
12451 | if (s != NULL) | |
12452 | { | |
12453 | if (s == (asection *) 0 - 1) | |
12454 | return NULL; | |
12455 | return s; | |
12456 | } | |
12457 | ||
12458 | sec_name = NULL; | |
12459 | if (strncmp (h->root.root.string, "__start_", 8) == 0) | |
12460 | sec_name = h->root.root.string + 8; | |
12461 | else if (strncmp (h->root.root.string, "__stop_", 7) == 0) | |
12462 | sec_name = h->root.root.string + 7; | |
12463 | ||
12464 | if (sec_name != NULL && *sec_name != '\0') | |
12465 | { | |
12466 | bfd *i; | |
12467 | ||
12468 | for (i = info->input_bfds; i != NULL; i = i->link.next) | |
12469 | { | |
12470 | s = bfd_get_section_by_name (i, sec_name); | |
12471 | if (s != NULL) | |
12472 | { | |
12473 | h->root.u.undef.section = s; | |
12474 | break; | |
12475 | } | |
12476 | } | |
12477 | } | |
12478 | ||
12479 | if (s == NULL) | |
12480 | h->root.u.undef.section = (asection *) 0 - 1; | |
12481 | ||
12482 | return s; | |
12483 | } | |
12484 | ||
5241d853 RS |
12485 | /* COOKIE->rel describes a relocation against section SEC, which is |
12486 | a section we've decided to keep. Return the section that contains | |
12487 | the relocation symbol, or NULL if no section contains it. */ | |
12488 | ||
12489 | asection * | |
12490 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
12491 | elf_gc_mark_hook_fn gc_mark_hook, | |
1cce69b9 AM |
12492 | struct elf_reloc_cookie *cookie, |
12493 | bfd_boolean *start_stop) | |
5241d853 RS |
12494 | { |
12495 | unsigned long r_symndx; | |
12496 | struct elf_link_hash_entry *h; | |
12497 | ||
12498 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
cf35638d | 12499 | if (r_symndx == STN_UNDEF) |
5241d853 RS |
12500 | return NULL; |
12501 | ||
12502 | if (r_symndx >= cookie->locsymcount | |
12503 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
12504 | { | |
12505 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
263ddf68 L |
12506 | if (h == NULL) |
12507 | { | |
12508 | info->callbacks->einfo (_("%F%P: corrupt input: %B\n"), | |
12509 | sec->owner); | |
12510 | return NULL; | |
12511 | } | |
5241d853 RS |
12512 | while (h->root.type == bfd_link_hash_indirect |
12513 | || h->root.type == bfd_link_hash_warning) | |
12514 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1d5316ab | 12515 | h->mark = 1; |
4e6b54a6 AM |
12516 | /* If this symbol is weak and there is a non-weak definition, we |
12517 | keep the non-weak definition because many backends put | |
12518 | dynamic reloc info on the non-weak definition for code | |
12519 | handling copy relocs. */ | |
12520 | if (h->u.weakdef != NULL) | |
12521 | h->u.weakdef->mark = 1; | |
1cce69b9 | 12522 | |
a6a4679f | 12523 | if (start_stop != NULL) |
1cce69b9 AM |
12524 | { |
12525 | /* To work around a glibc bug, mark all XXX input sections | |
12526 | when there is an as yet undefined reference to __start_XXX | |
12527 | or __stop_XXX symbols. The linker will later define such | |
12528 | symbols for orphan input sections that have a name | |
12529 | representable as a C identifier. */ | |
a6a4679f | 12530 | asection *s = _bfd_elf_is_start_stop (info, h); |
1cce69b9 | 12531 | |
a6a4679f | 12532 | if (s != NULL) |
1cce69b9 | 12533 | { |
a6a4679f AM |
12534 | *start_stop = !s->gc_mark; |
12535 | return s; | |
1cce69b9 AM |
12536 | } |
12537 | } | |
12538 | ||
5241d853 RS |
12539 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); |
12540 | } | |
12541 | ||
12542 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
12543 | &cookie->locsyms[r_symndx]); | |
12544 | } | |
12545 | ||
12546 | /* COOKIE->rel describes a relocation against section SEC, which is | |
12547 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 12548 | the relocation symbol. */ |
5241d853 RS |
12549 | |
12550 | bfd_boolean | |
12551 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, | |
12552 | asection *sec, | |
12553 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 12554 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
12555 | { |
12556 | asection *rsec; | |
1cce69b9 | 12557 | bfd_boolean start_stop = FALSE; |
5241d853 | 12558 | |
1cce69b9 AM |
12559 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); |
12560 | while (rsec != NULL) | |
5241d853 | 12561 | { |
1cce69b9 AM |
12562 | if (!rsec->gc_mark) |
12563 | { | |
12564 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour | |
12565 | || (rsec->owner->flags & DYNAMIC) != 0) | |
12566 | rsec->gc_mark = 1; | |
12567 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) | |
12568 | return FALSE; | |
12569 | } | |
12570 | if (!start_stop) | |
12571 | break; | |
199af150 | 12572 | rsec = bfd_get_next_section_by_name (rsec->owner, rsec); |
5241d853 RS |
12573 | } |
12574 | return TRUE; | |
12575 | } | |
12576 | ||
07adf181 AM |
12577 | /* The mark phase of garbage collection. For a given section, mark |
12578 | it and any sections in this section's group, and all the sections | |
12579 | which define symbols to which it refers. */ | |
12580 | ||
ccfa59ea AM |
12581 | bfd_boolean |
12582 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
12583 | asection *sec, | |
6a5bb875 | 12584 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
12585 | { |
12586 | bfd_boolean ret; | |
9d0a14d3 | 12587 | asection *group_sec, *eh_frame; |
c152c796 AM |
12588 | |
12589 | sec->gc_mark = 1; | |
12590 | ||
12591 | /* Mark all the sections in the group. */ | |
12592 | group_sec = elf_section_data (sec)->next_in_group; | |
12593 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 12594 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
12595 | return FALSE; |
12596 | ||
12597 | /* Look through the section relocs. */ | |
12598 | ret = TRUE; | |
9d0a14d3 RS |
12599 | eh_frame = elf_eh_frame_section (sec->owner); |
12600 | if ((sec->flags & SEC_RELOC) != 0 | |
12601 | && sec->reloc_count > 0 | |
12602 | && sec != eh_frame) | |
c152c796 | 12603 | { |
5241d853 | 12604 | struct elf_reloc_cookie cookie; |
c152c796 | 12605 | |
5241d853 RS |
12606 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
12607 | ret = FALSE; | |
c152c796 | 12608 | else |
c152c796 | 12609 | { |
5241d853 | 12610 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 12611 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 RS |
12612 | { |
12613 | ret = FALSE; | |
12614 | break; | |
12615 | } | |
12616 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
12617 | } |
12618 | } | |
9d0a14d3 RS |
12619 | |
12620 | if (ret && eh_frame && elf_fde_list (sec)) | |
12621 | { | |
12622 | struct elf_reloc_cookie cookie; | |
12623 | ||
12624 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
12625 | ret = FALSE; | |
12626 | else | |
12627 | { | |
12628 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
12629 | gc_mark_hook, &cookie)) | |
12630 | ret = FALSE; | |
12631 | fini_reloc_cookie_for_section (&cookie, eh_frame); | |
12632 | } | |
12633 | } | |
12634 | ||
2f0c68f2 CM |
12635 | eh_frame = elf_section_eh_frame_entry (sec); |
12636 | if (ret && eh_frame && !eh_frame->gc_mark) | |
12637 | if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) | |
12638 | ret = FALSE; | |
12639 | ||
c152c796 AM |
12640 | return ret; |
12641 | } | |
12642 | ||
3c758495 TG |
12643 | /* Scan and mark sections in a special or debug section group. */ |
12644 | ||
12645 | static void | |
12646 | _bfd_elf_gc_mark_debug_special_section_group (asection *grp) | |
12647 | { | |
12648 | /* Point to first section of section group. */ | |
12649 | asection *ssec; | |
12650 | /* Used to iterate the section group. */ | |
12651 | asection *msec; | |
12652 | ||
12653 | bfd_boolean is_special_grp = TRUE; | |
12654 | bfd_boolean is_debug_grp = TRUE; | |
12655 | ||
12656 | /* First scan to see if group contains any section other than debug | |
12657 | and special section. */ | |
12658 | ssec = msec = elf_next_in_group (grp); | |
12659 | do | |
12660 | { | |
12661 | if ((msec->flags & SEC_DEBUGGING) == 0) | |
12662 | is_debug_grp = FALSE; | |
12663 | ||
12664 | if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) | |
12665 | is_special_grp = FALSE; | |
12666 | ||
12667 | msec = elf_next_in_group (msec); | |
12668 | } | |
12669 | while (msec != ssec); | |
12670 | ||
12671 | /* If this is a pure debug section group or pure special section group, | |
12672 | keep all sections in this group. */ | |
12673 | if (is_debug_grp || is_special_grp) | |
12674 | { | |
12675 | do | |
12676 | { | |
12677 | msec->gc_mark = 1; | |
12678 | msec = elf_next_in_group (msec); | |
12679 | } | |
12680 | while (msec != ssec); | |
12681 | } | |
12682 | } | |
12683 | ||
7f6ab9f8 AM |
12684 | /* Keep debug and special sections. */ |
12685 | ||
12686 | bfd_boolean | |
12687 | _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, | |
12688 | elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) | |
12689 | { | |
12690 | bfd *ibfd; | |
12691 | ||
c72f2fb2 | 12692 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
7f6ab9f8 AM |
12693 | { |
12694 | asection *isec; | |
12695 | bfd_boolean some_kept; | |
b40bf0a2 | 12696 | bfd_boolean debug_frag_seen; |
7f6ab9f8 AM |
12697 | |
12698 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
12699 | continue; | |
12700 | ||
b40bf0a2 NC |
12701 | /* Ensure all linker created sections are kept, |
12702 | see if any other section is already marked, | |
12703 | and note if we have any fragmented debug sections. */ | |
12704 | debug_frag_seen = some_kept = FALSE; | |
7f6ab9f8 AM |
12705 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
12706 | { | |
12707 | if ((isec->flags & SEC_LINKER_CREATED) != 0) | |
12708 | isec->gc_mark = 1; | |
12709 | else if (isec->gc_mark) | |
12710 | some_kept = TRUE; | |
b40bf0a2 NC |
12711 | |
12712 | if (debug_frag_seen == FALSE | |
12713 | && (isec->flags & SEC_DEBUGGING) | |
12714 | && CONST_STRNEQ (isec->name, ".debug_line.")) | |
12715 | debug_frag_seen = TRUE; | |
7f6ab9f8 AM |
12716 | } |
12717 | ||
12718 | /* If no section in this file will be kept, then we can | |
b40bf0a2 | 12719 | toss out the debug and special sections. */ |
7f6ab9f8 AM |
12720 | if (!some_kept) |
12721 | continue; | |
12722 | ||
12723 | /* Keep debug and special sections like .comment when they are | |
3c758495 TG |
12724 | not part of a group. Also keep section groups that contain |
12725 | just debug sections or special sections. */ | |
7f6ab9f8 | 12726 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
3c758495 TG |
12727 | { |
12728 | if ((isec->flags & SEC_GROUP) != 0) | |
12729 | _bfd_elf_gc_mark_debug_special_section_group (isec); | |
12730 | else if (((isec->flags & SEC_DEBUGGING) != 0 | |
12731 | || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) | |
12732 | && elf_next_in_group (isec) == NULL) | |
12733 | isec->gc_mark = 1; | |
12734 | } | |
b40bf0a2 NC |
12735 | |
12736 | if (! debug_frag_seen) | |
12737 | continue; | |
12738 | ||
12739 | /* Look for CODE sections which are going to be discarded, | |
12740 | and find and discard any fragmented debug sections which | |
12741 | are associated with that code section. */ | |
12742 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
12743 | if ((isec->flags & SEC_CODE) != 0 | |
12744 | && isec->gc_mark == 0) | |
12745 | { | |
12746 | unsigned int ilen; | |
12747 | asection *dsec; | |
12748 | ||
12749 | ilen = strlen (isec->name); | |
12750 | ||
12751 | /* Association is determined by the name of the debug section | |
12752 | containing the name of the code section as a suffix. For | |
12753 | example .debug_line.text.foo is a debug section associated | |
12754 | with .text.foo. */ | |
12755 | for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) | |
12756 | { | |
12757 | unsigned int dlen; | |
12758 | ||
12759 | if (dsec->gc_mark == 0 | |
12760 | || (dsec->flags & SEC_DEBUGGING) == 0) | |
12761 | continue; | |
12762 | ||
12763 | dlen = strlen (dsec->name); | |
12764 | ||
12765 | if (dlen > ilen | |
12766 | && strncmp (dsec->name + (dlen - ilen), | |
12767 | isec->name, ilen) == 0) | |
12768 | { | |
12769 | dsec->gc_mark = 0; | |
b40bf0a2 NC |
12770 | } |
12771 | } | |
12772 | } | |
7f6ab9f8 AM |
12773 | } |
12774 | return TRUE; | |
12775 | } | |
12776 | ||
c152c796 AM |
12777 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ |
12778 | ||
c17d87de NC |
12779 | struct elf_gc_sweep_symbol_info |
12780 | { | |
ccabcbe5 AM |
12781 | struct bfd_link_info *info; |
12782 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
12783 | bfd_boolean); | |
12784 | }; | |
12785 | ||
c152c796 | 12786 | static bfd_boolean |
ccabcbe5 | 12787 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) |
c152c796 | 12788 | { |
1d5316ab AM |
12789 | if (!h->mark |
12790 | && (((h->root.type == bfd_link_hash_defined | |
12791 | || h->root.type == bfd_link_hash_defweak) | |
c4621b33 | 12792 | && !((h->def_regular || ELF_COMMON_DEF_P (h)) |
6673f753 | 12793 | && h->root.u.def.section->gc_mark)) |
1d5316ab AM |
12794 | || h->root.type == bfd_link_hash_undefined |
12795 | || h->root.type == bfd_link_hash_undefweak)) | |
12796 | { | |
12797 | struct elf_gc_sweep_symbol_info *inf; | |
12798 | ||
12799 | inf = (struct elf_gc_sweep_symbol_info *) data; | |
ccabcbe5 | 12800 | (*inf->hide_symbol) (inf->info, h, TRUE); |
1d5316ab AM |
12801 | h->def_regular = 0; |
12802 | h->ref_regular = 0; | |
12803 | h->ref_regular_nonweak = 0; | |
ccabcbe5 | 12804 | } |
c152c796 AM |
12805 | |
12806 | return TRUE; | |
12807 | } | |
12808 | ||
12809 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
12810 | ||
12811 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
12812 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
12813 | ||
12814 | static bfd_boolean | |
ccabcbe5 | 12815 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
12816 | { |
12817 | bfd *sub; | |
ccabcbe5 AM |
12818 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
12819 | gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; | |
12820 | unsigned long section_sym_count; | |
12821 | struct elf_gc_sweep_symbol_info sweep_info; | |
c152c796 | 12822 | |
c72f2fb2 | 12823 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
12824 | { |
12825 | asection *o; | |
12826 | ||
b19a8f85 L |
12827 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
12828 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) | |
c152c796 AM |
12829 | continue; |
12830 | ||
12831 | for (o = sub->sections; o != NULL; o = o->next) | |
12832 | { | |
a33dafc3 L |
12833 | /* When any section in a section group is kept, we keep all |
12834 | sections in the section group. If the first member of | |
12835 | the section group is excluded, we will also exclude the | |
12836 | group section. */ | |
12837 | if (o->flags & SEC_GROUP) | |
12838 | { | |
12839 | asection *first = elf_next_in_group (o); | |
12840 | o->gc_mark = first->gc_mark; | |
12841 | } | |
c152c796 | 12842 | |
1e7eae0d | 12843 | if (o->gc_mark) |
c152c796 AM |
12844 | continue; |
12845 | ||
12846 | /* Skip sweeping sections already excluded. */ | |
12847 | if (o->flags & SEC_EXCLUDE) | |
12848 | continue; | |
12849 | ||
12850 | /* Since this is early in the link process, it is simple | |
12851 | to remove a section from the output. */ | |
12852 | o->flags |= SEC_EXCLUDE; | |
12853 | ||
c55fe096 | 12854 | if (info->print_gc_sections && o->size != 0) |
c17d87de NC |
12855 | _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name); |
12856 | ||
c152c796 AM |
12857 | /* But we also have to update some of the relocation |
12858 | info we collected before. */ | |
12859 | if (gc_sweep_hook | |
e8aaee2a | 12860 | && (o->flags & SEC_RELOC) != 0 |
9850436d AM |
12861 | && o->reloc_count != 0 |
12862 | && !((info->strip == strip_all || info->strip == strip_debugger) | |
12863 | && (o->flags & SEC_DEBUGGING) != 0) | |
e8aaee2a | 12864 | && !bfd_is_abs_section (o->output_section)) |
c152c796 AM |
12865 | { |
12866 | Elf_Internal_Rela *internal_relocs; | |
12867 | bfd_boolean r; | |
12868 | ||
12869 | internal_relocs | |
12870 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
12871 | info->keep_memory); | |
12872 | if (internal_relocs == NULL) | |
12873 | return FALSE; | |
12874 | ||
12875 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
12876 | ||
12877 | if (elf_section_data (o)->relocs != internal_relocs) | |
12878 | free (internal_relocs); | |
12879 | ||
12880 | if (!r) | |
12881 | return FALSE; | |
12882 | } | |
12883 | } | |
12884 | } | |
12885 | ||
12886 | /* Remove the symbols that were in the swept sections from the dynamic | |
12887 | symbol table. GCFIXME: Anyone know how to get them out of the | |
12888 | static symbol table as well? */ | |
ccabcbe5 AM |
12889 | sweep_info.info = info; |
12890 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
12891 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
12892 | &sweep_info); | |
c152c796 | 12893 | |
ccabcbe5 | 12894 | _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); |
c152c796 AM |
12895 | return TRUE; |
12896 | } | |
12897 | ||
12898 | /* Propagate collected vtable information. This is called through | |
12899 | elf_link_hash_traverse. */ | |
12900 | ||
12901 | static bfd_boolean | |
12902 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
12903 | { | |
c152c796 | 12904 | /* Those that are not vtables. */ |
f6e332e6 | 12905 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
12906 | return TRUE; |
12907 | ||
12908 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 12909 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
12910 | return TRUE; |
12911 | ||
12912 | /* If we've already been done, exit. */ | |
f6e332e6 | 12913 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
12914 | return TRUE; |
12915 | ||
12916 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 12917 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 12918 | |
f6e332e6 | 12919 | if (h->vtable->used == NULL) |
c152c796 AM |
12920 | { |
12921 | /* None of this table's entries were referenced. Re-use the | |
12922 | parent's table. */ | |
f6e332e6 AM |
12923 | h->vtable->used = h->vtable->parent->vtable->used; |
12924 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
12925 | } |
12926 | else | |
12927 | { | |
12928 | size_t n; | |
12929 | bfd_boolean *cu, *pu; | |
12930 | ||
12931 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 12932 | cu = h->vtable->used; |
c152c796 | 12933 | cu[-1] = TRUE; |
f6e332e6 | 12934 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
12935 | if (pu != NULL) |
12936 | { | |
12937 | const struct elf_backend_data *bed; | |
12938 | unsigned int log_file_align; | |
12939 | ||
12940 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
12941 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 12942 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
12943 | while (n--) |
12944 | { | |
12945 | if (*pu) | |
12946 | *cu = TRUE; | |
12947 | pu++; | |
12948 | cu++; | |
12949 | } | |
12950 | } | |
12951 | } | |
12952 | ||
12953 | return TRUE; | |
12954 | } | |
12955 | ||
12956 | static bfd_boolean | |
12957 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
12958 | { | |
12959 | asection *sec; | |
12960 | bfd_vma hstart, hend; | |
12961 | Elf_Internal_Rela *relstart, *relend, *rel; | |
12962 | const struct elf_backend_data *bed; | |
12963 | unsigned int log_file_align; | |
12964 | ||
c152c796 AM |
12965 | /* Take care of both those symbols that do not describe vtables as |
12966 | well as those that are not loaded. */ | |
f6e332e6 | 12967 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
12968 | return TRUE; |
12969 | ||
12970 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
12971 | || h->root.type == bfd_link_hash_defweak); | |
12972 | ||
12973 | sec = h->root.u.def.section; | |
12974 | hstart = h->root.u.def.value; | |
12975 | hend = hstart + h->size; | |
12976 | ||
12977 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
12978 | if (!relstart) | |
12979 | return *(bfd_boolean *) okp = FALSE; | |
12980 | bed = get_elf_backend_data (sec->owner); | |
12981 | log_file_align = bed->s->log_file_align; | |
12982 | ||
12983 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
12984 | ||
12985 | for (rel = relstart; rel < relend; ++rel) | |
12986 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
12987 | { | |
12988 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
12989 | if (h->vtable->used |
12990 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
12991 | { |
12992 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 12993 | if (h->vtable->used[entry]) |
c152c796 AM |
12994 | continue; |
12995 | } | |
12996 | /* Otherwise, kill it. */ | |
12997 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
12998 | } | |
12999 | ||
13000 | return TRUE; | |
13001 | } | |
13002 | ||
87538722 AM |
13003 | /* Mark sections containing dynamically referenced symbols. When |
13004 | building shared libraries, we must assume that any visible symbol is | |
13005 | referenced. */ | |
715df9b8 | 13006 | |
64d03ab5 AM |
13007 | bfd_boolean |
13008 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 13009 | { |
87538722 | 13010 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
d6f6f455 | 13011 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
87538722 | 13012 | |
715df9b8 EB |
13013 | if ((h->root.type == bfd_link_hash_defined |
13014 | || h->root.type == bfd_link_hash_defweak) | |
87538722 | 13015 | && (h->ref_dynamic |
c4621b33 | 13016 | || ((h->def_regular || ELF_COMMON_DEF_P (h)) |
87538722 | 13017 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL |
fd91d419 | 13018 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN |
0e1862bb | 13019 | && (!bfd_link_executable (info) |
b407645f AM |
13020 | || info->export_dynamic |
13021 | || (h->dynamic | |
13022 | && d != NULL | |
13023 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
422f1182 | 13024 | && (h->versioned >= versioned |
54e8959c L |
13025 | || !bfd_hide_sym_by_version (info->version_info, |
13026 | h->root.root.string))))) | |
715df9b8 EB |
13027 | h->root.u.def.section->flags |= SEC_KEEP; |
13028 | ||
13029 | return TRUE; | |
13030 | } | |
3b36f7e6 | 13031 | |
74f0fb50 AM |
13032 | /* Keep all sections containing symbols undefined on the command-line, |
13033 | and the section containing the entry symbol. */ | |
13034 | ||
13035 | void | |
13036 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
13037 | { | |
13038 | struct bfd_sym_chain *sym; | |
13039 | ||
13040 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
13041 | { | |
13042 | struct elf_link_hash_entry *h; | |
13043 | ||
13044 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
13045 | FALSE, FALSE, FALSE); | |
13046 | ||
13047 | if (h != NULL | |
13048 | && (h->root.type == bfd_link_hash_defined | |
13049 | || h->root.type == bfd_link_hash_defweak) | |
13050 | && !bfd_is_abs_section (h->root.u.def.section)) | |
13051 | h->root.u.def.section->flags |= SEC_KEEP; | |
13052 | } | |
13053 | } | |
13054 | ||
2f0c68f2 CM |
13055 | bfd_boolean |
13056 | bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, | |
13057 | struct bfd_link_info *info) | |
13058 | { | |
13059 | bfd *ibfd = info->input_bfds; | |
13060 | ||
13061 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) | |
13062 | { | |
13063 | asection *sec; | |
13064 | struct elf_reloc_cookie cookie; | |
13065 | ||
13066 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13067 | continue; | |
13068 | ||
13069 | if (!init_reloc_cookie (&cookie, info, ibfd)) | |
13070 | return FALSE; | |
13071 | ||
13072 | for (sec = ibfd->sections; sec; sec = sec->next) | |
13073 | { | |
13074 | if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry") | |
13075 | && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) | |
13076 | { | |
13077 | _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); | |
13078 | fini_reloc_cookie_rels (&cookie, sec); | |
13079 | } | |
13080 | } | |
13081 | } | |
13082 | return TRUE; | |
13083 | } | |
13084 | ||
c152c796 AM |
13085 | /* Do mark and sweep of unused sections. */ |
13086 | ||
13087 | bfd_boolean | |
13088 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
13089 | { | |
13090 | bfd_boolean ok = TRUE; | |
13091 | bfd *sub; | |
6a5bb875 | 13092 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 13093 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
da44f4e5 | 13094 | struct elf_link_hash_table *htab; |
c152c796 | 13095 | |
64d03ab5 | 13096 | if (!bed->can_gc_sections |
715df9b8 | 13097 | || !is_elf_hash_table (info->hash)) |
c152c796 AM |
13098 | { |
13099 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
13100 | return TRUE; | |
13101 | } | |
13102 | ||
74f0fb50 | 13103 | bed->gc_keep (info); |
da44f4e5 | 13104 | htab = elf_hash_table (info); |
74f0fb50 | 13105 | |
9d0a14d3 RS |
13106 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
13107 | at the .eh_frame section if we can mark the FDEs individually. */ | |
2f0c68f2 CM |
13108 | for (sub = info->input_bfds; |
13109 | info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; | |
13110 | sub = sub->link.next) | |
9d0a14d3 RS |
13111 | { |
13112 | asection *sec; | |
13113 | struct elf_reloc_cookie cookie; | |
13114 | ||
13115 | sec = bfd_get_section_by_name (sub, ".eh_frame"); | |
9a2a56cc | 13116 | while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) |
9d0a14d3 RS |
13117 | { |
13118 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
9a2a56cc AM |
13119 | if (elf_section_data (sec)->sec_info |
13120 | && (sec->flags & SEC_LINKER_CREATED) == 0) | |
9d0a14d3 RS |
13121 | elf_eh_frame_section (sub) = sec; |
13122 | fini_reloc_cookie_for_section (&cookie, sec); | |
199af150 | 13123 | sec = bfd_get_next_section_by_name (NULL, sec); |
9d0a14d3 RS |
13124 | } |
13125 | } | |
9d0a14d3 | 13126 | |
c152c796 | 13127 | /* Apply transitive closure to the vtable entry usage info. */ |
da44f4e5 | 13128 | elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); |
c152c796 AM |
13129 | if (!ok) |
13130 | return FALSE; | |
13131 | ||
13132 | /* Kill the vtable relocations that were not used. */ | |
da44f4e5 | 13133 | elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok); |
c152c796 AM |
13134 | if (!ok) |
13135 | return FALSE; | |
13136 | ||
715df9b8 | 13137 | /* Mark dynamically referenced symbols. */ |
da44f4e5 AM |
13138 | if (htab->dynamic_sections_created) |
13139 | elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); | |
c152c796 | 13140 | |
715df9b8 | 13141 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 13142 | gc_mark_hook = bed->gc_mark_hook; |
c72f2fb2 | 13143 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13144 | { |
13145 | asection *o; | |
13146 | ||
b19a8f85 L |
13147 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
13148 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) | |
c152c796 AM |
13149 | continue; |
13150 | ||
7f6ab9f8 AM |
13151 | /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). |
13152 | Also treat note sections as a root, if the section is not part | |
13153 | of a group. */ | |
c152c796 | 13154 | for (o = sub->sections; o != NULL; o = o->next) |
7f6ab9f8 AM |
13155 | if (!o->gc_mark |
13156 | && (o->flags & SEC_EXCLUDE) == 0 | |
24007750 | 13157 | && ((o->flags & SEC_KEEP) != 0 |
7f6ab9f8 AM |
13158 | || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE |
13159 | && elf_next_in_group (o) == NULL ))) | |
13160 | { | |
13161 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
13162 | return FALSE; | |
13163 | } | |
c152c796 AM |
13164 | } |
13165 | ||
6a5bb875 | 13166 | /* Allow the backend to mark additional target specific sections. */ |
7f6ab9f8 | 13167 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 13168 | |
c152c796 | 13169 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 13170 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
13171 | } |
13172 | \f | |
13173 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
13174 | ||
13175 | bfd_boolean | |
13176 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
13177 | asection *sec, | |
13178 | struct elf_link_hash_entry *h, | |
13179 | bfd_vma offset) | |
13180 | { | |
13181 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
13182 | struct elf_link_hash_entry **search, *child; | |
ef53be89 | 13183 | size_t extsymcount; |
c152c796 AM |
13184 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
13185 | ||
13186 | /* The sh_info field of the symtab header tells us where the | |
13187 | external symbols start. We don't care about the local symbols at | |
13188 | this point. */ | |
13189 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
13190 | if (!elf_bad_symtab (abfd)) | |
13191 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
13192 | ||
13193 | sym_hashes = elf_sym_hashes (abfd); | |
13194 | sym_hashes_end = sym_hashes + extsymcount; | |
13195 | ||
13196 | /* Hunt down the child symbol, which is in this section at the same | |
13197 | offset as the relocation. */ | |
13198 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
13199 | { | |
13200 | if ((child = *search) != NULL | |
13201 | && (child->root.type == bfd_link_hash_defined | |
13202 | || child->root.type == bfd_link_hash_defweak) | |
13203 | && child->root.u.def.section == sec | |
13204 | && child->root.u.def.value == offset) | |
13205 | goto win; | |
13206 | } | |
13207 | ||
d003868e AM |
13208 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
13209 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
13210 | bfd_set_error (bfd_error_invalid_operation); |
13211 | return FALSE; | |
13212 | ||
13213 | win: | |
f6e332e6 AM |
13214 | if (!child->vtable) |
13215 | { | |
ca4be51c AM |
13216 | child->vtable = ((struct elf_link_virtual_table_entry *) |
13217 | bfd_zalloc (abfd, sizeof (*child->vtable))); | |
f6e332e6 AM |
13218 | if (!child->vtable) |
13219 | return FALSE; | |
13220 | } | |
c152c796 AM |
13221 | if (!h) |
13222 | { | |
13223 | /* This *should* only be the absolute section. It could potentially | |
13224 | be that someone has defined a non-global vtable though, which | |
13225 | would be bad. It isn't worth paging in the local symbols to be | |
13226 | sure though; that case should simply be handled by the assembler. */ | |
13227 | ||
f6e332e6 | 13228 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
13229 | } |
13230 | else | |
f6e332e6 | 13231 | child->vtable->parent = h; |
c152c796 AM |
13232 | |
13233 | return TRUE; | |
13234 | } | |
13235 | ||
13236 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
13237 | ||
13238 | bfd_boolean | |
13239 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
13240 | asection *sec ATTRIBUTE_UNUSED, | |
13241 | struct elf_link_hash_entry *h, | |
13242 | bfd_vma addend) | |
13243 | { | |
13244 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13245 | unsigned int log_file_align = bed->s->log_file_align; | |
13246 | ||
f6e332e6 AM |
13247 | if (!h->vtable) |
13248 | { | |
ca4be51c AM |
13249 | h->vtable = ((struct elf_link_virtual_table_entry *) |
13250 | bfd_zalloc (abfd, sizeof (*h->vtable))); | |
f6e332e6 AM |
13251 | if (!h->vtable) |
13252 | return FALSE; | |
13253 | } | |
13254 | ||
13255 | if (addend >= h->vtable->size) | |
c152c796 AM |
13256 | { |
13257 | size_t size, bytes, file_align; | |
f6e332e6 | 13258 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
13259 | |
13260 | /* While the symbol is undefined, we have to be prepared to handle | |
13261 | a zero size. */ | |
13262 | file_align = 1 << log_file_align; | |
13263 | if (h->root.type == bfd_link_hash_undefined) | |
13264 | size = addend + file_align; | |
13265 | else | |
13266 | { | |
13267 | size = h->size; | |
13268 | if (addend >= size) | |
13269 | { | |
13270 | /* Oops! We've got a reference past the defined end of | |
13271 | the table. This is probably a bug -- shall we warn? */ | |
13272 | size = addend + file_align; | |
13273 | } | |
13274 | } | |
13275 | size = (size + file_align - 1) & -file_align; | |
13276 | ||
13277 | /* Allocate one extra entry for use as a "done" flag for the | |
13278 | consolidation pass. */ | |
13279 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
13280 | ||
13281 | if (ptr) | |
13282 | { | |
a50b1753 | 13283 | ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); |
c152c796 AM |
13284 | |
13285 | if (ptr != NULL) | |
13286 | { | |
13287 | size_t oldbytes; | |
13288 | ||
f6e332e6 | 13289 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
13290 | * sizeof (bfd_boolean)); |
13291 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
13292 | } | |
13293 | } | |
13294 | else | |
a50b1753 | 13295 | ptr = (bfd_boolean *) bfd_zmalloc (bytes); |
c152c796 AM |
13296 | |
13297 | if (ptr == NULL) | |
13298 | return FALSE; | |
13299 | ||
13300 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
13301 | h->vtable->used = ptr + 1; |
13302 | h->vtable->size = size; | |
c152c796 AM |
13303 | } |
13304 | ||
f6e332e6 | 13305 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
13306 | |
13307 | return TRUE; | |
13308 | } | |
13309 | ||
ae17ab41 CM |
13310 | /* Map an ELF section header flag to its corresponding string. */ |
13311 | typedef struct | |
13312 | { | |
13313 | char *flag_name; | |
13314 | flagword flag_value; | |
13315 | } elf_flags_to_name_table; | |
13316 | ||
13317 | static elf_flags_to_name_table elf_flags_to_names [] = | |
13318 | { | |
13319 | { "SHF_WRITE", SHF_WRITE }, | |
13320 | { "SHF_ALLOC", SHF_ALLOC }, | |
13321 | { "SHF_EXECINSTR", SHF_EXECINSTR }, | |
13322 | { "SHF_MERGE", SHF_MERGE }, | |
13323 | { "SHF_STRINGS", SHF_STRINGS }, | |
13324 | { "SHF_INFO_LINK", SHF_INFO_LINK}, | |
13325 | { "SHF_LINK_ORDER", SHF_LINK_ORDER}, | |
13326 | { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, | |
13327 | { "SHF_GROUP", SHF_GROUP }, | |
13328 | { "SHF_TLS", SHF_TLS }, | |
13329 | { "SHF_MASKOS", SHF_MASKOS }, | |
13330 | { "SHF_EXCLUDE", SHF_EXCLUDE }, | |
13331 | }; | |
13332 | ||
b9c361e0 JL |
13333 | /* Returns TRUE if the section is to be included, otherwise FALSE. */ |
13334 | bfd_boolean | |
ae17ab41 | 13335 | bfd_elf_lookup_section_flags (struct bfd_link_info *info, |
8b127cbc | 13336 | struct flag_info *flaginfo, |
b9c361e0 | 13337 | asection *section) |
ae17ab41 | 13338 | { |
8b127cbc | 13339 | const bfd_vma sh_flags = elf_section_flags (section); |
ae17ab41 | 13340 | |
8b127cbc | 13341 | if (!flaginfo->flags_initialized) |
ae17ab41 | 13342 | { |
8b127cbc AM |
13343 | bfd *obfd = info->output_bfd; |
13344 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
13345 | struct flag_info_list *tf = flaginfo->flag_list; | |
b9c361e0 JL |
13346 | int with_hex = 0; |
13347 | int without_hex = 0; | |
13348 | ||
8b127cbc | 13349 | for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) |
ae17ab41 | 13350 | { |
b9c361e0 | 13351 | unsigned i; |
8b127cbc | 13352 | flagword (*lookup) (char *); |
ae17ab41 | 13353 | |
8b127cbc AM |
13354 | lookup = bed->elf_backend_lookup_section_flags_hook; |
13355 | if (lookup != NULL) | |
ae17ab41 | 13356 | { |
8b127cbc | 13357 | flagword hexval = (*lookup) ((char *) tf->name); |
b9c361e0 JL |
13358 | |
13359 | if (hexval != 0) | |
13360 | { | |
13361 | if (tf->with == with_flags) | |
13362 | with_hex |= hexval; | |
13363 | else if (tf->with == without_flags) | |
13364 | without_hex |= hexval; | |
13365 | tf->valid = TRUE; | |
13366 | continue; | |
13367 | } | |
ae17ab41 | 13368 | } |
8b127cbc | 13369 | for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) |
ae17ab41 | 13370 | { |
8b127cbc | 13371 | if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) |
b9c361e0 JL |
13372 | { |
13373 | if (tf->with == with_flags) | |
13374 | with_hex |= elf_flags_to_names[i].flag_value; | |
13375 | else if (tf->with == without_flags) | |
13376 | without_hex |= elf_flags_to_names[i].flag_value; | |
13377 | tf->valid = TRUE; | |
13378 | break; | |
13379 | } | |
13380 | } | |
8b127cbc | 13381 | if (!tf->valid) |
b9c361e0 | 13382 | { |
68ffbac6 | 13383 | info->callbacks->einfo |
8b127cbc | 13384 | (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); |
b9c361e0 | 13385 | return FALSE; |
ae17ab41 CM |
13386 | } |
13387 | } | |
8b127cbc AM |
13388 | flaginfo->flags_initialized = TRUE; |
13389 | flaginfo->only_with_flags |= with_hex; | |
13390 | flaginfo->not_with_flags |= without_hex; | |
ae17ab41 | 13391 | } |
ae17ab41 | 13392 | |
8b127cbc | 13393 | if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) |
b9c361e0 JL |
13394 | return FALSE; |
13395 | ||
8b127cbc | 13396 | if ((flaginfo->not_with_flags & sh_flags) != 0) |
b9c361e0 JL |
13397 | return FALSE; |
13398 | ||
13399 | return TRUE; | |
ae17ab41 CM |
13400 | } |
13401 | ||
c152c796 AM |
13402 | struct alloc_got_off_arg { |
13403 | bfd_vma gotoff; | |
10455f89 | 13404 | struct bfd_link_info *info; |
c152c796 AM |
13405 | }; |
13406 | ||
13407 | /* We need a special top-level link routine to convert got reference counts | |
13408 | to real got offsets. */ | |
13409 | ||
13410 | static bfd_boolean | |
13411 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
13412 | { | |
a50b1753 | 13413 | struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; |
10455f89 HPN |
13414 | bfd *obfd = gofarg->info->output_bfd; |
13415 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
c152c796 | 13416 | |
c152c796 AM |
13417 | if (h->got.refcount > 0) |
13418 | { | |
13419 | h->got.offset = gofarg->gotoff; | |
10455f89 | 13420 | gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); |
c152c796 AM |
13421 | } |
13422 | else | |
13423 | h->got.offset = (bfd_vma) -1; | |
13424 | ||
13425 | return TRUE; | |
13426 | } | |
13427 | ||
13428 | /* And an accompanying bit to work out final got entry offsets once | |
13429 | we're done. Should be called from final_link. */ | |
13430 | ||
13431 | bfd_boolean | |
13432 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
13433 | struct bfd_link_info *info) | |
13434 | { | |
13435 | bfd *i; | |
13436 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13437 | bfd_vma gotoff; | |
c152c796 AM |
13438 | struct alloc_got_off_arg gofarg; |
13439 | ||
10455f89 HPN |
13440 | BFD_ASSERT (abfd == info->output_bfd); |
13441 | ||
c152c796 AM |
13442 | if (! is_elf_hash_table (info->hash)) |
13443 | return FALSE; | |
13444 | ||
13445 | /* The GOT offset is relative to the .got section, but the GOT header is | |
13446 | put into the .got.plt section, if the backend uses it. */ | |
13447 | if (bed->want_got_plt) | |
13448 | gotoff = 0; | |
13449 | else | |
13450 | gotoff = bed->got_header_size; | |
13451 | ||
13452 | /* Do the local .got entries first. */ | |
c72f2fb2 | 13453 | for (i = info->input_bfds; i; i = i->link.next) |
c152c796 AM |
13454 | { |
13455 | bfd_signed_vma *local_got; | |
ef53be89 | 13456 | size_t j, locsymcount; |
c152c796 AM |
13457 | Elf_Internal_Shdr *symtab_hdr; |
13458 | ||
13459 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
13460 | continue; | |
13461 | ||
13462 | local_got = elf_local_got_refcounts (i); | |
13463 | if (!local_got) | |
13464 | continue; | |
13465 | ||
13466 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
13467 | if (elf_bad_symtab (i)) | |
13468 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
13469 | else | |
13470 | locsymcount = symtab_hdr->sh_info; | |
13471 | ||
13472 | for (j = 0; j < locsymcount; ++j) | |
13473 | { | |
13474 | if (local_got[j] > 0) | |
13475 | { | |
13476 | local_got[j] = gotoff; | |
10455f89 | 13477 | gotoff += bed->got_elt_size (abfd, info, NULL, i, j); |
c152c796 AM |
13478 | } |
13479 | else | |
13480 | local_got[j] = (bfd_vma) -1; | |
13481 | } | |
13482 | } | |
13483 | ||
13484 | /* Then the global .got entries. .plt refcounts are handled by | |
13485 | adjust_dynamic_symbol */ | |
13486 | gofarg.gotoff = gotoff; | |
10455f89 | 13487 | gofarg.info = info; |
c152c796 AM |
13488 | elf_link_hash_traverse (elf_hash_table (info), |
13489 | elf_gc_allocate_got_offsets, | |
13490 | &gofarg); | |
13491 | return TRUE; | |
13492 | } | |
13493 | ||
13494 | /* Many folk need no more in the way of final link than this, once | |
13495 | got entry reference counting is enabled. */ | |
13496 | ||
13497 | bfd_boolean | |
13498 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
13499 | { | |
13500 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
13501 | return FALSE; | |
13502 | ||
13503 | /* Invoke the regular ELF backend linker to do all the work. */ | |
13504 | return bfd_elf_final_link (abfd, info); | |
13505 | } | |
13506 | ||
13507 | bfd_boolean | |
13508 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
13509 | { | |
a50b1753 | 13510 | struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; |
c152c796 AM |
13511 | |
13512 | if (rcookie->bad_symtab) | |
13513 | rcookie->rel = rcookie->rels; | |
13514 | ||
13515 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
13516 | { | |
13517 | unsigned long r_symndx; | |
13518 | ||
13519 | if (! rcookie->bad_symtab) | |
13520 | if (rcookie->rel->r_offset > offset) | |
13521 | return FALSE; | |
13522 | if (rcookie->rel->r_offset != offset) | |
13523 | continue; | |
13524 | ||
13525 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
2c2fa401 | 13526 | if (r_symndx == STN_UNDEF) |
c152c796 AM |
13527 | return TRUE; |
13528 | ||
13529 | if (r_symndx >= rcookie->locsymcount | |
13530 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
13531 | { | |
13532 | struct elf_link_hash_entry *h; | |
13533 | ||
13534 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
13535 | ||
13536 | while (h->root.type == bfd_link_hash_indirect | |
13537 | || h->root.type == bfd_link_hash_warning) | |
13538 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
13539 | ||
13540 | if ((h->root.type == bfd_link_hash_defined | |
13541 | || h->root.type == bfd_link_hash_defweak) | |
5b69e357 AM |
13542 | && (h->root.u.def.section->owner != rcookie->abfd |
13543 | || h->root.u.def.section->kept_section != NULL | |
13544 | || discarded_section (h->root.u.def.section))) | |
c152c796 | 13545 | return TRUE; |
c152c796 AM |
13546 | } |
13547 | else | |
13548 | { | |
13549 | /* It's not a relocation against a global symbol, | |
13550 | but it could be a relocation against a local | |
13551 | symbol for a discarded section. */ | |
13552 | asection *isec; | |
13553 | Elf_Internal_Sym *isym; | |
13554 | ||
13555 | /* Need to: get the symbol; get the section. */ | |
13556 | isym = &rcookie->locsyms[r_symndx]; | |
cb33740c | 13557 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); |
5b69e357 AM |
13558 | if (isec != NULL |
13559 | && (isec->kept_section != NULL | |
13560 | || discarded_section (isec))) | |
cb33740c | 13561 | return TRUE; |
c152c796 AM |
13562 | } |
13563 | return FALSE; | |
13564 | } | |
13565 | return FALSE; | |
13566 | } | |
13567 | ||
13568 | /* Discard unneeded references to discarded sections. | |
75938853 AM |
13569 | Returns -1 on error, 1 if any section's size was changed, 0 if |
13570 | nothing changed. This function assumes that the relocations are in | |
13571 | sorted order, which is true for all known assemblers. */ | |
c152c796 | 13572 | |
75938853 | 13573 | int |
c152c796 AM |
13574 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) |
13575 | { | |
13576 | struct elf_reloc_cookie cookie; | |
18cd5bce | 13577 | asection *o; |
c152c796 | 13578 | bfd *abfd; |
75938853 | 13579 | int changed = 0; |
c152c796 AM |
13580 | |
13581 | if (info->traditional_format | |
13582 | || !is_elf_hash_table (info->hash)) | |
75938853 | 13583 | return 0; |
c152c796 | 13584 | |
18cd5bce AM |
13585 | o = bfd_get_section_by_name (output_bfd, ".stab"); |
13586 | if (o != NULL) | |
c152c796 | 13587 | { |
18cd5bce | 13588 | asection *i; |
c152c796 | 13589 | |
18cd5bce | 13590 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
8da3dbc5 | 13591 | { |
18cd5bce AM |
13592 | if (i->size == 0 |
13593 | || i->reloc_count == 0 | |
13594 | || i->sec_info_type != SEC_INFO_TYPE_STABS) | |
13595 | continue; | |
c152c796 | 13596 | |
18cd5bce AM |
13597 | abfd = i->owner; |
13598 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
13599 | continue; | |
c152c796 | 13600 | |
18cd5bce | 13601 | if (!init_reloc_cookie_for_section (&cookie, info, i)) |
75938853 | 13602 | return -1; |
c152c796 | 13603 | |
18cd5bce AM |
13604 | if (_bfd_discard_section_stabs (abfd, i, |
13605 | elf_section_data (i)->sec_info, | |
5241d853 RS |
13606 | bfd_elf_reloc_symbol_deleted_p, |
13607 | &cookie)) | |
75938853 | 13608 | changed = 1; |
18cd5bce AM |
13609 | |
13610 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 13611 | } |
18cd5bce AM |
13612 | } |
13613 | ||
2f0c68f2 CM |
13614 | o = NULL; |
13615 | if (info->eh_frame_hdr_type != COMPACT_EH_HDR) | |
13616 | o = bfd_get_section_by_name (output_bfd, ".eh_frame"); | |
18cd5bce AM |
13617 | if (o != NULL) |
13618 | { | |
13619 | asection *i; | |
c152c796 | 13620 | |
18cd5bce | 13621 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
c152c796 | 13622 | { |
18cd5bce AM |
13623 | if (i->size == 0) |
13624 | continue; | |
13625 | ||
13626 | abfd = i->owner; | |
13627 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
13628 | continue; | |
13629 | ||
13630 | if (!init_reloc_cookie_for_section (&cookie, info, i)) | |
75938853 | 13631 | return -1; |
18cd5bce AM |
13632 | |
13633 | _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); | |
13634 | if (_bfd_elf_discard_section_eh_frame (abfd, info, i, | |
c152c796 AM |
13635 | bfd_elf_reloc_symbol_deleted_p, |
13636 | &cookie)) | |
75938853 | 13637 | changed = 1; |
18cd5bce AM |
13638 | |
13639 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 13640 | } |
18cd5bce | 13641 | } |
c152c796 | 13642 | |
18cd5bce AM |
13643 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) |
13644 | { | |
13645 | const struct elf_backend_data *bed; | |
c152c796 | 13646 | |
18cd5bce AM |
13647 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) |
13648 | continue; | |
13649 | ||
13650 | bed = get_elf_backend_data (abfd); | |
13651 | ||
13652 | if (bed->elf_backend_discard_info != NULL) | |
13653 | { | |
13654 | if (!init_reloc_cookie (&cookie, info, abfd)) | |
75938853 | 13655 | return -1; |
18cd5bce AM |
13656 | |
13657 | if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
75938853 | 13658 | changed = 1; |
18cd5bce AM |
13659 | |
13660 | fini_reloc_cookie (&cookie, abfd); | |
13661 | } | |
c152c796 AM |
13662 | } |
13663 | ||
2f0c68f2 CM |
13664 | if (info->eh_frame_hdr_type == COMPACT_EH_HDR) |
13665 | _bfd_elf_end_eh_frame_parsing (info); | |
13666 | ||
13667 | if (info->eh_frame_hdr_type | |
0e1862bb | 13668 | && !bfd_link_relocatable (info) |
c152c796 | 13669 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) |
75938853 | 13670 | changed = 1; |
c152c796 | 13671 | |
75938853 | 13672 | return changed; |
c152c796 | 13673 | } |
082b7297 | 13674 | |
43e1669b | 13675 | bfd_boolean |
0c511000 | 13676 | _bfd_elf_section_already_linked (bfd *abfd, |
c77ec726 | 13677 | asection *sec, |
c0f00686 | 13678 | struct bfd_link_info *info) |
082b7297 L |
13679 | { |
13680 | flagword flags; | |
c77ec726 | 13681 | const char *name, *key; |
082b7297 L |
13682 | struct bfd_section_already_linked *l; |
13683 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
0c511000 | 13684 | |
c77ec726 AM |
13685 | if (sec->output_section == bfd_abs_section_ptr) |
13686 | return FALSE; | |
0c511000 | 13687 | |
c77ec726 | 13688 | flags = sec->flags; |
0c511000 | 13689 | |
c77ec726 AM |
13690 | /* Return if it isn't a linkonce section. A comdat group section |
13691 | also has SEC_LINK_ONCE set. */ | |
13692 | if ((flags & SEC_LINK_ONCE) == 0) | |
13693 | return FALSE; | |
0c511000 | 13694 | |
c77ec726 AM |
13695 | /* Don't put group member sections on our list of already linked |
13696 | sections. They are handled as a group via their group section. */ | |
13697 | if (elf_sec_group (sec) != NULL) | |
13698 | return FALSE; | |
0c511000 | 13699 | |
c77ec726 AM |
13700 | /* For a SHT_GROUP section, use the group signature as the key. */ |
13701 | name = sec->name; | |
13702 | if ((flags & SEC_GROUP) != 0 | |
13703 | && elf_next_in_group (sec) != NULL | |
13704 | && elf_group_name (elf_next_in_group (sec)) != NULL) | |
13705 | key = elf_group_name (elf_next_in_group (sec)); | |
13706 | else | |
13707 | { | |
13708 | /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ | |
0c511000 | 13709 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
c77ec726 AM |
13710 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
13711 | key++; | |
0c511000 | 13712 | else |
c77ec726 AM |
13713 | /* Must be a user linkonce section that doesn't follow gcc's |
13714 | naming convention. In this case we won't be matching | |
13715 | single member groups. */ | |
13716 | key = name; | |
0c511000 | 13717 | } |
6d2cd210 | 13718 | |
c77ec726 | 13719 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
082b7297 L |
13720 | |
13721 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13722 | { | |
c2370991 | 13723 | /* We may have 2 different types of sections on the list: group |
c77ec726 AM |
13724 | sections with a signature of <key> (<key> is some string), |
13725 | and linkonce sections named .gnu.linkonce.<type>.<key>. | |
13726 | Match like sections. LTO plugin sections are an exception. | |
13727 | They are always named .gnu.linkonce.t.<key> and match either | |
13728 | type of section. */ | |
13729 | if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) | |
13730 | && ((flags & SEC_GROUP) != 0 | |
13731 | || strcmp (name, l->sec->name) == 0)) | |
13732 | || (l->sec->owner->flags & BFD_PLUGIN) != 0) | |
082b7297 L |
13733 | { |
13734 | /* The section has already been linked. See if we should | |
6d2cd210 | 13735 | issue a warning. */ |
c77ec726 AM |
13736 | if (!_bfd_handle_already_linked (sec, l, info)) |
13737 | return FALSE; | |
082b7297 | 13738 | |
c77ec726 | 13739 | if (flags & SEC_GROUP) |
3d7f7666 | 13740 | { |
c77ec726 AM |
13741 | asection *first = elf_next_in_group (sec); |
13742 | asection *s = first; | |
3d7f7666 | 13743 | |
c77ec726 | 13744 | while (s != NULL) |
3d7f7666 | 13745 | { |
c77ec726 AM |
13746 | s->output_section = bfd_abs_section_ptr; |
13747 | /* Record which group discards it. */ | |
13748 | s->kept_section = l->sec; | |
13749 | s = elf_next_in_group (s); | |
13750 | /* These lists are circular. */ | |
13751 | if (s == first) | |
13752 | break; | |
3d7f7666 L |
13753 | } |
13754 | } | |
082b7297 | 13755 | |
43e1669b | 13756 | return TRUE; |
082b7297 L |
13757 | } |
13758 | } | |
13759 | ||
c77ec726 AM |
13760 | /* A single member comdat group section may be discarded by a |
13761 | linkonce section and vice versa. */ | |
13762 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 13763 | { |
c77ec726 | 13764 | asection *first = elf_next_in_group (sec); |
c2370991 | 13765 | |
c77ec726 AM |
13766 | if (first != NULL && elf_next_in_group (first) == first) |
13767 | /* Check this single member group against linkonce sections. */ | |
13768 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13769 | if ((l->sec->flags & SEC_GROUP) == 0 | |
13770 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
13771 | { | |
13772 | first->output_section = bfd_abs_section_ptr; | |
13773 | first->kept_section = l->sec; | |
13774 | sec->output_section = bfd_abs_section_ptr; | |
13775 | break; | |
13776 | } | |
13777 | } | |
13778 | else | |
13779 | /* Check this linkonce section against single member groups. */ | |
13780 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13781 | if (l->sec->flags & SEC_GROUP) | |
6d2cd210 | 13782 | { |
c77ec726 | 13783 | asection *first = elf_next_in_group (l->sec); |
6d2cd210 | 13784 | |
c77ec726 AM |
13785 | if (first != NULL |
13786 | && elf_next_in_group (first) == first | |
13787 | && bfd_elf_match_symbols_in_sections (first, sec, info)) | |
13788 | { | |
13789 | sec->output_section = bfd_abs_section_ptr; | |
13790 | sec->kept_section = first; | |
13791 | break; | |
13792 | } | |
6d2cd210 | 13793 | } |
0c511000 | 13794 | |
c77ec726 AM |
13795 | /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' |
13796 | referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 | |
13797 | specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' | |
13798 | prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its | |
13799 | matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded | |
13800 | but its `.gnu.linkonce.t.F' is discarded means we chose one-only | |
13801 | `.gnu.linkonce.t.F' section from a different bfd not requiring any | |
13802 | `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. | |
13803 | The reverse order cannot happen as there is never a bfd with only the | |
13804 | `.gnu.linkonce.r.F' section. The order of sections in a bfd does not | |
13805 | matter as here were are looking only for cross-bfd sections. */ | |
13806 | ||
13807 | if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) | |
13808 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13809 | if ((l->sec->flags & SEC_GROUP) == 0 | |
13810 | && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) | |
13811 | { | |
13812 | if (abfd != l->sec->owner) | |
13813 | sec->output_section = bfd_abs_section_ptr; | |
13814 | break; | |
13815 | } | |
80c29487 | 13816 | |
082b7297 | 13817 | /* This is the first section with this name. Record it. */ |
c77ec726 | 13818 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
bb6198d2 | 13819 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
c77ec726 | 13820 | return sec->output_section == bfd_abs_section_ptr; |
082b7297 | 13821 | } |
81e1b023 | 13822 | |
a4d8e49b L |
13823 | bfd_boolean |
13824 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
13825 | { | |
13826 | return sym->st_shndx == SHN_COMMON; | |
13827 | } | |
13828 | ||
13829 | unsigned int | |
13830 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
13831 | { | |
13832 | return SHN_COMMON; | |
13833 | } | |
13834 | ||
13835 | asection * | |
13836 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
13837 | { | |
13838 | return bfd_com_section_ptr; | |
13839 | } | |
10455f89 HPN |
13840 | |
13841 | bfd_vma | |
13842 | _bfd_elf_default_got_elt_size (bfd *abfd, | |
13843 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
13844 | struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, | |
13845 | bfd *ibfd ATTRIBUTE_UNUSED, | |
13846 | unsigned long symndx ATTRIBUTE_UNUSED) | |
13847 | { | |
13848 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13849 | return bed->s->arch_size / 8; | |
13850 | } | |
83bac4b0 NC |
13851 | |
13852 | /* Routines to support the creation of dynamic relocs. */ | |
13853 | ||
83bac4b0 NC |
13854 | /* Returns the name of the dynamic reloc section associated with SEC. */ |
13855 | ||
13856 | static const char * | |
13857 | get_dynamic_reloc_section_name (bfd * abfd, | |
13858 | asection * sec, | |
13859 | bfd_boolean is_rela) | |
13860 | { | |
ddcf1fcf BS |
13861 | char *name; |
13862 | const char *old_name = bfd_get_section_name (NULL, sec); | |
13863 | const char *prefix = is_rela ? ".rela" : ".rel"; | |
83bac4b0 | 13864 | |
ddcf1fcf | 13865 | if (old_name == NULL) |
83bac4b0 NC |
13866 | return NULL; |
13867 | ||
ddcf1fcf | 13868 | name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); |
68ffbac6 | 13869 | sprintf (name, "%s%s", prefix, old_name); |
83bac4b0 NC |
13870 | |
13871 | return name; | |
13872 | } | |
13873 | ||
13874 | /* Returns the dynamic reloc section associated with SEC. | |
13875 | If necessary compute the name of the dynamic reloc section based | |
13876 | on SEC's name (looked up in ABFD's string table) and the setting | |
13877 | of IS_RELA. */ | |
13878 | ||
13879 | asection * | |
13880 | _bfd_elf_get_dynamic_reloc_section (bfd * abfd, | |
13881 | asection * sec, | |
13882 | bfd_boolean is_rela) | |
13883 | { | |
13884 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
13885 | ||
13886 | if (reloc_sec == NULL) | |
13887 | { | |
13888 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
13889 | ||
13890 | if (name != NULL) | |
13891 | { | |
3d4d4302 | 13892 | reloc_sec = bfd_get_linker_section (abfd, name); |
83bac4b0 NC |
13893 | |
13894 | if (reloc_sec != NULL) | |
13895 | elf_section_data (sec)->sreloc = reloc_sec; | |
13896 | } | |
13897 | } | |
13898 | ||
13899 | return reloc_sec; | |
13900 | } | |
13901 | ||
13902 | /* Returns the dynamic reloc section associated with SEC. If the | |
13903 | section does not exist it is created and attached to the DYNOBJ | |
13904 | bfd and stored in the SRELOC field of SEC's elf_section_data | |
13905 | structure. | |
f8076f98 | 13906 | |
83bac4b0 NC |
13907 | ALIGNMENT is the alignment for the newly created section and |
13908 | IS_RELA defines whether the name should be .rela.<SEC's name> | |
13909 | or .rel.<SEC's name>. The section name is looked up in the | |
13910 | string table associated with ABFD. */ | |
13911 | ||
13912 | asection * | |
ca4be51c AM |
13913 | _bfd_elf_make_dynamic_reloc_section (asection *sec, |
13914 | bfd *dynobj, | |
13915 | unsigned int alignment, | |
13916 | bfd *abfd, | |
13917 | bfd_boolean is_rela) | |
83bac4b0 NC |
13918 | { |
13919 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
13920 | ||
13921 | if (reloc_sec == NULL) | |
13922 | { | |
13923 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
13924 | ||
13925 | if (name == NULL) | |
13926 | return NULL; | |
13927 | ||
3d4d4302 | 13928 | reloc_sec = bfd_get_linker_section (dynobj, name); |
83bac4b0 NC |
13929 | |
13930 | if (reloc_sec == NULL) | |
13931 | { | |
3d4d4302 AM |
13932 | flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY |
13933 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
83bac4b0 NC |
13934 | if ((sec->flags & SEC_ALLOC) != 0) |
13935 | flags |= SEC_ALLOC | SEC_LOAD; | |
13936 | ||
3d4d4302 | 13937 | reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); |
83bac4b0 NC |
13938 | if (reloc_sec != NULL) |
13939 | { | |
8877b5e5 AM |
13940 | /* _bfd_elf_get_sec_type_attr chooses a section type by |
13941 | name. Override as it may be wrong, eg. for a user | |
13942 | section named "auto" we'll get ".relauto" which is | |
13943 | seen to be a .rela section. */ | |
13944 | elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; | |
83bac4b0 NC |
13945 | if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) |
13946 | reloc_sec = NULL; | |
13947 | } | |
13948 | } | |
13949 | ||
13950 | elf_section_data (sec)->sreloc = reloc_sec; | |
13951 | } | |
13952 | ||
13953 | return reloc_sec; | |
13954 | } | |
1338dd10 | 13955 | |
bffebb6b AM |
13956 | /* Copy the ELF symbol type and other attributes for a linker script |
13957 | assignment from HSRC to HDEST. Generally this should be treated as | |
13958 | if we found a strong non-dynamic definition for HDEST (except that | |
13959 | ld ignores multiple definition errors). */ | |
1338dd10 | 13960 | void |
bffebb6b AM |
13961 | _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, |
13962 | struct bfd_link_hash_entry *hdest, | |
13963 | struct bfd_link_hash_entry *hsrc) | |
1338dd10 | 13964 | { |
bffebb6b AM |
13965 | struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; |
13966 | struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; | |
13967 | Elf_Internal_Sym isym; | |
1338dd10 PB |
13968 | |
13969 | ehdest->type = ehsrc->type; | |
35fc36a8 | 13970 | ehdest->target_internal = ehsrc->target_internal; |
bffebb6b AM |
13971 | |
13972 | isym.st_other = ehsrc->other; | |
b8417128 | 13973 | elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE); |
1338dd10 | 13974 | } |
351f65ca L |
13975 | |
13976 | /* Append a RELA relocation REL to section S in BFD. */ | |
13977 | ||
13978 | void | |
13979 | elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
13980 | { | |
13981 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13982 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); | |
13983 | BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); | |
13984 | bed->s->swap_reloca_out (abfd, rel, loc); | |
13985 | } | |
13986 | ||
13987 | /* Append a REL relocation REL to section S in BFD. */ | |
13988 | ||
13989 | void | |
13990 | elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
13991 | { | |
13992 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13993 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); | |
13994 | BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); | |
59d6ffb2 | 13995 | bed->s->swap_reloc_out (abfd, rel, loc); |
351f65ca | 13996 | } |