Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
64d03ab5 | 2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
74f0fb50 | 3 | 2005, 2006, 2007, 2008 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 | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
8fdd7217 | 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 | |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
252b5132 | 21 | |
252b5132 | 22 | #include "sysdep.h" |
3db64b00 | 23 | #include "bfd.h" |
252b5132 RH |
24 | #include "bfdlink.h" |
25 | #include "libbfd.h" | |
26 | #define ARCH_SIZE 0 | |
27 | #include "elf-bfd.h" | |
4ad4eba5 | 28 | #include "safe-ctype.h" |
ccf2f652 | 29 | #include "libiberty.h" |
66eb6687 | 30 | #include "objalloc.h" |
252b5132 | 31 | |
d98685ac AM |
32 | /* Define a symbol in a dynamic linkage section. */ |
33 | ||
34 | struct elf_link_hash_entry * | |
35 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
36 | struct bfd_link_info *info, | |
37 | asection *sec, | |
38 | const char *name) | |
39 | { | |
40 | struct elf_link_hash_entry *h; | |
41 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 42 | const struct elf_backend_data *bed; |
d98685ac AM |
43 | |
44 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
45 | if (h != NULL) | |
46 | { | |
47 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
48 | This is a symptom of a larger problem: Absolute symbols | |
49 | defined in shared libraries can't be overridden, because we | |
50 | lose the link to the bfd which is via the symbol section. */ | |
51 | h->root.type = bfd_link_hash_new; | |
52 | } | |
53 | ||
54 | bh = &h->root; | |
55 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, | |
56 | sec, 0, NULL, FALSE, | |
57 | get_elf_backend_data (abfd)->collect, | |
58 | &bh)) | |
59 | return NULL; | |
60 | h = (struct elf_link_hash_entry *) bh; | |
61 | h->def_regular = 1; | |
62 | h->type = STT_OBJECT; | |
63 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
64 | ||
ccabcbe5 AM |
65 | bed = get_elf_backend_data (abfd); |
66 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
d98685ac AM |
67 | return h; |
68 | } | |
69 | ||
b34976b6 | 70 | bfd_boolean |
268b6b39 | 71 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
72 | { |
73 | flagword flags; | |
aad5d350 | 74 | asection *s; |
252b5132 | 75 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 76 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
77 | int ptralign; |
78 | ||
79 | /* This function may be called more than once. */ | |
aad5d350 AM |
80 | s = bfd_get_section_by_name (abfd, ".got"); |
81 | if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0) | |
b34976b6 | 82 | return TRUE; |
252b5132 RH |
83 | |
84 | switch (bed->s->arch_size) | |
85 | { | |
bb0deeff AO |
86 | case 32: |
87 | ptralign = 2; | |
88 | break; | |
89 | ||
90 | case 64: | |
91 | ptralign = 3; | |
92 | break; | |
93 | ||
94 | default: | |
95 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 96 | return FALSE; |
252b5132 RH |
97 | } |
98 | ||
e5a52504 | 99 | flags = bed->dynamic_sec_flags; |
252b5132 | 100 | |
3496cb2a | 101 | s = bfd_make_section_with_flags (abfd, ".got", flags); |
252b5132 | 102 | if (s == NULL |
252b5132 | 103 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 104 | return FALSE; |
252b5132 RH |
105 | |
106 | if (bed->want_got_plt) | |
107 | { | |
3496cb2a | 108 | s = bfd_make_section_with_flags (abfd, ".got.plt", flags); |
252b5132 | 109 | if (s == NULL |
252b5132 | 110 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 111 | return FALSE; |
252b5132 RH |
112 | } |
113 | ||
2517a57f AM |
114 | if (bed->want_got_sym) |
115 | { | |
116 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
117 | (or .got.plt) section. We don't do this in the linker script | |
118 | because we don't want to define the symbol if we are not creating | |
119 | a global offset table. */ | |
d98685ac | 120 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_"); |
2517a57f | 121 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
122 | if (h == NULL) |
123 | return FALSE; | |
2517a57f | 124 | } |
252b5132 RH |
125 | |
126 | /* The first bit of the global offset table is the header. */ | |
3b36f7e6 | 127 | s->size += bed->got_header_size; |
252b5132 | 128 | |
b34976b6 | 129 | return TRUE; |
252b5132 RH |
130 | } |
131 | \f | |
7e9f0867 AM |
132 | /* Create a strtab to hold the dynamic symbol names. */ |
133 | static bfd_boolean | |
134 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
135 | { | |
136 | struct elf_link_hash_table *hash_table; | |
137 | ||
138 | hash_table = elf_hash_table (info); | |
139 | if (hash_table->dynobj == NULL) | |
140 | hash_table->dynobj = abfd; | |
141 | ||
142 | if (hash_table->dynstr == NULL) | |
143 | { | |
144 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
145 | if (hash_table->dynstr == NULL) | |
146 | return FALSE; | |
147 | } | |
148 | return TRUE; | |
149 | } | |
150 | ||
45d6a902 AM |
151 | /* Create some sections which will be filled in with dynamic linking |
152 | information. ABFD is an input file which requires dynamic sections | |
153 | to be created. The dynamic sections take up virtual memory space | |
154 | when the final executable is run, so we need to create them before | |
155 | addresses are assigned to the output sections. We work out the | |
156 | actual contents and size of these sections later. */ | |
252b5132 | 157 | |
b34976b6 | 158 | bfd_boolean |
268b6b39 | 159 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 160 | { |
45d6a902 AM |
161 | flagword flags; |
162 | register asection *s; | |
9c5bfbb7 | 163 | const struct elf_backend_data *bed; |
252b5132 | 164 | |
0eddce27 | 165 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
166 | return FALSE; |
167 | ||
168 | if (elf_hash_table (info)->dynamic_sections_created) | |
169 | return TRUE; | |
170 | ||
7e9f0867 AM |
171 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
172 | return FALSE; | |
45d6a902 | 173 | |
7e9f0867 | 174 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
175 | bed = get_elf_backend_data (abfd); |
176 | ||
177 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
178 | |
179 | /* A dynamically linked executable has a .interp section, but a | |
180 | shared library does not. */ | |
36af4a4e | 181 | if (info->executable) |
252b5132 | 182 | { |
3496cb2a L |
183 | s = bfd_make_section_with_flags (abfd, ".interp", |
184 | flags | SEC_READONLY); | |
185 | if (s == NULL) | |
45d6a902 AM |
186 | return FALSE; |
187 | } | |
bb0deeff | 188 | |
45d6a902 AM |
189 | /* Create sections to hold version informations. These are removed |
190 | if they are not needed. */ | |
3496cb2a L |
191 | s = bfd_make_section_with_flags (abfd, ".gnu.version_d", |
192 | flags | SEC_READONLY); | |
45d6a902 | 193 | if (s == NULL |
45d6a902 AM |
194 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
195 | return FALSE; | |
196 | ||
3496cb2a L |
197 | s = bfd_make_section_with_flags (abfd, ".gnu.version", |
198 | flags | SEC_READONLY); | |
45d6a902 | 199 | if (s == NULL |
45d6a902 AM |
200 | || ! bfd_set_section_alignment (abfd, s, 1)) |
201 | return FALSE; | |
202 | ||
3496cb2a L |
203 | s = bfd_make_section_with_flags (abfd, ".gnu.version_r", |
204 | flags | SEC_READONLY); | |
45d6a902 | 205 | if (s == NULL |
45d6a902 AM |
206 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
207 | return FALSE; | |
208 | ||
3496cb2a L |
209 | s = bfd_make_section_with_flags (abfd, ".dynsym", |
210 | flags | SEC_READONLY); | |
45d6a902 | 211 | if (s == NULL |
45d6a902 AM |
212 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
213 | return FALSE; | |
214 | ||
3496cb2a L |
215 | s = bfd_make_section_with_flags (abfd, ".dynstr", |
216 | flags | SEC_READONLY); | |
217 | if (s == NULL) | |
45d6a902 AM |
218 | return FALSE; |
219 | ||
3496cb2a | 220 | s = bfd_make_section_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 221 | if (s == NULL |
45d6a902 AM |
222 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
223 | return FALSE; | |
224 | ||
225 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
226 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
227 | only want to define it if we are, in fact, creating a .dynamic | |
228 | section. We don't want to define it if there is no .dynamic | |
229 | section, since on some ELF platforms the start up code examines it | |
230 | to decide how to initialize the process. */ | |
d98685ac | 231 | if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC")) |
45d6a902 AM |
232 | return FALSE; |
233 | ||
fdc90cb4 JJ |
234 | if (info->emit_hash) |
235 | { | |
236 | s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY); | |
237 | if (s == NULL | |
238 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
239 | return FALSE; | |
240 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
241 | } | |
242 | ||
243 | if (info->emit_gnu_hash) | |
244 | { | |
245 | s = bfd_make_section_with_flags (abfd, ".gnu.hash", | |
246 | flags | SEC_READONLY); | |
247 | if (s == NULL | |
248 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
249 | return FALSE; | |
250 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: | |
251 | 4 32-bit words followed by variable count of 64-bit words, then | |
252 | variable count of 32-bit words. */ | |
253 | if (bed->s->arch_size == 64) | |
254 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
255 | else | |
256 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
257 | } | |
45d6a902 AM |
258 | |
259 | /* Let the backend create the rest of the sections. This lets the | |
260 | backend set the right flags. The backend will normally create | |
261 | the .got and .plt sections. */ | |
262 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
263 | return FALSE; | |
264 | ||
265 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
266 | ||
267 | return TRUE; | |
268 | } | |
269 | ||
270 | /* Create dynamic sections when linking against a dynamic object. */ | |
271 | ||
272 | bfd_boolean | |
268b6b39 | 273 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
274 | { |
275 | flagword flags, pltflags; | |
7325306f | 276 | struct elf_link_hash_entry *h; |
45d6a902 | 277 | asection *s; |
9c5bfbb7 | 278 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 279 | |
252b5132 RH |
280 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
281 | .rel[a].bss sections. */ | |
e5a52504 | 282 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
283 | |
284 | pltflags = flags; | |
252b5132 | 285 | if (bed->plt_not_loaded) |
6df4d94c MM |
286 | /* We do not clear SEC_ALLOC here because we still want the OS to |
287 | allocate space for the section; it's just that there's nothing | |
288 | to read in from the object file. */ | |
5d1634d7 | 289 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
290 | else |
291 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
292 | if (bed->plt_readonly) |
293 | pltflags |= SEC_READONLY; | |
294 | ||
3496cb2a | 295 | s = bfd_make_section_with_flags (abfd, ".plt", pltflags); |
252b5132 | 296 | if (s == NULL |
252b5132 | 297 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 298 | return FALSE; |
252b5132 | 299 | |
d98685ac AM |
300 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
301 | .plt section. */ | |
7325306f RS |
302 | if (bed->want_plt_sym) |
303 | { | |
304 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
305 | "_PROCEDURE_LINKAGE_TABLE_"); | |
306 | elf_hash_table (info)->hplt = h; | |
307 | if (h == NULL) | |
308 | return FALSE; | |
309 | } | |
252b5132 | 310 | |
3496cb2a L |
311 | s = bfd_make_section_with_flags (abfd, |
312 | (bed->default_use_rela_p | |
313 | ? ".rela.plt" : ".rel.plt"), | |
314 | flags | SEC_READONLY); | |
252b5132 | 315 | if (s == NULL |
45d6a902 | 316 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 317 | return FALSE; |
252b5132 RH |
318 | |
319 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 320 | return FALSE; |
252b5132 | 321 | |
3018b441 RH |
322 | if (bed->want_dynbss) |
323 | { | |
324 | /* The .dynbss section is a place to put symbols which are defined | |
325 | by dynamic objects, are referenced by regular objects, and are | |
326 | not functions. We must allocate space for them in the process | |
327 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
328 | initialize them at run time. The linker script puts the .dynbss | |
329 | section into the .bss section of the final image. */ | |
3496cb2a L |
330 | s = bfd_make_section_with_flags (abfd, ".dynbss", |
331 | (SEC_ALLOC | |
332 | | SEC_LINKER_CREATED)); | |
333 | if (s == NULL) | |
b34976b6 | 334 | return FALSE; |
252b5132 | 335 | |
3018b441 | 336 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
337 | normally needed. We need to create it here, though, so that the |
338 | linker will map it to an output section. We can't just create it | |
339 | only if we need it, because we will not know whether we need it | |
340 | until we have seen all the input files, and the first time the | |
341 | main linker code calls BFD after examining all the input files | |
342 | (size_dynamic_sections) the input sections have already been | |
343 | mapped to the output sections. If the section turns out not to | |
344 | be needed, we can discard it later. We will never need this | |
345 | section when generating a shared object, since they do not use | |
346 | copy relocs. */ | |
3018b441 RH |
347 | if (! info->shared) |
348 | { | |
3496cb2a L |
349 | s = bfd_make_section_with_flags (abfd, |
350 | (bed->default_use_rela_p | |
351 | ? ".rela.bss" : ".rel.bss"), | |
352 | flags | SEC_READONLY); | |
3018b441 | 353 | if (s == NULL |
45d6a902 | 354 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 355 | return FALSE; |
3018b441 | 356 | } |
252b5132 RH |
357 | } |
358 | ||
b34976b6 | 359 | return TRUE; |
252b5132 RH |
360 | } |
361 | \f | |
252b5132 RH |
362 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
363 | read the input files, since we need to have a list of all of them | |
364 | before we can determine the final sizes of the output sections. | |
365 | Note that we may actually call this function even though we are not | |
366 | going to output any dynamic symbols; in some cases we know that a | |
367 | symbol should be in the dynamic symbol table, but only if there is | |
368 | one. */ | |
369 | ||
b34976b6 | 370 | bfd_boolean |
c152c796 AM |
371 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
372 | struct elf_link_hash_entry *h) | |
252b5132 RH |
373 | { |
374 | if (h->dynindx == -1) | |
375 | { | |
2b0f7ef9 | 376 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 377 | char *p; |
252b5132 | 378 | const char *name; |
252b5132 RH |
379 | bfd_size_type indx; |
380 | ||
7a13edea NC |
381 | /* XXX: The ABI draft says the linker must turn hidden and |
382 | internal symbols into STB_LOCAL symbols when producing the | |
383 | DSO. However, if ld.so honors st_other in the dynamic table, | |
384 | this would not be necessary. */ | |
385 | switch (ELF_ST_VISIBILITY (h->other)) | |
386 | { | |
387 | case STV_INTERNAL: | |
388 | case STV_HIDDEN: | |
9d6eee78 L |
389 | if (h->root.type != bfd_link_hash_undefined |
390 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 391 | { |
f5385ebf | 392 | h->forced_local = 1; |
67687978 PB |
393 | if (!elf_hash_table (info)->is_relocatable_executable) |
394 | return TRUE; | |
7a13edea | 395 | } |
0444bdd4 | 396 | |
7a13edea NC |
397 | default: |
398 | break; | |
399 | } | |
400 | ||
252b5132 RH |
401 | h->dynindx = elf_hash_table (info)->dynsymcount; |
402 | ++elf_hash_table (info)->dynsymcount; | |
403 | ||
404 | dynstr = elf_hash_table (info)->dynstr; | |
405 | if (dynstr == NULL) | |
406 | { | |
407 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 408 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 409 | if (dynstr == NULL) |
b34976b6 | 410 | return FALSE; |
252b5132 RH |
411 | } |
412 | ||
413 | /* We don't put any version information in the dynamic string | |
aad5d350 | 414 | table. */ |
252b5132 RH |
415 | name = h->root.root.string; |
416 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
417 | if (p != NULL) |
418 | /* We know that the p points into writable memory. In fact, | |
419 | there are only a few symbols that have read-only names, being | |
420 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
421 | by the backends. Most symbols will have names pointing into | |
422 | an ELF string table read from a file, or to objalloc memory. */ | |
423 | *p = 0; | |
424 | ||
425 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
426 | ||
427 | if (p != NULL) | |
428 | *p = ELF_VER_CHR; | |
252b5132 RH |
429 | |
430 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 431 | return FALSE; |
252b5132 RH |
432 | h->dynstr_index = indx; |
433 | } | |
434 | ||
b34976b6 | 435 | return TRUE; |
252b5132 | 436 | } |
45d6a902 | 437 | \f |
55255dae L |
438 | /* Mark a symbol dynamic. */ |
439 | ||
440 | void | |
441 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, | |
40b36307 L |
442 | struct elf_link_hash_entry *h, |
443 | Elf_Internal_Sym *sym) | |
55255dae | 444 | { |
40b36307 | 445 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
55255dae | 446 | |
40b36307 L |
447 | /* It may be called more than once on the same H. */ |
448 | if(h->dynamic || info->relocatable) | |
55255dae L |
449 | return; |
450 | ||
40b36307 L |
451 | if ((info->dynamic_data |
452 | && (h->type == STT_OBJECT | |
453 | || (sym != NULL | |
454 | && ELF_ST_TYPE (sym->st_info) == STT_OBJECT))) | |
a0c8462f | 455 | || (d != NULL |
40b36307 L |
456 | && h->root.type == bfd_link_hash_new |
457 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
55255dae L |
458 | h->dynamic = 1; |
459 | } | |
460 | ||
45d6a902 AM |
461 | /* Record an assignment to a symbol made by a linker script. We need |
462 | this in case some dynamic object refers to this symbol. */ | |
463 | ||
464 | bfd_boolean | |
fe21a8fc L |
465 | bfd_elf_record_link_assignment (bfd *output_bfd, |
466 | struct bfd_link_info *info, | |
268b6b39 | 467 | const char *name, |
fe21a8fc L |
468 | bfd_boolean provide, |
469 | bfd_boolean hidden) | |
45d6a902 | 470 | { |
00cbee0a | 471 | struct elf_link_hash_entry *h, *hv; |
4ea42fb7 | 472 | struct elf_link_hash_table *htab; |
00cbee0a | 473 | const struct elf_backend_data *bed; |
45d6a902 | 474 | |
0eddce27 | 475 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
476 | return TRUE; |
477 | ||
4ea42fb7 AM |
478 | htab = elf_hash_table (info); |
479 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 480 | if (h == NULL) |
4ea42fb7 | 481 | return provide; |
45d6a902 | 482 | |
00cbee0a | 483 | switch (h->root.type) |
77cfaee6 | 484 | { |
00cbee0a L |
485 | case bfd_link_hash_defined: |
486 | case bfd_link_hash_defweak: | |
487 | case bfd_link_hash_common: | |
488 | break; | |
489 | case bfd_link_hash_undefweak: | |
490 | case bfd_link_hash_undefined: | |
491 | /* Since we're defining the symbol, don't let it seem to have not | |
492 | been defined. record_dynamic_symbol and size_dynamic_sections | |
493 | may depend on this. */ | |
4ea42fb7 | 494 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
495 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
496 | bfd_link_repair_undef_list (&htab->root); | |
00cbee0a L |
497 | break; |
498 | case bfd_link_hash_new: | |
40b36307 | 499 | bfd_elf_link_mark_dynamic_symbol (info, h, NULL); |
55255dae | 500 | h->non_elf = 0; |
00cbee0a L |
501 | break; |
502 | case bfd_link_hash_indirect: | |
503 | /* We had a versioned symbol in a dynamic library. We make the | |
a0c8462f | 504 | the versioned symbol point to this one. */ |
00cbee0a L |
505 | bed = get_elf_backend_data (output_bfd); |
506 | hv = h; | |
507 | while (hv->root.type == bfd_link_hash_indirect | |
508 | || hv->root.type == bfd_link_hash_warning) | |
509 | hv = (struct elf_link_hash_entry *) hv->root.u.i.link; | |
510 | /* We don't need to update h->root.u since linker will set them | |
511 | later. */ | |
512 | h->root.type = bfd_link_hash_undefined; | |
513 | hv->root.type = bfd_link_hash_indirect; | |
514 | hv->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
515 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); | |
516 | break; | |
517 | case bfd_link_hash_warning: | |
518 | abort (); | |
519 | break; | |
55255dae | 520 | } |
45d6a902 AM |
521 | |
522 | /* If this symbol is being provided by the linker script, and it is | |
523 | currently defined by a dynamic object, but not by a regular | |
524 | object, then mark it as undefined so that the generic linker will | |
525 | force the correct value. */ | |
526 | if (provide | |
f5385ebf AM |
527 | && h->def_dynamic |
528 | && !h->def_regular) | |
45d6a902 AM |
529 | h->root.type = bfd_link_hash_undefined; |
530 | ||
531 | /* If this symbol is not being provided by the linker script, and it is | |
532 | currently defined by a dynamic object, but not by a regular object, | |
533 | then clear out any version information because the symbol will not be | |
534 | associated with the dynamic object any more. */ | |
535 | if (!provide | |
f5385ebf AM |
536 | && h->def_dynamic |
537 | && !h->def_regular) | |
45d6a902 AM |
538 | h->verinfo.verdef = NULL; |
539 | ||
f5385ebf | 540 | h->def_regular = 1; |
45d6a902 | 541 | |
fe21a8fc L |
542 | if (provide && hidden) |
543 | { | |
544 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
545 | ||
546 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
547 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
548 | } | |
549 | ||
6fa3860b PB |
550 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
551 | and executables. */ | |
552 | if (!info->relocatable | |
553 | && h->dynindx != -1 | |
554 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
555 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
556 | h->forced_local = 1; | |
557 | ||
f5385ebf AM |
558 | if ((h->def_dynamic |
559 | || h->ref_dynamic | |
67687978 PB |
560 | || info->shared |
561 | || (info->executable && elf_hash_table (info)->is_relocatable_executable)) | |
45d6a902 AM |
562 | && h->dynindx == -1) |
563 | { | |
c152c796 | 564 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
565 | return FALSE; |
566 | ||
567 | /* If this is a weak defined symbol, and we know a corresponding | |
568 | real symbol from the same dynamic object, make sure the real | |
569 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
570 | if (h->u.weakdef != NULL |
571 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 572 | { |
f6e332e6 | 573 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
574 | return FALSE; |
575 | } | |
576 | } | |
577 | ||
578 | return TRUE; | |
579 | } | |
42751cf3 | 580 | |
8c58d23b AM |
581 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
582 | success, and 2 on a failure caused by attempting to record a symbol | |
583 | in a discarded section, eg. a discarded link-once section symbol. */ | |
584 | ||
585 | int | |
c152c796 AM |
586 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
587 | bfd *input_bfd, | |
588 | long input_indx) | |
8c58d23b AM |
589 | { |
590 | bfd_size_type amt; | |
591 | struct elf_link_local_dynamic_entry *entry; | |
592 | struct elf_link_hash_table *eht; | |
593 | struct elf_strtab_hash *dynstr; | |
594 | unsigned long dynstr_index; | |
595 | char *name; | |
596 | Elf_External_Sym_Shndx eshndx; | |
597 | char esym[sizeof (Elf64_External_Sym)]; | |
598 | ||
0eddce27 | 599 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
600 | return 0; |
601 | ||
602 | /* See if the entry exists already. */ | |
603 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
604 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
605 | return 1; | |
606 | ||
607 | amt = sizeof (*entry); | |
268b6b39 | 608 | entry = bfd_alloc (input_bfd, amt); |
8c58d23b AM |
609 | if (entry == NULL) |
610 | return 0; | |
611 | ||
612 | /* Go find the symbol, so that we can find it's name. */ | |
613 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 614 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
615 | { |
616 | bfd_release (input_bfd, entry); | |
617 | return 0; | |
618 | } | |
619 | ||
620 | if (entry->isym.st_shndx != SHN_UNDEF | |
621 | && (entry->isym.st_shndx < SHN_LORESERVE | |
622 | || entry->isym.st_shndx > SHN_HIRESERVE)) | |
623 | { | |
624 | asection *s; | |
625 | ||
626 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
627 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
628 | { | |
629 | /* We can still bfd_release here as nothing has done another | |
630 | bfd_alloc. We can't do this later in this function. */ | |
631 | bfd_release (input_bfd, entry); | |
632 | return 2; | |
633 | } | |
634 | } | |
635 | ||
636 | name = (bfd_elf_string_from_elf_section | |
637 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
638 | entry->isym.st_name)); | |
639 | ||
640 | dynstr = elf_hash_table (info)->dynstr; | |
641 | if (dynstr == NULL) | |
642 | { | |
643 | /* Create a strtab to hold the dynamic symbol names. */ | |
644 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
645 | if (dynstr == NULL) | |
646 | return 0; | |
647 | } | |
648 | ||
b34976b6 | 649 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
650 | if (dynstr_index == (unsigned long) -1) |
651 | return 0; | |
652 | entry->isym.st_name = dynstr_index; | |
653 | ||
654 | eht = elf_hash_table (info); | |
655 | ||
656 | entry->next = eht->dynlocal; | |
657 | eht->dynlocal = entry; | |
658 | entry->input_bfd = input_bfd; | |
659 | entry->input_indx = input_indx; | |
660 | eht->dynsymcount++; | |
661 | ||
662 | /* Whatever binding the symbol had before, it's now local. */ | |
663 | entry->isym.st_info | |
664 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
665 | ||
666 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
667 | ||
668 | return 1; | |
669 | } | |
670 | ||
30b30c21 | 671 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 672 | |
30b30c21 | 673 | long |
268b6b39 AM |
674 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
675 | bfd *input_bfd, | |
676 | long input_indx) | |
30b30c21 RH |
677 | { |
678 | struct elf_link_local_dynamic_entry *e; | |
679 | ||
680 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
681 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
682 | return e->dynindx; | |
683 | return -1; | |
684 | } | |
685 | ||
686 | /* This function is used to renumber the dynamic symbols, if some of | |
687 | them are removed because they are marked as local. This is called | |
688 | via elf_link_hash_traverse. */ | |
689 | ||
b34976b6 | 690 | static bfd_boolean |
268b6b39 AM |
691 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
692 | void *data) | |
42751cf3 | 693 | { |
268b6b39 | 694 | size_t *count = data; |
30b30c21 | 695 | |
e92d460e AM |
696 | if (h->root.type == bfd_link_hash_warning) |
697 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
698 | ||
6fa3860b PB |
699 | if (h->forced_local) |
700 | return TRUE; | |
701 | ||
702 | if (h->dynindx != -1) | |
703 | h->dynindx = ++(*count); | |
704 | ||
705 | return TRUE; | |
706 | } | |
707 | ||
708 | ||
709 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
710 | STB_LOCAL binding. */ | |
711 | ||
712 | static bfd_boolean | |
713 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
714 | void *data) | |
715 | { | |
716 | size_t *count = data; | |
717 | ||
718 | if (h->root.type == bfd_link_hash_warning) | |
719 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
720 | ||
721 | if (!h->forced_local) | |
722 | return TRUE; | |
723 | ||
42751cf3 | 724 | if (h->dynindx != -1) |
30b30c21 RH |
725 | h->dynindx = ++(*count); |
726 | ||
b34976b6 | 727 | return TRUE; |
42751cf3 | 728 | } |
30b30c21 | 729 | |
aee6f5b4 AO |
730 | /* Return true if the dynamic symbol for a given section should be |
731 | omitted when creating a shared library. */ | |
732 | bfd_boolean | |
733 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
734 | struct bfd_link_info *info, | |
735 | asection *p) | |
736 | { | |
74541ad4 AM |
737 | struct elf_link_hash_table *htab; |
738 | ||
aee6f5b4 AO |
739 | switch (elf_section_data (p)->this_hdr.sh_type) |
740 | { | |
741 | case SHT_PROGBITS: | |
742 | case SHT_NOBITS: | |
743 | /* If sh_type is yet undecided, assume it could be | |
744 | SHT_PROGBITS/SHT_NOBITS. */ | |
745 | case SHT_NULL: | |
74541ad4 AM |
746 | htab = elf_hash_table (info); |
747 | if (p == htab->tls_sec) | |
748 | return FALSE; | |
749 | ||
750 | if (htab->text_index_section != NULL) | |
751 | return p != htab->text_index_section && p != htab->data_index_section; | |
752 | ||
aee6f5b4 AO |
753 | if (strcmp (p->name, ".got") == 0 |
754 | || strcmp (p->name, ".got.plt") == 0 | |
755 | || strcmp (p->name, ".plt") == 0) | |
756 | { | |
757 | asection *ip; | |
aee6f5b4 | 758 | |
74541ad4 AM |
759 | if (htab->dynobj != NULL |
760 | && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL | |
aee6f5b4 AO |
761 | && (ip->flags & SEC_LINKER_CREATED) |
762 | && ip->output_section == p) | |
763 | return TRUE; | |
764 | } | |
765 | return FALSE; | |
766 | ||
767 | /* There shouldn't be section relative relocations | |
768 | against any other section. */ | |
769 | default: | |
770 | return TRUE; | |
771 | } | |
772 | } | |
773 | ||
062e2358 | 774 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
775 | symbol for each output section, which come first. Next come symbols |
776 | which have been forced to local binding. Then all of the back-end | |
777 | allocated local dynamic syms, followed by the rest of the global | |
778 | symbols. */ | |
30b30c21 | 779 | |
554220db AM |
780 | static unsigned long |
781 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
782 | struct bfd_link_info *info, | |
783 | unsigned long *section_sym_count) | |
30b30c21 RH |
784 | { |
785 | unsigned long dynsymcount = 0; | |
786 | ||
67687978 | 787 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
30b30c21 | 788 | { |
aee6f5b4 | 789 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
790 | asection *p; |
791 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 792 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
793 | && (p->flags & SEC_ALLOC) != 0 |
794 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
795 | elf_section_data (p)->dynindx = ++dynsymcount; | |
74541ad4 AM |
796 | else |
797 | elf_section_data (p)->dynindx = 0; | |
30b30c21 | 798 | } |
554220db | 799 | *section_sym_count = dynsymcount; |
30b30c21 | 800 | |
6fa3860b PB |
801 | elf_link_hash_traverse (elf_hash_table (info), |
802 | elf_link_renumber_local_hash_table_dynsyms, | |
803 | &dynsymcount); | |
804 | ||
30b30c21 RH |
805 | if (elf_hash_table (info)->dynlocal) |
806 | { | |
807 | struct elf_link_local_dynamic_entry *p; | |
808 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
809 | p->dynindx = ++dynsymcount; | |
810 | } | |
811 | ||
812 | elf_link_hash_traverse (elf_hash_table (info), | |
813 | elf_link_renumber_hash_table_dynsyms, | |
814 | &dynsymcount); | |
815 | ||
816 | /* There is an unused NULL entry at the head of the table which | |
817 | we must account for in our count. Unless there weren't any | |
818 | symbols, which means we'll have no table at all. */ | |
819 | if (dynsymcount != 0) | |
820 | ++dynsymcount; | |
821 | ||
ccabcbe5 AM |
822 | elf_hash_table (info)->dynsymcount = dynsymcount; |
823 | return dynsymcount; | |
30b30c21 | 824 | } |
252b5132 | 825 | |
45d6a902 AM |
826 | /* This function is called when we want to define a new symbol. It |
827 | handles the various cases which arise when we find a definition in | |
828 | a dynamic object, or when there is already a definition in a | |
829 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
830 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
831 | OVERRIDE if the old symbol is overriding a new definition. We set | |
832 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
833 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
834 | change, we mean that we shouldn't warn if the type or size does | |
af44c138 L |
835 | change. We set POLD_ALIGNMENT if an old common symbol in a dynamic |
836 | object is overridden by a regular object. */ | |
45d6a902 AM |
837 | |
838 | bfd_boolean | |
268b6b39 AM |
839 | _bfd_elf_merge_symbol (bfd *abfd, |
840 | struct bfd_link_info *info, | |
841 | const char *name, | |
842 | Elf_Internal_Sym *sym, | |
843 | asection **psec, | |
844 | bfd_vma *pvalue, | |
af44c138 | 845 | unsigned int *pold_alignment, |
268b6b39 AM |
846 | struct elf_link_hash_entry **sym_hash, |
847 | bfd_boolean *skip, | |
848 | bfd_boolean *override, | |
849 | bfd_boolean *type_change_ok, | |
0f8a2703 | 850 | bfd_boolean *size_change_ok) |
252b5132 | 851 | { |
7479dfd4 | 852 | asection *sec, *oldsec; |
45d6a902 AM |
853 | struct elf_link_hash_entry *h; |
854 | struct elf_link_hash_entry *flip; | |
855 | int bind; | |
856 | bfd *oldbfd; | |
857 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
77cfaee6 | 858 | bfd_boolean newweak, oldweak; |
a4d8e49b | 859 | const struct elf_backend_data *bed; |
45d6a902 AM |
860 | |
861 | *skip = FALSE; | |
862 | *override = FALSE; | |
863 | ||
864 | sec = *psec; | |
865 | bind = ELF_ST_BIND (sym->st_info); | |
866 | ||
cd7be95b KH |
867 | /* Silently discard TLS symbols from --just-syms. There's no way to |
868 | combine a static TLS block with a new TLS block for this executable. */ | |
869 | if (ELF_ST_TYPE (sym->st_info) == STT_TLS | |
870 | && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
871 | { | |
872 | *skip = TRUE; | |
873 | return TRUE; | |
874 | } | |
875 | ||
45d6a902 AM |
876 | if (! bfd_is_und_section (sec)) |
877 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
878 | else | |
879 | h = ((struct elf_link_hash_entry *) | |
880 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
881 | if (h == NULL) | |
882 | return FALSE; | |
883 | *sym_hash = h; | |
252b5132 | 884 | |
45d6a902 AM |
885 | /* This code is for coping with dynamic objects, and is only useful |
886 | if we are doing an ELF link. */ | |
f13a99db | 887 | if (info->output_bfd->xvec != abfd->xvec) |
45d6a902 | 888 | return TRUE; |
252b5132 | 889 | |
45d6a902 AM |
890 | /* For merging, we only care about real symbols. */ |
891 | ||
892 | while (h->root.type == bfd_link_hash_indirect | |
893 | || h->root.type == bfd_link_hash_warning) | |
894 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
895 | ||
40b36307 L |
896 | /* We have to check it for every instance since the first few may be |
897 | refereences and not all compilers emit symbol type for undefined | |
898 | symbols. */ | |
899 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
900 | ||
45d6a902 AM |
901 | /* If we just created the symbol, mark it as being an ELF symbol. |
902 | Other than that, there is nothing to do--there is no merge issue | |
903 | with a newly defined symbol--so we just return. */ | |
904 | ||
905 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 906 | { |
f5385ebf | 907 | h->non_elf = 0; |
45d6a902 AM |
908 | return TRUE; |
909 | } | |
252b5132 | 910 | |
7479dfd4 L |
911 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
912 | existing symbol. */ | |
252b5132 | 913 | |
45d6a902 AM |
914 | switch (h->root.type) |
915 | { | |
916 | default: | |
917 | oldbfd = NULL; | |
7479dfd4 | 918 | oldsec = NULL; |
45d6a902 | 919 | break; |
252b5132 | 920 | |
45d6a902 AM |
921 | case bfd_link_hash_undefined: |
922 | case bfd_link_hash_undefweak: | |
923 | oldbfd = h->root.u.undef.abfd; | |
7479dfd4 | 924 | oldsec = NULL; |
45d6a902 AM |
925 | break; |
926 | ||
927 | case bfd_link_hash_defined: | |
928 | case bfd_link_hash_defweak: | |
929 | oldbfd = h->root.u.def.section->owner; | |
7479dfd4 | 930 | oldsec = h->root.u.def.section; |
45d6a902 AM |
931 | break; |
932 | ||
933 | case bfd_link_hash_common: | |
934 | oldbfd = h->root.u.c.p->section->owner; | |
7479dfd4 | 935 | oldsec = h->root.u.c.p->section; |
45d6a902 AM |
936 | break; |
937 | } | |
938 | ||
939 | /* In cases involving weak versioned symbols, we may wind up trying | |
940 | to merge a symbol with itself. Catch that here, to avoid the | |
941 | confusion that results if we try to override a symbol with | |
942 | itself. The additional tests catch cases like | |
943 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
944 | dynamic object, which we do want to handle here. */ | |
945 | if (abfd == oldbfd | |
946 | && ((abfd->flags & DYNAMIC) == 0 | |
f5385ebf | 947 | || !h->def_regular)) |
45d6a902 AM |
948 | return TRUE; |
949 | ||
950 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
951 | respectively, is from a dynamic object. */ | |
952 | ||
707bba77 | 953 | newdyn = (abfd->flags & DYNAMIC) != 0; |
45d6a902 | 954 | |
707bba77 | 955 | olddyn = FALSE; |
45d6a902 AM |
956 | if (oldbfd != NULL) |
957 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 958 | else if (oldsec != NULL) |
45d6a902 | 959 | { |
707bba77 | 960 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 961 | indices used by MIPS ELF. */ |
707bba77 | 962 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 963 | } |
252b5132 | 964 | |
45d6a902 AM |
965 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
966 | respectively, appear to be a definition rather than reference. */ | |
967 | ||
707bba77 | 968 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 969 | |
707bba77 AM |
970 | olddef = (h->root.type != bfd_link_hash_undefined |
971 | && h->root.type != bfd_link_hash_undefweak | |
972 | && h->root.type != bfd_link_hash_common); | |
45d6a902 | 973 | |
fcb93ecf | 974 | bed = get_elf_backend_data (abfd); |
580a2b6e L |
975 | /* When we try to create a default indirect symbol from the dynamic |
976 | definition with the default version, we skip it if its type and | |
977 | the type of existing regular definition mismatch. We only do it | |
978 | if the existing regular definition won't be dynamic. */ | |
979 | if (pold_alignment == NULL | |
980 | && !info->shared | |
981 | && !info->export_dynamic | |
982 | && !h->ref_dynamic | |
983 | && newdyn | |
984 | && newdef | |
985 | && !olddyn | |
986 | && (olddef || h->root.type == bfd_link_hash_common) | |
987 | && ELF_ST_TYPE (sym->st_info) != h->type | |
988 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
fcb93ecf PB |
989 | && h->type != STT_NOTYPE |
990 | && !(bed->is_function_type (ELF_ST_TYPE (sym->st_info)) | |
991 | && bed->is_function_type (h->type))) | |
580a2b6e L |
992 | { |
993 | *skip = TRUE; | |
994 | return TRUE; | |
995 | } | |
996 | ||
68f49ba3 L |
997 | /* Check TLS symbol. We don't check undefined symbol introduced by |
998 | "ld -u". */ | |
7479dfd4 | 999 | if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS) |
68f49ba3 L |
1000 | && ELF_ST_TYPE (sym->st_info) != h->type |
1001 | && oldbfd != NULL) | |
7479dfd4 L |
1002 | { |
1003 | bfd *ntbfd, *tbfd; | |
1004 | bfd_boolean ntdef, tdef; | |
1005 | asection *ntsec, *tsec; | |
1006 | ||
1007 | if (h->type == STT_TLS) | |
1008 | { | |
3b36f7e6 | 1009 | ntbfd = abfd; |
7479dfd4 L |
1010 | ntsec = sec; |
1011 | ntdef = newdef; | |
1012 | tbfd = oldbfd; | |
1013 | tsec = oldsec; | |
1014 | tdef = olddef; | |
1015 | } | |
1016 | else | |
1017 | { | |
1018 | ntbfd = oldbfd; | |
1019 | ntsec = oldsec; | |
1020 | ntdef = olddef; | |
1021 | tbfd = abfd; | |
1022 | tsec = sec; | |
1023 | tdef = newdef; | |
1024 | } | |
1025 | ||
1026 | if (tdef && ntdef) | |
1027 | (*_bfd_error_handler) | |
1028 | (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), | |
1029 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); | |
1030 | else if (!tdef && !ntdef) | |
1031 | (*_bfd_error_handler) | |
1032 | (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), | |
1033 | tbfd, ntbfd, h->root.root.string); | |
1034 | else if (tdef) | |
1035 | (*_bfd_error_handler) | |
1036 | (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), | |
1037 | tbfd, tsec, ntbfd, h->root.root.string); | |
1038 | else | |
1039 | (*_bfd_error_handler) | |
1040 | (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), | |
1041 | tbfd, ntbfd, ntsec, h->root.root.string); | |
1042 | ||
1043 | bfd_set_error (bfd_error_bad_value); | |
1044 | return FALSE; | |
1045 | } | |
1046 | ||
4cc11e76 | 1047 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
1048 | object or is weak in all dynamic objects. Internal and hidden |
1049 | visibility will make it unavailable to dynamic objects. */ | |
f5385ebf | 1050 | if (newdyn && !h->dynamic_def) |
45d6a902 AM |
1051 | { |
1052 | if (!bfd_is_und_section (sec)) | |
f5385ebf | 1053 | h->dynamic_def = 1; |
45d6a902 | 1054 | else |
252b5132 | 1055 | { |
45d6a902 AM |
1056 | /* Check if this symbol is weak in all dynamic objects. If it |
1057 | is the first time we see it in a dynamic object, we mark | |
1058 | if it is weak. Otherwise, we clear it. */ | |
f5385ebf | 1059 | if (!h->ref_dynamic) |
79349b09 | 1060 | { |
45d6a902 | 1061 | if (bind == STB_WEAK) |
f5385ebf | 1062 | h->dynamic_weak = 1; |
252b5132 | 1063 | } |
45d6a902 | 1064 | else if (bind != STB_WEAK) |
f5385ebf | 1065 | h->dynamic_weak = 0; |
252b5132 | 1066 | } |
45d6a902 | 1067 | } |
252b5132 | 1068 | |
45d6a902 AM |
1069 | /* If the old symbol has non-default visibility, we ignore the new |
1070 | definition from a dynamic object. */ | |
1071 | if (newdyn | |
9c7a29a3 | 1072 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1073 | && !bfd_is_und_section (sec)) |
1074 | { | |
1075 | *skip = TRUE; | |
1076 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1077 | h->ref_dynamic = 1; |
45d6a902 AM |
1078 | /* A protected symbol has external availability. Make sure it is |
1079 | recorded as dynamic. | |
1080 | ||
1081 | FIXME: Should we check type and size for protected symbol? */ | |
1082 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1083 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1084 | else |
1085 | return TRUE; | |
1086 | } | |
1087 | else if (!newdyn | |
9c7a29a3 | 1088 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1089 | && h->def_dynamic) |
45d6a902 AM |
1090 | { |
1091 | /* If the new symbol with non-default visibility comes from a | |
1092 | relocatable file and the old definition comes from a dynamic | |
1093 | object, we remove the old definition. */ | |
1094 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
d2dee3b2 L |
1095 | { |
1096 | /* Handle the case where the old dynamic definition is | |
1097 | default versioned. We need to copy the symbol info from | |
1098 | the symbol with default version to the normal one if it | |
1099 | was referenced before. */ | |
1100 | if (h->ref_regular) | |
1101 | { | |
1102 | const struct elf_backend_data *bed | |
1103 | = get_elf_backend_data (abfd); | |
1104 | struct elf_link_hash_entry *vh = *sym_hash; | |
1105 | vh->root.type = h->root.type; | |
1106 | h->root.type = bfd_link_hash_indirect; | |
1107 | (*bed->elf_backend_copy_indirect_symbol) (info, vh, h); | |
1108 | /* Protected symbols will override the dynamic definition | |
1109 | with default version. */ | |
1110 | if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED) | |
1111 | { | |
1112 | h->root.u.i.link = (struct bfd_link_hash_entry *) vh; | |
1113 | vh->dynamic_def = 1; | |
1114 | vh->ref_dynamic = 1; | |
1115 | } | |
1116 | else | |
1117 | { | |
1118 | h->root.type = vh->root.type; | |
1119 | vh->ref_dynamic = 0; | |
1120 | /* We have to hide it here since it was made dynamic | |
1121 | global with extra bits when the symbol info was | |
1122 | copied from the old dynamic definition. */ | |
1123 | (*bed->elf_backend_hide_symbol) (info, vh, TRUE); | |
1124 | } | |
1125 | h = vh; | |
1126 | } | |
1127 | else | |
1128 | h = *sym_hash; | |
1129 | } | |
1de1a317 | 1130 | |
f6e332e6 | 1131 | if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root) |
1de1a317 L |
1132 | && bfd_is_und_section (sec)) |
1133 | { | |
1134 | /* If the new symbol is undefined and the old symbol was | |
1135 | also undefined before, we need to make sure | |
1136 | _bfd_generic_link_add_one_symbol doesn't mess | |
f6e332e6 | 1137 | up the linker hash table undefs list. Since the old |
1de1a317 L |
1138 | definition came from a dynamic object, it is still on the |
1139 | undefs list. */ | |
1140 | h->root.type = bfd_link_hash_undefined; | |
1de1a317 L |
1141 | h->root.u.undef.abfd = abfd; |
1142 | } | |
1143 | else | |
1144 | { | |
1145 | h->root.type = bfd_link_hash_new; | |
1146 | h->root.u.undef.abfd = NULL; | |
1147 | } | |
1148 | ||
f5385ebf | 1149 | if (h->def_dynamic) |
252b5132 | 1150 | { |
f5385ebf AM |
1151 | h->def_dynamic = 0; |
1152 | h->ref_dynamic = 1; | |
1153 | h->dynamic_def = 1; | |
45d6a902 AM |
1154 | } |
1155 | /* FIXME: Should we check type and size for protected symbol? */ | |
1156 | h->size = 0; | |
1157 | h->type = 0; | |
1158 | return TRUE; | |
1159 | } | |
14a793b2 | 1160 | |
79349b09 AM |
1161 | /* Differentiate strong and weak symbols. */ |
1162 | newweak = bind == STB_WEAK; | |
1163 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1164 | || h->root.type == bfd_link_hash_undefweak); | |
14a793b2 | 1165 | |
15b43f48 AM |
1166 | /* If a new weak symbol definition comes from a regular file and the |
1167 | old symbol comes from a dynamic library, we treat the new one as | |
1168 | strong. Similarly, an old weak symbol definition from a regular | |
1169 | file is treated as strong when the new symbol comes from a dynamic | |
1170 | library. Further, an old weak symbol from a dynamic library is | |
1171 | treated as strong if the new symbol is from a dynamic library. | |
1172 | This reflects the way glibc's ld.so works. | |
1173 | ||
1174 | Do this before setting *type_change_ok or *size_change_ok so that | |
1175 | we warn properly when dynamic library symbols are overridden. */ | |
1176 | ||
1177 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1178 | newweak = FALSE; |
15b43f48 | 1179 | if (olddef && newdyn) |
0f8a2703 AM |
1180 | oldweak = FALSE; |
1181 | ||
fcb93ecf PB |
1182 | /* Allow changes between different types of funciton symbol. */ |
1183 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)) | |
1184 | && bed->is_function_type (h->type)) | |
1185 | *type_change_ok = TRUE; | |
1186 | ||
79349b09 AM |
1187 | /* It's OK to change the type if either the existing symbol or the |
1188 | new symbol is weak. A type change is also OK if the old symbol | |
1189 | is undefined and the new symbol is defined. */ | |
252b5132 | 1190 | |
79349b09 AM |
1191 | if (oldweak |
1192 | || newweak | |
1193 | || (newdef | |
1194 | && h->root.type == bfd_link_hash_undefined)) | |
1195 | *type_change_ok = TRUE; | |
1196 | ||
1197 | /* It's OK to change the size if either the existing symbol or the | |
1198 | new symbol is weak, or if the old symbol is undefined. */ | |
1199 | ||
1200 | if (*type_change_ok | |
1201 | || h->root.type == bfd_link_hash_undefined) | |
1202 | *size_change_ok = TRUE; | |
45d6a902 | 1203 | |
45d6a902 AM |
1204 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1205 | symbol, respectively, appears to be a common symbol in a dynamic | |
1206 | object. If a symbol appears in an uninitialized section, and is | |
1207 | not weak, and is not a function, then it may be a common symbol | |
1208 | which was resolved when the dynamic object was created. We want | |
1209 | to treat such symbols specially, because they raise special | |
1210 | considerations when setting the symbol size: if the symbol | |
1211 | appears as a common symbol in a regular object, and the size in | |
1212 | the regular object is larger, we must make sure that we use the | |
1213 | larger size. This problematic case can always be avoided in C, | |
1214 | but it must be handled correctly when using Fortran shared | |
1215 | libraries. | |
1216 | ||
1217 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1218 | likewise for OLDDYNCOMMON and OLDDEF. | |
1219 | ||
1220 | Note that this test is just a heuristic, and that it is quite | |
1221 | possible to have an uninitialized symbol in a shared object which | |
1222 | is really a definition, rather than a common symbol. This could | |
1223 | lead to some minor confusion when the symbol really is a common | |
1224 | symbol in some regular object. However, I think it will be | |
1225 | harmless. */ | |
1226 | ||
1227 | if (newdyn | |
1228 | && newdef | |
79349b09 | 1229 | && !newweak |
45d6a902 AM |
1230 | && (sec->flags & SEC_ALLOC) != 0 |
1231 | && (sec->flags & SEC_LOAD) == 0 | |
1232 | && sym->st_size > 0 | |
fcb93ecf | 1233 | && !bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
45d6a902 AM |
1234 | newdyncommon = TRUE; |
1235 | else | |
1236 | newdyncommon = FALSE; | |
1237 | ||
1238 | if (olddyn | |
1239 | && olddef | |
1240 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1241 | && h->def_dynamic |
45d6a902 AM |
1242 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1243 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1244 | && h->size > 0 | |
fcb93ecf | 1245 | && !bed->is_function_type (h->type)) |
45d6a902 AM |
1246 | olddyncommon = TRUE; |
1247 | else | |
1248 | olddyncommon = FALSE; | |
1249 | ||
a4d8e49b L |
1250 | /* We now know everything about the old and new symbols. We ask the |
1251 | backend to check if we can merge them. */ | |
a4d8e49b L |
1252 | if (bed->merge_symbol |
1253 | && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue, | |
1254 | pold_alignment, skip, override, | |
1255 | type_change_ok, size_change_ok, | |
1256 | &newdyn, &newdef, &newdyncommon, &newweak, | |
1257 | abfd, &sec, | |
1258 | &olddyn, &olddef, &olddyncommon, &oldweak, | |
1259 | oldbfd, &oldsec)) | |
1260 | return FALSE; | |
1261 | ||
45d6a902 AM |
1262 | /* If both the old and the new symbols look like common symbols in a |
1263 | dynamic object, set the size of the symbol to the larger of the | |
1264 | two. */ | |
1265 | ||
1266 | if (olddyncommon | |
1267 | && newdyncommon | |
1268 | && sym->st_size != h->size) | |
1269 | { | |
1270 | /* Since we think we have two common symbols, issue a multiple | |
1271 | common warning if desired. Note that we only warn if the | |
1272 | size is different. If the size is the same, we simply let | |
1273 | the old symbol override the new one as normally happens with | |
1274 | symbols defined in dynamic objects. */ | |
1275 | ||
1276 | if (! ((*info->callbacks->multiple_common) | |
1277 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1278 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1279 | return FALSE; | |
252b5132 | 1280 | |
45d6a902 AM |
1281 | if (sym->st_size > h->size) |
1282 | h->size = sym->st_size; | |
252b5132 | 1283 | |
45d6a902 | 1284 | *size_change_ok = TRUE; |
252b5132 RH |
1285 | } |
1286 | ||
45d6a902 AM |
1287 | /* If we are looking at a dynamic object, and we have found a |
1288 | definition, we need to see if the symbol was already defined by | |
1289 | some other object. If so, we want to use the existing | |
1290 | definition, and we do not want to report a multiple symbol | |
1291 | definition error; we do this by clobbering *PSEC to be | |
1292 | bfd_und_section_ptr. | |
1293 | ||
1294 | We treat a common symbol as a definition if the symbol in the | |
1295 | shared library is a function, since common symbols always | |
1296 | represent variables; this can cause confusion in principle, but | |
1297 | any such confusion would seem to indicate an erroneous program or | |
1298 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1299 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1300 | |
1301 | if (newdyn | |
1302 | && newdef | |
77cfaee6 | 1303 | && (olddef |
45d6a902 | 1304 | || (h->root.type == bfd_link_hash_common |
79349b09 | 1305 | && (newweak |
fcb93ecf | 1306 | || bed->is_function_type (ELF_ST_TYPE (sym->st_info)))))) |
45d6a902 AM |
1307 | { |
1308 | *override = TRUE; | |
1309 | newdef = FALSE; | |
1310 | newdyncommon = FALSE; | |
252b5132 | 1311 | |
45d6a902 AM |
1312 | *psec = sec = bfd_und_section_ptr; |
1313 | *size_change_ok = TRUE; | |
252b5132 | 1314 | |
45d6a902 AM |
1315 | /* If we get here when the old symbol is a common symbol, then |
1316 | we are explicitly letting it override a weak symbol or | |
1317 | function in a dynamic object, and we don't want to warn about | |
1318 | a type change. If the old symbol is a defined symbol, a type | |
1319 | change warning may still be appropriate. */ | |
252b5132 | 1320 | |
45d6a902 AM |
1321 | if (h->root.type == bfd_link_hash_common) |
1322 | *type_change_ok = TRUE; | |
1323 | } | |
1324 | ||
1325 | /* Handle the special case of an old common symbol merging with a | |
1326 | new symbol which looks like a common symbol in a shared object. | |
1327 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1328 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1329 | right thing. */ | |
45d6a902 AM |
1330 | |
1331 | if (newdyncommon | |
1332 | && h->root.type == bfd_link_hash_common) | |
1333 | { | |
1334 | *override = TRUE; | |
1335 | newdef = FALSE; | |
1336 | newdyncommon = FALSE; | |
1337 | *pvalue = sym->st_size; | |
a4d8e49b | 1338 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1339 | *size_change_ok = TRUE; |
1340 | } | |
1341 | ||
c5e2cead | 1342 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1343 | if (newdef && olddef && newweak) |
c5e2cead L |
1344 | *skip = TRUE; |
1345 | ||
45d6a902 AM |
1346 | /* If the old symbol is from a dynamic object, and the new symbol is |
1347 | a definition which is not from a dynamic object, then the new | |
1348 | symbol overrides the old symbol. Symbols from regular files | |
1349 | always take precedence over symbols from dynamic objects, even if | |
1350 | they are defined after the dynamic object in the link. | |
1351 | ||
1352 | As above, we again permit a common symbol in a regular object to | |
1353 | override a definition in a shared object if the shared object | |
0f8a2703 | 1354 | symbol is a function or is weak. */ |
45d6a902 AM |
1355 | |
1356 | flip = NULL; | |
77cfaee6 | 1357 | if (!newdyn |
45d6a902 AM |
1358 | && (newdef |
1359 | || (bfd_is_com_section (sec) | |
79349b09 | 1360 | && (oldweak |
fcb93ecf | 1361 | || bed->is_function_type (h->type)))) |
45d6a902 AM |
1362 | && olddyn |
1363 | && olddef | |
f5385ebf | 1364 | && h->def_dynamic) |
45d6a902 AM |
1365 | { |
1366 | /* Change the hash table entry to undefined, and let | |
1367 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1368 | new definition. */ | |
1369 | ||
1370 | h->root.type = bfd_link_hash_undefined; | |
1371 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1372 | *size_change_ok = TRUE; | |
1373 | ||
1374 | olddef = FALSE; | |
1375 | olddyncommon = FALSE; | |
1376 | ||
1377 | /* We again permit a type change when a common symbol may be | |
1378 | overriding a function. */ | |
1379 | ||
1380 | if (bfd_is_com_section (sec)) | |
1381 | *type_change_ok = TRUE; | |
1382 | ||
1383 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1384 | flip = *sym_hash; | |
1385 | else | |
1386 | /* This union may have been set to be non-NULL when this symbol | |
1387 | was seen in a dynamic object. We must force the union to be | |
1388 | NULL, so that it is correct for a regular symbol. */ | |
1389 | h->verinfo.vertree = NULL; | |
1390 | } | |
1391 | ||
1392 | /* Handle the special case of a new common symbol merging with an | |
1393 | old symbol that looks like it might be a common symbol defined in | |
1394 | a shared object. Note that we have already handled the case in | |
1395 | which a new common symbol should simply override the definition | |
1396 | in the shared library. */ | |
1397 | ||
1398 | if (! newdyn | |
1399 | && bfd_is_com_section (sec) | |
1400 | && olddyncommon) | |
1401 | { | |
1402 | /* It would be best if we could set the hash table entry to a | |
1403 | common symbol, but we don't know what to use for the section | |
1404 | or the alignment. */ | |
1405 | if (! ((*info->callbacks->multiple_common) | |
1406 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1407 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1408 | return FALSE; | |
1409 | ||
4cc11e76 | 1410 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1411 | larger, pretend that the new symbol has its size. */ |
1412 | ||
1413 | if (h->size > *pvalue) | |
1414 | *pvalue = h->size; | |
1415 | ||
af44c138 L |
1416 | /* We need to remember the alignment required by the symbol |
1417 | in the dynamic object. */ | |
1418 | BFD_ASSERT (pold_alignment); | |
1419 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1420 | |
1421 | olddef = FALSE; | |
1422 | olddyncommon = FALSE; | |
1423 | ||
1424 | h->root.type = bfd_link_hash_undefined; | |
1425 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1426 | ||
1427 | *size_change_ok = TRUE; | |
1428 | *type_change_ok = TRUE; | |
1429 | ||
1430 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1431 | flip = *sym_hash; | |
1432 | else | |
1433 | h->verinfo.vertree = NULL; | |
1434 | } | |
1435 | ||
1436 | if (flip != NULL) | |
1437 | { | |
1438 | /* Handle the case where we had a versioned symbol in a dynamic | |
1439 | library and now find a definition in a normal object. In this | |
1440 | case, we make the versioned symbol point to the normal one. */ | |
9c5bfbb7 | 1441 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 1442 | flip->root.type = h->root.type; |
00cbee0a | 1443 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1444 | h->root.type = bfd_link_hash_indirect; |
1445 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1446 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1447 | if (h->def_dynamic) |
45d6a902 | 1448 | { |
f5385ebf AM |
1449 | h->def_dynamic = 0; |
1450 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1451 | } |
1452 | } | |
1453 | ||
45d6a902 AM |
1454 | return TRUE; |
1455 | } | |
1456 | ||
1457 | /* This function is called to create an indirect symbol from the | |
1458 | default for the symbol with the default version if needed. The | |
1459 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1460 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 AM |
1461 | |
1462 | bfd_boolean | |
268b6b39 AM |
1463 | _bfd_elf_add_default_symbol (bfd *abfd, |
1464 | struct bfd_link_info *info, | |
1465 | struct elf_link_hash_entry *h, | |
1466 | const char *name, | |
1467 | Elf_Internal_Sym *sym, | |
1468 | asection **psec, | |
1469 | bfd_vma *value, | |
1470 | bfd_boolean *dynsym, | |
0f8a2703 | 1471 | bfd_boolean override) |
45d6a902 AM |
1472 | { |
1473 | bfd_boolean type_change_ok; | |
1474 | bfd_boolean size_change_ok; | |
1475 | bfd_boolean skip; | |
1476 | char *shortname; | |
1477 | struct elf_link_hash_entry *hi; | |
1478 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1479 | const struct elf_backend_data *bed; |
45d6a902 AM |
1480 | bfd_boolean collect; |
1481 | bfd_boolean dynamic; | |
1482 | char *p; | |
1483 | size_t len, shortlen; | |
1484 | asection *sec; | |
1485 | ||
1486 | /* If this symbol has a version, and it is the default version, we | |
1487 | create an indirect symbol from the default name to the fully | |
1488 | decorated name. This will cause external references which do not | |
1489 | specify a version to be bound to this version of the symbol. */ | |
1490 | p = strchr (name, ELF_VER_CHR); | |
1491 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1492 | return TRUE; | |
1493 | ||
1494 | if (override) | |
1495 | { | |
4cc11e76 | 1496 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1497 | need to create the indirect symbol from the default name. */ |
1498 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1499 | FALSE, FALSE); | |
1500 | BFD_ASSERT (hi != NULL); | |
1501 | if (hi == h) | |
1502 | return TRUE; | |
1503 | while (hi->root.type == bfd_link_hash_indirect | |
1504 | || hi->root.type == bfd_link_hash_warning) | |
1505 | { | |
1506 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1507 | if (hi == h) | |
1508 | return TRUE; | |
1509 | } | |
1510 | } | |
1511 | ||
1512 | bed = get_elf_backend_data (abfd); | |
1513 | collect = bed->collect; | |
1514 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1515 | ||
1516 | shortlen = p - name; | |
1517 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1518 | if (shortname == NULL) | |
1519 | return FALSE; | |
1520 | memcpy (shortname, name, shortlen); | |
1521 | shortname[shortlen] = '\0'; | |
1522 | ||
1523 | /* We are going to create a new symbol. Merge it with any existing | |
1524 | symbol with this name. For the purposes of the merge, act as | |
1525 | though we were defining the symbol we just defined, although we | |
1526 | actually going to define an indirect symbol. */ | |
1527 | type_change_ok = FALSE; | |
1528 | size_change_ok = FALSE; | |
1529 | sec = *psec; | |
1530 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1531 | NULL, &hi, &skip, &override, |
1532 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1533 | return FALSE; |
1534 | ||
1535 | if (skip) | |
1536 | goto nondefault; | |
1537 | ||
1538 | if (! override) | |
1539 | { | |
1540 | bh = &hi->root; | |
1541 | if (! (_bfd_generic_link_add_one_symbol | |
1542 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1543 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1544 | return FALSE; |
1545 | hi = (struct elf_link_hash_entry *) bh; | |
1546 | } | |
1547 | else | |
1548 | { | |
1549 | /* In this case the symbol named SHORTNAME is overriding the | |
1550 | indirect symbol we want to add. We were planning on making | |
1551 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1552 | is the name without a version. NAME is the fully versioned | |
1553 | name, and it is the default version. | |
1554 | ||
1555 | Overriding means that we already saw a definition for the | |
1556 | symbol SHORTNAME in a regular object, and it is overriding | |
1557 | the symbol defined in the dynamic object. | |
1558 | ||
1559 | When this happens, we actually want to change NAME, the | |
1560 | symbol we just added, to refer to SHORTNAME. This will cause | |
1561 | references to NAME in the shared object to become references | |
1562 | to SHORTNAME in the regular object. This is what we expect | |
1563 | when we override a function in a shared object: that the | |
1564 | references in the shared object will be mapped to the | |
1565 | definition in the regular object. */ | |
1566 | ||
1567 | while (hi->root.type == bfd_link_hash_indirect | |
1568 | || hi->root.type == bfd_link_hash_warning) | |
1569 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1570 | ||
1571 | h->root.type = bfd_link_hash_indirect; | |
1572 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1573 | if (h->def_dynamic) |
45d6a902 | 1574 | { |
f5385ebf AM |
1575 | h->def_dynamic = 0; |
1576 | hi->ref_dynamic = 1; | |
1577 | if (hi->ref_regular | |
1578 | || hi->def_regular) | |
45d6a902 | 1579 | { |
c152c796 | 1580 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1581 | return FALSE; |
1582 | } | |
1583 | } | |
1584 | ||
1585 | /* Now set HI to H, so that the following code will set the | |
1586 | other fields correctly. */ | |
1587 | hi = h; | |
1588 | } | |
1589 | ||
fab4a87f L |
1590 | /* Check if HI is a warning symbol. */ |
1591 | if (hi->root.type == bfd_link_hash_warning) | |
1592 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1593 | ||
45d6a902 AM |
1594 | /* If there is a duplicate definition somewhere, then HI may not |
1595 | point to an indirect symbol. We will have reported an error to | |
1596 | the user in that case. */ | |
1597 | ||
1598 | if (hi->root.type == bfd_link_hash_indirect) | |
1599 | { | |
1600 | struct elf_link_hash_entry *ht; | |
1601 | ||
45d6a902 | 1602 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 1603 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 AM |
1604 | |
1605 | /* See if the new flags lead us to realize that the symbol must | |
1606 | be dynamic. */ | |
1607 | if (! *dynsym) | |
1608 | { | |
1609 | if (! dynamic) | |
1610 | { | |
1611 | if (info->shared | |
f5385ebf | 1612 | || hi->ref_dynamic) |
45d6a902 AM |
1613 | *dynsym = TRUE; |
1614 | } | |
1615 | else | |
1616 | { | |
f5385ebf | 1617 | if (hi->ref_regular) |
45d6a902 AM |
1618 | *dynsym = TRUE; |
1619 | } | |
1620 | } | |
1621 | } | |
1622 | ||
1623 | /* We also need to define an indirection from the nondefault version | |
1624 | of the symbol. */ | |
1625 | ||
1626 | nondefault: | |
1627 | len = strlen (name); | |
1628 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1629 | if (shortname == NULL) | |
1630 | return FALSE; | |
1631 | memcpy (shortname, name, shortlen); | |
1632 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1633 | ||
1634 | /* Once again, merge with any existing symbol. */ | |
1635 | type_change_ok = FALSE; | |
1636 | size_change_ok = FALSE; | |
1637 | sec = *psec; | |
1638 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1639 | NULL, &hi, &skip, &override, |
1640 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1641 | return FALSE; |
1642 | ||
1643 | if (skip) | |
1644 | return TRUE; | |
1645 | ||
1646 | if (override) | |
1647 | { | |
1648 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1649 | the type of override we do in the case above unless it is | |
4cc11e76 | 1650 | overridden by a versioned definition. */ |
45d6a902 AM |
1651 | if (hi->root.type != bfd_link_hash_defined |
1652 | && hi->root.type != bfd_link_hash_defweak) | |
1653 | (*_bfd_error_handler) | |
d003868e AM |
1654 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1655 | abfd, shortname); | |
45d6a902 AM |
1656 | } |
1657 | else | |
1658 | { | |
1659 | bh = &hi->root; | |
1660 | if (! (_bfd_generic_link_add_one_symbol | |
1661 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1662 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1663 | return FALSE; |
1664 | hi = (struct elf_link_hash_entry *) bh; | |
1665 | ||
1666 | /* If there is a duplicate definition somewhere, then HI may not | |
1667 | point to an indirect symbol. We will have reported an error | |
1668 | to the user in that case. */ | |
1669 | ||
1670 | if (hi->root.type == bfd_link_hash_indirect) | |
1671 | { | |
fcfa13d2 | 1672 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
45d6a902 AM |
1673 | |
1674 | /* See if the new flags lead us to realize that the symbol | |
1675 | must be dynamic. */ | |
1676 | if (! *dynsym) | |
1677 | { | |
1678 | if (! dynamic) | |
1679 | { | |
1680 | if (info->shared | |
f5385ebf | 1681 | || hi->ref_dynamic) |
45d6a902 AM |
1682 | *dynsym = TRUE; |
1683 | } | |
1684 | else | |
1685 | { | |
f5385ebf | 1686 | if (hi->ref_regular) |
45d6a902 AM |
1687 | *dynsym = TRUE; |
1688 | } | |
1689 | } | |
1690 | } | |
1691 | } | |
1692 | ||
1693 | return TRUE; | |
1694 | } | |
1695 | \f | |
1696 | /* This routine is used to export all defined symbols into the dynamic | |
1697 | symbol table. It is called via elf_link_hash_traverse. */ | |
1698 | ||
1699 | bfd_boolean | |
268b6b39 | 1700 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1701 | { |
268b6b39 | 1702 | struct elf_info_failed *eif = data; |
45d6a902 | 1703 | |
55255dae L |
1704 | /* Ignore this if we won't export it. */ |
1705 | if (!eif->info->export_dynamic && !h->dynamic) | |
1706 | return TRUE; | |
1707 | ||
45d6a902 AM |
1708 | /* Ignore indirect symbols. These are added by the versioning code. */ |
1709 | if (h->root.type == bfd_link_hash_indirect) | |
1710 | return TRUE; | |
1711 | ||
1712 | if (h->root.type == bfd_link_hash_warning) | |
1713 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1714 | ||
1715 | if (h->dynindx == -1 | |
f5385ebf AM |
1716 | && (h->def_regular |
1717 | || h->ref_regular)) | |
45d6a902 AM |
1718 | { |
1719 | struct bfd_elf_version_tree *t; | |
1720 | struct bfd_elf_version_expr *d; | |
1721 | ||
1722 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1723 | { | |
108ba305 | 1724 | if (t->globals.list != NULL) |
45d6a902 | 1725 | { |
108ba305 JJ |
1726 | d = (*t->match) (&t->globals, NULL, h->root.root.string); |
1727 | if (d != NULL) | |
1728 | goto doit; | |
45d6a902 AM |
1729 | } |
1730 | ||
108ba305 | 1731 | if (t->locals.list != NULL) |
45d6a902 | 1732 | { |
108ba305 JJ |
1733 | d = (*t->match) (&t->locals, NULL, h->root.root.string); |
1734 | if (d != NULL) | |
1735 | return TRUE; | |
45d6a902 AM |
1736 | } |
1737 | } | |
1738 | ||
1739 | if (!eif->verdefs) | |
1740 | { | |
1741 | doit: | |
c152c796 | 1742 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
1743 | { |
1744 | eif->failed = TRUE; | |
1745 | return FALSE; | |
1746 | } | |
1747 | } | |
1748 | } | |
1749 | ||
1750 | return TRUE; | |
1751 | } | |
1752 | \f | |
1753 | /* Look through the symbols which are defined in other shared | |
1754 | libraries and referenced here. Update the list of version | |
1755 | dependencies. This will be put into the .gnu.version_r section. | |
1756 | This function is called via elf_link_hash_traverse. */ | |
1757 | ||
1758 | bfd_boolean | |
268b6b39 AM |
1759 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1760 | void *data) | |
45d6a902 | 1761 | { |
268b6b39 | 1762 | struct elf_find_verdep_info *rinfo = data; |
45d6a902 AM |
1763 | Elf_Internal_Verneed *t; |
1764 | Elf_Internal_Vernaux *a; | |
1765 | bfd_size_type amt; | |
1766 | ||
1767 | if (h->root.type == bfd_link_hash_warning) | |
1768 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1769 | ||
1770 | /* We only care about symbols defined in shared objects with version | |
1771 | information. */ | |
f5385ebf AM |
1772 | if (!h->def_dynamic |
1773 | || h->def_regular | |
45d6a902 AM |
1774 | || h->dynindx == -1 |
1775 | || h->verinfo.verdef == NULL) | |
1776 | return TRUE; | |
1777 | ||
1778 | /* See if we already know about this version. */ | |
1779 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1780 | { | |
1781 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1782 | continue; | |
1783 | ||
1784 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1785 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1786 | return TRUE; | |
1787 | ||
1788 | break; | |
1789 | } | |
1790 | ||
1791 | /* This is a new version. Add it to tree we are building. */ | |
1792 | ||
1793 | if (t == NULL) | |
1794 | { | |
1795 | amt = sizeof *t; | |
268b6b39 | 1796 | t = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1797 | if (t == NULL) |
1798 | { | |
1799 | rinfo->failed = TRUE; | |
1800 | return FALSE; | |
1801 | } | |
1802 | ||
1803 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1804 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1805 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1806 | } | |
1807 | ||
1808 | amt = sizeof *a; | |
268b6b39 | 1809 | a = bfd_zalloc (rinfo->output_bfd, amt); |
14b1c01e AM |
1810 | if (a == NULL) |
1811 | { | |
1812 | rinfo->failed = TRUE; | |
1813 | return FALSE; | |
1814 | } | |
45d6a902 AM |
1815 | |
1816 | /* Note that we are copying a string pointer here, and testing it | |
1817 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1818 | discard the string data when low in memory, this will have to be | |
1819 | fixed. */ | |
1820 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1821 | ||
1822 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1823 | a->vna_nextptr = t->vn_auxptr; | |
1824 | ||
1825 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1826 | ++rinfo->vers; | |
1827 | ||
1828 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1829 | ||
1830 | t->vn_auxptr = a; | |
1831 | ||
1832 | return TRUE; | |
1833 | } | |
1834 | ||
1835 | /* Figure out appropriate versions for all the symbols. We may not | |
1836 | have the version number script until we have read all of the input | |
1837 | files, so until that point we don't know which symbols should be | |
1838 | local. This function is called via elf_link_hash_traverse. */ | |
1839 | ||
1840 | bfd_boolean | |
268b6b39 | 1841 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
1842 | { |
1843 | struct elf_assign_sym_version_info *sinfo; | |
1844 | struct bfd_link_info *info; | |
9c5bfbb7 | 1845 | const struct elf_backend_data *bed; |
45d6a902 AM |
1846 | struct elf_info_failed eif; |
1847 | char *p; | |
1848 | bfd_size_type amt; | |
1849 | ||
268b6b39 | 1850 | sinfo = data; |
45d6a902 AM |
1851 | info = sinfo->info; |
1852 | ||
1853 | if (h->root.type == bfd_link_hash_warning) | |
1854 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1855 | ||
1856 | /* Fix the symbol flags. */ | |
1857 | eif.failed = FALSE; | |
1858 | eif.info = info; | |
1859 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1860 | { | |
1861 | if (eif.failed) | |
1862 | sinfo->failed = TRUE; | |
1863 | return FALSE; | |
1864 | } | |
1865 | ||
1866 | /* We only need version numbers for symbols defined in regular | |
1867 | objects. */ | |
f5385ebf | 1868 | if (!h->def_regular) |
45d6a902 AM |
1869 | return TRUE; |
1870 | ||
1871 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1872 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1873 | if (p != NULL && h->verinfo.vertree == NULL) | |
1874 | { | |
1875 | struct bfd_elf_version_tree *t; | |
1876 | bfd_boolean hidden; | |
1877 | ||
1878 | hidden = TRUE; | |
1879 | ||
1880 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1881 | not a hidden symbol. */ | |
1882 | ++p; | |
1883 | if (*p == ELF_VER_CHR) | |
1884 | { | |
1885 | hidden = FALSE; | |
1886 | ++p; | |
1887 | } | |
1888 | ||
1889 | /* If there is no version string, we can just return out. */ | |
1890 | if (*p == '\0') | |
1891 | { | |
1892 | if (hidden) | |
f5385ebf | 1893 | h->hidden = 1; |
45d6a902 AM |
1894 | return TRUE; |
1895 | } | |
1896 | ||
1897 | /* Look for the version. If we find it, it is no longer weak. */ | |
1898 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1899 | { | |
1900 | if (strcmp (t->name, p) == 0) | |
1901 | { | |
1902 | size_t len; | |
1903 | char *alc; | |
1904 | struct bfd_elf_version_expr *d; | |
1905 | ||
1906 | len = p - h->root.root.string; | |
268b6b39 | 1907 | alc = bfd_malloc (len); |
45d6a902 | 1908 | if (alc == NULL) |
14b1c01e AM |
1909 | { |
1910 | sinfo->failed = TRUE; | |
1911 | return FALSE; | |
1912 | } | |
45d6a902 AM |
1913 | memcpy (alc, h->root.root.string, len - 1); |
1914 | alc[len - 1] = '\0'; | |
1915 | if (alc[len - 2] == ELF_VER_CHR) | |
1916 | alc[len - 2] = '\0'; | |
1917 | ||
1918 | h->verinfo.vertree = t; | |
1919 | t->used = TRUE; | |
1920 | d = NULL; | |
1921 | ||
108ba305 JJ |
1922 | if (t->globals.list != NULL) |
1923 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
1924 | |
1925 | /* See if there is anything to force this symbol to | |
1926 | local scope. */ | |
108ba305 | 1927 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 1928 | { |
108ba305 JJ |
1929 | d = (*t->match) (&t->locals, NULL, alc); |
1930 | if (d != NULL | |
1931 | && h->dynindx != -1 | |
108ba305 JJ |
1932 | && ! info->export_dynamic) |
1933 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
1934 | } |
1935 | ||
1936 | free (alc); | |
1937 | break; | |
1938 | } | |
1939 | } | |
1940 | ||
1941 | /* If we are building an application, we need to create a | |
1942 | version node for this version. */ | |
36af4a4e | 1943 | if (t == NULL && info->executable) |
45d6a902 AM |
1944 | { |
1945 | struct bfd_elf_version_tree **pp; | |
1946 | int version_index; | |
1947 | ||
1948 | /* If we aren't going to export this symbol, we don't need | |
1949 | to worry about it. */ | |
1950 | if (h->dynindx == -1) | |
1951 | return TRUE; | |
1952 | ||
1953 | amt = sizeof *t; | |
108ba305 | 1954 | t = bfd_zalloc (sinfo->output_bfd, amt); |
45d6a902 AM |
1955 | if (t == NULL) |
1956 | { | |
1957 | sinfo->failed = TRUE; | |
1958 | return FALSE; | |
1959 | } | |
1960 | ||
45d6a902 | 1961 | t->name = p; |
45d6a902 AM |
1962 | t->name_indx = (unsigned int) -1; |
1963 | t->used = TRUE; | |
1964 | ||
1965 | version_index = 1; | |
1966 | /* Don't count anonymous version tag. */ | |
1967 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1968 | version_index = 0; | |
1969 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1970 | ++version_index; | |
1971 | t->vernum = version_index; | |
1972 | ||
1973 | *pp = t; | |
1974 | ||
1975 | h->verinfo.vertree = t; | |
1976 | } | |
1977 | else if (t == NULL) | |
1978 | { | |
1979 | /* We could not find the version for a symbol when | |
1980 | generating a shared archive. Return an error. */ | |
1981 | (*_bfd_error_handler) | |
c55fe096 | 1982 | (_("%B: version node not found for symbol %s"), |
d003868e | 1983 | sinfo->output_bfd, h->root.root.string); |
45d6a902 AM |
1984 | bfd_set_error (bfd_error_bad_value); |
1985 | sinfo->failed = TRUE; | |
1986 | return FALSE; | |
1987 | } | |
1988 | ||
1989 | if (hidden) | |
f5385ebf | 1990 | h->hidden = 1; |
45d6a902 AM |
1991 | } |
1992 | ||
1993 | /* If we don't have a version for this symbol, see if we can find | |
1994 | something. */ | |
1995 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
1996 | { | |
1997 | struct bfd_elf_version_tree *t; | |
1998 | struct bfd_elf_version_tree *local_ver; | |
1999 | struct bfd_elf_version_expr *d; | |
2000 | ||
2001 | /* See if can find what version this symbol is in. If the | |
2002 | symbol is supposed to be local, then don't actually register | |
2003 | it. */ | |
2004 | local_ver = NULL; | |
2005 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
2006 | { | |
108ba305 | 2007 | if (t->globals.list != NULL) |
45d6a902 AM |
2008 | { |
2009 | bfd_boolean matched; | |
2010 | ||
2011 | matched = FALSE; | |
108ba305 JJ |
2012 | d = NULL; |
2013 | while ((d = (*t->match) (&t->globals, d, | |
2014 | h->root.root.string)) != NULL) | |
2015 | if (d->symver) | |
2016 | matched = TRUE; | |
2017 | else | |
2018 | { | |
2019 | /* There is a version without definition. Make | |
2020 | the symbol the default definition for this | |
2021 | version. */ | |
2022 | h->verinfo.vertree = t; | |
2023 | local_ver = NULL; | |
2024 | d->script = 1; | |
2025 | break; | |
2026 | } | |
45d6a902 AM |
2027 | if (d != NULL) |
2028 | break; | |
2029 | else if (matched) | |
2030 | /* There is no undefined version for this symbol. Hide the | |
2031 | default one. */ | |
2032 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2033 | } | |
2034 | ||
108ba305 | 2035 | if (t->locals.list != NULL) |
45d6a902 | 2036 | { |
108ba305 JJ |
2037 | d = NULL; |
2038 | while ((d = (*t->match) (&t->locals, d, | |
2039 | h->root.root.string)) != NULL) | |
45d6a902 | 2040 | { |
108ba305 | 2041 | local_ver = t; |
45d6a902 | 2042 | /* If the match is "*", keep looking for a more |
108ba305 JJ |
2043 | explicit, perhaps even global, match. |
2044 | XXX: Shouldn't this be !d->wildcard instead? */ | |
2045 | if (d->pattern[0] != '*' || d->pattern[1] != '\0') | |
2046 | break; | |
45d6a902 AM |
2047 | } |
2048 | ||
2049 | if (d != NULL) | |
2050 | break; | |
2051 | } | |
2052 | } | |
2053 | ||
2054 | if (local_ver != NULL) | |
2055 | { | |
2056 | h->verinfo.vertree = local_ver; | |
2057 | if (h->dynindx != -1 | |
45d6a902 AM |
2058 | && ! info->export_dynamic) |
2059 | { | |
2060 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2061 | } | |
2062 | } | |
2063 | } | |
2064 | ||
2065 | return TRUE; | |
2066 | } | |
2067 | \f | |
45d6a902 AM |
2068 | /* Read and swap the relocs from the section indicated by SHDR. This |
2069 | may be either a REL or a RELA section. The relocations are | |
2070 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2071 | which should have already been allocated to contain enough space. | |
2072 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2073 | relocations should be stored. | |
2074 | ||
2075 | Returns FALSE if something goes wrong. */ | |
2076 | ||
2077 | static bfd_boolean | |
268b6b39 | 2078 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2079 | asection *sec, |
268b6b39 AM |
2080 | Elf_Internal_Shdr *shdr, |
2081 | void *external_relocs, | |
2082 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2083 | { |
9c5bfbb7 | 2084 | const struct elf_backend_data *bed; |
268b6b39 | 2085 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2086 | const bfd_byte *erela; |
2087 | const bfd_byte *erelaend; | |
2088 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2089 | Elf_Internal_Shdr *symtab_hdr; |
2090 | size_t nsyms; | |
45d6a902 | 2091 | |
45d6a902 AM |
2092 | /* Position ourselves at the start of the section. */ |
2093 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2094 | return FALSE; | |
2095 | ||
2096 | /* Read the relocations. */ | |
2097 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2098 | return FALSE; | |
2099 | ||
243ef1e0 L |
2100 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2101 | nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
2102 | ||
45d6a902 AM |
2103 | bed = get_elf_backend_data (abfd); |
2104 | ||
2105 | /* Convert the external relocations to the internal format. */ | |
2106 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2107 | swap_in = bed->s->swap_reloc_in; | |
2108 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2109 | swap_in = bed->s->swap_reloca_in; | |
2110 | else | |
2111 | { | |
2112 | bfd_set_error (bfd_error_wrong_format); | |
2113 | return FALSE; | |
2114 | } | |
2115 | ||
2116 | erela = external_relocs; | |
51992aec | 2117 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2118 | irela = internal_relocs; |
2119 | while (erela < erelaend) | |
2120 | { | |
243ef1e0 L |
2121 | bfd_vma r_symndx; |
2122 | ||
45d6a902 | 2123 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2124 | r_symndx = ELF32_R_SYM (irela->r_info); |
2125 | if (bed->s->arch_size == 64) | |
2126 | r_symndx >>= 24; | |
2127 | if ((size_t) r_symndx >= nsyms) | |
2128 | { | |
2129 | (*_bfd_error_handler) | |
d003868e AM |
2130 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" |
2131 | " for offset 0x%lx in section `%A'"), | |
2132 | abfd, sec, | |
2133 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
2134 | bfd_set_error (bfd_error_bad_value); |
2135 | return FALSE; | |
2136 | } | |
45d6a902 AM |
2137 | irela += bed->s->int_rels_per_ext_rel; |
2138 | erela += shdr->sh_entsize; | |
2139 | } | |
2140 | ||
2141 | return TRUE; | |
2142 | } | |
2143 | ||
2144 | /* Read and swap the relocs for a section O. They may have been | |
2145 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2146 | not NULL, they are used as buffers to read into. They are known to | |
2147 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2148 | the return value is allocated using either malloc or bfd_alloc, | |
2149 | according to the KEEP_MEMORY argument. If O has two relocation | |
2150 | sections (both REL and RELA relocations), then the REL_HDR | |
2151 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
2152 | REL_HDR2 relocations. */ | |
2153 | ||
2154 | Elf_Internal_Rela * | |
268b6b39 AM |
2155 | _bfd_elf_link_read_relocs (bfd *abfd, |
2156 | asection *o, | |
2157 | void *external_relocs, | |
2158 | Elf_Internal_Rela *internal_relocs, | |
2159 | bfd_boolean keep_memory) | |
45d6a902 AM |
2160 | { |
2161 | Elf_Internal_Shdr *rel_hdr; | |
268b6b39 | 2162 | void *alloc1 = NULL; |
45d6a902 | 2163 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2164 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
2165 | |
2166 | if (elf_section_data (o)->relocs != NULL) | |
2167 | return elf_section_data (o)->relocs; | |
2168 | ||
2169 | if (o->reloc_count == 0) | |
2170 | return NULL; | |
2171 | ||
2172 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
2173 | ||
2174 | if (internal_relocs == NULL) | |
2175 | { | |
2176 | bfd_size_type size; | |
2177 | ||
2178 | size = o->reloc_count; | |
2179 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
2180 | if (keep_memory) | |
268b6b39 | 2181 | internal_relocs = bfd_alloc (abfd, size); |
45d6a902 | 2182 | else |
268b6b39 | 2183 | internal_relocs = alloc2 = bfd_malloc (size); |
45d6a902 AM |
2184 | if (internal_relocs == NULL) |
2185 | goto error_return; | |
2186 | } | |
2187 | ||
2188 | if (external_relocs == NULL) | |
2189 | { | |
2190 | bfd_size_type size = rel_hdr->sh_size; | |
2191 | ||
2192 | if (elf_section_data (o)->rel_hdr2) | |
2193 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
268b6b39 | 2194 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2195 | if (alloc1 == NULL) |
2196 | goto error_return; | |
2197 | external_relocs = alloc1; | |
2198 | } | |
2199 | ||
243ef1e0 | 2200 | if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, |
45d6a902 AM |
2201 | external_relocs, |
2202 | internal_relocs)) | |
2203 | goto error_return; | |
51992aec AM |
2204 | if (elf_section_data (o)->rel_hdr2 |
2205 | && (!elf_link_read_relocs_from_section | |
2206 | (abfd, o, | |
2207 | elf_section_data (o)->rel_hdr2, | |
2208 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
2209 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
2210 | * bed->s->int_rels_per_ext_rel)))) | |
45d6a902 AM |
2211 | goto error_return; |
2212 | ||
2213 | /* Cache the results for next time, if we can. */ | |
2214 | if (keep_memory) | |
2215 | elf_section_data (o)->relocs = internal_relocs; | |
2216 | ||
2217 | if (alloc1 != NULL) | |
2218 | free (alloc1); | |
2219 | ||
2220 | /* Don't free alloc2, since if it was allocated we are passing it | |
2221 | back (under the name of internal_relocs). */ | |
2222 | ||
2223 | return internal_relocs; | |
2224 | ||
2225 | error_return: | |
2226 | if (alloc1 != NULL) | |
2227 | free (alloc1); | |
2228 | if (alloc2 != NULL) | |
2229 | free (alloc2); | |
2230 | return NULL; | |
2231 | } | |
2232 | ||
2233 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2234 | section header for a section containing relocations for O. */ | |
2235 | ||
2236 | bfd_boolean | |
268b6b39 AM |
2237 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2238 | Elf_Internal_Shdr *rel_hdr, | |
2239 | asection *o) | |
45d6a902 AM |
2240 | { |
2241 | bfd_size_type reloc_count; | |
2242 | bfd_size_type num_rel_hashes; | |
2243 | ||
2244 | /* Figure out how many relocations there will be. */ | |
2245 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
2246 | reloc_count = elf_section_data (o)->rel_count; | |
2247 | else | |
2248 | reloc_count = elf_section_data (o)->rel_count2; | |
2249 | ||
2250 | num_rel_hashes = o->reloc_count; | |
2251 | if (num_rel_hashes < reloc_count) | |
2252 | num_rel_hashes = reloc_count; | |
2253 | ||
2254 | /* That allows us to calculate the size of the section. */ | |
2255 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
2256 | ||
2257 | /* The contents field must last into write_object_contents, so we | |
2258 | allocate it with bfd_alloc rather than malloc. Also since we | |
2259 | cannot be sure that the contents will actually be filled in, | |
2260 | we zero the allocated space. */ | |
268b6b39 | 2261 | rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2262 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2263 | return FALSE; | |
2264 | ||
2265 | /* We only allocate one set of hash entries, so we only do it the | |
2266 | first time we are called. */ | |
2267 | if (elf_section_data (o)->rel_hashes == NULL | |
2268 | && num_rel_hashes) | |
2269 | { | |
2270 | struct elf_link_hash_entry **p; | |
2271 | ||
268b6b39 | 2272 | p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
2273 | if (p == NULL) |
2274 | return FALSE; | |
2275 | ||
2276 | elf_section_data (o)->rel_hashes = p; | |
2277 | } | |
2278 | ||
2279 | return TRUE; | |
2280 | } | |
2281 | ||
2282 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2283 | originated from the section given by INPUT_REL_HDR) to the | |
2284 | OUTPUT_BFD. */ | |
2285 | ||
2286 | bfd_boolean | |
268b6b39 AM |
2287 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2288 | asection *input_section, | |
2289 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2290 | Elf_Internal_Rela *internal_relocs, |
2291 | struct elf_link_hash_entry **rel_hash | |
2292 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2293 | { |
2294 | Elf_Internal_Rela *irela; | |
2295 | Elf_Internal_Rela *irelaend; | |
2296 | bfd_byte *erel; | |
2297 | Elf_Internal_Shdr *output_rel_hdr; | |
2298 | asection *output_section; | |
2299 | unsigned int *rel_countp = NULL; | |
9c5bfbb7 | 2300 | const struct elf_backend_data *bed; |
268b6b39 | 2301 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
45d6a902 AM |
2302 | |
2303 | output_section = input_section->output_section; | |
2304 | output_rel_hdr = NULL; | |
2305 | ||
2306 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
2307 | == input_rel_hdr->sh_entsize) | |
2308 | { | |
2309 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
2310 | rel_countp = &elf_section_data (output_section)->rel_count; | |
2311 | } | |
2312 | else if (elf_section_data (output_section)->rel_hdr2 | |
2313 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
2314 | == input_rel_hdr->sh_entsize)) | |
2315 | { | |
2316 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2317 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2318 | } | |
2319 | else | |
2320 | { | |
2321 | (*_bfd_error_handler) | |
d003868e AM |
2322 | (_("%B: relocation size mismatch in %B section %A"), |
2323 | output_bfd, input_section->owner, input_section); | |
297d8443 | 2324 | bfd_set_error (bfd_error_wrong_format); |
45d6a902 AM |
2325 | return FALSE; |
2326 | } | |
2327 | ||
2328 | bed = get_elf_backend_data (output_bfd); | |
2329 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2330 | swap_out = bed->s->swap_reloc_out; | |
2331 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2332 | swap_out = bed->s->swap_reloca_out; | |
2333 | else | |
2334 | abort (); | |
2335 | ||
2336 | erel = output_rel_hdr->contents; | |
2337 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2338 | irela = internal_relocs; | |
2339 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2340 | * bed->s->int_rels_per_ext_rel); | |
2341 | while (irela < irelaend) | |
2342 | { | |
2343 | (*swap_out) (output_bfd, irela, erel); | |
2344 | irela += bed->s->int_rels_per_ext_rel; | |
2345 | erel += input_rel_hdr->sh_entsize; | |
2346 | } | |
2347 | ||
2348 | /* Bump the counter, so that we know where to add the next set of | |
2349 | relocations. */ | |
2350 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2351 | ||
2352 | return TRUE; | |
2353 | } | |
2354 | \f | |
508c3946 L |
2355 | /* Make weak undefined symbols in PIE dynamic. */ |
2356 | ||
2357 | bfd_boolean | |
2358 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2359 | struct elf_link_hash_entry *h) | |
2360 | { | |
2361 | if (info->pie | |
2362 | && h->dynindx == -1 | |
2363 | && h->root.type == bfd_link_hash_undefweak) | |
2364 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2365 | ||
2366 | return TRUE; | |
2367 | } | |
2368 | ||
45d6a902 AM |
2369 | /* Fix up the flags for a symbol. This handles various cases which |
2370 | can only be fixed after all the input files are seen. This is | |
2371 | currently called by both adjust_dynamic_symbol and | |
2372 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2373 | the face of future changes. */ | |
2374 | ||
2375 | bfd_boolean | |
268b6b39 AM |
2376 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2377 | struct elf_info_failed *eif) | |
45d6a902 | 2378 | { |
33774f08 | 2379 | const struct elf_backend_data *bed; |
508c3946 | 2380 | |
45d6a902 AM |
2381 | /* If this symbol was mentioned in a non-ELF file, try to set |
2382 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2383 | permit a non-ELF file to correctly refer to a symbol defined in | |
2384 | an ELF dynamic object. */ | |
f5385ebf | 2385 | if (h->non_elf) |
45d6a902 AM |
2386 | { |
2387 | while (h->root.type == bfd_link_hash_indirect) | |
2388 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2389 | ||
2390 | if (h->root.type != bfd_link_hash_defined | |
2391 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2392 | { |
2393 | h->ref_regular = 1; | |
2394 | h->ref_regular_nonweak = 1; | |
2395 | } | |
45d6a902 AM |
2396 | else |
2397 | { | |
2398 | if (h->root.u.def.section->owner != NULL | |
2399 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2400 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2401 | { |
2402 | h->ref_regular = 1; | |
2403 | h->ref_regular_nonweak = 1; | |
2404 | } | |
45d6a902 | 2405 | else |
f5385ebf | 2406 | h->def_regular = 1; |
45d6a902 AM |
2407 | } |
2408 | ||
2409 | if (h->dynindx == -1 | |
f5385ebf AM |
2410 | && (h->def_dynamic |
2411 | || h->ref_dynamic)) | |
45d6a902 | 2412 | { |
c152c796 | 2413 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2414 | { |
2415 | eif->failed = TRUE; | |
2416 | return FALSE; | |
2417 | } | |
2418 | } | |
2419 | } | |
2420 | else | |
2421 | { | |
f5385ebf | 2422 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2423 | was first seen in a non-ELF file. Fortunately, if the symbol |
2424 | was first seen in an ELF file, we're probably OK unless the | |
2425 | symbol was defined in a non-ELF file. Catch that case here. | |
2426 | FIXME: We're still in trouble if the symbol was first seen in | |
2427 | a dynamic object, and then later in a non-ELF regular object. */ | |
2428 | if ((h->root.type == bfd_link_hash_defined | |
2429 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2430 | && !h->def_regular |
45d6a902 AM |
2431 | && (h->root.u.def.section->owner != NULL |
2432 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2433 | != bfd_target_elf_flavour) | |
2434 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2435 | && !h->def_dynamic))) |
2436 | h->def_regular = 1; | |
45d6a902 AM |
2437 | } |
2438 | ||
508c3946 | 2439 | /* Backend specific symbol fixup. */ |
33774f08 AM |
2440 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2441 | if (bed->elf_backend_fixup_symbol | |
2442 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2443 | return FALSE; | |
508c3946 | 2444 | |
45d6a902 AM |
2445 | /* If this is a final link, and the symbol was defined as a common |
2446 | symbol in a regular object file, and there was no definition in | |
2447 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2448 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2449 | flag will not have been set. */ |
2450 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2451 | && !h->def_regular |
2452 | && h->ref_regular | |
2453 | && !h->def_dynamic | |
45d6a902 | 2454 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) |
f5385ebf | 2455 | h->def_regular = 1; |
45d6a902 AM |
2456 | |
2457 | /* If -Bsymbolic was used (which means to bind references to global | |
2458 | symbols to the definition within the shared object), and this | |
2459 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2460 | need a PLT entry. Likewise, if the symbol has non-default |
2461 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2462 | will force it local. */ |
f5385ebf | 2463 | if (h->needs_plt |
45d6a902 | 2464 | && eif->info->shared |
0eddce27 | 2465 | && is_elf_hash_table (eif->info->hash) |
55255dae | 2466 | && (SYMBOLIC_BIND (eif->info, h) |
c1be741f | 2467 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2468 | && h->def_regular) |
45d6a902 | 2469 | { |
45d6a902 AM |
2470 | bfd_boolean force_local; |
2471 | ||
45d6a902 AM |
2472 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2473 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2474 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2475 | } | |
2476 | ||
2477 | /* If a weak undefined symbol has non-default visibility, we also | |
2478 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2479 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 | 2480 | && h->root.type == bfd_link_hash_undefweak) |
33774f08 | 2481 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
45d6a902 AM |
2482 | |
2483 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2484 | the real definition in the dynamic object, copy interesting flags | |
2485 | over to the real definition. */ | |
f6e332e6 | 2486 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2487 | { |
2488 | struct elf_link_hash_entry *weakdef; | |
2489 | ||
f6e332e6 | 2490 | weakdef = h->u.weakdef; |
45d6a902 AM |
2491 | if (h->root.type == bfd_link_hash_indirect) |
2492 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2493 | ||
2494 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2495 | || h->root.type == bfd_link_hash_defweak); | |
f5385ebf | 2496 | BFD_ASSERT (weakdef->def_dynamic); |
45d6a902 AM |
2497 | |
2498 | /* If the real definition is defined by a regular object file, | |
2499 | don't do anything special. See the longer description in | |
2500 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
f5385ebf | 2501 | if (weakdef->def_regular) |
f6e332e6 | 2502 | h->u.weakdef = NULL; |
45d6a902 | 2503 | else |
a26587ba RS |
2504 | { |
2505 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2506 | || weakdef->root.type == bfd_link_hash_defweak); | |
2507 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h); | |
2508 | } | |
45d6a902 AM |
2509 | } |
2510 | ||
2511 | return TRUE; | |
2512 | } | |
2513 | ||
2514 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2515 | called via elf_link_hash_traverse, and also calls itself | |
2516 | recursively. */ | |
2517 | ||
2518 | bfd_boolean | |
268b6b39 | 2519 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2520 | { |
268b6b39 | 2521 | struct elf_info_failed *eif = data; |
45d6a902 | 2522 | bfd *dynobj; |
9c5bfbb7 | 2523 | const struct elf_backend_data *bed; |
45d6a902 | 2524 | |
0eddce27 | 2525 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2526 | return FALSE; |
2527 | ||
2528 | if (h->root.type == bfd_link_hash_warning) | |
2529 | { | |
a6aa5195 AM |
2530 | h->got = elf_hash_table (eif->info)->init_got_offset; |
2531 | h->plt = elf_hash_table (eif->info)->init_plt_offset; | |
45d6a902 AM |
2532 | |
2533 | /* When warning symbols are created, they **replace** the "real" | |
2534 | entry in the hash table, thus we never get to see the real | |
2535 | symbol in a hash traversal. So look at it now. */ | |
2536 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2537 | } | |
2538 | ||
2539 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2540 | if (h->root.type == bfd_link_hash_indirect) | |
2541 | return TRUE; | |
2542 | ||
2543 | /* Fix the symbol flags. */ | |
2544 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2545 | return FALSE; | |
2546 | ||
2547 | /* If this symbol does not require a PLT entry, and it is not | |
2548 | defined by a dynamic object, or is not referenced by a regular | |
2549 | object, ignore it. We do have to handle a weak defined symbol, | |
2550 | even if no regular object refers to it, if we decided to add it | |
2551 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2552 | about symbols which are defined by one dynamic object and | |
2553 | referenced by another one? */ | |
f5385ebf AM |
2554 | if (!h->needs_plt |
2555 | && (h->def_regular | |
2556 | || !h->def_dynamic | |
2557 | || (!h->ref_regular | |
f6e332e6 | 2558 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 | 2559 | { |
a6aa5195 | 2560 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2561 | return TRUE; |
2562 | } | |
2563 | ||
2564 | /* If we've already adjusted this symbol, don't do it again. This | |
2565 | can happen via a recursive call. */ | |
f5385ebf | 2566 | if (h->dynamic_adjusted) |
45d6a902 AM |
2567 | return TRUE; |
2568 | ||
2569 | /* Don't look at this symbol again. Note that we must set this | |
2570 | after checking the above conditions, because we may look at a | |
2571 | symbol once, decide not to do anything, and then get called | |
2572 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2573 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2574 | |
2575 | /* If this is a weak definition, and we know a real definition, and | |
2576 | the real symbol is not itself defined by a regular object file, | |
2577 | then get a good value for the real definition. We handle the | |
2578 | real symbol first, for the convenience of the backend routine. | |
2579 | ||
2580 | Note that there is a confusing case here. If the real definition | |
2581 | is defined by a regular object file, we don't get the real symbol | |
2582 | from the dynamic object, but we do get the weak symbol. If the | |
2583 | processor backend uses a COPY reloc, then if some routine in the | |
2584 | dynamic object changes the real symbol, we will not see that | |
2585 | change in the corresponding weak symbol. This is the way other | |
2586 | ELF linkers work as well, and seems to be a result of the shared | |
2587 | library model. | |
2588 | ||
2589 | I will clarify this issue. Most SVR4 shared libraries define the | |
2590 | variable _timezone and define timezone as a weak synonym. The | |
2591 | tzset call changes _timezone. If you write | |
2592 | extern int timezone; | |
2593 | int _timezone = 5; | |
2594 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2595 | you might expect that, since timezone is a synonym for _timezone, | |
2596 | the same number will print both times. However, if the processor | |
2597 | backend uses a COPY reloc, then actually timezone will be copied | |
2598 | into your process image, and, since you define _timezone | |
2599 | yourself, _timezone will not. Thus timezone and _timezone will | |
2600 | wind up at different memory locations. The tzset call will set | |
2601 | _timezone, leaving timezone unchanged. */ | |
2602 | ||
f6e332e6 | 2603 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2604 | { |
2605 | /* If we get to this point, we know there is an implicit | |
2606 | reference by a regular object file via the weak symbol H. | |
2607 | FIXME: Is this really true? What if the traversal finds | |
f6e332e6 AM |
2608 | H->U.WEAKDEF before it finds H? */ |
2609 | h->u.weakdef->ref_regular = 1; | |
45d6a902 | 2610 | |
f6e332e6 | 2611 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2612 | return FALSE; |
2613 | } | |
2614 | ||
2615 | /* If a symbol has no type and no size and does not require a PLT | |
2616 | entry, then we are probably about to do the wrong thing here: we | |
2617 | are probably going to create a COPY reloc for an empty object. | |
2618 | This case can arise when a shared object is built with assembly | |
2619 | code, and the assembly code fails to set the symbol type. */ | |
2620 | if (h->size == 0 | |
2621 | && h->type == STT_NOTYPE | |
f5385ebf | 2622 | && !h->needs_plt) |
45d6a902 AM |
2623 | (*_bfd_error_handler) |
2624 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2625 | h->root.root.string); | |
2626 | ||
2627 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2628 | bed = get_elf_backend_data (dynobj); | |
2629 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2630 | { | |
2631 | eif->failed = TRUE; | |
2632 | return FALSE; | |
2633 | } | |
2634 | ||
2635 | return TRUE; | |
2636 | } | |
2637 | ||
027297b7 L |
2638 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
2639 | DYNBSS. */ | |
2640 | ||
2641 | bfd_boolean | |
2642 | _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h, | |
2643 | asection *dynbss) | |
2644 | { | |
91ac5911 | 2645 | unsigned int power_of_two; |
027297b7 L |
2646 | bfd_vma mask; |
2647 | asection *sec = h->root.u.def.section; | |
2648 | ||
2649 | /* The section aligment of definition is the maximum alignment | |
91ac5911 L |
2650 | requirement of symbols defined in the section. Since we don't |
2651 | know the symbol alignment requirement, we start with the | |
2652 | maximum alignment and check low bits of the symbol address | |
2653 | for the minimum alignment. */ | |
2654 | power_of_two = bfd_get_section_alignment (sec->owner, sec); | |
2655 | mask = ((bfd_vma) 1 << power_of_two) - 1; | |
2656 | while ((h->root.u.def.value & mask) != 0) | |
2657 | { | |
2658 | mask >>= 1; | |
2659 | --power_of_two; | |
2660 | } | |
027297b7 | 2661 | |
91ac5911 L |
2662 | if (power_of_two > bfd_get_section_alignment (dynbss->owner, |
2663 | dynbss)) | |
027297b7 L |
2664 | { |
2665 | /* Adjust the section alignment if needed. */ | |
2666 | if (! bfd_set_section_alignment (dynbss->owner, dynbss, | |
91ac5911 | 2667 | power_of_two)) |
027297b7 L |
2668 | return FALSE; |
2669 | } | |
2670 | ||
91ac5911 | 2671 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
2672 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
2673 | ||
2674 | /* Define the symbol as being at this point in DYNBSS. */ | |
2675 | h->root.u.def.section = dynbss; | |
2676 | h->root.u.def.value = dynbss->size; | |
2677 | ||
2678 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
2679 | dynbss->size += h->size; | |
2680 | ||
2681 | return TRUE; | |
2682 | } | |
2683 | ||
45d6a902 AM |
2684 | /* Adjust all external symbols pointing into SEC_MERGE sections |
2685 | to reflect the object merging within the sections. */ | |
2686 | ||
2687 | bfd_boolean | |
268b6b39 | 2688 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2689 | { |
2690 | asection *sec; | |
2691 | ||
2692 | if (h->root.type == bfd_link_hash_warning) | |
2693 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2694 | ||
2695 | if ((h->root.type == bfd_link_hash_defined | |
2696 | || h->root.type == bfd_link_hash_defweak) | |
2697 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2698 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2699 | { | |
268b6b39 | 2700 | bfd *output_bfd = data; |
45d6a902 AM |
2701 | |
2702 | h->root.u.def.value = | |
2703 | _bfd_merged_section_offset (output_bfd, | |
2704 | &h->root.u.def.section, | |
2705 | elf_section_data (sec)->sec_info, | |
753731ee | 2706 | h->root.u.def.value); |
45d6a902 AM |
2707 | } |
2708 | ||
2709 | return TRUE; | |
2710 | } | |
986a241f RH |
2711 | |
2712 | /* Returns false if the symbol referred to by H should be considered | |
2713 | to resolve local to the current module, and true if it should be | |
2714 | considered to bind dynamically. */ | |
2715 | ||
2716 | bfd_boolean | |
268b6b39 AM |
2717 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2718 | struct bfd_link_info *info, | |
2719 | bfd_boolean ignore_protected) | |
986a241f RH |
2720 | { |
2721 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
2722 | const struct elf_backend_data *bed; |
2723 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
2724 | |
2725 | if (h == NULL) | |
2726 | return FALSE; | |
2727 | ||
2728 | while (h->root.type == bfd_link_hash_indirect | |
2729 | || h->root.type == bfd_link_hash_warning) | |
2730 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2731 | ||
2732 | /* If it was forced local, then clearly it's not dynamic. */ | |
2733 | if (h->dynindx == -1) | |
2734 | return FALSE; | |
f5385ebf | 2735 | if (h->forced_local) |
986a241f RH |
2736 | return FALSE; |
2737 | ||
2738 | /* Identify the cases where name binding rules say that a | |
2739 | visible symbol resolves locally. */ | |
55255dae | 2740 | binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h); |
986a241f RH |
2741 | |
2742 | switch (ELF_ST_VISIBILITY (h->other)) | |
2743 | { | |
2744 | case STV_INTERNAL: | |
2745 | case STV_HIDDEN: | |
2746 | return FALSE; | |
2747 | ||
2748 | case STV_PROTECTED: | |
fcb93ecf PB |
2749 | hash_table = elf_hash_table (info); |
2750 | if (!is_elf_hash_table (hash_table)) | |
2751 | return FALSE; | |
2752 | ||
2753 | bed = get_elf_backend_data (hash_table->dynobj); | |
2754 | ||
986a241f RH |
2755 | /* Proper resolution for function pointer equality may require |
2756 | that these symbols perhaps be resolved dynamically, even though | |
2757 | we should be resolving them to the current module. */ | |
fcb93ecf | 2758 | if (!ignore_protected || !bed->is_function_type (h->type)) |
986a241f RH |
2759 | binding_stays_local_p = TRUE; |
2760 | break; | |
2761 | ||
2762 | default: | |
986a241f RH |
2763 | break; |
2764 | } | |
2765 | ||
aa37626c | 2766 | /* If it isn't defined locally, then clearly it's dynamic. */ |
f5385ebf | 2767 | if (!h->def_regular) |
aa37626c L |
2768 | return TRUE; |
2769 | ||
986a241f RH |
2770 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2771 | us that it remains local. */ | |
2772 | return !binding_stays_local_p; | |
2773 | } | |
f6c52c13 AM |
2774 | |
2775 | /* Return true if the symbol referred to by H should be considered | |
2776 | to resolve local to the current module, and false otherwise. Differs | |
2777 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2778 | undefined symbols and weak symbols. */ | |
2779 | ||
2780 | bfd_boolean | |
268b6b39 AM |
2781 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2782 | struct bfd_link_info *info, | |
2783 | bfd_boolean local_protected) | |
f6c52c13 | 2784 | { |
fcb93ecf PB |
2785 | const struct elf_backend_data *bed; |
2786 | struct elf_link_hash_table *hash_table; | |
2787 | ||
f6c52c13 AM |
2788 | /* If it's a local sym, of course we resolve locally. */ |
2789 | if (h == NULL) | |
2790 | return TRUE; | |
2791 | ||
7e2294f9 AO |
2792 | /* Common symbols that become definitions don't get the DEF_REGULAR |
2793 | flag set, so test it first, and don't bail out. */ | |
2794 | if (ELF_COMMON_DEF_P (h)) | |
2795 | /* Do nothing. */; | |
f6c52c13 | 2796 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
2797 | resolve locally. The sym is either undefined or dynamic. */ |
2798 | else if (!h->def_regular) | |
f6c52c13 AM |
2799 | return FALSE; |
2800 | ||
2801 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 2802 | if (h->forced_local) |
f6c52c13 AM |
2803 | return TRUE; |
2804 | ||
2805 | /* As do non-dynamic symbols. */ | |
2806 | if (h->dynindx == -1) | |
2807 | return TRUE; | |
2808 | ||
2809 | /* At this point, we know the symbol is defined and dynamic. In an | |
2810 | executable it must resolve locally, likewise when building symbolic | |
2811 | shared libraries. */ | |
55255dae | 2812 | if (info->executable || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
2813 | return TRUE; |
2814 | ||
2815 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2816 | with default visibility might not resolve locally. */ | |
2817 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2818 | return FALSE; | |
2819 | ||
2820 | /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */ | |
2821 | if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED) | |
2822 | return TRUE; | |
2823 | ||
fcb93ecf PB |
2824 | hash_table = elf_hash_table (info); |
2825 | if (!is_elf_hash_table (hash_table)) | |
2826 | return TRUE; | |
2827 | ||
2828 | bed = get_elf_backend_data (hash_table->dynobj); | |
2829 | ||
1c16dfa5 | 2830 | /* STV_PROTECTED non-function symbols are local. */ |
fcb93ecf | 2831 | if (!bed->is_function_type (h->type)) |
1c16dfa5 L |
2832 | return TRUE; |
2833 | ||
f6c52c13 AM |
2834 | /* Function pointer equality tests may require that STV_PROTECTED |
2835 | symbols be treated as dynamic symbols, even when we know that the | |
2836 | dynamic linker will resolve them locally. */ | |
2837 | return local_protected; | |
2838 | } | |
e1918d23 AM |
2839 | |
2840 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2841 | aligned. Returns the first TLS output section. */ | |
2842 | ||
2843 | struct bfd_section * | |
2844 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2845 | { | |
2846 | struct bfd_section *sec, *tls; | |
2847 | unsigned int align = 0; | |
2848 | ||
2849 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2850 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2851 | break; | |
2852 | tls = sec; | |
2853 | ||
2854 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2855 | if (sec->alignment_power > align) | |
2856 | align = sec->alignment_power; | |
2857 | ||
2858 | elf_hash_table (info)->tls_sec = tls; | |
2859 | ||
2860 | /* Ensure the alignment of the first section is the largest alignment, | |
2861 | so that the tls segment starts aligned. */ | |
2862 | if (tls != NULL) | |
2863 | tls->alignment_power = align; | |
2864 | ||
2865 | return tls; | |
2866 | } | |
0ad989f9 L |
2867 | |
2868 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2869 | static bfd_boolean | |
2870 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2871 | Elf_Internal_Sym *sym) | |
2872 | { | |
a4d8e49b L |
2873 | const struct elf_backend_data *bed; |
2874 | ||
0ad989f9 L |
2875 | /* Local symbols do not count, but target specific ones might. */ |
2876 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2877 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2878 | return FALSE; | |
2879 | ||
fcb93ecf | 2880 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 2881 | /* Function symbols do not count. */ |
fcb93ecf | 2882 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
2883 | return FALSE; |
2884 | ||
2885 | /* If the section is undefined, then so is the symbol. */ | |
2886 | if (sym->st_shndx == SHN_UNDEF) | |
2887 | return FALSE; | |
2888 | ||
2889 | /* If the symbol is defined in the common section, then | |
2890 | it is a common definition and so does not count. */ | |
a4d8e49b | 2891 | if (bed->common_definition (sym)) |
0ad989f9 L |
2892 | return FALSE; |
2893 | ||
2894 | /* If the symbol is in a target specific section then we | |
2895 | must rely upon the backend to tell us what it is. */ | |
2896 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2897 | /* FIXME - this function is not coded yet: | |
2898 | ||
2899 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2900 | ||
2901 | Instead for now assume that the definition is not global, | |
2902 | Even if this is wrong, at least the linker will behave | |
2903 | in the same way that it used to do. */ | |
2904 | return FALSE; | |
2905 | ||
2906 | return TRUE; | |
2907 | } | |
2908 | ||
2909 | /* Search the symbol table of the archive element of the archive ABFD | |
2910 | whose archive map contains a mention of SYMDEF, and determine if | |
2911 | the symbol is defined in this element. */ | |
2912 | static bfd_boolean | |
2913 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2914 | { | |
2915 | Elf_Internal_Shdr * hdr; | |
2916 | bfd_size_type symcount; | |
2917 | bfd_size_type extsymcount; | |
2918 | bfd_size_type extsymoff; | |
2919 | Elf_Internal_Sym *isymbuf; | |
2920 | Elf_Internal_Sym *isym; | |
2921 | Elf_Internal_Sym *isymend; | |
2922 | bfd_boolean result; | |
2923 | ||
2924 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2925 | if (abfd == NULL) | |
2926 | return FALSE; | |
2927 | ||
2928 | if (! bfd_check_format (abfd, bfd_object)) | |
2929 | return FALSE; | |
2930 | ||
2931 | /* If we have already included the element containing this symbol in the | |
2932 | link then we do not need to include it again. Just claim that any symbol | |
2933 | it contains is not a definition, so that our caller will not decide to | |
2934 | (re)include this element. */ | |
2935 | if (abfd->archive_pass) | |
2936 | return FALSE; | |
2937 | ||
2938 | /* Select the appropriate symbol table. */ | |
2939 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2940 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2941 | else | |
2942 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2943 | ||
2944 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2945 | ||
2946 | /* The sh_info field of the symtab header tells us where the | |
2947 | external symbols start. We don't care about the local symbols. */ | |
2948 | if (elf_bad_symtab (abfd)) | |
2949 | { | |
2950 | extsymcount = symcount; | |
2951 | extsymoff = 0; | |
2952 | } | |
2953 | else | |
2954 | { | |
2955 | extsymcount = symcount - hdr->sh_info; | |
2956 | extsymoff = hdr->sh_info; | |
2957 | } | |
2958 | ||
2959 | if (extsymcount == 0) | |
2960 | return FALSE; | |
2961 | ||
2962 | /* Read in the symbol table. */ | |
2963 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2964 | NULL, NULL, NULL); | |
2965 | if (isymbuf == NULL) | |
2966 | return FALSE; | |
2967 | ||
2968 | /* Scan the symbol table looking for SYMDEF. */ | |
2969 | result = FALSE; | |
2970 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2971 | { | |
2972 | const char *name; | |
2973 | ||
2974 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
2975 | isym->st_name); | |
2976 | if (name == NULL) | |
2977 | break; | |
2978 | ||
2979 | if (strcmp (name, symdef->name) == 0) | |
2980 | { | |
2981 | result = is_global_data_symbol_definition (abfd, isym); | |
2982 | break; | |
2983 | } | |
2984 | } | |
2985 | ||
2986 | free (isymbuf); | |
2987 | ||
2988 | return result; | |
2989 | } | |
2990 | \f | |
5a580b3a AM |
2991 | /* Add an entry to the .dynamic table. */ |
2992 | ||
2993 | bfd_boolean | |
2994 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
2995 | bfd_vma tag, | |
2996 | bfd_vma val) | |
2997 | { | |
2998 | struct elf_link_hash_table *hash_table; | |
2999 | const struct elf_backend_data *bed; | |
3000 | asection *s; | |
3001 | bfd_size_type newsize; | |
3002 | bfd_byte *newcontents; | |
3003 | Elf_Internal_Dyn dyn; | |
3004 | ||
3005 | hash_table = elf_hash_table (info); | |
3006 | if (! is_elf_hash_table (hash_table)) | |
3007 | return FALSE; | |
3008 | ||
3009 | bed = get_elf_backend_data (hash_table->dynobj); | |
3010 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
3011 | BFD_ASSERT (s != NULL); | |
3012 | ||
eea6121a | 3013 | newsize = s->size + bed->s->sizeof_dyn; |
5a580b3a AM |
3014 | newcontents = bfd_realloc (s->contents, newsize); |
3015 | if (newcontents == NULL) | |
3016 | return FALSE; | |
3017 | ||
3018 | dyn.d_tag = tag; | |
3019 | dyn.d_un.d_val = val; | |
eea6121a | 3020 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3021 | |
eea6121a | 3022 | s->size = newsize; |
5a580b3a AM |
3023 | s->contents = newcontents; |
3024 | ||
3025 | return TRUE; | |
3026 | } | |
3027 | ||
3028 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
3029 | otherwise just check whether one already exists. Returns -1 on error, | |
3030 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
3031 | ||
4ad4eba5 | 3032 | static int |
7e9f0867 AM |
3033 | elf_add_dt_needed_tag (bfd *abfd, |
3034 | struct bfd_link_info *info, | |
4ad4eba5 AM |
3035 | const char *soname, |
3036 | bfd_boolean do_it) | |
5a580b3a AM |
3037 | { |
3038 | struct elf_link_hash_table *hash_table; | |
3039 | bfd_size_type oldsize; | |
3040 | bfd_size_type strindex; | |
3041 | ||
7e9f0867 AM |
3042 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3043 | return -1; | |
3044 | ||
5a580b3a AM |
3045 | hash_table = elf_hash_table (info); |
3046 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
3047 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
3048 | if (strindex == (bfd_size_type) -1) | |
3049 | return -1; | |
3050 | ||
3051 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
3052 | { | |
3053 | asection *sdyn; | |
3054 | const struct elf_backend_data *bed; | |
3055 | bfd_byte *extdyn; | |
3056 | ||
3057 | bed = get_elf_backend_data (hash_table->dynobj); | |
3058 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
7e9f0867 AM |
3059 | if (sdyn != NULL) |
3060 | for (extdyn = sdyn->contents; | |
3061 | extdyn < sdyn->contents + sdyn->size; | |
3062 | extdyn += bed->s->sizeof_dyn) | |
3063 | { | |
3064 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3065 | |
7e9f0867 AM |
3066 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3067 | if (dyn.d_tag == DT_NEEDED | |
3068 | && dyn.d_un.d_val == strindex) | |
3069 | { | |
3070 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3071 | return 1; | |
3072 | } | |
3073 | } | |
5a580b3a AM |
3074 | } |
3075 | ||
3076 | if (do_it) | |
3077 | { | |
7e9f0867 AM |
3078 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3079 | return -1; | |
3080 | ||
5a580b3a AM |
3081 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3082 | return -1; | |
3083 | } | |
3084 | else | |
3085 | /* We were just checking for existence of the tag. */ | |
3086 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3087 | ||
3088 | return 0; | |
3089 | } | |
3090 | ||
3091 | /* Sort symbol by value and section. */ | |
4ad4eba5 AM |
3092 | static int |
3093 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3094 | { |
3095 | const struct elf_link_hash_entry *h1; | |
3096 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3097 | bfd_signed_vma vdiff; |
5a580b3a AM |
3098 | |
3099 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3100 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3101 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3102 | if (vdiff != 0) | |
3103 | return vdiff > 0 ? 1 : -1; | |
3104 | else | |
3105 | { | |
3106 | long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
3107 | if (sdiff != 0) | |
3108 | return sdiff > 0 ? 1 : -1; | |
3109 | } | |
5a580b3a AM |
3110 | return 0; |
3111 | } | |
4ad4eba5 | 3112 | |
5a580b3a AM |
3113 | /* This function is used to adjust offsets into .dynstr for |
3114 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3115 | ||
3116 | static bfd_boolean | |
3117 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3118 | { | |
3119 | struct elf_strtab_hash *dynstr = data; | |
3120 | ||
3121 | if (h->root.type == bfd_link_hash_warning) | |
3122 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3123 | ||
3124 | if (h->dynindx != -1) | |
3125 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3126 | return TRUE; | |
3127 | } | |
3128 | ||
3129 | /* Assign string offsets in .dynstr, update all structures referencing | |
3130 | them. */ | |
3131 | ||
4ad4eba5 AM |
3132 | static bfd_boolean |
3133 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3134 | { |
3135 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3136 | struct elf_link_local_dynamic_entry *entry; | |
3137 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3138 | bfd *dynobj = hash_table->dynobj; | |
3139 | asection *sdyn; | |
3140 | bfd_size_type size; | |
3141 | const struct elf_backend_data *bed; | |
3142 | bfd_byte *extdyn; | |
3143 | ||
3144 | _bfd_elf_strtab_finalize (dynstr); | |
3145 | size = _bfd_elf_strtab_size (dynstr); | |
3146 | ||
3147 | bed = get_elf_backend_data (dynobj); | |
3148 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
3149 | BFD_ASSERT (sdyn != NULL); | |
3150 | ||
3151 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3152 | for (extdyn = sdyn->contents; | |
eea6121a | 3153 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3154 | extdyn += bed->s->sizeof_dyn) |
3155 | { | |
3156 | Elf_Internal_Dyn dyn; | |
3157 | ||
3158 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3159 | switch (dyn.d_tag) | |
3160 | { | |
3161 | case DT_STRSZ: | |
3162 | dyn.d_un.d_val = size; | |
3163 | break; | |
3164 | case DT_NEEDED: | |
3165 | case DT_SONAME: | |
3166 | case DT_RPATH: | |
3167 | case DT_RUNPATH: | |
3168 | case DT_FILTER: | |
3169 | case DT_AUXILIARY: | |
3170 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
3171 | break; | |
3172 | default: | |
3173 | continue; | |
3174 | } | |
3175 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3176 | } | |
3177 | ||
3178 | /* Now update local dynamic symbols. */ | |
3179 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3180 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3181 | entry->isym.st_name); | |
3182 | ||
3183 | /* And the rest of dynamic symbols. */ | |
3184 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3185 | ||
3186 | /* Adjust version definitions. */ | |
3187 | if (elf_tdata (output_bfd)->cverdefs) | |
3188 | { | |
3189 | asection *s; | |
3190 | bfd_byte *p; | |
3191 | bfd_size_type i; | |
3192 | Elf_Internal_Verdef def; | |
3193 | Elf_Internal_Verdaux defaux; | |
3194 | ||
3195 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
3196 | p = s->contents; | |
3197 | do | |
3198 | { | |
3199 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3200 | &def); | |
3201 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3202 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3203 | continue; | |
5a580b3a AM |
3204 | for (i = 0; i < def.vd_cnt; ++i) |
3205 | { | |
3206 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3207 | (Elf_External_Verdaux *) p, &defaux); | |
3208 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3209 | defaux.vda_name); | |
3210 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3211 | &defaux, (Elf_External_Verdaux *) p); | |
3212 | p += sizeof (Elf_External_Verdaux); | |
3213 | } | |
3214 | } | |
3215 | while (def.vd_next); | |
3216 | } | |
3217 | ||
3218 | /* Adjust version references. */ | |
3219 | if (elf_tdata (output_bfd)->verref) | |
3220 | { | |
3221 | asection *s; | |
3222 | bfd_byte *p; | |
3223 | bfd_size_type i; | |
3224 | Elf_Internal_Verneed need; | |
3225 | Elf_Internal_Vernaux needaux; | |
3226 | ||
3227 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
3228 | p = s->contents; | |
3229 | do | |
3230 | { | |
3231 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3232 | &need); | |
3233 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3234 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3235 | (Elf_External_Verneed *) p); | |
3236 | p += sizeof (Elf_External_Verneed); | |
3237 | for (i = 0; i < need.vn_cnt; ++i) | |
3238 | { | |
3239 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3240 | (Elf_External_Vernaux *) p, &needaux); | |
3241 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3242 | needaux.vna_name); | |
3243 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3244 | &needaux, | |
3245 | (Elf_External_Vernaux *) p); | |
3246 | p += sizeof (Elf_External_Vernaux); | |
3247 | } | |
3248 | } | |
3249 | while (need.vn_next); | |
3250 | } | |
3251 | ||
3252 | return TRUE; | |
3253 | } | |
3254 | \f | |
13285a1b AM |
3255 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
3256 | The default is to only match when the INPUT and OUTPUT are exactly | |
3257 | the same target. */ | |
3258 | ||
3259 | bfd_boolean | |
3260 | _bfd_elf_default_relocs_compatible (const bfd_target *input, | |
3261 | const bfd_target *output) | |
3262 | { | |
3263 | return input == output; | |
3264 | } | |
3265 | ||
3266 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
3267 | This version is used when different targets for the same architecture | |
3268 | are virtually identical. */ | |
3269 | ||
3270 | bfd_boolean | |
3271 | _bfd_elf_relocs_compatible (const bfd_target *input, | |
3272 | const bfd_target *output) | |
3273 | { | |
3274 | const struct elf_backend_data *obed, *ibed; | |
3275 | ||
3276 | if (input == output) | |
3277 | return TRUE; | |
3278 | ||
3279 | ibed = xvec_get_elf_backend_data (input); | |
3280 | obed = xvec_get_elf_backend_data (output); | |
3281 | ||
3282 | if (ibed->arch != obed->arch) | |
3283 | return FALSE; | |
3284 | ||
3285 | /* If both backends are using this function, deem them compatible. */ | |
3286 | return ibed->relocs_compatible == obed->relocs_compatible; | |
3287 | } | |
3288 | ||
4ad4eba5 AM |
3289 | /* Add symbols from an ELF object file to the linker hash table. */ |
3290 | ||
3291 | static bfd_boolean | |
3292 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3293 | { | |
4ad4eba5 AM |
3294 | Elf_Internal_Shdr *hdr; |
3295 | bfd_size_type symcount; | |
3296 | bfd_size_type extsymcount; | |
3297 | bfd_size_type extsymoff; | |
3298 | struct elf_link_hash_entry **sym_hash; | |
3299 | bfd_boolean dynamic; | |
3300 | Elf_External_Versym *extversym = NULL; | |
3301 | Elf_External_Versym *ever; | |
3302 | struct elf_link_hash_entry *weaks; | |
3303 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
3304 | bfd_size_type nondeflt_vers_cnt = 0; | |
3305 | Elf_Internal_Sym *isymbuf = NULL; | |
3306 | Elf_Internal_Sym *isym; | |
3307 | Elf_Internal_Sym *isymend; | |
3308 | const struct elf_backend_data *bed; | |
3309 | bfd_boolean add_needed; | |
66eb6687 | 3310 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3311 | bfd_size_type amt; |
66eb6687 | 3312 | void *alloc_mark = NULL; |
4f87808c AM |
3313 | struct bfd_hash_entry **old_table = NULL; |
3314 | unsigned int old_size = 0; | |
3315 | unsigned int old_count = 0; | |
66eb6687 AM |
3316 | void *old_tab = NULL; |
3317 | void *old_hash; | |
3318 | void *old_ent; | |
3319 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3320 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
3321 | long old_dynsymcount = 0; | |
3322 | size_t tabsize = 0; | |
3323 | size_t hashsize = 0; | |
4ad4eba5 | 3324 | |
66eb6687 | 3325 | htab = elf_hash_table (info); |
4ad4eba5 | 3326 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3327 | |
3328 | if ((abfd->flags & DYNAMIC) == 0) | |
3329 | dynamic = FALSE; | |
3330 | else | |
3331 | { | |
3332 | dynamic = TRUE; | |
3333 | ||
3334 | /* You can't use -r against a dynamic object. Also, there's no | |
3335 | hope of using a dynamic object which does not exactly match | |
3336 | the format of the output file. */ | |
3337 | if (info->relocatable | |
66eb6687 | 3338 | || !is_elf_hash_table (htab) |
f13a99db | 3339 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 3340 | { |
9a0789ec NC |
3341 | if (info->relocatable) |
3342 | bfd_set_error (bfd_error_invalid_operation); | |
3343 | else | |
3344 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3345 | goto error_return; |
3346 | } | |
3347 | } | |
3348 | ||
3349 | /* As a GNU extension, any input sections which are named | |
3350 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3351 | symbol. This differs from .gnu.warning sections, which generate | |
3352 | warnings when they are included in an output file. */ | |
3353 | if (info->executable) | |
3354 | { | |
3355 | asection *s; | |
3356 | ||
3357 | for (s = abfd->sections; s != NULL; s = s->next) | |
3358 | { | |
3359 | const char *name; | |
3360 | ||
3361 | name = bfd_get_section_name (abfd, s); | |
0112cd26 | 3362 | if (CONST_STRNEQ (name, ".gnu.warning.")) |
4ad4eba5 AM |
3363 | { |
3364 | char *msg; | |
3365 | bfd_size_type sz; | |
4ad4eba5 AM |
3366 | |
3367 | name += sizeof ".gnu.warning." - 1; | |
3368 | ||
3369 | /* If this is a shared object, then look up the symbol | |
3370 | in the hash table. If it is there, and it is already | |
3371 | been defined, then we will not be using the entry | |
3372 | from this shared object, so we don't need to warn. | |
3373 | FIXME: If we see the definition in a regular object | |
3374 | later on, we will warn, but we shouldn't. The only | |
3375 | fix is to keep track of what warnings we are supposed | |
3376 | to emit, and then handle them all at the end of the | |
3377 | link. */ | |
3378 | if (dynamic) | |
3379 | { | |
3380 | struct elf_link_hash_entry *h; | |
3381 | ||
66eb6687 | 3382 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
4ad4eba5 AM |
3383 | |
3384 | /* FIXME: What about bfd_link_hash_common? */ | |
3385 | if (h != NULL | |
3386 | && (h->root.type == bfd_link_hash_defined | |
3387 | || h->root.type == bfd_link_hash_defweak)) | |
3388 | { | |
3389 | /* We don't want to issue this warning. Clobber | |
3390 | the section size so that the warning does not | |
3391 | get copied into the output file. */ | |
eea6121a | 3392 | s->size = 0; |
4ad4eba5 AM |
3393 | continue; |
3394 | } | |
3395 | } | |
3396 | ||
eea6121a | 3397 | sz = s->size; |
370a0e1b | 3398 | msg = bfd_alloc (abfd, sz + 1); |
4ad4eba5 AM |
3399 | if (msg == NULL) |
3400 | goto error_return; | |
3401 | ||
370a0e1b | 3402 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
4ad4eba5 AM |
3403 | goto error_return; |
3404 | ||
370a0e1b | 3405 | msg[sz] = '\0'; |
4ad4eba5 AM |
3406 | |
3407 | if (! (_bfd_generic_link_add_one_symbol | |
3408 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
66eb6687 | 3409 | FALSE, bed->collect, NULL))) |
4ad4eba5 AM |
3410 | goto error_return; |
3411 | ||
3412 | if (! info->relocatable) | |
3413 | { | |
3414 | /* Clobber the section size so that the warning does | |
3415 | not get copied into the output file. */ | |
eea6121a | 3416 | s->size = 0; |
11d2f718 AM |
3417 | |
3418 | /* Also set SEC_EXCLUDE, so that symbols defined in | |
3419 | the warning section don't get copied to the output. */ | |
3420 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3421 | } |
3422 | } | |
3423 | } | |
3424 | } | |
3425 | ||
3426 | add_needed = TRUE; | |
3427 | if (! dynamic) | |
3428 | { | |
3429 | /* If we are creating a shared library, create all the dynamic | |
3430 | sections immediately. We need to attach them to something, | |
3431 | so we attach them to this BFD, provided it is the right | |
3432 | format. FIXME: If there are no input BFD's of the same | |
3433 | format as the output, we can't make a shared library. */ | |
3434 | if (info->shared | |
66eb6687 | 3435 | && is_elf_hash_table (htab) |
f13a99db | 3436 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 3437 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
3438 | { |
3439 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3440 | goto error_return; | |
3441 | } | |
3442 | } | |
66eb6687 | 3443 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
3444 | goto error_return; |
3445 | else | |
3446 | { | |
3447 | asection *s; | |
3448 | const char *soname = NULL; | |
3449 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; | |
3450 | int ret; | |
3451 | ||
3452 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3453 | ld shouldn't allow it. */ |
4ad4eba5 AM |
3454 | if ((s = abfd->sections) != NULL |
3455 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
92fd189d | 3456 | abort (); |
4ad4eba5 AM |
3457 | |
3458 | /* If this dynamic lib was specified on the command line with | |
3459 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3460 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3461 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3462 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3463 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3464 | all. */ | |
3465 | add_needed = (elf_dyn_lib_class (abfd) | |
3466 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3467 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3468 | |
3469 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3470 | if (s != NULL) | |
3471 | { | |
3472 | bfd_byte *dynbuf; | |
3473 | bfd_byte *extdyn; | |
3474 | int elfsec; | |
3475 | unsigned long shlink; | |
3476 | ||
eea6121a | 3477 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
4ad4eba5 AM |
3478 | goto error_free_dyn; |
3479 | ||
3480 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
3481 | if (elfsec == -1) | |
3482 | goto error_free_dyn; | |
3483 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3484 | ||
3485 | for (extdyn = dynbuf; | |
eea6121a | 3486 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3487 | extdyn += bed->s->sizeof_dyn) |
3488 | { | |
3489 | Elf_Internal_Dyn dyn; | |
3490 | ||
3491 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3492 | if (dyn.d_tag == DT_SONAME) | |
3493 | { | |
3494 | unsigned int tagv = dyn.d_un.d_val; | |
3495 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3496 | if (soname == NULL) | |
3497 | goto error_free_dyn; | |
3498 | } | |
3499 | if (dyn.d_tag == DT_NEEDED) | |
3500 | { | |
3501 | struct bfd_link_needed_list *n, **pn; | |
3502 | char *fnm, *anm; | |
3503 | unsigned int tagv = dyn.d_un.d_val; | |
3504 | ||
3505 | amt = sizeof (struct bfd_link_needed_list); | |
3506 | n = bfd_alloc (abfd, amt); | |
3507 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3508 | if (n == NULL || fnm == NULL) | |
3509 | goto error_free_dyn; | |
3510 | amt = strlen (fnm) + 1; | |
3511 | anm = bfd_alloc (abfd, amt); | |
3512 | if (anm == NULL) | |
3513 | goto error_free_dyn; | |
3514 | memcpy (anm, fnm, amt); | |
3515 | n->name = anm; | |
3516 | n->by = abfd; | |
3517 | n->next = NULL; | |
66eb6687 | 3518 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3519 | ; |
3520 | *pn = n; | |
3521 | } | |
3522 | if (dyn.d_tag == DT_RUNPATH) | |
3523 | { | |
3524 | struct bfd_link_needed_list *n, **pn; | |
3525 | char *fnm, *anm; | |
3526 | unsigned int tagv = dyn.d_un.d_val; | |
3527 | ||
3528 | amt = sizeof (struct bfd_link_needed_list); | |
3529 | n = bfd_alloc (abfd, amt); | |
3530 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3531 | if (n == NULL || fnm == NULL) | |
3532 | goto error_free_dyn; | |
3533 | amt = strlen (fnm) + 1; | |
3534 | anm = bfd_alloc (abfd, amt); | |
3535 | if (anm == NULL) | |
3536 | goto error_free_dyn; | |
3537 | memcpy (anm, fnm, amt); | |
3538 | n->name = anm; | |
3539 | n->by = abfd; | |
3540 | n->next = NULL; | |
3541 | for (pn = & runpath; | |
3542 | *pn != NULL; | |
3543 | pn = &(*pn)->next) | |
3544 | ; | |
3545 | *pn = n; | |
3546 | } | |
3547 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3548 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3549 | { | |
3550 | struct bfd_link_needed_list *n, **pn; | |
3551 | char *fnm, *anm; | |
3552 | unsigned int tagv = dyn.d_un.d_val; | |
3553 | ||
3554 | amt = sizeof (struct bfd_link_needed_list); | |
3555 | n = bfd_alloc (abfd, amt); | |
3556 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3557 | if (n == NULL || fnm == NULL) | |
3558 | goto error_free_dyn; | |
3559 | amt = strlen (fnm) + 1; | |
3560 | anm = bfd_alloc (abfd, amt); | |
3561 | if (anm == NULL) | |
3562 | { | |
3563 | error_free_dyn: | |
3564 | free (dynbuf); | |
3565 | goto error_return; | |
3566 | } | |
3567 | memcpy (anm, fnm, amt); | |
3568 | n->name = anm; | |
3569 | n->by = abfd; | |
3570 | n->next = NULL; | |
3571 | for (pn = & rpath; | |
3572 | *pn != NULL; | |
3573 | pn = &(*pn)->next) | |
3574 | ; | |
3575 | *pn = n; | |
3576 | } | |
3577 | } | |
3578 | ||
3579 | free (dynbuf); | |
3580 | } | |
3581 | ||
3582 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3583 | frees all more recently bfd_alloc'd blocks as well. */ | |
3584 | if (runpath) | |
3585 | rpath = runpath; | |
3586 | ||
3587 | if (rpath) | |
3588 | { | |
3589 | struct bfd_link_needed_list **pn; | |
66eb6687 | 3590 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3591 | ; |
3592 | *pn = rpath; | |
3593 | } | |
3594 | ||
3595 | /* We do not want to include any of the sections in a dynamic | |
3596 | object in the output file. We hack by simply clobbering the | |
3597 | list of sections in the BFD. This could be handled more | |
3598 | cleanly by, say, a new section flag; the existing | |
3599 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3600 | still implies that the section takes up space in the output | |
3601 | file. */ | |
3602 | bfd_section_list_clear (abfd); | |
3603 | ||
4ad4eba5 AM |
3604 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3605 | object. If the object has a DT_SONAME entry, we use it. | |
3606 | Otherwise, if the generic linker stuck something in | |
3607 | elf_dt_name, we use that. Otherwise, we just use the file | |
3608 | name. */ | |
3609 | if (soname == NULL || *soname == '\0') | |
3610 | { | |
3611 | soname = elf_dt_name (abfd); | |
3612 | if (soname == NULL || *soname == '\0') | |
3613 | soname = bfd_get_filename (abfd); | |
3614 | } | |
3615 | ||
3616 | /* Save the SONAME because sometimes the linker emulation code | |
3617 | will need to know it. */ | |
3618 | elf_dt_name (abfd) = soname; | |
3619 | ||
7e9f0867 | 3620 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3621 | if (ret < 0) |
3622 | goto error_return; | |
3623 | ||
3624 | /* If we have already included this dynamic object in the | |
3625 | link, just ignore it. There is no reason to include a | |
3626 | particular dynamic object more than once. */ | |
3627 | if (ret > 0) | |
3628 | return TRUE; | |
3629 | } | |
3630 | ||
3631 | /* If this is a dynamic object, we always link against the .dynsym | |
3632 | symbol table, not the .symtab symbol table. The dynamic linker | |
3633 | will only see the .dynsym symbol table, so there is no reason to | |
3634 | look at .symtab for a dynamic object. */ | |
3635 | ||
3636 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3637 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3638 | else | |
3639 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3640 | ||
3641 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3642 | ||
3643 | /* The sh_info field of the symtab header tells us where the | |
3644 | external symbols start. We don't care about the local symbols at | |
3645 | this point. */ | |
3646 | if (elf_bad_symtab (abfd)) | |
3647 | { | |
3648 | extsymcount = symcount; | |
3649 | extsymoff = 0; | |
3650 | } | |
3651 | else | |
3652 | { | |
3653 | extsymcount = symcount - hdr->sh_info; | |
3654 | extsymoff = hdr->sh_info; | |
3655 | } | |
3656 | ||
3657 | sym_hash = NULL; | |
3658 | if (extsymcount != 0) | |
3659 | { | |
3660 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3661 | NULL, NULL, NULL); | |
3662 | if (isymbuf == NULL) | |
3663 | goto error_return; | |
3664 | ||
3665 | /* We store a pointer to the hash table entry for each external | |
3666 | symbol. */ | |
3667 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3668 | sym_hash = bfd_alloc (abfd, amt); | |
3669 | if (sym_hash == NULL) | |
3670 | goto error_free_sym; | |
3671 | elf_sym_hashes (abfd) = sym_hash; | |
3672 | } | |
3673 | ||
3674 | if (dynamic) | |
3675 | { | |
3676 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
3677 | if (!_bfd_elf_slurp_version_tables (abfd, |
3678 | info->default_imported_symver)) | |
4ad4eba5 AM |
3679 | goto error_free_sym; |
3680 | ||
3681 | /* Read in the symbol versions, but don't bother to convert them | |
3682 | to internal format. */ | |
3683 | if (elf_dynversym (abfd) != 0) | |
3684 | { | |
3685 | Elf_Internal_Shdr *versymhdr; | |
3686 | ||
3687 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
3688 | extversym = bfd_malloc (versymhdr->sh_size); | |
3689 | if (extversym == NULL) | |
3690 | goto error_free_sym; | |
3691 | amt = versymhdr->sh_size; | |
3692 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3693 | || bfd_bread (extversym, amt, abfd) != amt) | |
3694 | goto error_free_vers; | |
3695 | } | |
3696 | } | |
3697 | ||
66eb6687 AM |
3698 | /* If we are loading an as-needed shared lib, save the symbol table |
3699 | state before we start adding symbols. If the lib turns out | |
3700 | to be unneeded, restore the state. */ | |
3701 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
3702 | { | |
3703 | unsigned int i; | |
3704 | size_t entsize; | |
3705 | ||
3706 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
3707 | { | |
3708 | struct bfd_hash_entry *p; | |
2de92251 | 3709 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3710 | |
3711 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
3712 | { |
3713 | h = (struct elf_link_hash_entry *) p; | |
3714 | entsize += htab->root.table.entsize; | |
3715 | if (h->root.type == bfd_link_hash_warning) | |
3716 | entsize += htab->root.table.entsize; | |
3717 | } | |
66eb6687 AM |
3718 | } |
3719 | ||
3720 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
3721 | hashsize = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3722 | old_tab = bfd_malloc (tabsize + entsize + hashsize); | |
3723 | if (old_tab == NULL) | |
3724 | goto error_free_vers; | |
3725 | ||
3726 | /* Remember the current objalloc pointer, so that all mem for | |
3727 | symbols added can later be reclaimed. */ | |
3728 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
3729 | if (alloc_mark == NULL) | |
3730 | goto error_free_vers; | |
3731 | ||
5061a885 AM |
3732 | /* Make a special call to the linker "notice" function to |
3733 | tell it that we are about to handle an as-needed lib. */ | |
3734 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, | |
3735 | notice_as_needed)) | |
9af2a943 | 3736 | goto error_free_vers; |
5061a885 | 3737 | |
66eb6687 AM |
3738 | /* Clone the symbol table and sym hashes. Remember some |
3739 | pointers into the symbol table, and dynamic symbol count. */ | |
3740 | old_hash = (char *) old_tab + tabsize; | |
3741 | old_ent = (char *) old_hash + hashsize; | |
3742 | memcpy (old_tab, htab->root.table.table, tabsize); | |
3743 | memcpy (old_hash, sym_hash, hashsize); | |
3744 | old_undefs = htab->root.undefs; | |
3745 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
3746 | old_table = htab->root.table.table; |
3747 | old_size = htab->root.table.size; | |
3748 | old_count = htab->root.table.count; | |
66eb6687 AM |
3749 | old_dynsymcount = htab->dynsymcount; |
3750 | ||
3751 | for (i = 0; i < htab->root.table.size; i++) | |
3752 | { | |
3753 | struct bfd_hash_entry *p; | |
2de92251 | 3754 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3755 | |
3756 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
3757 | { | |
3758 | memcpy (old_ent, p, htab->root.table.entsize); | |
3759 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
3760 | h = (struct elf_link_hash_entry *) p; |
3761 | if (h->root.type == bfd_link_hash_warning) | |
3762 | { | |
3763 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
3764 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
3765 | } | |
66eb6687 AM |
3766 | } |
3767 | } | |
3768 | } | |
4ad4eba5 | 3769 | |
66eb6687 | 3770 | weaks = NULL; |
4ad4eba5 AM |
3771 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
3772 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3773 | isym < isymend; | |
3774 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3775 | { | |
3776 | int bind; | |
3777 | bfd_vma value; | |
af44c138 | 3778 | asection *sec, *new_sec; |
4ad4eba5 AM |
3779 | flagword flags; |
3780 | const char *name; | |
3781 | struct elf_link_hash_entry *h; | |
3782 | bfd_boolean definition; | |
3783 | bfd_boolean size_change_ok; | |
3784 | bfd_boolean type_change_ok; | |
3785 | bfd_boolean new_weakdef; | |
3786 | bfd_boolean override; | |
a4d8e49b | 3787 | bfd_boolean common; |
4ad4eba5 AM |
3788 | unsigned int old_alignment; |
3789 | bfd *old_bfd; | |
3790 | ||
3791 | override = FALSE; | |
3792 | ||
3793 | flags = BSF_NO_FLAGS; | |
3794 | sec = NULL; | |
3795 | value = isym->st_value; | |
3796 | *sym_hash = NULL; | |
a4d8e49b | 3797 | common = bed->common_definition (isym); |
4ad4eba5 AM |
3798 | |
3799 | bind = ELF_ST_BIND (isym->st_info); | |
3800 | if (bind == STB_LOCAL) | |
3801 | { | |
3802 | /* This should be impossible, since ELF requires that all | |
3803 | global symbols follow all local symbols, and that sh_info | |
3804 | point to the first global symbol. Unfortunately, Irix 5 | |
3805 | screws this up. */ | |
3806 | continue; | |
3807 | } | |
3808 | else if (bind == STB_GLOBAL) | |
3809 | { | |
a4d8e49b | 3810 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 AM |
3811 | flags = BSF_GLOBAL; |
3812 | } | |
3813 | else if (bind == STB_WEAK) | |
3814 | flags = BSF_WEAK; | |
3815 | else | |
3816 | { | |
3817 | /* Leave it up to the processor backend. */ | |
3818 | } | |
3819 | ||
3820 | if (isym->st_shndx == SHN_UNDEF) | |
3821 | sec = bfd_und_section_ptr; | |
66eb6687 AM |
3822 | else if (isym->st_shndx < SHN_LORESERVE |
3823 | || isym->st_shndx > SHN_HIRESERVE) | |
4ad4eba5 AM |
3824 | { |
3825 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3826 | if (sec == NULL) | |
3827 | sec = bfd_abs_section_ptr; | |
529fcb95 PB |
3828 | else if (sec->kept_section) |
3829 | { | |
e5d08002 L |
3830 | /* Symbols from discarded section are undefined. We keep |
3831 | its visibility. */ | |
529fcb95 PB |
3832 | sec = bfd_und_section_ptr; |
3833 | isym->st_shndx = SHN_UNDEF; | |
3834 | } | |
4ad4eba5 AM |
3835 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
3836 | value -= sec->vma; | |
3837 | } | |
3838 | else if (isym->st_shndx == SHN_ABS) | |
3839 | sec = bfd_abs_section_ptr; | |
3840 | else if (isym->st_shndx == SHN_COMMON) | |
3841 | { | |
3842 | sec = bfd_com_section_ptr; | |
3843 | /* What ELF calls the size we call the value. What ELF | |
3844 | calls the value we call the alignment. */ | |
3845 | value = isym->st_size; | |
3846 | } | |
3847 | else | |
3848 | { | |
3849 | /* Leave it up to the processor backend. */ | |
3850 | } | |
3851 | ||
3852 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3853 | isym->st_name); | |
3854 | if (name == NULL) | |
3855 | goto error_free_vers; | |
3856 | ||
3857 | if (isym->st_shndx == SHN_COMMON | |
6a4a0940 JJ |
3858 | && ELF_ST_TYPE (isym->st_info) == STT_TLS |
3859 | && !info->relocatable) | |
4ad4eba5 AM |
3860 | { |
3861 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3862 | ||
3863 | if (tcomm == NULL) | |
3864 | { | |
3496cb2a L |
3865 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", |
3866 | (SEC_ALLOC | |
3867 | | SEC_IS_COMMON | |
3868 | | SEC_LINKER_CREATED | |
3869 | | SEC_THREAD_LOCAL)); | |
3870 | if (tcomm == NULL) | |
4ad4eba5 AM |
3871 | goto error_free_vers; |
3872 | } | |
3873 | sec = tcomm; | |
3874 | } | |
66eb6687 | 3875 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 3876 | { |
66eb6687 AM |
3877 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
3878 | &sec, &value)) | |
4ad4eba5 AM |
3879 | goto error_free_vers; |
3880 | ||
3881 | /* The hook function sets the name to NULL if this symbol | |
3882 | should be skipped for some reason. */ | |
3883 | if (name == NULL) | |
3884 | continue; | |
3885 | } | |
3886 | ||
3887 | /* Sanity check that all possibilities were handled. */ | |
3888 | if (sec == NULL) | |
3889 | { | |
3890 | bfd_set_error (bfd_error_bad_value); | |
3891 | goto error_free_vers; | |
3892 | } | |
3893 | ||
3894 | if (bfd_is_und_section (sec) | |
3895 | || bfd_is_com_section (sec)) | |
3896 | definition = FALSE; | |
3897 | else | |
3898 | definition = TRUE; | |
3899 | ||
3900 | size_change_ok = FALSE; | |
66eb6687 | 3901 | type_change_ok = bed->type_change_ok; |
4ad4eba5 AM |
3902 | old_alignment = 0; |
3903 | old_bfd = NULL; | |
af44c138 | 3904 | new_sec = sec; |
4ad4eba5 | 3905 | |
66eb6687 | 3906 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
3907 | { |
3908 | Elf_Internal_Versym iver; | |
3909 | unsigned int vernum = 0; | |
3910 | bfd_boolean skip; | |
3911 | ||
fc0e6df6 | 3912 | if (ever == NULL) |
4ad4eba5 | 3913 | { |
fc0e6df6 PB |
3914 | if (info->default_imported_symver) |
3915 | /* Use the default symbol version created earlier. */ | |
3916 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
3917 | else | |
3918 | iver.vs_vers = 0; | |
3919 | } | |
3920 | else | |
3921 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
3922 | ||
3923 | vernum = iver.vs_vers & VERSYM_VERSION; | |
3924 | ||
3925 | /* If this is a hidden symbol, or if it is not version | |
3926 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
3927 | However, we do not modify a non-hidden absolute symbol |
3928 | if it is not a function, because it might be the version | |
3929 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 3930 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
3931 | || (vernum > 1 |
3932 | && (!bfd_is_abs_section (sec) | |
3933 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
3934 | { |
3935 | const char *verstr; | |
3936 | size_t namelen, verlen, newlen; | |
3937 | char *newname, *p; | |
3938 | ||
3939 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 3940 | { |
fc0e6df6 PB |
3941 | if (vernum > elf_tdata (abfd)->cverdefs) |
3942 | verstr = NULL; | |
3943 | else if (vernum > 1) | |
3944 | verstr = | |
3945 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
3946 | else | |
3947 | verstr = ""; | |
4ad4eba5 | 3948 | |
fc0e6df6 | 3949 | if (verstr == NULL) |
4ad4eba5 | 3950 | { |
fc0e6df6 PB |
3951 | (*_bfd_error_handler) |
3952 | (_("%B: %s: invalid version %u (max %d)"), | |
3953 | abfd, name, vernum, | |
3954 | elf_tdata (abfd)->cverdefs); | |
3955 | bfd_set_error (bfd_error_bad_value); | |
3956 | goto error_free_vers; | |
4ad4eba5 | 3957 | } |
fc0e6df6 PB |
3958 | } |
3959 | else | |
3960 | { | |
3961 | /* We cannot simply test for the number of | |
3962 | entries in the VERNEED section since the | |
3963 | numbers for the needed versions do not start | |
3964 | at 0. */ | |
3965 | Elf_Internal_Verneed *t; | |
3966 | ||
3967 | verstr = NULL; | |
3968 | for (t = elf_tdata (abfd)->verref; | |
3969 | t != NULL; | |
3970 | t = t->vn_nextref) | |
4ad4eba5 | 3971 | { |
fc0e6df6 | 3972 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 3973 | |
fc0e6df6 PB |
3974 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
3975 | { | |
3976 | if (a->vna_other == vernum) | |
4ad4eba5 | 3977 | { |
fc0e6df6 PB |
3978 | verstr = a->vna_nodename; |
3979 | break; | |
4ad4eba5 | 3980 | } |
4ad4eba5 | 3981 | } |
fc0e6df6 PB |
3982 | if (a != NULL) |
3983 | break; | |
3984 | } | |
3985 | if (verstr == NULL) | |
3986 | { | |
3987 | (*_bfd_error_handler) | |
3988 | (_("%B: %s: invalid needed version %d"), | |
3989 | abfd, name, vernum); | |
3990 | bfd_set_error (bfd_error_bad_value); | |
3991 | goto error_free_vers; | |
4ad4eba5 | 3992 | } |
4ad4eba5 | 3993 | } |
fc0e6df6 PB |
3994 | |
3995 | namelen = strlen (name); | |
3996 | verlen = strlen (verstr); | |
3997 | newlen = namelen + verlen + 2; | |
3998 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3999 | && isym->st_shndx != SHN_UNDEF) | |
4000 | ++newlen; | |
4001 | ||
66eb6687 | 4002 | newname = bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
4003 | if (newname == NULL) |
4004 | goto error_free_vers; | |
4005 | memcpy (newname, name, namelen); | |
4006 | p = newname + namelen; | |
4007 | *p++ = ELF_VER_CHR; | |
4008 | /* If this is a defined non-hidden version symbol, | |
4009 | we add another @ to the name. This indicates the | |
4010 | default version of the symbol. */ | |
4011 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4012 | && isym->st_shndx != SHN_UNDEF) | |
4013 | *p++ = ELF_VER_CHR; | |
4014 | memcpy (p, verstr, verlen + 1); | |
4015 | ||
4016 | name = newname; | |
4ad4eba5 AM |
4017 | } |
4018 | ||
af44c138 L |
4019 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, |
4020 | &value, &old_alignment, | |
4ad4eba5 AM |
4021 | sym_hash, &skip, &override, |
4022 | &type_change_ok, &size_change_ok)) | |
4023 | goto error_free_vers; | |
4024 | ||
4025 | if (skip) | |
4026 | continue; | |
4027 | ||
4028 | if (override) | |
4029 | definition = FALSE; | |
4030 | ||
4031 | h = *sym_hash; | |
4032 | while (h->root.type == bfd_link_hash_indirect | |
4033 | || h->root.type == bfd_link_hash_warning) | |
4034 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4035 | ||
4036 | /* Remember the old alignment if this is a common symbol, so | |
4037 | that we don't reduce the alignment later on. We can't | |
4038 | check later, because _bfd_generic_link_add_one_symbol | |
4039 | will set a default for the alignment which we want to | |
4040 | override. We also remember the old bfd where the existing | |
4041 | definition comes from. */ | |
4042 | switch (h->root.type) | |
4043 | { | |
4044 | default: | |
4045 | break; | |
4046 | ||
4047 | case bfd_link_hash_defined: | |
4048 | case bfd_link_hash_defweak: | |
4049 | old_bfd = h->root.u.def.section->owner; | |
4050 | break; | |
4051 | ||
4052 | case bfd_link_hash_common: | |
4053 | old_bfd = h->root.u.c.p->section->owner; | |
4054 | old_alignment = h->root.u.c.p->alignment_power; | |
4055 | break; | |
4056 | } | |
4057 | ||
4058 | if (elf_tdata (abfd)->verdef != NULL | |
4059 | && ! override | |
4060 | && vernum > 1 | |
4061 | && definition) | |
4062 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4063 | } | |
4064 | ||
4065 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4066 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4067 | (struct bfd_link_hash_entry **) sym_hash))) |
4068 | goto error_free_vers; | |
4069 | ||
4070 | h = *sym_hash; | |
4071 | while (h->root.type == bfd_link_hash_indirect | |
4072 | || h->root.type == bfd_link_hash_warning) | |
4073 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4074 | *sym_hash = h; | |
4075 | ||
4076 | new_weakdef = FALSE; | |
4077 | if (dynamic | |
4078 | && definition | |
4079 | && (flags & BSF_WEAK) != 0 | |
fcb93ecf | 4080 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4081 | && is_elf_hash_table (htab) |
f6e332e6 | 4082 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
4083 | { |
4084 | /* Keep a list of all weak defined non function symbols from | |
4085 | a dynamic object, using the weakdef field. Later in this | |
4086 | function we will set the weakdef field to the correct | |
4087 | value. We only put non-function symbols from dynamic | |
4088 | objects on this list, because that happens to be the only | |
4089 | time we need to know the normal symbol corresponding to a | |
4090 | weak symbol, and the information is time consuming to | |
4091 | figure out. If the weakdef field is not already NULL, | |
4092 | then this symbol was already defined by some previous | |
4093 | dynamic object, and we will be using that previous | |
4094 | definition anyhow. */ | |
4095 | ||
f6e332e6 | 4096 | h->u.weakdef = weaks; |
4ad4eba5 AM |
4097 | weaks = h; |
4098 | new_weakdef = TRUE; | |
4099 | } | |
4100 | ||
4101 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4102 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4103 | && h->root.type == bfd_link_hash_common) |
4104 | { | |
4105 | unsigned int align; | |
4106 | ||
a4d8e49b | 4107 | if (common) |
af44c138 L |
4108 | align = bfd_log2 (isym->st_value); |
4109 | else | |
4110 | { | |
4111 | /* The new symbol is a common symbol in a shared object. | |
4112 | We need to get the alignment from the section. */ | |
4113 | align = new_sec->alignment_power; | |
4114 | } | |
4ad4eba5 AM |
4115 | if (align > old_alignment |
4116 | /* Permit an alignment power of zero if an alignment of one | |
4117 | is specified and no other alignments have been specified. */ | |
4118 | || (isym->st_value == 1 && old_alignment == 0)) | |
4119 | h->root.u.c.p->alignment_power = align; | |
4120 | else | |
4121 | h->root.u.c.p->alignment_power = old_alignment; | |
4122 | } | |
4123 | ||
66eb6687 | 4124 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4125 | { |
4ad4eba5 | 4126 | bfd_boolean dynsym; |
4ad4eba5 AM |
4127 | |
4128 | /* Check the alignment when a common symbol is involved. This | |
4129 | can change when a common symbol is overridden by a normal | |
4130 | definition or a common symbol is ignored due to the old | |
4131 | normal definition. We need to make sure the maximum | |
4132 | alignment is maintained. */ | |
a4d8e49b | 4133 | if ((old_alignment || common) |
4ad4eba5 AM |
4134 | && h->root.type != bfd_link_hash_common) |
4135 | { | |
4136 | unsigned int common_align; | |
4137 | unsigned int normal_align; | |
4138 | unsigned int symbol_align; | |
4139 | bfd *normal_bfd; | |
4140 | bfd *common_bfd; | |
4141 | ||
4142 | symbol_align = ffs (h->root.u.def.value) - 1; | |
4143 | if (h->root.u.def.section->owner != NULL | |
4144 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
4145 | { | |
4146 | normal_align = h->root.u.def.section->alignment_power; | |
4147 | if (normal_align > symbol_align) | |
4148 | normal_align = symbol_align; | |
4149 | } | |
4150 | else | |
4151 | normal_align = symbol_align; | |
4152 | ||
4153 | if (old_alignment) | |
4154 | { | |
4155 | common_align = old_alignment; | |
4156 | common_bfd = old_bfd; | |
4157 | normal_bfd = abfd; | |
4158 | } | |
4159 | else | |
4160 | { | |
4161 | common_align = bfd_log2 (isym->st_value); | |
4162 | common_bfd = abfd; | |
4163 | normal_bfd = old_bfd; | |
4164 | } | |
4165 | ||
4166 | if (normal_align < common_align) | |
d07676f8 NC |
4167 | { |
4168 | /* PR binutils/2735 */ | |
4169 | if (normal_bfd == NULL) | |
4170 | (*_bfd_error_handler) | |
4171 | (_("Warning: alignment %u of common symbol `%s' in %B" | |
4172 | " is greater than the alignment (%u) of its section %A"), | |
4173 | common_bfd, h->root.u.def.section, | |
4174 | 1 << common_align, name, 1 << normal_align); | |
4175 | else | |
4176 | (*_bfd_error_handler) | |
4177 | (_("Warning: alignment %u of symbol `%s' in %B" | |
4178 | " is smaller than %u in %B"), | |
4179 | normal_bfd, common_bfd, | |
4180 | 1 << normal_align, name, 1 << common_align); | |
4181 | } | |
4ad4eba5 AM |
4182 | } |
4183 | ||
83ad0046 L |
4184 | /* Remember the symbol size if it isn't undefined. */ |
4185 | if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 AM |
4186 | && (definition || h->size == 0)) |
4187 | { | |
83ad0046 L |
4188 | if (h->size != 0 |
4189 | && h->size != isym->st_size | |
4190 | && ! size_change_ok) | |
4ad4eba5 | 4191 | (*_bfd_error_handler) |
d003868e AM |
4192 | (_("Warning: size of symbol `%s' changed" |
4193 | " from %lu in %B to %lu in %B"), | |
4194 | old_bfd, abfd, | |
4ad4eba5 | 4195 | name, (unsigned long) h->size, |
d003868e | 4196 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
4197 | |
4198 | h->size = isym->st_size; | |
4199 | } | |
4200 | ||
4201 | /* If this is a common symbol, then we always want H->SIZE | |
4202 | to be the size of the common symbol. The code just above | |
4203 | won't fix the size if a common symbol becomes larger. We | |
4204 | don't warn about a size change here, because that is | |
fcb93ecf PB |
4205 | covered by --warn-common. Allow changed between different |
4206 | function types. */ | |
4ad4eba5 AM |
4207 | if (h->root.type == bfd_link_hash_common) |
4208 | h->size = h->root.u.c.size; | |
4209 | ||
4210 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
4211 | && (definition || h->type == STT_NOTYPE)) | |
4212 | { | |
4213 | if (h->type != STT_NOTYPE | |
4214 | && h->type != ELF_ST_TYPE (isym->st_info) | |
4215 | && ! type_change_ok) | |
4216 | (*_bfd_error_handler) | |
d003868e AM |
4217 | (_("Warning: type of symbol `%s' changed" |
4218 | " from %d to %d in %B"), | |
4219 | abfd, name, h->type, ELF_ST_TYPE (isym->st_info)); | |
4ad4eba5 AM |
4220 | |
4221 | h->type = ELF_ST_TYPE (isym->st_info); | |
4222 | } | |
4223 | ||
4224 | /* If st_other has a processor-specific meaning, specific | |
4225 | code might be needed here. We never merge the visibility | |
4226 | attribute with the one from a dynamic object. */ | |
4227 | if (bed->elf_backend_merge_symbol_attribute) | |
4228 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
4229 | dynamic); | |
4230 | ||
b58f81ae DJ |
4231 | /* If this symbol has default visibility and the user has requested |
4232 | we not re-export it, then mark it as hidden. */ | |
4233 | if (definition && !dynamic | |
4234 | && (abfd->no_export | |
4235 | || (abfd->my_archive && abfd->my_archive->no_export)) | |
4236 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) | |
66eb6687 AM |
4237 | isym->st_other = (STV_HIDDEN |
4238 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
b58f81ae | 4239 | |
8992f0d7 | 4240 | if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic) |
4ad4eba5 AM |
4241 | { |
4242 | unsigned char hvis, symvis, other, nvis; | |
4243 | ||
8992f0d7 TS |
4244 | /* Only merge the visibility. Leave the remainder of the |
4245 | st_other field to elf_backend_merge_symbol_attribute. */ | |
4246 | other = h->other & ~ELF_ST_VISIBILITY (-1); | |
4ad4eba5 AM |
4247 | |
4248 | /* Combine visibilities, using the most constraining one. */ | |
4249 | hvis = ELF_ST_VISIBILITY (h->other); | |
4250 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
4251 | if (! hvis) | |
4252 | nvis = symvis; | |
4253 | else if (! symvis) | |
4254 | nvis = hvis; | |
4255 | else | |
4256 | nvis = hvis < symvis ? hvis : symvis; | |
4257 | ||
4258 | h->other = other | nvis; | |
4259 | } | |
4260 | ||
4261 | /* Set a flag in the hash table entry indicating the type of | |
4262 | reference or definition we just found. Keep a count of | |
4263 | the number of dynamic symbols we find. A dynamic symbol | |
4264 | is one which is referenced or defined by both a regular | |
4265 | object and a shared object. */ | |
4ad4eba5 AM |
4266 | dynsym = FALSE; |
4267 | if (! dynamic) | |
4268 | { | |
4269 | if (! definition) | |
4270 | { | |
f5385ebf | 4271 | h->ref_regular = 1; |
4ad4eba5 | 4272 | if (bind != STB_WEAK) |
f5385ebf | 4273 | h->ref_regular_nonweak = 1; |
4ad4eba5 AM |
4274 | } |
4275 | else | |
f5385ebf | 4276 | h->def_regular = 1; |
4ad4eba5 | 4277 | if (! info->executable |
f5385ebf AM |
4278 | || h->def_dynamic |
4279 | || h->ref_dynamic) | |
4ad4eba5 AM |
4280 | dynsym = TRUE; |
4281 | } | |
4282 | else | |
4283 | { | |
4284 | if (! definition) | |
f5385ebf | 4285 | h->ref_dynamic = 1; |
4ad4eba5 | 4286 | else |
f5385ebf AM |
4287 | h->def_dynamic = 1; |
4288 | if (h->def_regular | |
4289 | || h->ref_regular | |
f6e332e6 | 4290 | || (h->u.weakdef != NULL |
4ad4eba5 | 4291 | && ! new_weakdef |
f6e332e6 | 4292 | && h->u.weakdef->dynindx != -1)) |
4ad4eba5 AM |
4293 | dynsym = TRUE; |
4294 | } | |
4295 | ||
92b7c7b6 L |
4296 | if (definition && (sec->flags & SEC_DEBUGGING)) |
4297 | { | |
4298 | /* We don't want to make debug symbol dynamic. */ | |
4299 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4300 | dynsym = FALSE; | |
4301 | } | |
4302 | ||
4ad4eba5 AM |
4303 | /* Check to see if we need to add an indirect symbol for |
4304 | the default name. */ | |
4305 | if (definition || h->root.type == bfd_link_hash_common) | |
4306 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4307 | &sec, &value, &dynsym, | |
4308 | override)) | |
4309 | goto error_free_vers; | |
4310 | ||
4311 | if (definition && !dynamic) | |
4312 | { | |
4313 | char *p = strchr (name, ELF_VER_CHR); | |
4314 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4315 | { | |
4316 | /* Queue non-default versions so that .symver x, x@FOO | |
4317 | aliases can be checked. */ | |
66eb6687 | 4318 | if (!nondeflt_vers) |
4ad4eba5 | 4319 | { |
66eb6687 AM |
4320 | amt = ((isymend - isym + 1) |
4321 | * sizeof (struct elf_link_hash_entry *)); | |
4ad4eba5 | 4322 | nondeflt_vers = bfd_malloc (amt); |
14b1c01e AM |
4323 | if (!nondeflt_vers) |
4324 | goto error_free_vers; | |
4ad4eba5 | 4325 | } |
66eb6687 | 4326 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4327 | } |
4328 | } | |
4329 | ||
4330 | if (dynsym && h->dynindx == -1) | |
4331 | { | |
c152c796 | 4332 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4333 | goto error_free_vers; |
f6e332e6 | 4334 | if (h->u.weakdef != NULL |
4ad4eba5 | 4335 | && ! new_weakdef |
f6e332e6 | 4336 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 4337 | { |
66eb6687 | 4338 | if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
4339 | goto error_free_vers; |
4340 | } | |
4341 | } | |
4342 | else if (dynsym && h->dynindx != -1) | |
4343 | /* If the symbol already has a dynamic index, but | |
4344 | visibility says it should not be visible, turn it into | |
4345 | a local symbol. */ | |
4346 | switch (ELF_ST_VISIBILITY (h->other)) | |
4347 | { | |
4348 | case STV_INTERNAL: | |
4349 | case STV_HIDDEN: | |
4350 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4351 | dynsym = FALSE; | |
4352 | break; | |
4353 | } | |
4354 | ||
4355 | if (!add_needed | |
4356 | && definition | |
4357 | && dynsym | |
f5385ebf | 4358 | && h->ref_regular) |
4ad4eba5 AM |
4359 | { |
4360 | int ret; | |
4361 | const char *soname = elf_dt_name (abfd); | |
4362 | ||
4363 | /* A symbol from a library loaded via DT_NEEDED of some | |
4364 | other library is referenced by a regular object. | |
e56f61be L |
4365 | Add a DT_NEEDED entry for it. Issue an error if |
4366 | --no-add-needed is used. */ | |
4367 | if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) | |
4368 | { | |
4369 | (*_bfd_error_handler) | |
4370 | (_("%s: invalid DSO for symbol `%s' definition"), | |
d003868e | 4371 | abfd, name); |
e56f61be L |
4372 | bfd_set_error (bfd_error_bad_value); |
4373 | goto error_free_vers; | |
4374 | } | |
4375 | ||
a5db907e AM |
4376 | elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED; |
4377 | ||
4ad4eba5 | 4378 | add_needed = TRUE; |
7e9f0867 | 4379 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4380 | if (ret < 0) |
4381 | goto error_free_vers; | |
4382 | ||
4383 | BFD_ASSERT (ret == 0); | |
4384 | } | |
4385 | } | |
4386 | } | |
4387 | ||
66eb6687 AM |
4388 | if (extversym != NULL) |
4389 | { | |
4390 | free (extversym); | |
4391 | extversym = NULL; | |
4392 | } | |
4393 | ||
4394 | if (isymbuf != NULL) | |
4395 | { | |
4396 | free (isymbuf); | |
4397 | isymbuf = NULL; | |
4398 | } | |
4399 | ||
4400 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4401 | { | |
4402 | unsigned int i; | |
4403 | ||
4404 | /* Restore the symbol table. */ | |
97fed1c9 JJ |
4405 | if (bed->as_needed_cleanup) |
4406 | (*bed->as_needed_cleanup) (abfd, info); | |
66eb6687 AM |
4407 | old_hash = (char *) old_tab + tabsize; |
4408 | old_ent = (char *) old_hash + hashsize; | |
4409 | sym_hash = elf_sym_hashes (abfd); | |
4f87808c AM |
4410 | htab->root.table.table = old_table; |
4411 | htab->root.table.size = old_size; | |
4412 | htab->root.table.count = old_count; | |
66eb6687 AM |
4413 | memcpy (htab->root.table.table, old_tab, tabsize); |
4414 | memcpy (sym_hash, old_hash, hashsize); | |
4415 | htab->root.undefs = old_undefs; | |
4416 | htab->root.undefs_tail = old_undefs_tail; | |
4417 | for (i = 0; i < htab->root.table.size; i++) | |
4418 | { | |
4419 | struct bfd_hash_entry *p; | |
4420 | struct elf_link_hash_entry *h; | |
4421 | ||
4422 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4423 | { | |
4424 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
4425 | if (h->root.type == bfd_link_hash_warning) |
4426 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
66eb6687 AM |
4427 | if (h->dynindx >= old_dynsymcount) |
4428 | _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index); | |
2de92251 | 4429 | |
66eb6687 AM |
4430 | memcpy (p, old_ent, htab->root.table.entsize); |
4431 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4432 | h = (struct elf_link_hash_entry *) p; |
4433 | if (h->root.type == bfd_link_hash_warning) | |
4434 | { | |
4435 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
4436 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4437 | } | |
66eb6687 AM |
4438 | } |
4439 | } | |
4440 | ||
5061a885 AM |
4441 | /* Make a special call to the linker "notice" function to |
4442 | tell it that symbols added for crefs may need to be removed. */ | |
4443 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, | |
4444 | notice_not_needed)) | |
9af2a943 | 4445 | goto error_free_vers; |
5061a885 | 4446 | |
66eb6687 AM |
4447 | free (old_tab); |
4448 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
4449 | alloc_mark); | |
4450 | if (nondeflt_vers != NULL) | |
4451 | free (nondeflt_vers); | |
4452 | return TRUE; | |
4453 | } | |
2de92251 | 4454 | |
66eb6687 AM |
4455 | if (old_tab != NULL) |
4456 | { | |
5061a885 AM |
4457 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, |
4458 | notice_needed)) | |
9af2a943 | 4459 | goto error_free_vers; |
66eb6687 AM |
4460 | free (old_tab); |
4461 | old_tab = NULL; | |
4462 | } | |
4463 | ||
4ad4eba5 AM |
4464 | /* Now that all the symbols from this input file are created, handle |
4465 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
4466 | if (nondeflt_vers != NULL) | |
4467 | { | |
4468 | bfd_size_type cnt, symidx; | |
4469 | ||
4470 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
4471 | { | |
4472 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
4473 | char *shortname, *p; | |
4474 | ||
4475 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
4476 | if (p == NULL | |
4477 | || (h->root.type != bfd_link_hash_defined | |
4478 | && h->root.type != bfd_link_hash_defweak)) | |
4479 | continue; | |
4480 | ||
4481 | amt = p - h->root.root.string; | |
4482 | shortname = bfd_malloc (amt + 1); | |
14b1c01e AM |
4483 | if (!shortname) |
4484 | goto error_free_vers; | |
4ad4eba5 AM |
4485 | memcpy (shortname, h->root.root.string, amt); |
4486 | shortname[amt] = '\0'; | |
4487 | ||
4488 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 4489 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
4490 | FALSE, FALSE, FALSE); |
4491 | if (hi != NULL | |
4492 | && hi->root.type == h->root.type | |
4493 | && hi->root.u.def.value == h->root.u.def.value | |
4494 | && hi->root.u.def.section == h->root.u.def.section) | |
4495 | { | |
4496 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4497 | hi->root.type = bfd_link_hash_indirect; | |
4498 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 4499 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
4500 | sym_hash = elf_sym_hashes (abfd); |
4501 | if (sym_hash) | |
4502 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4503 | if (sym_hash[symidx] == hi) | |
4504 | { | |
4505 | sym_hash[symidx] = h; | |
4506 | break; | |
4507 | } | |
4508 | } | |
4509 | free (shortname); | |
4510 | } | |
4511 | free (nondeflt_vers); | |
4512 | nondeflt_vers = NULL; | |
4513 | } | |
4514 | ||
4ad4eba5 AM |
4515 | /* Now set the weakdefs field correctly for all the weak defined |
4516 | symbols we found. The only way to do this is to search all the | |
4517 | symbols. Since we only need the information for non functions in | |
4518 | dynamic objects, that's the only time we actually put anything on | |
4519 | the list WEAKS. We need this information so that if a regular | |
4520 | object refers to a symbol defined weakly in a dynamic object, the | |
4521 | real symbol in the dynamic object is also put in the dynamic | |
4522 | symbols; we also must arrange for both symbols to point to the | |
4523 | same memory location. We could handle the general case of symbol | |
4524 | aliasing, but a general symbol alias can only be generated in | |
4525 | assembler code, handling it correctly would be very time | |
4526 | consuming, and other ELF linkers don't handle general aliasing | |
4527 | either. */ | |
4528 | if (weaks != NULL) | |
4529 | { | |
4530 | struct elf_link_hash_entry **hpp; | |
4531 | struct elf_link_hash_entry **hppend; | |
4532 | struct elf_link_hash_entry **sorted_sym_hash; | |
4533 | struct elf_link_hash_entry *h; | |
4534 | size_t sym_count; | |
4535 | ||
4536 | /* Since we have to search the whole symbol list for each weak | |
4537 | defined symbol, search time for N weak defined symbols will be | |
4538 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
4539 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
4540 | sorted_sym_hash = bfd_malloc (amt); | |
4541 | if (sorted_sym_hash == NULL) | |
4542 | goto error_return; | |
4543 | sym_hash = sorted_sym_hash; | |
4544 | hpp = elf_sym_hashes (abfd); | |
4545 | hppend = hpp + extsymcount; | |
4546 | sym_count = 0; | |
4547 | for (; hpp < hppend; hpp++) | |
4548 | { | |
4549 | h = *hpp; | |
4550 | if (h != NULL | |
4551 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 4552 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
4553 | { |
4554 | *sym_hash = h; | |
4555 | sym_hash++; | |
4556 | sym_count++; | |
4557 | } | |
4558 | } | |
4559 | ||
4560 | qsort (sorted_sym_hash, sym_count, | |
4561 | sizeof (struct elf_link_hash_entry *), | |
4562 | elf_sort_symbol); | |
4563 | ||
4564 | while (weaks != NULL) | |
4565 | { | |
4566 | struct elf_link_hash_entry *hlook; | |
4567 | asection *slook; | |
4568 | bfd_vma vlook; | |
4569 | long ilook; | |
4570 | size_t i, j, idx; | |
4571 | ||
4572 | hlook = weaks; | |
f6e332e6 AM |
4573 | weaks = hlook->u.weakdef; |
4574 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
4575 | |
4576 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
4577 | || hlook->root.type == bfd_link_hash_defweak | |
4578 | || hlook->root.type == bfd_link_hash_common | |
4579 | || hlook->root.type == bfd_link_hash_indirect); | |
4580 | slook = hlook->root.u.def.section; | |
4581 | vlook = hlook->root.u.def.value; | |
4582 | ||
4583 | ilook = -1; | |
4584 | i = 0; | |
4585 | j = sym_count; | |
4586 | while (i < j) | |
4587 | { | |
4588 | bfd_signed_vma vdiff; | |
4589 | idx = (i + j) / 2; | |
4590 | h = sorted_sym_hash [idx]; | |
4591 | vdiff = vlook - h->root.u.def.value; | |
4592 | if (vdiff < 0) | |
4593 | j = idx; | |
4594 | else if (vdiff > 0) | |
4595 | i = idx + 1; | |
4596 | else | |
4597 | { | |
a9b881be | 4598 | long sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
4599 | if (sdiff < 0) |
4600 | j = idx; | |
4601 | else if (sdiff > 0) | |
4602 | i = idx + 1; | |
4603 | else | |
4604 | { | |
4605 | ilook = idx; | |
4606 | break; | |
4607 | } | |
4608 | } | |
4609 | } | |
4610 | ||
4611 | /* We didn't find a value/section match. */ | |
4612 | if (ilook == -1) | |
4613 | continue; | |
4614 | ||
4615 | for (i = ilook; i < sym_count; i++) | |
4616 | { | |
4617 | h = sorted_sym_hash [i]; | |
4618 | ||
4619 | /* Stop if value or section doesn't match. */ | |
4620 | if (h->root.u.def.value != vlook | |
4621 | || h->root.u.def.section != slook) | |
4622 | break; | |
4623 | else if (h != hlook) | |
4624 | { | |
f6e332e6 | 4625 | hlook->u.weakdef = h; |
4ad4eba5 AM |
4626 | |
4627 | /* If the weak definition is in the list of dynamic | |
4628 | symbols, make sure the real definition is put | |
4629 | there as well. */ | |
4630 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
4631 | { | |
c152c796 | 4632 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 AM |
4633 | goto error_return; |
4634 | } | |
4635 | ||
4636 | /* If the real definition is in the list of dynamic | |
4637 | symbols, make sure the weak definition is put | |
4638 | there as well. If we don't do this, then the | |
4639 | dynamic loader might not merge the entries for the | |
4640 | real definition and the weak definition. */ | |
4641 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
4642 | { | |
c152c796 | 4643 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4ad4eba5 AM |
4644 | goto error_return; |
4645 | } | |
4646 | break; | |
4647 | } | |
4648 | } | |
4649 | } | |
4650 | ||
4651 | free (sorted_sym_hash); | |
4652 | } | |
4653 | ||
66eb6687 AM |
4654 | if (bed->check_directives) |
4655 | (*bed->check_directives) (abfd, info); | |
85fbca6a | 4656 | |
4ad4eba5 AM |
4657 | /* If this object is the same format as the output object, and it is |
4658 | not a shared library, then let the backend look through the | |
4659 | relocs. | |
4660 | ||
4661 | This is required to build global offset table entries and to | |
4662 | arrange for dynamic relocs. It is not required for the | |
4663 | particular common case of linking non PIC code, even when linking | |
4664 | against shared libraries, but unfortunately there is no way of | |
4665 | knowing whether an object file has been compiled PIC or not. | |
4666 | Looking through the relocs is not particularly time consuming. | |
4667 | The problem is that we must either (1) keep the relocs in memory, | |
4668 | which causes the linker to require additional runtime memory or | |
4669 | (2) read the relocs twice from the input file, which wastes time. | |
4670 | This would be a good case for using mmap. | |
4671 | ||
4672 | I have no idea how to handle linking PIC code into a file of a | |
4673 | different format. It probably can't be done. */ | |
4ad4eba5 | 4674 | if (! dynamic |
66eb6687 | 4675 | && is_elf_hash_table (htab) |
13285a1b | 4676 | && bed->check_relocs != NULL |
f13a99db | 4677 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) |
4ad4eba5 AM |
4678 | { |
4679 | asection *o; | |
4680 | ||
4681 | for (o = abfd->sections; o != NULL; o = o->next) | |
4682 | { | |
4683 | Elf_Internal_Rela *internal_relocs; | |
4684 | bfd_boolean ok; | |
4685 | ||
4686 | if ((o->flags & SEC_RELOC) == 0 | |
4687 | || o->reloc_count == 0 | |
4688 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4689 | && (o->flags & SEC_DEBUGGING) != 0) | |
4690 | || bfd_is_abs_section (o->output_section)) | |
4691 | continue; | |
4692 | ||
4693 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4694 | info->keep_memory); | |
4695 | if (internal_relocs == NULL) | |
4696 | goto error_return; | |
4697 | ||
66eb6687 | 4698 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); |
4ad4eba5 AM |
4699 | |
4700 | if (elf_section_data (o)->relocs != internal_relocs) | |
4701 | free (internal_relocs); | |
4702 | ||
4703 | if (! ok) | |
4704 | goto error_return; | |
4705 | } | |
4706 | } | |
4707 | ||
4708 | /* If this is a non-traditional link, try to optimize the handling | |
4709 | of the .stab/.stabstr sections. */ | |
4710 | if (! dynamic | |
4711 | && ! info->traditional_format | |
66eb6687 | 4712 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
4713 | && (info->strip != strip_all && info->strip != strip_debugger)) |
4714 | { | |
4715 | asection *stabstr; | |
4716 | ||
4717 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4718 | if (stabstr != NULL) | |
4719 | { | |
4720 | bfd_size_type string_offset = 0; | |
4721 | asection *stab; | |
4722 | ||
4723 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 4724 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
4725 | && (!stab->name[5] || |
4726 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4727 | && (stab->flags & SEC_MERGE) == 0 | |
4728 | && !bfd_is_abs_section (stab->output_section)) | |
4729 | { | |
4730 | struct bfd_elf_section_data *secdata; | |
4731 | ||
4732 | secdata = elf_section_data (stab); | |
66eb6687 AM |
4733 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
4734 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
4735 | &string_offset)) |
4736 | goto error_return; | |
4737 | if (secdata->sec_info) | |
4738 | stab->sec_info_type = ELF_INFO_TYPE_STABS; | |
4739 | } | |
4740 | } | |
4741 | } | |
4742 | ||
66eb6687 | 4743 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
4744 | { |
4745 | /* Add this bfd to the loaded list. */ | |
4746 | struct elf_link_loaded_list *n; | |
4747 | ||
4748 | n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); | |
4749 | if (n == NULL) | |
4750 | goto error_return; | |
4751 | n->abfd = abfd; | |
66eb6687 AM |
4752 | n->next = htab->loaded; |
4753 | htab->loaded = n; | |
4ad4eba5 AM |
4754 | } |
4755 | ||
4756 | return TRUE; | |
4757 | ||
4758 | error_free_vers: | |
66eb6687 AM |
4759 | if (old_tab != NULL) |
4760 | free (old_tab); | |
4ad4eba5 AM |
4761 | if (nondeflt_vers != NULL) |
4762 | free (nondeflt_vers); | |
4763 | if (extversym != NULL) | |
4764 | free (extversym); | |
4765 | error_free_sym: | |
4766 | if (isymbuf != NULL) | |
4767 | free (isymbuf); | |
4768 | error_return: | |
4769 | return FALSE; | |
4770 | } | |
4771 | ||
8387904d AM |
4772 | /* Return the linker hash table entry of a symbol that might be |
4773 | satisfied by an archive symbol. Return -1 on error. */ | |
4774 | ||
4775 | struct elf_link_hash_entry * | |
4776 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
4777 | struct bfd_link_info *info, | |
4778 | const char *name) | |
4779 | { | |
4780 | struct elf_link_hash_entry *h; | |
4781 | char *p, *copy; | |
4782 | size_t len, first; | |
4783 | ||
4784 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
4785 | if (h != NULL) | |
4786 | return h; | |
4787 | ||
4788 | /* If this is a default version (the name contains @@), look up the | |
4789 | symbol again with only one `@' as well as without the version. | |
4790 | The effect is that references to the symbol with and without the | |
4791 | version will be matched by the default symbol in the archive. */ | |
4792 | ||
4793 | p = strchr (name, ELF_VER_CHR); | |
4794 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4795 | return h; | |
4796 | ||
4797 | /* First check with only one `@'. */ | |
4798 | len = strlen (name); | |
4799 | copy = bfd_alloc (abfd, len); | |
4800 | if (copy == NULL) | |
4801 | return (struct elf_link_hash_entry *) 0 - 1; | |
4802 | ||
4803 | first = p - name + 1; | |
4804 | memcpy (copy, name, first); | |
4805 | memcpy (copy + first, name + first + 1, len - first); | |
4806 | ||
4807 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE); | |
4808 | if (h == NULL) | |
4809 | { | |
4810 | /* We also need to check references to the symbol without the | |
4811 | version. */ | |
4812 | copy[first - 1] = '\0'; | |
4813 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
4814 | FALSE, FALSE, FALSE); | |
4815 | } | |
4816 | ||
4817 | bfd_release (abfd, copy); | |
4818 | return h; | |
4819 | } | |
4820 | ||
0ad989f9 L |
4821 | /* Add symbols from an ELF archive file to the linker hash table. We |
4822 | don't use _bfd_generic_link_add_archive_symbols because of a | |
4823 | problem which arises on UnixWare. The UnixWare libc.so is an | |
4824 | archive which includes an entry libc.so.1 which defines a bunch of | |
4825 | symbols. The libc.so archive also includes a number of other | |
4826 | object files, which also define symbols, some of which are the same | |
4827 | as those defined in libc.so.1. Correct linking requires that we | |
4828 | consider each object file in turn, and include it if it defines any | |
4829 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
4830 | this; it looks through the list of undefined symbols, and includes | |
4831 | any object file which defines them. When this algorithm is used on | |
4832 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
4833 | bunch of symbols. This means that some of the other objects in the | |
4834 | archive are not included in the link, which is incorrect since they | |
4835 | precede libc.so.1 in the archive. | |
4836 | ||
4837 | Fortunately, ELF archive handling is simpler than that done by | |
4838 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4839 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4840 | symbol is currently undefined, we know that we must pull in that | |
4841 | object file. | |
4842 | ||
4843 | Unfortunately, we do have to make multiple passes over the symbol | |
4844 | table until nothing further is resolved. */ | |
4845 | ||
4ad4eba5 AM |
4846 | static bfd_boolean |
4847 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
4848 | { |
4849 | symindex c; | |
4850 | bfd_boolean *defined = NULL; | |
4851 | bfd_boolean *included = NULL; | |
4852 | carsym *symdefs; | |
4853 | bfd_boolean loop; | |
4854 | bfd_size_type amt; | |
8387904d AM |
4855 | const struct elf_backend_data *bed; |
4856 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
4857 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
4858 | |
4859 | if (! bfd_has_map (abfd)) | |
4860 | { | |
4861 | /* An empty archive is a special case. */ | |
4862 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
4863 | return TRUE; | |
4864 | bfd_set_error (bfd_error_no_armap); | |
4865 | return FALSE; | |
4866 | } | |
4867 | ||
4868 | /* Keep track of all symbols we know to be already defined, and all | |
4869 | files we know to be already included. This is to speed up the | |
4870 | second and subsequent passes. */ | |
4871 | c = bfd_ardata (abfd)->symdef_count; | |
4872 | if (c == 0) | |
4873 | return TRUE; | |
4874 | amt = c; | |
4875 | amt *= sizeof (bfd_boolean); | |
4876 | defined = bfd_zmalloc (amt); | |
4877 | included = bfd_zmalloc (amt); | |
4878 | if (defined == NULL || included == NULL) | |
4879 | goto error_return; | |
4880 | ||
4881 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
4882 | bed = get_elf_backend_data (abfd); |
4883 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
4884 | |
4885 | do | |
4886 | { | |
4887 | file_ptr last; | |
4888 | symindex i; | |
4889 | carsym *symdef; | |
4890 | carsym *symdefend; | |
4891 | ||
4892 | loop = FALSE; | |
4893 | last = -1; | |
4894 | ||
4895 | symdef = symdefs; | |
4896 | symdefend = symdef + c; | |
4897 | for (i = 0; symdef < symdefend; symdef++, i++) | |
4898 | { | |
4899 | struct elf_link_hash_entry *h; | |
4900 | bfd *element; | |
4901 | struct bfd_link_hash_entry *undefs_tail; | |
4902 | symindex mark; | |
4903 | ||
4904 | if (defined[i] || included[i]) | |
4905 | continue; | |
4906 | if (symdef->file_offset == last) | |
4907 | { | |
4908 | included[i] = TRUE; | |
4909 | continue; | |
4910 | } | |
4911 | ||
8387904d AM |
4912 | h = archive_symbol_lookup (abfd, info, symdef->name); |
4913 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
4914 | goto error_return; | |
0ad989f9 L |
4915 | |
4916 | if (h == NULL) | |
4917 | continue; | |
4918 | ||
4919 | if (h->root.type == bfd_link_hash_common) | |
4920 | { | |
4921 | /* We currently have a common symbol. The archive map contains | |
4922 | a reference to this symbol, so we may want to include it. We | |
4923 | only want to include it however, if this archive element | |
4924 | contains a definition of the symbol, not just another common | |
4925 | declaration of it. | |
4926 | ||
4927 | Unfortunately some archivers (including GNU ar) will put | |
4928 | declarations of common symbols into their archive maps, as | |
4929 | well as real definitions, so we cannot just go by the archive | |
4930 | map alone. Instead we must read in the element's symbol | |
4931 | table and check that to see what kind of symbol definition | |
4932 | this is. */ | |
4933 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
4934 | continue; | |
4935 | } | |
4936 | else if (h->root.type != bfd_link_hash_undefined) | |
4937 | { | |
4938 | if (h->root.type != bfd_link_hash_undefweak) | |
4939 | defined[i] = TRUE; | |
4940 | continue; | |
4941 | } | |
4942 | ||
4943 | /* We need to include this archive member. */ | |
4944 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
4945 | if (element == NULL) | |
4946 | goto error_return; | |
4947 | ||
4948 | if (! bfd_check_format (element, bfd_object)) | |
4949 | goto error_return; | |
4950 | ||
4951 | /* Doublecheck that we have not included this object | |
4952 | already--it should be impossible, but there may be | |
4953 | something wrong with the archive. */ | |
4954 | if (element->archive_pass != 0) | |
4955 | { | |
4956 | bfd_set_error (bfd_error_bad_value); | |
4957 | goto error_return; | |
4958 | } | |
4959 | element->archive_pass = 1; | |
4960 | ||
4961 | undefs_tail = info->hash->undefs_tail; | |
4962 | ||
4963 | if (! (*info->callbacks->add_archive_element) (info, element, | |
4964 | symdef->name)) | |
4965 | goto error_return; | |
4966 | if (! bfd_link_add_symbols (element, info)) | |
4967 | goto error_return; | |
4968 | ||
4969 | /* If there are any new undefined symbols, we need to make | |
4970 | another pass through the archive in order to see whether | |
4971 | they can be defined. FIXME: This isn't perfect, because | |
4972 | common symbols wind up on undefs_tail and because an | |
4973 | undefined symbol which is defined later on in this pass | |
4974 | does not require another pass. This isn't a bug, but it | |
4975 | does make the code less efficient than it could be. */ | |
4976 | if (undefs_tail != info->hash->undefs_tail) | |
4977 | loop = TRUE; | |
4978 | ||
4979 | /* Look backward to mark all symbols from this object file | |
4980 | which we have already seen in this pass. */ | |
4981 | mark = i; | |
4982 | do | |
4983 | { | |
4984 | included[mark] = TRUE; | |
4985 | if (mark == 0) | |
4986 | break; | |
4987 | --mark; | |
4988 | } | |
4989 | while (symdefs[mark].file_offset == symdef->file_offset); | |
4990 | ||
4991 | /* We mark subsequent symbols from this object file as we go | |
4992 | on through the loop. */ | |
4993 | last = symdef->file_offset; | |
4994 | } | |
4995 | } | |
4996 | while (loop); | |
4997 | ||
4998 | free (defined); | |
4999 | free (included); | |
5000 | ||
5001 | return TRUE; | |
5002 | ||
5003 | error_return: | |
5004 | if (defined != NULL) | |
5005 | free (defined); | |
5006 | if (included != NULL) | |
5007 | free (included); | |
5008 | return FALSE; | |
5009 | } | |
4ad4eba5 AM |
5010 | |
5011 | /* Given an ELF BFD, add symbols to the global hash table as | |
5012 | appropriate. */ | |
5013 | ||
5014 | bfd_boolean | |
5015 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
5016 | { | |
5017 | switch (bfd_get_format (abfd)) | |
5018 | { | |
5019 | case bfd_object: | |
5020 | return elf_link_add_object_symbols (abfd, info); | |
5021 | case bfd_archive: | |
5022 | return elf_link_add_archive_symbols (abfd, info); | |
5023 | default: | |
5024 | bfd_set_error (bfd_error_wrong_format); | |
5025 | return FALSE; | |
5026 | } | |
5027 | } | |
5a580b3a | 5028 | \f |
14b1c01e AM |
5029 | struct hash_codes_info |
5030 | { | |
5031 | unsigned long *hashcodes; | |
5032 | bfd_boolean error; | |
5033 | }; | |
a0c8462f | 5034 | |
5a580b3a AM |
5035 | /* This function will be called though elf_link_hash_traverse to store |
5036 | all hash value of the exported symbols in an array. */ | |
5037 | ||
5038 | static bfd_boolean | |
5039 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5040 | { | |
14b1c01e | 5041 | struct hash_codes_info *inf = data; |
5a580b3a AM |
5042 | const char *name; |
5043 | char *p; | |
5044 | unsigned long ha; | |
5045 | char *alc = NULL; | |
5046 | ||
5047 | if (h->root.type == bfd_link_hash_warning) | |
5048 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5049 | ||
5050 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
5051 | if (h->dynindx == -1) | |
5052 | return TRUE; | |
5053 | ||
5054 | name = h->root.root.string; | |
5055 | p = strchr (name, ELF_VER_CHR); | |
5056 | if (p != NULL) | |
5057 | { | |
5058 | alc = bfd_malloc (p - name + 1); | |
14b1c01e AM |
5059 | if (alc == NULL) |
5060 | { | |
5061 | inf->error = TRUE; | |
5062 | return FALSE; | |
5063 | } | |
5a580b3a AM |
5064 | memcpy (alc, name, p - name); |
5065 | alc[p - name] = '\0'; | |
5066 | name = alc; | |
5067 | } | |
5068 | ||
5069 | /* Compute the hash value. */ | |
5070 | ha = bfd_elf_hash (name); | |
5071 | ||
5072 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 5073 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
5074 | |
5075 | /* And store it in the struct so that we can put it in the hash table | |
5076 | later. */ | |
f6e332e6 | 5077 | h->u.elf_hash_value = ha; |
5a580b3a AM |
5078 | |
5079 | if (alc != NULL) | |
5080 | free (alc); | |
5081 | ||
5082 | return TRUE; | |
5083 | } | |
5084 | ||
fdc90cb4 JJ |
5085 | struct collect_gnu_hash_codes |
5086 | { | |
5087 | bfd *output_bfd; | |
5088 | const struct elf_backend_data *bed; | |
5089 | unsigned long int nsyms; | |
5090 | unsigned long int maskbits; | |
5091 | unsigned long int *hashcodes; | |
5092 | unsigned long int *hashval; | |
5093 | unsigned long int *indx; | |
5094 | unsigned long int *counts; | |
5095 | bfd_vma *bitmask; | |
5096 | bfd_byte *contents; | |
5097 | long int min_dynindx; | |
5098 | unsigned long int bucketcount; | |
5099 | unsigned long int symindx; | |
5100 | long int local_indx; | |
5101 | long int shift1, shift2; | |
5102 | unsigned long int mask; | |
14b1c01e | 5103 | bfd_boolean error; |
fdc90cb4 JJ |
5104 | }; |
5105 | ||
5106 | /* This function will be called though elf_link_hash_traverse to store | |
5107 | all hash value of the exported symbols in an array. */ | |
5108 | ||
5109 | static bfd_boolean | |
5110 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5111 | { | |
5112 | struct collect_gnu_hash_codes *s = data; | |
5113 | const char *name; | |
5114 | char *p; | |
5115 | unsigned long ha; | |
5116 | char *alc = NULL; | |
5117 | ||
5118 | if (h->root.type == bfd_link_hash_warning) | |
5119 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5120 | ||
5121 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
5122 | if (h->dynindx == -1) | |
5123 | return TRUE; | |
5124 | ||
5125 | /* Ignore also local symbols and undefined symbols. */ | |
5126 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5127 | return TRUE; | |
5128 | ||
5129 | name = h->root.root.string; | |
5130 | p = strchr (name, ELF_VER_CHR); | |
5131 | if (p != NULL) | |
5132 | { | |
5133 | alc = bfd_malloc (p - name + 1); | |
14b1c01e AM |
5134 | if (alc == NULL) |
5135 | { | |
5136 | s->error = TRUE; | |
5137 | return FALSE; | |
5138 | } | |
fdc90cb4 JJ |
5139 | memcpy (alc, name, p - name); |
5140 | alc[p - name] = '\0'; | |
5141 | name = alc; | |
5142 | } | |
5143 | ||
5144 | /* Compute the hash value. */ | |
5145 | ha = bfd_elf_gnu_hash (name); | |
5146 | ||
5147 | /* Store the found hash value in the array for compute_bucket_count, | |
5148 | and also for .dynsym reordering purposes. */ | |
5149 | s->hashcodes[s->nsyms] = ha; | |
5150 | s->hashval[h->dynindx] = ha; | |
5151 | ++s->nsyms; | |
5152 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5153 | s->min_dynindx = h->dynindx; | |
5154 | ||
5155 | if (alc != NULL) | |
5156 | free (alc); | |
5157 | ||
5158 | return TRUE; | |
5159 | } | |
5160 | ||
5161 | /* This function will be called though elf_link_hash_traverse to do | |
5162 | final dynaminc symbol renumbering. */ | |
5163 | ||
5164 | static bfd_boolean | |
5165 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5166 | { | |
5167 | struct collect_gnu_hash_codes *s = data; | |
5168 | unsigned long int bucket; | |
5169 | unsigned long int val; | |
5170 | ||
5171 | if (h->root.type == bfd_link_hash_warning) | |
5172 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5173 | ||
5174 | /* Ignore indirect symbols. */ | |
5175 | if (h->dynindx == -1) | |
5176 | return TRUE; | |
5177 | ||
5178 | /* Ignore also local symbols and undefined symbols. */ | |
5179 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5180 | { | |
5181 | if (h->dynindx >= s->min_dynindx) | |
5182 | h->dynindx = s->local_indx++; | |
5183 | return TRUE; | |
5184 | } | |
5185 | ||
5186 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5187 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5188 | & ((s->maskbits >> s->shift1) - 1); | |
5189 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5190 | s->bitmask[val] | |
5191 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5192 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5193 | if (s->counts[bucket] == 1) | |
5194 | /* Last element terminates the chain. */ | |
5195 | val |= 1; | |
5196 | bfd_put_32 (s->output_bfd, val, | |
5197 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5198 | --s->counts[bucket]; | |
5199 | h->dynindx = s->indx[bucket]++; | |
5200 | return TRUE; | |
5201 | } | |
5202 | ||
5203 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5204 | ||
5205 | bfd_boolean | |
5206 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5207 | { | |
5208 | return !(h->forced_local | |
5209 | || h->root.type == bfd_link_hash_undefined | |
5210 | || h->root.type == bfd_link_hash_undefweak | |
5211 | || ((h->root.type == bfd_link_hash_defined | |
5212 | || h->root.type == bfd_link_hash_defweak) | |
5213 | && h->root.u.def.section->output_section == NULL)); | |
5214 | } | |
5215 | ||
5a580b3a AM |
5216 | /* Array used to determine the number of hash table buckets to use |
5217 | based on the number of symbols there are. If there are fewer than | |
5218 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5219 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5220 | than 32771 buckets. */ | |
5221 | ||
5222 | static const size_t elf_buckets[] = | |
5223 | { | |
5224 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5225 | 16411, 32771, 0 | |
5226 | }; | |
5227 | ||
5228 | /* Compute bucket count for hashing table. We do not use a static set | |
5229 | of possible tables sizes anymore. Instead we determine for all | |
5230 | possible reasonable sizes of the table the outcome (i.e., the | |
5231 | number of collisions etc) and choose the best solution. The | |
5232 | weighting functions are not too simple to allow the table to grow | |
5233 | without bounds. Instead one of the weighting factors is the size. | |
5234 | Therefore the result is always a good payoff between few collisions | |
5235 | (= short chain lengths) and table size. */ | |
5236 | static size_t | |
d40f3da9 AM |
5237 | compute_bucket_count (struct bfd_link_info *info, |
5238 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, | |
5239 | unsigned long int nsyms, | |
5240 | int gnu_hash) | |
5a580b3a | 5241 | { |
5a580b3a | 5242 | size_t best_size = 0; |
5a580b3a | 5243 | unsigned long int i; |
5a580b3a | 5244 | |
5a580b3a AM |
5245 | /* We have a problem here. The following code to optimize the table |
5246 | size requires an integer type with more the 32 bits. If | |
5247 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5248 | #ifdef BFD_HOST_U_64_BIT | |
5249 | if (info->optimize) | |
5250 | { | |
5a580b3a AM |
5251 | size_t minsize; |
5252 | size_t maxsize; | |
5253 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a | 5254 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 5255 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 5256 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 5257 | unsigned long int *counts; |
d40f3da9 | 5258 | bfd_size_type amt; |
5a580b3a AM |
5259 | |
5260 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5261 | that the hashing table must at least have NSYMS/4 and at most | |
5262 | 2*NSYMS buckets. */ | |
5263 | minsize = nsyms / 4; | |
5264 | if (minsize == 0) | |
5265 | minsize = 1; | |
5266 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5267 | if (gnu_hash) |
5268 | { | |
5269 | if (minsize < 2) | |
5270 | minsize = 2; | |
5271 | if ((best_size & 31) == 0) | |
5272 | ++best_size; | |
5273 | } | |
5a580b3a AM |
5274 | |
5275 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5276 | since the size could be large. */ | |
5277 | amt = maxsize; | |
5278 | amt *= sizeof (unsigned long int); | |
5279 | counts = bfd_malloc (amt); | |
5280 | if (counts == NULL) | |
fdc90cb4 | 5281 | return 0; |
5a580b3a AM |
5282 | |
5283 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5284 | minimal chain length. The minor criteria is (of course) the size | |
5285 | of the table. */ | |
5286 | for (i = minsize; i < maxsize; ++i) | |
5287 | { | |
5288 | /* Walk through the array of hashcodes and count the collisions. */ | |
5289 | BFD_HOST_U_64_BIT max; | |
5290 | unsigned long int j; | |
5291 | unsigned long int fact; | |
5292 | ||
fdc90cb4 JJ |
5293 | if (gnu_hash && (i & 31) == 0) |
5294 | continue; | |
5295 | ||
5a580b3a AM |
5296 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5297 | ||
5298 | /* Determine how often each hash bucket is used. */ | |
5299 | for (j = 0; j < nsyms; ++j) | |
5300 | ++counts[hashcodes[j] % i]; | |
5301 | ||
5302 | /* For the weight function we need some information about the | |
5303 | pagesize on the target. This is information need not be 100% | |
5304 | accurate. Since this information is not available (so far) we | |
5305 | define it here to a reasonable default value. If it is crucial | |
5306 | to have a better value some day simply define this value. */ | |
5307 | # ifndef BFD_TARGET_PAGESIZE | |
5308 | # define BFD_TARGET_PAGESIZE (4096) | |
5309 | # endif | |
5310 | ||
fdc90cb4 JJ |
5311 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5312 | and the chains. */ | |
5313 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
5314 | |
5315 | # if 1 | |
5316 | /* Variant 1: optimize for short chains. We add the squares | |
5317 | of all the chain lengths (which favors many small chain | |
5318 | over a few long chains). */ | |
5319 | for (j = 0; j < i; ++j) | |
5320 | max += counts[j] * counts[j]; | |
5321 | ||
5322 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 5323 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5324 | max *= fact * fact; |
5325 | # else | |
5326 | /* Variant 2: Optimize a lot more for small table. Here we | |
5327 | also add squares of the size but we also add penalties for | |
5328 | empty slots (the +1 term). */ | |
5329 | for (j = 0; j < i; ++j) | |
5330 | max += (1 + counts[j]) * (1 + counts[j]); | |
5331 | ||
5332 | /* The overall size of the table is considered, but not as | |
5333 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 5334 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5335 | max *= fact; |
5336 | # endif | |
5337 | ||
5338 | /* Compare with current best results. */ | |
5339 | if (max < best_chlen) | |
5340 | { | |
5341 | best_chlen = max; | |
5342 | best_size = i; | |
5343 | } | |
5344 | } | |
5345 | ||
5346 | free (counts); | |
5347 | } | |
5348 | else | |
5349 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
5350 | { | |
5351 | /* This is the fallback solution if no 64bit type is available or if we | |
5352 | are not supposed to spend much time on optimizations. We select the | |
5353 | bucket count using a fixed set of numbers. */ | |
5354 | for (i = 0; elf_buckets[i] != 0; i++) | |
5355 | { | |
5356 | best_size = elf_buckets[i]; | |
fdc90cb4 | 5357 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
5358 | break; |
5359 | } | |
fdc90cb4 JJ |
5360 | if (gnu_hash && best_size < 2) |
5361 | best_size = 2; | |
5a580b3a AM |
5362 | } |
5363 | ||
5a580b3a AM |
5364 | return best_size; |
5365 | } | |
5366 | ||
5367 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
5368 | called by the ELF linker emulation before_allocation routine. We | |
5369 | must set the sizes of the sections before the linker sets the | |
5370 | addresses of the various sections. */ | |
5371 | ||
5372 | bfd_boolean | |
5373 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
5374 | const char *soname, | |
5375 | const char *rpath, | |
5376 | const char *filter_shlib, | |
5377 | const char * const *auxiliary_filters, | |
5378 | struct bfd_link_info *info, | |
5379 | asection **sinterpptr, | |
5380 | struct bfd_elf_version_tree *verdefs) | |
5381 | { | |
5382 | bfd_size_type soname_indx; | |
5383 | bfd *dynobj; | |
5384 | const struct elf_backend_data *bed; | |
5385 | struct elf_assign_sym_version_info asvinfo; | |
5386 | ||
5387 | *sinterpptr = NULL; | |
5388 | ||
5389 | soname_indx = (bfd_size_type) -1; | |
5390 | ||
5391 | if (!is_elf_hash_table (info->hash)) | |
5392 | return TRUE; | |
5393 | ||
6bfdb61b | 5394 | bed = get_elf_backend_data (output_bfd); |
5a580b3a AM |
5395 | if (info->execstack) |
5396 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
5397 | else if (info->noexecstack) | |
5398 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
5399 | else | |
5400 | { | |
5401 | bfd *inputobj; | |
5402 | asection *notesec = NULL; | |
5403 | int exec = 0; | |
5404 | ||
5405 | for (inputobj = info->input_bfds; | |
5406 | inputobj; | |
5407 | inputobj = inputobj->link_next) | |
5408 | { | |
5409 | asection *s; | |
5410 | ||
d457dcf6 | 5411 | if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED)) |
5a580b3a AM |
5412 | continue; |
5413 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
5414 | if (s) | |
5415 | { | |
5416 | if (s->flags & SEC_CODE) | |
5417 | exec = PF_X; | |
5418 | notesec = s; | |
5419 | } | |
6bfdb61b | 5420 | else if (bed->default_execstack) |
5a580b3a AM |
5421 | exec = PF_X; |
5422 | } | |
5423 | if (notesec) | |
5424 | { | |
5425 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
5426 | if (exec && info->relocatable | |
5427 | && notesec->output_section != bfd_abs_section_ptr) | |
5428 | notesec->output_section->flags |= SEC_CODE; | |
5429 | } | |
5430 | } | |
5431 | ||
5432 | /* Any syms created from now on start with -1 in | |
5433 | got.refcount/offset and plt.refcount/offset. */ | |
a6aa5195 AM |
5434 | elf_hash_table (info)->init_got_refcount |
5435 | = elf_hash_table (info)->init_got_offset; | |
5436 | elf_hash_table (info)->init_plt_refcount | |
5437 | = elf_hash_table (info)->init_plt_offset; | |
5a580b3a AM |
5438 | |
5439 | /* The backend may have to create some sections regardless of whether | |
5440 | we're dynamic or not. */ | |
5a580b3a AM |
5441 | if (bed->elf_backend_always_size_sections |
5442 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
5443 | return FALSE; | |
5444 | ||
eb3d5f3b JB |
5445 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) |
5446 | return FALSE; | |
5447 | ||
5a580b3a AM |
5448 | dynobj = elf_hash_table (info)->dynobj; |
5449 | ||
5450 | /* If there were no dynamic objects in the link, there is nothing to | |
5451 | do here. */ | |
5452 | if (dynobj == NULL) | |
5453 | return TRUE; | |
5454 | ||
5a580b3a AM |
5455 | if (elf_hash_table (info)->dynamic_sections_created) |
5456 | { | |
5457 | struct elf_info_failed eif; | |
5458 | struct elf_link_hash_entry *h; | |
5459 | asection *dynstr; | |
5460 | struct bfd_elf_version_tree *t; | |
5461 | struct bfd_elf_version_expr *d; | |
046183de | 5462 | asection *s; |
5a580b3a AM |
5463 | bfd_boolean all_defined; |
5464 | ||
5465 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
5466 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
5467 | ||
5468 | if (soname != NULL) | |
5469 | { | |
5470 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5471 | soname, TRUE); | |
5472 | if (soname_indx == (bfd_size_type) -1 | |
5473 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
5474 | return FALSE; | |
5475 | } | |
5476 | ||
5477 | if (info->symbolic) | |
5478 | { | |
5479 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
5480 | return FALSE; | |
5481 | info->flags |= DF_SYMBOLIC; | |
5482 | } | |
5483 | ||
5484 | if (rpath != NULL) | |
5485 | { | |
5486 | bfd_size_type indx; | |
5487 | ||
5488 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
5489 | TRUE); | |
5490 | if (indx == (bfd_size_type) -1 | |
5491 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
5492 | return FALSE; | |
5493 | ||
5494 | if (info->new_dtags) | |
5495 | { | |
5496 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
5497 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
5498 | return FALSE; | |
5499 | } | |
5500 | } | |
5501 | ||
5502 | if (filter_shlib != NULL) | |
5503 | { | |
5504 | bfd_size_type indx; | |
5505 | ||
5506 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5507 | filter_shlib, TRUE); | |
5508 | if (indx == (bfd_size_type) -1 | |
5509 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
5510 | return FALSE; | |
5511 | } | |
5512 | ||
5513 | if (auxiliary_filters != NULL) | |
5514 | { | |
5515 | const char * const *p; | |
5516 | ||
5517 | for (p = auxiliary_filters; *p != NULL; p++) | |
5518 | { | |
5519 | bfd_size_type indx; | |
5520 | ||
5521 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5522 | *p, TRUE); | |
5523 | if (indx == (bfd_size_type) -1 | |
5524 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
5525 | return FALSE; | |
5526 | } | |
5527 | } | |
5528 | ||
5529 | eif.info = info; | |
5530 | eif.verdefs = verdefs; | |
5531 | eif.failed = FALSE; | |
5532 | ||
5533 | /* If we are supposed to export all symbols into the dynamic symbol | |
5534 | table (this is not the normal case), then do so. */ | |
55255dae L |
5535 | if (info->export_dynamic |
5536 | || (info->executable && info->dynamic)) | |
5a580b3a AM |
5537 | { |
5538 | elf_link_hash_traverse (elf_hash_table (info), | |
5539 | _bfd_elf_export_symbol, | |
5540 | &eif); | |
5541 | if (eif.failed) | |
5542 | return FALSE; | |
5543 | } | |
5544 | ||
5545 | /* Make all global versions with definition. */ | |
5546 | for (t = verdefs; t != NULL; t = t->next) | |
5547 | for (d = t->globals.list; d != NULL; d = d->next) | |
5548 | if (!d->symver && d->symbol) | |
5549 | { | |
5550 | const char *verstr, *name; | |
5551 | size_t namelen, verlen, newlen; | |
5552 | char *newname, *p; | |
5553 | struct elf_link_hash_entry *newh; | |
5554 | ||
5555 | name = d->symbol; | |
5556 | namelen = strlen (name); | |
5557 | verstr = t->name; | |
5558 | verlen = strlen (verstr); | |
5559 | newlen = namelen + verlen + 3; | |
5560 | ||
5561 | newname = bfd_malloc (newlen); | |
5562 | if (newname == NULL) | |
5563 | return FALSE; | |
5564 | memcpy (newname, name, namelen); | |
5565 | ||
5566 | /* Check the hidden versioned definition. */ | |
5567 | p = newname + namelen; | |
5568 | *p++ = ELF_VER_CHR; | |
5569 | memcpy (p, verstr, verlen + 1); | |
5570 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5571 | newname, FALSE, FALSE, | |
5572 | FALSE); | |
5573 | if (newh == NULL | |
5574 | || (newh->root.type != bfd_link_hash_defined | |
5575 | && newh->root.type != bfd_link_hash_defweak)) | |
5576 | { | |
5577 | /* Check the default versioned definition. */ | |
5578 | *p++ = ELF_VER_CHR; | |
5579 | memcpy (p, verstr, verlen + 1); | |
5580 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5581 | newname, FALSE, FALSE, | |
5582 | FALSE); | |
5583 | } | |
5584 | free (newname); | |
5585 | ||
5586 | /* Mark this version if there is a definition and it is | |
5587 | not defined in a shared object. */ | |
5588 | if (newh != NULL | |
f5385ebf | 5589 | && !newh->def_dynamic |
5a580b3a AM |
5590 | && (newh->root.type == bfd_link_hash_defined |
5591 | || newh->root.type == bfd_link_hash_defweak)) | |
5592 | d->symver = 1; | |
5593 | } | |
5594 | ||
5595 | /* Attach all the symbols to their version information. */ | |
5596 | asvinfo.output_bfd = output_bfd; | |
5597 | asvinfo.info = info; | |
5598 | asvinfo.verdefs = verdefs; | |
5599 | asvinfo.failed = FALSE; | |
5600 | ||
5601 | elf_link_hash_traverse (elf_hash_table (info), | |
5602 | _bfd_elf_link_assign_sym_version, | |
5603 | &asvinfo); | |
5604 | if (asvinfo.failed) | |
5605 | return FALSE; | |
5606 | ||
5607 | if (!info->allow_undefined_version) | |
5608 | { | |
5609 | /* Check if all global versions have a definition. */ | |
5610 | all_defined = TRUE; | |
5611 | for (t = verdefs; t != NULL; t = t->next) | |
5612 | for (d = t->globals.list; d != NULL; d = d->next) | |
5613 | if (!d->symver && !d->script) | |
5614 | { | |
5615 | (*_bfd_error_handler) | |
5616 | (_("%s: undefined version: %s"), | |
5617 | d->pattern, t->name); | |
5618 | all_defined = FALSE; | |
5619 | } | |
5620 | ||
5621 | if (!all_defined) | |
5622 | { | |
5623 | bfd_set_error (bfd_error_bad_value); | |
5624 | return FALSE; | |
5625 | } | |
5626 | } | |
5627 | ||
5628 | /* Find all symbols which were defined in a dynamic object and make | |
5629 | the backend pick a reasonable value for them. */ | |
5630 | elf_link_hash_traverse (elf_hash_table (info), | |
5631 | _bfd_elf_adjust_dynamic_symbol, | |
5632 | &eif); | |
5633 | if (eif.failed) | |
5634 | return FALSE; | |
5635 | ||
5636 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 5637 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
5638 | now so that we know the final size of the .dynamic section. */ |
5639 | ||
5640 | /* If there are initialization and/or finalization functions to | |
5641 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
5642 | h = (info->init_function | |
5643 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5644 | info->init_function, FALSE, | |
5645 | FALSE, FALSE) | |
5646 | : NULL); | |
5647 | if (h != NULL | |
f5385ebf AM |
5648 | && (h->ref_regular |
5649 | || h->def_regular)) | |
5a580b3a AM |
5650 | { |
5651 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
5652 | return FALSE; | |
5653 | } | |
5654 | h = (info->fini_function | |
5655 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5656 | info->fini_function, FALSE, | |
5657 | FALSE, FALSE) | |
5658 | : NULL); | |
5659 | if (h != NULL | |
f5385ebf AM |
5660 | && (h->ref_regular |
5661 | || h->def_regular)) | |
5a580b3a AM |
5662 | { |
5663 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
5664 | return FALSE; | |
5665 | } | |
5666 | ||
046183de AM |
5667 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); |
5668 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5669 | { |
5670 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
5671 | if (! info->executable) | |
5672 | { | |
5673 | bfd *sub; | |
5674 | asection *o; | |
5675 | ||
5676 | for (sub = info->input_bfds; sub != NULL; | |
5677 | sub = sub->link_next) | |
3fcd97f1 JJ |
5678 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour) |
5679 | for (o = sub->sections; o != NULL; o = o->next) | |
5680 | if (elf_section_data (o)->this_hdr.sh_type | |
5681 | == SHT_PREINIT_ARRAY) | |
5682 | { | |
5683 | (*_bfd_error_handler) | |
5684 | (_("%B: .preinit_array section is not allowed in DSO"), | |
5685 | sub); | |
5686 | break; | |
5687 | } | |
5a580b3a AM |
5688 | |
5689 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5690 | return FALSE; | |
5691 | } | |
5692 | ||
5693 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
5694 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
5695 | return FALSE; | |
5696 | } | |
046183de AM |
5697 | s = bfd_get_section_by_name (output_bfd, ".init_array"); |
5698 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5699 | { |
5700 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
5701 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
5702 | return FALSE; | |
5703 | } | |
046183de AM |
5704 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); |
5705 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5706 | { |
5707 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
5708 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
5709 | return FALSE; | |
5710 | } | |
5711 | ||
5712 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5713 | /* If .dynstr is excluded from the link, we don't want any of | |
5714 | these tags. Strictly, we should be checking each section | |
5715 | individually; This quick check covers for the case where | |
5716 | someone does a /DISCARD/ : { *(*) }. */ | |
5717 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
5718 | { | |
5719 | bfd_size_type strsize; | |
5720 | ||
5721 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
fdc90cb4 JJ |
5722 | if ((info->emit_hash |
5723 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
5724 | || (info->emit_gnu_hash | |
5725 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
5a580b3a AM |
5726 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) |
5727 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
5728 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
5729 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
5730 | bed->s->sizeof_sym)) | |
5731 | return FALSE; | |
5732 | } | |
5733 | } | |
5734 | ||
5735 | /* The backend must work out the sizes of all the other dynamic | |
5736 | sections. */ | |
5737 | if (bed->elf_backend_size_dynamic_sections | |
5738 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
5739 | return FALSE; | |
5740 | ||
5741 | if (elf_hash_table (info)->dynamic_sections_created) | |
5742 | { | |
554220db | 5743 | unsigned long section_sym_count; |
5a580b3a | 5744 | asection *s; |
5a580b3a AM |
5745 | |
5746 | /* Set up the version definition section. */ | |
5747 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
5748 | BFD_ASSERT (s != NULL); | |
5749 | ||
5750 | /* We may have created additional version definitions if we are | |
5751 | just linking a regular application. */ | |
5752 | verdefs = asvinfo.verdefs; | |
5753 | ||
5754 | /* Skip anonymous version tag. */ | |
5755 | if (verdefs != NULL && verdefs->vernum == 0) | |
5756 | verdefs = verdefs->next; | |
5757 | ||
3e3b46e5 | 5758 | if (verdefs == NULL && !info->create_default_symver) |
8423293d | 5759 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5760 | else |
5761 | { | |
5762 | unsigned int cdefs; | |
5763 | bfd_size_type size; | |
5764 | struct bfd_elf_version_tree *t; | |
5765 | bfd_byte *p; | |
5766 | Elf_Internal_Verdef def; | |
5767 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
5768 | struct bfd_link_hash_entry *bh; |
5769 | struct elf_link_hash_entry *h; | |
5770 | const char *name; | |
5a580b3a AM |
5771 | |
5772 | cdefs = 0; | |
5773 | size = 0; | |
5774 | ||
5775 | /* Make space for the base version. */ | |
5776 | size += sizeof (Elf_External_Verdef); | |
5777 | size += sizeof (Elf_External_Verdaux); | |
5778 | ++cdefs; | |
5779 | ||
3e3b46e5 PB |
5780 | /* Make space for the default version. */ |
5781 | if (info->create_default_symver) | |
5782 | { | |
5783 | size += sizeof (Elf_External_Verdef); | |
5784 | ++cdefs; | |
5785 | } | |
5786 | ||
5a580b3a AM |
5787 | for (t = verdefs; t != NULL; t = t->next) |
5788 | { | |
5789 | struct bfd_elf_version_deps *n; | |
5790 | ||
5791 | size += sizeof (Elf_External_Verdef); | |
5792 | size += sizeof (Elf_External_Verdaux); | |
5793 | ++cdefs; | |
5794 | ||
5795 | for (n = t->deps; n != NULL; n = n->next) | |
5796 | size += sizeof (Elf_External_Verdaux); | |
5797 | } | |
5798 | ||
eea6121a AM |
5799 | s->size = size; |
5800 | s->contents = bfd_alloc (output_bfd, s->size); | |
5801 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5802 | return FALSE; |
5803 | ||
5804 | /* Fill in the version definition section. */ | |
5805 | ||
5806 | p = s->contents; | |
5807 | ||
5808 | def.vd_version = VER_DEF_CURRENT; | |
5809 | def.vd_flags = VER_FLG_BASE; | |
5810 | def.vd_ndx = 1; | |
5811 | def.vd_cnt = 1; | |
3e3b46e5 PB |
5812 | if (info->create_default_symver) |
5813 | { | |
5814 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
5815 | def.vd_next = sizeof (Elf_External_Verdef); | |
5816 | } | |
5817 | else | |
5818 | { | |
5819 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5820 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5821 | + sizeof (Elf_External_Verdaux)); | |
5822 | } | |
5a580b3a AM |
5823 | |
5824 | if (soname_indx != (bfd_size_type) -1) | |
5825 | { | |
5826 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5827 | soname_indx); | |
5828 | def.vd_hash = bfd_elf_hash (soname); | |
5829 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 5830 | name = soname; |
5a580b3a AM |
5831 | } |
5832 | else | |
5833 | { | |
5a580b3a AM |
5834 | bfd_size_type indx; |
5835 | ||
06084812 | 5836 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
5837 | def.vd_hash = bfd_elf_hash (name); |
5838 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5839 | name, FALSE); | |
5840 | if (indx == (bfd_size_type) -1) | |
5841 | return FALSE; | |
5842 | defaux.vda_name = indx; | |
5843 | } | |
5844 | defaux.vda_next = 0; | |
5845 | ||
5846 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5847 | (Elf_External_Verdef *) p); | |
5848 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
5849 | if (info->create_default_symver) |
5850 | { | |
5851 | /* Add a symbol representing this version. */ | |
5852 | bh = NULL; | |
5853 | if (! (_bfd_generic_link_add_one_symbol | |
5854 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5855 | 0, NULL, FALSE, | |
5856 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5857 | return FALSE; | |
5858 | h = (struct elf_link_hash_entry *) bh; | |
5859 | h->non_elf = 0; | |
5860 | h->def_regular = 1; | |
5861 | h->type = STT_OBJECT; | |
5862 | h->verinfo.vertree = NULL; | |
5863 | ||
5864 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
5865 | return FALSE; | |
5866 | ||
5867 | /* Create a duplicate of the base version with the same | |
5868 | aux block, but different flags. */ | |
5869 | def.vd_flags = 0; | |
5870 | def.vd_ndx = 2; | |
5871 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5872 | if (verdefs) | |
5873 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5874 | + sizeof (Elf_External_Verdaux)); | |
5875 | else | |
5876 | def.vd_next = 0; | |
5877 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5878 | (Elf_External_Verdef *) p); | |
5879 | p += sizeof (Elf_External_Verdef); | |
5880 | } | |
5a580b3a AM |
5881 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
5882 | (Elf_External_Verdaux *) p); | |
5883 | p += sizeof (Elf_External_Verdaux); | |
5884 | ||
5885 | for (t = verdefs; t != NULL; t = t->next) | |
5886 | { | |
5887 | unsigned int cdeps; | |
5888 | struct bfd_elf_version_deps *n; | |
5a580b3a AM |
5889 | |
5890 | cdeps = 0; | |
5891 | for (n = t->deps; n != NULL; n = n->next) | |
5892 | ++cdeps; | |
5893 | ||
5894 | /* Add a symbol representing this version. */ | |
5895 | bh = NULL; | |
5896 | if (! (_bfd_generic_link_add_one_symbol | |
5897 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5898 | 0, NULL, FALSE, | |
5899 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5900 | return FALSE; | |
5901 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
5902 | h->non_elf = 0; |
5903 | h->def_regular = 1; | |
5a580b3a AM |
5904 | h->type = STT_OBJECT; |
5905 | h->verinfo.vertree = t; | |
5906 | ||
c152c796 | 5907 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
5908 | return FALSE; |
5909 | ||
5910 | def.vd_version = VER_DEF_CURRENT; | |
5911 | def.vd_flags = 0; | |
5912 | if (t->globals.list == NULL | |
5913 | && t->locals.list == NULL | |
5914 | && ! t->used) | |
5915 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 5916 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
5917 | def.vd_cnt = cdeps + 1; |
5918 | def.vd_hash = bfd_elf_hash (t->name); | |
5919 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5920 | def.vd_next = 0; | |
5921 | if (t->next != NULL) | |
5922 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5923 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
5924 | ||
5925 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5926 | (Elf_External_Verdef *) p); | |
5927 | p += sizeof (Elf_External_Verdef); | |
5928 | ||
5929 | defaux.vda_name = h->dynstr_index; | |
5930 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5931 | h->dynstr_index); | |
5932 | defaux.vda_next = 0; | |
5933 | if (t->deps != NULL) | |
5934 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5935 | t->name_indx = defaux.vda_name; | |
5936 | ||
5937 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5938 | (Elf_External_Verdaux *) p); | |
5939 | p += sizeof (Elf_External_Verdaux); | |
5940 | ||
5941 | for (n = t->deps; n != NULL; n = n->next) | |
5942 | { | |
5943 | if (n->version_needed == NULL) | |
5944 | { | |
5945 | /* This can happen if there was an error in the | |
5946 | version script. */ | |
5947 | defaux.vda_name = 0; | |
5948 | } | |
5949 | else | |
5950 | { | |
5951 | defaux.vda_name = n->version_needed->name_indx; | |
5952 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5953 | defaux.vda_name); | |
5954 | } | |
5955 | if (n->next == NULL) | |
5956 | defaux.vda_next = 0; | |
5957 | else | |
5958 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5959 | ||
5960 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5961 | (Elf_External_Verdaux *) p); | |
5962 | p += sizeof (Elf_External_Verdaux); | |
5963 | } | |
5964 | } | |
5965 | ||
5966 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
5967 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
5968 | return FALSE; | |
5969 | ||
5970 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
5971 | } | |
5972 | ||
5973 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
5974 | { | |
5975 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
5976 | return FALSE; | |
5977 | } | |
5978 | else if (info->flags & DF_BIND_NOW) | |
5979 | { | |
5980 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
5981 | return FALSE; | |
5982 | } | |
5983 | ||
5984 | if (info->flags_1) | |
5985 | { | |
5986 | if (info->executable) | |
5987 | info->flags_1 &= ~ (DF_1_INITFIRST | |
5988 | | DF_1_NODELETE | |
5989 | | DF_1_NOOPEN); | |
5990 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
5991 | return FALSE; | |
5992 | } | |
5993 | ||
5994 | /* Work out the size of the version reference section. */ | |
5995 | ||
5996 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
5997 | BFD_ASSERT (s != NULL); | |
5998 | { | |
5999 | struct elf_find_verdep_info sinfo; | |
6000 | ||
6001 | sinfo.output_bfd = output_bfd; | |
6002 | sinfo.info = info; | |
6003 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
6004 | if (sinfo.vers == 0) | |
6005 | sinfo.vers = 1; | |
6006 | sinfo.failed = FALSE; | |
6007 | ||
6008 | elf_link_hash_traverse (elf_hash_table (info), | |
6009 | _bfd_elf_link_find_version_dependencies, | |
6010 | &sinfo); | |
14b1c01e AM |
6011 | if (sinfo.failed) |
6012 | return FALSE; | |
5a580b3a AM |
6013 | |
6014 | if (elf_tdata (output_bfd)->verref == NULL) | |
8423293d | 6015 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
6016 | else |
6017 | { | |
6018 | Elf_Internal_Verneed *t; | |
6019 | unsigned int size; | |
6020 | unsigned int crefs; | |
6021 | bfd_byte *p; | |
6022 | ||
6023 | /* Build the version definition section. */ | |
6024 | size = 0; | |
6025 | crefs = 0; | |
6026 | for (t = elf_tdata (output_bfd)->verref; | |
6027 | t != NULL; | |
6028 | t = t->vn_nextref) | |
6029 | { | |
6030 | Elf_Internal_Vernaux *a; | |
6031 | ||
6032 | size += sizeof (Elf_External_Verneed); | |
6033 | ++crefs; | |
6034 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6035 | size += sizeof (Elf_External_Vernaux); | |
6036 | } | |
6037 | ||
eea6121a AM |
6038 | s->size = size; |
6039 | s->contents = bfd_alloc (output_bfd, s->size); | |
5a580b3a AM |
6040 | if (s->contents == NULL) |
6041 | return FALSE; | |
6042 | ||
6043 | p = s->contents; | |
6044 | for (t = elf_tdata (output_bfd)->verref; | |
6045 | t != NULL; | |
6046 | t = t->vn_nextref) | |
6047 | { | |
6048 | unsigned int caux; | |
6049 | Elf_Internal_Vernaux *a; | |
6050 | bfd_size_type indx; | |
6051 | ||
6052 | caux = 0; | |
6053 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6054 | ++caux; | |
6055 | ||
6056 | t->vn_version = VER_NEED_CURRENT; | |
6057 | t->vn_cnt = caux; | |
6058 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6059 | elf_dt_name (t->vn_bfd) != NULL | |
6060 | ? elf_dt_name (t->vn_bfd) | |
06084812 | 6061 | : lbasename (t->vn_bfd->filename), |
5a580b3a AM |
6062 | FALSE); |
6063 | if (indx == (bfd_size_type) -1) | |
6064 | return FALSE; | |
6065 | t->vn_file = indx; | |
6066 | t->vn_aux = sizeof (Elf_External_Verneed); | |
6067 | if (t->vn_nextref == NULL) | |
6068 | t->vn_next = 0; | |
6069 | else | |
6070 | t->vn_next = (sizeof (Elf_External_Verneed) | |
6071 | + caux * sizeof (Elf_External_Vernaux)); | |
6072 | ||
6073 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
6074 | (Elf_External_Verneed *) p); | |
6075 | p += sizeof (Elf_External_Verneed); | |
6076 | ||
6077 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6078 | { | |
6079 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6080 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6081 | a->vna_nodename, FALSE); | |
6082 | if (indx == (bfd_size_type) -1) | |
6083 | return FALSE; | |
6084 | a->vna_name = indx; | |
6085 | if (a->vna_nextptr == NULL) | |
6086 | a->vna_next = 0; | |
6087 | else | |
6088 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6089 | ||
6090 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6091 | (Elf_External_Vernaux *) p); | |
6092 | p += sizeof (Elf_External_Vernaux); | |
6093 | } | |
6094 | } | |
6095 | ||
6096 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
6097 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
6098 | return FALSE; | |
6099 | ||
6100 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6101 | } | |
6102 | } | |
6103 | ||
8423293d AM |
6104 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
6105 | && elf_tdata (output_bfd)->cverdefs == 0) | |
6106 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
6107 | §ion_sym_count) == 0) | |
6108 | { | |
6109 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
6110 | s->flags |= SEC_EXCLUDE; | |
6111 | } | |
6112 | } | |
6113 | return TRUE; | |
6114 | } | |
6115 | ||
74541ad4 AM |
6116 | /* Find the first non-excluded output section. We'll use its |
6117 | section symbol for some emitted relocs. */ | |
6118 | void | |
6119 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
6120 | { | |
6121 | asection *s; | |
6122 | ||
6123 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6124 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
6125 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6126 | { | |
6127 | elf_hash_table (info)->text_index_section = s; | |
6128 | break; | |
6129 | } | |
6130 | } | |
6131 | ||
6132 | /* Find two non-excluded output sections, one for code, one for data. | |
6133 | We'll use their section symbols for some emitted relocs. */ | |
6134 | void | |
6135 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
6136 | { | |
6137 | asection *s; | |
6138 | ||
6139 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6140 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) | |
6141 | == (SEC_ALLOC | SEC_READONLY)) | |
6142 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6143 | { | |
6144 | elf_hash_table (info)->text_index_section = s; | |
6145 | break; | |
6146 | } | |
6147 | ||
6148 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6149 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) | |
6150 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6151 | { | |
6152 | elf_hash_table (info)->data_index_section = s; | |
6153 | break; | |
6154 | } | |
6155 | ||
6156 | if (elf_hash_table (info)->text_index_section == NULL) | |
6157 | elf_hash_table (info)->text_index_section | |
6158 | = elf_hash_table (info)->data_index_section; | |
6159 | } | |
6160 | ||
8423293d AM |
6161 | bfd_boolean |
6162 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
6163 | { | |
74541ad4 AM |
6164 | const struct elf_backend_data *bed; |
6165 | ||
8423293d AM |
6166 | if (!is_elf_hash_table (info->hash)) |
6167 | return TRUE; | |
6168 | ||
74541ad4 AM |
6169 | bed = get_elf_backend_data (output_bfd); |
6170 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
6171 | ||
8423293d AM |
6172 | if (elf_hash_table (info)->dynamic_sections_created) |
6173 | { | |
6174 | bfd *dynobj; | |
8423293d AM |
6175 | asection *s; |
6176 | bfd_size_type dynsymcount; | |
6177 | unsigned long section_sym_count; | |
8423293d AM |
6178 | unsigned int dtagcount; |
6179 | ||
6180 | dynobj = elf_hash_table (info)->dynobj; | |
6181 | ||
5a580b3a AM |
6182 | /* Assign dynsym indicies. In a shared library we generate a |
6183 | section symbol for each output section, which come first. | |
6184 | Next come all of the back-end allocated local dynamic syms, | |
6185 | followed by the rest of the global symbols. */ | |
6186 | ||
554220db AM |
6187 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, |
6188 | §ion_sym_count); | |
5a580b3a AM |
6189 | |
6190 | /* Work out the size of the symbol version section. */ | |
6191 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
6192 | BFD_ASSERT (s != NULL); | |
8423293d AM |
6193 | if (dynsymcount != 0 |
6194 | && (s->flags & SEC_EXCLUDE) == 0) | |
5a580b3a | 6195 | { |
eea6121a AM |
6196 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
6197 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
6198 | if (s->contents == NULL) |
6199 | return FALSE; | |
6200 | ||
6201 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
6202 | return FALSE; | |
6203 | } | |
6204 | ||
6205 | /* Set the size of the .dynsym and .hash sections. We counted | |
6206 | the number of dynamic symbols in elf_link_add_object_symbols. | |
6207 | We will build the contents of .dynsym and .hash when we build | |
6208 | the final symbol table, because until then we do not know the | |
6209 | correct value to give the symbols. We built the .dynstr | |
6210 | section as we went along in elf_link_add_object_symbols. */ | |
6211 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
6212 | BFD_ASSERT (s != NULL); | |
eea6121a | 6213 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a AM |
6214 | |
6215 | if (dynsymcount != 0) | |
6216 | { | |
554220db AM |
6217 | s->contents = bfd_alloc (output_bfd, s->size); |
6218 | if (s->contents == NULL) | |
6219 | return FALSE; | |
5a580b3a | 6220 | |
554220db AM |
6221 | /* The first entry in .dynsym is a dummy symbol. |
6222 | Clear all the section syms, in case we don't output them all. */ | |
6223 | ++section_sym_count; | |
6224 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a AM |
6225 | } |
6226 | ||
fdc90cb4 JJ |
6227 | elf_hash_table (info)->bucketcount = 0; |
6228 | ||
5a580b3a AM |
6229 | /* Compute the size of the hashing table. As a side effect this |
6230 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
6231 | if (info->emit_hash) |
6232 | { | |
6233 | unsigned long int *hashcodes; | |
14b1c01e | 6234 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
6235 | bfd_size_type amt; |
6236 | unsigned long int nsyms; | |
6237 | size_t bucketcount; | |
6238 | size_t hash_entry_size; | |
6239 | ||
6240 | /* Compute the hash values for all exported symbols. At the same | |
6241 | time store the values in an array so that we could use them for | |
6242 | optimizations. */ | |
6243 | amt = dynsymcount * sizeof (unsigned long int); | |
6244 | hashcodes = bfd_malloc (amt); | |
6245 | if (hashcodes == NULL) | |
6246 | return FALSE; | |
14b1c01e AM |
6247 | hashinf.hashcodes = hashcodes; |
6248 | hashinf.error = FALSE; | |
5a580b3a | 6249 | |
fdc90cb4 JJ |
6250 | /* Put all hash values in HASHCODES. */ |
6251 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
6252 | elf_collect_hash_codes, &hashinf); |
6253 | if (hashinf.error) | |
6254 | return FALSE; | |
5a580b3a | 6255 | |
14b1c01e | 6256 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
6257 | bucketcount |
6258 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
6259 | free (hashcodes); | |
6260 | ||
6261 | if (bucketcount == 0) | |
6262 | return FALSE; | |
5a580b3a | 6263 | |
fdc90cb4 JJ |
6264 | elf_hash_table (info)->bucketcount = bucketcount; |
6265 | ||
6266 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
6267 | BFD_ASSERT (s != NULL); | |
6268 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
6269 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
6270 | s->contents = bfd_zalloc (output_bfd, s->size); | |
6271 | if (s->contents == NULL) | |
6272 | return FALSE; | |
6273 | ||
6274 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
6275 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
6276 | s->contents + hash_entry_size); | |
6277 | } | |
6278 | ||
6279 | if (info->emit_gnu_hash) | |
6280 | { | |
6281 | size_t i, cnt; | |
6282 | unsigned char *contents; | |
6283 | struct collect_gnu_hash_codes cinfo; | |
6284 | bfd_size_type amt; | |
6285 | size_t bucketcount; | |
6286 | ||
6287 | memset (&cinfo, 0, sizeof (cinfo)); | |
6288 | ||
6289 | /* Compute the hash values for all exported symbols. At the same | |
6290 | time store the values in an array so that we could use them for | |
6291 | optimizations. */ | |
6292 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
6293 | cinfo.hashcodes = bfd_malloc (amt); | |
6294 | if (cinfo.hashcodes == NULL) | |
6295 | return FALSE; | |
6296 | ||
6297 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
6298 | cinfo.min_dynindx = -1; | |
6299 | cinfo.output_bfd = output_bfd; | |
6300 | cinfo.bed = bed; | |
6301 | ||
6302 | /* Put all hash values in HASHCODES. */ | |
6303 | elf_link_hash_traverse (elf_hash_table (info), | |
6304 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e AM |
6305 | if (cinfo.error) |
6306 | return FALSE; | |
fdc90cb4 JJ |
6307 | |
6308 | bucketcount | |
6309 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
6310 | ||
6311 | if (bucketcount == 0) | |
6312 | { | |
6313 | free (cinfo.hashcodes); | |
6314 | return FALSE; | |
6315 | } | |
6316 | ||
6317 | s = bfd_get_section_by_name (dynobj, ".gnu.hash"); | |
6318 | BFD_ASSERT (s != NULL); | |
6319 | ||
6320 | if (cinfo.nsyms == 0) | |
6321 | { | |
6322 | /* Empty .gnu.hash section is special. */ | |
6323 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
6324 | free (cinfo.hashcodes); | |
6325 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
6326 | contents = bfd_zalloc (output_bfd, s->size); | |
6327 | if (contents == NULL) | |
6328 | return FALSE; | |
6329 | s->contents = contents; | |
6330 | /* 1 empty bucket. */ | |
6331 | bfd_put_32 (output_bfd, 1, contents); | |
6332 | /* SYMIDX above the special symbol 0. */ | |
6333 | bfd_put_32 (output_bfd, 1, contents + 4); | |
6334 | /* Just one word for bitmask. */ | |
6335 | bfd_put_32 (output_bfd, 1, contents + 8); | |
6336 | /* Only hash fn bloom filter. */ | |
6337 | bfd_put_32 (output_bfd, 0, contents + 12); | |
6338 | /* No hashes are valid - empty bitmask. */ | |
6339 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
6340 | /* No hashes in the only bucket. */ | |
6341 | bfd_put_32 (output_bfd, 0, | |
6342 | contents + 16 + bed->s->arch_size / 8); | |
6343 | } | |
6344 | else | |
6345 | { | |
fdc90cb4 | 6346 | unsigned long int maskwords, maskbitslog2; |
0b33793d | 6347 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 JJ |
6348 | |
6349 | maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1; | |
6350 | if (maskbitslog2 < 3) | |
6351 | maskbitslog2 = 5; | |
6352 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
6353 | maskbitslog2 = maskbitslog2 + 3; | |
6354 | else | |
6355 | maskbitslog2 = maskbitslog2 + 2; | |
6356 | if (bed->s->arch_size == 64) | |
6357 | { | |
6358 | if (maskbitslog2 == 5) | |
6359 | maskbitslog2 = 6; | |
6360 | cinfo.shift1 = 6; | |
6361 | } | |
6362 | else | |
6363 | cinfo.shift1 = 5; | |
6364 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 6365 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
6366 | cinfo.maskbits = 1 << maskbitslog2; |
6367 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
6368 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
6369 | amt += maskwords * sizeof (bfd_vma); | |
6370 | cinfo.bitmask = bfd_malloc (amt); | |
6371 | if (cinfo.bitmask == NULL) | |
6372 | { | |
6373 | free (cinfo.hashcodes); | |
6374 | return FALSE; | |
6375 | } | |
6376 | ||
6377 | cinfo.counts = (void *) (cinfo.bitmask + maskwords); | |
6378 | cinfo.indx = cinfo.counts + bucketcount; | |
6379 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
6380 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
6381 | ||
6382 | /* Determine how often each hash bucket is used. */ | |
6383 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
6384 | for (i = 0; i < cinfo.nsyms; ++i) | |
6385 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
6386 | ||
6387 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
6388 | if (cinfo.counts[i] != 0) | |
6389 | { | |
6390 | cinfo.indx[i] = cnt; | |
6391 | cnt += cinfo.counts[i]; | |
6392 | } | |
6393 | BFD_ASSERT (cnt == dynsymcount); | |
6394 | cinfo.bucketcount = bucketcount; | |
6395 | cinfo.local_indx = cinfo.min_dynindx; | |
6396 | ||
6397 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
6398 | s->size += cinfo.maskbits / 8; | |
6399 | contents = bfd_zalloc (output_bfd, s->size); | |
6400 | if (contents == NULL) | |
6401 | { | |
6402 | free (cinfo.bitmask); | |
6403 | free (cinfo.hashcodes); | |
6404 | return FALSE; | |
6405 | } | |
6406 | ||
6407 | s->contents = contents; | |
6408 | bfd_put_32 (output_bfd, bucketcount, contents); | |
6409 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
6410 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
6411 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
6412 | contents += 16 + cinfo.maskbits / 8; | |
6413 | ||
6414 | for (i = 0; i < bucketcount; ++i) | |
6415 | { | |
6416 | if (cinfo.counts[i] == 0) | |
6417 | bfd_put_32 (output_bfd, 0, contents); | |
6418 | else | |
6419 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
6420 | contents += 4; | |
6421 | } | |
6422 | ||
6423 | cinfo.contents = contents; | |
6424 | ||
6425 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
6426 | elf_link_hash_traverse (elf_hash_table (info), | |
6427 | elf_renumber_gnu_hash_syms, &cinfo); | |
6428 | ||
6429 | contents = s->contents + 16; | |
6430 | for (i = 0; i < maskwords; ++i) | |
6431 | { | |
6432 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
6433 | contents); | |
6434 | contents += bed->s->arch_size / 8; | |
6435 | } | |
6436 | ||
6437 | free (cinfo.bitmask); | |
6438 | free (cinfo.hashcodes); | |
6439 | } | |
6440 | } | |
5a580b3a AM |
6441 | |
6442 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
6443 | BFD_ASSERT (s != NULL); | |
6444 | ||
4ad4eba5 | 6445 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 6446 | |
eea6121a | 6447 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
6448 | |
6449 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
6450 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
6451 | return FALSE; | |
6452 | } | |
6453 | ||
6454 | return TRUE; | |
6455 | } | |
4d269e42 AM |
6456 | \f |
6457 | /* Indicate that we are only retrieving symbol values from this | |
6458 | section. */ | |
6459 | ||
6460 | void | |
6461 | _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info) | |
6462 | { | |
6463 | if (is_elf_hash_table (info->hash)) | |
6464 | sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS; | |
6465 | _bfd_generic_link_just_syms (sec, info); | |
6466 | } | |
6467 | ||
6468 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ | |
6469 | ||
6470 | static void | |
6471 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
6472 | asection *sec) | |
6473 | { | |
6474 | BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE); | |
6475 | sec->sec_info_type = ELF_INFO_TYPE_NONE; | |
6476 | } | |
6477 | ||
6478 | /* Finish SHF_MERGE section merging. */ | |
6479 | ||
6480 | bfd_boolean | |
6481 | _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info) | |
6482 | { | |
6483 | bfd *ibfd; | |
6484 | asection *sec; | |
6485 | ||
6486 | if (!is_elf_hash_table (info->hash)) | |
6487 | return FALSE; | |
6488 | ||
6489 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
6490 | if ((ibfd->flags & DYNAMIC) == 0) | |
6491 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) | |
6492 | if ((sec->flags & SEC_MERGE) != 0 | |
6493 | && !bfd_is_abs_section (sec->output_section)) | |
6494 | { | |
6495 | struct bfd_elf_section_data *secdata; | |
6496 | ||
6497 | secdata = elf_section_data (sec); | |
6498 | if (! _bfd_add_merge_section (abfd, | |
6499 | &elf_hash_table (info)->merge_info, | |
6500 | sec, &secdata->sec_info)) | |
6501 | return FALSE; | |
6502 | else if (secdata->sec_info) | |
6503 | sec->sec_info_type = ELF_INFO_TYPE_MERGE; | |
6504 | } | |
6505 | ||
6506 | if (elf_hash_table (info)->merge_info != NULL) | |
6507 | _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info, | |
6508 | merge_sections_remove_hook); | |
6509 | return TRUE; | |
6510 | } | |
6511 | ||
6512 | /* Create an entry in an ELF linker hash table. */ | |
6513 | ||
6514 | struct bfd_hash_entry * | |
6515 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
6516 | struct bfd_hash_table *table, | |
6517 | const char *string) | |
6518 | { | |
6519 | /* Allocate the structure if it has not already been allocated by a | |
6520 | subclass. */ | |
6521 | if (entry == NULL) | |
6522 | { | |
6523 | entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); | |
6524 | if (entry == NULL) | |
6525 | return entry; | |
6526 | } | |
6527 | ||
6528 | /* Call the allocation method of the superclass. */ | |
6529 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
6530 | if (entry != NULL) | |
6531 | { | |
6532 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
6533 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
6534 | ||
6535 | /* Set local fields. */ | |
6536 | ret->indx = -1; | |
6537 | ret->dynindx = -1; | |
6538 | ret->got = htab->init_got_refcount; | |
6539 | ret->plt = htab->init_plt_refcount; | |
6540 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
6541 | - offsetof (struct elf_link_hash_entry, size))); | |
6542 | /* Assume that we have been called by a non-ELF symbol reader. | |
6543 | This flag is then reset by the code which reads an ELF input | |
6544 | file. This ensures that a symbol created by a non-ELF symbol | |
6545 | reader will have the flag set correctly. */ | |
6546 | ret->non_elf = 1; | |
6547 | } | |
6548 | ||
6549 | return entry; | |
6550 | } | |
6551 | ||
6552 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
6553 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
6554 | ||
6555 | void | |
6556 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
6557 | struct elf_link_hash_entry *dir, | |
6558 | struct elf_link_hash_entry *ind) | |
6559 | { | |
6560 | struct elf_link_hash_table *htab; | |
6561 | ||
6562 | /* Copy down any references that we may have already seen to the | |
6563 | symbol which just became indirect. */ | |
6564 | ||
6565 | dir->ref_dynamic |= ind->ref_dynamic; | |
6566 | dir->ref_regular |= ind->ref_regular; | |
6567 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
6568 | dir->non_got_ref |= ind->non_got_ref; | |
6569 | dir->needs_plt |= ind->needs_plt; | |
6570 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
6571 | ||
6572 | if (ind->root.type != bfd_link_hash_indirect) | |
6573 | return; | |
6574 | ||
6575 | /* Copy over the global and procedure linkage table refcount entries. | |
6576 | These may have been already set up by a check_relocs routine. */ | |
6577 | htab = elf_hash_table (info); | |
6578 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
6579 | { | |
6580 | if (dir->got.refcount < 0) | |
6581 | dir->got.refcount = 0; | |
6582 | dir->got.refcount += ind->got.refcount; | |
6583 | ind->got.refcount = htab->init_got_refcount.refcount; | |
6584 | } | |
6585 | ||
6586 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
6587 | { | |
6588 | if (dir->plt.refcount < 0) | |
6589 | dir->plt.refcount = 0; | |
6590 | dir->plt.refcount += ind->plt.refcount; | |
6591 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
6592 | } | |
6593 | ||
6594 | if (ind->dynindx != -1) | |
6595 | { | |
6596 | if (dir->dynindx != -1) | |
6597 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
6598 | dir->dynindx = ind->dynindx; | |
6599 | dir->dynstr_index = ind->dynstr_index; | |
6600 | ind->dynindx = -1; | |
6601 | ind->dynstr_index = 0; | |
6602 | } | |
6603 | } | |
6604 | ||
6605 | void | |
6606 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
6607 | struct elf_link_hash_entry *h, | |
6608 | bfd_boolean force_local) | |
6609 | { | |
6610 | h->plt = elf_hash_table (info)->init_plt_offset; | |
6611 | h->needs_plt = 0; | |
6612 | if (force_local) | |
6613 | { | |
6614 | h->forced_local = 1; | |
6615 | if (h->dynindx != -1) | |
6616 | { | |
6617 | h->dynindx = -1; | |
6618 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, | |
6619 | h->dynstr_index); | |
6620 | } | |
6621 | } | |
6622 | } | |
6623 | ||
6624 | /* Initialize an ELF linker hash table. */ | |
6625 | ||
6626 | bfd_boolean | |
6627 | _bfd_elf_link_hash_table_init | |
6628 | (struct elf_link_hash_table *table, | |
6629 | bfd *abfd, | |
6630 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
6631 | struct bfd_hash_table *, | |
6632 | const char *), | |
6633 | unsigned int entsize) | |
6634 | { | |
6635 | bfd_boolean ret; | |
6636 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; | |
6637 | ||
6638 | memset (table, 0, sizeof * table); | |
6639 | table->init_got_refcount.refcount = can_refcount - 1; | |
6640 | table->init_plt_refcount.refcount = can_refcount - 1; | |
6641 | table->init_got_offset.offset = -(bfd_vma) 1; | |
6642 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
6643 | /* The first dynamic symbol is a dummy. */ | |
6644 | table->dynsymcount = 1; | |
6645 | ||
6646 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
6647 | table->root.type = bfd_link_elf_hash_table; | |
6648 | ||
6649 | return ret; | |
6650 | } | |
6651 | ||
6652 | /* Create an ELF linker hash table. */ | |
6653 | ||
6654 | struct bfd_link_hash_table * | |
6655 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
6656 | { | |
6657 | struct elf_link_hash_table *ret; | |
6658 | bfd_size_type amt = sizeof (struct elf_link_hash_table); | |
6659 | ||
6660 | ret = bfd_malloc (amt); | |
6661 | if (ret == NULL) | |
6662 | return NULL; | |
6663 | ||
6664 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
6665 | sizeof (struct elf_link_hash_entry))) | |
6666 | { | |
6667 | free (ret); | |
6668 | return NULL; | |
6669 | } | |
6670 | ||
6671 | return &ret->root; | |
6672 | } | |
6673 | ||
6674 | /* This is a hook for the ELF emulation code in the generic linker to | |
6675 | tell the backend linker what file name to use for the DT_NEEDED | |
6676 | entry for a dynamic object. */ | |
6677 | ||
6678 | void | |
6679 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
6680 | { | |
6681 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6682 | && bfd_get_format (abfd) == bfd_object) | |
6683 | elf_dt_name (abfd) = name; | |
6684 | } | |
6685 | ||
6686 | int | |
6687 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
6688 | { | |
6689 | int lib_class; | |
6690 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6691 | && bfd_get_format (abfd) == bfd_object) | |
6692 | lib_class = elf_dyn_lib_class (abfd); | |
6693 | else | |
6694 | lib_class = 0; | |
6695 | return lib_class; | |
6696 | } | |
6697 | ||
6698 | void | |
6699 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
6700 | { | |
6701 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6702 | && bfd_get_format (abfd) == bfd_object) | |
6703 | elf_dyn_lib_class (abfd) = lib_class; | |
6704 | } | |
6705 | ||
6706 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
6707 | the linker ELF emulation code. */ | |
6708 | ||
6709 | struct bfd_link_needed_list * | |
6710 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
6711 | struct bfd_link_info *info) | |
6712 | { | |
6713 | if (! is_elf_hash_table (info->hash)) | |
6714 | return NULL; | |
6715 | return elf_hash_table (info)->needed; | |
6716 | } | |
6717 | ||
6718 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
6719 | hook for the linker ELF emulation code. */ | |
6720 | ||
6721 | struct bfd_link_needed_list * | |
6722 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
6723 | struct bfd_link_info *info) | |
6724 | { | |
6725 | if (! is_elf_hash_table (info->hash)) | |
6726 | return NULL; | |
6727 | return elf_hash_table (info)->runpath; | |
6728 | } | |
6729 | ||
6730 | /* Get the name actually used for a dynamic object for a link. This | |
6731 | is the SONAME entry if there is one. Otherwise, it is the string | |
6732 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
6733 | ||
6734 | const char * | |
6735 | bfd_elf_get_dt_soname (bfd *abfd) | |
6736 | { | |
6737 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6738 | && bfd_get_format (abfd) == bfd_object) | |
6739 | return elf_dt_name (abfd); | |
6740 | return NULL; | |
6741 | } | |
6742 | ||
6743 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
6744 | the ELF linker emulation code. */ | |
6745 | ||
6746 | bfd_boolean | |
6747 | bfd_elf_get_bfd_needed_list (bfd *abfd, | |
6748 | struct bfd_link_needed_list **pneeded) | |
6749 | { | |
6750 | asection *s; | |
6751 | bfd_byte *dynbuf = NULL; | |
6752 | int elfsec; | |
6753 | unsigned long shlink; | |
6754 | bfd_byte *extdyn, *extdynend; | |
6755 | size_t extdynsize; | |
6756 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
6757 | ||
6758 | *pneeded = NULL; | |
6759 | ||
6760 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
6761 | || bfd_get_format (abfd) != bfd_object) | |
6762 | return TRUE; | |
6763 | ||
6764 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
6765 | if (s == NULL || s->size == 0) | |
6766 | return TRUE; | |
6767 | ||
6768 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
6769 | goto error_return; | |
6770 | ||
6771 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
6772 | if (elfsec == -1) | |
6773 | goto error_return; | |
6774 | ||
6775 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 6776 | |
4d269e42 AM |
6777 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
6778 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
6779 | ||
6780 | extdyn = dynbuf; | |
6781 | extdynend = extdyn + s->size; | |
6782 | for (; extdyn < extdynend; extdyn += extdynsize) | |
6783 | { | |
6784 | Elf_Internal_Dyn dyn; | |
6785 | ||
6786 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
6787 | ||
6788 | if (dyn.d_tag == DT_NULL) | |
6789 | break; | |
6790 | ||
6791 | if (dyn.d_tag == DT_NEEDED) | |
6792 | { | |
6793 | const char *string; | |
6794 | struct bfd_link_needed_list *l; | |
6795 | unsigned int tagv = dyn.d_un.d_val; | |
6796 | bfd_size_type amt; | |
6797 | ||
6798 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
6799 | if (string == NULL) | |
6800 | goto error_return; | |
6801 | ||
6802 | amt = sizeof *l; | |
6803 | l = bfd_alloc (abfd, amt); | |
6804 | if (l == NULL) | |
6805 | goto error_return; | |
6806 | ||
6807 | l->by = abfd; | |
6808 | l->name = string; | |
6809 | l->next = *pneeded; | |
6810 | *pneeded = l; | |
6811 | } | |
6812 | } | |
6813 | ||
6814 | free (dynbuf); | |
6815 | ||
6816 | return TRUE; | |
6817 | ||
6818 | error_return: | |
6819 | if (dynbuf != NULL) | |
6820 | free (dynbuf); | |
6821 | return FALSE; | |
6822 | } | |
6823 | ||
6824 | struct elf_symbuf_symbol | |
6825 | { | |
6826 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
6827 | unsigned char st_info; /* Type and binding attributes */ | |
6828 | unsigned char st_other; /* Visibilty, and target specific */ | |
6829 | }; | |
6830 | ||
6831 | struct elf_symbuf_head | |
6832 | { | |
6833 | struct elf_symbuf_symbol *ssym; | |
6834 | bfd_size_type count; | |
6835 | unsigned int st_shndx; | |
6836 | }; | |
6837 | ||
6838 | struct elf_symbol | |
6839 | { | |
6840 | union | |
6841 | { | |
6842 | Elf_Internal_Sym *isym; | |
6843 | struct elf_symbuf_symbol *ssym; | |
6844 | } u; | |
6845 | const char *name; | |
6846 | }; | |
6847 | ||
6848 | /* Sort references to symbols by ascending section number. */ | |
6849 | ||
6850 | static int | |
6851 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
6852 | { | |
6853 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
6854 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
6855 | ||
6856 | return s1->st_shndx - s2->st_shndx; | |
6857 | } | |
6858 | ||
6859 | static int | |
6860 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
6861 | { | |
6862 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
6863 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
6864 | return strcmp (s1->name, s2->name); | |
6865 | } | |
6866 | ||
6867 | static struct elf_symbuf_head * | |
6868 | elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf) | |
6869 | { | |
14b1c01e | 6870 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
6871 | struct elf_symbuf_symbol *ssym; |
6872 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
6873 | bfd_size_type i, shndx_count; | |
6874 | ||
14b1c01e | 6875 | indbuf = bfd_malloc2 (symcount, sizeof (*indbuf)); |
4d269e42 AM |
6876 | if (indbuf == NULL) |
6877 | return NULL; | |
6878 | ||
6879 | for (ind = indbuf, i = 0; i < symcount; i++) | |
6880 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
6881 | *ind++ = &isymbuf[i]; | |
6882 | indbufend = ind; | |
6883 | ||
6884 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
6885 | elf_sort_elf_symbol); | |
6886 | ||
6887 | shndx_count = 0; | |
6888 | if (indbufend > indbuf) | |
6889 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
6890 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
6891 | shndx_count++; | |
6892 | ||
6893 | ssymbuf = bfd_malloc ((shndx_count + 1) * sizeof (*ssymbuf) | |
6894 | + (indbufend - indbuf) * sizeof (*ssymbuf)); | |
6895 | if (ssymbuf == NULL) | |
6896 | { | |
6897 | free (indbuf); | |
6898 | return NULL; | |
6899 | } | |
6900 | ||
6901 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count); | |
6902 | ssymbuf->ssym = NULL; | |
6903 | ssymbuf->count = shndx_count; | |
6904 | ssymbuf->st_shndx = 0; | |
6905 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
6906 | { | |
6907 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
6908 | { | |
6909 | ssymhead++; | |
6910 | ssymhead->ssym = ssym; | |
6911 | ssymhead->count = 0; | |
6912 | ssymhead->st_shndx = (*ind)->st_shndx; | |
6913 | } | |
6914 | ssym->st_name = (*ind)->st_name; | |
6915 | ssym->st_info = (*ind)->st_info; | |
6916 | ssym->st_other = (*ind)->st_other; | |
6917 | ssymhead->count++; | |
6918 | } | |
6919 | BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count); | |
6920 | ||
6921 | free (indbuf); | |
6922 | return ssymbuf; | |
6923 | } | |
6924 | ||
6925 | /* Check if 2 sections define the same set of local and global | |
6926 | symbols. */ | |
6927 | ||
8f317e31 | 6928 | static bfd_boolean |
4d269e42 AM |
6929 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
6930 | struct bfd_link_info *info) | |
6931 | { | |
6932 | bfd *bfd1, *bfd2; | |
6933 | const struct elf_backend_data *bed1, *bed2; | |
6934 | Elf_Internal_Shdr *hdr1, *hdr2; | |
6935 | bfd_size_type symcount1, symcount2; | |
6936 | Elf_Internal_Sym *isymbuf1, *isymbuf2; | |
6937 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
6938 | Elf_Internal_Sym *isym, *isymend; | |
6939 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
6940 | bfd_size_type count1, count2, i; | |
6941 | int shndx1, shndx2; | |
6942 | bfd_boolean result; | |
6943 | ||
6944 | bfd1 = sec1->owner; | |
6945 | bfd2 = sec2->owner; | |
6946 | ||
4d269e42 AM |
6947 | /* Both sections have to be in ELF. */ |
6948 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
6949 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
6950 | return FALSE; | |
6951 | ||
6952 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
6953 | return FALSE; | |
6954 | ||
4d269e42 AM |
6955 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
6956 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
6957 | if (shndx1 == -1 || shndx2 == -1) | |
6958 | return FALSE; | |
6959 | ||
6960 | bed1 = get_elf_backend_data (bfd1); | |
6961 | bed2 = get_elf_backend_data (bfd2); | |
6962 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
6963 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
6964 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
6965 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
6966 | ||
6967 | if (symcount1 == 0 || symcount2 == 0) | |
6968 | return FALSE; | |
6969 | ||
6970 | result = FALSE; | |
6971 | isymbuf1 = NULL; | |
6972 | isymbuf2 = NULL; | |
6973 | ssymbuf1 = elf_tdata (bfd1)->symbuf; | |
6974 | ssymbuf2 = elf_tdata (bfd2)->symbuf; | |
6975 | ||
6976 | if (ssymbuf1 == NULL) | |
6977 | { | |
6978 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
6979 | NULL, NULL, NULL); | |
6980 | if (isymbuf1 == NULL) | |
6981 | goto done; | |
6982 | ||
6983 | if (!info->reduce_memory_overheads) | |
6984 | elf_tdata (bfd1)->symbuf = ssymbuf1 | |
6985 | = elf_create_symbuf (symcount1, isymbuf1); | |
6986 | } | |
6987 | ||
6988 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
6989 | { | |
6990 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
6991 | NULL, NULL, NULL); | |
6992 | if (isymbuf2 == NULL) | |
6993 | goto done; | |
6994 | ||
6995 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
6996 | elf_tdata (bfd2)->symbuf = ssymbuf2 | |
6997 | = elf_create_symbuf (symcount2, isymbuf2); | |
6998 | } | |
6999 | ||
7000 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
7001 | { | |
7002 | /* Optimized faster version. */ | |
7003 | bfd_size_type lo, hi, mid; | |
7004 | struct elf_symbol *symp; | |
7005 | struct elf_symbuf_symbol *ssym, *ssymend; | |
7006 | ||
7007 | lo = 0; | |
7008 | hi = ssymbuf1->count; | |
7009 | ssymbuf1++; | |
7010 | count1 = 0; | |
7011 | while (lo < hi) | |
7012 | { | |
7013 | mid = (lo + hi) / 2; | |
7014 | if ((unsigned int) shndx1 < ssymbuf1[mid].st_shndx) | |
7015 | hi = mid; | |
7016 | else if ((unsigned int) shndx1 > ssymbuf1[mid].st_shndx) | |
7017 | lo = mid + 1; | |
7018 | else | |
7019 | { | |
7020 | count1 = ssymbuf1[mid].count; | |
7021 | ssymbuf1 += mid; | |
7022 | break; | |
7023 | } | |
7024 | } | |
7025 | ||
7026 | lo = 0; | |
7027 | hi = ssymbuf2->count; | |
7028 | ssymbuf2++; | |
7029 | count2 = 0; | |
7030 | while (lo < hi) | |
7031 | { | |
7032 | mid = (lo + hi) / 2; | |
7033 | if ((unsigned int) shndx2 < ssymbuf2[mid].st_shndx) | |
7034 | hi = mid; | |
7035 | else if ((unsigned int) shndx2 > ssymbuf2[mid].st_shndx) | |
7036 | lo = mid + 1; | |
7037 | else | |
7038 | { | |
7039 | count2 = ssymbuf2[mid].count; | |
7040 | ssymbuf2 += mid; | |
7041 | break; | |
7042 | } | |
7043 | } | |
7044 | ||
7045 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7046 | goto done; | |
7047 | ||
7048 | symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol)); | |
7049 | symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol)); | |
7050 | if (symtable1 == NULL || symtable2 == NULL) | |
7051 | goto done; | |
7052 | ||
7053 | symp = symtable1; | |
7054 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
7055 | ssym < ssymend; ssym++, symp++) | |
7056 | { | |
7057 | symp->u.ssym = ssym; | |
7058 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
7059 | hdr1->sh_link, | |
7060 | ssym->st_name); | |
7061 | } | |
7062 | ||
7063 | symp = symtable2; | |
7064 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
7065 | ssym < ssymend; ssym++, symp++) | |
7066 | { | |
7067 | symp->u.ssym = ssym; | |
7068 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
7069 | hdr2->sh_link, | |
7070 | ssym->st_name); | |
7071 | } | |
7072 | ||
7073 | /* Sort symbol by name. */ | |
7074 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7075 | elf_sym_name_compare); | |
7076 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7077 | elf_sym_name_compare); | |
7078 | ||
7079 | for (i = 0; i < count1; i++) | |
7080 | /* Two symbols must have the same binding, type and name. */ | |
7081 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
7082 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
7083 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7084 | goto done; | |
7085 | ||
7086 | result = TRUE; | |
7087 | goto done; | |
7088 | } | |
7089 | ||
7090 | symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
7091 | symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
7092 | if (symtable1 == NULL || symtable2 == NULL) | |
7093 | goto done; | |
7094 | ||
7095 | /* Count definitions in the section. */ | |
7096 | count1 = 0; | |
7097 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
7098 | if (isym->st_shndx == (unsigned int) shndx1) | |
7099 | symtable1[count1++].u.isym = isym; | |
7100 | ||
7101 | count2 = 0; | |
7102 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
7103 | if (isym->st_shndx == (unsigned int) shndx2) | |
7104 | symtable2[count2++].u.isym = isym; | |
7105 | ||
7106 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7107 | goto done; | |
7108 | ||
7109 | for (i = 0; i < count1; i++) | |
7110 | symtable1[i].name | |
7111 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
7112 | symtable1[i].u.isym->st_name); | |
7113 | ||
7114 | for (i = 0; i < count2; i++) | |
7115 | symtable2[i].name | |
7116 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
7117 | symtable2[i].u.isym->st_name); | |
7118 | ||
7119 | /* Sort symbol by name. */ | |
7120 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7121 | elf_sym_name_compare); | |
7122 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7123 | elf_sym_name_compare); | |
7124 | ||
7125 | for (i = 0; i < count1; i++) | |
7126 | /* Two symbols must have the same binding, type and name. */ | |
7127 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
7128 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
7129 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7130 | goto done; | |
7131 | ||
7132 | result = TRUE; | |
7133 | ||
7134 | done: | |
7135 | if (symtable1) | |
7136 | free (symtable1); | |
7137 | if (symtable2) | |
7138 | free (symtable2); | |
7139 | if (isymbuf1) | |
7140 | free (isymbuf1); | |
7141 | if (isymbuf2) | |
7142 | free (isymbuf2); | |
7143 | ||
7144 | return result; | |
7145 | } | |
7146 | ||
7147 | /* Return TRUE if 2 section types are compatible. */ | |
7148 | ||
7149 | bfd_boolean | |
7150 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
7151 | bfd *bbfd, const asection *bsec) | |
7152 | { | |
7153 | if (asec == NULL | |
7154 | || bsec == NULL | |
7155 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
7156 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
7157 | return TRUE; | |
7158 | ||
7159 | return elf_section_type (asec) == elf_section_type (bsec); | |
7160 | } | |
7161 | \f | |
c152c796 AM |
7162 | /* Final phase of ELF linker. */ |
7163 | ||
7164 | /* A structure we use to avoid passing large numbers of arguments. */ | |
7165 | ||
7166 | struct elf_final_link_info | |
7167 | { | |
7168 | /* General link information. */ | |
7169 | struct bfd_link_info *info; | |
7170 | /* Output BFD. */ | |
7171 | bfd *output_bfd; | |
7172 | /* Symbol string table. */ | |
7173 | struct bfd_strtab_hash *symstrtab; | |
7174 | /* .dynsym section. */ | |
7175 | asection *dynsym_sec; | |
7176 | /* .hash section. */ | |
7177 | asection *hash_sec; | |
7178 | /* symbol version section (.gnu.version). */ | |
7179 | asection *symver_sec; | |
7180 | /* Buffer large enough to hold contents of any section. */ | |
7181 | bfd_byte *contents; | |
7182 | /* Buffer large enough to hold external relocs of any section. */ | |
7183 | void *external_relocs; | |
7184 | /* Buffer large enough to hold internal relocs of any section. */ | |
7185 | Elf_Internal_Rela *internal_relocs; | |
7186 | /* Buffer large enough to hold external local symbols of any input | |
7187 | BFD. */ | |
7188 | bfd_byte *external_syms; | |
7189 | /* And a buffer for symbol section indices. */ | |
7190 | Elf_External_Sym_Shndx *locsym_shndx; | |
7191 | /* Buffer large enough to hold internal local symbols of any input | |
7192 | BFD. */ | |
7193 | Elf_Internal_Sym *internal_syms; | |
7194 | /* Array large enough to hold a symbol index for each local symbol | |
7195 | of any input BFD. */ | |
7196 | long *indices; | |
7197 | /* Array large enough to hold a section pointer for each local | |
7198 | symbol of any input BFD. */ | |
7199 | asection **sections; | |
7200 | /* Buffer to hold swapped out symbols. */ | |
7201 | bfd_byte *symbuf; | |
7202 | /* And one for symbol section indices. */ | |
7203 | Elf_External_Sym_Shndx *symshndxbuf; | |
7204 | /* Number of swapped out symbols in buffer. */ | |
7205 | size_t symbuf_count; | |
7206 | /* Number of symbols which fit in symbuf. */ | |
7207 | size_t symbuf_size; | |
7208 | /* And same for symshndxbuf. */ | |
7209 | size_t shndxbuf_size; | |
7210 | }; | |
7211 | ||
7212 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
7213 | ||
7214 | struct elf_outext_info | |
7215 | { | |
7216 | bfd_boolean failed; | |
7217 | bfd_boolean localsyms; | |
7218 | struct elf_final_link_info *finfo; | |
7219 | }; | |
7220 | ||
d9352518 DB |
7221 | |
7222 | /* Support for evaluating a complex relocation. | |
7223 | ||
7224 | Complex relocations are generalized, self-describing relocations. The | |
7225 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 7226 | relocations themselves. |
d9352518 DB |
7227 | |
7228 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
7229 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
7230 | information (start bit, end bit, word width, etc) into the addend. This | |
7231 | information is extracted from CGEN-generated operand tables within gas. | |
7232 | ||
7233 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
7234 | internal) representing prefix-notation expressions, including but not | |
7235 | limited to those sorts of expressions normally encoded as addends in the | |
7236 | addend field. The symbol mangling format is: | |
7237 | ||
7238 | <node> := <literal> | |
7239 | | <unary-operator> ':' <node> | |
7240 | | <binary-operator> ':' <node> ':' <node> | |
7241 | ; | |
7242 | ||
7243 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
7244 | | 'S' <digits=N> ':' <N character section name> | |
7245 | | '#' <hexdigits> | |
7246 | ; | |
7247 | ||
7248 | <binary-operator> := as in C | |
7249 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
7250 | ||
7251 | static void | |
a0c8462f AM |
7252 | set_symbol_value (bfd *bfd_with_globals, |
7253 | Elf_Internal_Sym *isymbuf, | |
7254 | size_t locsymcount, | |
7255 | size_t symidx, | |
7256 | bfd_vma val) | |
d9352518 | 7257 | { |
8977835c AM |
7258 | struct elf_link_hash_entry **sym_hashes; |
7259 | struct elf_link_hash_entry *h; | |
7260 | size_t extsymoff = locsymcount; | |
d9352518 | 7261 | |
8977835c | 7262 | if (symidx < locsymcount) |
d9352518 | 7263 | { |
8977835c AM |
7264 | Elf_Internal_Sym *sym; |
7265 | ||
7266 | sym = isymbuf + symidx; | |
7267 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
7268 | { | |
7269 | /* It is a local symbol: move it to the | |
7270 | "absolute" section and give it a value. */ | |
7271 | sym->st_shndx = SHN_ABS; | |
7272 | sym->st_value = val; | |
7273 | return; | |
7274 | } | |
7275 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
7276 | extsymoff = 0; | |
d9352518 | 7277 | } |
8977835c AM |
7278 | |
7279 | /* It is a global symbol: set its link type | |
7280 | to "defined" and give it a value. */ | |
7281 | ||
7282 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
7283 | h = sym_hashes [symidx - extsymoff]; | |
7284 | while (h->root.type == bfd_link_hash_indirect | |
7285 | || h->root.type == bfd_link_hash_warning) | |
7286 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7287 | h->root.type = bfd_link_hash_defined; | |
7288 | h->root.u.def.value = val; | |
7289 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
7290 | } |
7291 | ||
a0c8462f AM |
7292 | static bfd_boolean |
7293 | resolve_symbol (const char *name, | |
7294 | bfd *input_bfd, | |
7295 | struct elf_final_link_info *finfo, | |
7296 | bfd_vma *result, | |
7297 | Elf_Internal_Sym *isymbuf, | |
7298 | size_t locsymcount) | |
d9352518 | 7299 | { |
a0c8462f AM |
7300 | Elf_Internal_Sym *sym; |
7301 | struct bfd_link_hash_entry *global_entry; | |
7302 | const char *candidate = NULL; | |
7303 | Elf_Internal_Shdr *symtab_hdr; | |
7304 | size_t i; | |
7305 | ||
d9352518 DB |
7306 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
7307 | ||
7308 | for (i = 0; i < locsymcount; ++ i) | |
7309 | { | |
8977835c | 7310 | sym = isymbuf + i; |
d9352518 DB |
7311 | |
7312 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
7313 | continue; | |
7314 | ||
7315 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
7316 | symtab_hdr->sh_link, | |
7317 | sym->st_name); | |
7318 | #ifdef DEBUG | |
0f02bbd9 AM |
7319 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
7320 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
7321 | #endif |
7322 | if (candidate && strcmp (candidate, name) == 0) | |
7323 | { | |
0f02bbd9 | 7324 | asection *sec = finfo->sections [i]; |
d9352518 | 7325 | |
0f02bbd9 AM |
7326 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
7327 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 7328 | #ifdef DEBUG |
0f02bbd9 AM |
7329 | printf ("Found symbol with value %8.8lx\n", |
7330 | (unsigned long) *result); | |
d9352518 DB |
7331 | #endif |
7332 | return TRUE; | |
7333 | } | |
7334 | } | |
7335 | ||
7336 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
a0c8462f AM |
7337 | global_entry = bfd_link_hash_lookup (finfo->info->hash, name, |
7338 | FALSE, FALSE, TRUE); | |
d9352518 DB |
7339 | if (!global_entry) |
7340 | return FALSE; | |
a0c8462f | 7341 | |
d9352518 DB |
7342 | if (global_entry->type == bfd_link_hash_defined |
7343 | || global_entry->type == bfd_link_hash_defweak) | |
7344 | { | |
a0c8462f AM |
7345 | *result = (global_entry->u.def.value |
7346 | + global_entry->u.def.section->output_section->vma | |
7347 | + global_entry->u.def.section->output_offset); | |
d9352518 | 7348 | #ifdef DEBUG |
0f02bbd9 AM |
7349 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
7350 | global_entry->root.string, (unsigned long) *result); | |
d9352518 DB |
7351 | #endif |
7352 | return TRUE; | |
a0c8462f | 7353 | } |
d9352518 | 7354 | |
d9352518 DB |
7355 | return FALSE; |
7356 | } | |
7357 | ||
7358 | static bfd_boolean | |
a0c8462f AM |
7359 | resolve_section (const char *name, |
7360 | asection *sections, | |
7361 | bfd_vma *result) | |
d9352518 | 7362 | { |
a0c8462f AM |
7363 | asection *curr; |
7364 | unsigned int len; | |
d9352518 | 7365 | |
a0c8462f | 7366 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
7367 | if (strcmp (curr->name, name) == 0) |
7368 | { | |
7369 | *result = curr->vma; | |
7370 | return TRUE; | |
7371 | } | |
7372 | ||
7373 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
a0c8462f | 7374 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
7375 | { |
7376 | len = strlen (curr->name); | |
a0c8462f | 7377 | if (len > strlen (name)) |
d9352518 DB |
7378 | continue; |
7379 | ||
7380 | if (strncmp (curr->name, name, len) == 0) | |
7381 | { | |
7382 | if (strncmp (".end", name + len, 4) == 0) | |
7383 | { | |
7384 | *result = curr->vma + curr->size; | |
7385 | return TRUE; | |
7386 | } | |
7387 | ||
7388 | /* Insert more pseudo-section names here, if you like. */ | |
7389 | } | |
7390 | } | |
a0c8462f | 7391 | |
d9352518 DB |
7392 | return FALSE; |
7393 | } | |
7394 | ||
7395 | static void | |
a0c8462f | 7396 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 7397 | { |
a0c8462f AM |
7398 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
7399 | reftype, name); | |
d9352518 DB |
7400 | } |
7401 | ||
7402 | static bfd_boolean | |
a0c8462f AM |
7403 | eval_symbol (bfd_vma *result, |
7404 | const char **symp, | |
7405 | bfd *input_bfd, | |
7406 | struct elf_final_link_info *finfo, | |
7407 | bfd_vma dot, | |
7408 | Elf_Internal_Sym *isymbuf, | |
7409 | size_t locsymcount, | |
7410 | int signed_p) | |
d9352518 | 7411 | { |
4b93929b NC |
7412 | size_t len; |
7413 | size_t symlen; | |
a0c8462f AM |
7414 | bfd_vma a; |
7415 | bfd_vma b; | |
4b93929b | 7416 | char symbuf[4096]; |
0f02bbd9 | 7417 | const char *sym = *symp; |
a0c8462f AM |
7418 | const char *symend; |
7419 | bfd_boolean symbol_is_section = FALSE; | |
d9352518 DB |
7420 | |
7421 | len = strlen (sym); | |
7422 | symend = sym + len; | |
7423 | ||
4b93929b | 7424 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
7425 | { |
7426 | bfd_set_error (bfd_error_invalid_operation); | |
7427 | return FALSE; | |
7428 | } | |
a0c8462f | 7429 | |
d9352518 DB |
7430 | switch (* sym) |
7431 | { | |
7432 | case '.': | |
0f02bbd9 AM |
7433 | *result = dot; |
7434 | *symp = sym + 1; | |
d9352518 DB |
7435 | return TRUE; |
7436 | ||
7437 | case '#': | |
0f02bbd9 AM |
7438 | ++sym; |
7439 | *result = strtoul (sym, (char **) symp, 16); | |
d9352518 DB |
7440 | return TRUE; |
7441 | ||
7442 | case 'S': | |
7443 | symbol_is_section = TRUE; | |
a0c8462f | 7444 | case 's': |
0f02bbd9 AM |
7445 | ++sym; |
7446 | symlen = strtol (sym, (char **) symp, 10); | |
7447 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 7448 | |
4b93929b | 7449 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
7450 | { |
7451 | bfd_set_error (bfd_error_invalid_operation); | |
7452 | return FALSE; | |
7453 | } | |
7454 | ||
7455 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 7456 | symbuf[symlen] = '\0'; |
0f02bbd9 | 7457 | *symp = sym + symlen; |
a0c8462f AM |
7458 | |
7459 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
7460 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
7461 | interpretation here; section means "try section first", not "must be a | |
7462 | section", and likewise with symbol. */ | |
7463 | ||
a0c8462f | 7464 | if (symbol_is_section) |
d9352518 | 7465 | { |
8977835c AM |
7466 | if (!resolve_section (symbuf, finfo->output_bfd->sections, result) |
7467 | && !resolve_symbol (symbuf, input_bfd, finfo, result, | |
7468 | isymbuf, locsymcount)) | |
d9352518 DB |
7469 | { |
7470 | undefined_reference ("section", symbuf); | |
7471 | return FALSE; | |
7472 | } | |
a0c8462f AM |
7473 | } |
7474 | else | |
d9352518 | 7475 | { |
8977835c AM |
7476 | if (!resolve_symbol (symbuf, input_bfd, finfo, result, |
7477 | isymbuf, locsymcount) | |
7478 | && !resolve_section (symbuf, finfo->output_bfd->sections, | |
7479 | result)) | |
d9352518 DB |
7480 | { |
7481 | undefined_reference ("symbol", symbuf); | |
7482 | return FALSE; | |
7483 | } | |
7484 | } | |
7485 | ||
7486 | return TRUE; | |
a0c8462f | 7487 | |
d9352518 DB |
7488 | /* All that remains are operators. */ |
7489 | ||
7490 | #define UNARY_OP(op) \ | |
7491 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
7492 | { \ | |
7493 | sym += strlen (#op); \ | |
a0c8462f AM |
7494 | if (*sym == ':') \ |
7495 | ++sym; \ | |
0f02bbd9 AM |
7496 | *symp = sym; \ |
7497 | if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \ | |
7498 | isymbuf, locsymcount, signed_p)) \ | |
a0c8462f AM |
7499 | return FALSE; \ |
7500 | if (signed_p) \ | |
0f02bbd9 | 7501 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
7502 | else \ |
7503 | *result = op a; \ | |
d9352518 DB |
7504 | return TRUE; \ |
7505 | } | |
7506 | ||
7507 | #define BINARY_OP(op) \ | |
7508 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
7509 | { \ | |
7510 | sym += strlen (#op); \ | |
a0c8462f AM |
7511 | if (*sym == ':') \ |
7512 | ++sym; \ | |
0f02bbd9 AM |
7513 | *symp = sym; \ |
7514 | if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \ | |
7515 | isymbuf, locsymcount, signed_p)) \ | |
a0c8462f | 7516 | return FALSE; \ |
0f02bbd9 AM |
7517 | ++*symp; \ |
7518 | if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \ | |
7519 | isymbuf, locsymcount, signed_p)) \ | |
a0c8462f AM |
7520 | return FALSE; \ |
7521 | if (signed_p) \ | |
0f02bbd9 | 7522 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
7523 | else \ |
7524 | *result = a op b; \ | |
d9352518 DB |
7525 | return TRUE; \ |
7526 | } | |
7527 | ||
7528 | default: | |
7529 | UNARY_OP (0-); | |
7530 | BINARY_OP (<<); | |
7531 | BINARY_OP (>>); | |
7532 | BINARY_OP (==); | |
7533 | BINARY_OP (!=); | |
7534 | BINARY_OP (<=); | |
7535 | BINARY_OP (>=); | |
7536 | BINARY_OP (&&); | |
7537 | BINARY_OP (||); | |
7538 | UNARY_OP (~); | |
7539 | UNARY_OP (!); | |
7540 | BINARY_OP (*); | |
7541 | BINARY_OP (/); | |
7542 | BINARY_OP (%); | |
7543 | BINARY_OP (^); | |
7544 | BINARY_OP (|); | |
7545 | BINARY_OP (&); | |
7546 | BINARY_OP (+); | |
7547 | BINARY_OP (-); | |
7548 | BINARY_OP (<); | |
7549 | BINARY_OP (>); | |
7550 | #undef UNARY_OP | |
7551 | #undef BINARY_OP | |
7552 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
7553 | bfd_set_error (bfd_error_invalid_operation); | |
7554 | return FALSE; | |
7555 | } | |
7556 | } | |
7557 | ||
d9352518 | 7558 | static void |
a0c8462f AM |
7559 | put_value (bfd_vma size, |
7560 | unsigned long chunksz, | |
7561 | bfd *input_bfd, | |
7562 | bfd_vma x, | |
7563 | bfd_byte *location) | |
d9352518 DB |
7564 | { |
7565 | location += (size - chunksz); | |
7566 | ||
a0c8462f | 7567 | for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8)) |
d9352518 DB |
7568 | { |
7569 | switch (chunksz) | |
7570 | { | |
7571 | default: | |
7572 | case 0: | |
7573 | abort (); | |
7574 | case 1: | |
7575 | bfd_put_8 (input_bfd, x, location); | |
7576 | break; | |
7577 | case 2: | |
7578 | bfd_put_16 (input_bfd, x, location); | |
7579 | break; | |
7580 | case 4: | |
7581 | bfd_put_32 (input_bfd, x, location); | |
7582 | break; | |
7583 | case 8: | |
7584 | #ifdef BFD64 | |
7585 | bfd_put_64 (input_bfd, x, location); | |
7586 | #else | |
7587 | abort (); | |
7588 | #endif | |
7589 | break; | |
7590 | } | |
7591 | } | |
7592 | } | |
7593 | ||
a0c8462f AM |
7594 | static bfd_vma |
7595 | get_value (bfd_vma size, | |
7596 | unsigned long chunksz, | |
7597 | bfd *input_bfd, | |
7598 | bfd_byte *location) | |
d9352518 DB |
7599 | { |
7600 | bfd_vma x = 0; | |
7601 | ||
a0c8462f | 7602 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
7603 | { |
7604 | switch (chunksz) | |
7605 | { | |
7606 | default: | |
7607 | case 0: | |
7608 | abort (); | |
7609 | case 1: | |
7610 | x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location); | |
7611 | break; | |
7612 | case 2: | |
7613 | x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location); | |
7614 | break; | |
7615 | case 4: | |
7616 | x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location); | |
7617 | break; | |
7618 | case 8: | |
7619 | #ifdef BFD64 | |
7620 | x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location); | |
7621 | #else | |
7622 | abort (); | |
7623 | #endif | |
7624 | break; | |
7625 | } | |
7626 | } | |
7627 | return x; | |
7628 | } | |
7629 | ||
a0c8462f AM |
7630 | static void |
7631 | decode_complex_addend (unsigned long *start, /* in bits */ | |
7632 | unsigned long *oplen, /* in bits */ | |
7633 | unsigned long *len, /* in bits */ | |
7634 | unsigned long *wordsz, /* in bytes */ | |
7635 | unsigned long *chunksz, /* in bytes */ | |
7636 | unsigned long *lsb0_p, | |
7637 | unsigned long *signed_p, | |
7638 | unsigned long *trunc_p, | |
7639 | unsigned long encoded) | |
d9352518 DB |
7640 | { |
7641 | * start = encoded & 0x3F; | |
7642 | * len = (encoded >> 6) & 0x3F; | |
7643 | * oplen = (encoded >> 12) & 0x3F; | |
7644 | * wordsz = (encoded >> 18) & 0xF; | |
7645 | * chunksz = (encoded >> 22) & 0xF; | |
7646 | * lsb0_p = (encoded >> 27) & 1; | |
7647 | * signed_p = (encoded >> 28) & 1; | |
7648 | * trunc_p = (encoded >> 29) & 1; | |
7649 | } | |
7650 | ||
cdfeee4f | 7651 | bfd_reloc_status_type |
0f02bbd9 | 7652 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
cdfeee4f | 7653 | asection *input_section ATTRIBUTE_UNUSED, |
0f02bbd9 AM |
7654 | bfd_byte *contents, |
7655 | Elf_Internal_Rela *rel, | |
7656 | bfd_vma relocation) | |
d9352518 | 7657 | { |
0f02bbd9 AM |
7658 | bfd_vma shift, x, mask; |
7659 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 7660 | bfd_reloc_status_type r; |
d9352518 DB |
7661 | |
7662 | /* Perform this reloc, since it is complex. | |
7663 | (this is not to say that it necessarily refers to a complex | |
7664 | symbol; merely that it is a self-describing CGEN based reloc. | |
7665 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 7666 | word size, etc) encoded within it.). */ |
d9352518 | 7667 | |
a0c8462f AM |
7668 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
7669 | &chunksz, &lsb0_p, &signed_p, | |
7670 | &trunc_p, rel->r_addend); | |
d9352518 DB |
7671 | |
7672 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
7673 | ||
7674 | if (lsb0_p) | |
7675 | shift = (start + 1) - len; | |
7676 | else | |
7677 | shift = (8 * wordsz) - (start + len); | |
7678 | ||
a0c8462f | 7679 | x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset); |
d9352518 DB |
7680 | |
7681 | #ifdef DEBUG | |
7682 | printf ("Doing complex reloc: " | |
7683 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
7684 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
7685 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
7686 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
7687 | oplen, x, mask, relocation); | |
7688 | #endif | |
7689 | ||
cdfeee4f | 7690 | r = bfd_reloc_ok; |
d9352518 | 7691 | if (! trunc_p) |
cdfeee4f AM |
7692 | /* Now do an overflow check. */ |
7693 | r = bfd_check_overflow ((signed_p | |
7694 | ? complain_overflow_signed | |
7695 | : complain_overflow_unsigned), | |
7696 | len, 0, (8 * wordsz), | |
7697 | relocation); | |
a0c8462f | 7698 | |
d9352518 DB |
7699 | /* Do the deed. */ |
7700 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
7701 | ||
7702 | #ifdef DEBUG | |
7703 | printf (" relocation: %8.8lx\n" | |
7704 | " shifted mask: %8.8lx\n" | |
7705 | " shifted/masked reloc: %8.8lx\n" | |
7706 | " result: %8.8lx\n", | |
a0c8462f | 7707 | relocation, (mask << shift), |
d9352518 DB |
7708 | ((relocation & mask) << shift), x); |
7709 | #endif | |
7710 | put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset); | |
cdfeee4f | 7711 | return r; |
d9352518 DB |
7712 | } |
7713 | ||
c152c796 AM |
7714 | /* When performing a relocatable link, the input relocations are |
7715 | preserved. But, if they reference global symbols, the indices | |
7716 | referenced must be updated. Update all the relocations in | |
7717 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ | |
7718 | ||
7719 | static void | |
7720 | elf_link_adjust_relocs (bfd *abfd, | |
7721 | Elf_Internal_Shdr *rel_hdr, | |
7722 | unsigned int count, | |
7723 | struct elf_link_hash_entry **rel_hash) | |
7724 | { | |
7725 | unsigned int i; | |
7726 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7727 | bfd_byte *erela; | |
7728 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
7729 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
7730 | bfd_vma r_type_mask; | |
7731 | int r_sym_shift; | |
7732 | ||
7733 | if (rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
7734 | { | |
7735 | swap_in = bed->s->swap_reloc_in; | |
7736 | swap_out = bed->s->swap_reloc_out; | |
7737 | } | |
7738 | else if (rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
7739 | { | |
7740 | swap_in = bed->s->swap_reloca_in; | |
7741 | swap_out = bed->s->swap_reloca_out; | |
7742 | } | |
7743 | else | |
7744 | abort (); | |
7745 | ||
7746 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
7747 | abort (); | |
7748 | ||
7749 | if (bed->s->arch_size == 32) | |
7750 | { | |
7751 | r_type_mask = 0xff; | |
7752 | r_sym_shift = 8; | |
7753 | } | |
7754 | else | |
7755 | { | |
7756 | r_type_mask = 0xffffffff; | |
7757 | r_sym_shift = 32; | |
7758 | } | |
7759 | ||
7760 | erela = rel_hdr->contents; | |
7761 | for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) | |
7762 | { | |
7763 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
7764 | unsigned int j; | |
7765 | ||
7766 | if (*rel_hash == NULL) | |
7767 | continue; | |
7768 | ||
7769 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
7770 | ||
7771 | (*swap_in) (abfd, erela, irela); | |
7772 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
7773 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
7774 | | (irela[j].r_info & r_type_mask)); | |
7775 | (*swap_out) (abfd, irela, erela); | |
7776 | } | |
7777 | } | |
7778 | ||
7779 | struct elf_link_sort_rela | |
7780 | { | |
7781 | union { | |
7782 | bfd_vma offset; | |
7783 | bfd_vma sym_mask; | |
7784 | } u; | |
7785 | enum elf_reloc_type_class type; | |
7786 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
7787 | Elf_Internal_Rela rela[1]; | |
7788 | }; | |
7789 | ||
7790 | static int | |
7791 | elf_link_sort_cmp1 (const void *A, const void *B) | |
7792 | { | |
7793 | const struct elf_link_sort_rela *a = A; | |
7794 | const struct elf_link_sort_rela *b = B; | |
7795 | int relativea, relativeb; | |
7796 | ||
7797 | relativea = a->type == reloc_class_relative; | |
7798 | relativeb = b->type == reloc_class_relative; | |
7799 | ||
7800 | if (relativea < relativeb) | |
7801 | return 1; | |
7802 | if (relativea > relativeb) | |
7803 | return -1; | |
7804 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
7805 | return -1; | |
7806 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
7807 | return 1; | |
7808 | if (a->rela->r_offset < b->rela->r_offset) | |
7809 | return -1; | |
7810 | if (a->rela->r_offset > b->rela->r_offset) | |
7811 | return 1; | |
7812 | return 0; | |
7813 | } | |
7814 | ||
7815 | static int | |
7816 | elf_link_sort_cmp2 (const void *A, const void *B) | |
7817 | { | |
7818 | const struct elf_link_sort_rela *a = A; | |
7819 | const struct elf_link_sort_rela *b = B; | |
7820 | int copya, copyb; | |
7821 | ||
7822 | if (a->u.offset < b->u.offset) | |
7823 | return -1; | |
7824 | if (a->u.offset > b->u.offset) | |
7825 | return 1; | |
7826 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); | |
7827 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
7828 | if (copya < copyb) | |
7829 | return -1; | |
7830 | if (copya > copyb) | |
7831 | return 1; | |
7832 | if (a->rela->r_offset < b->rela->r_offset) | |
7833 | return -1; | |
7834 | if (a->rela->r_offset > b->rela->r_offset) | |
7835 | return 1; | |
7836 | return 0; | |
7837 | } | |
7838 | ||
7839 | static size_t | |
7840 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
7841 | { | |
3410fea8 | 7842 | asection *dynamic_relocs; |
fc66a176 L |
7843 | asection *rela_dyn; |
7844 | asection *rel_dyn; | |
c152c796 AM |
7845 | bfd_size_type count, size; |
7846 | size_t i, ret, sort_elt, ext_size; | |
7847 | bfd_byte *sort, *s_non_relative, *p; | |
7848 | struct elf_link_sort_rela *sq; | |
7849 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7850 | int i2e = bed->s->int_rels_per_ext_rel; | |
7851 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
7852 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
7853 | struct bfd_link_order *lo; | |
7854 | bfd_vma r_sym_mask; | |
3410fea8 | 7855 | bfd_boolean use_rela; |
c152c796 | 7856 | |
3410fea8 NC |
7857 | /* Find a dynamic reloc section. */ |
7858 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
7859 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
7860 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
7861 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 7862 | { |
3410fea8 NC |
7863 | bfd_boolean use_rela_initialised = FALSE; |
7864 | ||
7865 | /* This is just here to stop gcc from complaining. | |
7866 | It's initialization checking code is not perfect. */ | |
7867 | use_rela = TRUE; | |
7868 | ||
7869 | /* Both sections are present. Examine the sizes | |
7870 | of the indirect sections to help us choose. */ | |
7871 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
7872 | if (lo->type == bfd_indirect_link_order) | |
7873 | { | |
7874 | asection *o = lo->u.indirect.section; | |
7875 | ||
7876 | if ((o->size % bed->s->sizeof_rela) == 0) | |
7877 | { | |
7878 | if ((o->size % bed->s->sizeof_rel) == 0) | |
7879 | /* Section size is divisible by both rel and rela sizes. | |
7880 | It is of no help to us. */ | |
7881 | ; | |
7882 | else | |
7883 | { | |
7884 | /* Section size is only divisible by rela. */ | |
7885 | if (use_rela_initialised && (use_rela == FALSE)) | |
7886 | { | |
7887 | _bfd_error_handler | |
7888 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7889 | bfd_set_error (bfd_error_invalid_operation); | |
7890 | return 0; | |
7891 | } | |
7892 | else | |
7893 | { | |
7894 | use_rela = TRUE; | |
7895 | use_rela_initialised = TRUE; | |
7896 | } | |
7897 | } | |
7898 | } | |
7899 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
7900 | { | |
7901 | /* Section size is only divisible by rel. */ | |
7902 | if (use_rela_initialised && (use_rela == TRUE)) | |
7903 | { | |
7904 | _bfd_error_handler | |
7905 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7906 | bfd_set_error (bfd_error_invalid_operation); | |
7907 | return 0; | |
7908 | } | |
7909 | else | |
7910 | { | |
7911 | use_rela = FALSE; | |
7912 | use_rela_initialised = TRUE; | |
7913 | } | |
7914 | } | |
7915 | else | |
7916 | { | |
7917 | /* The section size is not divisible by either - something is wrong. */ | |
7918 | _bfd_error_handler | |
7919 | (_("%B: Unable to sort relocs - they are of an unknown size"), abfd); | |
7920 | bfd_set_error (bfd_error_invalid_operation); | |
7921 | return 0; | |
7922 | } | |
7923 | } | |
7924 | ||
7925 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
7926 | if (lo->type == bfd_indirect_link_order) | |
7927 | { | |
7928 | asection *o = lo->u.indirect.section; | |
7929 | ||
7930 | if ((o->size % bed->s->sizeof_rela) == 0) | |
7931 | { | |
7932 | if ((o->size % bed->s->sizeof_rel) == 0) | |
7933 | /* Section size is divisible by both rel and rela sizes. | |
7934 | It is of no help to us. */ | |
7935 | ; | |
7936 | else | |
7937 | { | |
7938 | /* Section size is only divisible by rela. */ | |
7939 | if (use_rela_initialised && (use_rela == FALSE)) | |
7940 | { | |
7941 | _bfd_error_handler | |
7942 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7943 | bfd_set_error (bfd_error_invalid_operation); | |
7944 | return 0; | |
7945 | } | |
7946 | else | |
7947 | { | |
7948 | use_rela = TRUE; | |
7949 | use_rela_initialised = TRUE; | |
7950 | } | |
7951 | } | |
7952 | } | |
7953 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
7954 | { | |
7955 | /* Section size is only divisible by rel. */ | |
7956 | if (use_rela_initialised && (use_rela == TRUE)) | |
7957 | { | |
7958 | _bfd_error_handler | |
7959 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7960 | bfd_set_error (bfd_error_invalid_operation); | |
7961 | return 0; | |
7962 | } | |
7963 | else | |
7964 | { | |
7965 | use_rela = FALSE; | |
7966 | use_rela_initialised = TRUE; | |
7967 | } | |
7968 | } | |
7969 | else | |
7970 | { | |
7971 | /* The section size is not divisible by either - something is wrong. */ | |
7972 | _bfd_error_handler | |
7973 | (_("%B: Unable to sort relocs - they are of an unknown size"), abfd); | |
7974 | bfd_set_error (bfd_error_invalid_operation); | |
7975 | return 0; | |
7976 | } | |
7977 | } | |
7978 | ||
7979 | if (! use_rela_initialised) | |
7980 | /* Make a guess. */ | |
7981 | use_rela = TRUE; | |
c152c796 | 7982 | } |
fc66a176 L |
7983 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
7984 | use_rela = TRUE; | |
7985 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 7986 | use_rela = FALSE; |
c152c796 | 7987 | else |
fc66a176 | 7988 | return 0; |
3410fea8 NC |
7989 | |
7990 | if (use_rela) | |
c152c796 | 7991 | { |
3410fea8 | 7992 | dynamic_relocs = rela_dyn; |
c152c796 AM |
7993 | ext_size = bed->s->sizeof_rela; |
7994 | swap_in = bed->s->swap_reloca_in; | |
7995 | swap_out = bed->s->swap_reloca_out; | |
7996 | } | |
3410fea8 NC |
7997 | else |
7998 | { | |
7999 | dynamic_relocs = rel_dyn; | |
8000 | ext_size = bed->s->sizeof_rel; | |
8001 | swap_in = bed->s->swap_reloc_in; | |
8002 | swap_out = bed->s->swap_reloc_out; | |
8003 | } | |
c152c796 AM |
8004 | |
8005 | size = 0; | |
3410fea8 | 8006 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 8007 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 8008 | size += lo->u.indirect.section->size; |
c152c796 | 8009 | |
3410fea8 | 8010 | if (size != dynamic_relocs->size) |
c152c796 AM |
8011 | return 0; |
8012 | ||
8013 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
8014 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
8015 | |
8016 | count = dynamic_relocs->size / ext_size; | |
c152c796 | 8017 | sort = bfd_zmalloc (sort_elt * count); |
3410fea8 | 8018 | |
c152c796 AM |
8019 | if (sort == NULL) |
8020 | { | |
8021 | (*info->callbacks->warning) | |
8022 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
8023 | return 0; | |
8024 | } | |
8025 | ||
8026 | if (bed->s->arch_size == 32) | |
8027 | r_sym_mask = ~(bfd_vma) 0xff; | |
8028 | else | |
8029 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
8030 | ||
3410fea8 | 8031 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
8032 | if (lo->type == bfd_indirect_link_order) |
8033 | { | |
8034 | bfd_byte *erel, *erelend; | |
8035 | asection *o = lo->u.indirect.section; | |
8036 | ||
1da212d6 AM |
8037 | if (o->contents == NULL && o->size != 0) |
8038 | { | |
8039 | /* This is a reloc section that is being handled as a normal | |
8040 | section. See bfd_section_from_shdr. We can't combine | |
8041 | relocs in this case. */ | |
8042 | free (sort); | |
8043 | return 0; | |
8044 | } | |
c152c796 | 8045 | erel = o->contents; |
eea6121a | 8046 | erelend = o->contents + o->size; |
c152c796 | 8047 | p = sort + o->output_offset / ext_size * sort_elt; |
3410fea8 | 8048 | |
c152c796 AM |
8049 | while (erel < erelend) |
8050 | { | |
8051 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 8052 | |
c152c796 AM |
8053 | (*swap_in) (abfd, erel, s->rela); |
8054 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
8055 | s->u.sym_mask = r_sym_mask; | |
8056 | p += sort_elt; | |
8057 | erel += ext_size; | |
8058 | } | |
8059 | } | |
8060 | ||
8061 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
8062 | ||
8063 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
8064 | { | |
8065 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
8066 | if (s->type != reloc_class_relative) | |
8067 | break; | |
8068 | } | |
8069 | ret = i; | |
8070 | s_non_relative = p; | |
8071 | ||
8072 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
8073 | for (; i < count; i++, p += sort_elt) | |
8074 | { | |
8075 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
8076 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
8077 | sq = sp; | |
8078 | sp->u.offset = sq->rela->r_offset; | |
8079 | } | |
8080 | ||
8081 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
8082 | ||
3410fea8 | 8083 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
8084 | if (lo->type == bfd_indirect_link_order) |
8085 | { | |
8086 | bfd_byte *erel, *erelend; | |
8087 | asection *o = lo->u.indirect.section; | |
8088 | ||
8089 | erel = o->contents; | |
eea6121a | 8090 | erelend = o->contents + o->size; |
c152c796 AM |
8091 | p = sort + o->output_offset / ext_size * sort_elt; |
8092 | while (erel < erelend) | |
8093 | { | |
8094 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
8095 | (*swap_out) (abfd, s->rela, erel); | |
8096 | p += sort_elt; | |
8097 | erel += ext_size; | |
8098 | } | |
8099 | } | |
8100 | ||
8101 | free (sort); | |
3410fea8 | 8102 | *psec = dynamic_relocs; |
c152c796 AM |
8103 | return ret; |
8104 | } | |
8105 | ||
8106 | /* Flush the output symbols to the file. */ | |
8107 | ||
8108 | static bfd_boolean | |
8109 | elf_link_flush_output_syms (struct elf_final_link_info *finfo, | |
8110 | const struct elf_backend_data *bed) | |
8111 | { | |
8112 | if (finfo->symbuf_count > 0) | |
8113 | { | |
8114 | Elf_Internal_Shdr *hdr; | |
8115 | file_ptr pos; | |
8116 | bfd_size_type amt; | |
8117 | ||
8118 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
8119 | pos = hdr->sh_offset + hdr->sh_size; | |
8120 | amt = finfo->symbuf_count * bed->s->sizeof_sym; | |
8121 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
8122 | || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) | |
8123 | return FALSE; | |
8124 | ||
8125 | hdr->sh_size += amt; | |
8126 | finfo->symbuf_count = 0; | |
8127 | } | |
8128 | ||
8129 | return TRUE; | |
8130 | } | |
8131 | ||
8132 | /* Add a symbol to the output symbol table. */ | |
8133 | ||
8134 | static bfd_boolean | |
8135 | elf_link_output_sym (struct elf_final_link_info *finfo, | |
8136 | const char *name, | |
8137 | Elf_Internal_Sym *elfsym, | |
8138 | asection *input_sec, | |
8139 | struct elf_link_hash_entry *h) | |
8140 | { | |
8141 | bfd_byte *dest; | |
8142 | Elf_External_Sym_Shndx *destshndx; | |
8143 | bfd_boolean (*output_symbol_hook) | |
8144 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, | |
8145 | struct elf_link_hash_entry *); | |
8146 | const struct elf_backend_data *bed; | |
8147 | ||
8148 | bed = get_elf_backend_data (finfo->output_bfd); | |
8149 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; | |
8150 | if (output_symbol_hook != NULL) | |
8151 | { | |
8152 | if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h)) | |
8153 | return FALSE; | |
8154 | } | |
8155 | ||
8156 | if (name == NULL || *name == '\0') | |
8157 | elfsym->st_name = 0; | |
8158 | else if (input_sec->flags & SEC_EXCLUDE) | |
8159 | elfsym->st_name = 0; | |
8160 | else | |
8161 | { | |
8162 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
8163 | name, TRUE, FALSE); | |
8164 | if (elfsym->st_name == (unsigned long) -1) | |
8165 | return FALSE; | |
8166 | } | |
8167 | ||
8168 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
8169 | { | |
8170 | if (! elf_link_flush_output_syms (finfo, bed)) | |
8171 | return FALSE; | |
8172 | } | |
8173 | ||
8174 | dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; | |
8175 | destshndx = finfo->symshndxbuf; | |
8176 | if (destshndx != NULL) | |
8177 | { | |
8178 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
8179 | { | |
8180 | bfd_size_type amt; | |
8181 | ||
8182 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); | |
515ef31d | 8183 | destshndx = bfd_realloc (destshndx, amt * 2); |
c152c796 AM |
8184 | if (destshndx == NULL) |
8185 | return FALSE; | |
515ef31d | 8186 | finfo->symshndxbuf = destshndx; |
c152c796 AM |
8187 | memset ((char *) destshndx + amt, 0, amt); |
8188 | finfo->shndxbuf_size *= 2; | |
8189 | } | |
8190 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
8191 | } | |
8192 | ||
8193 | bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); | |
8194 | finfo->symbuf_count += 1; | |
8195 | bfd_get_symcount (finfo->output_bfd) += 1; | |
8196 | ||
8197 | return TRUE; | |
8198 | } | |
8199 | ||
c0d5a53d L |
8200 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
8201 | ||
8202 | static bfd_boolean | |
8203 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
8204 | { | |
8205 | if (sym->st_shndx > SHN_HIRESERVE) | |
8206 | { | |
8207 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 8208 | beyond 64k. */ |
c0d5a53d L |
8209 | (*_bfd_error_handler) |
8210 | (_("%B: Too many sections: %d (>= %d)"), | |
8211 | abfd, bfd_count_sections (abfd), SHN_LORESERVE); | |
8212 | bfd_set_error (bfd_error_nonrepresentable_section); | |
8213 | return FALSE; | |
8214 | } | |
8215 | return TRUE; | |
8216 | } | |
8217 | ||
c152c796 AM |
8218 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
8219 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
8220 | versioned symbol that would normally require an explicit version. | |
8221 | We also handle the case that a DSO references a hidden symbol | |
8222 | which may be satisfied by a versioned symbol in another DSO. */ | |
8223 | ||
8224 | static bfd_boolean | |
8225 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
8226 | const struct elf_backend_data *bed, | |
8227 | struct elf_link_hash_entry *h) | |
8228 | { | |
8229 | bfd *abfd; | |
8230 | struct elf_link_loaded_list *loaded; | |
8231 | ||
8232 | if (!is_elf_hash_table (info->hash)) | |
8233 | return FALSE; | |
8234 | ||
8235 | switch (h->root.type) | |
8236 | { | |
8237 | default: | |
8238 | abfd = NULL; | |
8239 | break; | |
8240 | ||
8241 | case bfd_link_hash_undefined: | |
8242 | case bfd_link_hash_undefweak: | |
8243 | abfd = h->root.u.undef.abfd; | |
8244 | if ((abfd->flags & DYNAMIC) == 0 | |
e56f61be | 8245 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
8246 | return FALSE; |
8247 | break; | |
8248 | ||
8249 | case bfd_link_hash_defined: | |
8250 | case bfd_link_hash_defweak: | |
8251 | abfd = h->root.u.def.section->owner; | |
8252 | break; | |
8253 | ||
8254 | case bfd_link_hash_common: | |
8255 | abfd = h->root.u.c.p->section->owner; | |
8256 | break; | |
8257 | } | |
8258 | BFD_ASSERT (abfd != NULL); | |
8259 | ||
8260 | for (loaded = elf_hash_table (info)->loaded; | |
8261 | loaded != NULL; | |
8262 | loaded = loaded->next) | |
8263 | { | |
8264 | bfd *input; | |
8265 | Elf_Internal_Shdr *hdr; | |
8266 | bfd_size_type symcount; | |
8267 | bfd_size_type extsymcount; | |
8268 | bfd_size_type extsymoff; | |
8269 | Elf_Internal_Shdr *versymhdr; | |
8270 | Elf_Internal_Sym *isym; | |
8271 | Elf_Internal_Sym *isymend; | |
8272 | Elf_Internal_Sym *isymbuf; | |
8273 | Elf_External_Versym *ever; | |
8274 | Elf_External_Versym *extversym; | |
8275 | ||
8276 | input = loaded->abfd; | |
8277 | ||
8278 | /* We check each DSO for a possible hidden versioned definition. */ | |
8279 | if (input == abfd | |
8280 | || (input->flags & DYNAMIC) == 0 | |
8281 | || elf_dynversym (input) == 0) | |
8282 | continue; | |
8283 | ||
8284 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
8285 | ||
8286 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
8287 | if (elf_bad_symtab (input)) | |
8288 | { | |
8289 | extsymcount = symcount; | |
8290 | extsymoff = 0; | |
8291 | } | |
8292 | else | |
8293 | { | |
8294 | extsymcount = symcount - hdr->sh_info; | |
8295 | extsymoff = hdr->sh_info; | |
8296 | } | |
8297 | ||
8298 | if (extsymcount == 0) | |
8299 | continue; | |
8300 | ||
8301 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
8302 | NULL, NULL, NULL); | |
8303 | if (isymbuf == NULL) | |
8304 | return FALSE; | |
8305 | ||
8306 | /* Read in any version definitions. */ | |
8307 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
8308 | extversym = bfd_malloc (versymhdr->sh_size); | |
8309 | if (extversym == NULL) | |
8310 | goto error_ret; | |
8311 | ||
8312 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
8313 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
8314 | != versymhdr->sh_size)) | |
8315 | { | |
8316 | free (extversym); | |
8317 | error_ret: | |
8318 | free (isymbuf); | |
8319 | return FALSE; | |
8320 | } | |
8321 | ||
8322 | ever = extversym + extsymoff; | |
8323 | isymend = isymbuf + extsymcount; | |
8324 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
8325 | { | |
8326 | const char *name; | |
8327 | Elf_Internal_Versym iver; | |
8328 | unsigned short version_index; | |
8329 | ||
8330 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
8331 | || isym->st_shndx == SHN_UNDEF) | |
8332 | continue; | |
8333 | ||
8334 | name = bfd_elf_string_from_elf_section (input, | |
8335 | hdr->sh_link, | |
8336 | isym->st_name); | |
8337 | if (strcmp (name, h->root.root.string) != 0) | |
8338 | continue; | |
8339 | ||
8340 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
8341 | ||
8342 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
8343 | { | |
8344 | /* If we have a non-hidden versioned sym, then it should | |
8345 | have provided a definition for the undefined sym. */ | |
8346 | abort (); | |
8347 | } | |
8348 | ||
8349 | version_index = iver.vs_vers & VERSYM_VERSION; | |
8350 | if (version_index == 1 || version_index == 2) | |
8351 | { | |
8352 | /* This is the base or first version. We can use it. */ | |
8353 | free (extversym); | |
8354 | free (isymbuf); | |
8355 | return TRUE; | |
8356 | } | |
8357 | } | |
8358 | ||
8359 | free (extversym); | |
8360 | free (isymbuf); | |
8361 | } | |
8362 | ||
8363 | return FALSE; | |
8364 | } | |
8365 | ||
8366 | /* Add an external symbol to the symbol table. This is called from | |
8367 | the hash table traversal routine. When generating a shared object, | |
8368 | we go through the symbol table twice. The first time we output | |
8369 | anything that might have been forced to local scope in a version | |
8370 | script. The second time we output the symbols that are still | |
8371 | global symbols. */ | |
8372 | ||
8373 | static bfd_boolean | |
8374 | elf_link_output_extsym (struct elf_link_hash_entry *h, void *data) | |
8375 | { | |
8376 | struct elf_outext_info *eoinfo = data; | |
8377 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
8378 | bfd_boolean strip; | |
8379 | Elf_Internal_Sym sym; | |
8380 | asection *input_sec; | |
8381 | const struct elf_backend_data *bed; | |
8382 | ||
8383 | if (h->root.type == bfd_link_hash_warning) | |
8384 | { | |
8385 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8386 | if (h->root.type == bfd_link_hash_new) | |
8387 | return TRUE; | |
8388 | } | |
8389 | ||
8390 | /* Decide whether to output this symbol in this pass. */ | |
8391 | if (eoinfo->localsyms) | |
8392 | { | |
f5385ebf | 8393 | if (!h->forced_local) |
c152c796 AM |
8394 | return TRUE; |
8395 | } | |
8396 | else | |
8397 | { | |
f5385ebf | 8398 | if (h->forced_local) |
c152c796 AM |
8399 | return TRUE; |
8400 | } | |
8401 | ||
8402 | bed = get_elf_backend_data (finfo->output_bfd); | |
8403 | ||
12ac1cf5 | 8404 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 8405 | { |
12ac1cf5 NC |
8406 | /* If we have an undefined symbol reference here then it must have |
8407 | come from a shared library that is being linked in. (Undefined | |
8408 | references in regular files have already been handled). */ | |
8409 | bfd_boolean ignore_undef = FALSE; | |
8410 | ||
8411 | /* Some symbols may be special in that the fact that they're | |
8412 | undefined can be safely ignored - let backend determine that. */ | |
8413 | if (bed->elf_backend_ignore_undef_symbol) | |
8414 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
8415 | ||
8416 | /* If we are reporting errors for this situation then do so now. */ | |
8417 | if (ignore_undef == FALSE | |
8418 | && h->ref_dynamic | |
8419 | && ! h->ref_regular | |
8420 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h) | |
8421 | && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
c152c796 | 8422 | { |
12ac1cf5 NC |
8423 | if (! (finfo->info->callbacks->undefined_symbol |
8424 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
8425 | NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) | |
8426 | { | |
8427 | eoinfo->failed = TRUE; | |
8428 | return FALSE; | |
8429 | } | |
c152c796 AM |
8430 | } |
8431 | } | |
8432 | ||
8433 | /* We should also warn if a forced local symbol is referenced from | |
8434 | shared libraries. */ | |
8435 | if (! finfo->info->relocatable | |
8436 | && (! finfo->info->shared) | |
f5385ebf AM |
8437 | && h->forced_local |
8438 | && h->ref_dynamic | |
8439 | && !h->dynamic_def | |
8440 | && !h->dynamic_weak | |
c152c796 AM |
8441 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) |
8442 | { | |
8443 | (*_bfd_error_handler) | |
d003868e | 8444 | (_("%B: %s symbol `%s' in %B is referenced by DSO"), |
cfca085c L |
8445 | finfo->output_bfd, |
8446 | h->root.u.def.section == bfd_abs_section_ptr | |
8447 | ? finfo->output_bfd : h->root.u.def.section->owner, | |
c152c796 AM |
8448 | ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
8449 | ? "internal" | |
8450 | : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
d003868e AM |
8451 | ? "hidden" : "local", |
8452 | h->root.root.string); | |
c152c796 AM |
8453 | eoinfo->failed = TRUE; |
8454 | return FALSE; | |
8455 | } | |
8456 | ||
8457 | /* We don't want to output symbols that have never been mentioned by | |
8458 | a regular file, or that we have been told to strip. However, if | |
8459 | h->indx is set to -2, the symbol is used by a reloc and we must | |
8460 | output it. */ | |
8461 | if (h->indx == -2) | |
8462 | strip = FALSE; | |
f5385ebf | 8463 | else if ((h->def_dynamic |
77cfaee6 AM |
8464 | || h->ref_dynamic |
8465 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
8466 | && !h->def_regular |
8467 | && !h->ref_regular) | |
c152c796 AM |
8468 | strip = TRUE; |
8469 | else if (finfo->info->strip == strip_all) | |
8470 | strip = TRUE; | |
8471 | else if (finfo->info->strip == strip_some | |
8472 | && bfd_hash_lookup (finfo->info->keep_hash, | |
8473 | h->root.root.string, FALSE, FALSE) == NULL) | |
8474 | strip = TRUE; | |
8475 | else if (finfo->info->strip_discarded | |
8476 | && (h->root.type == bfd_link_hash_defined | |
8477 | || h->root.type == bfd_link_hash_defweak) | |
8478 | && elf_discarded_section (h->root.u.def.section)) | |
8479 | strip = TRUE; | |
8480 | else | |
8481 | strip = FALSE; | |
8482 | ||
8483 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
8484 | nothing else to do unless it is a forced local symbol. */ | |
8485 | if (strip | |
8486 | && h->dynindx == -1 | |
f5385ebf | 8487 | && !h->forced_local) |
c152c796 AM |
8488 | return TRUE; |
8489 | ||
8490 | sym.st_value = 0; | |
8491 | sym.st_size = h->size; | |
8492 | sym.st_other = h->other; | |
f5385ebf | 8493 | if (h->forced_local) |
c152c796 AM |
8494 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); |
8495 | else if (h->root.type == bfd_link_hash_undefweak | |
8496 | || h->root.type == bfd_link_hash_defweak) | |
8497 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
8498 | else | |
8499 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
8500 | ||
8501 | switch (h->root.type) | |
8502 | { | |
8503 | default: | |
8504 | case bfd_link_hash_new: | |
8505 | case bfd_link_hash_warning: | |
8506 | abort (); | |
8507 | return FALSE; | |
8508 | ||
8509 | case bfd_link_hash_undefined: | |
8510 | case bfd_link_hash_undefweak: | |
8511 | input_sec = bfd_und_section_ptr; | |
8512 | sym.st_shndx = SHN_UNDEF; | |
8513 | break; | |
8514 | ||
8515 | case bfd_link_hash_defined: | |
8516 | case bfd_link_hash_defweak: | |
8517 | { | |
8518 | input_sec = h->root.u.def.section; | |
8519 | if (input_sec->output_section != NULL) | |
8520 | { | |
8521 | sym.st_shndx = | |
8522 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
8523 | input_sec->output_section); | |
8524 | if (sym.st_shndx == SHN_BAD) | |
8525 | { | |
8526 | (*_bfd_error_handler) | |
d003868e AM |
8527 | (_("%B: could not find output section %A for input section %A"), |
8528 | finfo->output_bfd, input_sec->output_section, input_sec); | |
c152c796 AM |
8529 | eoinfo->failed = TRUE; |
8530 | return FALSE; | |
8531 | } | |
8532 | ||
8533 | /* ELF symbols in relocatable files are section relative, | |
8534 | but in nonrelocatable files they are virtual | |
8535 | addresses. */ | |
8536 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
8537 | if (! finfo->info->relocatable) | |
8538 | { | |
8539 | sym.st_value += input_sec->output_section->vma; | |
8540 | if (h->type == STT_TLS) | |
8541 | { | |
430a16a5 NC |
8542 | asection *tls_sec = elf_hash_table (finfo->info)->tls_sec; |
8543 | if (tls_sec != NULL) | |
8544 | sym.st_value -= tls_sec->vma; | |
8545 | else | |
8546 | { | |
8547 | /* The TLS section may have been garbage collected. */ | |
8548 | BFD_ASSERT (finfo->info->gc_sections | |
8549 | && !input_sec->gc_mark); | |
8550 | } | |
c152c796 AM |
8551 | } |
8552 | } | |
8553 | } | |
8554 | else | |
8555 | { | |
8556 | BFD_ASSERT (input_sec->owner == NULL | |
8557 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
8558 | sym.st_shndx = SHN_UNDEF; | |
8559 | input_sec = bfd_und_section_ptr; | |
8560 | } | |
8561 | } | |
8562 | break; | |
8563 | ||
8564 | case bfd_link_hash_common: | |
8565 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 8566 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
8567 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
8568 | break; | |
8569 | ||
8570 | case bfd_link_hash_indirect: | |
8571 | /* These symbols are created by symbol versioning. They point | |
8572 | to the decorated version of the name. For example, if the | |
8573 | symbol foo@@GNU_1.2 is the default, which should be used when | |
8574 | foo is used with no version, then we add an indirect symbol | |
8575 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
8576 | since the indirected symbol is already in the hash table. */ | |
8577 | return TRUE; | |
8578 | } | |
8579 | ||
8580 | /* Give the processor backend a chance to tweak the symbol value, | |
8581 | and also to finish up anything that needs to be done for this | |
8582 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
8583 | forced local syms when non-shared is due to a historical quirk. */ | |
8584 | if ((h->dynindx != -1 | |
f5385ebf | 8585 | || h->forced_local) |
c152c796 AM |
8586 | && ((finfo->info->shared |
8587 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
8588 | || h->root.type != bfd_link_hash_undefweak)) | |
f5385ebf | 8589 | || !h->forced_local) |
c152c796 AM |
8590 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
8591 | { | |
8592 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8593 | (finfo->output_bfd, finfo->info, h, &sym))) | |
8594 | { | |
8595 | eoinfo->failed = TRUE; | |
8596 | return FALSE; | |
8597 | } | |
8598 | } | |
8599 | ||
8600 | /* If we are marking the symbol as undefined, and there are no | |
8601 | non-weak references to this symbol from a regular object, then | |
8602 | mark the symbol as weak undefined; if there are non-weak | |
8603 | references, mark the symbol as strong. We can't do this earlier, | |
8604 | because it might not be marked as undefined until the | |
8605 | finish_dynamic_symbol routine gets through with it. */ | |
8606 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 8607 | && h->ref_regular |
c152c796 AM |
8608 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
8609 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
8610 | { | |
8611 | int bindtype; | |
8612 | ||
f5385ebf | 8613 | if (h->ref_regular_nonweak) |
c152c796 AM |
8614 | bindtype = STB_GLOBAL; |
8615 | else | |
8616 | bindtype = STB_WEAK; | |
8617 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); | |
8618 | } | |
8619 | ||
8620 | /* If a non-weak symbol with non-default visibility is not defined | |
8621 | locally, it is a fatal error. */ | |
8622 | if (! finfo->info->relocatable | |
8623 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT | |
8624 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
8625 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 8626 | && !h->def_regular) |
c152c796 AM |
8627 | { |
8628 | (*_bfd_error_handler) | |
d003868e AM |
8629 | (_("%B: %s symbol `%s' isn't defined"), |
8630 | finfo->output_bfd, | |
8631 | ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED | |
8632 | ? "protected" | |
8633 | : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL | |
8634 | ? "internal" : "hidden", | |
8635 | h->root.root.string); | |
c152c796 AM |
8636 | eoinfo->failed = TRUE; |
8637 | return FALSE; | |
8638 | } | |
8639 | ||
8640 | /* If this symbol should be put in the .dynsym section, then put it | |
8641 | there now. We already know the symbol index. We also fill in | |
8642 | the entry in the .hash section. */ | |
8643 | if (h->dynindx != -1 | |
8644 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
8645 | { | |
c152c796 AM |
8646 | bfd_byte *esym; |
8647 | ||
8648 | sym.st_name = h->dynstr_index; | |
8649 | esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; | |
c0d5a53d L |
8650 | if (! check_dynsym (finfo->output_bfd, &sym)) |
8651 | { | |
8652 | eoinfo->failed = TRUE; | |
8653 | return FALSE; | |
8654 | } | |
c152c796 AM |
8655 | bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); |
8656 | ||
fdc90cb4 JJ |
8657 | if (finfo->hash_sec != NULL) |
8658 | { | |
8659 | size_t hash_entry_size; | |
8660 | bfd_byte *bucketpos; | |
8661 | bfd_vma chain; | |
41198d0c L |
8662 | size_t bucketcount; |
8663 | size_t bucket; | |
8664 | ||
8665 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
8666 | bucket = h->u.elf_hash_value % bucketcount; | |
fdc90cb4 JJ |
8667 | |
8668 | hash_entry_size | |
8669 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; | |
8670 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents | |
8671 | + (bucket + 2) * hash_entry_size); | |
8672 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
8673 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); | |
8674 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, | |
8675 | ((bfd_byte *) finfo->hash_sec->contents | |
8676 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
8677 | } | |
c152c796 AM |
8678 | |
8679 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
8680 | { | |
8681 | Elf_Internal_Versym iversym; | |
8682 | Elf_External_Versym *eversym; | |
8683 | ||
f5385ebf | 8684 | if (!h->def_regular) |
c152c796 AM |
8685 | { |
8686 | if (h->verinfo.verdef == NULL) | |
8687 | iversym.vs_vers = 0; | |
8688 | else | |
8689 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
8690 | } | |
8691 | else | |
8692 | { | |
8693 | if (h->verinfo.vertree == NULL) | |
8694 | iversym.vs_vers = 1; | |
8695 | else | |
8696 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
3e3b46e5 PB |
8697 | if (finfo->info->create_default_symver) |
8698 | iversym.vs_vers++; | |
c152c796 AM |
8699 | } |
8700 | ||
f5385ebf | 8701 | if (h->hidden) |
c152c796 AM |
8702 | iversym.vs_vers |= VERSYM_HIDDEN; |
8703 | ||
8704 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; | |
8705 | eversym += h->dynindx; | |
8706 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
8707 | } | |
8708 | } | |
8709 | ||
8710 | /* If we're stripping it, then it was just a dynamic symbol, and | |
8711 | there's nothing else to do. */ | |
8712 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) | |
8713 | return TRUE; | |
8714 | ||
8715 | h->indx = bfd_get_symcount (finfo->output_bfd); | |
8716 | ||
8717 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h)) | |
8718 | { | |
8719 | eoinfo->failed = TRUE; | |
8720 | return FALSE; | |
8721 | } | |
8722 | ||
8723 | return TRUE; | |
8724 | } | |
8725 | ||
cdd3575c AM |
8726 | /* Return TRUE if special handling is done for relocs in SEC against |
8727 | symbols defined in discarded sections. */ | |
8728 | ||
c152c796 AM |
8729 | static bfd_boolean |
8730 | elf_section_ignore_discarded_relocs (asection *sec) | |
8731 | { | |
8732 | const struct elf_backend_data *bed; | |
8733 | ||
cdd3575c AM |
8734 | switch (sec->sec_info_type) |
8735 | { | |
8736 | case ELF_INFO_TYPE_STABS: | |
8737 | case ELF_INFO_TYPE_EH_FRAME: | |
8738 | return TRUE; | |
8739 | default: | |
8740 | break; | |
8741 | } | |
c152c796 AM |
8742 | |
8743 | bed = get_elf_backend_data (sec->owner); | |
8744 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
8745 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
8746 | return TRUE; | |
8747 | ||
8748 | return FALSE; | |
8749 | } | |
8750 | ||
9e66c942 AM |
8751 | /* Return a mask saying how ld should treat relocations in SEC against |
8752 | symbols defined in discarded sections. If this function returns | |
8753 | COMPLAIN set, ld will issue a warning message. If this function | |
8754 | returns PRETEND set, and the discarded section was link-once and the | |
8755 | same size as the kept link-once section, ld will pretend that the | |
8756 | symbol was actually defined in the kept section. Otherwise ld will | |
8757 | zero the reloc (at least that is the intent, but some cooperation by | |
8758 | the target dependent code is needed, particularly for REL targets). */ | |
8759 | ||
8a696751 AM |
8760 | unsigned int |
8761 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 8762 | { |
9e66c942 | 8763 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 8764 | return PRETEND; |
cdd3575c AM |
8765 | |
8766 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 8767 | return 0; |
cdd3575c AM |
8768 | |
8769 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 8770 | return 0; |
cdd3575c | 8771 | |
9e66c942 | 8772 | return COMPLAIN | PRETEND; |
cdd3575c AM |
8773 | } |
8774 | ||
3d7f7666 L |
8775 | /* Find a match between a section and a member of a section group. */ |
8776 | ||
8777 | static asection * | |
c0f00686 L |
8778 | match_group_member (asection *sec, asection *group, |
8779 | struct bfd_link_info *info) | |
3d7f7666 L |
8780 | { |
8781 | asection *first = elf_next_in_group (group); | |
8782 | asection *s = first; | |
8783 | ||
8784 | while (s != NULL) | |
8785 | { | |
c0f00686 | 8786 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
8787 | return s; |
8788 | ||
83180ade | 8789 | s = elf_next_in_group (s); |
3d7f7666 L |
8790 | if (s == first) |
8791 | break; | |
8792 | } | |
8793 | ||
8794 | return NULL; | |
8795 | } | |
8796 | ||
01b3c8ab | 8797 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
8798 | to replace it. Return the replacement if it is OK. Otherwise return |
8799 | NULL. */ | |
01b3c8ab L |
8800 | |
8801 | asection * | |
c0f00686 | 8802 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
8803 | { |
8804 | asection *kept; | |
8805 | ||
8806 | kept = sec->kept_section; | |
8807 | if (kept != NULL) | |
8808 | { | |
c2370991 | 8809 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 8810 | kept = match_group_member (sec, kept, info); |
1dd2625f BW |
8811 | if (kept != NULL |
8812 | && ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
8813 | != (kept->rawsize != 0 ? kept->rawsize : kept->size))) | |
01b3c8ab | 8814 | kept = NULL; |
c2370991 | 8815 | sec->kept_section = kept; |
01b3c8ab L |
8816 | } |
8817 | return kept; | |
8818 | } | |
8819 | ||
c152c796 AM |
8820 | /* Link an input file into the linker output file. This function |
8821 | handles all the sections and relocations of the input file at once. | |
8822 | This is so that we only have to read the local symbols once, and | |
8823 | don't have to keep them in memory. */ | |
8824 | ||
8825 | static bfd_boolean | |
8826 | elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) | |
8827 | { | |
ece5ef60 | 8828 | int (*relocate_section) |
c152c796 AM |
8829 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
8830 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
8831 | bfd *output_bfd; | |
8832 | Elf_Internal_Shdr *symtab_hdr; | |
8833 | size_t locsymcount; | |
8834 | size_t extsymoff; | |
8835 | Elf_Internal_Sym *isymbuf; | |
8836 | Elf_Internal_Sym *isym; | |
8837 | Elf_Internal_Sym *isymend; | |
8838 | long *pindex; | |
8839 | asection **ppsection; | |
8840 | asection *o; | |
8841 | const struct elf_backend_data *bed; | |
c152c796 AM |
8842 | struct elf_link_hash_entry **sym_hashes; |
8843 | ||
8844 | output_bfd = finfo->output_bfd; | |
8845 | bed = get_elf_backend_data (output_bfd); | |
8846 | relocate_section = bed->elf_backend_relocate_section; | |
8847 | ||
8848 | /* If this is a dynamic object, we don't want to do anything here: | |
8849 | we don't want the local symbols, and we don't want the section | |
8850 | contents. */ | |
8851 | if ((input_bfd->flags & DYNAMIC) != 0) | |
8852 | return TRUE; | |
8853 | ||
c152c796 AM |
8854 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
8855 | if (elf_bad_symtab (input_bfd)) | |
8856 | { | |
8857 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
8858 | extsymoff = 0; | |
8859 | } | |
8860 | else | |
8861 | { | |
8862 | locsymcount = symtab_hdr->sh_info; | |
8863 | extsymoff = symtab_hdr->sh_info; | |
8864 | } | |
8865 | ||
8866 | /* Read the local symbols. */ | |
8867 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
8868 | if (isymbuf == NULL && locsymcount != 0) | |
8869 | { | |
8870 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8871 | finfo->internal_syms, | |
8872 | finfo->external_syms, | |
8873 | finfo->locsym_shndx); | |
8874 | if (isymbuf == NULL) | |
8875 | return FALSE; | |
8876 | } | |
8877 | ||
8878 | /* Find local symbol sections and adjust values of symbols in | |
8879 | SEC_MERGE sections. Write out those local symbols we know are | |
8880 | going into the output file. */ | |
8881 | isymend = isymbuf + locsymcount; | |
8882 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
8883 | isym < isymend; | |
8884 | isym++, pindex++, ppsection++) | |
8885 | { | |
8886 | asection *isec; | |
8887 | const char *name; | |
8888 | Elf_Internal_Sym osym; | |
8889 | ||
8890 | *pindex = -1; | |
8891 | ||
8892 | if (elf_bad_symtab (input_bfd)) | |
8893 | { | |
8894 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
8895 | { | |
8896 | *ppsection = NULL; | |
8897 | continue; | |
8898 | } | |
8899 | } | |
8900 | ||
8901 | if (isym->st_shndx == SHN_UNDEF) | |
8902 | isec = bfd_und_section_ptr; | |
8903 | else if (isym->st_shndx < SHN_LORESERVE | |
8904 | || isym->st_shndx > SHN_HIRESERVE) | |
8905 | { | |
8906 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); | |
8907 | if (isec | |
8908 | && isec->sec_info_type == ELF_INFO_TYPE_MERGE | |
8909 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) | |
8910 | isym->st_value = | |
8911 | _bfd_merged_section_offset (output_bfd, &isec, | |
8912 | elf_section_data (isec)->sec_info, | |
753731ee | 8913 | isym->st_value); |
c152c796 AM |
8914 | } |
8915 | else if (isym->st_shndx == SHN_ABS) | |
8916 | isec = bfd_abs_section_ptr; | |
8917 | else if (isym->st_shndx == SHN_COMMON) | |
8918 | isec = bfd_com_section_ptr; | |
8919 | else | |
8920 | { | |
f02571c5 AM |
8921 | /* Don't attempt to output symbols with st_shnx in the |
8922 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
8923 | *ppsection = NULL; | |
8924 | continue; | |
c152c796 AM |
8925 | } |
8926 | ||
8927 | *ppsection = isec; | |
8928 | ||
8929 | /* Don't output the first, undefined, symbol. */ | |
8930 | if (ppsection == finfo->sections) | |
8931 | continue; | |
8932 | ||
8933 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
8934 | { | |
8935 | /* We never output section symbols. Instead, we use the | |
8936 | section symbol of the corresponding section in the output | |
8937 | file. */ | |
8938 | continue; | |
8939 | } | |
8940 | ||
8941 | /* If we are stripping all symbols, we don't want to output this | |
8942 | one. */ | |
8943 | if (finfo->info->strip == strip_all) | |
8944 | continue; | |
8945 | ||
8946 | /* If we are discarding all local symbols, we don't want to | |
8947 | output this one. If we are generating a relocatable output | |
8948 | file, then some of the local symbols may be required by | |
8949 | relocs; we output them below as we discover that they are | |
8950 | needed. */ | |
8951 | if (finfo->info->discard == discard_all) | |
8952 | continue; | |
8953 | ||
8954 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
8955 | discarding, we don't need to keep it. */ |
8956 | if (isym->st_shndx != SHN_UNDEF | |
8957 | && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
ccf5f610 | 8958 | && (isec == NULL |
f02571c5 AM |
8959 | || bfd_section_removed_from_list (output_bfd, |
8960 | isec->output_section))) | |
e75a280b L |
8961 | continue; |
8962 | ||
c152c796 AM |
8963 | /* Get the name of the symbol. */ |
8964 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
8965 | isym->st_name); | |
8966 | if (name == NULL) | |
8967 | return FALSE; | |
8968 | ||
8969 | /* See if we are discarding symbols with this name. */ | |
8970 | if ((finfo->info->strip == strip_some | |
8971 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) | |
8972 | == NULL)) | |
8973 | || (((finfo->info->discard == discard_sec_merge | |
8974 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) | |
8975 | || finfo->info->discard == discard_l) | |
8976 | && bfd_is_local_label_name (input_bfd, name))) | |
8977 | continue; | |
8978 | ||
8979 | /* If we get here, we are going to output this symbol. */ | |
8980 | ||
8981 | osym = *isym; | |
8982 | ||
8983 | /* Adjust the section index for the output file. */ | |
8984 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
8985 | isec->output_section); | |
8986 | if (osym.st_shndx == SHN_BAD) | |
8987 | return FALSE; | |
8988 | ||
8989 | *pindex = bfd_get_symcount (output_bfd); | |
8990 | ||
8991 | /* ELF symbols in relocatable files are section relative, but | |
8992 | in executable files they are virtual addresses. Note that | |
8993 | this code assumes that all ELF sections have an associated | |
8994 | BFD section with a reasonable value for output_offset; below | |
8995 | we assume that they also have a reasonable value for | |
8996 | output_section. Any special sections must be set up to meet | |
8997 | these requirements. */ | |
8998 | osym.st_value += isec->output_offset; | |
8999 | if (! finfo->info->relocatable) | |
9000 | { | |
9001 | osym.st_value += isec->output_section->vma; | |
9002 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
9003 | { | |
9004 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
9005 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
9006 | osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
9007 | } | |
9008 | } | |
9009 | ||
9010 | if (! elf_link_output_sym (finfo, name, &osym, isec, NULL)) | |
9011 | return FALSE; | |
9012 | } | |
9013 | ||
9014 | /* Relocate the contents of each section. */ | |
9015 | sym_hashes = elf_sym_hashes (input_bfd); | |
9016 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
9017 | { | |
9018 | bfd_byte *contents; | |
9019 | ||
9020 | if (! o->linker_mark) | |
9021 | { | |
9022 | /* This section was omitted from the link. */ | |
9023 | continue; | |
9024 | } | |
9025 | ||
9026 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 9027 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
9028 | continue; |
9029 | ||
9030 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
9031 | { | |
9032 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
9033 | or somesuch. */ | |
9034 | continue; | |
9035 | } | |
9036 | ||
9037 | /* Get the contents of the section. They have been cached by a | |
9038 | relaxation routine. Note that o is a section in an input | |
9039 | file, so the contents field will not have been set by any of | |
9040 | the routines which work on output files. */ | |
9041 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
9042 | contents = elf_section_data (o)->this_hdr.contents; | |
9043 | else | |
9044 | { | |
eea6121a AM |
9045 | bfd_size_type amt = o->rawsize ? o->rawsize : o->size; |
9046 | ||
c152c796 | 9047 | contents = finfo->contents; |
eea6121a | 9048 | if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt)) |
c152c796 AM |
9049 | return FALSE; |
9050 | } | |
9051 | ||
9052 | if ((o->flags & SEC_RELOC) != 0) | |
9053 | { | |
9054 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 9055 | Elf_Internal_Rela *rel, *relend; |
c152c796 AM |
9056 | bfd_vma r_type_mask; |
9057 | int r_sym_shift; | |
0f02bbd9 | 9058 | int action_discarded; |
ece5ef60 | 9059 | int ret; |
c152c796 AM |
9060 | |
9061 | /* Get the swapped relocs. */ | |
9062 | internal_relocs | |
9063 | = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, | |
9064 | finfo->internal_relocs, FALSE); | |
9065 | if (internal_relocs == NULL | |
9066 | && o->reloc_count > 0) | |
9067 | return FALSE; | |
9068 | ||
9069 | if (bed->s->arch_size == 32) | |
9070 | { | |
9071 | r_type_mask = 0xff; | |
9072 | r_sym_shift = 8; | |
9073 | } | |
9074 | else | |
9075 | { | |
9076 | r_type_mask = 0xffffffff; | |
9077 | r_sym_shift = 32; | |
9078 | } | |
9079 | ||
0f02bbd9 | 9080 | action_discarded = -1; |
c152c796 | 9081 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
9082 | action_discarded = (*bed->action_discarded) (o); |
9083 | ||
9084 | /* Run through the relocs evaluating complex reloc symbols and | |
9085 | looking for relocs against symbols from discarded sections | |
9086 | or section symbols from removed link-once sections. | |
9087 | Complain about relocs against discarded sections. Zero | |
9088 | relocs against removed link-once sections. */ | |
9089 | ||
9090 | rel = internal_relocs; | |
9091 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
9092 | for ( ; rel < relend; rel++) | |
c152c796 | 9093 | { |
0f02bbd9 AM |
9094 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
9095 | unsigned int s_type; | |
9096 | asection **ps, *sec; | |
9097 | struct elf_link_hash_entry *h = NULL; | |
9098 | const char *sym_name; | |
c152c796 | 9099 | |
0f02bbd9 AM |
9100 | if (r_symndx == STN_UNDEF) |
9101 | continue; | |
c152c796 | 9102 | |
0f02bbd9 AM |
9103 | if (r_symndx >= locsymcount |
9104 | || (elf_bad_symtab (input_bfd) | |
9105 | && finfo->sections[r_symndx] == NULL)) | |
9106 | { | |
9107 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 9108 | |
0f02bbd9 AM |
9109 | /* Badly formatted input files can contain relocs that |
9110 | reference non-existant symbols. Check here so that | |
9111 | we do not seg fault. */ | |
9112 | if (h == NULL) | |
c152c796 | 9113 | { |
0f02bbd9 | 9114 | char buffer [32]; |
dce669a1 | 9115 | |
0f02bbd9 AM |
9116 | sprintf_vma (buffer, rel->r_info); |
9117 | (*_bfd_error_handler) | |
9118 | (_("error: %B contains a reloc (0x%s) for section %A " | |
9119 | "that references a non-existent global symbol"), | |
9120 | input_bfd, o, buffer); | |
9121 | bfd_set_error (bfd_error_bad_value); | |
9122 | return FALSE; | |
9123 | } | |
3b36f7e6 | 9124 | |
0f02bbd9 AM |
9125 | while (h->root.type == bfd_link_hash_indirect |
9126 | || h->root.type == bfd_link_hash_warning) | |
9127 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 9128 | |
0f02bbd9 | 9129 | s_type = h->type; |
cdd3575c | 9130 | |
0f02bbd9 AM |
9131 | ps = NULL; |
9132 | if (h->root.type == bfd_link_hash_defined | |
9133 | || h->root.type == bfd_link_hash_defweak) | |
9134 | ps = &h->root.u.def.section; | |
9135 | ||
9136 | sym_name = h->root.root.string; | |
9137 | } | |
9138 | else | |
9139 | { | |
9140 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
9141 | ||
9142 | s_type = ELF_ST_TYPE (sym->st_info); | |
9143 | ps = &finfo->sections[r_symndx]; | |
9144 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, | |
9145 | sym, *ps); | |
9146 | } | |
c152c796 | 9147 | |
0f02bbd9 AM |
9148 | if (s_type == STT_RELC || s_type == STT_SRELC) |
9149 | { | |
9150 | bfd_vma val; | |
9151 | bfd_vma dot = (rel->r_offset | |
9152 | + o->output_offset + o->output_section->vma); | |
9153 | #ifdef DEBUG | |
9154 | printf ("Encountered a complex symbol!"); | |
9155 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
9156 | input_bfd->filename, o->name, rel - internal_relocs); | |
9157 | printf (" symbol: idx %8.8lx, name %s\n", | |
9158 | r_symndx, sym_name); | |
9159 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
9160 | (unsigned long) rel->r_info, | |
9161 | (unsigned long) rel->r_offset); | |
9162 | #endif | |
9163 | if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot, | |
9164 | isymbuf, locsymcount, s_type == STT_SRELC)) | |
9165 | return FALSE; | |
9166 | ||
9167 | /* Symbol evaluated OK. Update to absolute value. */ | |
9168 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
9169 | r_symndx, val); | |
9170 | continue; | |
9171 | } | |
9172 | ||
9173 | if (action_discarded != -1 && ps != NULL) | |
9174 | { | |
cdd3575c AM |
9175 | /* Complain if the definition comes from a |
9176 | discarded section. */ | |
9177 | if ((sec = *ps) != NULL && elf_discarded_section (sec)) | |
9178 | { | |
87e5235d | 9179 | BFD_ASSERT (r_symndx != 0); |
0f02bbd9 | 9180 | if (action_discarded & COMPLAIN) |
e1fffbe6 AM |
9181 | (*finfo->info->callbacks->einfo) |
9182 | (_("%X`%s' referenced in section `%A' of %B: " | |
58ac56d0 | 9183 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 9184 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 9185 | |
87e5235d | 9186 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 9187 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
9188 | really defined in the kept linkonce section. |
9189 | FIXME: This is quite broken. Modifying the | |
9190 | symbol here means we will be changing all later | |
e0ae6d6f | 9191 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 9192 | if (action_discarded & PRETEND) |
87e5235d | 9193 | { |
01b3c8ab L |
9194 | asection *kept; |
9195 | ||
c0f00686 L |
9196 | kept = _bfd_elf_check_kept_section (sec, |
9197 | finfo->info); | |
01b3c8ab | 9198 | if (kept != NULL) |
87e5235d AM |
9199 | { |
9200 | *ps = kept; | |
9201 | continue; | |
9202 | } | |
9203 | } | |
c152c796 AM |
9204 | } |
9205 | } | |
9206 | } | |
9207 | ||
9208 | /* Relocate the section by invoking a back end routine. | |
9209 | ||
9210 | The back end routine is responsible for adjusting the | |
9211 | section contents as necessary, and (if using Rela relocs | |
9212 | and generating a relocatable output file) adjusting the | |
9213 | reloc addend as necessary. | |
9214 | ||
9215 | The back end routine does not have to worry about setting | |
9216 | the reloc address or the reloc symbol index. | |
9217 | ||
9218 | The back end routine is given a pointer to the swapped in | |
9219 | internal symbols, and can access the hash table entries | |
9220 | for the external symbols via elf_sym_hashes (input_bfd). | |
9221 | ||
9222 | When generating relocatable output, the back end routine | |
9223 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
9224 | output symbol is going to be a section symbol | |
9225 | corresponding to the output section, which will require | |
9226 | the addend to be adjusted. */ | |
9227 | ||
ece5ef60 | 9228 | ret = (*relocate_section) (output_bfd, finfo->info, |
c152c796 AM |
9229 | input_bfd, o, contents, |
9230 | internal_relocs, | |
9231 | isymbuf, | |
ece5ef60 AM |
9232 | finfo->sections); |
9233 | if (!ret) | |
c152c796 AM |
9234 | return FALSE; |
9235 | ||
ece5ef60 AM |
9236 | if (ret == 2 |
9237 | || finfo->info->relocatable | |
9238 | || finfo->info->emitrelocations) | |
c152c796 AM |
9239 | { |
9240 | Elf_Internal_Rela *irela; | |
9241 | Elf_Internal_Rela *irelaend; | |
9242 | bfd_vma last_offset; | |
9243 | struct elf_link_hash_entry **rel_hash; | |
eac338cf | 9244 | struct elf_link_hash_entry **rel_hash_list; |
c152c796 AM |
9245 | Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2; |
9246 | unsigned int next_erel; | |
c152c796 AM |
9247 | bfd_boolean rela_normal; |
9248 | ||
9249 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
9250 | rela_normal = (bed->rela_normal | |
9251 | && (input_rel_hdr->sh_entsize | |
9252 | == bed->s->sizeof_rela)); | |
9253 | ||
9254 | /* Adjust the reloc addresses and symbol indices. */ | |
9255 | ||
9256 | irela = internal_relocs; | |
9257 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
9258 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
9259 | + elf_section_data (o->output_section)->rel_count | |
9260 | + elf_section_data (o->output_section)->rel_count2); | |
eac338cf | 9261 | rel_hash_list = rel_hash; |
c152c796 AM |
9262 | last_offset = o->output_offset; |
9263 | if (!finfo->info->relocatable) | |
9264 | last_offset += o->output_section->vma; | |
9265 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
9266 | { | |
9267 | unsigned long r_symndx; | |
9268 | asection *sec; | |
9269 | Elf_Internal_Sym sym; | |
9270 | ||
9271 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
9272 | { | |
9273 | rel_hash++; | |
9274 | next_erel = 0; | |
9275 | } | |
9276 | ||
9277 | irela->r_offset = _bfd_elf_section_offset (output_bfd, | |
9278 | finfo->info, o, | |
9279 | irela->r_offset); | |
9280 | if (irela->r_offset >= (bfd_vma) -2) | |
9281 | { | |
9282 | /* This is a reloc for a deleted entry or somesuch. | |
9283 | Turn it into an R_*_NONE reloc, at the same | |
9284 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 9285 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
9286 | being ordered. */ |
9287 | irela->r_offset = last_offset; | |
9288 | irela->r_info = 0; | |
9289 | irela->r_addend = 0; | |
9290 | continue; | |
9291 | } | |
9292 | ||
9293 | irela->r_offset += o->output_offset; | |
9294 | ||
9295 | /* Relocs in an executable have to be virtual addresses. */ | |
9296 | if (!finfo->info->relocatable) | |
9297 | irela->r_offset += o->output_section->vma; | |
9298 | ||
9299 | last_offset = irela->r_offset; | |
9300 | ||
9301 | r_symndx = irela->r_info >> r_sym_shift; | |
9302 | if (r_symndx == STN_UNDEF) | |
9303 | continue; | |
9304 | ||
9305 | if (r_symndx >= locsymcount | |
9306 | || (elf_bad_symtab (input_bfd) | |
9307 | && finfo->sections[r_symndx] == NULL)) | |
9308 | { | |
9309 | struct elf_link_hash_entry *rh; | |
9310 | unsigned long indx; | |
9311 | ||
9312 | /* This is a reloc against a global symbol. We | |
9313 | have not yet output all the local symbols, so | |
9314 | we do not know the symbol index of any global | |
9315 | symbol. We set the rel_hash entry for this | |
9316 | reloc to point to the global hash table entry | |
9317 | for this symbol. The symbol index is then | |
ee75fd95 | 9318 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
9319 | indx = r_symndx - extsymoff; |
9320 | rh = elf_sym_hashes (input_bfd)[indx]; | |
9321 | while (rh->root.type == bfd_link_hash_indirect | |
9322 | || rh->root.type == bfd_link_hash_warning) | |
9323 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
9324 | ||
9325 | /* Setting the index to -2 tells | |
9326 | elf_link_output_extsym that this symbol is | |
9327 | used by a reloc. */ | |
9328 | BFD_ASSERT (rh->indx < 0); | |
9329 | rh->indx = -2; | |
9330 | ||
9331 | *rel_hash = rh; | |
9332 | ||
9333 | continue; | |
9334 | } | |
9335 | ||
9336 | /* This is a reloc against a local symbol. */ | |
9337 | ||
9338 | *rel_hash = NULL; | |
9339 | sym = isymbuf[r_symndx]; | |
9340 | sec = finfo->sections[r_symndx]; | |
9341 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) | |
9342 | { | |
9343 | /* I suppose the backend ought to fill in the | |
9344 | section of any STT_SECTION symbol against a | |
6a8d1586 AM |
9345 | processor specific section. */ |
9346 | r_symndx = 0; | |
9347 | if (bfd_is_abs_section (sec)) | |
9348 | ; | |
c152c796 AM |
9349 | else if (sec == NULL || sec->owner == NULL) |
9350 | { | |
9351 | bfd_set_error (bfd_error_bad_value); | |
9352 | return FALSE; | |
9353 | } | |
9354 | else | |
9355 | { | |
6a8d1586 AM |
9356 | asection *osec = sec->output_section; |
9357 | ||
9358 | /* If we have discarded a section, the output | |
9359 | section will be the absolute section. In | |
ab96bf03 AM |
9360 | case of discarded SEC_MERGE sections, use |
9361 | the kept section. relocate_section should | |
9362 | have already handled discarded linkonce | |
9363 | sections. */ | |
6a8d1586 AM |
9364 | if (bfd_is_abs_section (osec) |
9365 | && sec->kept_section != NULL | |
9366 | && sec->kept_section->output_section != NULL) | |
9367 | { | |
9368 | osec = sec->kept_section->output_section; | |
9369 | irela->r_addend -= osec->vma; | |
9370 | } | |
9371 | ||
9372 | if (!bfd_is_abs_section (osec)) | |
9373 | { | |
9374 | r_symndx = osec->target_index; | |
74541ad4 AM |
9375 | if (r_symndx == 0) |
9376 | { | |
9377 | struct elf_link_hash_table *htab; | |
9378 | asection *oi; | |
9379 | ||
9380 | htab = elf_hash_table (finfo->info); | |
9381 | oi = htab->text_index_section; | |
9382 | if ((osec->flags & SEC_READONLY) == 0 | |
9383 | && htab->data_index_section != NULL) | |
9384 | oi = htab->data_index_section; | |
9385 | ||
9386 | if (oi != NULL) | |
9387 | { | |
9388 | irela->r_addend += osec->vma - oi->vma; | |
9389 | r_symndx = oi->target_index; | |
9390 | } | |
9391 | } | |
9392 | ||
6a8d1586 AM |
9393 | BFD_ASSERT (r_symndx != 0); |
9394 | } | |
c152c796 AM |
9395 | } |
9396 | ||
9397 | /* Adjust the addend according to where the | |
9398 | section winds up in the output section. */ | |
9399 | if (rela_normal) | |
9400 | irela->r_addend += sec->output_offset; | |
9401 | } | |
9402 | else | |
9403 | { | |
9404 | if (finfo->indices[r_symndx] == -1) | |
9405 | { | |
9406 | unsigned long shlink; | |
9407 | const char *name; | |
9408 | asection *osec; | |
9409 | ||
9410 | if (finfo->info->strip == strip_all) | |
9411 | { | |
9412 | /* You can't do ld -r -s. */ | |
9413 | bfd_set_error (bfd_error_invalid_operation); | |
9414 | return FALSE; | |
9415 | } | |
9416 | ||
9417 | /* This symbol was skipped earlier, but | |
9418 | since it is needed by a reloc, we | |
9419 | must output it now. */ | |
9420 | shlink = symtab_hdr->sh_link; | |
9421 | name = (bfd_elf_string_from_elf_section | |
9422 | (input_bfd, shlink, sym.st_name)); | |
9423 | if (name == NULL) | |
9424 | return FALSE; | |
9425 | ||
9426 | osec = sec->output_section; | |
9427 | sym.st_shndx = | |
9428 | _bfd_elf_section_from_bfd_section (output_bfd, | |
9429 | osec); | |
9430 | if (sym.st_shndx == SHN_BAD) | |
9431 | return FALSE; | |
9432 | ||
9433 | sym.st_value += sec->output_offset; | |
9434 | if (! finfo->info->relocatable) | |
9435 | { | |
9436 | sym.st_value += osec->vma; | |
9437 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
9438 | { | |
9439 | /* STT_TLS symbols are relative to PT_TLS | |
9440 | segment base. */ | |
9441 | BFD_ASSERT (elf_hash_table (finfo->info) | |
9442 | ->tls_sec != NULL); | |
9443 | sym.st_value -= (elf_hash_table (finfo->info) | |
9444 | ->tls_sec->vma); | |
9445 | } | |
9446 | } | |
9447 | ||
9448 | finfo->indices[r_symndx] | |
9449 | = bfd_get_symcount (output_bfd); | |
9450 | ||
9451 | if (! elf_link_output_sym (finfo, name, &sym, sec, | |
9452 | NULL)) | |
9453 | return FALSE; | |
9454 | } | |
9455 | ||
9456 | r_symndx = finfo->indices[r_symndx]; | |
9457 | } | |
9458 | ||
9459 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
9460 | | (irela->r_info & r_type_mask)); | |
9461 | } | |
9462 | ||
9463 | /* Swap out the relocs. */ | |
c152c796 | 9464 | if (input_rel_hdr->sh_size != 0 |
eac338cf PB |
9465 | && !bed->elf_backend_emit_relocs (output_bfd, o, |
9466 | input_rel_hdr, | |
9467 | internal_relocs, | |
9468 | rel_hash_list)) | |
c152c796 AM |
9469 | return FALSE; |
9470 | ||
9471 | input_rel_hdr2 = elf_section_data (o)->rel_hdr2; | |
9472 | if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0) | |
9473 | { | |
9474 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) | |
9475 | * bed->s->int_rels_per_ext_rel); | |
eac338cf PB |
9476 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
9477 | if (!bed->elf_backend_emit_relocs (output_bfd, o, | |
9478 | input_rel_hdr2, | |
9479 | internal_relocs, | |
9480 | rel_hash_list)) | |
c152c796 AM |
9481 | return FALSE; |
9482 | } | |
9483 | } | |
9484 | } | |
9485 | ||
9486 | /* Write out the modified section contents. */ | |
9487 | if (bed->elf_backend_write_section | |
c7b8f16e JB |
9488 | && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o, |
9489 | contents)) | |
c152c796 AM |
9490 | { |
9491 | /* Section written out. */ | |
9492 | } | |
9493 | else switch (o->sec_info_type) | |
9494 | { | |
9495 | case ELF_INFO_TYPE_STABS: | |
9496 | if (! (_bfd_write_section_stabs | |
9497 | (output_bfd, | |
9498 | &elf_hash_table (finfo->info)->stab_info, | |
9499 | o, &elf_section_data (o)->sec_info, contents))) | |
9500 | return FALSE; | |
9501 | break; | |
9502 | case ELF_INFO_TYPE_MERGE: | |
9503 | if (! _bfd_write_merged_section (output_bfd, o, | |
9504 | elf_section_data (o)->sec_info)) | |
9505 | return FALSE; | |
9506 | break; | |
9507 | case ELF_INFO_TYPE_EH_FRAME: | |
9508 | { | |
9509 | if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, | |
9510 | o, contents)) | |
9511 | return FALSE; | |
9512 | } | |
9513 | break; | |
9514 | default: | |
9515 | { | |
c152c796 | 9516 | if (! (o->flags & SEC_EXCLUDE) |
ace79388 | 9517 | && ! (o->output_section->flags & SEC_NEVER_LOAD) |
c152c796 AM |
9518 | && ! bfd_set_section_contents (output_bfd, o->output_section, |
9519 | contents, | |
9520 | (file_ptr) o->output_offset, | |
eea6121a | 9521 | o->size)) |
c152c796 AM |
9522 | return FALSE; |
9523 | } | |
9524 | break; | |
9525 | } | |
9526 | } | |
9527 | ||
9528 | return TRUE; | |
9529 | } | |
9530 | ||
9531 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 9532 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
9533 | is used to build constructor and destructor tables when linking |
9534 | with -Ur. */ | |
9535 | ||
9536 | static bfd_boolean | |
9537 | elf_reloc_link_order (bfd *output_bfd, | |
9538 | struct bfd_link_info *info, | |
9539 | asection *output_section, | |
9540 | struct bfd_link_order *link_order) | |
9541 | { | |
9542 | reloc_howto_type *howto; | |
9543 | long indx; | |
9544 | bfd_vma offset; | |
9545 | bfd_vma addend; | |
9546 | struct elf_link_hash_entry **rel_hash_ptr; | |
9547 | Elf_Internal_Shdr *rel_hdr; | |
9548 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
9549 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
9550 | bfd_byte *erel; | |
9551 | unsigned int i; | |
9552 | ||
9553 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
9554 | if (howto == NULL) | |
9555 | { | |
9556 | bfd_set_error (bfd_error_bad_value); | |
9557 | return FALSE; | |
9558 | } | |
9559 | ||
9560 | addend = link_order->u.reloc.p->addend; | |
9561 | ||
9562 | /* Figure out the symbol index. */ | |
9563 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
9564 | + elf_section_data (output_section)->rel_count | |
9565 | + elf_section_data (output_section)->rel_count2); | |
9566 | if (link_order->type == bfd_section_reloc_link_order) | |
9567 | { | |
9568 | indx = link_order->u.reloc.p->u.section->target_index; | |
9569 | BFD_ASSERT (indx != 0); | |
9570 | *rel_hash_ptr = NULL; | |
9571 | } | |
9572 | else | |
9573 | { | |
9574 | struct elf_link_hash_entry *h; | |
9575 | ||
9576 | /* Treat a reloc against a defined symbol as though it were | |
9577 | actually against the section. */ | |
9578 | h = ((struct elf_link_hash_entry *) | |
9579 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
9580 | link_order->u.reloc.p->u.name, | |
9581 | FALSE, FALSE, TRUE)); | |
9582 | if (h != NULL | |
9583 | && (h->root.type == bfd_link_hash_defined | |
9584 | || h->root.type == bfd_link_hash_defweak)) | |
9585 | { | |
9586 | asection *section; | |
9587 | ||
9588 | section = h->root.u.def.section; | |
9589 | indx = section->output_section->target_index; | |
9590 | *rel_hash_ptr = NULL; | |
9591 | /* It seems that we ought to add the symbol value to the | |
9592 | addend here, but in practice it has already been added | |
9593 | because it was passed to constructor_callback. */ | |
9594 | addend += section->output_section->vma + section->output_offset; | |
9595 | } | |
9596 | else if (h != NULL) | |
9597 | { | |
9598 | /* Setting the index to -2 tells elf_link_output_extsym that | |
9599 | this symbol is used by a reloc. */ | |
9600 | h->indx = -2; | |
9601 | *rel_hash_ptr = h; | |
9602 | indx = 0; | |
9603 | } | |
9604 | else | |
9605 | { | |
9606 | if (! ((*info->callbacks->unattached_reloc) | |
9607 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) | |
9608 | return FALSE; | |
9609 | indx = 0; | |
9610 | } | |
9611 | } | |
9612 | ||
9613 | /* If this is an inplace reloc, we must write the addend into the | |
9614 | object file. */ | |
9615 | if (howto->partial_inplace && addend != 0) | |
9616 | { | |
9617 | bfd_size_type size; | |
9618 | bfd_reloc_status_type rstat; | |
9619 | bfd_byte *buf; | |
9620 | bfd_boolean ok; | |
9621 | const char *sym_name; | |
9622 | ||
9623 | size = bfd_get_reloc_size (howto); | |
9624 | buf = bfd_zmalloc (size); | |
9625 | if (buf == NULL) | |
9626 | return FALSE; | |
9627 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
9628 | switch (rstat) | |
9629 | { | |
9630 | case bfd_reloc_ok: | |
9631 | break; | |
9632 | ||
9633 | default: | |
9634 | case bfd_reloc_outofrange: | |
9635 | abort (); | |
9636 | ||
9637 | case bfd_reloc_overflow: | |
9638 | if (link_order->type == bfd_section_reloc_link_order) | |
9639 | sym_name = bfd_section_name (output_bfd, | |
9640 | link_order->u.reloc.p->u.section); | |
9641 | else | |
9642 | sym_name = link_order->u.reloc.p->u.name; | |
9643 | if (! ((*info->callbacks->reloc_overflow) | |
dfeffb9f L |
9644 | (info, NULL, sym_name, howto->name, addend, NULL, |
9645 | NULL, (bfd_vma) 0))) | |
c152c796 AM |
9646 | { |
9647 | free (buf); | |
9648 | return FALSE; | |
9649 | } | |
9650 | break; | |
9651 | } | |
9652 | ok = bfd_set_section_contents (output_bfd, output_section, buf, | |
9653 | link_order->offset, size); | |
9654 | free (buf); | |
9655 | if (! ok) | |
9656 | return FALSE; | |
9657 | } | |
9658 | ||
9659 | /* The address of a reloc is relative to the section in a | |
9660 | relocatable file, and is a virtual address in an executable | |
9661 | file. */ | |
9662 | offset = link_order->offset; | |
9663 | if (! info->relocatable) | |
9664 | offset += output_section->vma; | |
9665 | ||
9666 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
9667 | { | |
9668 | irel[i].r_offset = offset; | |
9669 | irel[i].r_info = 0; | |
9670 | irel[i].r_addend = 0; | |
9671 | } | |
9672 | if (bed->s->arch_size == 32) | |
9673 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
9674 | else | |
9675 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
9676 | ||
9677 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
9678 | erel = rel_hdr->contents; | |
9679 | if (rel_hdr->sh_type == SHT_REL) | |
9680 | { | |
9681 | erel += (elf_section_data (output_section)->rel_count | |
9682 | * bed->s->sizeof_rel); | |
9683 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); | |
9684 | } | |
9685 | else | |
9686 | { | |
9687 | irel[0].r_addend = addend; | |
9688 | erel += (elf_section_data (output_section)->rel_count | |
9689 | * bed->s->sizeof_rela); | |
9690 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); | |
9691 | } | |
9692 | ||
9693 | ++elf_section_data (output_section)->rel_count; | |
9694 | ||
9695 | return TRUE; | |
9696 | } | |
9697 | ||
0b52efa6 PB |
9698 | |
9699 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
9700 | ||
9701 | static bfd_vma | |
9702 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
9703 | { | |
9704 | Elf_Internal_Shdr **elf_shdrp; | |
9705 | asection *s; | |
9706 | int elfsec; | |
9707 | ||
9708 | s = p->u.indirect.section; | |
9709 | elf_shdrp = elf_elfsections (s->owner); | |
9710 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
9711 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
9712 | /* PR 290: |
9713 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 9714 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
9715 | sh_info fields. Hence we could get the situation |
9716 | where elfsec is 0. */ | |
9717 | if (elfsec == 0) | |
9718 | { | |
9719 | const struct elf_backend_data *bed | |
9720 | = get_elf_backend_data (s->owner); | |
9721 | if (bed->link_order_error_handler) | |
d003868e AM |
9722 | bed->link_order_error_handler |
9723 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); | |
185d09ad L |
9724 | return 0; |
9725 | } | |
9726 | else | |
9727 | { | |
9728 | s = elf_shdrp[elfsec]->bfd_section; | |
9729 | return s->output_section->vma + s->output_offset; | |
9730 | } | |
0b52efa6 PB |
9731 | } |
9732 | ||
9733 | ||
9734 | /* Compare two sections based on the locations of the sections they are | |
9735 | linked to. Used by elf_fixup_link_order. */ | |
9736 | ||
9737 | static int | |
9738 | compare_link_order (const void * a, const void * b) | |
9739 | { | |
9740 | bfd_vma apos; | |
9741 | bfd_vma bpos; | |
9742 | ||
9743 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
9744 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
9745 | if (apos < bpos) | |
9746 | return -1; | |
9747 | return apos > bpos; | |
9748 | } | |
9749 | ||
9750 | ||
9751 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
9752 | order as their linked sections. Returns false if this could not be done | |
9753 | because an output section includes both ordered and unordered | |
9754 | sections. Ideally we'd do this in the linker proper. */ | |
9755 | ||
9756 | static bfd_boolean | |
9757 | elf_fixup_link_order (bfd *abfd, asection *o) | |
9758 | { | |
9759 | int seen_linkorder; | |
9760 | int seen_other; | |
9761 | int n; | |
9762 | struct bfd_link_order *p; | |
9763 | bfd *sub; | |
9764 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 9765 | unsigned elfsec; |
0b52efa6 | 9766 | struct bfd_link_order **sections; |
d33cdfe3 | 9767 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 9768 | bfd_vma offset; |
3b36f7e6 | 9769 | |
d33cdfe3 L |
9770 | other_sec = NULL; |
9771 | linkorder_sec = NULL; | |
0b52efa6 PB |
9772 | seen_other = 0; |
9773 | seen_linkorder = 0; | |
8423293d | 9774 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 9775 | { |
d33cdfe3 | 9776 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
9777 | { |
9778 | s = p->u.indirect.section; | |
d33cdfe3 L |
9779 | sub = s->owner; |
9780 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
9781 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
9782 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
9783 | && elfsec < elf_numsections (sub) | |
0b52efa6 | 9784 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER) |
d33cdfe3 L |
9785 | { |
9786 | seen_linkorder++; | |
9787 | linkorder_sec = s; | |
9788 | } | |
0b52efa6 | 9789 | else |
d33cdfe3 L |
9790 | { |
9791 | seen_other++; | |
9792 | other_sec = s; | |
9793 | } | |
0b52efa6 PB |
9794 | } |
9795 | else | |
9796 | seen_other++; | |
d33cdfe3 L |
9797 | |
9798 | if (seen_other && seen_linkorder) | |
9799 | { | |
9800 | if (other_sec && linkorder_sec) | |
9801 | (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"), | |
9802 | o, linkorder_sec, | |
9803 | linkorder_sec->owner, other_sec, | |
9804 | other_sec->owner); | |
9805 | else | |
9806 | (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), | |
9807 | o); | |
9808 | bfd_set_error (bfd_error_bad_value); | |
9809 | return FALSE; | |
9810 | } | |
0b52efa6 PB |
9811 | } |
9812 | ||
9813 | if (!seen_linkorder) | |
9814 | return TRUE; | |
9815 | ||
0b52efa6 | 9816 | sections = (struct bfd_link_order **) |
14b1c01e AM |
9817 | bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); |
9818 | if (sections == NULL) | |
9819 | return FALSE; | |
0b52efa6 | 9820 | seen_linkorder = 0; |
3b36f7e6 | 9821 | |
8423293d | 9822 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
9823 | { |
9824 | sections[seen_linkorder++] = p; | |
9825 | } | |
9826 | /* Sort the input sections in the order of their linked section. */ | |
9827 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
9828 | compare_link_order); | |
9829 | ||
9830 | /* Change the offsets of the sections. */ | |
9831 | offset = 0; | |
9832 | for (n = 0; n < seen_linkorder; n++) | |
9833 | { | |
9834 | s = sections[n]->u.indirect.section; | |
461686a3 | 9835 | offset &= ~(bfd_vma) 0 << s->alignment_power; |
0b52efa6 PB |
9836 | s->output_offset = offset; |
9837 | sections[n]->offset = offset; | |
9838 | offset += sections[n]->size; | |
9839 | } | |
9840 | ||
9841 | return TRUE; | |
9842 | } | |
9843 | ||
9844 | ||
c152c796 AM |
9845 | /* Do the final step of an ELF link. */ |
9846 | ||
9847 | bfd_boolean | |
9848 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
9849 | { | |
9850 | bfd_boolean dynamic; | |
9851 | bfd_boolean emit_relocs; | |
9852 | bfd *dynobj; | |
9853 | struct elf_final_link_info finfo; | |
9854 | register asection *o; | |
9855 | register struct bfd_link_order *p; | |
9856 | register bfd *sub; | |
9857 | bfd_size_type max_contents_size; | |
9858 | bfd_size_type max_external_reloc_size; | |
9859 | bfd_size_type max_internal_reloc_count; | |
9860 | bfd_size_type max_sym_count; | |
9861 | bfd_size_type max_sym_shndx_count; | |
9862 | file_ptr off; | |
9863 | Elf_Internal_Sym elfsym; | |
9864 | unsigned int i; | |
9865 | Elf_Internal_Shdr *symtab_hdr; | |
9866 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
9867 | Elf_Internal_Shdr *symstrtab_hdr; | |
9868 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9869 | struct elf_outext_info eoinfo; | |
9870 | bfd_boolean merged; | |
9871 | size_t relativecount = 0; | |
9872 | asection *reldyn = 0; | |
9873 | bfd_size_type amt; | |
104d59d1 JM |
9874 | asection *attr_section = NULL; |
9875 | bfd_vma attr_size = 0; | |
9876 | const char *std_attrs_section; | |
c152c796 AM |
9877 | |
9878 | if (! is_elf_hash_table (info->hash)) | |
9879 | return FALSE; | |
9880 | ||
9881 | if (info->shared) | |
9882 | abfd->flags |= DYNAMIC; | |
9883 | ||
9884 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
9885 | dynobj = elf_hash_table (info)->dynobj; | |
9886 | ||
9887 | emit_relocs = (info->relocatable | |
a4676736 | 9888 | || info->emitrelocations); |
c152c796 AM |
9889 | |
9890 | finfo.info = info; | |
9891 | finfo.output_bfd = abfd; | |
9892 | finfo.symstrtab = _bfd_elf_stringtab_init (); | |
9893 | if (finfo.symstrtab == NULL) | |
9894 | return FALSE; | |
9895 | ||
9896 | if (! dynamic) | |
9897 | { | |
9898 | finfo.dynsym_sec = NULL; | |
9899 | finfo.hash_sec = NULL; | |
9900 | finfo.symver_sec = NULL; | |
9901 | } | |
9902 | else | |
9903 | { | |
9904 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
9905 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
fdc90cb4 | 9906 | BFD_ASSERT (finfo.dynsym_sec != NULL); |
c152c796 AM |
9907 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); |
9908 | /* Note that it is OK if symver_sec is NULL. */ | |
9909 | } | |
9910 | ||
9911 | finfo.contents = NULL; | |
9912 | finfo.external_relocs = NULL; | |
9913 | finfo.internal_relocs = NULL; | |
9914 | finfo.external_syms = NULL; | |
9915 | finfo.locsym_shndx = NULL; | |
9916 | finfo.internal_syms = NULL; | |
9917 | finfo.indices = NULL; | |
9918 | finfo.sections = NULL; | |
9919 | finfo.symbuf = NULL; | |
9920 | finfo.symshndxbuf = NULL; | |
9921 | finfo.symbuf_count = 0; | |
9922 | finfo.shndxbuf_size = 0; | |
9923 | ||
104d59d1 JM |
9924 | /* The object attributes have been merged. Remove the input |
9925 | sections from the link, and set the contents of the output | |
9926 | secton. */ | |
9927 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; | |
9928 | for (o = abfd->sections; o != NULL; o = o->next) | |
9929 | { | |
9930 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) | |
9931 | || strcmp (o->name, ".gnu.attributes") == 0) | |
9932 | { | |
9933 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
9934 | { | |
9935 | asection *input_section; | |
9936 | ||
9937 | if (p->type != bfd_indirect_link_order) | |
9938 | continue; | |
9939 | input_section = p->u.indirect.section; | |
9940 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
9941 | elf_link_input_bfd ignores this section. */ | |
9942 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
9943 | } | |
a0c8462f | 9944 | |
104d59d1 JM |
9945 | attr_size = bfd_elf_obj_attr_size (abfd); |
9946 | if (attr_size) | |
9947 | { | |
9948 | bfd_set_section_size (abfd, o, attr_size); | |
9949 | attr_section = o; | |
9950 | /* Skip this section later on. */ | |
9951 | o->map_head.link_order = NULL; | |
9952 | } | |
9953 | else | |
9954 | o->flags |= SEC_EXCLUDE; | |
9955 | } | |
9956 | } | |
9957 | ||
c152c796 AM |
9958 | /* Count up the number of relocations we will output for each output |
9959 | section, so that we know the sizes of the reloc sections. We | |
9960 | also figure out some maximum sizes. */ | |
9961 | max_contents_size = 0; | |
9962 | max_external_reloc_size = 0; | |
9963 | max_internal_reloc_count = 0; | |
9964 | max_sym_count = 0; | |
9965 | max_sym_shndx_count = 0; | |
9966 | merged = FALSE; | |
9967 | for (o = abfd->sections; o != NULL; o = o->next) | |
9968 | { | |
9969 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
9970 | o->reloc_count = 0; | |
9971 | ||
8423293d | 9972 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
9973 | { |
9974 | unsigned int reloc_count = 0; | |
9975 | struct bfd_elf_section_data *esdi = NULL; | |
9976 | unsigned int *rel_count1; | |
9977 | ||
9978 | if (p->type == bfd_section_reloc_link_order | |
9979 | || p->type == bfd_symbol_reloc_link_order) | |
9980 | reloc_count = 1; | |
9981 | else if (p->type == bfd_indirect_link_order) | |
9982 | { | |
9983 | asection *sec; | |
9984 | ||
9985 | sec = p->u.indirect.section; | |
9986 | esdi = elf_section_data (sec); | |
9987 | ||
9988 | /* Mark all sections which are to be included in the | |
9989 | link. This will normally be every section. We need | |
9990 | to do this so that we can identify any sections which | |
9991 | the linker has decided to not include. */ | |
9992 | sec->linker_mark = TRUE; | |
9993 | ||
9994 | if (sec->flags & SEC_MERGE) | |
9995 | merged = TRUE; | |
9996 | ||
9997 | if (info->relocatable || info->emitrelocations) | |
9998 | reloc_count = sec->reloc_count; | |
9999 | else if (bed->elf_backend_count_relocs) | |
10000 | { | |
10001 | Elf_Internal_Rela * relocs; | |
10002 | ||
1182cb93 AM |
10003 | relocs = _bfd_elf_link_read_relocs (sec->owner, sec, |
10004 | NULL, NULL, | |
c152c796 AM |
10005 | info->keep_memory); |
10006 | ||
1182cb93 AM |
10007 | if (relocs != NULL) |
10008 | { | |
10009 | reloc_count | |
10010 | = (*bed->elf_backend_count_relocs) (sec, relocs); | |
c152c796 | 10011 | |
1182cb93 AM |
10012 | if (elf_section_data (sec)->relocs != relocs) |
10013 | free (relocs); | |
10014 | } | |
c152c796 AM |
10015 | } |
10016 | ||
eea6121a AM |
10017 | if (sec->rawsize > max_contents_size) |
10018 | max_contents_size = sec->rawsize; | |
10019 | if (sec->size > max_contents_size) | |
10020 | max_contents_size = sec->size; | |
c152c796 AM |
10021 | |
10022 | /* We are interested in just local symbols, not all | |
10023 | symbols. */ | |
10024 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
10025 | && (sec->owner->flags & DYNAMIC) == 0) | |
10026 | { | |
10027 | size_t sym_count; | |
10028 | ||
10029 | if (elf_bad_symtab (sec->owner)) | |
10030 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
10031 | / bed->s->sizeof_sym); | |
10032 | else | |
10033 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
10034 | ||
10035 | if (sym_count > max_sym_count) | |
10036 | max_sym_count = sym_count; | |
10037 | ||
10038 | if (sym_count > max_sym_shndx_count | |
10039 | && elf_symtab_shndx (sec->owner) != 0) | |
10040 | max_sym_shndx_count = sym_count; | |
10041 | ||
10042 | if ((sec->flags & SEC_RELOC) != 0) | |
10043 | { | |
10044 | size_t ext_size; | |
10045 | ||
10046 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
10047 | if (ext_size > max_external_reloc_size) | |
10048 | max_external_reloc_size = ext_size; | |
10049 | if (sec->reloc_count > max_internal_reloc_count) | |
10050 | max_internal_reloc_count = sec->reloc_count; | |
10051 | } | |
10052 | } | |
10053 | } | |
10054 | ||
10055 | if (reloc_count == 0) | |
10056 | continue; | |
10057 | ||
10058 | o->reloc_count += reloc_count; | |
10059 | ||
10060 | /* MIPS may have a mix of REL and RELA relocs on sections. | |
10061 | To support this curious ABI we keep reloc counts in | |
10062 | elf_section_data too. We must be careful to add the | |
10063 | relocations from the input section to the right output | |
10064 | count. FIXME: Get rid of one count. We have | |
10065 | o->reloc_count == esdo->rel_count + esdo->rel_count2. */ | |
10066 | rel_count1 = &esdo->rel_count; | |
10067 | if (esdi != NULL) | |
10068 | { | |
10069 | bfd_boolean same_size; | |
10070 | bfd_size_type entsize1; | |
10071 | ||
10072 | entsize1 = esdi->rel_hdr.sh_entsize; | |
10073 | BFD_ASSERT (entsize1 == bed->s->sizeof_rel | |
10074 | || entsize1 == bed->s->sizeof_rela); | |
10075 | same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel); | |
10076 | ||
10077 | if (!same_size) | |
10078 | rel_count1 = &esdo->rel_count2; | |
10079 | ||
10080 | if (esdi->rel_hdr2 != NULL) | |
10081 | { | |
10082 | bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize; | |
10083 | unsigned int alt_count; | |
10084 | unsigned int *rel_count2; | |
10085 | ||
10086 | BFD_ASSERT (entsize2 != entsize1 | |
10087 | && (entsize2 == bed->s->sizeof_rel | |
10088 | || entsize2 == bed->s->sizeof_rela)); | |
10089 | ||
10090 | rel_count2 = &esdo->rel_count2; | |
10091 | if (!same_size) | |
10092 | rel_count2 = &esdo->rel_count; | |
10093 | ||
10094 | /* The following is probably too simplistic if the | |
10095 | backend counts output relocs unusually. */ | |
10096 | BFD_ASSERT (bed->elf_backend_count_relocs == NULL); | |
10097 | alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2); | |
10098 | *rel_count2 += alt_count; | |
10099 | reloc_count -= alt_count; | |
10100 | } | |
10101 | } | |
10102 | *rel_count1 += reloc_count; | |
10103 | } | |
10104 | ||
10105 | if (o->reloc_count > 0) | |
10106 | o->flags |= SEC_RELOC; | |
10107 | else | |
10108 | { | |
10109 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
10110 | set it (this is probably a bug) and if it is set | |
10111 | assign_section_numbers will create a reloc section. */ | |
10112 | o->flags &=~ SEC_RELOC; | |
10113 | } | |
10114 | ||
10115 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
10116 | zero. This is done in elf_fake_sections as well, but forcing | |
10117 | the VMA to 0 here will ensure that relocs against these | |
10118 | sections are handled correctly. */ | |
10119 | if ((o->flags & SEC_ALLOC) == 0 | |
10120 | && ! o->user_set_vma) | |
10121 | o->vma = 0; | |
10122 | } | |
10123 | ||
10124 | if (! info->relocatable && merged) | |
10125 | elf_link_hash_traverse (elf_hash_table (info), | |
10126 | _bfd_elf_link_sec_merge_syms, abfd); | |
10127 | ||
10128 | /* Figure out the file positions for everything but the symbol table | |
10129 | and the relocs. We set symcount to force assign_section_numbers | |
10130 | to create a symbol table. */ | |
10131 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; | |
10132 | BFD_ASSERT (! abfd->output_has_begun); | |
10133 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
10134 | goto error_return; | |
10135 | ||
ee75fd95 | 10136 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
10137 | for (o = abfd->sections; o != NULL; o = o->next) |
10138 | { | |
10139 | if ((o->flags & SEC_RELOC) != 0) | |
10140 | { | |
10141 | if (!(_bfd_elf_link_size_reloc_section | |
10142 | (abfd, &elf_section_data (o)->rel_hdr, o))) | |
10143 | goto error_return; | |
10144 | ||
10145 | if (elf_section_data (o)->rel_hdr2 | |
10146 | && !(_bfd_elf_link_size_reloc_section | |
10147 | (abfd, elf_section_data (o)->rel_hdr2, o))) | |
10148 | goto error_return; | |
10149 | } | |
10150 | ||
10151 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
10152 | to count upwards while actually outputting the relocations. */ | |
10153 | elf_section_data (o)->rel_count = 0; | |
10154 | elf_section_data (o)->rel_count2 = 0; | |
10155 | } | |
10156 | ||
10157 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
10158 | ||
10159 | /* We have now assigned file positions for all the sections except | |
10160 | .symtab and .strtab. We start the .symtab section at the current | |
10161 | file position, and write directly to it. We build the .strtab | |
10162 | section in memory. */ | |
10163 | bfd_get_symcount (abfd) = 0; | |
10164 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
10165 | /* sh_name is set in prep_headers. */ | |
10166 | symtab_hdr->sh_type = SHT_SYMTAB; | |
10167 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
10168 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
10169 | /* sh_link is set in assign_section_numbers. */ | |
10170 | /* sh_info is set below. */ | |
10171 | /* sh_offset is set just below. */ | |
10172 | symtab_hdr->sh_addralign = 1 << bed->s->log_file_align; | |
10173 | ||
10174 | off = elf_tdata (abfd)->next_file_pos; | |
10175 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
10176 | ||
10177 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
10178 | incorrect. We do not yet know the size of the .symtab section. | |
10179 | We correct next_file_pos below, after we do know the size. */ | |
10180 | ||
10181 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
10182 | continuously seeking to the right position in the file. */ | |
10183 | if (! info->keep_memory || max_sym_count < 20) | |
10184 | finfo.symbuf_size = 20; | |
10185 | else | |
10186 | finfo.symbuf_size = max_sym_count; | |
10187 | amt = finfo.symbuf_size; | |
10188 | amt *= bed->s->sizeof_sym; | |
10189 | finfo.symbuf = bfd_malloc (amt); | |
10190 | if (finfo.symbuf == NULL) | |
10191 | goto error_return; | |
10192 | if (elf_numsections (abfd) > SHN_LORESERVE) | |
10193 | { | |
10194 | /* Wild guess at number of output symbols. realloc'd as needed. */ | |
10195 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
10196 | finfo.shndxbuf_size = amt; | |
10197 | amt *= sizeof (Elf_External_Sym_Shndx); | |
10198 | finfo.symshndxbuf = bfd_zmalloc (amt); | |
10199 | if (finfo.symshndxbuf == NULL) | |
10200 | goto error_return; | |
10201 | } | |
10202 | ||
10203 | /* Start writing out the symbol table. The first symbol is always a | |
10204 | dummy symbol. */ | |
10205 | if (info->strip != strip_all | |
10206 | || emit_relocs) | |
10207 | { | |
10208 | elfsym.st_value = 0; | |
10209 | elfsym.st_size = 0; | |
10210 | elfsym.st_info = 0; | |
10211 | elfsym.st_other = 0; | |
10212 | elfsym.st_shndx = SHN_UNDEF; | |
10213 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr, | |
10214 | NULL)) | |
10215 | goto error_return; | |
10216 | } | |
10217 | ||
c152c796 AM |
10218 | /* Output a symbol for each section. We output these even if we are |
10219 | discarding local symbols, since they are used for relocs. These | |
10220 | symbols have no names. We store the index of each one in the | |
10221 | index field of the section, so that we can find it again when | |
10222 | outputting relocs. */ | |
10223 | if (info->strip != strip_all | |
10224 | || emit_relocs) | |
10225 | { | |
10226 | elfsym.st_size = 0; | |
10227 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
10228 | elfsym.st_other = 0; | |
f0b5bb34 | 10229 | elfsym.st_value = 0; |
c152c796 AM |
10230 | for (i = 1; i < elf_numsections (abfd); i++) |
10231 | { | |
10232 | o = bfd_section_from_elf_index (abfd, i); | |
10233 | if (o != NULL) | |
f0b5bb34 AM |
10234 | { |
10235 | o->target_index = bfd_get_symcount (abfd); | |
10236 | elfsym.st_shndx = i; | |
10237 | if (!info->relocatable) | |
10238 | elfsym.st_value = o->vma; | |
10239 | if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL)) | |
10240 | goto error_return; | |
10241 | } | |
c152c796 AM |
10242 | if (i == SHN_LORESERVE - 1) |
10243 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
10244 | } | |
10245 | } | |
10246 | ||
10247 | /* Allocate some memory to hold information read in from the input | |
10248 | files. */ | |
10249 | if (max_contents_size != 0) | |
10250 | { | |
10251 | finfo.contents = bfd_malloc (max_contents_size); | |
10252 | if (finfo.contents == NULL) | |
10253 | goto error_return; | |
10254 | } | |
10255 | ||
10256 | if (max_external_reloc_size != 0) | |
10257 | { | |
10258 | finfo.external_relocs = bfd_malloc (max_external_reloc_size); | |
10259 | if (finfo.external_relocs == NULL) | |
10260 | goto error_return; | |
10261 | } | |
10262 | ||
10263 | if (max_internal_reloc_count != 0) | |
10264 | { | |
10265 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
10266 | amt *= sizeof (Elf_Internal_Rela); | |
10267 | finfo.internal_relocs = bfd_malloc (amt); | |
10268 | if (finfo.internal_relocs == NULL) | |
10269 | goto error_return; | |
10270 | } | |
10271 | ||
10272 | if (max_sym_count != 0) | |
10273 | { | |
10274 | amt = max_sym_count * bed->s->sizeof_sym; | |
10275 | finfo.external_syms = bfd_malloc (amt); | |
10276 | if (finfo.external_syms == NULL) | |
10277 | goto error_return; | |
10278 | ||
10279 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
10280 | finfo.internal_syms = bfd_malloc (amt); | |
10281 | if (finfo.internal_syms == NULL) | |
10282 | goto error_return; | |
10283 | ||
10284 | amt = max_sym_count * sizeof (long); | |
10285 | finfo.indices = bfd_malloc (amt); | |
10286 | if (finfo.indices == NULL) | |
10287 | goto error_return; | |
10288 | ||
10289 | amt = max_sym_count * sizeof (asection *); | |
10290 | finfo.sections = bfd_malloc (amt); | |
10291 | if (finfo.sections == NULL) | |
10292 | goto error_return; | |
10293 | } | |
10294 | ||
10295 | if (max_sym_shndx_count != 0) | |
10296 | { | |
10297 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
10298 | finfo.locsym_shndx = bfd_malloc (amt); | |
10299 | if (finfo.locsym_shndx == NULL) | |
10300 | goto error_return; | |
10301 | } | |
10302 | ||
10303 | if (elf_hash_table (info)->tls_sec) | |
10304 | { | |
10305 | bfd_vma base, end = 0; | |
10306 | asection *sec; | |
10307 | ||
10308 | for (sec = elf_hash_table (info)->tls_sec; | |
10309 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
10310 | sec = sec->next) | |
10311 | { | |
3a800eb9 | 10312 | bfd_size_type size = sec->size; |
c152c796 | 10313 | |
3a800eb9 AM |
10314 | if (size == 0 |
10315 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 10316 | { |
3a800eb9 AM |
10317 | struct bfd_link_order *o = sec->map_tail.link_order; |
10318 | if (o != NULL) | |
10319 | size = o->offset + o->size; | |
c152c796 AM |
10320 | } |
10321 | end = sec->vma + size; | |
10322 | } | |
10323 | base = elf_hash_table (info)->tls_sec->vma; | |
10324 | end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power); | |
10325 | elf_hash_table (info)->tls_size = end - base; | |
10326 | } | |
10327 | ||
0b52efa6 PB |
10328 | /* Reorder SHF_LINK_ORDER sections. */ |
10329 | for (o = abfd->sections; o != NULL; o = o->next) | |
10330 | { | |
10331 | if (!elf_fixup_link_order (abfd, o)) | |
10332 | return FALSE; | |
10333 | } | |
10334 | ||
c152c796 AM |
10335 | /* Since ELF permits relocations to be against local symbols, we |
10336 | must have the local symbols available when we do the relocations. | |
10337 | Since we would rather only read the local symbols once, and we | |
10338 | would rather not keep them in memory, we handle all the | |
10339 | relocations for a single input file at the same time. | |
10340 | ||
10341 | Unfortunately, there is no way to know the total number of local | |
10342 | symbols until we have seen all of them, and the local symbol | |
10343 | indices precede the global symbol indices. This means that when | |
10344 | we are generating relocatable output, and we see a reloc against | |
10345 | a global symbol, we can not know the symbol index until we have | |
10346 | finished examining all the local symbols to see which ones we are | |
10347 | going to output. To deal with this, we keep the relocations in | |
10348 | memory, and don't output them until the end of the link. This is | |
10349 | an unfortunate waste of memory, but I don't see a good way around | |
10350 | it. Fortunately, it only happens when performing a relocatable | |
10351 | link, which is not the common case. FIXME: If keep_memory is set | |
10352 | we could write the relocs out and then read them again; I don't | |
10353 | know how bad the memory loss will be. */ | |
10354 | ||
10355 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
10356 | sub->output_has_begun = FALSE; | |
10357 | for (o = abfd->sections; o != NULL; o = o->next) | |
10358 | { | |
8423293d | 10359 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
10360 | { |
10361 | if (p->type == bfd_indirect_link_order | |
10362 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
10363 | == bfd_target_elf_flavour) | |
10364 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
10365 | { | |
10366 | if (! sub->output_has_begun) | |
10367 | { | |
10368 | if (! elf_link_input_bfd (&finfo, sub)) | |
10369 | goto error_return; | |
10370 | sub->output_has_begun = TRUE; | |
10371 | } | |
10372 | } | |
10373 | else if (p->type == bfd_section_reloc_link_order | |
10374 | || p->type == bfd_symbol_reloc_link_order) | |
10375 | { | |
10376 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
10377 | goto error_return; | |
10378 | } | |
10379 | else | |
10380 | { | |
10381 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
10382 | goto error_return; | |
10383 | } | |
10384 | } | |
10385 | } | |
10386 | ||
c0f00686 L |
10387 | /* Free symbol buffer if needed. */ |
10388 | if (!info->reduce_memory_overheads) | |
10389 | { | |
10390 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
3fcd97f1 JJ |
10391 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
10392 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
10393 | { |
10394 | free (elf_tdata (sub)->symbuf); | |
10395 | elf_tdata (sub)->symbuf = NULL; | |
10396 | } | |
10397 | } | |
10398 | ||
c152c796 AM |
10399 | /* Output any global symbols that got converted to local in a |
10400 | version script or due to symbol visibility. We do this in a | |
10401 | separate step since ELF requires all local symbols to appear | |
10402 | prior to any global symbols. FIXME: We should only do this if | |
10403 | some global symbols were, in fact, converted to become local. | |
10404 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
10405 | eoinfo.failed = FALSE; | |
10406 | eoinfo.finfo = &finfo; | |
10407 | eoinfo.localsyms = TRUE; | |
10408 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
10409 | &eoinfo); | |
10410 | if (eoinfo.failed) | |
10411 | return FALSE; | |
10412 | ||
4e617b1e PB |
10413 | /* If backend needs to output some local symbols not present in the hash |
10414 | table, do it now. */ | |
10415 | if (bed->elf_backend_output_arch_local_syms) | |
10416 | { | |
10417 | typedef bfd_boolean (*out_sym_func) | |
10418 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
10419 | struct elf_link_hash_entry *); | |
10420 | ||
10421 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
10422 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
10423 | return FALSE; | |
10424 | } | |
10425 | ||
c152c796 AM |
10426 | /* That wrote out all the local symbols. Finish up the symbol table |
10427 | with the global symbols. Even if we want to strip everything we | |
10428 | can, we still need to deal with those global symbols that got | |
10429 | converted to local in a version script. */ | |
10430 | ||
10431 | /* The sh_info field records the index of the first non local symbol. */ | |
10432 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
10433 | ||
10434 | if (dynamic | |
10435 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
10436 | { | |
10437 | Elf_Internal_Sym sym; | |
10438 | bfd_byte *dynsym = finfo.dynsym_sec->contents; | |
10439 | long last_local = 0; | |
10440 | ||
10441 | /* Write out the section symbols for the output sections. */ | |
67687978 | 10442 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
c152c796 AM |
10443 | { |
10444 | asection *s; | |
10445 | ||
10446 | sym.st_size = 0; | |
10447 | sym.st_name = 0; | |
10448 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
10449 | sym.st_other = 0; | |
10450 | ||
10451 | for (s = abfd->sections; s != NULL; s = s->next) | |
10452 | { | |
10453 | int indx; | |
10454 | bfd_byte *dest; | |
10455 | long dynindx; | |
10456 | ||
c152c796 | 10457 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
10458 | if (dynindx <= 0) |
10459 | continue; | |
10460 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
10461 | BFD_ASSERT (indx > 0); |
10462 | sym.st_shndx = indx; | |
c0d5a53d L |
10463 | if (! check_dynsym (abfd, &sym)) |
10464 | return FALSE; | |
c152c796 AM |
10465 | sym.st_value = s->vma; |
10466 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
8c37241b JJ |
10467 | if (last_local < dynindx) |
10468 | last_local = dynindx; | |
c152c796 AM |
10469 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
10470 | } | |
c152c796 AM |
10471 | } |
10472 | ||
10473 | /* Write out the local dynsyms. */ | |
10474 | if (elf_hash_table (info)->dynlocal) | |
10475 | { | |
10476 | struct elf_link_local_dynamic_entry *e; | |
10477 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
10478 | { | |
10479 | asection *s; | |
10480 | bfd_byte *dest; | |
10481 | ||
10482 | sym.st_size = e->isym.st_size; | |
10483 | sym.st_other = e->isym.st_other; | |
10484 | ||
10485 | /* Copy the internal symbol as is. | |
10486 | Note that we saved a word of storage and overwrote | |
10487 | the original st_name with the dynstr_index. */ | |
10488 | sym = e->isym; | |
10489 | ||
10490 | if (e->isym.st_shndx != SHN_UNDEF | |
10491 | && (e->isym.st_shndx < SHN_LORESERVE | |
10492 | || e->isym.st_shndx > SHN_HIRESERVE)) | |
10493 | { | |
10494 | s = bfd_section_from_elf_index (e->input_bfd, | |
10495 | e->isym.st_shndx); | |
10496 | ||
10497 | sym.st_shndx = | |
10498 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
10499 | if (! check_dynsym (abfd, &sym)) |
10500 | return FALSE; | |
c152c796 AM |
10501 | sym.st_value = (s->output_section->vma |
10502 | + s->output_offset | |
10503 | + e->isym.st_value); | |
10504 | } | |
10505 | ||
10506 | if (last_local < e->dynindx) | |
10507 | last_local = e->dynindx; | |
10508 | ||
10509 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
10510 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
10511 | } | |
10512 | } | |
10513 | ||
10514 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = | |
10515 | last_local + 1; | |
10516 | } | |
10517 | ||
10518 | /* We get the global symbols from the hash table. */ | |
10519 | eoinfo.failed = FALSE; | |
10520 | eoinfo.localsyms = FALSE; | |
10521 | eoinfo.finfo = &finfo; | |
10522 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
10523 | &eoinfo); | |
10524 | if (eoinfo.failed) | |
10525 | return FALSE; | |
10526 | ||
10527 | /* If backend needs to output some symbols not present in the hash | |
10528 | table, do it now. */ | |
10529 | if (bed->elf_backend_output_arch_syms) | |
10530 | { | |
10531 | typedef bfd_boolean (*out_sym_func) | |
10532 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
10533 | struct elf_link_hash_entry *); | |
10534 | ||
10535 | if (! ((*bed->elf_backend_output_arch_syms) | |
10536 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
10537 | return FALSE; | |
10538 | } | |
10539 | ||
10540 | /* Flush all symbols to the file. */ | |
10541 | if (! elf_link_flush_output_syms (&finfo, bed)) | |
10542 | return FALSE; | |
10543 | ||
10544 | /* Now we know the size of the symtab section. */ | |
10545 | off += symtab_hdr->sh_size; | |
10546 | ||
10547 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
10548 | if (symtab_shndx_hdr->sh_name != 0) | |
10549 | { | |
10550 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
10551 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
10552 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
10553 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
10554 | symtab_shndx_hdr->sh_size = amt; | |
10555 | ||
10556 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
10557 | off, TRUE); | |
10558 | ||
10559 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
10560 | || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) | |
10561 | return FALSE; | |
10562 | } | |
10563 | ||
10564 | ||
10565 | /* Finish up and write out the symbol string table (.strtab) | |
10566 | section. */ | |
10567 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
10568 | /* sh_name was set in prep_headers. */ | |
10569 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
10570 | symstrtab_hdr->sh_flags = 0; | |
10571 | symstrtab_hdr->sh_addr = 0; | |
10572 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
10573 | symstrtab_hdr->sh_entsize = 0; | |
10574 | symstrtab_hdr->sh_link = 0; | |
10575 | symstrtab_hdr->sh_info = 0; | |
10576 | /* sh_offset is set just below. */ | |
10577 | symstrtab_hdr->sh_addralign = 1; | |
10578 | ||
10579 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); | |
10580 | elf_tdata (abfd)->next_file_pos = off; | |
10581 | ||
10582 | if (bfd_get_symcount (abfd) > 0) | |
10583 | { | |
10584 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
10585 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
10586 | return FALSE; | |
10587 | } | |
10588 | ||
10589 | /* Adjust the relocs to have the correct symbol indices. */ | |
10590 | for (o = abfd->sections; o != NULL; o = o->next) | |
10591 | { | |
10592 | if ((o->flags & SEC_RELOC) == 0) | |
10593 | continue; | |
10594 | ||
10595 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, | |
10596 | elf_section_data (o)->rel_count, | |
10597 | elf_section_data (o)->rel_hashes); | |
10598 | if (elf_section_data (o)->rel_hdr2 != NULL) | |
10599 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, | |
10600 | elf_section_data (o)->rel_count2, | |
10601 | (elf_section_data (o)->rel_hashes | |
10602 | + elf_section_data (o)->rel_count)); | |
10603 | ||
10604 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
10605 | trying to swap the relocs out itself. */ | |
10606 | o->reloc_count = 0; | |
10607 | } | |
10608 | ||
10609 | if (dynamic && info->combreloc && dynobj != NULL) | |
10610 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
10611 | ||
10612 | /* If we are linking against a dynamic object, or generating a | |
10613 | shared library, finish up the dynamic linking information. */ | |
10614 | if (dynamic) | |
10615 | { | |
10616 | bfd_byte *dyncon, *dynconend; | |
10617 | ||
10618 | /* Fix up .dynamic entries. */ | |
10619 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
10620 | BFD_ASSERT (o != NULL); | |
10621 | ||
10622 | dyncon = o->contents; | |
eea6121a | 10623 | dynconend = o->contents + o->size; |
c152c796 AM |
10624 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
10625 | { | |
10626 | Elf_Internal_Dyn dyn; | |
10627 | const char *name; | |
10628 | unsigned int type; | |
10629 | ||
10630 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
10631 | ||
10632 | switch (dyn.d_tag) | |
10633 | { | |
10634 | default: | |
10635 | continue; | |
10636 | case DT_NULL: | |
10637 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
10638 | { | |
10639 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
10640 | { | |
10641 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
10642 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
10643 | default: continue; | |
10644 | } | |
10645 | dyn.d_un.d_val = relativecount; | |
10646 | relativecount = 0; | |
10647 | break; | |
10648 | } | |
10649 | continue; | |
10650 | ||
10651 | case DT_INIT: | |
10652 | name = info->init_function; | |
10653 | goto get_sym; | |
10654 | case DT_FINI: | |
10655 | name = info->fini_function; | |
10656 | get_sym: | |
10657 | { | |
10658 | struct elf_link_hash_entry *h; | |
10659 | ||
10660 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
10661 | FALSE, FALSE, TRUE); | |
10662 | if (h != NULL | |
10663 | && (h->root.type == bfd_link_hash_defined | |
10664 | || h->root.type == bfd_link_hash_defweak)) | |
10665 | { | |
10666 | dyn.d_un.d_val = h->root.u.def.value; | |
10667 | o = h->root.u.def.section; | |
10668 | if (o->output_section != NULL) | |
10669 | dyn.d_un.d_val += (o->output_section->vma | |
10670 | + o->output_offset); | |
10671 | else | |
10672 | { | |
10673 | /* The symbol is imported from another shared | |
10674 | library and does not apply to this one. */ | |
10675 | dyn.d_un.d_val = 0; | |
10676 | } | |
10677 | break; | |
10678 | } | |
10679 | } | |
10680 | continue; | |
10681 | ||
10682 | case DT_PREINIT_ARRAYSZ: | |
10683 | name = ".preinit_array"; | |
10684 | goto get_size; | |
10685 | case DT_INIT_ARRAYSZ: | |
10686 | name = ".init_array"; | |
10687 | goto get_size; | |
10688 | case DT_FINI_ARRAYSZ: | |
10689 | name = ".fini_array"; | |
10690 | get_size: | |
10691 | o = bfd_get_section_by_name (abfd, name); | |
10692 | if (o == NULL) | |
10693 | { | |
10694 | (*_bfd_error_handler) | |
d003868e | 10695 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
10696 | goto error_return; |
10697 | } | |
eea6121a | 10698 | if (o->size == 0) |
c152c796 AM |
10699 | (*_bfd_error_handler) |
10700 | (_("warning: %s section has zero size"), name); | |
eea6121a | 10701 | dyn.d_un.d_val = o->size; |
c152c796 AM |
10702 | break; |
10703 | ||
10704 | case DT_PREINIT_ARRAY: | |
10705 | name = ".preinit_array"; | |
10706 | goto get_vma; | |
10707 | case DT_INIT_ARRAY: | |
10708 | name = ".init_array"; | |
10709 | goto get_vma; | |
10710 | case DT_FINI_ARRAY: | |
10711 | name = ".fini_array"; | |
10712 | goto get_vma; | |
10713 | ||
10714 | case DT_HASH: | |
10715 | name = ".hash"; | |
10716 | goto get_vma; | |
fdc90cb4 JJ |
10717 | case DT_GNU_HASH: |
10718 | name = ".gnu.hash"; | |
10719 | goto get_vma; | |
c152c796 AM |
10720 | case DT_STRTAB: |
10721 | name = ".dynstr"; | |
10722 | goto get_vma; | |
10723 | case DT_SYMTAB: | |
10724 | name = ".dynsym"; | |
10725 | goto get_vma; | |
10726 | case DT_VERDEF: | |
10727 | name = ".gnu.version_d"; | |
10728 | goto get_vma; | |
10729 | case DT_VERNEED: | |
10730 | name = ".gnu.version_r"; | |
10731 | goto get_vma; | |
10732 | case DT_VERSYM: | |
10733 | name = ".gnu.version"; | |
10734 | get_vma: | |
10735 | o = bfd_get_section_by_name (abfd, name); | |
10736 | if (o == NULL) | |
10737 | { | |
10738 | (*_bfd_error_handler) | |
d003868e | 10739 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
10740 | goto error_return; |
10741 | } | |
10742 | dyn.d_un.d_ptr = o->vma; | |
10743 | break; | |
10744 | ||
10745 | case DT_REL: | |
10746 | case DT_RELA: | |
10747 | case DT_RELSZ: | |
10748 | case DT_RELASZ: | |
10749 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
10750 | type = SHT_REL; | |
10751 | else | |
10752 | type = SHT_RELA; | |
10753 | dyn.d_un.d_val = 0; | |
10754 | for (i = 1; i < elf_numsections (abfd); i++) | |
10755 | { | |
10756 | Elf_Internal_Shdr *hdr; | |
10757 | ||
10758 | hdr = elf_elfsections (abfd)[i]; | |
10759 | if (hdr->sh_type == type | |
10760 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
10761 | { | |
10762 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
10763 | dyn.d_un.d_val += hdr->sh_size; | |
10764 | else | |
10765 | { | |
10766 | if (dyn.d_un.d_val == 0 | |
10767 | || hdr->sh_addr < dyn.d_un.d_val) | |
10768 | dyn.d_un.d_val = hdr->sh_addr; | |
10769 | } | |
10770 | } | |
10771 | } | |
10772 | break; | |
10773 | } | |
10774 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
10775 | } | |
10776 | } | |
10777 | ||
10778 | /* If we have created any dynamic sections, then output them. */ | |
10779 | if (dynobj != NULL) | |
10780 | { | |
10781 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
10782 | goto error_return; | |
10783 | ||
943284cc DJ |
10784 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
10785 | if (info->warn_shared_textrel && info->shared) | |
10786 | { | |
10787 | bfd_byte *dyncon, *dynconend; | |
10788 | ||
10789 | /* Fix up .dynamic entries. */ | |
10790 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
10791 | BFD_ASSERT (o != NULL); | |
10792 | ||
10793 | dyncon = o->contents; | |
10794 | dynconend = o->contents + o->size; | |
10795 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
10796 | { | |
10797 | Elf_Internal_Dyn dyn; | |
10798 | ||
10799 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
10800 | ||
10801 | if (dyn.d_tag == DT_TEXTREL) | |
10802 | { | |
a0c8462f | 10803 | info->callbacks->einfo |
9267588c | 10804 | (_("%P: warning: creating a DT_TEXTREL in a shared object.\n")); |
943284cc DJ |
10805 | break; |
10806 | } | |
10807 | } | |
10808 | } | |
10809 | ||
c152c796 AM |
10810 | for (o = dynobj->sections; o != NULL; o = o->next) |
10811 | { | |
10812 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 10813 | || o->size == 0 |
c152c796 AM |
10814 | || o->output_section == bfd_abs_section_ptr) |
10815 | continue; | |
10816 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
10817 | { | |
10818 | /* At this point, we are only interested in sections | |
10819 | created by _bfd_elf_link_create_dynamic_sections. */ | |
10820 | continue; | |
10821 | } | |
3722b82f AM |
10822 | if (elf_hash_table (info)->stab_info.stabstr == o) |
10823 | continue; | |
eea6121a AM |
10824 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
10825 | continue; | |
c152c796 AM |
10826 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
10827 | != SHT_STRTAB) | |
10828 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
10829 | { | |
10830 | if (! bfd_set_section_contents (abfd, o->output_section, | |
10831 | o->contents, | |
10832 | (file_ptr) o->output_offset, | |
eea6121a | 10833 | o->size)) |
c152c796 AM |
10834 | goto error_return; |
10835 | } | |
10836 | else | |
10837 | { | |
10838 | /* The contents of the .dynstr section are actually in a | |
10839 | stringtab. */ | |
10840 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
10841 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
10842 | || ! _bfd_elf_strtab_emit (abfd, | |
10843 | elf_hash_table (info)->dynstr)) | |
10844 | goto error_return; | |
10845 | } | |
10846 | } | |
10847 | } | |
10848 | ||
10849 | if (info->relocatable) | |
10850 | { | |
10851 | bfd_boolean failed = FALSE; | |
10852 | ||
10853 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
10854 | if (failed) | |
10855 | goto error_return; | |
10856 | } | |
10857 | ||
10858 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 10859 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
10860 | { |
10861 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
10862 | goto error_return; | |
10863 | } | |
10864 | ||
10865 | if (info->eh_frame_hdr) | |
10866 | { | |
10867 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) | |
10868 | goto error_return; | |
10869 | } | |
10870 | ||
10871 | if (finfo.symstrtab != NULL) | |
10872 | _bfd_stringtab_free (finfo.symstrtab); | |
10873 | if (finfo.contents != NULL) | |
10874 | free (finfo.contents); | |
10875 | if (finfo.external_relocs != NULL) | |
10876 | free (finfo.external_relocs); | |
10877 | if (finfo.internal_relocs != NULL) | |
10878 | free (finfo.internal_relocs); | |
10879 | if (finfo.external_syms != NULL) | |
10880 | free (finfo.external_syms); | |
10881 | if (finfo.locsym_shndx != NULL) | |
10882 | free (finfo.locsym_shndx); | |
10883 | if (finfo.internal_syms != NULL) | |
10884 | free (finfo.internal_syms); | |
10885 | if (finfo.indices != NULL) | |
10886 | free (finfo.indices); | |
10887 | if (finfo.sections != NULL) | |
10888 | free (finfo.sections); | |
10889 | if (finfo.symbuf != NULL) | |
10890 | free (finfo.symbuf); | |
10891 | if (finfo.symshndxbuf != NULL) | |
10892 | free (finfo.symshndxbuf); | |
10893 | for (o = abfd->sections; o != NULL; o = o->next) | |
10894 | { | |
10895 | if ((o->flags & SEC_RELOC) != 0 | |
10896 | && elf_section_data (o)->rel_hashes != NULL) | |
10897 | free (elf_section_data (o)->rel_hashes); | |
10898 | } | |
10899 | ||
10900 | elf_tdata (abfd)->linker = TRUE; | |
10901 | ||
104d59d1 JM |
10902 | if (attr_section) |
10903 | { | |
10904 | bfd_byte *contents = bfd_malloc (attr_size); | |
10905 | if (contents == NULL) | |
d0f16d5e | 10906 | return FALSE; /* Bail out and fail. */ |
104d59d1 JM |
10907 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
10908 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
10909 | free (contents); | |
10910 | } | |
10911 | ||
c152c796 AM |
10912 | return TRUE; |
10913 | ||
10914 | error_return: | |
10915 | if (finfo.symstrtab != NULL) | |
10916 | _bfd_stringtab_free (finfo.symstrtab); | |
10917 | if (finfo.contents != NULL) | |
10918 | free (finfo.contents); | |
10919 | if (finfo.external_relocs != NULL) | |
10920 | free (finfo.external_relocs); | |
10921 | if (finfo.internal_relocs != NULL) | |
10922 | free (finfo.internal_relocs); | |
10923 | if (finfo.external_syms != NULL) | |
10924 | free (finfo.external_syms); | |
10925 | if (finfo.locsym_shndx != NULL) | |
10926 | free (finfo.locsym_shndx); | |
10927 | if (finfo.internal_syms != NULL) | |
10928 | free (finfo.internal_syms); | |
10929 | if (finfo.indices != NULL) | |
10930 | free (finfo.indices); | |
10931 | if (finfo.sections != NULL) | |
10932 | free (finfo.sections); | |
10933 | if (finfo.symbuf != NULL) | |
10934 | free (finfo.symbuf); | |
10935 | if (finfo.symshndxbuf != NULL) | |
10936 | free (finfo.symshndxbuf); | |
10937 | for (o = abfd->sections; o != NULL; o = o->next) | |
10938 | { | |
10939 | if ((o->flags & SEC_RELOC) != 0 | |
10940 | && elf_section_data (o)->rel_hashes != NULL) | |
10941 | free (elf_section_data (o)->rel_hashes); | |
10942 | } | |
10943 | ||
10944 | return FALSE; | |
10945 | } | |
10946 | \f | |
5241d853 RS |
10947 | /* Initialize COOKIE for input bfd ABFD. */ |
10948 | ||
10949 | static bfd_boolean | |
10950 | init_reloc_cookie (struct elf_reloc_cookie *cookie, | |
10951 | struct bfd_link_info *info, bfd *abfd) | |
10952 | { | |
10953 | Elf_Internal_Shdr *symtab_hdr; | |
10954 | const struct elf_backend_data *bed; | |
10955 | ||
10956 | bed = get_elf_backend_data (abfd); | |
10957 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
10958 | ||
10959 | cookie->abfd = abfd; | |
10960 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
10961 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
10962 | if (cookie->bad_symtab) | |
10963 | { | |
10964 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10965 | cookie->extsymoff = 0; | |
10966 | } | |
10967 | else | |
10968 | { | |
10969 | cookie->locsymcount = symtab_hdr->sh_info; | |
10970 | cookie->extsymoff = symtab_hdr->sh_info; | |
10971 | } | |
10972 | ||
10973 | if (bed->s->arch_size == 32) | |
10974 | cookie->r_sym_shift = 8; | |
10975 | else | |
10976 | cookie->r_sym_shift = 32; | |
10977 | ||
10978 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
10979 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
10980 | { | |
10981 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
10982 | cookie->locsymcount, 0, | |
10983 | NULL, NULL, NULL); | |
10984 | if (cookie->locsyms == NULL) | |
10985 | { | |
10986 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
10987 | return FALSE; | |
10988 | } | |
10989 | if (info->keep_memory) | |
10990 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
10991 | } | |
10992 | return TRUE; | |
10993 | } | |
10994 | ||
10995 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
10996 | ||
10997 | static void | |
10998 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
10999 | { | |
11000 | Elf_Internal_Shdr *symtab_hdr; | |
11001 | ||
11002 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11003 | if (cookie->locsyms != NULL | |
11004 | && symtab_hdr->contents != (unsigned char *) cookie->locsyms) | |
11005 | free (cookie->locsyms); | |
11006 | } | |
11007 | ||
11008 | /* Initialize the relocation information in COOKIE for input section SEC | |
11009 | of input bfd ABFD. */ | |
11010 | ||
11011 | static bfd_boolean | |
11012 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
11013 | struct bfd_link_info *info, bfd *abfd, | |
11014 | asection *sec) | |
11015 | { | |
11016 | const struct elf_backend_data *bed; | |
11017 | ||
11018 | if (sec->reloc_count == 0) | |
11019 | { | |
11020 | cookie->rels = NULL; | |
11021 | cookie->relend = NULL; | |
11022 | } | |
11023 | else | |
11024 | { | |
11025 | bed = get_elf_backend_data (abfd); | |
11026 | ||
11027 | cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
11028 | info->keep_memory); | |
11029 | if (cookie->rels == NULL) | |
11030 | return FALSE; | |
11031 | cookie->rel = cookie->rels; | |
11032 | cookie->relend = (cookie->rels | |
11033 | + sec->reloc_count * bed->s->int_rels_per_ext_rel); | |
11034 | } | |
11035 | cookie->rel = cookie->rels; | |
11036 | return TRUE; | |
11037 | } | |
11038 | ||
11039 | /* Free the memory allocated by init_reloc_cookie_rels, | |
11040 | if appropriate. */ | |
11041 | ||
11042 | static void | |
11043 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
11044 | asection *sec) | |
11045 | { | |
11046 | if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) | |
11047 | free (cookie->rels); | |
11048 | } | |
11049 | ||
11050 | /* Initialize the whole of COOKIE for input section SEC. */ | |
11051 | ||
11052 | static bfd_boolean | |
11053 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
11054 | struct bfd_link_info *info, | |
11055 | asection *sec) | |
11056 | { | |
11057 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
11058 | goto error1; | |
11059 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
11060 | goto error2; | |
11061 | return TRUE; | |
11062 | ||
11063 | error2: | |
11064 | fini_reloc_cookie (cookie, sec->owner); | |
11065 | error1: | |
11066 | return FALSE; | |
11067 | } | |
11068 | ||
11069 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
11070 | if appropriate. */ | |
11071 | ||
11072 | static void | |
11073 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
11074 | asection *sec) | |
11075 | { | |
11076 | fini_reloc_cookie_rels (cookie, sec); | |
11077 | fini_reloc_cookie (cookie, sec->owner); | |
11078 | } | |
11079 | \f | |
c152c796 AM |
11080 | /* Garbage collect unused sections. */ |
11081 | ||
07adf181 AM |
11082 | /* Default gc_mark_hook. */ |
11083 | ||
11084 | asection * | |
11085 | _bfd_elf_gc_mark_hook (asection *sec, | |
11086 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
11087 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
11088 | struct elf_link_hash_entry *h, | |
11089 | Elf_Internal_Sym *sym) | |
11090 | { | |
11091 | if (h != NULL) | |
11092 | { | |
11093 | switch (h->root.type) | |
11094 | { | |
11095 | case bfd_link_hash_defined: | |
11096 | case bfd_link_hash_defweak: | |
11097 | return h->root.u.def.section; | |
11098 | ||
11099 | case bfd_link_hash_common: | |
11100 | return h->root.u.c.p->section; | |
11101 | ||
11102 | default: | |
11103 | break; | |
11104 | } | |
11105 | } | |
11106 | else | |
11107 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
11108 | ||
11109 | return NULL; | |
11110 | } | |
11111 | ||
5241d853 RS |
11112 | /* COOKIE->rel describes a relocation against section SEC, which is |
11113 | a section we've decided to keep. Return the section that contains | |
11114 | the relocation symbol, or NULL if no section contains it. */ | |
11115 | ||
11116 | asection * | |
11117 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
11118 | elf_gc_mark_hook_fn gc_mark_hook, | |
11119 | struct elf_reloc_cookie *cookie) | |
11120 | { | |
11121 | unsigned long r_symndx; | |
11122 | struct elf_link_hash_entry *h; | |
11123 | ||
11124 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
11125 | if (r_symndx == 0) | |
11126 | return NULL; | |
11127 | ||
11128 | if (r_symndx >= cookie->locsymcount | |
11129 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
11130 | { | |
11131 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
11132 | while (h->root.type == bfd_link_hash_indirect | |
11133 | || h->root.type == bfd_link_hash_warning) | |
11134 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11135 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); | |
11136 | } | |
11137 | ||
11138 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
11139 | &cookie->locsyms[r_symndx]); | |
11140 | } | |
11141 | ||
11142 | /* COOKIE->rel describes a relocation against section SEC, which is | |
11143 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 11144 | the relocation symbol. */ |
5241d853 RS |
11145 | |
11146 | bfd_boolean | |
11147 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, | |
11148 | asection *sec, | |
11149 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 11150 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
11151 | { |
11152 | asection *rsec; | |
11153 | ||
11154 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie); | |
11155 | if (rsec && !rsec->gc_mark) | |
11156 | { | |
11157 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
11158 | rsec->gc_mark = 1; | |
5241d853 RS |
11159 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) |
11160 | return FALSE; | |
11161 | } | |
11162 | return TRUE; | |
11163 | } | |
11164 | ||
07adf181 AM |
11165 | /* The mark phase of garbage collection. For a given section, mark |
11166 | it and any sections in this section's group, and all the sections | |
11167 | which define symbols to which it refers. */ | |
11168 | ||
ccfa59ea AM |
11169 | bfd_boolean |
11170 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
11171 | asection *sec, | |
6a5bb875 | 11172 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
11173 | { |
11174 | bfd_boolean ret; | |
9d0a14d3 | 11175 | asection *group_sec, *eh_frame; |
c152c796 AM |
11176 | |
11177 | sec->gc_mark = 1; | |
11178 | ||
11179 | /* Mark all the sections in the group. */ | |
11180 | group_sec = elf_section_data (sec)->next_in_group; | |
11181 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 11182 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
11183 | return FALSE; |
11184 | ||
11185 | /* Look through the section relocs. */ | |
11186 | ret = TRUE; | |
9d0a14d3 RS |
11187 | eh_frame = elf_eh_frame_section (sec->owner); |
11188 | if ((sec->flags & SEC_RELOC) != 0 | |
11189 | && sec->reloc_count > 0 | |
11190 | && sec != eh_frame) | |
c152c796 | 11191 | { |
5241d853 | 11192 | struct elf_reloc_cookie cookie; |
c152c796 | 11193 | |
5241d853 RS |
11194 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
11195 | ret = FALSE; | |
c152c796 | 11196 | else |
c152c796 | 11197 | { |
5241d853 | 11198 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 11199 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 RS |
11200 | { |
11201 | ret = FALSE; | |
11202 | break; | |
11203 | } | |
11204 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
11205 | } |
11206 | } | |
9d0a14d3 RS |
11207 | |
11208 | if (ret && eh_frame && elf_fde_list (sec)) | |
11209 | { | |
11210 | struct elf_reloc_cookie cookie; | |
11211 | ||
11212 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
11213 | ret = FALSE; | |
11214 | else | |
11215 | { | |
11216 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
11217 | gc_mark_hook, &cookie)) | |
11218 | ret = FALSE; | |
11219 | fini_reloc_cookie_for_section (&cookie, eh_frame); | |
11220 | } | |
11221 | } | |
11222 | ||
c152c796 AM |
11223 | return ret; |
11224 | } | |
11225 | ||
11226 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ | |
11227 | ||
c17d87de NC |
11228 | struct elf_gc_sweep_symbol_info |
11229 | { | |
ccabcbe5 AM |
11230 | struct bfd_link_info *info; |
11231 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
11232 | bfd_boolean); | |
11233 | }; | |
11234 | ||
c152c796 | 11235 | static bfd_boolean |
ccabcbe5 | 11236 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) |
c152c796 | 11237 | { |
c152c796 AM |
11238 | if (h->root.type == bfd_link_hash_warning) |
11239 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11240 | ||
ccabcbe5 AM |
11241 | if ((h->root.type == bfd_link_hash_defined |
11242 | || h->root.type == bfd_link_hash_defweak) | |
11243 | && !h->root.u.def.section->gc_mark | |
11244 | && !(h->root.u.def.section->owner->flags & DYNAMIC)) | |
11245 | { | |
11246 | struct elf_gc_sweep_symbol_info *inf = data; | |
11247 | (*inf->hide_symbol) (inf->info, h, TRUE); | |
11248 | } | |
c152c796 AM |
11249 | |
11250 | return TRUE; | |
11251 | } | |
11252 | ||
11253 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
11254 | ||
11255 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
11256 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
11257 | ||
11258 | static bfd_boolean | |
ccabcbe5 | 11259 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
11260 | { |
11261 | bfd *sub; | |
ccabcbe5 AM |
11262 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11263 | gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; | |
11264 | unsigned long section_sym_count; | |
11265 | struct elf_gc_sweep_symbol_info sweep_info; | |
c152c796 AM |
11266 | |
11267 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
11268 | { | |
11269 | asection *o; | |
11270 | ||
11271 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
11272 | continue; | |
11273 | ||
11274 | for (o = sub->sections; o != NULL; o = o->next) | |
11275 | { | |
7c2c8505 AM |
11276 | /* Keep debug and special sections. */ |
11277 | if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 | |
dea5f36a | 11278 | || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) |
c152c796 AM |
11279 | o->gc_mark = 1; |
11280 | ||
11281 | if (o->gc_mark) | |
11282 | continue; | |
11283 | ||
11284 | /* Skip sweeping sections already excluded. */ | |
11285 | if (o->flags & SEC_EXCLUDE) | |
11286 | continue; | |
11287 | ||
11288 | /* Since this is early in the link process, it is simple | |
11289 | to remove a section from the output. */ | |
11290 | o->flags |= SEC_EXCLUDE; | |
11291 | ||
c55fe096 | 11292 | if (info->print_gc_sections && o->size != 0) |
c17d87de NC |
11293 | _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name); |
11294 | ||
c152c796 AM |
11295 | /* But we also have to update some of the relocation |
11296 | info we collected before. */ | |
11297 | if (gc_sweep_hook | |
e8aaee2a AM |
11298 | && (o->flags & SEC_RELOC) != 0 |
11299 | && o->reloc_count > 0 | |
11300 | && !bfd_is_abs_section (o->output_section)) | |
c152c796 AM |
11301 | { |
11302 | Elf_Internal_Rela *internal_relocs; | |
11303 | bfd_boolean r; | |
11304 | ||
11305 | internal_relocs | |
11306 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
11307 | info->keep_memory); | |
11308 | if (internal_relocs == NULL) | |
11309 | return FALSE; | |
11310 | ||
11311 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
11312 | ||
11313 | if (elf_section_data (o)->relocs != internal_relocs) | |
11314 | free (internal_relocs); | |
11315 | ||
11316 | if (!r) | |
11317 | return FALSE; | |
11318 | } | |
11319 | } | |
11320 | } | |
11321 | ||
11322 | /* Remove the symbols that were in the swept sections from the dynamic | |
11323 | symbol table. GCFIXME: Anyone know how to get them out of the | |
11324 | static symbol table as well? */ | |
ccabcbe5 AM |
11325 | sweep_info.info = info; |
11326 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
11327 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
11328 | &sweep_info); | |
c152c796 | 11329 | |
ccabcbe5 | 11330 | _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); |
c152c796 AM |
11331 | return TRUE; |
11332 | } | |
11333 | ||
11334 | /* Propagate collected vtable information. This is called through | |
11335 | elf_link_hash_traverse. */ | |
11336 | ||
11337 | static bfd_boolean | |
11338 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
11339 | { | |
11340 | if (h->root.type == bfd_link_hash_warning) | |
11341 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11342 | ||
11343 | /* Those that are not vtables. */ | |
f6e332e6 | 11344 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
11345 | return TRUE; |
11346 | ||
11347 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 11348 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
11349 | return TRUE; |
11350 | ||
11351 | /* If we've already been done, exit. */ | |
f6e332e6 | 11352 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
11353 | return TRUE; |
11354 | ||
11355 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 11356 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 11357 | |
f6e332e6 | 11358 | if (h->vtable->used == NULL) |
c152c796 AM |
11359 | { |
11360 | /* None of this table's entries were referenced. Re-use the | |
11361 | parent's table. */ | |
f6e332e6 AM |
11362 | h->vtable->used = h->vtable->parent->vtable->used; |
11363 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
11364 | } |
11365 | else | |
11366 | { | |
11367 | size_t n; | |
11368 | bfd_boolean *cu, *pu; | |
11369 | ||
11370 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 11371 | cu = h->vtable->used; |
c152c796 | 11372 | cu[-1] = TRUE; |
f6e332e6 | 11373 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
11374 | if (pu != NULL) |
11375 | { | |
11376 | const struct elf_backend_data *bed; | |
11377 | unsigned int log_file_align; | |
11378 | ||
11379 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
11380 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 11381 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
11382 | while (n--) |
11383 | { | |
11384 | if (*pu) | |
11385 | *cu = TRUE; | |
11386 | pu++; | |
11387 | cu++; | |
11388 | } | |
11389 | } | |
11390 | } | |
11391 | ||
11392 | return TRUE; | |
11393 | } | |
11394 | ||
11395 | static bfd_boolean | |
11396 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
11397 | { | |
11398 | asection *sec; | |
11399 | bfd_vma hstart, hend; | |
11400 | Elf_Internal_Rela *relstart, *relend, *rel; | |
11401 | const struct elf_backend_data *bed; | |
11402 | unsigned int log_file_align; | |
11403 | ||
11404 | if (h->root.type == bfd_link_hash_warning) | |
11405 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11406 | ||
11407 | /* Take care of both those symbols that do not describe vtables as | |
11408 | well as those that are not loaded. */ | |
f6e332e6 | 11409 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
11410 | return TRUE; |
11411 | ||
11412 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
11413 | || h->root.type == bfd_link_hash_defweak); | |
11414 | ||
11415 | sec = h->root.u.def.section; | |
11416 | hstart = h->root.u.def.value; | |
11417 | hend = hstart + h->size; | |
11418 | ||
11419 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
11420 | if (!relstart) | |
11421 | return *(bfd_boolean *) okp = FALSE; | |
11422 | bed = get_elf_backend_data (sec->owner); | |
11423 | log_file_align = bed->s->log_file_align; | |
11424 | ||
11425 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
11426 | ||
11427 | for (rel = relstart; rel < relend; ++rel) | |
11428 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
11429 | { | |
11430 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
11431 | if (h->vtable->used |
11432 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
11433 | { |
11434 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 11435 | if (h->vtable->used[entry]) |
c152c796 AM |
11436 | continue; |
11437 | } | |
11438 | /* Otherwise, kill it. */ | |
11439 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
11440 | } | |
11441 | ||
11442 | return TRUE; | |
11443 | } | |
11444 | ||
87538722 AM |
11445 | /* Mark sections containing dynamically referenced symbols. When |
11446 | building shared libraries, we must assume that any visible symbol is | |
11447 | referenced. */ | |
715df9b8 | 11448 | |
64d03ab5 AM |
11449 | bfd_boolean |
11450 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 11451 | { |
87538722 AM |
11452 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
11453 | ||
715df9b8 EB |
11454 | if (h->root.type == bfd_link_hash_warning) |
11455 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11456 | ||
11457 | if ((h->root.type == bfd_link_hash_defined | |
11458 | || h->root.type == bfd_link_hash_defweak) | |
87538722 | 11459 | && (h->ref_dynamic |
5adcfd8b | 11460 | || (!info->executable |
87538722 AM |
11461 | && h->def_regular |
11462 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL | |
11463 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN))) | |
715df9b8 EB |
11464 | h->root.u.def.section->flags |= SEC_KEEP; |
11465 | ||
11466 | return TRUE; | |
11467 | } | |
3b36f7e6 | 11468 | |
74f0fb50 AM |
11469 | /* Keep all sections containing symbols undefined on the command-line, |
11470 | and the section containing the entry symbol. */ | |
11471 | ||
11472 | void | |
11473 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
11474 | { | |
11475 | struct bfd_sym_chain *sym; | |
11476 | ||
11477 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
11478 | { | |
11479 | struct elf_link_hash_entry *h; | |
11480 | ||
11481 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
11482 | FALSE, FALSE, FALSE); | |
11483 | ||
11484 | if (h != NULL | |
11485 | && (h->root.type == bfd_link_hash_defined | |
11486 | || h->root.type == bfd_link_hash_defweak) | |
11487 | && !bfd_is_abs_section (h->root.u.def.section)) | |
11488 | h->root.u.def.section->flags |= SEC_KEEP; | |
11489 | } | |
11490 | } | |
11491 | ||
c152c796 AM |
11492 | /* Do mark and sweep of unused sections. */ |
11493 | ||
11494 | bfd_boolean | |
11495 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
11496 | { | |
11497 | bfd_boolean ok = TRUE; | |
11498 | bfd *sub; | |
6a5bb875 | 11499 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 11500 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 11501 | |
64d03ab5 | 11502 | if (!bed->can_gc_sections |
715df9b8 | 11503 | || !is_elf_hash_table (info->hash)) |
c152c796 AM |
11504 | { |
11505 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
11506 | return TRUE; | |
11507 | } | |
11508 | ||
74f0fb50 AM |
11509 | bed->gc_keep (info); |
11510 | ||
9d0a14d3 RS |
11511 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
11512 | at the .eh_frame section if we can mark the FDEs individually. */ | |
11513 | _bfd_elf_begin_eh_frame_parsing (info); | |
11514 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
11515 | { | |
11516 | asection *sec; | |
11517 | struct elf_reloc_cookie cookie; | |
11518 | ||
11519 | sec = bfd_get_section_by_name (sub, ".eh_frame"); | |
11520 | if (sec && init_reloc_cookie_for_section (&cookie, info, sec)) | |
11521 | { | |
11522 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
11523 | if (elf_section_data (sec)->sec_info) | |
11524 | elf_eh_frame_section (sub) = sec; | |
11525 | fini_reloc_cookie_for_section (&cookie, sec); | |
11526 | } | |
11527 | } | |
11528 | _bfd_elf_end_eh_frame_parsing (info); | |
11529 | ||
c152c796 AM |
11530 | /* Apply transitive closure to the vtable entry usage info. */ |
11531 | elf_link_hash_traverse (elf_hash_table (info), | |
11532 | elf_gc_propagate_vtable_entries_used, | |
11533 | &ok); | |
11534 | if (!ok) | |
11535 | return FALSE; | |
11536 | ||
11537 | /* Kill the vtable relocations that were not used. */ | |
11538 | elf_link_hash_traverse (elf_hash_table (info), | |
11539 | elf_gc_smash_unused_vtentry_relocs, | |
11540 | &ok); | |
11541 | if (!ok) | |
11542 | return FALSE; | |
11543 | ||
715df9b8 EB |
11544 | /* Mark dynamically referenced symbols. */ |
11545 | if (elf_hash_table (info)->dynamic_sections_created) | |
11546 | elf_link_hash_traverse (elf_hash_table (info), | |
64d03ab5 | 11547 | bed->gc_mark_dynamic_ref, |
87538722 | 11548 | info); |
c152c796 | 11549 | |
715df9b8 | 11550 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 11551 | gc_mark_hook = bed->gc_mark_hook; |
c152c796 AM |
11552 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) |
11553 | { | |
11554 | asection *o; | |
11555 | ||
11556 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
11557 | continue; | |
11558 | ||
11559 | for (o = sub->sections; o != NULL; o = o->next) | |
a14a5de3 | 11560 | if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark) |
39c2f51b AM |
11561 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) |
11562 | return FALSE; | |
c152c796 AM |
11563 | } |
11564 | ||
6a5bb875 PB |
11565 | /* Allow the backend to mark additional target specific sections. */ |
11566 | if (bed->gc_mark_extra_sections) | |
74f0fb50 | 11567 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 11568 | |
c152c796 | 11569 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 11570 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
11571 | } |
11572 | \f | |
11573 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
11574 | ||
11575 | bfd_boolean | |
11576 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
11577 | asection *sec, | |
11578 | struct elf_link_hash_entry *h, | |
11579 | bfd_vma offset) | |
11580 | { | |
11581 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
11582 | struct elf_link_hash_entry **search, *child; | |
11583 | bfd_size_type extsymcount; | |
11584 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11585 | ||
11586 | /* The sh_info field of the symtab header tells us where the | |
11587 | external symbols start. We don't care about the local symbols at | |
11588 | this point. */ | |
11589 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
11590 | if (!elf_bad_symtab (abfd)) | |
11591 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
11592 | ||
11593 | sym_hashes = elf_sym_hashes (abfd); | |
11594 | sym_hashes_end = sym_hashes + extsymcount; | |
11595 | ||
11596 | /* Hunt down the child symbol, which is in this section at the same | |
11597 | offset as the relocation. */ | |
11598 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
11599 | { | |
11600 | if ((child = *search) != NULL | |
11601 | && (child->root.type == bfd_link_hash_defined | |
11602 | || child->root.type == bfd_link_hash_defweak) | |
11603 | && child->root.u.def.section == sec | |
11604 | && child->root.u.def.value == offset) | |
11605 | goto win; | |
11606 | } | |
11607 | ||
d003868e AM |
11608 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
11609 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
11610 | bfd_set_error (bfd_error_invalid_operation); |
11611 | return FALSE; | |
11612 | ||
11613 | win: | |
f6e332e6 AM |
11614 | if (!child->vtable) |
11615 | { | |
11616 | child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable)); | |
11617 | if (!child->vtable) | |
11618 | return FALSE; | |
11619 | } | |
c152c796 AM |
11620 | if (!h) |
11621 | { | |
11622 | /* This *should* only be the absolute section. It could potentially | |
11623 | be that someone has defined a non-global vtable though, which | |
11624 | would be bad. It isn't worth paging in the local symbols to be | |
11625 | sure though; that case should simply be handled by the assembler. */ | |
11626 | ||
f6e332e6 | 11627 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
11628 | } |
11629 | else | |
f6e332e6 | 11630 | child->vtable->parent = h; |
c152c796 AM |
11631 | |
11632 | return TRUE; | |
11633 | } | |
11634 | ||
11635 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
11636 | ||
11637 | bfd_boolean | |
11638 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
11639 | asection *sec ATTRIBUTE_UNUSED, | |
11640 | struct elf_link_hash_entry *h, | |
11641 | bfd_vma addend) | |
11642 | { | |
11643 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11644 | unsigned int log_file_align = bed->s->log_file_align; | |
11645 | ||
f6e332e6 AM |
11646 | if (!h->vtable) |
11647 | { | |
11648 | h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable)); | |
11649 | if (!h->vtable) | |
11650 | return FALSE; | |
11651 | } | |
11652 | ||
11653 | if (addend >= h->vtable->size) | |
c152c796 AM |
11654 | { |
11655 | size_t size, bytes, file_align; | |
f6e332e6 | 11656 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
11657 | |
11658 | /* While the symbol is undefined, we have to be prepared to handle | |
11659 | a zero size. */ | |
11660 | file_align = 1 << log_file_align; | |
11661 | if (h->root.type == bfd_link_hash_undefined) | |
11662 | size = addend + file_align; | |
11663 | else | |
11664 | { | |
11665 | size = h->size; | |
11666 | if (addend >= size) | |
11667 | { | |
11668 | /* Oops! We've got a reference past the defined end of | |
11669 | the table. This is probably a bug -- shall we warn? */ | |
11670 | size = addend + file_align; | |
11671 | } | |
11672 | } | |
11673 | size = (size + file_align - 1) & -file_align; | |
11674 | ||
11675 | /* Allocate one extra entry for use as a "done" flag for the | |
11676 | consolidation pass. */ | |
11677 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
11678 | ||
11679 | if (ptr) | |
11680 | { | |
11681 | ptr = bfd_realloc (ptr - 1, bytes); | |
11682 | ||
11683 | if (ptr != NULL) | |
11684 | { | |
11685 | size_t oldbytes; | |
11686 | ||
f6e332e6 | 11687 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
11688 | * sizeof (bfd_boolean)); |
11689 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
11690 | } | |
11691 | } | |
11692 | else | |
11693 | ptr = bfd_zmalloc (bytes); | |
11694 | ||
11695 | if (ptr == NULL) | |
11696 | return FALSE; | |
11697 | ||
11698 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
11699 | h->vtable->used = ptr + 1; |
11700 | h->vtable->size = size; | |
c152c796 AM |
11701 | } |
11702 | ||
f6e332e6 | 11703 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
11704 | |
11705 | return TRUE; | |
11706 | } | |
11707 | ||
11708 | struct alloc_got_off_arg { | |
11709 | bfd_vma gotoff; | |
11710 | unsigned int got_elt_size; | |
11711 | }; | |
11712 | ||
11713 | /* We need a special top-level link routine to convert got reference counts | |
11714 | to real got offsets. */ | |
11715 | ||
11716 | static bfd_boolean | |
11717 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
11718 | { | |
11719 | struct alloc_got_off_arg *gofarg = arg; | |
11720 | ||
11721 | if (h->root.type == bfd_link_hash_warning) | |
11722 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11723 | ||
11724 | if (h->got.refcount > 0) | |
11725 | { | |
11726 | h->got.offset = gofarg->gotoff; | |
11727 | gofarg->gotoff += gofarg->got_elt_size; | |
11728 | } | |
11729 | else | |
11730 | h->got.offset = (bfd_vma) -1; | |
11731 | ||
11732 | return TRUE; | |
11733 | } | |
11734 | ||
11735 | /* And an accompanying bit to work out final got entry offsets once | |
11736 | we're done. Should be called from final_link. */ | |
11737 | ||
11738 | bfd_boolean | |
11739 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
11740 | struct bfd_link_info *info) | |
11741 | { | |
11742 | bfd *i; | |
11743 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11744 | bfd_vma gotoff; | |
11745 | unsigned int got_elt_size = bed->s->arch_size / 8; | |
11746 | struct alloc_got_off_arg gofarg; | |
11747 | ||
11748 | if (! is_elf_hash_table (info->hash)) | |
11749 | return FALSE; | |
11750 | ||
11751 | /* The GOT offset is relative to the .got section, but the GOT header is | |
11752 | put into the .got.plt section, if the backend uses it. */ | |
11753 | if (bed->want_got_plt) | |
11754 | gotoff = 0; | |
11755 | else | |
11756 | gotoff = bed->got_header_size; | |
11757 | ||
11758 | /* Do the local .got entries first. */ | |
11759 | for (i = info->input_bfds; i; i = i->link_next) | |
11760 | { | |
11761 | bfd_signed_vma *local_got; | |
11762 | bfd_size_type j, locsymcount; | |
11763 | Elf_Internal_Shdr *symtab_hdr; | |
11764 | ||
11765 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
11766 | continue; | |
11767 | ||
11768 | local_got = elf_local_got_refcounts (i); | |
11769 | if (!local_got) | |
11770 | continue; | |
11771 | ||
11772 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
11773 | if (elf_bad_symtab (i)) | |
11774 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
11775 | else | |
11776 | locsymcount = symtab_hdr->sh_info; | |
11777 | ||
11778 | for (j = 0; j < locsymcount; ++j) | |
11779 | { | |
11780 | if (local_got[j] > 0) | |
11781 | { | |
11782 | local_got[j] = gotoff; | |
11783 | gotoff += got_elt_size; | |
11784 | } | |
11785 | else | |
11786 | local_got[j] = (bfd_vma) -1; | |
11787 | } | |
11788 | } | |
11789 | ||
11790 | /* Then the global .got entries. .plt refcounts are handled by | |
11791 | adjust_dynamic_symbol */ | |
11792 | gofarg.gotoff = gotoff; | |
11793 | gofarg.got_elt_size = got_elt_size; | |
11794 | elf_link_hash_traverse (elf_hash_table (info), | |
11795 | elf_gc_allocate_got_offsets, | |
11796 | &gofarg); | |
11797 | return TRUE; | |
11798 | } | |
11799 | ||
11800 | /* Many folk need no more in the way of final link than this, once | |
11801 | got entry reference counting is enabled. */ | |
11802 | ||
11803 | bfd_boolean | |
11804 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
11805 | { | |
11806 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
11807 | return FALSE; | |
11808 | ||
11809 | /* Invoke the regular ELF backend linker to do all the work. */ | |
11810 | return bfd_elf_final_link (abfd, info); | |
11811 | } | |
11812 | ||
11813 | bfd_boolean | |
11814 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
11815 | { | |
11816 | struct elf_reloc_cookie *rcookie = cookie; | |
11817 | ||
11818 | if (rcookie->bad_symtab) | |
11819 | rcookie->rel = rcookie->rels; | |
11820 | ||
11821 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
11822 | { | |
11823 | unsigned long r_symndx; | |
11824 | ||
11825 | if (! rcookie->bad_symtab) | |
11826 | if (rcookie->rel->r_offset > offset) | |
11827 | return FALSE; | |
11828 | if (rcookie->rel->r_offset != offset) | |
11829 | continue; | |
11830 | ||
11831 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
11832 | if (r_symndx == SHN_UNDEF) | |
11833 | return TRUE; | |
11834 | ||
11835 | if (r_symndx >= rcookie->locsymcount | |
11836 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
11837 | { | |
11838 | struct elf_link_hash_entry *h; | |
11839 | ||
11840 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
11841 | ||
11842 | while (h->root.type == bfd_link_hash_indirect | |
11843 | || h->root.type == bfd_link_hash_warning) | |
11844 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11845 | ||
11846 | if ((h->root.type == bfd_link_hash_defined | |
11847 | || h->root.type == bfd_link_hash_defweak) | |
11848 | && elf_discarded_section (h->root.u.def.section)) | |
11849 | return TRUE; | |
11850 | else | |
11851 | return FALSE; | |
11852 | } | |
11853 | else | |
11854 | { | |
11855 | /* It's not a relocation against a global symbol, | |
11856 | but it could be a relocation against a local | |
11857 | symbol for a discarded section. */ | |
11858 | asection *isec; | |
11859 | Elf_Internal_Sym *isym; | |
11860 | ||
11861 | /* Need to: get the symbol; get the section. */ | |
11862 | isym = &rcookie->locsyms[r_symndx]; | |
11863 | if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
11864 | { | |
11865 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); | |
11866 | if (isec != NULL && elf_discarded_section (isec)) | |
11867 | return TRUE; | |
11868 | } | |
11869 | } | |
11870 | return FALSE; | |
11871 | } | |
11872 | return FALSE; | |
11873 | } | |
11874 | ||
11875 | /* Discard unneeded references to discarded sections. | |
11876 | Returns TRUE if any section's size was changed. */ | |
11877 | /* This function assumes that the relocations are in sorted order, | |
11878 | which is true for all known assemblers. */ | |
11879 | ||
11880 | bfd_boolean | |
11881 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) | |
11882 | { | |
11883 | struct elf_reloc_cookie cookie; | |
11884 | asection *stab, *eh; | |
c152c796 AM |
11885 | const struct elf_backend_data *bed; |
11886 | bfd *abfd; | |
c152c796 AM |
11887 | bfd_boolean ret = FALSE; |
11888 | ||
11889 | if (info->traditional_format | |
11890 | || !is_elf_hash_table (info->hash)) | |
11891 | return FALSE; | |
11892 | ||
ca92cecb | 11893 | _bfd_elf_begin_eh_frame_parsing (info); |
c152c796 AM |
11894 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) |
11895 | { | |
11896 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
11897 | continue; | |
11898 | ||
11899 | bed = get_elf_backend_data (abfd); | |
11900 | ||
11901 | if ((abfd->flags & DYNAMIC) != 0) | |
11902 | continue; | |
11903 | ||
8da3dbc5 AM |
11904 | eh = NULL; |
11905 | if (!info->relocatable) | |
11906 | { | |
11907 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); | |
11908 | if (eh != NULL | |
eea6121a | 11909 | && (eh->size == 0 |
8da3dbc5 AM |
11910 | || bfd_is_abs_section (eh->output_section))) |
11911 | eh = NULL; | |
11912 | } | |
c152c796 AM |
11913 | |
11914 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
11915 | if (stab != NULL | |
eea6121a | 11916 | && (stab->size == 0 |
c152c796 AM |
11917 | || bfd_is_abs_section (stab->output_section) |
11918 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) | |
11919 | stab = NULL; | |
11920 | ||
11921 | if (stab == NULL | |
11922 | && eh == NULL | |
11923 | && bed->elf_backend_discard_info == NULL) | |
11924 | continue; | |
11925 | ||
5241d853 RS |
11926 | if (!init_reloc_cookie (&cookie, info, abfd)) |
11927 | return FALSE; | |
c152c796 | 11928 | |
5241d853 RS |
11929 | if (stab != NULL |
11930 | && stab->reloc_count > 0 | |
11931 | && init_reloc_cookie_rels (&cookie, info, abfd, stab)) | |
c152c796 | 11932 | { |
5241d853 RS |
11933 | if (_bfd_discard_section_stabs (abfd, stab, |
11934 | elf_section_data (stab)->sec_info, | |
11935 | bfd_elf_reloc_symbol_deleted_p, | |
11936 | &cookie)) | |
11937 | ret = TRUE; | |
11938 | fini_reloc_cookie_rels (&cookie, stab); | |
c152c796 AM |
11939 | } |
11940 | ||
5241d853 RS |
11941 | if (eh != NULL |
11942 | && init_reloc_cookie_rels (&cookie, info, abfd, eh)) | |
c152c796 | 11943 | { |
ca92cecb | 11944 | _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie); |
c152c796 AM |
11945 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, |
11946 | bfd_elf_reloc_symbol_deleted_p, | |
11947 | &cookie)) | |
11948 | ret = TRUE; | |
5241d853 | 11949 | fini_reloc_cookie_rels (&cookie, eh); |
c152c796 AM |
11950 | } |
11951 | ||
11952 | if (bed->elf_backend_discard_info != NULL | |
11953 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
11954 | ret = TRUE; | |
11955 | ||
5241d853 | 11956 | fini_reloc_cookie (&cookie, abfd); |
c152c796 | 11957 | } |
ca92cecb | 11958 | _bfd_elf_end_eh_frame_parsing (info); |
c152c796 AM |
11959 | |
11960 | if (info->eh_frame_hdr | |
11961 | && !info->relocatable | |
11962 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
11963 | ret = TRUE; | |
11964 | ||
11965 | return ret; | |
11966 | } | |
082b7297 L |
11967 | |
11968 | void | |
c0f00686 L |
11969 | _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec, |
11970 | struct bfd_link_info *info) | |
082b7297 L |
11971 | { |
11972 | flagword flags; | |
6d2cd210 | 11973 | const char *name, *p; |
082b7297 L |
11974 | struct bfd_section_already_linked *l; |
11975 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
3d7f7666 | 11976 | |
3d7f7666 L |
11977 | if (sec->output_section == bfd_abs_section_ptr) |
11978 | return; | |
082b7297 L |
11979 | |
11980 | flags = sec->flags; | |
3d7f7666 | 11981 | |
c2370991 AM |
11982 | /* Return if it isn't a linkonce section. A comdat group section |
11983 | also has SEC_LINK_ONCE set. */ | |
11984 | if ((flags & SEC_LINK_ONCE) == 0) | |
082b7297 L |
11985 | return; |
11986 | ||
c2370991 AM |
11987 | /* Don't put group member sections on our list of already linked |
11988 | sections. They are handled as a group via their group section. */ | |
11989 | if (elf_sec_group (sec) != NULL) | |
11990 | return; | |
3d7f7666 | 11991 | |
082b7297 L |
11992 | /* FIXME: When doing a relocatable link, we may have trouble |
11993 | copying relocations in other sections that refer to local symbols | |
11994 | in the section being discarded. Those relocations will have to | |
11995 | be converted somehow; as of this writing I'm not sure that any of | |
11996 | the backends handle that correctly. | |
11997 | ||
11998 | It is tempting to instead not discard link once sections when | |
11999 | doing a relocatable link (technically, they should be discarded | |
12000 | whenever we are building constructors). However, that fails, | |
12001 | because the linker winds up combining all the link once sections | |
12002 | into a single large link once section, which defeats the purpose | |
12003 | of having link once sections in the first place. | |
12004 | ||
12005 | Also, not merging link once sections in a relocatable link | |
12006 | causes trouble for MIPS ELF, which relies on link once semantics | |
12007 | to handle the .reginfo section correctly. */ | |
12008 | ||
12009 | name = bfd_get_section_name (abfd, sec); | |
12010 | ||
0112cd26 | 12011 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
6d2cd210 JJ |
12012 | && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
12013 | p++; | |
12014 | else | |
12015 | p = name; | |
12016 | ||
12017 | already_linked_list = bfd_section_already_linked_table_lookup (p); | |
082b7297 L |
12018 | |
12019 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
12020 | { | |
c2370991 AM |
12021 | /* We may have 2 different types of sections on the list: group |
12022 | sections and linkonce sections. Match like sections. */ | |
3d7f7666 | 12023 | if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) |
6d2cd210 | 12024 | && strcmp (name, l->sec->name) == 0 |
082b7297 L |
12025 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL) |
12026 | { | |
12027 | /* The section has already been linked. See if we should | |
6d2cd210 | 12028 | issue a warning. */ |
082b7297 L |
12029 | switch (flags & SEC_LINK_DUPLICATES) |
12030 | { | |
12031 | default: | |
12032 | abort (); | |
12033 | ||
12034 | case SEC_LINK_DUPLICATES_DISCARD: | |
12035 | break; | |
12036 | ||
12037 | case SEC_LINK_DUPLICATES_ONE_ONLY: | |
12038 | (*_bfd_error_handler) | |
c93625e2 | 12039 | (_("%B: ignoring duplicate section `%A'"), |
d003868e | 12040 | abfd, sec); |
082b7297 L |
12041 | break; |
12042 | ||
12043 | case SEC_LINK_DUPLICATES_SAME_SIZE: | |
12044 | if (sec->size != l->sec->size) | |
12045 | (*_bfd_error_handler) | |
c93625e2 | 12046 | (_("%B: duplicate section `%A' has different size"), |
d003868e | 12047 | abfd, sec); |
082b7297 | 12048 | break; |
ea5158d8 DJ |
12049 | |
12050 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: | |
12051 | if (sec->size != l->sec->size) | |
12052 | (*_bfd_error_handler) | |
c93625e2 | 12053 | (_("%B: duplicate section `%A' has different size"), |
ea5158d8 DJ |
12054 | abfd, sec); |
12055 | else if (sec->size != 0) | |
12056 | { | |
12057 | bfd_byte *sec_contents, *l_sec_contents; | |
12058 | ||
12059 | if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents)) | |
12060 | (*_bfd_error_handler) | |
c93625e2 | 12061 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
12062 | abfd, sec); |
12063 | else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec, | |
12064 | &l_sec_contents)) | |
12065 | (*_bfd_error_handler) | |
c93625e2 | 12066 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
12067 | l->sec->owner, l->sec); |
12068 | else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) | |
12069 | (*_bfd_error_handler) | |
c93625e2 | 12070 | (_("%B: warning: duplicate section `%A' has different contents"), |
ea5158d8 DJ |
12071 | abfd, sec); |
12072 | ||
12073 | if (sec_contents) | |
12074 | free (sec_contents); | |
12075 | if (l_sec_contents) | |
12076 | free (l_sec_contents); | |
12077 | } | |
12078 | break; | |
082b7297 L |
12079 | } |
12080 | ||
12081 | /* Set the output_section field so that lang_add_section | |
12082 | does not create a lang_input_section structure for this | |
12083 | section. Since there might be a symbol in the section | |
12084 | being discarded, we must retain a pointer to the section | |
12085 | which we are really going to use. */ | |
12086 | sec->output_section = bfd_abs_section_ptr; | |
12087 | sec->kept_section = l->sec; | |
3b36f7e6 | 12088 | |
082b7297 | 12089 | if (flags & SEC_GROUP) |
3d7f7666 L |
12090 | { |
12091 | asection *first = elf_next_in_group (sec); | |
12092 | asection *s = first; | |
12093 | ||
12094 | while (s != NULL) | |
12095 | { | |
12096 | s->output_section = bfd_abs_section_ptr; | |
12097 | /* Record which group discards it. */ | |
12098 | s->kept_section = l->sec; | |
12099 | s = elf_next_in_group (s); | |
12100 | /* These lists are circular. */ | |
12101 | if (s == first) | |
12102 | break; | |
12103 | } | |
12104 | } | |
082b7297 L |
12105 | |
12106 | return; | |
12107 | } | |
12108 | } | |
12109 | ||
c2370991 AM |
12110 | /* A single member comdat group section may be discarded by a |
12111 | linkonce section and vice versa. */ | |
12112 | ||
12113 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 12114 | { |
c2370991 AM |
12115 | asection *first = elf_next_in_group (sec); |
12116 | ||
12117 | if (first != NULL && elf_next_in_group (first) == first) | |
12118 | /* Check this single member group against linkonce sections. */ | |
12119 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
12120 | if ((l->sec->flags & SEC_GROUP) == 0 | |
12121 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL | |
12122 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
12123 | { | |
12124 | first->output_section = bfd_abs_section_ptr; | |
12125 | first->kept_section = l->sec; | |
12126 | sec->output_section = bfd_abs_section_ptr; | |
12127 | break; | |
12128 | } | |
3d7f7666 L |
12129 | } |
12130 | else | |
c2370991 | 12131 | /* Check this linkonce section against single member groups. */ |
6d2cd210 JJ |
12132 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
12133 | if (l->sec->flags & SEC_GROUP) | |
12134 | { | |
12135 | asection *first = elf_next_in_group (l->sec); | |
12136 | ||
12137 | if (first != NULL | |
12138 | && elf_next_in_group (first) == first | |
c0f00686 | 12139 | && bfd_elf_match_symbols_in_sections (first, sec, info)) |
6d2cd210 JJ |
12140 | { |
12141 | sec->output_section = bfd_abs_section_ptr; | |
c2370991 | 12142 | sec->kept_section = first; |
6d2cd210 JJ |
12143 | break; |
12144 | } | |
12145 | } | |
12146 | ||
082b7297 | 12147 | /* This is the first section with this name. Record it. */ |
a6626e8c MS |
12148 | if (! bfd_section_already_linked_table_insert (already_linked_list, sec)) |
12149 | info->callbacks->einfo (_("%F%P: already_linked_table: %E")); | |
082b7297 | 12150 | } |
81e1b023 | 12151 | |
a4d8e49b L |
12152 | bfd_boolean |
12153 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
12154 | { | |
12155 | return sym->st_shndx == SHN_COMMON; | |
12156 | } | |
12157 | ||
12158 | unsigned int | |
12159 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
12160 | { | |
12161 | return SHN_COMMON; | |
12162 | } | |
12163 | ||
12164 | asection * | |
12165 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
12166 | { | |
12167 | return bfd_com_section_ptr; | |
12168 | } |