]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
7e9f0867 | 2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 |
7898deda | 3 | 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; | |
40 | ||
41 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
42 | if (h != NULL) | |
43 | { | |
44 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
45 | This is a symptom of a larger problem: Absolute symbols | |
46 | defined in shared libraries can't be overridden, because we | |
47 | lose the link to the bfd which is via the symbol section. */ | |
48 | h->root.type = bfd_link_hash_new; | |
49 | } | |
50 | ||
51 | bh = &h->root; | |
52 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, | |
53 | sec, 0, NULL, FALSE, | |
54 | get_elf_backend_data (abfd)->collect, | |
55 | &bh)) | |
56 | return NULL; | |
57 | h = (struct elf_link_hash_entry *) bh; | |
58 | h->def_regular = 1; | |
59 | h->type = STT_OBJECT; | |
60 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
61 | ||
62 | if (!info->executable | |
63 | && !bfd_elf_link_record_dynamic_symbol (info, h)) | |
64 | return NULL; | |
65 | ||
66 | return h; | |
67 | } | |
68 | ||
b34976b6 | 69 | bfd_boolean |
268b6b39 | 70 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
71 | { |
72 | flagword flags; | |
aad5d350 | 73 | asection *s; |
252b5132 | 74 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 75 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
76 | int ptralign; |
77 | ||
78 | /* This function may be called more than once. */ | |
aad5d350 AM |
79 | s = bfd_get_section_by_name (abfd, ".got"); |
80 | if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0) | |
b34976b6 | 81 | return TRUE; |
252b5132 RH |
82 | |
83 | switch (bed->s->arch_size) | |
84 | { | |
bb0deeff AO |
85 | case 32: |
86 | ptralign = 2; | |
87 | break; | |
88 | ||
89 | case 64: | |
90 | ptralign = 3; | |
91 | break; | |
92 | ||
93 | default: | |
94 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 95 | return FALSE; |
252b5132 RH |
96 | } |
97 | ||
e5a52504 | 98 | flags = bed->dynamic_sec_flags; |
252b5132 | 99 | |
3496cb2a | 100 | s = bfd_make_section_with_flags (abfd, ".got", flags); |
252b5132 | 101 | if (s == NULL |
252b5132 | 102 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 103 | return FALSE; |
252b5132 RH |
104 | |
105 | if (bed->want_got_plt) | |
106 | { | |
3496cb2a | 107 | s = bfd_make_section_with_flags (abfd, ".got.plt", flags); |
252b5132 | 108 | if (s == NULL |
252b5132 | 109 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 110 | return FALSE; |
252b5132 RH |
111 | } |
112 | ||
2517a57f AM |
113 | if (bed->want_got_sym) |
114 | { | |
115 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
116 | (or .got.plt) section. We don't do this in the linker script | |
117 | because we don't want to define the symbol if we are not creating | |
118 | a global offset table. */ | |
d98685ac | 119 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_"); |
2517a57f | 120 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
121 | if (h == NULL) |
122 | return FALSE; | |
2517a57f | 123 | } |
252b5132 RH |
124 | |
125 | /* The first bit of the global offset table is the header. */ | |
3b36f7e6 | 126 | s->size += bed->got_header_size; |
252b5132 | 127 | |
b34976b6 | 128 | return TRUE; |
252b5132 RH |
129 | } |
130 | \f | |
7e9f0867 AM |
131 | /* Create a strtab to hold the dynamic symbol names. */ |
132 | static bfd_boolean | |
133 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
134 | { | |
135 | struct elf_link_hash_table *hash_table; | |
136 | ||
137 | hash_table = elf_hash_table (info); | |
138 | if (hash_table->dynobj == NULL) | |
139 | hash_table->dynobj = abfd; | |
140 | ||
141 | if (hash_table->dynstr == NULL) | |
142 | { | |
143 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
144 | if (hash_table->dynstr == NULL) | |
145 | return FALSE; | |
146 | } | |
147 | return TRUE; | |
148 | } | |
149 | ||
45d6a902 AM |
150 | /* Create some sections which will be filled in with dynamic linking |
151 | information. ABFD is an input file which requires dynamic sections | |
152 | to be created. The dynamic sections take up virtual memory space | |
153 | when the final executable is run, so we need to create them before | |
154 | addresses are assigned to the output sections. We work out the | |
155 | actual contents and size of these sections later. */ | |
252b5132 | 156 | |
b34976b6 | 157 | bfd_boolean |
268b6b39 | 158 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 159 | { |
45d6a902 AM |
160 | flagword flags; |
161 | register asection *s; | |
9c5bfbb7 | 162 | const struct elf_backend_data *bed; |
252b5132 | 163 | |
0eddce27 | 164 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
165 | return FALSE; |
166 | ||
167 | if (elf_hash_table (info)->dynamic_sections_created) | |
168 | return TRUE; | |
169 | ||
7e9f0867 AM |
170 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
171 | return FALSE; | |
45d6a902 | 172 | |
7e9f0867 | 173 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
174 | bed = get_elf_backend_data (abfd); |
175 | ||
176 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
177 | |
178 | /* A dynamically linked executable has a .interp section, but a | |
179 | shared library does not. */ | |
36af4a4e | 180 | if (info->executable) |
252b5132 | 181 | { |
3496cb2a L |
182 | s = bfd_make_section_with_flags (abfd, ".interp", |
183 | flags | SEC_READONLY); | |
184 | if (s == NULL) | |
45d6a902 AM |
185 | return FALSE; |
186 | } | |
bb0deeff | 187 | |
0eddce27 | 188 | if (! info->traditional_format) |
45d6a902 | 189 | { |
3496cb2a L |
190 | s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr", |
191 | flags | SEC_READONLY); | |
45d6a902 | 192 | if (s == NULL |
45d6a902 AM |
193 | || ! bfd_set_section_alignment (abfd, s, 2)) |
194 | return FALSE; | |
195 | elf_hash_table (info)->eh_info.hdr_sec = s; | |
196 | } | |
bb0deeff | 197 | |
45d6a902 AM |
198 | /* Create sections to hold version informations. These are removed |
199 | if they are not needed. */ | |
3496cb2a L |
200 | s = bfd_make_section_with_flags (abfd, ".gnu.version_d", |
201 | flags | SEC_READONLY); | |
45d6a902 | 202 | if (s == NULL |
45d6a902 AM |
203 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
204 | return FALSE; | |
205 | ||
3496cb2a L |
206 | s = bfd_make_section_with_flags (abfd, ".gnu.version", |
207 | flags | SEC_READONLY); | |
45d6a902 | 208 | if (s == NULL |
45d6a902 AM |
209 | || ! bfd_set_section_alignment (abfd, s, 1)) |
210 | return FALSE; | |
211 | ||
3496cb2a L |
212 | s = bfd_make_section_with_flags (abfd, ".gnu.version_r", |
213 | flags | SEC_READONLY); | |
45d6a902 | 214 | if (s == NULL |
45d6a902 AM |
215 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
216 | return FALSE; | |
217 | ||
3496cb2a L |
218 | s = bfd_make_section_with_flags (abfd, ".dynsym", |
219 | flags | SEC_READONLY); | |
45d6a902 | 220 | if (s == NULL |
45d6a902 AM |
221 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
222 | return FALSE; | |
223 | ||
3496cb2a L |
224 | s = bfd_make_section_with_flags (abfd, ".dynstr", |
225 | flags | SEC_READONLY); | |
226 | if (s == NULL) | |
45d6a902 AM |
227 | return FALSE; |
228 | ||
3496cb2a | 229 | s = bfd_make_section_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 230 | if (s == NULL |
45d6a902 AM |
231 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
232 | return FALSE; | |
233 | ||
234 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
235 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
236 | only want to define it if we are, in fact, creating a .dynamic | |
237 | section. We don't want to define it if there is no .dynamic | |
238 | section, since on some ELF platforms the start up code examines it | |
239 | to decide how to initialize the process. */ | |
d98685ac | 240 | if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC")) |
45d6a902 AM |
241 | return FALSE; |
242 | ||
3496cb2a L |
243 | s = bfd_make_section_with_flags (abfd, ".hash", |
244 | flags | SEC_READONLY); | |
45d6a902 | 245 | if (s == NULL |
45d6a902 AM |
246 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
247 | return FALSE; | |
248 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
249 | ||
250 | /* Let the backend create the rest of the sections. This lets the | |
251 | backend set the right flags. The backend will normally create | |
252 | the .got and .plt sections. */ | |
253 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
254 | return FALSE; | |
255 | ||
256 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
257 | ||
258 | return TRUE; | |
259 | } | |
260 | ||
261 | /* Create dynamic sections when linking against a dynamic object. */ | |
262 | ||
263 | bfd_boolean | |
268b6b39 | 264 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
265 | { |
266 | flagword flags, pltflags; | |
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. */ | |
292 | if (bed->want_plt_sym | |
293 | && !_bfd_elf_define_linkage_sym (abfd, info, s, | |
294 | "_PROCEDURE_LINKAGE_TABLE_")) | |
295 | return FALSE; | |
252b5132 | 296 | |
3496cb2a L |
297 | s = bfd_make_section_with_flags (abfd, |
298 | (bed->default_use_rela_p | |
299 | ? ".rela.plt" : ".rel.plt"), | |
300 | flags | SEC_READONLY); | |
252b5132 | 301 | if (s == NULL |
45d6a902 | 302 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 303 | return FALSE; |
252b5132 RH |
304 | |
305 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 306 | return FALSE; |
252b5132 | 307 | |
3018b441 RH |
308 | if (bed->want_dynbss) |
309 | { | |
310 | /* The .dynbss section is a place to put symbols which are defined | |
311 | by dynamic objects, are referenced by regular objects, and are | |
312 | not functions. We must allocate space for them in the process | |
313 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
314 | initialize them at run time. The linker script puts the .dynbss | |
315 | section into the .bss section of the final image. */ | |
3496cb2a L |
316 | s = bfd_make_section_with_flags (abfd, ".dynbss", |
317 | (SEC_ALLOC | |
318 | | SEC_LINKER_CREATED)); | |
319 | if (s == NULL) | |
b34976b6 | 320 | return FALSE; |
252b5132 | 321 | |
3018b441 | 322 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
323 | normally needed. We need to create it here, though, so that the |
324 | linker will map it to an output section. We can't just create it | |
325 | only if we need it, because we will not know whether we need it | |
326 | until we have seen all the input files, and the first time the | |
327 | main linker code calls BFD after examining all the input files | |
328 | (size_dynamic_sections) the input sections have already been | |
329 | mapped to the output sections. If the section turns out not to | |
330 | be needed, we can discard it later. We will never need this | |
331 | section when generating a shared object, since they do not use | |
332 | copy relocs. */ | |
3018b441 RH |
333 | if (! info->shared) |
334 | { | |
3496cb2a L |
335 | s = bfd_make_section_with_flags (abfd, |
336 | (bed->default_use_rela_p | |
337 | ? ".rela.bss" : ".rel.bss"), | |
338 | flags | SEC_READONLY); | |
3018b441 | 339 | if (s == NULL |
45d6a902 | 340 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 341 | return FALSE; |
3018b441 | 342 | } |
252b5132 RH |
343 | } |
344 | ||
b34976b6 | 345 | return TRUE; |
252b5132 RH |
346 | } |
347 | \f | |
252b5132 RH |
348 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
349 | read the input files, since we need to have a list of all of them | |
350 | before we can determine the final sizes of the output sections. | |
351 | Note that we may actually call this function even though we are not | |
352 | going to output any dynamic symbols; in some cases we know that a | |
353 | symbol should be in the dynamic symbol table, but only if there is | |
354 | one. */ | |
355 | ||
b34976b6 | 356 | bfd_boolean |
c152c796 AM |
357 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
358 | struct elf_link_hash_entry *h) | |
252b5132 RH |
359 | { |
360 | if (h->dynindx == -1) | |
361 | { | |
2b0f7ef9 | 362 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 363 | char *p; |
252b5132 | 364 | const char *name; |
252b5132 RH |
365 | bfd_size_type indx; |
366 | ||
7a13edea NC |
367 | /* XXX: The ABI draft says the linker must turn hidden and |
368 | internal symbols into STB_LOCAL symbols when producing the | |
369 | DSO. However, if ld.so honors st_other in the dynamic table, | |
370 | this would not be necessary. */ | |
371 | switch (ELF_ST_VISIBILITY (h->other)) | |
372 | { | |
373 | case STV_INTERNAL: | |
374 | case STV_HIDDEN: | |
9d6eee78 L |
375 | if (h->root.type != bfd_link_hash_undefined |
376 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 377 | { |
f5385ebf | 378 | h->forced_local = 1; |
67687978 PB |
379 | if (!elf_hash_table (info)->is_relocatable_executable) |
380 | return TRUE; | |
7a13edea | 381 | } |
0444bdd4 | 382 | |
7a13edea NC |
383 | default: |
384 | break; | |
385 | } | |
386 | ||
252b5132 RH |
387 | h->dynindx = elf_hash_table (info)->dynsymcount; |
388 | ++elf_hash_table (info)->dynsymcount; | |
389 | ||
390 | dynstr = elf_hash_table (info)->dynstr; | |
391 | if (dynstr == NULL) | |
392 | { | |
393 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 394 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 395 | if (dynstr == NULL) |
b34976b6 | 396 | return FALSE; |
252b5132 RH |
397 | } |
398 | ||
399 | /* We don't put any version information in the dynamic string | |
aad5d350 | 400 | table. */ |
252b5132 RH |
401 | name = h->root.root.string; |
402 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
403 | if (p != NULL) |
404 | /* We know that the p points into writable memory. In fact, | |
405 | there are only a few symbols that have read-only names, being | |
406 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
407 | by the backends. Most symbols will have names pointing into | |
408 | an ELF string table read from a file, or to objalloc memory. */ | |
409 | *p = 0; | |
410 | ||
411 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
412 | ||
413 | if (p != NULL) | |
414 | *p = ELF_VER_CHR; | |
252b5132 RH |
415 | |
416 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 417 | return FALSE; |
252b5132 RH |
418 | h->dynstr_index = indx; |
419 | } | |
420 | ||
b34976b6 | 421 | return TRUE; |
252b5132 | 422 | } |
45d6a902 AM |
423 | \f |
424 | /* Record an assignment to a symbol made by a linker script. We need | |
425 | this in case some dynamic object refers to this symbol. */ | |
426 | ||
427 | bfd_boolean | |
eaae8ded | 428 | bfd_elf_record_link_assignment (struct bfd_link_info *info, |
268b6b39 AM |
429 | const char *name, |
430 | bfd_boolean provide) | |
45d6a902 AM |
431 | { |
432 | struct elf_link_hash_entry *h; | |
4ea42fb7 | 433 | struct elf_link_hash_table *htab; |
45d6a902 | 434 | |
0eddce27 | 435 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
436 | return TRUE; |
437 | ||
4ea42fb7 AM |
438 | htab = elf_hash_table (info); |
439 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 440 | if (h == NULL) |
4ea42fb7 | 441 | return provide; |
45d6a902 | 442 | |
02bb6eae AO |
443 | /* Since we're defining the symbol, don't let it seem to have not |
444 | been defined. record_dynamic_symbol and size_dynamic_sections | |
77cfaee6 | 445 | may depend on this. */ |
02bb6eae AO |
446 | if (h->root.type == bfd_link_hash_undefweak |
447 | || h->root.type == bfd_link_hash_undefined) | |
77cfaee6 | 448 | { |
4ea42fb7 | 449 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
450 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
451 | bfd_link_repair_undef_list (&htab->root); | |
77cfaee6 | 452 | } |
02bb6eae | 453 | |
45d6a902 | 454 | if (h->root.type == bfd_link_hash_new) |
f5385ebf | 455 | h->non_elf = 0; |
45d6a902 AM |
456 | |
457 | /* If this symbol is being provided by the linker script, and it is | |
458 | currently defined by a dynamic object, but not by a regular | |
459 | object, then mark it as undefined so that the generic linker will | |
460 | force the correct value. */ | |
461 | if (provide | |
f5385ebf AM |
462 | && h->def_dynamic |
463 | && !h->def_regular) | |
45d6a902 AM |
464 | h->root.type = bfd_link_hash_undefined; |
465 | ||
466 | /* If this symbol is not being provided by the linker script, and it is | |
467 | currently defined by a dynamic object, but not by a regular object, | |
468 | then clear out any version information because the symbol will not be | |
469 | associated with the dynamic object any more. */ | |
470 | if (!provide | |
f5385ebf AM |
471 | && h->def_dynamic |
472 | && !h->def_regular) | |
45d6a902 AM |
473 | h->verinfo.verdef = NULL; |
474 | ||
f5385ebf | 475 | h->def_regular = 1; |
45d6a902 | 476 | |
6fa3860b PB |
477 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
478 | and executables. */ | |
479 | if (!info->relocatable | |
480 | && h->dynindx != -1 | |
481 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
482 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
483 | h->forced_local = 1; | |
484 | ||
f5385ebf AM |
485 | if ((h->def_dynamic |
486 | || h->ref_dynamic | |
67687978 PB |
487 | || info->shared |
488 | || (info->executable && elf_hash_table (info)->is_relocatable_executable)) | |
45d6a902 AM |
489 | && h->dynindx == -1) |
490 | { | |
c152c796 | 491 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
492 | return FALSE; |
493 | ||
494 | /* If this is a weak defined symbol, and we know a corresponding | |
495 | real symbol from the same dynamic object, make sure the real | |
496 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
497 | if (h->u.weakdef != NULL |
498 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 499 | { |
f6e332e6 | 500 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
501 | return FALSE; |
502 | } | |
503 | } | |
504 | ||
505 | return TRUE; | |
506 | } | |
42751cf3 | 507 | |
8c58d23b AM |
508 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
509 | success, and 2 on a failure caused by attempting to record a symbol | |
510 | in a discarded section, eg. a discarded link-once section symbol. */ | |
511 | ||
512 | int | |
c152c796 AM |
513 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
514 | bfd *input_bfd, | |
515 | long input_indx) | |
8c58d23b AM |
516 | { |
517 | bfd_size_type amt; | |
518 | struct elf_link_local_dynamic_entry *entry; | |
519 | struct elf_link_hash_table *eht; | |
520 | struct elf_strtab_hash *dynstr; | |
521 | unsigned long dynstr_index; | |
522 | char *name; | |
523 | Elf_External_Sym_Shndx eshndx; | |
524 | char esym[sizeof (Elf64_External_Sym)]; | |
525 | ||
0eddce27 | 526 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
527 | return 0; |
528 | ||
529 | /* See if the entry exists already. */ | |
530 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
531 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
532 | return 1; | |
533 | ||
534 | amt = sizeof (*entry); | |
268b6b39 | 535 | entry = bfd_alloc (input_bfd, amt); |
8c58d23b AM |
536 | if (entry == NULL) |
537 | return 0; | |
538 | ||
539 | /* Go find the symbol, so that we can find it's name. */ | |
540 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 541 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
542 | { |
543 | bfd_release (input_bfd, entry); | |
544 | return 0; | |
545 | } | |
546 | ||
547 | if (entry->isym.st_shndx != SHN_UNDEF | |
548 | && (entry->isym.st_shndx < SHN_LORESERVE | |
549 | || entry->isym.st_shndx > SHN_HIRESERVE)) | |
550 | { | |
551 | asection *s; | |
552 | ||
553 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
554 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
555 | { | |
556 | /* We can still bfd_release here as nothing has done another | |
557 | bfd_alloc. We can't do this later in this function. */ | |
558 | bfd_release (input_bfd, entry); | |
559 | return 2; | |
560 | } | |
561 | } | |
562 | ||
563 | name = (bfd_elf_string_from_elf_section | |
564 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
565 | entry->isym.st_name)); | |
566 | ||
567 | dynstr = elf_hash_table (info)->dynstr; | |
568 | if (dynstr == NULL) | |
569 | { | |
570 | /* Create a strtab to hold the dynamic symbol names. */ | |
571 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
572 | if (dynstr == NULL) | |
573 | return 0; | |
574 | } | |
575 | ||
b34976b6 | 576 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
577 | if (dynstr_index == (unsigned long) -1) |
578 | return 0; | |
579 | entry->isym.st_name = dynstr_index; | |
580 | ||
581 | eht = elf_hash_table (info); | |
582 | ||
583 | entry->next = eht->dynlocal; | |
584 | eht->dynlocal = entry; | |
585 | entry->input_bfd = input_bfd; | |
586 | entry->input_indx = input_indx; | |
587 | eht->dynsymcount++; | |
588 | ||
589 | /* Whatever binding the symbol had before, it's now local. */ | |
590 | entry->isym.st_info | |
591 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
592 | ||
593 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
594 | ||
595 | return 1; | |
596 | } | |
597 | ||
30b30c21 | 598 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 599 | |
30b30c21 | 600 | long |
268b6b39 AM |
601 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
602 | bfd *input_bfd, | |
603 | long input_indx) | |
30b30c21 RH |
604 | { |
605 | struct elf_link_local_dynamic_entry *e; | |
606 | ||
607 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
608 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
609 | return e->dynindx; | |
610 | return -1; | |
611 | } | |
612 | ||
613 | /* This function is used to renumber the dynamic symbols, if some of | |
614 | them are removed because they are marked as local. This is called | |
615 | via elf_link_hash_traverse. */ | |
616 | ||
b34976b6 | 617 | static bfd_boolean |
268b6b39 AM |
618 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
619 | void *data) | |
42751cf3 | 620 | { |
268b6b39 | 621 | size_t *count = data; |
30b30c21 | 622 | |
e92d460e AM |
623 | if (h->root.type == bfd_link_hash_warning) |
624 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
625 | ||
6fa3860b PB |
626 | if (h->forced_local) |
627 | return TRUE; | |
628 | ||
629 | if (h->dynindx != -1) | |
630 | h->dynindx = ++(*count); | |
631 | ||
632 | return TRUE; | |
633 | } | |
634 | ||
635 | ||
636 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
637 | STB_LOCAL binding. */ | |
638 | ||
639 | static bfd_boolean | |
640 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
641 | void *data) | |
642 | { | |
643 | size_t *count = data; | |
644 | ||
645 | if (h->root.type == bfd_link_hash_warning) | |
646 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
647 | ||
648 | if (!h->forced_local) | |
649 | return TRUE; | |
650 | ||
42751cf3 | 651 | if (h->dynindx != -1) |
30b30c21 RH |
652 | h->dynindx = ++(*count); |
653 | ||
b34976b6 | 654 | return TRUE; |
42751cf3 | 655 | } |
30b30c21 | 656 | |
aee6f5b4 AO |
657 | /* Return true if the dynamic symbol for a given section should be |
658 | omitted when creating a shared library. */ | |
659 | bfd_boolean | |
660 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
661 | struct bfd_link_info *info, | |
662 | asection *p) | |
663 | { | |
664 | switch (elf_section_data (p)->this_hdr.sh_type) | |
665 | { | |
666 | case SHT_PROGBITS: | |
667 | case SHT_NOBITS: | |
668 | /* If sh_type is yet undecided, assume it could be | |
669 | SHT_PROGBITS/SHT_NOBITS. */ | |
670 | case SHT_NULL: | |
671 | if (strcmp (p->name, ".got") == 0 | |
672 | || strcmp (p->name, ".got.plt") == 0 | |
673 | || strcmp (p->name, ".plt") == 0) | |
674 | { | |
675 | asection *ip; | |
676 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
677 | ||
678 | if (dynobj != NULL | |
1da212d6 | 679 | && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL |
aee6f5b4 AO |
680 | && (ip->flags & SEC_LINKER_CREATED) |
681 | && ip->output_section == p) | |
682 | return TRUE; | |
683 | } | |
684 | return FALSE; | |
685 | ||
686 | /* There shouldn't be section relative relocations | |
687 | against any other section. */ | |
688 | default: | |
689 | return TRUE; | |
690 | } | |
691 | } | |
692 | ||
062e2358 | 693 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
694 | symbol for each output section, which come first. Next come symbols |
695 | which have been forced to local binding. Then all of the back-end | |
696 | allocated local dynamic syms, followed by the rest of the global | |
697 | symbols. */ | |
30b30c21 | 698 | |
554220db AM |
699 | static unsigned long |
700 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
701 | struct bfd_link_info *info, | |
702 | unsigned long *section_sym_count) | |
30b30c21 RH |
703 | { |
704 | unsigned long dynsymcount = 0; | |
705 | ||
67687978 | 706 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
30b30c21 | 707 | { |
aee6f5b4 | 708 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
709 | asection *p; |
710 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 711 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
712 | && (p->flags & SEC_ALLOC) != 0 |
713 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
714 | elf_section_data (p)->dynindx = ++dynsymcount; | |
30b30c21 | 715 | } |
554220db | 716 | *section_sym_count = dynsymcount; |
30b30c21 | 717 | |
6fa3860b PB |
718 | elf_link_hash_traverse (elf_hash_table (info), |
719 | elf_link_renumber_local_hash_table_dynsyms, | |
720 | &dynsymcount); | |
721 | ||
30b30c21 RH |
722 | if (elf_hash_table (info)->dynlocal) |
723 | { | |
724 | struct elf_link_local_dynamic_entry *p; | |
725 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
726 | p->dynindx = ++dynsymcount; | |
727 | } | |
728 | ||
729 | elf_link_hash_traverse (elf_hash_table (info), | |
730 | elf_link_renumber_hash_table_dynsyms, | |
731 | &dynsymcount); | |
732 | ||
733 | /* There is an unused NULL entry at the head of the table which | |
734 | we must account for in our count. Unless there weren't any | |
735 | symbols, which means we'll have no table at all. */ | |
736 | if (dynsymcount != 0) | |
737 | ++dynsymcount; | |
738 | ||
739 | return elf_hash_table (info)->dynsymcount = dynsymcount; | |
740 | } | |
252b5132 | 741 | |
45d6a902 AM |
742 | /* This function is called when we want to define a new symbol. It |
743 | handles the various cases which arise when we find a definition in | |
744 | a dynamic object, or when there is already a definition in a | |
745 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
746 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
747 | OVERRIDE if the old symbol is overriding a new definition. We set | |
748 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
749 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
750 | change, we mean that we shouldn't warn if the type or size does | |
af44c138 L |
751 | change. We set POLD_ALIGNMENT if an old common symbol in a dynamic |
752 | object is overridden by a regular object. */ | |
45d6a902 AM |
753 | |
754 | bfd_boolean | |
268b6b39 AM |
755 | _bfd_elf_merge_symbol (bfd *abfd, |
756 | struct bfd_link_info *info, | |
757 | const char *name, | |
758 | Elf_Internal_Sym *sym, | |
759 | asection **psec, | |
760 | bfd_vma *pvalue, | |
af44c138 | 761 | unsigned int *pold_alignment, |
268b6b39 AM |
762 | struct elf_link_hash_entry **sym_hash, |
763 | bfd_boolean *skip, | |
764 | bfd_boolean *override, | |
765 | bfd_boolean *type_change_ok, | |
0f8a2703 | 766 | bfd_boolean *size_change_ok) |
252b5132 | 767 | { |
7479dfd4 | 768 | asection *sec, *oldsec; |
45d6a902 AM |
769 | struct elf_link_hash_entry *h; |
770 | struct elf_link_hash_entry *flip; | |
771 | int bind; | |
772 | bfd *oldbfd; | |
773 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
77cfaee6 | 774 | bfd_boolean newweak, oldweak; |
a4d8e49b | 775 | const struct elf_backend_data *bed; |
45d6a902 AM |
776 | |
777 | *skip = FALSE; | |
778 | *override = FALSE; | |
779 | ||
780 | sec = *psec; | |
781 | bind = ELF_ST_BIND (sym->st_info); | |
782 | ||
783 | if (! bfd_is_und_section (sec)) | |
784 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
785 | else | |
786 | h = ((struct elf_link_hash_entry *) | |
787 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
788 | if (h == NULL) | |
789 | return FALSE; | |
790 | *sym_hash = h; | |
252b5132 | 791 | |
45d6a902 AM |
792 | /* This code is for coping with dynamic objects, and is only useful |
793 | if we are doing an ELF link. */ | |
794 | if (info->hash->creator != abfd->xvec) | |
795 | return TRUE; | |
252b5132 | 796 | |
45d6a902 AM |
797 | /* For merging, we only care about real symbols. */ |
798 | ||
799 | while (h->root.type == bfd_link_hash_indirect | |
800 | || h->root.type == bfd_link_hash_warning) | |
801 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
802 | ||
803 | /* If we just created the symbol, mark it as being an ELF symbol. | |
804 | Other than that, there is nothing to do--there is no merge issue | |
805 | with a newly defined symbol--so we just return. */ | |
806 | ||
807 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 808 | { |
f5385ebf | 809 | h->non_elf = 0; |
45d6a902 AM |
810 | return TRUE; |
811 | } | |
252b5132 | 812 | |
7479dfd4 L |
813 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
814 | existing symbol. */ | |
252b5132 | 815 | |
45d6a902 AM |
816 | switch (h->root.type) |
817 | { | |
818 | default: | |
819 | oldbfd = NULL; | |
7479dfd4 | 820 | oldsec = NULL; |
45d6a902 | 821 | break; |
252b5132 | 822 | |
45d6a902 AM |
823 | case bfd_link_hash_undefined: |
824 | case bfd_link_hash_undefweak: | |
825 | oldbfd = h->root.u.undef.abfd; | |
7479dfd4 | 826 | oldsec = NULL; |
45d6a902 AM |
827 | break; |
828 | ||
829 | case bfd_link_hash_defined: | |
830 | case bfd_link_hash_defweak: | |
831 | oldbfd = h->root.u.def.section->owner; | |
7479dfd4 | 832 | oldsec = h->root.u.def.section; |
45d6a902 AM |
833 | break; |
834 | ||
835 | case bfd_link_hash_common: | |
836 | oldbfd = h->root.u.c.p->section->owner; | |
7479dfd4 | 837 | oldsec = h->root.u.c.p->section; |
45d6a902 AM |
838 | break; |
839 | } | |
840 | ||
841 | /* In cases involving weak versioned symbols, we may wind up trying | |
842 | to merge a symbol with itself. Catch that here, to avoid the | |
843 | confusion that results if we try to override a symbol with | |
844 | itself. The additional tests catch cases like | |
845 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
846 | dynamic object, which we do want to handle here. */ | |
847 | if (abfd == oldbfd | |
848 | && ((abfd->flags & DYNAMIC) == 0 | |
f5385ebf | 849 | || !h->def_regular)) |
45d6a902 AM |
850 | return TRUE; |
851 | ||
852 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
853 | respectively, is from a dynamic object. */ | |
854 | ||
855 | if ((abfd->flags & DYNAMIC) != 0) | |
856 | newdyn = TRUE; | |
857 | else | |
858 | newdyn = FALSE; | |
859 | ||
860 | if (oldbfd != NULL) | |
861 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
862 | else | |
863 | { | |
864 | asection *hsec; | |
865 | ||
866 | /* This code handles the special SHN_MIPS_{TEXT,DATA} section | |
867 | indices used by MIPS ELF. */ | |
868 | switch (h->root.type) | |
252b5132 | 869 | { |
45d6a902 AM |
870 | default: |
871 | hsec = NULL; | |
872 | break; | |
252b5132 | 873 | |
45d6a902 AM |
874 | case bfd_link_hash_defined: |
875 | case bfd_link_hash_defweak: | |
876 | hsec = h->root.u.def.section; | |
877 | break; | |
252b5132 | 878 | |
45d6a902 AM |
879 | case bfd_link_hash_common: |
880 | hsec = h->root.u.c.p->section; | |
881 | break; | |
252b5132 | 882 | } |
252b5132 | 883 | |
45d6a902 AM |
884 | if (hsec == NULL) |
885 | olddyn = FALSE; | |
886 | else | |
887 | olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0; | |
888 | } | |
252b5132 | 889 | |
45d6a902 AM |
890 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
891 | respectively, appear to be a definition rather than reference. */ | |
892 | ||
893 | if (bfd_is_und_section (sec) || bfd_is_com_section (sec)) | |
894 | newdef = FALSE; | |
895 | else | |
896 | newdef = TRUE; | |
897 | ||
898 | if (h->root.type == bfd_link_hash_undefined | |
899 | || h->root.type == bfd_link_hash_undefweak | |
900 | || h->root.type == bfd_link_hash_common) | |
901 | olddef = FALSE; | |
902 | else | |
903 | olddef = TRUE; | |
904 | ||
7479dfd4 L |
905 | /* Check TLS symbol. */ |
906 | if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS) | |
907 | && ELF_ST_TYPE (sym->st_info) != h->type) | |
908 | { | |
909 | bfd *ntbfd, *tbfd; | |
910 | bfd_boolean ntdef, tdef; | |
911 | asection *ntsec, *tsec; | |
912 | ||
913 | if (h->type == STT_TLS) | |
914 | { | |
3b36f7e6 | 915 | ntbfd = abfd; |
7479dfd4 L |
916 | ntsec = sec; |
917 | ntdef = newdef; | |
918 | tbfd = oldbfd; | |
919 | tsec = oldsec; | |
920 | tdef = olddef; | |
921 | } | |
922 | else | |
923 | { | |
924 | ntbfd = oldbfd; | |
925 | ntsec = oldsec; | |
926 | ntdef = olddef; | |
927 | tbfd = abfd; | |
928 | tsec = sec; | |
929 | tdef = newdef; | |
930 | } | |
931 | ||
932 | if (tdef && ntdef) | |
933 | (*_bfd_error_handler) | |
934 | (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), | |
935 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); | |
936 | else if (!tdef && !ntdef) | |
937 | (*_bfd_error_handler) | |
938 | (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), | |
939 | tbfd, ntbfd, h->root.root.string); | |
940 | else if (tdef) | |
941 | (*_bfd_error_handler) | |
942 | (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), | |
943 | tbfd, tsec, ntbfd, h->root.root.string); | |
944 | else | |
945 | (*_bfd_error_handler) | |
946 | (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), | |
947 | tbfd, ntbfd, ntsec, h->root.root.string); | |
948 | ||
949 | bfd_set_error (bfd_error_bad_value); | |
950 | return FALSE; | |
951 | } | |
952 | ||
4cc11e76 | 953 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
954 | object or is weak in all dynamic objects. Internal and hidden |
955 | visibility will make it unavailable to dynamic objects. */ | |
f5385ebf | 956 | if (newdyn && !h->dynamic_def) |
45d6a902 AM |
957 | { |
958 | if (!bfd_is_und_section (sec)) | |
f5385ebf | 959 | h->dynamic_def = 1; |
45d6a902 | 960 | else |
252b5132 | 961 | { |
45d6a902 AM |
962 | /* Check if this symbol is weak in all dynamic objects. If it |
963 | is the first time we see it in a dynamic object, we mark | |
964 | if it is weak. Otherwise, we clear it. */ | |
f5385ebf | 965 | if (!h->ref_dynamic) |
79349b09 | 966 | { |
45d6a902 | 967 | if (bind == STB_WEAK) |
f5385ebf | 968 | h->dynamic_weak = 1; |
252b5132 | 969 | } |
45d6a902 | 970 | else if (bind != STB_WEAK) |
f5385ebf | 971 | h->dynamic_weak = 0; |
252b5132 | 972 | } |
45d6a902 | 973 | } |
252b5132 | 974 | |
45d6a902 AM |
975 | /* If the old symbol has non-default visibility, we ignore the new |
976 | definition from a dynamic object. */ | |
977 | if (newdyn | |
9c7a29a3 | 978 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
979 | && !bfd_is_und_section (sec)) |
980 | { | |
981 | *skip = TRUE; | |
982 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 983 | h->ref_dynamic = 1; |
45d6a902 AM |
984 | /* A protected symbol has external availability. Make sure it is |
985 | recorded as dynamic. | |
986 | ||
987 | FIXME: Should we check type and size for protected symbol? */ | |
988 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 989 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
990 | else |
991 | return TRUE; | |
992 | } | |
993 | else if (!newdyn | |
9c7a29a3 | 994 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 995 | && h->def_dynamic) |
45d6a902 AM |
996 | { |
997 | /* If the new symbol with non-default visibility comes from a | |
998 | relocatable file and the old definition comes from a dynamic | |
999 | object, we remove the old definition. */ | |
1000 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1001 | h = *sym_hash; | |
1de1a317 | 1002 | |
f6e332e6 | 1003 | if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root) |
1de1a317 L |
1004 | && bfd_is_und_section (sec)) |
1005 | { | |
1006 | /* If the new symbol is undefined and the old symbol was | |
1007 | also undefined before, we need to make sure | |
1008 | _bfd_generic_link_add_one_symbol doesn't mess | |
f6e332e6 | 1009 | up the linker hash table undefs list. Since the old |
1de1a317 L |
1010 | definition came from a dynamic object, it is still on the |
1011 | undefs list. */ | |
1012 | h->root.type = bfd_link_hash_undefined; | |
1de1a317 L |
1013 | h->root.u.undef.abfd = abfd; |
1014 | } | |
1015 | else | |
1016 | { | |
1017 | h->root.type = bfd_link_hash_new; | |
1018 | h->root.u.undef.abfd = NULL; | |
1019 | } | |
1020 | ||
f5385ebf | 1021 | if (h->def_dynamic) |
252b5132 | 1022 | { |
f5385ebf AM |
1023 | h->def_dynamic = 0; |
1024 | h->ref_dynamic = 1; | |
1025 | h->dynamic_def = 1; | |
45d6a902 AM |
1026 | } |
1027 | /* FIXME: Should we check type and size for protected symbol? */ | |
1028 | h->size = 0; | |
1029 | h->type = 0; | |
1030 | return TRUE; | |
1031 | } | |
14a793b2 | 1032 | |
79349b09 AM |
1033 | /* Differentiate strong and weak symbols. */ |
1034 | newweak = bind == STB_WEAK; | |
1035 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1036 | || h->root.type == bfd_link_hash_undefweak); | |
14a793b2 | 1037 | |
15b43f48 AM |
1038 | /* If a new weak symbol definition comes from a regular file and the |
1039 | old symbol comes from a dynamic library, we treat the new one as | |
1040 | strong. Similarly, an old weak symbol definition from a regular | |
1041 | file is treated as strong when the new symbol comes from a dynamic | |
1042 | library. Further, an old weak symbol from a dynamic library is | |
1043 | treated as strong if the new symbol is from a dynamic library. | |
1044 | This reflects the way glibc's ld.so works. | |
1045 | ||
1046 | Do this before setting *type_change_ok or *size_change_ok so that | |
1047 | we warn properly when dynamic library symbols are overridden. */ | |
1048 | ||
1049 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1050 | newweak = FALSE; |
15b43f48 | 1051 | if (olddef && newdyn) |
0f8a2703 AM |
1052 | oldweak = FALSE; |
1053 | ||
79349b09 AM |
1054 | /* It's OK to change the type if either the existing symbol or the |
1055 | new symbol is weak. A type change is also OK if the old symbol | |
1056 | is undefined and the new symbol is defined. */ | |
252b5132 | 1057 | |
79349b09 AM |
1058 | if (oldweak |
1059 | || newweak | |
1060 | || (newdef | |
1061 | && h->root.type == bfd_link_hash_undefined)) | |
1062 | *type_change_ok = TRUE; | |
1063 | ||
1064 | /* It's OK to change the size if either the existing symbol or the | |
1065 | new symbol is weak, or if the old symbol is undefined. */ | |
1066 | ||
1067 | if (*type_change_ok | |
1068 | || h->root.type == bfd_link_hash_undefined) | |
1069 | *size_change_ok = TRUE; | |
45d6a902 | 1070 | |
45d6a902 AM |
1071 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1072 | symbol, respectively, appears to be a common symbol in a dynamic | |
1073 | object. If a symbol appears in an uninitialized section, and is | |
1074 | not weak, and is not a function, then it may be a common symbol | |
1075 | which was resolved when the dynamic object was created. We want | |
1076 | to treat such symbols specially, because they raise special | |
1077 | considerations when setting the symbol size: if the symbol | |
1078 | appears as a common symbol in a regular object, and the size in | |
1079 | the regular object is larger, we must make sure that we use the | |
1080 | larger size. This problematic case can always be avoided in C, | |
1081 | but it must be handled correctly when using Fortran shared | |
1082 | libraries. | |
1083 | ||
1084 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1085 | likewise for OLDDYNCOMMON and OLDDEF. | |
1086 | ||
1087 | Note that this test is just a heuristic, and that it is quite | |
1088 | possible to have an uninitialized symbol in a shared object which | |
1089 | is really a definition, rather than a common symbol. This could | |
1090 | lead to some minor confusion when the symbol really is a common | |
1091 | symbol in some regular object. However, I think it will be | |
1092 | harmless. */ | |
1093 | ||
1094 | if (newdyn | |
1095 | && newdef | |
79349b09 | 1096 | && !newweak |
45d6a902 AM |
1097 | && (sec->flags & SEC_ALLOC) != 0 |
1098 | && (sec->flags & SEC_LOAD) == 0 | |
1099 | && sym->st_size > 0 | |
45d6a902 AM |
1100 | && ELF_ST_TYPE (sym->st_info) != STT_FUNC) |
1101 | newdyncommon = TRUE; | |
1102 | else | |
1103 | newdyncommon = FALSE; | |
1104 | ||
1105 | if (olddyn | |
1106 | && olddef | |
1107 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1108 | && h->def_dynamic |
45d6a902 AM |
1109 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1110 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1111 | && h->size > 0 | |
1112 | && h->type != STT_FUNC) | |
1113 | olddyncommon = TRUE; | |
1114 | else | |
1115 | olddyncommon = FALSE; | |
1116 | ||
a4d8e49b L |
1117 | /* We now know everything about the old and new symbols. We ask the |
1118 | backend to check if we can merge them. */ | |
1119 | bed = get_elf_backend_data (abfd); | |
1120 | if (bed->merge_symbol | |
1121 | && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue, | |
1122 | pold_alignment, skip, override, | |
1123 | type_change_ok, size_change_ok, | |
1124 | &newdyn, &newdef, &newdyncommon, &newweak, | |
1125 | abfd, &sec, | |
1126 | &olddyn, &olddef, &olddyncommon, &oldweak, | |
1127 | oldbfd, &oldsec)) | |
1128 | return FALSE; | |
1129 | ||
45d6a902 AM |
1130 | /* If both the old and the new symbols look like common symbols in a |
1131 | dynamic object, set the size of the symbol to the larger of the | |
1132 | two. */ | |
1133 | ||
1134 | if (olddyncommon | |
1135 | && newdyncommon | |
1136 | && sym->st_size != h->size) | |
1137 | { | |
1138 | /* Since we think we have two common symbols, issue a multiple | |
1139 | common warning if desired. Note that we only warn if the | |
1140 | size is different. If the size is the same, we simply let | |
1141 | the old symbol override the new one as normally happens with | |
1142 | symbols defined in dynamic objects. */ | |
1143 | ||
1144 | if (! ((*info->callbacks->multiple_common) | |
1145 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1146 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1147 | return FALSE; | |
252b5132 | 1148 | |
45d6a902 AM |
1149 | if (sym->st_size > h->size) |
1150 | h->size = sym->st_size; | |
252b5132 | 1151 | |
45d6a902 | 1152 | *size_change_ok = TRUE; |
252b5132 RH |
1153 | } |
1154 | ||
45d6a902 AM |
1155 | /* If we are looking at a dynamic object, and we have found a |
1156 | definition, we need to see if the symbol was already defined by | |
1157 | some other object. If so, we want to use the existing | |
1158 | definition, and we do not want to report a multiple symbol | |
1159 | definition error; we do this by clobbering *PSEC to be | |
1160 | bfd_und_section_ptr. | |
1161 | ||
1162 | We treat a common symbol as a definition if the symbol in the | |
1163 | shared library is a function, since common symbols always | |
1164 | represent variables; this can cause confusion in principle, but | |
1165 | any such confusion would seem to indicate an erroneous program or | |
1166 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1167 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1168 | |
1169 | if (newdyn | |
1170 | && newdef | |
77cfaee6 | 1171 | && (olddef |
45d6a902 | 1172 | || (h->root.type == bfd_link_hash_common |
79349b09 | 1173 | && (newweak |
0f8a2703 | 1174 | || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))) |
45d6a902 AM |
1175 | { |
1176 | *override = TRUE; | |
1177 | newdef = FALSE; | |
1178 | newdyncommon = FALSE; | |
252b5132 | 1179 | |
45d6a902 AM |
1180 | *psec = sec = bfd_und_section_ptr; |
1181 | *size_change_ok = TRUE; | |
252b5132 | 1182 | |
45d6a902 AM |
1183 | /* If we get here when the old symbol is a common symbol, then |
1184 | we are explicitly letting it override a weak symbol or | |
1185 | function in a dynamic object, and we don't want to warn about | |
1186 | a type change. If the old symbol is a defined symbol, a type | |
1187 | change warning may still be appropriate. */ | |
252b5132 | 1188 | |
45d6a902 AM |
1189 | if (h->root.type == bfd_link_hash_common) |
1190 | *type_change_ok = TRUE; | |
1191 | } | |
1192 | ||
1193 | /* Handle the special case of an old common symbol merging with a | |
1194 | new symbol which looks like a common symbol in a shared object. | |
1195 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1196 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1197 | right thing. */ | |
45d6a902 AM |
1198 | |
1199 | if (newdyncommon | |
1200 | && h->root.type == bfd_link_hash_common) | |
1201 | { | |
1202 | *override = TRUE; | |
1203 | newdef = FALSE; | |
1204 | newdyncommon = FALSE; | |
1205 | *pvalue = sym->st_size; | |
a4d8e49b | 1206 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1207 | *size_change_ok = TRUE; |
1208 | } | |
1209 | ||
c5e2cead | 1210 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1211 | if (newdef && olddef && newweak) |
c5e2cead L |
1212 | *skip = TRUE; |
1213 | ||
45d6a902 AM |
1214 | /* If the old symbol is from a dynamic object, and the new symbol is |
1215 | a definition which is not from a dynamic object, then the new | |
1216 | symbol overrides the old symbol. Symbols from regular files | |
1217 | always take precedence over symbols from dynamic objects, even if | |
1218 | they are defined after the dynamic object in the link. | |
1219 | ||
1220 | As above, we again permit a common symbol in a regular object to | |
1221 | override a definition in a shared object if the shared object | |
0f8a2703 | 1222 | symbol is a function or is weak. */ |
45d6a902 AM |
1223 | |
1224 | flip = NULL; | |
77cfaee6 | 1225 | if (!newdyn |
45d6a902 AM |
1226 | && (newdef |
1227 | || (bfd_is_com_section (sec) | |
79349b09 AM |
1228 | && (oldweak |
1229 | || h->type == STT_FUNC))) | |
45d6a902 AM |
1230 | && olddyn |
1231 | && olddef | |
f5385ebf | 1232 | && h->def_dynamic) |
45d6a902 AM |
1233 | { |
1234 | /* Change the hash table entry to undefined, and let | |
1235 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1236 | new definition. */ | |
1237 | ||
1238 | h->root.type = bfd_link_hash_undefined; | |
1239 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1240 | *size_change_ok = TRUE; | |
1241 | ||
1242 | olddef = FALSE; | |
1243 | olddyncommon = FALSE; | |
1244 | ||
1245 | /* We again permit a type change when a common symbol may be | |
1246 | overriding a function. */ | |
1247 | ||
1248 | if (bfd_is_com_section (sec)) | |
1249 | *type_change_ok = TRUE; | |
1250 | ||
1251 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1252 | flip = *sym_hash; | |
1253 | else | |
1254 | /* This union may have been set to be non-NULL when this symbol | |
1255 | was seen in a dynamic object. We must force the union to be | |
1256 | NULL, so that it is correct for a regular symbol. */ | |
1257 | h->verinfo.vertree = NULL; | |
1258 | } | |
1259 | ||
1260 | /* Handle the special case of a new common symbol merging with an | |
1261 | old symbol that looks like it might be a common symbol defined in | |
1262 | a shared object. Note that we have already handled the case in | |
1263 | which a new common symbol should simply override the definition | |
1264 | in the shared library. */ | |
1265 | ||
1266 | if (! newdyn | |
1267 | && bfd_is_com_section (sec) | |
1268 | && olddyncommon) | |
1269 | { | |
1270 | /* It would be best if we could set the hash table entry to a | |
1271 | common symbol, but we don't know what to use for the section | |
1272 | or the alignment. */ | |
1273 | if (! ((*info->callbacks->multiple_common) | |
1274 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1275 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1276 | return FALSE; | |
1277 | ||
4cc11e76 | 1278 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1279 | larger, pretend that the new symbol has its size. */ |
1280 | ||
1281 | if (h->size > *pvalue) | |
1282 | *pvalue = h->size; | |
1283 | ||
af44c138 L |
1284 | /* We need to remember the alignment required by the symbol |
1285 | in the dynamic object. */ | |
1286 | BFD_ASSERT (pold_alignment); | |
1287 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1288 | |
1289 | olddef = FALSE; | |
1290 | olddyncommon = FALSE; | |
1291 | ||
1292 | h->root.type = bfd_link_hash_undefined; | |
1293 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1294 | ||
1295 | *size_change_ok = TRUE; | |
1296 | *type_change_ok = TRUE; | |
1297 | ||
1298 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1299 | flip = *sym_hash; | |
1300 | else | |
1301 | h->verinfo.vertree = NULL; | |
1302 | } | |
1303 | ||
1304 | if (flip != NULL) | |
1305 | { | |
1306 | /* Handle the case where we had a versioned symbol in a dynamic | |
1307 | library and now find a definition in a normal object. In this | |
1308 | case, we make the versioned symbol point to the normal one. */ | |
9c5bfbb7 | 1309 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1310 | flip->root.type = h->root.type; |
1311 | h->root.type = bfd_link_hash_indirect; | |
1312 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
1313 | (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h); | |
1314 | flip->root.u.undef.abfd = h->root.u.undef.abfd; | |
f5385ebf | 1315 | if (h->def_dynamic) |
45d6a902 | 1316 | { |
f5385ebf AM |
1317 | h->def_dynamic = 0; |
1318 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1319 | } |
1320 | } | |
1321 | ||
45d6a902 AM |
1322 | return TRUE; |
1323 | } | |
1324 | ||
1325 | /* This function is called to create an indirect symbol from the | |
1326 | default for the symbol with the default version if needed. The | |
1327 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1328 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 AM |
1329 | |
1330 | bfd_boolean | |
268b6b39 AM |
1331 | _bfd_elf_add_default_symbol (bfd *abfd, |
1332 | struct bfd_link_info *info, | |
1333 | struct elf_link_hash_entry *h, | |
1334 | const char *name, | |
1335 | Elf_Internal_Sym *sym, | |
1336 | asection **psec, | |
1337 | bfd_vma *value, | |
1338 | bfd_boolean *dynsym, | |
0f8a2703 | 1339 | bfd_boolean override) |
45d6a902 AM |
1340 | { |
1341 | bfd_boolean type_change_ok; | |
1342 | bfd_boolean size_change_ok; | |
1343 | bfd_boolean skip; | |
1344 | char *shortname; | |
1345 | struct elf_link_hash_entry *hi; | |
1346 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1347 | const struct elf_backend_data *bed; |
45d6a902 AM |
1348 | bfd_boolean collect; |
1349 | bfd_boolean dynamic; | |
1350 | char *p; | |
1351 | size_t len, shortlen; | |
1352 | asection *sec; | |
1353 | ||
1354 | /* If this symbol has a version, and it is the default version, we | |
1355 | create an indirect symbol from the default name to the fully | |
1356 | decorated name. This will cause external references which do not | |
1357 | specify a version to be bound to this version of the symbol. */ | |
1358 | p = strchr (name, ELF_VER_CHR); | |
1359 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1360 | return TRUE; | |
1361 | ||
1362 | if (override) | |
1363 | { | |
4cc11e76 | 1364 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1365 | need to create the indirect symbol from the default name. */ |
1366 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1367 | FALSE, FALSE); | |
1368 | BFD_ASSERT (hi != NULL); | |
1369 | if (hi == h) | |
1370 | return TRUE; | |
1371 | while (hi->root.type == bfd_link_hash_indirect | |
1372 | || hi->root.type == bfd_link_hash_warning) | |
1373 | { | |
1374 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1375 | if (hi == h) | |
1376 | return TRUE; | |
1377 | } | |
1378 | } | |
1379 | ||
1380 | bed = get_elf_backend_data (abfd); | |
1381 | collect = bed->collect; | |
1382 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1383 | ||
1384 | shortlen = p - name; | |
1385 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1386 | if (shortname == NULL) | |
1387 | return FALSE; | |
1388 | memcpy (shortname, name, shortlen); | |
1389 | shortname[shortlen] = '\0'; | |
1390 | ||
1391 | /* We are going to create a new symbol. Merge it with any existing | |
1392 | symbol with this name. For the purposes of the merge, act as | |
1393 | though we were defining the symbol we just defined, although we | |
1394 | actually going to define an indirect symbol. */ | |
1395 | type_change_ok = FALSE; | |
1396 | size_change_ok = FALSE; | |
1397 | sec = *psec; | |
1398 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1399 | NULL, &hi, &skip, &override, |
1400 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1401 | return FALSE; |
1402 | ||
1403 | if (skip) | |
1404 | goto nondefault; | |
1405 | ||
1406 | if (! override) | |
1407 | { | |
1408 | bh = &hi->root; | |
1409 | if (! (_bfd_generic_link_add_one_symbol | |
1410 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1411 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1412 | return FALSE; |
1413 | hi = (struct elf_link_hash_entry *) bh; | |
1414 | } | |
1415 | else | |
1416 | { | |
1417 | /* In this case the symbol named SHORTNAME is overriding the | |
1418 | indirect symbol we want to add. We were planning on making | |
1419 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1420 | is the name without a version. NAME is the fully versioned | |
1421 | name, and it is the default version. | |
1422 | ||
1423 | Overriding means that we already saw a definition for the | |
1424 | symbol SHORTNAME in a regular object, and it is overriding | |
1425 | the symbol defined in the dynamic object. | |
1426 | ||
1427 | When this happens, we actually want to change NAME, the | |
1428 | symbol we just added, to refer to SHORTNAME. This will cause | |
1429 | references to NAME in the shared object to become references | |
1430 | to SHORTNAME in the regular object. This is what we expect | |
1431 | when we override a function in a shared object: that the | |
1432 | references in the shared object will be mapped to the | |
1433 | definition in the regular object. */ | |
1434 | ||
1435 | while (hi->root.type == bfd_link_hash_indirect | |
1436 | || hi->root.type == bfd_link_hash_warning) | |
1437 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1438 | ||
1439 | h->root.type = bfd_link_hash_indirect; | |
1440 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1441 | if (h->def_dynamic) |
45d6a902 | 1442 | { |
f5385ebf AM |
1443 | h->def_dynamic = 0; |
1444 | hi->ref_dynamic = 1; | |
1445 | if (hi->ref_regular | |
1446 | || hi->def_regular) | |
45d6a902 | 1447 | { |
c152c796 | 1448 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1449 | return FALSE; |
1450 | } | |
1451 | } | |
1452 | ||
1453 | /* Now set HI to H, so that the following code will set the | |
1454 | other fields correctly. */ | |
1455 | hi = h; | |
1456 | } | |
1457 | ||
1458 | /* If there is a duplicate definition somewhere, then HI may not | |
1459 | point to an indirect symbol. We will have reported an error to | |
1460 | the user in that case. */ | |
1461 | ||
1462 | if (hi->root.type == bfd_link_hash_indirect) | |
1463 | { | |
1464 | struct elf_link_hash_entry *ht; | |
1465 | ||
45d6a902 AM |
1466 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
1467 | (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi); | |
1468 | ||
1469 | /* See if the new flags lead us to realize that the symbol must | |
1470 | be dynamic. */ | |
1471 | if (! *dynsym) | |
1472 | { | |
1473 | if (! dynamic) | |
1474 | { | |
1475 | if (info->shared | |
f5385ebf | 1476 | || hi->ref_dynamic) |
45d6a902 AM |
1477 | *dynsym = TRUE; |
1478 | } | |
1479 | else | |
1480 | { | |
f5385ebf | 1481 | if (hi->ref_regular) |
45d6a902 AM |
1482 | *dynsym = TRUE; |
1483 | } | |
1484 | } | |
1485 | } | |
1486 | ||
1487 | /* We also need to define an indirection from the nondefault version | |
1488 | of the symbol. */ | |
1489 | ||
1490 | nondefault: | |
1491 | len = strlen (name); | |
1492 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1493 | if (shortname == NULL) | |
1494 | return FALSE; | |
1495 | memcpy (shortname, name, shortlen); | |
1496 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1497 | ||
1498 | /* Once again, merge with any existing symbol. */ | |
1499 | type_change_ok = FALSE; | |
1500 | size_change_ok = FALSE; | |
1501 | sec = *psec; | |
1502 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1503 | NULL, &hi, &skip, &override, |
1504 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1505 | return FALSE; |
1506 | ||
1507 | if (skip) | |
1508 | return TRUE; | |
1509 | ||
1510 | if (override) | |
1511 | { | |
1512 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1513 | the type of override we do in the case above unless it is | |
4cc11e76 | 1514 | overridden by a versioned definition. */ |
45d6a902 AM |
1515 | if (hi->root.type != bfd_link_hash_defined |
1516 | && hi->root.type != bfd_link_hash_defweak) | |
1517 | (*_bfd_error_handler) | |
d003868e AM |
1518 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1519 | abfd, shortname); | |
45d6a902 AM |
1520 | } |
1521 | else | |
1522 | { | |
1523 | bh = &hi->root; | |
1524 | if (! (_bfd_generic_link_add_one_symbol | |
1525 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1526 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1527 | return FALSE; |
1528 | hi = (struct elf_link_hash_entry *) bh; | |
1529 | ||
1530 | /* If there is a duplicate definition somewhere, then HI may not | |
1531 | point to an indirect symbol. We will have reported an error | |
1532 | to the user in that case. */ | |
1533 | ||
1534 | if (hi->root.type == bfd_link_hash_indirect) | |
1535 | { | |
45d6a902 AM |
1536 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); |
1537 | ||
1538 | /* See if the new flags lead us to realize that the symbol | |
1539 | must be dynamic. */ | |
1540 | if (! *dynsym) | |
1541 | { | |
1542 | if (! dynamic) | |
1543 | { | |
1544 | if (info->shared | |
f5385ebf | 1545 | || hi->ref_dynamic) |
45d6a902 AM |
1546 | *dynsym = TRUE; |
1547 | } | |
1548 | else | |
1549 | { | |
f5385ebf | 1550 | if (hi->ref_regular) |
45d6a902 AM |
1551 | *dynsym = TRUE; |
1552 | } | |
1553 | } | |
1554 | } | |
1555 | } | |
1556 | ||
1557 | return TRUE; | |
1558 | } | |
1559 | \f | |
1560 | /* This routine is used to export all defined symbols into the dynamic | |
1561 | symbol table. It is called via elf_link_hash_traverse. */ | |
1562 | ||
1563 | bfd_boolean | |
268b6b39 | 1564 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1565 | { |
268b6b39 | 1566 | struct elf_info_failed *eif = data; |
45d6a902 AM |
1567 | |
1568 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
1569 | if (h->root.type == bfd_link_hash_indirect) | |
1570 | return TRUE; | |
1571 | ||
1572 | if (h->root.type == bfd_link_hash_warning) | |
1573 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1574 | ||
1575 | if (h->dynindx == -1 | |
f5385ebf AM |
1576 | && (h->def_regular |
1577 | || h->ref_regular)) | |
45d6a902 AM |
1578 | { |
1579 | struct bfd_elf_version_tree *t; | |
1580 | struct bfd_elf_version_expr *d; | |
1581 | ||
1582 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1583 | { | |
108ba305 | 1584 | if (t->globals.list != NULL) |
45d6a902 | 1585 | { |
108ba305 JJ |
1586 | d = (*t->match) (&t->globals, NULL, h->root.root.string); |
1587 | if (d != NULL) | |
1588 | goto doit; | |
45d6a902 AM |
1589 | } |
1590 | ||
108ba305 | 1591 | if (t->locals.list != NULL) |
45d6a902 | 1592 | { |
108ba305 JJ |
1593 | d = (*t->match) (&t->locals, NULL, h->root.root.string); |
1594 | if (d != NULL) | |
1595 | return TRUE; | |
45d6a902 AM |
1596 | } |
1597 | } | |
1598 | ||
1599 | if (!eif->verdefs) | |
1600 | { | |
1601 | doit: | |
c152c796 | 1602 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
1603 | { |
1604 | eif->failed = TRUE; | |
1605 | return FALSE; | |
1606 | } | |
1607 | } | |
1608 | } | |
1609 | ||
1610 | return TRUE; | |
1611 | } | |
1612 | \f | |
1613 | /* Look through the symbols which are defined in other shared | |
1614 | libraries and referenced here. Update the list of version | |
1615 | dependencies. This will be put into the .gnu.version_r section. | |
1616 | This function is called via elf_link_hash_traverse. */ | |
1617 | ||
1618 | bfd_boolean | |
268b6b39 AM |
1619 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1620 | void *data) | |
45d6a902 | 1621 | { |
268b6b39 | 1622 | struct elf_find_verdep_info *rinfo = data; |
45d6a902 AM |
1623 | Elf_Internal_Verneed *t; |
1624 | Elf_Internal_Vernaux *a; | |
1625 | bfd_size_type amt; | |
1626 | ||
1627 | if (h->root.type == bfd_link_hash_warning) | |
1628 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1629 | ||
1630 | /* We only care about symbols defined in shared objects with version | |
1631 | information. */ | |
f5385ebf AM |
1632 | if (!h->def_dynamic |
1633 | || h->def_regular | |
45d6a902 AM |
1634 | || h->dynindx == -1 |
1635 | || h->verinfo.verdef == NULL) | |
1636 | return TRUE; | |
1637 | ||
1638 | /* See if we already know about this version. */ | |
1639 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1640 | { | |
1641 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1642 | continue; | |
1643 | ||
1644 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1645 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1646 | return TRUE; | |
1647 | ||
1648 | break; | |
1649 | } | |
1650 | ||
1651 | /* This is a new version. Add it to tree we are building. */ | |
1652 | ||
1653 | if (t == NULL) | |
1654 | { | |
1655 | amt = sizeof *t; | |
268b6b39 | 1656 | t = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1657 | if (t == NULL) |
1658 | { | |
1659 | rinfo->failed = TRUE; | |
1660 | return FALSE; | |
1661 | } | |
1662 | ||
1663 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1664 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1665 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1666 | } | |
1667 | ||
1668 | amt = sizeof *a; | |
268b6b39 | 1669 | a = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1670 | |
1671 | /* Note that we are copying a string pointer here, and testing it | |
1672 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1673 | discard the string data when low in memory, this will have to be | |
1674 | fixed. */ | |
1675 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1676 | ||
1677 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1678 | a->vna_nextptr = t->vn_auxptr; | |
1679 | ||
1680 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1681 | ++rinfo->vers; | |
1682 | ||
1683 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1684 | ||
1685 | t->vn_auxptr = a; | |
1686 | ||
1687 | return TRUE; | |
1688 | } | |
1689 | ||
1690 | /* Figure out appropriate versions for all the symbols. We may not | |
1691 | have the version number script until we have read all of the input | |
1692 | files, so until that point we don't know which symbols should be | |
1693 | local. This function is called via elf_link_hash_traverse. */ | |
1694 | ||
1695 | bfd_boolean | |
268b6b39 | 1696 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
1697 | { |
1698 | struct elf_assign_sym_version_info *sinfo; | |
1699 | struct bfd_link_info *info; | |
9c5bfbb7 | 1700 | const struct elf_backend_data *bed; |
45d6a902 AM |
1701 | struct elf_info_failed eif; |
1702 | char *p; | |
1703 | bfd_size_type amt; | |
1704 | ||
268b6b39 | 1705 | sinfo = data; |
45d6a902 AM |
1706 | info = sinfo->info; |
1707 | ||
1708 | if (h->root.type == bfd_link_hash_warning) | |
1709 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1710 | ||
1711 | /* Fix the symbol flags. */ | |
1712 | eif.failed = FALSE; | |
1713 | eif.info = info; | |
1714 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1715 | { | |
1716 | if (eif.failed) | |
1717 | sinfo->failed = TRUE; | |
1718 | return FALSE; | |
1719 | } | |
1720 | ||
1721 | /* We only need version numbers for symbols defined in regular | |
1722 | objects. */ | |
f5385ebf | 1723 | if (!h->def_regular) |
45d6a902 AM |
1724 | return TRUE; |
1725 | ||
1726 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1727 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1728 | if (p != NULL && h->verinfo.vertree == NULL) | |
1729 | { | |
1730 | struct bfd_elf_version_tree *t; | |
1731 | bfd_boolean hidden; | |
1732 | ||
1733 | hidden = TRUE; | |
1734 | ||
1735 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1736 | not a hidden symbol. */ | |
1737 | ++p; | |
1738 | if (*p == ELF_VER_CHR) | |
1739 | { | |
1740 | hidden = FALSE; | |
1741 | ++p; | |
1742 | } | |
1743 | ||
1744 | /* If there is no version string, we can just return out. */ | |
1745 | if (*p == '\0') | |
1746 | { | |
1747 | if (hidden) | |
f5385ebf | 1748 | h->hidden = 1; |
45d6a902 AM |
1749 | return TRUE; |
1750 | } | |
1751 | ||
1752 | /* Look for the version. If we find it, it is no longer weak. */ | |
1753 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1754 | { | |
1755 | if (strcmp (t->name, p) == 0) | |
1756 | { | |
1757 | size_t len; | |
1758 | char *alc; | |
1759 | struct bfd_elf_version_expr *d; | |
1760 | ||
1761 | len = p - h->root.root.string; | |
268b6b39 | 1762 | alc = bfd_malloc (len); |
45d6a902 AM |
1763 | if (alc == NULL) |
1764 | return FALSE; | |
1765 | memcpy (alc, h->root.root.string, len - 1); | |
1766 | alc[len - 1] = '\0'; | |
1767 | if (alc[len - 2] == ELF_VER_CHR) | |
1768 | alc[len - 2] = '\0'; | |
1769 | ||
1770 | h->verinfo.vertree = t; | |
1771 | t->used = TRUE; | |
1772 | d = NULL; | |
1773 | ||
108ba305 JJ |
1774 | if (t->globals.list != NULL) |
1775 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
1776 | |
1777 | /* See if there is anything to force this symbol to | |
1778 | local scope. */ | |
108ba305 | 1779 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 1780 | { |
108ba305 JJ |
1781 | d = (*t->match) (&t->locals, NULL, alc); |
1782 | if (d != NULL | |
1783 | && h->dynindx != -1 | |
108ba305 JJ |
1784 | && ! info->export_dynamic) |
1785 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
1786 | } |
1787 | ||
1788 | free (alc); | |
1789 | break; | |
1790 | } | |
1791 | } | |
1792 | ||
1793 | /* If we are building an application, we need to create a | |
1794 | version node for this version. */ | |
36af4a4e | 1795 | if (t == NULL && info->executable) |
45d6a902 AM |
1796 | { |
1797 | struct bfd_elf_version_tree **pp; | |
1798 | int version_index; | |
1799 | ||
1800 | /* If we aren't going to export this symbol, we don't need | |
1801 | to worry about it. */ | |
1802 | if (h->dynindx == -1) | |
1803 | return TRUE; | |
1804 | ||
1805 | amt = sizeof *t; | |
108ba305 | 1806 | t = bfd_zalloc (sinfo->output_bfd, amt); |
45d6a902 AM |
1807 | if (t == NULL) |
1808 | { | |
1809 | sinfo->failed = TRUE; | |
1810 | return FALSE; | |
1811 | } | |
1812 | ||
45d6a902 | 1813 | t->name = p; |
45d6a902 AM |
1814 | t->name_indx = (unsigned int) -1; |
1815 | t->used = TRUE; | |
1816 | ||
1817 | version_index = 1; | |
1818 | /* Don't count anonymous version tag. */ | |
1819 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1820 | version_index = 0; | |
1821 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1822 | ++version_index; | |
1823 | t->vernum = version_index; | |
1824 | ||
1825 | *pp = t; | |
1826 | ||
1827 | h->verinfo.vertree = t; | |
1828 | } | |
1829 | else if (t == NULL) | |
1830 | { | |
1831 | /* We could not find the version for a symbol when | |
1832 | generating a shared archive. Return an error. */ | |
1833 | (*_bfd_error_handler) | |
d003868e AM |
1834 | (_("%B: undefined versioned symbol name %s"), |
1835 | sinfo->output_bfd, h->root.root.string); | |
45d6a902 AM |
1836 | bfd_set_error (bfd_error_bad_value); |
1837 | sinfo->failed = TRUE; | |
1838 | return FALSE; | |
1839 | } | |
1840 | ||
1841 | if (hidden) | |
f5385ebf | 1842 | h->hidden = 1; |
45d6a902 AM |
1843 | } |
1844 | ||
1845 | /* If we don't have a version for this symbol, see if we can find | |
1846 | something. */ | |
1847 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
1848 | { | |
1849 | struct bfd_elf_version_tree *t; | |
1850 | struct bfd_elf_version_tree *local_ver; | |
1851 | struct bfd_elf_version_expr *d; | |
1852 | ||
1853 | /* See if can find what version this symbol is in. If the | |
1854 | symbol is supposed to be local, then don't actually register | |
1855 | it. */ | |
1856 | local_ver = NULL; | |
1857 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1858 | { | |
108ba305 | 1859 | if (t->globals.list != NULL) |
45d6a902 AM |
1860 | { |
1861 | bfd_boolean matched; | |
1862 | ||
1863 | matched = FALSE; | |
108ba305 JJ |
1864 | d = NULL; |
1865 | while ((d = (*t->match) (&t->globals, d, | |
1866 | h->root.root.string)) != NULL) | |
1867 | if (d->symver) | |
1868 | matched = TRUE; | |
1869 | else | |
1870 | { | |
1871 | /* There is a version without definition. Make | |
1872 | the symbol the default definition for this | |
1873 | version. */ | |
1874 | h->verinfo.vertree = t; | |
1875 | local_ver = NULL; | |
1876 | d->script = 1; | |
1877 | break; | |
1878 | } | |
45d6a902 AM |
1879 | if (d != NULL) |
1880 | break; | |
1881 | else if (matched) | |
1882 | /* There is no undefined version for this symbol. Hide the | |
1883 | default one. */ | |
1884 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1885 | } | |
1886 | ||
108ba305 | 1887 | if (t->locals.list != NULL) |
45d6a902 | 1888 | { |
108ba305 JJ |
1889 | d = NULL; |
1890 | while ((d = (*t->match) (&t->locals, d, | |
1891 | h->root.root.string)) != NULL) | |
45d6a902 | 1892 | { |
108ba305 | 1893 | local_ver = t; |
45d6a902 | 1894 | /* If the match is "*", keep looking for a more |
108ba305 JJ |
1895 | explicit, perhaps even global, match. |
1896 | XXX: Shouldn't this be !d->wildcard instead? */ | |
1897 | if (d->pattern[0] != '*' || d->pattern[1] != '\0') | |
1898 | break; | |
45d6a902 AM |
1899 | } |
1900 | ||
1901 | if (d != NULL) | |
1902 | break; | |
1903 | } | |
1904 | } | |
1905 | ||
1906 | if (local_ver != NULL) | |
1907 | { | |
1908 | h->verinfo.vertree = local_ver; | |
1909 | if (h->dynindx != -1 | |
45d6a902 AM |
1910 | && ! info->export_dynamic) |
1911 | { | |
1912 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1913 | } | |
1914 | } | |
1915 | } | |
1916 | ||
1917 | return TRUE; | |
1918 | } | |
1919 | \f | |
45d6a902 AM |
1920 | /* Read and swap the relocs from the section indicated by SHDR. This |
1921 | may be either a REL or a RELA section. The relocations are | |
1922 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
1923 | which should have already been allocated to contain enough space. | |
1924 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
1925 | relocations should be stored. | |
1926 | ||
1927 | Returns FALSE if something goes wrong. */ | |
1928 | ||
1929 | static bfd_boolean | |
268b6b39 | 1930 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 1931 | asection *sec, |
268b6b39 AM |
1932 | Elf_Internal_Shdr *shdr, |
1933 | void *external_relocs, | |
1934 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 1935 | { |
9c5bfbb7 | 1936 | const struct elf_backend_data *bed; |
268b6b39 | 1937 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
1938 | const bfd_byte *erela; |
1939 | const bfd_byte *erelaend; | |
1940 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
1941 | Elf_Internal_Shdr *symtab_hdr; |
1942 | size_t nsyms; | |
45d6a902 | 1943 | |
45d6a902 AM |
1944 | /* Position ourselves at the start of the section. */ |
1945 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
1946 | return FALSE; | |
1947 | ||
1948 | /* Read the relocations. */ | |
1949 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
1950 | return FALSE; | |
1951 | ||
243ef1e0 L |
1952 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1953 | nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
1954 | ||
45d6a902 AM |
1955 | bed = get_elf_backend_data (abfd); |
1956 | ||
1957 | /* Convert the external relocations to the internal format. */ | |
1958 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
1959 | swap_in = bed->s->swap_reloc_in; | |
1960 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
1961 | swap_in = bed->s->swap_reloca_in; | |
1962 | else | |
1963 | { | |
1964 | bfd_set_error (bfd_error_wrong_format); | |
1965 | return FALSE; | |
1966 | } | |
1967 | ||
1968 | erela = external_relocs; | |
51992aec | 1969 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
1970 | irela = internal_relocs; |
1971 | while (erela < erelaend) | |
1972 | { | |
243ef1e0 L |
1973 | bfd_vma r_symndx; |
1974 | ||
45d6a902 | 1975 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
1976 | r_symndx = ELF32_R_SYM (irela->r_info); |
1977 | if (bed->s->arch_size == 64) | |
1978 | r_symndx >>= 24; | |
1979 | if ((size_t) r_symndx >= nsyms) | |
1980 | { | |
1981 | (*_bfd_error_handler) | |
d003868e AM |
1982 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" |
1983 | " for offset 0x%lx in section `%A'"), | |
1984 | abfd, sec, | |
1985 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
1986 | bfd_set_error (bfd_error_bad_value); |
1987 | return FALSE; | |
1988 | } | |
45d6a902 AM |
1989 | irela += bed->s->int_rels_per_ext_rel; |
1990 | erela += shdr->sh_entsize; | |
1991 | } | |
1992 | ||
1993 | return TRUE; | |
1994 | } | |
1995 | ||
1996 | /* Read and swap the relocs for a section O. They may have been | |
1997 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
1998 | not NULL, they are used as buffers to read into. They are known to | |
1999 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2000 | the return value is allocated using either malloc or bfd_alloc, | |
2001 | according to the KEEP_MEMORY argument. If O has two relocation | |
2002 | sections (both REL and RELA relocations), then the REL_HDR | |
2003 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
2004 | REL_HDR2 relocations. */ | |
2005 | ||
2006 | Elf_Internal_Rela * | |
268b6b39 AM |
2007 | _bfd_elf_link_read_relocs (bfd *abfd, |
2008 | asection *o, | |
2009 | void *external_relocs, | |
2010 | Elf_Internal_Rela *internal_relocs, | |
2011 | bfd_boolean keep_memory) | |
45d6a902 AM |
2012 | { |
2013 | Elf_Internal_Shdr *rel_hdr; | |
268b6b39 | 2014 | void *alloc1 = NULL; |
45d6a902 | 2015 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2016 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
2017 | |
2018 | if (elf_section_data (o)->relocs != NULL) | |
2019 | return elf_section_data (o)->relocs; | |
2020 | ||
2021 | if (o->reloc_count == 0) | |
2022 | return NULL; | |
2023 | ||
2024 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
2025 | ||
2026 | if (internal_relocs == NULL) | |
2027 | { | |
2028 | bfd_size_type size; | |
2029 | ||
2030 | size = o->reloc_count; | |
2031 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
2032 | if (keep_memory) | |
268b6b39 | 2033 | internal_relocs = bfd_alloc (abfd, size); |
45d6a902 | 2034 | else |
268b6b39 | 2035 | internal_relocs = alloc2 = bfd_malloc (size); |
45d6a902 AM |
2036 | if (internal_relocs == NULL) |
2037 | goto error_return; | |
2038 | } | |
2039 | ||
2040 | if (external_relocs == NULL) | |
2041 | { | |
2042 | bfd_size_type size = rel_hdr->sh_size; | |
2043 | ||
2044 | if (elf_section_data (o)->rel_hdr2) | |
2045 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
268b6b39 | 2046 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2047 | if (alloc1 == NULL) |
2048 | goto error_return; | |
2049 | external_relocs = alloc1; | |
2050 | } | |
2051 | ||
243ef1e0 | 2052 | if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, |
45d6a902 AM |
2053 | external_relocs, |
2054 | internal_relocs)) | |
2055 | goto error_return; | |
51992aec AM |
2056 | if (elf_section_data (o)->rel_hdr2 |
2057 | && (!elf_link_read_relocs_from_section | |
2058 | (abfd, o, | |
2059 | elf_section_data (o)->rel_hdr2, | |
2060 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
2061 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
2062 | * bed->s->int_rels_per_ext_rel)))) | |
45d6a902 AM |
2063 | goto error_return; |
2064 | ||
2065 | /* Cache the results for next time, if we can. */ | |
2066 | if (keep_memory) | |
2067 | elf_section_data (o)->relocs = internal_relocs; | |
2068 | ||
2069 | if (alloc1 != NULL) | |
2070 | free (alloc1); | |
2071 | ||
2072 | /* Don't free alloc2, since if it was allocated we are passing it | |
2073 | back (under the name of internal_relocs). */ | |
2074 | ||
2075 | return internal_relocs; | |
2076 | ||
2077 | error_return: | |
2078 | if (alloc1 != NULL) | |
2079 | free (alloc1); | |
2080 | if (alloc2 != NULL) | |
2081 | free (alloc2); | |
2082 | return NULL; | |
2083 | } | |
2084 | ||
2085 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2086 | section header for a section containing relocations for O. */ | |
2087 | ||
2088 | bfd_boolean | |
268b6b39 AM |
2089 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2090 | Elf_Internal_Shdr *rel_hdr, | |
2091 | asection *o) | |
45d6a902 AM |
2092 | { |
2093 | bfd_size_type reloc_count; | |
2094 | bfd_size_type num_rel_hashes; | |
2095 | ||
2096 | /* Figure out how many relocations there will be. */ | |
2097 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
2098 | reloc_count = elf_section_data (o)->rel_count; | |
2099 | else | |
2100 | reloc_count = elf_section_data (o)->rel_count2; | |
2101 | ||
2102 | num_rel_hashes = o->reloc_count; | |
2103 | if (num_rel_hashes < reloc_count) | |
2104 | num_rel_hashes = reloc_count; | |
2105 | ||
2106 | /* That allows us to calculate the size of the section. */ | |
2107 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
2108 | ||
2109 | /* The contents field must last into write_object_contents, so we | |
2110 | allocate it with bfd_alloc rather than malloc. Also since we | |
2111 | cannot be sure that the contents will actually be filled in, | |
2112 | we zero the allocated space. */ | |
268b6b39 | 2113 | rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2114 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2115 | return FALSE; | |
2116 | ||
2117 | /* We only allocate one set of hash entries, so we only do it the | |
2118 | first time we are called. */ | |
2119 | if (elf_section_data (o)->rel_hashes == NULL | |
2120 | && num_rel_hashes) | |
2121 | { | |
2122 | struct elf_link_hash_entry **p; | |
2123 | ||
268b6b39 | 2124 | p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
2125 | if (p == NULL) |
2126 | return FALSE; | |
2127 | ||
2128 | elf_section_data (o)->rel_hashes = p; | |
2129 | } | |
2130 | ||
2131 | return TRUE; | |
2132 | } | |
2133 | ||
2134 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2135 | originated from the section given by INPUT_REL_HDR) to the | |
2136 | OUTPUT_BFD. */ | |
2137 | ||
2138 | bfd_boolean | |
268b6b39 AM |
2139 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2140 | asection *input_section, | |
2141 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2142 | Elf_Internal_Rela *internal_relocs, |
2143 | struct elf_link_hash_entry **rel_hash | |
2144 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2145 | { |
2146 | Elf_Internal_Rela *irela; | |
2147 | Elf_Internal_Rela *irelaend; | |
2148 | bfd_byte *erel; | |
2149 | Elf_Internal_Shdr *output_rel_hdr; | |
2150 | asection *output_section; | |
2151 | unsigned int *rel_countp = NULL; | |
9c5bfbb7 | 2152 | const struct elf_backend_data *bed; |
268b6b39 | 2153 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
45d6a902 AM |
2154 | |
2155 | output_section = input_section->output_section; | |
2156 | output_rel_hdr = NULL; | |
2157 | ||
2158 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
2159 | == input_rel_hdr->sh_entsize) | |
2160 | { | |
2161 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
2162 | rel_countp = &elf_section_data (output_section)->rel_count; | |
2163 | } | |
2164 | else if (elf_section_data (output_section)->rel_hdr2 | |
2165 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
2166 | == input_rel_hdr->sh_entsize)) | |
2167 | { | |
2168 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2169 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2170 | } | |
2171 | else | |
2172 | { | |
2173 | (*_bfd_error_handler) | |
d003868e AM |
2174 | (_("%B: relocation size mismatch in %B section %A"), |
2175 | output_bfd, input_section->owner, input_section); | |
45d6a902 AM |
2176 | bfd_set_error (bfd_error_wrong_object_format); |
2177 | return FALSE; | |
2178 | } | |
2179 | ||
2180 | bed = get_elf_backend_data (output_bfd); | |
2181 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2182 | swap_out = bed->s->swap_reloc_out; | |
2183 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2184 | swap_out = bed->s->swap_reloca_out; | |
2185 | else | |
2186 | abort (); | |
2187 | ||
2188 | erel = output_rel_hdr->contents; | |
2189 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2190 | irela = internal_relocs; | |
2191 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2192 | * bed->s->int_rels_per_ext_rel); | |
2193 | while (irela < irelaend) | |
2194 | { | |
2195 | (*swap_out) (output_bfd, irela, erel); | |
2196 | irela += bed->s->int_rels_per_ext_rel; | |
2197 | erel += input_rel_hdr->sh_entsize; | |
2198 | } | |
2199 | ||
2200 | /* Bump the counter, so that we know where to add the next set of | |
2201 | relocations. */ | |
2202 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2203 | ||
2204 | return TRUE; | |
2205 | } | |
2206 | \f | |
2207 | /* Fix up the flags for a symbol. This handles various cases which | |
2208 | can only be fixed after all the input files are seen. This is | |
2209 | currently called by both adjust_dynamic_symbol and | |
2210 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2211 | the face of future changes. */ | |
2212 | ||
2213 | bfd_boolean | |
268b6b39 AM |
2214 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2215 | struct elf_info_failed *eif) | |
45d6a902 AM |
2216 | { |
2217 | /* If this symbol was mentioned in a non-ELF file, try to set | |
2218 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2219 | permit a non-ELF file to correctly refer to a symbol defined in | |
2220 | an ELF dynamic object. */ | |
f5385ebf | 2221 | if (h->non_elf) |
45d6a902 AM |
2222 | { |
2223 | while (h->root.type == bfd_link_hash_indirect) | |
2224 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2225 | ||
2226 | if (h->root.type != bfd_link_hash_defined | |
2227 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2228 | { |
2229 | h->ref_regular = 1; | |
2230 | h->ref_regular_nonweak = 1; | |
2231 | } | |
45d6a902 AM |
2232 | else |
2233 | { | |
2234 | if (h->root.u.def.section->owner != NULL | |
2235 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2236 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2237 | { |
2238 | h->ref_regular = 1; | |
2239 | h->ref_regular_nonweak = 1; | |
2240 | } | |
45d6a902 | 2241 | else |
f5385ebf | 2242 | h->def_regular = 1; |
45d6a902 AM |
2243 | } |
2244 | ||
2245 | if (h->dynindx == -1 | |
f5385ebf AM |
2246 | && (h->def_dynamic |
2247 | || h->ref_dynamic)) | |
45d6a902 | 2248 | { |
c152c796 | 2249 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2250 | { |
2251 | eif->failed = TRUE; | |
2252 | return FALSE; | |
2253 | } | |
2254 | } | |
2255 | } | |
2256 | else | |
2257 | { | |
f5385ebf | 2258 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2259 | was first seen in a non-ELF file. Fortunately, if the symbol |
2260 | was first seen in an ELF file, we're probably OK unless the | |
2261 | symbol was defined in a non-ELF file. Catch that case here. | |
2262 | FIXME: We're still in trouble if the symbol was first seen in | |
2263 | a dynamic object, and then later in a non-ELF regular object. */ | |
2264 | if ((h->root.type == bfd_link_hash_defined | |
2265 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2266 | && !h->def_regular |
45d6a902 AM |
2267 | && (h->root.u.def.section->owner != NULL |
2268 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2269 | != bfd_target_elf_flavour) | |
2270 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2271 | && !h->def_dynamic))) |
2272 | h->def_regular = 1; | |
45d6a902 AM |
2273 | } |
2274 | ||
2275 | /* If this is a final link, and the symbol was defined as a common | |
2276 | symbol in a regular object file, and there was no definition in | |
2277 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2278 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2279 | flag will not have been set. */ |
2280 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2281 | && !h->def_regular |
2282 | && h->ref_regular | |
2283 | && !h->def_dynamic | |
45d6a902 | 2284 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) |
f5385ebf | 2285 | h->def_regular = 1; |
45d6a902 AM |
2286 | |
2287 | /* If -Bsymbolic was used (which means to bind references to global | |
2288 | symbols to the definition within the shared object), and this | |
2289 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2290 | need a PLT entry. Likewise, if the symbol has non-default |
2291 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2292 | will force it local. */ |
f5385ebf | 2293 | if (h->needs_plt |
45d6a902 | 2294 | && eif->info->shared |
0eddce27 | 2295 | && is_elf_hash_table (eif->info->hash) |
45d6a902 | 2296 | && (eif->info->symbolic |
c1be741f | 2297 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2298 | && h->def_regular) |
45d6a902 | 2299 | { |
9c5bfbb7 | 2300 | const struct elf_backend_data *bed; |
45d6a902 AM |
2301 | bfd_boolean force_local; |
2302 | ||
2303 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2304 | ||
2305 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL | |
2306 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2307 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2308 | } | |
2309 | ||
2310 | /* If a weak undefined symbol has non-default visibility, we also | |
2311 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2312 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
2313 | && h->root.type == bfd_link_hash_undefweak) |
2314 | { | |
9c5bfbb7 | 2315 | const struct elf_backend_data *bed; |
45d6a902 AM |
2316 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2317 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2318 | } | |
2319 | ||
2320 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2321 | the real definition in the dynamic object, copy interesting flags | |
2322 | over to the real definition. */ | |
f6e332e6 | 2323 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2324 | { |
2325 | struct elf_link_hash_entry *weakdef; | |
2326 | ||
f6e332e6 | 2327 | weakdef = h->u.weakdef; |
45d6a902 AM |
2328 | if (h->root.type == bfd_link_hash_indirect) |
2329 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2330 | ||
2331 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2332 | || h->root.type == bfd_link_hash_defweak); | |
2333 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2334 | || weakdef->root.type == bfd_link_hash_defweak); | |
f5385ebf | 2335 | BFD_ASSERT (weakdef->def_dynamic); |
45d6a902 AM |
2336 | |
2337 | /* If the real definition is defined by a regular object file, | |
2338 | don't do anything special. See the longer description in | |
2339 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
f5385ebf | 2340 | if (weakdef->def_regular) |
f6e332e6 | 2341 | h->u.weakdef = NULL; |
45d6a902 AM |
2342 | else |
2343 | { | |
9c5bfbb7 | 2344 | const struct elf_backend_data *bed; |
45d6a902 AM |
2345 | |
2346 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2347 | (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h); | |
2348 | } | |
2349 | } | |
2350 | ||
2351 | return TRUE; | |
2352 | } | |
2353 | ||
2354 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2355 | called via elf_link_hash_traverse, and also calls itself | |
2356 | recursively. */ | |
2357 | ||
2358 | bfd_boolean | |
268b6b39 | 2359 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2360 | { |
268b6b39 | 2361 | struct elf_info_failed *eif = data; |
45d6a902 | 2362 | bfd *dynobj; |
9c5bfbb7 | 2363 | const struct elf_backend_data *bed; |
45d6a902 | 2364 | |
0eddce27 | 2365 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2366 | return FALSE; |
2367 | ||
2368 | if (h->root.type == bfd_link_hash_warning) | |
2369 | { | |
a6aa5195 AM |
2370 | h->got = elf_hash_table (eif->info)->init_got_offset; |
2371 | h->plt = elf_hash_table (eif->info)->init_plt_offset; | |
45d6a902 AM |
2372 | |
2373 | /* When warning symbols are created, they **replace** the "real" | |
2374 | entry in the hash table, thus we never get to see the real | |
2375 | symbol in a hash traversal. So look at it now. */ | |
2376 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2377 | } | |
2378 | ||
2379 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2380 | if (h->root.type == bfd_link_hash_indirect) | |
2381 | return TRUE; | |
2382 | ||
2383 | /* Fix the symbol flags. */ | |
2384 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2385 | return FALSE; | |
2386 | ||
2387 | /* If this symbol does not require a PLT entry, and it is not | |
2388 | defined by a dynamic object, or is not referenced by a regular | |
2389 | object, ignore it. We do have to handle a weak defined symbol, | |
2390 | even if no regular object refers to it, if we decided to add it | |
2391 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2392 | about symbols which are defined by one dynamic object and | |
2393 | referenced by another one? */ | |
f5385ebf AM |
2394 | if (!h->needs_plt |
2395 | && (h->def_regular | |
2396 | || !h->def_dynamic | |
2397 | || (!h->ref_regular | |
f6e332e6 | 2398 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 | 2399 | { |
a6aa5195 | 2400 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2401 | return TRUE; |
2402 | } | |
2403 | ||
2404 | /* If we've already adjusted this symbol, don't do it again. This | |
2405 | can happen via a recursive call. */ | |
f5385ebf | 2406 | if (h->dynamic_adjusted) |
45d6a902 AM |
2407 | return TRUE; |
2408 | ||
2409 | /* Don't look at this symbol again. Note that we must set this | |
2410 | after checking the above conditions, because we may look at a | |
2411 | symbol once, decide not to do anything, and then get called | |
2412 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2413 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2414 | |
2415 | /* If this is a weak definition, and we know a real definition, and | |
2416 | the real symbol is not itself defined by a regular object file, | |
2417 | then get a good value for the real definition. We handle the | |
2418 | real symbol first, for the convenience of the backend routine. | |
2419 | ||
2420 | Note that there is a confusing case here. If the real definition | |
2421 | is defined by a regular object file, we don't get the real symbol | |
2422 | from the dynamic object, but we do get the weak symbol. If the | |
2423 | processor backend uses a COPY reloc, then if some routine in the | |
2424 | dynamic object changes the real symbol, we will not see that | |
2425 | change in the corresponding weak symbol. This is the way other | |
2426 | ELF linkers work as well, and seems to be a result of the shared | |
2427 | library model. | |
2428 | ||
2429 | I will clarify this issue. Most SVR4 shared libraries define the | |
2430 | variable _timezone and define timezone as a weak synonym. The | |
2431 | tzset call changes _timezone. If you write | |
2432 | extern int timezone; | |
2433 | int _timezone = 5; | |
2434 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2435 | you might expect that, since timezone is a synonym for _timezone, | |
2436 | the same number will print both times. However, if the processor | |
2437 | backend uses a COPY reloc, then actually timezone will be copied | |
2438 | into your process image, and, since you define _timezone | |
2439 | yourself, _timezone will not. Thus timezone and _timezone will | |
2440 | wind up at different memory locations. The tzset call will set | |
2441 | _timezone, leaving timezone unchanged. */ | |
2442 | ||
f6e332e6 | 2443 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2444 | { |
2445 | /* If we get to this point, we know there is an implicit | |
2446 | reference by a regular object file via the weak symbol H. | |
2447 | FIXME: Is this really true? What if the traversal finds | |
f6e332e6 AM |
2448 | H->U.WEAKDEF before it finds H? */ |
2449 | h->u.weakdef->ref_regular = 1; | |
45d6a902 | 2450 | |
f6e332e6 | 2451 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2452 | return FALSE; |
2453 | } | |
2454 | ||
2455 | /* If a symbol has no type and no size and does not require a PLT | |
2456 | entry, then we are probably about to do the wrong thing here: we | |
2457 | are probably going to create a COPY reloc for an empty object. | |
2458 | This case can arise when a shared object is built with assembly | |
2459 | code, and the assembly code fails to set the symbol type. */ | |
2460 | if (h->size == 0 | |
2461 | && h->type == STT_NOTYPE | |
f5385ebf | 2462 | && !h->needs_plt) |
45d6a902 AM |
2463 | (*_bfd_error_handler) |
2464 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2465 | h->root.root.string); | |
2466 | ||
2467 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2468 | bed = get_elf_backend_data (dynobj); | |
2469 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2470 | { | |
2471 | eif->failed = TRUE; | |
2472 | return FALSE; | |
2473 | } | |
2474 | ||
2475 | return TRUE; | |
2476 | } | |
2477 | ||
2478 | /* Adjust all external symbols pointing into SEC_MERGE sections | |
2479 | to reflect the object merging within the sections. */ | |
2480 | ||
2481 | bfd_boolean | |
268b6b39 | 2482 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2483 | { |
2484 | asection *sec; | |
2485 | ||
2486 | if (h->root.type == bfd_link_hash_warning) | |
2487 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2488 | ||
2489 | if ((h->root.type == bfd_link_hash_defined | |
2490 | || h->root.type == bfd_link_hash_defweak) | |
2491 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2492 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2493 | { | |
268b6b39 | 2494 | bfd *output_bfd = data; |
45d6a902 AM |
2495 | |
2496 | h->root.u.def.value = | |
2497 | _bfd_merged_section_offset (output_bfd, | |
2498 | &h->root.u.def.section, | |
2499 | elf_section_data (sec)->sec_info, | |
753731ee | 2500 | h->root.u.def.value); |
45d6a902 AM |
2501 | } |
2502 | ||
2503 | return TRUE; | |
2504 | } | |
986a241f RH |
2505 | |
2506 | /* Returns false if the symbol referred to by H should be considered | |
2507 | to resolve local to the current module, and true if it should be | |
2508 | considered to bind dynamically. */ | |
2509 | ||
2510 | bfd_boolean | |
268b6b39 AM |
2511 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2512 | struct bfd_link_info *info, | |
2513 | bfd_boolean ignore_protected) | |
986a241f RH |
2514 | { |
2515 | bfd_boolean binding_stays_local_p; | |
2516 | ||
2517 | if (h == NULL) | |
2518 | return FALSE; | |
2519 | ||
2520 | while (h->root.type == bfd_link_hash_indirect | |
2521 | || h->root.type == bfd_link_hash_warning) | |
2522 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2523 | ||
2524 | /* If it was forced local, then clearly it's not dynamic. */ | |
2525 | if (h->dynindx == -1) | |
2526 | return FALSE; | |
f5385ebf | 2527 | if (h->forced_local) |
986a241f RH |
2528 | return FALSE; |
2529 | ||
2530 | /* Identify the cases where name binding rules say that a | |
2531 | visible symbol resolves locally. */ | |
2532 | binding_stays_local_p = info->executable || info->symbolic; | |
2533 | ||
2534 | switch (ELF_ST_VISIBILITY (h->other)) | |
2535 | { | |
2536 | case STV_INTERNAL: | |
2537 | case STV_HIDDEN: | |
2538 | return FALSE; | |
2539 | ||
2540 | case STV_PROTECTED: | |
2541 | /* Proper resolution for function pointer equality may require | |
2542 | that these symbols perhaps be resolved dynamically, even though | |
2543 | we should be resolving them to the current module. */ | |
1c16dfa5 | 2544 | if (!ignore_protected || h->type != STT_FUNC) |
986a241f RH |
2545 | binding_stays_local_p = TRUE; |
2546 | break; | |
2547 | ||
2548 | default: | |
986a241f RH |
2549 | break; |
2550 | } | |
2551 | ||
aa37626c | 2552 | /* If it isn't defined locally, then clearly it's dynamic. */ |
f5385ebf | 2553 | if (!h->def_regular) |
aa37626c L |
2554 | return TRUE; |
2555 | ||
986a241f RH |
2556 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2557 | us that it remains local. */ | |
2558 | return !binding_stays_local_p; | |
2559 | } | |
f6c52c13 AM |
2560 | |
2561 | /* Return true if the symbol referred to by H should be considered | |
2562 | to resolve local to the current module, and false otherwise. Differs | |
2563 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2564 | undefined symbols and weak symbols. */ | |
2565 | ||
2566 | bfd_boolean | |
268b6b39 AM |
2567 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2568 | struct bfd_link_info *info, | |
2569 | bfd_boolean local_protected) | |
f6c52c13 AM |
2570 | { |
2571 | /* If it's a local sym, of course we resolve locally. */ | |
2572 | if (h == NULL) | |
2573 | return TRUE; | |
2574 | ||
7e2294f9 AO |
2575 | /* Common symbols that become definitions don't get the DEF_REGULAR |
2576 | flag set, so test it first, and don't bail out. */ | |
2577 | if (ELF_COMMON_DEF_P (h)) | |
2578 | /* Do nothing. */; | |
f6c52c13 | 2579 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
2580 | resolve locally. The sym is either undefined or dynamic. */ |
2581 | else if (!h->def_regular) | |
f6c52c13 AM |
2582 | return FALSE; |
2583 | ||
2584 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 2585 | if (h->forced_local) |
f6c52c13 AM |
2586 | return TRUE; |
2587 | ||
2588 | /* As do non-dynamic symbols. */ | |
2589 | if (h->dynindx == -1) | |
2590 | return TRUE; | |
2591 | ||
2592 | /* At this point, we know the symbol is defined and dynamic. In an | |
2593 | executable it must resolve locally, likewise when building symbolic | |
2594 | shared libraries. */ | |
2595 | if (info->executable || info->symbolic) | |
2596 | return TRUE; | |
2597 | ||
2598 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2599 | with default visibility might not resolve locally. */ | |
2600 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2601 | return FALSE; | |
2602 | ||
2603 | /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */ | |
2604 | if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED) | |
2605 | return TRUE; | |
2606 | ||
1c16dfa5 L |
2607 | /* STV_PROTECTED non-function symbols are local. */ |
2608 | if (h->type != STT_FUNC) | |
2609 | return TRUE; | |
2610 | ||
f6c52c13 AM |
2611 | /* Function pointer equality tests may require that STV_PROTECTED |
2612 | symbols be treated as dynamic symbols, even when we know that the | |
2613 | dynamic linker will resolve them locally. */ | |
2614 | return local_protected; | |
2615 | } | |
e1918d23 AM |
2616 | |
2617 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2618 | aligned. Returns the first TLS output section. */ | |
2619 | ||
2620 | struct bfd_section * | |
2621 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2622 | { | |
2623 | struct bfd_section *sec, *tls; | |
2624 | unsigned int align = 0; | |
2625 | ||
2626 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2627 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2628 | break; | |
2629 | tls = sec; | |
2630 | ||
2631 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2632 | if (sec->alignment_power > align) | |
2633 | align = sec->alignment_power; | |
2634 | ||
2635 | elf_hash_table (info)->tls_sec = tls; | |
2636 | ||
2637 | /* Ensure the alignment of the first section is the largest alignment, | |
2638 | so that the tls segment starts aligned. */ | |
2639 | if (tls != NULL) | |
2640 | tls->alignment_power = align; | |
2641 | ||
2642 | return tls; | |
2643 | } | |
0ad989f9 L |
2644 | |
2645 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2646 | static bfd_boolean | |
2647 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2648 | Elf_Internal_Sym *sym) | |
2649 | { | |
a4d8e49b L |
2650 | const struct elf_backend_data *bed; |
2651 | ||
0ad989f9 L |
2652 | /* Local symbols do not count, but target specific ones might. */ |
2653 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2654 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2655 | return FALSE; | |
2656 | ||
2657 | /* Function symbols do not count. */ | |
2658 | if (ELF_ST_TYPE (sym->st_info) == STT_FUNC) | |
2659 | return FALSE; | |
2660 | ||
2661 | /* If the section is undefined, then so is the symbol. */ | |
2662 | if (sym->st_shndx == SHN_UNDEF) | |
2663 | return FALSE; | |
2664 | ||
2665 | /* If the symbol is defined in the common section, then | |
2666 | it is a common definition and so does not count. */ | |
a4d8e49b L |
2667 | bed = get_elf_backend_data (abfd); |
2668 | if (bed->common_definition (sym)) | |
0ad989f9 L |
2669 | return FALSE; |
2670 | ||
2671 | /* If the symbol is in a target specific section then we | |
2672 | must rely upon the backend to tell us what it is. */ | |
2673 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2674 | /* FIXME - this function is not coded yet: | |
2675 | ||
2676 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2677 | ||
2678 | Instead for now assume that the definition is not global, | |
2679 | Even if this is wrong, at least the linker will behave | |
2680 | in the same way that it used to do. */ | |
2681 | return FALSE; | |
2682 | ||
2683 | return TRUE; | |
2684 | } | |
2685 | ||
2686 | /* Search the symbol table of the archive element of the archive ABFD | |
2687 | whose archive map contains a mention of SYMDEF, and determine if | |
2688 | the symbol is defined in this element. */ | |
2689 | static bfd_boolean | |
2690 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2691 | { | |
2692 | Elf_Internal_Shdr * hdr; | |
2693 | bfd_size_type symcount; | |
2694 | bfd_size_type extsymcount; | |
2695 | bfd_size_type extsymoff; | |
2696 | Elf_Internal_Sym *isymbuf; | |
2697 | Elf_Internal_Sym *isym; | |
2698 | Elf_Internal_Sym *isymend; | |
2699 | bfd_boolean result; | |
2700 | ||
2701 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2702 | if (abfd == NULL) | |
2703 | return FALSE; | |
2704 | ||
2705 | if (! bfd_check_format (abfd, bfd_object)) | |
2706 | return FALSE; | |
2707 | ||
2708 | /* If we have already included the element containing this symbol in the | |
2709 | link then we do not need to include it again. Just claim that any symbol | |
2710 | it contains is not a definition, so that our caller will not decide to | |
2711 | (re)include this element. */ | |
2712 | if (abfd->archive_pass) | |
2713 | return FALSE; | |
2714 | ||
2715 | /* Select the appropriate symbol table. */ | |
2716 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2717 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2718 | else | |
2719 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2720 | ||
2721 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2722 | ||
2723 | /* The sh_info field of the symtab header tells us where the | |
2724 | external symbols start. We don't care about the local symbols. */ | |
2725 | if (elf_bad_symtab (abfd)) | |
2726 | { | |
2727 | extsymcount = symcount; | |
2728 | extsymoff = 0; | |
2729 | } | |
2730 | else | |
2731 | { | |
2732 | extsymcount = symcount - hdr->sh_info; | |
2733 | extsymoff = hdr->sh_info; | |
2734 | } | |
2735 | ||
2736 | if (extsymcount == 0) | |
2737 | return FALSE; | |
2738 | ||
2739 | /* Read in the symbol table. */ | |
2740 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2741 | NULL, NULL, NULL); | |
2742 | if (isymbuf == NULL) | |
2743 | return FALSE; | |
2744 | ||
2745 | /* Scan the symbol table looking for SYMDEF. */ | |
2746 | result = FALSE; | |
2747 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2748 | { | |
2749 | const char *name; | |
2750 | ||
2751 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
2752 | isym->st_name); | |
2753 | if (name == NULL) | |
2754 | break; | |
2755 | ||
2756 | if (strcmp (name, symdef->name) == 0) | |
2757 | { | |
2758 | result = is_global_data_symbol_definition (abfd, isym); | |
2759 | break; | |
2760 | } | |
2761 | } | |
2762 | ||
2763 | free (isymbuf); | |
2764 | ||
2765 | return result; | |
2766 | } | |
2767 | \f | |
5a580b3a AM |
2768 | /* Add an entry to the .dynamic table. */ |
2769 | ||
2770 | bfd_boolean | |
2771 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
2772 | bfd_vma tag, | |
2773 | bfd_vma val) | |
2774 | { | |
2775 | struct elf_link_hash_table *hash_table; | |
2776 | const struct elf_backend_data *bed; | |
2777 | asection *s; | |
2778 | bfd_size_type newsize; | |
2779 | bfd_byte *newcontents; | |
2780 | Elf_Internal_Dyn dyn; | |
2781 | ||
2782 | hash_table = elf_hash_table (info); | |
2783 | if (! is_elf_hash_table (hash_table)) | |
2784 | return FALSE; | |
2785 | ||
8fdd7217 NC |
2786 | if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL) |
2787 | _bfd_error_handler | |
2788 | (_("warning: creating a DT_TEXTREL in a shared object.")); | |
2789 | ||
5a580b3a AM |
2790 | bed = get_elf_backend_data (hash_table->dynobj); |
2791 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2792 | BFD_ASSERT (s != NULL); | |
2793 | ||
eea6121a | 2794 | newsize = s->size + bed->s->sizeof_dyn; |
5a580b3a AM |
2795 | newcontents = bfd_realloc (s->contents, newsize); |
2796 | if (newcontents == NULL) | |
2797 | return FALSE; | |
2798 | ||
2799 | dyn.d_tag = tag; | |
2800 | dyn.d_un.d_val = val; | |
eea6121a | 2801 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 2802 | |
eea6121a | 2803 | s->size = newsize; |
5a580b3a AM |
2804 | s->contents = newcontents; |
2805 | ||
2806 | return TRUE; | |
2807 | } | |
2808 | ||
2809 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
2810 | otherwise just check whether one already exists. Returns -1 on error, | |
2811 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
2812 | ||
4ad4eba5 | 2813 | static int |
7e9f0867 AM |
2814 | elf_add_dt_needed_tag (bfd *abfd, |
2815 | struct bfd_link_info *info, | |
4ad4eba5 AM |
2816 | const char *soname, |
2817 | bfd_boolean do_it) | |
5a580b3a AM |
2818 | { |
2819 | struct elf_link_hash_table *hash_table; | |
2820 | bfd_size_type oldsize; | |
2821 | bfd_size_type strindex; | |
2822 | ||
7e9f0867 AM |
2823 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
2824 | return -1; | |
2825 | ||
5a580b3a AM |
2826 | hash_table = elf_hash_table (info); |
2827 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
2828 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
2829 | if (strindex == (bfd_size_type) -1) | |
2830 | return -1; | |
2831 | ||
2832 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
2833 | { | |
2834 | asection *sdyn; | |
2835 | const struct elf_backend_data *bed; | |
2836 | bfd_byte *extdyn; | |
2837 | ||
2838 | bed = get_elf_backend_data (hash_table->dynobj); | |
2839 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
7e9f0867 AM |
2840 | if (sdyn != NULL) |
2841 | for (extdyn = sdyn->contents; | |
2842 | extdyn < sdyn->contents + sdyn->size; | |
2843 | extdyn += bed->s->sizeof_dyn) | |
2844 | { | |
2845 | Elf_Internal_Dyn dyn; | |
5a580b3a | 2846 | |
7e9f0867 AM |
2847 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
2848 | if (dyn.d_tag == DT_NEEDED | |
2849 | && dyn.d_un.d_val == strindex) | |
2850 | { | |
2851 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2852 | return 1; | |
2853 | } | |
2854 | } | |
5a580b3a AM |
2855 | } |
2856 | ||
2857 | if (do_it) | |
2858 | { | |
7e9f0867 AM |
2859 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
2860 | return -1; | |
2861 | ||
5a580b3a AM |
2862 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
2863 | return -1; | |
2864 | } | |
2865 | else | |
2866 | /* We were just checking for existence of the tag. */ | |
2867 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2868 | ||
2869 | return 0; | |
2870 | } | |
2871 | ||
77cfaee6 AM |
2872 | /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols |
2873 | belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no | |
ec13b3bb AM |
2874 | references from regular objects to these symbols. |
2875 | ||
2876 | ??? Should we do something about references from other dynamic | |
2877 | obects? If not, we potentially lose some warnings about undefined | |
2878 | symbols. But how can we recover the initial undefined / undefweak | |
2879 | state? */ | |
77cfaee6 AM |
2880 | |
2881 | struct elf_smash_syms_data | |
2882 | { | |
2883 | bfd *not_needed; | |
2884 | struct elf_link_hash_table *htab; | |
2885 | bfd_boolean twiddled; | |
2886 | }; | |
2887 | ||
2888 | static bfd_boolean | |
2889 | elf_smash_syms (struct elf_link_hash_entry *h, void *data) | |
2890 | { | |
2891 | struct elf_smash_syms_data *inf = (struct elf_smash_syms_data *) data; | |
2892 | struct bfd_link_hash_entry *bh; | |
2893 | ||
2894 | switch (h->root.type) | |
2895 | { | |
2896 | default: | |
2897 | case bfd_link_hash_new: | |
2898 | return TRUE; | |
2899 | ||
2900 | case bfd_link_hash_undefined: | |
11f25ea6 AM |
2901 | if (h->root.u.undef.abfd != inf->not_needed) |
2902 | return TRUE; | |
4ea42fb7 AM |
2903 | if (h->root.u.undef.weak != NULL |
2904 | && h->root.u.undef.weak != inf->not_needed) | |
11f25ea6 AM |
2905 | { |
2906 | /* Symbol was undefweak in u.undef.weak bfd, and has become | |
2907 | undefined in as-needed lib. Restore weak. */ | |
2908 | h->root.type = bfd_link_hash_undefweak; | |
2909 | h->root.u.undef.abfd = h->root.u.undef.weak; | |
2910 | if (h->root.u.undef.next != NULL | |
2911 | || inf->htab->root.undefs_tail == &h->root) | |
2912 | inf->twiddled = TRUE; | |
2913 | return TRUE; | |
2914 | } | |
2915 | break; | |
2916 | ||
77cfaee6 AM |
2917 | case bfd_link_hash_undefweak: |
2918 | if (h->root.u.undef.abfd != inf->not_needed) | |
2919 | return TRUE; | |
2920 | break; | |
2921 | ||
2922 | case bfd_link_hash_defined: | |
2923 | case bfd_link_hash_defweak: | |
2924 | if (h->root.u.def.section->owner != inf->not_needed) | |
2925 | return TRUE; | |
2926 | break; | |
2927 | ||
2928 | case bfd_link_hash_common: | |
2929 | if (h->root.u.c.p->section->owner != inf->not_needed) | |
2930 | return TRUE; | |
2931 | break; | |
2932 | ||
2933 | case bfd_link_hash_warning: | |
2934 | case bfd_link_hash_indirect: | |
2935 | elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data); | |
2936 | if (h->root.u.i.link->type != bfd_link_hash_new) | |
2937 | return TRUE; | |
2938 | if (h->root.u.i.link->u.undef.abfd != inf->not_needed) | |
2939 | return TRUE; | |
2940 | break; | |
2941 | } | |
2942 | ||
11f25ea6 AM |
2943 | /* There is no way we can undo symbol table state from defined or |
2944 | defweak back to undefined. */ | |
2945 | if (h->ref_regular) | |
2946 | abort (); | |
2947 | ||
2e8b3a61 AM |
2948 | /* Set sym back to newly created state, but keep undef.next if it is |
2949 | being used as a list pointer. */ | |
77cfaee6 | 2950 | bh = h->root.u.undef.next; |
2e8b3a61 AM |
2951 | if (bh == &h->root) |
2952 | bh = NULL; | |
77cfaee6 AM |
2953 | if (bh != NULL || inf->htab->root.undefs_tail == &h->root) |
2954 | inf->twiddled = TRUE; | |
2955 | (*inf->htab->root.table.newfunc) (&h->root.root, | |
2956 | &inf->htab->root.table, | |
2957 | h->root.root.string); | |
2958 | h->root.u.undef.next = bh; | |
2959 | h->root.u.undef.abfd = inf->not_needed; | |
2960 | h->non_elf = 0; | |
2961 | return TRUE; | |
2962 | } | |
2963 | ||
5a580b3a | 2964 | /* Sort symbol by value and section. */ |
4ad4eba5 AM |
2965 | static int |
2966 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
2967 | { |
2968 | const struct elf_link_hash_entry *h1; | |
2969 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 2970 | bfd_signed_vma vdiff; |
5a580b3a AM |
2971 | |
2972 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
2973 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
2974 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
2975 | if (vdiff != 0) | |
2976 | return vdiff > 0 ? 1 : -1; | |
2977 | else | |
2978 | { | |
2979 | long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
2980 | if (sdiff != 0) | |
2981 | return sdiff > 0 ? 1 : -1; | |
2982 | } | |
5a580b3a AM |
2983 | return 0; |
2984 | } | |
4ad4eba5 | 2985 | |
5a580b3a AM |
2986 | /* This function is used to adjust offsets into .dynstr for |
2987 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
2988 | ||
2989 | static bfd_boolean | |
2990 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
2991 | { | |
2992 | struct elf_strtab_hash *dynstr = data; | |
2993 | ||
2994 | if (h->root.type == bfd_link_hash_warning) | |
2995 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2996 | ||
2997 | if (h->dynindx != -1) | |
2998 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
2999 | return TRUE; | |
3000 | } | |
3001 | ||
3002 | /* Assign string offsets in .dynstr, update all structures referencing | |
3003 | them. */ | |
3004 | ||
4ad4eba5 AM |
3005 | static bfd_boolean |
3006 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3007 | { |
3008 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3009 | struct elf_link_local_dynamic_entry *entry; | |
3010 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3011 | bfd *dynobj = hash_table->dynobj; | |
3012 | asection *sdyn; | |
3013 | bfd_size_type size; | |
3014 | const struct elf_backend_data *bed; | |
3015 | bfd_byte *extdyn; | |
3016 | ||
3017 | _bfd_elf_strtab_finalize (dynstr); | |
3018 | size = _bfd_elf_strtab_size (dynstr); | |
3019 | ||
3020 | bed = get_elf_backend_data (dynobj); | |
3021 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
3022 | BFD_ASSERT (sdyn != NULL); | |
3023 | ||
3024 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3025 | for (extdyn = sdyn->contents; | |
eea6121a | 3026 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3027 | extdyn += bed->s->sizeof_dyn) |
3028 | { | |
3029 | Elf_Internal_Dyn dyn; | |
3030 | ||
3031 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3032 | switch (dyn.d_tag) | |
3033 | { | |
3034 | case DT_STRSZ: | |
3035 | dyn.d_un.d_val = size; | |
3036 | break; | |
3037 | case DT_NEEDED: | |
3038 | case DT_SONAME: | |
3039 | case DT_RPATH: | |
3040 | case DT_RUNPATH: | |
3041 | case DT_FILTER: | |
3042 | case DT_AUXILIARY: | |
3043 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
3044 | break; | |
3045 | default: | |
3046 | continue; | |
3047 | } | |
3048 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3049 | } | |
3050 | ||
3051 | /* Now update local dynamic symbols. */ | |
3052 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3053 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3054 | entry->isym.st_name); | |
3055 | ||
3056 | /* And the rest of dynamic symbols. */ | |
3057 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3058 | ||
3059 | /* Adjust version definitions. */ | |
3060 | if (elf_tdata (output_bfd)->cverdefs) | |
3061 | { | |
3062 | asection *s; | |
3063 | bfd_byte *p; | |
3064 | bfd_size_type i; | |
3065 | Elf_Internal_Verdef def; | |
3066 | Elf_Internal_Verdaux defaux; | |
3067 | ||
3068 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
3069 | p = s->contents; | |
3070 | do | |
3071 | { | |
3072 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3073 | &def); | |
3074 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3075 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3076 | continue; | |
5a580b3a AM |
3077 | for (i = 0; i < def.vd_cnt; ++i) |
3078 | { | |
3079 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3080 | (Elf_External_Verdaux *) p, &defaux); | |
3081 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3082 | defaux.vda_name); | |
3083 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3084 | &defaux, (Elf_External_Verdaux *) p); | |
3085 | p += sizeof (Elf_External_Verdaux); | |
3086 | } | |
3087 | } | |
3088 | while (def.vd_next); | |
3089 | } | |
3090 | ||
3091 | /* Adjust version references. */ | |
3092 | if (elf_tdata (output_bfd)->verref) | |
3093 | { | |
3094 | asection *s; | |
3095 | bfd_byte *p; | |
3096 | bfd_size_type i; | |
3097 | Elf_Internal_Verneed need; | |
3098 | Elf_Internal_Vernaux needaux; | |
3099 | ||
3100 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
3101 | p = s->contents; | |
3102 | do | |
3103 | { | |
3104 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3105 | &need); | |
3106 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3107 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3108 | (Elf_External_Verneed *) p); | |
3109 | p += sizeof (Elf_External_Verneed); | |
3110 | for (i = 0; i < need.vn_cnt; ++i) | |
3111 | { | |
3112 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3113 | (Elf_External_Vernaux *) p, &needaux); | |
3114 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3115 | needaux.vna_name); | |
3116 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3117 | &needaux, | |
3118 | (Elf_External_Vernaux *) p); | |
3119 | p += sizeof (Elf_External_Vernaux); | |
3120 | } | |
3121 | } | |
3122 | while (need.vn_next); | |
3123 | } | |
3124 | ||
3125 | return TRUE; | |
3126 | } | |
3127 | \f | |
4ad4eba5 AM |
3128 | /* Add symbols from an ELF object file to the linker hash table. */ |
3129 | ||
3130 | static bfd_boolean | |
3131 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3132 | { | |
3133 | bfd_boolean (*add_symbol_hook) | |
555cd476 | 3134 | (bfd *, struct bfd_link_info *, Elf_Internal_Sym *, |
4ad4eba5 AM |
3135 | const char **, flagword *, asection **, bfd_vma *); |
3136 | bfd_boolean (*check_relocs) | |
3137 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
85fbca6a NC |
3138 | bfd_boolean (*check_directives) |
3139 | (bfd *, struct bfd_link_info *); | |
4ad4eba5 AM |
3140 | bfd_boolean collect; |
3141 | Elf_Internal_Shdr *hdr; | |
3142 | bfd_size_type symcount; | |
3143 | bfd_size_type extsymcount; | |
3144 | bfd_size_type extsymoff; | |
3145 | struct elf_link_hash_entry **sym_hash; | |
3146 | bfd_boolean dynamic; | |
3147 | Elf_External_Versym *extversym = NULL; | |
3148 | Elf_External_Versym *ever; | |
3149 | struct elf_link_hash_entry *weaks; | |
3150 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
3151 | bfd_size_type nondeflt_vers_cnt = 0; | |
3152 | Elf_Internal_Sym *isymbuf = NULL; | |
3153 | Elf_Internal_Sym *isym; | |
3154 | Elf_Internal_Sym *isymend; | |
3155 | const struct elf_backend_data *bed; | |
3156 | bfd_boolean add_needed; | |
3157 | struct elf_link_hash_table * hash_table; | |
3158 | bfd_size_type amt; | |
3159 | ||
3160 | hash_table = elf_hash_table (info); | |
3161 | ||
3162 | bed = get_elf_backend_data (abfd); | |
3163 | add_symbol_hook = bed->elf_add_symbol_hook; | |
3164 | collect = bed->collect; | |
3165 | ||
3166 | if ((abfd->flags & DYNAMIC) == 0) | |
3167 | dynamic = FALSE; | |
3168 | else | |
3169 | { | |
3170 | dynamic = TRUE; | |
3171 | ||
3172 | /* You can't use -r against a dynamic object. Also, there's no | |
3173 | hope of using a dynamic object which does not exactly match | |
3174 | the format of the output file. */ | |
3175 | if (info->relocatable | |
3176 | || !is_elf_hash_table (hash_table) | |
3177 | || hash_table->root.creator != abfd->xvec) | |
3178 | { | |
9a0789ec NC |
3179 | if (info->relocatable) |
3180 | bfd_set_error (bfd_error_invalid_operation); | |
3181 | else | |
3182 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3183 | goto error_return; |
3184 | } | |
3185 | } | |
3186 | ||
3187 | /* As a GNU extension, any input sections which are named | |
3188 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3189 | symbol. This differs from .gnu.warning sections, which generate | |
3190 | warnings when they are included in an output file. */ | |
3191 | if (info->executable) | |
3192 | { | |
3193 | asection *s; | |
3194 | ||
3195 | for (s = abfd->sections; s != NULL; s = s->next) | |
3196 | { | |
3197 | const char *name; | |
3198 | ||
3199 | name = bfd_get_section_name (abfd, s); | |
3200 | if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0) | |
3201 | { | |
3202 | char *msg; | |
3203 | bfd_size_type sz; | |
4ad4eba5 AM |
3204 | |
3205 | name += sizeof ".gnu.warning." - 1; | |
3206 | ||
3207 | /* If this is a shared object, then look up the symbol | |
3208 | in the hash table. If it is there, and it is already | |
3209 | been defined, then we will not be using the entry | |
3210 | from this shared object, so we don't need to warn. | |
3211 | FIXME: If we see the definition in a regular object | |
3212 | later on, we will warn, but we shouldn't. The only | |
3213 | fix is to keep track of what warnings we are supposed | |
3214 | to emit, and then handle them all at the end of the | |
3215 | link. */ | |
3216 | if (dynamic) | |
3217 | { | |
3218 | struct elf_link_hash_entry *h; | |
3219 | ||
3220 | h = elf_link_hash_lookup (hash_table, name, | |
3221 | FALSE, FALSE, TRUE); | |
3222 | ||
3223 | /* FIXME: What about bfd_link_hash_common? */ | |
3224 | if (h != NULL | |
3225 | && (h->root.type == bfd_link_hash_defined | |
3226 | || h->root.type == bfd_link_hash_defweak)) | |
3227 | { | |
3228 | /* We don't want to issue this warning. Clobber | |
3229 | the section size so that the warning does not | |
3230 | get copied into the output file. */ | |
eea6121a | 3231 | s->size = 0; |
4ad4eba5 AM |
3232 | continue; |
3233 | } | |
3234 | } | |
3235 | ||
eea6121a | 3236 | sz = s->size; |
370a0e1b | 3237 | msg = bfd_alloc (abfd, sz + 1); |
4ad4eba5 AM |
3238 | if (msg == NULL) |
3239 | goto error_return; | |
3240 | ||
370a0e1b | 3241 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
4ad4eba5 AM |
3242 | goto error_return; |
3243 | ||
370a0e1b | 3244 | msg[sz] = '\0'; |
4ad4eba5 AM |
3245 | |
3246 | if (! (_bfd_generic_link_add_one_symbol | |
3247 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3248 | FALSE, collect, NULL))) | |
3249 | goto error_return; | |
3250 | ||
3251 | if (! info->relocatable) | |
3252 | { | |
3253 | /* Clobber the section size so that the warning does | |
3254 | not get copied into the output file. */ | |
eea6121a | 3255 | s->size = 0; |
11d2f718 AM |
3256 | |
3257 | /* Also set SEC_EXCLUDE, so that symbols defined in | |
3258 | the warning section don't get copied to the output. */ | |
3259 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3260 | } |
3261 | } | |
3262 | } | |
3263 | } | |
3264 | ||
3265 | add_needed = TRUE; | |
3266 | if (! dynamic) | |
3267 | { | |
3268 | /* If we are creating a shared library, create all the dynamic | |
3269 | sections immediately. We need to attach them to something, | |
3270 | so we attach them to this BFD, provided it is the right | |
3271 | format. FIXME: If there are no input BFD's of the same | |
3272 | format as the output, we can't make a shared library. */ | |
3273 | if (info->shared | |
3274 | && is_elf_hash_table (hash_table) | |
3275 | && hash_table->root.creator == abfd->xvec | |
3276 | && ! hash_table->dynamic_sections_created) | |
3277 | { | |
3278 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3279 | goto error_return; | |
3280 | } | |
3281 | } | |
3282 | else if (!is_elf_hash_table (hash_table)) | |
3283 | goto error_return; | |
3284 | else | |
3285 | { | |
3286 | asection *s; | |
3287 | const char *soname = NULL; | |
3288 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; | |
3289 | int ret; | |
3290 | ||
3291 | /* ld --just-symbols and dynamic objects don't mix very well. | |
3292 | Test for --just-symbols by looking at info set up by | |
3293 | _bfd_elf_link_just_syms. */ | |
3294 | if ((s = abfd->sections) != NULL | |
3295 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
3296 | goto error_return; | |
3297 | ||
3298 | /* If this dynamic lib was specified on the command line with | |
3299 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3300 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3301 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3302 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3303 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3304 | all. */ | |
3305 | add_needed = (elf_dyn_lib_class (abfd) | |
3306 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3307 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3308 | |
3309 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3310 | if (s != NULL) | |
3311 | { | |
3312 | bfd_byte *dynbuf; | |
3313 | bfd_byte *extdyn; | |
3314 | int elfsec; | |
3315 | unsigned long shlink; | |
3316 | ||
eea6121a | 3317 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
4ad4eba5 AM |
3318 | goto error_free_dyn; |
3319 | ||
3320 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
3321 | if (elfsec == -1) | |
3322 | goto error_free_dyn; | |
3323 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3324 | ||
3325 | for (extdyn = dynbuf; | |
eea6121a | 3326 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3327 | extdyn += bed->s->sizeof_dyn) |
3328 | { | |
3329 | Elf_Internal_Dyn dyn; | |
3330 | ||
3331 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3332 | if (dyn.d_tag == DT_SONAME) | |
3333 | { | |
3334 | unsigned int tagv = dyn.d_un.d_val; | |
3335 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3336 | if (soname == NULL) | |
3337 | goto error_free_dyn; | |
3338 | } | |
3339 | if (dyn.d_tag == DT_NEEDED) | |
3340 | { | |
3341 | struct bfd_link_needed_list *n, **pn; | |
3342 | char *fnm, *anm; | |
3343 | unsigned int tagv = dyn.d_un.d_val; | |
3344 | ||
3345 | amt = sizeof (struct bfd_link_needed_list); | |
3346 | n = bfd_alloc (abfd, amt); | |
3347 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3348 | if (n == NULL || fnm == NULL) | |
3349 | goto error_free_dyn; | |
3350 | amt = strlen (fnm) + 1; | |
3351 | anm = bfd_alloc (abfd, amt); | |
3352 | if (anm == NULL) | |
3353 | goto error_free_dyn; | |
3354 | memcpy (anm, fnm, amt); | |
3355 | n->name = anm; | |
3356 | n->by = abfd; | |
3357 | n->next = NULL; | |
3358 | for (pn = & hash_table->needed; | |
3359 | *pn != NULL; | |
3360 | pn = &(*pn)->next) | |
3361 | ; | |
3362 | *pn = n; | |
3363 | } | |
3364 | if (dyn.d_tag == DT_RUNPATH) | |
3365 | { | |
3366 | struct bfd_link_needed_list *n, **pn; | |
3367 | char *fnm, *anm; | |
3368 | unsigned int tagv = dyn.d_un.d_val; | |
3369 | ||
3370 | amt = sizeof (struct bfd_link_needed_list); | |
3371 | n = bfd_alloc (abfd, amt); | |
3372 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3373 | if (n == NULL || fnm == NULL) | |
3374 | goto error_free_dyn; | |
3375 | amt = strlen (fnm) + 1; | |
3376 | anm = bfd_alloc (abfd, amt); | |
3377 | if (anm == NULL) | |
3378 | goto error_free_dyn; | |
3379 | memcpy (anm, fnm, amt); | |
3380 | n->name = anm; | |
3381 | n->by = abfd; | |
3382 | n->next = NULL; | |
3383 | for (pn = & runpath; | |
3384 | *pn != NULL; | |
3385 | pn = &(*pn)->next) | |
3386 | ; | |
3387 | *pn = n; | |
3388 | } | |
3389 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3390 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3391 | { | |
3392 | struct bfd_link_needed_list *n, **pn; | |
3393 | char *fnm, *anm; | |
3394 | unsigned int tagv = dyn.d_un.d_val; | |
3395 | ||
3396 | amt = sizeof (struct bfd_link_needed_list); | |
3397 | n = bfd_alloc (abfd, amt); | |
3398 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3399 | if (n == NULL || fnm == NULL) | |
3400 | goto error_free_dyn; | |
3401 | amt = strlen (fnm) + 1; | |
3402 | anm = bfd_alloc (abfd, amt); | |
3403 | if (anm == NULL) | |
3404 | { | |
3405 | error_free_dyn: | |
3406 | free (dynbuf); | |
3407 | goto error_return; | |
3408 | } | |
3409 | memcpy (anm, fnm, amt); | |
3410 | n->name = anm; | |
3411 | n->by = abfd; | |
3412 | n->next = NULL; | |
3413 | for (pn = & rpath; | |
3414 | *pn != NULL; | |
3415 | pn = &(*pn)->next) | |
3416 | ; | |
3417 | *pn = n; | |
3418 | } | |
3419 | } | |
3420 | ||
3421 | free (dynbuf); | |
3422 | } | |
3423 | ||
3424 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3425 | frees all more recently bfd_alloc'd blocks as well. */ | |
3426 | if (runpath) | |
3427 | rpath = runpath; | |
3428 | ||
3429 | if (rpath) | |
3430 | { | |
3431 | struct bfd_link_needed_list **pn; | |
3432 | for (pn = & hash_table->runpath; | |
3433 | *pn != NULL; | |
3434 | pn = &(*pn)->next) | |
3435 | ; | |
3436 | *pn = rpath; | |
3437 | } | |
3438 | ||
3439 | /* We do not want to include any of the sections in a dynamic | |
3440 | object in the output file. We hack by simply clobbering the | |
3441 | list of sections in the BFD. This could be handled more | |
3442 | cleanly by, say, a new section flag; the existing | |
3443 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3444 | still implies that the section takes up space in the output | |
3445 | file. */ | |
3446 | bfd_section_list_clear (abfd); | |
3447 | ||
4ad4eba5 AM |
3448 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3449 | object. If the object has a DT_SONAME entry, we use it. | |
3450 | Otherwise, if the generic linker stuck something in | |
3451 | elf_dt_name, we use that. Otherwise, we just use the file | |
3452 | name. */ | |
3453 | if (soname == NULL || *soname == '\0') | |
3454 | { | |
3455 | soname = elf_dt_name (abfd); | |
3456 | if (soname == NULL || *soname == '\0') | |
3457 | soname = bfd_get_filename (abfd); | |
3458 | } | |
3459 | ||
3460 | /* Save the SONAME because sometimes the linker emulation code | |
3461 | will need to know it. */ | |
3462 | elf_dt_name (abfd) = soname; | |
3463 | ||
7e9f0867 | 3464 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3465 | if (ret < 0) |
3466 | goto error_return; | |
3467 | ||
3468 | /* If we have already included this dynamic object in the | |
3469 | link, just ignore it. There is no reason to include a | |
3470 | particular dynamic object more than once. */ | |
3471 | if (ret > 0) | |
3472 | return TRUE; | |
3473 | } | |
3474 | ||
3475 | /* If this is a dynamic object, we always link against the .dynsym | |
3476 | symbol table, not the .symtab symbol table. The dynamic linker | |
3477 | will only see the .dynsym symbol table, so there is no reason to | |
3478 | look at .symtab for a dynamic object. */ | |
3479 | ||
3480 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3481 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3482 | else | |
3483 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3484 | ||
3485 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3486 | ||
3487 | /* The sh_info field of the symtab header tells us where the | |
3488 | external symbols start. We don't care about the local symbols at | |
3489 | this point. */ | |
3490 | if (elf_bad_symtab (abfd)) | |
3491 | { | |
3492 | extsymcount = symcount; | |
3493 | extsymoff = 0; | |
3494 | } | |
3495 | else | |
3496 | { | |
3497 | extsymcount = symcount - hdr->sh_info; | |
3498 | extsymoff = hdr->sh_info; | |
3499 | } | |
3500 | ||
3501 | sym_hash = NULL; | |
3502 | if (extsymcount != 0) | |
3503 | { | |
3504 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3505 | NULL, NULL, NULL); | |
3506 | if (isymbuf == NULL) | |
3507 | goto error_return; | |
3508 | ||
3509 | /* We store a pointer to the hash table entry for each external | |
3510 | symbol. */ | |
3511 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3512 | sym_hash = bfd_alloc (abfd, amt); | |
3513 | if (sym_hash == NULL) | |
3514 | goto error_free_sym; | |
3515 | elf_sym_hashes (abfd) = sym_hash; | |
3516 | } | |
3517 | ||
3518 | if (dynamic) | |
3519 | { | |
3520 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
3521 | if (!_bfd_elf_slurp_version_tables (abfd, |
3522 | info->default_imported_symver)) | |
4ad4eba5 AM |
3523 | goto error_free_sym; |
3524 | ||
3525 | /* Read in the symbol versions, but don't bother to convert them | |
3526 | to internal format. */ | |
3527 | if (elf_dynversym (abfd) != 0) | |
3528 | { | |
3529 | Elf_Internal_Shdr *versymhdr; | |
3530 | ||
3531 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
3532 | extversym = bfd_malloc (versymhdr->sh_size); | |
3533 | if (extversym == NULL) | |
3534 | goto error_free_sym; | |
3535 | amt = versymhdr->sh_size; | |
3536 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3537 | || bfd_bread (extversym, amt, abfd) != amt) | |
3538 | goto error_free_vers; | |
3539 | } | |
3540 | } | |
3541 | ||
3542 | weaks = NULL; | |
3543 | ||
3544 | ever = extversym != NULL ? extversym + extsymoff : NULL; | |
3545 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3546 | isym < isymend; | |
3547 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3548 | { | |
3549 | int bind; | |
3550 | bfd_vma value; | |
af44c138 | 3551 | asection *sec, *new_sec; |
4ad4eba5 AM |
3552 | flagword flags; |
3553 | const char *name; | |
3554 | struct elf_link_hash_entry *h; | |
3555 | bfd_boolean definition; | |
3556 | bfd_boolean size_change_ok; | |
3557 | bfd_boolean type_change_ok; | |
3558 | bfd_boolean new_weakdef; | |
3559 | bfd_boolean override; | |
a4d8e49b | 3560 | bfd_boolean common; |
4ad4eba5 AM |
3561 | unsigned int old_alignment; |
3562 | bfd *old_bfd; | |
3563 | ||
3564 | override = FALSE; | |
3565 | ||
3566 | flags = BSF_NO_FLAGS; | |
3567 | sec = NULL; | |
3568 | value = isym->st_value; | |
3569 | *sym_hash = NULL; | |
a4d8e49b | 3570 | common = bed->common_definition (isym); |
4ad4eba5 AM |
3571 | |
3572 | bind = ELF_ST_BIND (isym->st_info); | |
3573 | if (bind == STB_LOCAL) | |
3574 | { | |
3575 | /* This should be impossible, since ELF requires that all | |
3576 | global symbols follow all local symbols, and that sh_info | |
3577 | point to the first global symbol. Unfortunately, Irix 5 | |
3578 | screws this up. */ | |
3579 | continue; | |
3580 | } | |
3581 | else if (bind == STB_GLOBAL) | |
3582 | { | |
a4d8e49b | 3583 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 AM |
3584 | flags = BSF_GLOBAL; |
3585 | } | |
3586 | else if (bind == STB_WEAK) | |
3587 | flags = BSF_WEAK; | |
3588 | else | |
3589 | { | |
3590 | /* Leave it up to the processor backend. */ | |
3591 | } | |
3592 | ||
3593 | if (isym->st_shndx == SHN_UNDEF) | |
3594 | sec = bfd_und_section_ptr; | |
3595 | else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
3596 | { | |
3597 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3598 | if (sec == NULL) | |
3599 | sec = bfd_abs_section_ptr; | |
529fcb95 PB |
3600 | else if (sec->kept_section) |
3601 | { | |
1f02cbd9 | 3602 | /* Symbols from discarded section are undefined, and have |
3b36f7e6 | 3603 | default visibility. */ |
529fcb95 PB |
3604 | sec = bfd_und_section_ptr; |
3605 | isym->st_shndx = SHN_UNDEF; | |
1f02cbd9 JB |
3606 | isym->st_other = STV_DEFAULT |
3607 | | (isym->st_other & ~ ELF_ST_VISIBILITY(-1)); | |
529fcb95 | 3608 | } |
4ad4eba5 AM |
3609 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
3610 | value -= sec->vma; | |
3611 | } | |
3612 | else if (isym->st_shndx == SHN_ABS) | |
3613 | sec = bfd_abs_section_ptr; | |
3614 | else if (isym->st_shndx == SHN_COMMON) | |
3615 | { | |
3616 | sec = bfd_com_section_ptr; | |
3617 | /* What ELF calls the size we call the value. What ELF | |
3618 | calls the value we call the alignment. */ | |
3619 | value = isym->st_size; | |
3620 | } | |
3621 | else | |
3622 | { | |
3623 | /* Leave it up to the processor backend. */ | |
3624 | } | |
3625 | ||
3626 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3627 | isym->st_name); | |
3628 | if (name == NULL) | |
3629 | goto error_free_vers; | |
3630 | ||
3631 | if (isym->st_shndx == SHN_COMMON | |
3632 | && ELF_ST_TYPE (isym->st_info) == STT_TLS) | |
3633 | { | |
3634 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3635 | ||
3636 | if (tcomm == NULL) | |
3637 | { | |
3496cb2a L |
3638 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", |
3639 | (SEC_ALLOC | |
3640 | | SEC_IS_COMMON | |
3641 | | SEC_LINKER_CREATED | |
3642 | | SEC_THREAD_LOCAL)); | |
3643 | if (tcomm == NULL) | |
4ad4eba5 AM |
3644 | goto error_free_vers; |
3645 | } | |
3646 | sec = tcomm; | |
3647 | } | |
3648 | else if (add_symbol_hook) | |
3649 | { | |
3650 | if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec, | |
3651 | &value)) | |
3652 | goto error_free_vers; | |
3653 | ||
3654 | /* The hook function sets the name to NULL if this symbol | |
3655 | should be skipped for some reason. */ | |
3656 | if (name == NULL) | |
3657 | continue; | |
3658 | } | |
3659 | ||
3660 | /* Sanity check that all possibilities were handled. */ | |
3661 | if (sec == NULL) | |
3662 | { | |
3663 | bfd_set_error (bfd_error_bad_value); | |
3664 | goto error_free_vers; | |
3665 | } | |
3666 | ||
3667 | if (bfd_is_und_section (sec) | |
3668 | || bfd_is_com_section (sec)) | |
3669 | definition = FALSE; | |
3670 | else | |
3671 | definition = TRUE; | |
3672 | ||
3673 | size_change_ok = FALSE; | |
3674 | type_change_ok = get_elf_backend_data (abfd)->type_change_ok; | |
3675 | old_alignment = 0; | |
3676 | old_bfd = NULL; | |
af44c138 | 3677 | new_sec = sec; |
4ad4eba5 AM |
3678 | |
3679 | if (is_elf_hash_table (hash_table)) | |
3680 | { | |
3681 | Elf_Internal_Versym iver; | |
3682 | unsigned int vernum = 0; | |
3683 | bfd_boolean skip; | |
3684 | ||
fc0e6df6 | 3685 | if (ever == NULL) |
4ad4eba5 | 3686 | { |
fc0e6df6 PB |
3687 | if (info->default_imported_symver) |
3688 | /* Use the default symbol version created earlier. */ | |
3689 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
3690 | else | |
3691 | iver.vs_vers = 0; | |
3692 | } | |
3693 | else | |
3694 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
3695 | ||
3696 | vernum = iver.vs_vers & VERSYM_VERSION; | |
3697 | ||
3698 | /* If this is a hidden symbol, or if it is not version | |
3699 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
3700 | However, we do not modify a non-hidden absolute symbol |
3701 | if it is not a function, because it might be the version | |
3702 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 3703 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
cc86ff91 EB |
3704 | || (vernum > 1 && (! bfd_is_abs_section (sec) |
3705 | || ELF_ST_TYPE (isym->st_info) == STT_FUNC))) | |
fc0e6df6 PB |
3706 | { |
3707 | const char *verstr; | |
3708 | size_t namelen, verlen, newlen; | |
3709 | char *newname, *p; | |
3710 | ||
3711 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 3712 | { |
fc0e6df6 PB |
3713 | if (vernum > elf_tdata (abfd)->cverdefs) |
3714 | verstr = NULL; | |
3715 | else if (vernum > 1) | |
3716 | verstr = | |
3717 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
3718 | else | |
3719 | verstr = ""; | |
4ad4eba5 | 3720 | |
fc0e6df6 | 3721 | if (verstr == NULL) |
4ad4eba5 | 3722 | { |
fc0e6df6 PB |
3723 | (*_bfd_error_handler) |
3724 | (_("%B: %s: invalid version %u (max %d)"), | |
3725 | abfd, name, vernum, | |
3726 | elf_tdata (abfd)->cverdefs); | |
3727 | bfd_set_error (bfd_error_bad_value); | |
3728 | goto error_free_vers; | |
4ad4eba5 | 3729 | } |
fc0e6df6 PB |
3730 | } |
3731 | else | |
3732 | { | |
3733 | /* We cannot simply test for the number of | |
3734 | entries in the VERNEED section since the | |
3735 | numbers for the needed versions do not start | |
3736 | at 0. */ | |
3737 | Elf_Internal_Verneed *t; | |
3738 | ||
3739 | verstr = NULL; | |
3740 | for (t = elf_tdata (abfd)->verref; | |
3741 | t != NULL; | |
3742 | t = t->vn_nextref) | |
4ad4eba5 | 3743 | { |
fc0e6df6 | 3744 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 3745 | |
fc0e6df6 PB |
3746 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
3747 | { | |
3748 | if (a->vna_other == vernum) | |
4ad4eba5 | 3749 | { |
fc0e6df6 PB |
3750 | verstr = a->vna_nodename; |
3751 | break; | |
4ad4eba5 | 3752 | } |
4ad4eba5 | 3753 | } |
fc0e6df6 PB |
3754 | if (a != NULL) |
3755 | break; | |
3756 | } | |
3757 | if (verstr == NULL) | |
3758 | { | |
3759 | (*_bfd_error_handler) | |
3760 | (_("%B: %s: invalid needed version %d"), | |
3761 | abfd, name, vernum); | |
3762 | bfd_set_error (bfd_error_bad_value); | |
3763 | goto error_free_vers; | |
4ad4eba5 | 3764 | } |
4ad4eba5 | 3765 | } |
fc0e6df6 PB |
3766 | |
3767 | namelen = strlen (name); | |
3768 | verlen = strlen (verstr); | |
3769 | newlen = namelen + verlen + 2; | |
3770 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3771 | && isym->st_shndx != SHN_UNDEF) | |
3772 | ++newlen; | |
3773 | ||
3774 | newname = bfd_alloc (abfd, newlen); | |
3775 | if (newname == NULL) | |
3776 | goto error_free_vers; | |
3777 | memcpy (newname, name, namelen); | |
3778 | p = newname + namelen; | |
3779 | *p++ = ELF_VER_CHR; | |
3780 | /* If this is a defined non-hidden version symbol, | |
3781 | we add another @ to the name. This indicates the | |
3782 | default version of the symbol. */ | |
3783 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3784 | && isym->st_shndx != SHN_UNDEF) | |
3785 | *p++ = ELF_VER_CHR; | |
3786 | memcpy (p, verstr, verlen + 1); | |
3787 | ||
3788 | name = newname; | |
4ad4eba5 AM |
3789 | } |
3790 | ||
af44c138 L |
3791 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, |
3792 | &value, &old_alignment, | |
4ad4eba5 AM |
3793 | sym_hash, &skip, &override, |
3794 | &type_change_ok, &size_change_ok)) | |
3795 | goto error_free_vers; | |
3796 | ||
3797 | if (skip) | |
3798 | continue; | |
3799 | ||
3800 | if (override) | |
3801 | definition = FALSE; | |
3802 | ||
3803 | h = *sym_hash; | |
3804 | while (h->root.type == bfd_link_hash_indirect | |
3805 | || h->root.type == bfd_link_hash_warning) | |
3806 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3807 | ||
3808 | /* Remember the old alignment if this is a common symbol, so | |
3809 | that we don't reduce the alignment later on. We can't | |
3810 | check later, because _bfd_generic_link_add_one_symbol | |
3811 | will set a default for the alignment which we want to | |
3812 | override. We also remember the old bfd where the existing | |
3813 | definition comes from. */ | |
3814 | switch (h->root.type) | |
3815 | { | |
3816 | default: | |
3817 | break; | |
3818 | ||
3819 | case bfd_link_hash_defined: | |
3820 | case bfd_link_hash_defweak: | |
3821 | old_bfd = h->root.u.def.section->owner; | |
3822 | break; | |
3823 | ||
3824 | case bfd_link_hash_common: | |
3825 | old_bfd = h->root.u.c.p->section->owner; | |
3826 | old_alignment = h->root.u.c.p->alignment_power; | |
3827 | break; | |
3828 | } | |
3829 | ||
3830 | if (elf_tdata (abfd)->verdef != NULL | |
3831 | && ! override | |
3832 | && vernum > 1 | |
3833 | && definition) | |
3834 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
3835 | } | |
3836 | ||
3837 | if (! (_bfd_generic_link_add_one_symbol | |
3838 | (info, abfd, name, flags, sec, value, NULL, FALSE, collect, | |
3839 | (struct bfd_link_hash_entry **) sym_hash))) | |
3840 | goto error_free_vers; | |
3841 | ||
3842 | h = *sym_hash; | |
3843 | while (h->root.type == bfd_link_hash_indirect | |
3844 | || h->root.type == bfd_link_hash_warning) | |
3845 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3846 | *sym_hash = h; | |
3847 | ||
3848 | new_weakdef = FALSE; | |
3849 | if (dynamic | |
3850 | && definition | |
3851 | && (flags & BSF_WEAK) != 0 | |
3852 | && ELF_ST_TYPE (isym->st_info) != STT_FUNC | |
3853 | && is_elf_hash_table (hash_table) | |
f6e332e6 | 3854 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
3855 | { |
3856 | /* Keep a list of all weak defined non function symbols from | |
3857 | a dynamic object, using the weakdef field. Later in this | |
3858 | function we will set the weakdef field to the correct | |
3859 | value. We only put non-function symbols from dynamic | |
3860 | objects on this list, because that happens to be the only | |
3861 | time we need to know the normal symbol corresponding to a | |
3862 | weak symbol, and the information is time consuming to | |
3863 | figure out. If the weakdef field is not already NULL, | |
3864 | then this symbol was already defined by some previous | |
3865 | dynamic object, and we will be using that previous | |
3866 | definition anyhow. */ | |
3867 | ||
f6e332e6 | 3868 | h->u.weakdef = weaks; |
4ad4eba5 AM |
3869 | weaks = h; |
3870 | new_weakdef = TRUE; | |
3871 | } | |
3872 | ||
3873 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 3874 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
3875 | && h->root.type == bfd_link_hash_common) |
3876 | { | |
3877 | unsigned int align; | |
3878 | ||
a4d8e49b | 3879 | if (common) |
af44c138 L |
3880 | align = bfd_log2 (isym->st_value); |
3881 | else | |
3882 | { | |
3883 | /* The new symbol is a common symbol in a shared object. | |
3884 | We need to get the alignment from the section. */ | |
3885 | align = new_sec->alignment_power; | |
3886 | } | |
4ad4eba5 AM |
3887 | if (align > old_alignment |
3888 | /* Permit an alignment power of zero if an alignment of one | |
3889 | is specified and no other alignments have been specified. */ | |
3890 | || (isym->st_value == 1 && old_alignment == 0)) | |
3891 | h->root.u.c.p->alignment_power = align; | |
3892 | else | |
3893 | h->root.u.c.p->alignment_power = old_alignment; | |
3894 | } | |
3895 | ||
3896 | if (is_elf_hash_table (hash_table)) | |
3897 | { | |
4ad4eba5 | 3898 | bfd_boolean dynsym; |
4ad4eba5 AM |
3899 | |
3900 | /* Check the alignment when a common symbol is involved. This | |
3901 | can change when a common symbol is overridden by a normal | |
3902 | definition or a common symbol is ignored due to the old | |
3903 | normal definition. We need to make sure the maximum | |
3904 | alignment is maintained. */ | |
a4d8e49b | 3905 | if ((old_alignment || common) |
4ad4eba5 AM |
3906 | && h->root.type != bfd_link_hash_common) |
3907 | { | |
3908 | unsigned int common_align; | |
3909 | unsigned int normal_align; | |
3910 | unsigned int symbol_align; | |
3911 | bfd *normal_bfd; | |
3912 | bfd *common_bfd; | |
3913 | ||
3914 | symbol_align = ffs (h->root.u.def.value) - 1; | |
3915 | if (h->root.u.def.section->owner != NULL | |
3916 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
3917 | { | |
3918 | normal_align = h->root.u.def.section->alignment_power; | |
3919 | if (normal_align > symbol_align) | |
3920 | normal_align = symbol_align; | |
3921 | } | |
3922 | else | |
3923 | normal_align = symbol_align; | |
3924 | ||
3925 | if (old_alignment) | |
3926 | { | |
3927 | common_align = old_alignment; | |
3928 | common_bfd = old_bfd; | |
3929 | normal_bfd = abfd; | |
3930 | } | |
3931 | else | |
3932 | { | |
3933 | common_align = bfd_log2 (isym->st_value); | |
3934 | common_bfd = abfd; | |
3935 | normal_bfd = old_bfd; | |
3936 | } | |
3937 | ||
3938 | if (normal_align < common_align) | |
3939 | (*_bfd_error_handler) | |
d003868e AM |
3940 | (_("Warning: alignment %u of symbol `%s' in %B" |
3941 | " is smaller than %u in %B"), | |
3942 | normal_bfd, common_bfd, | |
3943 | 1 << normal_align, name, 1 << common_align); | |
4ad4eba5 AM |
3944 | } |
3945 | ||
3946 | /* Remember the symbol size and type. */ | |
3947 | if (isym->st_size != 0 | |
3948 | && (definition || h->size == 0)) | |
3949 | { | |
3950 | if (h->size != 0 && h->size != isym->st_size && ! size_change_ok) | |
3951 | (*_bfd_error_handler) | |
d003868e AM |
3952 | (_("Warning: size of symbol `%s' changed" |
3953 | " from %lu in %B to %lu in %B"), | |
3954 | old_bfd, abfd, | |
4ad4eba5 | 3955 | name, (unsigned long) h->size, |
d003868e | 3956 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
3957 | |
3958 | h->size = isym->st_size; | |
3959 | } | |
3960 | ||
3961 | /* If this is a common symbol, then we always want H->SIZE | |
3962 | to be the size of the common symbol. The code just above | |
3963 | won't fix the size if a common symbol becomes larger. We | |
3964 | don't warn about a size change here, because that is | |
3965 | covered by --warn-common. */ | |
3966 | if (h->root.type == bfd_link_hash_common) | |
3967 | h->size = h->root.u.c.size; | |
3968 | ||
3969 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
3970 | && (definition || h->type == STT_NOTYPE)) | |
3971 | { | |
3972 | if (h->type != STT_NOTYPE | |
3973 | && h->type != ELF_ST_TYPE (isym->st_info) | |
3974 | && ! type_change_ok) | |
3975 | (*_bfd_error_handler) | |
d003868e AM |
3976 | (_("Warning: type of symbol `%s' changed" |
3977 | " from %d to %d in %B"), | |
3978 | abfd, name, h->type, ELF_ST_TYPE (isym->st_info)); | |
4ad4eba5 AM |
3979 | |
3980 | h->type = ELF_ST_TYPE (isym->st_info); | |
3981 | } | |
3982 | ||
3983 | /* If st_other has a processor-specific meaning, specific | |
3984 | code might be needed here. We never merge the visibility | |
3985 | attribute with the one from a dynamic object. */ | |
3986 | if (bed->elf_backend_merge_symbol_attribute) | |
3987 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
3988 | dynamic); | |
3989 | ||
b58f81ae DJ |
3990 | /* If this symbol has default visibility and the user has requested |
3991 | we not re-export it, then mark it as hidden. */ | |
3992 | if (definition && !dynamic | |
3993 | && (abfd->no_export | |
3994 | || (abfd->my_archive && abfd->my_archive->no_export)) | |
3995 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) | |
3996 | isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1)); | |
3997 | ||
4ad4eba5 AM |
3998 | if (isym->st_other != 0 && !dynamic) |
3999 | { | |
4000 | unsigned char hvis, symvis, other, nvis; | |
4001 | ||
4002 | /* Take the balance of OTHER from the definition. */ | |
4003 | other = (definition ? isym->st_other : h->other); | |
4004 | other &= ~ ELF_ST_VISIBILITY (-1); | |
4005 | ||
4006 | /* Combine visibilities, using the most constraining one. */ | |
4007 | hvis = ELF_ST_VISIBILITY (h->other); | |
4008 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
4009 | if (! hvis) | |
4010 | nvis = symvis; | |
4011 | else if (! symvis) | |
4012 | nvis = hvis; | |
4013 | else | |
4014 | nvis = hvis < symvis ? hvis : symvis; | |
4015 | ||
4016 | h->other = other | nvis; | |
4017 | } | |
4018 | ||
4019 | /* Set a flag in the hash table entry indicating the type of | |
4020 | reference or definition we just found. Keep a count of | |
4021 | the number of dynamic symbols we find. A dynamic symbol | |
4022 | is one which is referenced or defined by both a regular | |
4023 | object and a shared object. */ | |
4ad4eba5 AM |
4024 | dynsym = FALSE; |
4025 | if (! dynamic) | |
4026 | { | |
4027 | if (! definition) | |
4028 | { | |
f5385ebf | 4029 | h->ref_regular = 1; |
4ad4eba5 | 4030 | if (bind != STB_WEAK) |
f5385ebf | 4031 | h->ref_regular_nonweak = 1; |
4ad4eba5 AM |
4032 | } |
4033 | else | |
f5385ebf | 4034 | h->def_regular = 1; |
4ad4eba5 | 4035 | if (! info->executable |
f5385ebf AM |
4036 | || h->def_dynamic |
4037 | || h->ref_dynamic) | |
4ad4eba5 AM |
4038 | dynsym = TRUE; |
4039 | } | |
4040 | else | |
4041 | { | |
4042 | if (! definition) | |
f5385ebf | 4043 | h->ref_dynamic = 1; |
4ad4eba5 | 4044 | else |
f5385ebf AM |
4045 | h->def_dynamic = 1; |
4046 | if (h->def_regular | |
4047 | || h->ref_regular | |
f6e332e6 | 4048 | || (h->u.weakdef != NULL |
4ad4eba5 | 4049 | && ! new_weakdef |
f6e332e6 | 4050 | && h->u.weakdef->dynindx != -1)) |
4ad4eba5 AM |
4051 | dynsym = TRUE; |
4052 | } | |
4053 | ||
4ad4eba5 AM |
4054 | /* Check to see if we need to add an indirect symbol for |
4055 | the default name. */ | |
4056 | if (definition || h->root.type == bfd_link_hash_common) | |
4057 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4058 | &sec, &value, &dynsym, | |
4059 | override)) | |
4060 | goto error_free_vers; | |
4061 | ||
4062 | if (definition && !dynamic) | |
4063 | { | |
4064 | char *p = strchr (name, ELF_VER_CHR); | |
4065 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4066 | { | |
4067 | /* Queue non-default versions so that .symver x, x@FOO | |
4068 | aliases can be checked. */ | |
4069 | if (! nondeflt_vers) | |
4070 | { | |
4071 | amt = (isymend - isym + 1) | |
4072 | * sizeof (struct elf_link_hash_entry *); | |
4073 | nondeflt_vers = bfd_malloc (amt); | |
4074 | } | |
4075 | nondeflt_vers [nondeflt_vers_cnt++] = h; | |
4076 | } | |
4077 | } | |
4078 | ||
4079 | if (dynsym && h->dynindx == -1) | |
4080 | { | |
c152c796 | 4081 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4082 | goto error_free_vers; |
f6e332e6 | 4083 | if (h->u.weakdef != NULL |
4ad4eba5 | 4084 | && ! new_weakdef |
f6e332e6 | 4085 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 4086 | { |
f6e332e6 | 4087 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
4088 | goto error_free_vers; |
4089 | } | |
4090 | } | |
4091 | else if (dynsym && h->dynindx != -1) | |
4092 | /* If the symbol already has a dynamic index, but | |
4093 | visibility says it should not be visible, turn it into | |
4094 | a local symbol. */ | |
4095 | switch (ELF_ST_VISIBILITY (h->other)) | |
4096 | { | |
4097 | case STV_INTERNAL: | |
4098 | case STV_HIDDEN: | |
4099 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4100 | dynsym = FALSE; | |
4101 | break; | |
4102 | } | |
4103 | ||
4104 | if (!add_needed | |
4105 | && definition | |
4106 | && dynsym | |
f5385ebf | 4107 | && h->ref_regular) |
4ad4eba5 AM |
4108 | { |
4109 | int ret; | |
4110 | const char *soname = elf_dt_name (abfd); | |
4111 | ||
4112 | /* A symbol from a library loaded via DT_NEEDED of some | |
4113 | other library is referenced by a regular object. | |
e56f61be L |
4114 | Add a DT_NEEDED entry for it. Issue an error if |
4115 | --no-add-needed is used. */ | |
4116 | if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) | |
4117 | { | |
4118 | (*_bfd_error_handler) | |
4119 | (_("%s: invalid DSO for symbol `%s' definition"), | |
d003868e | 4120 | abfd, name); |
e56f61be L |
4121 | bfd_set_error (bfd_error_bad_value); |
4122 | goto error_free_vers; | |
4123 | } | |
4124 | ||
a5db907e AM |
4125 | elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED; |
4126 | ||
4ad4eba5 | 4127 | add_needed = TRUE; |
7e9f0867 | 4128 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4129 | if (ret < 0) |
4130 | goto error_free_vers; | |
4131 | ||
4132 | BFD_ASSERT (ret == 0); | |
4133 | } | |
4134 | } | |
4135 | } | |
4136 | ||
4137 | /* Now that all the symbols from this input file are created, handle | |
4138 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
4139 | if (nondeflt_vers != NULL) | |
4140 | { | |
4141 | bfd_size_type cnt, symidx; | |
4142 | ||
4143 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
4144 | { | |
4145 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
4146 | char *shortname, *p; | |
4147 | ||
4148 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
4149 | if (p == NULL | |
4150 | || (h->root.type != bfd_link_hash_defined | |
4151 | && h->root.type != bfd_link_hash_defweak)) | |
4152 | continue; | |
4153 | ||
4154 | amt = p - h->root.root.string; | |
4155 | shortname = bfd_malloc (amt + 1); | |
4156 | memcpy (shortname, h->root.root.string, amt); | |
4157 | shortname[amt] = '\0'; | |
4158 | ||
4159 | hi = (struct elf_link_hash_entry *) | |
4160 | bfd_link_hash_lookup (&hash_table->root, shortname, | |
4161 | FALSE, FALSE, FALSE); | |
4162 | if (hi != NULL | |
4163 | && hi->root.type == h->root.type | |
4164 | && hi->root.u.def.value == h->root.u.def.value | |
4165 | && hi->root.u.def.section == h->root.u.def.section) | |
4166 | { | |
4167 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4168 | hi->root.type = bfd_link_hash_indirect; | |
4169 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
4170 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); | |
4171 | sym_hash = elf_sym_hashes (abfd); | |
4172 | if (sym_hash) | |
4173 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4174 | if (sym_hash[symidx] == hi) | |
4175 | { | |
4176 | sym_hash[symidx] = h; | |
4177 | break; | |
4178 | } | |
4179 | } | |
4180 | free (shortname); | |
4181 | } | |
4182 | free (nondeflt_vers); | |
4183 | nondeflt_vers = NULL; | |
4184 | } | |
4185 | ||
4186 | if (extversym != NULL) | |
4187 | { | |
4188 | free (extversym); | |
4189 | extversym = NULL; | |
4190 | } | |
4191 | ||
4192 | if (isymbuf != NULL) | |
4193 | free (isymbuf); | |
4194 | isymbuf = NULL; | |
4195 | ||
ec13b3bb AM |
4196 | if (!add_needed |
4197 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
77cfaee6 | 4198 | { |
ec13b3bb AM |
4199 | /* Remove symbols defined in an as-needed shared lib that wasn't |
4200 | needed. */ | |
77cfaee6 AM |
4201 | struct elf_smash_syms_data inf; |
4202 | inf.not_needed = abfd; | |
4203 | inf.htab = hash_table; | |
4204 | inf.twiddled = FALSE; | |
4205 | elf_link_hash_traverse (hash_table, elf_smash_syms, &inf); | |
4206 | if (inf.twiddled) | |
4207 | bfd_link_repair_undef_list (&hash_table->root); | |
4208 | weaks = NULL; | |
4209 | } | |
4210 | ||
4ad4eba5 AM |
4211 | /* Now set the weakdefs field correctly for all the weak defined |
4212 | symbols we found. The only way to do this is to search all the | |
4213 | symbols. Since we only need the information for non functions in | |
4214 | dynamic objects, that's the only time we actually put anything on | |
4215 | the list WEAKS. We need this information so that if a regular | |
4216 | object refers to a symbol defined weakly in a dynamic object, the | |
4217 | real symbol in the dynamic object is also put in the dynamic | |
4218 | symbols; we also must arrange for both symbols to point to the | |
4219 | same memory location. We could handle the general case of symbol | |
4220 | aliasing, but a general symbol alias can only be generated in | |
4221 | assembler code, handling it correctly would be very time | |
4222 | consuming, and other ELF linkers don't handle general aliasing | |
4223 | either. */ | |
4224 | if (weaks != NULL) | |
4225 | { | |
4226 | struct elf_link_hash_entry **hpp; | |
4227 | struct elf_link_hash_entry **hppend; | |
4228 | struct elf_link_hash_entry **sorted_sym_hash; | |
4229 | struct elf_link_hash_entry *h; | |
4230 | size_t sym_count; | |
4231 | ||
4232 | /* Since we have to search the whole symbol list for each weak | |
4233 | defined symbol, search time for N weak defined symbols will be | |
4234 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
4235 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
4236 | sorted_sym_hash = bfd_malloc (amt); | |
4237 | if (sorted_sym_hash == NULL) | |
4238 | goto error_return; | |
4239 | sym_hash = sorted_sym_hash; | |
4240 | hpp = elf_sym_hashes (abfd); | |
4241 | hppend = hpp + extsymcount; | |
4242 | sym_count = 0; | |
4243 | for (; hpp < hppend; hpp++) | |
4244 | { | |
4245 | h = *hpp; | |
4246 | if (h != NULL | |
4247 | && h->root.type == bfd_link_hash_defined | |
4248 | && h->type != STT_FUNC) | |
4249 | { | |
4250 | *sym_hash = h; | |
4251 | sym_hash++; | |
4252 | sym_count++; | |
4253 | } | |
4254 | } | |
4255 | ||
4256 | qsort (sorted_sym_hash, sym_count, | |
4257 | sizeof (struct elf_link_hash_entry *), | |
4258 | elf_sort_symbol); | |
4259 | ||
4260 | while (weaks != NULL) | |
4261 | { | |
4262 | struct elf_link_hash_entry *hlook; | |
4263 | asection *slook; | |
4264 | bfd_vma vlook; | |
4265 | long ilook; | |
4266 | size_t i, j, idx; | |
4267 | ||
4268 | hlook = weaks; | |
f6e332e6 AM |
4269 | weaks = hlook->u.weakdef; |
4270 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
4271 | |
4272 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
4273 | || hlook->root.type == bfd_link_hash_defweak | |
4274 | || hlook->root.type == bfd_link_hash_common | |
4275 | || hlook->root.type == bfd_link_hash_indirect); | |
4276 | slook = hlook->root.u.def.section; | |
4277 | vlook = hlook->root.u.def.value; | |
4278 | ||
4279 | ilook = -1; | |
4280 | i = 0; | |
4281 | j = sym_count; | |
4282 | while (i < j) | |
4283 | { | |
4284 | bfd_signed_vma vdiff; | |
4285 | idx = (i + j) / 2; | |
4286 | h = sorted_sym_hash [idx]; | |
4287 | vdiff = vlook - h->root.u.def.value; | |
4288 | if (vdiff < 0) | |
4289 | j = idx; | |
4290 | else if (vdiff > 0) | |
4291 | i = idx + 1; | |
4292 | else | |
4293 | { | |
a9b881be | 4294 | long sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
4295 | if (sdiff < 0) |
4296 | j = idx; | |
4297 | else if (sdiff > 0) | |
4298 | i = idx + 1; | |
4299 | else | |
4300 | { | |
4301 | ilook = idx; | |
4302 | break; | |
4303 | } | |
4304 | } | |
4305 | } | |
4306 | ||
4307 | /* We didn't find a value/section match. */ | |
4308 | if (ilook == -1) | |
4309 | continue; | |
4310 | ||
4311 | for (i = ilook; i < sym_count; i++) | |
4312 | { | |
4313 | h = sorted_sym_hash [i]; | |
4314 | ||
4315 | /* Stop if value or section doesn't match. */ | |
4316 | if (h->root.u.def.value != vlook | |
4317 | || h->root.u.def.section != slook) | |
4318 | break; | |
4319 | else if (h != hlook) | |
4320 | { | |
f6e332e6 | 4321 | hlook->u.weakdef = h; |
4ad4eba5 AM |
4322 | |
4323 | /* If the weak definition is in the list of dynamic | |
4324 | symbols, make sure the real definition is put | |
4325 | there as well. */ | |
4326 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
4327 | { | |
c152c796 | 4328 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 AM |
4329 | goto error_return; |
4330 | } | |
4331 | ||
4332 | /* If the real definition is in the list of dynamic | |
4333 | symbols, make sure the weak definition is put | |
4334 | there as well. If we don't do this, then the | |
4335 | dynamic loader might not merge the entries for the | |
4336 | real definition and the weak definition. */ | |
4337 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
4338 | { | |
c152c796 | 4339 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4ad4eba5 AM |
4340 | goto error_return; |
4341 | } | |
4342 | break; | |
4343 | } | |
4344 | } | |
4345 | } | |
4346 | ||
4347 | free (sorted_sym_hash); | |
4348 | } | |
4349 | ||
85fbca6a NC |
4350 | check_directives = get_elf_backend_data (abfd)->check_directives; |
4351 | if (check_directives) | |
4352 | check_directives (abfd, info); | |
4353 | ||
4ad4eba5 AM |
4354 | /* If this object is the same format as the output object, and it is |
4355 | not a shared library, then let the backend look through the | |
4356 | relocs. | |
4357 | ||
4358 | This is required to build global offset table entries and to | |
4359 | arrange for dynamic relocs. It is not required for the | |
4360 | particular common case of linking non PIC code, even when linking | |
4361 | against shared libraries, but unfortunately there is no way of | |
4362 | knowing whether an object file has been compiled PIC or not. | |
4363 | Looking through the relocs is not particularly time consuming. | |
4364 | The problem is that we must either (1) keep the relocs in memory, | |
4365 | which causes the linker to require additional runtime memory or | |
4366 | (2) read the relocs twice from the input file, which wastes time. | |
4367 | This would be a good case for using mmap. | |
4368 | ||
4369 | I have no idea how to handle linking PIC code into a file of a | |
4370 | different format. It probably can't be done. */ | |
4371 | check_relocs = get_elf_backend_data (abfd)->check_relocs; | |
4372 | if (! dynamic | |
4373 | && is_elf_hash_table (hash_table) | |
4374 | && hash_table->root.creator == abfd->xvec | |
4375 | && check_relocs != NULL) | |
4376 | { | |
4377 | asection *o; | |
4378 | ||
4379 | for (o = abfd->sections; o != NULL; o = o->next) | |
4380 | { | |
4381 | Elf_Internal_Rela *internal_relocs; | |
4382 | bfd_boolean ok; | |
4383 | ||
4384 | if ((o->flags & SEC_RELOC) == 0 | |
4385 | || o->reloc_count == 0 | |
4386 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4387 | && (o->flags & SEC_DEBUGGING) != 0) | |
4388 | || bfd_is_abs_section (o->output_section)) | |
4389 | continue; | |
4390 | ||
4391 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4392 | info->keep_memory); | |
4393 | if (internal_relocs == NULL) | |
4394 | goto error_return; | |
4395 | ||
4396 | ok = (*check_relocs) (abfd, info, o, internal_relocs); | |
4397 | ||
4398 | if (elf_section_data (o)->relocs != internal_relocs) | |
4399 | free (internal_relocs); | |
4400 | ||
4401 | if (! ok) | |
4402 | goto error_return; | |
4403 | } | |
4404 | } | |
4405 | ||
4406 | /* If this is a non-traditional link, try to optimize the handling | |
4407 | of the .stab/.stabstr sections. */ | |
4408 | if (! dynamic | |
4409 | && ! info->traditional_format | |
4410 | && is_elf_hash_table (hash_table) | |
4411 | && (info->strip != strip_all && info->strip != strip_debugger)) | |
4412 | { | |
4413 | asection *stabstr; | |
4414 | ||
4415 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4416 | if (stabstr != NULL) | |
4417 | { | |
4418 | bfd_size_type string_offset = 0; | |
4419 | asection *stab; | |
4420 | ||
4421 | for (stab = abfd->sections; stab; stab = stab->next) | |
4422 | if (strncmp (".stab", stab->name, 5) == 0 | |
4423 | && (!stab->name[5] || | |
4424 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4425 | && (stab->flags & SEC_MERGE) == 0 | |
4426 | && !bfd_is_abs_section (stab->output_section)) | |
4427 | { | |
4428 | struct bfd_elf_section_data *secdata; | |
4429 | ||
4430 | secdata = elf_section_data (stab); | |
4431 | if (! _bfd_link_section_stabs (abfd, | |
3722b82f | 4432 | &hash_table->stab_info, |
4ad4eba5 AM |
4433 | stab, stabstr, |
4434 | &secdata->sec_info, | |
4435 | &string_offset)) | |
4436 | goto error_return; | |
4437 | if (secdata->sec_info) | |
4438 | stab->sec_info_type = ELF_INFO_TYPE_STABS; | |
4439 | } | |
4440 | } | |
4441 | } | |
4442 | ||
77cfaee6 | 4443 | if (is_elf_hash_table (hash_table) && add_needed) |
4ad4eba5 AM |
4444 | { |
4445 | /* Add this bfd to the loaded list. */ | |
4446 | struct elf_link_loaded_list *n; | |
4447 | ||
4448 | n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); | |
4449 | if (n == NULL) | |
4450 | goto error_return; | |
4451 | n->abfd = abfd; | |
4452 | n->next = hash_table->loaded; | |
4453 | hash_table->loaded = n; | |
4454 | } | |
4455 | ||
4456 | return TRUE; | |
4457 | ||
4458 | error_free_vers: | |
4459 | if (nondeflt_vers != NULL) | |
4460 | free (nondeflt_vers); | |
4461 | if (extversym != NULL) | |
4462 | free (extversym); | |
4463 | error_free_sym: | |
4464 | if (isymbuf != NULL) | |
4465 | free (isymbuf); | |
4466 | error_return: | |
4467 | return FALSE; | |
4468 | } | |
4469 | ||
8387904d AM |
4470 | /* Return the linker hash table entry of a symbol that might be |
4471 | satisfied by an archive symbol. Return -1 on error. */ | |
4472 | ||
4473 | struct elf_link_hash_entry * | |
4474 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
4475 | struct bfd_link_info *info, | |
4476 | const char *name) | |
4477 | { | |
4478 | struct elf_link_hash_entry *h; | |
4479 | char *p, *copy; | |
4480 | size_t len, first; | |
4481 | ||
4482 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
4483 | if (h != NULL) | |
4484 | return h; | |
4485 | ||
4486 | /* If this is a default version (the name contains @@), look up the | |
4487 | symbol again with only one `@' as well as without the version. | |
4488 | The effect is that references to the symbol with and without the | |
4489 | version will be matched by the default symbol in the archive. */ | |
4490 | ||
4491 | p = strchr (name, ELF_VER_CHR); | |
4492 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4493 | return h; | |
4494 | ||
4495 | /* First check with only one `@'. */ | |
4496 | len = strlen (name); | |
4497 | copy = bfd_alloc (abfd, len); | |
4498 | if (copy == NULL) | |
4499 | return (struct elf_link_hash_entry *) 0 - 1; | |
4500 | ||
4501 | first = p - name + 1; | |
4502 | memcpy (copy, name, first); | |
4503 | memcpy (copy + first, name + first + 1, len - first); | |
4504 | ||
4505 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE); | |
4506 | if (h == NULL) | |
4507 | { | |
4508 | /* We also need to check references to the symbol without the | |
4509 | version. */ | |
4510 | copy[first - 1] = '\0'; | |
4511 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
4512 | FALSE, FALSE, FALSE); | |
4513 | } | |
4514 | ||
4515 | bfd_release (abfd, copy); | |
4516 | return h; | |
4517 | } | |
4518 | ||
0ad989f9 L |
4519 | /* Add symbols from an ELF archive file to the linker hash table. We |
4520 | don't use _bfd_generic_link_add_archive_symbols because of a | |
4521 | problem which arises on UnixWare. The UnixWare libc.so is an | |
4522 | archive which includes an entry libc.so.1 which defines a bunch of | |
4523 | symbols. The libc.so archive also includes a number of other | |
4524 | object files, which also define symbols, some of which are the same | |
4525 | as those defined in libc.so.1. Correct linking requires that we | |
4526 | consider each object file in turn, and include it if it defines any | |
4527 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
4528 | this; it looks through the list of undefined symbols, and includes | |
4529 | any object file which defines them. When this algorithm is used on | |
4530 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
4531 | bunch of symbols. This means that some of the other objects in the | |
4532 | archive are not included in the link, which is incorrect since they | |
4533 | precede libc.so.1 in the archive. | |
4534 | ||
4535 | Fortunately, ELF archive handling is simpler than that done by | |
4536 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4537 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4538 | symbol is currently undefined, we know that we must pull in that | |
4539 | object file. | |
4540 | ||
4541 | Unfortunately, we do have to make multiple passes over the symbol | |
4542 | table until nothing further is resolved. */ | |
4543 | ||
4ad4eba5 AM |
4544 | static bfd_boolean |
4545 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
4546 | { |
4547 | symindex c; | |
4548 | bfd_boolean *defined = NULL; | |
4549 | bfd_boolean *included = NULL; | |
4550 | carsym *symdefs; | |
4551 | bfd_boolean loop; | |
4552 | bfd_size_type amt; | |
8387904d AM |
4553 | const struct elf_backend_data *bed; |
4554 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
4555 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
4556 | |
4557 | if (! bfd_has_map (abfd)) | |
4558 | { | |
4559 | /* An empty archive is a special case. */ | |
4560 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
4561 | return TRUE; | |
4562 | bfd_set_error (bfd_error_no_armap); | |
4563 | return FALSE; | |
4564 | } | |
4565 | ||
4566 | /* Keep track of all symbols we know to be already defined, and all | |
4567 | files we know to be already included. This is to speed up the | |
4568 | second and subsequent passes. */ | |
4569 | c = bfd_ardata (abfd)->symdef_count; | |
4570 | if (c == 0) | |
4571 | return TRUE; | |
4572 | amt = c; | |
4573 | amt *= sizeof (bfd_boolean); | |
4574 | defined = bfd_zmalloc (amt); | |
4575 | included = bfd_zmalloc (amt); | |
4576 | if (defined == NULL || included == NULL) | |
4577 | goto error_return; | |
4578 | ||
4579 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
4580 | bed = get_elf_backend_data (abfd); |
4581 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
4582 | |
4583 | do | |
4584 | { | |
4585 | file_ptr last; | |
4586 | symindex i; | |
4587 | carsym *symdef; | |
4588 | carsym *symdefend; | |
4589 | ||
4590 | loop = FALSE; | |
4591 | last = -1; | |
4592 | ||
4593 | symdef = symdefs; | |
4594 | symdefend = symdef + c; | |
4595 | for (i = 0; symdef < symdefend; symdef++, i++) | |
4596 | { | |
4597 | struct elf_link_hash_entry *h; | |
4598 | bfd *element; | |
4599 | struct bfd_link_hash_entry *undefs_tail; | |
4600 | symindex mark; | |
4601 | ||
4602 | if (defined[i] || included[i]) | |
4603 | continue; | |
4604 | if (symdef->file_offset == last) | |
4605 | { | |
4606 | included[i] = TRUE; | |
4607 | continue; | |
4608 | } | |
4609 | ||
8387904d AM |
4610 | h = archive_symbol_lookup (abfd, info, symdef->name); |
4611 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
4612 | goto error_return; | |
0ad989f9 L |
4613 | |
4614 | if (h == NULL) | |
4615 | continue; | |
4616 | ||
4617 | if (h->root.type == bfd_link_hash_common) | |
4618 | { | |
4619 | /* We currently have a common symbol. The archive map contains | |
4620 | a reference to this symbol, so we may want to include it. We | |
4621 | only want to include it however, if this archive element | |
4622 | contains a definition of the symbol, not just another common | |
4623 | declaration of it. | |
4624 | ||
4625 | Unfortunately some archivers (including GNU ar) will put | |
4626 | declarations of common symbols into their archive maps, as | |
4627 | well as real definitions, so we cannot just go by the archive | |
4628 | map alone. Instead we must read in the element's symbol | |
4629 | table and check that to see what kind of symbol definition | |
4630 | this is. */ | |
4631 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
4632 | continue; | |
4633 | } | |
4634 | else if (h->root.type != bfd_link_hash_undefined) | |
4635 | { | |
4636 | if (h->root.type != bfd_link_hash_undefweak) | |
4637 | defined[i] = TRUE; | |
4638 | continue; | |
4639 | } | |
4640 | ||
4641 | /* We need to include this archive member. */ | |
4642 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
4643 | if (element == NULL) | |
4644 | goto error_return; | |
4645 | ||
4646 | if (! bfd_check_format (element, bfd_object)) | |
4647 | goto error_return; | |
4648 | ||
4649 | /* Doublecheck that we have not included this object | |
4650 | already--it should be impossible, but there may be | |
4651 | something wrong with the archive. */ | |
4652 | if (element->archive_pass != 0) | |
4653 | { | |
4654 | bfd_set_error (bfd_error_bad_value); | |
4655 | goto error_return; | |
4656 | } | |
4657 | element->archive_pass = 1; | |
4658 | ||
4659 | undefs_tail = info->hash->undefs_tail; | |
4660 | ||
4661 | if (! (*info->callbacks->add_archive_element) (info, element, | |
4662 | symdef->name)) | |
4663 | goto error_return; | |
4664 | if (! bfd_link_add_symbols (element, info)) | |
4665 | goto error_return; | |
4666 | ||
4667 | /* If there are any new undefined symbols, we need to make | |
4668 | another pass through the archive in order to see whether | |
4669 | they can be defined. FIXME: This isn't perfect, because | |
4670 | common symbols wind up on undefs_tail and because an | |
4671 | undefined symbol which is defined later on in this pass | |
4672 | does not require another pass. This isn't a bug, but it | |
4673 | does make the code less efficient than it could be. */ | |
4674 | if (undefs_tail != info->hash->undefs_tail) | |
4675 | loop = TRUE; | |
4676 | ||
4677 | /* Look backward to mark all symbols from this object file | |
4678 | which we have already seen in this pass. */ | |
4679 | mark = i; | |
4680 | do | |
4681 | { | |
4682 | included[mark] = TRUE; | |
4683 | if (mark == 0) | |
4684 | break; | |
4685 | --mark; | |
4686 | } | |
4687 | while (symdefs[mark].file_offset == symdef->file_offset); | |
4688 | ||
4689 | /* We mark subsequent symbols from this object file as we go | |
4690 | on through the loop. */ | |
4691 | last = symdef->file_offset; | |
4692 | } | |
4693 | } | |
4694 | while (loop); | |
4695 | ||
4696 | free (defined); | |
4697 | free (included); | |
4698 | ||
4699 | return TRUE; | |
4700 | ||
4701 | error_return: | |
4702 | if (defined != NULL) | |
4703 | free (defined); | |
4704 | if (included != NULL) | |
4705 | free (included); | |
4706 | return FALSE; | |
4707 | } | |
4ad4eba5 AM |
4708 | |
4709 | /* Given an ELF BFD, add symbols to the global hash table as | |
4710 | appropriate. */ | |
4711 | ||
4712 | bfd_boolean | |
4713 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
4714 | { | |
4715 | switch (bfd_get_format (abfd)) | |
4716 | { | |
4717 | case bfd_object: | |
4718 | return elf_link_add_object_symbols (abfd, info); | |
4719 | case bfd_archive: | |
4720 | return elf_link_add_archive_symbols (abfd, info); | |
4721 | default: | |
4722 | bfd_set_error (bfd_error_wrong_format); | |
4723 | return FALSE; | |
4724 | } | |
4725 | } | |
5a580b3a AM |
4726 | \f |
4727 | /* This function will be called though elf_link_hash_traverse to store | |
4728 | all hash value of the exported symbols in an array. */ | |
4729 | ||
4730 | static bfd_boolean | |
4731 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
4732 | { | |
4733 | unsigned long **valuep = data; | |
4734 | const char *name; | |
4735 | char *p; | |
4736 | unsigned long ha; | |
4737 | char *alc = NULL; | |
4738 | ||
4739 | if (h->root.type == bfd_link_hash_warning) | |
4740 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4741 | ||
4742 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
4743 | if (h->dynindx == -1) | |
4744 | return TRUE; | |
4745 | ||
4746 | name = h->root.root.string; | |
4747 | p = strchr (name, ELF_VER_CHR); | |
4748 | if (p != NULL) | |
4749 | { | |
4750 | alc = bfd_malloc (p - name + 1); | |
4751 | memcpy (alc, name, p - name); | |
4752 | alc[p - name] = '\0'; | |
4753 | name = alc; | |
4754 | } | |
4755 | ||
4756 | /* Compute the hash value. */ | |
4757 | ha = bfd_elf_hash (name); | |
4758 | ||
4759 | /* Store the found hash value in the array given as the argument. */ | |
4760 | *(*valuep)++ = ha; | |
4761 | ||
4762 | /* And store it in the struct so that we can put it in the hash table | |
4763 | later. */ | |
f6e332e6 | 4764 | h->u.elf_hash_value = ha; |
5a580b3a AM |
4765 | |
4766 | if (alc != NULL) | |
4767 | free (alc); | |
4768 | ||
4769 | return TRUE; | |
4770 | } | |
4771 | ||
4772 | /* Array used to determine the number of hash table buckets to use | |
4773 | based on the number of symbols there are. If there are fewer than | |
4774 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
4775 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
4776 | than 32771 buckets. */ | |
4777 | ||
4778 | static const size_t elf_buckets[] = | |
4779 | { | |
4780 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
4781 | 16411, 32771, 0 | |
4782 | }; | |
4783 | ||
4784 | /* Compute bucket count for hashing table. We do not use a static set | |
4785 | of possible tables sizes anymore. Instead we determine for all | |
4786 | possible reasonable sizes of the table the outcome (i.e., the | |
4787 | number of collisions etc) and choose the best solution. The | |
4788 | weighting functions are not too simple to allow the table to grow | |
4789 | without bounds. Instead one of the weighting factors is the size. | |
4790 | Therefore the result is always a good payoff between few collisions | |
4791 | (= short chain lengths) and table size. */ | |
4792 | static size_t | |
4793 | compute_bucket_count (struct bfd_link_info *info) | |
4794 | { | |
4795 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; | |
4796 | size_t best_size = 0; | |
4797 | unsigned long int *hashcodes; | |
4798 | unsigned long int *hashcodesp; | |
4799 | unsigned long int i; | |
4800 | bfd_size_type amt; | |
4801 | ||
4802 | /* Compute the hash values for all exported symbols. At the same | |
4803 | time store the values in an array so that we could use them for | |
4804 | optimizations. */ | |
4805 | amt = dynsymcount; | |
4806 | amt *= sizeof (unsigned long int); | |
4807 | hashcodes = bfd_malloc (amt); | |
4808 | if (hashcodes == NULL) | |
4809 | return 0; | |
4810 | hashcodesp = hashcodes; | |
4811 | ||
4812 | /* Put all hash values in HASHCODES. */ | |
4813 | elf_link_hash_traverse (elf_hash_table (info), | |
4814 | elf_collect_hash_codes, &hashcodesp); | |
4815 | ||
4816 | /* We have a problem here. The following code to optimize the table | |
4817 | size requires an integer type with more the 32 bits. If | |
4818 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
4819 | #ifdef BFD_HOST_U_64_BIT | |
4820 | if (info->optimize) | |
4821 | { | |
4822 | unsigned long int nsyms = hashcodesp - hashcodes; | |
4823 | size_t minsize; | |
4824 | size_t maxsize; | |
4825 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
4826 | unsigned long int *counts ; | |
4827 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
4828 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); | |
4829 | ||
4830 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
4831 | that the hashing table must at least have NSYMS/4 and at most | |
4832 | 2*NSYMS buckets. */ | |
4833 | minsize = nsyms / 4; | |
4834 | if (minsize == 0) | |
4835 | minsize = 1; | |
4836 | best_size = maxsize = nsyms * 2; | |
4837 | ||
4838 | /* Create array where we count the collisions in. We must use bfd_malloc | |
4839 | since the size could be large. */ | |
4840 | amt = maxsize; | |
4841 | amt *= sizeof (unsigned long int); | |
4842 | counts = bfd_malloc (amt); | |
4843 | if (counts == NULL) | |
4844 | { | |
4845 | free (hashcodes); | |
4846 | return 0; | |
4847 | } | |
4848 | ||
4849 | /* Compute the "optimal" size for the hash table. The criteria is a | |
4850 | minimal chain length. The minor criteria is (of course) the size | |
4851 | of the table. */ | |
4852 | for (i = minsize; i < maxsize; ++i) | |
4853 | { | |
4854 | /* Walk through the array of hashcodes and count the collisions. */ | |
4855 | BFD_HOST_U_64_BIT max; | |
4856 | unsigned long int j; | |
4857 | unsigned long int fact; | |
4858 | ||
4859 | memset (counts, '\0', i * sizeof (unsigned long int)); | |
4860 | ||
4861 | /* Determine how often each hash bucket is used. */ | |
4862 | for (j = 0; j < nsyms; ++j) | |
4863 | ++counts[hashcodes[j] % i]; | |
4864 | ||
4865 | /* For the weight function we need some information about the | |
4866 | pagesize on the target. This is information need not be 100% | |
4867 | accurate. Since this information is not available (so far) we | |
4868 | define it here to a reasonable default value. If it is crucial | |
4869 | to have a better value some day simply define this value. */ | |
4870 | # ifndef BFD_TARGET_PAGESIZE | |
4871 | # define BFD_TARGET_PAGESIZE (4096) | |
4872 | # endif | |
4873 | ||
4874 | /* We in any case need 2 + NSYMS entries for the size values and | |
4875 | the chains. */ | |
4876 | max = (2 + nsyms) * (bed->s->arch_size / 8); | |
4877 | ||
4878 | # if 1 | |
4879 | /* Variant 1: optimize for short chains. We add the squares | |
4880 | of all the chain lengths (which favors many small chain | |
4881 | over a few long chains). */ | |
4882 | for (j = 0; j < i; ++j) | |
4883 | max += counts[j] * counts[j]; | |
4884 | ||
4885 | /* This adds penalties for the overall size of the table. */ | |
4886 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
4887 | max *= fact * fact; | |
4888 | # else | |
4889 | /* Variant 2: Optimize a lot more for small table. Here we | |
4890 | also add squares of the size but we also add penalties for | |
4891 | empty slots (the +1 term). */ | |
4892 | for (j = 0; j < i; ++j) | |
4893 | max += (1 + counts[j]) * (1 + counts[j]); | |
4894 | ||
4895 | /* The overall size of the table is considered, but not as | |
4896 | strong as in variant 1, where it is squared. */ | |
4897 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
4898 | max *= fact; | |
4899 | # endif | |
4900 | ||
4901 | /* Compare with current best results. */ | |
4902 | if (max < best_chlen) | |
4903 | { | |
4904 | best_chlen = max; | |
4905 | best_size = i; | |
4906 | } | |
4907 | } | |
4908 | ||
4909 | free (counts); | |
4910 | } | |
4911 | else | |
4912 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
4913 | { | |
4914 | /* This is the fallback solution if no 64bit type is available or if we | |
4915 | are not supposed to spend much time on optimizations. We select the | |
4916 | bucket count using a fixed set of numbers. */ | |
4917 | for (i = 0; elf_buckets[i] != 0; i++) | |
4918 | { | |
4919 | best_size = elf_buckets[i]; | |
4920 | if (dynsymcount < elf_buckets[i + 1]) | |
4921 | break; | |
4922 | } | |
4923 | } | |
4924 | ||
4925 | /* Free the arrays we needed. */ | |
4926 | free (hashcodes); | |
4927 | ||
4928 | return best_size; | |
4929 | } | |
4930 | ||
4931 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
4932 | called by the ELF linker emulation before_allocation routine. We | |
4933 | must set the sizes of the sections before the linker sets the | |
4934 | addresses of the various sections. */ | |
4935 | ||
4936 | bfd_boolean | |
4937 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
4938 | const char *soname, | |
4939 | const char *rpath, | |
4940 | const char *filter_shlib, | |
4941 | const char * const *auxiliary_filters, | |
4942 | struct bfd_link_info *info, | |
4943 | asection **sinterpptr, | |
4944 | struct bfd_elf_version_tree *verdefs) | |
4945 | { | |
4946 | bfd_size_type soname_indx; | |
4947 | bfd *dynobj; | |
4948 | const struct elf_backend_data *bed; | |
4949 | struct elf_assign_sym_version_info asvinfo; | |
4950 | ||
4951 | *sinterpptr = NULL; | |
4952 | ||
4953 | soname_indx = (bfd_size_type) -1; | |
4954 | ||
4955 | if (!is_elf_hash_table (info->hash)) | |
4956 | return TRUE; | |
4957 | ||
8c37241b | 4958 | elf_tdata (output_bfd)->relro = info->relro; |
5a580b3a AM |
4959 | if (info->execstack) |
4960 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
4961 | else if (info->noexecstack) | |
4962 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
4963 | else | |
4964 | { | |
4965 | bfd *inputobj; | |
4966 | asection *notesec = NULL; | |
4967 | int exec = 0; | |
4968 | ||
4969 | for (inputobj = info->input_bfds; | |
4970 | inputobj; | |
4971 | inputobj = inputobj->link_next) | |
4972 | { | |
4973 | asection *s; | |
4974 | ||
d457dcf6 | 4975 | if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED)) |
5a580b3a AM |
4976 | continue; |
4977 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
4978 | if (s) | |
4979 | { | |
4980 | if (s->flags & SEC_CODE) | |
4981 | exec = PF_X; | |
4982 | notesec = s; | |
4983 | } | |
4984 | else | |
4985 | exec = PF_X; | |
4986 | } | |
4987 | if (notesec) | |
4988 | { | |
4989 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
4990 | if (exec && info->relocatable | |
4991 | && notesec->output_section != bfd_abs_section_ptr) | |
4992 | notesec->output_section->flags |= SEC_CODE; | |
4993 | } | |
4994 | } | |
4995 | ||
4996 | /* Any syms created from now on start with -1 in | |
4997 | got.refcount/offset and plt.refcount/offset. */ | |
a6aa5195 AM |
4998 | elf_hash_table (info)->init_got_refcount |
4999 | = elf_hash_table (info)->init_got_offset; | |
5000 | elf_hash_table (info)->init_plt_refcount | |
5001 | = elf_hash_table (info)->init_plt_offset; | |
5a580b3a AM |
5002 | |
5003 | /* The backend may have to create some sections regardless of whether | |
5004 | we're dynamic or not. */ | |
5005 | bed = get_elf_backend_data (output_bfd); | |
5006 | if (bed->elf_backend_always_size_sections | |
5007 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
5008 | return FALSE; | |
5009 | ||
5010 | dynobj = elf_hash_table (info)->dynobj; | |
5011 | ||
5012 | /* If there were no dynamic objects in the link, there is nothing to | |
5013 | do here. */ | |
5014 | if (dynobj == NULL) | |
5015 | return TRUE; | |
5016 | ||
5017 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
5018 | return FALSE; | |
5019 | ||
5020 | if (elf_hash_table (info)->dynamic_sections_created) | |
5021 | { | |
5022 | struct elf_info_failed eif; | |
5023 | struct elf_link_hash_entry *h; | |
5024 | asection *dynstr; | |
5025 | struct bfd_elf_version_tree *t; | |
5026 | struct bfd_elf_version_expr *d; | |
046183de | 5027 | asection *s; |
5a580b3a AM |
5028 | bfd_boolean all_defined; |
5029 | ||
5030 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
5031 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
5032 | ||
5033 | if (soname != NULL) | |
5034 | { | |
5035 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5036 | soname, TRUE); | |
5037 | if (soname_indx == (bfd_size_type) -1 | |
5038 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
5039 | return FALSE; | |
5040 | } | |
5041 | ||
5042 | if (info->symbolic) | |
5043 | { | |
5044 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
5045 | return FALSE; | |
5046 | info->flags |= DF_SYMBOLIC; | |
5047 | } | |
5048 | ||
5049 | if (rpath != NULL) | |
5050 | { | |
5051 | bfd_size_type indx; | |
5052 | ||
5053 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
5054 | TRUE); | |
5055 | if (indx == (bfd_size_type) -1 | |
5056 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
5057 | return FALSE; | |
5058 | ||
5059 | if (info->new_dtags) | |
5060 | { | |
5061 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
5062 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
5063 | return FALSE; | |
5064 | } | |
5065 | } | |
5066 | ||
5067 | if (filter_shlib != NULL) | |
5068 | { | |
5069 | bfd_size_type indx; | |
5070 | ||
5071 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5072 | filter_shlib, TRUE); | |
5073 | if (indx == (bfd_size_type) -1 | |
5074 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
5075 | return FALSE; | |
5076 | } | |
5077 | ||
5078 | if (auxiliary_filters != NULL) | |
5079 | { | |
5080 | const char * const *p; | |
5081 | ||
5082 | for (p = auxiliary_filters; *p != NULL; p++) | |
5083 | { | |
5084 | bfd_size_type indx; | |
5085 | ||
5086 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5087 | *p, TRUE); | |
5088 | if (indx == (bfd_size_type) -1 | |
5089 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
5090 | return FALSE; | |
5091 | } | |
5092 | } | |
5093 | ||
5094 | eif.info = info; | |
5095 | eif.verdefs = verdefs; | |
5096 | eif.failed = FALSE; | |
5097 | ||
5098 | /* If we are supposed to export all symbols into the dynamic symbol | |
5099 | table (this is not the normal case), then do so. */ | |
5100 | if (info->export_dynamic) | |
5101 | { | |
5102 | elf_link_hash_traverse (elf_hash_table (info), | |
5103 | _bfd_elf_export_symbol, | |
5104 | &eif); | |
5105 | if (eif.failed) | |
5106 | return FALSE; | |
5107 | } | |
5108 | ||
5109 | /* Make all global versions with definition. */ | |
5110 | for (t = verdefs; t != NULL; t = t->next) | |
5111 | for (d = t->globals.list; d != NULL; d = d->next) | |
5112 | if (!d->symver && d->symbol) | |
5113 | { | |
5114 | const char *verstr, *name; | |
5115 | size_t namelen, verlen, newlen; | |
5116 | char *newname, *p; | |
5117 | struct elf_link_hash_entry *newh; | |
5118 | ||
5119 | name = d->symbol; | |
5120 | namelen = strlen (name); | |
5121 | verstr = t->name; | |
5122 | verlen = strlen (verstr); | |
5123 | newlen = namelen + verlen + 3; | |
5124 | ||
5125 | newname = bfd_malloc (newlen); | |
5126 | if (newname == NULL) | |
5127 | return FALSE; | |
5128 | memcpy (newname, name, namelen); | |
5129 | ||
5130 | /* Check the hidden versioned definition. */ | |
5131 | p = newname + namelen; | |
5132 | *p++ = ELF_VER_CHR; | |
5133 | memcpy (p, verstr, verlen + 1); | |
5134 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5135 | newname, FALSE, FALSE, | |
5136 | FALSE); | |
5137 | if (newh == NULL | |
5138 | || (newh->root.type != bfd_link_hash_defined | |
5139 | && newh->root.type != bfd_link_hash_defweak)) | |
5140 | { | |
5141 | /* Check the default versioned definition. */ | |
5142 | *p++ = ELF_VER_CHR; | |
5143 | memcpy (p, verstr, verlen + 1); | |
5144 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5145 | newname, FALSE, FALSE, | |
5146 | FALSE); | |
5147 | } | |
5148 | free (newname); | |
5149 | ||
5150 | /* Mark this version if there is a definition and it is | |
5151 | not defined in a shared object. */ | |
5152 | if (newh != NULL | |
f5385ebf | 5153 | && !newh->def_dynamic |
5a580b3a AM |
5154 | && (newh->root.type == bfd_link_hash_defined |
5155 | || newh->root.type == bfd_link_hash_defweak)) | |
5156 | d->symver = 1; | |
5157 | } | |
5158 | ||
5159 | /* Attach all the symbols to their version information. */ | |
5160 | asvinfo.output_bfd = output_bfd; | |
5161 | asvinfo.info = info; | |
5162 | asvinfo.verdefs = verdefs; | |
5163 | asvinfo.failed = FALSE; | |
5164 | ||
5165 | elf_link_hash_traverse (elf_hash_table (info), | |
5166 | _bfd_elf_link_assign_sym_version, | |
5167 | &asvinfo); | |
5168 | if (asvinfo.failed) | |
5169 | return FALSE; | |
5170 | ||
5171 | if (!info->allow_undefined_version) | |
5172 | { | |
5173 | /* Check if all global versions have a definition. */ | |
5174 | all_defined = TRUE; | |
5175 | for (t = verdefs; t != NULL; t = t->next) | |
5176 | for (d = t->globals.list; d != NULL; d = d->next) | |
5177 | if (!d->symver && !d->script) | |
5178 | { | |
5179 | (*_bfd_error_handler) | |
5180 | (_("%s: undefined version: %s"), | |
5181 | d->pattern, t->name); | |
5182 | all_defined = FALSE; | |
5183 | } | |
5184 | ||
5185 | if (!all_defined) | |
5186 | { | |
5187 | bfd_set_error (bfd_error_bad_value); | |
5188 | return FALSE; | |
5189 | } | |
5190 | } | |
5191 | ||
5192 | /* Find all symbols which were defined in a dynamic object and make | |
5193 | the backend pick a reasonable value for them. */ | |
5194 | elf_link_hash_traverse (elf_hash_table (info), | |
5195 | _bfd_elf_adjust_dynamic_symbol, | |
5196 | &eif); | |
5197 | if (eif.failed) | |
5198 | return FALSE; | |
5199 | ||
5200 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 5201 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
5202 | now so that we know the final size of the .dynamic section. */ |
5203 | ||
5204 | /* If there are initialization and/or finalization functions to | |
5205 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
5206 | h = (info->init_function | |
5207 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5208 | info->init_function, FALSE, | |
5209 | FALSE, FALSE) | |
5210 | : NULL); | |
5211 | if (h != NULL | |
f5385ebf AM |
5212 | && (h->ref_regular |
5213 | || h->def_regular)) | |
5a580b3a AM |
5214 | { |
5215 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
5216 | return FALSE; | |
5217 | } | |
5218 | h = (info->fini_function | |
5219 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5220 | info->fini_function, FALSE, | |
5221 | FALSE, FALSE) | |
5222 | : NULL); | |
5223 | if (h != NULL | |
f5385ebf AM |
5224 | && (h->ref_regular |
5225 | || h->def_regular)) | |
5a580b3a AM |
5226 | { |
5227 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
5228 | return FALSE; | |
5229 | } | |
5230 | ||
046183de AM |
5231 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); |
5232 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5233 | { |
5234 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
5235 | if (! info->executable) | |
5236 | { | |
5237 | bfd *sub; | |
5238 | asection *o; | |
5239 | ||
5240 | for (sub = info->input_bfds; sub != NULL; | |
5241 | sub = sub->link_next) | |
5242 | for (o = sub->sections; o != NULL; o = o->next) | |
5243 | if (elf_section_data (o)->this_hdr.sh_type | |
5244 | == SHT_PREINIT_ARRAY) | |
5245 | { | |
5246 | (*_bfd_error_handler) | |
d003868e AM |
5247 | (_("%B: .preinit_array section is not allowed in DSO"), |
5248 | sub); | |
5a580b3a AM |
5249 | break; |
5250 | } | |
5251 | ||
5252 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5253 | return FALSE; | |
5254 | } | |
5255 | ||
5256 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
5257 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
5258 | return FALSE; | |
5259 | } | |
046183de AM |
5260 | s = bfd_get_section_by_name (output_bfd, ".init_array"); |
5261 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5262 | { |
5263 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
5264 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
5265 | return FALSE; | |
5266 | } | |
046183de AM |
5267 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); |
5268 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5269 | { |
5270 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
5271 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
5272 | return FALSE; | |
5273 | } | |
5274 | ||
5275 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5276 | /* If .dynstr is excluded from the link, we don't want any of | |
5277 | these tags. Strictly, we should be checking each section | |
5278 | individually; This quick check covers for the case where | |
5279 | someone does a /DISCARD/ : { *(*) }. */ | |
5280 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
5281 | { | |
5282 | bfd_size_type strsize; | |
5283 | ||
5284 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
5285 | if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0) | |
5286 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
5287 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
5288 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
5289 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
5290 | bed->s->sizeof_sym)) | |
5291 | return FALSE; | |
5292 | } | |
5293 | } | |
5294 | ||
5295 | /* The backend must work out the sizes of all the other dynamic | |
5296 | sections. */ | |
5297 | if (bed->elf_backend_size_dynamic_sections | |
5298 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
5299 | return FALSE; | |
5300 | ||
5301 | if (elf_hash_table (info)->dynamic_sections_created) | |
5302 | { | |
554220db | 5303 | unsigned long section_sym_count; |
5a580b3a | 5304 | asection *s; |
5a580b3a AM |
5305 | |
5306 | /* Set up the version definition section. */ | |
5307 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
5308 | BFD_ASSERT (s != NULL); | |
5309 | ||
5310 | /* We may have created additional version definitions if we are | |
5311 | just linking a regular application. */ | |
5312 | verdefs = asvinfo.verdefs; | |
5313 | ||
5314 | /* Skip anonymous version tag. */ | |
5315 | if (verdefs != NULL && verdefs->vernum == 0) | |
5316 | verdefs = verdefs->next; | |
5317 | ||
3e3b46e5 | 5318 | if (verdefs == NULL && !info->create_default_symver) |
8423293d | 5319 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5320 | else |
5321 | { | |
5322 | unsigned int cdefs; | |
5323 | bfd_size_type size; | |
5324 | struct bfd_elf_version_tree *t; | |
5325 | bfd_byte *p; | |
5326 | Elf_Internal_Verdef def; | |
5327 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
5328 | struct bfd_link_hash_entry *bh; |
5329 | struct elf_link_hash_entry *h; | |
5330 | const char *name; | |
5a580b3a AM |
5331 | |
5332 | cdefs = 0; | |
5333 | size = 0; | |
5334 | ||
5335 | /* Make space for the base version. */ | |
5336 | size += sizeof (Elf_External_Verdef); | |
5337 | size += sizeof (Elf_External_Verdaux); | |
5338 | ++cdefs; | |
5339 | ||
3e3b46e5 PB |
5340 | /* Make space for the default version. */ |
5341 | if (info->create_default_symver) | |
5342 | { | |
5343 | size += sizeof (Elf_External_Verdef); | |
5344 | ++cdefs; | |
5345 | } | |
5346 | ||
5a580b3a AM |
5347 | for (t = verdefs; t != NULL; t = t->next) |
5348 | { | |
5349 | struct bfd_elf_version_deps *n; | |
5350 | ||
5351 | size += sizeof (Elf_External_Verdef); | |
5352 | size += sizeof (Elf_External_Verdaux); | |
5353 | ++cdefs; | |
5354 | ||
5355 | for (n = t->deps; n != NULL; n = n->next) | |
5356 | size += sizeof (Elf_External_Verdaux); | |
5357 | } | |
5358 | ||
eea6121a AM |
5359 | s->size = size; |
5360 | s->contents = bfd_alloc (output_bfd, s->size); | |
5361 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5362 | return FALSE; |
5363 | ||
5364 | /* Fill in the version definition section. */ | |
5365 | ||
5366 | p = s->contents; | |
5367 | ||
5368 | def.vd_version = VER_DEF_CURRENT; | |
5369 | def.vd_flags = VER_FLG_BASE; | |
5370 | def.vd_ndx = 1; | |
5371 | def.vd_cnt = 1; | |
3e3b46e5 PB |
5372 | if (info->create_default_symver) |
5373 | { | |
5374 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
5375 | def.vd_next = sizeof (Elf_External_Verdef); | |
5376 | } | |
5377 | else | |
5378 | { | |
5379 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5380 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5381 | + sizeof (Elf_External_Verdaux)); | |
5382 | } | |
5a580b3a AM |
5383 | |
5384 | if (soname_indx != (bfd_size_type) -1) | |
5385 | { | |
5386 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5387 | soname_indx); | |
5388 | def.vd_hash = bfd_elf_hash (soname); | |
5389 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 5390 | name = soname; |
5a580b3a AM |
5391 | } |
5392 | else | |
5393 | { | |
5a580b3a AM |
5394 | bfd_size_type indx; |
5395 | ||
06084812 | 5396 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
5397 | def.vd_hash = bfd_elf_hash (name); |
5398 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5399 | name, FALSE); | |
5400 | if (indx == (bfd_size_type) -1) | |
5401 | return FALSE; | |
5402 | defaux.vda_name = indx; | |
5403 | } | |
5404 | defaux.vda_next = 0; | |
5405 | ||
5406 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5407 | (Elf_External_Verdef *) p); | |
5408 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
5409 | if (info->create_default_symver) |
5410 | { | |
5411 | /* Add a symbol representing this version. */ | |
5412 | bh = NULL; | |
5413 | if (! (_bfd_generic_link_add_one_symbol | |
5414 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5415 | 0, NULL, FALSE, | |
5416 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5417 | return FALSE; | |
5418 | h = (struct elf_link_hash_entry *) bh; | |
5419 | h->non_elf = 0; | |
5420 | h->def_regular = 1; | |
5421 | h->type = STT_OBJECT; | |
5422 | h->verinfo.vertree = NULL; | |
5423 | ||
5424 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
5425 | return FALSE; | |
5426 | ||
5427 | /* Create a duplicate of the base version with the same | |
5428 | aux block, but different flags. */ | |
5429 | def.vd_flags = 0; | |
5430 | def.vd_ndx = 2; | |
5431 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5432 | if (verdefs) | |
5433 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5434 | + sizeof (Elf_External_Verdaux)); | |
5435 | else | |
5436 | def.vd_next = 0; | |
5437 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5438 | (Elf_External_Verdef *) p); | |
5439 | p += sizeof (Elf_External_Verdef); | |
5440 | } | |
5a580b3a AM |
5441 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
5442 | (Elf_External_Verdaux *) p); | |
5443 | p += sizeof (Elf_External_Verdaux); | |
5444 | ||
5445 | for (t = verdefs; t != NULL; t = t->next) | |
5446 | { | |
5447 | unsigned int cdeps; | |
5448 | struct bfd_elf_version_deps *n; | |
5a580b3a AM |
5449 | |
5450 | cdeps = 0; | |
5451 | for (n = t->deps; n != NULL; n = n->next) | |
5452 | ++cdeps; | |
5453 | ||
5454 | /* Add a symbol representing this version. */ | |
5455 | bh = NULL; | |
5456 | if (! (_bfd_generic_link_add_one_symbol | |
5457 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5458 | 0, NULL, FALSE, | |
5459 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5460 | return FALSE; | |
5461 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
5462 | h->non_elf = 0; |
5463 | h->def_regular = 1; | |
5a580b3a AM |
5464 | h->type = STT_OBJECT; |
5465 | h->verinfo.vertree = t; | |
5466 | ||
c152c796 | 5467 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
5468 | return FALSE; |
5469 | ||
5470 | def.vd_version = VER_DEF_CURRENT; | |
5471 | def.vd_flags = 0; | |
5472 | if (t->globals.list == NULL | |
5473 | && t->locals.list == NULL | |
5474 | && ! t->used) | |
5475 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 5476 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
5477 | def.vd_cnt = cdeps + 1; |
5478 | def.vd_hash = bfd_elf_hash (t->name); | |
5479 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5480 | def.vd_next = 0; | |
5481 | if (t->next != NULL) | |
5482 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5483 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
5484 | ||
5485 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5486 | (Elf_External_Verdef *) p); | |
5487 | p += sizeof (Elf_External_Verdef); | |
5488 | ||
5489 | defaux.vda_name = h->dynstr_index; | |
5490 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5491 | h->dynstr_index); | |
5492 | defaux.vda_next = 0; | |
5493 | if (t->deps != NULL) | |
5494 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5495 | t->name_indx = defaux.vda_name; | |
5496 | ||
5497 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5498 | (Elf_External_Verdaux *) p); | |
5499 | p += sizeof (Elf_External_Verdaux); | |
5500 | ||
5501 | for (n = t->deps; n != NULL; n = n->next) | |
5502 | { | |
5503 | if (n->version_needed == NULL) | |
5504 | { | |
5505 | /* This can happen if there was an error in the | |
5506 | version script. */ | |
5507 | defaux.vda_name = 0; | |
5508 | } | |
5509 | else | |
5510 | { | |
5511 | defaux.vda_name = n->version_needed->name_indx; | |
5512 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5513 | defaux.vda_name); | |
5514 | } | |
5515 | if (n->next == NULL) | |
5516 | defaux.vda_next = 0; | |
5517 | else | |
5518 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5519 | ||
5520 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5521 | (Elf_External_Verdaux *) p); | |
5522 | p += sizeof (Elf_External_Verdaux); | |
5523 | } | |
5524 | } | |
5525 | ||
5526 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
5527 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
5528 | return FALSE; | |
5529 | ||
5530 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
5531 | } | |
5532 | ||
5533 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
5534 | { | |
5535 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
5536 | return FALSE; | |
5537 | } | |
5538 | else if (info->flags & DF_BIND_NOW) | |
5539 | { | |
5540 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
5541 | return FALSE; | |
5542 | } | |
5543 | ||
5544 | if (info->flags_1) | |
5545 | { | |
5546 | if (info->executable) | |
5547 | info->flags_1 &= ~ (DF_1_INITFIRST | |
5548 | | DF_1_NODELETE | |
5549 | | DF_1_NOOPEN); | |
5550 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
5551 | return FALSE; | |
5552 | } | |
5553 | ||
5554 | /* Work out the size of the version reference section. */ | |
5555 | ||
5556 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
5557 | BFD_ASSERT (s != NULL); | |
5558 | { | |
5559 | struct elf_find_verdep_info sinfo; | |
5560 | ||
5561 | sinfo.output_bfd = output_bfd; | |
5562 | sinfo.info = info; | |
5563 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
5564 | if (sinfo.vers == 0) | |
5565 | sinfo.vers = 1; | |
5566 | sinfo.failed = FALSE; | |
5567 | ||
5568 | elf_link_hash_traverse (elf_hash_table (info), | |
5569 | _bfd_elf_link_find_version_dependencies, | |
5570 | &sinfo); | |
5571 | ||
5572 | if (elf_tdata (output_bfd)->verref == NULL) | |
8423293d | 5573 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5574 | else |
5575 | { | |
5576 | Elf_Internal_Verneed *t; | |
5577 | unsigned int size; | |
5578 | unsigned int crefs; | |
5579 | bfd_byte *p; | |
5580 | ||
5581 | /* Build the version definition section. */ | |
5582 | size = 0; | |
5583 | crefs = 0; | |
5584 | for (t = elf_tdata (output_bfd)->verref; | |
5585 | t != NULL; | |
5586 | t = t->vn_nextref) | |
5587 | { | |
5588 | Elf_Internal_Vernaux *a; | |
5589 | ||
5590 | size += sizeof (Elf_External_Verneed); | |
5591 | ++crefs; | |
5592 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5593 | size += sizeof (Elf_External_Vernaux); | |
5594 | } | |
5595 | ||
eea6121a AM |
5596 | s->size = size; |
5597 | s->contents = bfd_alloc (output_bfd, s->size); | |
5a580b3a AM |
5598 | if (s->contents == NULL) |
5599 | return FALSE; | |
5600 | ||
5601 | p = s->contents; | |
5602 | for (t = elf_tdata (output_bfd)->verref; | |
5603 | t != NULL; | |
5604 | t = t->vn_nextref) | |
5605 | { | |
5606 | unsigned int caux; | |
5607 | Elf_Internal_Vernaux *a; | |
5608 | bfd_size_type indx; | |
5609 | ||
5610 | caux = 0; | |
5611 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5612 | ++caux; | |
5613 | ||
5614 | t->vn_version = VER_NEED_CURRENT; | |
5615 | t->vn_cnt = caux; | |
5616 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5617 | elf_dt_name (t->vn_bfd) != NULL | |
5618 | ? elf_dt_name (t->vn_bfd) | |
06084812 | 5619 | : lbasename (t->vn_bfd->filename), |
5a580b3a AM |
5620 | FALSE); |
5621 | if (indx == (bfd_size_type) -1) | |
5622 | return FALSE; | |
5623 | t->vn_file = indx; | |
5624 | t->vn_aux = sizeof (Elf_External_Verneed); | |
5625 | if (t->vn_nextref == NULL) | |
5626 | t->vn_next = 0; | |
5627 | else | |
5628 | t->vn_next = (sizeof (Elf_External_Verneed) | |
5629 | + caux * sizeof (Elf_External_Vernaux)); | |
5630 | ||
5631 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
5632 | (Elf_External_Verneed *) p); | |
5633 | p += sizeof (Elf_External_Verneed); | |
5634 | ||
5635 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5636 | { | |
5637 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
5638 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5639 | a->vna_nodename, FALSE); | |
5640 | if (indx == (bfd_size_type) -1) | |
5641 | return FALSE; | |
5642 | a->vna_name = indx; | |
5643 | if (a->vna_nextptr == NULL) | |
5644 | a->vna_next = 0; | |
5645 | else | |
5646 | a->vna_next = sizeof (Elf_External_Vernaux); | |
5647 | ||
5648 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
5649 | (Elf_External_Vernaux *) p); | |
5650 | p += sizeof (Elf_External_Vernaux); | |
5651 | } | |
5652 | } | |
5653 | ||
5654 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
5655 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
5656 | return FALSE; | |
5657 | ||
5658 | elf_tdata (output_bfd)->cverrefs = crefs; | |
5659 | } | |
5660 | } | |
5661 | ||
8423293d AM |
5662 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
5663 | && elf_tdata (output_bfd)->cverdefs == 0) | |
5664 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
5665 | §ion_sym_count) == 0) | |
5666 | { | |
5667 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
5668 | s->flags |= SEC_EXCLUDE; | |
5669 | } | |
5670 | } | |
5671 | return TRUE; | |
5672 | } | |
5673 | ||
5674 | bfd_boolean | |
5675 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5676 | { | |
5677 | if (!is_elf_hash_table (info->hash)) | |
5678 | return TRUE; | |
5679 | ||
5680 | if (elf_hash_table (info)->dynamic_sections_created) | |
5681 | { | |
5682 | bfd *dynobj; | |
5683 | const struct elf_backend_data *bed; | |
5684 | asection *s; | |
5685 | bfd_size_type dynsymcount; | |
5686 | unsigned long section_sym_count; | |
5687 | size_t bucketcount = 0; | |
5688 | size_t hash_entry_size; | |
5689 | unsigned int dtagcount; | |
5690 | ||
5691 | dynobj = elf_hash_table (info)->dynobj; | |
5692 | ||
5a580b3a AM |
5693 | /* Assign dynsym indicies. In a shared library we generate a |
5694 | section symbol for each output section, which come first. | |
5695 | Next come all of the back-end allocated local dynamic syms, | |
5696 | followed by the rest of the global symbols. */ | |
5697 | ||
554220db AM |
5698 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, |
5699 | §ion_sym_count); | |
5a580b3a AM |
5700 | |
5701 | /* Work out the size of the symbol version section. */ | |
5702 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
5703 | BFD_ASSERT (s != NULL); | |
8423293d AM |
5704 | if (dynsymcount != 0 |
5705 | && (s->flags & SEC_EXCLUDE) == 0) | |
5a580b3a | 5706 | { |
eea6121a AM |
5707 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
5708 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
5709 | if (s->contents == NULL) |
5710 | return FALSE; | |
5711 | ||
5712 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
5713 | return FALSE; | |
5714 | } | |
5715 | ||
5716 | /* Set the size of the .dynsym and .hash sections. We counted | |
5717 | the number of dynamic symbols in elf_link_add_object_symbols. | |
5718 | We will build the contents of .dynsym and .hash when we build | |
5719 | the final symbol table, because until then we do not know the | |
5720 | correct value to give the symbols. We built the .dynstr | |
5721 | section as we went along in elf_link_add_object_symbols. */ | |
5722 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
5723 | BFD_ASSERT (s != NULL); | |
8423293d | 5724 | bed = get_elf_backend_data (output_bfd); |
eea6121a | 5725 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a AM |
5726 | |
5727 | if (dynsymcount != 0) | |
5728 | { | |
554220db AM |
5729 | s->contents = bfd_alloc (output_bfd, s->size); |
5730 | if (s->contents == NULL) | |
5731 | return FALSE; | |
5a580b3a | 5732 | |
554220db AM |
5733 | /* The first entry in .dynsym is a dummy symbol. |
5734 | Clear all the section syms, in case we don't output them all. */ | |
5735 | ++section_sym_count; | |
5736 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a AM |
5737 | } |
5738 | ||
5739 | /* Compute the size of the hashing table. As a side effect this | |
5740 | computes the hash values for all the names we export. */ | |
5741 | bucketcount = compute_bucket_count (info); | |
5742 | ||
5743 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
5744 | BFD_ASSERT (s != NULL); | |
5745 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
eea6121a AM |
5746 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); |
5747 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
5748 | if (s->contents == NULL) |
5749 | return FALSE; | |
5750 | ||
5751 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
5752 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
5753 | s->contents + hash_entry_size); | |
5754 | ||
5755 | elf_hash_table (info)->bucketcount = bucketcount; | |
5756 | ||
5757 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5758 | BFD_ASSERT (s != NULL); | |
5759 | ||
4ad4eba5 | 5760 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 5761 | |
eea6121a | 5762 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
5763 | |
5764 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
5765 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
5766 | return FALSE; | |
5767 | } | |
5768 | ||
5769 | return TRUE; | |
5770 | } | |
c152c796 AM |
5771 | |
5772 | /* Final phase of ELF linker. */ | |
5773 | ||
5774 | /* A structure we use to avoid passing large numbers of arguments. */ | |
5775 | ||
5776 | struct elf_final_link_info | |
5777 | { | |
5778 | /* General link information. */ | |
5779 | struct bfd_link_info *info; | |
5780 | /* Output BFD. */ | |
5781 | bfd *output_bfd; | |
5782 | /* Symbol string table. */ | |
5783 | struct bfd_strtab_hash *symstrtab; | |
5784 | /* .dynsym section. */ | |
5785 | asection *dynsym_sec; | |
5786 | /* .hash section. */ | |
5787 | asection *hash_sec; | |
5788 | /* symbol version section (.gnu.version). */ | |
5789 | asection *symver_sec; | |
5790 | /* Buffer large enough to hold contents of any section. */ | |
5791 | bfd_byte *contents; | |
5792 | /* Buffer large enough to hold external relocs of any section. */ | |
5793 | void *external_relocs; | |
5794 | /* Buffer large enough to hold internal relocs of any section. */ | |
5795 | Elf_Internal_Rela *internal_relocs; | |
5796 | /* Buffer large enough to hold external local symbols of any input | |
5797 | BFD. */ | |
5798 | bfd_byte *external_syms; | |
5799 | /* And a buffer for symbol section indices. */ | |
5800 | Elf_External_Sym_Shndx *locsym_shndx; | |
5801 | /* Buffer large enough to hold internal local symbols of any input | |
5802 | BFD. */ | |
5803 | Elf_Internal_Sym *internal_syms; | |
5804 | /* Array large enough to hold a symbol index for each local symbol | |
5805 | of any input BFD. */ | |
5806 | long *indices; | |
5807 | /* Array large enough to hold a section pointer for each local | |
5808 | symbol of any input BFD. */ | |
5809 | asection **sections; | |
5810 | /* Buffer to hold swapped out symbols. */ | |
5811 | bfd_byte *symbuf; | |
5812 | /* And one for symbol section indices. */ | |
5813 | Elf_External_Sym_Shndx *symshndxbuf; | |
5814 | /* Number of swapped out symbols in buffer. */ | |
5815 | size_t symbuf_count; | |
5816 | /* Number of symbols which fit in symbuf. */ | |
5817 | size_t symbuf_size; | |
5818 | /* And same for symshndxbuf. */ | |
5819 | size_t shndxbuf_size; | |
5820 | }; | |
5821 | ||
5822 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
5823 | ||
5824 | struct elf_outext_info | |
5825 | { | |
5826 | bfd_boolean failed; | |
5827 | bfd_boolean localsyms; | |
5828 | struct elf_final_link_info *finfo; | |
5829 | }; | |
5830 | ||
5831 | /* When performing a relocatable link, the input relocations are | |
5832 | preserved. But, if they reference global symbols, the indices | |
5833 | referenced must be updated. Update all the relocations in | |
5834 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ | |
5835 | ||
5836 | static void | |
5837 | elf_link_adjust_relocs (bfd *abfd, | |
5838 | Elf_Internal_Shdr *rel_hdr, | |
5839 | unsigned int count, | |
5840 | struct elf_link_hash_entry **rel_hash) | |
5841 | { | |
5842 | unsigned int i; | |
5843 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5844 | bfd_byte *erela; | |
5845 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
5846 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
5847 | bfd_vma r_type_mask; | |
5848 | int r_sym_shift; | |
5849 | ||
5850 | if (rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
5851 | { | |
5852 | swap_in = bed->s->swap_reloc_in; | |
5853 | swap_out = bed->s->swap_reloc_out; | |
5854 | } | |
5855 | else if (rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
5856 | { | |
5857 | swap_in = bed->s->swap_reloca_in; | |
5858 | swap_out = bed->s->swap_reloca_out; | |
5859 | } | |
5860 | else | |
5861 | abort (); | |
5862 | ||
5863 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
5864 | abort (); | |
5865 | ||
5866 | if (bed->s->arch_size == 32) | |
5867 | { | |
5868 | r_type_mask = 0xff; | |
5869 | r_sym_shift = 8; | |
5870 | } | |
5871 | else | |
5872 | { | |
5873 | r_type_mask = 0xffffffff; | |
5874 | r_sym_shift = 32; | |
5875 | } | |
5876 | ||
5877 | erela = rel_hdr->contents; | |
5878 | for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) | |
5879 | { | |
5880 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
5881 | unsigned int j; | |
5882 | ||
5883 | if (*rel_hash == NULL) | |
5884 | continue; | |
5885 | ||
5886 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
5887 | ||
5888 | (*swap_in) (abfd, erela, irela); | |
5889 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
5890 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
5891 | | (irela[j].r_info & r_type_mask)); | |
5892 | (*swap_out) (abfd, irela, erela); | |
5893 | } | |
5894 | } | |
5895 | ||
5896 | struct elf_link_sort_rela | |
5897 | { | |
5898 | union { | |
5899 | bfd_vma offset; | |
5900 | bfd_vma sym_mask; | |
5901 | } u; | |
5902 | enum elf_reloc_type_class type; | |
5903 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
5904 | Elf_Internal_Rela rela[1]; | |
5905 | }; | |
5906 | ||
5907 | static int | |
5908 | elf_link_sort_cmp1 (const void *A, const void *B) | |
5909 | { | |
5910 | const struct elf_link_sort_rela *a = A; | |
5911 | const struct elf_link_sort_rela *b = B; | |
5912 | int relativea, relativeb; | |
5913 | ||
5914 | relativea = a->type == reloc_class_relative; | |
5915 | relativeb = b->type == reloc_class_relative; | |
5916 | ||
5917 | if (relativea < relativeb) | |
5918 | return 1; | |
5919 | if (relativea > relativeb) | |
5920 | return -1; | |
5921 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
5922 | return -1; | |
5923 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
5924 | return 1; | |
5925 | if (a->rela->r_offset < b->rela->r_offset) | |
5926 | return -1; | |
5927 | if (a->rela->r_offset > b->rela->r_offset) | |
5928 | return 1; | |
5929 | return 0; | |
5930 | } | |
5931 | ||
5932 | static int | |
5933 | elf_link_sort_cmp2 (const void *A, const void *B) | |
5934 | { | |
5935 | const struct elf_link_sort_rela *a = A; | |
5936 | const struct elf_link_sort_rela *b = B; | |
5937 | int copya, copyb; | |
5938 | ||
5939 | if (a->u.offset < b->u.offset) | |
5940 | return -1; | |
5941 | if (a->u.offset > b->u.offset) | |
5942 | return 1; | |
5943 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); | |
5944 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
5945 | if (copya < copyb) | |
5946 | return -1; | |
5947 | if (copya > copyb) | |
5948 | return 1; | |
5949 | if (a->rela->r_offset < b->rela->r_offset) | |
5950 | return -1; | |
5951 | if (a->rela->r_offset > b->rela->r_offset) | |
5952 | return 1; | |
5953 | return 0; | |
5954 | } | |
5955 | ||
5956 | static size_t | |
5957 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
5958 | { | |
5959 | asection *reldyn; | |
5960 | bfd_size_type count, size; | |
5961 | size_t i, ret, sort_elt, ext_size; | |
5962 | bfd_byte *sort, *s_non_relative, *p; | |
5963 | struct elf_link_sort_rela *sq; | |
5964 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5965 | int i2e = bed->s->int_rels_per_ext_rel; | |
5966 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
5967 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
5968 | struct bfd_link_order *lo; | |
5969 | bfd_vma r_sym_mask; | |
5970 | ||
5971 | reldyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
eea6121a | 5972 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
5973 | { |
5974 | reldyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
eea6121a | 5975 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
5976 | return 0; |
5977 | ext_size = bed->s->sizeof_rel; | |
5978 | swap_in = bed->s->swap_reloc_in; | |
5979 | swap_out = bed->s->swap_reloc_out; | |
5980 | } | |
5981 | else | |
5982 | { | |
5983 | ext_size = bed->s->sizeof_rela; | |
5984 | swap_in = bed->s->swap_reloca_in; | |
5985 | swap_out = bed->s->swap_reloca_out; | |
5986 | } | |
eea6121a | 5987 | count = reldyn->size / ext_size; |
c152c796 AM |
5988 | |
5989 | size = 0; | |
8423293d | 5990 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
5991 | if (lo->type == bfd_indirect_link_order) |
5992 | { | |
5993 | asection *o = lo->u.indirect.section; | |
eea6121a | 5994 | size += o->size; |
c152c796 AM |
5995 | } |
5996 | ||
eea6121a | 5997 | if (size != reldyn->size) |
c152c796 AM |
5998 | return 0; |
5999 | ||
6000 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
6001 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
6002 | sort = bfd_zmalloc (sort_elt * count); | |
6003 | if (sort == NULL) | |
6004 | { | |
6005 | (*info->callbacks->warning) | |
6006 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
6007 | return 0; | |
6008 | } | |
6009 | ||
6010 | if (bed->s->arch_size == 32) | |
6011 | r_sym_mask = ~(bfd_vma) 0xff; | |
6012 | else | |
6013 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
6014 | ||
8423293d | 6015 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
6016 | if (lo->type == bfd_indirect_link_order) |
6017 | { | |
6018 | bfd_byte *erel, *erelend; | |
6019 | asection *o = lo->u.indirect.section; | |
6020 | ||
1da212d6 AM |
6021 | if (o->contents == NULL && o->size != 0) |
6022 | { | |
6023 | /* This is a reloc section that is being handled as a normal | |
6024 | section. See bfd_section_from_shdr. We can't combine | |
6025 | relocs in this case. */ | |
6026 | free (sort); | |
6027 | return 0; | |
6028 | } | |
c152c796 | 6029 | erel = o->contents; |
eea6121a | 6030 | erelend = o->contents + o->size; |
c152c796 AM |
6031 | p = sort + o->output_offset / ext_size * sort_elt; |
6032 | while (erel < erelend) | |
6033 | { | |
6034 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6035 | (*swap_in) (abfd, erel, s->rela); | |
6036 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
6037 | s->u.sym_mask = r_sym_mask; | |
6038 | p += sort_elt; | |
6039 | erel += ext_size; | |
6040 | } | |
6041 | } | |
6042 | ||
6043 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
6044 | ||
6045 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
6046 | { | |
6047 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6048 | if (s->type != reloc_class_relative) | |
6049 | break; | |
6050 | } | |
6051 | ret = i; | |
6052 | s_non_relative = p; | |
6053 | ||
6054 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
6055 | for (; i < count; i++, p += sort_elt) | |
6056 | { | |
6057 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
6058 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
6059 | sq = sp; | |
6060 | sp->u.offset = sq->rela->r_offset; | |
6061 | } | |
6062 | ||
6063 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
6064 | ||
8423293d | 6065 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
6066 | if (lo->type == bfd_indirect_link_order) |
6067 | { | |
6068 | bfd_byte *erel, *erelend; | |
6069 | asection *o = lo->u.indirect.section; | |
6070 | ||
6071 | erel = o->contents; | |
eea6121a | 6072 | erelend = o->contents + o->size; |
c152c796 AM |
6073 | p = sort + o->output_offset / ext_size * sort_elt; |
6074 | while (erel < erelend) | |
6075 | { | |
6076 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6077 | (*swap_out) (abfd, s->rela, erel); | |
6078 | p += sort_elt; | |
6079 | erel += ext_size; | |
6080 | } | |
6081 | } | |
6082 | ||
6083 | free (sort); | |
6084 | *psec = reldyn; | |
6085 | return ret; | |
6086 | } | |
6087 | ||
6088 | /* Flush the output symbols to the file. */ | |
6089 | ||
6090 | static bfd_boolean | |
6091 | elf_link_flush_output_syms (struct elf_final_link_info *finfo, | |
6092 | const struct elf_backend_data *bed) | |
6093 | { | |
6094 | if (finfo->symbuf_count > 0) | |
6095 | { | |
6096 | Elf_Internal_Shdr *hdr; | |
6097 | file_ptr pos; | |
6098 | bfd_size_type amt; | |
6099 | ||
6100 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
6101 | pos = hdr->sh_offset + hdr->sh_size; | |
6102 | amt = finfo->symbuf_count * bed->s->sizeof_sym; | |
6103 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
6104 | || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) | |
6105 | return FALSE; | |
6106 | ||
6107 | hdr->sh_size += amt; | |
6108 | finfo->symbuf_count = 0; | |
6109 | } | |
6110 | ||
6111 | return TRUE; | |
6112 | } | |
6113 | ||
6114 | /* Add a symbol to the output symbol table. */ | |
6115 | ||
6116 | static bfd_boolean | |
6117 | elf_link_output_sym (struct elf_final_link_info *finfo, | |
6118 | const char *name, | |
6119 | Elf_Internal_Sym *elfsym, | |
6120 | asection *input_sec, | |
6121 | struct elf_link_hash_entry *h) | |
6122 | { | |
6123 | bfd_byte *dest; | |
6124 | Elf_External_Sym_Shndx *destshndx; | |
6125 | bfd_boolean (*output_symbol_hook) | |
6126 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, | |
6127 | struct elf_link_hash_entry *); | |
6128 | const struct elf_backend_data *bed; | |
6129 | ||
6130 | bed = get_elf_backend_data (finfo->output_bfd); | |
6131 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; | |
6132 | if (output_symbol_hook != NULL) | |
6133 | { | |
6134 | if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h)) | |
6135 | return FALSE; | |
6136 | } | |
6137 | ||
6138 | if (name == NULL || *name == '\0') | |
6139 | elfsym->st_name = 0; | |
6140 | else if (input_sec->flags & SEC_EXCLUDE) | |
6141 | elfsym->st_name = 0; | |
6142 | else | |
6143 | { | |
6144 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
6145 | name, TRUE, FALSE); | |
6146 | if (elfsym->st_name == (unsigned long) -1) | |
6147 | return FALSE; | |
6148 | } | |
6149 | ||
6150 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
6151 | { | |
6152 | if (! elf_link_flush_output_syms (finfo, bed)) | |
6153 | return FALSE; | |
6154 | } | |
6155 | ||
6156 | dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; | |
6157 | destshndx = finfo->symshndxbuf; | |
6158 | if (destshndx != NULL) | |
6159 | { | |
6160 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
6161 | { | |
6162 | bfd_size_type amt; | |
6163 | ||
6164 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); | |
6165 | finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2); | |
6166 | if (destshndx == NULL) | |
6167 | return FALSE; | |
6168 | memset ((char *) destshndx + amt, 0, amt); | |
6169 | finfo->shndxbuf_size *= 2; | |
6170 | } | |
6171 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
6172 | } | |
6173 | ||
6174 | bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); | |
6175 | finfo->symbuf_count += 1; | |
6176 | bfd_get_symcount (finfo->output_bfd) += 1; | |
6177 | ||
6178 | return TRUE; | |
6179 | } | |
6180 | ||
6181 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in | |
6182 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
6183 | versioned symbol that would normally require an explicit version. | |
6184 | We also handle the case that a DSO references a hidden symbol | |
6185 | which may be satisfied by a versioned symbol in another DSO. */ | |
6186 | ||
6187 | static bfd_boolean | |
6188 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
6189 | const struct elf_backend_data *bed, | |
6190 | struct elf_link_hash_entry *h) | |
6191 | { | |
6192 | bfd *abfd; | |
6193 | struct elf_link_loaded_list *loaded; | |
6194 | ||
6195 | if (!is_elf_hash_table (info->hash)) | |
6196 | return FALSE; | |
6197 | ||
6198 | switch (h->root.type) | |
6199 | { | |
6200 | default: | |
6201 | abfd = NULL; | |
6202 | break; | |
6203 | ||
6204 | case bfd_link_hash_undefined: | |
6205 | case bfd_link_hash_undefweak: | |
6206 | abfd = h->root.u.undef.abfd; | |
6207 | if ((abfd->flags & DYNAMIC) == 0 | |
e56f61be | 6208 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
6209 | return FALSE; |
6210 | break; | |
6211 | ||
6212 | case bfd_link_hash_defined: | |
6213 | case bfd_link_hash_defweak: | |
6214 | abfd = h->root.u.def.section->owner; | |
6215 | break; | |
6216 | ||
6217 | case bfd_link_hash_common: | |
6218 | abfd = h->root.u.c.p->section->owner; | |
6219 | break; | |
6220 | } | |
6221 | BFD_ASSERT (abfd != NULL); | |
6222 | ||
6223 | for (loaded = elf_hash_table (info)->loaded; | |
6224 | loaded != NULL; | |
6225 | loaded = loaded->next) | |
6226 | { | |
6227 | bfd *input; | |
6228 | Elf_Internal_Shdr *hdr; | |
6229 | bfd_size_type symcount; | |
6230 | bfd_size_type extsymcount; | |
6231 | bfd_size_type extsymoff; | |
6232 | Elf_Internal_Shdr *versymhdr; | |
6233 | Elf_Internal_Sym *isym; | |
6234 | Elf_Internal_Sym *isymend; | |
6235 | Elf_Internal_Sym *isymbuf; | |
6236 | Elf_External_Versym *ever; | |
6237 | Elf_External_Versym *extversym; | |
6238 | ||
6239 | input = loaded->abfd; | |
6240 | ||
6241 | /* We check each DSO for a possible hidden versioned definition. */ | |
6242 | if (input == abfd | |
6243 | || (input->flags & DYNAMIC) == 0 | |
6244 | || elf_dynversym (input) == 0) | |
6245 | continue; | |
6246 | ||
6247 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
6248 | ||
6249 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
6250 | if (elf_bad_symtab (input)) | |
6251 | { | |
6252 | extsymcount = symcount; | |
6253 | extsymoff = 0; | |
6254 | } | |
6255 | else | |
6256 | { | |
6257 | extsymcount = symcount - hdr->sh_info; | |
6258 | extsymoff = hdr->sh_info; | |
6259 | } | |
6260 | ||
6261 | if (extsymcount == 0) | |
6262 | continue; | |
6263 | ||
6264 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
6265 | NULL, NULL, NULL); | |
6266 | if (isymbuf == NULL) | |
6267 | return FALSE; | |
6268 | ||
6269 | /* Read in any version definitions. */ | |
6270 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
6271 | extversym = bfd_malloc (versymhdr->sh_size); | |
6272 | if (extversym == NULL) | |
6273 | goto error_ret; | |
6274 | ||
6275 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
6276 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
6277 | != versymhdr->sh_size)) | |
6278 | { | |
6279 | free (extversym); | |
6280 | error_ret: | |
6281 | free (isymbuf); | |
6282 | return FALSE; | |
6283 | } | |
6284 | ||
6285 | ever = extversym + extsymoff; | |
6286 | isymend = isymbuf + extsymcount; | |
6287 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
6288 | { | |
6289 | const char *name; | |
6290 | Elf_Internal_Versym iver; | |
6291 | unsigned short version_index; | |
6292 | ||
6293 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
6294 | || isym->st_shndx == SHN_UNDEF) | |
6295 | continue; | |
6296 | ||
6297 | name = bfd_elf_string_from_elf_section (input, | |
6298 | hdr->sh_link, | |
6299 | isym->st_name); | |
6300 | if (strcmp (name, h->root.root.string) != 0) | |
6301 | continue; | |
6302 | ||
6303 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
6304 | ||
6305 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
6306 | { | |
6307 | /* If we have a non-hidden versioned sym, then it should | |
6308 | have provided a definition for the undefined sym. */ | |
6309 | abort (); | |
6310 | } | |
6311 | ||
6312 | version_index = iver.vs_vers & VERSYM_VERSION; | |
6313 | if (version_index == 1 || version_index == 2) | |
6314 | { | |
6315 | /* This is the base or first version. We can use it. */ | |
6316 | free (extversym); | |
6317 | free (isymbuf); | |
6318 | return TRUE; | |
6319 | } | |
6320 | } | |
6321 | ||
6322 | free (extversym); | |
6323 | free (isymbuf); | |
6324 | } | |
6325 | ||
6326 | return FALSE; | |
6327 | } | |
6328 | ||
6329 | /* Add an external symbol to the symbol table. This is called from | |
6330 | the hash table traversal routine. When generating a shared object, | |
6331 | we go through the symbol table twice. The first time we output | |
6332 | anything that might have been forced to local scope in a version | |
6333 | script. The second time we output the symbols that are still | |
6334 | global symbols. */ | |
6335 | ||
6336 | static bfd_boolean | |
6337 | elf_link_output_extsym (struct elf_link_hash_entry *h, void *data) | |
6338 | { | |
6339 | struct elf_outext_info *eoinfo = data; | |
6340 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
6341 | bfd_boolean strip; | |
6342 | Elf_Internal_Sym sym; | |
6343 | asection *input_sec; | |
6344 | const struct elf_backend_data *bed; | |
6345 | ||
6346 | if (h->root.type == bfd_link_hash_warning) | |
6347 | { | |
6348 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6349 | if (h->root.type == bfd_link_hash_new) | |
6350 | return TRUE; | |
6351 | } | |
6352 | ||
6353 | /* Decide whether to output this symbol in this pass. */ | |
6354 | if (eoinfo->localsyms) | |
6355 | { | |
f5385ebf | 6356 | if (!h->forced_local) |
c152c796 AM |
6357 | return TRUE; |
6358 | } | |
6359 | else | |
6360 | { | |
f5385ebf | 6361 | if (h->forced_local) |
c152c796 AM |
6362 | return TRUE; |
6363 | } | |
6364 | ||
6365 | bed = get_elf_backend_data (finfo->output_bfd); | |
6366 | ||
6367 | /* If we have an undefined symbol reference here then it must have | |
6368 | come from a shared library that is being linked in. (Undefined | |
6369 | references in regular files have already been handled). If we | |
6370 | are reporting errors for this situation then do so now. */ | |
6371 | if (h->root.type == bfd_link_hash_undefined | |
f5385ebf AM |
6372 | && h->ref_dynamic |
6373 | && !h->ref_regular | |
c152c796 AM |
6374 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h) |
6375 | && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
6376 | { | |
6377 | if (! ((*finfo->info->callbacks->undefined_symbol) | |
6378 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
6379 | NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) | |
6380 | { | |
6381 | eoinfo->failed = TRUE; | |
6382 | return FALSE; | |
6383 | } | |
6384 | } | |
6385 | ||
6386 | /* We should also warn if a forced local symbol is referenced from | |
6387 | shared libraries. */ | |
6388 | if (! finfo->info->relocatable | |
6389 | && (! finfo->info->shared) | |
f5385ebf AM |
6390 | && h->forced_local |
6391 | && h->ref_dynamic | |
6392 | && !h->dynamic_def | |
6393 | && !h->dynamic_weak | |
c152c796 AM |
6394 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) |
6395 | { | |
6396 | (*_bfd_error_handler) | |
d003868e | 6397 | (_("%B: %s symbol `%s' in %B is referenced by DSO"), |
cfca085c L |
6398 | finfo->output_bfd, |
6399 | h->root.u.def.section == bfd_abs_section_ptr | |
6400 | ? finfo->output_bfd : h->root.u.def.section->owner, | |
c152c796 AM |
6401 | ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
6402 | ? "internal" | |
6403 | : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
d003868e AM |
6404 | ? "hidden" : "local", |
6405 | h->root.root.string); | |
c152c796 AM |
6406 | eoinfo->failed = TRUE; |
6407 | return FALSE; | |
6408 | } | |
6409 | ||
6410 | /* We don't want to output symbols that have never been mentioned by | |
6411 | a regular file, or that we have been told to strip. However, if | |
6412 | h->indx is set to -2, the symbol is used by a reloc and we must | |
6413 | output it. */ | |
6414 | if (h->indx == -2) | |
6415 | strip = FALSE; | |
f5385ebf | 6416 | else if ((h->def_dynamic |
77cfaee6 AM |
6417 | || h->ref_dynamic |
6418 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
6419 | && !h->def_regular |
6420 | && !h->ref_regular) | |
c152c796 AM |
6421 | strip = TRUE; |
6422 | else if (finfo->info->strip == strip_all) | |
6423 | strip = TRUE; | |
6424 | else if (finfo->info->strip == strip_some | |
6425 | && bfd_hash_lookup (finfo->info->keep_hash, | |
6426 | h->root.root.string, FALSE, FALSE) == NULL) | |
6427 | strip = TRUE; | |
6428 | else if (finfo->info->strip_discarded | |
6429 | && (h->root.type == bfd_link_hash_defined | |
6430 | || h->root.type == bfd_link_hash_defweak) | |
6431 | && elf_discarded_section (h->root.u.def.section)) | |
6432 | strip = TRUE; | |
6433 | else | |
6434 | strip = FALSE; | |
6435 | ||
6436 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
6437 | nothing else to do unless it is a forced local symbol. */ | |
6438 | if (strip | |
6439 | && h->dynindx == -1 | |
f5385ebf | 6440 | && !h->forced_local) |
c152c796 AM |
6441 | return TRUE; |
6442 | ||
6443 | sym.st_value = 0; | |
6444 | sym.st_size = h->size; | |
6445 | sym.st_other = h->other; | |
f5385ebf | 6446 | if (h->forced_local) |
c152c796 AM |
6447 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); |
6448 | else if (h->root.type == bfd_link_hash_undefweak | |
6449 | || h->root.type == bfd_link_hash_defweak) | |
6450 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
6451 | else | |
6452 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
6453 | ||
6454 | switch (h->root.type) | |
6455 | { | |
6456 | default: | |
6457 | case bfd_link_hash_new: | |
6458 | case bfd_link_hash_warning: | |
6459 | abort (); | |
6460 | return FALSE; | |
6461 | ||
6462 | case bfd_link_hash_undefined: | |
6463 | case bfd_link_hash_undefweak: | |
6464 | input_sec = bfd_und_section_ptr; | |
6465 | sym.st_shndx = SHN_UNDEF; | |
6466 | break; | |
6467 | ||
6468 | case bfd_link_hash_defined: | |
6469 | case bfd_link_hash_defweak: | |
6470 | { | |
6471 | input_sec = h->root.u.def.section; | |
6472 | if (input_sec->output_section != NULL) | |
6473 | { | |
6474 | sym.st_shndx = | |
6475 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
6476 | input_sec->output_section); | |
6477 | if (sym.st_shndx == SHN_BAD) | |
6478 | { | |
6479 | (*_bfd_error_handler) | |
d003868e AM |
6480 | (_("%B: could not find output section %A for input section %A"), |
6481 | finfo->output_bfd, input_sec->output_section, input_sec); | |
c152c796 AM |
6482 | eoinfo->failed = TRUE; |
6483 | return FALSE; | |
6484 | } | |
6485 | ||
6486 | /* ELF symbols in relocatable files are section relative, | |
6487 | but in nonrelocatable files they are virtual | |
6488 | addresses. */ | |
6489 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
6490 | if (! finfo->info->relocatable) | |
6491 | { | |
6492 | sym.st_value += input_sec->output_section->vma; | |
6493 | if (h->type == STT_TLS) | |
6494 | { | |
6495 | /* STT_TLS symbols are relative to PT_TLS segment | |
6496 | base. */ | |
6497 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
6498 | sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
6499 | } | |
6500 | } | |
6501 | } | |
6502 | else | |
6503 | { | |
6504 | BFD_ASSERT (input_sec->owner == NULL | |
6505 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
6506 | sym.st_shndx = SHN_UNDEF; | |
6507 | input_sec = bfd_und_section_ptr; | |
6508 | } | |
6509 | } | |
6510 | break; | |
6511 | ||
6512 | case bfd_link_hash_common: | |
6513 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 6514 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
6515 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
6516 | break; | |
6517 | ||
6518 | case bfd_link_hash_indirect: | |
6519 | /* These symbols are created by symbol versioning. They point | |
6520 | to the decorated version of the name. For example, if the | |
6521 | symbol foo@@GNU_1.2 is the default, which should be used when | |
6522 | foo is used with no version, then we add an indirect symbol | |
6523 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
6524 | since the indirected symbol is already in the hash table. */ | |
6525 | return TRUE; | |
6526 | } | |
6527 | ||
6528 | /* Give the processor backend a chance to tweak the symbol value, | |
6529 | and also to finish up anything that needs to be done for this | |
6530 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
6531 | forced local syms when non-shared is due to a historical quirk. */ | |
6532 | if ((h->dynindx != -1 | |
f5385ebf | 6533 | || h->forced_local) |
c152c796 AM |
6534 | && ((finfo->info->shared |
6535 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
6536 | || h->root.type != bfd_link_hash_undefweak)) | |
f5385ebf | 6537 | || !h->forced_local) |
c152c796 AM |
6538 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
6539 | { | |
6540 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
6541 | (finfo->output_bfd, finfo->info, h, &sym))) | |
6542 | { | |
6543 | eoinfo->failed = TRUE; | |
6544 | return FALSE; | |
6545 | } | |
6546 | } | |
6547 | ||
6548 | /* If we are marking the symbol as undefined, and there are no | |
6549 | non-weak references to this symbol from a regular object, then | |
6550 | mark the symbol as weak undefined; if there are non-weak | |
6551 | references, mark the symbol as strong. We can't do this earlier, | |
6552 | because it might not be marked as undefined until the | |
6553 | finish_dynamic_symbol routine gets through with it. */ | |
6554 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 6555 | && h->ref_regular |
c152c796 AM |
6556 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
6557 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
6558 | { | |
6559 | int bindtype; | |
6560 | ||
f5385ebf | 6561 | if (h->ref_regular_nonweak) |
c152c796 AM |
6562 | bindtype = STB_GLOBAL; |
6563 | else | |
6564 | bindtype = STB_WEAK; | |
6565 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); | |
6566 | } | |
6567 | ||
6568 | /* If a non-weak symbol with non-default visibility is not defined | |
6569 | locally, it is a fatal error. */ | |
6570 | if (! finfo->info->relocatable | |
6571 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT | |
6572 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
6573 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 6574 | && !h->def_regular) |
c152c796 AM |
6575 | { |
6576 | (*_bfd_error_handler) | |
d003868e AM |
6577 | (_("%B: %s symbol `%s' isn't defined"), |
6578 | finfo->output_bfd, | |
6579 | ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED | |
6580 | ? "protected" | |
6581 | : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL | |
6582 | ? "internal" : "hidden", | |
6583 | h->root.root.string); | |
c152c796 AM |
6584 | eoinfo->failed = TRUE; |
6585 | return FALSE; | |
6586 | } | |
6587 | ||
6588 | /* If this symbol should be put in the .dynsym section, then put it | |
6589 | there now. We already know the symbol index. We also fill in | |
6590 | the entry in the .hash section. */ | |
6591 | if (h->dynindx != -1 | |
6592 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
6593 | { | |
6594 | size_t bucketcount; | |
6595 | size_t bucket; | |
6596 | size_t hash_entry_size; | |
6597 | bfd_byte *bucketpos; | |
6598 | bfd_vma chain; | |
6599 | bfd_byte *esym; | |
6600 | ||
6601 | sym.st_name = h->dynstr_index; | |
6602 | esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; | |
6603 | bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); | |
6604 | ||
6605 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
f6e332e6 | 6606 | bucket = h->u.elf_hash_value % bucketcount; |
c152c796 AM |
6607 | hash_entry_size |
6608 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; | |
6609 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents | |
6610 | + (bucket + 2) * hash_entry_size); | |
6611 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
6612 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); | |
6613 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, | |
6614 | ((bfd_byte *) finfo->hash_sec->contents | |
6615 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
6616 | ||
6617 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
6618 | { | |
6619 | Elf_Internal_Versym iversym; | |
6620 | Elf_External_Versym *eversym; | |
6621 | ||
f5385ebf | 6622 | if (!h->def_regular) |
c152c796 AM |
6623 | { |
6624 | if (h->verinfo.verdef == NULL) | |
6625 | iversym.vs_vers = 0; | |
6626 | else | |
6627 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
6628 | } | |
6629 | else | |
6630 | { | |
6631 | if (h->verinfo.vertree == NULL) | |
6632 | iversym.vs_vers = 1; | |
6633 | else | |
6634 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
3e3b46e5 PB |
6635 | if (finfo->info->create_default_symver) |
6636 | iversym.vs_vers++; | |
c152c796 AM |
6637 | } |
6638 | ||
f5385ebf | 6639 | if (h->hidden) |
c152c796 AM |
6640 | iversym.vs_vers |= VERSYM_HIDDEN; |
6641 | ||
6642 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; | |
6643 | eversym += h->dynindx; | |
6644 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
6645 | } | |
6646 | } | |
6647 | ||
6648 | /* If we're stripping it, then it was just a dynamic symbol, and | |
6649 | there's nothing else to do. */ | |
6650 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) | |
6651 | return TRUE; | |
6652 | ||
6653 | h->indx = bfd_get_symcount (finfo->output_bfd); | |
6654 | ||
6655 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h)) | |
6656 | { | |
6657 | eoinfo->failed = TRUE; | |
6658 | return FALSE; | |
6659 | } | |
6660 | ||
6661 | return TRUE; | |
6662 | } | |
6663 | ||
cdd3575c AM |
6664 | /* Return TRUE if special handling is done for relocs in SEC against |
6665 | symbols defined in discarded sections. */ | |
6666 | ||
c152c796 AM |
6667 | static bfd_boolean |
6668 | elf_section_ignore_discarded_relocs (asection *sec) | |
6669 | { | |
6670 | const struct elf_backend_data *bed; | |
6671 | ||
cdd3575c AM |
6672 | switch (sec->sec_info_type) |
6673 | { | |
6674 | case ELF_INFO_TYPE_STABS: | |
6675 | case ELF_INFO_TYPE_EH_FRAME: | |
6676 | return TRUE; | |
6677 | default: | |
6678 | break; | |
6679 | } | |
c152c796 AM |
6680 | |
6681 | bed = get_elf_backend_data (sec->owner); | |
6682 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
6683 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
6684 | return TRUE; | |
6685 | ||
6686 | return FALSE; | |
6687 | } | |
6688 | ||
9e66c942 AM |
6689 | /* Return a mask saying how ld should treat relocations in SEC against |
6690 | symbols defined in discarded sections. If this function returns | |
6691 | COMPLAIN set, ld will issue a warning message. If this function | |
6692 | returns PRETEND set, and the discarded section was link-once and the | |
6693 | same size as the kept link-once section, ld will pretend that the | |
6694 | symbol was actually defined in the kept section. Otherwise ld will | |
6695 | zero the reloc (at least that is the intent, but some cooperation by | |
6696 | the target dependent code is needed, particularly for REL targets). */ | |
6697 | ||
8a696751 AM |
6698 | unsigned int |
6699 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 6700 | { |
9e66c942 AM |
6701 | if (sec->flags & SEC_DEBUGGING) |
6702 | return PRETEND; | |
cdd3575c AM |
6703 | |
6704 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 6705 | return 0; |
cdd3575c AM |
6706 | |
6707 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 6708 | return 0; |
cdd3575c | 6709 | |
9e66c942 | 6710 | return COMPLAIN | PRETEND; |
cdd3575c AM |
6711 | } |
6712 | ||
3d7f7666 L |
6713 | /* Find a match between a section and a member of a section group. */ |
6714 | ||
6715 | static asection * | |
6716 | match_group_member (asection *sec, asection *group) | |
6717 | { | |
6718 | asection *first = elf_next_in_group (group); | |
6719 | asection *s = first; | |
6720 | ||
6721 | while (s != NULL) | |
6722 | { | |
6723 | if (bfd_elf_match_symbols_in_sections (s, sec)) | |
6724 | return s; | |
6725 | ||
6726 | if (s == first) | |
6727 | break; | |
6728 | } | |
6729 | ||
6730 | return NULL; | |
6731 | } | |
6732 | ||
01b3c8ab L |
6733 | /* Check if the kept section of a discarded section SEC can be used |
6734 | to replace it. Return the replacement if it is OK. Otherwise return | |
6735 | NULL. */ | |
6736 | ||
6737 | asection * | |
6738 | _bfd_elf_check_kept_section (asection *sec) | |
6739 | { | |
6740 | asection *kept; | |
6741 | ||
6742 | kept = sec->kept_section; | |
6743 | if (kept != NULL) | |
6744 | { | |
6745 | if (elf_sec_group (sec) != NULL) | |
6746 | kept = match_group_member (sec, kept); | |
6747 | if (kept != NULL && sec->size != kept->size) | |
6748 | kept = NULL; | |
6749 | } | |
6750 | return kept; | |
6751 | } | |
6752 | ||
c152c796 AM |
6753 | /* Link an input file into the linker output file. This function |
6754 | handles all the sections and relocations of the input file at once. | |
6755 | This is so that we only have to read the local symbols once, and | |
6756 | don't have to keep them in memory. */ | |
6757 | ||
6758 | static bfd_boolean | |
6759 | elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) | |
6760 | { | |
6761 | bfd_boolean (*relocate_section) | |
6762 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
6763 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
6764 | bfd *output_bfd; | |
6765 | Elf_Internal_Shdr *symtab_hdr; | |
6766 | size_t locsymcount; | |
6767 | size_t extsymoff; | |
6768 | Elf_Internal_Sym *isymbuf; | |
6769 | Elf_Internal_Sym *isym; | |
6770 | Elf_Internal_Sym *isymend; | |
6771 | long *pindex; | |
6772 | asection **ppsection; | |
6773 | asection *o; | |
6774 | const struct elf_backend_data *bed; | |
6775 | bfd_boolean emit_relocs; | |
6776 | struct elf_link_hash_entry **sym_hashes; | |
6777 | ||
6778 | output_bfd = finfo->output_bfd; | |
6779 | bed = get_elf_backend_data (output_bfd); | |
6780 | relocate_section = bed->elf_backend_relocate_section; | |
6781 | ||
6782 | /* If this is a dynamic object, we don't want to do anything here: | |
6783 | we don't want the local symbols, and we don't want the section | |
6784 | contents. */ | |
6785 | if ((input_bfd->flags & DYNAMIC) != 0) | |
6786 | return TRUE; | |
6787 | ||
6788 | emit_relocs = (finfo->info->relocatable | |
eac338cf | 6789 | || finfo->info->emitrelocations); |
c152c796 AM |
6790 | |
6791 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
6792 | if (elf_bad_symtab (input_bfd)) | |
6793 | { | |
6794 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
6795 | extsymoff = 0; | |
6796 | } | |
6797 | else | |
6798 | { | |
6799 | locsymcount = symtab_hdr->sh_info; | |
6800 | extsymoff = symtab_hdr->sh_info; | |
6801 | } | |
6802 | ||
6803 | /* Read the local symbols. */ | |
6804 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
6805 | if (isymbuf == NULL && locsymcount != 0) | |
6806 | { | |
6807 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
6808 | finfo->internal_syms, | |
6809 | finfo->external_syms, | |
6810 | finfo->locsym_shndx); | |
6811 | if (isymbuf == NULL) | |
6812 | return FALSE; | |
6813 | } | |
6814 | ||
6815 | /* Find local symbol sections and adjust values of symbols in | |
6816 | SEC_MERGE sections. Write out those local symbols we know are | |
6817 | going into the output file. */ | |
6818 | isymend = isymbuf + locsymcount; | |
6819 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
6820 | isym < isymend; | |
6821 | isym++, pindex++, ppsection++) | |
6822 | { | |
6823 | asection *isec; | |
6824 | const char *name; | |
6825 | Elf_Internal_Sym osym; | |
6826 | ||
6827 | *pindex = -1; | |
6828 | ||
6829 | if (elf_bad_symtab (input_bfd)) | |
6830 | { | |
6831 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
6832 | { | |
6833 | *ppsection = NULL; | |
6834 | continue; | |
6835 | } | |
6836 | } | |
6837 | ||
6838 | if (isym->st_shndx == SHN_UNDEF) | |
6839 | isec = bfd_und_section_ptr; | |
6840 | else if (isym->st_shndx < SHN_LORESERVE | |
6841 | || isym->st_shndx > SHN_HIRESERVE) | |
6842 | { | |
6843 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); | |
6844 | if (isec | |
6845 | && isec->sec_info_type == ELF_INFO_TYPE_MERGE | |
6846 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) | |
6847 | isym->st_value = | |
6848 | _bfd_merged_section_offset (output_bfd, &isec, | |
6849 | elf_section_data (isec)->sec_info, | |
753731ee | 6850 | isym->st_value); |
c152c796 AM |
6851 | } |
6852 | else if (isym->st_shndx == SHN_ABS) | |
6853 | isec = bfd_abs_section_ptr; | |
6854 | else if (isym->st_shndx == SHN_COMMON) | |
6855 | isec = bfd_com_section_ptr; | |
6856 | else | |
6857 | { | |
6858 | /* Who knows? */ | |
6859 | isec = NULL; | |
6860 | } | |
6861 | ||
6862 | *ppsection = isec; | |
6863 | ||
6864 | /* Don't output the first, undefined, symbol. */ | |
6865 | if (ppsection == finfo->sections) | |
6866 | continue; | |
6867 | ||
6868 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
6869 | { | |
6870 | /* We never output section symbols. Instead, we use the | |
6871 | section symbol of the corresponding section in the output | |
6872 | file. */ | |
6873 | continue; | |
6874 | } | |
6875 | ||
6876 | /* If we are stripping all symbols, we don't want to output this | |
6877 | one. */ | |
6878 | if (finfo->info->strip == strip_all) | |
6879 | continue; | |
6880 | ||
6881 | /* If we are discarding all local symbols, we don't want to | |
6882 | output this one. If we are generating a relocatable output | |
6883 | file, then some of the local symbols may be required by | |
6884 | relocs; we output them below as we discover that they are | |
6885 | needed. */ | |
6886 | if (finfo->info->discard == discard_all) | |
6887 | continue; | |
6888 | ||
6889 | /* If this symbol is defined in a section which we are | |
6890 | discarding, we don't need to keep it, but note that | |
6891 | linker_mark is only reliable for sections that have contents. | |
6892 | For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE | |
6893 | as well as linker_mark. */ | |
6894 | if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
ccf5f610 PB |
6895 | && (isec == NULL |
6896 | || (! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0) | |
c152c796 AM |
6897 | || (! finfo->info->relocatable |
6898 | && (isec->flags & SEC_EXCLUDE) != 0))) | |
6899 | continue; | |
6900 | ||
e75a280b L |
6901 | /* If the section is not in the output BFD's section list, it is not |
6902 | being output. */ | |
6903 | if (bfd_section_removed_from_list (output_bfd, isec->output_section)) | |
6904 | continue; | |
6905 | ||
c152c796 AM |
6906 | /* Get the name of the symbol. */ |
6907 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
6908 | isym->st_name); | |
6909 | if (name == NULL) | |
6910 | return FALSE; | |
6911 | ||
6912 | /* See if we are discarding symbols with this name. */ | |
6913 | if ((finfo->info->strip == strip_some | |
6914 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) | |
6915 | == NULL)) | |
6916 | || (((finfo->info->discard == discard_sec_merge | |
6917 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) | |
6918 | || finfo->info->discard == discard_l) | |
6919 | && bfd_is_local_label_name (input_bfd, name))) | |
6920 | continue; | |
6921 | ||
6922 | /* If we get here, we are going to output this symbol. */ | |
6923 | ||
6924 | osym = *isym; | |
6925 | ||
6926 | /* Adjust the section index for the output file. */ | |
6927 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
6928 | isec->output_section); | |
6929 | if (osym.st_shndx == SHN_BAD) | |
6930 | return FALSE; | |
6931 | ||
6932 | *pindex = bfd_get_symcount (output_bfd); | |
6933 | ||
6934 | /* ELF symbols in relocatable files are section relative, but | |
6935 | in executable files they are virtual addresses. Note that | |
6936 | this code assumes that all ELF sections have an associated | |
6937 | BFD section with a reasonable value for output_offset; below | |
6938 | we assume that they also have a reasonable value for | |
6939 | output_section. Any special sections must be set up to meet | |
6940 | these requirements. */ | |
6941 | osym.st_value += isec->output_offset; | |
6942 | if (! finfo->info->relocatable) | |
6943 | { | |
6944 | osym.st_value += isec->output_section->vma; | |
6945 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
6946 | { | |
6947 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
6948 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
6949 | osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
6950 | } | |
6951 | } | |
6952 | ||
6953 | if (! elf_link_output_sym (finfo, name, &osym, isec, NULL)) | |
6954 | return FALSE; | |
6955 | } | |
6956 | ||
6957 | /* Relocate the contents of each section. */ | |
6958 | sym_hashes = elf_sym_hashes (input_bfd); | |
6959 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
6960 | { | |
6961 | bfd_byte *contents; | |
6962 | ||
6963 | if (! o->linker_mark) | |
6964 | { | |
6965 | /* This section was omitted from the link. */ | |
6966 | continue; | |
6967 | } | |
6968 | ||
6969 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 6970 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
6971 | continue; |
6972 | ||
6973 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
6974 | { | |
6975 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
6976 | or somesuch. */ | |
6977 | continue; | |
6978 | } | |
6979 | ||
6980 | /* Get the contents of the section. They have been cached by a | |
6981 | relaxation routine. Note that o is a section in an input | |
6982 | file, so the contents field will not have been set by any of | |
6983 | the routines which work on output files. */ | |
6984 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
6985 | contents = elf_section_data (o)->this_hdr.contents; | |
6986 | else | |
6987 | { | |
eea6121a AM |
6988 | bfd_size_type amt = o->rawsize ? o->rawsize : o->size; |
6989 | ||
c152c796 | 6990 | contents = finfo->contents; |
eea6121a | 6991 | if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt)) |
c152c796 AM |
6992 | return FALSE; |
6993 | } | |
6994 | ||
6995 | if ((o->flags & SEC_RELOC) != 0) | |
6996 | { | |
6997 | Elf_Internal_Rela *internal_relocs; | |
6998 | bfd_vma r_type_mask; | |
6999 | int r_sym_shift; | |
7000 | ||
7001 | /* Get the swapped relocs. */ | |
7002 | internal_relocs | |
7003 | = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, | |
7004 | finfo->internal_relocs, FALSE); | |
7005 | if (internal_relocs == NULL | |
7006 | && o->reloc_count > 0) | |
7007 | return FALSE; | |
7008 | ||
7009 | if (bed->s->arch_size == 32) | |
7010 | { | |
7011 | r_type_mask = 0xff; | |
7012 | r_sym_shift = 8; | |
7013 | } | |
7014 | else | |
7015 | { | |
7016 | r_type_mask = 0xffffffff; | |
7017 | r_sym_shift = 32; | |
7018 | } | |
7019 | ||
7020 | /* Run through the relocs looking for any against symbols | |
7021 | from discarded sections and section symbols from | |
7022 | removed link-once sections. Complain about relocs | |
7023 | against discarded sections. Zero relocs against removed | |
7024 | link-once sections. Preserve debug information as much | |
7025 | as we can. */ | |
7026 | if (!elf_section_ignore_discarded_relocs (o)) | |
7027 | { | |
7028 | Elf_Internal_Rela *rel, *relend; | |
8a696751 | 7029 | unsigned int action = (*bed->action_discarded) (o); |
c152c796 AM |
7030 | |
7031 | rel = internal_relocs; | |
7032 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
7033 | for ( ; rel < relend; rel++) | |
7034 | { | |
7035 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
cdd3575c AM |
7036 | asection **ps, *sec; |
7037 | struct elf_link_hash_entry *h = NULL; | |
7038 | const char *sym_name; | |
c152c796 | 7039 | |
ee75fd95 AM |
7040 | if (r_symndx == STN_UNDEF) |
7041 | continue; | |
7042 | ||
c152c796 AM |
7043 | if (r_symndx >= locsymcount |
7044 | || (elf_bad_symtab (input_bfd) | |
7045 | && finfo->sections[r_symndx] == NULL)) | |
7046 | { | |
c152c796 | 7047 | h = sym_hashes[r_symndx - extsymoff]; |
dce669a1 | 7048 | |
8c19749a NC |
7049 | /* Badly formatted input files can contain relocs that |
7050 | reference non-existant symbols. Check here so that | |
7051 | we do not seg fault. */ | |
7052 | if (h == NULL) | |
7053 | { | |
7054 | char buffer [32]; | |
7055 | ||
7056 | sprintf_vma (buffer, rel->r_info); | |
7057 | (*_bfd_error_handler) | |
7058 | (_("error: %B contains a reloc (0x%s) for section %A " | |
7059 | "that references a non-existent global symbol"), | |
7060 | input_bfd, o, buffer); | |
7061 | bfd_set_error (bfd_error_bad_value); | |
7062 | return FALSE; | |
7063 | } | |
3b36f7e6 | 7064 | |
c152c796 AM |
7065 | while (h->root.type == bfd_link_hash_indirect |
7066 | || h->root.type == bfd_link_hash_warning) | |
7067 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7068 | ||
cdd3575c AM |
7069 | if (h->root.type != bfd_link_hash_defined |
7070 | && h->root.type != bfd_link_hash_defweak) | |
7071 | continue; | |
7072 | ||
7073 | ps = &h->root.u.def.section; | |
7074 | sym_name = h->root.root.string; | |
c152c796 AM |
7075 | } |
7076 | else | |
7077 | { | |
cdd3575c AM |
7078 | Elf_Internal_Sym *sym = isymbuf + r_symndx; |
7079 | ps = &finfo->sections[r_symndx]; | |
26c61ae5 L |
7080 | sym_name = bfd_elf_sym_name (input_bfd, |
7081 | symtab_hdr, | |
7082 | sym, *ps); | |
cdd3575c | 7083 | } |
c152c796 | 7084 | |
cdd3575c AM |
7085 | /* Complain if the definition comes from a |
7086 | discarded section. */ | |
7087 | if ((sec = *ps) != NULL && elf_discarded_section (sec)) | |
7088 | { | |
87e5235d | 7089 | BFD_ASSERT (r_symndx != 0); |
9e66c942 | 7090 | if (action & COMPLAIN) |
e1fffbe6 AM |
7091 | (*finfo->info->callbacks->einfo) |
7092 | (_("%X`%s' referenced in section `%A' of %B: " | |
58ac56d0 | 7093 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 7094 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 7095 | |
87e5235d AM |
7096 | /* Try to do the best we can to support buggy old |
7097 | versions of gcc. If we've warned, or this is | |
7098 | debugging info, pretend that the symbol is | |
7099 | really defined in the kept linkonce section. | |
7100 | FIXME: This is quite broken. Modifying the | |
7101 | symbol here means we will be changing all later | |
7102 | uses of the symbol, not just in this section. | |
7103 | The only thing that makes this half reasonable | |
7104 | is that we warn in non-debug sections, and | |
7105 | debug sections tend to come after other | |
7106 | sections. */ | |
01b3c8ab | 7107 | if (action & PRETEND) |
87e5235d | 7108 | { |
01b3c8ab L |
7109 | asection *kept; |
7110 | ||
7111 | kept = _bfd_elf_check_kept_section (sec); | |
7112 | if (kept != NULL) | |
87e5235d AM |
7113 | { |
7114 | *ps = kept; | |
7115 | continue; | |
7116 | } | |
7117 | } | |
7118 | ||
cdd3575c AM |
7119 | /* Remove the symbol reference from the reloc, but |
7120 | don't kill the reloc completely. This is so that | |
7121 | a zero value will be written into the section, | |
7122 | which may have non-zero contents put there by the | |
7123 | assembler. Zero in things like an eh_frame fde | |
7124 | pc_begin allows stack unwinders to recognize the | |
7125 | fde as bogus. */ | |
7126 | rel->r_info &= r_type_mask; | |
7127 | rel->r_addend = 0; | |
c152c796 AM |
7128 | } |
7129 | } | |
7130 | } | |
7131 | ||
7132 | /* Relocate the section by invoking a back end routine. | |
7133 | ||
7134 | The back end routine is responsible for adjusting the | |
7135 | section contents as necessary, and (if using Rela relocs | |
7136 | and generating a relocatable output file) adjusting the | |
7137 | reloc addend as necessary. | |
7138 | ||
7139 | The back end routine does not have to worry about setting | |
7140 | the reloc address or the reloc symbol index. | |
7141 | ||
7142 | The back end routine is given a pointer to the swapped in | |
7143 | internal symbols, and can access the hash table entries | |
7144 | for the external symbols via elf_sym_hashes (input_bfd). | |
7145 | ||
7146 | When generating relocatable output, the back end routine | |
7147 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
7148 | output symbol is going to be a section symbol | |
7149 | corresponding to the output section, which will require | |
7150 | the addend to be adjusted. */ | |
7151 | ||
7152 | if (! (*relocate_section) (output_bfd, finfo->info, | |
7153 | input_bfd, o, contents, | |
7154 | internal_relocs, | |
7155 | isymbuf, | |
7156 | finfo->sections)) | |
7157 | return FALSE; | |
7158 | ||
7159 | if (emit_relocs) | |
7160 | { | |
7161 | Elf_Internal_Rela *irela; | |
7162 | Elf_Internal_Rela *irelaend; | |
7163 | bfd_vma last_offset; | |
7164 | struct elf_link_hash_entry **rel_hash; | |
eac338cf | 7165 | struct elf_link_hash_entry **rel_hash_list; |
c152c796 AM |
7166 | Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2; |
7167 | unsigned int next_erel; | |
c152c796 AM |
7168 | bfd_boolean rela_normal; |
7169 | ||
7170 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
7171 | rela_normal = (bed->rela_normal | |
7172 | && (input_rel_hdr->sh_entsize | |
7173 | == bed->s->sizeof_rela)); | |
7174 | ||
7175 | /* Adjust the reloc addresses and symbol indices. */ | |
7176 | ||
7177 | irela = internal_relocs; | |
7178 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
7179 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
7180 | + elf_section_data (o->output_section)->rel_count | |
7181 | + elf_section_data (o->output_section)->rel_count2); | |
eac338cf | 7182 | rel_hash_list = rel_hash; |
c152c796 AM |
7183 | last_offset = o->output_offset; |
7184 | if (!finfo->info->relocatable) | |
7185 | last_offset += o->output_section->vma; | |
7186 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
7187 | { | |
7188 | unsigned long r_symndx; | |
7189 | asection *sec; | |
7190 | Elf_Internal_Sym sym; | |
7191 | ||
7192 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
7193 | { | |
7194 | rel_hash++; | |
7195 | next_erel = 0; | |
7196 | } | |
7197 | ||
7198 | irela->r_offset = _bfd_elf_section_offset (output_bfd, | |
7199 | finfo->info, o, | |
7200 | irela->r_offset); | |
7201 | if (irela->r_offset >= (bfd_vma) -2) | |
7202 | { | |
7203 | /* This is a reloc for a deleted entry or somesuch. | |
7204 | Turn it into an R_*_NONE reloc, at the same | |
7205 | offset as the last reloc. elf_eh_frame.c and | |
7206 | elf_bfd_discard_info rely on reloc offsets | |
7207 | being ordered. */ | |
7208 | irela->r_offset = last_offset; | |
7209 | irela->r_info = 0; | |
7210 | irela->r_addend = 0; | |
7211 | continue; | |
7212 | } | |
7213 | ||
7214 | irela->r_offset += o->output_offset; | |
7215 | ||
7216 | /* Relocs in an executable have to be virtual addresses. */ | |
7217 | if (!finfo->info->relocatable) | |
7218 | irela->r_offset += o->output_section->vma; | |
7219 | ||
7220 | last_offset = irela->r_offset; | |
7221 | ||
7222 | r_symndx = irela->r_info >> r_sym_shift; | |
7223 | if (r_symndx == STN_UNDEF) | |
7224 | continue; | |
7225 | ||
7226 | if (r_symndx >= locsymcount | |
7227 | || (elf_bad_symtab (input_bfd) | |
7228 | && finfo->sections[r_symndx] == NULL)) | |
7229 | { | |
7230 | struct elf_link_hash_entry *rh; | |
7231 | unsigned long indx; | |
7232 | ||
7233 | /* This is a reloc against a global symbol. We | |
7234 | have not yet output all the local symbols, so | |
7235 | we do not know the symbol index of any global | |
7236 | symbol. We set the rel_hash entry for this | |
7237 | reloc to point to the global hash table entry | |
7238 | for this symbol. The symbol index is then | |
ee75fd95 | 7239 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
7240 | indx = r_symndx - extsymoff; |
7241 | rh = elf_sym_hashes (input_bfd)[indx]; | |
7242 | while (rh->root.type == bfd_link_hash_indirect | |
7243 | || rh->root.type == bfd_link_hash_warning) | |
7244 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
7245 | ||
7246 | /* Setting the index to -2 tells | |
7247 | elf_link_output_extsym that this symbol is | |
7248 | used by a reloc. */ | |
7249 | BFD_ASSERT (rh->indx < 0); | |
7250 | rh->indx = -2; | |
7251 | ||
7252 | *rel_hash = rh; | |
7253 | ||
7254 | continue; | |
7255 | } | |
7256 | ||
7257 | /* This is a reloc against a local symbol. */ | |
7258 | ||
7259 | *rel_hash = NULL; | |
7260 | sym = isymbuf[r_symndx]; | |
7261 | sec = finfo->sections[r_symndx]; | |
7262 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) | |
7263 | { | |
7264 | /* I suppose the backend ought to fill in the | |
7265 | section of any STT_SECTION symbol against a | |
6a8d1586 AM |
7266 | processor specific section. */ |
7267 | r_symndx = 0; | |
7268 | if (bfd_is_abs_section (sec)) | |
7269 | ; | |
c152c796 AM |
7270 | else if (sec == NULL || sec->owner == NULL) |
7271 | { | |
7272 | bfd_set_error (bfd_error_bad_value); | |
7273 | return FALSE; | |
7274 | } | |
7275 | else | |
7276 | { | |
6a8d1586 AM |
7277 | asection *osec = sec->output_section; |
7278 | ||
7279 | /* If we have discarded a section, the output | |
7280 | section will be the absolute section. In | |
7281 | case of discarded link-once and discarded | |
7282 | SEC_MERGE sections, use the kept section. */ | |
7283 | if (bfd_is_abs_section (osec) | |
7284 | && sec->kept_section != NULL | |
7285 | && sec->kept_section->output_section != NULL) | |
7286 | { | |
7287 | osec = sec->kept_section->output_section; | |
7288 | irela->r_addend -= osec->vma; | |
7289 | } | |
7290 | ||
7291 | if (!bfd_is_abs_section (osec)) | |
7292 | { | |
7293 | r_symndx = osec->target_index; | |
7294 | BFD_ASSERT (r_symndx != 0); | |
7295 | } | |
c152c796 AM |
7296 | } |
7297 | ||
7298 | /* Adjust the addend according to where the | |
7299 | section winds up in the output section. */ | |
7300 | if (rela_normal) | |
7301 | irela->r_addend += sec->output_offset; | |
7302 | } | |
7303 | else | |
7304 | { | |
7305 | if (finfo->indices[r_symndx] == -1) | |
7306 | { | |
7307 | unsigned long shlink; | |
7308 | const char *name; | |
7309 | asection *osec; | |
7310 | ||
7311 | if (finfo->info->strip == strip_all) | |
7312 | { | |
7313 | /* You can't do ld -r -s. */ | |
7314 | bfd_set_error (bfd_error_invalid_operation); | |
7315 | return FALSE; | |
7316 | } | |
7317 | ||
7318 | /* This symbol was skipped earlier, but | |
7319 | since it is needed by a reloc, we | |
7320 | must output it now. */ | |
7321 | shlink = symtab_hdr->sh_link; | |
7322 | name = (bfd_elf_string_from_elf_section | |
7323 | (input_bfd, shlink, sym.st_name)); | |
7324 | if (name == NULL) | |
7325 | return FALSE; | |
7326 | ||
7327 | osec = sec->output_section; | |
7328 | sym.st_shndx = | |
7329 | _bfd_elf_section_from_bfd_section (output_bfd, | |
7330 | osec); | |
7331 | if (sym.st_shndx == SHN_BAD) | |
7332 | return FALSE; | |
7333 | ||
7334 | sym.st_value += sec->output_offset; | |
7335 | if (! finfo->info->relocatable) | |
7336 | { | |
7337 | sym.st_value += osec->vma; | |
7338 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
7339 | { | |
7340 | /* STT_TLS symbols are relative to PT_TLS | |
7341 | segment base. */ | |
7342 | BFD_ASSERT (elf_hash_table (finfo->info) | |
7343 | ->tls_sec != NULL); | |
7344 | sym.st_value -= (elf_hash_table (finfo->info) | |
7345 | ->tls_sec->vma); | |
7346 | } | |
7347 | } | |
7348 | ||
7349 | finfo->indices[r_symndx] | |
7350 | = bfd_get_symcount (output_bfd); | |
7351 | ||
7352 | if (! elf_link_output_sym (finfo, name, &sym, sec, | |
7353 | NULL)) | |
7354 | return FALSE; | |
7355 | } | |
7356 | ||
7357 | r_symndx = finfo->indices[r_symndx]; | |
7358 | } | |
7359 | ||
7360 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
7361 | | (irela->r_info & r_type_mask)); | |
7362 | } | |
7363 | ||
7364 | /* Swap out the relocs. */ | |
c152c796 | 7365 | if (input_rel_hdr->sh_size != 0 |
eac338cf PB |
7366 | && !bed->elf_backend_emit_relocs (output_bfd, o, |
7367 | input_rel_hdr, | |
7368 | internal_relocs, | |
7369 | rel_hash_list)) | |
c152c796 AM |
7370 | return FALSE; |
7371 | ||
7372 | input_rel_hdr2 = elf_section_data (o)->rel_hdr2; | |
7373 | if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0) | |
7374 | { | |
7375 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) | |
7376 | * bed->s->int_rels_per_ext_rel); | |
eac338cf PB |
7377 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
7378 | if (!bed->elf_backend_emit_relocs (output_bfd, o, | |
7379 | input_rel_hdr2, | |
7380 | internal_relocs, | |
7381 | rel_hash_list)) | |
c152c796 AM |
7382 | return FALSE; |
7383 | } | |
7384 | } | |
7385 | } | |
7386 | ||
7387 | /* Write out the modified section contents. */ | |
7388 | if (bed->elf_backend_write_section | |
7389 | && (*bed->elf_backend_write_section) (output_bfd, o, contents)) | |
7390 | { | |
7391 | /* Section written out. */ | |
7392 | } | |
7393 | else switch (o->sec_info_type) | |
7394 | { | |
7395 | case ELF_INFO_TYPE_STABS: | |
7396 | if (! (_bfd_write_section_stabs | |
7397 | (output_bfd, | |
7398 | &elf_hash_table (finfo->info)->stab_info, | |
7399 | o, &elf_section_data (o)->sec_info, contents))) | |
7400 | return FALSE; | |
7401 | break; | |
7402 | case ELF_INFO_TYPE_MERGE: | |
7403 | if (! _bfd_write_merged_section (output_bfd, o, | |
7404 | elf_section_data (o)->sec_info)) | |
7405 | return FALSE; | |
7406 | break; | |
7407 | case ELF_INFO_TYPE_EH_FRAME: | |
7408 | { | |
7409 | if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, | |
7410 | o, contents)) | |
7411 | return FALSE; | |
7412 | } | |
7413 | break; | |
7414 | default: | |
7415 | { | |
c152c796 AM |
7416 | if (! (o->flags & SEC_EXCLUDE) |
7417 | && ! bfd_set_section_contents (output_bfd, o->output_section, | |
7418 | contents, | |
7419 | (file_ptr) o->output_offset, | |
eea6121a | 7420 | o->size)) |
c152c796 AM |
7421 | return FALSE; |
7422 | } | |
7423 | break; | |
7424 | } | |
7425 | } | |
7426 | ||
7427 | return TRUE; | |
7428 | } | |
7429 | ||
7430 | /* Generate a reloc when linking an ELF file. This is a reloc | |
7431 | requested by the linker, and does come from any input file. This | |
7432 | is used to build constructor and destructor tables when linking | |
7433 | with -Ur. */ | |
7434 | ||
7435 | static bfd_boolean | |
7436 | elf_reloc_link_order (bfd *output_bfd, | |
7437 | struct bfd_link_info *info, | |
7438 | asection *output_section, | |
7439 | struct bfd_link_order *link_order) | |
7440 | { | |
7441 | reloc_howto_type *howto; | |
7442 | long indx; | |
7443 | bfd_vma offset; | |
7444 | bfd_vma addend; | |
7445 | struct elf_link_hash_entry **rel_hash_ptr; | |
7446 | Elf_Internal_Shdr *rel_hdr; | |
7447 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
7448 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
7449 | bfd_byte *erel; | |
7450 | unsigned int i; | |
7451 | ||
7452 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
7453 | if (howto == NULL) | |
7454 | { | |
7455 | bfd_set_error (bfd_error_bad_value); | |
7456 | return FALSE; | |
7457 | } | |
7458 | ||
7459 | addend = link_order->u.reloc.p->addend; | |
7460 | ||
7461 | /* Figure out the symbol index. */ | |
7462 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
7463 | + elf_section_data (output_section)->rel_count | |
7464 | + elf_section_data (output_section)->rel_count2); | |
7465 | if (link_order->type == bfd_section_reloc_link_order) | |
7466 | { | |
7467 | indx = link_order->u.reloc.p->u.section->target_index; | |
7468 | BFD_ASSERT (indx != 0); | |
7469 | *rel_hash_ptr = NULL; | |
7470 | } | |
7471 | else | |
7472 | { | |
7473 | struct elf_link_hash_entry *h; | |
7474 | ||
7475 | /* Treat a reloc against a defined symbol as though it were | |
7476 | actually against the section. */ | |
7477 | h = ((struct elf_link_hash_entry *) | |
7478 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
7479 | link_order->u.reloc.p->u.name, | |
7480 | FALSE, FALSE, TRUE)); | |
7481 | if (h != NULL | |
7482 | && (h->root.type == bfd_link_hash_defined | |
7483 | || h->root.type == bfd_link_hash_defweak)) | |
7484 | { | |
7485 | asection *section; | |
7486 | ||
7487 | section = h->root.u.def.section; | |
7488 | indx = section->output_section->target_index; | |
7489 | *rel_hash_ptr = NULL; | |
7490 | /* It seems that we ought to add the symbol value to the | |
7491 | addend here, but in practice it has already been added | |
7492 | because it was passed to constructor_callback. */ | |
7493 | addend += section->output_section->vma + section->output_offset; | |
7494 | } | |
7495 | else if (h != NULL) | |
7496 | { | |
7497 | /* Setting the index to -2 tells elf_link_output_extsym that | |
7498 | this symbol is used by a reloc. */ | |
7499 | h->indx = -2; | |
7500 | *rel_hash_ptr = h; | |
7501 | indx = 0; | |
7502 | } | |
7503 | else | |
7504 | { | |
7505 | if (! ((*info->callbacks->unattached_reloc) | |
7506 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) | |
7507 | return FALSE; | |
7508 | indx = 0; | |
7509 | } | |
7510 | } | |
7511 | ||
7512 | /* If this is an inplace reloc, we must write the addend into the | |
7513 | object file. */ | |
7514 | if (howto->partial_inplace && addend != 0) | |
7515 | { | |
7516 | bfd_size_type size; | |
7517 | bfd_reloc_status_type rstat; | |
7518 | bfd_byte *buf; | |
7519 | bfd_boolean ok; | |
7520 | const char *sym_name; | |
7521 | ||
7522 | size = bfd_get_reloc_size (howto); | |
7523 | buf = bfd_zmalloc (size); | |
7524 | if (buf == NULL) | |
7525 | return FALSE; | |
7526 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
7527 | switch (rstat) | |
7528 | { | |
7529 | case bfd_reloc_ok: | |
7530 | break; | |
7531 | ||
7532 | default: | |
7533 | case bfd_reloc_outofrange: | |
7534 | abort (); | |
7535 | ||
7536 | case bfd_reloc_overflow: | |
7537 | if (link_order->type == bfd_section_reloc_link_order) | |
7538 | sym_name = bfd_section_name (output_bfd, | |
7539 | link_order->u.reloc.p->u.section); | |
7540 | else | |
7541 | sym_name = link_order->u.reloc.p->u.name; | |
7542 | if (! ((*info->callbacks->reloc_overflow) | |
dfeffb9f L |
7543 | (info, NULL, sym_name, howto->name, addend, NULL, |
7544 | NULL, (bfd_vma) 0))) | |
c152c796 AM |
7545 | { |
7546 | free (buf); | |
7547 | return FALSE; | |
7548 | } | |
7549 | break; | |
7550 | } | |
7551 | ok = bfd_set_section_contents (output_bfd, output_section, buf, | |
7552 | link_order->offset, size); | |
7553 | free (buf); | |
7554 | if (! ok) | |
7555 | return FALSE; | |
7556 | } | |
7557 | ||
7558 | /* The address of a reloc is relative to the section in a | |
7559 | relocatable file, and is a virtual address in an executable | |
7560 | file. */ | |
7561 | offset = link_order->offset; | |
7562 | if (! info->relocatable) | |
7563 | offset += output_section->vma; | |
7564 | ||
7565 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
7566 | { | |
7567 | irel[i].r_offset = offset; | |
7568 | irel[i].r_info = 0; | |
7569 | irel[i].r_addend = 0; | |
7570 | } | |
7571 | if (bed->s->arch_size == 32) | |
7572 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
7573 | else | |
7574 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
7575 | ||
7576 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
7577 | erel = rel_hdr->contents; | |
7578 | if (rel_hdr->sh_type == SHT_REL) | |
7579 | { | |
7580 | erel += (elf_section_data (output_section)->rel_count | |
7581 | * bed->s->sizeof_rel); | |
7582 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); | |
7583 | } | |
7584 | else | |
7585 | { | |
7586 | irel[0].r_addend = addend; | |
7587 | erel += (elf_section_data (output_section)->rel_count | |
7588 | * bed->s->sizeof_rela); | |
7589 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); | |
7590 | } | |
7591 | ||
7592 | ++elf_section_data (output_section)->rel_count; | |
7593 | ||
7594 | return TRUE; | |
7595 | } | |
7596 | ||
0b52efa6 PB |
7597 | |
7598 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
7599 | ||
7600 | static bfd_vma | |
7601 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
7602 | { | |
7603 | Elf_Internal_Shdr **elf_shdrp; | |
7604 | asection *s; | |
7605 | int elfsec; | |
7606 | ||
7607 | s = p->u.indirect.section; | |
7608 | elf_shdrp = elf_elfsections (s->owner); | |
7609 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
7610 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
7611 | /* PR 290: |
7612 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
7613 | SHF_LINK_ORDER. But it doesn't set theh sh_link or | |
7614 | sh_info fields. Hence we could get the situation | |
7615 | where elfsec is 0. */ | |
7616 | if (elfsec == 0) | |
7617 | { | |
7618 | const struct elf_backend_data *bed | |
7619 | = get_elf_backend_data (s->owner); | |
7620 | if (bed->link_order_error_handler) | |
d003868e AM |
7621 | bed->link_order_error_handler |
7622 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); | |
185d09ad L |
7623 | return 0; |
7624 | } | |
7625 | else | |
7626 | { | |
7627 | s = elf_shdrp[elfsec]->bfd_section; | |
7628 | return s->output_section->vma + s->output_offset; | |
7629 | } | |
0b52efa6 PB |
7630 | } |
7631 | ||
7632 | ||
7633 | /* Compare two sections based on the locations of the sections they are | |
7634 | linked to. Used by elf_fixup_link_order. */ | |
7635 | ||
7636 | static int | |
7637 | compare_link_order (const void * a, const void * b) | |
7638 | { | |
7639 | bfd_vma apos; | |
7640 | bfd_vma bpos; | |
7641 | ||
7642 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
7643 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
7644 | if (apos < bpos) | |
7645 | return -1; | |
7646 | return apos > bpos; | |
7647 | } | |
7648 | ||
7649 | ||
7650 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
7651 | order as their linked sections. Returns false if this could not be done | |
7652 | because an output section includes both ordered and unordered | |
7653 | sections. Ideally we'd do this in the linker proper. */ | |
7654 | ||
7655 | static bfd_boolean | |
7656 | elf_fixup_link_order (bfd *abfd, asection *o) | |
7657 | { | |
7658 | int seen_linkorder; | |
7659 | int seen_other; | |
7660 | int n; | |
7661 | struct bfd_link_order *p; | |
7662 | bfd *sub; | |
7663 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7664 | int elfsec; | |
7665 | struct bfd_link_order **sections; | |
7666 | asection *s; | |
7667 | bfd_vma offset; | |
3b36f7e6 | 7668 | |
0b52efa6 PB |
7669 | seen_other = 0; |
7670 | seen_linkorder = 0; | |
8423293d | 7671 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
7672 | { |
7673 | if (p->type == bfd_indirect_link_order | |
7674 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
7675 | == bfd_target_elf_flavour) | |
7676 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
7677 | { | |
7678 | s = p->u.indirect.section; | |
7679 | elfsec = _bfd_elf_section_from_bfd_section (sub, s); | |
7680 | if (elfsec != -1 | |
7681 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER) | |
7682 | seen_linkorder++; | |
7683 | else | |
7684 | seen_other++; | |
7685 | } | |
7686 | else | |
7687 | seen_other++; | |
7688 | } | |
7689 | ||
7690 | if (!seen_linkorder) | |
7691 | return TRUE; | |
7692 | ||
7693 | if (seen_other && seen_linkorder) | |
08ccf96b | 7694 | { |
d003868e AM |
7695 | (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), |
7696 | o); | |
08ccf96b L |
7697 | bfd_set_error (bfd_error_bad_value); |
7698 | return FALSE; | |
7699 | } | |
3b36f7e6 | 7700 | |
0b52efa6 PB |
7701 | sections = (struct bfd_link_order **) |
7702 | xmalloc (seen_linkorder * sizeof (struct bfd_link_order *)); | |
7703 | seen_linkorder = 0; | |
3b36f7e6 | 7704 | |
8423293d | 7705 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
7706 | { |
7707 | sections[seen_linkorder++] = p; | |
7708 | } | |
7709 | /* Sort the input sections in the order of their linked section. */ | |
7710 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
7711 | compare_link_order); | |
7712 | ||
7713 | /* Change the offsets of the sections. */ | |
7714 | offset = 0; | |
7715 | for (n = 0; n < seen_linkorder; n++) | |
7716 | { | |
7717 | s = sections[n]->u.indirect.section; | |
7718 | offset &= ~(bfd_vma)((1 << s->alignment_power) - 1); | |
7719 | s->output_offset = offset; | |
7720 | sections[n]->offset = offset; | |
7721 | offset += sections[n]->size; | |
7722 | } | |
7723 | ||
7724 | return TRUE; | |
7725 | } | |
7726 | ||
7727 | ||
c152c796 AM |
7728 | /* Do the final step of an ELF link. */ |
7729 | ||
7730 | bfd_boolean | |
7731 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
7732 | { | |
7733 | bfd_boolean dynamic; | |
7734 | bfd_boolean emit_relocs; | |
7735 | bfd *dynobj; | |
7736 | struct elf_final_link_info finfo; | |
7737 | register asection *o; | |
7738 | register struct bfd_link_order *p; | |
7739 | register bfd *sub; | |
7740 | bfd_size_type max_contents_size; | |
7741 | bfd_size_type max_external_reloc_size; | |
7742 | bfd_size_type max_internal_reloc_count; | |
7743 | bfd_size_type max_sym_count; | |
7744 | bfd_size_type max_sym_shndx_count; | |
7745 | file_ptr off; | |
7746 | Elf_Internal_Sym elfsym; | |
7747 | unsigned int i; | |
7748 | Elf_Internal_Shdr *symtab_hdr; | |
7749 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
7750 | Elf_Internal_Shdr *symstrtab_hdr; | |
7751 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7752 | struct elf_outext_info eoinfo; | |
7753 | bfd_boolean merged; | |
7754 | size_t relativecount = 0; | |
7755 | asection *reldyn = 0; | |
7756 | bfd_size_type amt; | |
7757 | ||
7758 | if (! is_elf_hash_table (info->hash)) | |
7759 | return FALSE; | |
7760 | ||
7761 | if (info->shared) | |
7762 | abfd->flags |= DYNAMIC; | |
7763 | ||
7764 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
7765 | dynobj = elf_hash_table (info)->dynobj; | |
7766 | ||
7767 | emit_relocs = (info->relocatable | |
7768 | || info->emitrelocations | |
7769 | || bed->elf_backend_emit_relocs); | |
7770 | ||
7771 | finfo.info = info; | |
7772 | finfo.output_bfd = abfd; | |
7773 | finfo.symstrtab = _bfd_elf_stringtab_init (); | |
7774 | if (finfo.symstrtab == NULL) | |
7775 | return FALSE; | |
7776 | ||
7777 | if (! dynamic) | |
7778 | { | |
7779 | finfo.dynsym_sec = NULL; | |
7780 | finfo.hash_sec = NULL; | |
7781 | finfo.symver_sec = NULL; | |
7782 | } | |
7783 | else | |
7784 | { | |
7785 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
7786 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
7787 | BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL); | |
7788 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
7789 | /* Note that it is OK if symver_sec is NULL. */ | |
7790 | } | |
7791 | ||
7792 | finfo.contents = NULL; | |
7793 | finfo.external_relocs = NULL; | |
7794 | finfo.internal_relocs = NULL; | |
7795 | finfo.external_syms = NULL; | |
7796 | finfo.locsym_shndx = NULL; | |
7797 | finfo.internal_syms = NULL; | |
7798 | finfo.indices = NULL; | |
7799 | finfo.sections = NULL; | |
7800 | finfo.symbuf = NULL; | |
7801 | finfo.symshndxbuf = NULL; | |
7802 | finfo.symbuf_count = 0; | |
7803 | finfo.shndxbuf_size = 0; | |
7804 | ||
7805 | /* Count up the number of relocations we will output for each output | |
7806 | section, so that we know the sizes of the reloc sections. We | |
7807 | also figure out some maximum sizes. */ | |
7808 | max_contents_size = 0; | |
7809 | max_external_reloc_size = 0; | |
7810 | max_internal_reloc_count = 0; | |
7811 | max_sym_count = 0; | |
7812 | max_sym_shndx_count = 0; | |
7813 | merged = FALSE; | |
7814 | for (o = abfd->sections; o != NULL; o = o->next) | |
7815 | { | |
7816 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
7817 | o->reloc_count = 0; | |
7818 | ||
8423293d | 7819 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
7820 | { |
7821 | unsigned int reloc_count = 0; | |
7822 | struct bfd_elf_section_data *esdi = NULL; | |
7823 | unsigned int *rel_count1; | |
7824 | ||
7825 | if (p->type == bfd_section_reloc_link_order | |
7826 | || p->type == bfd_symbol_reloc_link_order) | |
7827 | reloc_count = 1; | |
7828 | else if (p->type == bfd_indirect_link_order) | |
7829 | { | |
7830 | asection *sec; | |
7831 | ||
7832 | sec = p->u.indirect.section; | |
7833 | esdi = elf_section_data (sec); | |
7834 | ||
7835 | /* Mark all sections which are to be included in the | |
7836 | link. This will normally be every section. We need | |
7837 | to do this so that we can identify any sections which | |
7838 | the linker has decided to not include. */ | |
7839 | sec->linker_mark = TRUE; | |
7840 | ||
7841 | if (sec->flags & SEC_MERGE) | |
7842 | merged = TRUE; | |
7843 | ||
7844 | if (info->relocatable || info->emitrelocations) | |
7845 | reloc_count = sec->reloc_count; | |
7846 | else if (bed->elf_backend_count_relocs) | |
7847 | { | |
7848 | Elf_Internal_Rela * relocs; | |
7849 | ||
7850 | relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
7851 | info->keep_memory); | |
7852 | ||
7853 | reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs); | |
7854 | ||
7855 | if (elf_section_data (o)->relocs != relocs) | |
7856 | free (relocs); | |
7857 | } | |
7858 | ||
eea6121a AM |
7859 | if (sec->rawsize > max_contents_size) |
7860 | max_contents_size = sec->rawsize; | |
7861 | if (sec->size > max_contents_size) | |
7862 | max_contents_size = sec->size; | |
c152c796 AM |
7863 | |
7864 | /* We are interested in just local symbols, not all | |
7865 | symbols. */ | |
7866 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
7867 | && (sec->owner->flags & DYNAMIC) == 0) | |
7868 | { | |
7869 | size_t sym_count; | |
7870 | ||
7871 | if (elf_bad_symtab (sec->owner)) | |
7872 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
7873 | / bed->s->sizeof_sym); | |
7874 | else | |
7875 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
7876 | ||
7877 | if (sym_count > max_sym_count) | |
7878 | max_sym_count = sym_count; | |
7879 | ||
7880 | if (sym_count > max_sym_shndx_count | |
7881 | && elf_symtab_shndx (sec->owner) != 0) | |
7882 | max_sym_shndx_count = sym_count; | |
7883 | ||
7884 | if ((sec->flags & SEC_RELOC) != 0) | |
7885 | { | |
7886 | size_t ext_size; | |
7887 | ||
7888 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
7889 | if (ext_size > max_external_reloc_size) | |
7890 | max_external_reloc_size = ext_size; | |
7891 | if (sec->reloc_count > max_internal_reloc_count) | |
7892 | max_internal_reloc_count = sec->reloc_count; | |
7893 | } | |
7894 | } | |
7895 | } | |
7896 | ||
7897 | if (reloc_count == 0) | |
7898 | continue; | |
7899 | ||
7900 | o->reloc_count += reloc_count; | |
7901 | ||
7902 | /* MIPS may have a mix of REL and RELA relocs on sections. | |
7903 | To support this curious ABI we keep reloc counts in | |
7904 | elf_section_data too. We must be careful to add the | |
7905 | relocations from the input section to the right output | |
7906 | count. FIXME: Get rid of one count. We have | |
7907 | o->reloc_count == esdo->rel_count + esdo->rel_count2. */ | |
7908 | rel_count1 = &esdo->rel_count; | |
7909 | if (esdi != NULL) | |
7910 | { | |
7911 | bfd_boolean same_size; | |
7912 | bfd_size_type entsize1; | |
7913 | ||
7914 | entsize1 = esdi->rel_hdr.sh_entsize; | |
7915 | BFD_ASSERT (entsize1 == bed->s->sizeof_rel | |
7916 | || entsize1 == bed->s->sizeof_rela); | |
7917 | same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel); | |
7918 | ||
7919 | if (!same_size) | |
7920 | rel_count1 = &esdo->rel_count2; | |
7921 | ||
7922 | if (esdi->rel_hdr2 != NULL) | |
7923 | { | |
7924 | bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize; | |
7925 | unsigned int alt_count; | |
7926 | unsigned int *rel_count2; | |
7927 | ||
7928 | BFD_ASSERT (entsize2 != entsize1 | |
7929 | && (entsize2 == bed->s->sizeof_rel | |
7930 | || entsize2 == bed->s->sizeof_rela)); | |
7931 | ||
7932 | rel_count2 = &esdo->rel_count2; | |
7933 | if (!same_size) | |
7934 | rel_count2 = &esdo->rel_count; | |
7935 | ||
7936 | /* The following is probably too simplistic if the | |
7937 | backend counts output relocs unusually. */ | |
7938 | BFD_ASSERT (bed->elf_backend_count_relocs == NULL); | |
7939 | alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2); | |
7940 | *rel_count2 += alt_count; | |
7941 | reloc_count -= alt_count; | |
7942 | } | |
7943 | } | |
7944 | *rel_count1 += reloc_count; | |
7945 | } | |
7946 | ||
7947 | if (o->reloc_count > 0) | |
7948 | o->flags |= SEC_RELOC; | |
7949 | else | |
7950 | { | |
7951 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
7952 | set it (this is probably a bug) and if it is set | |
7953 | assign_section_numbers will create a reloc section. */ | |
7954 | o->flags &=~ SEC_RELOC; | |
7955 | } | |
7956 | ||
7957 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
7958 | zero. This is done in elf_fake_sections as well, but forcing | |
7959 | the VMA to 0 here will ensure that relocs against these | |
7960 | sections are handled correctly. */ | |
7961 | if ((o->flags & SEC_ALLOC) == 0 | |
7962 | && ! o->user_set_vma) | |
7963 | o->vma = 0; | |
7964 | } | |
7965 | ||
7966 | if (! info->relocatable && merged) | |
7967 | elf_link_hash_traverse (elf_hash_table (info), | |
7968 | _bfd_elf_link_sec_merge_syms, abfd); | |
7969 | ||
7970 | /* Figure out the file positions for everything but the symbol table | |
7971 | and the relocs. We set symcount to force assign_section_numbers | |
7972 | to create a symbol table. */ | |
7973 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; | |
7974 | BFD_ASSERT (! abfd->output_has_begun); | |
7975 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
7976 | goto error_return; | |
7977 | ||
ee75fd95 | 7978 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
7979 | for (o = abfd->sections; o != NULL; o = o->next) |
7980 | { | |
7981 | if ((o->flags & SEC_RELOC) != 0) | |
7982 | { | |
7983 | if (!(_bfd_elf_link_size_reloc_section | |
7984 | (abfd, &elf_section_data (o)->rel_hdr, o))) | |
7985 | goto error_return; | |
7986 | ||
7987 | if (elf_section_data (o)->rel_hdr2 | |
7988 | && !(_bfd_elf_link_size_reloc_section | |
7989 | (abfd, elf_section_data (o)->rel_hdr2, o))) | |
7990 | goto error_return; | |
7991 | } | |
7992 | ||
7993 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
7994 | to count upwards while actually outputting the relocations. */ | |
7995 | elf_section_data (o)->rel_count = 0; | |
7996 | elf_section_data (o)->rel_count2 = 0; | |
7997 | } | |
7998 | ||
7999 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
8000 | ||
8001 | /* We have now assigned file positions for all the sections except | |
8002 | .symtab and .strtab. We start the .symtab section at the current | |
8003 | file position, and write directly to it. We build the .strtab | |
8004 | section in memory. */ | |
8005 | bfd_get_symcount (abfd) = 0; | |
8006 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
8007 | /* sh_name is set in prep_headers. */ | |
8008 | symtab_hdr->sh_type = SHT_SYMTAB; | |
8009 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
8010 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
8011 | /* sh_link is set in assign_section_numbers. */ | |
8012 | /* sh_info is set below. */ | |
8013 | /* sh_offset is set just below. */ | |
8014 | symtab_hdr->sh_addralign = 1 << bed->s->log_file_align; | |
8015 | ||
8016 | off = elf_tdata (abfd)->next_file_pos; | |
8017 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
8018 | ||
8019 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
8020 | incorrect. We do not yet know the size of the .symtab section. | |
8021 | We correct next_file_pos below, after we do know the size. */ | |
8022 | ||
8023 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
8024 | continuously seeking to the right position in the file. */ | |
8025 | if (! info->keep_memory || max_sym_count < 20) | |
8026 | finfo.symbuf_size = 20; | |
8027 | else | |
8028 | finfo.symbuf_size = max_sym_count; | |
8029 | amt = finfo.symbuf_size; | |
8030 | amt *= bed->s->sizeof_sym; | |
8031 | finfo.symbuf = bfd_malloc (amt); | |
8032 | if (finfo.symbuf == NULL) | |
8033 | goto error_return; | |
8034 | if (elf_numsections (abfd) > SHN_LORESERVE) | |
8035 | { | |
8036 | /* Wild guess at number of output symbols. realloc'd as needed. */ | |
8037 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
8038 | finfo.shndxbuf_size = amt; | |
8039 | amt *= sizeof (Elf_External_Sym_Shndx); | |
8040 | finfo.symshndxbuf = bfd_zmalloc (amt); | |
8041 | if (finfo.symshndxbuf == NULL) | |
8042 | goto error_return; | |
8043 | } | |
8044 | ||
8045 | /* Start writing out the symbol table. The first symbol is always a | |
8046 | dummy symbol. */ | |
8047 | if (info->strip != strip_all | |
8048 | || emit_relocs) | |
8049 | { | |
8050 | elfsym.st_value = 0; | |
8051 | elfsym.st_size = 0; | |
8052 | elfsym.st_info = 0; | |
8053 | elfsym.st_other = 0; | |
8054 | elfsym.st_shndx = SHN_UNDEF; | |
8055 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr, | |
8056 | NULL)) | |
8057 | goto error_return; | |
8058 | } | |
8059 | ||
c152c796 AM |
8060 | /* Output a symbol for each section. We output these even if we are |
8061 | discarding local symbols, since they are used for relocs. These | |
8062 | symbols have no names. We store the index of each one in the | |
8063 | index field of the section, so that we can find it again when | |
8064 | outputting relocs. */ | |
8065 | if (info->strip != strip_all | |
8066 | || emit_relocs) | |
8067 | { | |
8068 | elfsym.st_size = 0; | |
8069 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8070 | elfsym.st_other = 0; | |
8071 | for (i = 1; i < elf_numsections (abfd); i++) | |
8072 | { | |
8073 | o = bfd_section_from_elf_index (abfd, i); | |
8074 | if (o != NULL) | |
8075 | o->target_index = bfd_get_symcount (abfd); | |
8076 | elfsym.st_shndx = i; | |
8077 | if (info->relocatable || o == NULL) | |
8078 | elfsym.st_value = 0; | |
8079 | else | |
8080 | elfsym.st_value = o->vma; | |
8081 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL)) | |
8082 | goto error_return; | |
8083 | if (i == SHN_LORESERVE - 1) | |
8084 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
8085 | } | |
8086 | } | |
8087 | ||
8088 | /* Allocate some memory to hold information read in from the input | |
8089 | files. */ | |
8090 | if (max_contents_size != 0) | |
8091 | { | |
8092 | finfo.contents = bfd_malloc (max_contents_size); | |
8093 | if (finfo.contents == NULL) | |
8094 | goto error_return; | |
8095 | } | |
8096 | ||
8097 | if (max_external_reloc_size != 0) | |
8098 | { | |
8099 | finfo.external_relocs = bfd_malloc (max_external_reloc_size); | |
8100 | if (finfo.external_relocs == NULL) | |
8101 | goto error_return; | |
8102 | } | |
8103 | ||
8104 | if (max_internal_reloc_count != 0) | |
8105 | { | |
8106 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
8107 | amt *= sizeof (Elf_Internal_Rela); | |
8108 | finfo.internal_relocs = bfd_malloc (amt); | |
8109 | if (finfo.internal_relocs == NULL) | |
8110 | goto error_return; | |
8111 | } | |
8112 | ||
8113 | if (max_sym_count != 0) | |
8114 | { | |
8115 | amt = max_sym_count * bed->s->sizeof_sym; | |
8116 | finfo.external_syms = bfd_malloc (amt); | |
8117 | if (finfo.external_syms == NULL) | |
8118 | goto error_return; | |
8119 | ||
8120 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8121 | finfo.internal_syms = bfd_malloc (amt); | |
8122 | if (finfo.internal_syms == NULL) | |
8123 | goto error_return; | |
8124 | ||
8125 | amt = max_sym_count * sizeof (long); | |
8126 | finfo.indices = bfd_malloc (amt); | |
8127 | if (finfo.indices == NULL) | |
8128 | goto error_return; | |
8129 | ||
8130 | amt = max_sym_count * sizeof (asection *); | |
8131 | finfo.sections = bfd_malloc (amt); | |
8132 | if (finfo.sections == NULL) | |
8133 | goto error_return; | |
8134 | } | |
8135 | ||
8136 | if (max_sym_shndx_count != 0) | |
8137 | { | |
8138 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8139 | finfo.locsym_shndx = bfd_malloc (amt); | |
8140 | if (finfo.locsym_shndx == NULL) | |
8141 | goto error_return; | |
8142 | } | |
8143 | ||
8144 | if (elf_hash_table (info)->tls_sec) | |
8145 | { | |
8146 | bfd_vma base, end = 0; | |
8147 | asection *sec; | |
8148 | ||
8149 | for (sec = elf_hash_table (info)->tls_sec; | |
8150 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
8151 | sec = sec->next) | |
8152 | { | |
eea6121a | 8153 | bfd_vma size = sec->size; |
c152c796 AM |
8154 | |
8155 | if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
8156 | { | |
8157 | struct bfd_link_order *o; | |
8158 | ||
8423293d | 8159 | for (o = sec->map_head.link_order; o != NULL; o = o->next) |
c152c796 AM |
8160 | if (size < o->offset + o->size) |
8161 | size = o->offset + o->size; | |
8162 | } | |
8163 | end = sec->vma + size; | |
8164 | } | |
8165 | base = elf_hash_table (info)->tls_sec->vma; | |
8166 | end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power); | |
8167 | elf_hash_table (info)->tls_size = end - base; | |
8168 | } | |
8169 | ||
0b52efa6 PB |
8170 | /* Reorder SHF_LINK_ORDER sections. */ |
8171 | for (o = abfd->sections; o != NULL; o = o->next) | |
8172 | { | |
8173 | if (!elf_fixup_link_order (abfd, o)) | |
8174 | return FALSE; | |
8175 | } | |
8176 | ||
c152c796 AM |
8177 | /* Since ELF permits relocations to be against local symbols, we |
8178 | must have the local symbols available when we do the relocations. | |
8179 | Since we would rather only read the local symbols once, and we | |
8180 | would rather not keep them in memory, we handle all the | |
8181 | relocations for a single input file at the same time. | |
8182 | ||
8183 | Unfortunately, there is no way to know the total number of local | |
8184 | symbols until we have seen all of them, and the local symbol | |
8185 | indices precede the global symbol indices. This means that when | |
8186 | we are generating relocatable output, and we see a reloc against | |
8187 | a global symbol, we can not know the symbol index until we have | |
8188 | finished examining all the local symbols to see which ones we are | |
8189 | going to output. To deal with this, we keep the relocations in | |
8190 | memory, and don't output them until the end of the link. This is | |
8191 | an unfortunate waste of memory, but I don't see a good way around | |
8192 | it. Fortunately, it only happens when performing a relocatable | |
8193 | link, which is not the common case. FIXME: If keep_memory is set | |
8194 | we could write the relocs out and then read them again; I don't | |
8195 | know how bad the memory loss will be. */ | |
8196 | ||
8197 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8198 | sub->output_has_begun = FALSE; | |
8199 | for (o = abfd->sections; o != NULL; o = o->next) | |
8200 | { | |
8423293d | 8201 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
8202 | { |
8203 | if (p->type == bfd_indirect_link_order | |
8204 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
8205 | == bfd_target_elf_flavour) | |
8206 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
8207 | { | |
8208 | if (! sub->output_has_begun) | |
8209 | { | |
8210 | if (! elf_link_input_bfd (&finfo, sub)) | |
8211 | goto error_return; | |
8212 | sub->output_has_begun = TRUE; | |
8213 | } | |
8214 | } | |
8215 | else if (p->type == bfd_section_reloc_link_order | |
8216 | || p->type == bfd_symbol_reloc_link_order) | |
8217 | { | |
8218 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
8219 | goto error_return; | |
8220 | } | |
8221 | else | |
8222 | { | |
8223 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
8224 | goto error_return; | |
8225 | } | |
8226 | } | |
8227 | } | |
8228 | ||
8229 | /* Output any global symbols that got converted to local in a | |
8230 | version script or due to symbol visibility. We do this in a | |
8231 | separate step since ELF requires all local symbols to appear | |
8232 | prior to any global symbols. FIXME: We should only do this if | |
8233 | some global symbols were, in fact, converted to become local. | |
8234 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
8235 | eoinfo.failed = FALSE; | |
8236 | eoinfo.finfo = &finfo; | |
8237 | eoinfo.localsyms = TRUE; | |
8238 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8239 | &eoinfo); | |
8240 | if (eoinfo.failed) | |
8241 | return FALSE; | |
8242 | ||
8243 | /* That wrote out all the local symbols. Finish up the symbol table | |
8244 | with the global symbols. Even if we want to strip everything we | |
8245 | can, we still need to deal with those global symbols that got | |
8246 | converted to local in a version script. */ | |
8247 | ||
8248 | /* The sh_info field records the index of the first non local symbol. */ | |
8249 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
8250 | ||
8251 | if (dynamic | |
8252 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
8253 | { | |
8254 | Elf_Internal_Sym sym; | |
8255 | bfd_byte *dynsym = finfo.dynsym_sec->contents; | |
8256 | long last_local = 0; | |
8257 | ||
8258 | /* Write out the section symbols for the output sections. */ | |
67687978 | 8259 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
c152c796 AM |
8260 | { |
8261 | asection *s; | |
8262 | ||
8263 | sym.st_size = 0; | |
8264 | sym.st_name = 0; | |
8265 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8266 | sym.st_other = 0; | |
8267 | ||
8268 | for (s = abfd->sections; s != NULL; s = s->next) | |
8269 | { | |
8270 | int indx; | |
8271 | bfd_byte *dest; | |
8272 | long dynindx; | |
8273 | ||
c152c796 | 8274 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
8275 | if (dynindx <= 0) |
8276 | continue; | |
8277 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
8278 | BFD_ASSERT (indx > 0); |
8279 | sym.st_shndx = indx; | |
8280 | sym.st_value = s->vma; | |
8281 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
8c37241b JJ |
8282 | if (last_local < dynindx) |
8283 | last_local = dynindx; | |
c152c796 AM |
8284 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
8285 | } | |
c152c796 AM |
8286 | } |
8287 | ||
8288 | /* Write out the local dynsyms. */ | |
8289 | if (elf_hash_table (info)->dynlocal) | |
8290 | { | |
8291 | struct elf_link_local_dynamic_entry *e; | |
8292 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
8293 | { | |
8294 | asection *s; | |
8295 | bfd_byte *dest; | |
8296 | ||
8297 | sym.st_size = e->isym.st_size; | |
8298 | sym.st_other = e->isym.st_other; | |
8299 | ||
8300 | /* Copy the internal symbol as is. | |
8301 | Note that we saved a word of storage and overwrote | |
8302 | the original st_name with the dynstr_index. */ | |
8303 | sym = e->isym; | |
8304 | ||
8305 | if (e->isym.st_shndx != SHN_UNDEF | |
8306 | && (e->isym.st_shndx < SHN_LORESERVE | |
8307 | || e->isym.st_shndx > SHN_HIRESERVE)) | |
8308 | { | |
8309 | s = bfd_section_from_elf_index (e->input_bfd, | |
8310 | e->isym.st_shndx); | |
8311 | ||
8312 | sym.st_shndx = | |
8313 | elf_section_data (s->output_section)->this_idx; | |
8314 | sym.st_value = (s->output_section->vma | |
8315 | + s->output_offset | |
8316 | + e->isym.st_value); | |
8317 | } | |
8318 | ||
8319 | if (last_local < e->dynindx) | |
8320 | last_local = e->dynindx; | |
8321 | ||
8322 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
8323 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
8324 | } | |
8325 | } | |
8326 | ||
8327 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = | |
8328 | last_local + 1; | |
8329 | } | |
8330 | ||
8331 | /* We get the global symbols from the hash table. */ | |
8332 | eoinfo.failed = FALSE; | |
8333 | eoinfo.localsyms = FALSE; | |
8334 | eoinfo.finfo = &finfo; | |
8335 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8336 | &eoinfo); | |
8337 | if (eoinfo.failed) | |
8338 | return FALSE; | |
8339 | ||
8340 | /* If backend needs to output some symbols not present in the hash | |
8341 | table, do it now. */ | |
8342 | if (bed->elf_backend_output_arch_syms) | |
8343 | { | |
8344 | typedef bfd_boolean (*out_sym_func) | |
8345 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
8346 | struct elf_link_hash_entry *); | |
8347 | ||
8348 | if (! ((*bed->elf_backend_output_arch_syms) | |
8349 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
8350 | return FALSE; | |
8351 | } | |
8352 | ||
8353 | /* Flush all symbols to the file. */ | |
8354 | if (! elf_link_flush_output_syms (&finfo, bed)) | |
8355 | return FALSE; | |
8356 | ||
8357 | /* Now we know the size of the symtab section. */ | |
8358 | off += symtab_hdr->sh_size; | |
8359 | ||
8360 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
8361 | if (symtab_shndx_hdr->sh_name != 0) | |
8362 | { | |
8363 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
8364 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
8365 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
8366 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
8367 | symtab_shndx_hdr->sh_size = amt; | |
8368 | ||
8369 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
8370 | off, TRUE); | |
8371 | ||
8372 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
8373 | || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) | |
8374 | return FALSE; | |
8375 | } | |
8376 | ||
8377 | ||
8378 | /* Finish up and write out the symbol string table (.strtab) | |
8379 | section. */ | |
8380 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
8381 | /* sh_name was set in prep_headers. */ | |
8382 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
8383 | symstrtab_hdr->sh_flags = 0; | |
8384 | symstrtab_hdr->sh_addr = 0; | |
8385 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
8386 | symstrtab_hdr->sh_entsize = 0; | |
8387 | symstrtab_hdr->sh_link = 0; | |
8388 | symstrtab_hdr->sh_info = 0; | |
8389 | /* sh_offset is set just below. */ | |
8390 | symstrtab_hdr->sh_addralign = 1; | |
8391 | ||
8392 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); | |
8393 | elf_tdata (abfd)->next_file_pos = off; | |
8394 | ||
8395 | if (bfd_get_symcount (abfd) > 0) | |
8396 | { | |
8397 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
8398 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
8399 | return FALSE; | |
8400 | } | |
8401 | ||
8402 | /* Adjust the relocs to have the correct symbol indices. */ | |
8403 | for (o = abfd->sections; o != NULL; o = o->next) | |
8404 | { | |
8405 | if ((o->flags & SEC_RELOC) == 0) | |
8406 | continue; | |
8407 | ||
8408 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, | |
8409 | elf_section_data (o)->rel_count, | |
8410 | elf_section_data (o)->rel_hashes); | |
8411 | if (elf_section_data (o)->rel_hdr2 != NULL) | |
8412 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, | |
8413 | elf_section_data (o)->rel_count2, | |
8414 | (elf_section_data (o)->rel_hashes | |
8415 | + elf_section_data (o)->rel_count)); | |
8416 | ||
8417 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
8418 | trying to swap the relocs out itself. */ | |
8419 | o->reloc_count = 0; | |
8420 | } | |
8421 | ||
8422 | if (dynamic && info->combreloc && dynobj != NULL) | |
8423 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
8424 | ||
8425 | /* If we are linking against a dynamic object, or generating a | |
8426 | shared library, finish up the dynamic linking information. */ | |
8427 | if (dynamic) | |
8428 | { | |
8429 | bfd_byte *dyncon, *dynconend; | |
8430 | ||
8431 | /* Fix up .dynamic entries. */ | |
8432 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
8433 | BFD_ASSERT (o != NULL); | |
8434 | ||
8435 | dyncon = o->contents; | |
eea6121a | 8436 | dynconend = o->contents + o->size; |
c152c796 AM |
8437 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
8438 | { | |
8439 | Elf_Internal_Dyn dyn; | |
8440 | const char *name; | |
8441 | unsigned int type; | |
8442 | ||
8443 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
8444 | ||
8445 | switch (dyn.d_tag) | |
8446 | { | |
8447 | default: | |
8448 | continue; | |
8449 | case DT_NULL: | |
8450 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
8451 | { | |
8452 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
8453 | { | |
8454 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
8455 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
8456 | default: continue; | |
8457 | } | |
8458 | dyn.d_un.d_val = relativecount; | |
8459 | relativecount = 0; | |
8460 | break; | |
8461 | } | |
8462 | continue; | |
8463 | ||
8464 | case DT_INIT: | |
8465 | name = info->init_function; | |
8466 | goto get_sym; | |
8467 | case DT_FINI: | |
8468 | name = info->fini_function; | |
8469 | get_sym: | |
8470 | { | |
8471 | struct elf_link_hash_entry *h; | |
8472 | ||
8473 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
8474 | FALSE, FALSE, TRUE); | |
8475 | if (h != NULL | |
8476 | && (h->root.type == bfd_link_hash_defined | |
8477 | || h->root.type == bfd_link_hash_defweak)) | |
8478 | { | |
8479 | dyn.d_un.d_val = h->root.u.def.value; | |
8480 | o = h->root.u.def.section; | |
8481 | if (o->output_section != NULL) | |
8482 | dyn.d_un.d_val += (o->output_section->vma | |
8483 | + o->output_offset); | |
8484 | else | |
8485 | { | |
8486 | /* The symbol is imported from another shared | |
8487 | library and does not apply to this one. */ | |
8488 | dyn.d_un.d_val = 0; | |
8489 | } | |
8490 | break; | |
8491 | } | |
8492 | } | |
8493 | continue; | |
8494 | ||
8495 | case DT_PREINIT_ARRAYSZ: | |
8496 | name = ".preinit_array"; | |
8497 | goto get_size; | |
8498 | case DT_INIT_ARRAYSZ: | |
8499 | name = ".init_array"; | |
8500 | goto get_size; | |
8501 | case DT_FINI_ARRAYSZ: | |
8502 | name = ".fini_array"; | |
8503 | get_size: | |
8504 | o = bfd_get_section_by_name (abfd, name); | |
8505 | if (o == NULL) | |
8506 | { | |
8507 | (*_bfd_error_handler) | |
d003868e | 8508 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
8509 | goto error_return; |
8510 | } | |
eea6121a | 8511 | if (o->size == 0) |
c152c796 AM |
8512 | (*_bfd_error_handler) |
8513 | (_("warning: %s section has zero size"), name); | |
eea6121a | 8514 | dyn.d_un.d_val = o->size; |
c152c796 AM |
8515 | break; |
8516 | ||
8517 | case DT_PREINIT_ARRAY: | |
8518 | name = ".preinit_array"; | |
8519 | goto get_vma; | |
8520 | case DT_INIT_ARRAY: | |
8521 | name = ".init_array"; | |
8522 | goto get_vma; | |
8523 | case DT_FINI_ARRAY: | |
8524 | name = ".fini_array"; | |
8525 | goto get_vma; | |
8526 | ||
8527 | case DT_HASH: | |
8528 | name = ".hash"; | |
8529 | goto get_vma; | |
8530 | case DT_STRTAB: | |
8531 | name = ".dynstr"; | |
8532 | goto get_vma; | |
8533 | case DT_SYMTAB: | |
8534 | name = ".dynsym"; | |
8535 | goto get_vma; | |
8536 | case DT_VERDEF: | |
8537 | name = ".gnu.version_d"; | |
8538 | goto get_vma; | |
8539 | case DT_VERNEED: | |
8540 | name = ".gnu.version_r"; | |
8541 | goto get_vma; | |
8542 | case DT_VERSYM: | |
8543 | name = ".gnu.version"; | |
8544 | get_vma: | |
8545 | o = bfd_get_section_by_name (abfd, name); | |
8546 | if (o == NULL) | |
8547 | { | |
8548 | (*_bfd_error_handler) | |
d003868e | 8549 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
8550 | goto error_return; |
8551 | } | |
8552 | dyn.d_un.d_ptr = o->vma; | |
8553 | break; | |
8554 | ||
8555 | case DT_REL: | |
8556 | case DT_RELA: | |
8557 | case DT_RELSZ: | |
8558 | case DT_RELASZ: | |
8559 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
8560 | type = SHT_REL; | |
8561 | else | |
8562 | type = SHT_RELA; | |
8563 | dyn.d_un.d_val = 0; | |
8564 | for (i = 1; i < elf_numsections (abfd); i++) | |
8565 | { | |
8566 | Elf_Internal_Shdr *hdr; | |
8567 | ||
8568 | hdr = elf_elfsections (abfd)[i]; | |
8569 | if (hdr->sh_type == type | |
8570 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
8571 | { | |
8572 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
8573 | dyn.d_un.d_val += hdr->sh_size; | |
8574 | else | |
8575 | { | |
8576 | if (dyn.d_un.d_val == 0 | |
8577 | || hdr->sh_addr < dyn.d_un.d_val) | |
8578 | dyn.d_un.d_val = hdr->sh_addr; | |
8579 | } | |
8580 | } | |
8581 | } | |
8582 | break; | |
8583 | } | |
8584 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
8585 | } | |
8586 | } | |
8587 | ||
8588 | /* If we have created any dynamic sections, then output them. */ | |
8589 | if (dynobj != NULL) | |
8590 | { | |
8591 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
8592 | goto error_return; | |
8593 | ||
8594 | for (o = dynobj->sections; o != NULL; o = o->next) | |
8595 | { | |
8596 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 8597 | || o->size == 0 |
c152c796 AM |
8598 | || o->output_section == bfd_abs_section_ptr) |
8599 | continue; | |
8600 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
8601 | { | |
8602 | /* At this point, we are only interested in sections | |
8603 | created by _bfd_elf_link_create_dynamic_sections. */ | |
8604 | continue; | |
8605 | } | |
3722b82f AM |
8606 | if (elf_hash_table (info)->stab_info.stabstr == o) |
8607 | continue; | |
eea6121a AM |
8608 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
8609 | continue; | |
c152c796 AM |
8610 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
8611 | != SHT_STRTAB) | |
8612 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
8613 | { | |
8614 | if (! bfd_set_section_contents (abfd, o->output_section, | |
8615 | o->contents, | |
8616 | (file_ptr) o->output_offset, | |
eea6121a | 8617 | o->size)) |
c152c796 AM |
8618 | goto error_return; |
8619 | } | |
8620 | else | |
8621 | { | |
8622 | /* The contents of the .dynstr section are actually in a | |
8623 | stringtab. */ | |
8624 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
8625 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
8626 | || ! _bfd_elf_strtab_emit (abfd, | |
8627 | elf_hash_table (info)->dynstr)) | |
8628 | goto error_return; | |
8629 | } | |
8630 | } | |
8631 | } | |
8632 | ||
8633 | if (info->relocatable) | |
8634 | { | |
8635 | bfd_boolean failed = FALSE; | |
8636 | ||
8637 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
8638 | if (failed) | |
8639 | goto error_return; | |
8640 | } | |
8641 | ||
8642 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 8643 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
8644 | { |
8645 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
8646 | goto error_return; | |
8647 | } | |
8648 | ||
8649 | if (info->eh_frame_hdr) | |
8650 | { | |
8651 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) | |
8652 | goto error_return; | |
8653 | } | |
8654 | ||
8655 | if (finfo.symstrtab != NULL) | |
8656 | _bfd_stringtab_free (finfo.symstrtab); | |
8657 | if (finfo.contents != NULL) | |
8658 | free (finfo.contents); | |
8659 | if (finfo.external_relocs != NULL) | |
8660 | free (finfo.external_relocs); | |
8661 | if (finfo.internal_relocs != NULL) | |
8662 | free (finfo.internal_relocs); | |
8663 | if (finfo.external_syms != NULL) | |
8664 | free (finfo.external_syms); | |
8665 | if (finfo.locsym_shndx != NULL) | |
8666 | free (finfo.locsym_shndx); | |
8667 | if (finfo.internal_syms != NULL) | |
8668 | free (finfo.internal_syms); | |
8669 | if (finfo.indices != NULL) | |
8670 | free (finfo.indices); | |
8671 | if (finfo.sections != NULL) | |
8672 | free (finfo.sections); | |
8673 | if (finfo.symbuf != NULL) | |
8674 | free (finfo.symbuf); | |
8675 | if (finfo.symshndxbuf != NULL) | |
8676 | free (finfo.symshndxbuf); | |
8677 | for (o = abfd->sections; o != NULL; o = o->next) | |
8678 | { | |
8679 | if ((o->flags & SEC_RELOC) != 0 | |
8680 | && elf_section_data (o)->rel_hashes != NULL) | |
8681 | free (elf_section_data (o)->rel_hashes); | |
8682 | } | |
8683 | ||
8684 | elf_tdata (abfd)->linker = TRUE; | |
8685 | ||
8686 | return TRUE; | |
8687 | ||
8688 | error_return: | |
8689 | if (finfo.symstrtab != NULL) | |
8690 | _bfd_stringtab_free (finfo.symstrtab); | |
8691 | if (finfo.contents != NULL) | |
8692 | free (finfo.contents); | |
8693 | if (finfo.external_relocs != NULL) | |
8694 | free (finfo.external_relocs); | |
8695 | if (finfo.internal_relocs != NULL) | |
8696 | free (finfo.internal_relocs); | |
8697 | if (finfo.external_syms != NULL) | |
8698 | free (finfo.external_syms); | |
8699 | if (finfo.locsym_shndx != NULL) | |
8700 | free (finfo.locsym_shndx); | |
8701 | if (finfo.internal_syms != NULL) | |
8702 | free (finfo.internal_syms); | |
8703 | if (finfo.indices != NULL) | |
8704 | free (finfo.indices); | |
8705 | if (finfo.sections != NULL) | |
8706 | free (finfo.sections); | |
8707 | if (finfo.symbuf != NULL) | |
8708 | free (finfo.symbuf); | |
8709 | if (finfo.symshndxbuf != NULL) | |
8710 | free (finfo.symshndxbuf); | |
8711 | for (o = abfd->sections; o != NULL; o = o->next) | |
8712 | { | |
8713 | if ((o->flags & SEC_RELOC) != 0 | |
8714 | && elf_section_data (o)->rel_hashes != NULL) | |
8715 | free (elf_section_data (o)->rel_hashes); | |
8716 | } | |
8717 | ||
8718 | return FALSE; | |
8719 | } | |
8720 | \f | |
8721 | /* Garbage collect unused sections. */ | |
8722 | ||
8723 | /* The mark phase of garbage collection. For a given section, mark | |
8724 | it and any sections in this section's group, and all the sections | |
8725 | which define symbols to which it refers. */ | |
8726 | ||
8727 | typedef asection * (*gc_mark_hook_fn) | |
8728 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
8729 | struct elf_link_hash_entry *, Elf_Internal_Sym *); | |
8730 | ||
ccfa59ea AM |
8731 | bfd_boolean |
8732 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
8733 | asection *sec, | |
8734 | gc_mark_hook_fn gc_mark_hook) | |
c152c796 AM |
8735 | { |
8736 | bfd_boolean ret; | |
39c2f51b | 8737 | bfd_boolean is_eh; |
c152c796 AM |
8738 | asection *group_sec; |
8739 | ||
8740 | sec->gc_mark = 1; | |
8741 | ||
8742 | /* Mark all the sections in the group. */ | |
8743 | group_sec = elf_section_data (sec)->next_in_group; | |
8744 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 8745 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
8746 | return FALSE; |
8747 | ||
8748 | /* Look through the section relocs. */ | |
8749 | ret = TRUE; | |
39c2f51b | 8750 | is_eh = strcmp (sec->name, ".eh_frame") == 0; |
c152c796 AM |
8751 | if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0) |
8752 | { | |
8753 | Elf_Internal_Rela *relstart, *rel, *relend; | |
8754 | Elf_Internal_Shdr *symtab_hdr; | |
8755 | struct elf_link_hash_entry **sym_hashes; | |
8756 | size_t nlocsyms; | |
8757 | size_t extsymoff; | |
8758 | bfd *input_bfd = sec->owner; | |
8759 | const struct elf_backend_data *bed = get_elf_backend_data (input_bfd); | |
8760 | Elf_Internal_Sym *isym = NULL; | |
8761 | int r_sym_shift; | |
8762 | ||
8763 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
8764 | sym_hashes = elf_sym_hashes (input_bfd); | |
8765 | ||
8766 | /* Read the local symbols. */ | |
8767 | if (elf_bad_symtab (input_bfd)) | |
8768 | { | |
8769 | nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
8770 | extsymoff = 0; | |
8771 | } | |
8772 | else | |
8773 | extsymoff = nlocsyms = symtab_hdr->sh_info; | |
8774 | ||
8775 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
8776 | if (isym == NULL && nlocsyms != 0) | |
8777 | { | |
8778 | isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0, | |
8779 | NULL, NULL, NULL); | |
8780 | if (isym == NULL) | |
8781 | return FALSE; | |
8782 | } | |
8783 | ||
8784 | /* Read the relocations. */ | |
8785 | relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL, | |
8786 | info->keep_memory); | |
8787 | if (relstart == NULL) | |
8788 | { | |
8789 | ret = FALSE; | |
8790 | goto out1; | |
8791 | } | |
8792 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
8793 | ||
8794 | if (bed->s->arch_size == 32) | |
8795 | r_sym_shift = 8; | |
8796 | else | |
8797 | r_sym_shift = 32; | |
8798 | ||
8799 | for (rel = relstart; rel < relend; rel++) | |
8800 | { | |
8801 | unsigned long r_symndx; | |
8802 | asection *rsec; | |
8803 | struct elf_link_hash_entry *h; | |
8804 | ||
8805 | r_symndx = rel->r_info >> r_sym_shift; | |
8806 | if (r_symndx == 0) | |
8807 | continue; | |
8808 | ||
8809 | if (r_symndx >= nlocsyms | |
8810 | || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL) | |
8811 | { | |
8812 | h = sym_hashes[r_symndx - extsymoff]; | |
20f0a1ad AM |
8813 | while (h->root.type == bfd_link_hash_indirect |
8814 | || h->root.type == bfd_link_hash_warning) | |
8815 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 AM |
8816 | rsec = (*gc_mark_hook) (sec, info, rel, h, NULL); |
8817 | } | |
8818 | else | |
8819 | { | |
8820 | rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]); | |
8821 | } | |
8822 | ||
8823 | if (rsec && !rsec->gc_mark) | |
8824 | { | |
8825 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
8826 | rsec->gc_mark = 1; | |
39c2f51b AM |
8827 | else if (is_eh) |
8828 | rsec->gc_mark_from_eh = 1; | |
ccfa59ea | 8829 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) |
c152c796 AM |
8830 | { |
8831 | ret = FALSE; | |
8832 | goto out2; | |
8833 | } | |
8834 | } | |
8835 | } | |
8836 | ||
8837 | out2: | |
8838 | if (elf_section_data (sec)->relocs != relstart) | |
8839 | free (relstart); | |
8840 | out1: | |
8841 | if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym) | |
8842 | { | |
8843 | if (! info->keep_memory) | |
8844 | free (isym); | |
8845 | else | |
8846 | symtab_hdr->contents = (unsigned char *) isym; | |
8847 | } | |
8848 | } | |
8849 | ||
8850 | return ret; | |
8851 | } | |
8852 | ||
8853 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ | |
8854 | ||
8855 | static bfd_boolean | |
8856 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr) | |
8857 | { | |
8858 | int *idx = idxptr; | |
8859 | ||
8860 | if (h->root.type == bfd_link_hash_warning) | |
8861 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8862 | ||
8863 | if (h->dynindx != -1 | |
8864 | && ((h->root.type != bfd_link_hash_defined | |
8865 | && h->root.type != bfd_link_hash_defweak) | |
8866 | || h->root.u.def.section->gc_mark)) | |
8867 | h->dynindx = (*idx)++; | |
8868 | ||
8869 | return TRUE; | |
8870 | } | |
8871 | ||
8872 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
8873 | ||
8874 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
8875 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
8876 | ||
8877 | static bfd_boolean | |
8878 | elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook) | |
8879 | { | |
8880 | bfd *sub; | |
8881 | ||
8882 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8883 | { | |
8884 | asection *o; | |
8885 | ||
8886 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
8887 | continue; | |
8888 | ||
8889 | for (o = sub->sections; o != NULL; o = o->next) | |
8890 | { | |
7c2c8505 AM |
8891 | /* Keep debug and special sections. */ |
8892 | if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 | |
8893 | || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0) | |
c152c796 AM |
8894 | o->gc_mark = 1; |
8895 | ||
8896 | if (o->gc_mark) | |
8897 | continue; | |
8898 | ||
8899 | /* Skip sweeping sections already excluded. */ | |
8900 | if (o->flags & SEC_EXCLUDE) | |
8901 | continue; | |
8902 | ||
8903 | /* Since this is early in the link process, it is simple | |
8904 | to remove a section from the output. */ | |
8905 | o->flags |= SEC_EXCLUDE; | |
8906 | ||
8907 | /* But we also have to update some of the relocation | |
8908 | info we collected before. */ | |
8909 | if (gc_sweep_hook | |
e8aaee2a AM |
8910 | && (o->flags & SEC_RELOC) != 0 |
8911 | && o->reloc_count > 0 | |
8912 | && !bfd_is_abs_section (o->output_section)) | |
c152c796 AM |
8913 | { |
8914 | Elf_Internal_Rela *internal_relocs; | |
8915 | bfd_boolean r; | |
8916 | ||
8917 | internal_relocs | |
8918 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
8919 | info->keep_memory); | |
8920 | if (internal_relocs == NULL) | |
8921 | return FALSE; | |
8922 | ||
8923 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
8924 | ||
8925 | if (elf_section_data (o)->relocs != internal_relocs) | |
8926 | free (internal_relocs); | |
8927 | ||
8928 | if (!r) | |
8929 | return FALSE; | |
8930 | } | |
8931 | } | |
8932 | } | |
8933 | ||
8934 | /* Remove the symbols that were in the swept sections from the dynamic | |
8935 | symbol table. GCFIXME: Anyone know how to get them out of the | |
8936 | static symbol table as well? */ | |
8937 | { | |
8938 | int i = 0; | |
8939 | ||
8940 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i); | |
8941 | ||
1b9ee6a2 DD |
8942 | /* There is an unused NULL entry at the head of the table which |
8943 | we must account for in our count. Unless there weren't any | |
8944 | symbols, which means we'll have no table at all. */ | |
8945 | if (i != 0) | |
8946 | ++i; | |
8947 | ||
c152c796 AM |
8948 | elf_hash_table (info)->dynsymcount = i; |
8949 | } | |
8950 | ||
8951 | return TRUE; | |
8952 | } | |
8953 | ||
8954 | /* Propagate collected vtable information. This is called through | |
8955 | elf_link_hash_traverse. */ | |
8956 | ||
8957 | static bfd_boolean | |
8958 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
8959 | { | |
8960 | if (h->root.type == bfd_link_hash_warning) | |
8961 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8962 | ||
8963 | /* Those that are not vtables. */ | |
f6e332e6 | 8964 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
8965 | return TRUE; |
8966 | ||
8967 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 8968 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
8969 | return TRUE; |
8970 | ||
8971 | /* If we've already been done, exit. */ | |
f6e332e6 | 8972 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
8973 | return TRUE; |
8974 | ||
8975 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 8976 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 8977 | |
f6e332e6 | 8978 | if (h->vtable->used == NULL) |
c152c796 AM |
8979 | { |
8980 | /* None of this table's entries were referenced. Re-use the | |
8981 | parent's table. */ | |
f6e332e6 AM |
8982 | h->vtable->used = h->vtable->parent->vtable->used; |
8983 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
8984 | } |
8985 | else | |
8986 | { | |
8987 | size_t n; | |
8988 | bfd_boolean *cu, *pu; | |
8989 | ||
8990 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 8991 | cu = h->vtable->used; |
c152c796 | 8992 | cu[-1] = TRUE; |
f6e332e6 | 8993 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
8994 | if (pu != NULL) |
8995 | { | |
8996 | const struct elf_backend_data *bed; | |
8997 | unsigned int log_file_align; | |
8998 | ||
8999 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
9000 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 9001 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
9002 | while (n--) |
9003 | { | |
9004 | if (*pu) | |
9005 | *cu = TRUE; | |
9006 | pu++; | |
9007 | cu++; | |
9008 | } | |
9009 | } | |
9010 | } | |
9011 | ||
9012 | return TRUE; | |
9013 | } | |
9014 | ||
9015 | static bfd_boolean | |
9016 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
9017 | { | |
9018 | asection *sec; | |
9019 | bfd_vma hstart, hend; | |
9020 | Elf_Internal_Rela *relstart, *relend, *rel; | |
9021 | const struct elf_backend_data *bed; | |
9022 | unsigned int log_file_align; | |
9023 | ||
9024 | if (h->root.type == bfd_link_hash_warning) | |
9025 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9026 | ||
9027 | /* Take care of both those symbols that do not describe vtables as | |
9028 | well as those that are not loaded. */ | |
f6e332e6 | 9029 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
9030 | return TRUE; |
9031 | ||
9032 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
9033 | || h->root.type == bfd_link_hash_defweak); | |
9034 | ||
9035 | sec = h->root.u.def.section; | |
9036 | hstart = h->root.u.def.value; | |
9037 | hend = hstart + h->size; | |
9038 | ||
9039 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
9040 | if (!relstart) | |
9041 | return *(bfd_boolean *) okp = FALSE; | |
9042 | bed = get_elf_backend_data (sec->owner); | |
9043 | log_file_align = bed->s->log_file_align; | |
9044 | ||
9045 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
9046 | ||
9047 | for (rel = relstart; rel < relend; ++rel) | |
9048 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
9049 | { | |
9050 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
9051 | if (h->vtable->used |
9052 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
9053 | { |
9054 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 9055 | if (h->vtable->used[entry]) |
c152c796 AM |
9056 | continue; |
9057 | } | |
9058 | /* Otherwise, kill it. */ | |
9059 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
9060 | } | |
9061 | ||
9062 | return TRUE; | |
9063 | } | |
9064 | ||
87538722 AM |
9065 | /* Mark sections containing dynamically referenced symbols. When |
9066 | building shared libraries, we must assume that any visible symbol is | |
9067 | referenced. */ | |
715df9b8 EB |
9068 | |
9069 | static bfd_boolean | |
87538722 | 9070 | elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) |
715df9b8 | 9071 | { |
87538722 AM |
9072 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
9073 | ||
715df9b8 EB |
9074 | if (h->root.type == bfd_link_hash_warning) |
9075 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9076 | ||
9077 | if ((h->root.type == bfd_link_hash_defined | |
9078 | || h->root.type == bfd_link_hash_defweak) | |
87538722 AM |
9079 | && (h->ref_dynamic |
9080 | || (info->shared | |
9081 | && h->def_regular | |
9082 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL | |
9083 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN))) | |
715df9b8 EB |
9084 | h->root.u.def.section->flags |= SEC_KEEP; |
9085 | ||
9086 | return TRUE; | |
9087 | } | |
3b36f7e6 | 9088 | |
c152c796 AM |
9089 | /* Do mark and sweep of unused sections. */ |
9090 | ||
9091 | bfd_boolean | |
9092 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
9093 | { | |
9094 | bfd_boolean ok = TRUE; | |
9095 | bfd *sub; | |
9096 | asection * (*gc_mark_hook) | |
9097 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
9098 | struct elf_link_hash_entry *h, Elf_Internal_Sym *); | |
9099 | ||
9100 | if (!get_elf_backend_data (abfd)->can_gc_sections | |
9101 | || info->relocatable | |
9102 | || info->emitrelocations | |
715df9b8 | 9103 | || !is_elf_hash_table (info->hash)) |
c152c796 AM |
9104 | { |
9105 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
9106 | return TRUE; | |
9107 | } | |
9108 | ||
9109 | /* Apply transitive closure to the vtable entry usage info. */ | |
9110 | elf_link_hash_traverse (elf_hash_table (info), | |
9111 | elf_gc_propagate_vtable_entries_used, | |
9112 | &ok); | |
9113 | if (!ok) | |
9114 | return FALSE; | |
9115 | ||
9116 | /* Kill the vtable relocations that were not used. */ | |
9117 | elf_link_hash_traverse (elf_hash_table (info), | |
9118 | elf_gc_smash_unused_vtentry_relocs, | |
9119 | &ok); | |
9120 | if (!ok) | |
9121 | return FALSE; | |
9122 | ||
715df9b8 EB |
9123 | /* Mark dynamically referenced symbols. */ |
9124 | if (elf_hash_table (info)->dynamic_sections_created) | |
9125 | elf_link_hash_traverse (elf_hash_table (info), | |
9126 | elf_gc_mark_dynamic_ref_symbol, | |
87538722 | 9127 | info); |
c152c796 | 9128 | |
715df9b8 | 9129 | /* Grovel through relocs to find out who stays ... */ |
c152c796 AM |
9130 | gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook; |
9131 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
9132 | { | |
9133 | asection *o; | |
9134 | ||
9135 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
9136 | continue; | |
9137 | ||
9138 | for (o = sub->sections; o != NULL; o = o->next) | |
39c2f51b AM |
9139 | if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark) |
9140 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
9141 | return FALSE; | |
c152c796 AM |
9142 | } |
9143 | ||
9e8cc8b4 AM |
9144 | /* ... again for sections marked from eh_frame. */ |
9145 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
9146 | { | |
9147 | asection *o; | |
9148 | ||
9149 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
9150 | continue; | |
9151 | ||
9152 | /* Keep .gcc_except_table.* if the associated .text.* is | |
9153 | marked. This isn't very nice, but the proper solution, | |
9154 | splitting .eh_frame up and using comdat doesn't pan out | |
9155 | easily due to needing special relocs to handle the | |
9156 | difference of two symbols in separate sections. | |
9157 | Don't keep code sections referenced by .eh_frame. */ | |
9158 | for (o = sub->sections; o != NULL; o = o->next) | |
9159 | if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0) | |
9160 | { | |
9161 | if (strncmp (o->name, ".gcc_except_table.", 18) == 0) | |
9162 | { | |
9163 | unsigned long len; | |
9164 | char *fn_name; | |
9165 | asection *fn_text; | |
9166 | ||
9167 | len = strlen (o->name + 18) + 1; | |
9168 | fn_name = bfd_malloc (len + 6); | |
9169 | if (fn_name == NULL) | |
9170 | return FALSE; | |
9171 | memcpy (fn_name, ".text.", 6); | |
9172 | memcpy (fn_name + 6, o->name + 18, len); | |
9173 | fn_text = bfd_get_section_by_name (sub, fn_name); | |
9174 | free (fn_name); | |
9175 | if (fn_text == NULL || !fn_text->gc_mark) | |
9176 | continue; | |
9177 | } | |
9178 | ||
9179 | /* If not using specially named exception table section, | |
9180 | then keep whatever we are using. */ | |
9181 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
9182 | return FALSE; | |
9183 | } | |
9184 | } | |
9185 | ||
c152c796 AM |
9186 | /* ... and mark SEC_EXCLUDE for those that go. */ |
9187 | if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook)) | |
9188 | return FALSE; | |
9189 | ||
9190 | return TRUE; | |
9191 | } | |
9192 | \f | |
9193 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
9194 | ||
9195 | bfd_boolean | |
9196 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
9197 | asection *sec, | |
9198 | struct elf_link_hash_entry *h, | |
9199 | bfd_vma offset) | |
9200 | { | |
9201 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
9202 | struct elf_link_hash_entry **search, *child; | |
9203 | bfd_size_type extsymcount; | |
9204 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9205 | ||
9206 | /* The sh_info field of the symtab header tells us where the | |
9207 | external symbols start. We don't care about the local symbols at | |
9208 | this point. */ | |
9209 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
9210 | if (!elf_bad_symtab (abfd)) | |
9211 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
9212 | ||
9213 | sym_hashes = elf_sym_hashes (abfd); | |
9214 | sym_hashes_end = sym_hashes + extsymcount; | |
9215 | ||
9216 | /* Hunt down the child symbol, which is in this section at the same | |
9217 | offset as the relocation. */ | |
9218 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
9219 | { | |
9220 | if ((child = *search) != NULL | |
9221 | && (child->root.type == bfd_link_hash_defined | |
9222 | || child->root.type == bfd_link_hash_defweak) | |
9223 | && child->root.u.def.section == sec | |
9224 | && child->root.u.def.value == offset) | |
9225 | goto win; | |
9226 | } | |
9227 | ||
d003868e AM |
9228 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
9229 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
9230 | bfd_set_error (bfd_error_invalid_operation); |
9231 | return FALSE; | |
9232 | ||
9233 | win: | |
f6e332e6 AM |
9234 | if (!child->vtable) |
9235 | { | |
9236 | child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable)); | |
9237 | if (!child->vtable) | |
9238 | return FALSE; | |
9239 | } | |
c152c796 AM |
9240 | if (!h) |
9241 | { | |
9242 | /* This *should* only be the absolute section. It could potentially | |
9243 | be that someone has defined a non-global vtable though, which | |
9244 | would be bad. It isn't worth paging in the local symbols to be | |
9245 | sure though; that case should simply be handled by the assembler. */ | |
9246 | ||
f6e332e6 | 9247 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
9248 | } |
9249 | else | |
f6e332e6 | 9250 | child->vtable->parent = h; |
c152c796 AM |
9251 | |
9252 | return TRUE; | |
9253 | } | |
9254 | ||
9255 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
9256 | ||
9257 | bfd_boolean | |
9258 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
9259 | asection *sec ATTRIBUTE_UNUSED, | |
9260 | struct elf_link_hash_entry *h, | |
9261 | bfd_vma addend) | |
9262 | { | |
9263 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9264 | unsigned int log_file_align = bed->s->log_file_align; | |
9265 | ||
f6e332e6 AM |
9266 | if (!h->vtable) |
9267 | { | |
9268 | h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable)); | |
9269 | if (!h->vtable) | |
9270 | return FALSE; | |
9271 | } | |
9272 | ||
9273 | if (addend >= h->vtable->size) | |
c152c796 AM |
9274 | { |
9275 | size_t size, bytes, file_align; | |
f6e332e6 | 9276 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
9277 | |
9278 | /* While the symbol is undefined, we have to be prepared to handle | |
9279 | a zero size. */ | |
9280 | file_align = 1 << log_file_align; | |
9281 | if (h->root.type == bfd_link_hash_undefined) | |
9282 | size = addend + file_align; | |
9283 | else | |
9284 | { | |
9285 | size = h->size; | |
9286 | if (addend >= size) | |
9287 | { | |
9288 | /* Oops! We've got a reference past the defined end of | |
9289 | the table. This is probably a bug -- shall we warn? */ | |
9290 | size = addend + file_align; | |
9291 | } | |
9292 | } | |
9293 | size = (size + file_align - 1) & -file_align; | |
9294 | ||
9295 | /* Allocate one extra entry for use as a "done" flag for the | |
9296 | consolidation pass. */ | |
9297 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
9298 | ||
9299 | if (ptr) | |
9300 | { | |
9301 | ptr = bfd_realloc (ptr - 1, bytes); | |
9302 | ||
9303 | if (ptr != NULL) | |
9304 | { | |
9305 | size_t oldbytes; | |
9306 | ||
f6e332e6 | 9307 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
9308 | * sizeof (bfd_boolean)); |
9309 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
9310 | } | |
9311 | } | |
9312 | else | |
9313 | ptr = bfd_zmalloc (bytes); | |
9314 | ||
9315 | if (ptr == NULL) | |
9316 | return FALSE; | |
9317 | ||
9318 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
9319 | h->vtable->used = ptr + 1; |
9320 | h->vtable->size = size; | |
c152c796 AM |
9321 | } |
9322 | ||
f6e332e6 | 9323 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
9324 | |
9325 | return TRUE; | |
9326 | } | |
9327 | ||
9328 | struct alloc_got_off_arg { | |
9329 | bfd_vma gotoff; | |
9330 | unsigned int got_elt_size; | |
9331 | }; | |
9332 | ||
9333 | /* We need a special top-level link routine to convert got reference counts | |
9334 | to real got offsets. */ | |
9335 | ||
9336 | static bfd_boolean | |
9337 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
9338 | { | |
9339 | struct alloc_got_off_arg *gofarg = arg; | |
9340 | ||
9341 | if (h->root.type == bfd_link_hash_warning) | |
9342 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9343 | ||
9344 | if (h->got.refcount > 0) | |
9345 | { | |
9346 | h->got.offset = gofarg->gotoff; | |
9347 | gofarg->gotoff += gofarg->got_elt_size; | |
9348 | } | |
9349 | else | |
9350 | h->got.offset = (bfd_vma) -1; | |
9351 | ||
9352 | return TRUE; | |
9353 | } | |
9354 | ||
9355 | /* And an accompanying bit to work out final got entry offsets once | |
9356 | we're done. Should be called from final_link. */ | |
9357 | ||
9358 | bfd_boolean | |
9359 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
9360 | struct bfd_link_info *info) | |
9361 | { | |
9362 | bfd *i; | |
9363 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9364 | bfd_vma gotoff; | |
9365 | unsigned int got_elt_size = bed->s->arch_size / 8; | |
9366 | struct alloc_got_off_arg gofarg; | |
9367 | ||
9368 | if (! is_elf_hash_table (info->hash)) | |
9369 | return FALSE; | |
9370 | ||
9371 | /* The GOT offset is relative to the .got section, but the GOT header is | |
9372 | put into the .got.plt section, if the backend uses it. */ | |
9373 | if (bed->want_got_plt) | |
9374 | gotoff = 0; | |
9375 | else | |
9376 | gotoff = bed->got_header_size; | |
9377 | ||
9378 | /* Do the local .got entries first. */ | |
9379 | for (i = info->input_bfds; i; i = i->link_next) | |
9380 | { | |
9381 | bfd_signed_vma *local_got; | |
9382 | bfd_size_type j, locsymcount; | |
9383 | Elf_Internal_Shdr *symtab_hdr; | |
9384 | ||
9385 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
9386 | continue; | |
9387 | ||
9388 | local_got = elf_local_got_refcounts (i); | |
9389 | if (!local_got) | |
9390 | continue; | |
9391 | ||
9392 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
9393 | if (elf_bad_symtab (i)) | |
9394 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9395 | else | |
9396 | locsymcount = symtab_hdr->sh_info; | |
9397 | ||
9398 | for (j = 0; j < locsymcount; ++j) | |
9399 | { | |
9400 | if (local_got[j] > 0) | |
9401 | { | |
9402 | local_got[j] = gotoff; | |
9403 | gotoff += got_elt_size; | |
9404 | } | |
9405 | else | |
9406 | local_got[j] = (bfd_vma) -1; | |
9407 | } | |
9408 | } | |
9409 | ||
9410 | /* Then the global .got entries. .plt refcounts are handled by | |
9411 | adjust_dynamic_symbol */ | |
9412 | gofarg.gotoff = gotoff; | |
9413 | gofarg.got_elt_size = got_elt_size; | |
9414 | elf_link_hash_traverse (elf_hash_table (info), | |
9415 | elf_gc_allocate_got_offsets, | |
9416 | &gofarg); | |
9417 | return TRUE; | |
9418 | } | |
9419 | ||
9420 | /* Many folk need no more in the way of final link than this, once | |
9421 | got entry reference counting is enabled. */ | |
9422 | ||
9423 | bfd_boolean | |
9424 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
9425 | { | |
9426 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
9427 | return FALSE; | |
9428 | ||
9429 | /* Invoke the regular ELF backend linker to do all the work. */ | |
9430 | return bfd_elf_final_link (abfd, info); | |
9431 | } | |
9432 | ||
9433 | bfd_boolean | |
9434 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
9435 | { | |
9436 | struct elf_reloc_cookie *rcookie = cookie; | |
9437 | ||
9438 | if (rcookie->bad_symtab) | |
9439 | rcookie->rel = rcookie->rels; | |
9440 | ||
9441 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
9442 | { | |
9443 | unsigned long r_symndx; | |
9444 | ||
9445 | if (! rcookie->bad_symtab) | |
9446 | if (rcookie->rel->r_offset > offset) | |
9447 | return FALSE; | |
9448 | if (rcookie->rel->r_offset != offset) | |
9449 | continue; | |
9450 | ||
9451 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
9452 | if (r_symndx == SHN_UNDEF) | |
9453 | return TRUE; | |
9454 | ||
9455 | if (r_symndx >= rcookie->locsymcount | |
9456 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
9457 | { | |
9458 | struct elf_link_hash_entry *h; | |
9459 | ||
9460 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
9461 | ||
9462 | while (h->root.type == bfd_link_hash_indirect | |
9463 | || h->root.type == bfd_link_hash_warning) | |
9464 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9465 | ||
9466 | if ((h->root.type == bfd_link_hash_defined | |
9467 | || h->root.type == bfd_link_hash_defweak) | |
9468 | && elf_discarded_section (h->root.u.def.section)) | |
9469 | return TRUE; | |
9470 | else | |
9471 | return FALSE; | |
9472 | } | |
9473 | else | |
9474 | { | |
9475 | /* It's not a relocation against a global symbol, | |
9476 | but it could be a relocation against a local | |
9477 | symbol for a discarded section. */ | |
9478 | asection *isec; | |
9479 | Elf_Internal_Sym *isym; | |
9480 | ||
9481 | /* Need to: get the symbol; get the section. */ | |
9482 | isym = &rcookie->locsyms[r_symndx]; | |
9483 | if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
9484 | { | |
9485 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); | |
9486 | if (isec != NULL && elf_discarded_section (isec)) | |
9487 | return TRUE; | |
9488 | } | |
9489 | } | |
9490 | return FALSE; | |
9491 | } | |
9492 | return FALSE; | |
9493 | } | |
9494 | ||
9495 | /* Discard unneeded references to discarded sections. | |
9496 | Returns TRUE if any section's size was changed. */ | |
9497 | /* This function assumes that the relocations are in sorted order, | |
9498 | which is true for all known assemblers. */ | |
9499 | ||
9500 | bfd_boolean | |
9501 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) | |
9502 | { | |
9503 | struct elf_reloc_cookie cookie; | |
9504 | asection *stab, *eh; | |
9505 | Elf_Internal_Shdr *symtab_hdr; | |
9506 | const struct elf_backend_data *bed; | |
9507 | bfd *abfd; | |
9508 | unsigned int count; | |
9509 | bfd_boolean ret = FALSE; | |
9510 | ||
9511 | if (info->traditional_format | |
9512 | || !is_elf_hash_table (info->hash)) | |
9513 | return FALSE; | |
9514 | ||
9515 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) | |
9516 | { | |
9517 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
9518 | continue; | |
9519 | ||
9520 | bed = get_elf_backend_data (abfd); | |
9521 | ||
9522 | if ((abfd->flags & DYNAMIC) != 0) | |
9523 | continue; | |
9524 | ||
9525 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); | |
9526 | if (info->relocatable | |
9527 | || (eh != NULL | |
eea6121a | 9528 | && (eh->size == 0 |
c152c796 AM |
9529 | || bfd_is_abs_section (eh->output_section)))) |
9530 | eh = NULL; | |
9531 | ||
9532 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
9533 | if (stab != NULL | |
eea6121a | 9534 | && (stab->size == 0 |
c152c796 AM |
9535 | || bfd_is_abs_section (stab->output_section) |
9536 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) | |
9537 | stab = NULL; | |
9538 | ||
9539 | if (stab == NULL | |
9540 | && eh == NULL | |
9541 | && bed->elf_backend_discard_info == NULL) | |
9542 | continue; | |
9543 | ||
9544 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
9545 | cookie.abfd = abfd; | |
9546 | cookie.sym_hashes = elf_sym_hashes (abfd); | |
9547 | cookie.bad_symtab = elf_bad_symtab (abfd); | |
9548 | if (cookie.bad_symtab) | |
9549 | { | |
9550 | cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9551 | cookie.extsymoff = 0; | |
9552 | } | |
9553 | else | |
9554 | { | |
9555 | cookie.locsymcount = symtab_hdr->sh_info; | |
9556 | cookie.extsymoff = symtab_hdr->sh_info; | |
9557 | } | |
9558 | ||
9559 | if (bed->s->arch_size == 32) | |
9560 | cookie.r_sym_shift = 8; | |
9561 | else | |
9562 | cookie.r_sym_shift = 32; | |
9563 | ||
9564 | cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
9565 | if (cookie.locsyms == NULL && cookie.locsymcount != 0) | |
9566 | { | |
9567 | cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
9568 | cookie.locsymcount, 0, | |
9569 | NULL, NULL, NULL); | |
9570 | if (cookie.locsyms == NULL) | |
9571 | return FALSE; | |
9572 | } | |
9573 | ||
9574 | if (stab != NULL) | |
9575 | { | |
9576 | cookie.rels = NULL; | |
9577 | count = stab->reloc_count; | |
9578 | if (count != 0) | |
9579 | cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL, | |
9580 | info->keep_memory); | |
9581 | if (cookie.rels != NULL) | |
9582 | { | |
9583 | cookie.rel = cookie.rels; | |
9584 | cookie.relend = cookie.rels; | |
9585 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
9586 | if (_bfd_discard_section_stabs (abfd, stab, | |
9587 | elf_section_data (stab)->sec_info, | |
9588 | bfd_elf_reloc_symbol_deleted_p, | |
9589 | &cookie)) | |
9590 | ret = TRUE; | |
9591 | if (elf_section_data (stab)->relocs != cookie.rels) | |
9592 | free (cookie.rels); | |
9593 | } | |
9594 | } | |
9595 | ||
9596 | if (eh != NULL) | |
9597 | { | |
9598 | cookie.rels = NULL; | |
9599 | count = eh->reloc_count; | |
9600 | if (count != 0) | |
9601 | cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL, | |
9602 | info->keep_memory); | |
9603 | cookie.rel = cookie.rels; | |
9604 | cookie.relend = cookie.rels; | |
9605 | if (cookie.rels != NULL) | |
9606 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
9607 | ||
9608 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, | |
9609 | bfd_elf_reloc_symbol_deleted_p, | |
9610 | &cookie)) | |
9611 | ret = TRUE; | |
9612 | ||
9613 | if (cookie.rels != NULL | |
9614 | && elf_section_data (eh)->relocs != cookie.rels) | |
9615 | free (cookie.rels); | |
9616 | } | |
9617 | ||
9618 | if (bed->elf_backend_discard_info != NULL | |
9619 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
9620 | ret = TRUE; | |
9621 | ||
9622 | if (cookie.locsyms != NULL | |
9623 | && symtab_hdr->contents != (unsigned char *) cookie.locsyms) | |
9624 | { | |
9625 | if (! info->keep_memory) | |
9626 | free (cookie.locsyms); | |
9627 | else | |
9628 | symtab_hdr->contents = (unsigned char *) cookie.locsyms; | |
9629 | } | |
9630 | } | |
9631 | ||
9632 | if (info->eh_frame_hdr | |
9633 | && !info->relocatable | |
9634 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
9635 | ret = TRUE; | |
9636 | ||
9637 | return ret; | |
9638 | } | |
082b7297 L |
9639 | |
9640 | void | |
9641 | _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec) | |
9642 | { | |
9643 | flagword flags; | |
6d2cd210 | 9644 | const char *name, *p; |
082b7297 L |
9645 | struct bfd_section_already_linked *l; |
9646 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
3d7f7666 L |
9647 | asection *group; |
9648 | ||
9649 | /* A single member comdat group section may be discarded by a | |
9650 | linkonce section. See below. */ | |
9651 | if (sec->output_section == bfd_abs_section_ptr) | |
9652 | return; | |
082b7297 L |
9653 | |
9654 | flags = sec->flags; | |
3d7f7666 L |
9655 | |
9656 | /* Check if it belongs to a section group. */ | |
9657 | group = elf_sec_group (sec); | |
9658 | ||
9659 | /* Return if it isn't a linkonce section nor a member of a group. A | |
9660 | comdat group section also has SEC_LINK_ONCE set. */ | |
9661 | if ((flags & SEC_LINK_ONCE) == 0 && group == NULL) | |
082b7297 L |
9662 | return; |
9663 | ||
3d7f7666 L |
9664 | if (group) |
9665 | { | |
9666 | /* If this is the member of a single member comdat group, check if | |
9667 | the group should be discarded. */ | |
9668 | if (elf_next_in_group (sec) == sec | |
9669 | && (group->flags & SEC_LINK_ONCE) != 0) | |
9670 | sec = group; | |
9671 | else | |
9672 | return; | |
9673 | } | |
9674 | ||
082b7297 L |
9675 | /* FIXME: When doing a relocatable link, we may have trouble |
9676 | copying relocations in other sections that refer to local symbols | |
9677 | in the section being discarded. Those relocations will have to | |
9678 | be converted somehow; as of this writing I'm not sure that any of | |
9679 | the backends handle that correctly. | |
9680 | ||
9681 | It is tempting to instead not discard link once sections when | |
9682 | doing a relocatable link (technically, they should be discarded | |
9683 | whenever we are building constructors). However, that fails, | |
9684 | because the linker winds up combining all the link once sections | |
9685 | into a single large link once section, which defeats the purpose | |
9686 | of having link once sections in the first place. | |
9687 | ||
9688 | Also, not merging link once sections in a relocatable link | |
9689 | causes trouble for MIPS ELF, which relies on link once semantics | |
9690 | to handle the .reginfo section correctly. */ | |
9691 | ||
9692 | name = bfd_get_section_name (abfd, sec); | |
9693 | ||
6d2cd210 JJ |
9694 | if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0 |
9695 | && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) | |
9696 | p++; | |
9697 | else | |
9698 | p = name; | |
9699 | ||
9700 | already_linked_list = bfd_section_already_linked_table_lookup (p); | |
082b7297 L |
9701 | |
9702 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
9703 | { | |
9704 | /* We may have 3 different sections on the list: group section, | |
9705 | comdat section and linkonce section. SEC may be a linkonce or | |
9706 | group section. We match a group section with a group section, | |
9707 | a linkonce section with a linkonce section, and ignore comdat | |
9708 | section. */ | |
3d7f7666 | 9709 | if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) |
6d2cd210 | 9710 | && strcmp (name, l->sec->name) == 0 |
082b7297 L |
9711 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL) |
9712 | { | |
9713 | /* The section has already been linked. See if we should | |
6d2cd210 | 9714 | issue a warning. */ |
082b7297 L |
9715 | switch (flags & SEC_LINK_DUPLICATES) |
9716 | { | |
9717 | default: | |
9718 | abort (); | |
9719 | ||
9720 | case SEC_LINK_DUPLICATES_DISCARD: | |
9721 | break; | |
9722 | ||
9723 | case SEC_LINK_DUPLICATES_ONE_ONLY: | |
9724 | (*_bfd_error_handler) | |
c93625e2 | 9725 | (_("%B: ignoring duplicate section `%A'"), |
d003868e | 9726 | abfd, sec); |
082b7297 L |
9727 | break; |
9728 | ||
9729 | case SEC_LINK_DUPLICATES_SAME_SIZE: | |
9730 | if (sec->size != l->sec->size) | |
9731 | (*_bfd_error_handler) | |
c93625e2 | 9732 | (_("%B: duplicate section `%A' has different size"), |
d003868e | 9733 | abfd, sec); |
082b7297 | 9734 | break; |
ea5158d8 DJ |
9735 | |
9736 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: | |
9737 | if (sec->size != l->sec->size) | |
9738 | (*_bfd_error_handler) | |
c93625e2 | 9739 | (_("%B: duplicate section `%A' has different size"), |
ea5158d8 DJ |
9740 | abfd, sec); |
9741 | else if (sec->size != 0) | |
9742 | { | |
9743 | bfd_byte *sec_contents, *l_sec_contents; | |
9744 | ||
9745 | if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents)) | |
9746 | (*_bfd_error_handler) | |
c93625e2 | 9747 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
9748 | abfd, sec); |
9749 | else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec, | |
9750 | &l_sec_contents)) | |
9751 | (*_bfd_error_handler) | |
c93625e2 | 9752 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
9753 | l->sec->owner, l->sec); |
9754 | else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) | |
9755 | (*_bfd_error_handler) | |
c93625e2 | 9756 | (_("%B: warning: duplicate section `%A' has different contents"), |
ea5158d8 DJ |
9757 | abfd, sec); |
9758 | ||
9759 | if (sec_contents) | |
9760 | free (sec_contents); | |
9761 | if (l_sec_contents) | |
9762 | free (l_sec_contents); | |
9763 | } | |
9764 | break; | |
082b7297 L |
9765 | } |
9766 | ||
9767 | /* Set the output_section field so that lang_add_section | |
9768 | does not create a lang_input_section structure for this | |
9769 | section. Since there might be a symbol in the section | |
9770 | being discarded, we must retain a pointer to the section | |
9771 | which we are really going to use. */ | |
9772 | sec->output_section = bfd_abs_section_ptr; | |
9773 | sec->kept_section = l->sec; | |
3b36f7e6 | 9774 | |
082b7297 | 9775 | if (flags & SEC_GROUP) |
3d7f7666 L |
9776 | { |
9777 | asection *first = elf_next_in_group (sec); | |
9778 | asection *s = first; | |
9779 | ||
9780 | while (s != NULL) | |
9781 | { | |
9782 | s->output_section = bfd_abs_section_ptr; | |
9783 | /* Record which group discards it. */ | |
9784 | s->kept_section = l->sec; | |
9785 | s = elf_next_in_group (s); | |
9786 | /* These lists are circular. */ | |
9787 | if (s == first) | |
9788 | break; | |
9789 | } | |
9790 | } | |
082b7297 L |
9791 | |
9792 | return; | |
9793 | } | |
9794 | } | |
9795 | ||
3d7f7666 L |
9796 | if (group) |
9797 | { | |
9798 | /* If this is the member of a single member comdat group and the | |
9799 | group hasn't be discarded, we check if it matches a linkonce | |
9800 | section. We only record the discarded comdat group. Otherwise | |
9801 | the undiscarded group will be discarded incorrectly later since | |
9802 | itself has been recorded. */ | |
6d2cd210 JJ |
9803 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
9804 | if ((l->sec->flags & SEC_GROUP) == 0 | |
9805 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL | |
9806 | && bfd_elf_match_symbols_in_sections (l->sec, | |
9807 | elf_next_in_group (sec))) | |
9808 | { | |
9809 | elf_next_in_group (sec)->output_section = bfd_abs_section_ptr; | |
9810 | elf_next_in_group (sec)->kept_section = l->sec; | |
9811 | group->output_section = bfd_abs_section_ptr; | |
9812 | break; | |
9813 | } | |
9814 | if (l == NULL) | |
3d7f7666 L |
9815 | return; |
9816 | } | |
9817 | else | |
9818 | /* There is no direct match. But for linkonce section, we should | |
9819 | check if there is a match with comdat group member. We always | |
9820 | record the linkonce section, discarded or not. */ | |
6d2cd210 JJ |
9821 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
9822 | if (l->sec->flags & SEC_GROUP) | |
9823 | { | |
9824 | asection *first = elf_next_in_group (l->sec); | |
9825 | ||
9826 | if (first != NULL | |
9827 | && elf_next_in_group (first) == first | |
9828 | && bfd_elf_match_symbols_in_sections (first, sec)) | |
9829 | { | |
9830 | sec->output_section = bfd_abs_section_ptr; | |
9831 | sec->kept_section = l->sec; | |
9832 | break; | |
9833 | } | |
9834 | } | |
9835 | ||
082b7297 L |
9836 | /* This is the first section with this name. Record it. */ |
9837 | bfd_section_already_linked_table_insert (already_linked_list, sec); | |
9838 | } | |
81e1b023 | 9839 | |
a4d8e49b L |
9840 | bfd_boolean |
9841 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
9842 | { | |
9843 | return sym->st_shndx == SHN_COMMON; | |
9844 | } | |
9845 | ||
9846 | unsigned int | |
9847 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
9848 | { | |
9849 | return SHN_COMMON; | |
9850 | } | |
9851 | ||
9852 | asection * | |
9853 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
9854 | { | |
9855 | return bfd_com_section_ptr; | |
9856 | } |