Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
64d03ab5 AM |
2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
3 | 2005, 2006 Free Software Foundation, Inc. | |
252b5132 | 4 | |
8fdd7217 | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
8fdd7217 NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
252b5132 | 11 | |
8fdd7217 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
8fdd7217 NC |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
3e110533 | 19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
252b5132 RH |
20 | |
21 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
23 | #include "bfdlink.h" | |
24 | #include "libbfd.h" | |
25 | #define ARCH_SIZE 0 | |
26 | #include "elf-bfd.h" | |
4ad4eba5 | 27 | #include "safe-ctype.h" |
ccf2f652 | 28 | #include "libiberty.h" |
252b5132 | 29 | |
d98685ac AM |
30 | /* Define a symbol in a dynamic linkage section. */ |
31 | ||
32 | struct elf_link_hash_entry * | |
33 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
34 | struct bfd_link_info *info, | |
35 | asection *sec, | |
36 | const char *name) | |
37 | { | |
38 | struct elf_link_hash_entry *h; | |
39 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 40 | const struct elf_backend_data *bed; |
d98685ac AM |
41 | |
42 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
43 | if (h != NULL) | |
44 | { | |
45 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
46 | This is a symptom of a larger problem: Absolute symbols | |
47 | defined in shared libraries can't be overridden, because we | |
48 | lose the link to the bfd which is via the symbol section. */ | |
49 | h->root.type = bfd_link_hash_new; | |
50 | } | |
51 | ||
52 | bh = &h->root; | |
53 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, | |
54 | sec, 0, NULL, FALSE, | |
55 | get_elf_backend_data (abfd)->collect, | |
56 | &bh)) | |
57 | return NULL; | |
58 | h = (struct elf_link_hash_entry *) bh; | |
59 | h->def_regular = 1; | |
60 | h->type = STT_OBJECT; | |
61 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
62 | ||
ccabcbe5 AM |
63 | bed = get_elf_backend_data (abfd); |
64 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
d98685ac AM |
65 | return h; |
66 | } | |
67 | ||
b34976b6 | 68 | bfd_boolean |
268b6b39 | 69 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
70 | { |
71 | flagword flags; | |
aad5d350 | 72 | asection *s; |
252b5132 | 73 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 74 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
75 | int ptralign; |
76 | ||
77 | /* This function may be called more than once. */ | |
aad5d350 AM |
78 | s = bfd_get_section_by_name (abfd, ".got"); |
79 | if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0) | |
b34976b6 | 80 | return TRUE; |
252b5132 RH |
81 | |
82 | switch (bed->s->arch_size) | |
83 | { | |
bb0deeff AO |
84 | case 32: |
85 | ptralign = 2; | |
86 | break; | |
87 | ||
88 | case 64: | |
89 | ptralign = 3; | |
90 | break; | |
91 | ||
92 | default: | |
93 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 94 | return FALSE; |
252b5132 RH |
95 | } |
96 | ||
e5a52504 | 97 | flags = bed->dynamic_sec_flags; |
252b5132 | 98 | |
3496cb2a | 99 | s = bfd_make_section_with_flags (abfd, ".got", flags); |
252b5132 | 100 | if (s == NULL |
252b5132 | 101 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 102 | return FALSE; |
252b5132 RH |
103 | |
104 | if (bed->want_got_plt) | |
105 | { | |
3496cb2a | 106 | s = bfd_make_section_with_flags (abfd, ".got.plt", flags); |
252b5132 | 107 | if (s == NULL |
252b5132 | 108 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 109 | return FALSE; |
252b5132 RH |
110 | } |
111 | ||
2517a57f AM |
112 | if (bed->want_got_sym) |
113 | { | |
114 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
115 | (or .got.plt) section. We don't do this in the linker script | |
116 | because we don't want to define the symbol if we are not creating | |
117 | a global offset table. */ | |
d98685ac | 118 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_"); |
2517a57f | 119 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
120 | if (h == NULL) |
121 | return FALSE; | |
2517a57f | 122 | } |
252b5132 RH |
123 | |
124 | /* The first bit of the global offset table is the header. */ | |
3b36f7e6 | 125 | s->size += bed->got_header_size; |
252b5132 | 126 | |
b34976b6 | 127 | return TRUE; |
252b5132 RH |
128 | } |
129 | \f | |
7e9f0867 AM |
130 | /* Create a strtab to hold the dynamic symbol names. */ |
131 | static bfd_boolean | |
132 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
133 | { | |
134 | struct elf_link_hash_table *hash_table; | |
135 | ||
136 | hash_table = elf_hash_table (info); | |
137 | if (hash_table->dynobj == NULL) | |
138 | hash_table->dynobj = abfd; | |
139 | ||
140 | if (hash_table->dynstr == NULL) | |
141 | { | |
142 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
143 | if (hash_table->dynstr == NULL) | |
144 | return FALSE; | |
145 | } | |
146 | return TRUE; | |
147 | } | |
148 | ||
45d6a902 AM |
149 | /* Create some sections which will be filled in with dynamic linking |
150 | information. ABFD is an input file which requires dynamic sections | |
151 | to be created. The dynamic sections take up virtual memory space | |
152 | when the final executable is run, so we need to create them before | |
153 | addresses are assigned to the output sections. We work out the | |
154 | actual contents and size of these sections later. */ | |
252b5132 | 155 | |
b34976b6 | 156 | bfd_boolean |
268b6b39 | 157 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 158 | { |
45d6a902 AM |
159 | flagword flags; |
160 | register asection *s; | |
9c5bfbb7 | 161 | const struct elf_backend_data *bed; |
252b5132 | 162 | |
0eddce27 | 163 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
164 | return FALSE; |
165 | ||
166 | if (elf_hash_table (info)->dynamic_sections_created) | |
167 | return TRUE; | |
168 | ||
7e9f0867 AM |
169 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
170 | return FALSE; | |
45d6a902 | 171 | |
7e9f0867 | 172 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
173 | bed = get_elf_backend_data (abfd); |
174 | ||
175 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
176 | |
177 | /* A dynamically linked executable has a .interp section, but a | |
178 | shared library does not. */ | |
36af4a4e | 179 | if (info->executable) |
252b5132 | 180 | { |
3496cb2a L |
181 | s = bfd_make_section_with_flags (abfd, ".interp", |
182 | flags | SEC_READONLY); | |
183 | if (s == NULL) | |
45d6a902 AM |
184 | return FALSE; |
185 | } | |
bb0deeff | 186 | |
0eddce27 | 187 | if (! info->traditional_format) |
45d6a902 | 188 | { |
3496cb2a L |
189 | s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr", |
190 | flags | SEC_READONLY); | |
45d6a902 | 191 | if (s == NULL |
45d6a902 AM |
192 | || ! bfd_set_section_alignment (abfd, s, 2)) |
193 | return FALSE; | |
194 | elf_hash_table (info)->eh_info.hdr_sec = s; | |
195 | } | |
bb0deeff | 196 | |
45d6a902 AM |
197 | /* Create sections to hold version informations. These are removed |
198 | if they are not needed. */ | |
3496cb2a L |
199 | s = bfd_make_section_with_flags (abfd, ".gnu.version_d", |
200 | flags | SEC_READONLY); | |
45d6a902 | 201 | if (s == NULL |
45d6a902 AM |
202 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
203 | return FALSE; | |
204 | ||
3496cb2a L |
205 | s = bfd_make_section_with_flags (abfd, ".gnu.version", |
206 | flags | SEC_READONLY); | |
45d6a902 | 207 | if (s == NULL |
45d6a902 AM |
208 | || ! bfd_set_section_alignment (abfd, s, 1)) |
209 | return FALSE; | |
210 | ||
3496cb2a L |
211 | s = bfd_make_section_with_flags (abfd, ".gnu.version_r", |
212 | flags | SEC_READONLY); | |
45d6a902 | 213 | if (s == NULL |
45d6a902 AM |
214 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
215 | return FALSE; | |
216 | ||
3496cb2a L |
217 | s = bfd_make_section_with_flags (abfd, ".dynsym", |
218 | flags | SEC_READONLY); | |
45d6a902 | 219 | if (s == NULL |
45d6a902 AM |
220 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
221 | return FALSE; | |
222 | ||
3496cb2a L |
223 | s = bfd_make_section_with_flags (abfd, ".dynstr", |
224 | flags | SEC_READONLY); | |
225 | if (s == NULL) | |
45d6a902 AM |
226 | return FALSE; |
227 | ||
3496cb2a | 228 | s = bfd_make_section_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 229 | if (s == NULL |
45d6a902 AM |
230 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
231 | return FALSE; | |
232 | ||
233 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
234 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
235 | only want to define it if we are, in fact, creating a .dynamic | |
236 | section. We don't want to define it if there is no .dynamic | |
237 | section, since on some ELF platforms the start up code examines it | |
238 | to decide how to initialize the process. */ | |
d98685ac | 239 | if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC")) |
45d6a902 AM |
240 | return FALSE; |
241 | ||
3496cb2a L |
242 | s = bfd_make_section_with_flags (abfd, ".hash", |
243 | flags | SEC_READONLY); | |
45d6a902 | 244 | if (s == NULL |
45d6a902 AM |
245 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
246 | return FALSE; | |
247 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
248 | ||
249 | /* Let the backend create the rest of the sections. This lets the | |
250 | backend set the right flags. The backend will normally create | |
251 | the .got and .plt sections. */ | |
252 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
253 | return FALSE; | |
254 | ||
255 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
256 | ||
257 | return TRUE; | |
258 | } | |
259 | ||
260 | /* Create dynamic sections when linking against a dynamic object. */ | |
261 | ||
262 | bfd_boolean | |
268b6b39 | 263 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
264 | { |
265 | flagword flags, pltflags; | |
7325306f | 266 | struct elf_link_hash_entry *h; |
45d6a902 | 267 | asection *s; |
9c5bfbb7 | 268 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 269 | |
252b5132 RH |
270 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
271 | .rel[a].bss sections. */ | |
e5a52504 | 272 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
273 | |
274 | pltflags = flags; | |
252b5132 | 275 | if (bed->plt_not_loaded) |
6df4d94c MM |
276 | /* We do not clear SEC_ALLOC here because we still want the OS to |
277 | allocate space for the section; it's just that there's nothing | |
278 | to read in from the object file. */ | |
5d1634d7 | 279 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
280 | else |
281 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
282 | if (bed->plt_readonly) |
283 | pltflags |= SEC_READONLY; | |
284 | ||
3496cb2a | 285 | s = bfd_make_section_with_flags (abfd, ".plt", pltflags); |
252b5132 | 286 | if (s == NULL |
252b5132 | 287 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 288 | return FALSE; |
252b5132 | 289 | |
d98685ac AM |
290 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
291 | .plt section. */ | |
7325306f RS |
292 | if (bed->want_plt_sym) |
293 | { | |
294 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
295 | "_PROCEDURE_LINKAGE_TABLE_"); | |
296 | elf_hash_table (info)->hplt = h; | |
297 | if (h == NULL) | |
298 | return FALSE; | |
299 | } | |
252b5132 | 300 | |
3496cb2a L |
301 | s = bfd_make_section_with_flags (abfd, |
302 | (bed->default_use_rela_p | |
303 | ? ".rela.plt" : ".rel.plt"), | |
304 | flags | SEC_READONLY); | |
252b5132 | 305 | if (s == NULL |
45d6a902 | 306 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 307 | return FALSE; |
252b5132 RH |
308 | |
309 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 310 | return FALSE; |
252b5132 | 311 | |
3018b441 RH |
312 | if (bed->want_dynbss) |
313 | { | |
314 | /* The .dynbss section is a place to put symbols which are defined | |
315 | by dynamic objects, are referenced by regular objects, and are | |
316 | not functions. We must allocate space for them in the process | |
317 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
318 | initialize them at run time. The linker script puts the .dynbss | |
319 | section into the .bss section of the final image. */ | |
3496cb2a L |
320 | s = bfd_make_section_with_flags (abfd, ".dynbss", |
321 | (SEC_ALLOC | |
322 | | SEC_LINKER_CREATED)); | |
323 | if (s == NULL) | |
b34976b6 | 324 | return FALSE; |
252b5132 | 325 | |
3018b441 | 326 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
327 | normally needed. We need to create it here, though, so that the |
328 | linker will map it to an output section. We can't just create it | |
329 | only if we need it, because we will not know whether we need it | |
330 | until we have seen all the input files, and the first time the | |
331 | main linker code calls BFD after examining all the input files | |
332 | (size_dynamic_sections) the input sections have already been | |
333 | mapped to the output sections. If the section turns out not to | |
334 | be needed, we can discard it later. We will never need this | |
335 | section when generating a shared object, since they do not use | |
336 | copy relocs. */ | |
3018b441 RH |
337 | if (! info->shared) |
338 | { | |
3496cb2a L |
339 | s = bfd_make_section_with_flags (abfd, |
340 | (bed->default_use_rela_p | |
341 | ? ".rela.bss" : ".rel.bss"), | |
342 | flags | SEC_READONLY); | |
3018b441 | 343 | if (s == NULL |
45d6a902 | 344 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 345 | return FALSE; |
3018b441 | 346 | } |
252b5132 RH |
347 | } |
348 | ||
b34976b6 | 349 | return TRUE; |
252b5132 RH |
350 | } |
351 | \f | |
252b5132 RH |
352 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
353 | read the input files, since we need to have a list of all of them | |
354 | before we can determine the final sizes of the output sections. | |
355 | Note that we may actually call this function even though we are not | |
356 | going to output any dynamic symbols; in some cases we know that a | |
357 | symbol should be in the dynamic symbol table, but only if there is | |
358 | one. */ | |
359 | ||
b34976b6 | 360 | bfd_boolean |
c152c796 AM |
361 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
362 | struct elf_link_hash_entry *h) | |
252b5132 RH |
363 | { |
364 | if (h->dynindx == -1) | |
365 | { | |
2b0f7ef9 | 366 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 367 | char *p; |
252b5132 | 368 | const char *name; |
252b5132 RH |
369 | bfd_size_type indx; |
370 | ||
7a13edea NC |
371 | /* XXX: The ABI draft says the linker must turn hidden and |
372 | internal symbols into STB_LOCAL symbols when producing the | |
373 | DSO. However, if ld.so honors st_other in the dynamic table, | |
374 | this would not be necessary. */ | |
375 | switch (ELF_ST_VISIBILITY (h->other)) | |
376 | { | |
377 | case STV_INTERNAL: | |
378 | case STV_HIDDEN: | |
9d6eee78 L |
379 | if (h->root.type != bfd_link_hash_undefined |
380 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 381 | { |
f5385ebf | 382 | h->forced_local = 1; |
67687978 PB |
383 | if (!elf_hash_table (info)->is_relocatable_executable) |
384 | return TRUE; | |
7a13edea | 385 | } |
0444bdd4 | 386 | |
7a13edea NC |
387 | default: |
388 | break; | |
389 | } | |
390 | ||
252b5132 RH |
391 | h->dynindx = elf_hash_table (info)->dynsymcount; |
392 | ++elf_hash_table (info)->dynsymcount; | |
393 | ||
394 | dynstr = elf_hash_table (info)->dynstr; | |
395 | if (dynstr == NULL) | |
396 | { | |
397 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 398 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 399 | if (dynstr == NULL) |
b34976b6 | 400 | return FALSE; |
252b5132 RH |
401 | } |
402 | ||
403 | /* We don't put any version information in the dynamic string | |
aad5d350 | 404 | table. */ |
252b5132 RH |
405 | name = h->root.root.string; |
406 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
407 | if (p != NULL) |
408 | /* We know that the p points into writable memory. In fact, | |
409 | there are only a few symbols that have read-only names, being | |
410 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
411 | by the backends. Most symbols will have names pointing into | |
412 | an ELF string table read from a file, or to objalloc memory. */ | |
413 | *p = 0; | |
414 | ||
415 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
416 | ||
417 | if (p != NULL) | |
418 | *p = ELF_VER_CHR; | |
252b5132 RH |
419 | |
420 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 421 | return FALSE; |
252b5132 RH |
422 | h->dynstr_index = indx; |
423 | } | |
424 | ||
b34976b6 | 425 | return TRUE; |
252b5132 | 426 | } |
45d6a902 AM |
427 | \f |
428 | /* Record an assignment to a symbol made by a linker script. We need | |
429 | this in case some dynamic object refers to this symbol. */ | |
430 | ||
431 | bfd_boolean | |
fe21a8fc L |
432 | bfd_elf_record_link_assignment (bfd *output_bfd, |
433 | struct bfd_link_info *info, | |
268b6b39 | 434 | const char *name, |
fe21a8fc L |
435 | bfd_boolean provide, |
436 | bfd_boolean hidden) | |
45d6a902 AM |
437 | { |
438 | struct elf_link_hash_entry *h; | |
4ea42fb7 | 439 | struct elf_link_hash_table *htab; |
45d6a902 | 440 | |
0eddce27 | 441 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
442 | return TRUE; |
443 | ||
4ea42fb7 AM |
444 | htab = elf_hash_table (info); |
445 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 446 | if (h == NULL) |
4ea42fb7 | 447 | return provide; |
45d6a902 | 448 | |
02bb6eae AO |
449 | /* Since we're defining the symbol, don't let it seem to have not |
450 | been defined. record_dynamic_symbol and size_dynamic_sections | |
77cfaee6 | 451 | may depend on this. */ |
02bb6eae AO |
452 | if (h->root.type == bfd_link_hash_undefweak |
453 | || h->root.type == bfd_link_hash_undefined) | |
77cfaee6 | 454 | { |
4ea42fb7 | 455 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
456 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
457 | bfd_link_repair_undef_list (&htab->root); | |
77cfaee6 | 458 | } |
02bb6eae | 459 | |
45d6a902 | 460 | if (h->root.type == bfd_link_hash_new) |
f5385ebf | 461 | h->non_elf = 0; |
45d6a902 AM |
462 | |
463 | /* If this symbol is being provided by the linker script, and it is | |
464 | currently defined by a dynamic object, but not by a regular | |
465 | object, then mark it as undefined so that the generic linker will | |
466 | force the correct value. */ | |
467 | if (provide | |
f5385ebf AM |
468 | && h->def_dynamic |
469 | && !h->def_regular) | |
45d6a902 AM |
470 | h->root.type = bfd_link_hash_undefined; |
471 | ||
472 | /* If this symbol is not being provided by the linker script, and it is | |
473 | currently defined by a dynamic object, but not by a regular object, | |
474 | then clear out any version information because the symbol will not be | |
475 | associated with the dynamic object any more. */ | |
476 | if (!provide | |
f5385ebf AM |
477 | && h->def_dynamic |
478 | && !h->def_regular) | |
45d6a902 AM |
479 | h->verinfo.verdef = NULL; |
480 | ||
f5385ebf | 481 | h->def_regular = 1; |
45d6a902 | 482 | |
fe21a8fc L |
483 | if (provide && hidden) |
484 | { | |
485 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
486 | ||
487 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
488 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
489 | } | |
490 | ||
6fa3860b PB |
491 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
492 | and executables. */ | |
493 | if (!info->relocatable | |
494 | && h->dynindx != -1 | |
495 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
496 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
497 | h->forced_local = 1; | |
498 | ||
f5385ebf AM |
499 | if ((h->def_dynamic |
500 | || h->ref_dynamic | |
67687978 PB |
501 | || info->shared |
502 | || (info->executable && elf_hash_table (info)->is_relocatable_executable)) | |
45d6a902 AM |
503 | && h->dynindx == -1) |
504 | { | |
c152c796 | 505 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
506 | return FALSE; |
507 | ||
508 | /* If this is a weak defined symbol, and we know a corresponding | |
509 | real symbol from the same dynamic object, make sure the real | |
510 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
511 | if (h->u.weakdef != NULL |
512 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 513 | { |
f6e332e6 | 514 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
515 | return FALSE; |
516 | } | |
517 | } | |
518 | ||
519 | return TRUE; | |
520 | } | |
42751cf3 | 521 | |
8c58d23b AM |
522 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
523 | success, and 2 on a failure caused by attempting to record a symbol | |
524 | in a discarded section, eg. a discarded link-once section symbol. */ | |
525 | ||
526 | int | |
c152c796 AM |
527 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
528 | bfd *input_bfd, | |
529 | long input_indx) | |
8c58d23b AM |
530 | { |
531 | bfd_size_type amt; | |
532 | struct elf_link_local_dynamic_entry *entry; | |
533 | struct elf_link_hash_table *eht; | |
534 | struct elf_strtab_hash *dynstr; | |
535 | unsigned long dynstr_index; | |
536 | char *name; | |
537 | Elf_External_Sym_Shndx eshndx; | |
538 | char esym[sizeof (Elf64_External_Sym)]; | |
539 | ||
0eddce27 | 540 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
541 | return 0; |
542 | ||
543 | /* See if the entry exists already. */ | |
544 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
545 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
546 | return 1; | |
547 | ||
548 | amt = sizeof (*entry); | |
268b6b39 | 549 | entry = bfd_alloc (input_bfd, amt); |
8c58d23b AM |
550 | if (entry == NULL) |
551 | return 0; | |
552 | ||
553 | /* Go find the symbol, so that we can find it's name. */ | |
554 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 555 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
556 | { |
557 | bfd_release (input_bfd, entry); | |
558 | return 0; | |
559 | } | |
560 | ||
561 | if (entry->isym.st_shndx != SHN_UNDEF | |
562 | && (entry->isym.st_shndx < SHN_LORESERVE | |
563 | || entry->isym.st_shndx > SHN_HIRESERVE)) | |
564 | { | |
565 | asection *s; | |
566 | ||
567 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
568 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
569 | { | |
570 | /* We can still bfd_release here as nothing has done another | |
571 | bfd_alloc. We can't do this later in this function. */ | |
572 | bfd_release (input_bfd, entry); | |
573 | return 2; | |
574 | } | |
575 | } | |
576 | ||
577 | name = (bfd_elf_string_from_elf_section | |
578 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
579 | entry->isym.st_name)); | |
580 | ||
581 | dynstr = elf_hash_table (info)->dynstr; | |
582 | if (dynstr == NULL) | |
583 | { | |
584 | /* Create a strtab to hold the dynamic symbol names. */ | |
585 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
586 | if (dynstr == NULL) | |
587 | return 0; | |
588 | } | |
589 | ||
b34976b6 | 590 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
591 | if (dynstr_index == (unsigned long) -1) |
592 | return 0; | |
593 | entry->isym.st_name = dynstr_index; | |
594 | ||
595 | eht = elf_hash_table (info); | |
596 | ||
597 | entry->next = eht->dynlocal; | |
598 | eht->dynlocal = entry; | |
599 | entry->input_bfd = input_bfd; | |
600 | entry->input_indx = input_indx; | |
601 | eht->dynsymcount++; | |
602 | ||
603 | /* Whatever binding the symbol had before, it's now local. */ | |
604 | entry->isym.st_info | |
605 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
606 | ||
607 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
608 | ||
609 | return 1; | |
610 | } | |
611 | ||
30b30c21 | 612 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 613 | |
30b30c21 | 614 | long |
268b6b39 AM |
615 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
616 | bfd *input_bfd, | |
617 | long input_indx) | |
30b30c21 RH |
618 | { |
619 | struct elf_link_local_dynamic_entry *e; | |
620 | ||
621 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
622 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
623 | return e->dynindx; | |
624 | return -1; | |
625 | } | |
626 | ||
627 | /* This function is used to renumber the dynamic symbols, if some of | |
628 | them are removed because they are marked as local. This is called | |
629 | via elf_link_hash_traverse. */ | |
630 | ||
b34976b6 | 631 | static bfd_boolean |
268b6b39 AM |
632 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
633 | void *data) | |
42751cf3 | 634 | { |
268b6b39 | 635 | size_t *count = data; |
30b30c21 | 636 | |
e92d460e AM |
637 | if (h->root.type == bfd_link_hash_warning) |
638 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
639 | ||
6fa3860b PB |
640 | if (h->forced_local) |
641 | return TRUE; | |
642 | ||
643 | if (h->dynindx != -1) | |
644 | h->dynindx = ++(*count); | |
645 | ||
646 | return TRUE; | |
647 | } | |
648 | ||
649 | ||
650 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
651 | STB_LOCAL binding. */ | |
652 | ||
653 | static bfd_boolean | |
654 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
655 | void *data) | |
656 | { | |
657 | size_t *count = data; | |
658 | ||
659 | if (h->root.type == bfd_link_hash_warning) | |
660 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
661 | ||
662 | if (!h->forced_local) | |
663 | return TRUE; | |
664 | ||
42751cf3 | 665 | if (h->dynindx != -1) |
30b30c21 RH |
666 | h->dynindx = ++(*count); |
667 | ||
b34976b6 | 668 | return TRUE; |
42751cf3 | 669 | } |
30b30c21 | 670 | |
aee6f5b4 AO |
671 | /* Return true if the dynamic symbol for a given section should be |
672 | omitted when creating a shared library. */ | |
673 | bfd_boolean | |
674 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
675 | struct bfd_link_info *info, | |
676 | asection *p) | |
677 | { | |
678 | switch (elf_section_data (p)->this_hdr.sh_type) | |
679 | { | |
680 | case SHT_PROGBITS: | |
681 | case SHT_NOBITS: | |
682 | /* If sh_type is yet undecided, assume it could be | |
683 | SHT_PROGBITS/SHT_NOBITS. */ | |
684 | case SHT_NULL: | |
685 | if (strcmp (p->name, ".got") == 0 | |
686 | || strcmp (p->name, ".got.plt") == 0 | |
687 | || strcmp (p->name, ".plt") == 0) | |
688 | { | |
689 | asection *ip; | |
690 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
691 | ||
692 | if (dynobj != NULL | |
1da212d6 | 693 | && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL |
aee6f5b4 AO |
694 | && (ip->flags & SEC_LINKER_CREATED) |
695 | && ip->output_section == p) | |
696 | return TRUE; | |
697 | } | |
698 | return FALSE; | |
699 | ||
700 | /* There shouldn't be section relative relocations | |
701 | against any other section. */ | |
702 | default: | |
703 | return TRUE; | |
704 | } | |
705 | } | |
706 | ||
062e2358 | 707 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
708 | symbol for each output section, which come first. Next come symbols |
709 | which have been forced to local binding. Then all of the back-end | |
710 | allocated local dynamic syms, followed by the rest of the global | |
711 | symbols. */ | |
30b30c21 | 712 | |
554220db AM |
713 | static unsigned long |
714 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
715 | struct bfd_link_info *info, | |
716 | unsigned long *section_sym_count) | |
30b30c21 RH |
717 | { |
718 | unsigned long dynsymcount = 0; | |
719 | ||
67687978 | 720 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
30b30c21 | 721 | { |
aee6f5b4 | 722 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
723 | asection *p; |
724 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 725 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
726 | && (p->flags & SEC_ALLOC) != 0 |
727 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
728 | elf_section_data (p)->dynindx = ++dynsymcount; | |
30b30c21 | 729 | } |
554220db | 730 | *section_sym_count = dynsymcount; |
30b30c21 | 731 | |
6fa3860b PB |
732 | elf_link_hash_traverse (elf_hash_table (info), |
733 | elf_link_renumber_local_hash_table_dynsyms, | |
734 | &dynsymcount); | |
735 | ||
30b30c21 RH |
736 | if (elf_hash_table (info)->dynlocal) |
737 | { | |
738 | struct elf_link_local_dynamic_entry *p; | |
739 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
740 | p->dynindx = ++dynsymcount; | |
741 | } | |
742 | ||
743 | elf_link_hash_traverse (elf_hash_table (info), | |
744 | elf_link_renumber_hash_table_dynsyms, | |
745 | &dynsymcount); | |
746 | ||
747 | /* There is an unused NULL entry at the head of the table which | |
748 | we must account for in our count. Unless there weren't any | |
749 | symbols, which means we'll have no table at all. */ | |
750 | if (dynsymcount != 0) | |
751 | ++dynsymcount; | |
752 | ||
ccabcbe5 AM |
753 | elf_hash_table (info)->dynsymcount = dynsymcount; |
754 | return dynsymcount; | |
30b30c21 | 755 | } |
252b5132 | 756 | |
45d6a902 AM |
757 | /* This function is called when we want to define a new symbol. It |
758 | handles the various cases which arise when we find a definition in | |
759 | a dynamic object, or when there is already a definition in a | |
760 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
761 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
762 | OVERRIDE if the old symbol is overriding a new definition. We set | |
763 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
764 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
765 | change, we mean that we shouldn't warn if the type or size does | |
af44c138 L |
766 | change. We set POLD_ALIGNMENT if an old common symbol in a dynamic |
767 | object is overridden by a regular object. */ | |
45d6a902 AM |
768 | |
769 | bfd_boolean | |
268b6b39 AM |
770 | _bfd_elf_merge_symbol (bfd *abfd, |
771 | struct bfd_link_info *info, | |
772 | const char *name, | |
773 | Elf_Internal_Sym *sym, | |
774 | asection **psec, | |
775 | bfd_vma *pvalue, | |
af44c138 | 776 | unsigned int *pold_alignment, |
268b6b39 AM |
777 | struct elf_link_hash_entry **sym_hash, |
778 | bfd_boolean *skip, | |
779 | bfd_boolean *override, | |
780 | bfd_boolean *type_change_ok, | |
0f8a2703 | 781 | bfd_boolean *size_change_ok) |
252b5132 | 782 | { |
7479dfd4 | 783 | asection *sec, *oldsec; |
45d6a902 AM |
784 | struct elf_link_hash_entry *h; |
785 | struct elf_link_hash_entry *flip; | |
786 | int bind; | |
787 | bfd *oldbfd; | |
788 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
77cfaee6 | 789 | bfd_boolean newweak, oldweak; |
a4d8e49b | 790 | const struct elf_backend_data *bed; |
45d6a902 AM |
791 | |
792 | *skip = FALSE; | |
793 | *override = FALSE; | |
794 | ||
795 | sec = *psec; | |
796 | bind = ELF_ST_BIND (sym->st_info); | |
797 | ||
798 | if (! bfd_is_und_section (sec)) | |
799 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
800 | else | |
801 | h = ((struct elf_link_hash_entry *) | |
802 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
803 | if (h == NULL) | |
804 | return FALSE; | |
805 | *sym_hash = h; | |
252b5132 | 806 | |
45d6a902 AM |
807 | /* This code is for coping with dynamic objects, and is only useful |
808 | if we are doing an ELF link. */ | |
809 | if (info->hash->creator != abfd->xvec) | |
810 | return TRUE; | |
252b5132 | 811 | |
45d6a902 AM |
812 | /* For merging, we only care about real symbols. */ |
813 | ||
814 | while (h->root.type == bfd_link_hash_indirect | |
815 | || h->root.type == bfd_link_hash_warning) | |
816 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
817 | ||
818 | /* If we just created the symbol, mark it as being an ELF symbol. | |
819 | Other than that, there is nothing to do--there is no merge issue | |
820 | with a newly defined symbol--so we just return. */ | |
821 | ||
822 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 823 | { |
f5385ebf | 824 | h->non_elf = 0; |
45d6a902 AM |
825 | return TRUE; |
826 | } | |
252b5132 | 827 | |
7479dfd4 L |
828 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
829 | existing symbol. */ | |
252b5132 | 830 | |
45d6a902 AM |
831 | switch (h->root.type) |
832 | { | |
833 | default: | |
834 | oldbfd = NULL; | |
7479dfd4 | 835 | oldsec = NULL; |
45d6a902 | 836 | break; |
252b5132 | 837 | |
45d6a902 AM |
838 | case bfd_link_hash_undefined: |
839 | case bfd_link_hash_undefweak: | |
840 | oldbfd = h->root.u.undef.abfd; | |
7479dfd4 | 841 | oldsec = NULL; |
45d6a902 AM |
842 | break; |
843 | ||
844 | case bfd_link_hash_defined: | |
845 | case bfd_link_hash_defweak: | |
846 | oldbfd = h->root.u.def.section->owner; | |
7479dfd4 | 847 | oldsec = h->root.u.def.section; |
45d6a902 AM |
848 | break; |
849 | ||
850 | case bfd_link_hash_common: | |
851 | oldbfd = h->root.u.c.p->section->owner; | |
7479dfd4 | 852 | oldsec = h->root.u.c.p->section; |
45d6a902 AM |
853 | break; |
854 | } | |
855 | ||
856 | /* In cases involving weak versioned symbols, we may wind up trying | |
857 | to merge a symbol with itself. Catch that here, to avoid the | |
858 | confusion that results if we try to override a symbol with | |
859 | itself. The additional tests catch cases like | |
860 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
861 | dynamic object, which we do want to handle here. */ | |
862 | if (abfd == oldbfd | |
863 | && ((abfd->flags & DYNAMIC) == 0 | |
f5385ebf | 864 | || !h->def_regular)) |
45d6a902 AM |
865 | return TRUE; |
866 | ||
867 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
868 | respectively, is from a dynamic object. */ | |
869 | ||
707bba77 | 870 | newdyn = (abfd->flags & DYNAMIC) != 0; |
45d6a902 | 871 | |
707bba77 | 872 | olddyn = FALSE; |
45d6a902 AM |
873 | if (oldbfd != NULL) |
874 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 875 | else if (oldsec != NULL) |
45d6a902 | 876 | { |
707bba77 | 877 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 878 | indices used by MIPS ELF. */ |
707bba77 | 879 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 880 | } |
252b5132 | 881 | |
45d6a902 AM |
882 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
883 | respectively, appear to be a definition rather than reference. */ | |
884 | ||
707bba77 | 885 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 886 | |
707bba77 AM |
887 | olddef = (h->root.type != bfd_link_hash_undefined |
888 | && h->root.type != bfd_link_hash_undefweak | |
889 | && h->root.type != bfd_link_hash_common); | |
45d6a902 | 890 | |
68f49ba3 L |
891 | /* Check TLS symbol. We don't check undefined symbol introduced by |
892 | "ld -u". */ | |
7479dfd4 | 893 | if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS) |
68f49ba3 L |
894 | && ELF_ST_TYPE (sym->st_info) != h->type |
895 | && oldbfd != NULL) | |
7479dfd4 L |
896 | { |
897 | bfd *ntbfd, *tbfd; | |
898 | bfd_boolean ntdef, tdef; | |
899 | asection *ntsec, *tsec; | |
900 | ||
901 | if (h->type == STT_TLS) | |
902 | { | |
3b36f7e6 | 903 | ntbfd = abfd; |
7479dfd4 L |
904 | ntsec = sec; |
905 | ntdef = newdef; | |
906 | tbfd = oldbfd; | |
907 | tsec = oldsec; | |
908 | tdef = olddef; | |
909 | } | |
910 | else | |
911 | { | |
912 | ntbfd = oldbfd; | |
913 | ntsec = oldsec; | |
914 | ntdef = olddef; | |
915 | tbfd = abfd; | |
916 | tsec = sec; | |
917 | tdef = newdef; | |
918 | } | |
919 | ||
920 | if (tdef && ntdef) | |
921 | (*_bfd_error_handler) | |
922 | (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), | |
923 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); | |
924 | else if (!tdef && !ntdef) | |
925 | (*_bfd_error_handler) | |
926 | (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), | |
927 | tbfd, ntbfd, h->root.root.string); | |
928 | else if (tdef) | |
929 | (*_bfd_error_handler) | |
930 | (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), | |
931 | tbfd, tsec, ntbfd, h->root.root.string); | |
932 | else | |
933 | (*_bfd_error_handler) | |
934 | (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), | |
935 | tbfd, ntbfd, ntsec, h->root.root.string); | |
936 | ||
937 | bfd_set_error (bfd_error_bad_value); | |
938 | return FALSE; | |
939 | } | |
940 | ||
4cc11e76 | 941 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
942 | object or is weak in all dynamic objects. Internal and hidden |
943 | visibility will make it unavailable to dynamic objects. */ | |
f5385ebf | 944 | if (newdyn && !h->dynamic_def) |
45d6a902 AM |
945 | { |
946 | if (!bfd_is_und_section (sec)) | |
f5385ebf | 947 | h->dynamic_def = 1; |
45d6a902 | 948 | else |
252b5132 | 949 | { |
45d6a902 AM |
950 | /* Check if this symbol is weak in all dynamic objects. If it |
951 | is the first time we see it in a dynamic object, we mark | |
952 | if it is weak. Otherwise, we clear it. */ | |
f5385ebf | 953 | if (!h->ref_dynamic) |
79349b09 | 954 | { |
45d6a902 | 955 | if (bind == STB_WEAK) |
f5385ebf | 956 | h->dynamic_weak = 1; |
252b5132 | 957 | } |
45d6a902 | 958 | else if (bind != STB_WEAK) |
f5385ebf | 959 | h->dynamic_weak = 0; |
252b5132 | 960 | } |
45d6a902 | 961 | } |
252b5132 | 962 | |
45d6a902 AM |
963 | /* If the old symbol has non-default visibility, we ignore the new |
964 | definition from a dynamic object. */ | |
965 | if (newdyn | |
9c7a29a3 | 966 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
967 | && !bfd_is_und_section (sec)) |
968 | { | |
969 | *skip = TRUE; | |
970 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 971 | h->ref_dynamic = 1; |
45d6a902 AM |
972 | /* A protected symbol has external availability. Make sure it is |
973 | recorded as dynamic. | |
974 | ||
975 | FIXME: Should we check type and size for protected symbol? */ | |
976 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 977 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
978 | else |
979 | return TRUE; | |
980 | } | |
981 | else if (!newdyn | |
9c7a29a3 | 982 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 983 | && h->def_dynamic) |
45d6a902 AM |
984 | { |
985 | /* If the new symbol with non-default visibility comes from a | |
986 | relocatable file and the old definition comes from a dynamic | |
987 | object, we remove the old definition. */ | |
988 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
989 | h = *sym_hash; | |
1de1a317 | 990 | |
f6e332e6 | 991 | if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root) |
1de1a317 L |
992 | && bfd_is_und_section (sec)) |
993 | { | |
994 | /* If the new symbol is undefined and the old symbol was | |
995 | also undefined before, we need to make sure | |
996 | _bfd_generic_link_add_one_symbol doesn't mess | |
f6e332e6 | 997 | up the linker hash table undefs list. Since the old |
1de1a317 L |
998 | definition came from a dynamic object, it is still on the |
999 | undefs list. */ | |
1000 | h->root.type = bfd_link_hash_undefined; | |
1de1a317 L |
1001 | h->root.u.undef.abfd = abfd; |
1002 | } | |
1003 | else | |
1004 | { | |
1005 | h->root.type = bfd_link_hash_new; | |
1006 | h->root.u.undef.abfd = NULL; | |
1007 | } | |
1008 | ||
f5385ebf | 1009 | if (h->def_dynamic) |
252b5132 | 1010 | { |
f5385ebf AM |
1011 | h->def_dynamic = 0; |
1012 | h->ref_dynamic = 1; | |
1013 | h->dynamic_def = 1; | |
45d6a902 AM |
1014 | } |
1015 | /* FIXME: Should we check type and size for protected symbol? */ | |
1016 | h->size = 0; | |
1017 | h->type = 0; | |
1018 | return TRUE; | |
1019 | } | |
14a793b2 | 1020 | |
79349b09 AM |
1021 | /* Differentiate strong and weak symbols. */ |
1022 | newweak = bind == STB_WEAK; | |
1023 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1024 | || h->root.type == bfd_link_hash_undefweak); | |
14a793b2 | 1025 | |
15b43f48 AM |
1026 | /* If a new weak symbol definition comes from a regular file and the |
1027 | old symbol comes from a dynamic library, we treat the new one as | |
1028 | strong. Similarly, an old weak symbol definition from a regular | |
1029 | file is treated as strong when the new symbol comes from a dynamic | |
1030 | library. Further, an old weak symbol from a dynamic library is | |
1031 | treated as strong if the new symbol is from a dynamic library. | |
1032 | This reflects the way glibc's ld.so works. | |
1033 | ||
1034 | Do this before setting *type_change_ok or *size_change_ok so that | |
1035 | we warn properly when dynamic library symbols are overridden. */ | |
1036 | ||
1037 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1038 | newweak = FALSE; |
15b43f48 | 1039 | if (olddef && newdyn) |
0f8a2703 AM |
1040 | oldweak = FALSE; |
1041 | ||
79349b09 AM |
1042 | /* It's OK to change the type if either the existing symbol or the |
1043 | new symbol is weak. A type change is also OK if the old symbol | |
1044 | is undefined and the new symbol is defined. */ | |
252b5132 | 1045 | |
79349b09 AM |
1046 | if (oldweak |
1047 | || newweak | |
1048 | || (newdef | |
1049 | && h->root.type == bfd_link_hash_undefined)) | |
1050 | *type_change_ok = TRUE; | |
1051 | ||
1052 | /* It's OK to change the size if either the existing symbol or the | |
1053 | new symbol is weak, or if the old symbol is undefined. */ | |
1054 | ||
1055 | if (*type_change_ok | |
1056 | || h->root.type == bfd_link_hash_undefined) | |
1057 | *size_change_ok = TRUE; | |
45d6a902 | 1058 | |
45d6a902 AM |
1059 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1060 | symbol, respectively, appears to be a common symbol in a dynamic | |
1061 | object. If a symbol appears in an uninitialized section, and is | |
1062 | not weak, and is not a function, then it may be a common symbol | |
1063 | which was resolved when the dynamic object was created. We want | |
1064 | to treat such symbols specially, because they raise special | |
1065 | considerations when setting the symbol size: if the symbol | |
1066 | appears as a common symbol in a regular object, and the size in | |
1067 | the regular object is larger, we must make sure that we use the | |
1068 | larger size. This problematic case can always be avoided in C, | |
1069 | but it must be handled correctly when using Fortran shared | |
1070 | libraries. | |
1071 | ||
1072 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1073 | likewise for OLDDYNCOMMON and OLDDEF. | |
1074 | ||
1075 | Note that this test is just a heuristic, and that it is quite | |
1076 | possible to have an uninitialized symbol in a shared object which | |
1077 | is really a definition, rather than a common symbol. This could | |
1078 | lead to some minor confusion when the symbol really is a common | |
1079 | symbol in some regular object. However, I think it will be | |
1080 | harmless. */ | |
1081 | ||
1082 | if (newdyn | |
1083 | && newdef | |
79349b09 | 1084 | && !newweak |
45d6a902 AM |
1085 | && (sec->flags & SEC_ALLOC) != 0 |
1086 | && (sec->flags & SEC_LOAD) == 0 | |
1087 | && sym->st_size > 0 | |
45d6a902 AM |
1088 | && ELF_ST_TYPE (sym->st_info) != STT_FUNC) |
1089 | newdyncommon = TRUE; | |
1090 | else | |
1091 | newdyncommon = FALSE; | |
1092 | ||
1093 | if (olddyn | |
1094 | && olddef | |
1095 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1096 | && h->def_dynamic |
45d6a902 AM |
1097 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1098 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1099 | && h->size > 0 | |
1100 | && h->type != STT_FUNC) | |
1101 | olddyncommon = TRUE; | |
1102 | else | |
1103 | olddyncommon = FALSE; | |
1104 | ||
a4d8e49b L |
1105 | /* We now know everything about the old and new symbols. We ask the |
1106 | backend to check if we can merge them. */ | |
1107 | bed = get_elf_backend_data (abfd); | |
1108 | if (bed->merge_symbol | |
1109 | && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue, | |
1110 | pold_alignment, skip, override, | |
1111 | type_change_ok, size_change_ok, | |
1112 | &newdyn, &newdef, &newdyncommon, &newweak, | |
1113 | abfd, &sec, | |
1114 | &olddyn, &olddef, &olddyncommon, &oldweak, | |
1115 | oldbfd, &oldsec)) | |
1116 | return FALSE; | |
1117 | ||
45d6a902 AM |
1118 | /* If both the old and the new symbols look like common symbols in a |
1119 | dynamic object, set the size of the symbol to the larger of the | |
1120 | two. */ | |
1121 | ||
1122 | if (olddyncommon | |
1123 | && newdyncommon | |
1124 | && sym->st_size != h->size) | |
1125 | { | |
1126 | /* Since we think we have two common symbols, issue a multiple | |
1127 | common warning if desired. Note that we only warn if the | |
1128 | size is different. If the size is the same, we simply let | |
1129 | the old symbol override the new one as normally happens with | |
1130 | symbols defined in dynamic objects. */ | |
1131 | ||
1132 | if (! ((*info->callbacks->multiple_common) | |
1133 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1134 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1135 | return FALSE; | |
252b5132 | 1136 | |
45d6a902 AM |
1137 | if (sym->st_size > h->size) |
1138 | h->size = sym->st_size; | |
252b5132 | 1139 | |
45d6a902 | 1140 | *size_change_ok = TRUE; |
252b5132 RH |
1141 | } |
1142 | ||
45d6a902 AM |
1143 | /* If we are looking at a dynamic object, and we have found a |
1144 | definition, we need to see if the symbol was already defined by | |
1145 | some other object. If so, we want to use the existing | |
1146 | definition, and we do not want to report a multiple symbol | |
1147 | definition error; we do this by clobbering *PSEC to be | |
1148 | bfd_und_section_ptr. | |
1149 | ||
1150 | We treat a common symbol as a definition if the symbol in the | |
1151 | shared library is a function, since common symbols always | |
1152 | represent variables; this can cause confusion in principle, but | |
1153 | any such confusion would seem to indicate an erroneous program or | |
1154 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1155 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1156 | |
1157 | if (newdyn | |
1158 | && newdef | |
77cfaee6 | 1159 | && (olddef |
45d6a902 | 1160 | || (h->root.type == bfd_link_hash_common |
79349b09 | 1161 | && (newweak |
0f8a2703 | 1162 | || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))) |
45d6a902 AM |
1163 | { |
1164 | *override = TRUE; | |
1165 | newdef = FALSE; | |
1166 | newdyncommon = FALSE; | |
252b5132 | 1167 | |
45d6a902 AM |
1168 | *psec = sec = bfd_und_section_ptr; |
1169 | *size_change_ok = TRUE; | |
252b5132 | 1170 | |
45d6a902 AM |
1171 | /* If we get here when the old symbol is a common symbol, then |
1172 | we are explicitly letting it override a weak symbol or | |
1173 | function in a dynamic object, and we don't want to warn about | |
1174 | a type change. If the old symbol is a defined symbol, a type | |
1175 | change warning may still be appropriate. */ | |
252b5132 | 1176 | |
45d6a902 AM |
1177 | if (h->root.type == bfd_link_hash_common) |
1178 | *type_change_ok = TRUE; | |
1179 | } | |
1180 | ||
1181 | /* Handle the special case of an old common symbol merging with a | |
1182 | new symbol which looks like a common symbol in a shared object. | |
1183 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1184 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1185 | right thing. */ | |
45d6a902 AM |
1186 | |
1187 | if (newdyncommon | |
1188 | && h->root.type == bfd_link_hash_common) | |
1189 | { | |
1190 | *override = TRUE; | |
1191 | newdef = FALSE; | |
1192 | newdyncommon = FALSE; | |
1193 | *pvalue = sym->st_size; | |
a4d8e49b | 1194 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1195 | *size_change_ok = TRUE; |
1196 | } | |
1197 | ||
c5e2cead | 1198 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1199 | if (newdef && olddef && newweak) |
c5e2cead L |
1200 | *skip = TRUE; |
1201 | ||
45d6a902 AM |
1202 | /* If the old symbol is from a dynamic object, and the new symbol is |
1203 | a definition which is not from a dynamic object, then the new | |
1204 | symbol overrides the old symbol. Symbols from regular files | |
1205 | always take precedence over symbols from dynamic objects, even if | |
1206 | they are defined after the dynamic object in the link. | |
1207 | ||
1208 | As above, we again permit a common symbol in a regular object to | |
1209 | override a definition in a shared object if the shared object | |
0f8a2703 | 1210 | symbol is a function or is weak. */ |
45d6a902 AM |
1211 | |
1212 | flip = NULL; | |
77cfaee6 | 1213 | if (!newdyn |
45d6a902 AM |
1214 | && (newdef |
1215 | || (bfd_is_com_section (sec) | |
79349b09 AM |
1216 | && (oldweak |
1217 | || h->type == STT_FUNC))) | |
45d6a902 AM |
1218 | && olddyn |
1219 | && olddef | |
f5385ebf | 1220 | && h->def_dynamic) |
45d6a902 AM |
1221 | { |
1222 | /* Change the hash table entry to undefined, and let | |
1223 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1224 | new definition. */ | |
1225 | ||
1226 | h->root.type = bfd_link_hash_undefined; | |
1227 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1228 | *size_change_ok = TRUE; | |
1229 | ||
1230 | olddef = FALSE; | |
1231 | olddyncommon = FALSE; | |
1232 | ||
1233 | /* We again permit a type change when a common symbol may be | |
1234 | overriding a function. */ | |
1235 | ||
1236 | if (bfd_is_com_section (sec)) | |
1237 | *type_change_ok = TRUE; | |
1238 | ||
1239 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1240 | flip = *sym_hash; | |
1241 | else | |
1242 | /* This union may have been set to be non-NULL when this symbol | |
1243 | was seen in a dynamic object. We must force the union to be | |
1244 | NULL, so that it is correct for a regular symbol. */ | |
1245 | h->verinfo.vertree = NULL; | |
1246 | } | |
1247 | ||
1248 | /* Handle the special case of a new common symbol merging with an | |
1249 | old symbol that looks like it might be a common symbol defined in | |
1250 | a shared object. Note that we have already handled the case in | |
1251 | which a new common symbol should simply override the definition | |
1252 | in the shared library. */ | |
1253 | ||
1254 | if (! newdyn | |
1255 | && bfd_is_com_section (sec) | |
1256 | && olddyncommon) | |
1257 | { | |
1258 | /* It would be best if we could set the hash table entry to a | |
1259 | common symbol, but we don't know what to use for the section | |
1260 | or the alignment. */ | |
1261 | if (! ((*info->callbacks->multiple_common) | |
1262 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1263 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1264 | return FALSE; | |
1265 | ||
4cc11e76 | 1266 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1267 | larger, pretend that the new symbol has its size. */ |
1268 | ||
1269 | if (h->size > *pvalue) | |
1270 | *pvalue = h->size; | |
1271 | ||
af44c138 L |
1272 | /* We need to remember the alignment required by the symbol |
1273 | in the dynamic object. */ | |
1274 | BFD_ASSERT (pold_alignment); | |
1275 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1276 | |
1277 | olddef = FALSE; | |
1278 | olddyncommon = FALSE; | |
1279 | ||
1280 | h->root.type = bfd_link_hash_undefined; | |
1281 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1282 | ||
1283 | *size_change_ok = TRUE; | |
1284 | *type_change_ok = TRUE; | |
1285 | ||
1286 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1287 | flip = *sym_hash; | |
1288 | else | |
1289 | h->verinfo.vertree = NULL; | |
1290 | } | |
1291 | ||
1292 | if (flip != NULL) | |
1293 | { | |
1294 | /* Handle the case where we had a versioned symbol in a dynamic | |
1295 | library and now find a definition in a normal object. In this | |
1296 | case, we make the versioned symbol point to the normal one. */ | |
9c5bfbb7 | 1297 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1298 | flip->root.type = h->root.type; |
1299 | h->root.type = bfd_link_hash_indirect; | |
1300 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1301 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
45d6a902 | 1302 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
f5385ebf | 1303 | if (h->def_dynamic) |
45d6a902 | 1304 | { |
f5385ebf AM |
1305 | h->def_dynamic = 0; |
1306 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1307 | } |
1308 | } | |
1309 | ||
45d6a902 AM |
1310 | return TRUE; |
1311 | } | |
1312 | ||
1313 | /* This function is called to create an indirect symbol from the | |
1314 | default for the symbol with the default version if needed. The | |
1315 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1316 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 AM |
1317 | |
1318 | bfd_boolean | |
268b6b39 AM |
1319 | _bfd_elf_add_default_symbol (bfd *abfd, |
1320 | struct bfd_link_info *info, | |
1321 | struct elf_link_hash_entry *h, | |
1322 | const char *name, | |
1323 | Elf_Internal_Sym *sym, | |
1324 | asection **psec, | |
1325 | bfd_vma *value, | |
1326 | bfd_boolean *dynsym, | |
0f8a2703 | 1327 | bfd_boolean override) |
45d6a902 AM |
1328 | { |
1329 | bfd_boolean type_change_ok; | |
1330 | bfd_boolean size_change_ok; | |
1331 | bfd_boolean skip; | |
1332 | char *shortname; | |
1333 | struct elf_link_hash_entry *hi; | |
1334 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1335 | const struct elf_backend_data *bed; |
45d6a902 AM |
1336 | bfd_boolean collect; |
1337 | bfd_boolean dynamic; | |
1338 | char *p; | |
1339 | size_t len, shortlen; | |
1340 | asection *sec; | |
1341 | ||
1342 | /* If this symbol has a version, and it is the default version, we | |
1343 | create an indirect symbol from the default name to the fully | |
1344 | decorated name. This will cause external references which do not | |
1345 | specify a version to be bound to this version of the symbol. */ | |
1346 | p = strchr (name, ELF_VER_CHR); | |
1347 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1348 | return TRUE; | |
1349 | ||
1350 | if (override) | |
1351 | { | |
4cc11e76 | 1352 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1353 | need to create the indirect symbol from the default name. */ |
1354 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1355 | FALSE, FALSE); | |
1356 | BFD_ASSERT (hi != NULL); | |
1357 | if (hi == h) | |
1358 | return TRUE; | |
1359 | while (hi->root.type == bfd_link_hash_indirect | |
1360 | || hi->root.type == bfd_link_hash_warning) | |
1361 | { | |
1362 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1363 | if (hi == h) | |
1364 | return TRUE; | |
1365 | } | |
1366 | } | |
1367 | ||
1368 | bed = get_elf_backend_data (abfd); | |
1369 | collect = bed->collect; | |
1370 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1371 | ||
1372 | shortlen = p - name; | |
1373 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1374 | if (shortname == NULL) | |
1375 | return FALSE; | |
1376 | memcpy (shortname, name, shortlen); | |
1377 | shortname[shortlen] = '\0'; | |
1378 | ||
1379 | /* We are going to create a new symbol. Merge it with any existing | |
1380 | symbol with this name. For the purposes of the merge, act as | |
1381 | though we were defining the symbol we just defined, although we | |
1382 | actually going to define an indirect symbol. */ | |
1383 | type_change_ok = FALSE; | |
1384 | size_change_ok = FALSE; | |
1385 | sec = *psec; | |
1386 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1387 | NULL, &hi, &skip, &override, |
1388 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1389 | return FALSE; |
1390 | ||
1391 | if (skip) | |
1392 | goto nondefault; | |
1393 | ||
1394 | if (! override) | |
1395 | { | |
1396 | bh = &hi->root; | |
1397 | if (! (_bfd_generic_link_add_one_symbol | |
1398 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1399 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1400 | return FALSE; |
1401 | hi = (struct elf_link_hash_entry *) bh; | |
1402 | } | |
1403 | else | |
1404 | { | |
1405 | /* In this case the symbol named SHORTNAME is overriding the | |
1406 | indirect symbol we want to add. We were planning on making | |
1407 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1408 | is the name without a version. NAME is the fully versioned | |
1409 | name, and it is the default version. | |
1410 | ||
1411 | Overriding means that we already saw a definition for the | |
1412 | symbol SHORTNAME in a regular object, and it is overriding | |
1413 | the symbol defined in the dynamic object. | |
1414 | ||
1415 | When this happens, we actually want to change NAME, the | |
1416 | symbol we just added, to refer to SHORTNAME. This will cause | |
1417 | references to NAME in the shared object to become references | |
1418 | to SHORTNAME in the regular object. This is what we expect | |
1419 | when we override a function in a shared object: that the | |
1420 | references in the shared object will be mapped to the | |
1421 | definition in the regular object. */ | |
1422 | ||
1423 | while (hi->root.type == bfd_link_hash_indirect | |
1424 | || hi->root.type == bfd_link_hash_warning) | |
1425 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1426 | ||
1427 | h->root.type = bfd_link_hash_indirect; | |
1428 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1429 | if (h->def_dynamic) |
45d6a902 | 1430 | { |
f5385ebf AM |
1431 | h->def_dynamic = 0; |
1432 | hi->ref_dynamic = 1; | |
1433 | if (hi->ref_regular | |
1434 | || hi->def_regular) | |
45d6a902 | 1435 | { |
c152c796 | 1436 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1437 | return FALSE; |
1438 | } | |
1439 | } | |
1440 | ||
1441 | /* Now set HI to H, so that the following code will set the | |
1442 | other fields correctly. */ | |
1443 | hi = h; | |
1444 | } | |
1445 | ||
1446 | /* If there is a duplicate definition somewhere, then HI may not | |
1447 | point to an indirect symbol. We will have reported an error to | |
1448 | the user in that case. */ | |
1449 | ||
1450 | if (hi->root.type == bfd_link_hash_indirect) | |
1451 | { | |
1452 | struct elf_link_hash_entry *ht; | |
1453 | ||
45d6a902 | 1454 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 1455 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 AM |
1456 | |
1457 | /* See if the new flags lead us to realize that the symbol must | |
1458 | be dynamic. */ | |
1459 | if (! *dynsym) | |
1460 | { | |
1461 | if (! dynamic) | |
1462 | { | |
1463 | if (info->shared | |
f5385ebf | 1464 | || hi->ref_dynamic) |
45d6a902 AM |
1465 | *dynsym = TRUE; |
1466 | } | |
1467 | else | |
1468 | { | |
f5385ebf | 1469 | if (hi->ref_regular) |
45d6a902 AM |
1470 | *dynsym = TRUE; |
1471 | } | |
1472 | } | |
1473 | } | |
1474 | ||
1475 | /* We also need to define an indirection from the nondefault version | |
1476 | of the symbol. */ | |
1477 | ||
1478 | nondefault: | |
1479 | len = strlen (name); | |
1480 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1481 | if (shortname == NULL) | |
1482 | return FALSE; | |
1483 | memcpy (shortname, name, shortlen); | |
1484 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1485 | ||
1486 | /* Once again, merge with any existing symbol. */ | |
1487 | type_change_ok = FALSE; | |
1488 | size_change_ok = FALSE; | |
1489 | sec = *psec; | |
1490 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1491 | NULL, &hi, &skip, &override, |
1492 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1493 | return FALSE; |
1494 | ||
1495 | if (skip) | |
1496 | return TRUE; | |
1497 | ||
1498 | if (override) | |
1499 | { | |
1500 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1501 | the type of override we do in the case above unless it is | |
4cc11e76 | 1502 | overridden by a versioned definition. */ |
45d6a902 AM |
1503 | if (hi->root.type != bfd_link_hash_defined |
1504 | && hi->root.type != bfd_link_hash_defweak) | |
1505 | (*_bfd_error_handler) | |
d003868e AM |
1506 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1507 | abfd, shortname); | |
45d6a902 AM |
1508 | } |
1509 | else | |
1510 | { | |
1511 | bh = &hi->root; | |
1512 | if (! (_bfd_generic_link_add_one_symbol | |
1513 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1514 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1515 | return FALSE; |
1516 | hi = (struct elf_link_hash_entry *) bh; | |
1517 | ||
1518 | /* If there is a duplicate definition somewhere, then HI may not | |
1519 | point to an indirect symbol. We will have reported an error | |
1520 | to the user in that case. */ | |
1521 | ||
1522 | if (hi->root.type == bfd_link_hash_indirect) | |
1523 | { | |
fcfa13d2 | 1524 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
45d6a902 AM |
1525 | |
1526 | /* See if the new flags lead us to realize that the symbol | |
1527 | must be dynamic. */ | |
1528 | if (! *dynsym) | |
1529 | { | |
1530 | if (! dynamic) | |
1531 | { | |
1532 | if (info->shared | |
f5385ebf | 1533 | || hi->ref_dynamic) |
45d6a902 AM |
1534 | *dynsym = TRUE; |
1535 | } | |
1536 | else | |
1537 | { | |
f5385ebf | 1538 | if (hi->ref_regular) |
45d6a902 AM |
1539 | *dynsym = TRUE; |
1540 | } | |
1541 | } | |
1542 | } | |
1543 | } | |
1544 | ||
1545 | return TRUE; | |
1546 | } | |
1547 | \f | |
1548 | /* This routine is used to export all defined symbols into the dynamic | |
1549 | symbol table. It is called via elf_link_hash_traverse. */ | |
1550 | ||
1551 | bfd_boolean | |
268b6b39 | 1552 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1553 | { |
268b6b39 | 1554 | struct elf_info_failed *eif = data; |
45d6a902 AM |
1555 | |
1556 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
1557 | if (h->root.type == bfd_link_hash_indirect) | |
1558 | return TRUE; | |
1559 | ||
1560 | if (h->root.type == bfd_link_hash_warning) | |
1561 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1562 | ||
1563 | if (h->dynindx == -1 | |
f5385ebf AM |
1564 | && (h->def_regular |
1565 | || h->ref_regular)) | |
45d6a902 AM |
1566 | { |
1567 | struct bfd_elf_version_tree *t; | |
1568 | struct bfd_elf_version_expr *d; | |
1569 | ||
1570 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1571 | { | |
108ba305 | 1572 | if (t->globals.list != NULL) |
45d6a902 | 1573 | { |
108ba305 JJ |
1574 | d = (*t->match) (&t->globals, NULL, h->root.root.string); |
1575 | if (d != NULL) | |
1576 | goto doit; | |
45d6a902 AM |
1577 | } |
1578 | ||
108ba305 | 1579 | if (t->locals.list != NULL) |
45d6a902 | 1580 | { |
108ba305 JJ |
1581 | d = (*t->match) (&t->locals, NULL, h->root.root.string); |
1582 | if (d != NULL) | |
1583 | return TRUE; | |
45d6a902 AM |
1584 | } |
1585 | } | |
1586 | ||
1587 | if (!eif->verdefs) | |
1588 | { | |
1589 | doit: | |
c152c796 | 1590 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
1591 | { |
1592 | eif->failed = TRUE; | |
1593 | return FALSE; | |
1594 | } | |
1595 | } | |
1596 | } | |
1597 | ||
1598 | return TRUE; | |
1599 | } | |
1600 | \f | |
1601 | /* Look through the symbols which are defined in other shared | |
1602 | libraries and referenced here. Update the list of version | |
1603 | dependencies. This will be put into the .gnu.version_r section. | |
1604 | This function is called via elf_link_hash_traverse. */ | |
1605 | ||
1606 | bfd_boolean | |
268b6b39 AM |
1607 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1608 | void *data) | |
45d6a902 | 1609 | { |
268b6b39 | 1610 | struct elf_find_verdep_info *rinfo = data; |
45d6a902 AM |
1611 | Elf_Internal_Verneed *t; |
1612 | Elf_Internal_Vernaux *a; | |
1613 | bfd_size_type amt; | |
1614 | ||
1615 | if (h->root.type == bfd_link_hash_warning) | |
1616 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1617 | ||
1618 | /* We only care about symbols defined in shared objects with version | |
1619 | information. */ | |
f5385ebf AM |
1620 | if (!h->def_dynamic |
1621 | || h->def_regular | |
45d6a902 AM |
1622 | || h->dynindx == -1 |
1623 | || h->verinfo.verdef == NULL) | |
1624 | return TRUE; | |
1625 | ||
1626 | /* See if we already know about this version. */ | |
1627 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1628 | { | |
1629 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1630 | continue; | |
1631 | ||
1632 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1633 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1634 | return TRUE; | |
1635 | ||
1636 | break; | |
1637 | } | |
1638 | ||
1639 | /* This is a new version. Add it to tree we are building. */ | |
1640 | ||
1641 | if (t == NULL) | |
1642 | { | |
1643 | amt = sizeof *t; | |
268b6b39 | 1644 | t = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1645 | if (t == NULL) |
1646 | { | |
1647 | rinfo->failed = TRUE; | |
1648 | return FALSE; | |
1649 | } | |
1650 | ||
1651 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1652 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1653 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1654 | } | |
1655 | ||
1656 | amt = sizeof *a; | |
268b6b39 | 1657 | a = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1658 | |
1659 | /* Note that we are copying a string pointer here, and testing it | |
1660 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1661 | discard the string data when low in memory, this will have to be | |
1662 | fixed. */ | |
1663 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1664 | ||
1665 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1666 | a->vna_nextptr = t->vn_auxptr; | |
1667 | ||
1668 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1669 | ++rinfo->vers; | |
1670 | ||
1671 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1672 | ||
1673 | t->vn_auxptr = a; | |
1674 | ||
1675 | return TRUE; | |
1676 | } | |
1677 | ||
1678 | /* Figure out appropriate versions for all the symbols. We may not | |
1679 | have the version number script until we have read all of the input | |
1680 | files, so until that point we don't know which symbols should be | |
1681 | local. This function is called via elf_link_hash_traverse. */ | |
1682 | ||
1683 | bfd_boolean | |
268b6b39 | 1684 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
1685 | { |
1686 | struct elf_assign_sym_version_info *sinfo; | |
1687 | struct bfd_link_info *info; | |
9c5bfbb7 | 1688 | const struct elf_backend_data *bed; |
45d6a902 AM |
1689 | struct elf_info_failed eif; |
1690 | char *p; | |
1691 | bfd_size_type amt; | |
1692 | ||
268b6b39 | 1693 | sinfo = data; |
45d6a902 AM |
1694 | info = sinfo->info; |
1695 | ||
1696 | if (h->root.type == bfd_link_hash_warning) | |
1697 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1698 | ||
1699 | /* Fix the symbol flags. */ | |
1700 | eif.failed = FALSE; | |
1701 | eif.info = info; | |
1702 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1703 | { | |
1704 | if (eif.failed) | |
1705 | sinfo->failed = TRUE; | |
1706 | return FALSE; | |
1707 | } | |
1708 | ||
1709 | /* We only need version numbers for symbols defined in regular | |
1710 | objects. */ | |
f5385ebf | 1711 | if (!h->def_regular) |
45d6a902 AM |
1712 | return TRUE; |
1713 | ||
1714 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1715 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1716 | if (p != NULL && h->verinfo.vertree == NULL) | |
1717 | { | |
1718 | struct bfd_elf_version_tree *t; | |
1719 | bfd_boolean hidden; | |
1720 | ||
1721 | hidden = TRUE; | |
1722 | ||
1723 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1724 | not a hidden symbol. */ | |
1725 | ++p; | |
1726 | if (*p == ELF_VER_CHR) | |
1727 | { | |
1728 | hidden = FALSE; | |
1729 | ++p; | |
1730 | } | |
1731 | ||
1732 | /* If there is no version string, we can just return out. */ | |
1733 | if (*p == '\0') | |
1734 | { | |
1735 | if (hidden) | |
f5385ebf | 1736 | h->hidden = 1; |
45d6a902 AM |
1737 | return TRUE; |
1738 | } | |
1739 | ||
1740 | /* Look for the version. If we find it, it is no longer weak. */ | |
1741 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1742 | { | |
1743 | if (strcmp (t->name, p) == 0) | |
1744 | { | |
1745 | size_t len; | |
1746 | char *alc; | |
1747 | struct bfd_elf_version_expr *d; | |
1748 | ||
1749 | len = p - h->root.root.string; | |
268b6b39 | 1750 | alc = bfd_malloc (len); |
45d6a902 AM |
1751 | if (alc == NULL) |
1752 | return FALSE; | |
1753 | memcpy (alc, h->root.root.string, len - 1); | |
1754 | alc[len - 1] = '\0'; | |
1755 | if (alc[len - 2] == ELF_VER_CHR) | |
1756 | alc[len - 2] = '\0'; | |
1757 | ||
1758 | h->verinfo.vertree = t; | |
1759 | t->used = TRUE; | |
1760 | d = NULL; | |
1761 | ||
108ba305 JJ |
1762 | if (t->globals.list != NULL) |
1763 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
1764 | |
1765 | /* See if there is anything to force this symbol to | |
1766 | local scope. */ | |
108ba305 | 1767 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 1768 | { |
108ba305 JJ |
1769 | d = (*t->match) (&t->locals, NULL, alc); |
1770 | if (d != NULL | |
1771 | && h->dynindx != -1 | |
108ba305 JJ |
1772 | && ! info->export_dynamic) |
1773 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
1774 | } |
1775 | ||
1776 | free (alc); | |
1777 | break; | |
1778 | } | |
1779 | } | |
1780 | ||
1781 | /* If we are building an application, we need to create a | |
1782 | version node for this version. */ | |
36af4a4e | 1783 | if (t == NULL && info->executable) |
45d6a902 AM |
1784 | { |
1785 | struct bfd_elf_version_tree **pp; | |
1786 | int version_index; | |
1787 | ||
1788 | /* If we aren't going to export this symbol, we don't need | |
1789 | to worry about it. */ | |
1790 | if (h->dynindx == -1) | |
1791 | return TRUE; | |
1792 | ||
1793 | amt = sizeof *t; | |
108ba305 | 1794 | t = bfd_zalloc (sinfo->output_bfd, amt); |
45d6a902 AM |
1795 | if (t == NULL) |
1796 | { | |
1797 | sinfo->failed = TRUE; | |
1798 | return FALSE; | |
1799 | } | |
1800 | ||
45d6a902 | 1801 | t->name = p; |
45d6a902 AM |
1802 | t->name_indx = (unsigned int) -1; |
1803 | t->used = TRUE; | |
1804 | ||
1805 | version_index = 1; | |
1806 | /* Don't count anonymous version tag. */ | |
1807 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1808 | version_index = 0; | |
1809 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1810 | ++version_index; | |
1811 | t->vernum = version_index; | |
1812 | ||
1813 | *pp = t; | |
1814 | ||
1815 | h->verinfo.vertree = t; | |
1816 | } | |
1817 | else if (t == NULL) | |
1818 | { | |
1819 | /* We could not find the version for a symbol when | |
1820 | generating a shared archive. Return an error. */ | |
1821 | (*_bfd_error_handler) | |
d003868e AM |
1822 | (_("%B: undefined versioned symbol name %s"), |
1823 | sinfo->output_bfd, h->root.root.string); | |
45d6a902 AM |
1824 | bfd_set_error (bfd_error_bad_value); |
1825 | sinfo->failed = TRUE; | |
1826 | return FALSE; | |
1827 | } | |
1828 | ||
1829 | if (hidden) | |
f5385ebf | 1830 | h->hidden = 1; |
45d6a902 AM |
1831 | } |
1832 | ||
1833 | /* If we don't have a version for this symbol, see if we can find | |
1834 | something. */ | |
1835 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
1836 | { | |
1837 | struct bfd_elf_version_tree *t; | |
1838 | struct bfd_elf_version_tree *local_ver; | |
1839 | struct bfd_elf_version_expr *d; | |
1840 | ||
1841 | /* See if can find what version this symbol is in. If the | |
1842 | symbol is supposed to be local, then don't actually register | |
1843 | it. */ | |
1844 | local_ver = NULL; | |
1845 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1846 | { | |
108ba305 | 1847 | if (t->globals.list != NULL) |
45d6a902 AM |
1848 | { |
1849 | bfd_boolean matched; | |
1850 | ||
1851 | matched = FALSE; | |
108ba305 JJ |
1852 | d = NULL; |
1853 | while ((d = (*t->match) (&t->globals, d, | |
1854 | h->root.root.string)) != NULL) | |
1855 | if (d->symver) | |
1856 | matched = TRUE; | |
1857 | else | |
1858 | { | |
1859 | /* There is a version without definition. Make | |
1860 | the symbol the default definition for this | |
1861 | version. */ | |
1862 | h->verinfo.vertree = t; | |
1863 | local_ver = NULL; | |
1864 | d->script = 1; | |
1865 | break; | |
1866 | } | |
45d6a902 AM |
1867 | if (d != NULL) |
1868 | break; | |
1869 | else if (matched) | |
1870 | /* There is no undefined version for this symbol. Hide the | |
1871 | default one. */ | |
1872 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1873 | } | |
1874 | ||
108ba305 | 1875 | if (t->locals.list != NULL) |
45d6a902 | 1876 | { |
108ba305 JJ |
1877 | d = NULL; |
1878 | while ((d = (*t->match) (&t->locals, d, | |
1879 | h->root.root.string)) != NULL) | |
45d6a902 | 1880 | { |
108ba305 | 1881 | local_ver = t; |
45d6a902 | 1882 | /* If the match is "*", keep looking for a more |
108ba305 JJ |
1883 | explicit, perhaps even global, match. |
1884 | XXX: Shouldn't this be !d->wildcard instead? */ | |
1885 | if (d->pattern[0] != '*' || d->pattern[1] != '\0') | |
1886 | break; | |
45d6a902 AM |
1887 | } |
1888 | ||
1889 | if (d != NULL) | |
1890 | break; | |
1891 | } | |
1892 | } | |
1893 | ||
1894 | if (local_ver != NULL) | |
1895 | { | |
1896 | h->verinfo.vertree = local_ver; | |
1897 | if (h->dynindx != -1 | |
45d6a902 AM |
1898 | && ! info->export_dynamic) |
1899 | { | |
1900 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1901 | } | |
1902 | } | |
1903 | } | |
1904 | ||
1905 | return TRUE; | |
1906 | } | |
1907 | \f | |
45d6a902 AM |
1908 | /* Read and swap the relocs from the section indicated by SHDR. This |
1909 | may be either a REL or a RELA section. The relocations are | |
1910 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
1911 | which should have already been allocated to contain enough space. | |
1912 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
1913 | relocations should be stored. | |
1914 | ||
1915 | Returns FALSE if something goes wrong. */ | |
1916 | ||
1917 | static bfd_boolean | |
268b6b39 | 1918 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 1919 | asection *sec, |
268b6b39 AM |
1920 | Elf_Internal_Shdr *shdr, |
1921 | void *external_relocs, | |
1922 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 1923 | { |
9c5bfbb7 | 1924 | const struct elf_backend_data *bed; |
268b6b39 | 1925 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
1926 | const bfd_byte *erela; |
1927 | const bfd_byte *erelaend; | |
1928 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
1929 | Elf_Internal_Shdr *symtab_hdr; |
1930 | size_t nsyms; | |
45d6a902 | 1931 | |
45d6a902 AM |
1932 | /* Position ourselves at the start of the section. */ |
1933 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
1934 | return FALSE; | |
1935 | ||
1936 | /* Read the relocations. */ | |
1937 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
1938 | return FALSE; | |
1939 | ||
243ef1e0 L |
1940 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1941 | nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
1942 | ||
45d6a902 AM |
1943 | bed = get_elf_backend_data (abfd); |
1944 | ||
1945 | /* Convert the external relocations to the internal format. */ | |
1946 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
1947 | swap_in = bed->s->swap_reloc_in; | |
1948 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
1949 | swap_in = bed->s->swap_reloca_in; | |
1950 | else | |
1951 | { | |
1952 | bfd_set_error (bfd_error_wrong_format); | |
1953 | return FALSE; | |
1954 | } | |
1955 | ||
1956 | erela = external_relocs; | |
51992aec | 1957 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
1958 | irela = internal_relocs; |
1959 | while (erela < erelaend) | |
1960 | { | |
243ef1e0 L |
1961 | bfd_vma r_symndx; |
1962 | ||
45d6a902 | 1963 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
1964 | r_symndx = ELF32_R_SYM (irela->r_info); |
1965 | if (bed->s->arch_size == 64) | |
1966 | r_symndx >>= 24; | |
1967 | if ((size_t) r_symndx >= nsyms) | |
1968 | { | |
1969 | (*_bfd_error_handler) | |
d003868e AM |
1970 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" |
1971 | " for offset 0x%lx in section `%A'"), | |
1972 | abfd, sec, | |
1973 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
1974 | bfd_set_error (bfd_error_bad_value); |
1975 | return FALSE; | |
1976 | } | |
45d6a902 AM |
1977 | irela += bed->s->int_rels_per_ext_rel; |
1978 | erela += shdr->sh_entsize; | |
1979 | } | |
1980 | ||
1981 | return TRUE; | |
1982 | } | |
1983 | ||
1984 | /* Read and swap the relocs for a section O. They may have been | |
1985 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
1986 | not NULL, they are used as buffers to read into. They are known to | |
1987 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
1988 | the return value is allocated using either malloc or bfd_alloc, | |
1989 | according to the KEEP_MEMORY argument. If O has two relocation | |
1990 | sections (both REL and RELA relocations), then the REL_HDR | |
1991 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
1992 | REL_HDR2 relocations. */ | |
1993 | ||
1994 | Elf_Internal_Rela * | |
268b6b39 AM |
1995 | _bfd_elf_link_read_relocs (bfd *abfd, |
1996 | asection *o, | |
1997 | void *external_relocs, | |
1998 | Elf_Internal_Rela *internal_relocs, | |
1999 | bfd_boolean keep_memory) | |
45d6a902 AM |
2000 | { |
2001 | Elf_Internal_Shdr *rel_hdr; | |
268b6b39 | 2002 | void *alloc1 = NULL; |
45d6a902 | 2003 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2004 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
2005 | |
2006 | if (elf_section_data (o)->relocs != NULL) | |
2007 | return elf_section_data (o)->relocs; | |
2008 | ||
2009 | if (o->reloc_count == 0) | |
2010 | return NULL; | |
2011 | ||
2012 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
2013 | ||
2014 | if (internal_relocs == NULL) | |
2015 | { | |
2016 | bfd_size_type size; | |
2017 | ||
2018 | size = o->reloc_count; | |
2019 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
2020 | if (keep_memory) | |
268b6b39 | 2021 | internal_relocs = bfd_alloc (abfd, size); |
45d6a902 | 2022 | else |
268b6b39 | 2023 | internal_relocs = alloc2 = bfd_malloc (size); |
45d6a902 AM |
2024 | if (internal_relocs == NULL) |
2025 | goto error_return; | |
2026 | } | |
2027 | ||
2028 | if (external_relocs == NULL) | |
2029 | { | |
2030 | bfd_size_type size = rel_hdr->sh_size; | |
2031 | ||
2032 | if (elf_section_data (o)->rel_hdr2) | |
2033 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
268b6b39 | 2034 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2035 | if (alloc1 == NULL) |
2036 | goto error_return; | |
2037 | external_relocs = alloc1; | |
2038 | } | |
2039 | ||
243ef1e0 | 2040 | if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, |
45d6a902 AM |
2041 | external_relocs, |
2042 | internal_relocs)) | |
2043 | goto error_return; | |
51992aec AM |
2044 | if (elf_section_data (o)->rel_hdr2 |
2045 | && (!elf_link_read_relocs_from_section | |
2046 | (abfd, o, | |
2047 | elf_section_data (o)->rel_hdr2, | |
2048 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
2049 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
2050 | * bed->s->int_rels_per_ext_rel)))) | |
45d6a902 AM |
2051 | goto error_return; |
2052 | ||
2053 | /* Cache the results for next time, if we can. */ | |
2054 | if (keep_memory) | |
2055 | elf_section_data (o)->relocs = internal_relocs; | |
2056 | ||
2057 | if (alloc1 != NULL) | |
2058 | free (alloc1); | |
2059 | ||
2060 | /* Don't free alloc2, since if it was allocated we are passing it | |
2061 | back (under the name of internal_relocs). */ | |
2062 | ||
2063 | return internal_relocs; | |
2064 | ||
2065 | error_return: | |
2066 | if (alloc1 != NULL) | |
2067 | free (alloc1); | |
2068 | if (alloc2 != NULL) | |
2069 | free (alloc2); | |
2070 | return NULL; | |
2071 | } | |
2072 | ||
2073 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2074 | section header for a section containing relocations for O. */ | |
2075 | ||
2076 | bfd_boolean | |
268b6b39 AM |
2077 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2078 | Elf_Internal_Shdr *rel_hdr, | |
2079 | asection *o) | |
45d6a902 AM |
2080 | { |
2081 | bfd_size_type reloc_count; | |
2082 | bfd_size_type num_rel_hashes; | |
2083 | ||
2084 | /* Figure out how many relocations there will be. */ | |
2085 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
2086 | reloc_count = elf_section_data (o)->rel_count; | |
2087 | else | |
2088 | reloc_count = elf_section_data (o)->rel_count2; | |
2089 | ||
2090 | num_rel_hashes = o->reloc_count; | |
2091 | if (num_rel_hashes < reloc_count) | |
2092 | num_rel_hashes = reloc_count; | |
2093 | ||
2094 | /* That allows us to calculate the size of the section. */ | |
2095 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
2096 | ||
2097 | /* The contents field must last into write_object_contents, so we | |
2098 | allocate it with bfd_alloc rather than malloc. Also since we | |
2099 | cannot be sure that the contents will actually be filled in, | |
2100 | we zero the allocated space. */ | |
268b6b39 | 2101 | rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2102 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2103 | return FALSE; | |
2104 | ||
2105 | /* We only allocate one set of hash entries, so we only do it the | |
2106 | first time we are called. */ | |
2107 | if (elf_section_data (o)->rel_hashes == NULL | |
2108 | && num_rel_hashes) | |
2109 | { | |
2110 | struct elf_link_hash_entry **p; | |
2111 | ||
268b6b39 | 2112 | p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
2113 | if (p == NULL) |
2114 | return FALSE; | |
2115 | ||
2116 | elf_section_data (o)->rel_hashes = p; | |
2117 | } | |
2118 | ||
2119 | return TRUE; | |
2120 | } | |
2121 | ||
2122 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2123 | originated from the section given by INPUT_REL_HDR) to the | |
2124 | OUTPUT_BFD. */ | |
2125 | ||
2126 | bfd_boolean | |
268b6b39 AM |
2127 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2128 | asection *input_section, | |
2129 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2130 | Elf_Internal_Rela *internal_relocs, |
2131 | struct elf_link_hash_entry **rel_hash | |
2132 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2133 | { |
2134 | Elf_Internal_Rela *irela; | |
2135 | Elf_Internal_Rela *irelaend; | |
2136 | bfd_byte *erel; | |
2137 | Elf_Internal_Shdr *output_rel_hdr; | |
2138 | asection *output_section; | |
2139 | unsigned int *rel_countp = NULL; | |
9c5bfbb7 | 2140 | const struct elf_backend_data *bed; |
268b6b39 | 2141 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
45d6a902 AM |
2142 | |
2143 | output_section = input_section->output_section; | |
2144 | output_rel_hdr = NULL; | |
2145 | ||
2146 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
2147 | == input_rel_hdr->sh_entsize) | |
2148 | { | |
2149 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
2150 | rel_countp = &elf_section_data (output_section)->rel_count; | |
2151 | } | |
2152 | else if (elf_section_data (output_section)->rel_hdr2 | |
2153 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
2154 | == input_rel_hdr->sh_entsize)) | |
2155 | { | |
2156 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2157 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2158 | } | |
2159 | else | |
2160 | { | |
2161 | (*_bfd_error_handler) | |
d003868e AM |
2162 | (_("%B: relocation size mismatch in %B section %A"), |
2163 | output_bfd, input_section->owner, input_section); | |
45d6a902 AM |
2164 | bfd_set_error (bfd_error_wrong_object_format); |
2165 | return FALSE; | |
2166 | } | |
2167 | ||
2168 | bed = get_elf_backend_data (output_bfd); | |
2169 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2170 | swap_out = bed->s->swap_reloc_out; | |
2171 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2172 | swap_out = bed->s->swap_reloca_out; | |
2173 | else | |
2174 | abort (); | |
2175 | ||
2176 | erel = output_rel_hdr->contents; | |
2177 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2178 | irela = internal_relocs; | |
2179 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2180 | * bed->s->int_rels_per_ext_rel); | |
2181 | while (irela < irelaend) | |
2182 | { | |
2183 | (*swap_out) (output_bfd, irela, erel); | |
2184 | irela += bed->s->int_rels_per_ext_rel; | |
2185 | erel += input_rel_hdr->sh_entsize; | |
2186 | } | |
2187 | ||
2188 | /* Bump the counter, so that we know where to add the next set of | |
2189 | relocations. */ | |
2190 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2191 | ||
2192 | return TRUE; | |
2193 | } | |
2194 | \f | |
508c3946 L |
2195 | /* Make weak undefined symbols in PIE dynamic. */ |
2196 | ||
2197 | bfd_boolean | |
2198 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2199 | struct elf_link_hash_entry *h) | |
2200 | { | |
2201 | if (info->pie | |
2202 | && h->dynindx == -1 | |
2203 | && h->root.type == bfd_link_hash_undefweak) | |
2204 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2205 | ||
2206 | return TRUE; | |
2207 | } | |
2208 | ||
45d6a902 AM |
2209 | /* Fix up the flags for a symbol. This handles various cases which |
2210 | can only be fixed after all the input files are seen. This is | |
2211 | currently called by both adjust_dynamic_symbol and | |
2212 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2213 | the face of future changes. */ | |
2214 | ||
2215 | bfd_boolean | |
268b6b39 AM |
2216 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2217 | struct elf_info_failed *eif) | |
45d6a902 | 2218 | { |
508c3946 L |
2219 | const struct elf_backend_data *bed = NULL; |
2220 | ||
45d6a902 AM |
2221 | /* If this symbol was mentioned in a non-ELF file, try to set |
2222 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2223 | permit a non-ELF file to correctly refer to a symbol defined in | |
2224 | an ELF dynamic object. */ | |
f5385ebf | 2225 | if (h->non_elf) |
45d6a902 AM |
2226 | { |
2227 | while (h->root.type == bfd_link_hash_indirect) | |
2228 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2229 | ||
2230 | if (h->root.type != bfd_link_hash_defined | |
2231 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2232 | { |
2233 | h->ref_regular = 1; | |
2234 | h->ref_regular_nonweak = 1; | |
2235 | } | |
45d6a902 AM |
2236 | else |
2237 | { | |
2238 | if (h->root.u.def.section->owner != NULL | |
2239 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2240 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2241 | { |
2242 | h->ref_regular = 1; | |
2243 | h->ref_regular_nonweak = 1; | |
2244 | } | |
45d6a902 | 2245 | else |
f5385ebf | 2246 | h->def_regular = 1; |
45d6a902 AM |
2247 | } |
2248 | ||
2249 | if (h->dynindx == -1 | |
f5385ebf AM |
2250 | && (h->def_dynamic |
2251 | || h->ref_dynamic)) | |
45d6a902 | 2252 | { |
c152c796 | 2253 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2254 | { |
2255 | eif->failed = TRUE; | |
2256 | return FALSE; | |
2257 | } | |
2258 | } | |
2259 | } | |
2260 | else | |
2261 | { | |
f5385ebf | 2262 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2263 | was first seen in a non-ELF file. Fortunately, if the symbol |
2264 | was first seen in an ELF file, we're probably OK unless the | |
2265 | symbol was defined in a non-ELF file. Catch that case here. | |
2266 | FIXME: We're still in trouble if the symbol was first seen in | |
2267 | a dynamic object, and then later in a non-ELF regular object. */ | |
2268 | if ((h->root.type == bfd_link_hash_defined | |
2269 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2270 | && !h->def_regular |
45d6a902 AM |
2271 | && (h->root.u.def.section->owner != NULL |
2272 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2273 | != bfd_target_elf_flavour) | |
2274 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2275 | && !h->def_dynamic))) |
2276 | h->def_regular = 1; | |
45d6a902 AM |
2277 | } |
2278 | ||
508c3946 L |
2279 | /* Backend specific symbol fixup. */ |
2280 | if (elf_hash_table (eif->info)->dynobj) | |
2281 | { | |
2282 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2283 | if (bed->elf_backend_fixup_symbol | |
2284 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2285 | return FALSE; | |
2286 | } | |
2287 | ||
45d6a902 AM |
2288 | /* If this is a final link, and the symbol was defined as a common |
2289 | symbol in a regular object file, and there was no definition in | |
2290 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2291 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2292 | flag will not have been set. */ |
2293 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2294 | && !h->def_regular |
2295 | && h->ref_regular | |
2296 | && !h->def_dynamic | |
45d6a902 | 2297 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) |
f5385ebf | 2298 | h->def_regular = 1; |
45d6a902 AM |
2299 | |
2300 | /* If -Bsymbolic was used (which means to bind references to global | |
2301 | symbols to the definition within the shared object), and this | |
2302 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2303 | need a PLT entry. Likewise, if the symbol has non-default |
2304 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2305 | will force it local. */ |
f5385ebf | 2306 | if (h->needs_plt |
45d6a902 | 2307 | && eif->info->shared |
0eddce27 | 2308 | && is_elf_hash_table (eif->info->hash) |
45d6a902 | 2309 | && (eif->info->symbolic |
c1be741f | 2310 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2311 | && h->def_regular) |
45d6a902 | 2312 | { |
45d6a902 AM |
2313 | bfd_boolean force_local; |
2314 | ||
45d6a902 AM |
2315 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2316 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2317 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2318 | } | |
2319 | ||
2320 | /* If a weak undefined symbol has non-default visibility, we also | |
2321 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2322 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
2323 | && h->root.type == bfd_link_hash_undefweak) |
2324 | { | |
9c5bfbb7 | 2325 | const struct elf_backend_data *bed; |
45d6a902 AM |
2326 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2327 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2328 | } | |
2329 | ||
2330 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2331 | the real definition in the dynamic object, copy interesting flags | |
2332 | over to the real definition. */ | |
f6e332e6 | 2333 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2334 | { |
2335 | struct elf_link_hash_entry *weakdef; | |
2336 | ||
f6e332e6 | 2337 | weakdef = h->u.weakdef; |
45d6a902 AM |
2338 | if (h->root.type == bfd_link_hash_indirect) |
2339 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2340 | ||
2341 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2342 | || h->root.type == bfd_link_hash_defweak); | |
2343 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2344 | || weakdef->root.type == bfd_link_hash_defweak); | |
f5385ebf | 2345 | BFD_ASSERT (weakdef->def_dynamic); |
45d6a902 AM |
2346 | |
2347 | /* If the real definition is defined by a regular object file, | |
2348 | don't do anything special. See the longer description in | |
2349 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
f5385ebf | 2350 | if (weakdef->def_regular) |
f6e332e6 | 2351 | h->u.weakdef = NULL; |
45d6a902 | 2352 | else |
508c3946 L |
2353 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, |
2354 | h); | |
45d6a902 AM |
2355 | } |
2356 | ||
2357 | return TRUE; | |
2358 | } | |
2359 | ||
2360 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2361 | called via elf_link_hash_traverse, and also calls itself | |
2362 | recursively. */ | |
2363 | ||
2364 | bfd_boolean | |
268b6b39 | 2365 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2366 | { |
268b6b39 | 2367 | struct elf_info_failed *eif = data; |
45d6a902 | 2368 | bfd *dynobj; |
9c5bfbb7 | 2369 | const struct elf_backend_data *bed; |
45d6a902 | 2370 | |
0eddce27 | 2371 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2372 | return FALSE; |
2373 | ||
2374 | if (h->root.type == bfd_link_hash_warning) | |
2375 | { | |
a6aa5195 AM |
2376 | h->got = elf_hash_table (eif->info)->init_got_offset; |
2377 | h->plt = elf_hash_table (eif->info)->init_plt_offset; | |
45d6a902 AM |
2378 | |
2379 | /* When warning symbols are created, they **replace** the "real" | |
2380 | entry in the hash table, thus we never get to see the real | |
2381 | symbol in a hash traversal. So look at it now. */ | |
2382 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2383 | } | |
2384 | ||
2385 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2386 | if (h->root.type == bfd_link_hash_indirect) | |
2387 | return TRUE; | |
2388 | ||
2389 | /* Fix the symbol flags. */ | |
2390 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2391 | return FALSE; | |
2392 | ||
2393 | /* If this symbol does not require a PLT entry, and it is not | |
2394 | defined by a dynamic object, or is not referenced by a regular | |
2395 | object, ignore it. We do have to handle a weak defined symbol, | |
2396 | even if no regular object refers to it, if we decided to add it | |
2397 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2398 | about symbols which are defined by one dynamic object and | |
2399 | referenced by another one? */ | |
f5385ebf AM |
2400 | if (!h->needs_plt |
2401 | && (h->def_regular | |
2402 | || !h->def_dynamic | |
2403 | || (!h->ref_regular | |
f6e332e6 | 2404 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 | 2405 | { |
a6aa5195 | 2406 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2407 | return TRUE; |
2408 | } | |
2409 | ||
2410 | /* If we've already adjusted this symbol, don't do it again. This | |
2411 | can happen via a recursive call. */ | |
f5385ebf | 2412 | if (h->dynamic_adjusted) |
45d6a902 AM |
2413 | return TRUE; |
2414 | ||
2415 | /* Don't look at this symbol again. Note that we must set this | |
2416 | after checking the above conditions, because we may look at a | |
2417 | symbol once, decide not to do anything, and then get called | |
2418 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2419 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2420 | |
2421 | /* If this is a weak definition, and we know a real definition, and | |
2422 | the real symbol is not itself defined by a regular object file, | |
2423 | then get a good value for the real definition. We handle the | |
2424 | real symbol first, for the convenience of the backend routine. | |
2425 | ||
2426 | Note that there is a confusing case here. If the real definition | |
2427 | is defined by a regular object file, we don't get the real symbol | |
2428 | from the dynamic object, but we do get the weak symbol. If the | |
2429 | processor backend uses a COPY reloc, then if some routine in the | |
2430 | dynamic object changes the real symbol, we will not see that | |
2431 | change in the corresponding weak symbol. This is the way other | |
2432 | ELF linkers work as well, and seems to be a result of the shared | |
2433 | library model. | |
2434 | ||
2435 | I will clarify this issue. Most SVR4 shared libraries define the | |
2436 | variable _timezone and define timezone as a weak synonym. The | |
2437 | tzset call changes _timezone. If you write | |
2438 | extern int timezone; | |
2439 | int _timezone = 5; | |
2440 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2441 | you might expect that, since timezone is a synonym for _timezone, | |
2442 | the same number will print both times. However, if the processor | |
2443 | backend uses a COPY reloc, then actually timezone will be copied | |
2444 | into your process image, and, since you define _timezone | |
2445 | yourself, _timezone will not. Thus timezone and _timezone will | |
2446 | wind up at different memory locations. The tzset call will set | |
2447 | _timezone, leaving timezone unchanged. */ | |
2448 | ||
f6e332e6 | 2449 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2450 | { |
2451 | /* If we get to this point, we know there is an implicit | |
2452 | reference by a regular object file via the weak symbol H. | |
2453 | FIXME: Is this really true? What if the traversal finds | |
f6e332e6 AM |
2454 | H->U.WEAKDEF before it finds H? */ |
2455 | h->u.weakdef->ref_regular = 1; | |
45d6a902 | 2456 | |
f6e332e6 | 2457 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2458 | return FALSE; |
2459 | } | |
2460 | ||
2461 | /* If a symbol has no type and no size and does not require a PLT | |
2462 | entry, then we are probably about to do the wrong thing here: we | |
2463 | are probably going to create a COPY reloc for an empty object. | |
2464 | This case can arise when a shared object is built with assembly | |
2465 | code, and the assembly code fails to set the symbol type. */ | |
2466 | if (h->size == 0 | |
2467 | && h->type == STT_NOTYPE | |
f5385ebf | 2468 | && !h->needs_plt) |
45d6a902 AM |
2469 | (*_bfd_error_handler) |
2470 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2471 | h->root.root.string); | |
2472 | ||
2473 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2474 | bed = get_elf_backend_data (dynobj); | |
2475 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2476 | { | |
2477 | eif->failed = TRUE; | |
2478 | return FALSE; | |
2479 | } | |
2480 | ||
2481 | return TRUE; | |
2482 | } | |
2483 | ||
2484 | /* Adjust all external symbols pointing into SEC_MERGE sections | |
2485 | to reflect the object merging within the sections. */ | |
2486 | ||
2487 | bfd_boolean | |
268b6b39 | 2488 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2489 | { |
2490 | asection *sec; | |
2491 | ||
2492 | if (h->root.type == bfd_link_hash_warning) | |
2493 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2494 | ||
2495 | if ((h->root.type == bfd_link_hash_defined | |
2496 | || h->root.type == bfd_link_hash_defweak) | |
2497 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2498 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2499 | { | |
268b6b39 | 2500 | bfd *output_bfd = data; |
45d6a902 AM |
2501 | |
2502 | h->root.u.def.value = | |
2503 | _bfd_merged_section_offset (output_bfd, | |
2504 | &h->root.u.def.section, | |
2505 | elf_section_data (sec)->sec_info, | |
753731ee | 2506 | h->root.u.def.value); |
45d6a902 AM |
2507 | } |
2508 | ||
2509 | return TRUE; | |
2510 | } | |
986a241f RH |
2511 | |
2512 | /* Returns false if the symbol referred to by H should be considered | |
2513 | to resolve local to the current module, and true if it should be | |
2514 | considered to bind dynamically. */ | |
2515 | ||
2516 | bfd_boolean | |
268b6b39 AM |
2517 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2518 | struct bfd_link_info *info, | |
2519 | bfd_boolean ignore_protected) | |
986a241f RH |
2520 | { |
2521 | bfd_boolean binding_stays_local_p; | |
2522 | ||
2523 | if (h == NULL) | |
2524 | return FALSE; | |
2525 | ||
2526 | while (h->root.type == bfd_link_hash_indirect | |
2527 | || h->root.type == bfd_link_hash_warning) | |
2528 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2529 | ||
2530 | /* If it was forced local, then clearly it's not dynamic. */ | |
2531 | if (h->dynindx == -1) | |
2532 | return FALSE; | |
f5385ebf | 2533 | if (h->forced_local) |
986a241f RH |
2534 | return FALSE; |
2535 | ||
2536 | /* Identify the cases where name binding rules say that a | |
2537 | visible symbol resolves locally. */ | |
2538 | binding_stays_local_p = info->executable || info->symbolic; | |
2539 | ||
2540 | switch (ELF_ST_VISIBILITY (h->other)) | |
2541 | { | |
2542 | case STV_INTERNAL: | |
2543 | case STV_HIDDEN: | |
2544 | return FALSE; | |
2545 | ||
2546 | case STV_PROTECTED: | |
2547 | /* Proper resolution for function pointer equality may require | |
2548 | that these symbols perhaps be resolved dynamically, even though | |
2549 | we should be resolving them to the current module. */ | |
1c16dfa5 | 2550 | if (!ignore_protected || h->type != STT_FUNC) |
986a241f RH |
2551 | binding_stays_local_p = TRUE; |
2552 | break; | |
2553 | ||
2554 | default: | |
986a241f RH |
2555 | break; |
2556 | } | |
2557 | ||
aa37626c | 2558 | /* If it isn't defined locally, then clearly it's dynamic. */ |
f5385ebf | 2559 | if (!h->def_regular) |
aa37626c L |
2560 | return TRUE; |
2561 | ||
986a241f RH |
2562 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2563 | us that it remains local. */ | |
2564 | return !binding_stays_local_p; | |
2565 | } | |
f6c52c13 AM |
2566 | |
2567 | /* Return true if the symbol referred to by H should be considered | |
2568 | to resolve local to the current module, and false otherwise. Differs | |
2569 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2570 | undefined symbols and weak symbols. */ | |
2571 | ||
2572 | bfd_boolean | |
268b6b39 AM |
2573 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2574 | struct bfd_link_info *info, | |
2575 | bfd_boolean local_protected) | |
f6c52c13 AM |
2576 | { |
2577 | /* If it's a local sym, of course we resolve locally. */ | |
2578 | if (h == NULL) | |
2579 | return TRUE; | |
2580 | ||
7e2294f9 AO |
2581 | /* Common symbols that become definitions don't get the DEF_REGULAR |
2582 | flag set, so test it first, and don't bail out. */ | |
2583 | if (ELF_COMMON_DEF_P (h)) | |
2584 | /* Do nothing. */; | |
f6c52c13 | 2585 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
2586 | resolve locally. The sym is either undefined or dynamic. */ |
2587 | else if (!h->def_regular) | |
f6c52c13 AM |
2588 | return FALSE; |
2589 | ||
2590 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 2591 | if (h->forced_local) |
f6c52c13 AM |
2592 | return TRUE; |
2593 | ||
2594 | /* As do non-dynamic symbols. */ | |
2595 | if (h->dynindx == -1) | |
2596 | return TRUE; | |
2597 | ||
2598 | /* At this point, we know the symbol is defined and dynamic. In an | |
2599 | executable it must resolve locally, likewise when building symbolic | |
2600 | shared libraries. */ | |
2601 | if (info->executable || info->symbolic) | |
2602 | return TRUE; | |
2603 | ||
2604 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2605 | with default visibility might not resolve locally. */ | |
2606 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2607 | return FALSE; | |
2608 | ||
2609 | /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */ | |
2610 | if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED) | |
2611 | return TRUE; | |
2612 | ||
1c16dfa5 L |
2613 | /* STV_PROTECTED non-function symbols are local. */ |
2614 | if (h->type != STT_FUNC) | |
2615 | return TRUE; | |
2616 | ||
f6c52c13 AM |
2617 | /* Function pointer equality tests may require that STV_PROTECTED |
2618 | symbols be treated as dynamic symbols, even when we know that the | |
2619 | dynamic linker will resolve them locally. */ | |
2620 | return local_protected; | |
2621 | } | |
e1918d23 AM |
2622 | |
2623 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2624 | aligned. Returns the first TLS output section. */ | |
2625 | ||
2626 | struct bfd_section * | |
2627 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2628 | { | |
2629 | struct bfd_section *sec, *tls; | |
2630 | unsigned int align = 0; | |
2631 | ||
2632 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2633 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2634 | break; | |
2635 | tls = sec; | |
2636 | ||
2637 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2638 | if (sec->alignment_power > align) | |
2639 | align = sec->alignment_power; | |
2640 | ||
2641 | elf_hash_table (info)->tls_sec = tls; | |
2642 | ||
2643 | /* Ensure the alignment of the first section is the largest alignment, | |
2644 | so that the tls segment starts aligned. */ | |
2645 | if (tls != NULL) | |
2646 | tls->alignment_power = align; | |
2647 | ||
2648 | return tls; | |
2649 | } | |
0ad989f9 L |
2650 | |
2651 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2652 | static bfd_boolean | |
2653 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2654 | Elf_Internal_Sym *sym) | |
2655 | { | |
a4d8e49b L |
2656 | const struct elf_backend_data *bed; |
2657 | ||
0ad989f9 L |
2658 | /* Local symbols do not count, but target specific ones might. */ |
2659 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2660 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2661 | return FALSE; | |
2662 | ||
2663 | /* Function symbols do not count. */ | |
2664 | if (ELF_ST_TYPE (sym->st_info) == STT_FUNC) | |
2665 | return FALSE; | |
2666 | ||
2667 | /* If the section is undefined, then so is the symbol. */ | |
2668 | if (sym->st_shndx == SHN_UNDEF) | |
2669 | return FALSE; | |
2670 | ||
2671 | /* If the symbol is defined in the common section, then | |
2672 | it is a common definition and so does not count. */ | |
a4d8e49b L |
2673 | bed = get_elf_backend_data (abfd); |
2674 | if (bed->common_definition (sym)) | |
0ad989f9 L |
2675 | return FALSE; |
2676 | ||
2677 | /* If the symbol is in a target specific section then we | |
2678 | must rely upon the backend to tell us what it is. */ | |
2679 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2680 | /* FIXME - this function is not coded yet: | |
2681 | ||
2682 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2683 | ||
2684 | Instead for now assume that the definition is not global, | |
2685 | Even if this is wrong, at least the linker will behave | |
2686 | in the same way that it used to do. */ | |
2687 | return FALSE; | |
2688 | ||
2689 | return TRUE; | |
2690 | } | |
2691 | ||
2692 | /* Search the symbol table of the archive element of the archive ABFD | |
2693 | whose archive map contains a mention of SYMDEF, and determine if | |
2694 | the symbol is defined in this element. */ | |
2695 | static bfd_boolean | |
2696 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2697 | { | |
2698 | Elf_Internal_Shdr * hdr; | |
2699 | bfd_size_type symcount; | |
2700 | bfd_size_type extsymcount; | |
2701 | bfd_size_type extsymoff; | |
2702 | Elf_Internal_Sym *isymbuf; | |
2703 | Elf_Internal_Sym *isym; | |
2704 | Elf_Internal_Sym *isymend; | |
2705 | bfd_boolean result; | |
2706 | ||
2707 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2708 | if (abfd == NULL) | |
2709 | return FALSE; | |
2710 | ||
2711 | if (! bfd_check_format (abfd, bfd_object)) | |
2712 | return FALSE; | |
2713 | ||
2714 | /* If we have already included the element containing this symbol in the | |
2715 | link then we do not need to include it again. Just claim that any symbol | |
2716 | it contains is not a definition, so that our caller will not decide to | |
2717 | (re)include this element. */ | |
2718 | if (abfd->archive_pass) | |
2719 | return FALSE; | |
2720 | ||
2721 | /* Select the appropriate symbol table. */ | |
2722 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2723 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2724 | else | |
2725 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2726 | ||
2727 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2728 | ||
2729 | /* The sh_info field of the symtab header tells us where the | |
2730 | external symbols start. We don't care about the local symbols. */ | |
2731 | if (elf_bad_symtab (abfd)) | |
2732 | { | |
2733 | extsymcount = symcount; | |
2734 | extsymoff = 0; | |
2735 | } | |
2736 | else | |
2737 | { | |
2738 | extsymcount = symcount - hdr->sh_info; | |
2739 | extsymoff = hdr->sh_info; | |
2740 | } | |
2741 | ||
2742 | if (extsymcount == 0) | |
2743 | return FALSE; | |
2744 | ||
2745 | /* Read in the symbol table. */ | |
2746 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2747 | NULL, NULL, NULL); | |
2748 | if (isymbuf == NULL) | |
2749 | return FALSE; | |
2750 | ||
2751 | /* Scan the symbol table looking for SYMDEF. */ | |
2752 | result = FALSE; | |
2753 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2754 | { | |
2755 | const char *name; | |
2756 | ||
2757 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
2758 | isym->st_name); | |
2759 | if (name == NULL) | |
2760 | break; | |
2761 | ||
2762 | if (strcmp (name, symdef->name) == 0) | |
2763 | { | |
2764 | result = is_global_data_symbol_definition (abfd, isym); | |
2765 | break; | |
2766 | } | |
2767 | } | |
2768 | ||
2769 | free (isymbuf); | |
2770 | ||
2771 | return result; | |
2772 | } | |
2773 | \f | |
5a580b3a AM |
2774 | /* Add an entry to the .dynamic table. */ |
2775 | ||
2776 | bfd_boolean | |
2777 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
2778 | bfd_vma tag, | |
2779 | bfd_vma val) | |
2780 | { | |
2781 | struct elf_link_hash_table *hash_table; | |
2782 | const struct elf_backend_data *bed; | |
2783 | asection *s; | |
2784 | bfd_size_type newsize; | |
2785 | bfd_byte *newcontents; | |
2786 | Elf_Internal_Dyn dyn; | |
2787 | ||
2788 | hash_table = elf_hash_table (info); | |
2789 | if (! is_elf_hash_table (hash_table)) | |
2790 | return FALSE; | |
2791 | ||
8fdd7217 NC |
2792 | if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL) |
2793 | _bfd_error_handler | |
2794 | (_("warning: creating a DT_TEXTREL in a shared object.")); | |
2795 | ||
5a580b3a AM |
2796 | bed = get_elf_backend_data (hash_table->dynobj); |
2797 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2798 | BFD_ASSERT (s != NULL); | |
2799 | ||
eea6121a | 2800 | newsize = s->size + bed->s->sizeof_dyn; |
5a580b3a AM |
2801 | newcontents = bfd_realloc (s->contents, newsize); |
2802 | if (newcontents == NULL) | |
2803 | return FALSE; | |
2804 | ||
2805 | dyn.d_tag = tag; | |
2806 | dyn.d_un.d_val = val; | |
eea6121a | 2807 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 2808 | |
eea6121a | 2809 | s->size = newsize; |
5a580b3a AM |
2810 | s->contents = newcontents; |
2811 | ||
2812 | return TRUE; | |
2813 | } | |
2814 | ||
2815 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
2816 | otherwise just check whether one already exists. Returns -1 on error, | |
2817 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
2818 | ||
4ad4eba5 | 2819 | static int |
7e9f0867 AM |
2820 | elf_add_dt_needed_tag (bfd *abfd, |
2821 | struct bfd_link_info *info, | |
4ad4eba5 AM |
2822 | const char *soname, |
2823 | bfd_boolean do_it) | |
5a580b3a AM |
2824 | { |
2825 | struct elf_link_hash_table *hash_table; | |
2826 | bfd_size_type oldsize; | |
2827 | bfd_size_type strindex; | |
2828 | ||
7e9f0867 AM |
2829 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
2830 | return -1; | |
2831 | ||
5a580b3a AM |
2832 | hash_table = elf_hash_table (info); |
2833 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
2834 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
2835 | if (strindex == (bfd_size_type) -1) | |
2836 | return -1; | |
2837 | ||
2838 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
2839 | { | |
2840 | asection *sdyn; | |
2841 | const struct elf_backend_data *bed; | |
2842 | bfd_byte *extdyn; | |
2843 | ||
2844 | bed = get_elf_backend_data (hash_table->dynobj); | |
2845 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
7e9f0867 AM |
2846 | if (sdyn != NULL) |
2847 | for (extdyn = sdyn->contents; | |
2848 | extdyn < sdyn->contents + sdyn->size; | |
2849 | extdyn += bed->s->sizeof_dyn) | |
2850 | { | |
2851 | Elf_Internal_Dyn dyn; | |
5a580b3a | 2852 | |
7e9f0867 AM |
2853 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
2854 | if (dyn.d_tag == DT_NEEDED | |
2855 | && dyn.d_un.d_val == strindex) | |
2856 | { | |
2857 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2858 | return 1; | |
2859 | } | |
2860 | } | |
5a580b3a AM |
2861 | } |
2862 | ||
2863 | if (do_it) | |
2864 | { | |
7e9f0867 AM |
2865 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
2866 | return -1; | |
2867 | ||
5a580b3a AM |
2868 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
2869 | return -1; | |
2870 | } | |
2871 | else | |
2872 | /* We were just checking for existence of the tag. */ | |
2873 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2874 | ||
2875 | return 0; | |
2876 | } | |
2877 | ||
77cfaee6 AM |
2878 | /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols |
2879 | belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no | |
ec13b3bb AM |
2880 | references from regular objects to these symbols. |
2881 | ||
2882 | ??? Should we do something about references from other dynamic | |
2883 | obects? If not, we potentially lose some warnings about undefined | |
2884 | symbols. But how can we recover the initial undefined / undefweak | |
2885 | state? */ | |
77cfaee6 AM |
2886 | |
2887 | struct elf_smash_syms_data | |
2888 | { | |
2889 | bfd *not_needed; | |
2890 | struct elf_link_hash_table *htab; | |
2891 | bfd_boolean twiddled; | |
2892 | }; | |
2893 | ||
2894 | static bfd_boolean | |
2895 | elf_smash_syms (struct elf_link_hash_entry *h, void *data) | |
2896 | { | |
2897 | struct elf_smash_syms_data *inf = (struct elf_smash_syms_data *) data; | |
2898 | struct bfd_link_hash_entry *bh; | |
2899 | ||
2900 | switch (h->root.type) | |
2901 | { | |
2902 | default: | |
2903 | case bfd_link_hash_new: | |
2904 | return TRUE; | |
2905 | ||
2906 | case bfd_link_hash_undefined: | |
11f25ea6 AM |
2907 | if (h->root.u.undef.abfd != inf->not_needed) |
2908 | return TRUE; | |
4ea42fb7 AM |
2909 | if (h->root.u.undef.weak != NULL |
2910 | && h->root.u.undef.weak != inf->not_needed) | |
11f25ea6 AM |
2911 | { |
2912 | /* Symbol was undefweak in u.undef.weak bfd, and has become | |
2913 | undefined in as-needed lib. Restore weak. */ | |
2914 | h->root.type = bfd_link_hash_undefweak; | |
2915 | h->root.u.undef.abfd = h->root.u.undef.weak; | |
2916 | if (h->root.u.undef.next != NULL | |
2917 | || inf->htab->root.undefs_tail == &h->root) | |
2918 | inf->twiddled = TRUE; | |
2919 | return TRUE; | |
2920 | } | |
2921 | break; | |
2922 | ||
77cfaee6 AM |
2923 | case bfd_link_hash_undefweak: |
2924 | if (h->root.u.undef.abfd != inf->not_needed) | |
2925 | return TRUE; | |
2926 | break; | |
2927 | ||
2928 | case bfd_link_hash_defined: | |
2929 | case bfd_link_hash_defweak: | |
2930 | if (h->root.u.def.section->owner != inf->not_needed) | |
2931 | return TRUE; | |
2932 | break; | |
2933 | ||
2934 | case bfd_link_hash_common: | |
2935 | if (h->root.u.c.p->section->owner != inf->not_needed) | |
2936 | return TRUE; | |
2937 | break; | |
2938 | ||
2939 | case bfd_link_hash_warning: | |
2940 | case bfd_link_hash_indirect: | |
2941 | elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data); | |
2942 | if (h->root.u.i.link->type != bfd_link_hash_new) | |
2943 | return TRUE; | |
2944 | if (h->root.u.i.link->u.undef.abfd != inf->not_needed) | |
2945 | return TRUE; | |
2946 | break; | |
2947 | } | |
2948 | ||
11f25ea6 AM |
2949 | /* There is no way we can undo symbol table state from defined or |
2950 | defweak back to undefined. */ | |
2951 | if (h->ref_regular) | |
2952 | abort (); | |
2953 | ||
2e8b3a61 AM |
2954 | /* Set sym back to newly created state, but keep undef.next if it is |
2955 | being used as a list pointer. */ | |
77cfaee6 | 2956 | bh = h->root.u.undef.next; |
2e8b3a61 AM |
2957 | if (bh == &h->root) |
2958 | bh = NULL; | |
77cfaee6 AM |
2959 | if (bh != NULL || inf->htab->root.undefs_tail == &h->root) |
2960 | inf->twiddled = TRUE; | |
2961 | (*inf->htab->root.table.newfunc) (&h->root.root, | |
2962 | &inf->htab->root.table, | |
2963 | h->root.root.string); | |
2964 | h->root.u.undef.next = bh; | |
2965 | h->root.u.undef.abfd = inf->not_needed; | |
2966 | h->non_elf = 0; | |
2967 | return TRUE; | |
2968 | } | |
2969 | ||
5a580b3a | 2970 | /* Sort symbol by value and section. */ |
4ad4eba5 AM |
2971 | static int |
2972 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
2973 | { |
2974 | const struct elf_link_hash_entry *h1; | |
2975 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 2976 | bfd_signed_vma vdiff; |
5a580b3a AM |
2977 | |
2978 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
2979 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
2980 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
2981 | if (vdiff != 0) | |
2982 | return vdiff > 0 ? 1 : -1; | |
2983 | else | |
2984 | { | |
2985 | long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
2986 | if (sdiff != 0) | |
2987 | return sdiff > 0 ? 1 : -1; | |
2988 | } | |
5a580b3a AM |
2989 | return 0; |
2990 | } | |
4ad4eba5 | 2991 | |
5a580b3a AM |
2992 | /* This function is used to adjust offsets into .dynstr for |
2993 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
2994 | ||
2995 | static bfd_boolean | |
2996 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
2997 | { | |
2998 | struct elf_strtab_hash *dynstr = data; | |
2999 | ||
3000 | if (h->root.type == bfd_link_hash_warning) | |
3001 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3002 | ||
3003 | if (h->dynindx != -1) | |
3004 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3005 | return TRUE; | |
3006 | } | |
3007 | ||
3008 | /* Assign string offsets in .dynstr, update all structures referencing | |
3009 | them. */ | |
3010 | ||
4ad4eba5 AM |
3011 | static bfd_boolean |
3012 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3013 | { |
3014 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3015 | struct elf_link_local_dynamic_entry *entry; | |
3016 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3017 | bfd *dynobj = hash_table->dynobj; | |
3018 | asection *sdyn; | |
3019 | bfd_size_type size; | |
3020 | const struct elf_backend_data *bed; | |
3021 | bfd_byte *extdyn; | |
3022 | ||
3023 | _bfd_elf_strtab_finalize (dynstr); | |
3024 | size = _bfd_elf_strtab_size (dynstr); | |
3025 | ||
3026 | bed = get_elf_backend_data (dynobj); | |
3027 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
3028 | BFD_ASSERT (sdyn != NULL); | |
3029 | ||
3030 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3031 | for (extdyn = sdyn->contents; | |
eea6121a | 3032 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3033 | extdyn += bed->s->sizeof_dyn) |
3034 | { | |
3035 | Elf_Internal_Dyn dyn; | |
3036 | ||
3037 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3038 | switch (dyn.d_tag) | |
3039 | { | |
3040 | case DT_STRSZ: | |
3041 | dyn.d_un.d_val = size; | |
3042 | break; | |
3043 | case DT_NEEDED: | |
3044 | case DT_SONAME: | |
3045 | case DT_RPATH: | |
3046 | case DT_RUNPATH: | |
3047 | case DT_FILTER: | |
3048 | case DT_AUXILIARY: | |
3049 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
3050 | break; | |
3051 | default: | |
3052 | continue; | |
3053 | } | |
3054 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3055 | } | |
3056 | ||
3057 | /* Now update local dynamic symbols. */ | |
3058 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3059 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3060 | entry->isym.st_name); | |
3061 | ||
3062 | /* And the rest of dynamic symbols. */ | |
3063 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3064 | ||
3065 | /* Adjust version definitions. */ | |
3066 | if (elf_tdata (output_bfd)->cverdefs) | |
3067 | { | |
3068 | asection *s; | |
3069 | bfd_byte *p; | |
3070 | bfd_size_type i; | |
3071 | Elf_Internal_Verdef def; | |
3072 | Elf_Internal_Verdaux defaux; | |
3073 | ||
3074 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
3075 | p = s->contents; | |
3076 | do | |
3077 | { | |
3078 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3079 | &def); | |
3080 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3081 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3082 | continue; | |
5a580b3a AM |
3083 | for (i = 0; i < def.vd_cnt; ++i) |
3084 | { | |
3085 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3086 | (Elf_External_Verdaux *) p, &defaux); | |
3087 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3088 | defaux.vda_name); | |
3089 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3090 | &defaux, (Elf_External_Verdaux *) p); | |
3091 | p += sizeof (Elf_External_Verdaux); | |
3092 | } | |
3093 | } | |
3094 | while (def.vd_next); | |
3095 | } | |
3096 | ||
3097 | /* Adjust version references. */ | |
3098 | if (elf_tdata (output_bfd)->verref) | |
3099 | { | |
3100 | asection *s; | |
3101 | bfd_byte *p; | |
3102 | bfd_size_type i; | |
3103 | Elf_Internal_Verneed need; | |
3104 | Elf_Internal_Vernaux needaux; | |
3105 | ||
3106 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
3107 | p = s->contents; | |
3108 | do | |
3109 | { | |
3110 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3111 | &need); | |
3112 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3113 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3114 | (Elf_External_Verneed *) p); | |
3115 | p += sizeof (Elf_External_Verneed); | |
3116 | for (i = 0; i < need.vn_cnt; ++i) | |
3117 | { | |
3118 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3119 | (Elf_External_Vernaux *) p, &needaux); | |
3120 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3121 | needaux.vna_name); | |
3122 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3123 | &needaux, | |
3124 | (Elf_External_Vernaux *) p); | |
3125 | p += sizeof (Elf_External_Vernaux); | |
3126 | } | |
3127 | } | |
3128 | while (need.vn_next); | |
3129 | } | |
3130 | ||
3131 | return TRUE; | |
3132 | } | |
3133 | \f | |
4ad4eba5 AM |
3134 | /* Add symbols from an ELF object file to the linker hash table. */ |
3135 | ||
3136 | static bfd_boolean | |
3137 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3138 | { | |
3139 | bfd_boolean (*add_symbol_hook) | |
555cd476 | 3140 | (bfd *, struct bfd_link_info *, Elf_Internal_Sym *, |
4ad4eba5 AM |
3141 | const char **, flagword *, asection **, bfd_vma *); |
3142 | bfd_boolean (*check_relocs) | |
3143 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
85fbca6a NC |
3144 | bfd_boolean (*check_directives) |
3145 | (bfd *, struct bfd_link_info *); | |
4ad4eba5 AM |
3146 | bfd_boolean collect; |
3147 | Elf_Internal_Shdr *hdr; | |
3148 | bfd_size_type symcount; | |
3149 | bfd_size_type extsymcount; | |
3150 | bfd_size_type extsymoff; | |
3151 | struct elf_link_hash_entry **sym_hash; | |
3152 | bfd_boolean dynamic; | |
3153 | Elf_External_Versym *extversym = NULL; | |
3154 | Elf_External_Versym *ever; | |
3155 | struct elf_link_hash_entry *weaks; | |
3156 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
3157 | bfd_size_type nondeflt_vers_cnt = 0; | |
3158 | Elf_Internal_Sym *isymbuf = NULL; | |
3159 | Elf_Internal_Sym *isym; | |
3160 | Elf_Internal_Sym *isymend; | |
3161 | const struct elf_backend_data *bed; | |
3162 | bfd_boolean add_needed; | |
3163 | struct elf_link_hash_table * hash_table; | |
3164 | bfd_size_type amt; | |
3165 | ||
3166 | hash_table = elf_hash_table (info); | |
3167 | ||
3168 | bed = get_elf_backend_data (abfd); | |
3169 | add_symbol_hook = bed->elf_add_symbol_hook; | |
3170 | collect = bed->collect; | |
3171 | ||
3172 | if ((abfd->flags & DYNAMIC) == 0) | |
3173 | dynamic = FALSE; | |
3174 | else | |
3175 | { | |
3176 | dynamic = TRUE; | |
3177 | ||
3178 | /* You can't use -r against a dynamic object. Also, there's no | |
3179 | hope of using a dynamic object which does not exactly match | |
3180 | the format of the output file. */ | |
3181 | if (info->relocatable | |
3182 | || !is_elf_hash_table (hash_table) | |
3183 | || hash_table->root.creator != abfd->xvec) | |
3184 | { | |
9a0789ec NC |
3185 | if (info->relocatable) |
3186 | bfd_set_error (bfd_error_invalid_operation); | |
3187 | else | |
3188 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3189 | goto error_return; |
3190 | } | |
3191 | } | |
3192 | ||
3193 | /* As a GNU extension, any input sections which are named | |
3194 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3195 | symbol. This differs from .gnu.warning sections, which generate | |
3196 | warnings when they are included in an output file. */ | |
3197 | if (info->executable) | |
3198 | { | |
3199 | asection *s; | |
3200 | ||
3201 | for (s = abfd->sections; s != NULL; s = s->next) | |
3202 | { | |
3203 | const char *name; | |
3204 | ||
3205 | name = bfd_get_section_name (abfd, s); | |
3206 | if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0) | |
3207 | { | |
3208 | char *msg; | |
3209 | bfd_size_type sz; | |
4ad4eba5 AM |
3210 | |
3211 | name += sizeof ".gnu.warning." - 1; | |
3212 | ||
3213 | /* If this is a shared object, then look up the symbol | |
3214 | in the hash table. If it is there, and it is already | |
3215 | been defined, then we will not be using the entry | |
3216 | from this shared object, so we don't need to warn. | |
3217 | FIXME: If we see the definition in a regular object | |
3218 | later on, we will warn, but we shouldn't. The only | |
3219 | fix is to keep track of what warnings we are supposed | |
3220 | to emit, and then handle them all at the end of the | |
3221 | link. */ | |
3222 | if (dynamic) | |
3223 | { | |
3224 | struct elf_link_hash_entry *h; | |
3225 | ||
3226 | h = elf_link_hash_lookup (hash_table, name, | |
3227 | FALSE, FALSE, TRUE); | |
3228 | ||
3229 | /* FIXME: What about bfd_link_hash_common? */ | |
3230 | if (h != NULL | |
3231 | && (h->root.type == bfd_link_hash_defined | |
3232 | || h->root.type == bfd_link_hash_defweak)) | |
3233 | { | |
3234 | /* We don't want to issue this warning. Clobber | |
3235 | the section size so that the warning does not | |
3236 | get copied into the output file. */ | |
eea6121a | 3237 | s->size = 0; |
4ad4eba5 AM |
3238 | continue; |
3239 | } | |
3240 | } | |
3241 | ||
eea6121a | 3242 | sz = s->size; |
370a0e1b | 3243 | msg = bfd_alloc (abfd, sz + 1); |
4ad4eba5 AM |
3244 | if (msg == NULL) |
3245 | goto error_return; | |
3246 | ||
370a0e1b | 3247 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
4ad4eba5 AM |
3248 | goto error_return; |
3249 | ||
370a0e1b | 3250 | msg[sz] = '\0'; |
4ad4eba5 AM |
3251 | |
3252 | if (! (_bfd_generic_link_add_one_symbol | |
3253 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3254 | FALSE, collect, NULL))) | |
3255 | goto error_return; | |
3256 | ||
3257 | if (! info->relocatable) | |
3258 | { | |
3259 | /* Clobber the section size so that the warning does | |
3260 | not get copied into the output file. */ | |
eea6121a | 3261 | s->size = 0; |
11d2f718 AM |
3262 | |
3263 | /* Also set SEC_EXCLUDE, so that symbols defined in | |
3264 | the warning section don't get copied to the output. */ | |
3265 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3266 | } |
3267 | } | |
3268 | } | |
3269 | } | |
3270 | ||
3271 | add_needed = TRUE; | |
3272 | if (! dynamic) | |
3273 | { | |
3274 | /* If we are creating a shared library, create all the dynamic | |
3275 | sections immediately. We need to attach them to something, | |
3276 | so we attach them to this BFD, provided it is the right | |
3277 | format. FIXME: If there are no input BFD's of the same | |
3278 | format as the output, we can't make a shared library. */ | |
3279 | if (info->shared | |
3280 | && is_elf_hash_table (hash_table) | |
3281 | && hash_table->root.creator == abfd->xvec | |
3282 | && ! hash_table->dynamic_sections_created) | |
3283 | { | |
3284 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3285 | goto error_return; | |
3286 | } | |
3287 | } | |
3288 | else if (!is_elf_hash_table (hash_table)) | |
3289 | goto error_return; | |
3290 | else | |
3291 | { | |
3292 | asection *s; | |
3293 | const char *soname = NULL; | |
3294 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; | |
3295 | int ret; | |
3296 | ||
3297 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3298 | ld shouldn't allow it. */ |
4ad4eba5 AM |
3299 | if ((s = abfd->sections) != NULL |
3300 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
92fd189d | 3301 | abort (); |
4ad4eba5 AM |
3302 | |
3303 | /* If this dynamic lib was specified on the command line with | |
3304 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3305 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3306 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3307 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3308 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3309 | all. */ | |
3310 | add_needed = (elf_dyn_lib_class (abfd) | |
3311 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3312 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3313 | |
3314 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3315 | if (s != NULL) | |
3316 | { | |
3317 | bfd_byte *dynbuf; | |
3318 | bfd_byte *extdyn; | |
3319 | int elfsec; | |
3320 | unsigned long shlink; | |
3321 | ||
eea6121a | 3322 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
4ad4eba5 AM |
3323 | goto error_free_dyn; |
3324 | ||
3325 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
3326 | if (elfsec == -1) | |
3327 | goto error_free_dyn; | |
3328 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3329 | ||
3330 | for (extdyn = dynbuf; | |
eea6121a | 3331 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3332 | extdyn += bed->s->sizeof_dyn) |
3333 | { | |
3334 | Elf_Internal_Dyn dyn; | |
3335 | ||
3336 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3337 | if (dyn.d_tag == DT_SONAME) | |
3338 | { | |
3339 | unsigned int tagv = dyn.d_un.d_val; | |
3340 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3341 | if (soname == NULL) | |
3342 | goto error_free_dyn; | |
3343 | } | |
3344 | if (dyn.d_tag == DT_NEEDED) | |
3345 | { | |
3346 | struct bfd_link_needed_list *n, **pn; | |
3347 | char *fnm, *anm; | |
3348 | unsigned int tagv = dyn.d_un.d_val; | |
3349 | ||
3350 | amt = sizeof (struct bfd_link_needed_list); | |
3351 | n = bfd_alloc (abfd, amt); | |
3352 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3353 | if (n == NULL || fnm == NULL) | |
3354 | goto error_free_dyn; | |
3355 | amt = strlen (fnm) + 1; | |
3356 | anm = bfd_alloc (abfd, amt); | |
3357 | if (anm == NULL) | |
3358 | goto error_free_dyn; | |
3359 | memcpy (anm, fnm, amt); | |
3360 | n->name = anm; | |
3361 | n->by = abfd; | |
3362 | n->next = NULL; | |
3363 | for (pn = & hash_table->needed; | |
3364 | *pn != NULL; | |
3365 | pn = &(*pn)->next) | |
3366 | ; | |
3367 | *pn = n; | |
3368 | } | |
3369 | if (dyn.d_tag == DT_RUNPATH) | |
3370 | { | |
3371 | struct bfd_link_needed_list *n, **pn; | |
3372 | char *fnm, *anm; | |
3373 | unsigned int tagv = dyn.d_un.d_val; | |
3374 | ||
3375 | amt = sizeof (struct bfd_link_needed_list); | |
3376 | n = bfd_alloc (abfd, amt); | |
3377 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3378 | if (n == NULL || fnm == NULL) | |
3379 | goto error_free_dyn; | |
3380 | amt = strlen (fnm) + 1; | |
3381 | anm = bfd_alloc (abfd, amt); | |
3382 | if (anm == NULL) | |
3383 | goto error_free_dyn; | |
3384 | memcpy (anm, fnm, amt); | |
3385 | n->name = anm; | |
3386 | n->by = abfd; | |
3387 | n->next = NULL; | |
3388 | for (pn = & runpath; | |
3389 | *pn != NULL; | |
3390 | pn = &(*pn)->next) | |
3391 | ; | |
3392 | *pn = n; | |
3393 | } | |
3394 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3395 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3396 | { | |
3397 | struct bfd_link_needed_list *n, **pn; | |
3398 | char *fnm, *anm; | |
3399 | unsigned int tagv = dyn.d_un.d_val; | |
3400 | ||
3401 | amt = sizeof (struct bfd_link_needed_list); | |
3402 | n = bfd_alloc (abfd, amt); | |
3403 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3404 | if (n == NULL || fnm == NULL) | |
3405 | goto error_free_dyn; | |
3406 | amt = strlen (fnm) + 1; | |
3407 | anm = bfd_alloc (abfd, amt); | |
3408 | if (anm == NULL) | |
3409 | { | |
3410 | error_free_dyn: | |
3411 | free (dynbuf); | |
3412 | goto error_return; | |
3413 | } | |
3414 | memcpy (anm, fnm, amt); | |
3415 | n->name = anm; | |
3416 | n->by = abfd; | |
3417 | n->next = NULL; | |
3418 | for (pn = & rpath; | |
3419 | *pn != NULL; | |
3420 | pn = &(*pn)->next) | |
3421 | ; | |
3422 | *pn = n; | |
3423 | } | |
3424 | } | |
3425 | ||
3426 | free (dynbuf); | |
3427 | } | |
3428 | ||
3429 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3430 | frees all more recently bfd_alloc'd blocks as well. */ | |
3431 | if (runpath) | |
3432 | rpath = runpath; | |
3433 | ||
3434 | if (rpath) | |
3435 | { | |
3436 | struct bfd_link_needed_list **pn; | |
3437 | for (pn = & hash_table->runpath; | |
3438 | *pn != NULL; | |
3439 | pn = &(*pn)->next) | |
3440 | ; | |
3441 | *pn = rpath; | |
3442 | } | |
3443 | ||
3444 | /* We do not want to include any of the sections in a dynamic | |
3445 | object in the output file. We hack by simply clobbering the | |
3446 | list of sections in the BFD. This could be handled more | |
3447 | cleanly by, say, a new section flag; the existing | |
3448 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3449 | still implies that the section takes up space in the output | |
3450 | file. */ | |
3451 | bfd_section_list_clear (abfd); | |
3452 | ||
4ad4eba5 AM |
3453 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3454 | object. If the object has a DT_SONAME entry, we use it. | |
3455 | Otherwise, if the generic linker stuck something in | |
3456 | elf_dt_name, we use that. Otherwise, we just use the file | |
3457 | name. */ | |
3458 | if (soname == NULL || *soname == '\0') | |
3459 | { | |
3460 | soname = elf_dt_name (abfd); | |
3461 | if (soname == NULL || *soname == '\0') | |
3462 | soname = bfd_get_filename (abfd); | |
3463 | } | |
3464 | ||
3465 | /* Save the SONAME because sometimes the linker emulation code | |
3466 | will need to know it. */ | |
3467 | elf_dt_name (abfd) = soname; | |
3468 | ||
7e9f0867 | 3469 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3470 | if (ret < 0) |
3471 | goto error_return; | |
3472 | ||
3473 | /* If we have already included this dynamic object in the | |
3474 | link, just ignore it. There is no reason to include a | |
3475 | particular dynamic object more than once. */ | |
3476 | if (ret > 0) | |
3477 | return TRUE; | |
3478 | } | |
3479 | ||
3480 | /* If this is a dynamic object, we always link against the .dynsym | |
3481 | symbol table, not the .symtab symbol table. The dynamic linker | |
3482 | will only see the .dynsym symbol table, so there is no reason to | |
3483 | look at .symtab for a dynamic object. */ | |
3484 | ||
3485 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3486 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3487 | else | |
3488 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3489 | ||
3490 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3491 | ||
3492 | /* The sh_info field of the symtab header tells us where the | |
3493 | external symbols start. We don't care about the local symbols at | |
3494 | this point. */ | |
3495 | if (elf_bad_symtab (abfd)) | |
3496 | { | |
3497 | extsymcount = symcount; | |
3498 | extsymoff = 0; | |
3499 | } | |
3500 | else | |
3501 | { | |
3502 | extsymcount = symcount - hdr->sh_info; | |
3503 | extsymoff = hdr->sh_info; | |
3504 | } | |
3505 | ||
3506 | sym_hash = NULL; | |
3507 | if (extsymcount != 0) | |
3508 | { | |
3509 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3510 | NULL, NULL, NULL); | |
3511 | if (isymbuf == NULL) | |
3512 | goto error_return; | |
3513 | ||
3514 | /* We store a pointer to the hash table entry for each external | |
3515 | symbol. */ | |
3516 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3517 | sym_hash = bfd_alloc (abfd, amt); | |
3518 | if (sym_hash == NULL) | |
3519 | goto error_free_sym; | |
3520 | elf_sym_hashes (abfd) = sym_hash; | |
3521 | } | |
3522 | ||
3523 | if (dynamic) | |
3524 | { | |
3525 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
3526 | if (!_bfd_elf_slurp_version_tables (abfd, |
3527 | info->default_imported_symver)) | |
4ad4eba5 AM |
3528 | goto error_free_sym; |
3529 | ||
3530 | /* Read in the symbol versions, but don't bother to convert them | |
3531 | to internal format. */ | |
3532 | if (elf_dynversym (abfd) != 0) | |
3533 | { | |
3534 | Elf_Internal_Shdr *versymhdr; | |
3535 | ||
3536 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
3537 | extversym = bfd_malloc (versymhdr->sh_size); | |
3538 | if (extversym == NULL) | |
3539 | goto error_free_sym; | |
3540 | amt = versymhdr->sh_size; | |
3541 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3542 | || bfd_bread (extversym, amt, abfd) != amt) | |
3543 | goto error_free_vers; | |
3544 | } | |
3545 | } | |
3546 | ||
3547 | weaks = NULL; | |
3548 | ||
3549 | ever = extversym != NULL ? extversym + extsymoff : NULL; | |
3550 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3551 | isym < isymend; | |
3552 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3553 | { | |
3554 | int bind; | |
3555 | bfd_vma value; | |
af44c138 | 3556 | asection *sec, *new_sec; |
4ad4eba5 AM |
3557 | flagword flags; |
3558 | const char *name; | |
3559 | struct elf_link_hash_entry *h; | |
3560 | bfd_boolean definition; | |
3561 | bfd_boolean size_change_ok; | |
3562 | bfd_boolean type_change_ok; | |
3563 | bfd_boolean new_weakdef; | |
3564 | bfd_boolean override; | |
a4d8e49b | 3565 | bfd_boolean common; |
4ad4eba5 AM |
3566 | unsigned int old_alignment; |
3567 | bfd *old_bfd; | |
3568 | ||
3569 | override = FALSE; | |
3570 | ||
3571 | flags = BSF_NO_FLAGS; | |
3572 | sec = NULL; | |
3573 | value = isym->st_value; | |
3574 | *sym_hash = NULL; | |
a4d8e49b | 3575 | common = bed->common_definition (isym); |
4ad4eba5 AM |
3576 | |
3577 | bind = ELF_ST_BIND (isym->st_info); | |
3578 | if (bind == STB_LOCAL) | |
3579 | { | |
3580 | /* This should be impossible, since ELF requires that all | |
3581 | global symbols follow all local symbols, and that sh_info | |
3582 | point to the first global symbol. Unfortunately, Irix 5 | |
3583 | screws this up. */ | |
3584 | continue; | |
3585 | } | |
3586 | else if (bind == STB_GLOBAL) | |
3587 | { | |
a4d8e49b | 3588 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 AM |
3589 | flags = BSF_GLOBAL; |
3590 | } | |
3591 | else if (bind == STB_WEAK) | |
3592 | flags = BSF_WEAK; | |
3593 | else | |
3594 | { | |
3595 | /* Leave it up to the processor backend. */ | |
3596 | } | |
3597 | ||
3598 | if (isym->st_shndx == SHN_UNDEF) | |
3599 | sec = bfd_und_section_ptr; | |
3600 | else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
3601 | { | |
3602 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3603 | if (sec == NULL) | |
3604 | sec = bfd_abs_section_ptr; | |
529fcb95 PB |
3605 | else if (sec->kept_section) |
3606 | { | |
1f02cbd9 | 3607 | /* Symbols from discarded section are undefined, and have |
3b36f7e6 | 3608 | default visibility. */ |
529fcb95 PB |
3609 | sec = bfd_und_section_ptr; |
3610 | isym->st_shndx = SHN_UNDEF; | |
1f02cbd9 JB |
3611 | isym->st_other = STV_DEFAULT |
3612 | | (isym->st_other & ~ ELF_ST_VISIBILITY(-1)); | |
529fcb95 | 3613 | } |
4ad4eba5 AM |
3614 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
3615 | value -= sec->vma; | |
3616 | } | |
3617 | else if (isym->st_shndx == SHN_ABS) | |
3618 | sec = bfd_abs_section_ptr; | |
3619 | else if (isym->st_shndx == SHN_COMMON) | |
3620 | { | |
3621 | sec = bfd_com_section_ptr; | |
3622 | /* What ELF calls the size we call the value. What ELF | |
3623 | calls the value we call the alignment. */ | |
3624 | value = isym->st_size; | |
3625 | } | |
3626 | else | |
3627 | { | |
3628 | /* Leave it up to the processor backend. */ | |
3629 | } | |
3630 | ||
3631 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3632 | isym->st_name); | |
3633 | if (name == NULL) | |
3634 | goto error_free_vers; | |
3635 | ||
3636 | if (isym->st_shndx == SHN_COMMON | |
3637 | && ELF_ST_TYPE (isym->st_info) == STT_TLS) | |
3638 | { | |
3639 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3640 | ||
3641 | if (tcomm == NULL) | |
3642 | { | |
3496cb2a L |
3643 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", |
3644 | (SEC_ALLOC | |
3645 | | SEC_IS_COMMON | |
3646 | | SEC_LINKER_CREATED | |
3647 | | SEC_THREAD_LOCAL)); | |
3648 | if (tcomm == NULL) | |
4ad4eba5 AM |
3649 | goto error_free_vers; |
3650 | } | |
3651 | sec = tcomm; | |
3652 | } | |
3653 | else if (add_symbol_hook) | |
3654 | { | |
3655 | if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec, | |
3656 | &value)) | |
3657 | goto error_free_vers; | |
3658 | ||
3659 | /* The hook function sets the name to NULL if this symbol | |
3660 | should be skipped for some reason. */ | |
3661 | if (name == NULL) | |
3662 | continue; | |
3663 | } | |
3664 | ||
3665 | /* Sanity check that all possibilities were handled. */ | |
3666 | if (sec == NULL) | |
3667 | { | |
3668 | bfd_set_error (bfd_error_bad_value); | |
3669 | goto error_free_vers; | |
3670 | } | |
3671 | ||
3672 | if (bfd_is_und_section (sec) | |
3673 | || bfd_is_com_section (sec)) | |
3674 | definition = FALSE; | |
3675 | else | |
3676 | definition = TRUE; | |
3677 | ||
3678 | size_change_ok = FALSE; | |
3679 | type_change_ok = get_elf_backend_data (abfd)->type_change_ok; | |
3680 | old_alignment = 0; | |
3681 | old_bfd = NULL; | |
af44c138 | 3682 | new_sec = sec; |
4ad4eba5 AM |
3683 | |
3684 | if (is_elf_hash_table (hash_table)) | |
3685 | { | |
3686 | Elf_Internal_Versym iver; | |
3687 | unsigned int vernum = 0; | |
3688 | bfd_boolean skip; | |
3689 | ||
fc0e6df6 | 3690 | if (ever == NULL) |
4ad4eba5 | 3691 | { |
fc0e6df6 PB |
3692 | if (info->default_imported_symver) |
3693 | /* Use the default symbol version created earlier. */ | |
3694 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
3695 | else | |
3696 | iver.vs_vers = 0; | |
3697 | } | |
3698 | else | |
3699 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
3700 | ||
3701 | vernum = iver.vs_vers & VERSYM_VERSION; | |
3702 | ||
3703 | /* If this is a hidden symbol, or if it is not version | |
3704 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
3705 | However, we do not modify a non-hidden absolute symbol |
3706 | if it is not a function, because it might be the version | |
3707 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 3708 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
cc86ff91 EB |
3709 | || (vernum > 1 && (! bfd_is_abs_section (sec) |
3710 | || ELF_ST_TYPE (isym->st_info) == STT_FUNC))) | |
fc0e6df6 PB |
3711 | { |
3712 | const char *verstr; | |
3713 | size_t namelen, verlen, newlen; | |
3714 | char *newname, *p; | |
3715 | ||
3716 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 3717 | { |
fc0e6df6 PB |
3718 | if (vernum > elf_tdata (abfd)->cverdefs) |
3719 | verstr = NULL; | |
3720 | else if (vernum > 1) | |
3721 | verstr = | |
3722 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
3723 | else | |
3724 | verstr = ""; | |
4ad4eba5 | 3725 | |
fc0e6df6 | 3726 | if (verstr == NULL) |
4ad4eba5 | 3727 | { |
fc0e6df6 PB |
3728 | (*_bfd_error_handler) |
3729 | (_("%B: %s: invalid version %u (max %d)"), | |
3730 | abfd, name, vernum, | |
3731 | elf_tdata (abfd)->cverdefs); | |
3732 | bfd_set_error (bfd_error_bad_value); | |
3733 | goto error_free_vers; | |
4ad4eba5 | 3734 | } |
fc0e6df6 PB |
3735 | } |
3736 | else | |
3737 | { | |
3738 | /* We cannot simply test for the number of | |
3739 | entries in the VERNEED section since the | |
3740 | numbers for the needed versions do not start | |
3741 | at 0. */ | |
3742 | Elf_Internal_Verneed *t; | |
3743 | ||
3744 | verstr = NULL; | |
3745 | for (t = elf_tdata (abfd)->verref; | |
3746 | t != NULL; | |
3747 | t = t->vn_nextref) | |
4ad4eba5 | 3748 | { |
fc0e6df6 | 3749 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 3750 | |
fc0e6df6 PB |
3751 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
3752 | { | |
3753 | if (a->vna_other == vernum) | |
4ad4eba5 | 3754 | { |
fc0e6df6 PB |
3755 | verstr = a->vna_nodename; |
3756 | break; | |
4ad4eba5 | 3757 | } |
4ad4eba5 | 3758 | } |
fc0e6df6 PB |
3759 | if (a != NULL) |
3760 | break; | |
3761 | } | |
3762 | if (verstr == NULL) | |
3763 | { | |
3764 | (*_bfd_error_handler) | |
3765 | (_("%B: %s: invalid needed version %d"), | |
3766 | abfd, name, vernum); | |
3767 | bfd_set_error (bfd_error_bad_value); | |
3768 | goto error_free_vers; | |
4ad4eba5 | 3769 | } |
4ad4eba5 | 3770 | } |
fc0e6df6 PB |
3771 | |
3772 | namelen = strlen (name); | |
3773 | verlen = strlen (verstr); | |
3774 | newlen = namelen + verlen + 2; | |
3775 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3776 | && isym->st_shndx != SHN_UNDEF) | |
3777 | ++newlen; | |
3778 | ||
3779 | newname = bfd_alloc (abfd, newlen); | |
3780 | if (newname == NULL) | |
3781 | goto error_free_vers; | |
3782 | memcpy (newname, name, namelen); | |
3783 | p = newname + namelen; | |
3784 | *p++ = ELF_VER_CHR; | |
3785 | /* If this is a defined non-hidden version symbol, | |
3786 | we add another @ to the name. This indicates the | |
3787 | default version of the symbol. */ | |
3788 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3789 | && isym->st_shndx != SHN_UNDEF) | |
3790 | *p++ = ELF_VER_CHR; | |
3791 | memcpy (p, verstr, verlen + 1); | |
3792 | ||
3793 | name = newname; | |
4ad4eba5 AM |
3794 | } |
3795 | ||
af44c138 L |
3796 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, |
3797 | &value, &old_alignment, | |
4ad4eba5 AM |
3798 | sym_hash, &skip, &override, |
3799 | &type_change_ok, &size_change_ok)) | |
3800 | goto error_free_vers; | |
3801 | ||
3802 | if (skip) | |
3803 | continue; | |
3804 | ||
3805 | if (override) | |
3806 | definition = FALSE; | |
3807 | ||
3808 | h = *sym_hash; | |
3809 | while (h->root.type == bfd_link_hash_indirect | |
3810 | || h->root.type == bfd_link_hash_warning) | |
3811 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3812 | ||
3813 | /* Remember the old alignment if this is a common symbol, so | |
3814 | that we don't reduce the alignment later on. We can't | |
3815 | check later, because _bfd_generic_link_add_one_symbol | |
3816 | will set a default for the alignment which we want to | |
3817 | override. We also remember the old bfd where the existing | |
3818 | definition comes from. */ | |
3819 | switch (h->root.type) | |
3820 | { | |
3821 | default: | |
3822 | break; | |
3823 | ||
3824 | case bfd_link_hash_defined: | |
3825 | case bfd_link_hash_defweak: | |
3826 | old_bfd = h->root.u.def.section->owner; | |
3827 | break; | |
3828 | ||
3829 | case bfd_link_hash_common: | |
3830 | old_bfd = h->root.u.c.p->section->owner; | |
3831 | old_alignment = h->root.u.c.p->alignment_power; | |
3832 | break; | |
3833 | } | |
3834 | ||
3835 | if (elf_tdata (abfd)->verdef != NULL | |
3836 | && ! override | |
3837 | && vernum > 1 | |
3838 | && definition) | |
3839 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
3840 | } | |
3841 | ||
3842 | if (! (_bfd_generic_link_add_one_symbol | |
3843 | (info, abfd, name, flags, sec, value, NULL, FALSE, collect, | |
3844 | (struct bfd_link_hash_entry **) sym_hash))) | |
3845 | goto error_free_vers; | |
3846 | ||
3847 | h = *sym_hash; | |
3848 | while (h->root.type == bfd_link_hash_indirect | |
3849 | || h->root.type == bfd_link_hash_warning) | |
3850 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3851 | *sym_hash = h; | |
3852 | ||
3853 | new_weakdef = FALSE; | |
3854 | if (dynamic | |
3855 | && definition | |
3856 | && (flags & BSF_WEAK) != 0 | |
3857 | && ELF_ST_TYPE (isym->st_info) != STT_FUNC | |
3858 | && is_elf_hash_table (hash_table) | |
f6e332e6 | 3859 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
3860 | { |
3861 | /* Keep a list of all weak defined non function symbols from | |
3862 | a dynamic object, using the weakdef field. Later in this | |
3863 | function we will set the weakdef field to the correct | |
3864 | value. We only put non-function symbols from dynamic | |
3865 | objects on this list, because that happens to be the only | |
3866 | time we need to know the normal symbol corresponding to a | |
3867 | weak symbol, and the information is time consuming to | |
3868 | figure out. If the weakdef field is not already NULL, | |
3869 | then this symbol was already defined by some previous | |
3870 | dynamic object, and we will be using that previous | |
3871 | definition anyhow. */ | |
3872 | ||
f6e332e6 | 3873 | h->u.weakdef = weaks; |
4ad4eba5 AM |
3874 | weaks = h; |
3875 | new_weakdef = TRUE; | |
3876 | } | |
3877 | ||
3878 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 3879 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
3880 | && h->root.type == bfd_link_hash_common) |
3881 | { | |
3882 | unsigned int align; | |
3883 | ||
a4d8e49b | 3884 | if (common) |
af44c138 L |
3885 | align = bfd_log2 (isym->st_value); |
3886 | else | |
3887 | { | |
3888 | /* The new symbol is a common symbol in a shared object. | |
3889 | We need to get the alignment from the section. */ | |
3890 | align = new_sec->alignment_power; | |
3891 | } | |
4ad4eba5 AM |
3892 | if (align > old_alignment |
3893 | /* Permit an alignment power of zero if an alignment of one | |
3894 | is specified and no other alignments have been specified. */ | |
3895 | || (isym->st_value == 1 && old_alignment == 0)) | |
3896 | h->root.u.c.p->alignment_power = align; | |
3897 | else | |
3898 | h->root.u.c.p->alignment_power = old_alignment; | |
3899 | } | |
3900 | ||
3901 | if (is_elf_hash_table (hash_table)) | |
3902 | { | |
4ad4eba5 | 3903 | bfd_boolean dynsym; |
4ad4eba5 AM |
3904 | |
3905 | /* Check the alignment when a common symbol is involved. This | |
3906 | can change when a common symbol is overridden by a normal | |
3907 | definition or a common symbol is ignored due to the old | |
3908 | normal definition. We need to make sure the maximum | |
3909 | alignment is maintained. */ | |
a4d8e49b | 3910 | if ((old_alignment || common) |
4ad4eba5 AM |
3911 | && h->root.type != bfd_link_hash_common) |
3912 | { | |
3913 | unsigned int common_align; | |
3914 | unsigned int normal_align; | |
3915 | unsigned int symbol_align; | |
3916 | bfd *normal_bfd; | |
3917 | bfd *common_bfd; | |
3918 | ||
3919 | symbol_align = ffs (h->root.u.def.value) - 1; | |
3920 | if (h->root.u.def.section->owner != NULL | |
3921 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
3922 | { | |
3923 | normal_align = h->root.u.def.section->alignment_power; | |
3924 | if (normal_align > symbol_align) | |
3925 | normal_align = symbol_align; | |
3926 | } | |
3927 | else | |
3928 | normal_align = symbol_align; | |
3929 | ||
3930 | if (old_alignment) | |
3931 | { | |
3932 | common_align = old_alignment; | |
3933 | common_bfd = old_bfd; | |
3934 | normal_bfd = abfd; | |
3935 | } | |
3936 | else | |
3937 | { | |
3938 | common_align = bfd_log2 (isym->st_value); | |
3939 | common_bfd = abfd; | |
3940 | normal_bfd = old_bfd; | |
3941 | } | |
3942 | ||
3943 | if (normal_align < common_align) | |
3944 | (*_bfd_error_handler) | |
d003868e AM |
3945 | (_("Warning: alignment %u of symbol `%s' in %B" |
3946 | " is smaller than %u in %B"), | |
3947 | normal_bfd, common_bfd, | |
3948 | 1 << normal_align, name, 1 << common_align); | |
4ad4eba5 AM |
3949 | } |
3950 | ||
3951 | /* Remember the symbol size and type. */ | |
3952 | if (isym->st_size != 0 | |
3953 | && (definition || h->size == 0)) | |
3954 | { | |
3955 | if (h->size != 0 && h->size != isym->st_size && ! size_change_ok) | |
3956 | (*_bfd_error_handler) | |
d003868e AM |
3957 | (_("Warning: size of symbol `%s' changed" |
3958 | " from %lu in %B to %lu in %B"), | |
3959 | old_bfd, abfd, | |
4ad4eba5 | 3960 | name, (unsigned long) h->size, |
d003868e | 3961 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
3962 | |
3963 | h->size = isym->st_size; | |
3964 | } | |
3965 | ||
3966 | /* If this is a common symbol, then we always want H->SIZE | |
3967 | to be the size of the common symbol. The code just above | |
3968 | won't fix the size if a common symbol becomes larger. We | |
3969 | don't warn about a size change here, because that is | |
3970 | covered by --warn-common. */ | |
3971 | if (h->root.type == bfd_link_hash_common) | |
3972 | h->size = h->root.u.c.size; | |
3973 | ||
3974 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
3975 | && (definition || h->type == STT_NOTYPE)) | |
3976 | { | |
3977 | if (h->type != STT_NOTYPE | |
3978 | && h->type != ELF_ST_TYPE (isym->st_info) | |
3979 | && ! type_change_ok) | |
3980 | (*_bfd_error_handler) | |
d003868e AM |
3981 | (_("Warning: type of symbol `%s' changed" |
3982 | " from %d to %d in %B"), | |
3983 | abfd, name, h->type, ELF_ST_TYPE (isym->st_info)); | |
4ad4eba5 AM |
3984 | |
3985 | h->type = ELF_ST_TYPE (isym->st_info); | |
3986 | } | |
3987 | ||
3988 | /* If st_other has a processor-specific meaning, specific | |
3989 | code might be needed here. We never merge the visibility | |
3990 | attribute with the one from a dynamic object. */ | |
3991 | if (bed->elf_backend_merge_symbol_attribute) | |
3992 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
3993 | dynamic); | |
3994 | ||
b58f81ae DJ |
3995 | /* If this symbol has default visibility and the user has requested |
3996 | we not re-export it, then mark it as hidden. */ | |
3997 | if (definition && !dynamic | |
3998 | && (abfd->no_export | |
3999 | || (abfd->my_archive && abfd->my_archive->no_export)) | |
4000 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) | |
4001 | isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1)); | |
4002 | ||
4ad4eba5 AM |
4003 | if (isym->st_other != 0 && !dynamic) |
4004 | { | |
4005 | unsigned char hvis, symvis, other, nvis; | |
4006 | ||
4007 | /* Take the balance of OTHER from the definition. */ | |
4008 | other = (definition ? isym->st_other : h->other); | |
4009 | other &= ~ ELF_ST_VISIBILITY (-1); | |
4010 | ||
4011 | /* Combine visibilities, using the most constraining one. */ | |
4012 | hvis = ELF_ST_VISIBILITY (h->other); | |
4013 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
4014 | if (! hvis) | |
4015 | nvis = symvis; | |
4016 | else if (! symvis) | |
4017 | nvis = hvis; | |
4018 | else | |
4019 | nvis = hvis < symvis ? hvis : symvis; | |
4020 | ||
4021 | h->other = other | nvis; | |
4022 | } | |
4023 | ||
4024 | /* Set a flag in the hash table entry indicating the type of | |
4025 | reference or definition we just found. Keep a count of | |
4026 | the number of dynamic symbols we find. A dynamic symbol | |
4027 | is one which is referenced or defined by both a regular | |
4028 | object and a shared object. */ | |
4ad4eba5 AM |
4029 | dynsym = FALSE; |
4030 | if (! dynamic) | |
4031 | { | |
4032 | if (! definition) | |
4033 | { | |
f5385ebf | 4034 | h->ref_regular = 1; |
4ad4eba5 | 4035 | if (bind != STB_WEAK) |
f5385ebf | 4036 | h->ref_regular_nonweak = 1; |
4ad4eba5 AM |
4037 | } |
4038 | else | |
f5385ebf | 4039 | h->def_regular = 1; |
4ad4eba5 | 4040 | if (! info->executable |
f5385ebf AM |
4041 | || h->def_dynamic |
4042 | || h->ref_dynamic) | |
4ad4eba5 AM |
4043 | dynsym = TRUE; |
4044 | } | |
4045 | else | |
4046 | { | |
4047 | if (! definition) | |
f5385ebf | 4048 | h->ref_dynamic = 1; |
4ad4eba5 | 4049 | else |
f5385ebf AM |
4050 | h->def_dynamic = 1; |
4051 | if (h->def_regular | |
4052 | || h->ref_regular | |
f6e332e6 | 4053 | || (h->u.weakdef != NULL |
4ad4eba5 | 4054 | && ! new_weakdef |
f6e332e6 | 4055 | && h->u.weakdef->dynindx != -1)) |
4ad4eba5 AM |
4056 | dynsym = TRUE; |
4057 | } | |
4058 | ||
4ad4eba5 AM |
4059 | /* Check to see if we need to add an indirect symbol for |
4060 | the default name. */ | |
4061 | if (definition || h->root.type == bfd_link_hash_common) | |
4062 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4063 | &sec, &value, &dynsym, | |
4064 | override)) | |
4065 | goto error_free_vers; | |
4066 | ||
4067 | if (definition && !dynamic) | |
4068 | { | |
4069 | char *p = strchr (name, ELF_VER_CHR); | |
4070 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4071 | { | |
4072 | /* Queue non-default versions so that .symver x, x@FOO | |
4073 | aliases can be checked. */ | |
4074 | if (! nondeflt_vers) | |
4075 | { | |
4076 | amt = (isymend - isym + 1) | |
4077 | * sizeof (struct elf_link_hash_entry *); | |
4078 | nondeflt_vers = bfd_malloc (amt); | |
4079 | } | |
4080 | nondeflt_vers [nondeflt_vers_cnt++] = h; | |
4081 | } | |
4082 | } | |
4083 | ||
4084 | if (dynsym && h->dynindx == -1) | |
4085 | { | |
c152c796 | 4086 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4087 | goto error_free_vers; |
f6e332e6 | 4088 | if (h->u.weakdef != NULL |
4ad4eba5 | 4089 | && ! new_weakdef |
f6e332e6 | 4090 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 4091 | { |
f6e332e6 | 4092 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
4093 | goto error_free_vers; |
4094 | } | |
4095 | } | |
4096 | else if (dynsym && h->dynindx != -1) | |
4097 | /* If the symbol already has a dynamic index, but | |
4098 | visibility says it should not be visible, turn it into | |
4099 | a local symbol. */ | |
4100 | switch (ELF_ST_VISIBILITY (h->other)) | |
4101 | { | |
4102 | case STV_INTERNAL: | |
4103 | case STV_HIDDEN: | |
4104 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4105 | dynsym = FALSE; | |
4106 | break; | |
4107 | } | |
4108 | ||
4109 | if (!add_needed | |
4110 | && definition | |
4111 | && dynsym | |
f5385ebf | 4112 | && h->ref_regular) |
4ad4eba5 AM |
4113 | { |
4114 | int ret; | |
4115 | const char *soname = elf_dt_name (abfd); | |
4116 | ||
4117 | /* A symbol from a library loaded via DT_NEEDED of some | |
4118 | other library is referenced by a regular object. | |
e56f61be L |
4119 | Add a DT_NEEDED entry for it. Issue an error if |
4120 | --no-add-needed is used. */ | |
4121 | if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) | |
4122 | { | |
4123 | (*_bfd_error_handler) | |
4124 | (_("%s: invalid DSO for symbol `%s' definition"), | |
d003868e | 4125 | abfd, name); |
e56f61be L |
4126 | bfd_set_error (bfd_error_bad_value); |
4127 | goto error_free_vers; | |
4128 | } | |
4129 | ||
a5db907e AM |
4130 | elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED; |
4131 | ||
4ad4eba5 | 4132 | add_needed = TRUE; |
7e9f0867 | 4133 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4134 | if (ret < 0) |
4135 | goto error_free_vers; | |
4136 | ||
4137 | BFD_ASSERT (ret == 0); | |
4138 | } | |
4139 | } | |
4140 | } | |
4141 | ||
4142 | /* Now that all the symbols from this input file are created, handle | |
4143 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
4144 | if (nondeflt_vers != NULL) | |
4145 | { | |
4146 | bfd_size_type cnt, symidx; | |
4147 | ||
4148 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
4149 | { | |
4150 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
4151 | char *shortname, *p; | |
4152 | ||
4153 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
4154 | if (p == NULL | |
4155 | || (h->root.type != bfd_link_hash_defined | |
4156 | && h->root.type != bfd_link_hash_defweak)) | |
4157 | continue; | |
4158 | ||
4159 | amt = p - h->root.root.string; | |
4160 | shortname = bfd_malloc (amt + 1); | |
4161 | memcpy (shortname, h->root.root.string, amt); | |
4162 | shortname[amt] = '\0'; | |
4163 | ||
4164 | hi = (struct elf_link_hash_entry *) | |
4165 | bfd_link_hash_lookup (&hash_table->root, shortname, | |
4166 | FALSE, FALSE, FALSE); | |
4167 | if (hi != NULL | |
4168 | && hi->root.type == h->root.type | |
4169 | && hi->root.u.def.value == h->root.u.def.value | |
4170 | && hi->root.u.def.section == h->root.u.def.section) | |
4171 | { | |
4172 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4173 | hi->root.type = bfd_link_hash_indirect; | |
4174 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 4175 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
4176 | sym_hash = elf_sym_hashes (abfd); |
4177 | if (sym_hash) | |
4178 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4179 | if (sym_hash[symidx] == hi) | |
4180 | { | |
4181 | sym_hash[symidx] = h; | |
4182 | break; | |
4183 | } | |
4184 | } | |
4185 | free (shortname); | |
4186 | } | |
4187 | free (nondeflt_vers); | |
4188 | nondeflt_vers = NULL; | |
4189 | } | |
4190 | ||
4191 | if (extversym != NULL) | |
4192 | { | |
4193 | free (extversym); | |
4194 | extversym = NULL; | |
4195 | } | |
4196 | ||
4197 | if (isymbuf != NULL) | |
4198 | free (isymbuf); | |
4199 | isymbuf = NULL; | |
4200 | ||
ec13b3bb AM |
4201 | if (!add_needed |
4202 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
77cfaee6 | 4203 | { |
ec13b3bb AM |
4204 | /* Remove symbols defined in an as-needed shared lib that wasn't |
4205 | needed. */ | |
77cfaee6 AM |
4206 | struct elf_smash_syms_data inf; |
4207 | inf.not_needed = abfd; | |
4208 | inf.htab = hash_table; | |
4209 | inf.twiddled = FALSE; | |
4210 | elf_link_hash_traverse (hash_table, elf_smash_syms, &inf); | |
4211 | if (inf.twiddled) | |
4212 | bfd_link_repair_undef_list (&hash_table->root); | |
4213 | weaks = NULL; | |
4214 | } | |
4215 | ||
4ad4eba5 AM |
4216 | /* Now set the weakdefs field correctly for all the weak defined |
4217 | symbols we found. The only way to do this is to search all the | |
4218 | symbols. Since we only need the information for non functions in | |
4219 | dynamic objects, that's the only time we actually put anything on | |
4220 | the list WEAKS. We need this information so that if a regular | |
4221 | object refers to a symbol defined weakly in a dynamic object, the | |
4222 | real symbol in the dynamic object is also put in the dynamic | |
4223 | symbols; we also must arrange for both symbols to point to the | |
4224 | same memory location. We could handle the general case of symbol | |
4225 | aliasing, but a general symbol alias can only be generated in | |
4226 | assembler code, handling it correctly would be very time | |
4227 | consuming, and other ELF linkers don't handle general aliasing | |
4228 | either. */ | |
4229 | if (weaks != NULL) | |
4230 | { | |
4231 | struct elf_link_hash_entry **hpp; | |
4232 | struct elf_link_hash_entry **hppend; | |
4233 | struct elf_link_hash_entry **sorted_sym_hash; | |
4234 | struct elf_link_hash_entry *h; | |
4235 | size_t sym_count; | |
4236 | ||
4237 | /* Since we have to search the whole symbol list for each weak | |
4238 | defined symbol, search time for N weak defined symbols will be | |
4239 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
4240 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
4241 | sorted_sym_hash = bfd_malloc (amt); | |
4242 | if (sorted_sym_hash == NULL) | |
4243 | goto error_return; | |
4244 | sym_hash = sorted_sym_hash; | |
4245 | hpp = elf_sym_hashes (abfd); | |
4246 | hppend = hpp + extsymcount; | |
4247 | sym_count = 0; | |
4248 | for (; hpp < hppend; hpp++) | |
4249 | { | |
4250 | h = *hpp; | |
4251 | if (h != NULL | |
4252 | && h->root.type == bfd_link_hash_defined | |
4253 | && h->type != STT_FUNC) | |
4254 | { | |
4255 | *sym_hash = h; | |
4256 | sym_hash++; | |
4257 | sym_count++; | |
4258 | } | |
4259 | } | |
4260 | ||
4261 | qsort (sorted_sym_hash, sym_count, | |
4262 | sizeof (struct elf_link_hash_entry *), | |
4263 | elf_sort_symbol); | |
4264 | ||
4265 | while (weaks != NULL) | |
4266 | { | |
4267 | struct elf_link_hash_entry *hlook; | |
4268 | asection *slook; | |
4269 | bfd_vma vlook; | |
4270 | long ilook; | |
4271 | size_t i, j, idx; | |
4272 | ||
4273 | hlook = weaks; | |
f6e332e6 AM |
4274 | weaks = hlook->u.weakdef; |
4275 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
4276 | |
4277 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
4278 | || hlook->root.type == bfd_link_hash_defweak | |
4279 | || hlook->root.type == bfd_link_hash_common | |
4280 | || hlook->root.type == bfd_link_hash_indirect); | |
4281 | slook = hlook->root.u.def.section; | |
4282 | vlook = hlook->root.u.def.value; | |
4283 | ||
4284 | ilook = -1; | |
4285 | i = 0; | |
4286 | j = sym_count; | |
4287 | while (i < j) | |
4288 | { | |
4289 | bfd_signed_vma vdiff; | |
4290 | idx = (i + j) / 2; | |
4291 | h = sorted_sym_hash [idx]; | |
4292 | vdiff = vlook - h->root.u.def.value; | |
4293 | if (vdiff < 0) | |
4294 | j = idx; | |
4295 | else if (vdiff > 0) | |
4296 | i = idx + 1; | |
4297 | else | |
4298 | { | |
a9b881be | 4299 | long sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
4300 | if (sdiff < 0) |
4301 | j = idx; | |
4302 | else if (sdiff > 0) | |
4303 | i = idx + 1; | |
4304 | else | |
4305 | { | |
4306 | ilook = idx; | |
4307 | break; | |
4308 | } | |
4309 | } | |
4310 | } | |
4311 | ||
4312 | /* We didn't find a value/section match. */ | |
4313 | if (ilook == -1) | |
4314 | continue; | |
4315 | ||
4316 | for (i = ilook; i < sym_count; i++) | |
4317 | { | |
4318 | h = sorted_sym_hash [i]; | |
4319 | ||
4320 | /* Stop if value or section doesn't match. */ | |
4321 | if (h->root.u.def.value != vlook | |
4322 | || h->root.u.def.section != slook) | |
4323 | break; | |
4324 | else if (h != hlook) | |
4325 | { | |
f6e332e6 | 4326 | hlook->u.weakdef = h; |
4ad4eba5 AM |
4327 | |
4328 | /* If the weak definition is in the list of dynamic | |
4329 | symbols, make sure the real definition is put | |
4330 | there as well. */ | |
4331 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
4332 | { | |
c152c796 | 4333 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 AM |
4334 | goto error_return; |
4335 | } | |
4336 | ||
4337 | /* If the real definition is in the list of dynamic | |
4338 | symbols, make sure the weak definition is put | |
4339 | there as well. If we don't do this, then the | |
4340 | dynamic loader might not merge the entries for the | |
4341 | real definition and the weak definition. */ | |
4342 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
4343 | { | |
c152c796 | 4344 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4ad4eba5 AM |
4345 | goto error_return; |
4346 | } | |
4347 | break; | |
4348 | } | |
4349 | } | |
4350 | } | |
4351 | ||
4352 | free (sorted_sym_hash); | |
4353 | } | |
4354 | ||
85fbca6a NC |
4355 | check_directives = get_elf_backend_data (abfd)->check_directives; |
4356 | if (check_directives) | |
4357 | check_directives (abfd, info); | |
4358 | ||
4ad4eba5 AM |
4359 | /* If this object is the same format as the output object, and it is |
4360 | not a shared library, then let the backend look through the | |
4361 | relocs. | |
4362 | ||
4363 | This is required to build global offset table entries and to | |
4364 | arrange for dynamic relocs. It is not required for the | |
4365 | particular common case of linking non PIC code, even when linking | |
4366 | against shared libraries, but unfortunately there is no way of | |
4367 | knowing whether an object file has been compiled PIC or not. | |
4368 | Looking through the relocs is not particularly time consuming. | |
4369 | The problem is that we must either (1) keep the relocs in memory, | |
4370 | which causes the linker to require additional runtime memory or | |
4371 | (2) read the relocs twice from the input file, which wastes time. | |
4372 | This would be a good case for using mmap. | |
4373 | ||
4374 | I have no idea how to handle linking PIC code into a file of a | |
4375 | different format. It probably can't be done. */ | |
4376 | check_relocs = get_elf_backend_data (abfd)->check_relocs; | |
4377 | if (! dynamic | |
4378 | && is_elf_hash_table (hash_table) | |
4379 | && hash_table->root.creator == abfd->xvec | |
4380 | && check_relocs != NULL) | |
4381 | { | |
4382 | asection *o; | |
4383 | ||
4384 | for (o = abfd->sections; o != NULL; o = o->next) | |
4385 | { | |
4386 | Elf_Internal_Rela *internal_relocs; | |
4387 | bfd_boolean ok; | |
4388 | ||
4389 | if ((o->flags & SEC_RELOC) == 0 | |
4390 | || o->reloc_count == 0 | |
4391 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4392 | && (o->flags & SEC_DEBUGGING) != 0) | |
4393 | || bfd_is_abs_section (o->output_section)) | |
4394 | continue; | |
4395 | ||
4396 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4397 | info->keep_memory); | |
4398 | if (internal_relocs == NULL) | |
4399 | goto error_return; | |
4400 | ||
4401 | ok = (*check_relocs) (abfd, info, o, internal_relocs); | |
4402 | ||
4403 | if (elf_section_data (o)->relocs != internal_relocs) | |
4404 | free (internal_relocs); | |
4405 | ||
4406 | if (! ok) | |
4407 | goto error_return; | |
4408 | } | |
4409 | } | |
4410 | ||
4411 | /* If this is a non-traditional link, try to optimize the handling | |
4412 | of the .stab/.stabstr sections. */ | |
4413 | if (! dynamic | |
4414 | && ! info->traditional_format | |
4415 | && is_elf_hash_table (hash_table) | |
4416 | && (info->strip != strip_all && info->strip != strip_debugger)) | |
4417 | { | |
4418 | asection *stabstr; | |
4419 | ||
4420 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4421 | if (stabstr != NULL) | |
4422 | { | |
4423 | bfd_size_type string_offset = 0; | |
4424 | asection *stab; | |
4425 | ||
4426 | for (stab = abfd->sections; stab; stab = stab->next) | |
4427 | if (strncmp (".stab", stab->name, 5) == 0 | |
4428 | && (!stab->name[5] || | |
4429 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4430 | && (stab->flags & SEC_MERGE) == 0 | |
4431 | && !bfd_is_abs_section (stab->output_section)) | |
4432 | { | |
4433 | struct bfd_elf_section_data *secdata; | |
4434 | ||
4435 | secdata = elf_section_data (stab); | |
4436 | if (! _bfd_link_section_stabs (abfd, | |
3722b82f | 4437 | &hash_table->stab_info, |
4ad4eba5 AM |
4438 | stab, stabstr, |
4439 | &secdata->sec_info, | |
4440 | &string_offset)) | |
4441 | goto error_return; | |
4442 | if (secdata->sec_info) | |
4443 | stab->sec_info_type = ELF_INFO_TYPE_STABS; | |
4444 | } | |
4445 | } | |
4446 | } | |
4447 | ||
77cfaee6 | 4448 | if (is_elf_hash_table (hash_table) && add_needed) |
4ad4eba5 AM |
4449 | { |
4450 | /* Add this bfd to the loaded list. */ | |
4451 | struct elf_link_loaded_list *n; | |
4452 | ||
4453 | n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); | |
4454 | if (n == NULL) | |
4455 | goto error_return; | |
4456 | n->abfd = abfd; | |
4457 | n->next = hash_table->loaded; | |
4458 | hash_table->loaded = n; | |
4459 | } | |
4460 | ||
4461 | return TRUE; | |
4462 | ||
4463 | error_free_vers: | |
4464 | if (nondeflt_vers != NULL) | |
4465 | free (nondeflt_vers); | |
4466 | if (extversym != NULL) | |
4467 | free (extversym); | |
4468 | error_free_sym: | |
4469 | if (isymbuf != NULL) | |
4470 | free (isymbuf); | |
4471 | error_return: | |
4472 | return FALSE; | |
4473 | } | |
4474 | ||
8387904d AM |
4475 | /* Return the linker hash table entry of a symbol that might be |
4476 | satisfied by an archive symbol. Return -1 on error. */ | |
4477 | ||
4478 | struct elf_link_hash_entry * | |
4479 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
4480 | struct bfd_link_info *info, | |
4481 | const char *name) | |
4482 | { | |
4483 | struct elf_link_hash_entry *h; | |
4484 | char *p, *copy; | |
4485 | size_t len, first; | |
4486 | ||
4487 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
4488 | if (h != NULL) | |
4489 | return h; | |
4490 | ||
4491 | /* If this is a default version (the name contains @@), look up the | |
4492 | symbol again with only one `@' as well as without the version. | |
4493 | The effect is that references to the symbol with and without the | |
4494 | version will be matched by the default symbol in the archive. */ | |
4495 | ||
4496 | p = strchr (name, ELF_VER_CHR); | |
4497 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4498 | return h; | |
4499 | ||
4500 | /* First check with only one `@'. */ | |
4501 | len = strlen (name); | |
4502 | copy = bfd_alloc (abfd, len); | |
4503 | if (copy == NULL) | |
4504 | return (struct elf_link_hash_entry *) 0 - 1; | |
4505 | ||
4506 | first = p - name + 1; | |
4507 | memcpy (copy, name, first); | |
4508 | memcpy (copy + first, name + first + 1, len - first); | |
4509 | ||
4510 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE); | |
4511 | if (h == NULL) | |
4512 | { | |
4513 | /* We also need to check references to the symbol without the | |
4514 | version. */ | |
4515 | copy[first - 1] = '\0'; | |
4516 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
4517 | FALSE, FALSE, FALSE); | |
4518 | } | |
4519 | ||
4520 | bfd_release (abfd, copy); | |
4521 | return h; | |
4522 | } | |
4523 | ||
0ad989f9 L |
4524 | /* Add symbols from an ELF archive file to the linker hash table. We |
4525 | don't use _bfd_generic_link_add_archive_symbols because of a | |
4526 | problem which arises on UnixWare. The UnixWare libc.so is an | |
4527 | archive which includes an entry libc.so.1 which defines a bunch of | |
4528 | symbols. The libc.so archive also includes a number of other | |
4529 | object files, which also define symbols, some of which are the same | |
4530 | as those defined in libc.so.1. Correct linking requires that we | |
4531 | consider each object file in turn, and include it if it defines any | |
4532 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
4533 | this; it looks through the list of undefined symbols, and includes | |
4534 | any object file which defines them. When this algorithm is used on | |
4535 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
4536 | bunch of symbols. This means that some of the other objects in the | |
4537 | archive are not included in the link, which is incorrect since they | |
4538 | precede libc.so.1 in the archive. | |
4539 | ||
4540 | Fortunately, ELF archive handling is simpler than that done by | |
4541 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4542 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4543 | symbol is currently undefined, we know that we must pull in that | |
4544 | object file. | |
4545 | ||
4546 | Unfortunately, we do have to make multiple passes over the symbol | |
4547 | table until nothing further is resolved. */ | |
4548 | ||
4ad4eba5 AM |
4549 | static bfd_boolean |
4550 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
4551 | { |
4552 | symindex c; | |
4553 | bfd_boolean *defined = NULL; | |
4554 | bfd_boolean *included = NULL; | |
4555 | carsym *symdefs; | |
4556 | bfd_boolean loop; | |
4557 | bfd_size_type amt; | |
8387904d AM |
4558 | const struct elf_backend_data *bed; |
4559 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
4560 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
4561 | |
4562 | if (! bfd_has_map (abfd)) | |
4563 | { | |
4564 | /* An empty archive is a special case. */ | |
4565 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
4566 | return TRUE; | |
4567 | bfd_set_error (bfd_error_no_armap); | |
4568 | return FALSE; | |
4569 | } | |
4570 | ||
4571 | /* Keep track of all symbols we know to be already defined, and all | |
4572 | files we know to be already included. This is to speed up the | |
4573 | second and subsequent passes. */ | |
4574 | c = bfd_ardata (abfd)->symdef_count; | |
4575 | if (c == 0) | |
4576 | return TRUE; | |
4577 | amt = c; | |
4578 | amt *= sizeof (bfd_boolean); | |
4579 | defined = bfd_zmalloc (amt); | |
4580 | included = bfd_zmalloc (amt); | |
4581 | if (defined == NULL || included == NULL) | |
4582 | goto error_return; | |
4583 | ||
4584 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
4585 | bed = get_elf_backend_data (abfd); |
4586 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
4587 | |
4588 | do | |
4589 | { | |
4590 | file_ptr last; | |
4591 | symindex i; | |
4592 | carsym *symdef; | |
4593 | carsym *symdefend; | |
4594 | ||
4595 | loop = FALSE; | |
4596 | last = -1; | |
4597 | ||
4598 | symdef = symdefs; | |
4599 | symdefend = symdef + c; | |
4600 | for (i = 0; symdef < symdefend; symdef++, i++) | |
4601 | { | |
4602 | struct elf_link_hash_entry *h; | |
4603 | bfd *element; | |
4604 | struct bfd_link_hash_entry *undefs_tail; | |
4605 | symindex mark; | |
4606 | ||
4607 | if (defined[i] || included[i]) | |
4608 | continue; | |
4609 | if (symdef->file_offset == last) | |
4610 | { | |
4611 | included[i] = TRUE; | |
4612 | continue; | |
4613 | } | |
4614 | ||
8387904d AM |
4615 | h = archive_symbol_lookup (abfd, info, symdef->name); |
4616 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
4617 | goto error_return; | |
0ad989f9 L |
4618 | |
4619 | if (h == NULL) | |
4620 | continue; | |
4621 | ||
4622 | if (h->root.type == bfd_link_hash_common) | |
4623 | { | |
4624 | /* We currently have a common symbol. The archive map contains | |
4625 | a reference to this symbol, so we may want to include it. We | |
4626 | only want to include it however, if this archive element | |
4627 | contains a definition of the symbol, not just another common | |
4628 | declaration of it. | |
4629 | ||
4630 | Unfortunately some archivers (including GNU ar) will put | |
4631 | declarations of common symbols into their archive maps, as | |
4632 | well as real definitions, so we cannot just go by the archive | |
4633 | map alone. Instead we must read in the element's symbol | |
4634 | table and check that to see what kind of symbol definition | |
4635 | this is. */ | |
4636 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
4637 | continue; | |
4638 | } | |
4639 | else if (h->root.type != bfd_link_hash_undefined) | |
4640 | { | |
4641 | if (h->root.type != bfd_link_hash_undefweak) | |
4642 | defined[i] = TRUE; | |
4643 | continue; | |
4644 | } | |
4645 | ||
4646 | /* We need to include this archive member. */ | |
4647 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
4648 | if (element == NULL) | |
4649 | goto error_return; | |
4650 | ||
4651 | if (! bfd_check_format (element, bfd_object)) | |
4652 | goto error_return; | |
4653 | ||
4654 | /* Doublecheck that we have not included this object | |
4655 | already--it should be impossible, but there may be | |
4656 | something wrong with the archive. */ | |
4657 | if (element->archive_pass != 0) | |
4658 | { | |
4659 | bfd_set_error (bfd_error_bad_value); | |
4660 | goto error_return; | |
4661 | } | |
4662 | element->archive_pass = 1; | |
4663 | ||
4664 | undefs_tail = info->hash->undefs_tail; | |
4665 | ||
4666 | if (! (*info->callbacks->add_archive_element) (info, element, | |
4667 | symdef->name)) | |
4668 | goto error_return; | |
4669 | if (! bfd_link_add_symbols (element, info)) | |
4670 | goto error_return; | |
4671 | ||
4672 | /* If there are any new undefined symbols, we need to make | |
4673 | another pass through the archive in order to see whether | |
4674 | they can be defined. FIXME: This isn't perfect, because | |
4675 | common symbols wind up on undefs_tail and because an | |
4676 | undefined symbol which is defined later on in this pass | |
4677 | does not require another pass. This isn't a bug, but it | |
4678 | does make the code less efficient than it could be. */ | |
4679 | if (undefs_tail != info->hash->undefs_tail) | |
4680 | loop = TRUE; | |
4681 | ||
4682 | /* Look backward to mark all symbols from this object file | |
4683 | which we have already seen in this pass. */ | |
4684 | mark = i; | |
4685 | do | |
4686 | { | |
4687 | included[mark] = TRUE; | |
4688 | if (mark == 0) | |
4689 | break; | |
4690 | --mark; | |
4691 | } | |
4692 | while (symdefs[mark].file_offset == symdef->file_offset); | |
4693 | ||
4694 | /* We mark subsequent symbols from this object file as we go | |
4695 | on through the loop. */ | |
4696 | last = symdef->file_offset; | |
4697 | } | |
4698 | } | |
4699 | while (loop); | |
4700 | ||
4701 | free (defined); | |
4702 | free (included); | |
4703 | ||
4704 | return TRUE; | |
4705 | ||
4706 | error_return: | |
4707 | if (defined != NULL) | |
4708 | free (defined); | |
4709 | if (included != NULL) | |
4710 | free (included); | |
4711 | return FALSE; | |
4712 | } | |
4ad4eba5 AM |
4713 | |
4714 | /* Given an ELF BFD, add symbols to the global hash table as | |
4715 | appropriate. */ | |
4716 | ||
4717 | bfd_boolean | |
4718 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
4719 | { | |
4720 | switch (bfd_get_format (abfd)) | |
4721 | { | |
4722 | case bfd_object: | |
4723 | return elf_link_add_object_symbols (abfd, info); | |
4724 | case bfd_archive: | |
4725 | return elf_link_add_archive_symbols (abfd, info); | |
4726 | default: | |
4727 | bfd_set_error (bfd_error_wrong_format); | |
4728 | return FALSE; | |
4729 | } | |
4730 | } | |
5a580b3a AM |
4731 | \f |
4732 | /* This function will be called though elf_link_hash_traverse to store | |
4733 | all hash value of the exported symbols in an array. */ | |
4734 | ||
4735 | static bfd_boolean | |
4736 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
4737 | { | |
4738 | unsigned long **valuep = data; | |
4739 | const char *name; | |
4740 | char *p; | |
4741 | unsigned long ha; | |
4742 | char *alc = NULL; | |
4743 | ||
4744 | if (h->root.type == bfd_link_hash_warning) | |
4745 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4746 | ||
4747 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
4748 | if (h->dynindx == -1) | |
4749 | return TRUE; | |
4750 | ||
4751 | name = h->root.root.string; | |
4752 | p = strchr (name, ELF_VER_CHR); | |
4753 | if (p != NULL) | |
4754 | { | |
4755 | alc = bfd_malloc (p - name + 1); | |
4756 | memcpy (alc, name, p - name); | |
4757 | alc[p - name] = '\0'; | |
4758 | name = alc; | |
4759 | } | |
4760 | ||
4761 | /* Compute the hash value. */ | |
4762 | ha = bfd_elf_hash (name); | |
4763 | ||
4764 | /* Store the found hash value in the array given as the argument. */ | |
4765 | *(*valuep)++ = ha; | |
4766 | ||
4767 | /* And store it in the struct so that we can put it in the hash table | |
4768 | later. */ | |
f6e332e6 | 4769 | h->u.elf_hash_value = ha; |
5a580b3a AM |
4770 | |
4771 | if (alc != NULL) | |
4772 | free (alc); | |
4773 | ||
4774 | return TRUE; | |
4775 | } | |
4776 | ||
4777 | /* Array used to determine the number of hash table buckets to use | |
4778 | based on the number of symbols there are. If there are fewer than | |
4779 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
4780 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
4781 | than 32771 buckets. */ | |
4782 | ||
4783 | static const size_t elf_buckets[] = | |
4784 | { | |
4785 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
4786 | 16411, 32771, 0 | |
4787 | }; | |
4788 | ||
4789 | /* Compute bucket count for hashing table. We do not use a static set | |
4790 | of possible tables sizes anymore. Instead we determine for all | |
4791 | possible reasonable sizes of the table the outcome (i.e., the | |
4792 | number of collisions etc) and choose the best solution. The | |
4793 | weighting functions are not too simple to allow the table to grow | |
4794 | without bounds. Instead one of the weighting factors is the size. | |
4795 | Therefore the result is always a good payoff between few collisions | |
4796 | (= short chain lengths) and table size. */ | |
4797 | static size_t | |
4798 | compute_bucket_count (struct bfd_link_info *info) | |
4799 | { | |
4800 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; | |
4801 | size_t best_size = 0; | |
4802 | unsigned long int *hashcodes; | |
4803 | unsigned long int *hashcodesp; | |
4804 | unsigned long int i; | |
4805 | bfd_size_type amt; | |
4806 | ||
4807 | /* Compute the hash values for all exported symbols. At the same | |
4808 | time store the values in an array so that we could use them for | |
4809 | optimizations. */ | |
4810 | amt = dynsymcount; | |
4811 | amt *= sizeof (unsigned long int); | |
4812 | hashcodes = bfd_malloc (amt); | |
4813 | if (hashcodes == NULL) | |
4814 | return 0; | |
4815 | hashcodesp = hashcodes; | |
4816 | ||
4817 | /* Put all hash values in HASHCODES. */ | |
4818 | elf_link_hash_traverse (elf_hash_table (info), | |
4819 | elf_collect_hash_codes, &hashcodesp); | |
4820 | ||
4821 | /* We have a problem here. The following code to optimize the table | |
4822 | size requires an integer type with more the 32 bits. If | |
4823 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
4824 | #ifdef BFD_HOST_U_64_BIT | |
4825 | if (info->optimize) | |
4826 | { | |
4827 | unsigned long int nsyms = hashcodesp - hashcodes; | |
4828 | size_t minsize; | |
4829 | size_t maxsize; | |
4830 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
4831 | unsigned long int *counts ; | |
4832 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
4833 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); | |
4834 | ||
4835 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
4836 | that the hashing table must at least have NSYMS/4 and at most | |
4837 | 2*NSYMS buckets. */ | |
4838 | minsize = nsyms / 4; | |
4839 | if (minsize == 0) | |
4840 | minsize = 1; | |
4841 | best_size = maxsize = nsyms * 2; | |
4842 | ||
4843 | /* Create array where we count the collisions in. We must use bfd_malloc | |
4844 | since the size could be large. */ | |
4845 | amt = maxsize; | |
4846 | amt *= sizeof (unsigned long int); | |
4847 | counts = bfd_malloc (amt); | |
4848 | if (counts == NULL) | |
4849 | { | |
4850 | free (hashcodes); | |
4851 | return 0; | |
4852 | } | |
4853 | ||
4854 | /* Compute the "optimal" size for the hash table. The criteria is a | |
4855 | minimal chain length. The minor criteria is (of course) the size | |
4856 | of the table. */ | |
4857 | for (i = minsize; i < maxsize; ++i) | |
4858 | { | |
4859 | /* Walk through the array of hashcodes and count the collisions. */ | |
4860 | BFD_HOST_U_64_BIT max; | |
4861 | unsigned long int j; | |
4862 | unsigned long int fact; | |
4863 | ||
4864 | memset (counts, '\0', i * sizeof (unsigned long int)); | |
4865 | ||
4866 | /* Determine how often each hash bucket is used. */ | |
4867 | for (j = 0; j < nsyms; ++j) | |
4868 | ++counts[hashcodes[j] % i]; | |
4869 | ||
4870 | /* For the weight function we need some information about the | |
4871 | pagesize on the target. This is information need not be 100% | |
4872 | accurate. Since this information is not available (so far) we | |
4873 | define it here to a reasonable default value. If it is crucial | |
4874 | to have a better value some day simply define this value. */ | |
4875 | # ifndef BFD_TARGET_PAGESIZE | |
4876 | # define BFD_TARGET_PAGESIZE (4096) | |
4877 | # endif | |
4878 | ||
4879 | /* We in any case need 2 + NSYMS entries for the size values and | |
4880 | the chains. */ | |
4881 | max = (2 + nsyms) * (bed->s->arch_size / 8); | |
4882 | ||
4883 | # if 1 | |
4884 | /* Variant 1: optimize for short chains. We add the squares | |
4885 | of all the chain lengths (which favors many small chain | |
4886 | over a few long chains). */ | |
4887 | for (j = 0; j < i; ++j) | |
4888 | max += counts[j] * counts[j]; | |
4889 | ||
4890 | /* This adds penalties for the overall size of the table. */ | |
4891 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
4892 | max *= fact * fact; | |
4893 | # else | |
4894 | /* Variant 2: Optimize a lot more for small table. Here we | |
4895 | also add squares of the size but we also add penalties for | |
4896 | empty slots (the +1 term). */ | |
4897 | for (j = 0; j < i; ++j) | |
4898 | max += (1 + counts[j]) * (1 + counts[j]); | |
4899 | ||
4900 | /* The overall size of the table is considered, but not as | |
4901 | strong as in variant 1, where it is squared. */ | |
4902 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
4903 | max *= fact; | |
4904 | # endif | |
4905 | ||
4906 | /* Compare with current best results. */ | |
4907 | if (max < best_chlen) | |
4908 | { | |
4909 | best_chlen = max; | |
4910 | best_size = i; | |
4911 | } | |
4912 | } | |
4913 | ||
4914 | free (counts); | |
4915 | } | |
4916 | else | |
4917 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
4918 | { | |
4919 | /* This is the fallback solution if no 64bit type is available or if we | |
4920 | are not supposed to spend much time on optimizations. We select the | |
4921 | bucket count using a fixed set of numbers. */ | |
4922 | for (i = 0; elf_buckets[i] != 0; i++) | |
4923 | { | |
4924 | best_size = elf_buckets[i]; | |
4925 | if (dynsymcount < elf_buckets[i + 1]) | |
4926 | break; | |
4927 | } | |
4928 | } | |
4929 | ||
4930 | /* Free the arrays we needed. */ | |
4931 | free (hashcodes); | |
4932 | ||
4933 | return best_size; | |
4934 | } | |
4935 | ||
4936 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
4937 | called by the ELF linker emulation before_allocation routine. We | |
4938 | must set the sizes of the sections before the linker sets the | |
4939 | addresses of the various sections. */ | |
4940 | ||
4941 | bfd_boolean | |
4942 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
4943 | const char *soname, | |
4944 | const char *rpath, | |
4945 | const char *filter_shlib, | |
4946 | const char * const *auxiliary_filters, | |
4947 | struct bfd_link_info *info, | |
4948 | asection **sinterpptr, | |
4949 | struct bfd_elf_version_tree *verdefs) | |
4950 | { | |
4951 | bfd_size_type soname_indx; | |
4952 | bfd *dynobj; | |
4953 | const struct elf_backend_data *bed; | |
4954 | struct elf_assign_sym_version_info asvinfo; | |
4955 | ||
4956 | *sinterpptr = NULL; | |
4957 | ||
4958 | soname_indx = (bfd_size_type) -1; | |
4959 | ||
4960 | if (!is_elf_hash_table (info->hash)) | |
4961 | return TRUE; | |
4962 | ||
8c37241b | 4963 | elf_tdata (output_bfd)->relro = info->relro; |
5a580b3a AM |
4964 | if (info->execstack) |
4965 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
4966 | else if (info->noexecstack) | |
4967 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
4968 | else | |
4969 | { | |
4970 | bfd *inputobj; | |
4971 | asection *notesec = NULL; | |
4972 | int exec = 0; | |
4973 | ||
4974 | for (inputobj = info->input_bfds; | |
4975 | inputobj; | |
4976 | inputobj = inputobj->link_next) | |
4977 | { | |
4978 | asection *s; | |
4979 | ||
d457dcf6 | 4980 | if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED)) |
5a580b3a AM |
4981 | continue; |
4982 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
4983 | if (s) | |
4984 | { | |
4985 | if (s->flags & SEC_CODE) | |
4986 | exec = PF_X; | |
4987 | notesec = s; | |
4988 | } | |
4989 | else | |
4990 | exec = PF_X; | |
4991 | } | |
4992 | if (notesec) | |
4993 | { | |
4994 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
4995 | if (exec && info->relocatable | |
4996 | && notesec->output_section != bfd_abs_section_ptr) | |
4997 | notesec->output_section->flags |= SEC_CODE; | |
4998 | } | |
4999 | } | |
5000 | ||
5001 | /* Any syms created from now on start with -1 in | |
5002 | got.refcount/offset and plt.refcount/offset. */ | |
a6aa5195 AM |
5003 | elf_hash_table (info)->init_got_refcount |
5004 | = elf_hash_table (info)->init_got_offset; | |
5005 | elf_hash_table (info)->init_plt_refcount | |
5006 | = elf_hash_table (info)->init_plt_offset; | |
5a580b3a AM |
5007 | |
5008 | /* The backend may have to create some sections regardless of whether | |
5009 | we're dynamic or not. */ | |
5010 | bed = get_elf_backend_data (output_bfd); | |
5011 | if (bed->elf_backend_always_size_sections | |
5012 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
5013 | return FALSE; | |
5014 | ||
5015 | dynobj = elf_hash_table (info)->dynobj; | |
5016 | ||
5017 | /* If there were no dynamic objects in the link, there is nothing to | |
5018 | do here. */ | |
5019 | if (dynobj == NULL) | |
5020 | return TRUE; | |
5021 | ||
5022 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
5023 | return FALSE; | |
5024 | ||
5025 | if (elf_hash_table (info)->dynamic_sections_created) | |
5026 | { | |
5027 | struct elf_info_failed eif; | |
5028 | struct elf_link_hash_entry *h; | |
5029 | asection *dynstr; | |
5030 | struct bfd_elf_version_tree *t; | |
5031 | struct bfd_elf_version_expr *d; | |
046183de | 5032 | asection *s; |
5a580b3a AM |
5033 | bfd_boolean all_defined; |
5034 | ||
5035 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
5036 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
5037 | ||
5038 | if (soname != NULL) | |
5039 | { | |
5040 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5041 | soname, TRUE); | |
5042 | if (soname_indx == (bfd_size_type) -1 | |
5043 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
5044 | return FALSE; | |
5045 | } | |
5046 | ||
5047 | if (info->symbolic) | |
5048 | { | |
5049 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
5050 | return FALSE; | |
5051 | info->flags |= DF_SYMBOLIC; | |
5052 | } | |
5053 | ||
5054 | if (rpath != NULL) | |
5055 | { | |
5056 | bfd_size_type indx; | |
5057 | ||
5058 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
5059 | TRUE); | |
5060 | if (indx == (bfd_size_type) -1 | |
5061 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
5062 | return FALSE; | |
5063 | ||
5064 | if (info->new_dtags) | |
5065 | { | |
5066 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
5067 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
5068 | return FALSE; | |
5069 | } | |
5070 | } | |
5071 | ||
5072 | if (filter_shlib != NULL) | |
5073 | { | |
5074 | bfd_size_type indx; | |
5075 | ||
5076 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5077 | filter_shlib, TRUE); | |
5078 | if (indx == (bfd_size_type) -1 | |
5079 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
5080 | return FALSE; | |
5081 | } | |
5082 | ||
5083 | if (auxiliary_filters != NULL) | |
5084 | { | |
5085 | const char * const *p; | |
5086 | ||
5087 | for (p = auxiliary_filters; *p != NULL; p++) | |
5088 | { | |
5089 | bfd_size_type indx; | |
5090 | ||
5091 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5092 | *p, TRUE); | |
5093 | if (indx == (bfd_size_type) -1 | |
5094 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
5095 | return FALSE; | |
5096 | } | |
5097 | } | |
5098 | ||
5099 | eif.info = info; | |
5100 | eif.verdefs = verdefs; | |
5101 | eif.failed = FALSE; | |
5102 | ||
5103 | /* If we are supposed to export all symbols into the dynamic symbol | |
5104 | table (this is not the normal case), then do so. */ | |
5105 | if (info->export_dynamic) | |
5106 | { | |
5107 | elf_link_hash_traverse (elf_hash_table (info), | |
5108 | _bfd_elf_export_symbol, | |
5109 | &eif); | |
5110 | if (eif.failed) | |
5111 | return FALSE; | |
5112 | } | |
5113 | ||
5114 | /* Make all global versions with definition. */ | |
5115 | for (t = verdefs; t != NULL; t = t->next) | |
5116 | for (d = t->globals.list; d != NULL; d = d->next) | |
5117 | if (!d->symver && d->symbol) | |
5118 | { | |
5119 | const char *verstr, *name; | |
5120 | size_t namelen, verlen, newlen; | |
5121 | char *newname, *p; | |
5122 | struct elf_link_hash_entry *newh; | |
5123 | ||
5124 | name = d->symbol; | |
5125 | namelen = strlen (name); | |
5126 | verstr = t->name; | |
5127 | verlen = strlen (verstr); | |
5128 | newlen = namelen + verlen + 3; | |
5129 | ||
5130 | newname = bfd_malloc (newlen); | |
5131 | if (newname == NULL) | |
5132 | return FALSE; | |
5133 | memcpy (newname, name, namelen); | |
5134 | ||
5135 | /* Check the hidden versioned definition. */ | |
5136 | p = newname + namelen; | |
5137 | *p++ = ELF_VER_CHR; | |
5138 | memcpy (p, verstr, verlen + 1); | |
5139 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5140 | newname, FALSE, FALSE, | |
5141 | FALSE); | |
5142 | if (newh == NULL | |
5143 | || (newh->root.type != bfd_link_hash_defined | |
5144 | && newh->root.type != bfd_link_hash_defweak)) | |
5145 | { | |
5146 | /* Check the default versioned definition. */ | |
5147 | *p++ = ELF_VER_CHR; | |
5148 | memcpy (p, verstr, verlen + 1); | |
5149 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5150 | newname, FALSE, FALSE, | |
5151 | FALSE); | |
5152 | } | |
5153 | free (newname); | |
5154 | ||
5155 | /* Mark this version if there is a definition and it is | |
5156 | not defined in a shared object. */ | |
5157 | if (newh != NULL | |
f5385ebf | 5158 | && !newh->def_dynamic |
5a580b3a AM |
5159 | && (newh->root.type == bfd_link_hash_defined |
5160 | || newh->root.type == bfd_link_hash_defweak)) | |
5161 | d->symver = 1; | |
5162 | } | |
5163 | ||
5164 | /* Attach all the symbols to their version information. */ | |
5165 | asvinfo.output_bfd = output_bfd; | |
5166 | asvinfo.info = info; | |
5167 | asvinfo.verdefs = verdefs; | |
5168 | asvinfo.failed = FALSE; | |
5169 | ||
5170 | elf_link_hash_traverse (elf_hash_table (info), | |
5171 | _bfd_elf_link_assign_sym_version, | |
5172 | &asvinfo); | |
5173 | if (asvinfo.failed) | |
5174 | return FALSE; | |
5175 | ||
5176 | if (!info->allow_undefined_version) | |
5177 | { | |
5178 | /* Check if all global versions have a definition. */ | |
5179 | all_defined = TRUE; | |
5180 | for (t = verdefs; t != NULL; t = t->next) | |
5181 | for (d = t->globals.list; d != NULL; d = d->next) | |
5182 | if (!d->symver && !d->script) | |
5183 | { | |
5184 | (*_bfd_error_handler) | |
5185 | (_("%s: undefined version: %s"), | |
5186 | d->pattern, t->name); | |
5187 | all_defined = FALSE; | |
5188 | } | |
5189 | ||
5190 | if (!all_defined) | |
5191 | { | |
5192 | bfd_set_error (bfd_error_bad_value); | |
5193 | return FALSE; | |
5194 | } | |
5195 | } | |
5196 | ||
5197 | /* Find all symbols which were defined in a dynamic object and make | |
5198 | the backend pick a reasonable value for them. */ | |
5199 | elf_link_hash_traverse (elf_hash_table (info), | |
5200 | _bfd_elf_adjust_dynamic_symbol, | |
5201 | &eif); | |
5202 | if (eif.failed) | |
5203 | return FALSE; | |
5204 | ||
5205 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 5206 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
5207 | now so that we know the final size of the .dynamic section. */ |
5208 | ||
5209 | /* If there are initialization and/or finalization functions to | |
5210 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
5211 | h = (info->init_function | |
5212 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5213 | info->init_function, FALSE, | |
5214 | FALSE, FALSE) | |
5215 | : NULL); | |
5216 | if (h != NULL | |
f5385ebf AM |
5217 | && (h->ref_regular |
5218 | || h->def_regular)) | |
5a580b3a AM |
5219 | { |
5220 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
5221 | return FALSE; | |
5222 | } | |
5223 | h = (info->fini_function | |
5224 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5225 | info->fini_function, FALSE, | |
5226 | FALSE, FALSE) | |
5227 | : NULL); | |
5228 | if (h != NULL | |
f5385ebf AM |
5229 | && (h->ref_regular |
5230 | || h->def_regular)) | |
5a580b3a AM |
5231 | { |
5232 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
5233 | return FALSE; | |
5234 | } | |
5235 | ||
046183de AM |
5236 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); |
5237 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5238 | { |
5239 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
5240 | if (! info->executable) | |
5241 | { | |
5242 | bfd *sub; | |
5243 | asection *o; | |
5244 | ||
5245 | for (sub = info->input_bfds; sub != NULL; | |
5246 | sub = sub->link_next) | |
5247 | for (o = sub->sections; o != NULL; o = o->next) | |
5248 | if (elf_section_data (o)->this_hdr.sh_type | |
5249 | == SHT_PREINIT_ARRAY) | |
5250 | { | |
5251 | (*_bfd_error_handler) | |
d003868e AM |
5252 | (_("%B: .preinit_array section is not allowed in DSO"), |
5253 | sub); | |
5a580b3a AM |
5254 | break; |
5255 | } | |
5256 | ||
5257 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5258 | return FALSE; | |
5259 | } | |
5260 | ||
5261 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
5262 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
5263 | return FALSE; | |
5264 | } | |
046183de AM |
5265 | s = bfd_get_section_by_name (output_bfd, ".init_array"); |
5266 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5267 | { |
5268 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
5269 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
5270 | return FALSE; | |
5271 | } | |
046183de AM |
5272 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); |
5273 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5274 | { |
5275 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
5276 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
5277 | return FALSE; | |
5278 | } | |
5279 | ||
5280 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5281 | /* If .dynstr is excluded from the link, we don't want any of | |
5282 | these tags. Strictly, we should be checking each section | |
5283 | individually; This quick check covers for the case where | |
5284 | someone does a /DISCARD/ : { *(*) }. */ | |
5285 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
5286 | { | |
5287 | bfd_size_type strsize; | |
5288 | ||
5289 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
5290 | if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0) | |
5291 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
5292 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
5293 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
5294 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
5295 | bed->s->sizeof_sym)) | |
5296 | return FALSE; | |
5297 | } | |
5298 | } | |
5299 | ||
5300 | /* The backend must work out the sizes of all the other dynamic | |
5301 | sections. */ | |
5302 | if (bed->elf_backend_size_dynamic_sections | |
5303 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
5304 | return FALSE; | |
5305 | ||
5306 | if (elf_hash_table (info)->dynamic_sections_created) | |
5307 | { | |
554220db | 5308 | unsigned long section_sym_count; |
5a580b3a | 5309 | asection *s; |
5a580b3a AM |
5310 | |
5311 | /* Set up the version definition section. */ | |
5312 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
5313 | BFD_ASSERT (s != NULL); | |
5314 | ||
5315 | /* We may have created additional version definitions if we are | |
5316 | just linking a regular application. */ | |
5317 | verdefs = asvinfo.verdefs; | |
5318 | ||
5319 | /* Skip anonymous version tag. */ | |
5320 | if (verdefs != NULL && verdefs->vernum == 0) | |
5321 | verdefs = verdefs->next; | |
5322 | ||
3e3b46e5 | 5323 | if (verdefs == NULL && !info->create_default_symver) |
8423293d | 5324 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5325 | else |
5326 | { | |
5327 | unsigned int cdefs; | |
5328 | bfd_size_type size; | |
5329 | struct bfd_elf_version_tree *t; | |
5330 | bfd_byte *p; | |
5331 | Elf_Internal_Verdef def; | |
5332 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
5333 | struct bfd_link_hash_entry *bh; |
5334 | struct elf_link_hash_entry *h; | |
5335 | const char *name; | |
5a580b3a AM |
5336 | |
5337 | cdefs = 0; | |
5338 | size = 0; | |
5339 | ||
5340 | /* Make space for the base version. */ | |
5341 | size += sizeof (Elf_External_Verdef); | |
5342 | size += sizeof (Elf_External_Verdaux); | |
5343 | ++cdefs; | |
5344 | ||
3e3b46e5 PB |
5345 | /* Make space for the default version. */ |
5346 | if (info->create_default_symver) | |
5347 | { | |
5348 | size += sizeof (Elf_External_Verdef); | |
5349 | ++cdefs; | |
5350 | } | |
5351 | ||
5a580b3a AM |
5352 | for (t = verdefs; t != NULL; t = t->next) |
5353 | { | |
5354 | struct bfd_elf_version_deps *n; | |
5355 | ||
5356 | size += sizeof (Elf_External_Verdef); | |
5357 | size += sizeof (Elf_External_Verdaux); | |
5358 | ++cdefs; | |
5359 | ||
5360 | for (n = t->deps; n != NULL; n = n->next) | |
5361 | size += sizeof (Elf_External_Verdaux); | |
5362 | } | |
5363 | ||
eea6121a AM |
5364 | s->size = size; |
5365 | s->contents = bfd_alloc (output_bfd, s->size); | |
5366 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5367 | return FALSE; |
5368 | ||
5369 | /* Fill in the version definition section. */ | |
5370 | ||
5371 | p = s->contents; | |
5372 | ||
5373 | def.vd_version = VER_DEF_CURRENT; | |
5374 | def.vd_flags = VER_FLG_BASE; | |
5375 | def.vd_ndx = 1; | |
5376 | def.vd_cnt = 1; | |
3e3b46e5 PB |
5377 | if (info->create_default_symver) |
5378 | { | |
5379 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
5380 | def.vd_next = sizeof (Elf_External_Verdef); | |
5381 | } | |
5382 | else | |
5383 | { | |
5384 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5385 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5386 | + sizeof (Elf_External_Verdaux)); | |
5387 | } | |
5a580b3a AM |
5388 | |
5389 | if (soname_indx != (bfd_size_type) -1) | |
5390 | { | |
5391 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5392 | soname_indx); | |
5393 | def.vd_hash = bfd_elf_hash (soname); | |
5394 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 5395 | name = soname; |
5a580b3a AM |
5396 | } |
5397 | else | |
5398 | { | |
5a580b3a AM |
5399 | bfd_size_type indx; |
5400 | ||
06084812 | 5401 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
5402 | def.vd_hash = bfd_elf_hash (name); |
5403 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5404 | name, FALSE); | |
5405 | if (indx == (bfd_size_type) -1) | |
5406 | return FALSE; | |
5407 | defaux.vda_name = indx; | |
5408 | } | |
5409 | defaux.vda_next = 0; | |
5410 | ||
5411 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5412 | (Elf_External_Verdef *) p); | |
5413 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
5414 | if (info->create_default_symver) |
5415 | { | |
5416 | /* Add a symbol representing this version. */ | |
5417 | bh = NULL; | |
5418 | if (! (_bfd_generic_link_add_one_symbol | |
5419 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5420 | 0, NULL, FALSE, | |
5421 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5422 | return FALSE; | |
5423 | h = (struct elf_link_hash_entry *) bh; | |
5424 | h->non_elf = 0; | |
5425 | h->def_regular = 1; | |
5426 | h->type = STT_OBJECT; | |
5427 | h->verinfo.vertree = NULL; | |
5428 | ||
5429 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
5430 | return FALSE; | |
5431 | ||
5432 | /* Create a duplicate of the base version with the same | |
5433 | aux block, but different flags. */ | |
5434 | def.vd_flags = 0; | |
5435 | def.vd_ndx = 2; | |
5436 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5437 | if (verdefs) | |
5438 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5439 | + sizeof (Elf_External_Verdaux)); | |
5440 | else | |
5441 | def.vd_next = 0; | |
5442 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5443 | (Elf_External_Verdef *) p); | |
5444 | p += sizeof (Elf_External_Verdef); | |
5445 | } | |
5a580b3a AM |
5446 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
5447 | (Elf_External_Verdaux *) p); | |
5448 | p += sizeof (Elf_External_Verdaux); | |
5449 | ||
5450 | for (t = verdefs; t != NULL; t = t->next) | |
5451 | { | |
5452 | unsigned int cdeps; | |
5453 | struct bfd_elf_version_deps *n; | |
5a580b3a AM |
5454 | |
5455 | cdeps = 0; | |
5456 | for (n = t->deps; n != NULL; n = n->next) | |
5457 | ++cdeps; | |
5458 | ||
5459 | /* Add a symbol representing this version. */ | |
5460 | bh = NULL; | |
5461 | if (! (_bfd_generic_link_add_one_symbol | |
5462 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5463 | 0, NULL, FALSE, | |
5464 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5465 | return FALSE; | |
5466 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
5467 | h->non_elf = 0; |
5468 | h->def_regular = 1; | |
5a580b3a AM |
5469 | h->type = STT_OBJECT; |
5470 | h->verinfo.vertree = t; | |
5471 | ||
c152c796 | 5472 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
5473 | return FALSE; |
5474 | ||
5475 | def.vd_version = VER_DEF_CURRENT; | |
5476 | def.vd_flags = 0; | |
5477 | if (t->globals.list == NULL | |
5478 | && t->locals.list == NULL | |
5479 | && ! t->used) | |
5480 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 5481 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
5482 | def.vd_cnt = cdeps + 1; |
5483 | def.vd_hash = bfd_elf_hash (t->name); | |
5484 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5485 | def.vd_next = 0; | |
5486 | if (t->next != NULL) | |
5487 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5488 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
5489 | ||
5490 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5491 | (Elf_External_Verdef *) p); | |
5492 | p += sizeof (Elf_External_Verdef); | |
5493 | ||
5494 | defaux.vda_name = h->dynstr_index; | |
5495 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5496 | h->dynstr_index); | |
5497 | defaux.vda_next = 0; | |
5498 | if (t->deps != NULL) | |
5499 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5500 | t->name_indx = defaux.vda_name; | |
5501 | ||
5502 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5503 | (Elf_External_Verdaux *) p); | |
5504 | p += sizeof (Elf_External_Verdaux); | |
5505 | ||
5506 | for (n = t->deps; n != NULL; n = n->next) | |
5507 | { | |
5508 | if (n->version_needed == NULL) | |
5509 | { | |
5510 | /* This can happen if there was an error in the | |
5511 | version script. */ | |
5512 | defaux.vda_name = 0; | |
5513 | } | |
5514 | else | |
5515 | { | |
5516 | defaux.vda_name = n->version_needed->name_indx; | |
5517 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5518 | defaux.vda_name); | |
5519 | } | |
5520 | if (n->next == NULL) | |
5521 | defaux.vda_next = 0; | |
5522 | else | |
5523 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5524 | ||
5525 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5526 | (Elf_External_Verdaux *) p); | |
5527 | p += sizeof (Elf_External_Verdaux); | |
5528 | } | |
5529 | } | |
5530 | ||
5531 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
5532 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
5533 | return FALSE; | |
5534 | ||
5535 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
5536 | } | |
5537 | ||
5538 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
5539 | { | |
5540 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
5541 | return FALSE; | |
5542 | } | |
5543 | else if (info->flags & DF_BIND_NOW) | |
5544 | { | |
5545 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
5546 | return FALSE; | |
5547 | } | |
5548 | ||
5549 | if (info->flags_1) | |
5550 | { | |
5551 | if (info->executable) | |
5552 | info->flags_1 &= ~ (DF_1_INITFIRST | |
5553 | | DF_1_NODELETE | |
5554 | | DF_1_NOOPEN); | |
5555 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
5556 | return FALSE; | |
5557 | } | |
5558 | ||
5559 | /* Work out the size of the version reference section. */ | |
5560 | ||
5561 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
5562 | BFD_ASSERT (s != NULL); | |
5563 | { | |
5564 | struct elf_find_verdep_info sinfo; | |
5565 | ||
5566 | sinfo.output_bfd = output_bfd; | |
5567 | sinfo.info = info; | |
5568 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
5569 | if (sinfo.vers == 0) | |
5570 | sinfo.vers = 1; | |
5571 | sinfo.failed = FALSE; | |
5572 | ||
5573 | elf_link_hash_traverse (elf_hash_table (info), | |
5574 | _bfd_elf_link_find_version_dependencies, | |
5575 | &sinfo); | |
5576 | ||
5577 | if (elf_tdata (output_bfd)->verref == NULL) | |
8423293d | 5578 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5579 | else |
5580 | { | |
5581 | Elf_Internal_Verneed *t; | |
5582 | unsigned int size; | |
5583 | unsigned int crefs; | |
5584 | bfd_byte *p; | |
5585 | ||
5586 | /* Build the version definition section. */ | |
5587 | size = 0; | |
5588 | crefs = 0; | |
5589 | for (t = elf_tdata (output_bfd)->verref; | |
5590 | t != NULL; | |
5591 | t = t->vn_nextref) | |
5592 | { | |
5593 | Elf_Internal_Vernaux *a; | |
5594 | ||
5595 | size += sizeof (Elf_External_Verneed); | |
5596 | ++crefs; | |
5597 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5598 | size += sizeof (Elf_External_Vernaux); | |
5599 | } | |
5600 | ||
eea6121a AM |
5601 | s->size = size; |
5602 | s->contents = bfd_alloc (output_bfd, s->size); | |
5a580b3a AM |
5603 | if (s->contents == NULL) |
5604 | return FALSE; | |
5605 | ||
5606 | p = s->contents; | |
5607 | for (t = elf_tdata (output_bfd)->verref; | |
5608 | t != NULL; | |
5609 | t = t->vn_nextref) | |
5610 | { | |
5611 | unsigned int caux; | |
5612 | Elf_Internal_Vernaux *a; | |
5613 | bfd_size_type indx; | |
5614 | ||
5615 | caux = 0; | |
5616 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5617 | ++caux; | |
5618 | ||
5619 | t->vn_version = VER_NEED_CURRENT; | |
5620 | t->vn_cnt = caux; | |
5621 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5622 | elf_dt_name (t->vn_bfd) != NULL | |
5623 | ? elf_dt_name (t->vn_bfd) | |
06084812 | 5624 | : lbasename (t->vn_bfd->filename), |
5a580b3a AM |
5625 | FALSE); |
5626 | if (indx == (bfd_size_type) -1) | |
5627 | return FALSE; | |
5628 | t->vn_file = indx; | |
5629 | t->vn_aux = sizeof (Elf_External_Verneed); | |
5630 | if (t->vn_nextref == NULL) | |
5631 | t->vn_next = 0; | |
5632 | else | |
5633 | t->vn_next = (sizeof (Elf_External_Verneed) | |
5634 | + caux * sizeof (Elf_External_Vernaux)); | |
5635 | ||
5636 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
5637 | (Elf_External_Verneed *) p); | |
5638 | p += sizeof (Elf_External_Verneed); | |
5639 | ||
5640 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5641 | { | |
5642 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
5643 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5644 | a->vna_nodename, FALSE); | |
5645 | if (indx == (bfd_size_type) -1) | |
5646 | return FALSE; | |
5647 | a->vna_name = indx; | |
5648 | if (a->vna_nextptr == NULL) | |
5649 | a->vna_next = 0; | |
5650 | else | |
5651 | a->vna_next = sizeof (Elf_External_Vernaux); | |
5652 | ||
5653 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
5654 | (Elf_External_Vernaux *) p); | |
5655 | p += sizeof (Elf_External_Vernaux); | |
5656 | } | |
5657 | } | |
5658 | ||
5659 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
5660 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
5661 | return FALSE; | |
5662 | ||
5663 | elf_tdata (output_bfd)->cverrefs = crefs; | |
5664 | } | |
5665 | } | |
5666 | ||
8423293d AM |
5667 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
5668 | && elf_tdata (output_bfd)->cverdefs == 0) | |
5669 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
5670 | §ion_sym_count) == 0) | |
5671 | { | |
5672 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
5673 | s->flags |= SEC_EXCLUDE; | |
5674 | } | |
5675 | } | |
5676 | return TRUE; | |
5677 | } | |
5678 | ||
5679 | bfd_boolean | |
5680 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5681 | { | |
5682 | if (!is_elf_hash_table (info->hash)) | |
5683 | return TRUE; | |
5684 | ||
5685 | if (elf_hash_table (info)->dynamic_sections_created) | |
5686 | { | |
5687 | bfd *dynobj; | |
5688 | const struct elf_backend_data *bed; | |
5689 | asection *s; | |
5690 | bfd_size_type dynsymcount; | |
5691 | unsigned long section_sym_count; | |
5692 | size_t bucketcount = 0; | |
5693 | size_t hash_entry_size; | |
5694 | unsigned int dtagcount; | |
5695 | ||
5696 | dynobj = elf_hash_table (info)->dynobj; | |
5697 | ||
5a580b3a AM |
5698 | /* Assign dynsym indicies. In a shared library we generate a |
5699 | section symbol for each output section, which come first. | |
5700 | Next come all of the back-end allocated local dynamic syms, | |
5701 | followed by the rest of the global symbols. */ | |
5702 | ||
554220db AM |
5703 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, |
5704 | §ion_sym_count); | |
5a580b3a AM |
5705 | |
5706 | /* Work out the size of the symbol version section. */ | |
5707 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
5708 | BFD_ASSERT (s != NULL); | |
8423293d AM |
5709 | if (dynsymcount != 0 |
5710 | && (s->flags & SEC_EXCLUDE) == 0) | |
5a580b3a | 5711 | { |
eea6121a AM |
5712 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
5713 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
5714 | if (s->contents == NULL) |
5715 | return FALSE; | |
5716 | ||
5717 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
5718 | return FALSE; | |
5719 | } | |
5720 | ||
5721 | /* Set the size of the .dynsym and .hash sections. We counted | |
5722 | the number of dynamic symbols in elf_link_add_object_symbols. | |
5723 | We will build the contents of .dynsym and .hash when we build | |
5724 | the final symbol table, because until then we do not know the | |
5725 | correct value to give the symbols. We built the .dynstr | |
5726 | section as we went along in elf_link_add_object_symbols. */ | |
5727 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
5728 | BFD_ASSERT (s != NULL); | |
8423293d | 5729 | bed = get_elf_backend_data (output_bfd); |
eea6121a | 5730 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a AM |
5731 | |
5732 | if (dynsymcount != 0) | |
5733 | { | |
554220db AM |
5734 | s->contents = bfd_alloc (output_bfd, s->size); |
5735 | if (s->contents == NULL) | |
5736 | return FALSE; | |
5a580b3a | 5737 | |
554220db AM |
5738 | /* The first entry in .dynsym is a dummy symbol. |
5739 | Clear all the section syms, in case we don't output them all. */ | |
5740 | ++section_sym_count; | |
5741 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a AM |
5742 | } |
5743 | ||
5744 | /* Compute the size of the hashing table. As a side effect this | |
5745 | computes the hash values for all the names we export. */ | |
5746 | bucketcount = compute_bucket_count (info); | |
5747 | ||
5748 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
5749 | BFD_ASSERT (s != NULL); | |
5750 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
eea6121a AM |
5751 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); |
5752 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
5753 | if (s->contents == NULL) |
5754 | return FALSE; | |
5755 | ||
5756 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
5757 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
5758 | s->contents + hash_entry_size); | |
5759 | ||
5760 | elf_hash_table (info)->bucketcount = bucketcount; | |
5761 | ||
5762 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5763 | BFD_ASSERT (s != NULL); | |
5764 | ||
4ad4eba5 | 5765 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 5766 | |
eea6121a | 5767 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
5768 | |
5769 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
5770 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
5771 | return FALSE; | |
5772 | } | |
5773 | ||
5774 | return TRUE; | |
5775 | } | |
c152c796 AM |
5776 | |
5777 | /* Final phase of ELF linker. */ | |
5778 | ||
5779 | /* A structure we use to avoid passing large numbers of arguments. */ | |
5780 | ||
5781 | struct elf_final_link_info | |
5782 | { | |
5783 | /* General link information. */ | |
5784 | struct bfd_link_info *info; | |
5785 | /* Output BFD. */ | |
5786 | bfd *output_bfd; | |
5787 | /* Symbol string table. */ | |
5788 | struct bfd_strtab_hash *symstrtab; | |
5789 | /* .dynsym section. */ | |
5790 | asection *dynsym_sec; | |
5791 | /* .hash section. */ | |
5792 | asection *hash_sec; | |
5793 | /* symbol version section (.gnu.version). */ | |
5794 | asection *symver_sec; | |
5795 | /* Buffer large enough to hold contents of any section. */ | |
5796 | bfd_byte *contents; | |
5797 | /* Buffer large enough to hold external relocs of any section. */ | |
5798 | void *external_relocs; | |
5799 | /* Buffer large enough to hold internal relocs of any section. */ | |
5800 | Elf_Internal_Rela *internal_relocs; | |
5801 | /* Buffer large enough to hold external local symbols of any input | |
5802 | BFD. */ | |
5803 | bfd_byte *external_syms; | |
5804 | /* And a buffer for symbol section indices. */ | |
5805 | Elf_External_Sym_Shndx *locsym_shndx; | |
5806 | /* Buffer large enough to hold internal local symbols of any input | |
5807 | BFD. */ | |
5808 | Elf_Internal_Sym *internal_syms; | |
5809 | /* Array large enough to hold a symbol index for each local symbol | |
5810 | of any input BFD. */ | |
5811 | long *indices; | |
5812 | /* Array large enough to hold a section pointer for each local | |
5813 | symbol of any input BFD. */ | |
5814 | asection **sections; | |
5815 | /* Buffer to hold swapped out symbols. */ | |
5816 | bfd_byte *symbuf; | |
5817 | /* And one for symbol section indices. */ | |
5818 | Elf_External_Sym_Shndx *symshndxbuf; | |
5819 | /* Number of swapped out symbols in buffer. */ | |
5820 | size_t symbuf_count; | |
5821 | /* Number of symbols which fit in symbuf. */ | |
5822 | size_t symbuf_size; | |
5823 | /* And same for symshndxbuf. */ | |
5824 | size_t shndxbuf_size; | |
5825 | }; | |
5826 | ||
5827 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
5828 | ||
5829 | struct elf_outext_info | |
5830 | { | |
5831 | bfd_boolean failed; | |
5832 | bfd_boolean localsyms; | |
5833 | struct elf_final_link_info *finfo; | |
5834 | }; | |
5835 | ||
5836 | /* When performing a relocatable link, the input relocations are | |
5837 | preserved. But, if they reference global symbols, the indices | |
5838 | referenced must be updated. Update all the relocations in | |
5839 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ | |
5840 | ||
5841 | static void | |
5842 | elf_link_adjust_relocs (bfd *abfd, | |
5843 | Elf_Internal_Shdr *rel_hdr, | |
5844 | unsigned int count, | |
5845 | struct elf_link_hash_entry **rel_hash) | |
5846 | { | |
5847 | unsigned int i; | |
5848 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5849 | bfd_byte *erela; | |
5850 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
5851 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
5852 | bfd_vma r_type_mask; | |
5853 | int r_sym_shift; | |
5854 | ||
5855 | if (rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
5856 | { | |
5857 | swap_in = bed->s->swap_reloc_in; | |
5858 | swap_out = bed->s->swap_reloc_out; | |
5859 | } | |
5860 | else if (rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
5861 | { | |
5862 | swap_in = bed->s->swap_reloca_in; | |
5863 | swap_out = bed->s->swap_reloca_out; | |
5864 | } | |
5865 | else | |
5866 | abort (); | |
5867 | ||
5868 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
5869 | abort (); | |
5870 | ||
5871 | if (bed->s->arch_size == 32) | |
5872 | { | |
5873 | r_type_mask = 0xff; | |
5874 | r_sym_shift = 8; | |
5875 | } | |
5876 | else | |
5877 | { | |
5878 | r_type_mask = 0xffffffff; | |
5879 | r_sym_shift = 32; | |
5880 | } | |
5881 | ||
5882 | erela = rel_hdr->contents; | |
5883 | for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) | |
5884 | { | |
5885 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
5886 | unsigned int j; | |
5887 | ||
5888 | if (*rel_hash == NULL) | |
5889 | continue; | |
5890 | ||
5891 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
5892 | ||
5893 | (*swap_in) (abfd, erela, irela); | |
5894 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
5895 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
5896 | | (irela[j].r_info & r_type_mask)); | |
5897 | (*swap_out) (abfd, irela, erela); | |
5898 | } | |
5899 | } | |
5900 | ||
5901 | struct elf_link_sort_rela | |
5902 | { | |
5903 | union { | |
5904 | bfd_vma offset; | |
5905 | bfd_vma sym_mask; | |
5906 | } u; | |
5907 | enum elf_reloc_type_class type; | |
5908 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
5909 | Elf_Internal_Rela rela[1]; | |
5910 | }; | |
5911 | ||
5912 | static int | |
5913 | elf_link_sort_cmp1 (const void *A, const void *B) | |
5914 | { | |
5915 | const struct elf_link_sort_rela *a = A; | |
5916 | const struct elf_link_sort_rela *b = B; | |
5917 | int relativea, relativeb; | |
5918 | ||
5919 | relativea = a->type == reloc_class_relative; | |
5920 | relativeb = b->type == reloc_class_relative; | |
5921 | ||
5922 | if (relativea < relativeb) | |
5923 | return 1; | |
5924 | if (relativea > relativeb) | |
5925 | return -1; | |
5926 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
5927 | return -1; | |
5928 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
5929 | return 1; | |
5930 | if (a->rela->r_offset < b->rela->r_offset) | |
5931 | return -1; | |
5932 | if (a->rela->r_offset > b->rela->r_offset) | |
5933 | return 1; | |
5934 | return 0; | |
5935 | } | |
5936 | ||
5937 | static int | |
5938 | elf_link_sort_cmp2 (const void *A, const void *B) | |
5939 | { | |
5940 | const struct elf_link_sort_rela *a = A; | |
5941 | const struct elf_link_sort_rela *b = B; | |
5942 | int copya, copyb; | |
5943 | ||
5944 | if (a->u.offset < b->u.offset) | |
5945 | return -1; | |
5946 | if (a->u.offset > b->u.offset) | |
5947 | return 1; | |
5948 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); | |
5949 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
5950 | if (copya < copyb) | |
5951 | return -1; | |
5952 | if (copya > copyb) | |
5953 | return 1; | |
5954 | if (a->rela->r_offset < b->rela->r_offset) | |
5955 | return -1; | |
5956 | if (a->rela->r_offset > b->rela->r_offset) | |
5957 | return 1; | |
5958 | return 0; | |
5959 | } | |
5960 | ||
5961 | static size_t | |
5962 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
5963 | { | |
5964 | asection *reldyn; | |
5965 | bfd_size_type count, size; | |
5966 | size_t i, ret, sort_elt, ext_size; | |
5967 | bfd_byte *sort, *s_non_relative, *p; | |
5968 | struct elf_link_sort_rela *sq; | |
5969 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5970 | int i2e = bed->s->int_rels_per_ext_rel; | |
5971 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
5972 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
5973 | struct bfd_link_order *lo; | |
5974 | bfd_vma r_sym_mask; | |
5975 | ||
5976 | reldyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
eea6121a | 5977 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
5978 | { |
5979 | reldyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
eea6121a | 5980 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
5981 | return 0; |
5982 | ext_size = bed->s->sizeof_rel; | |
5983 | swap_in = bed->s->swap_reloc_in; | |
5984 | swap_out = bed->s->swap_reloc_out; | |
5985 | } | |
5986 | else | |
5987 | { | |
5988 | ext_size = bed->s->sizeof_rela; | |
5989 | swap_in = bed->s->swap_reloca_in; | |
5990 | swap_out = bed->s->swap_reloca_out; | |
5991 | } | |
eea6121a | 5992 | count = reldyn->size / ext_size; |
c152c796 AM |
5993 | |
5994 | size = 0; | |
8423293d | 5995 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
5996 | if (lo->type == bfd_indirect_link_order) |
5997 | { | |
5998 | asection *o = lo->u.indirect.section; | |
eea6121a | 5999 | size += o->size; |
c152c796 AM |
6000 | } |
6001 | ||
eea6121a | 6002 | if (size != reldyn->size) |
c152c796 AM |
6003 | return 0; |
6004 | ||
6005 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
6006 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
6007 | sort = bfd_zmalloc (sort_elt * count); | |
6008 | if (sort == NULL) | |
6009 | { | |
6010 | (*info->callbacks->warning) | |
6011 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
6012 | return 0; | |
6013 | } | |
6014 | ||
6015 | if (bed->s->arch_size == 32) | |
6016 | r_sym_mask = ~(bfd_vma) 0xff; | |
6017 | else | |
6018 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
6019 | ||
8423293d | 6020 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
6021 | if (lo->type == bfd_indirect_link_order) |
6022 | { | |
6023 | bfd_byte *erel, *erelend; | |
6024 | asection *o = lo->u.indirect.section; | |
6025 | ||
1da212d6 AM |
6026 | if (o->contents == NULL && o->size != 0) |
6027 | { | |
6028 | /* This is a reloc section that is being handled as a normal | |
6029 | section. See bfd_section_from_shdr. We can't combine | |
6030 | relocs in this case. */ | |
6031 | free (sort); | |
6032 | return 0; | |
6033 | } | |
c152c796 | 6034 | erel = o->contents; |
eea6121a | 6035 | erelend = o->contents + o->size; |
c152c796 AM |
6036 | p = sort + o->output_offset / ext_size * sort_elt; |
6037 | while (erel < erelend) | |
6038 | { | |
6039 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6040 | (*swap_in) (abfd, erel, s->rela); | |
6041 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
6042 | s->u.sym_mask = r_sym_mask; | |
6043 | p += sort_elt; | |
6044 | erel += ext_size; | |
6045 | } | |
6046 | } | |
6047 | ||
6048 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
6049 | ||
6050 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
6051 | { | |
6052 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6053 | if (s->type != reloc_class_relative) | |
6054 | break; | |
6055 | } | |
6056 | ret = i; | |
6057 | s_non_relative = p; | |
6058 | ||
6059 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
6060 | for (; i < count; i++, p += sort_elt) | |
6061 | { | |
6062 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
6063 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
6064 | sq = sp; | |
6065 | sp->u.offset = sq->rela->r_offset; | |
6066 | } | |
6067 | ||
6068 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
6069 | ||
8423293d | 6070 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
6071 | if (lo->type == bfd_indirect_link_order) |
6072 | { | |
6073 | bfd_byte *erel, *erelend; | |
6074 | asection *o = lo->u.indirect.section; | |
6075 | ||
6076 | erel = o->contents; | |
eea6121a | 6077 | erelend = o->contents + o->size; |
c152c796 AM |
6078 | p = sort + o->output_offset / ext_size * sort_elt; |
6079 | while (erel < erelend) | |
6080 | { | |
6081 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6082 | (*swap_out) (abfd, s->rela, erel); | |
6083 | p += sort_elt; | |
6084 | erel += ext_size; | |
6085 | } | |
6086 | } | |
6087 | ||
6088 | free (sort); | |
6089 | *psec = reldyn; | |
6090 | return ret; | |
6091 | } | |
6092 | ||
6093 | /* Flush the output symbols to the file. */ | |
6094 | ||
6095 | static bfd_boolean | |
6096 | elf_link_flush_output_syms (struct elf_final_link_info *finfo, | |
6097 | const struct elf_backend_data *bed) | |
6098 | { | |
6099 | if (finfo->symbuf_count > 0) | |
6100 | { | |
6101 | Elf_Internal_Shdr *hdr; | |
6102 | file_ptr pos; | |
6103 | bfd_size_type amt; | |
6104 | ||
6105 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
6106 | pos = hdr->sh_offset + hdr->sh_size; | |
6107 | amt = finfo->symbuf_count * bed->s->sizeof_sym; | |
6108 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
6109 | || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) | |
6110 | return FALSE; | |
6111 | ||
6112 | hdr->sh_size += amt; | |
6113 | finfo->symbuf_count = 0; | |
6114 | } | |
6115 | ||
6116 | return TRUE; | |
6117 | } | |
6118 | ||
6119 | /* Add a symbol to the output symbol table. */ | |
6120 | ||
6121 | static bfd_boolean | |
6122 | elf_link_output_sym (struct elf_final_link_info *finfo, | |
6123 | const char *name, | |
6124 | Elf_Internal_Sym *elfsym, | |
6125 | asection *input_sec, | |
6126 | struct elf_link_hash_entry *h) | |
6127 | { | |
6128 | bfd_byte *dest; | |
6129 | Elf_External_Sym_Shndx *destshndx; | |
6130 | bfd_boolean (*output_symbol_hook) | |
6131 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, | |
6132 | struct elf_link_hash_entry *); | |
6133 | const struct elf_backend_data *bed; | |
6134 | ||
6135 | bed = get_elf_backend_data (finfo->output_bfd); | |
6136 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; | |
6137 | if (output_symbol_hook != NULL) | |
6138 | { | |
6139 | if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h)) | |
6140 | return FALSE; | |
6141 | } | |
6142 | ||
6143 | if (name == NULL || *name == '\0') | |
6144 | elfsym->st_name = 0; | |
6145 | else if (input_sec->flags & SEC_EXCLUDE) | |
6146 | elfsym->st_name = 0; | |
6147 | else | |
6148 | { | |
6149 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
6150 | name, TRUE, FALSE); | |
6151 | if (elfsym->st_name == (unsigned long) -1) | |
6152 | return FALSE; | |
6153 | } | |
6154 | ||
6155 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
6156 | { | |
6157 | if (! elf_link_flush_output_syms (finfo, bed)) | |
6158 | return FALSE; | |
6159 | } | |
6160 | ||
6161 | dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; | |
6162 | destshndx = finfo->symshndxbuf; | |
6163 | if (destshndx != NULL) | |
6164 | { | |
6165 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
6166 | { | |
6167 | bfd_size_type amt; | |
6168 | ||
6169 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); | |
6170 | finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2); | |
6171 | if (destshndx == NULL) | |
6172 | return FALSE; | |
6173 | memset ((char *) destshndx + amt, 0, amt); | |
6174 | finfo->shndxbuf_size *= 2; | |
6175 | } | |
6176 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
6177 | } | |
6178 | ||
6179 | bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); | |
6180 | finfo->symbuf_count += 1; | |
6181 | bfd_get_symcount (finfo->output_bfd) += 1; | |
6182 | ||
6183 | return TRUE; | |
6184 | } | |
6185 | ||
6186 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in | |
6187 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
6188 | versioned symbol that would normally require an explicit version. | |
6189 | We also handle the case that a DSO references a hidden symbol | |
6190 | which may be satisfied by a versioned symbol in another DSO. */ | |
6191 | ||
6192 | static bfd_boolean | |
6193 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
6194 | const struct elf_backend_data *bed, | |
6195 | struct elf_link_hash_entry *h) | |
6196 | { | |
6197 | bfd *abfd; | |
6198 | struct elf_link_loaded_list *loaded; | |
6199 | ||
6200 | if (!is_elf_hash_table (info->hash)) | |
6201 | return FALSE; | |
6202 | ||
6203 | switch (h->root.type) | |
6204 | { | |
6205 | default: | |
6206 | abfd = NULL; | |
6207 | break; | |
6208 | ||
6209 | case bfd_link_hash_undefined: | |
6210 | case bfd_link_hash_undefweak: | |
6211 | abfd = h->root.u.undef.abfd; | |
6212 | if ((abfd->flags & DYNAMIC) == 0 | |
e56f61be | 6213 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
6214 | return FALSE; |
6215 | break; | |
6216 | ||
6217 | case bfd_link_hash_defined: | |
6218 | case bfd_link_hash_defweak: | |
6219 | abfd = h->root.u.def.section->owner; | |
6220 | break; | |
6221 | ||
6222 | case bfd_link_hash_common: | |
6223 | abfd = h->root.u.c.p->section->owner; | |
6224 | break; | |
6225 | } | |
6226 | BFD_ASSERT (abfd != NULL); | |
6227 | ||
6228 | for (loaded = elf_hash_table (info)->loaded; | |
6229 | loaded != NULL; | |
6230 | loaded = loaded->next) | |
6231 | { | |
6232 | bfd *input; | |
6233 | Elf_Internal_Shdr *hdr; | |
6234 | bfd_size_type symcount; | |
6235 | bfd_size_type extsymcount; | |
6236 | bfd_size_type extsymoff; | |
6237 | Elf_Internal_Shdr *versymhdr; | |
6238 | Elf_Internal_Sym *isym; | |
6239 | Elf_Internal_Sym *isymend; | |
6240 | Elf_Internal_Sym *isymbuf; | |
6241 | Elf_External_Versym *ever; | |
6242 | Elf_External_Versym *extversym; | |
6243 | ||
6244 | input = loaded->abfd; | |
6245 | ||
6246 | /* We check each DSO for a possible hidden versioned definition. */ | |
6247 | if (input == abfd | |
6248 | || (input->flags & DYNAMIC) == 0 | |
6249 | || elf_dynversym (input) == 0) | |
6250 | continue; | |
6251 | ||
6252 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
6253 | ||
6254 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
6255 | if (elf_bad_symtab (input)) | |
6256 | { | |
6257 | extsymcount = symcount; | |
6258 | extsymoff = 0; | |
6259 | } | |
6260 | else | |
6261 | { | |
6262 | extsymcount = symcount - hdr->sh_info; | |
6263 | extsymoff = hdr->sh_info; | |
6264 | } | |
6265 | ||
6266 | if (extsymcount == 0) | |
6267 | continue; | |
6268 | ||
6269 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
6270 | NULL, NULL, NULL); | |
6271 | if (isymbuf == NULL) | |
6272 | return FALSE; | |
6273 | ||
6274 | /* Read in any version definitions. */ | |
6275 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
6276 | extversym = bfd_malloc (versymhdr->sh_size); | |
6277 | if (extversym == NULL) | |
6278 | goto error_ret; | |
6279 | ||
6280 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
6281 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
6282 | != versymhdr->sh_size)) | |
6283 | { | |
6284 | free (extversym); | |
6285 | error_ret: | |
6286 | free (isymbuf); | |
6287 | return FALSE; | |
6288 | } | |
6289 | ||
6290 | ever = extversym + extsymoff; | |
6291 | isymend = isymbuf + extsymcount; | |
6292 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
6293 | { | |
6294 | const char *name; | |
6295 | Elf_Internal_Versym iver; | |
6296 | unsigned short version_index; | |
6297 | ||
6298 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
6299 | || isym->st_shndx == SHN_UNDEF) | |
6300 | continue; | |
6301 | ||
6302 | name = bfd_elf_string_from_elf_section (input, | |
6303 | hdr->sh_link, | |
6304 | isym->st_name); | |
6305 | if (strcmp (name, h->root.root.string) != 0) | |
6306 | continue; | |
6307 | ||
6308 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
6309 | ||
6310 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
6311 | { | |
6312 | /* If we have a non-hidden versioned sym, then it should | |
6313 | have provided a definition for the undefined sym. */ | |
6314 | abort (); | |
6315 | } | |
6316 | ||
6317 | version_index = iver.vs_vers & VERSYM_VERSION; | |
6318 | if (version_index == 1 || version_index == 2) | |
6319 | { | |
6320 | /* This is the base or first version. We can use it. */ | |
6321 | free (extversym); | |
6322 | free (isymbuf); | |
6323 | return TRUE; | |
6324 | } | |
6325 | } | |
6326 | ||
6327 | free (extversym); | |
6328 | free (isymbuf); | |
6329 | } | |
6330 | ||
6331 | return FALSE; | |
6332 | } | |
6333 | ||
6334 | /* Add an external symbol to the symbol table. This is called from | |
6335 | the hash table traversal routine. When generating a shared object, | |
6336 | we go through the symbol table twice. The first time we output | |
6337 | anything that might have been forced to local scope in a version | |
6338 | script. The second time we output the symbols that are still | |
6339 | global symbols. */ | |
6340 | ||
6341 | static bfd_boolean | |
6342 | elf_link_output_extsym (struct elf_link_hash_entry *h, void *data) | |
6343 | { | |
6344 | struct elf_outext_info *eoinfo = data; | |
6345 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
6346 | bfd_boolean strip; | |
6347 | Elf_Internal_Sym sym; | |
6348 | asection *input_sec; | |
6349 | const struct elf_backend_data *bed; | |
6350 | ||
6351 | if (h->root.type == bfd_link_hash_warning) | |
6352 | { | |
6353 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6354 | if (h->root.type == bfd_link_hash_new) | |
6355 | return TRUE; | |
6356 | } | |
6357 | ||
6358 | /* Decide whether to output this symbol in this pass. */ | |
6359 | if (eoinfo->localsyms) | |
6360 | { | |
f5385ebf | 6361 | if (!h->forced_local) |
c152c796 AM |
6362 | return TRUE; |
6363 | } | |
6364 | else | |
6365 | { | |
f5385ebf | 6366 | if (h->forced_local) |
c152c796 AM |
6367 | return TRUE; |
6368 | } | |
6369 | ||
6370 | bed = get_elf_backend_data (finfo->output_bfd); | |
6371 | ||
12ac1cf5 | 6372 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 6373 | { |
12ac1cf5 NC |
6374 | /* If we have an undefined symbol reference here then it must have |
6375 | come from a shared library that is being linked in. (Undefined | |
6376 | references in regular files have already been handled). */ | |
6377 | bfd_boolean ignore_undef = FALSE; | |
6378 | ||
6379 | /* Some symbols may be special in that the fact that they're | |
6380 | undefined can be safely ignored - let backend determine that. */ | |
6381 | if (bed->elf_backend_ignore_undef_symbol) | |
6382 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
6383 | ||
6384 | /* If we are reporting errors for this situation then do so now. */ | |
6385 | if (ignore_undef == FALSE | |
6386 | && h->ref_dynamic | |
6387 | && ! h->ref_regular | |
6388 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h) | |
6389 | && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
c152c796 | 6390 | { |
12ac1cf5 NC |
6391 | if (! (finfo->info->callbacks->undefined_symbol |
6392 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
6393 | NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) | |
6394 | { | |
6395 | eoinfo->failed = TRUE; | |
6396 | return FALSE; | |
6397 | } | |
c152c796 AM |
6398 | } |
6399 | } | |
6400 | ||
6401 | /* We should also warn if a forced local symbol is referenced from | |
6402 | shared libraries. */ | |
6403 | if (! finfo->info->relocatable | |
6404 | && (! finfo->info->shared) | |
f5385ebf AM |
6405 | && h->forced_local |
6406 | && h->ref_dynamic | |
6407 | && !h->dynamic_def | |
6408 | && !h->dynamic_weak | |
c152c796 AM |
6409 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) |
6410 | { | |
6411 | (*_bfd_error_handler) | |
d003868e | 6412 | (_("%B: %s symbol `%s' in %B is referenced by DSO"), |
cfca085c L |
6413 | finfo->output_bfd, |
6414 | h->root.u.def.section == bfd_abs_section_ptr | |
6415 | ? finfo->output_bfd : h->root.u.def.section->owner, | |
c152c796 AM |
6416 | ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
6417 | ? "internal" | |
6418 | : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
d003868e AM |
6419 | ? "hidden" : "local", |
6420 | h->root.root.string); | |
c152c796 AM |
6421 | eoinfo->failed = TRUE; |
6422 | return FALSE; | |
6423 | } | |
6424 | ||
6425 | /* We don't want to output symbols that have never been mentioned by | |
6426 | a regular file, or that we have been told to strip. However, if | |
6427 | h->indx is set to -2, the symbol is used by a reloc and we must | |
6428 | output it. */ | |
6429 | if (h->indx == -2) | |
6430 | strip = FALSE; | |
f5385ebf | 6431 | else if ((h->def_dynamic |
77cfaee6 AM |
6432 | || h->ref_dynamic |
6433 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
6434 | && !h->def_regular |
6435 | && !h->ref_regular) | |
c152c796 AM |
6436 | strip = TRUE; |
6437 | else if (finfo->info->strip == strip_all) | |
6438 | strip = TRUE; | |
6439 | else if (finfo->info->strip == strip_some | |
6440 | && bfd_hash_lookup (finfo->info->keep_hash, | |
6441 | h->root.root.string, FALSE, FALSE) == NULL) | |
6442 | strip = TRUE; | |
6443 | else if (finfo->info->strip_discarded | |
6444 | && (h->root.type == bfd_link_hash_defined | |
6445 | || h->root.type == bfd_link_hash_defweak) | |
6446 | && elf_discarded_section (h->root.u.def.section)) | |
6447 | strip = TRUE; | |
6448 | else | |
6449 | strip = FALSE; | |
6450 | ||
6451 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
6452 | nothing else to do unless it is a forced local symbol. */ | |
6453 | if (strip | |
6454 | && h->dynindx == -1 | |
f5385ebf | 6455 | && !h->forced_local) |
c152c796 AM |
6456 | return TRUE; |
6457 | ||
6458 | sym.st_value = 0; | |
6459 | sym.st_size = h->size; | |
6460 | sym.st_other = h->other; | |
f5385ebf | 6461 | if (h->forced_local) |
c152c796 AM |
6462 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); |
6463 | else if (h->root.type == bfd_link_hash_undefweak | |
6464 | || h->root.type == bfd_link_hash_defweak) | |
6465 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
6466 | else | |
6467 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
6468 | ||
6469 | switch (h->root.type) | |
6470 | { | |
6471 | default: | |
6472 | case bfd_link_hash_new: | |
6473 | case bfd_link_hash_warning: | |
6474 | abort (); | |
6475 | return FALSE; | |
6476 | ||
6477 | case bfd_link_hash_undefined: | |
6478 | case bfd_link_hash_undefweak: | |
6479 | input_sec = bfd_und_section_ptr; | |
6480 | sym.st_shndx = SHN_UNDEF; | |
6481 | break; | |
6482 | ||
6483 | case bfd_link_hash_defined: | |
6484 | case bfd_link_hash_defweak: | |
6485 | { | |
6486 | input_sec = h->root.u.def.section; | |
6487 | if (input_sec->output_section != NULL) | |
6488 | { | |
6489 | sym.st_shndx = | |
6490 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
6491 | input_sec->output_section); | |
6492 | if (sym.st_shndx == SHN_BAD) | |
6493 | { | |
6494 | (*_bfd_error_handler) | |
d003868e AM |
6495 | (_("%B: could not find output section %A for input section %A"), |
6496 | finfo->output_bfd, input_sec->output_section, input_sec); | |
c152c796 AM |
6497 | eoinfo->failed = TRUE; |
6498 | return FALSE; | |
6499 | } | |
6500 | ||
6501 | /* ELF symbols in relocatable files are section relative, | |
6502 | but in nonrelocatable files they are virtual | |
6503 | addresses. */ | |
6504 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
6505 | if (! finfo->info->relocatable) | |
6506 | { | |
6507 | sym.st_value += input_sec->output_section->vma; | |
6508 | if (h->type == STT_TLS) | |
6509 | { | |
6510 | /* STT_TLS symbols are relative to PT_TLS segment | |
6511 | base. */ | |
6512 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
6513 | sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
6514 | } | |
6515 | } | |
6516 | } | |
6517 | else | |
6518 | { | |
6519 | BFD_ASSERT (input_sec->owner == NULL | |
6520 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
6521 | sym.st_shndx = SHN_UNDEF; | |
6522 | input_sec = bfd_und_section_ptr; | |
6523 | } | |
6524 | } | |
6525 | break; | |
6526 | ||
6527 | case bfd_link_hash_common: | |
6528 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 6529 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
6530 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
6531 | break; | |
6532 | ||
6533 | case bfd_link_hash_indirect: | |
6534 | /* These symbols are created by symbol versioning. They point | |
6535 | to the decorated version of the name. For example, if the | |
6536 | symbol foo@@GNU_1.2 is the default, which should be used when | |
6537 | foo is used with no version, then we add an indirect symbol | |
6538 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
6539 | since the indirected symbol is already in the hash table. */ | |
6540 | return TRUE; | |
6541 | } | |
6542 | ||
6543 | /* Give the processor backend a chance to tweak the symbol value, | |
6544 | and also to finish up anything that needs to be done for this | |
6545 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
6546 | forced local syms when non-shared is due to a historical quirk. */ | |
6547 | if ((h->dynindx != -1 | |
f5385ebf | 6548 | || h->forced_local) |
c152c796 AM |
6549 | && ((finfo->info->shared |
6550 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
6551 | || h->root.type != bfd_link_hash_undefweak)) | |
f5385ebf | 6552 | || !h->forced_local) |
c152c796 AM |
6553 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
6554 | { | |
6555 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
6556 | (finfo->output_bfd, finfo->info, h, &sym))) | |
6557 | { | |
6558 | eoinfo->failed = TRUE; | |
6559 | return FALSE; | |
6560 | } | |
6561 | } | |
6562 | ||
6563 | /* If we are marking the symbol as undefined, and there are no | |
6564 | non-weak references to this symbol from a regular object, then | |
6565 | mark the symbol as weak undefined; if there are non-weak | |
6566 | references, mark the symbol as strong. We can't do this earlier, | |
6567 | because it might not be marked as undefined until the | |
6568 | finish_dynamic_symbol routine gets through with it. */ | |
6569 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 6570 | && h->ref_regular |
c152c796 AM |
6571 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
6572 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
6573 | { | |
6574 | int bindtype; | |
6575 | ||
f5385ebf | 6576 | if (h->ref_regular_nonweak) |
c152c796 AM |
6577 | bindtype = STB_GLOBAL; |
6578 | else | |
6579 | bindtype = STB_WEAK; | |
6580 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); | |
6581 | } | |
6582 | ||
6583 | /* If a non-weak symbol with non-default visibility is not defined | |
6584 | locally, it is a fatal error. */ | |
6585 | if (! finfo->info->relocatable | |
6586 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT | |
6587 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
6588 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 6589 | && !h->def_regular) |
c152c796 AM |
6590 | { |
6591 | (*_bfd_error_handler) | |
d003868e AM |
6592 | (_("%B: %s symbol `%s' isn't defined"), |
6593 | finfo->output_bfd, | |
6594 | ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED | |
6595 | ? "protected" | |
6596 | : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL | |
6597 | ? "internal" : "hidden", | |
6598 | h->root.root.string); | |
c152c796 AM |
6599 | eoinfo->failed = TRUE; |
6600 | return FALSE; | |
6601 | } | |
6602 | ||
6603 | /* If this symbol should be put in the .dynsym section, then put it | |
6604 | there now. We already know the symbol index. We also fill in | |
6605 | the entry in the .hash section. */ | |
6606 | if (h->dynindx != -1 | |
6607 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
6608 | { | |
6609 | size_t bucketcount; | |
6610 | size_t bucket; | |
6611 | size_t hash_entry_size; | |
6612 | bfd_byte *bucketpos; | |
6613 | bfd_vma chain; | |
6614 | bfd_byte *esym; | |
6615 | ||
6616 | sym.st_name = h->dynstr_index; | |
6617 | esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; | |
6618 | bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); | |
6619 | ||
6620 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
f6e332e6 | 6621 | bucket = h->u.elf_hash_value % bucketcount; |
c152c796 AM |
6622 | hash_entry_size |
6623 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; | |
6624 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents | |
6625 | + (bucket + 2) * hash_entry_size); | |
6626 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
6627 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); | |
6628 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, | |
6629 | ((bfd_byte *) finfo->hash_sec->contents | |
6630 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
6631 | ||
6632 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
6633 | { | |
6634 | Elf_Internal_Versym iversym; | |
6635 | Elf_External_Versym *eversym; | |
6636 | ||
f5385ebf | 6637 | if (!h->def_regular) |
c152c796 AM |
6638 | { |
6639 | if (h->verinfo.verdef == NULL) | |
6640 | iversym.vs_vers = 0; | |
6641 | else | |
6642 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
6643 | } | |
6644 | else | |
6645 | { | |
6646 | if (h->verinfo.vertree == NULL) | |
6647 | iversym.vs_vers = 1; | |
6648 | else | |
6649 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
3e3b46e5 PB |
6650 | if (finfo->info->create_default_symver) |
6651 | iversym.vs_vers++; | |
c152c796 AM |
6652 | } |
6653 | ||
f5385ebf | 6654 | if (h->hidden) |
c152c796 AM |
6655 | iversym.vs_vers |= VERSYM_HIDDEN; |
6656 | ||
6657 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; | |
6658 | eversym += h->dynindx; | |
6659 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
6660 | } | |
6661 | } | |
6662 | ||
6663 | /* If we're stripping it, then it was just a dynamic symbol, and | |
6664 | there's nothing else to do. */ | |
6665 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) | |
6666 | return TRUE; | |
6667 | ||
6668 | h->indx = bfd_get_symcount (finfo->output_bfd); | |
6669 | ||
6670 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h)) | |
6671 | { | |
6672 | eoinfo->failed = TRUE; | |
6673 | return FALSE; | |
6674 | } | |
6675 | ||
6676 | return TRUE; | |
6677 | } | |
6678 | ||
cdd3575c AM |
6679 | /* Return TRUE if special handling is done for relocs in SEC against |
6680 | symbols defined in discarded sections. */ | |
6681 | ||
c152c796 AM |
6682 | static bfd_boolean |
6683 | elf_section_ignore_discarded_relocs (asection *sec) | |
6684 | { | |
6685 | const struct elf_backend_data *bed; | |
6686 | ||
cdd3575c AM |
6687 | switch (sec->sec_info_type) |
6688 | { | |
6689 | case ELF_INFO_TYPE_STABS: | |
6690 | case ELF_INFO_TYPE_EH_FRAME: | |
6691 | return TRUE; | |
6692 | default: | |
6693 | break; | |
6694 | } | |
c152c796 AM |
6695 | |
6696 | bed = get_elf_backend_data (sec->owner); | |
6697 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
6698 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
6699 | return TRUE; | |
6700 | ||
6701 | return FALSE; | |
6702 | } | |
6703 | ||
9e66c942 AM |
6704 | /* Return a mask saying how ld should treat relocations in SEC against |
6705 | symbols defined in discarded sections. If this function returns | |
6706 | COMPLAIN set, ld will issue a warning message. If this function | |
6707 | returns PRETEND set, and the discarded section was link-once and the | |
6708 | same size as the kept link-once section, ld will pretend that the | |
6709 | symbol was actually defined in the kept section. Otherwise ld will | |
6710 | zero the reloc (at least that is the intent, but some cooperation by | |
6711 | the target dependent code is needed, particularly for REL targets). */ | |
6712 | ||
8a696751 AM |
6713 | unsigned int |
6714 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 6715 | { |
9e66c942 | 6716 | if (sec->flags & SEC_DEBUGGING) |
e0ae6d6f | 6717 | return 0; |
cdd3575c AM |
6718 | |
6719 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 6720 | return 0; |
cdd3575c AM |
6721 | |
6722 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 6723 | return 0; |
cdd3575c | 6724 | |
9e66c942 | 6725 | return COMPLAIN | PRETEND; |
cdd3575c AM |
6726 | } |
6727 | ||
3d7f7666 L |
6728 | /* Find a match between a section and a member of a section group. */ |
6729 | ||
6730 | static asection * | |
6731 | match_group_member (asection *sec, asection *group) | |
6732 | { | |
6733 | asection *first = elf_next_in_group (group); | |
6734 | asection *s = first; | |
6735 | ||
6736 | while (s != NULL) | |
6737 | { | |
6738 | if (bfd_elf_match_symbols_in_sections (s, sec)) | |
6739 | return s; | |
6740 | ||
6741 | if (s == first) | |
6742 | break; | |
6743 | } | |
6744 | ||
6745 | return NULL; | |
6746 | } | |
6747 | ||
01b3c8ab L |
6748 | /* Check if the kept section of a discarded section SEC can be used |
6749 | to replace it. Return the replacement if it is OK. Otherwise return | |
6750 | NULL. */ | |
6751 | ||
6752 | asection * | |
6753 | _bfd_elf_check_kept_section (asection *sec) | |
6754 | { | |
6755 | asection *kept; | |
6756 | ||
6757 | kept = sec->kept_section; | |
6758 | if (kept != NULL) | |
6759 | { | |
6760 | if (elf_sec_group (sec) != NULL) | |
6761 | kept = match_group_member (sec, kept); | |
6762 | if (kept != NULL && sec->size != kept->size) | |
6763 | kept = NULL; | |
6764 | } | |
6765 | return kept; | |
6766 | } | |
6767 | ||
c152c796 AM |
6768 | /* Link an input file into the linker output file. This function |
6769 | handles all the sections and relocations of the input file at once. | |
6770 | This is so that we only have to read the local symbols once, and | |
6771 | don't have to keep them in memory. */ | |
6772 | ||
6773 | static bfd_boolean | |
6774 | elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) | |
6775 | { | |
6776 | bfd_boolean (*relocate_section) | |
6777 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
6778 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
6779 | bfd *output_bfd; | |
6780 | Elf_Internal_Shdr *symtab_hdr; | |
6781 | size_t locsymcount; | |
6782 | size_t extsymoff; | |
6783 | Elf_Internal_Sym *isymbuf; | |
6784 | Elf_Internal_Sym *isym; | |
6785 | Elf_Internal_Sym *isymend; | |
6786 | long *pindex; | |
6787 | asection **ppsection; | |
6788 | asection *o; | |
6789 | const struct elf_backend_data *bed; | |
6790 | bfd_boolean emit_relocs; | |
6791 | struct elf_link_hash_entry **sym_hashes; | |
6792 | ||
6793 | output_bfd = finfo->output_bfd; | |
6794 | bed = get_elf_backend_data (output_bfd); | |
6795 | relocate_section = bed->elf_backend_relocate_section; | |
6796 | ||
6797 | /* If this is a dynamic object, we don't want to do anything here: | |
6798 | we don't want the local symbols, and we don't want the section | |
6799 | contents. */ | |
6800 | if ((input_bfd->flags & DYNAMIC) != 0) | |
6801 | return TRUE; | |
6802 | ||
6803 | emit_relocs = (finfo->info->relocatable | |
eac338cf | 6804 | || finfo->info->emitrelocations); |
c152c796 AM |
6805 | |
6806 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
6807 | if (elf_bad_symtab (input_bfd)) | |
6808 | { | |
6809 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
6810 | extsymoff = 0; | |
6811 | } | |
6812 | else | |
6813 | { | |
6814 | locsymcount = symtab_hdr->sh_info; | |
6815 | extsymoff = symtab_hdr->sh_info; | |
6816 | } | |
6817 | ||
6818 | /* Read the local symbols. */ | |
6819 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
6820 | if (isymbuf == NULL && locsymcount != 0) | |
6821 | { | |
6822 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
6823 | finfo->internal_syms, | |
6824 | finfo->external_syms, | |
6825 | finfo->locsym_shndx); | |
6826 | if (isymbuf == NULL) | |
6827 | return FALSE; | |
6828 | } | |
6829 | ||
6830 | /* Find local symbol sections and adjust values of symbols in | |
6831 | SEC_MERGE sections. Write out those local symbols we know are | |
6832 | going into the output file. */ | |
6833 | isymend = isymbuf + locsymcount; | |
6834 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
6835 | isym < isymend; | |
6836 | isym++, pindex++, ppsection++) | |
6837 | { | |
6838 | asection *isec; | |
6839 | const char *name; | |
6840 | Elf_Internal_Sym osym; | |
6841 | ||
6842 | *pindex = -1; | |
6843 | ||
6844 | if (elf_bad_symtab (input_bfd)) | |
6845 | { | |
6846 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
6847 | { | |
6848 | *ppsection = NULL; | |
6849 | continue; | |
6850 | } | |
6851 | } | |
6852 | ||
6853 | if (isym->st_shndx == SHN_UNDEF) | |
6854 | isec = bfd_und_section_ptr; | |
6855 | else if (isym->st_shndx < SHN_LORESERVE | |
6856 | || isym->st_shndx > SHN_HIRESERVE) | |
6857 | { | |
6858 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); | |
6859 | if (isec | |
6860 | && isec->sec_info_type == ELF_INFO_TYPE_MERGE | |
6861 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) | |
6862 | isym->st_value = | |
6863 | _bfd_merged_section_offset (output_bfd, &isec, | |
6864 | elf_section_data (isec)->sec_info, | |
753731ee | 6865 | isym->st_value); |
c152c796 AM |
6866 | } |
6867 | else if (isym->st_shndx == SHN_ABS) | |
6868 | isec = bfd_abs_section_ptr; | |
6869 | else if (isym->st_shndx == SHN_COMMON) | |
6870 | isec = bfd_com_section_ptr; | |
6871 | else | |
6872 | { | |
f02571c5 AM |
6873 | /* Don't attempt to output symbols with st_shnx in the |
6874 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
6875 | *ppsection = NULL; | |
6876 | continue; | |
c152c796 AM |
6877 | } |
6878 | ||
6879 | *ppsection = isec; | |
6880 | ||
6881 | /* Don't output the first, undefined, symbol. */ | |
6882 | if (ppsection == finfo->sections) | |
6883 | continue; | |
6884 | ||
6885 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
6886 | { | |
6887 | /* We never output section symbols. Instead, we use the | |
6888 | section symbol of the corresponding section in the output | |
6889 | file. */ | |
6890 | continue; | |
6891 | } | |
6892 | ||
6893 | /* If we are stripping all symbols, we don't want to output this | |
6894 | one. */ | |
6895 | if (finfo->info->strip == strip_all) | |
6896 | continue; | |
6897 | ||
6898 | /* If we are discarding all local symbols, we don't want to | |
6899 | output this one. If we are generating a relocatable output | |
6900 | file, then some of the local symbols may be required by | |
6901 | relocs; we output them below as we discover that they are | |
6902 | needed. */ | |
6903 | if (finfo->info->discard == discard_all) | |
6904 | continue; | |
6905 | ||
6906 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
6907 | discarding, we don't need to keep it. */ |
6908 | if (isym->st_shndx != SHN_UNDEF | |
6909 | && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
ccf5f610 | 6910 | && (isec == NULL |
f02571c5 AM |
6911 | || bfd_section_removed_from_list (output_bfd, |
6912 | isec->output_section))) | |
e75a280b L |
6913 | continue; |
6914 | ||
c152c796 AM |
6915 | /* Get the name of the symbol. */ |
6916 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
6917 | isym->st_name); | |
6918 | if (name == NULL) | |
6919 | return FALSE; | |
6920 | ||
6921 | /* See if we are discarding symbols with this name. */ | |
6922 | if ((finfo->info->strip == strip_some | |
6923 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) | |
6924 | == NULL)) | |
6925 | || (((finfo->info->discard == discard_sec_merge | |
6926 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) | |
6927 | || finfo->info->discard == discard_l) | |
6928 | && bfd_is_local_label_name (input_bfd, name))) | |
6929 | continue; | |
6930 | ||
6931 | /* If we get here, we are going to output this symbol. */ | |
6932 | ||
6933 | osym = *isym; | |
6934 | ||
6935 | /* Adjust the section index for the output file. */ | |
6936 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
6937 | isec->output_section); | |
6938 | if (osym.st_shndx == SHN_BAD) | |
6939 | return FALSE; | |
6940 | ||
6941 | *pindex = bfd_get_symcount (output_bfd); | |
6942 | ||
6943 | /* ELF symbols in relocatable files are section relative, but | |
6944 | in executable files they are virtual addresses. Note that | |
6945 | this code assumes that all ELF sections have an associated | |
6946 | BFD section with a reasonable value for output_offset; below | |
6947 | we assume that they also have a reasonable value for | |
6948 | output_section. Any special sections must be set up to meet | |
6949 | these requirements. */ | |
6950 | osym.st_value += isec->output_offset; | |
6951 | if (! finfo->info->relocatable) | |
6952 | { | |
6953 | osym.st_value += isec->output_section->vma; | |
6954 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
6955 | { | |
6956 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
6957 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
6958 | osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
6959 | } | |
6960 | } | |
6961 | ||
6962 | if (! elf_link_output_sym (finfo, name, &osym, isec, NULL)) | |
6963 | return FALSE; | |
6964 | } | |
6965 | ||
6966 | /* Relocate the contents of each section. */ | |
6967 | sym_hashes = elf_sym_hashes (input_bfd); | |
6968 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
6969 | { | |
6970 | bfd_byte *contents; | |
6971 | ||
6972 | if (! o->linker_mark) | |
6973 | { | |
6974 | /* This section was omitted from the link. */ | |
6975 | continue; | |
6976 | } | |
6977 | ||
6978 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 6979 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
6980 | continue; |
6981 | ||
6982 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
6983 | { | |
6984 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
6985 | or somesuch. */ | |
6986 | continue; | |
6987 | } | |
6988 | ||
6989 | /* Get the contents of the section. They have been cached by a | |
6990 | relaxation routine. Note that o is a section in an input | |
6991 | file, so the contents field will not have been set by any of | |
6992 | the routines which work on output files. */ | |
6993 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
6994 | contents = elf_section_data (o)->this_hdr.contents; | |
6995 | else | |
6996 | { | |
eea6121a AM |
6997 | bfd_size_type amt = o->rawsize ? o->rawsize : o->size; |
6998 | ||
c152c796 | 6999 | contents = finfo->contents; |
eea6121a | 7000 | if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt)) |
c152c796 AM |
7001 | return FALSE; |
7002 | } | |
7003 | ||
7004 | if ((o->flags & SEC_RELOC) != 0) | |
7005 | { | |
7006 | Elf_Internal_Rela *internal_relocs; | |
7007 | bfd_vma r_type_mask; | |
7008 | int r_sym_shift; | |
7009 | ||
7010 | /* Get the swapped relocs. */ | |
7011 | internal_relocs | |
7012 | = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, | |
7013 | finfo->internal_relocs, FALSE); | |
7014 | if (internal_relocs == NULL | |
7015 | && o->reloc_count > 0) | |
7016 | return FALSE; | |
7017 | ||
7018 | if (bed->s->arch_size == 32) | |
7019 | { | |
7020 | r_type_mask = 0xff; | |
7021 | r_sym_shift = 8; | |
7022 | } | |
7023 | else | |
7024 | { | |
7025 | r_type_mask = 0xffffffff; | |
7026 | r_sym_shift = 32; | |
7027 | } | |
7028 | ||
7029 | /* Run through the relocs looking for any against symbols | |
7030 | from discarded sections and section symbols from | |
7031 | removed link-once sections. Complain about relocs | |
7032 | against discarded sections. Zero relocs against removed | |
e0ae6d6f | 7033 | link-once sections. */ |
c152c796 AM |
7034 | if (!elf_section_ignore_discarded_relocs (o)) |
7035 | { | |
7036 | Elf_Internal_Rela *rel, *relend; | |
8a696751 | 7037 | unsigned int action = (*bed->action_discarded) (o); |
c152c796 AM |
7038 | |
7039 | rel = internal_relocs; | |
7040 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
7041 | for ( ; rel < relend; rel++) | |
7042 | { | |
7043 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
cdd3575c AM |
7044 | asection **ps, *sec; |
7045 | struct elf_link_hash_entry *h = NULL; | |
7046 | const char *sym_name; | |
c152c796 | 7047 | |
ee75fd95 AM |
7048 | if (r_symndx == STN_UNDEF) |
7049 | continue; | |
7050 | ||
c152c796 AM |
7051 | if (r_symndx >= locsymcount |
7052 | || (elf_bad_symtab (input_bfd) | |
7053 | && finfo->sections[r_symndx] == NULL)) | |
7054 | { | |
c152c796 | 7055 | h = sym_hashes[r_symndx - extsymoff]; |
dce669a1 | 7056 | |
8c19749a NC |
7057 | /* Badly formatted input files can contain relocs that |
7058 | reference non-existant symbols. Check here so that | |
7059 | we do not seg fault. */ | |
7060 | if (h == NULL) | |
7061 | { | |
7062 | char buffer [32]; | |
7063 | ||
7064 | sprintf_vma (buffer, rel->r_info); | |
7065 | (*_bfd_error_handler) | |
7066 | (_("error: %B contains a reloc (0x%s) for section %A " | |
7067 | "that references a non-existent global symbol"), | |
7068 | input_bfd, o, buffer); | |
7069 | bfd_set_error (bfd_error_bad_value); | |
7070 | return FALSE; | |
7071 | } | |
3b36f7e6 | 7072 | |
c152c796 AM |
7073 | while (h->root.type == bfd_link_hash_indirect |
7074 | || h->root.type == bfd_link_hash_warning) | |
7075 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7076 | ||
cdd3575c AM |
7077 | if (h->root.type != bfd_link_hash_defined |
7078 | && h->root.type != bfd_link_hash_defweak) | |
7079 | continue; | |
7080 | ||
7081 | ps = &h->root.u.def.section; | |
7082 | sym_name = h->root.root.string; | |
c152c796 AM |
7083 | } |
7084 | else | |
7085 | { | |
cdd3575c AM |
7086 | Elf_Internal_Sym *sym = isymbuf + r_symndx; |
7087 | ps = &finfo->sections[r_symndx]; | |
26c61ae5 L |
7088 | sym_name = bfd_elf_sym_name (input_bfd, |
7089 | symtab_hdr, | |
7090 | sym, *ps); | |
cdd3575c | 7091 | } |
c152c796 | 7092 | |
cdd3575c AM |
7093 | /* Complain if the definition comes from a |
7094 | discarded section. */ | |
7095 | if ((sec = *ps) != NULL && elf_discarded_section (sec)) | |
7096 | { | |
87e5235d | 7097 | BFD_ASSERT (r_symndx != 0); |
9e66c942 | 7098 | if (action & COMPLAIN) |
e1fffbe6 AM |
7099 | (*finfo->info->callbacks->einfo) |
7100 | (_("%X`%s' referenced in section `%A' of %B: " | |
58ac56d0 | 7101 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 7102 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 7103 | |
87e5235d | 7104 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 7105 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
7106 | really defined in the kept linkonce section. |
7107 | FIXME: This is quite broken. Modifying the | |
7108 | symbol here means we will be changing all later | |
e0ae6d6f | 7109 | uses of the symbol, not just in this section. */ |
01b3c8ab | 7110 | if (action & PRETEND) |
87e5235d | 7111 | { |
01b3c8ab L |
7112 | asection *kept; |
7113 | ||
7114 | kept = _bfd_elf_check_kept_section (sec); | |
7115 | if (kept != NULL) | |
87e5235d AM |
7116 | { |
7117 | *ps = kept; | |
7118 | continue; | |
7119 | } | |
7120 | } | |
7121 | ||
cdd3575c AM |
7122 | /* Remove the symbol reference from the reloc, but |
7123 | don't kill the reloc completely. This is so that | |
7124 | a zero value will be written into the section, | |
7125 | which may have non-zero contents put there by the | |
7126 | assembler. Zero in things like an eh_frame fde | |
7127 | pc_begin allows stack unwinders to recognize the | |
7128 | fde as bogus. */ | |
7129 | rel->r_info &= r_type_mask; | |
7130 | rel->r_addend = 0; | |
c152c796 AM |
7131 | } |
7132 | } | |
7133 | } | |
7134 | ||
7135 | /* Relocate the section by invoking a back end routine. | |
7136 | ||
7137 | The back end routine is responsible for adjusting the | |
7138 | section contents as necessary, and (if using Rela relocs | |
7139 | and generating a relocatable output file) adjusting the | |
7140 | reloc addend as necessary. | |
7141 | ||
7142 | The back end routine does not have to worry about setting | |
7143 | the reloc address or the reloc symbol index. | |
7144 | ||
7145 | The back end routine is given a pointer to the swapped in | |
7146 | internal symbols, and can access the hash table entries | |
7147 | for the external symbols via elf_sym_hashes (input_bfd). | |
7148 | ||
7149 | When generating relocatable output, the back end routine | |
7150 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
7151 | output symbol is going to be a section symbol | |
7152 | corresponding to the output section, which will require | |
7153 | the addend to be adjusted. */ | |
7154 | ||
7155 | if (! (*relocate_section) (output_bfd, finfo->info, | |
7156 | input_bfd, o, contents, | |
7157 | internal_relocs, | |
7158 | isymbuf, | |
7159 | finfo->sections)) | |
7160 | return FALSE; | |
7161 | ||
7162 | if (emit_relocs) | |
7163 | { | |
7164 | Elf_Internal_Rela *irela; | |
7165 | Elf_Internal_Rela *irelaend; | |
7166 | bfd_vma last_offset; | |
7167 | struct elf_link_hash_entry **rel_hash; | |
eac338cf | 7168 | struct elf_link_hash_entry **rel_hash_list; |
c152c796 AM |
7169 | Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2; |
7170 | unsigned int next_erel; | |
c152c796 AM |
7171 | bfd_boolean rela_normal; |
7172 | ||
7173 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
7174 | rela_normal = (bed->rela_normal | |
7175 | && (input_rel_hdr->sh_entsize | |
7176 | == bed->s->sizeof_rela)); | |
7177 | ||
7178 | /* Adjust the reloc addresses and symbol indices. */ | |
7179 | ||
7180 | irela = internal_relocs; | |
7181 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
7182 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
7183 | + elf_section_data (o->output_section)->rel_count | |
7184 | + elf_section_data (o->output_section)->rel_count2); | |
eac338cf | 7185 | rel_hash_list = rel_hash; |
c152c796 AM |
7186 | last_offset = o->output_offset; |
7187 | if (!finfo->info->relocatable) | |
7188 | last_offset += o->output_section->vma; | |
7189 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
7190 | { | |
7191 | unsigned long r_symndx; | |
7192 | asection *sec; | |
7193 | Elf_Internal_Sym sym; | |
7194 | ||
7195 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
7196 | { | |
7197 | rel_hash++; | |
7198 | next_erel = 0; | |
7199 | } | |
7200 | ||
7201 | irela->r_offset = _bfd_elf_section_offset (output_bfd, | |
7202 | finfo->info, o, | |
7203 | irela->r_offset); | |
7204 | if (irela->r_offset >= (bfd_vma) -2) | |
7205 | { | |
7206 | /* This is a reloc for a deleted entry or somesuch. | |
7207 | Turn it into an R_*_NONE reloc, at the same | |
7208 | offset as the last reloc. elf_eh_frame.c and | |
7209 | elf_bfd_discard_info rely on reloc offsets | |
7210 | being ordered. */ | |
7211 | irela->r_offset = last_offset; | |
7212 | irela->r_info = 0; | |
7213 | irela->r_addend = 0; | |
7214 | continue; | |
7215 | } | |
7216 | ||
7217 | irela->r_offset += o->output_offset; | |
7218 | ||
7219 | /* Relocs in an executable have to be virtual addresses. */ | |
7220 | if (!finfo->info->relocatable) | |
7221 | irela->r_offset += o->output_section->vma; | |
7222 | ||
7223 | last_offset = irela->r_offset; | |
7224 | ||
7225 | r_symndx = irela->r_info >> r_sym_shift; | |
7226 | if (r_symndx == STN_UNDEF) | |
7227 | continue; | |
7228 | ||
7229 | if (r_symndx >= locsymcount | |
7230 | || (elf_bad_symtab (input_bfd) | |
7231 | && finfo->sections[r_symndx] == NULL)) | |
7232 | { | |
7233 | struct elf_link_hash_entry *rh; | |
7234 | unsigned long indx; | |
7235 | ||
7236 | /* This is a reloc against a global symbol. We | |
7237 | have not yet output all the local symbols, so | |
7238 | we do not know the symbol index of any global | |
7239 | symbol. We set the rel_hash entry for this | |
7240 | reloc to point to the global hash table entry | |
7241 | for this symbol. The symbol index is then | |
ee75fd95 | 7242 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
7243 | indx = r_symndx - extsymoff; |
7244 | rh = elf_sym_hashes (input_bfd)[indx]; | |
7245 | while (rh->root.type == bfd_link_hash_indirect | |
7246 | || rh->root.type == bfd_link_hash_warning) | |
7247 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
7248 | ||
7249 | /* Setting the index to -2 tells | |
7250 | elf_link_output_extsym that this symbol is | |
7251 | used by a reloc. */ | |
7252 | BFD_ASSERT (rh->indx < 0); | |
7253 | rh->indx = -2; | |
7254 | ||
7255 | *rel_hash = rh; | |
7256 | ||
7257 | continue; | |
7258 | } | |
7259 | ||
7260 | /* This is a reloc against a local symbol. */ | |
7261 | ||
7262 | *rel_hash = NULL; | |
7263 | sym = isymbuf[r_symndx]; | |
7264 | sec = finfo->sections[r_symndx]; | |
7265 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) | |
7266 | { | |
7267 | /* I suppose the backend ought to fill in the | |
7268 | section of any STT_SECTION symbol against a | |
6a8d1586 AM |
7269 | processor specific section. */ |
7270 | r_symndx = 0; | |
7271 | if (bfd_is_abs_section (sec)) | |
7272 | ; | |
c152c796 AM |
7273 | else if (sec == NULL || sec->owner == NULL) |
7274 | { | |
7275 | bfd_set_error (bfd_error_bad_value); | |
7276 | return FALSE; | |
7277 | } | |
7278 | else | |
7279 | { | |
6a8d1586 AM |
7280 | asection *osec = sec->output_section; |
7281 | ||
7282 | /* If we have discarded a section, the output | |
7283 | section will be the absolute section. In | |
7284 | case of discarded link-once and discarded | |
7285 | SEC_MERGE sections, use the kept section. */ | |
7286 | if (bfd_is_abs_section (osec) | |
7287 | && sec->kept_section != NULL | |
7288 | && sec->kept_section->output_section != NULL) | |
7289 | { | |
7290 | osec = sec->kept_section->output_section; | |
7291 | irela->r_addend -= osec->vma; | |
7292 | } | |
7293 | ||
7294 | if (!bfd_is_abs_section (osec)) | |
7295 | { | |
7296 | r_symndx = osec->target_index; | |
7297 | BFD_ASSERT (r_symndx != 0); | |
7298 | } | |
c152c796 AM |
7299 | } |
7300 | ||
7301 | /* Adjust the addend according to where the | |
7302 | section winds up in the output section. */ | |
7303 | if (rela_normal) | |
7304 | irela->r_addend += sec->output_offset; | |
7305 | } | |
7306 | else | |
7307 | { | |
7308 | if (finfo->indices[r_symndx] == -1) | |
7309 | { | |
7310 | unsigned long shlink; | |
7311 | const char *name; | |
7312 | asection *osec; | |
7313 | ||
7314 | if (finfo->info->strip == strip_all) | |
7315 | { | |
7316 | /* You can't do ld -r -s. */ | |
7317 | bfd_set_error (bfd_error_invalid_operation); | |
7318 | return FALSE; | |
7319 | } | |
7320 | ||
7321 | /* This symbol was skipped earlier, but | |
7322 | since it is needed by a reloc, we | |
7323 | must output it now. */ | |
7324 | shlink = symtab_hdr->sh_link; | |
7325 | name = (bfd_elf_string_from_elf_section | |
7326 | (input_bfd, shlink, sym.st_name)); | |
7327 | if (name == NULL) | |
7328 | return FALSE; | |
7329 | ||
7330 | osec = sec->output_section; | |
7331 | sym.st_shndx = | |
7332 | _bfd_elf_section_from_bfd_section (output_bfd, | |
7333 | osec); | |
7334 | if (sym.st_shndx == SHN_BAD) | |
7335 | return FALSE; | |
7336 | ||
7337 | sym.st_value += sec->output_offset; | |
7338 | if (! finfo->info->relocatable) | |
7339 | { | |
7340 | sym.st_value += osec->vma; | |
7341 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
7342 | { | |
7343 | /* STT_TLS symbols are relative to PT_TLS | |
7344 | segment base. */ | |
7345 | BFD_ASSERT (elf_hash_table (finfo->info) | |
7346 | ->tls_sec != NULL); | |
7347 | sym.st_value -= (elf_hash_table (finfo->info) | |
7348 | ->tls_sec->vma); | |
7349 | } | |
7350 | } | |
7351 | ||
7352 | finfo->indices[r_symndx] | |
7353 | = bfd_get_symcount (output_bfd); | |
7354 | ||
7355 | if (! elf_link_output_sym (finfo, name, &sym, sec, | |
7356 | NULL)) | |
7357 | return FALSE; | |
7358 | } | |
7359 | ||
7360 | r_symndx = finfo->indices[r_symndx]; | |
7361 | } | |
7362 | ||
7363 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
7364 | | (irela->r_info & r_type_mask)); | |
7365 | } | |
7366 | ||
7367 | /* Swap out the relocs. */ | |
c152c796 | 7368 | if (input_rel_hdr->sh_size != 0 |
eac338cf PB |
7369 | && !bed->elf_backend_emit_relocs (output_bfd, o, |
7370 | input_rel_hdr, | |
7371 | internal_relocs, | |
7372 | rel_hash_list)) | |
c152c796 AM |
7373 | return FALSE; |
7374 | ||
7375 | input_rel_hdr2 = elf_section_data (o)->rel_hdr2; | |
7376 | if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0) | |
7377 | { | |
7378 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) | |
7379 | * bed->s->int_rels_per_ext_rel); | |
eac338cf PB |
7380 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
7381 | if (!bed->elf_backend_emit_relocs (output_bfd, o, | |
7382 | input_rel_hdr2, | |
7383 | internal_relocs, | |
7384 | rel_hash_list)) | |
c152c796 AM |
7385 | return FALSE; |
7386 | } | |
7387 | } | |
7388 | } | |
7389 | ||
7390 | /* Write out the modified section contents. */ | |
7391 | if (bed->elf_backend_write_section | |
7392 | && (*bed->elf_backend_write_section) (output_bfd, o, contents)) | |
7393 | { | |
7394 | /* Section written out. */ | |
7395 | } | |
7396 | else switch (o->sec_info_type) | |
7397 | { | |
7398 | case ELF_INFO_TYPE_STABS: | |
7399 | if (! (_bfd_write_section_stabs | |
7400 | (output_bfd, | |
7401 | &elf_hash_table (finfo->info)->stab_info, | |
7402 | o, &elf_section_data (o)->sec_info, contents))) | |
7403 | return FALSE; | |
7404 | break; | |
7405 | case ELF_INFO_TYPE_MERGE: | |
7406 | if (! _bfd_write_merged_section (output_bfd, o, | |
7407 | elf_section_data (o)->sec_info)) | |
7408 | return FALSE; | |
7409 | break; | |
7410 | case ELF_INFO_TYPE_EH_FRAME: | |
7411 | { | |
7412 | if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, | |
7413 | o, contents)) | |
7414 | return FALSE; | |
7415 | } | |
7416 | break; | |
7417 | default: | |
7418 | { | |
c152c796 AM |
7419 | if (! (o->flags & SEC_EXCLUDE) |
7420 | && ! bfd_set_section_contents (output_bfd, o->output_section, | |
7421 | contents, | |
7422 | (file_ptr) o->output_offset, | |
eea6121a | 7423 | o->size)) |
c152c796 AM |
7424 | return FALSE; |
7425 | } | |
7426 | break; | |
7427 | } | |
7428 | } | |
7429 | ||
7430 | return TRUE; | |
7431 | } | |
7432 | ||
7433 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 7434 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
7435 | is used to build constructor and destructor tables when linking |
7436 | with -Ur. */ | |
7437 | ||
7438 | static bfd_boolean | |
7439 | elf_reloc_link_order (bfd *output_bfd, | |
7440 | struct bfd_link_info *info, | |
7441 | asection *output_section, | |
7442 | struct bfd_link_order *link_order) | |
7443 | { | |
7444 | reloc_howto_type *howto; | |
7445 | long indx; | |
7446 | bfd_vma offset; | |
7447 | bfd_vma addend; | |
7448 | struct elf_link_hash_entry **rel_hash_ptr; | |
7449 | Elf_Internal_Shdr *rel_hdr; | |
7450 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
7451 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
7452 | bfd_byte *erel; | |
7453 | unsigned int i; | |
7454 | ||
7455 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
7456 | if (howto == NULL) | |
7457 | { | |
7458 | bfd_set_error (bfd_error_bad_value); | |
7459 | return FALSE; | |
7460 | } | |
7461 | ||
7462 | addend = link_order->u.reloc.p->addend; | |
7463 | ||
7464 | /* Figure out the symbol index. */ | |
7465 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
7466 | + elf_section_data (output_section)->rel_count | |
7467 | + elf_section_data (output_section)->rel_count2); | |
7468 | if (link_order->type == bfd_section_reloc_link_order) | |
7469 | { | |
7470 | indx = link_order->u.reloc.p->u.section->target_index; | |
7471 | BFD_ASSERT (indx != 0); | |
7472 | *rel_hash_ptr = NULL; | |
7473 | } | |
7474 | else | |
7475 | { | |
7476 | struct elf_link_hash_entry *h; | |
7477 | ||
7478 | /* Treat a reloc against a defined symbol as though it were | |
7479 | actually against the section. */ | |
7480 | h = ((struct elf_link_hash_entry *) | |
7481 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
7482 | link_order->u.reloc.p->u.name, | |
7483 | FALSE, FALSE, TRUE)); | |
7484 | if (h != NULL | |
7485 | && (h->root.type == bfd_link_hash_defined | |
7486 | || h->root.type == bfd_link_hash_defweak)) | |
7487 | { | |
7488 | asection *section; | |
7489 | ||
7490 | section = h->root.u.def.section; | |
7491 | indx = section->output_section->target_index; | |
7492 | *rel_hash_ptr = NULL; | |
7493 | /* It seems that we ought to add the symbol value to the | |
7494 | addend here, but in practice it has already been added | |
7495 | because it was passed to constructor_callback. */ | |
7496 | addend += section->output_section->vma + section->output_offset; | |
7497 | } | |
7498 | else if (h != NULL) | |
7499 | { | |
7500 | /* Setting the index to -2 tells elf_link_output_extsym that | |
7501 | this symbol is used by a reloc. */ | |
7502 | h->indx = -2; | |
7503 | *rel_hash_ptr = h; | |
7504 | indx = 0; | |
7505 | } | |
7506 | else | |
7507 | { | |
7508 | if (! ((*info->callbacks->unattached_reloc) | |
7509 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) | |
7510 | return FALSE; | |
7511 | indx = 0; | |
7512 | } | |
7513 | } | |
7514 | ||
7515 | /* If this is an inplace reloc, we must write the addend into the | |
7516 | object file. */ | |
7517 | if (howto->partial_inplace && addend != 0) | |
7518 | { | |
7519 | bfd_size_type size; | |
7520 | bfd_reloc_status_type rstat; | |
7521 | bfd_byte *buf; | |
7522 | bfd_boolean ok; | |
7523 | const char *sym_name; | |
7524 | ||
7525 | size = bfd_get_reloc_size (howto); | |
7526 | buf = bfd_zmalloc (size); | |
7527 | if (buf == NULL) | |
7528 | return FALSE; | |
7529 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
7530 | switch (rstat) | |
7531 | { | |
7532 | case bfd_reloc_ok: | |
7533 | break; | |
7534 | ||
7535 | default: | |
7536 | case bfd_reloc_outofrange: | |
7537 | abort (); | |
7538 | ||
7539 | case bfd_reloc_overflow: | |
7540 | if (link_order->type == bfd_section_reloc_link_order) | |
7541 | sym_name = bfd_section_name (output_bfd, | |
7542 | link_order->u.reloc.p->u.section); | |
7543 | else | |
7544 | sym_name = link_order->u.reloc.p->u.name; | |
7545 | if (! ((*info->callbacks->reloc_overflow) | |
dfeffb9f L |
7546 | (info, NULL, sym_name, howto->name, addend, NULL, |
7547 | NULL, (bfd_vma) 0))) | |
c152c796 AM |
7548 | { |
7549 | free (buf); | |
7550 | return FALSE; | |
7551 | } | |
7552 | break; | |
7553 | } | |
7554 | ok = bfd_set_section_contents (output_bfd, output_section, buf, | |
7555 | link_order->offset, size); | |
7556 | free (buf); | |
7557 | if (! ok) | |
7558 | return FALSE; | |
7559 | } | |
7560 | ||
7561 | /* The address of a reloc is relative to the section in a | |
7562 | relocatable file, and is a virtual address in an executable | |
7563 | file. */ | |
7564 | offset = link_order->offset; | |
7565 | if (! info->relocatable) | |
7566 | offset += output_section->vma; | |
7567 | ||
7568 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
7569 | { | |
7570 | irel[i].r_offset = offset; | |
7571 | irel[i].r_info = 0; | |
7572 | irel[i].r_addend = 0; | |
7573 | } | |
7574 | if (bed->s->arch_size == 32) | |
7575 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
7576 | else | |
7577 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
7578 | ||
7579 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
7580 | erel = rel_hdr->contents; | |
7581 | if (rel_hdr->sh_type == SHT_REL) | |
7582 | { | |
7583 | erel += (elf_section_data (output_section)->rel_count | |
7584 | * bed->s->sizeof_rel); | |
7585 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); | |
7586 | } | |
7587 | else | |
7588 | { | |
7589 | irel[0].r_addend = addend; | |
7590 | erel += (elf_section_data (output_section)->rel_count | |
7591 | * bed->s->sizeof_rela); | |
7592 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); | |
7593 | } | |
7594 | ||
7595 | ++elf_section_data (output_section)->rel_count; | |
7596 | ||
7597 | return TRUE; | |
7598 | } | |
7599 | ||
0b52efa6 PB |
7600 | |
7601 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
7602 | ||
7603 | static bfd_vma | |
7604 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
7605 | { | |
7606 | Elf_Internal_Shdr **elf_shdrp; | |
7607 | asection *s; | |
7608 | int elfsec; | |
7609 | ||
7610 | s = p->u.indirect.section; | |
7611 | elf_shdrp = elf_elfsections (s->owner); | |
7612 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
7613 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
7614 | /* PR 290: |
7615 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 7616 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
7617 | sh_info fields. Hence we could get the situation |
7618 | where elfsec is 0. */ | |
7619 | if (elfsec == 0) | |
7620 | { | |
7621 | const struct elf_backend_data *bed | |
7622 | = get_elf_backend_data (s->owner); | |
7623 | if (bed->link_order_error_handler) | |
d003868e AM |
7624 | bed->link_order_error_handler |
7625 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); | |
185d09ad L |
7626 | return 0; |
7627 | } | |
7628 | else | |
7629 | { | |
7630 | s = elf_shdrp[elfsec]->bfd_section; | |
7631 | return s->output_section->vma + s->output_offset; | |
7632 | } | |
0b52efa6 PB |
7633 | } |
7634 | ||
7635 | ||
7636 | /* Compare two sections based on the locations of the sections they are | |
7637 | linked to. Used by elf_fixup_link_order. */ | |
7638 | ||
7639 | static int | |
7640 | compare_link_order (const void * a, const void * b) | |
7641 | { | |
7642 | bfd_vma apos; | |
7643 | bfd_vma bpos; | |
7644 | ||
7645 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
7646 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
7647 | if (apos < bpos) | |
7648 | return -1; | |
7649 | return apos > bpos; | |
7650 | } | |
7651 | ||
7652 | ||
7653 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
7654 | order as their linked sections. Returns false if this could not be done | |
7655 | because an output section includes both ordered and unordered | |
7656 | sections. Ideally we'd do this in the linker proper. */ | |
7657 | ||
7658 | static bfd_boolean | |
7659 | elf_fixup_link_order (bfd *abfd, asection *o) | |
7660 | { | |
7661 | int seen_linkorder; | |
7662 | int seen_other; | |
7663 | int n; | |
7664 | struct bfd_link_order *p; | |
7665 | bfd *sub; | |
7666 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7667 | int elfsec; | |
7668 | struct bfd_link_order **sections; | |
d33cdfe3 | 7669 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 7670 | bfd_vma offset; |
3b36f7e6 | 7671 | |
d33cdfe3 L |
7672 | other_sec = NULL; |
7673 | linkorder_sec = NULL; | |
0b52efa6 PB |
7674 | seen_other = 0; |
7675 | seen_linkorder = 0; | |
8423293d | 7676 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 7677 | { |
d33cdfe3 | 7678 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
7679 | { |
7680 | s = p->u.indirect.section; | |
d33cdfe3 L |
7681 | sub = s->owner; |
7682 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
7683 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
7684 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) != -1 | |
0b52efa6 | 7685 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER) |
d33cdfe3 L |
7686 | { |
7687 | seen_linkorder++; | |
7688 | linkorder_sec = s; | |
7689 | } | |
0b52efa6 | 7690 | else |
d33cdfe3 L |
7691 | { |
7692 | seen_other++; | |
7693 | other_sec = s; | |
7694 | } | |
0b52efa6 PB |
7695 | } |
7696 | else | |
7697 | seen_other++; | |
d33cdfe3 L |
7698 | |
7699 | if (seen_other && seen_linkorder) | |
7700 | { | |
7701 | if (other_sec && linkorder_sec) | |
7702 | (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"), | |
7703 | o, linkorder_sec, | |
7704 | linkorder_sec->owner, other_sec, | |
7705 | other_sec->owner); | |
7706 | else | |
7707 | (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), | |
7708 | o); | |
7709 | bfd_set_error (bfd_error_bad_value); | |
7710 | return FALSE; | |
7711 | } | |
0b52efa6 PB |
7712 | } |
7713 | ||
7714 | if (!seen_linkorder) | |
7715 | return TRUE; | |
7716 | ||
0b52efa6 PB |
7717 | sections = (struct bfd_link_order **) |
7718 | xmalloc (seen_linkorder * sizeof (struct bfd_link_order *)); | |
7719 | seen_linkorder = 0; | |
3b36f7e6 | 7720 | |
8423293d | 7721 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
7722 | { |
7723 | sections[seen_linkorder++] = p; | |
7724 | } | |
7725 | /* Sort the input sections in the order of their linked section. */ | |
7726 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
7727 | compare_link_order); | |
7728 | ||
7729 | /* Change the offsets of the sections. */ | |
7730 | offset = 0; | |
7731 | for (n = 0; n < seen_linkorder; n++) | |
7732 | { | |
7733 | s = sections[n]->u.indirect.section; | |
7734 | offset &= ~(bfd_vma)((1 << s->alignment_power) - 1); | |
7735 | s->output_offset = offset; | |
7736 | sections[n]->offset = offset; | |
7737 | offset += sections[n]->size; | |
7738 | } | |
7739 | ||
7740 | return TRUE; | |
7741 | } | |
7742 | ||
7743 | ||
c152c796 AM |
7744 | /* Do the final step of an ELF link. */ |
7745 | ||
7746 | bfd_boolean | |
7747 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
7748 | { | |
7749 | bfd_boolean dynamic; | |
7750 | bfd_boolean emit_relocs; | |
7751 | bfd *dynobj; | |
7752 | struct elf_final_link_info finfo; | |
7753 | register asection *o; | |
7754 | register struct bfd_link_order *p; | |
7755 | register bfd *sub; | |
7756 | bfd_size_type max_contents_size; | |
7757 | bfd_size_type max_external_reloc_size; | |
7758 | bfd_size_type max_internal_reloc_count; | |
7759 | bfd_size_type max_sym_count; | |
7760 | bfd_size_type max_sym_shndx_count; | |
7761 | file_ptr off; | |
7762 | Elf_Internal_Sym elfsym; | |
7763 | unsigned int i; | |
7764 | Elf_Internal_Shdr *symtab_hdr; | |
7765 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
7766 | Elf_Internal_Shdr *symstrtab_hdr; | |
7767 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7768 | struct elf_outext_info eoinfo; | |
7769 | bfd_boolean merged; | |
7770 | size_t relativecount = 0; | |
7771 | asection *reldyn = 0; | |
7772 | bfd_size_type amt; | |
7773 | ||
7774 | if (! is_elf_hash_table (info->hash)) | |
7775 | return FALSE; | |
7776 | ||
7777 | if (info->shared) | |
7778 | abfd->flags |= DYNAMIC; | |
7779 | ||
7780 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
7781 | dynobj = elf_hash_table (info)->dynobj; | |
7782 | ||
7783 | emit_relocs = (info->relocatable | |
7784 | || info->emitrelocations | |
7785 | || bed->elf_backend_emit_relocs); | |
7786 | ||
7787 | finfo.info = info; | |
7788 | finfo.output_bfd = abfd; | |
7789 | finfo.symstrtab = _bfd_elf_stringtab_init (); | |
7790 | if (finfo.symstrtab == NULL) | |
7791 | return FALSE; | |
7792 | ||
7793 | if (! dynamic) | |
7794 | { | |
7795 | finfo.dynsym_sec = NULL; | |
7796 | finfo.hash_sec = NULL; | |
7797 | finfo.symver_sec = NULL; | |
7798 | } | |
7799 | else | |
7800 | { | |
7801 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
7802 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
7803 | BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL); | |
7804 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
7805 | /* Note that it is OK if symver_sec is NULL. */ | |
7806 | } | |
7807 | ||
7808 | finfo.contents = NULL; | |
7809 | finfo.external_relocs = NULL; | |
7810 | finfo.internal_relocs = NULL; | |
7811 | finfo.external_syms = NULL; | |
7812 | finfo.locsym_shndx = NULL; | |
7813 | finfo.internal_syms = NULL; | |
7814 | finfo.indices = NULL; | |
7815 | finfo.sections = NULL; | |
7816 | finfo.symbuf = NULL; | |
7817 | finfo.symshndxbuf = NULL; | |
7818 | finfo.symbuf_count = 0; | |
7819 | finfo.shndxbuf_size = 0; | |
7820 | ||
7821 | /* Count up the number of relocations we will output for each output | |
7822 | section, so that we know the sizes of the reloc sections. We | |
7823 | also figure out some maximum sizes. */ | |
7824 | max_contents_size = 0; | |
7825 | max_external_reloc_size = 0; | |
7826 | max_internal_reloc_count = 0; | |
7827 | max_sym_count = 0; | |
7828 | max_sym_shndx_count = 0; | |
7829 | merged = FALSE; | |
7830 | for (o = abfd->sections; o != NULL; o = o->next) | |
7831 | { | |
7832 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
7833 | o->reloc_count = 0; | |
7834 | ||
8423293d | 7835 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
7836 | { |
7837 | unsigned int reloc_count = 0; | |
7838 | struct bfd_elf_section_data *esdi = NULL; | |
7839 | unsigned int *rel_count1; | |
7840 | ||
7841 | if (p->type == bfd_section_reloc_link_order | |
7842 | || p->type == bfd_symbol_reloc_link_order) | |
7843 | reloc_count = 1; | |
7844 | else if (p->type == bfd_indirect_link_order) | |
7845 | { | |
7846 | asection *sec; | |
7847 | ||
7848 | sec = p->u.indirect.section; | |
7849 | esdi = elf_section_data (sec); | |
7850 | ||
7851 | /* Mark all sections which are to be included in the | |
7852 | link. This will normally be every section. We need | |
7853 | to do this so that we can identify any sections which | |
7854 | the linker has decided to not include. */ | |
7855 | sec->linker_mark = TRUE; | |
7856 | ||
7857 | if (sec->flags & SEC_MERGE) | |
7858 | merged = TRUE; | |
7859 | ||
7860 | if (info->relocatable || info->emitrelocations) | |
7861 | reloc_count = sec->reloc_count; | |
7862 | else if (bed->elf_backend_count_relocs) | |
7863 | { | |
7864 | Elf_Internal_Rela * relocs; | |
7865 | ||
7866 | relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
7867 | info->keep_memory); | |
7868 | ||
7869 | reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs); | |
7870 | ||
7871 | if (elf_section_data (o)->relocs != relocs) | |
7872 | free (relocs); | |
7873 | } | |
7874 | ||
eea6121a AM |
7875 | if (sec->rawsize > max_contents_size) |
7876 | max_contents_size = sec->rawsize; | |
7877 | if (sec->size > max_contents_size) | |
7878 | max_contents_size = sec->size; | |
c152c796 AM |
7879 | |
7880 | /* We are interested in just local symbols, not all | |
7881 | symbols. */ | |
7882 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
7883 | && (sec->owner->flags & DYNAMIC) == 0) | |
7884 | { | |
7885 | size_t sym_count; | |
7886 | ||
7887 | if (elf_bad_symtab (sec->owner)) | |
7888 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
7889 | / bed->s->sizeof_sym); | |
7890 | else | |
7891 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
7892 | ||
7893 | if (sym_count > max_sym_count) | |
7894 | max_sym_count = sym_count; | |
7895 | ||
7896 | if (sym_count > max_sym_shndx_count | |
7897 | && elf_symtab_shndx (sec->owner) != 0) | |
7898 | max_sym_shndx_count = sym_count; | |
7899 | ||
7900 | if ((sec->flags & SEC_RELOC) != 0) | |
7901 | { | |
7902 | size_t ext_size; | |
7903 | ||
7904 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
7905 | if (ext_size > max_external_reloc_size) | |
7906 | max_external_reloc_size = ext_size; | |
7907 | if (sec->reloc_count > max_internal_reloc_count) | |
7908 | max_internal_reloc_count = sec->reloc_count; | |
7909 | } | |
7910 | } | |
7911 | } | |
7912 | ||
7913 | if (reloc_count == 0) | |
7914 | continue; | |
7915 | ||
7916 | o->reloc_count += reloc_count; | |
7917 | ||
7918 | /* MIPS may have a mix of REL and RELA relocs on sections. | |
7919 | To support this curious ABI we keep reloc counts in | |
7920 | elf_section_data too. We must be careful to add the | |
7921 | relocations from the input section to the right output | |
7922 | count. FIXME: Get rid of one count. We have | |
7923 | o->reloc_count == esdo->rel_count + esdo->rel_count2. */ | |
7924 | rel_count1 = &esdo->rel_count; | |
7925 | if (esdi != NULL) | |
7926 | { | |
7927 | bfd_boolean same_size; | |
7928 | bfd_size_type entsize1; | |
7929 | ||
7930 | entsize1 = esdi->rel_hdr.sh_entsize; | |
7931 | BFD_ASSERT (entsize1 == bed->s->sizeof_rel | |
7932 | || entsize1 == bed->s->sizeof_rela); | |
7933 | same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel); | |
7934 | ||
7935 | if (!same_size) | |
7936 | rel_count1 = &esdo->rel_count2; | |
7937 | ||
7938 | if (esdi->rel_hdr2 != NULL) | |
7939 | { | |
7940 | bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize; | |
7941 | unsigned int alt_count; | |
7942 | unsigned int *rel_count2; | |
7943 | ||
7944 | BFD_ASSERT (entsize2 != entsize1 | |
7945 | && (entsize2 == bed->s->sizeof_rel | |
7946 | || entsize2 == bed->s->sizeof_rela)); | |
7947 | ||
7948 | rel_count2 = &esdo->rel_count2; | |
7949 | if (!same_size) | |
7950 | rel_count2 = &esdo->rel_count; | |
7951 | ||
7952 | /* The following is probably too simplistic if the | |
7953 | backend counts output relocs unusually. */ | |
7954 | BFD_ASSERT (bed->elf_backend_count_relocs == NULL); | |
7955 | alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2); | |
7956 | *rel_count2 += alt_count; | |
7957 | reloc_count -= alt_count; | |
7958 | } | |
7959 | } | |
7960 | *rel_count1 += reloc_count; | |
7961 | } | |
7962 | ||
7963 | if (o->reloc_count > 0) | |
7964 | o->flags |= SEC_RELOC; | |
7965 | else | |
7966 | { | |
7967 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
7968 | set it (this is probably a bug) and if it is set | |
7969 | assign_section_numbers will create a reloc section. */ | |
7970 | o->flags &=~ SEC_RELOC; | |
7971 | } | |
7972 | ||
7973 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
7974 | zero. This is done in elf_fake_sections as well, but forcing | |
7975 | the VMA to 0 here will ensure that relocs against these | |
7976 | sections are handled correctly. */ | |
7977 | if ((o->flags & SEC_ALLOC) == 0 | |
7978 | && ! o->user_set_vma) | |
7979 | o->vma = 0; | |
7980 | } | |
7981 | ||
7982 | if (! info->relocatable && merged) | |
7983 | elf_link_hash_traverse (elf_hash_table (info), | |
7984 | _bfd_elf_link_sec_merge_syms, abfd); | |
7985 | ||
7986 | /* Figure out the file positions for everything but the symbol table | |
7987 | and the relocs. We set symcount to force assign_section_numbers | |
7988 | to create a symbol table. */ | |
7989 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; | |
7990 | BFD_ASSERT (! abfd->output_has_begun); | |
7991 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
7992 | goto error_return; | |
7993 | ||
ee75fd95 | 7994 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
7995 | for (o = abfd->sections; o != NULL; o = o->next) |
7996 | { | |
7997 | if ((o->flags & SEC_RELOC) != 0) | |
7998 | { | |
7999 | if (!(_bfd_elf_link_size_reloc_section | |
8000 | (abfd, &elf_section_data (o)->rel_hdr, o))) | |
8001 | goto error_return; | |
8002 | ||
8003 | if (elf_section_data (o)->rel_hdr2 | |
8004 | && !(_bfd_elf_link_size_reloc_section | |
8005 | (abfd, elf_section_data (o)->rel_hdr2, o))) | |
8006 | goto error_return; | |
8007 | } | |
8008 | ||
8009 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
8010 | to count upwards while actually outputting the relocations. */ | |
8011 | elf_section_data (o)->rel_count = 0; | |
8012 | elf_section_data (o)->rel_count2 = 0; | |
8013 | } | |
8014 | ||
8015 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
8016 | ||
8017 | /* We have now assigned file positions for all the sections except | |
8018 | .symtab and .strtab. We start the .symtab section at the current | |
8019 | file position, and write directly to it. We build the .strtab | |
8020 | section in memory. */ | |
8021 | bfd_get_symcount (abfd) = 0; | |
8022 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
8023 | /* sh_name is set in prep_headers. */ | |
8024 | symtab_hdr->sh_type = SHT_SYMTAB; | |
8025 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
8026 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
8027 | /* sh_link is set in assign_section_numbers. */ | |
8028 | /* sh_info is set below. */ | |
8029 | /* sh_offset is set just below. */ | |
8030 | symtab_hdr->sh_addralign = 1 << bed->s->log_file_align; | |
8031 | ||
8032 | off = elf_tdata (abfd)->next_file_pos; | |
8033 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
8034 | ||
8035 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
8036 | incorrect. We do not yet know the size of the .symtab section. | |
8037 | We correct next_file_pos below, after we do know the size. */ | |
8038 | ||
8039 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
8040 | continuously seeking to the right position in the file. */ | |
8041 | if (! info->keep_memory || max_sym_count < 20) | |
8042 | finfo.symbuf_size = 20; | |
8043 | else | |
8044 | finfo.symbuf_size = max_sym_count; | |
8045 | amt = finfo.symbuf_size; | |
8046 | amt *= bed->s->sizeof_sym; | |
8047 | finfo.symbuf = bfd_malloc (amt); | |
8048 | if (finfo.symbuf == NULL) | |
8049 | goto error_return; | |
8050 | if (elf_numsections (abfd) > SHN_LORESERVE) | |
8051 | { | |
8052 | /* Wild guess at number of output symbols. realloc'd as needed. */ | |
8053 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
8054 | finfo.shndxbuf_size = amt; | |
8055 | amt *= sizeof (Elf_External_Sym_Shndx); | |
8056 | finfo.symshndxbuf = bfd_zmalloc (amt); | |
8057 | if (finfo.symshndxbuf == NULL) | |
8058 | goto error_return; | |
8059 | } | |
8060 | ||
8061 | /* Start writing out the symbol table. The first symbol is always a | |
8062 | dummy symbol. */ | |
8063 | if (info->strip != strip_all | |
8064 | || emit_relocs) | |
8065 | { | |
8066 | elfsym.st_value = 0; | |
8067 | elfsym.st_size = 0; | |
8068 | elfsym.st_info = 0; | |
8069 | elfsym.st_other = 0; | |
8070 | elfsym.st_shndx = SHN_UNDEF; | |
8071 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr, | |
8072 | NULL)) | |
8073 | goto error_return; | |
8074 | } | |
8075 | ||
c152c796 AM |
8076 | /* Output a symbol for each section. We output these even if we are |
8077 | discarding local symbols, since they are used for relocs. These | |
8078 | symbols have no names. We store the index of each one in the | |
8079 | index field of the section, so that we can find it again when | |
8080 | outputting relocs. */ | |
8081 | if (info->strip != strip_all | |
8082 | || emit_relocs) | |
8083 | { | |
8084 | elfsym.st_size = 0; | |
8085 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8086 | elfsym.st_other = 0; | |
8087 | for (i = 1; i < elf_numsections (abfd); i++) | |
8088 | { | |
8089 | o = bfd_section_from_elf_index (abfd, i); | |
8090 | if (o != NULL) | |
8091 | o->target_index = bfd_get_symcount (abfd); | |
8092 | elfsym.st_shndx = i; | |
8093 | if (info->relocatable || o == NULL) | |
8094 | elfsym.st_value = 0; | |
8095 | else | |
8096 | elfsym.st_value = o->vma; | |
8097 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL)) | |
8098 | goto error_return; | |
8099 | if (i == SHN_LORESERVE - 1) | |
8100 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
8101 | } | |
8102 | } | |
8103 | ||
8104 | /* Allocate some memory to hold information read in from the input | |
8105 | files. */ | |
8106 | if (max_contents_size != 0) | |
8107 | { | |
8108 | finfo.contents = bfd_malloc (max_contents_size); | |
8109 | if (finfo.contents == NULL) | |
8110 | goto error_return; | |
8111 | } | |
8112 | ||
8113 | if (max_external_reloc_size != 0) | |
8114 | { | |
8115 | finfo.external_relocs = bfd_malloc (max_external_reloc_size); | |
8116 | if (finfo.external_relocs == NULL) | |
8117 | goto error_return; | |
8118 | } | |
8119 | ||
8120 | if (max_internal_reloc_count != 0) | |
8121 | { | |
8122 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
8123 | amt *= sizeof (Elf_Internal_Rela); | |
8124 | finfo.internal_relocs = bfd_malloc (amt); | |
8125 | if (finfo.internal_relocs == NULL) | |
8126 | goto error_return; | |
8127 | } | |
8128 | ||
8129 | if (max_sym_count != 0) | |
8130 | { | |
8131 | amt = max_sym_count * bed->s->sizeof_sym; | |
8132 | finfo.external_syms = bfd_malloc (amt); | |
8133 | if (finfo.external_syms == NULL) | |
8134 | goto error_return; | |
8135 | ||
8136 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8137 | finfo.internal_syms = bfd_malloc (amt); | |
8138 | if (finfo.internal_syms == NULL) | |
8139 | goto error_return; | |
8140 | ||
8141 | amt = max_sym_count * sizeof (long); | |
8142 | finfo.indices = bfd_malloc (amt); | |
8143 | if (finfo.indices == NULL) | |
8144 | goto error_return; | |
8145 | ||
8146 | amt = max_sym_count * sizeof (asection *); | |
8147 | finfo.sections = bfd_malloc (amt); | |
8148 | if (finfo.sections == NULL) | |
8149 | goto error_return; | |
8150 | } | |
8151 | ||
8152 | if (max_sym_shndx_count != 0) | |
8153 | { | |
8154 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8155 | finfo.locsym_shndx = bfd_malloc (amt); | |
8156 | if (finfo.locsym_shndx == NULL) | |
8157 | goto error_return; | |
8158 | } | |
8159 | ||
8160 | if (elf_hash_table (info)->tls_sec) | |
8161 | { | |
8162 | bfd_vma base, end = 0; | |
8163 | asection *sec; | |
8164 | ||
8165 | for (sec = elf_hash_table (info)->tls_sec; | |
8166 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
8167 | sec = sec->next) | |
8168 | { | |
3a800eb9 | 8169 | bfd_size_type size = sec->size; |
c152c796 | 8170 | |
3a800eb9 AM |
8171 | if (size == 0 |
8172 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 8173 | { |
3a800eb9 AM |
8174 | struct bfd_link_order *o = sec->map_tail.link_order; |
8175 | if (o != NULL) | |
8176 | size = o->offset + o->size; | |
c152c796 AM |
8177 | } |
8178 | end = sec->vma + size; | |
8179 | } | |
8180 | base = elf_hash_table (info)->tls_sec->vma; | |
8181 | end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power); | |
8182 | elf_hash_table (info)->tls_size = end - base; | |
8183 | } | |
8184 | ||
0b52efa6 PB |
8185 | /* Reorder SHF_LINK_ORDER sections. */ |
8186 | for (o = abfd->sections; o != NULL; o = o->next) | |
8187 | { | |
8188 | if (!elf_fixup_link_order (abfd, o)) | |
8189 | return FALSE; | |
8190 | } | |
8191 | ||
c152c796 AM |
8192 | /* Since ELF permits relocations to be against local symbols, we |
8193 | must have the local symbols available when we do the relocations. | |
8194 | Since we would rather only read the local symbols once, and we | |
8195 | would rather not keep them in memory, we handle all the | |
8196 | relocations for a single input file at the same time. | |
8197 | ||
8198 | Unfortunately, there is no way to know the total number of local | |
8199 | symbols until we have seen all of them, and the local symbol | |
8200 | indices precede the global symbol indices. This means that when | |
8201 | we are generating relocatable output, and we see a reloc against | |
8202 | a global symbol, we can not know the symbol index until we have | |
8203 | finished examining all the local symbols to see which ones we are | |
8204 | going to output. To deal with this, we keep the relocations in | |
8205 | memory, and don't output them until the end of the link. This is | |
8206 | an unfortunate waste of memory, but I don't see a good way around | |
8207 | it. Fortunately, it only happens when performing a relocatable | |
8208 | link, which is not the common case. FIXME: If keep_memory is set | |
8209 | we could write the relocs out and then read them again; I don't | |
8210 | know how bad the memory loss will be. */ | |
8211 | ||
8212 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8213 | sub->output_has_begun = FALSE; | |
8214 | for (o = abfd->sections; o != NULL; o = o->next) | |
8215 | { | |
8423293d | 8216 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
8217 | { |
8218 | if (p->type == bfd_indirect_link_order | |
8219 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
8220 | == bfd_target_elf_flavour) | |
8221 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
8222 | { | |
8223 | if (! sub->output_has_begun) | |
8224 | { | |
8225 | if (! elf_link_input_bfd (&finfo, sub)) | |
8226 | goto error_return; | |
8227 | sub->output_has_begun = TRUE; | |
8228 | } | |
8229 | } | |
8230 | else if (p->type == bfd_section_reloc_link_order | |
8231 | || p->type == bfd_symbol_reloc_link_order) | |
8232 | { | |
8233 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
8234 | goto error_return; | |
8235 | } | |
8236 | else | |
8237 | { | |
8238 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
8239 | goto error_return; | |
8240 | } | |
8241 | } | |
8242 | } | |
8243 | ||
8244 | /* Output any global symbols that got converted to local in a | |
8245 | version script or due to symbol visibility. We do this in a | |
8246 | separate step since ELF requires all local symbols to appear | |
8247 | prior to any global symbols. FIXME: We should only do this if | |
8248 | some global symbols were, in fact, converted to become local. | |
8249 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
8250 | eoinfo.failed = FALSE; | |
8251 | eoinfo.finfo = &finfo; | |
8252 | eoinfo.localsyms = TRUE; | |
8253 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8254 | &eoinfo); | |
8255 | if (eoinfo.failed) | |
8256 | return FALSE; | |
8257 | ||
8258 | /* That wrote out all the local symbols. Finish up the symbol table | |
8259 | with the global symbols. Even if we want to strip everything we | |
8260 | can, we still need to deal with those global symbols that got | |
8261 | converted to local in a version script. */ | |
8262 | ||
8263 | /* The sh_info field records the index of the first non local symbol. */ | |
8264 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
8265 | ||
8266 | if (dynamic | |
8267 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
8268 | { | |
8269 | Elf_Internal_Sym sym; | |
8270 | bfd_byte *dynsym = finfo.dynsym_sec->contents; | |
8271 | long last_local = 0; | |
8272 | ||
8273 | /* Write out the section symbols for the output sections. */ | |
67687978 | 8274 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
c152c796 AM |
8275 | { |
8276 | asection *s; | |
8277 | ||
8278 | sym.st_size = 0; | |
8279 | sym.st_name = 0; | |
8280 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8281 | sym.st_other = 0; | |
8282 | ||
8283 | for (s = abfd->sections; s != NULL; s = s->next) | |
8284 | { | |
8285 | int indx; | |
8286 | bfd_byte *dest; | |
8287 | long dynindx; | |
8288 | ||
c152c796 | 8289 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
8290 | if (dynindx <= 0) |
8291 | continue; | |
8292 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
8293 | BFD_ASSERT (indx > 0); |
8294 | sym.st_shndx = indx; | |
8295 | sym.st_value = s->vma; | |
8296 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
8c37241b JJ |
8297 | if (last_local < dynindx) |
8298 | last_local = dynindx; | |
c152c796 AM |
8299 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
8300 | } | |
c152c796 AM |
8301 | } |
8302 | ||
8303 | /* Write out the local dynsyms. */ | |
8304 | if (elf_hash_table (info)->dynlocal) | |
8305 | { | |
8306 | struct elf_link_local_dynamic_entry *e; | |
8307 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
8308 | { | |
8309 | asection *s; | |
8310 | bfd_byte *dest; | |
8311 | ||
8312 | sym.st_size = e->isym.st_size; | |
8313 | sym.st_other = e->isym.st_other; | |
8314 | ||
8315 | /* Copy the internal symbol as is. | |
8316 | Note that we saved a word of storage and overwrote | |
8317 | the original st_name with the dynstr_index. */ | |
8318 | sym = e->isym; | |
8319 | ||
8320 | if (e->isym.st_shndx != SHN_UNDEF | |
8321 | && (e->isym.st_shndx < SHN_LORESERVE | |
8322 | || e->isym.st_shndx > SHN_HIRESERVE)) | |
8323 | { | |
8324 | s = bfd_section_from_elf_index (e->input_bfd, | |
8325 | e->isym.st_shndx); | |
8326 | ||
8327 | sym.st_shndx = | |
8328 | elf_section_data (s->output_section)->this_idx; | |
8329 | sym.st_value = (s->output_section->vma | |
8330 | + s->output_offset | |
8331 | + e->isym.st_value); | |
8332 | } | |
8333 | ||
8334 | if (last_local < e->dynindx) | |
8335 | last_local = e->dynindx; | |
8336 | ||
8337 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
8338 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
8339 | } | |
8340 | } | |
8341 | ||
8342 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = | |
8343 | last_local + 1; | |
8344 | } | |
8345 | ||
8346 | /* We get the global symbols from the hash table. */ | |
8347 | eoinfo.failed = FALSE; | |
8348 | eoinfo.localsyms = FALSE; | |
8349 | eoinfo.finfo = &finfo; | |
8350 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8351 | &eoinfo); | |
8352 | if (eoinfo.failed) | |
8353 | return FALSE; | |
8354 | ||
8355 | /* If backend needs to output some symbols not present in the hash | |
8356 | table, do it now. */ | |
8357 | if (bed->elf_backend_output_arch_syms) | |
8358 | { | |
8359 | typedef bfd_boolean (*out_sym_func) | |
8360 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
8361 | struct elf_link_hash_entry *); | |
8362 | ||
8363 | if (! ((*bed->elf_backend_output_arch_syms) | |
8364 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
8365 | return FALSE; | |
8366 | } | |
8367 | ||
8368 | /* Flush all symbols to the file. */ | |
8369 | if (! elf_link_flush_output_syms (&finfo, bed)) | |
8370 | return FALSE; | |
8371 | ||
8372 | /* Now we know the size of the symtab section. */ | |
8373 | off += symtab_hdr->sh_size; | |
8374 | ||
8375 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
8376 | if (symtab_shndx_hdr->sh_name != 0) | |
8377 | { | |
8378 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
8379 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
8380 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
8381 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
8382 | symtab_shndx_hdr->sh_size = amt; | |
8383 | ||
8384 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
8385 | off, TRUE); | |
8386 | ||
8387 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
8388 | || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) | |
8389 | return FALSE; | |
8390 | } | |
8391 | ||
8392 | ||
8393 | /* Finish up and write out the symbol string table (.strtab) | |
8394 | section. */ | |
8395 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
8396 | /* sh_name was set in prep_headers. */ | |
8397 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
8398 | symstrtab_hdr->sh_flags = 0; | |
8399 | symstrtab_hdr->sh_addr = 0; | |
8400 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
8401 | symstrtab_hdr->sh_entsize = 0; | |
8402 | symstrtab_hdr->sh_link = 0; | |
8403 | symstrtab_hdr->sh_info = 0; | |
8404 | /* sh_offset is set just below. */ | |
8405 | symstrtab_hdr->sh_addralign = 1; | |
8406 | ||
8407 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); | |
8408 | elf_tdata (abfd)->next_file_pos = off; | |
8409 | ||
8410 | if (bfd_get_symcount (abfd) > 0) | |
8411 | { | |
8412 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
8413 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
8414 | return FALSE; | |
8415 | } | |
8416 | ||
8417 | /* Adjust the relocs to have the correct symbol indices. */ | |
8418 | for (o = abfd->sections; o != NULL; o = o->next) | |
8419 | { | |
8420 | if ((o->flags & SEC_RELOC) == 0) | |
8421 | continue; | |
8422 | ||
8423 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, | |
8424 | elf_section_data (o)->rel_count, | |
8425 | elf_section_data (o)->rel_hashes); | |
8426 | if (elf_section_data (o)->rel_hdr2 != NULL) | |
8427 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, | |
8428 | elf_section_data (o)->rel_count2, | |
8429 | (elf_section_data (o)->rel_hashes | |
8430 | + elf_section_data (o)->rel_count)); | |
8431 | ||
8432 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
8433 | trying to swap the relocs out itself. */ | |
8434 | o->reloc_count = 0; | |
8435 | } | |
8436 | ||
8437 | if (dynamic && info->combreloc && dynobj != NULL) | |
8438 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
8439 | ||
8440 | /* If we are linking against a dynamic object, or generating a | |
8441 | shared library, finish up the dynamic linking information. */ | |
8442 | if (dynamic) | |
8443 | { | |
8444 | bfd_byte *dyncon, *dynconend; | |
8445 | ||
8446 | /* Fix up .dynamic entries. */ | |
8447 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
8448 | BFD_ASSERT (o != NULL); | |
8449 | ||
8450 | dyncon = o->contents; | |
eea6121a | 8451 | dynconend = o->contents + o->size; |
c152c796 AM |
8452 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
8453 | { | |
8454 | Elf_Internal_Dyn dyn; | |
8455 | const char *name; | |
8456 | unsigned int type; | |
8457 | ||
8458 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
8459 | ||
8460 | switch (dyn.d_tag) | |
8461 | { | |
8462 | default: | |
8463 | continue; | |
8464 | case DT_NULL: | |
8465 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
8466 | { | |
8467 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
8468 | { | |
8469 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
8470 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
8471 | default: continue; | |
8472 | } | |
8473 | dyn.d_un.d_val = relativecount; | |
8474 | relativecount = 0; | |
8475 | break; | |
8476 | } | |
8477 | continue; | |
8478 | ||
8479 | case DT_INIT: | |
8480 | name = info->init_function; | |
8481 | goto get_sym; | |
8482 | case DT_FINI: | |
8483 | name = info->fini_function; | |
8484 | get_sym: | |
8485 | { | |
8486 | struct elf_link_hash_entry *h; | |
8487 | ||
8488 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
8489 | FALSE, FALSE, TRUE); | |
8490 | if (h != NULL | |
8491 | && (h->root.type == bfd_link_hash_defined | |
8492 | || h->root.type == bfd_link_hash_defweak)) | |
8493 | { | |
8494 | dyn.d_un.d_val = h->root.u.def.value; | |
8495 | o = h->root.u.def.section; | |
8496 | if (o->output_section != NULL) | |
8497 | dyn.d_un.d_val += (o->output_section->vma | |
8498 | + o->output_offset); | |
8499 | else | |
8500 | { | |
8501 | /* The symbol is imported from another shared | |
8502 | library and does not apply to this one. */ | |
8503 | dyn.d_un.d_val = 0; | |
8504 | } | |
8505 | break; | |
8506 | } | |
8507 | } | |
8508 | continue; | |
8509 | ||
8510 | case DT_PREINIT_ARRAYSZ: | |
8511 | name = ".preinit_array"; | |
8512 | goto get_size; | |
8513 | case DT_INIT_ARRAYSZ: | |
8514 | name = ".init_array"; | |
8515 | goto get_size; | |
8516 | case DT_FINI_ARRAYSZ: | |
8517 | name = ".fini_array"; | |
8518 | get_size: | |
8519 | o = bfd_get_section_by_name (abfd, name); | |
8520 | if (o == NULL) | |
8521 | { | |
8522 | (*_bfd_error_handler) | |
d003868e | 8523 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
8524 | goto error_return; |
8525 | } | |
eea6121a | 8526 | if (o->size == 0) |
c152c796 AM |
8527 | (*_bfd_error_handler) |
8528 | (_("warning: %s section has zero size"), name); | |
eea6121a | 8529 | dyn.d_un.d_val = o->size; |
c152c796 AM |
8530 | break; |
8531 | ||
8532 | case DT_PREINIT_ARRAY: | |
8533 | name = ".preinit_array"; | |
8534 | goto get_vma; | |
8535 | case DT_INIT_ARRAY: | |
8536 | name = ".init_array"; | |
8537 | goto get_vma; | |
8538 | case DT_FINI_ARRAY: | |
8539 | name = ".fini_array"; | |
8540 | goto get_vma; | |
8541 | ||
8542 | case DT_HASH: | |
8543 | name = ".hash"; | |
8544 | goto get_vma; | |
8545 | case DT_STRTAB: | |
8546 | name = ".dynstr"; | |
8547 | goto get_vma; | |
8548 | case DT_SYMTAB: | |
8549 | name = ".dynsym"; | |
8550 | goto get_vma; | |
8551 | case DT_VERDEF: | |
8552 | name = ".gnu.version_d"; | |
8553 | goto get_vma; | |
8554 | case DT_VERNEED: | |
8555 | name = ".gnu.version_r"; | |
8556 | goto get_vma; | |
8557 | case DT_VERSYM: | |
8558 | name = ".gnu.version"; | |
8559 | get_vma: | |
8560 | o = bfd_get_section_by_name (abfd, name); | |
8561 | if (o == NULL) | |
8562 | { | |
8563 | (*_bfd_error_handler) | |
d003868e | 8564 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
8565 | goto error_return; |
8566 | } | |
8567 | dyn.d_un.d_ptr = o->vma; | |
8568 | break; | |
8569 | ||
8570 | case DT_REL: | |
8571 | case DT_RELA: | |
8572 | case DT_RELSZ: | |
8573 | case DT_RELASZ: | |
8574 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
8575 | type = SHT_REL; | |
8576 | else | |
8577 | type = SHT_RELA; | |
8578 | dyn.d_un.d_val = 0; | |
8579 | for (i = 1; i < elf_numsections (abfd); i++) | |
8580 | { | |
8581 | Elf_Internal_Shdr *hdr; | |
8582 | ||
8583 | hdr = elf_elfsections (abfd)[i]; | |
8584 | if (hdr->sh_type == type | |
8585 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
8586 | { | |
8587 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
8588 | dyn.d_un.d_val += hdr->sh_size; | |
8589 | else | |
8590 | { | |
8591 | if (dyn.d_un.d_val == 0 | |
8592 | || hdr->sh_addr < dyn.d_un.d_val) | |
8593 | dyn.d_un.d_val = hdr->sh_addr; | |
8594 | } | |
8595 | } | |
8596 | } | |
8597 | break; | |
8598 | } | |
8599 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
8600 | } | |
8601 | } | |
8602 | ||
8603 | /* If we have created any dynamic sections, then output them. */ | |
8604 | if (dynobj != NULL) | |
8605 | { | |
8606 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
8607 | goto error_return; | |
8608 | ||
8609 | for (o = dynobj->sections; o != NULL; o = o->next) | |
8610 | { | |
8611 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 8612 | || o->size == 0 |
c152c796 AM |
8613 | || o->output_section == bfd_abs_section_ptr) |
8614 | continue; | |
8615 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
8616 | { | |
8617 | /* At this point, we are only interested in sections | |
8618 | created by _bfd_elf_link_create_dynamic_sections. */ | |
8619 | continue; | |
8620 | } | |
3722b82f AM |
8621 | if (elf_hash_table (info)->stab_info.stabstr == o) |
8622 | continue; | |
eea6121a AM |
8623 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
8624 | continue; | |
c152c796 AM |
8625 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
8626 | != SHT_STRTAB) | |
8627 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
8628 | { | |
8629 | if (! bfd_set_section_contents (abfd, o->output_section, | |
8630 | o->contents, | |
8631 | (file_ptr) o->output_offset, | |
eea6121a | 8632 | o->size)) |
c152c796 AM |
8633 | goto error_return; |
8634 | } | |
8635 | else | |
8636 | { | |
8637 | /* The contents of the .dynstr section are actually in a | |
8638 | stringtab. */ | |
8639 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
8640 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
8641 | || ! _bfd_elf_strtab_emit (abfd, | |
8642 | elf_hash_table (info)->dynstr)) | |
8643 | goto error_return; | |
8644 | } | |
8645 | } | |
8646 | } | |
8647 | ||
8648 | if (info->relocatable) | |
8649 | { | |
8650 | bfd_boolean failed = FALSE; | |
8651 | ||
8652 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
8653 | if (failed) | |
8654 | goto error_return; | |
8655 | } | |
8656 | ||
8657 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 8658 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
8659 | { |
8660 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
8661 | goto error_return; | |
8662 | } | |
8663 | ||
8664 | if (info->eh_frame_hdr) | |
8665 | { | |
8666 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) | |
8667 | goto error_return; | |
8668 | } | |
8669 | ||
8670 | if (finfo.symstrtab != NULL) | |
8671 | _bfd_stringtab_free (finfo.symstrtab); | |
8672 | if (finfo.contents != NULL) | |
8673 | free (finfo.contents); | |
8674 | if (finfo.external_relocs != NULL) | |
8675 | free (finfo.external_relocs); | |
8676 | if (finfo.internal_relocs != NULL) | |
8677 | free (finfo.internal_relocs); | |
8678 | if (finfo.external_syms != NULL) | |
8679 | free (finfo.external_syms); | |
8680 | if (finfo.locsym_shndx != NULL) | |
8681 | free (finfo.locsym_shndx); | |
8682 | if (finfo.internal_syms != NULL) | |
8683 | free (finfo.internal_syms); | |
8684 | if (finfo.indices != NULL) | |
8685 | free (finfo.indices); | |
8686 | if (finfo.sections != NULL) | |
8687 | free (finfo.sections); | |
8688 | if (finfo.symbuf != NULL) | |
8689 | free (finfo.symbuf); | |
8690 | if (finfo.symshndxbuf != NULL) | |
8691 | free (finfo.symshndxbuf); | |
8692 | for (o = abfd->sections; o != NULL; o = o->next) | |
8693 | { | |
8694 | if ((o->flags & SEC_RELOC) != 0 | |
8695 | && elf_section_data (o)->rel_hashes != NULL) | |
8696 | free (elf_section_data (o)->rel_hashes); | |
8697 | } | |
8698 | ||
8699 | elf_tdata (abfd)->linker = TRUE; | |
8700 | ||
8701 | return TRUE; | |
8702 | ||
8703 | error_return: | |
8704 | if (finfo.symstrtab != NULL) | |
8705 | _bfd_stringtab_free (finfo.symstrtab); | |
8706 | if (finfo.contents != NULL) | |
8707 | free (finfo.contents); | |
8708 | if (finfo.external_relocs != NULL) | |
8709 | free (finfo.external_relocs); | |
8710 | if (finfo.internal_relocs != NULL) | |
8711 | free (finfo.internal_relocs); | |
8712 | if (finfo.external_syms != NULL) | |
8713 | free (finfo.external_syms); | |
8714 | if (finfo.locsym_shndx != NULL) | |
8715 | free (finfo.locsym_shndx); | |
8716 | if (finfo.internal_syms != NULL) | |
8717 | free (finfo.internal_syms); | |
8718 | if (finfo.indices != NULL) | |
8719 | free (finfo.indices); | |
8720 | if (finfo.sections != NULL) | |
8721 | free (finfo.sections); | |
8722 | if (finfo.symbuf != NULL) | |
8723 | free (finfo.symbuf); | |
8724 | if (finfo.symshndxbuf != NULL) | |
8725 | free (finfo.symshndxbuf); | |
8726 | for (o = abfd->sections; o != NULL; o = o->next) | |
8727 | { | |
8728 | if ((o->flags & SEC_RELOC) != 0 | |
8729 | && elf_section_data (o)->rel_hashes != NULL) | |
8730 | free (elf_section_data (o)->rel_hashes); | |
8731 | } | |
8732 | ||
8733 | return FALSE; | |
8734 | } | |
8735 | \f | |
8736 | /* Garbage collect unused sections. */ | |
8737 | ||
8738 | /* The mark phase of garbage collection. For a given section, mark | |
8739 | it and any sections in this section's group, and all the sections | |
8740 | which define symbols to which it refers. */ | |
8741 | ||
8742 | typedef asection * (*gc_mark_hook_fn) | |
8743 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
8744 | struct elf_link_hash_entry *, Elf_Internal_Sym *); | |
8745 | ||
ccfa59ea AM |
8746 | bfd_boolean |
8747 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
8748 | asection *sec, | |
8749 | gc_mark_hook_fn gc_mark_hook) | |
c152c796 AM |
8750 | { |
8751 | bfd_boolean ret; | |
39c2f51b | 8752 | bfd_boolean is_eh; |
c152c796 AM |
8753 | asection *group_sec; |
8754 | ||
8755 | sec->gc_mark = 1; | |
8756 | ||
8757 | /* Mark all the sections in the group. */ | |
8758 | group_sec = elf_section_data (sec)->next_in_group; | |
8759 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 8760 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
8761 | return FALSE; |
8762 | ||
8763 | /* Look through the section relocs. */ | |
8764 | ret = TRUE; | |
39c2f51b | 8765 | is_eh = strcmp (sec->name, ".eh_frame") == 0; |
c152c796 AM |
8766 | if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0) |
8767 | { | |
8768 | Elf_Internal_Rela *relstart, *rel, *relend; | |
8769 | Elf_Internal_Shdr *symtab_hdr; | |
8770 | struct elf_link_hash_entry **sym_hashes; | |
8771 | size_t nlocsyms; | |
8772 | size_t extsymoff; | |
8773 | bfd *input_bfd = sec->owner; | |
8774 | const struct elf_backend_data *bed = get_elf_backend_data (input_bfd); | |
8775 | Elf_Internal_Sym *isym = NULL; | |
8776 | int r_sym_shift; | |
8777 | ||
8778 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
8779 | sym_hashes = elf_sym_hashes (input_bfd); | |
8780 | ||
8781 | /* Read the local symbols. */ | |
8782 | if (elf_bad_symtab (input_bfd)) | |
8783 | { | |
8784 | nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
8785 | extsymoff = 0; | |
8786 | } | |
8787 | else | |
8788 | extsymoff = nlocsyms = symtab_hdr->sh_info; | |
8789 | ||
8790 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
8791 | if (isym == NULL && nlocsyms != 0) | |
8792 | { | |
8793 | isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0, | |
8794 | NULL, NULL, NULL); | |
8795 | if (isym == NULL) | |
8796 | return FALSE; | |
8797 | } | |
8798 | ||
8799 | /* Read the relocations. */ | |
8800 | relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL, | |
8801 | info->keep_memory); | |
8802 | if (relstart == NULL) | |
8803 | { | |
8804 | ret = FALSE; | |
8805 | goto out1; | |
8806 | } | |
8807 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
8808 | ||
8809 | if (bed->s->arch_size == 32) | |
8810 | r_sym_shift = 8; | |
8811 | else | |
8812 | r_sym_shift = 32; | |
8813 | ||
8814 | for (rel = relstart; rel < relend; rel++) | |
8815 | { | |
8816 | unsigned long r_symndx; | |
8817 | asection *rsec; | |
8818 | struct elf_link_hash_entry *h; | |
8819 | ||
8820 | r_symndx = rel->r_info >> r_sym_shift; | |
8821 | if (r_symndx == 0) | |
8822 | continue; | |
8823 | ||
8824 | if (r_symndx >= nlocsyms | |
8825 | || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL) | |
8826 | { | |
8827 | h = sym_hashes[r_symndx - extsymoff]; | |
20f0a1ad AM |
8828 | while (h->root.type == bfd_link_hash_indirect |
8829 | || h->root.type == bfd_link_hash_warning) | |
8830 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 AM |
8831 | rsec = (*gc_mark_hook) (sec, info, rel, h, NULL); |
8832 | } | |
8833 | else | |
8834 | { | |
8835 | rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]); | |
8836 | } | |
8837 | ||
8838 | if (rsec && !rsec->gc_mark) | |
8839 | { | |
8840 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
8841 | rsec->gc_mark = 1; | |
39c2f51b AM |
8842 | else if (is_eh) |
8843 | rsec->gc_mark_from_eh = 1; | |
ccfa59ea | 8844 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) |
c152c796 AM |
8845 | { |
8846 | ret = FALSE; | |
8847 | goto out2; | |
8848 | } | |
8849 | } | |
8850 | } | |
8851 | ||
8852 | out2: | |
8853 | if (elf_section_data (sec)->relocs != relstart) | |
8854 | free (relstart); | |
8855 | out1: | |
8856 | if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym) | |
8857 | { | |
8858 | if (! info->keep_memory) | |
8859 | free (isym); | |
8860 | else | |
8861 | symtab_hdr->contents = (unsigned char *) isym; | |
8862 | } | |
8863 | } | |
8864 | ||
8865 | return ret; | |
8866 | } | |
8867 | ||
8868 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ | |
8869 | ||
ccabcbe5 AM |
8870 | struct elf_gc_sweep_symbol_info { |
8871 | struct bfd_link_info *info; | |
8872 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
8873 | bfd_boolean); | |
8874 | }; | |
8875 | ||
c152c796 | 8876 | static bfd_boolean |
ccabcbe5 | 8877 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) |
c152c796 | 8878 | { |
c152c796 AM |
8879 | if (h->root.type == bfd_link_hash_warning) |
8880 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8881 | ||
ccabcbe5 AM |
8882 | if ((h->root.type == bfd_link_hash_defined |
8883 | || h->root.type == bfd_link_hash_defweak) | |
8884 | && !h->root.u.def.section->gc_mark | |
8885 | && !(h->root.u.def.section->owner->flags & DYNAMIC)) | |
8886 | { | |
8887 | struct elf_gc_sweep_symbol_info *inf = data; | |
8888 | (*inf->hide_symbol) (inf->info, h, TRUE); | |
8889 | } | |
c152c796 AM |
8890 | |
8891 | return TRUE; | |
8892 | } | |
8893 | ||
8894 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
8895 | ||
8896 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
8897 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
8898 | ||
8899 | static bfd_boolean | |
ccabcbe5 | 8900 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
8901 | { |
8902 | bfd *sub; | |
ccabcbe5 AM |
8903 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
8904 | gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; | |
8905 | unsigned long section_sym_count; | |
8906 | struct elf_gc_sweep_symbol_info sweep_info; | |
c152c796 AM |
8907 | |
8908 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8909 | { | |
8910 | asection *o; | |
8911 | ||
8912 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
8913 | continue; | |
8914 | ||
8915 | for (o = sub->sections; o != NULL; o = o->next) | |
8916 | { | |
7c2c8505 AM |
8917 | /* Keep debug and special sections. */ |
8918 | if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 | |
8919 | || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0) | |
c152c796 AM |
8920 | o->gc_mark = 1; |
8921 | ||
8922 | if (o->gc_mark) | |
8923 | continue; | |
8924 | ||
8925 | /* Skip sweeping sections already excluded. */ | |
8926 | if (o->flags & SEC_EXCLUDE) | |
8927 | continue; | |
8928 | ||
8929 | /* Since this is early in the link process, it is simple | |
8930 | to remove a section from the output. */ | |
8931 | o->flags |= SEC_EXCLUDE; | |
8932 | ||
8933 | /* But we also have to update some of the relocation | |
8934 | info we collected before. */ | |
8935 | if (gc_sweep_hook | |
e8aaee2a AM |
8936 | && (o->flags & SEC_RELOC) != 0 |
8937 | && o->reloc_count > 0 | |
8938 | && !bfd_is_abs_section (o->output_section)) | |
c152c796 AM |
8939 | { |
8940 | Elf_Internal_Rela *internal_relocs; | |
8941 | bfd_boolean r; | |
8942 | ||
8943 | internal_relocs | |
8944 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
8945 | info->keep_memory); | |
8946 | if (internal_relocs == NULL) | |
8947 | return FALSE; | |
8948 | ||
8949 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
8950 | ||
8951 | if (elf_section_data (o)->relocs != internal_relocs) | |
8952 | free (internal_relocs); | |
8953 | ||
8954 | if (!r) | |
8955 | return FALSE; | |
8956 | } | |
8957 | } | |
8958 | } | |
8959 | ||
8960 | /* Remove the symbols that were in the swept sections from the dynamic | |
8961 | symbol table. GCFIXME: Anyone know how to get them out of the | |
8962 | static symbol table as well? */ | |
ccabcbe5 AM |
8963 | sweep_info.info = info; |
8964 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
8965 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
8966 | &sweep_info); | |
c152c796 | 8967 | |
ccabcbe5 | 8968 | _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); |
c152c796 AM |
8969 | return TRUE; |
8970 | } | |
8971 | ||
8972 | /* Propagate collected vtable information. This is called through | |
8973 | elf_link_hash_traverse. */ | |
8974 | ||
8975 | static bfd_boolean | |
8976 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
8977 | { | |
8978 | if (h->root.type == bfd_link_hash_warning) | |
8979 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8980 | ||
8981 | /* Those that are not vtables. */ | |
f6e332e6 | 8982 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
8983 | return TRUE; |
8984 | ||
8985 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 8986 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
8987 | return TRUE; |
8988 | ||
8989 | /* If we've already been done, exit. */ | |
f6e332e6 | 8990 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
8991 | return TRUE; |
8992 | ||
8993 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 8994 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 8995 | |
f6e332e6 | 8996 | if (h->vtable->used == NULL) |
c152c796 AM |
8997 | { |
8998 | /* None of this table's entries were referenced. Re-use the | |
8999 | parent's table. */ | |
f6e332e6 AM |
9000 | h->vtable->used = h->vtable->parent->vtable->used; |
9001 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
9002 | } |
9003 | else | |
9004 | { | |
9005 | size_t n; | |
9006 | bfd_boolean *cu, *pu; | |
9007 | ||
9008 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 9009 | cu = h->vtable->used; |
c152c796 | 9010 | cu[-1] = TRUE; |
f6e332e6 | 9011 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
9012 | if (pu != NULL) |
9013 | { | |
9014 | const struct elf_backend_data *bed; | |
9015 | unsigned int log_file_align; | |
9016 | ||
9017 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
9018 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 9019 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
9020 | while (n--) |
9021 | { | |
9022 | if (*pu) | |
9023 | *cu = TRUE; | |
9024 | pu++; | |
9025 | cu++; | |
9026 | } | |
9027 | } | |
9028 | } | |
9029 | ||
9030 | return TRUE; | |
9031 | } | |
9032 | ||
9033 | static bfd_boolean | |
9034 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
9035 | { | |
9036 | asection *sec; | |
9037 | bfd_vma hstart, hend; | |
9038 | Elf_Internal_Rela *relstart, *relend, *rel; | |
9039 | const struct elf_backend_data *bed; | |
9040 | unsigned int log_file_align; | |
9041 | ||
9042 | if (h->root.type == bfd_link_hash_warning) | |
9043 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9044 | ||
9045 | /* Take care of both those symbols that do not describe vtables as | |
9046 | well as those that are not loaded. */ | |
f6e332e6 | 9047 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
9048 | return TRUE; |
9049 | ||
9050 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
9051 | || h->root.type == bfd_link_hash_defweak); | |
9052 | ||
9053 | sec = h->root.u.def.section; | |
9054 | hstart = h->root.u.def.value; | |
9055 | hend = hstart + h->size; | |
9056 | ||
9057 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
9058 | if (!relstart) | |
9059 | return *(bfd_boolean *) okp = FALSE; | |
9060 | bed = get_elf_backend_data (sec->owner); | |
9061 | log_file_align = bed->s->log_file_align; | |
9062 | ||
9063 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
9064 | ||
9065 | for (rel = relstart; rel < relend; ++rel) | |
9066 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
9067 | { | |
9068 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
9069 | if (h->vtable->used |
9070 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
9071 | { |
9072 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 9073 | if (h->vtable->used[entry]) |
c152c796 AM |
9074 | continue; |
9075 | } | |
9076 | /* Otherwise, kill it. */ | |
9077 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
9078 | } | |
9079 | ||
9080 | return TRUE; | |
9081 | } | |
9082 | ||
87538722 AM |
9083 | /* Mark sections containing dynamically referenced symbols. When |
9084 | building shared libraries, we must assume that any visible symbol is | |
9085 | referenced. */ | |
715df9b8 | 9086 | |
64d03ab5 AM |
9087 | bfd_boolean |
9088 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 9089 | { |
87538722 AM |
9090 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
9091 | ||
715df9b8 EB |
9092 | if (h->root.type == bfd_link_hash_warning) |
9093 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9094 | ||
9095 | if ((h->root.type == bfd_link_hash_defined | |
9096 | || h->root.type == bfd_link_hash_defweak) | |
87538722 | 9097 | && (h->ref_dynamic |
5adcfd8b | 9098 | || (!info->executable |
87538722 AM |
9099 | && h->def_regular |
9100 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL | |
9101 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN))) | |
715df9b8 EB |
9102 | h->root.u.def.section->flags |= SEC_KEEP; |
9103 | ||
9104 | return TRUE; | |
9105 | } | |
3b36f7e6 | 9106 | |
c152c796 AM |
9107 | /* Do mark and sweep of unused sections. */ |
9108 | ||
9109 | bfd_boolean | |
9110 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
9111 | { | |
9112 | bfd_boolean ok = TRUE; | |
9113 | bfd *sub; | |
9114 | asection * (*gc_mark_hook) | |
9115 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
9116 | struct elf_link_hash_entry *h, Elf_Internal_Sym *); | |
64d03ab5 | 9117 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 9118 | |
64d03ab5 | 9119 | if (!bed->can_gc_sections |
c152c796 AM |
9120 | || info->relocatable |
9121 | || info->emitrelocations | |
715df9b8 | 9122 | || !is_elf_hash_table (info->hash)) |
c152c796 AM |
9123 | { |
9124 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
9125 | return TRUE; | |
9126 | } | |
9127 | ||
9128 | /* Apply transitive closure to the vtable entry usage info. */ | |
9129 | elf_link_hash_traverse (elf_hash_table (info), | |
9130 | elf_gc_propagate_vtable_entries_used, | |
9131 | &ok); | |
9132 | if (!ok) | |
9133 | return FALSE; | |
9134 | ||
9135 | /* Kill the vtable relocations that were not used. */ | |
9136 | elf_link_hash_traverse (elf_hash_table (info), | |
9137 | elf_gc_smash_unused_vtentry_relocs, | |
9138 | &ok); | |
9139 | if (!ok) | |
9140 | return FALSE; | |
9141 | ||
715df9b8 EB |
9142 | /* Mark dynamically referenced symbols. */ |
9143 | if (elf_hash_table (info)->dynamic_sections_created) | |
9144 | elf_link_hash_traverse (elf_hash_table (info), | |
64d03ab5 | 9145 | bed->gc_mark_dynamic_ref, |
87538722 | 9146 | info); |
c152c796 | 9147 | |
715df9b8 | 9148 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 9149 | gc_mark_hook = bed->gc_mark_hook; |
c152c796 AM |
9150 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) |
9151 | { | |
9152 | asection *o; | |
9153 | ||
9154 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
9155 | continue; | |
9156 | ||
9157 | for (o = sub->sections; o != NULL; o = o->next) | |
39c2f51b AM |
9158 | if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark) |
9159 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
9160 | return FALSE; | |
c152c796 AM |
9161 | } |
9162 | ||
9e8cc8b4 AM |
9163 | /* ... again for sections marked from eh_frame. */ |
9164 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
9165 | { | |
9166 | asection *o; | |
9167 | ||
9168 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
9169 | continue; | |
9170 | ||
9171 | /* Keep .gcc_except_table.* if the associated .text.* is | |
9172 | marked. This isn't very nice, but the proper solution, | |
9173 | splitting .eh_frame up and using comdat doesn't pan out | |
9174 | easily due to needing special relocs to handle the | |
9175 | difference of two symbols in separate sections. | |
9176 | Don't keep code sections referenced by .eh_frame. */ | |
9177 | for (o = sub->sections; o != NULL; o = o->next) | |
9178 | if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0) | |
9179 | { | |
9180 | if (strncmp (o->name, ".gcc_except_table.", 18) == 0) | |
9181 | { | |
9182 | unsigned long len; | |
9183 | char *fn_name; | |
9184 | asection *fn_text; | |
9185 | ||
9186 | len = strlen (o->name + 18) + 1; | |
9187 | fn_name = bfd_malloc (len + 6); | |
9188 | if (fn_name == NULL) | |
9189 | return FALSE; | |
9190 | memcpy (fn_name, ".text.", 6); | |
9191 | memcpy (fn_name + 6, o->name + 18, len); | |
9192 | fn_text = bfd_get_section_by_name (sub, fn_name); | |
9193 | free (fn_name); | |
9194 | if (fn_text == NULL || !fn_text->gc_mark) | |
9195 | continue; | |
9196 | } | |
9197 | ||
9198 | /* If not using specially named exception table section, | |
9199 | then keep whatever we are using. */ | |
9200 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
9201 | return FALSE; | |
9202 | } | |
9203 | } | |
9204 | ||
c152c796 | 9205 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 9206 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
9207 | } |
9208 | \f | |
9209 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
9210 | ||
9211 | bfd_boolean | |
9212 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
9213 | asection *sec, | |
9214 | struct elf_link_hash_entry *h, | |
9215 | bfd_vma offset) | |
9216 | { | |
9217 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
9218 | struct elf_link_hash_entry **search, *child; | |
9219 | bfd_size_type extsymcount; | |
9220 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9221 | ||
9222 | /* The sh_info field of the symtab header tells us where the | |
9223 | external symbols start. We don't care about the local symbols at | |
9224 | this point. */ | |
9225 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
9226 | if (!elf_bad_symtab (abfd)) | |
9227 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
9228 | ||
9229 | sym_hashes = elf_sym_hashes (abfd); | |
9230 | sym_hashes_end = sym_hashes + extsymcount; | |
9231 | ||
9232 | /* Hunt down the child symbol, which is in this section at the same | |
9233 | offset as the relocation. */ | |
9234 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
9235 | { | |
9236 | if ((child = *search) != NULL | |
9237 | && (child->root.type == bfd_link_hash_defined | |
9238 | || child->root.type == bfd_link_hash_defweak) | |
9239 | && child->root.u.def.section == sec | |
9240 | && child->root.u.def.value == offset) | |
9241 | goto win; | |
9242 | } | |
9243 | ||
d003868e AM |
9244 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
9245 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
9246 | bfd_set_error (bfd_error_invalid_operation); |
9247 | return FALSE; | |
9248 | ||
9249 | win: | |
f6e332e6 AM |
9250 | if (!child->vtable) |
9251 | { | |
9252 | child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable)); | |
9253 | if (!child->vtable) | |
9254 | return FALSE; | |
9255 | } | |
c152c796 AM |
9256 | if (!h) |
9257 | { | |
9258 | /* This *should* only be the absolute section. It could potentially | |
9259 | be that someone has defined a non-global vtable though, which | |
9260 | would be bad. It isn't worth paging in the local symbols to be | |
9261 | sure though; that case should simply be handled by the assembler. */ | |
9262 | ||
f6e332e6 | 9263 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
9264 | } |
9265 | else | |
f6e332e6 | 9266 | child->vtable->parent = h; |
c152c796 AM |
9267 | |
9268 | return TRUE; | |
9269 | } | |
9270 | ||
9271 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
9272 | ||
9273 | bfd_boolean | |
9274 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
9275 | asection *sec ATTRIBUTE_UNUSED, | |
9276 | struct elf_link_hash_entry *h, | |
9277 | bfd_vma addend) | |
9278 | { | |
9279 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9280 | unsigned int log_file_align = bed->s->log_file_align; | |
9281 | ||
f6e332e6 AM |
9282 | if (!h->vtable) |
9283 | { | |
9284 | h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable)); | |
9285 | if (!h->vtable) | |
9286 | return FALSE; | |
9287 | } | |
9288 | ||
9289 | if (addend >= h->vtable->size) | |
c152c796 AM |
9290 | { |
9291 | size_t size, bytes, file_align; | |
f6e332e6 | 9292 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
9293 | |
9294 | /* While the symbol is undefined, we have to be prepared to handle | |
9295 | a zero size. */ | |
9296 | file_align = 1 << log_file_align; | |
9297 | if (h->root.type == bfd_link_hash_undefined) | |
9298 | size = addend + file_align; | |
9299 | else | |
9300 | { | |
9301 | size = h->size; | |
9302 | if (addend >= size) | |
9303 | { | |
9304 | /* Oops! We've got a reference past the defined end of | |
9305 | the table. This is probably a bug -- shall we warn? */ | |
9306 | size = addend + file_align; | |
9307 | } | |
9308 | } | |
9309 | size = (size + file_align - 1) & -file_align; | |
9310 | ||
9311 | /* Allocate one extra entry for use as a "done" flag for the | |
9312 | consolidation pass. */ | |
9313 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
9314 | ||
9315 | if (ptr) | |
9316 | { | |
9317 | ptr = bfd_realloc (ptr - 1, bytes); | |
9318 | ||
9319 | if (ptr != NULL) | |
9320 | { | |
9321 | size_t oldbytes; | |
9322 | ||
f6e332e6 | 9323 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
9324 | * sizeof (bfd_boolean)); |
9325 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
9326 | } | |
9327 | } | |
9328 | else | |
9329 | ptr = bfd_zmalloc (bytes); | |
9330 | ||
9331 | if (ptr == NULL) | |
9332 | return FALSE; | |
9333 | ||
9334 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
9335 | h->vtable->used = ptr + 1; |
9336 | h->vtable->size = size; | |
c152c796 AM |
9337 | } |
9338 | ||
f6e332e6 | 9339 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
9340 | |
9341 | return TRUE; | |
9342 | } | |
9343 | ||
9344 | struct alloc_got_off_arg { | |
9345 | bfd_vma gotoff; | |
9346 | unsigned int got_elt_size; | |
9347 | }; | |
9348 | ||
9349 | /* We need a special top-level link routine to convert got reference counts | |
9350 | to real got offsets. */ | |
9351 | ||
9352 | static bfd_boolean | |
9353 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
9354 | { | |
9355 | struct alloc_got_off_arg *gofarg = arg; | |
9356 | ||
9357 | if (h->root.type == bfd_link_hash_warning) | |
9358 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9359 | ||
9360 | if (h->got.refcount > 0) | |
9361 | { | |
9362 | h->got.offset = gofarg->gotoff; | |
9363 | gofarg->gotoff += gofarg->got_elt_size; | |
9364 | } | |
9365 | else | |
9366 | h->got.offset = (bfd_vma) -1; | |
9367 | ||
9368 | return TRUE; | |
9369 | } | |
9370 | ||
9371 | /* And an accompanying bit to work out final got entry offsets once | |
9372 | we're done. Should be called from final_link. */ | |
9373 | ||
9374 | bfd_boolean | |
9375 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
9376 | struct bfd_link_info *info) | |
9377 | { | |
9378 | bfd *i; | |
9379 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9380 | bfd_vma gotoff; | |
9381 | unsigned int got_elt_size = bed->s->arch_size / 8; | |
9382 | struct alloc_got_off_arg gofarg; | |
9383 | ||
9384 | if (! is_elf_hash_table (info->hash)) | |
9385 | return FALSE; | |
9386 | ||
9387 | /* The GOT offset is relative to the .got section, but the GOT header is | |
9388 | put into the .got.plt section, if the backend uses it. */ | |
9389 | if (bed->want_got_plt) | |
9390 | gotoff = 0; | |
9391 | else | |
9392 | gotoff = bed->got_header_size; | |
9393 | ||
9394 | /* Do the local .got entries first. */ | |
9395 | for (i = info->input_bfds; i; i = i->link_next) | |
9396 | { | |
9397 | bfd_signed_vma *local_got; | |
9398 | bfd_size_type j, locsymcount; | |
9399 | Elf_Internal_Shdr *symtab_hdr; | |
9400 | ||
9401 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
9402 | continue; | |
9403 | ||
9404 | local_got = elf_local_got_refcounts (i); | |
9405 | if (!local_got) | |
9406 | continue; | |
9407 | ||
9408 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
9409 | if (elf_bad_symtab (i)) | |
9410 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9411 | else | |
9412 | locsymcount = symtab_hdr->sh_info; | |
9413 | ||
9414 | for (j = 0; j < locsymcount; ++j) | |
9415 | { | |
9416 | if (local_got[j] > 0) | |
9417 | { | |
9418 | local_got[j] = gotoff; | |
9419 | gotoff += got_elt_size; | |
9420 | } | |
9421 | else | |
9422 | local_got[j] = (bfd_vma) -1; | |
9423 | } | |
9424 | } | |
9425 | ||
9426 | /* Then the global .got entries. .plt refcounts are handled by | |
9427 | adjust_dynamic_symbol */ | |
9428 | gofarg.gotoff = gotoff; | |
9429 | gofarg.got_elt_size = got_elt_size; | |
9430 | elf_link_hash_traverse (elf_hash_table (info), | |
9431 | elf_gc_allocate_got_offsets, | |
9432 | &gofarg); | |
9433 | return TRUE; | |
9434 | } | |
9435 | ||
9436 | /* Many folk need no more in the way of final link than this, once | |
9437 | got entry reference counting is enabled. */ | |
9438 | ||
9439 | bfd_boolean | |
9440 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
9441 | { | |
9442 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
9443 | return FALSE; | |
9444 | ||
9445 | /* Invoke the regular ELF backend linker to do all the work. */ | |
9446 | return bfd_elf_final_link (abfd, info); | |
9447 | } | |
9448 | ||
9449 | bfd_boolean | |
9450 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
9451 | { | |
9452 | struct elf_reloc_cookie *rcookie = cookie; | |
9453 | ||
9454 | if (rcookie->bad_symtab) | |
9455 | rcookie->rel = rcookie->rels; | |
9456 | ||
9457 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
9458 | { | |
9459 | unsigned long r_symndx; | |
9460 | ||
9461 | if (! rcookie->bad_symtab) | |
9462 | if (rcookie->rel->r_offset > offset) | |
9463 | return FALSE; | |
9464 | if (rcookie->rel->r_offset != offset) | |
9465 | continue; | |
9466 | ||
9467 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
9468 | if (r_symndx == SHN_UNDEF) | |
9469 | return TRUE; | |
9470 | ||
9471 | if (r_symndx >= rcookie->locsymcount | |
9472 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
9473 | { | |
9474 | struct elf_link_hash_entry *h; | |
9475 | ||
9476 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
9477 | ||
9478 | while (h->root.type == bfd_link_hash_indirect | |
9479 | || h->root.type == bfd_link_hash_warning) | |
9480 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9481 | ||
9482 | if ((h->root.type == bfd_link_hash_defined | |
9483 | || h->root.type == bfd_link_hash_defweak) | |
9484 | && elf_discarded_section (h->root.u.def.section)) | |
9485 | return TRUE; | |
9486 | else | |
9487 | return FALSE; | |
9488 | } | |
9489 | else | |
9490 | { | |
9491 | /* It's not a relocation against a global symbol, | |
9492 | but it could be a relocation against a local | |
9493 | symbol for a discarded section. */ | |
9494 | asection *isec; | |
9495 | Elf_Internal_Sym *isym; | |
9496 | ||
9497 | /* Need to: get the symbol; get the section. */ | |
9498 | isym = &rcookie->locsyms[r_symndx]; | |
9499 | if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
9500 | { | |
9501 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); | |
9502 | if (isec != NULL && elf_discarded_section (isec)) | |
9503 | return TRUE; | |
9504 | } | |
9505 | } | |
9506 | return FALSE; | |
9507 | } | |
9508 | return FALSE; | |
9509 | } | |
9510 | ||
9511 | /* Discard unneeded references to discarded sections. | |
9512 | Returns TRUE if any section's size was changed. */ | |
9513 | /* This function assumes that the relocations are in sorted order, | |
9514 | which is true for all known assemblers. */ | |
9515 | ||
9516 | bfd_boolean | |
9517 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) | |
9518 | { | |
9519 | struct elf_reloc_cookie cookie; | |
9520 | asection *stab, *eh; | |
9521 | Elf_Internal_Shdr *symtab_hdr; | |
9522 | const struct elf_backend_data *bed; | |
9523 | bfd *abfd; | |
9524 | unsigned int count; | |
9525 | bfd_boolean ret = FALSE; | |
9526 | ||
9527 | if (info->traditional_format | |
9528 | || !is_elf_hash_table (info->hash)) | |
9529 | return FALSE; | |
9530 | ||
9531 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) | |
9532 | { | |
9533 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
9534 | continue; | |
9535 | ||
9536 | bed = get_elf_backend_data (abfd); | |
9537 | ||
9538 | if ((abfd->flags & DYNAMIC) != 0) | |
9539 | continue; | |
9540 | ||
9541 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); | |
9542 | if (info->relocatable | |
9543 | || (eh != NULL | |
eea6121a | 9544 | && (eh->size == 0 |
c152c796 AM |
9545 | || bfd_is_abs_section (eh->output_section)))) |
9546 | eh = NULL; | |
9547 | ||
9548 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
9549 | if (stab != NULL | |
eea6121a | 9550 | && (stab->size == 0 |
c152c796 AM |
9551 | || bfd_is_abs_section (stab->output_section) |
9552 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) | |
9553 | stab = NULL; | |
9554 | ||
9555 | if (stab == NULL | |
9556 | && eh == NULL | |
9557 | && bed->elf_backend_discard_info == NULL) | |
9558 | continue; | |
9559 | ||
9560 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
9561 | cookie.abfd = abfd; | |
9562 | cookie.sym_hashes = elf_sym_hashes (abfd); | |
9563 | cookie.bad_symtab = elf_bad_symtab (abfd); | |
9564 | if (cookie.bad_symtab) | |
9565 | { | |
9566 | cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9567 | cookie.extsymoff = 0; | |
9568 | } | |
9569 | else | |
9570 | { | |
9571 | cookie.locsymcount = symtab_hdr->sh_info; | |
9572 | cookie.extsymoff = symtab_hdr->sh_info; | |
9573 | } | |
9574 | ||
9575 | if (bed->s->arch_size == 32) | |
9576 | cookie.r_sym_shift = 8; | |
9577 | else | |
9578 | cookie.r_sym_shift = 32; | |
9579 | ||
9580 | cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
9581 | if (cookie.locsyms == NULL && cookie.locsymcount != 0) | |
9582 | { | |
9583 | cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
9584 | cookie.locsymcount, 0, | |
9585 | NULL, NULL, NULL); | |
9586 | if (cookie.locsyms == NULL) | |
9587 | return FALSE; | |
9588 | } | |
9589 | ||
9590 | if (stab != NULL) | |
9591 | { | |
9592 | cookie.rels = NULL; | |
9593 | count = stab->reloc_count; | |
9594 | if (count != 0) | |
9595 | cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL, | |
9596 | info->keep_memory); | |
9597 | if (cookie.rels != NULL) | |
9598 | { | |
9599 | cookie.rel = cookie.rels; | |
9600 | cookie.relend = cookie.rels; | |
9601 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
9602 | if (_bfd_discard_section_stabs (abfd, stab, | |
9603 | elf_section_data (stab)->sec_info, | |
9604 | bfd_elf_reloc_symbol_deleted_p, | |
9605 | &cookie)) | |
9606 | ret = TRUE; | |
9607 | if (elf_section_data (stab)->relocs != cookie.rels) | |
9608 | free (cookie.rels); | |
9609 | } | |
9610 | } | |
9611 | ||
9612 | if (eh != NULL) | |
9613 | { | |
9614 | cookie.rels = NULL; | |
9615 | count = eh->reloc_count; | |
9616 | if (count != 0) | |
9617 | cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL, | |
9618 | info->keep_memory); | |
9619 | cookie.rel = cookie.rels; | |
9620 | cookie.relend = cookie.rels; | |
9621 | if (cookie.rels != NULL) | |
9622 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
9623 | ||
9624 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, | |
9625 | bfd_elf_reloc_symbol_deleted_p, | |
9626 | &cookie)) | |
9627 | ret = TRUE; | |
9628 | ||
9629 | if (cookie.rels != NULL | |
9630 | && elf_section_data (eh)->relocs != cookie.rels) | |
9631 | free (cookie.rels); | |
9632 | } | |
9633 | ||
9634 | if (bed->elf_backend_discard_info != NULL | |
9635 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
9636 | ret = TRUE; | |
9637 | ||
9638 | if (cookie.locsyms != NULL | |
9639 | && symtab_hdr->contents != (unsigned char *) cookie.locsyms) | |
9640 | { | |
9641 | if (! info->keep_memory) | |
9642 | free (cookie.locsyms); | |
9643 | else | |
9644 | symtab_hdr->contents = (unsigned char *) cookie.locsyms; | |
9645 | } | |
9646 | } | |
9647 | ||
9648 | if (info->eh_frame_hdr | |
9649 | && !info->relocatable | |
9650 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
9651 | ret = TRUE; | |
9652 | ||
9653 | return ret; | |
9654 | } | |
082b7297 L |
9655 | |
9656 | void | |
9657 | _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec) | |
9658 | { | |
9659 | flagword flags; | |
6d2cd210 | 9660 | const char *name, *p; |
082b7297 L |
9661 | struct bfd_section_already_linked *l; |
9662 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
3d7f7666 L |
9663 | asection *group; |
9664 | ||
9665 | /* A single member comdat group section may be discarded by a | |
9666 | linkonce section. See below. */ | |
9667 | if (sec->output_section == bfd_abs_section_ptr) | |
9668 | return; | |
082b7297 L |
9669 | |
9670 | flags = sec->flags; | |
3d7f7666 L |
9671 | |
9672 | /* Check if it belongs to a section group. */ | |
9673 | group = elf_sec_group (sec); | |
9674 | ||
9675 | /* Return if it isn't a linkonce section nor a member of a group. A | |
9676 | comdat group section also has SEC_LINK_ONCE set. */ | |
9677 | if ((flags & SEC_LINK_ONCE) == 0 && group == NULL) | |
082b7297 L |
9678 | return; |
9679 | ||
3d7f7666 L |
9680 | if (group) |
9681 | { | |
9682 | /* If this is the member of a single member comdat group, check if | |
9683 | the group should be discarded. */ | |
9684 | if (elf_next_in_group (sec) == sec | |
9685 | && (group->flags & SEC_LINK_ONCE) != 0) | |
9686 | sec = group; | |
9687 | else | |
9688 | return; | |
9689 | } | |
9690 | ||
082b7297 L |
9691 | /* FIXME: When doing a relocatable link, we may have trouble |
9692 | copying relocations in other sections that refer to local symbols | |
9693 | in the section being discarded. Those relocations will have to | |
9694 | be converted somehow; as of this writing I'm not sure that any of | |
9695 | the backends handle that correctly. | |
9696 | ||
9697 | It is tempting to instead not discard link once sections when | |
9698 | doing a relocatable link (technically, they should be discarded | |
9699 | whenever we are building constructors). However, that fails, | |
9700 | because the linker winds up combining all the link once sections | |
9701 | into a single large link once section, which defeats the purpose | |
9702 | of having link once sections in the first place. | |
9703 | ||
9704 | Also, not merging link once sections in a relocatable link | |
9705 | causes trouble for MIPS ELF, which relies on link once semantics | |
9706 | to handle the .reginfo section correctly. */ | |
9707 | ||
9708 | name = bfd_get_section_name (abfd, sec); | |
9709 | ||
6d2cd210 JJ |
9710 | if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0 |
9711 | && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) | |
9712 | p++; | |
9713 | else | |
9714 | p = name; | |
9715 | ||
9716 | already_linked_list = bfd_section_already_linked_table_lookup (p); | |
082b7297 L |
9717 | |
9718 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
9719 | { | |
9720 | /* We may have 3 different sections on the list: group section, | |
9721 | comdat section and linkonce section. SEC may be a linkonce or | |
9722 | group section. We match a group section with a group section, | |
9723 | a linkonce section with a linkonce section, and ignore comdat | |
9724 | section. */ | |
3d7f7666 | 9725 | if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) |
6d2cd210 | 9726 | && strcmp (name, l->sec->name) == 0 |
082b7297 L |
9727 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL) |
9728 | { | |
9729 | /* The section has already been linked. See if we should | |
6d2cd210 | 9730 | issue a warning. */ |
082b7297 L |
9731 | switch (flags & SEC_LINK_DUPLICATES) |
9732 | { | |
9733 | default: | |
9734 | abort (); | |
9735 | ||
9736 | case SEC_LINK_DUPLICATES_DISCARD: | |
9737 | break; | |
9738 | ||
9739 | case SEC_LINK_DUPLICATES_ONE_ONLY: | |
9740 | (*_bfd_error_handler) | |
c93625e2 | 9741 | (_("%B: ignoring duplicate section `%A'"), |
d003868e | 9742 | abfd, sec); |
082b7297 L |
9743 | break; |
9744 | ||
9745 | case SEC_LINK_DUPLICATES_SAME_SIZE: | |
9746 | if (sec->size != l->sec->size) | |
9747 | (*_bfd_error_handler) | |
c93625e2 | 9748 | (_("%B: duplicate section `%A' has different size"), |
d003868e | 9749 | abfd, sec); |
082b7297 | 9750 | break; |
ea5158d8 DJ |
9751 | |
9752 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: | |
9753 | if (sec->size != l->sec->size) | |
9754 | (*_bfd_error_handler) | |
c93625e2 | 9755 | (_("%B: duplicate section `%A' has different size"), |
ea5158d8 DJ |
9756 | abfd, sec); |
9757 | else if (sec->size != 0) | |
9758 | { | |
9759 | bfd_byte *sec_contents, *l_sec_contents; | |
9760 | ||
9761 | if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents)) | |
9762 | (*_bfd_error_handler) | |
c93625e2 | 9763 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
9764 | abfd, sec); |
9765 | else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec, | |
9766 | &l_sec_contents)) | |
9767 | (*_bfd_error_handler) | |
c93625e2 | 9768 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
9769 | l->sec->owner, l->sec); |
9770 | else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) | |
9771 | (*_bfd_error_handler) | |
c93625e2 | 9772 | (_("%B: warning: duplicate section `%A' has different contents"), |
ea5158d8 DJ |
9773 | abfd, sec); |
9774 | ||
9775 | if (sec_contents) | |
9776 | free (sec_contents); | |
9777 | if (l_sec_contents) | |
9778 | free (l_sec_contents); | |
9779 | } | |
9780 | break; | |
082b7297 L |
9781 | } |
9782 | ||
9783 | /* Set the output_section field so that lang_add_section | |
9784 | does not create a lang_input_section structure for this | |
9785 | section. Since there might be a symbol in the section | |
9786 | being discarded, we must retain a pointer to the section | |
9787 | which we are really going to use. */ | |
9788 | sec->output_section = bfd_abs_section_ptr; | |
9789 | sec->kept_section = l->sec; | |
3b36f7e6 | 9790 | |
082b7297 | 9791 | if (flags & SEC_GROUP) |
3d7f7666 L |
9792 | { |
9793 | asection *first = elf_next_in_group (sec); | |
9794 | asection *s = first; | |
9795 | ||
9796 | while (s != NULL) | |
9797 | { | |
9798 | s->output_section = bfd_abs_section_ptr; | |
9799 | /* Record which group discards it. */ | |
9800 | s->kept_section = l->sec; | |
9801 | s = elf_next_in_group (s); | |
9802 | /* These lists are circular. */ | |
9803 | if (s == first) | |
9804 | break; | |
9805 | } | |
9806 | } | |
082b7297 L |
9807 | |
9808 | return; | |
9809 | } | |
9810 | } | |
9811 | ||
3d7f7666 L |
9812 | if (group) |
9813 | { | |
9814 | /* If this is the member of a single member comdat group and the | |
9815 | group hasn't be discarded, we check if it matches a linkonce | |
9816 | section. We only record the discarded comdat group. Otherwise | |
9817 | the undiscarded group will be discarded incorrectly later since | |
9818 | itself has been recorded. */ | |
6d2cd210 JJ |
9819 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
9820 | if ((l->sec->flags & SEC_GROUP) == 0 | |
9821 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL | |
9822 | && bfd_elf_match_symbols_in_sections (l->sec, | |
9823 | elf_next_in_group (sec))) | |
9824 | { | |
9825 | elf_next_in_group (sec)->output_section = bfd_abs_section_ptr; | |
9826 | elf_next_in_group (sec)->kept_section = l->sec; | |
9827 | group->output_section = bfd_abs_section_ptr; | |
9828 | break; | |
9829 | } | |
9830 | if (l == NULL) | |
3d7f7666 L |
9831 | return; |
9832 | } | |
9833 | else | |
9834 | /* There is no direct match. But for linkonce section, we should | |
9835 | check if there is a match with comdat group member. We always | |
9836 | record the linkonce section, discarded or not. */ | |
6d2cd210 JJ |
9837 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
9838 | if (l->sec->flags & SEC_GROUP) | |
9839 | { | |
9840 | asection *first = elf_next_in_group (l->sec); | |
9841 | ||
9842 | if (first != NULL | |
9843 | && elf_next_in_group (first) == first | |
9844 | && bfd_elf_match_symbols_in_sections (first, sec)) | |
9845 | { | |
9846 | sec->output_section = bfd_abs_section_ptr; | |
9847 | sec->kept_section = l->sec; | |
9848 | break; | |
9849 | } | |
9850 | } | |
9851 | ||
082b7297 L |
9852 | /* This is the first section with this name. Record it. */ |
9853 | bfd_section_already_linked_table_insert (already_linked_list, sec); | |
9854 | } | |
81e1b023 | 9855 | |
a4d8e49b L |
9856 | bfd_boolean |
9857 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
9858 | { | |
9859 | return sym->st_shndx == SHN_COMMON; | |
9860 | } | |
9861 | ||
9862 | unsigned int | |
9863 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
9864 | { | |
9865 | return SHN_COMMON; | |
9866 | } | |
9867 | ||
9868 | asection * | |
9869 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
9870 | { | |
9871 | return bfd_com_section_ptr; | |
9872 | } |