Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
64d03ab5 | 2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
8da3dbc5 | 3 | 2005, 2006, 2007 Free Software Foundation, Inc. |
252b5132 | 4 | |
8fdd7217 | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
8fdd7217 NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
252b5132 | 11 | |
8fdd7217 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
8fdd7217 NC |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
3e110533 | 19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
252b5132 | 20 | |
252b5132 | 21 | #include "sysdep.h" |
3db64b00 | 22 | #include "bfd.h" |
252b5132 RH |
23 | #include "bfdlink.h" |
24 | #include "libbfd.h" | |
25 | #define ARCH_SIZE 0 | |
26 | #include "elf-bfd.h" | |
4ad4eba5 | 27 | #include "safe-ctype.h" |
ccf2f652 | 28 | #include "libiberty.h" |
66eb6687 | 29 | #include "objalloc.h" |
252b5132 | 30 | |
d98685ac AM |
31 | /* Define a symbol in a dynamic linkage section. */ |
32 | ||
33 | struct elf_link_hash_entry * | |
34 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
35 | struct bfd_link_info *info, | |
36 | asection *sec, | |
37 | const char *name) | |
38 | { | |
39 | struct elf_link_hash_entry *h; | |
40 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 41 | const struct elf_backend_data *bed; |
d98685ac AM |
42 | |
43 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
44 | if (h != NULL) | |
45 | { | |
46 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
47 | This is a symptom of a larger problem: Absolute symbols | |
48 | defined in shared libraries can't be overridden, because we | |
49 | lose the link to the bfd which is via the symbol section. */ | |
50 | h->root.type = bfd_link_hash_new; | |
51 | } | |
52 | ||
53 | bh = &h->root; | |
54 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, | |
55 | sec, 0, NULL, FALSE, | |
56 | get_elf_backend_data (abfd)->collect, | |
57 | &bh)) | |
58 | return NULL; | |
59 | h = (struct elf_link_hash_entry *) bh; | |
60 | h->def_regular = 1; | |
61 | h->type = STT_OBJECT; | |
62 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
63 | ||
ccabcbe5 AM |
64 | bed = get_elf_backend_data (abfd); |
65 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
d98685ac AM |
66 | return h; |
67 | } | |
68 | ||
b34976b6 | 69 | bfd_boolean |
268b6b39 | 70 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
71 | { |
72 | flagword flags; | |
aad5d350 | 73 | asection *s; |
252b5132 | 74 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 75 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
76 | int ptralign; |
77 | ||
78 | /* This function may be called more than once. */ | |
aad5d350 AM |
79 | s = bfd_get_section_by_name (abfd, ".got"); |
80 | if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0) | |
b34976b6 | 81 | return TRUE; |
252b5132 RH |
82 | |
83 | switch (bed->s->arch_size) | |
84 | { | |
bb0deeff AO |
85 | case 32: |
86 | ptralign = 2; | |
87 | break; | |
88 | ||
89 | case 64: | |
90 | ptralign = 3; | |
91 | break; | |
92 | ||
93 | default: | |
94 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 95 | return FALSE; |
252b5132 RH |
96 | } |
97 | ||
e5a52504 | 98 | flags = bed->dynamic_sec_flags; |
252b5132 | 99 | |
3496cb2a | 100 | s = bfd_make_section_with_flags (abfd, ".got", flags); |
252b5132 | 101 | if (s == NULL |
252b5132 | 102 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 103 | return FALSE; |
252b5132 RH |
104 | |
105 | if (bed->want_got_plt) | |
106 | { | |
3496cb2a | 107 | s = bfd_make_section_with_flags (abfd, ".got.plt", flags); |
252b5132 | 108 | if (s == NULL |
252b5132 | 109 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 110 | return FALSE; |
252b5132 RH |
111 | } |
112 | ||
2517a57f AM |
113 | if (bed->want_got_sym) |
114 | { | |
115 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
116 | (or .got.plt) section. We don't do this in the linker script | |
117 | because we don't want to define the symbol if we are not creating | |
118 | a global offset table. */ | |
d98685ac | 119 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_"); |
2517a57f | 120 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
121 | if (h == NULL) |
122 | return FALSE; | |
2517a57f | 123 | } |
252b5132 RH |
124 | |
125 | /* The first bit of the global offset table is the header. */ | |
3b36f7e6 | 126 | s->size += bed->got_header_size; |
252b5132 | 127 | |
b34976b6 | 128 | return TRUE; |
252b5132 RH |
129 | } |
130 | \f | |
7e9f0867 AM |
131 | /* Create a strtab to hold the dynamic symbol names. */ |
132 | static bfd_boolean | |
133 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
134 | { | |
135 | struct elf_link_hash_table *hash_table; | |
136 | ||
137 | hash_table = elf_hash_table (info); | |
138 | if (hash_table->dynobj == NULL) | |
139 | hash_table->dynobj = abfd; | |
140 | ||
141 | if (hash_table->dynstr == NULL) | |
142 | { | |
143 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
144 | if (hash_table->dynstr == NULL) | |
145 | return FALSE; | |
146 | } | |
147 | return TRUE; | |
148 | } | |
149 | ||
45d6a902 AM |
150 | /* Create some sections which will be filled in with dynamic linking |
151 | information. ABFD is an input file which requires dynamic sections | |
152 | to be created. The dynamic sections take up virtual memory space | |
153 | when the final executable is run, so we need to create them before | |
154 | addresses are assigned to the output sections. We work out the | |
155 | actual contents and size of these sections later. */ | |
252b5132 | 156 | |
b34976b6 | 157 | bfd_boolean |
268b6b39 | 158 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 159 | { |
45d6a902 AM |
160 | flagword flags; |
161 | register asection *s; | |
9c5bfbb7 | 162 | const struct elf_backend_data *bed; |
252b5132 | 163 | |
0eddce27 | 164 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
165 | return FALSE; |
166 | ||
167 | if (elf_hash_table (info)->dynamic_sections_created) | |
168 | return TRUE; | |
169 | ||
7e9f0867 AM |
170 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
171 | return FALSE; | |
45d6a902 | 172 | |
7e9f0867 | 173 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
174 | bed = get_elf_backend_data (abfd); |
175 | ||
176 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
177 | |
178 | /* A dynamically linked executable has a .interp section, but a | |
179 | shared library does not. */ | |
36af4a4e | 180 | if (info->executable) |
252b5132 | 181 | { |
3496cb2a L |
182 | s = bfd_make_section_with_flags (abfd, ".interp", |
183 | flags | SEC_READONLY); | |
184 | if (s == NULL) | |
45d6a902 AM |
185 | return FALSE; |
186 | } | |
bb0deeff | 187 | |
45d6a902 AM |
188 | /* Create sections to hold version informations. These are removed |
189 | if they are not needed. */ | |
3496cb2a L |
190 | s = bfd_make_section_with_flags (abfd, ".gnu.version_d", |
191 | flags | SEC_READONLY); | |
45d6a902 | 192 | if (s == NULL |
45d6a902 AM |
193 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
194 | return FALSE; | |
195 | ||
3496cb2a L |
196 | s = bfd_make_section_with_flags (abfd, ".gnu.version", |
197 | flags | SEC_READONLY); | |
45d6a902 | 198 | if (s == NULL |
45d6a902 AM |
199 | || ! bfd_set_section_alignment (abfd, s, 1)) |
200 | return FALSE; | |
201 | ||
3496cb2a L |
202 | s = bfd_make_section_with_flags (abfd, ".gnu.version_r", |
203 | flags | SEC_READONLY); | |
45d6a902 | 204 | if (s == NULL |
45d6a902 AM |
205 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
206 | return FALSE; | |
207 | ||
3496cb2a L |
208 | s = bfd_make_section_with_flags (abfd, ".dynsym", |
209 | flags | SEC_READONLY); | |
45d6a902 | 210 | if (s == NULL |
45d6a902 AM |
211 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
212 | return FALSE; | |
213 | ||
3496cb2a L |
214 | s = bfd_make_section_with_flags (abfd, ".dynstr", |
215 | flags | SEC_READONLY); | |
216 | if (s == NULL) | |
45d6a902 AM |
217 | return FALSE; |
218 | ||
3496cb2a | 219 | s = bfd_make_section_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 220 | if (s == NULL |
45d6a902 AM |
221 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
222 | return FALSE; | |
223 | ||
224 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
225 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
226 | only want to define it if we are, in fact, creating a .dynamic | |
227 | section. We don't want to define it if there is no .dynamic | |
228 | section, since on some ELF platforms the start up code examines it | |
229 | to decide how to initialize the process. */ | |
d98685ac | 230 | if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC")) |
45d6a902 AM |
231 | return FALSE; |
232 | ||
fdc90cb4 JJ |
233 | if (info->emit_hash) |
234 | { | |
235 | s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY); | |
236 | if (s == NULL | |
237 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
238 | return FALSE; | |
239 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
240 | } | |
241 | ||
242 | if (info->emit_gnu_hash) | |
243 | { | |
244 | s = bfd_make_section_with_flags (abfd, ".gnu.hash", | |
245 | flags | SEC_READONLY); | |
246 | if (s == NULL | |
247 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
248 | return FALSE; | |
249 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: | |
250 | 4 32-bit words followed by variable count of 64-bit words, then | |
251 | variable count of 32-bit words. */ | |
252 | if (bed->s->arch_size == 64) | |
253 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
254 | else | |
255 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
256 | } | |
45d6a902 AM |
257 | |
258 | /* Let the backend create the rest of the sections. This lets the | |
259 | backend set the right flags. The backend will normally create | |
260 | the .got and .plt sections. */ | |
261 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
262 | return FALSE; | |
263 | ||
264 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
265 | ||
266 | return TRUE; | |
267 | } | |
268 | ||
269 | /* Create dynamic sections when linking against a dynamic object. */ | |
270 | ||
271 | bfd_boolean | |
268b6b39 | 272 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
273 | { |
274 | flagword flags, pltflags; | |
7325306f | 275 | struct elf_link_hash_entry *h; |
45d6a902 | 276 | asection *s; |
9c5bfbb7 | 277 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 278 | |
252b5132 RH |
279 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
280 | .rel[a].bss sections. */ | |
e5a52504 | 281 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
282 | |
283 | pltflags = flags; | |
252b5132 | 284 | if (bed->plt_not_loaded) |
6df4d94c MM |
285 | /* We do not clear SEC_ALLOC here because we still want the OS to |
286 | allocate space for the section; it's just that there's nothing | |
287 | to read in from the object file. */ | |
5d1634d7 | 288 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
289 | else |
290 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
291 | if (bed->plt_readonly) |
292 | pltflags |= SEC_READONLY; | |
293 | ||
3496cb2a | 294 | s = bfd_make_section_with_flags (abfd, ".plt", pltflags); |
252b5132 | 295 | if (s == NULL |
252b5132 | 296 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 297 | return FALSE; |
252b5132 | 298 | |
d98685ac AM |
299 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
300 | .plt section. */ | |
7325306f RS |
301 | if (bed->want_plt_sym) |
302 | { | |
303 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
304 | "_PROCEDURE_LINKAGE_TABLE_"); | |
305 | elf_hash_table (info)->hplt = h; | |
306 | if (h == NULL) | |
307 | return FALSE; | |
308 | } | |
252b5132 | 309 | |
3496cb2a L |
310 | s = bfd_make_section_with_flags (abfd, |
311 | (bed->default_use_rela_p | |
312 | ? ".rela.plt" : ".rel.plt"), | |
313 | flags | SEC_READONLY); | |
252b5132 | 314 | if (s == NULL |
45d6a902 | 315 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 316 | return FALSE; |
252b5132 RH |
317 | |
318 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 319 | return FALSE; |
252b5132 | 320 | |
3018b441 RH |
321 | if (bed->want_dynbss) |
322 | { | |
323 | /* The .dynbss section is a place to put symbols which are defined | |
324 | by dynamic objects, are referenced by regular objects, and are | |
325 | not functions. We must allocate space for them in the process | |
326 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
327 | initialize them at run time. The linker script puts the .dynbss | |
328 | section into the .bss section of the final image. */ | |
3496cb2a L |
329 | s = bfd_make_section_with_flags (abfd, ".dynbss", |
330 | (SEC_ALLOC | |
331 | | SEC_LINKER_CREATED)); | |
332 | if (s == NULL) | |
b34976b6 | 333 | return FALSE; |
252b5132 | 334 | |
3018b441 | 335 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
336 | normally needed. We need to create it here, though, so that the |
337 | linker will map it to an output section. We can't just create it | |
338 | only if we need it, because we will not know whether we need it | |
339 | until we have seen all the input files, and the first time the | |
340 | main linker code calls BFD after examining all the input files | |
341 | (size_dynamic_sections) the input sections have already been | |
342 | mapped to the output sections. If the section turns out not to | |
343 | be needed, we can discard it later. We will never need this | |
344 | section when generating a shared object, since they do not use | |
345 | copy relocs. */ | |
3018b441 RH |
346 | if (! info->shared) |
347 | { | |
3496cb2a L |
348 | s = bfd_make_section_with_flags (abfd, |
349 | (bed->default_use_rela_p | |
350 | ? ".rela.bss" : ".rel.bss"), | |
351 | flags | SEC_READONLY); | |
3018b441 | 352 | if (s == NULL |
45d6a902 | 353 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 354 | return FALSE; |
3018b441 | 355 | } |
252b5132 RH |
356 | } |
357 | ||
b34976b6 | 358 | return TRUE; |
252b5132 RH |
359 | } |
360 | \f | |
252b5132 RH |
361 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
362 | read the input files, since we need to have a list of all of them | |
363 | before we can determine the final sizes of the output sections. | |
364 | Note that we may actually call this function even though we are not | |
365 | going to output any dynamic symbols; in some cases we know that a | |
366 | symbol should be in the dynamic symbol table, but only if there is | |
367 | one. */ | |
368 | ||
b34976b6 | 369 | bfd_boolean |
c152c796 AM |
370 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
371 | struct elf_link_hash_entry *h) | |
252b5132 RH |
372 | { |
373 | if (h->dynindx == -1) | |
374 | { | |
2b0f7ef9 | 375 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 376 | char *p; |
252b5132 | 377 | const char *name; |
252b5132 RH |
378 | bfd_size_type indx; |
379 | ||
7a13edea NC |
380 | /* XXX: The ABI draft says the linker must turn hidden and |
381 | internal symbols into STB_LOCAL symbols when producing the | |
382 | DSO. However, if ld.so honors st_other in the dynamic table, | |
383 | this would not be necessary. */ | |
384 | switch (ELF_ST_VISIBILITY (h->other)) | |
385 | { | |
386 | case STV_INTERNAL: | |
387 | case STV_HIDDEN: | |
9d6eee78 L |
388 | if (h->root.type != bfd_link_hash_undefined |
389 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 390 | { |
f5385ebf | 391 | h->forced_local = 1; |
67687978 PB |
392 | if (!elf_hash_table (info)->is_relocatable_executable) |
393 | return TRUE; | |
7a13edea | 394 | } |
0444bdd4 | 395 | |
7a13edea NC |
396 | default: |
397 | break; | |
398 | } | |
399 | ||
252b5132 RH |
400 | h->dynindx = elf_hash_table (info)->dynsymcount; |
401 | ++elf_hash_table (info)->dynsymcount; | |
402 | ||
403 | dynstr = elf_hash_table (info)->dynstr; | |
404 | if (dynstr == NULL) | |
405 | { | |
406 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 407 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 408 | if (dynstr == NULL) |
b34976b6 | 409 | return FALSE; |
252b5132 RH |
410 | } |
411 | ||
412 | /* We don't put any version information in the dynamic string | |
aad5d350 | 413 | table. */ |
252b5132 RH |
414 | name = h->root.root.string; |
415 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
416 | if (p != NULL) |
417 | /* We know that the p points into writable memory. In fact, | |
418 | there are only a few symbols that have read-only names, being | |
419 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
420 | by the backends. Most symbols will have names pointing into | |
421 | an ELF string table read from a file, or to objalloc memory. */ | |
422 | *p = 0; | |
423 | ||
424 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
425 | ||
426 | if (p != NULL) | |
427 | *p = ELF_VER_CHR; | |
252b5132 RH |
428 | |
429 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 430 | return FALSE; |
252b5132 RH |
431 | h->dynstr_index = indx; |
432 | } | |
433 | ||
b34976b6 | 434 | return TRUE; |
252b5132 | 435 | } |
45d6a902 | 436 | \f |
55255dae L |
437 | /* Mark a symbol dynamic. */ |
438 | ||
439 | void | |
440 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, | |
40b36307 L |
441 | struct elf_link_hash_entry *h, |
442 | Elf_Internal_Sym *sym) | |
55255dae | 443 | { |
40b36307 | 444 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
55255dae | 445 | |
40b36307 L |
446 | /* It may be called more than once on the same H. */ |
447 | if(h->dynamic || info->relocatable) | |
55255dae L |
448 | return; |
449 | ||
40b36307 L |
450 | if ((info->dynamic_data |
451 | && (h->type == STT_OBJECT | |
452 | || (sym != NULL | |
453 | && ELF_ST_TYPE (sym->st_info) == STT_OBJECT))) | |
454 | || (d != NULL | |
455 | && h->root.type == bfd_link_hash_new | |
456 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
55255dae L |
457 | h->dynamic = 1; |
458 | } | |
459 | ||
45d6a902 AM |
460 | /* Record an assignment to a symbol made by a linker script. We need |
461 | this in case some dynamic object refers to this symbol. */ | |
462 | ||
463 | bfd_boolean | |
fe21a8fc L |
464 | bfd_elf_record_link_assignment (bfd *output_bfd, |
465 | struct bfd_link_info *info, | |
268b6b39 | 466 | const char *name, |
fe21a8fc L |
467 | bfd_boolean provide, |
468 | bfd_boolean hidden) | |
45d6a902 AM |
469 | { |
470 | struct elf_link_hash_entry *h; | |
4ea42fb7 | 471 | struct elf_link_hash_table *htab; |
45d6a902 | 472 | |
0eddce27 | 473 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
474 | return TRUE; |
475 | ||
4ea42fb7 AM |
476 | htab = elf_hash_table (info); |
477 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 478 | if (h == NULL) |
4ea42fb7 | 479 | return provide; |
45d6a902 | 480 | |
02bb6eae AO |
481 | /* Since we're defining the symbol, don't let it seem to have not |
482 | been defined. record_dynamic_symbol and size_dynamic_sections | |
77cfaee6 | 483 | may depend on this. */ |
02bb6eae AO |
484 | if (h->root.type == bfd_link_hash_undefweak |
485 | || h->root.type == bfd_link_hash_undefined) | |
77cfaee6 | 486 | { |
4ea42fb7 | 487 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
488 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
489 | bfd_link_repair_undef_list (&htab->root); | |
77cfaee6 | 490 | } |
02bb6eae | 491 | |
45d6a902 | 492 | if (h->root.type == bfd_link_hash_new) |
55255dae | 493 | { |
40b36307 | 494 | bfd_elf_link_mark_dynamic_symbol (info, h, NULL); |
55255dae L |
495 | h->non_elf = 0; |
496 | } | |
45d6a902 AM |
497 | |
498 | /* If this symbol is being provided by the linker script, and it is | |
499 | currently defined by a dynamic object, but not by a regular | |
500 | object, then mark it as undefined so that the generic linker will | |
501 | force the correct value. */ | |
502 | if (provide | |
f5385ebf AM |
503 | && h->def_dynamic |
504 | && !h->def_regular) | |
45d6a902 AM |
505 | h->root.type = bfd_link_hash_undefined; |
506 | ||
507 | /* If this symbol is not being provided by the linker script, and it is | |
508 | currently defined by a dynamic object, but not by a regular object, | |
509 | then clear out any version information because the symbol will not be | |
510 | associated with the dynamic object any more. */ | |
511 | if (!provide | |
f5385ebf AM |
512 | && h->def_dynamic |
513 | && !h->def_regular) | |
45d6a902 AM |
514 | h->verinfo.verdef = NULL; |
515 | ||
f5385ebf | 516 | h->def_regular = 1; |
45d6a902 | 517 | |
fe21a8fc L |
518 | if (provide && hidden) |
519 | { | |
520 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
521 | ||
522 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
523 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
524 | } | |
525 | ||
6fa3860b PB |
526 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
527 | and executables. */ | |
528 | if (!info->relocatable | |
529 | && h->dynindx != -1 | |
530 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
531 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
532 | h->forced_local = 1; | |
533 | ||
f5385ebf AM |
534 | if ((h->def_dynamic |
535 | || h->ref_dynamic | |
67687978 PB |
536 | || info->shared |
537 | || (info->executable && elf_hash_table (info)->is_relocatable_executable)) | |
45d6a902 AM |
538 | && h->dynindx == -1) |
539 | { | |
c152c796 | 540 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
541 | return FALSE; |
542 | ||
543 | /* If this is a weak defined symbol, and we know a corresponding | |
544 | real symbol from the same dynamic object, make sure the real | |
545 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
546 | if (h->u.weakdef != NULL |
547 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 548 | { |
f6e332e6 | 549 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
550 | return FALSE; |
551 | } | |
552 | } | |
553 | ||
554 | return TRUE; | |
555 | } | |
42751cf3 | 556 | |
8c58d23b AM |
557 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
558 | success, and 2 on a failure caused by attempting to record a symbol | |
559 | in a discarded section, eg. a discarded link-once section symbol. */ | |
560 | ||
561 | int | |
c152c796 AM |
562 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
563 | bfd *input_bfd, | |
564 | long input_indx) | |
8c58d23b AM |
565 | { |
566 | bfd_size_type amt; | |
567 | struct elf_link_local_dynamic_entry *entry; | |
568 | struct elf_link_hash_table *eht; | |
569 | struct elf_strtab_hash *dynstr; | |
570 | unsigned long dynstr_index; | |
571 | char *name; | |
572 | Elf_External_Sym_Shndx eshndx; | |
573 | char esym[sizeof (Elf64_External_Sym)]; | |
574 | ||
0eddce27 | 575 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
576 | return 0; |
577 | ||
578 | /* See if the entry exists already. */ | |
579 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
580 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
581 | return 1; | |
582 | ||
583 | amt = sizeof (*entry); | |
268b6b39 | 584 | entry = bfd_alloc (input_bfd, amt); |
8c58d23b AM |
585 | if (entry == NULL) |
586 | return 0; | |
587 | ||
588 | /* Go find the symbol, so that we can find it's name. */ | |
589 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 590 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
591 | { |
592 | bfd_release (input_bfd, entry); | |
593 | return 0; | |
594 | } | |
595 | ||
596 | if (entry->isym.st_shndx != SHN_UNDEF | |
597 | && (entry->isym.st_shndx < SHN_LORESERVE | |
598 | || entry->isym.st_shndx > SHN_HIRESERVE)) | |
599 | { | |
600 | asection *s; | |
601 | ||
602 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
603 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
604 | { | |
605 | /* We can still bfd_release here as nothing has done another | |
606 | bfd_alloc. We can't do this later in this function. */ | |
607 | bfd_release (input_bfd, entry); | |
608 | return 2; | |
609 | } | |
610 | } | |
611 | ||
612 | name = (bfd_elf_string_from_elf_section | |
613 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
614 | entry->isym.st_name)); | |
615 | ||
616 | dynstr = elf_hash_table (info)->dynstr; | |
617 | if (dynstr == NULL) | |
618 | { | |
619 | /* Create a strtab to hold the dynamic symbol names. */ | |
620 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
621 | if (dynstr == NULL) | |
622 | return 0; | |
623 | } | |
624 | ||
b34976b6 | 625 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
626 | if (dynstr_index == (unsigned long) -1) |
627 | return 0; | |
628 | entry->isym.st_name = dynstr_index; | |
629 | ||
630 | eht = elf_hash_table (info); | |
631 | ||
632 | entry->next = eht->dynlocal; | |
633 | eht->dynlocal = entry; | |
634 | entry->input_bfd = input_bfd; | |
635 | entry->input_indx = input_indx; | |
636 | eht->dynsymcount++; | |
637 | ||
638 | /* Whatever binding the symbol had before, it's now local. */ | |
639 | entry->isym.st_info | |
640 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
641 | ||
642 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
643 | ||
644 | return 1; | |
645 | } | |
646 | ||
30b30c21 | 647 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 648 | |
30b30c21 | 649 | long |
268b6b39 AM |
650 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
651 | bfd *input_bfd, | |
652 | long input_indx) | |
30b30c21 RH |
653 | { |
654 | struct elf_link_local_dynamic_entry *e; | |
655 | ||
656 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
657 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
658 | return e->dynindx; | |
659 | return -1; | |
660 | } | |
661 | ||
662 | /* This function is used to renumber the dynamic symbols, if some of | |
663 | them are removed because they are marked as local. This is called | |
664 | via elf_link_hash_traverse. */ | |
665 | ||
b34976b6 | 666 | static bfd_boolean |
268b6b39 AM |
667 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
668 | void *data) | |
42751cf3 | 669 | { |
268b6b39 | 670 | size_t *count = data; |
30b30c21 | 671 | |
e92d460e AM |
672 | if (h->root.type == bfd_link_hash_warning) |
673 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
674 | ||
6fa3860b PB |
675 | if (h->forced_local) |
676 | return TRUE; | |
677 | ||
678 | if (h->dynindx != -1) | |
679 | h->dynindx = ++(*count); | |
680 | ||
681 | return TRUE; | |
682 | } | |
683 | ||
684 | ||
685 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
686 | STB_LOCAL binding. */ | |
687 | ||
688 | static bfd_boolean | |
689 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
690 | void *data) | |
691 | { | |
692 | size_t *count = data; | |
693 | ||
694 | if (h->root.type == bfd_link_hash_warning) | |
695 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
696 | ||
697 | if (!h->forced_local) | |
698 | return TRUE; | |
699 | ||
42751cf3 | 700 | if (h->dynindx != -1) |
30b30c21 RH |
701 | h->dynindx = ++(*count); |
702 | ||
b34976b6 | 703 | return TRUE; |
42751cf3 | 704 | } |
30b30c21 | 705 | |
aee6f5b4 AO |
706 | /* Return true if the dynamic symbol for a given section should be |
707 | omitted when creating a shared library. */ | |
708 | bfd_boolean | |
709 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
710 | struct bfd_link_info *info, | |
711 | asection *p) | |
712 | { | |
74541ad4 AM |
713 | struct elf_link_hash_table *htab; |
714 | ||
aee6f5b4 AO |
715 | switch (elf_section_data (p)->this_hdr.sh_type) |
716 | { | |
717 | case SHT_PROGBITS: | |
718 | case SHT_NOBITS: | |
719 | /* If sh_type is yet undecided, assume it could be | |
720 | SHT_PROGBITS/SHT_NOBITS. */ | |
721 | case SHT_NULL: | |
74541ad4 AM |
722 | htab = elf_hash_table (info); |
723 | if (p == htab->tls_sec) | |
724 | return FALSE; | |
725 | ||
726 | if (htab->text_index_section != NULL) | |
727 | return p != htab->text_index_section && p != htab->data_index_section; | |
728 | ||
aee6f5b4 AO |
729 | if (strcmp (p->name, ".got") == 0 |
730 | || strcmp (p->name, ".got.plt") == 0 | |
731 | || strcmp (p->name, ".plt") == 0) | |
732 | { | |
733 | asection *ip; | |
aee6f5b4 | 734 | |
74541ad4 AM |
735 | if (htab->dynobj != NULL |
736 | && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL | |
aee6f5b4 AO |
737 | && (ip->flags & SEC_LINKER_CREATED) |
738 | && ip->output_section == p) | |
739 | return TRUE; | |
740 | } | |
741 | return FALSE; | |
742 | ||
743 | /* There shouldn't be section relative relocations | |
744 | against any other section. */ | |
745 | default: | |
746 | return TRUE; | |
747 | } | |
748 | } | |
749 | ||
062e2358 | 750 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
751 | symbol for each output section, which come first. Next come symbols |
752 | which have been forced to local binding. Then all of the back-end | |
753 | allocated local dynamic syms, followed by the rest of the global | |
754 | symbols. */ | |
30b30c21 | 755 | |
554220db AM |
756 | static unsigned long |
757 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
758 | struct bfd_link_info *info, | |
759 | unsigned long *section_sym_count) | |
30b30c21 RH |
760 | { |
761 | unsigned long dynsymcount = 0; | |
762 | ||
67687978 | 763 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
30b30c21 | 764 | { |
aee6f5b4 | 765 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
766 | asection *p; |
767 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 768 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
769 | && (p->flags & SEC_ALLOC) != 0 |
770 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
771 | elf_section_data (p)->dynindx = ++dynsymcount; | |
74541ad4 AM |
772 | else |
773 | elf_section_data (p)->dynindx = 0; | |
30b30c21 | 774 | } |
554220db | 775 | *section_sym_count = dynsymcount; |
30b30c21 | 776 | |
6fa3860b PB |
777 | elf_link_hash_traverse (elf_hash_table (info), |
778 | elf_link_renumber_local_hash_table_dynsyms, | |
779 | &dynsymcount); | |
780 | ||
30b30c21 RH |
781 | if (elf_hash_table (info)->dynlocal) |
782 | { | |
783 | struct elf_link_local_dynamic_entry *p; | |
784 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
785 | p->dynindx = ++dynsymcount; | |
786 | } | |
787 | ||
788 | elf_link_hash_traverse (elf_hash_table (info), | |
789 | elf_link_renumber_hash_table_dynsyms, | |
790 | &dynsymcount); | |
791 | ||
792 | /* There is an unused NULL entry at the head of the table which | |
793 | we must account for in our count. Unless there weren't any | |
794 | symbols, which means we'll have no table at all. */ | |
795 | if (dynsymcount != 0) | |
796 | ++dynsymcount; | |
797 | ||
ccabcbe5 AM |
798 | elf_hash_table (info)->dynsymcount = dynsymcount; |
799 | return dynsymcount; | |
30b30c21 | 800 | } |
252b5132 | 801 | |
45d6a902 AM |
802 | /* This function is called when we want to define a new symbol. It |
803 | handles the various cases which arise when we find a definition in | |
804 | a dynamic object, or when there is already a definition in a | |
805 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
806 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
807 | OVERRIDE if the old symbol is overriding a new definition. We set | |
808 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
809 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
810 | change, we mean that we shouldn't warn if the type or size does | |
af44c138 L |
811 | change. We set POLD_ALIGNMENT if an old common symbol in a dynamic |
812 | object is overridden by a regular object. */ | |
45d6a902 AM |
813 | |
814 | bfd_boolean | |
268b6b39 AM |
815 | _bfd_elf_merge_symbol (bfd *abfd, |
816 | struct bfd_link_info *info, | |
817 | const char *name, | |
818 | Elf_Internal_Sym *sym, | |
819 | asection **psec, | |
820 | bfd_vma *pvalue, | |
af44c138 | 821 | unsigned int *pold_alignment, |
268b6b39 AM |
822 | struct elf_link_hash_entry **sym_hash, |
823 | bfd_boolean *skip, | |
824 | bfd_boolean *override, | |
825 | bfd_boolean *type_change_ok, | |
0f8a2703 | 826 | bfd_boolean *size_change_ok) |
252b5132 | 827 | { |
7479dfd4 | 828 | asection *sec, *oldsec; |
45d6a902 AM |
829 | struct elf_link_hash_entry *h; |
830 | struct elf_link_hash_entry *flip; | |
831 | int bind; | |
832 | bfd *oldbfd; | |
833 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
77cfaee6 | 834 | bfd_boolean newweak, oldweak; |
a4d8e49b | 835 | const struct elf_backend_data *bed; |
45d6a902 AM |
836 | |
837 | *skip = FALSE; | |
838 | *override = FALSE; | |
839 | ||
840 | sec = *psec; | |
841 | bind = ELF_ST_BIND (sym->st_info); | |
842 | ||
cd7be95b KH |
843 | /* Silently discard TLS symbols from --just-syms. There's no way to |
844 | combine a static TLS block with a new TLS block for this executable. */ | |
845 | if (ELF_ST_TYPE (sym->st_info) == STT_TLS | |
846 | && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
847 | { | |
848 | *skip = TRUE; | |
849 | return TRUE; | |
850 | } | |
851 | ||
45d6a902 AM |
852 | if (! bfd_is_und_section (sec)) |
853 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
854 | else | |
855 | h = ((struct elf_link_hash_entry *) | |
856 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
857 | if (h == NULL) | |
858 | return FALSE; | |
859 | *sym_hash = h; | |
252b5132 | 860 | |
45d6a902 AM |
861 | /* This code is for coping with dynamic objects, and is only useful |
862 | if we are doing an ELF link. */ | |
863 | if (info->hash->creator != abfd->xvec) | |
864 | return TRUE; | |
252b5132 | 865 | |
45d6a902 AM |
866 | /* For merging, we only care about real symbols. */ |
867 | ||
868 | while (h->root.type == bfd_link_hash_indirect | |
869 | || h->root.type == bfd_link_hash_warning) | |
870 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
871 | ||
40b36307 L |
872 | /* We have to check it for every instance since the first few may be |
873 | refereences and not all compilers emit symbol type for undefined | |
874 | symbols. */ | |
875 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
876 | ||
45d6a902 AM |
877 | /* If we just created the symbol, mark it as being an ELF symbol. |
878 | Other than that, there is nothing to do--there is no merge issue | |
879 | with a newly defined symbol--so we just return. */ | |
880 | ||
881 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 882 | { |
f5385ebf | 883 | h->non_elf = 0; |
45d6a902 AM |
884 | return TRUE; |
885 | } | |
252b5132 | 886 | |
7479dfd4 L |
887 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
888 | existing symbol. */ | |
252b5132 | 889 | |
45d6a902 AM |
890 | switch (h->root.type) |
891 | { | |
892 | default: | |
893 | oldbfd = NULL; | |
7479dfd4 | 894 | oldsec = NULL; |
45d6a902 | 895 | break; |
252b5132 | 896 | |
45d6a902 AM |
897 | case bfd_link_hash_undefined: |
898 | case bfd_link_hash_undefweak: | |
899 | oldbfd = h->root.u.undef.abfd; | |
7479dfd4 | 900 | oldsec = NULL; |
45d6a902 AM |
901 | break; |
902 | ||
903 | case bfd_link_hash_defined: | |
904 | case bfd_link_hash_defweak: | |
905 | oldbfd = h->root.u.def.section->owner; | |
7479dfd4 | 906 | oldsec = h->root.u.def.section; |
45d6a902 AM |
907 | break; |
908 | ||
909 | case bfd_link_hash_common: | |
910 | oldbfd = h->root.u.c.p->section->owner; | |
7479dfd4 | 911 | oldsec = h->root.u.c.p->section; |
45d6a902 AM |
912 | break; |
913 | } | |
914 | ||
915 | /* In cases involving weak versioned symbols, we may wind up trying | |
916 | to merge a symbol with itself. Catch that here, to avoid the | |
917 | confusion that results if we try to override a symbol with | |
918 | itself. The additional tests catch cases like | |
919 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
920 | dynamic object, which we do want to handle here. */ | |
921 | if (abfd == oldbfd | |
922 | && ((abfd->flags & DYNAMIC) == 0 | |
f5385ebf | 923 | || !h->def_regular)) |
45d6a902 AM |
924 | return TRUE; |
925 | ||
926 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
927 | respectively, is from a dynamic object. */ | |
928 | ||
707bba77 | 929 | newdyn = (abfd->flags & DYNAMIC) != 0; |
45d6a902 | 930 | |
707bba77 | 931 | olddyn = FALSE; |
45d6a902 AM |
932 | if (oldbfd != NULL) |
933 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 934 | else if (oldsec != NULL) |
45d6a902 | 935 | { |
707bba77 | 936 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 937 | indices used by MIPS ELF. */ |
707bba77 | 938 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 939 | } |
252b5132 | 940 | |
45d6a902 AM |
941 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
942 | respectively, appear to be a definition rather than reference. */ | |
943 | ||
707bba77 | 944 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 945 | |
707bba77 AM |
946 | olddef = (h->root.type != bfd_link_hash_undefined |
947 | && h->root.type != bfd_link_hash_undefweak | |
948 | && h->root.type != bfd_link_hash_common); | |
45d6a902 | 949 | |
fcb93ecf | 950 | bed = get_elf_backend_data (abfd); |
580a2b6e L |
951 | /* When we try to create a default indirect symbol from the dynamic |
952 | definition with the default version, we skip it if its type and | |
953 | the type of existing regular definition mismatch. We only do it | |
954 | if the existing regular definition won't be dynamic. */ | |
955 | if (pold_alignment == NULL | |
956 | && !info->shared | |
957 | && !info->export_dynamic | |
958 | && !h->ref_dynamic | |
959 | && newdyn | |
960 | && newdef | |
961 | && !olddyn | |
962 | && (olddef || h->root.type == bfd_link_hash_common) | |
963 | && ELF_ST_TYPE (sym->st_info) != h->type | |
964 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
fcb93ecf PB |
965 | && h->type != STT_NOTYPE |
966 | && !(bed->is_function_type (ELF_ST_TYPE (sym->st_info)) | |
967 | && bed->is_function_type (h->type))) | |
580a2b6e L |
968 | { |
969 | *skip = TRUE; | |
970 | return TRUE; | |
971 | } | |
972 | ||
68f49ba3 L |
973 | /* Check TLS symbol. We don't check undefined symbol introduced by |
974 | "ld -u". */ | |
7479dfd4 | 975 | if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS) |
68f49ba3 L |
976 | && ELF_ST_TYPE (sym->st_info) != h->type |
977 | && oldbfd != NULL) | |
7479dfd4 L |
978 | { |
979 | bfd *ntbfd, *tbfd; | |
980 | bfd_boolean ntdef, tdef; | |
981 | asection *ntsec, *tsec; | |
982 | ||
983 | if (h->type == STT_TLS) | |
984 | { | |
3b36f7e6 | 985 | ntbfd = abfd; |
7479dfd4 L |
986 | ntsec = sec; |
987 | ntdef = newdef; | |
988 | tbfd = oldbfd; | |
989 | tsec = oldsec; | |
990 | tdef = olddef; | |
991 | } | |
992 | else | |
993 | { | |
994 | ntbfd = oldbfd; | |
995 | ntsec = oldsec; | |
996 | ntdef = olddef; | |
997 | tbfd = abfd; | |
998 | tsec = sec; | |
999 | tdef = newdef; | |
1000 | } | |
1001 | ||
1002 | if (tdef && ntdef) | |
1003 | (*_bfd_error_handler) | |
1004 | (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), | |
1005 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); | |
1006 | else if (!tdef && !ntdef) | |
1007 | (*_bfd_error_handler) | |
1008 | (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), | |
1009 | tbfd, ntbfd, h->root.root.string); | |
1010 | else if (tdef) | |
1011 | (*_bfd_error_handler) | |
1012 | (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), | |
1013 | tbfd, tsec, ntbfd, h->root.root.string); | |
1014 | else | |
1015 | (*_bfd_error_handler) | |
1016 | (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), | |
1017 | tbfd, ntbfd, ntsec, h->root.root.string); | |
1018 | ||
1019 | bfd_set_error (bfd_error_bad_value); | |
1020 | return FALSE; | |
1021 | } | |
1022 | ||
4cc11e76 | 1023 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
1024 | object or is weak in all dynamic objects. Internal and hidden |
1025 | visibility will make it unavailable to dynamic objects. */ | |
f5385ebf | 1026 | if (newdyn && !h->dynamic_def) |
45d6a902 AM |
1027 | { |
1028 | if (!bfd_is_und_section (sec)) | |
f5385ebf | 1029 | h->dynamic_def = 1; |
45d6a902 | 1030 | else |
252b5132 | 1031 | { |
45d6a902 AM |
1032 | /* Check if this symbol is weak in all dynamic objects. If it |
1033 | is the first time we see it in a dynamic object, we mark | |
1034 | if it is weak. Otherwise, we clear it. */ | |
f5385ebf | 1035 | if (!h->ref_dynamic) |
79349b09 | 1036 | { |
45d6a902 | 1037 | if (bind == STB_WEAK) |
f5385ebf | 1038 | h->dynamic_weak = 1; |
252b5132 | 1039 | } |
45d6a902 | 1040 | else if (bind != STB_WEAK) |
f5385ebf | 1041 | h->dynamic_weak = 0; |
252b5132 | 1042 | } |
45d6a902 | 1043 | } |
252b5132 | 1044 | |
45d6a902 AM |
1045 | /* If the old symbol has non-default visibility, we ignore the new |
1046 | definition from a dynamic object. */ | |
1047 | if (newdyn | |
9c7a29a3 | 1048 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1049 | && !bfd_is_und_section (sec)) |
1050 | { | |
1051 | *skip = TRUE; | |
1052 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1053 | h->ref_dynamic = 1; |
45d6a902 AM |
1054 | /* A protected symbol has external availability. Make sure it is |
1055 | recorded as dynamic. | |
1056 | ||
1057 | FIXME: Should we check type and size for protected symbol? */ | |
1058 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1059 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1060 | else |
1061 | return TRUE; | |
1062 | } | |
1063 | else if (!newdyn | |
9c7a29a3 | 1064 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1065 | && h->def_dynamic) |
45d6a902 AM |
1066 | { |
1067 | /* If the new symbol with non-default visibility comes from a | |
1068 | relocatable file and the old definition comes from a dynamic | |
1069 | object, we remove the old definition. */ | |
1070 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
d2dee3b2 L |
1071 | { |
1072 | /* Handle the case where the old dynamic definition is | |
1073 | default versioned. We need to copy the symbol info from | |
1074 | the symbol with default version to the normal one if it | |
1075 | was referenced before. */ | |
1076 | if (h->ref_regular) | |
1077 | { | |
1078 | const struct elf_backend_data *bed | |
1079 | = get_elf_backend_data (abfd); | |
1080 | struct elf_link_hash_entry *vh = *sym_hash; | |
1081 | vh->root.type = h->root.type; | |
1082 | h->root.type = bfd_link_hash_indirect; | |
1083 | (*bed->elf_backend_copy_indirect_symbol) (info, vh, h); | |
1084 | /* Protected symbols will override the dynamic definition | |
1085 | with default version. */ | |
1086 | if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED) | |
1087 | { | |
1088 | h->root.u.i.link = (struct bfd_link_hash_entry *) vh; | |
1089 | vh->dynamic_def = 1; | |
1090 | vh->ref_dynamic = 1; | |
1091 | } | |
1092 | else | |
1093 | { | |
1094 | h->root.type = vh->root.type; | |
1095 | vh->ref_dynamic = 0; | |
1096 | /* We have to hide it here since it was made dynamic | |
1097 | global with extra bits when the symbol info was | |
1098 | copied from the old dynamic definition. */ | |
1099 | (*bed->elf_backend_hide_symbol) (info, vh, TRUE); | |
1100 | } | |
1101 | h = vh; | |
1102 | } | |
1103 | else | |
1104 | h = *sym_hash; | |
1105 | } | |
1de1a317 | 1106 | |
f6e332e6 | 1107 | if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root) |
1de1a317 L |
1108 | && bfd_is_und_section (sec)) |
1109 | { | |
1110 | /* If the new symbol is undefined and the old symbol was | |
1111 | also undefined before, we need to make sure | |
1112 | _bfd_generic_link_add_one_symbol doesn't mess | |
f6e332e6 | 1113 | up the linker hash table undefs list. Since the old |
1de1a317 L |
1114 | definition came from a dynamic object, it is still on the |
1115 | undefs list. */ | |
1116 | h->root.type = bfd_link_hash_undefined; | |
1de1a317 L |
1117 | h->root.u.undef.abfd = abfd; |
1118 | } | |
1119 | else | |
1120 | { | |
1121 | h->root.type = bfd_link_hash_new; | |
1122 | h->root.u.undef.abfd = NULL; | |
1123 | } | |
1124 | ||
f5385ebf | 1125 | if (h->def_dynamic) |
252b5132 | 1126 | { |
f5385ebf AM |
1127 | h->def_dynamic = 0; |
1128 | h->ref_dynamic = 1; | |
1129 | h->dynamic_def = 1; | |
45d6a902 AM |
1130 | } |
1131 | /* FIXME: Should we check type and size for protected symbol? */ | |
1132 | h->size = 0; | |
1133 | h->type = 0; | |
1134 | return TRUE; | |
1135 | } | |
14a793b2 | 1136 | |
79349b09 AM |
1137 | /* Differentiate strong and weak symbols. */ |
1138 | newweak = bind == STB_WEAK; | |
1139 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1140 | || h->root.type == bfd_link_hash_undefweak); | |
14a793b2 | 1141 | |
15b43f48 AM |
1142 | /* If a new weak symbol definition comes from a regular file and the |
1143 | old symbol comes from a dynamic library, we treat the new one as | |
1144 | strong. Similarly, an old weak symbol definition from a regular | |
1145 | file is treated as strong when the new symbol comes from a dynamic | |
1146 | library. Further, an old weak symbol from a dynamic library is | |
1147 | treated as strong if the new symbol is from a dynamic library. | |
1148 | This reflects the way glibc's ld.so works. | |
1149 | ||
1150 | Do this before setting *type_change_ok or *size_change_ok so that | |
1151 | we warn properly when dynamic library symbols are overridden. */ | |
1152 | ||
1153 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1154 | newweak = FALSE; |
15b43f48 | 1155 | if (olddef && newdyn) |
0f8a2703 AM |
1156 | oldweak = FALSE; |
1157 | ||
fcb93ecf PB |
1158 | /* Allow changes between different types of funciton symbol. */ |
1159 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)) | |
1160 | && bed->is_function_type (h->type)) | |
1161 | *type_change_ok = TRUE; | |
1162 | ||
79349b09 AM |
1163 | /* It's OK to change the type if either the existing symbol or the |
1164 | new symbol is weak. A type change is also OK if the old symbol | |
1165 | is undefined and the new symbol is defined. */ | |
252b5132 | 1166 | |
79349b09 AM |
1167 | if (oldweak |
1168 | || newweak | |
1169 | || (newdef | |
1170 | && h->root.type == bfd_link_hash_undefined)) | |
1171 | *type_change_ok = TRUE; | |
1172 | ||
1173 | /* It's OK to change the size if either the existing symbol or the | |
1174 | new symbol is weak, or if the old symbol is undefined. */ | |
1175 | ||
1176 | if (*type_change_ok | |
1177 | || h->root.type == bfd_link_hash_undefined) | |
1178 | *size_change_ok = TRUE; | |
45d6a902 | 1179 | |
45d6a902 AM |
1180 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1181 | symbol, respectively, appears to be a common symbol in a dynamic | |
1182 | object. If a symbol appears in an uninitialized section, and is | |
1183 | not weak, and is not a function, then it may be a common symbol | |
1184 | which was resolved when the dynamic object was created. We want | |
1185 | to treat such symbols specially, because they raise special | |
1186 | considerations when setting the symbol size: if the symbol | |
1187 | appears as a common symbol in a regular object, and the size in | |
1188 | the regular object is larger, we must make sure that we use the | |
1189 | larger size. This problematic case can always be avoided in C, | |
1190 | but it must be handled correctly when using Fortran shared | |
1191 | libraries. | |
1192 | ||
1193 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1194 | likewise for OLDDYNCOMMON and OLDDEF. | |
1195 | ||
1196 | Note that this test is just a heuristic, and that it is quite | |
1197 | possible to have an uninitialized symbol in a shared object which | |
1198 | is really a definition, rather than a common symbol. This could | |
1199 | lead to some minor confusion when the symbol really is a common | |
1200 | symbol in some regular object. However, I think it will be | |
1201 | harmless. */ | |
1202 | ||
1203 | if (newdyn | |
1204 | && newdef | |
79349b09 | 1205 | && !newweak |
45d6a902 AM |
1206 | && (sec->flags & SEC_ALLOC) != 0 |
1207 | && (sec->flags & SEC_LOAD) == 0 | |
1208 | && sym->st_size > 0 | |
fcb93ecf | 1209 | && !bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
45d6a902 AM |
1210 | newdyncommon = TRUE; |
1211 | else | |
1212 | newdyncommon = FALSE; | |
1213 | ||
1214 | if (olddyn | |
1215 | && olddef | |
1216 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1217 | && h->def_dynamic |
45d6a902 AM |
1218 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1219 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1220 | && h->size > 0 | |
fcb93ecf | 1221 | && !bed->is_function_type (h->type)) |
45d6a902 AM |
1222 | olddyncommon = TRUE; |
1223 | else | |
1224 | olddyncommon = FALSE; | |
1225 | ||
a4d8e49b L |
1226 | /* We now know everything about the old and new symbols. We ask the |
1227 | backend to check if we can merge them. */ | |
a4d8e49b L |
1228 | if (bed->merge_symbol |
1229 | && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue, | |
1230 | pold_alignment, skip, override, | |
1231 | type_change_ok, size_change_ok, | |
1232 | &newdyn, &newdef, &newdyncommon, &newweak, | |
1233 | abfd, &sec, | |
1234 | &olddyn, &olddef, &olddyncommon, &oldweak, | |
1235 | oldbfd, &oldsec)) | |
1236 | return FALSE; | |
1237 | ||
45d6a902 AM |
1238 | /* If both the old and the new symbols look like common symbols in a |
1239 | dynamic object, set the size of the symbol to the larger of the | |
1240 | two. */ | |
1241 | ||
1242 | if (olddyncommon | |
1243 | && newdyncommon | |
1244 | && sym->st_size != h->size) | |
1245 | { | |
1246 | /* Since we think we have two common symbols, issue a multiple | |
1247 | common warning if desired. Note that we only warn if the | |
1248 | size is different. If the size is the same, we simply let | |
1249 | the old symbol override the new one as normally happens with | |
1250 | symbols defined in dynamic objects. */ | |
1251 | ||
1252 | if (! ((*info->callbacks->multiple_common) | |
1253 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1254 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1255 | return FALSE; | |
252b5132 | 1256 | |
45d6a902 AM |
1257 | if (sym->st_size > h->size) |
1258 | h->size = sym->st_size; | |
252b5132 | 1259 | |
45d6a902 | 1260 | *size_change_ok = TRUE; |
252b5132 RH |
1261 | } |
1262 | ||
45d6a902 AM |
1263 | /* If we are looking at a dynamic object, and we have found a |
1264 | definition, we need to see if the symbol was already defined by | |
1265 | some other object. If so, we want to use the existing | |
1266 | definition, and we do not want to report a multiple symbol | |
1267 | definition error; we do this by clobbering *PSEC to be | |
1268 | bfd_und_section_ptr. | |
1269 | ||
1270 | We treat a common symbol as a definition if the symbol in the | |
1271 | shared library is a function, since common symbols always | |
1272 | represent variables; this can cause confusion in principle, but | |
1273 | any such confusion would seem to indicate an erroneous program or | |
1274 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1275 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1276 | |
1277 | if (newdyn | |
1278 | && newdef | |
77cfaee6 | 1279 | && (olddef |
45d6a902 | 1280 | || (h->root.type == bfd_link_hash_common |
79349b09 | 1281 | && (newweak |
fcb93ecf | 1282 | || bed->is_function_type (ELF_ST_TYPE (sym->st_info)))))) |
45d6a902 AM |
1283 | { |
1284 | *override = TRUE; | |
1285 | newdef = FALSE; | |
1286 | newdyncommon = FALSE; | |
252b5132 | 1287 | |
45d6a902 AM |
1288 | *psec = sec = bfd_und_section_ptr; |
1289 | *size_change_ok = TRUE; | |
252b5132 | 1290 | |
45d6a902 AM |
1291 | /* If we get here when the old symbol is a common symbol, then |
1292 | we are explicitly letting it override a weak symbol or | |
1293 | function in a dynamic object, and we don't want to warn about | |
1294 | a type change. If the old symbol is a defined symbol, a type | |
1295 | change warning may still be appropriate. */ | |
252b5132 | 1296 | |
45d6a902 AM |
1297 | if (h->root.type == bfd_link_hash_common) |
1298 | *type_change_ok = TRUE; | |
1299 | } | |
1300 | ||
1301 | /* Handle the special case of an old common symbol merging with a | |
1302 | new symbol which looks like a common symbol in a shared object. | |
1303 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1304 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1305 | right thing. */ | |
45d6a902 AM |
1306 | |
1307 | if (newdyncommon | |
1308 | && h->root.type == bfd_link_hash_common) | |
1309 | { | |
1310 | *override = TRUE; | |
1311 | newdef = FALSE; | |
1312 | newdyncommon = FALSE; | |
1313 | *pvalue = sym->st_size; | |
a4d8e49b | 1314 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1315 | *size_change_ok = TRUE; |
1316 | } | |
1317 | ||
c5e2cead | 1318 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1319 | if (newdef && olddef && newweak) |
c5e2cead L |
1320 | *skip = TRUE; |
1321 | ||
45d6a902 AM |
1322 | /* If the old symbol is from a dynamic object, and the new symbol is |
1323 | a definition which is not from a dynamic object, then the new | |
1324 | symbol overrides the old symbol. Symbols from regular files | |
1325 | always take precedence over symbols from dynamic objects, even if | |
1326 | they are defined after the dynamic object in the link. | |
1327 | ||
1328 | As above, we again permit a common symbol in a regular object to | |
1329 | override a definition in a shared object if the shared object | |
0f8a2703 | 1330 | symbol is a function or is weak. */ |
45d6a902 AM |
1331 | |
1332 | flip = NULL; | |
77cfaee6 | 1333 | if (!newdyn |
45d6a902 AM |
1334 | && (newdef |
1335 | || (bfd_is_com_section (sec) | |
79349b09 | 1336 | && (oldweak |
fcb93ecf | 1337 | || bed->is_function_type (h->type)))) |
45d6a902 AM |
1338 | && olddyn |
1339 | && olddef | |
f5385ebf | 1340 | && h->def_dynamic) |
45d6a902 AM |
1341 | { |
1342 | /* Change the hash table entry to undefined, and let | |
1343 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1344 | new definition. */ | |
1345 | ||
1346 | h->root.type = bfd_link_hash_undefined; | |
1347 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1348 | *size_change_ok = TRUE; | |
1349 | ||
1350 | olddef = FALSE; | |
1351 | olddyncommon = FALSE; | |
1352 | ||
1353 | /* We again permit a type change when a common symbol may be | |
1354 | overriding a function. */ | |
1355 | ||
1356 | if (bfd_is_com_section (sec)) | |
1357 | *type_change_ok = TRUE; | |
1358 | ||
1359 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1360 | flip = *sym_hash; | |
1361 | else | |
1362 | /* This union may have been set to be non-NULL when this symbol | |
1363 | was seen in a dynamic object. We must force the union to be | |
1364 | NULL, so that it is correct for a regular symbol. */ | |
1365 | h->verinfo.vertree = NULL; | |
1366 | } | |
1367 | ||
1368 | /* Handle the special case of a new common symbol merging with an | |
1369 | old symbol that looks like it might be a common symbol defined in | |
1370 | a shared object. Note that we have already handled the case in | |
1371 | which a new common symbol should simply override the definition | |
1372 | in the shared library. */ | |
1373 | ||
1374 | if (! newdyn | |
1375 | && bfd_is_com_section (sec) | |
1376 | && olddyncommon) | |
1377 | { | |
1378 | /* It would be best if we could set the hash table entry to a | |
1379 | common symbol, but we don't know what to use for the section | |
1380 | or the alignment. */ | |
1381 | if (! ((*info->callbacks->multiple_common) | |
1382 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1383 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1384 | return FALSE; | |
1385 | ||
4cc11e76 | 1386 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1387 | larger, pretend that the new symbol has its size. */ |
1388 | ||
1389 | if (h->size > *pvalue) | |
1390 | *pvalue = h->size; | |
1391 | ||
af44c138 L |
1392 | /* We need to remember the alignment required by the symbol |
1393 | in the dynamic object. */ | |
1394 | BFD_ASSERT (pold_alignment); | |
1395 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1396 | |
1397 | olddef = FALSE; | |
1398 | olddyncommon = FALSE; | |
1399 | ||
1400 | h->root.type = bfd_link_hash_undefined; | |
1401 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1402 | ||
1403 | *size_change_ok = TRUE; | |
1404 | *type_change_ok = TRUE; | |
1405 | ||
1406 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1407 | flip = *sym_hash; | |
1408 | else | |
1409 | h->verinfo.vertree = NULL; | |
1410 | } | |
1411 | ||
1412 | if (flip != NULL) | |
1413 | { | |
1414 | /* Handle the case where we had a versioned symbol in a dynamic | |
1415 | library and now find a definition in a normal object. In this | |
1416 | case, we make the versioned symbol point to the normal one. */ | |
9c5bfbb7 | 1417 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1418 | flip->root.type = h->root.type; |
1419 | h->root.type = bfd_link_hash_indirect; | |
1420 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1421 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
45d6a902 | 1422 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
f5385ebf | 1423 | if (h->def_dynamic) |
45d6a902 | 1424 | { |
f5385ebf AM |
1425 | h->def_dynamic = 0; |
1426 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1427 | } |
1428 | } | |
1429 | ||
45d6a902 AM |
1430 | return TRUE; |
1431 | } | |
1432 | ||
1433 | /* This function is called to create an indirect symbol from the | |
1434 | default for the symbol with the default version if needed. The | |
1435 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1436 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 AM |
1437 | |
1438 | bfd_boolean | |
268b6b39 AM |
1439 | _bfd_elf_add_default_symbol (bfd *abfd, |
1440 | struct bfd_link_info *info, | |
1441 | struct elf_link_hash_entry *h, | |
1442 | const char *name, | |
1443 | Elf_Internal_Sym *sym, | |
1444 | asection **psec, | |
1445 | bfd_vma *value, | |
1446 | bfd_boolean *dynsym, | |
0f8a2703 | 1447 | bfd_boolean override) |
45d6a902 AM |
1448 | { |
1449 | bfd_boolean type_change_ok; | |
1450 | bfd_boolean size_change_ok; | |
1451 | bfd_boolean skip; | |
1452 | char *shortname; | |
1453 | struct elf_link_hash_entry *hi; | |
1454 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1455 | const struct elf_backend_data *bed; |
45d6a902 AM |
1456 | bfd_boolean collect; |
1457 | bfd_boolean dynamic; | |
1458 | char *p; | |
1459 | size_t len, shortlen; | |
1460 | asection *sec; | |
1461 | ||
1462 | /* If this symbol has a version, and it is the default version, we | |
1463 | create an indirect symbol from the default name to the fully | |
1464 | decorated name. This will cause external references which do not | |
1465 | specify a version to be bound to this version of the symbol. */ | |
1466 | p = strchr (name, ELF_VER_CHR); | |
1467 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1468 | return TRUE; | |
1469 | ||
1470 | if (override) | |
1471 | { | |
4cc11e76 | 1472 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1473 | need to create the indirect symbol from the default name. */ |
1474 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1475 | FALSE, FALSE); | |
1476 | BFD_ASSERT (hi != NULL); | |
1477 | if (hi == h) | |
1478 | return TRUE; | |
1479 | while (hi->root.type == bfd_link_hash_indirect | |
1480 | || hi->root.type == bfd_link_hash_warning) | |
1481 | { | |
1482 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1483 | if (hi == h) | |
1484 | return TRUE; | |
1485 | } | |
1486 | } | |
1487 | ||
1488 | bed = get_elf_backend_data (abfd); | |
1489 | collect = bed->collect; | |
1490 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1491 | ||
1492 | shortlen = p - name; | |
1493 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1494 | if (shortname == NULL) | |
1495 | return FALSE; | |
1496 | memcpy (shortname, name, shortlen); | |
1497 | shortname[shortlen] = '\0'; | |
1498 | ||
1499 | /* We are going to create a new symbol. Merge it with any existing | |
1500 | symbol with this name. For the purposes of the merge, act as | |
1501 | though we were defining the symbol we just defined, although we | |
1502 | actually going to define an indirect symbol. */ | |
1503 | type_change_ok = FALSE; | |
1504 | size_change_ok = FALSE; | |
1505 | sec = *psec; | |
1506 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1507 | NULL, &hi, &skip, &override, |
1508 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1509 | return FALSE; |
1510 | ||
1511 | if (skip) | |
1512 | goto nondefault; | |
1513 | ||
1514 | if (! override) | |
1515 | { | |
1516 | bh = &hi->root; | |
1517 | if (! (_bfd_generic_link_add_one_symbol | |
1518 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1519 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1520 | return FALSE; |
1521 | hi = (struct elf_link_hash_entry *) bh; | |
1522 | } | |
1523 | else | |
1524 | { | |
1525 | /* In this case the symbol named SHORTNAME is overriding the | |
1526 | indirect symbol we want to add. We were planning on making | |
1527 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1528 | is the name without a version. NAME is the fully versioned | |
1529 | name, and it is the default version. | |
1530 | ||
1531 | Overriding means that we already saw a definition for the | |
1532 | symbol SHORTNAME in a regular object, and it is overriding | |
1533 | the symbol defined in the dynamic object. | |
1534 | ||
1535 | When this happens, we actually want to change NAME, the | |
1536 | symbol we just added, to refer to SHORTNAME. This will cause | |
1537 | references to NAME in the shared object to become references | |
1538 | to SHORTNAME in the regular object. This is what we expect | |
1539 | when we override a function in a shared object: that the | |
1540 | references in the shared object will be mapped to the | |
1541 | definition in the regular object. */ | |
1542 | ||
1543 | while (hi->root.type == bfd_link_hash_indirect | |
1544 | || hi->root.type == bfd_link_hash_warning) | |
1545 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1546 | ||
1547 | h->root.type = bfd_link_hash_indirect; | |
1548 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1549 | if (h->def_dynamic) |
45d6a902 | 1550 | { |
f5385ebf AM |
1551 | h->def_dynamic = 0; |
1552 | hi->ref_dynamic = 1; | |
1553 | if (hi->ref_regular | |
1554 | || hi->def_regular) | |
45d6a902 | 1555 | { |
c152c796 | 1556 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1557 | return FALSE; |
1558 | } | |
1559 | } | |
1560 | ||
1561 | /* Now set HI to H, so that the following code will set the | |
1562 | other fields correctly. */ | |
1563 | hi = h; | |
1564 | } | |
1565 | ||
fab4a87f L |
1566 | /* Check if HI is a warning symbol. */ |
1567 | if (hi->root.type == bfd_link_hash_warning) | |
1568 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1569 | ||
45d6a902 AM |
1570 | /* If there is a duplicate definition somewhere, then HI may not |
1571 | point to an indirect symbol. We will have reported an error to | |
1572 | the user in that case. */ | |
1573 | ||
1574 | if (hi->root.type == bfd_link_hash_indirect) | |
1575 | { | |
1576 | struct elf_link_hash_entry *ht; | |
1577 | ||
45d6a902 | 1578 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 1579 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 AM |
1580 | |
1581 | /* See if the new flags lead us to realize that the symbol must | |
1582 | be dynamic. */ | |
1583 | if (! *dynsym) | |
1584 | { | |
1585 | if (! dynamic) | |
1586 | { | |
1587 | if (info->shared | |
f5385ebf | 1588 | || hi->ref_dynamic) |
45d6a902 AM |
1589 | *dynsym = TRUE; |
1590 | } | |
1591 | else | |
1592 | { | |
f5385ebf | 1593 | if (hi->ref_regular) |
45d6a902 AM |
1594 | *dynsym = TRUE; |
1595 | } | |
1596 | } | |
1597 | } | |
1598 | ||
1599 | /* We also need to define an indirection from the nondefault version | |
1600 | of the symbol. */ | |
1601 | ||
1602 | nondefault: | |
1603 | len = strlen (name); | |
1604 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1605 | if (shortname == NULL) | |
1606 | return FALSE; | |
1607 | memcpy (shortname, name, shortlen); | |
1608 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1609 | ||
1610 | /* Once again, merge with any existing symbol. */ | |
1611 | type_change_ok = FALSE; | |
1612 | size_change_ok = FALSE; | |
1613 | sec = *psec; | |
1614 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1615 | NULL, &hi, &skip, &override, |
1616 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1617 | return FALSE; |
1618 | ||
1619 | if (skip) | |
1620 | return TRUE; | |
1621 | ||
1622 | if (override) | |
1623 | { | |
1624 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1625 | the type of override we do in the case above unless it is | |
4cc11e76 | 1626 | overridden by a versioned definition. */ |
45d6a902 AM |
1627 | if (hi->root.type != bfd_link_hash_defined |
1628 | && hi->root.type != bfd_link_hash_defweak) | |
1629 | (*_bfd_error_handler) | |
d003868e AM |
1630 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1631 | abfd, shortname); | |
45d6a902 AM |
1632 | } |
1633 | else | |
1634 | { | |
1635 | bh = &hi->root; | |
1636 | if (! (_bfd_generic_link_add_one_symbol | |
1637 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1638 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1639 | return FALSE; |
1640 | hi = (struct elf_link_hash_entry *) bh; | |
1641 | ||
1642 | /* If there is a duplicate definition somewhere, then HI may not | |
1643 | point to an indirect symbol. We will have reported an error | |
1644 | to the user in that case. */ | |
1645 | ||
1646 | if (hi->root.type == bfd_link_hash_indirect) | |
1647 | { | |
fcfa13d2 | 1648 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
45d6a902 AM |
1649 | |
1650 | /* See if the new flags lead us to realize that the symbol | |
1651 | must be dynamic. */ | |
1652 | if (! *dynsym) | |
1653 | { | |
1654 | if (! dynamic) | |
1655 | { | |
1656 | if (info->shared | |
f5385ebf | 1657 | || hi->ref_dynamic) |
45d6a902 AM |
1658 | *dynsym = TRUE; |
1659 | } | |
1660 | else | |
1661 | { | |
f5385ebf | 1662 | if (hi->ref_regular) |
45d6a902 AM |
1663 | *dynsym = TRUE; |
1664 | } | |
1665 | } | |
1666 | } | |
1667 | } | |
1668 | ||
1669 | return TRUE; | |
1670 | } | |
1671 | \f | |
1672 | /* This routine is used to export all defined symbols into the dynamic | |
1673 | symbol table. It is called via elf_link_hash_traverse. */ | |
1674 | ||
1675 | bfd_boolean | |
268b6b39 | 1676 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1677 | { |
268b6b39 | 1678 | struct elf_info_failed *eif = data; |
45d6a902 | 1679 | |
55255dae L |
1680 | /* Ignore this if we won't export it. */ |
1681 | if (!eif->info->export_dynamic && !h->dynamic) | |
1682 | return TRUE; | |
1683 | ||
45d6a902 AM |
1684 | /* Ignore indirect symbols. These are added by the versioning code. */ |
1685 | if (h->root.type == bfd_link_hash_indirect) | |
1686 | return TRUE; | |
1687 | ||
1688 | if (h->root.type == bfd_link_hash_warning) | |
1689 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1690 | ||
1691 | if (h->dynindx == -1 | |
f5385ebf AM |
1692 | && (h->def_regular |
1693 | || h->ref_regular)) | |
45d6a902 AM |
1694 | { |
1695 | struct bfd_elf_version_tree *t; | |
1696 | struct bfd_elf_version_expr *d; | |
1697 | ||
1698 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1699 | { | |
108ba305 | 1700 | if (t->globals.list != NULL) |
45d6a902 | 1701 | { |
108ba305 JJ |
1702 | d = (*t->match) (&t->globals, NULL, h->root.root.string); |
1703 | if (d != NULL) | |
1704 | goto doit; | |
45d6a902 AM |
1705 | } |
1706 | ||
108ba305 | 1707 | if (t->locals.list != NULL) |
45d6a902 | 1708 | { |
108ba305 JJ |
1709 | d = (*t->match) (&t->locals, NULL, h->root.root.string); |
1710 | if (d != NULL) | |
1711 | return TRUE; | |
45d6a902 AM |
1712 | } |
1713 | } | |
1714 | ||
1715 | if (!eif->verdefs) | |
1716 | { | |
1717 | doit: | |
c152c796 | 1718 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
1719 | { |
1720 | eif->failed = TRUE; | |
1721 | return FALSE; | |
1722 | } | |
1723 | } | |
1724 | } | |
1725 | ||
1726 | return TRUE; | |
1727 | } | |
1728 | \f | |
1729 | /* Look through the symbols which are defined in other shared | |
1730 | libraries and referenced here. Update the list of version | |
1731 | dependencies. This will be put into the .gnu.version_r section. | |
1732 | This function is called via elf_link_hash_traverse. */ | |
1733 | ||
1734 | bfd_boolean | |
268b6b39 AM |
1735 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1736 | void *data) | |
45d6a902 | 1737 | { |
268b6b39 | 1738 | struct elf_find_verdep_info *rinfo = data; |
45d6a902 AM |
1739 | Elf_Internal_Verneed *t; |
1740 | Elf_Internal_Vernaux *a; | |
1741 | bfd_size_type amt; | |
1742 | ||
1743 | if (h->root.type == bfd_link_hash_warning) | |
1744 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1745 | ||
1746 | /* We only care about symbols defined in shared objects with version | |
1747 | information. */ | |
f5385ebf AM |
1748 | if (!h->def_dynamic |
1749 | || h->def_regular | |
45d6a902 AM |
1750 | || h->dynindx == -1 |
1751 | || h->verinfo.verdef == NULL) | |
1752 | return TRUE; | |
1753 | ||
1754 | /* See if we already know about this version. */ | |
1755 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1756 | { | |
1757 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1758 | continue; | |
1759 | ||
1760 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1761 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1762 | return TRUE; | |
1763 | ||
1764 | break; | |
1765 | } | |
1766 | ||
1767 | /* This is a new version. Add it to tree we are building. */ | |
1768 | ||
1769 | if (t == NULL) | |
1770 | { | |
1771 | amt = sizeof *t; | |
268b6b39 | 1772 | t = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1773 | if (t == NULL) |
1774 | { | |
1775 | rinfo->failed = TRUE; | |
1776 | return FALSE; | |
1777 | } | |
1778 | ||
1779 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1780 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1781 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1782 | } | |
1783 | ||
1784 | amt = sizeof *a; | |
268b6b39 | 1785 | a = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1786 | |
1787 | /* Note that we are copying a string pointer here, and testing it | |
1788 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1789 | discard the string data when low in memory, this will have to be | |
1790 | fixed. */ | |
1791 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1792 | ||
1793 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1794 | a->vna_nextptr = t->vn_auxptr; | |
1795 | ||
1796 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1797 | ++rinfo->vers; | |
1798 | ||
1799 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1800 | ||
1801 | t->vn_auxptr = a; | |
1802 | ||
1803 | return TRUE; | |
1804 | } | |
1805 | ||
1806 | /* Figure out appropriate versions for all the symbols. We may not | |
1807 | have the version number script until we have read all of the input | |
1808 | files, so until that point we don't know which symbols should be | |
1809 | local. This function is called via elf_link_hash_traverse. */ | |
1810 | ||
1811 | bfd_boolean | |
268b6b39 | 1812 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
1813 | { |
1814 | struct elf_assign_sym_version_info *sinfo; | |
1815 | struct bfd_link_info *info; | |
9c5bfbb7 | 1816 | const struct elf_backend_data *bed; |
45d6a902 AM |
1817 | struct elf_info_failed eif; |
1818 | char *p; | |
1819 | bfd_size_type amt; | |
1820 | ||
268b6b39 | 1821 | sinfo = data; |
45d6a902 AM |
1822 | info = sinfo->info; |
1823 | ||
1824 | if (h->root.type == bfd_link_hash_warning) | |
1825 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1826 | ||
1827 | /* Fix the symbol flags. */ | |
1828 | eif.failed = FALSE; | |
1829 | eif.info = info; | |
1830 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1831 | { | |
1832 | if (eif.failed) | |
1833 | sinfo->failed = TRUE; | |
1834 | return FALSE; | |
1835 | } | |
1836 | ||
1837 | /* We only need version numbers for symbols defined in regular | |
1838 | objects. */ | |
f5385ebf | 1839 | if (!h->def_regular) |
45d6a902 AM |
1840 | return TRUE; |
1841 | ||
1842 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1843 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1844 | if (p != NULL && h->verinfo.vertree == NULL) | |
1845 | { | |
1846 | struct bfd_elf_version_tree *t; | |
1847 | bfd_boolean hidden; | |
1848 | ||
1849 | hidden = TRUE; | |
1850 | ||
1851 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1852 | not a hidden symbol. */ | |
1853 | ++p; | |
1854 | if (*p == ELF_VER_CHR) | |
1855 | { | |
1856 | hidden = FALSE; | |
1857 | ++p; | |
1858 | } | |
1859 | ||
1860 | /* If there is no version string, we can just return out. */ | |
1861 | if (*p == '\0') | |
1862 | { | |
1863 | if (hidden) | |
f5385ebf | 1864 | h->hidden = 1; |
45d6a902 AM |
1865 | return TRUE; |
1866 | } | |
1867 | ||
1868 | /* Look for the version. If we find it, it is no longer weak. */ | |
1869 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1870 | { | |
1871 | if (strcmp (t->name, p) == 0) | |
1872 | { | |
1873 | size_t len; | |
1874 | char *alc; | |
1875 | struct bfd_elf_version_expr *d; | |
1876 | ||
1877 | len = p - h->root.root.string; | |
268b6b39 | 1878 | alc = bfd_malloc (len); |
45d6a902 AM |
1879 | if (alc == NULL) |
1880 | return FALSE; | |
1881 | memcpy (alc, h->root.root.string, len - 1); | |
1882 | alc[len - 1] = '\0'; | |
1883 | if (alc[len - 2] == ELF_VER_CHR) | |
1884 | alc[len - 2] = '\0'; | |
1885 | ||
1886 | h->verinfo.vertree = t; | |
1887 | t->used = TRUE; | |
1888 | d = NULL; | |
1889 | ||
108ba305 JJ |
1890 | if (t->globals.list != NULL) |
1891 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
1892 | |
1893 | /* See if there is anything to force this symbol to | |
1894 | local scope. */ | |
108ba305 | 1895 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 1896 | { |
108ba305 JJ |
1897 | d = (*t->match) (&t->locals, NULL, alc); |
1898 | if (d != NULL | |
1899 | && h->dynindx != -1 | |
108ba305 JJ |
1900 | && ! info->export_dynamic) |
1901 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
1902 | } |
1903 | ||
1904 | free (alc); | |
1905 | break; | |
1906 | } | |
1907 | } | |
1908 | ||
1909 | /* If we are building an application, we need to create a | |
1910 | version node for this version. */ | |
36af4a4e | 1911 | if (t == NULL && info->executable) |
45d6a902 AM |
1912 | { |
1913 | struct bfd_elf_version_tree **pp; | |
1914 | int version_index; | |
1915 | ||
1916 | /* If we aren't going to export this symbol, we don't need | |
1917 | to worry about it. */ | |
1918 | if (h->dynindx == -1) | |
1919 | return TRUE; | |
1920 | ||
1921 | amt = sizeof *t; | |
108ba305 | 1922 | t = bfd_zalloc (sinfo->output_bfd, amt); |
45d6a902 AM |
1923 | if (t == NULL) |
1924 | { | |
1925 | sinfo->failed = TRUE; | |
1926 | return FALSE; | |
1927 | } | |
1928 | ||
45d6a902 | 1929 | t->name = p; |
45d6a902 AM |
1930 | t->name_indx = (unsigned int) -1; |
1931 | t->used = TRUE; | |
1932 | ||
1933 | version_index = 1; | |
1934 | /* Don't count anonymous version tag. */ | |
1935 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1936 | version_index = 0; | |
1937 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1938 | ++version_index; | |
1939 | t->vernum = version_index; | |
1940 | ||
1941 | *pp = t; | |
1942 | ||
1943 | h->verinfo.vertree = t; | |
1944 | } | |
1945 | else if (t == NULL) | |
1946 | { | |
1947 | /* We could not find the version for a symbol when | |
1948 | generating a shared archive. Return an error. */ | |
1949 | (*_bfd_error_handler) | |
d003868e AM |
1950 | (_("%B: undefined versioned symbol name %s"), |
1951 | sinfo->output_bfd, h->root.root.string); | |
45d6a902 AM |
1952 | bfd_set_error (bfd_error_bad_value); |
1953 | sinfo->failed = TRUE; | |
1954 | return FALSE; | |
1955 | } | |
1956 | ||
1957 | if (hidden) | |
f5385ebf | 1958 | h->hidden = 1; |
45d6a902 AM |
1959 | } |
1960 | ||
1961 | /* If we don't have a version for this symbol, see if we can find | |
1962 | something. */ | |
1963 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
1964 | { | |
1965 | struct bfd_elf_version_tree *t; | |
1966 | struct bfd_elf_version_tree *local_ver; | |
1967 | struct bfd_elf_version_expr *d; | |
1968 | ||
1969 | /* See if can find what version this symbol is in. If the | |
1970 | symbol is supposed to be local, then don't actually register | |
1971 | it. */ | |
1972 | local_ver = NULL; | |
1973 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1974 | { | |
108ba305 | 1975 | if (t->globals.list != NULL) |
45d6a902 AM |
1976 | { |
1977 | bfd_boolean matched; | |
1978 | ||
1979 | matched = FALSE; | |
108ba305 JJ |
1980 | d = NULL; |
1981 | while ((d = (*t->match) (&t->globals, d, | |
1982 | h->root.root.string)) != NULL) | |
1983 | if (d->symver) | |
1984 | matched = TRUE; | |
1985 | else | |
1986 | { | |
1987 | /* There is a version without definition. Make | |
1988 | the symbol the default definition for this | |
1989 | version. */ | |
1990 | h->verinfo.vertree = t; | |
1991 | local_ver = NULL; | |
1992 | d->script = 1; | |
1993 | break; | |
1994 | } | |
45d6a902 AM |
1995 | if (d != NULL) |
1996 | break; | |
1997 | else if (matched) | |
1998 | /* There is no undefined version for this symbol. Hide the | |
1999 | default one. */ | |
2000 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2001 | } | |
2002 | ||
108ba305 | 2003 | if (t->locals.list != NULL) |
45d6a902 | 2004 | { |
108ba305 JJ |
2005 | d = NULL; |
2006 | while ((d = (*t->match) (&t->locals, d, | |
2007 | h->root.root.string)) != NULL) | |
45d6a902 | 2008 | { |
108ba305 | 2009 | local_ver = t; |
45d6a902 | 2010 | /* If the match is "*", keep looking for a more |
108ba305 JJ |
2011 | explicit, perhaps even global, match. |
2012 | XXX: Shouldn't this be !d->wildcard instead? */ | |
2013 | if (d->pattern[0] != '*' || d->pattern[1] != '\0') | |
2014 | break; | |
45d6a902 AM |
2015 | } |
2016 | ||
2017 | if (d != NULL) | |
2018 | break; | |
2019 | } | |
2020 | } | |
2021 | ||
2022 | if (local_ver != NULL) | |
2023 | { | |
2024 | h->verinfo.vertree = local_ver; | |
2025 | if (h->dynindx != -1 | |
45d6a902 AM |
2026 | && ! info->export_dynamic) |
2027 | { | |
2028 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2029 | } | |
2030 | } | |
2031 | } | |
2032 | ||
2033 | return TRUE; | |
2034 | } | |
2035 | \f | |
45d6a902 AM |
2036 | /* Read and swap the relocs from the section indicated by SHDR. This |
2037 | may be either a REL or a RELA section. The relocations are | |
2038 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2039 | which should have already been allocated to contain enough space. | |
2040 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2041 | relocations should be stored. | |
2042 | ||
2043 | Returns FALSE if something goes wrong. */ | |
2044 | ||
2045 | static bfd_boolean | |
268b6b39 | 2046 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2047 | asection *sec, |
268b6b39 AM |
2048 | Elf_Internal_Shdr *shdr, |
2049 | void *external_relocs, | |
2050 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2051 | { |
9c5bfbb7 | 2052 | const struct elf_backend_data *bed; |
268b6b39 | 2053 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2054 | const bfd_byte *erela; |
2055 | const bfd_byte *erelaend; | |
2056 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2057 | Elf_Internal_Shdr *symtab_hdr; |
2058 | size_t nsyms; | |
45d6a902 | 2059 | |
45d6a902 AM |
2060 | /* Position ourselves at the start of the section. */ |
2061 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2062 | return FALSE; | |
2063 | ||
2064 | /* Read the relocations. */ | |
2065 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2066 | return FALSE; | |
2067 | ||
243ef1e0 L |
2068 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2069 | nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
2070 | ||
45d6a902 AM |
2071 | bed = get_elf_backend_data (abfd); |
2072 | ||
2073 | /* Convert the external relocations to the internal format. */ | |
2074 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2075 | swap_in = bed->s->swap_reloc_in; | |
2076 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2077 | swap_in = bed->s->swap_reloca_in; | |
2078 | else | |
2079 | { | |
2080 | bfd_set_error (bfd_error_wrong_format); | |
2081 | return FALSE; | |
2082 | } | |
2083 | ||
2084 | erela = external_relocs; | |
51992aec | 2085 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2086 | irela = internal_relocs; |
2087 | while (erela < erelaend) | |
2088 | { | |
243ef1e0 L |
2089 | bfd_vma r_symndx; |
2090 | ||
45d6a902 | 2091 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2092 | r_symndx = ELF32_R_SYM (irela->r_info); |
2093 | if (bed->s->arch_size == 64) | |
2094 | r_symndx >>= 24; | |
2095 | if ((size_t) r_symndx >= nsyms) | |
2096 | { | |
2097 | (*_bfd_error_handler) | |
d003868e AM |
2098 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" |
2099 | " for offset 0x%lx in section `%A'"), | |
2100 | abfd, sec, | |
2101 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
2102 | bfd_set_error (bfd_error_bad_value); |
2103 | return FALSE; | |
2104 | } | |
45d6a902 AM |
2105 | irela += bed->s->int_rels_per_ext_rel; |
2106 | erela += shdr->sh_entsize; | |
2107 | } | |
2108 | ||
2109 | return TRUE; | |
2110 | } | |
2111 | ||
2112 | /* Read and swap the relocs for a section O. They may have been | |
2113 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2114 | not NULL, they are used as buffers to read into. They are known to | |
2115 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2116 | the return value is allocated using either malloc or bfd_alloc, | |
2117 | according to the KEEP_MEMORY argument. If O has two relocation | |
2118 | sections (both REL and RELA relocations), then the REL_HDR | |
2119 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
2120 | REL_HDR2 relocations. */ | |
2121 | ||
2122 | Elf_Internal_Rela * | |
268b6b39 AM |
2123 | _bfd_elf_link_read_relocs (bfd *abfd, |
2124 | asection *o, | |
2125 | void *external_relocs, | |
2126 | Elf_Internal_Rela *internal_relocs, | |
2127 | bfd_boolean keep_memory) | |
45d6a902 AM |
2128 | { |
2129 | Elf_Internal_Shdr *rel_hdr; | |
268b6b39 | 2130 | void *alloc1 = NULL; |
45d6a902 | 2131 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2132 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
2133 | |
2134 | if (elf_section_data (o)->relocs != NULL) | |
2135 | return elf_section_data (o)->relocs; | |
2136 | ||
2137 | if (o->reloc_count == 0) | |
2138 | return NULL; | |
2139 | ||
2140 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
2141 | ||
2142 | if (internal_relocs == NULL) | |
2143 | { | |
2144 | bfd_size_type size; | |
2145 | ||
2146 | size = o->reloc_count; | |
2147 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
2148 | if (keep_memory) | |
268b6b39 | 2149 | internal_relocs = bfd_alloc (abfd, size); |
45d6a902 | 2150 | else |
268b6b39 | 2151 | internal_relocs = alloc2 = bfd_malloc (size); |
45d6a902 AM |
2152 | if (internal_relocs == NULL) |
2153 | goto error_return; | |
2154 | } | |
2155 | ||
2156 | if (external_relocs == NULL) | |
2157 | { | |
2158 | bfd_size_type size = rel_hdr->sh_size; | |
2159 | ||
2160 | if (elf_section_data (o)->rel_hdr2) | |
2161 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
268b6b39 | 2162 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2163 | if (alloc1 == NULL) |
2164 | goto error_return; | |
2165 | external_relocs = alloc1; | |
2166 | } | |
2167 | ||
243ef1e0 | 2168 | if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, |
45d6a902 AM |
2169 | external_relocs, |
2170 | internal_relocs)) | |
2171 | goto error_return; | |
51992aec AM |
2172 | if (elf_section_data (o)->rel_hdr2 |
2173 | && (!elf_link_read_relocs_from_section | |
2174 | (abfd, o, | |
2175 | elf_section_data (o)->rel_hdr2, | |
2176 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
2177 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
2178 | * bed->s->int_rels_per_ext_rel)))) | |
45d6a902 AM |
2179 | goto error_return; |
2180 | ||
2181 | /* Cache the results for next time, if we can. */ | |
2182 | if (keep_memory) | |
2183 | elf_section_data (o)->relocs = internal_relocs; | |
2184 | ||
2185 | if (alloc1 != NULL) | |
2186 | free (alloc1); | |
2187 | ||
2188 | /* Don't free alloc2, since if it was allocated we are passing it | |
2189 | back (under the name of internal_relocs). */ | |
2190 | ||
2191 | return internal_relocs; | |
2192 | ||
2193 | error_return: | |
2194 | if (alloc1 != NULL) | |
2195 | free (alloc1); | |
2196 | if (alloc2 != NULL) | |
2197 | free (alloc2); | |
2198 | return NULL; | |
2199 | } | |
2200 | ||
2201 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2202 | section header for a section containing relocations for O. */ | |
2203 | ||
2204 | bfd_boolean | |
268b6b39 AM |
2205 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2206 | Elf_Internal_Shdr *rel_hdr, | |
2207 | asection *o) | |
45d6a902 AM |
2208 | { |
2209 | bfd_size_type reloc_count; | |
2210 | bfd_size_type num_rel_hashes; | |
2211 | ||
2212 | /* Figure out how many relocations there will be. */ | |
2213 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
2214 | reloc_count = elf_section_data (o)->rel_count; | |
2215 | else | |
2216 | reloc_count = elf_section_data (o)->rel_count2; | |
2217 | ||
2218 | num_rel_hashes = o->reloc_count; | |
2219 | if (num_rel_hashes < reloc_count) | |
2220 | num_rel_hashes = reloc_count; | |
2221 | ||
2222 | /* That allows us to calculate the size of the section. */ | |
2223 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
2224 | ||
2225 | /* The contents field must last into write_object_contents, so we | |
2226 | allocate it with bfd_alloc rather than malloc. Also since we | |
2227 | cannot be sure that the contents will actually be filled in, | |
2228 | we zero the allocated space. */ | |
268b6b39 | 2229 | rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2230 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2231 | return FALSE; | |
2232 | ||
2233 | /* We only allocate one set of hash entries, so we only do it the | |
2234 | first time we are called. */ | |
2235 | if (elf_section_data (o)->rel_hashes == NULL | |
2236 | && num_rel_hashes) | |
2237 | { | |
2238 | struct elf_link_hash_entry **p; | |
2239 | ||
268b6b39 | 2240 | p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
2241 | if (p == NULL) |
2242 | return FALSE; | |
2243 | ||
2244 | elf_section_data (o)->rel_hashes = p; | |
2245 | } | |
2246 | ||
2247 | return TRUE; | |
2248 | } | |
2249 | ||
2250 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2251 | originated from the section given by INPUT_REL_HDR) to the | |
2252 | OUTPUT_BFD. */ | |
2253 | ||
2254 | bfd_boolean | |
268b6b39 AM |
2255 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2256 | asection *input_section, | |
2257 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2258 | Elf_Internal_Rela *internal_relocs, |
2259 | struct elf_link_hash_entry **rel_hash | |
2260 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2261 | { |
2262 | Elf_Internal_Rela *irela; | |
2263 | Elf_Internal_Rela *irelaend; | |
2264 | bfd_byte *erel; | |
2265 | Elf_Internal_Shdr *output_rel_hdr; | |
2266 | asection *output_section; | |
2267 | unsigned int *rel_countp = NULL; | |
9c5bfbb7 | 2268 | const struct elf_backend_data *bed; |
268b6b39 | 2269 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
45d6a902 AM |
2270 | |
2271 | output_section = input_section->output_section; | |
2272 | output_rel_hdr = NULL; | |
2273 | ||
2274 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
2275 | == input_rel_hdr->sh_entsize) | |
2276 | { | |
2277 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
2278 | rel_countp = &elf_section_data (output_section)->rel_count; | |
2279 | } | |
2280 | else if (elf_section_data (output_section)->rel_hdr2 | |
2281 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
2282 | == input_rel_hdr->sh_entsize)) | |
2283 | { | |
2284 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2285 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2286 | } | |
2287 | else | |
2288 | { | |
2289 | (*_bfd_error_handler) | |
d003868e AM |
2290 | (_("%B: relocation size mismatch in %B section %A"), |
2291 | output_bfd, input_section->owner, input_section); | |
45d6a902 AM |
2292 | bfd_set_error (bfd_error_wrong_object_format); |
2293 | return FALSE; | |
2294 | } | |
2295 | ||
2296 | bed = get_elf_backend_data (output_bfd); | |
2297 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2298 | swap_out = bed->s->swap_reloc_out; | |
2299 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2300 | swap_out = bed->s->swap_reloca_out; | |
2301 | else | |
2302 | abort (); | |
2303 | ||
2304 | erel = output_rel_hdr->contents; | |
2305 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2306 | irela = internal_relocs; | |
2307 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2308 | * bed->s->int_rels_per_ext_rel); | |
2309 | while (irela < irelaend) | |
2310 | { | |
2311 | (*swap_out) (output_bfd, irela, erel); | |
2312 | irela += bed->s->int_rels_per_ext_rel; | |
2313 | erel += input_rel_hdr->sh_entsize; | |
2314 | } | |
2315 | ||
2316 | /* Bump the counter, so that we know where to add the next set of | |
2317 | relocations. */ | |
2318 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2319 | ||
2320 | return TRUE; | |
2321 | } | |
2322 | \f | |
508c3946 L |
2323 | /* Make weak undefined symbols in PIE dynamic. */ |
2324 | ||
2325 | bfd_boolean | |
2326 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2327 | struct elf_link_hash_entry *h) | |
2328 | { | |
2329 | if (info->pie | |
2330 | && h->dynindx == -1 | |
2331 | && h->root.type == bfd_link_hash_undefweak) | |
2332 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2333 | ||
2334 | return TRUE; | |
2335 | } | |
2336 | ||
45d6a902 AM |
2337 | /* Fix up the flags for a symbol. This handles various cases which |
2338 | can only be fixed after all the input files are seen. This is | |
2339 | currently called by both adjust_dynamic_symbol and | |
2340 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2341 | the face of future changes. */ | |
2342 | ||
2343 | bfd_boolean | |
268b6b39 AM |
2344 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2345 | struct elf_info_failed *eif) | |
45d6a902 | 2346 | { |
508c3946 L |
2347 | const struct elf_backend_data *bed = NULL; |
2348 | ||
45d6a902 AM |
2349 | /* If this symbol was mentioned in a non-ELF file, try to set |
2350 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2351 | permit a non-ELF file to correctly refer to a symbol defined in | |
2352 | an ELF dynamic object. */ | |
f5385ebf | 2353 | if (h->non_elf) |
45d6a902 AM |
2354 | { |
2355 | while (h->root.type == bfd_link_hash_indirect) | |
2356 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2357 | ||
2358 | if (h->root.type != bfd_link_hash_defined | |
2359 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2360 | { |
2361 | h->ref_regular = 1; | |
2362 | h->ref_regular_nonweak = 1; | |
2363 | } | |
45d6a902 AM |
2364 | else |
2365 | { | |
2366 | if (h->root.u.def.section->owner != NULL | |
2367 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2368 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2369 | { |
2370 | h->ref_regular = 1; | |
2371 | h->ref_regular_nonweak = 1; | |
2372 | } | |
45d6a902 | 2373 | else |
f5385ebf | 2374 | h->def_regular = 1; |
45d6a902 AM |
2375 | } |
2376 | ||
2377 | if (h->dynindx == -1 | |
f5385ebf AM |
2378 | && (h->def_dynamic |
2379 | || h->ref_dynamic)) | |
45d6a902 | 2380 | { |
c152c796 | 2381 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2382 | { |
2383 | eif->failed = TRUE; | |
2384 | return FALSE; | |
2385 | } | |
2386 | } | |
2387 | } | |
2388 | else | |
2389 | { | |
f5385ebf | 2390 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2391 | was first seen in a non-ELF file. Fortunately, if the symbol |
2392 | was first seen in an ELF file, we're probably OK unless the | |
2393 | symbol was defined in a non-ELF file. Catch that case here. | |
2394 | FIXME: We're still in trouble if the symbol was first seen in | |
2395 | a dynamic object, and then later in a non-ELF regular object. */ | |
2396 | if ((h->root.type == bfd_link_hash_defined | |
2397 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2398 | && !h->def_regular |
45d6a902 AM |
2399 | && (h->root.u.def.section->owner != NULL |
2400 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2401 | != bfd_target_elf_flavour) | |
2402 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2403 | && !h->def_dynamic))) |
2404 | h->def_regular = 1; | |
45d6a902 AM |
2405 | } |
2406 | ||
508c3946 L |
2407 | /* Backend specific symbol fixup. */ |
2408 | if (elf_hash_table (eif->info)->dynobj) | |
2409 | { | |
2410 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2411 | if (bed->elf_backend_fixup_symbol | |
2412 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2413 | return FALSE; | |
2414 | } | |
2415 | ||
45d6a902 AM |
2416 | /* If this is a final link, and the symbol was defined as a common |
2417 | symbol in a regular object file, and there was no definition in | |
2418 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2419 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2420 | flag will not have been set. */ |
2421 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2422 | && !h->def_regular |
2423 | && h->ref_regular | |
2424 | && !h->def_dynamic | |
45d6a902 | 2425 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) |
f5385ebf | 2426 | h->def_regular = 1; |
45d6a902 AM |
2427 | |
2428 | /* If -Bsymbolic was used (which means to bind references to global | |
2429 | symbols to the definition within the shared object), and this | |
2430 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2431 | need a PLT entry. Likewise, if the symbol has non-default |
2432 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2433 | will force it local. */ |
f5385ebf | 2434 | if (h->needs_plt |
45d6a902 | 2435 | && eif->info->shared |
0eddce27 | 2436 | && is_elf_hash_table (eif->info->hash) |
55255dae | 2437 | && (SYMBOLIC_BIND (eif->info, h) |
c1be741f | 2438 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2439 | && h->def_regular) |
45d6a902 | 2440 | { |
45d6a902 AM |
2441 | bfd_boolean force_local; |
2442 | ||
45d6a902 AM |
2443 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2444 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2445 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2446 | } | |
2447 | ||
2448 | /* If a weak undefined symbol has non-default visibility, we also | |
2449 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2450 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
2451 | && h->root.type == bfd_link_hash_undefweak) |
2452 | { | |
9c5bfbb7 | 2453 | const struct elf_backend_data *bed; |
45d6a902 AM |
2454 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2455 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2456 | } | |
2457 | ||
2458 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2459 | the real definition in the dynamic object, copy interesting flags | |
2460 | over to the real definition. */ | |
f6e332e6 | 2461 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2462 | { |
2463 | struct elf_link_hash_entry *weakdef; | |
2464 | ||
f6e332e6 | 2465 | weakdef = h->u.weakdef; |
45d6a902 AM |
2466 | if (h->root.type == bfd_link_hash_indirect) |
2467 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2468 | ||
2469 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2470 | || h->root.type == bfd_link_hash_defweak); | |
2471 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2472 | || weakdef->root.type == bfd_link_hash_defweak); | |
f5385ebf | 2473 | BFD_ASSERT (weakdef->def_dynamic); |
45d6a902 AM |
2474 | |
2475 | /* If the real definition is defined by a regular object file, | |
2476 | don't do anything special. See the longer description in | |
2477 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
f5385ebf | 2478 | if (weakdef->def_regular) |
f6e332e6 | 2479 | h->u.weakdef = NULL; |
45d6a902 | 2480 | else |
508c3946 L |
2481 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, |
2482 | h); | |
45d6a902 AM |
2483 | } |
2484 | ||
2485 | return TRUE; | |
2486 | } | |
2487 | ||
2488 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2489 | called via elf_link_hash_traverse, and also calls itself | |
2490 | recursively. */ | |
2491 | ||
2492 | bfd_boolean | |
268b6b39 | 2493 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2494 | { |
268b6b39 | 2495 | struct elf_info_failed *eif = data; |
45d6a902 | 2496 | bfd *dynobj; |
9c5bfbb7 | 2497 | const struct elf_backend_data *bed; |
45d6a902 | 2498 | |
0eddce27 | 2499 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2500 | return FALSE; |
2501 | ||
2502 | if (h->root.type == bfd_link_hash_warning) | |
2503 | { | |
a6aa5195 AM |
2504 | h->got = elf_hash_table (eif->info)->init_got_offset; |
2505 | h->plt = elf_hash_table (eif->info)->init_plt_offset; | |
45d6a902 AM |
2506 | |
2507 | /* When warning symbols are created, they **replace** the "real" | |
2508 | entry in the hash table, thus we never get to see the real | |
2509 | symbol in a hash traversal. So look at it now. */ | |
2510 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2511 | } | |
2512 | ||
2513 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2514 | if (h->root.type == bfd_link_hash_indirect) | |
2515 | return TRUE; | |
2516 | ||
2517 | /* Fix the symbol flags. */ | |
2518 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2519 | return FALSE; | |
2520 | ||
2521 | /* If this symbol does not require a PLT entry, and it is not | |
2522 | defined by a dynamic object, or is not referenced by a regular | |
2523 | object, ignore it. We do have to handle a weak defined symbol, | |
2524 | even if no regular object refers to it, if we decided to add it | |
2525 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2526 | about symbols which are defined by one dynamic object and | |
2527 | referenced by another one? */ | |
f5385ebf AM |
2528 | if (!h->needs_plt |
2529 | && (h->def_regular | |
2530 | || !h->def_dynamic | |
2531 | || (!h->ref_regular | |
f6e332e6 | 2532 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 | 2533 | { |
a6aa5195 | 2534 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2535 | return TRUE; |
2536 | } | |
2537 | ||
2538 | /* If we've already adjusted this symbol, don't do it again. This | |
2539 | can happen via a recursive call. */ | |
f5385ebf | 2540 | if (h->dynamic_adjusted) |
45d6a902 AM |
2541 | return TRUE; |
2542 | ||
2543 | /* Don't look at this symbol again. Note that we must set this | |
2544 | after checking the above conditions, because we may look at a | |
2545 | symbol once, decide not to do anything, and then get called | |
2546 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2547 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2548 | |
2549 | /* If this is a weak definition, and we know a real definition, and | |
2550 | the real symbol is not itself defined by a regular object file, | |
2551 | then get a good value for the real definition. We handle the | |
2552 | real symbol first, for the convenience of the backend routine. | |
2553 | ||
2554 | Note that there is a confusing case here. If the real definition | |
2555 | is defined by a regular object file, we don't get the real symbol | |
2556 | from the dynamic object, but we do get the weak symbol. If the | |
2557 | processor backend uses a COPY reloc, then if some routine in the | |
2558 | dynamic object changes the real symbol, we will not see that | |
2559 | change in the corresponding weak symbol. This is the way other | |
2560 | ELF linkers work as well, and seems to be a result of the shared | |
2561 | library model. | |
2562 | ||
2563 | I will clarify this issue. Most SVR4 shared libraries define the | |
2564 | variable _timezone and define timezone as a weak synonym. The | |
2565 | tzset call changes _timezone. If you write | |
2566 | extern int timezone; | |
2567 | int _timezone = 5; | |
2568 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2569 | you might expect that, since timezone is a synonym for _timezone, | |
2570 | the same number will print both times. However, if the processor | |
2571 | backend uses a COPY reloc, then actually timezone will be copied | |
2572 | into your process image, and, since you define _timezone | |
2573 | yourself, _timezone will not. Thus timezone and _timezone will | |
2574 | wind up at different memory locations. The tzset call will set | |
2575 | _timezone, leaving timezone unchanged. */ | |
2576 | ||
f6e332e6 | 2577 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2578 | { |
2579 | /* If we get to this point, we know there is an implicit | |
2580 | reference by a regular object file via the weak symbol H. | |
2581 | FIXME: Is this really true? What if the traversal finds | |
f6e332e6 AM |
2582 | H->U.WEAKDEF before it finds H? */ |
2583 | h->u.weakdef->ref_regular = 1; | |
45d6a902 | 2584 | |
f6e332e6 | 2585 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2586 | return FALSE; |
2587 | } | |
2588 | ||
2589 | /* If a symbol has no type and no size and does not require a PLT | |
2590 | entry, then we are probably about to do the wrong thing here: we | |
2591 | are probably going to create a COPY reloc for an empty object. | |
2592 | This case can arise when a shared object is built with assembly | |
2593 | code, and the assembly code fails to set the symbol type. */ | |
2594 | if (h->size == 0 | |
2595 | && h->type == STT_NOTYPE | |
f5385ebf | 2596 | && !h->needs_plt) |
45d6a902 AM |
2597 | (*_bfd_error_handler) |
2598 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2599 | h->root.root.string); | |
2600 | ||
2601 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2602 | bed = get_elf_backend_data (dynobj); | |
2603 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2604 | { | |
2605 | eif->failed = TRUE; | |
2606 | return FALSE; | |
2607 | } | |
2608 | ||
2609 | return TRUE; | |
2610 | } | |
2611 | ||
2612 | /* Adjust all external symbols pointing into SEC_MERGE sections | |
2613 | to reflect the object merging within the sections. */ | |
2614 | ||
2615 | bfd_boolean | |
268b6b39 | 2616 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2617 | { |
2618 | asection *sec; | |
2619 | ||
2620 | if (h->root.type == bfd_link_hash_warning) | |
2621 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2622 | ||
2623 | if ((h->root.type == bfd_link_hash_defined | |
2624 | || h->root.type == bfd_link_hash_defweak) | |
2625 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2626 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2627 | { | |
268b6b39 | 2628 | bfd *output_bfd = data; |
45d6a902 AM |
2629 | |
2630 | h->root.u.def.value = | |
2631 | _bfd_merged_section_offset (output_bfd, | |
2632 | &h->root.u.def.section, | |
2633 | elf_section_data (sec)->sec_info, | |
753731ee | 2634 | h->root.u.def.value); |
45d6a902 AM |
2635 | } |
2636 | ||
2637 | return TRUE; | |
2638 | } | |
986a241f RH |
2639 | |
2640 | /* Returns false if the symbol referred to by H should be considered | |
2641 | to resolve local to the current module, and true if it should be | |
2642 | considered to bind dynamically. */ | |
2643 | ||
2644 | bfd_boolean | |
268b6b39 AM |
2645 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2646 | struct bfd_link_info *info, | |
2647 | bfd_boolean ignore_protected) | |
986a241f RH |
2648 | { |
2649 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
2650 | const struct elf_backend_data *bed; |
2651 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
2652 | |
2653 | if (h == NULL) | |
2654 | return FALSE; | |
2655 | ||
2656 | while (h->root.type == bfd_link_hash_indirect | |
2657 | || h->root.type == bfd_link_hash_warning) | |
2658 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2659 | ||
2660 | /* If it was forced local, then clearly it's not dynamic. */ | |
2661 | if (h->dynindx == -1) | |
2662 | return FALSE; | |
f5385ebf | 2663 | if (h->forced_local) |
986a241f RH |
2664 | return FALSE; |
2665 | ||
2666 | /* Identify the cases where name binding rules say that a | |
2667 | visible symbol resolves locally. */ | |
55255dae | 2668 | binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h); |
986a241f RH |
2669 | |
2670 | switch (ELF_ST_VISIBILITY (h->other)) | |
2671 | { | |
2672 | case STV_INTERNAL: | |
2673 | case STV_HIDDEN: | |
2674 | return FALSE; | |
2675 | ||
2676 | case STV_PROTECTED: | |
fcb93ecf PB |
2677 | hash_table = elf_hash_table (info); |
2678 | if (!is_elf_hash_table (hash_table)) | |
2679 | return FALSE; | |
2680 | ||
2681 | bed = get_elf_backend_data (hash_table->dynobj); | |
2682 | ||
986a241f RH |
2683 | /* Proper resolution for function pointer equality may require |
2684 | that these symbols perhaps be resolved dynamically, even though | |
2685 | we should be resolving them to the current module. */ | |
fcb93ecf | 2686 | if (!ignore_protected || !bed->is_function_type (h->type)) |
986a241f RH |
2687 | binding_stays_local_p = TRUE; |
2688 | break; | |
2689 | ||
2690 | default: | |
986a241f RH |
2691 | break; |
2692 | } | |
2693 | ||
aa37626c | 2694 | /* If it isn't defined locally, then clearly it's dynamic. */ |
f5385ebf | 2695 | if (!h->def_regular) |
aa37626c L |
2696 | return TRUE; |
2697 | ||
986a241f RH |
2698 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2699 | us that it remains local. */ | |
2700 | return !binding_stays_local_p; | |
2701 | } | |
f6c52c13 AM |
2702 | |
2703 | /* Return true if the symbol referred to by H should be considered | |
2704 | to resolve local to the current module, and false otherwise. Differs | |
2705 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2706 | undefined symbols and weak symbols. */ | |
2707 | ||
2708 | bfd_boolean | |
268b6b39 AM |
2709 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2710 | struct bfd_link_info *info, | |
2711 | bfd_boolean local_protected) | |
f6c52c13 | 2712 | { |
fcb93ecf PB |
2713 | const struct elf_backend_data *bed; |
2714 | struct elf_link_hash_table *hash_table; | |
2715 | ||
f6c52c13 AM |
2716 | /* If it's a local sym, of course we resolve locally. */ |
2717 | if (h == NULL) | |
2718 | return TRUE; | |
2719 | ||
7e2294f9 AO |
2720 | /* Common symbols that become definitions don't get the DEF_REGULAR |
2721 | flag set, so test it first, and don't bail out. */ | |
2722 | if (ELF_COMMON_DEF_P (h)) | |
2723 | /* Do nothing. */; | |
f6c52c13 | 2724 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
2725 | resolve locally. The sym is either undefined or dynamic. */ |
2726 | else if (!h->def_regular) | |
f6c52c13 AM |
2727 | return FALSE; |
2728 | ||
2729 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 2730 | if (h->forced_local) |
f6c52c13 AM |
2731 | return TRUE; |
2732 | ||
2733 | /* As do non-dynamic symbols. */ | |
2734 | if (h->dynindx == -1) | |
2735 | return TRUE; | |
2736 | ||
2737 | /* At this point, we know the symbol is defined and dynamic. In an | |
2738 | executable it must resolve locally, likewise when building symbolic | |
2739 | shared libraries. */ | |
55255dae | 2740 | if (info->executable || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
2741 | return TRUE; |
2742 | ||
2743 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2744 | with default visibility might not resolve locally. */ | |
2745 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2746 | return FALSE; | |
2747 | ||
2748 | /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */ | |
2749 | if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED) | |
2750 | return TRUE; | |
2751 | ||
fcb93ecf PB |
2752 | hash_table = elf_hash_table (info); |
2753 | if (!is_elf_hash_table (hash_table)) | |
2754 | return TRUE; | |
2755 | ||
2756 | bed = get_elf_backend_data (hash_table->dynobj); | |
2757 | ||
1c16dfa5 | 2758 | /* STV_PROTECTED non-function symbols are local. */ |
fcb93ecf | 2759 | if (!bed->is_function_type (h->type)) |
1c16dfa5 L |
2760 | return TRUE; |
2761 | ||
f6c52c13 AM |
2762 | /* Function pointer equality tests may require that STV_PROTECTED |
2763 | symbols be treated as dynamic symbols, even when we know that the | |
2764 | dynamic linker will resolve them locally. */ | |
2765 | return local_protected; | |
2766 | } | |
e1918d23 AM |
2767 | |
2768 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2769 | aligned. Returns the first TLS output section. */ | |
2770 | ||
2771 | struct bfd_section * | |
2772 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2773 | { | |
2774 | struct bfd_section *sec, *tls; | |
2775 | unsigned int align = 0; | |
2776 | ||
2777 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2778 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2779 | break; | |
2780 | tls = sec; | |
2781 | ||
2782 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2783 | if (sec->alignment_power > align) | |
2784 | align = sec->alignment_power; | |
2785 | ||
2786 | elf_hash_table (info)->tls_sec = tls; | |
2787 | ||
2788 | /* Ensure the alignment of the first section is the largest alignment, | |
2789 | so that the tls segment starts aligned. */ | |
2790 | if (tls != NULL) | |
2791 | tls->alignment_power = align; | |
2792 | ||
2793 | return tls; | |
2794 | } | |
0ad989f9 L |
2795 | |
2796 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2797 | static bfd_boolean | |
2798 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2799 | Elf_Internal_Sym *sym) | |
2800 | { | |
a4d8e49b L |
2801 | const struct elf_backend_data *bed; |
2802 | ||
0ad989f9 L |
2803 | /* Local symbols do not count, but target specific ones might. */ |
2804 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2805 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2806 | return FALSE; | |
2807 | ||
fcb93ecf | 2808 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 2809 | /* Function symbols do not count. */ |
fcb93ecf | 2810 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
2811 | return FALSE; |
2812 | ||
2813 | /* If the section is undefined, then so is the symbol. */ | |
2814 | if (sym->st_shndx == SHN_UNDEF) | |
2815 | return FALSE; | |
2816 | ||
2817 | /* If the symbol is defined in the common section, then | |
2818 | it is a common definition and so does not count. */ | |
a4d8e49b | 2819 | if (bed->common_definition (sym)) |
0ad989f9 L |
2820 | return FALSE; |
2821 | ||
2822 | /* If the symbol is in a target specific section then we | |
2823 | must rely upon the backend to tell us what it is. */ | |
2824 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2825 | /* FIXME - this function is not coded yet: | |
2826 | ||
2827 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2828 | ||
2829 | Instead for now assume that the definition is not global, | |
2830 | Even if this is wrong, at least the linker will behave | |
2831 | in the same way that it used to do. */ | |
2832 | return FALSE; | |
2833 | ||
2834 | return TRUE; | |
2835 | } | |
2836 | ||
2837 | /* Search the symbol table of the archive element of the archive ABFD | |
2838 | whose archive map contains a mention of SYMDEF, and determine if | |
2839 | the symbol is defined in this element. */ | |
2840 | static bfd_boolean | |
2841 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2842 | { | |
2843 | Elf_Internal_Shdr * hdr; | |
2844 | bfd_size_type symcount; | |
2845 | bfd_size_type extsymcount; | |
2846 | bfd_size_type extsymoff; | |
2847 | Elf_Internal_Sym *isymbuf; | |
2848 | Elf_Internal_Sym *isym; | |
2849 | Elf_Internal_Sym *isymend; | |
2850 | bfd_boolean result; | |
2851 | ||
2852 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2853 | if (abfd == NULL) | |
2854 | return FALSE; | |
2855 | ||
2856 | if (! bfd_check_format (abfd, bfd_object)) | |
2857 | return FALSE; | |
2858 | ||
2859 | /* If we have already included the element containing this symbol in the | |
2860 | link then we do not need to include it again. Just claim that any symbol | |
2861 | it contains is not a definition, so that our caller will not decide to | |
2862 | (re)include this element. */ | |
2863 | if (abfd->archive_pass) | |
2864 | return FALSE; | |
2865 | ||
2866 | /* Select the appropriate symbol table. */ | |
2867 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2868 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2869 | else | |
2870 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2871 | ||
2872 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2873 | ||
2874 | /* The sh_info field of the symtab header tells us where the | |
2875 | external symbols start. We don't care about the local symbols. */ | |
2876 | if (elf_bad_symtab (abfd)) | |
2877 | { | |
2878 | extsymcount = symcount; | |
2879 | extsymoff = 0; | |
2880 | } | |
2881 | else | |
2882 | { | |
2883 | extsymcount = symcount - hdr->sh_info; | |
2884 | extsymoff = hdr->sh_info; | |
2885 | } | |
2886 | ||
2887 | if (extsymcount == 0) | |
2888 | return FALSE; | |
2889 | ||
2890 | /* Read in the symbol table. */ | |
2891 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2892 | NULL, NULL, NULL); | |
2893 | if (isymbuf == NULL) | |
2894 | return FALSE; | |
2895 | ||
2896 | /* Scan the symbol table looking for SYMDEF. */ | |
2897 | result = FALSE; | |
2898 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2899 | { | |
2900 | const char *name; | |
2901 | ||
2902 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
2903 | isym->st_name); | |
2904 | if (name == NULL) | |
2905 | break; | |
2906 | ||
2907 | if (strcmp (name, symdef->name) == 0) | |
2908 | { | |
2909 | result = is_global_data_symbol_definition (abfd, isym); | |
2910 | break; | |
2911 | } | |
2912 | } | |
2913 | ||
2914 | free (isymbuf); | |
2915 | ||
2916 | return result; | |
2917 | } | |
2918 | \f | |
5a580b3a AM |
2919 | /* Add an entry to the .dynamic table. */ |
2920 | ||
2921 | bfd_boolean | |
2922 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
2923 | bfd_vma tag, | |
2924 | bfd_vma val) | |
2925 | { | |
2926 | struct elf_link_hash_table *hash_table; | |
2927 | const struct elf_backend_data *bed; | |
2928 | asection *s; | |
2929 | bfd_size_type newsize; | |
2930 | bfd_byte *newcontents; | |
2931 | Elf_Internal_Dyn dyn; | |
2932 | ||
2933 | hash_table = elf_hash_table (info); | |
2934 | if (! is_elf_hash_table (hash_table)) | |
2935 | return FALSE; | |
2936 | ||
2937 | bed = get_elf_backend_data (hash_table->dynobj); | |
2938 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2939 | BFD_ASSERT (s != NULL); | |
2940 | ||
eea6121a | 2941 | newsize = s->size + bed->s->sizeof_dyn; |
5a580b3a AM |
2942 | newcontents = bfd_realloc (s->contents, newsize); |
2943 | if (newcontents == NULL) | |
2944 | return FALSE; | |
2945 | ||
2946 | dyn.d_tag = tag; | |
2947 | dyn.d_un.d_val = val; | |
eea6121a | 2948 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 2949 | |
eea6121a | 2950 | s->size = newsize; |
5a580b3a AM |
2951 | s->contents = newcontents; |
2952 | ||
2953 | return TRUE; | |
2954 | } | |
2955 | ||
2956 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
2957 | otherwise just check whether one already exists. Returns -1 on error, | |
2958 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
2959 | ||
4ad4eba5 | 2960 | static int |
7e9f0867 AM |
2961 | elf_add_dt_needed_tag (bfd *abfd, |
2962 | struct bfd_link_info *info, | |
4ad4eba5 AM |
2963 | const char *soname, |
2964 | bfd_boolean do_it) | |
5a580b3a AM |
2965 | { |
2966 | struct elf_link_hash_table *hash_table; | |
2967 | bfd_size_type oldsize; | |
2968 | bfd_size_type strindex; | |
2969 | ||
7e9f0867 AM |
2970 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
2971 | return -1; | |
2972 | ||
5a580b3a AM |
2973 | hash_table = elf_hash_table (info); |
2974 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
2975 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
2976 | if (strindex == (bfd_size_type) -1) | |
2977 | return -1; | |
2978 | ||
2979 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
2980 | { | |
2981 | asection *sdyn; | |
2982 | const struct elf_backend_data *bed; | |
2983 | bfd_byte *extdyn; | |
2984 | ||
2985 | bed = get_elf_backend_data (hash_table->dynobj); | |
2986 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
7e9f0867 AM |
2987 | if (sdyn != NULL) |
2988 | for (extdyn = sdyn->contents; | |
2989 | extdyn < sdyn->contents + sdyn->size; | |
2990 | extdyn += bed->s->sizeof_dyn) | |
2991 | { | |
2992 | Elf_Internal_Dyn dyn; | |
5a580b3a | 2993 | |
7e9f0867 AM |
2994 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
2995 | if (dyn.d_tag == DT_NEEDED | |
2996 | && dyn.d_un.d_val == strindex) | |
2997 | { | |
2998 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2999 | return 1; | |
3000 | } | |
3001 | } | |
5a580b3a AM |
3002 | } |
3003 | ||
3004 | if (do_it) | |
3005 | { | |
7e9f0867 AM |
3006 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3007 | return -1; | |
3008 | ||
5a580b3a AM |
3009 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3010 | return -1; | |
3011 | } | |
3012 | else | |
3013 | /* We were just checking for existence of the tag. */ | |
3014 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3015 | ||
3016 | return 0; | |
3017 | } | |
3018 | ||
3019 | /* Sort symbol by value and section. */ | |
4ad4eba5 AM |
3020 | static int |
3021 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3022 | { |
3023 | const struct elf_link_hash_entry *h1; | |
3024 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3025 | bfd_signed_vma vdiff; |
5a580b3a AM |
3026 | |
3027 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3028 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3029 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3030 | if (vdiff != 0) | |
3031 | return vdiff > 0 ? 1 : -1; | |
3032 | else | |
3033 | { | |
3034 | long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
3035 | if (sdiff != 0) | |
3036 | return sdiff > 0 ? 1 : -1; | |
3037 | } | |
5a580b3a AM |
3038 | return 0; |
3039 | } | |
4ad4eba5 | 3040 | |
5a580b3a AM |
3041 | /* This function is used to adjust offsets into .dynstr for |
3042 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3043 | ||
3044 | static bfd_boolean | |
3045 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3046 | { | |
3047 | struct elf_strtab_hash *dynstr = data; | |
3048 | ||
3049 | if (h->root.type == bfd_link_hash_warning) | |
3050 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3051 | ||
3052 | if (h->dynindx != -1) | |
3053 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3054 | return TRUE; | |
3055 | } | |
3056 | ||
3057 | /* Assign string offsets in .dynstr, update all structures referencing | |
3058 | them. */ | |
3059 | ||
4ad4eba5 AM |
3060 | static bfd_boolean |
3061 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3062 | { |
3063 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3064 | struct elf_link_local_dynamic_entry *entry; | |
3065 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3066 | bfd *dynobj = hash_table->dynobj; | |
3067 | asection *sdyn; | |
3068 | bfd_size_type size; | |
3069 | const struct elf_backend_data *bed; | |
3070 | bfd_byte *extdyn; | |
3071 | ||
3072 | _bfd_elf_strtab_finalize (dynstr); | |
3073 | size = _bfd_elf_strtab_size (dynstr); | |
3074 | ||
3075 | bed = get_elf_backend_data (dynobj); | |
3076 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
3077 | BFD_ASSERT (sdyn != NULL); | |
3078 | ||
3079 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3080 | for (extdyn = sdyn->contents; | |
eea6121a | 3081 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3082 | extdyn += bed->s->sizeof_dyn) |
3083 | { | |
3084 | Elf_Internal_Dyn dyn; | |
3085 | ||
3086 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3087 | switch (dyn.d_tag) | |
3088 | { | |
3089 | case DT_STRSZ: | |
3090 | dyn.d_un.d_val = size; | |
3091 | break; | |
3092 | case DT_NEEDED: | |
3093 | case DT_SONAME: | |
3094 | case DT_RPATH: | |
3095 | case DT_RUNPATH: | |
3096 | case DT_FILTER: | |
3097 | case DT_AUXILIARY: | |
3098 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
3099 | break; | |
3100 | default: | |
3101 | continue; | |
3102 | } | |
3103 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3104 | } | |
3105 | ||
3106 | /* Now update local dynamic symbols. */ | |
3107 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3108 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3109 | entry->isym.st_name); | |
3110 | ||
3111 | /* And the rest of dynamic symbols. */ | |
3112 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3113 | ||
3114 | /* Adjust version definitions. */ | |
3115 | if (elf_tdata (output_bfd)->cverdefs) | |
3116 | { | |
3117 | asection *s; | |
3118 | bfd_byte *p; | |
3119 | bfd_size_type i; | |
3120 | Elf_Internal_Verdef def; | |
3121 | Elf_Internal_Verdaux defaux; | |
3122 | ||
3123 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
3124 | p = s->contents; | |
3125 | do | |
3126 | { | |
3127 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3128 | &def); | |
3129 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3130 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3131 | continue; | |
5a580b3a AM |
3132 | for (i = 0; i < def.vd_cnt; ++i) |
3133 | { | |
3134 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3135 | (Elf_External_Verdaux *) p, &defaux); | |
3136 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3137 | defaux.vda_name); | |
3138 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3139 | &defaux, (Elf_External_Verdaux *) p); | |
3140 | p += sizeof (Elf_External_Verdaux); | |
3141 | } | |
3142 | } | |
3143 | while (def.vd_next); | |
3144 | } | |
3145 | ||
3146 | /* Adjust version references. */ | |
3147 | if (elf_tdata (output_bfd)->verref) | |
3148 | { | |
3149 | asection *s; | |
3150 | bfd_byte *p; | |
3151 | bfd_size_type i; | |
3152 | Elf_Internal_Verneed need; | |
3153 | Elf_Internal_Vernaux needaux; | |
3154 | ||
3155 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
3156 | p = s->contents; | |
3157 | do | |
3158 | { | |
3159 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3160 | &need); | |
3161 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3162 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3163 | (Elf_External_Verneed *) p); | |
3164 | p += sizeof (Elf_External_Verneed); | |
3165 | for (i = 0; i < need.vn_cnt; ++i) | |
3166 | { | |
3167 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3168 | (Elf_External_Vernaux *) p, &needaux); | |
3169 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3170 | needaux.vna_name); | |
3171 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3172 | &needaux, | |
3173 | (Elf_External_Vernaux *) p); | |
3174 | p += sizeof (Elf_External_Vernaux); | |
3175 | } | |
3176 | } | |
3177 | while (need.vn_next); | |
3178 | } | |
3179 | ||
3180 | return TRUE; | |
3181 | } | |
3182 | \f | |
4ad4eba5 AM |
3183 | /* Add symbols from an ELF object file to the linker hash table. */ |
3184 | ||
3185 | static bfd_boolean | |
3186 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3187 | { | |
4ad4eba5 AM |
3188 | Elf_Internal_Shdr *hdr; |
3189 | bfd_size_type symcount; | |
3190 | bfd_size_type extsymcount; | |
3191 | bfd_size_type extsymoff; | |
3192 | struct elf_link_hash_entry **sym_hash; | |
3193 | bfd_boolean dynamic; | |
3194 | Elf_External_Versym *extversym = NULL; | |
3195 | Elf_External_Versym *ever; | |
3196 | struct elf_link_hash_entry *weaks; | |
3197 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
3198 | bfd_size_type nondeflt_vers_cnt = 0; | |
3199 | Elf_Internal_Sym *isymbuf = NULL; | |
3200 | Elf_Internal_Sym *isym; | |
3201 | Elf_Internal_Sym *isymend; | |
3202 | const struct elf_backend_data *bed; | |
3203 | bfd_boolean add_needed; | |
66eb6687 | 3204 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3205 | bfd_size_type amt; |
66eb6687 | 3206 | void *alloc_mark = NULL; |
4f87808c AM |
3207 | struct bfd_hash_entry **old_table = NULL; |
3208 | unsigned int old_size = 0; | |
3209 | unsigned int old_count = 0; | |
66eb6687 AM |
3210 | void *old_tab = NULL; |
3211 | void *old_hash; | |
3212 | void *old_ent; | |
3213 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3214 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
3215 | long old_dynsymcount = 0; | |
3216 | size_t tabsize = 0; | |
3217 | size_t hashsize = 0; | |
4ad4eba5 | 3218 | |
66eb6687 | 3219 | htab = elf_hash_table (info); |
4ad4eba5 | 3220 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3221 | |
3222 | if ((abfd->flags & DYNAMIC) == 0) | |
3223 | dynamic = FALSE; | |
3224 | else | |
3225 | { | |
3226 | dynamic = TRUE; | |
3227 | ||
3228 | /* You can't use -r against a dynamic object. Also, there's no | |
3229 | hope of using a dynamic object which does not exactly match | |
3230 | the format of the output file. */ | |
3231 | if (info->relocatable | |
66eb6687 AM |
3232 | || !is_elf_hash_table (htab) |
3233 | || htab->root.creator != abfd->xvec) | |
4ad4eba5 | 3234 | { |
9a0789ec NC |
3235 | if (info->relocatable) |
3236 | bfd_set_error (bfd_error_invalid_operation); | |
3237 | else | |
3238 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3239 | goto error_return; |
3240 | } | |
3241 | } | |
3242 | ||
3243 | /* As a GNU extension, any input sections which are named | |
3244 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3245 | symbol. This differs from .gnu.warning sections, which generate | |
3246 | warnings when they are included in an output file. */ | |
3247 | if (info->executable) | |
3248 | { | |
3249 | asection *s; | |
3250 | ||
3251 | for (s = abfd->sections; s != NULL; s = s->next) | |
3252 | { | |
3253 | const char *name; | |
3254 | ||
3255 | name = bfd_get_section_name (abfd, s); | |
0112cd26 | 3256 | if (CONST_STRNEQ (name, ".gnu.warning.")) |
4ad4eba5 AM |
3257 | { |
3258 | char *msg; | |
3259 | bfd_size_type sz; | |
4ad4eba5 AM |
3260 | |
3261 | name += sizeof ".gnu.warning." - 1; | |
3262 | ||
3263 | /* If this is a shared object, then look up the symbol | |
3264 | in the hash table. If it is there, and it is already | |
3265 | been defined, then we will not be using the entry | |
3266 | from this shared object, so we don't need to warn. | |
3267 | FIXME: If we see the definition in a regular object | |
3268 | later on, we will warn, but we shouldn't. The only | |
3269 | fix is to keep track of what warnings we are supposed | |
3270 | to emit, and then handle them all at the end of the | |
3271 | link. */ | |
3272 | if (dynamic) | |
3273 | { | |
3274 | struct elf_link_hash_entry *h; | |
3275 | ||
66eb6687 | 3276 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
4ad4eba5 AM |
3277 | |
3278 | /* FIXME: What about bfd_link_hash_common? */ | |
3279 | if (h != NULL | |
3280 | && (h->root.type == bfd_link_hash_defined | |
3281 | || h->root.type == bfd_link_hash_defweak)) | |
3282 | { | |
3283 | /* We don't want to issue this warning. Clobber | |
3284 | the section size so that the warning does not | |
3285 | get copied into the output file. */ | |
eea6121a | 3286 | s->size = 0; |
4ad4eba5 AM |
3287 | continue; |
3288 | } | |
3289 | } | |
3290 | ||
eea6121a | 3291 | sz = s->size; |
370a0e1b | 3292 | msg = bfd_alloc (abfd, sz + 1); |
4ad4eba5 AM |
3293 | if (msg == NULL) |
3294 | goto error_return; | |
3295 | ||
370a0e1b | 3296 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
4ad4eba5 AM |
3297 | goto error_return; |
3298 | ||
370a0e1b | 3299 | msg[sz] = '\0'; |
4ad4eba5 AM |
3300 | |
3301 | if (! (_bfd_generic_link_add_one_symbol | |
3302 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
66eb6687 | 3303 | FALSE, bed->collect, NULL))) |
4ad4eba5 AM |
3304 | goto error_return; |
3305 | ||
3306 | if (! info->relocatable) | |
3307 | { | |
3308 | /* Clobber the section size so that the warning does | |
3309 | not get copied into the output file. */ | |
eea6121a | 3310 | s->size = 0; |
11d2f718 AM |
3311 | |
3312 | /* Also set SEC_EXCLUDE, so that symbols defined in | |
3313 | the warning section don't get copied to the output. */ | |
3314 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3315 | } |
3316 | } | |
3317 | } | |
3318 | } | |
3319 | ||
3320 | add_needed = TRUE; | |
3321 | if (! dynamic) | |
3322 | { | |
3323 | /* If we are creating a shared library, create all the dynamic | |
3324 | sections immediately. We need to attach them to something, | |
3325 | so we attach them to this BFD, provided it is the right | |
3326 | format. FIXME: If there are no input BFD's of the same | |
3327 | format as the output, we can't make a shared library. */ | |
3328 | if (info->shared | |
66eb6687 AM |
3329 | && is_elf_hash_table (htab) |
3330 | && htab->root.creator == abfd->xvec | |
3331 | && !htab->dynamic_sections_created) | |
4ad4eba5 AM |
3332 | { |
3333 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3334 | goto error_return; | |
3335 | } | |
3336 | } | |
66eb6687 | 3337 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
3338 | goto error_return; |
3339 | else | |
3340 | { | |
3341 | asection *s; | |
3342 | const char *soname = NULL; | |
3343 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; | |
3344 | int ret; | |
3345 | ||
3346 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3347 | ld shouldn't allow it. */ |
4ad4eba5 AM |
3348 | if ((s = abfd->sections) != NULL |
3349 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
92fd189d | 3350 | abort (); |
4ad4eba5 AM |
3351 | |
3352 | /* If this dynamic lib was specified on the command line with | |
3353 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3354 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3355 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3356 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3357 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3358 | all. */ | |
3359 | add_needed = (elf_dyn_lib_class (abfd) | |
3360 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3361 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3362 | |
3363 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3364 | if (s != NULL) | |
3365 | { | |
3366 | bfd_byte *dynbuf; | |
3367 | bfd_byte *extdyn; | |
3368 | int elfsec; | |
3369 | unsigned long shlink; | |
3370 | ||
eea6121a | 3371 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
4ad4eba5 AM |
3372 | goto error_free_dyn; |
3373 | ||
3374 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
3375 | if (elfsec == -1) | |
3376 | goto error_free_dyn; | |
3377 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3378 | ||
3379 | for (extdyn = dynbuf; | |
eea6121a | 3380 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3381 | extdyn += bed->s->sizeof_dyn) |
3382 | { | |
3383 | Elf_Internal_Dyn dyn; | |
3384 | ||
3385 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3386 | if (dyn.d_tag == DT_SONAME) | |
3387 | { | |
3388 | unsigned int tagv = dyn.d_un.d_val; | |
3389 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3390 | if (soname == NULL) | |
3391 | goto error_free_dyn; | |
3392 | } | |
3393 | if (dyn.d_tag == DT_NEEDED) | |
3394 | { | |
3395 | struct bfd_link_needed_list *n, **pn; | |
3396 | char *fnm, *anm; | |
3397 | unsigned int tagv = dyn.d_un.d_val; | |
3398 | ||
3399 | amt = sizeof (struct bfd_link_needed_list); | |
3400 | n = bfd_alloc (abfd, amt); | |
3401 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3402 | if (n == NULL || fnm == NULL) | |
3403 | goto error_free_dyn; | |
3404 | amt = strlen (fnm) + 1; | |
3405 | anm = bfd_alloc (abfd, amt); | |
3406 | if (anm == NULL) | |
3407 | goto error_free_dyn; | |
3408 | memcpy (anm, fnm, amt); | |
3409 | n->name = anm; | |
3410 | n->by = abfd; | |
3411 | n->next = NULL; | |
66eb6687 | 3412 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3413 | ; |
3414 | *pn = n; | |
3415 | } | |
3416 | if (dyn.d_tag == DT_RUNPATH) | |
3417 | { | |
3418 | struct bfd_link_needed_list *n, **pn; | |
3419 | char *fnm, *anm; | |
3420 | unsigned int tagv = dyn.d_un.d_val; | |
3421 | ||
3422 | amt = sizeof (struct bfd_link_needed_list); | |
3423 | n = bfd_alloc (abfd, amt); | |
3424 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3425 | if (n == NULL || fnm == NULL) | |
3426 | goto error_free_dyn; | |
3427 | amt = strlen (fnm) + 1; | |
3428 | anm = bfd_alloc (abfd, amt); | |
3429 | if (anm == NULL) | |
3430 | goto error_free_dyn; | |
3431 | memcpy (anm, fnm, amt); | |
3432 | n->name = anm; | |
3433 | n->by = abfd; | |
3434 | n->next = NULL; | |
3435 | for (pn = & runpath; | |
3436 | *pn != NULL; | |
3437 | pn = &(*pn)->next) | |
3438 | ; | |
3439 | *pn = n; | |
3440 | } | |
3441 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3442 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3443 | { | |
3444 | struct bfd_link_needed_list *n, **pn; | |
3445 | char *fnm, *anm; | |
3446 | unsigned int tagv = dyn.d_un.d_val; | |
3447 | ||
3448 | amt = sizeof (struct bfd_link_needed_list); | |
3449 | n = bfd_alloc (abfd, amt); | |
3450 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3451 | if (n == NULL || fnm == NULL) | |
3452 | goto error_free_dyn; | |
3453 | amt = strlen (fnm) + 1; | |
3454 | anm = bfd_alloc (abfd, amt); | |
3455 | if (anm == NULL) | |
3456 | { | |
3457 | error_free_dyn: | |
3458 | free (dynbuf); | |
3459 | goto error_return; | |
3460 | } | |
3461 | memcpy (anm, fnm, amt); | |
3462 | n->name = anm; | |
3463 | n->by = abfd; | |
3464 | n->next = NULL; | |
3465 | for (pn = & rpath; | |
3466 | *pn != NULL; | |
3467 | pn = &(*pn)->next) | |
3468 | ; | |
3469 | *pn = n; | |
3470 | } | |
3471 | } | |
3472 | ||
3473 | free (dynbuf); | |
3474 | } | |
3475 | ||
3476 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3477 | frees all more recently bfd_alloc'd blocks as well. */ | |
3478 | if (runpath) | |
3479 | rpath = runpath; | |
3480 | ||
3481 | if (rpath) | |
3482 | { | |
3483 | struct bfd_link_needed_list **pn; | |
66eb6687 | 3484 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3485 | ; |
3486 | *pn = rpath; | |
3487 | } | |
3488 | ||
3489 | /* We do not want to include any of the sections in a dynamic | |
3490 | object in the output file. We hack by simply clobbering the | |
3491 | list of sections in the BFD. This could be handled more | |
3492 | cleanly by, say, a new section flag; the existing | |
3493 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3494 | still implies that the section takes up space in the output | |
3495 | file. */ | |
3496 | bfd_section_list_clear (abfd); | |
3497 | ||
4ad4eba5 AM |
3498 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3499 | object. If the object has a DT_SONAME entry, we use it. | |
3500 | Otherwise, if the generic linker stuck something in | |
3501 | elf_dt_name, we use that. Otherwise, we just use the file | |
3502 | name. */ | |
3503 | if (soname == NULL || *soname == '\0') | |
3504 | { | |
3505 | soname = elf_dt_name (abfd); | |
3506 | if (soname == NULL || *soname == '\0') | |
3507 | soname = bfd_get_filename (abfd); | |
3508 | } | |
3509 | ||
3510 | /* Save the SONAME because sometimes the linker emulation code | |
3511 | will need to know it. */ | |
3512 | elf_dt_name (abfd) = soname; | |
3513 | ||
7e9f0867 | 3514 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3515 | if (ret < 0) |
3516 | goto error_return; | |
3517 | ||
3518 | /* If we have already included this dynamic object in the | |
3519 | link, just ignore it. There is no reason to include a | |
3520 | particular dynamic object more than once. */ | |
3521 | if (ret > 0) | |
3522 | return TRUE; | |
3523 | } | |
3524 | ||
3525 | /* If this is a dynamic object, we always link against the .dynsym | |
3526 | symbol table, not the .symtab symbol table. The dynamic linker | |
3527 | will only see the .dynsym symbol table, so there is no reason to | |
3528 | look at .symtab for a dynamic object. */ | |
3529 | ||
3530 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3531 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3532 | else | |
3533 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3534 | ||
3535 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3536 | ||
3537 | /* The sh_info field of the symtab header tells us where the | |
3538 | external symbols start. We don't care about the local symbols at | |
3539 | this point. */ | |
3540 | if (elf_bad_symtab (abfd)) | |
3541 | { | |
3542 | extsymcount = symcount; | |
3543 | extsymoff = 0; | |
3544 | } | |
3545 | else | |
3546 | { | |
3547 | extsymcount = symcount - hdr->sh_info; | |
3548 | extsymoff = hdr->sh_info; | |
3549 | } | |
3550 | ||
3551 | sym_hash = NULL; | |
3552 | if (extsymcount != 0) | |
3553 | { | |
3554 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3555 | NULL, NULL, NULL); | |
3556 | if (isymbuf == NULL) | |
3557 | goto error_return; | |
3558 | ||
3559 | /* We store a pointer to the hash table entry for each external | |
3560 | symbol. */ | |
3561 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3562 | sym_hash = bfd_alloc (abfd, amt); | |
3563 | if (sym_hash == NULL) | |
3564 | goto error_free_sym; | |
3565 | elf_sym_hashes (abfd) = sym_hash; | |
3566 | } | |
3567 | ||
3568 | if (dynamic) | |
3569 | { | |
3570 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
3571 | if (!_bfd_elf_slurp_version_tables (abfd, |
3572 | info->default_imported_symver)) | |
4ad4eba5 AM |
3573 | goto error_free_sym; |
3574 | ||
3575 | /* Read in the symbol versions, but don't bother to convert them | |
3576 | to internal format. */ | |
3577 | if (elf_dynversym (abfd) != 0) | |
3578 | { | |
3579 | Elf_Internal_Shdr *versymhdr; | |
3580 | ||
3581 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
3582 | extversym = bfd_malloc (versymhdr->sh_size); | |
3583 | if (extversym == NULL) | |
3584 | goto error_free_sym; | |
3585 | amt = versymhdr->sh_size; | |
3586 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3587 | || bfd_bread (extversym, amt, abfd) != amt) | |
3588 | goto error_free_vers; | |
3589 | } | |
3590 | } | |
3591 | ||
66eb6687 AM |
3592 | /* If we are loading an as-needed shared lib, save the symbol table |
3593 | state before we start adding symbols. If the lib turns out | |
3594 | to be unneeded, restore the state. */ | |
3595 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
3596 | { | |
3597 | unsigned int i; | |
3598 | size_t entsize; | |
3599 | ||
3600 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
3601 | { | |
3602 | struct bfd_hash_entry *p; | |
2de92251 | 3603 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3604 | |
3605 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
3606 | { |
3607 | h = (struct elf_link_hash_entry *) p; | |
3608 | entsize += htab->root.table.entsize; | |
3609 | if (h->root.type == bfd_link_hash_warning) | |
3610 | entsize += htab->root.table.entsize; | |
3611 | } | |
66eb6687 AM |
3612 | } |
3613 | ||
3614 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
3615 | hashsize = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3616 | old_tab = bfd_malloc (tabsize + entsize + hashsize); | |
3617 | if (old_tab == NULL) | |
3618 | goto error_free_vers; | |
3619 | ||
3620 | /* Remember the current objalloc pointer, so that all mem for | |
3621 | symbols added can later be reclaimed. */ | |
3622 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
3623 | if (alloc_mark == NULL) | |
3624 | goto error_free_vers; | |
3625 | ||
5061a885 AM |
3626 | /* Make a special call to the linker "notice" function to |
3627 | tell it that we are about to handle an as-needed lib. */ | |
3628 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, | |
3629 | notice_as_needed)) | |
3630 | return FALSE; | |
3631 | ||
3632 | ||
66eb6687 AM |
3633 | /* Clone the symbol table and sym hashes. Remember some |
3634 | pointers into the symbol table, and dynamic symbol count. */ | |
3635 | old_hash = (char *) old_tab + tabsize; | |
3636 | old_ent = (char *) old_hash + hashsize; | |
3637 | memcpy (old_tab, htab->root.table.table, tabsize); | |
3638 | memcpy (old_hash, sym_hash, hashsize); | |
3639 | old_undefs = htab->root.undefs; | |
3640 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
3641 | old_table = htab->root.table.table; |
3642 | old_size = htab->root.table.size; | |
3643 | old_count = htab->root.table.count; | |
66eb6687 AM |
3644 | old_dynsymcount = htab->dynsymcount; |
3645 | ||
3646 | for (i = 0; i < htab->root.table.size; i++) | |
3647 | { | |
3648 | struct bfd_hash_entry *p; | |
2de92251 | 3649 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3650 | |
3651 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
3652 | { | |
3653 | memcpy (old_ent, p, htab->root.table.entsize); | |
3654 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
3655 | h = (struct elf_link_hash_entry *) p; |
3656 | if (h->root.type == bfd_link_hash_warning) | |
3657 | { | |
3658 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
3659 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
3660 | } | |
66eb6687 AM |
3661 | } |
3662 | } | |
3663 | } | |
4ad4eba5 | 3664 | |
66eb6687 | 3665 | weaks = NULL; |
4ad4eba5 AM |
3666 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
3667 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3668 | isym < isymend; | |
3669 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3670 | { | |
3671 | int bind; | |
3672 | bfd_vma value; | |
af44c138 | 3673 | asection *sec, *new_sec; |
4ad4eba5 AM |
3674 | flagword flags; |
3675 | const char *name; | |
3676 | struct elf_link_hash_entry *h; | |
3677 | bfd_boolean definition; | |
3678 | bfd_boolean size_change_ok; | |
3679 | bfd_boolean type_change_ok; | |
3680 | bfd_boolean new_weakdef; | |
3681 | bfd_boolean override; | |
a4d8e49b | 3682 | bfd_boolean common; |
4ad4eba5 AM |
3683 | unsigned int old_alignment; |
3684 | bfd *old_bfd; | |
3685 | ||
3686 | override = FALSE; | |
3687 | ||
3688 | flags = BSF_NO_FLAGS; | |
3689 | sec = NULL; | |
3690 | value = isym->st_value; | |
3691 | *sym_hash = NULL; | |
a4d8e49b | 3692 | common = bed->common_definition (isym); |
4ad4eba5 AM |
3693 | |
3694 | bind = ELF_ST_BIND (isym->st_info); | |
3695 | if (bind == STB_LOCAL) | |
3696 | { | |
3697 | /* This should be impossible, since ELF requires that all | |
3698 | global symbols follow all local symbols, and that sh_info | |
3699 | point to the first global symbol. Unfortunately, Irix 5 | |
3700 | screws this up. */ | |
3701 | continue; | |
3702 | } | |
3703 | else if (bind == STB_GLOBAL) | |
3704 | { | |
a4d8e49b | 3705 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 AM |
3706 | flags = BSF_GLOBAL; |
3707 | } | |
3708 | else if (bind == STB_WEAK) | |
3709 | flags = BSF_WEAK; | |
3710 | else | |
3711 | { | |
3712 | /* Leave it up to the processor backend. */ | |
3713 | } | |
3714 | ||
3715 | if (isym->st_shndx == SHN_UNDEF) | |
3716 | sec = bfd_und_section_ptr; | |
66eb6687 AM |
3717 | else if (isym->st_shndx < SHN_LORESERVE |
3718 | || isym->st_shndx > SHN_HIRESERVE) | |
4ad4eba5 AM |
3719 | { |
3720 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3721 | if (sec == NULL) | |
3722 | sec = bfd_abs_section_ptr; | |
529fcb95 PB |
3723 | else if (sec->kept_section) |
3724 | { | |
e5d08002 L |
3725 | /* Symbols from discarded section are undefined. We keep |
3726 | its visibility. */ | |
529fcb95 PB |
3727 | sec = bfd_und_section_ptr; |
3728 | isym->st_shndx = SHN_UNDEF; | |
3729 | } | |
4ad4eba5 AM |
3730 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
3731 | value -= sec->vma; | |
3732 | } | |
3733 | else if (isym->st_shndx == SHN_ABS) | |
3734 | sec = bfd_abs_section_ptr; | |
3735 | else if (isym->st_shndx == SHN_COMMON) | |
3736 | { | |
3737 | sec = bfd_com_section_ptr; | |
3738 | /* What ELF calls the size we call the value. What ELF | |
3739 | calls the value we call the alignment. */ | |
3740 | value = isym->st_size; | |
3741 | } | |
3742 | else | |
3743 | { | |
3744 | /* Leave it up to the processor backend. */ | |
3745 | } | |
3746 | ||
3747 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3748 | isym->st_name); | |
3749 | if (name == NULL) | |
3750 | goto error_free_vers; | |
3751 | ||
3752 | if (isym->st_shndx == SHN_COMMON | |
6a4a0940 JJ |
3753 | && ELF_ST_TYPE (isym->st_info) == STT_TLS |
3754 | && !info->relocatable) | |
4ad4eba5 AM |
3755 | { |
3756 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3757 | ||
3758 | if (tcomm == NULL) | |
3759 | { | |
3496cb2a L |
3760 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", |
3761 | (SEC_ALLOC | |
3762 | | SEC_IS_COMMON | |
3763 | | SEC_LINKER_CREATED | |
3764 | | SEC_THREAD_LOCAL)); | |
3765 | if (tcomm == NULL) | |
4ad4eba5 AM |
3766 | goto error_free_vers; |
3767 | } | |
3768 | sec = tcomm; | |
3769 | } | |
66eb6687 | 3770 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 3771 | { |
66eb6687 AM |
3772 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
3773 | &sec, &value)) | |
4ad4eba5 AM |
3774 | goto error_free_vers; |
3775 | ||
3776 | /* The hook function sets the name to NULL if this symbol | |
3777 | should be skipped for some reason. */ | |
3778 | if (name == NULL) | |
3779 | continue; | |
3780 | } | |
3781 | ||
3782 | /* Sanity check that all possibilities were handled. */ | |
3783 | if (sec == NULL) | |
3784 | { | |
3785 | bfd_set_error (bfd_error_bad_value); | |
3786 | goto error_free_vers; | |
3787 | } | |
3788 | ||
3789 | if (bfd_is_und_section (sec) | |
3790 | || bfd_is_com_section (sec)) | |
3791 | definition = FALSE; | |
3792 | else | |
3793 | definition = TRUE; | |
3794 | ||
3795 | size_change_ok = FALSE; | |
66eb6687 | 3796 | type_change_ok = bed->type_change_ok; |
4ad4eba5 AM |
3797 | old_alignment = 0; |
3798 | old_bfd = NULL; | |
af44c138 | 3799 | new_sec = sec; |
4ad4eba5 | 3800 | |
66eb6687 | 3801 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
3802 | { |
3803 | Elf_Internal_Versym iver; | |
3804 | unsigned int vernum = 0; | |
3805 | bfd_boolean skip; | |
3806 | ||
fc0e6df6 | 3807 | if (ever == NULL) |
4ad4eba5 | 3808 | { |
fc0e6df6 PB |
3809 | if (info->default_imported_symver) |
3810 | /* Use the default symbol version created earlier. */ | |
3811 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
3812 | else | |
3813 | iver.vs_vers = 0; | |
3814 | } | |
3815 | else | |
3816 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
3817 | ||
3818 | vernum = iver.vs_vers & VERSYM_VERSION; | |
3819 | ||
3820 | /* If this is a hidden symbol, or if it is not version | |
3821 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
3822 | However, we do not modify a non-hidden absolute symbol |
3823 | if it is not a function, because it might be the version | |
3824 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 3825 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
3826 | || (vernum > 1 |
3827 | && (!bfd_is_abs_section (sec) | |
3828 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
3829 | { |
3830 | const char *verstr; | |
3831 | size_t namelen, verlen, newlen; | |
3832 | char *newname, *p; | |
3833 | ||
3834 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 3835 | { |
fc0e6df6 PB |
3836 | if (vernum > elf_tdata (abfd)->cverdefs) |
3837 | verstr = NULL; | |
3838 | else if (vernum > 1) | |
3839 | verstr = | |
3840 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
3841 | else | |
3842 | verstr = ""; | |
4ad4eba5 | 3843 | |
fc0e6df6 | 3844 | if (verstr == NULL) |
4ad4eba5 | 3845 | { |
fc0e6df6 PB |
3846 | (*_bfd_error_handler) |
3847 | (_("%B: %s: invalid version %u (max %d)"), | |
3848 | abfd, name, vernum, | |
3849 | elf_tdata (abfd)->cverdefs); | |
3850 | bfd_set_error (bfd_error_bad_value); | |
3851 | goto error_free_vers; | |
4ad4eba5 | 3852 | } |
fc0e6df6 PB |
3853 | } |
3854 | else | |
3855 | { | |
3856 | /* We cannot simply test for the number of | |
3857 | entries in the VERNEED section since the | |
3858 | numbers for the needed versions do not start | |
3859 | at 0. */ | |
3860 | Elf_Internal_Verneed *t; | |
3861 | ||
3862 | verstr = NULL; | |
3863 | for (t = elf_tdata (abfd)->verref; | |
3864 | t != NULL; | |
3865 | t = t->vn_nextref) | |
4ad4eba5 | 3866 | { |
fc0e6df6 | 3867 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 3868 | |
fc0e6df6 PB |
3869 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
3870 | { | |
3871 | if (a->vna_other == vernum) | |
4ad4eba5 | 3872 | { |
fc0e6df6 PB |
3873 | verstr = a->vna_nodename; |
3874 | break; | |
4ad4eba5 | 3875 | } |
4ad4eba5 | 3876 | } |
fc0e6df6 PB |
3877 | if (a != NULL) |
3878 | break; | |
3879 | } | |
3880 | if (verstr == NULL) | |
3881 | { | |
3882 | (*_bfd_error_handler) | |
3883 | (_("%B: %s: invalid needed version %d"), | |
3884 | abfd, name, vernum); | |
3885 | bfd_set_error (bfd_error_bad_value); | |
3886 | goto error_free_vers; | |
4ad4eba5 | 3887 | } |
4ad4eba5 | 3888 | } |
fc0e6df6 PB |
3889 | |
3890 | namelen = strlen (name); | |
3891 | verlen = strlen (verstr); | |
3892 | newlen = namelen + verlen + 2; | |
3893 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3894 | && isym->st_shndx != SHN_UNDEF) | |
3895 | ++newlen; | |
3896 | ||
66eb6687 | 3897 | newname = bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
3898 | if (newname == NULL) |
3899 | goto error_free_vers; | |
3900 | memcpy (newname, name, namelen); | |
3901 | p = newname + namelen; | |
3902 | *p++ = ELF_VER_CHR; | |
3903 | /* If this is a defined non-hidden version symbol, | |
3904 | we add another @ to the name. This indicates the | |
3905 | default version of the symbol. */ | |
3906 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3907 | && isym->st_shndx != SHN_UNDEF) | |
3908 | *p++ = ELF_VER_CHR; | |
3909 | memcpy (p, verstr, verlen + 1); | |
3910 | ||
3911 | name = newname; | |
4ad4eba5 AM |
3912 | } |
3913 | ||
af44c138 L |
3914 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, |
3915 | &value, &old_alignment, | |
4ad4eba5 AM |
3916 | sym_hash, &skip, &override, |
3917 | &type_change_ok, &size_change_ok)) | |
3918 | goto error_free_vers; | |
3919 | ||
3920 | if (skip) | |
3921 | continue; | |
3922 | ||
3923 | if (override) | |
3924 | definition = FALSE; | |
3925 | ||
3926 | h = *sym_hash; | |
3927 | while (h->root.type == bfd_link_hash_indirect | |
3928 | || h->root.type == bfd_link_hash_warning) | |
3929 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3930 | ||
3931 | /* Remember the old alignment if this is a common symbol, so | |
3932 | that we don't reduce the alignment later on. We can't | |
3933 | check later, because _bfd_generic_link_add_one_symbol | |
3934 | will set a default for the alignment which we want to | |
3935 | override. We also remember the old bfd where the existing | |
3936 | definition comes from. */ | |
3937 | switch (h->root.type) | |
3938 | { | |
3939 | default: | |
3940 | break; | |
3941 | ||
3942 | case bfd_link_hash_defined: | |
3943 | case bfd_link_hash_defweak: | |
3944 | old_bfd = h->root.u.def.section->owner; | |
3945 | break; | |
3946 | ||
3947 | case bfd_link_hash_common: | |
3948 | old_bfd = h->root.u.c.p->section->owner; | |
3949 | old_alignment = h->root.u.c.p->alignment_power; | |
3950 | break; | |
3951 | } | |
3952 | ||
3953 | if (elf_tdata (abfd)->verdef != NULL | |
3954 | && ! override | |
3955 | && vernum > 1 | |
3956 | && definition) | |
3957 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
3958 | } | |
3959 | ||
3960 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 3961 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
3962 | (struct bfd_link_hash_entry **) sym_hash))) |
3963 | goto error_free_vers; | |
3964 | ||
3965 | h = *sym_hash; | |
3966 | while (h->root.type == bfd_link_hash_indirect | |
3967 | || h->root.type == bfd_link_hash_warning) | |
3968 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3969 | *sym_hash = h; | |
3970 | ||
3971 | new_weakdef = FALSE; | |
3972 | if (dynamic | |
3973 | && definition | |
3974 | && (flags & BSF_WEAK) != 0 | |
fcb93ecf | 3975 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 3976 | && is_elf_hash_table (htab) |
f6e332e6 | 3977 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
3978 | { |
3979 | /* Keep a list of all weak defined non function symbols from | |
3980 | a dynamic object, using the weakdef field. Later in this | |
3981 | function we will set the weakdef field to the correct | |
3982 | value. We only put non-function symbols from dynamic | |
3983 | objects on this list, because that happens to be the only | |
3984 | time we need to know the normal symbol corresponding to a | |
3985 | weak symbol, and the information is time consuming to | |
3986 | figure out. If the weakdef field is not already NULL, | |
3987 | then this symbol was already defined by some previous | |
3988 | dynamic object, and we will be using that previous | |
3989 | definition anyhow. */ | |
3990 | ||
f6e332e6 | 3991 | h->u.weakdef = weaks; |
4ad4eba5 AM |
3992 | weaks = h; |
3993 | new_weakdef = TRUE; | |
3994 | } | |
3995 | ||
3996 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 3997 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
3998 | && h->root.type == bfd_link_hash_common) |
3999 | { | |
4000 | unsigned int align; | |
4001 | ||
a4d8e49b | 4002 | if (common) |
af44c138 L |
4003 | align = bfd_log2 (isym->st_value); |
4004 | else | |
4005 | { | |
4006 | /* The new symbol is a common symbol in a shared object. | |
4007 | We need to get the alignment from the section. */ | |
4008 | align = new_sec->alignment_power; | |
4009 | } | |
4ad4eba5 AM |
4010 | if (align > old_alignment |
4011 | /* Permit an alignment power of zero if an alignment of one | |
4012 | is specified and no other alignments have been specified. */ | |
4013 | || (isym->st_value == 1 && old_alignment == 0)) | |
4014 | h->root.u.c.p->alignment_power = align; | |
4015 | else | |
4016 | h->root.u.c.p->alignment_power = old_alignment; | |
4017 | } | |
4018 | ||
66eb6687 | 4019 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4020 | { |
4ad4eba5 | 4021 | bfd_boolean dynsym; |
4ad4eba5 AM |
4022 | |
4023 | /* Check the alignment when a common symbol is involved. This | |
4024 | can change when a common symbol is overridden by a normal | |
4025 | definition or a common symbol is ignored due to the old | |
4026 | normal definition. We need to make sure the maximum | |
4027 | alignment is maintained. */ | |
a4d8e49b | 4028 | if ((old_alignment || common) |
4ad4eba5 AM |
4029 | && h->root.type != bfd_link_hash_common) |
4030 | { | |
4031 | unsigned int common_align; | |
4032 | unsigned int normal_align; | |
4033 | unsigned int symbol_align; | |
4034 | bfd *normal_bfd; | |
4035 | bfd *common_bfd; | |
4036 | ||
4037 | symbol_align = ffs (h->root.u.def.value) - 1; | |
4038 | if (h->root.u.def.section->owner != NULL | |
4039 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
4040 | { | |
4041 | normal_align = h->root.u.def.section->alignment_power; | |
4042 | if (normal_align > symbol_align) | |
4043 | normal_align = symbol_align; | |
4044 | } | |
4045 | else | |
4046 | normal_align = symbol_align; | |
4047 | ||
4048 | if (old_alignment) | |
4049 | { | |
4050 | common_align = old_alignment; | |
4051 | common_bfd = old_bfd; | |
4052 | normal_bfd = abfd; | |
4053 | } | |
4054 | else | |
4055 | { | |
4056 | common_align = bfd_log2 (isym->st_value); | |
4057 | common_bfd = abfd; | |
4058 | normal_bfd = old_bfd; | |
4059 | } | |
4060 | ||
4061 | if (normal_align < common_align) | |
d07676f8 NC |
4062 | { |
4063 | /* PR binutils/2735 */ | |
4064 | if (normal_bfd == NULL) | |
4065 | (*_bfd_error_handler) | |
4066 | (_("Warning: alignment %u of common symbol `%s' in %B" | |
4067 | " is greater than the alignment (%u) of its section %A"), | |
4068 | common_bfd, h->root.u.def.section, | |
4069 | 1 << common_align, name, 1 << normal_align); | |
4070 | else | |
4071 | (*_bfd_error_handler) | |
4072 | (_("Warning: alignment %u of symbol `%s' in %B" | |
4073 | " is smaller than %u in %B"), | |
4074 | normal_bfd, common_bfd, | |
4075 | 1 << normal_align, name, 1 << common_align); | |
4076 | } | |
4ad4eba5 AM |
4077 | } |
4078 | ||
83ad0046 L |
4079 | /* Remember the symbol size if it isn't undefined. */ |
4080 | if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 AM |
4081 | && (definition || h->size == 0)) |
4082 | { | |
83ad0046 L |
4083 | if (h->size != 0 |
4084 | && h->size != isym->st_size | |
4085 | && ! size_change_ok) | |
4ad4eba5 | 4086 | (*_bfd_error_handler) |
d003868e AM |
4087 | (_("Warning: size of symbol `%s' changed" |
4088 | " from %lu in %B to %lu in %B"), | |
4089 | old_bfd, abfd, | |
4ad4eba5 | 4090 | name, (unsigned long) h->size, |
d003868e | 4091 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
4092 | |
4093 | h->size = isym->st_size; | |
4094 | } | |
4095 | ||
4096 | /* If this is a common symbol, then we always want H->SIZE | |
4097 | to be the size of the common symbol. The code just above | |
4098 | won't fix the size if a common symbol becomes larger. We | |
4099 | don't warn about a size change here, because that is | |
fcb93ecf PB |
4100 | covered by --warn-common. Allow changed between different |
4101 | function types. */ | |
4ad4eba5 AM |
4102 | if (h->root.type == bfd_link_hash_common) |
4103 | h->size = h->root.u.c.size; | |
4104 | ||
4105 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
4106 | && (definition || h->type == STT_NOTYPE)) | |
4107 | { | |
4108 | if (h->type != STT_NOTYPE | |
4109 | && h->type != ELF_ST_TYPE (isym->st_info) | |
4110 | && ! type_change_ok) | |
4111 | (*_bfd_error_handler) | |
d003868e AM |
4112 | (_("Warning: type of symbol `%s' changed" |
4113 | " from %d to %d in %B"), | |
4114 | abfd, name, h->type, ELF_ST_TYPE (isym->st_info)); | |
4ad4eba5 AM |
4115 | |
4116 | h->type = ELF_ST_TYPE (isym->st_info); | |
4117 | } | |
4118 | ||
4119 | /* If st_other has a processor-specific meaning, specific | |
4120 | code might be needed here. We never merge the visibility | |
4121 | attribute with the one from a dynamic object. */ | |
4122 | if (bed->elf_backend_merge_symbol_attribute) | |
4123 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
4124 | dynamic); | |
4125 | ||
b58f81ae DJ |
4126 | /* If this symbol has default visibility and the user has requested |
4127 | we not re-export it, then mark it as hidden. */ | |
4128 | if (definition && !dynamic | |
4129 | && (abfd->no_export | |
4130 | || (abfd->my_archive && abfd->my_archive->no_export)) | |
4131 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) | |
66eb6687 AM |
4132 | isym->st_other = (STV_HIDDEN |
4133 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
b58f81ae | 4134 | |
8992f0d7 | 4135 | if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic) |
4ad4eba5 AM |
4136 | { |
4137 | unsigned char hvis, symvis, other, nvis; | |
4138 | ||
8992f0d7 TS |
4139 | /* Only merge the visibility. Leave the remainder of the |
4140 | st_other field to elf_backend_merge_symbol_attribute. */ | |
4141 | other = h->other & ~ELF_ST_VISIBILITY (-1); | |
4ad4eba5 AM |
4142 | |
4143 | /* Combine visibilities, using the most constraining one. */ | |
4144 | hvis = ELF_ST_VISIBILITY (h->other); | |
4145 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
4146 | if (! hvis) | |
4147 | nvis = symvis; | |
4148 | else if (! symvis) | |
4149 | nvis = hvis; | |
4150 | else | |
4151 | nvis = hvis < symvis ? hvis : symvis; | |
4152 | ||
4153 | h->other = other | nvis; | |
4154 | } | |
4155 | ||
4156 | /* Set a flag in the hash table entry indicating the type of | |
4157 | reference or definition we just found. Keep a count of | |
4158 | the number of dynamic symbols we find. A dynamic symbol | |
4159 | is one which is referenced or defined by both a regular | |
4160 | object and a shared object. */ | |
4ad4eba5 AM |
4161 | dynsym = FALSE; |
4162 | if (! dynamic) | |
4163 | { | |
4164 | if (! definition) | |
4165 | { | |
f5385ebf | 4166 | h->ref_regular = 1; |
4ad4eba5 | 4167 | if (bind != STB_WEAK) |
f5385ebf | 4168 | h->ref_regular_nonweak = 1; |
4ad4eba5 AM |
4169 | } |
4170 | else | |
f5385ebf | 4171 | h->def_regular = 1; |
4ad4eba5 | 4172 | if (! info->executable |
f5385ebf AM |
4173 | || h->def_dynamic |
4174 | || h->ref_dynamic) | |
4ad4eba5 AM |
4175 | dynsym = TRUE; |
4176 | } | |
4177 | else | |
4178 | { | |
4179 | if (! definition) | |
f5385ebf | 4180 | h->ref_dynamic = 1; |
4ad4eba5 | 4181 | else |
f5385ebf AM |
4182 | h->def_dynamic = 1; |
4183 | if (h->def_regular | |
4184 | || h->ref_regular | |
f6e332e6 | 4185 | || (h->u.weakdef != NULL |
4ad4eba5 | 4186 | && ! new_weakdef |
f6e332e6 | 4187 | && h->u.weakdef->dynindx != -1)) |
4ad4eba5 AM |
4188 | dynsym = TRUE; |
4189 | } | |
4190 | ||
92b7c7b6 L |
4191 | if (definition && (sec->flags & SEC_DEBUGGING)) |
4192 | { | |
4193 | /* We don't want to make debug symbol dynamic. */ | |
4194 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4195 | dynsym = FALSE; | |
4196 | } | |
4197 | ||
4ad4eba5 AM |
4198 | /* Check to see if we need to add an indirect symbol for |
4199 | the default name. */ | |
4200 | if (definition || h->root.type == bfd_link_hash_common) | |
4201 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4202 | &sec, &value, &dynsym, | |
4203 | override)) | |
4204 | goto error_free_vers; | |
4205 | ||
4206 | if (definition && !dynamic) | |
4207 | { | |
4208 | char *p = strchr (name, ELF_VER_CHR); | |
4209 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4210 | { | |
4211 | /* Queue non-default versions so that .symver x, x@FOO | |
4212 | aliases can be checked. */ | |
66eb6687 | 4213 | if (!nondeflt_vers) |
4ad4eba5 | 4214 | { |
66eb6687 AM |
4215 | amt = ((isymend - isym + 1) |
4216 | * sizeof (struct elf_link_hash_entry *)); | |
4ad4eba5 AM |
4217 | nondeflt_vers = bfd_malloc (amt); |
4218 | } | |
66eb6687 | 4219 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4220 | } |
4221 | } | |
4222 | ||
4223 | if (dynsym && h->dynindx == -1) | |
4224 | { | |
c152c796 | 4225 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4226 | goto error_free_vers; |
f6e332e6 | 4227 | if (h->u.weakdef != NULL |
4ad4eba5 | 4228 | && ! new_weakdef |
f6e332e6 | 4229 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 4230 | { |
66eb6687 | 4231 | if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
4232 | goto error_free_vers; |
4233 | } | |
4234 | } | |
4235 | else if (dynsym && h->dynindx != -1) | |
4236 | /* If the symbol already has a dynamic index, but | |
4237 | visibility says it should not be visible, turn it into | |
4238 | a local symbol. */ | |
4239 | switch (ELF_ST_VISIBILITY (h->other)) | |
4240 | { | |
4241 | case STV_INTERNAL: | |
4242 | case STV_HIDDEN: | |
4243 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4244 | dynsym = FALSE; | |
4245 | break; | |
4246 | } | |
4247 | ||
4248 | if (!add_needed | |
4249 | && definition | |
4250 | && dynsym | |
f5385ebf | 4251 | && h->ref_regular) |
4ad4eba5 AM |
4252 | { |
4253 | int ret; | |
4254 | const char *soname = elf_dt_name (abfd); | |
4255 | ||
4256 | /* A symbol from a library loaded via DT_NEEDED of some | |
4257 | other library is referenced by a regular object. | |
e56f61be L |
4258 | Add a DT_NEEDED entry for it. Issue an error if |
4259 | --no-add-needed is used. */ | |
4260 | if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) | |
4261 | { | |
4262 | (*_bfd_error_handler) | |
4263 | (_("%s: invalid DSO for symbol `%s' definition"), | |
d003868e | 4264 | abfd, name); |
e56f61be L |
4265 | bfd_set_error (bfd_error_bad_value); |
4266 | goto error_free_vers; | |
4267 | } | |
4268 | ||
a5db907e AM |
4269 | elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED; |
4270 | ||
4ad4eba5 | 4271 | add_needed = TRUE; |
7e9f0867 | 4272 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4273 | if (ret < 0) |
4274 | goto error_free_vers; | |
4275 | ||
4276 | BFD_ASSERT (ret == 0); | |
4277 | } | |
4278 | } | |
4279 | } | |
4280 | ||
66eb6687 AM |
4281 | if (extversym != NULL) |
4282 | { | |
4283 | free (extversym); | |
4284 | extversym = NULL; | |
4285 | } | |
4286 | ||
4287 | if (isymbuf != NULL) | |
4288 | { | |
4289 | free (isymbuf); | |
4290 | isymbuf = NULL; | |
4291 | } | |
4292 | ||
4293 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4294 | { | |
4295 | unsigned int i; | |
4296 | ||
4297 | /* Restore the symbol table. */ | |
97fed1c9 JJ |
4298 | if (bed->as_needed_cleanup) |
4299 | (*bed->as_needed_cleanup) (abfd, info); | |
66eb6687 AM |
4300 | old_hash = (char *) old_tab + tabsize; |
4301 | old_ent = (char *) old_hash + hashsize; | |
4302 | sym_hash = elf_sym_hashes (abfd); | |
4f87808c AM |
4303 | htab->root.table.table = old_table; |
4304 | htab->root.table.size = old_size; | |
4305 | htab->root.table.count = old_count; | |
66eb6687 AM |
4306 | memcpy (htab->root.table.table, old_tab, tabsize); |
4307 | memcpy (sym_hash, old_hash, hashsize); | |
4308 | htab->root.undefs = old_undefs; | |
4309 | htab->root.undefs_tail = old_undefs_tail; | |
4310 | for (i = 0; i < htab->root.table.size; i++) | |
4311 | { | |
4312 | struct bfd_hash_entry *p; | |
4313 | struct elf_link_hash_entry *h; | |
4314 | ||
4315 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4316 | { | |
4317 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
4318 | if (h->root.type == bfd_link_hash_warning) |
4319 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
66eb6687 AM |
4320 | if (h->dynindx >= old_dynsymcount) |
4321 | _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index); | |
2de92251 | 4322 | |
66eb6687 AM |
4323 | memcpy (p, old_ent, htab->root.table.entsize); |
4324 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4325 | h = (struct elf_link_hash_entry *) p; |
4326 | if (h->root.type == bfd_link_hash_warning) | |
4327 | { | |
4328 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
4329 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4330 | } | |
66eb6687 AM |
4331 | } |
4332 | } | |
4333 | ||
5061a885 AM |
4334 | /* Make a special call to the linker "notice" function to |
4335 | tell it that symbols added for crefs may need to be removed. */ | |
4336 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, | |
4337 | notice_not_needed)) | |
4338 | return FALSE; | |
4339 | ||
66eb6687 AM |
4340 | free (old_tab); |
4341 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
4342 | alloc_mark); | |
4343 | if (nondeflt_vers != NULL) | |
4344 | free (nondeflt_vers); | |
4345 | return TRUE; | |
4346 | } | |
2de92251 | 4347 | |
66eb6687 AM |
4348 | if (old_tab != NULL) |
4349 | { | |
5061a885 AM |
4350 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, |
4351 | notice_needed)) | |
4352 | return FALSE; | |
66eb6687 AM |
4353 | free (old_tab); |
4354 | old_tab = NULL; | |
4355 | } | |
4356 | ||
4ad4eba5 AM |
4357 | /* Now that all the symbols from this input file are created, handle |
4358 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
4359 | if (nondeflt_vers != NULL) | |
4360 | { | |
4361 | bfd_size_type cnt, symidx; | |
4362 | ||
4363 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
4364 | { | |
4365 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
4366 | char *shortname, *p; | |
4367 | ||
4368 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
4369 | if (p == NULL | |
4370 | || (h->root.type != bfd_link_hash_defined | |
4371 | && h->root.type != bfd_link_hash_defweak)) | |
4372 | continue; | |
4373 | ||
4374 | amt = p - h->root.root.string; | |
4375 | shortname = bfd_malloc (amt + 1); | |
4376 | memcpy (shortname, h->root.root.string, amt); | |
4377 | shortname[amt] = '\0'; | |
4378 | ||
4379 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 4380 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
4381 | FALSE, FALSE, FALSE); |
4382 | if (hi != NULL | |
4383 | && hi->root.type == h->root.type | |
4384 | && hi->root.u.def.value == h->root.u.def.value | |
4385 | && hi->root.u.def.section == h->root.u.def.section) | |
4386 | { | |
4387 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4388 | hi->root.type = bfd_link_hash_indirect; | |
4389 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 4390 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
4391 | sym_hash = elf_sym_hashes (abfd); |
4392 | if (sym_hash) | |
4393 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4394 | if (sym_hash[symidx] == hi) | |
4395 | { | |
4396 | sym_hash[symidx] = h; | |
4397 | break; | |
4398 | } | |
4399 | } | |
4400 | free (shortname); | |
4401 | } | |
4402 | free (nondeflt_vers); | |
4403 | nondeflt_vers = NULL; | |
4404 | } | |
4405 | ||
4ad4eba5 AM |
4406 | /* Now set the weakdefs field correctly for all the weak defined |
4407 | symbols we found. The only way to do this is to search all the | |
4408 | symbols. Since we only need the information for non functions in | |
4409 | dynamic objects, that's the only time we actually put anything on | |
4410 | the list WEAKS. We need this information so that if a regular | |
4411 | object refers to a symbol defined weakly in a dynamic object, the | |
4412 | real symbol in the dynamic object is also put in the dynamic | |
4413 | symbols; we also must arrange for both symbols to point to the | |
4414 | same memory location. We could handle the general case of symbol | |
4415 | aliasing, but a general symbol alias can only be generated in | |
4416 | assembler code, handling it correctly would be very time | |
4417 | consuming, and other ELF linkers don't handle general aliasing | |
4418 | either. */ | |
4419 | if (weaks != NULL) | |
4420 | { | |
4421 | struct elf_link_hash_entry **hpp; | |
4422 | struct elf_link_hash_entry **hppend; | |
4423 | struct elf_link_hash_entry **sorted_sym_hash; | |
4424 | struct elf_link_hash_entry *h; | |
4425 | size_t sym_count; | |
4426 | ||
4427 | /* Since we have to search the whole symbol list for each weak | |
4428 | defined symbol, search time for N weak defined symbols will be | |
4429 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
4430 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
4431 | sorted_sym_hash = bfd_malloc (amt); | |
4432 | if (sorted_sym_hash == NULL) | |
4433 | goto error_return; | |
4434 | sym_hash = sorted_sym_hash; | |
4435 | hpp = elf_sym_hashes (abfd); | |
4436 | hppend = hpp + extsymcount; | |
4437 | sym_count = 0; | |
4438 | for (; hpp < hppend; hpp++) | |
4439 | { | |
4440 | h = *hpp; | |
4441 | if (h != NULL | |
4442 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 4443 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
4444 | { |
4445 | *sym_hash = h; | |
4446 | sym_hash++; | |
4447 | sym_count++; | |
4448 | } | |
4449 | } | |
4450 | ||
4451 | qsort (sorted_sym_hash, sym_count, | |
4452 | sizeof (struct elf_link_hash_entry *), | |
4453 | elf_sort_symbol); | |
4454 | ||
4455 | while (weaks != NULL) | |
4456 | { | |
4457 | struct elf_link_hash_entry *hlook; | |
4458 | asection *slook; | |
4459 | bfd_vma vlook; | |
4460 | long ilook; | |
4461 | size_t i, j, idx; | |
4462 | ||
4463 | hlook = weaks; | |
f6e332e6 AM |
4464 | weaks = hlook->u.weakdef; |
4465 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
4466 | |
4467 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
4468 | || hlook->root.type == bfd_link_hash_defweak | |
4469 | || hlook->root.type == bfd_link_hash_common | |
4470 | || hlook->root.type == bfd_link_hash_indirect); | |
4471 | slook = hlook->root.u.def.section; | |
4472 | vlook = hlook->root.u.def.value; | |
4473 | ||
4474 | ilook = -1; | |
4475 | i = 0; | |
4476 | j = sym_count; | |
4477 | while (i < j) | |
4478 | { | |
4479 | bfd_signed_vma vdiff; | |
4480 | idx = (i + j) / 2; | |
4481 | h = sorted_sym_hash [idx]; | |
4482 | vdiff = vlook - h->root.u.def.value; | |
4483 | if (vdiff < 0) | |
4484 | j = idx; | |
4485 | else if (vdiff > 0) | |
4486 | i = idx + 1; | |
4487 | else | |
4488 | { | |
a9b881be | 4489 | long sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
4490 | if (sdiff < 0) |
4491 | j = idx; | |
4492 | else if (sdiff > 0) | |
4493 | i = idx + 1; | |
4494 | else | |
4495 | { | |
4496 | ilook = idx; | |
4497 | break; | |
4498 | } | |
4499 | } | |
4500 | } | |
4501 | ||
4502 | /* We didn't find a value/section match. */ | |
4503 | if (ilook == -1) | |
4504 | continue; | |
4505 | ||
4506 | for (i = ilook; i < sym_count; i++) | |
4507 | { | |
4508 | h = sorted_sym_hash [i]; | |
4509 | ||
4510 | /* Stop if value or section doesn't match. */ | |
4511 | if (h->root.u.def.value != vlook | |
4512 | || h->root.u.def.section != slook) | |
4513 | break; | |
4514 | else if (h != hlook) | |
4515 | { | |
f6e332e6 | 4516 | hlook->u.weakdef = h; |
4ad4eba5 AM |
4517 | |
4518 | /* If the weak definition is in the list of dynamic | |
4519 | symbols, make sure the real definition is put | |
4520 | there as well. */ | |
4521 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
4522 | { | |
c152c796 | 4523 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 AM |
4524 | goto error_return; |
4525 | } | |
4526 | ||
4527 | /* If the real definition is in the list of dynamic | |
4528 | symbols, make sure the weak definition is put | |
4529 | there as well. If we don't do this, then the | |
4530 | dynamic loader might not merge the entries for the | |
4531 | real definition and the weak definition. */ | |
4532 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
4533 | { | |
c152c796 | 4534 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4ad4eba5 AM |
4535 | goto error_return; |
4536 | } | |
4537 | break; | |
4538 | } | |
4539 | } | |
4540 | } | |
4541 | ||
4542 | free (sorted_sym_hash); | |
4543 | } | |
4544 | ||
66eb6687 AM |
4545 | if (bed->check_directives) |
4546 | (*bed->check_directives) (abfd, info); | |
85fbca6a | 4547 | |
4ad4eba5 AM |
4548 | /* If this object is the same format as the output object, and it is |
4549 | not a shared library, then let the backend look through the | |
4550 | relocs. | |
4551 | ||
4552 | This is required to build global offset table entries and to | |
4553 | arrange for dynamic relocs. It is not required for the | |
4554 | particular common case of linking non PIC code, even when linking | |
4555 | against shared libraries, but unfortunately there is no way of | |
4556 | knowing whether an object file has been compiled PIC or not. | |
4557 | Looking through the relocs is not particularly time consuming. | |
4558 | The problem is that we must either (1) keep the relocs in memory, | |
4559 | which causes the linker to require additional runtime memory or | |
4560 | (2) read the relocs twice from the input file, which wastes time. | |
4561 | This would be a good case for using mmap. | |
4562 | ||
4563 | I have no idea how to handle linking PIC code into a file of a | |
4564 | different format. It probably can't be done. */ | |
4ad4eba5 | 4565 | if (! dynamic |
66eb6687 AM |
4566 | && is_elf_hash_table (htab) |
4567 | && htab->root.creator == abfd->xvec | |
4568 | && bed->check_relocs != NULL) | |
4ad4eba5 AM |
4569 | { |
4570 | asection *o; | |
4571 | ||
4572 | for (o = abfd->sections; o != NULL; o = o->next) | |
4573 | { | |
4574 | Elf_Internal_Rela *internal_relocs; | |
4575 | bfd_boolean ok; | |
4576 | ||
4577 | if ((o->flags & SEC_RELOC) == 0 | |
4578 | || o->reloc_count == 0 | |
4579 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4580 | && (o->flags & SEC_DEBUGGING) != 0) | |
4581 | || bfd_is_abs_section (o->output_section)) | |
4582 | continue; | |
4583 | ||
4584 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4585 | info->keep_memory); | |
4586 | if (internal_relocs == NULL) | |
4587 | goto error_return; | |
4588 | ||
66eb6687 | 4589 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); |
4ad4eba5 AM |
4590 | |
4591 | if (elf_section_data (o)->relocs != internal_relocs) | |
4592 | free (internal_relocs); | |
4593 | ||
4594 | if (! ok) | |
4595 | goto error_return; | |
4596 | } | |
4597 | } | |
4598 | ||
4599 | /* If this is a non-traditional link, try to optimize the handling | |
4600 | of the .stab/.stabstr sections. */ | |
4601 | if (! dynamic | |
4602 | && ! info->traditional_format | |
66eb6687 | 4603 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
4604 | && (info->strip != strip_all && info->strip != strip_debugger)) |
4605 | { | |
4606 | asection *stabstr; | |
4607 | ||
4608 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4609 | if (stabstr != NULL) | |
4610 | { | |
4611 | bfd_size_type string_offset = 0; | |
4612 | asection *stab; | |
4613 | ||
4614 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 4615 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
4616 | && (!stab->name[5] || |
4617 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4618 | && (stab->flags & SEC_MERGE) == 0 | |
4619 | && !bfd_is_abs_section (stab->output_section)) | |
4620 | { | |
4621 | struct bfd_elf_section_data *secdata; | |
4622 | ||
4623 | secdata = elf_section_data (stab); | |
66eb6687 AM |
4624 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
4625 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
4626 | &string_offset)) |
4627 | goto error_return; | |
4628 | if (secdata->sec_info) | |
4629 | stab->sec_info_type = ELF_INFO_TYPE_STABS; | |
4630 | } | |
4631 | } | |
4632 | } | |
4633 | ||
66eb6687 | 4634 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
4635 | { |
4636 | /* Add this bfd to the loaded list. */ | |
4637 | struct elf_link_loaded_list *n; | |
4638 | ||
4639 | n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); | |
4640 | if (n == NULL) | |
4641 | goto error_return; | |
4642 | n->abfd = abfd; | |
66eb6687 AM |
4643 | n->next = htab->loaded; |
4644 | htab->loaded = n; | |
4ad4eba5 AM |
4645 | } |
4646 | ||
4647 | return TRUE; | |
4648 | ||
4649 | error_free_vers: | |
66eb6687 AM |
4650 | if (old_tab != NULL) |
4651 | free (old_tab); | |
4ad4eba5 AM |
4652 | if (nondeflt_vers != NULL) |
4653 | free (nondeflt_vers); | |
4654 | if (extversym != NULL) | |
4655 | free (extversym); | |
4656 | error_free_sym: | |
4657 | if (isymbuf != NULL) | |
4658 | free (isymbuf); | |
4659 | error_return: | |
4660 | return FALSE; | |
4661 | } | |
4662 | ||
8387904d AM |
4663 | /* Return the linker hash table entry of a symbol that might be |
4664 | satisfied by an archive symbol. Return -1 on error. */ | |
4665 | ||
4666 | struct elf_link_hash_entry * | |
4667 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
4668 | struct bfd_link_info *info, | |
4669 | const char *name) | |
4670 | { | |
4671 | struct elf_link_hash_entry *h; | |
4672 | char *p, *copy; | |
4673 | size_t len, first; | |
4674 | ||
4675 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
4676 | if (h != NULL) | |
4677 | return h; | |
4678 | ||
4679 | /* If this is a default version (the name contains @@), look up the | |
4680 | symbol again with only one `@' as well as without the version. | |
4681 | The effect is that references to the symbol with and without the | |
4682 | version will be matched by the default symbol in the archive. */ | |
4683 | ||
4684 | p = strchr (name, ELF_VER_CHR); | |
4685 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4686 | return h; | |
4687 | ||
4688 | /* First check with only one `@'. */ | |
4689 | len = strlen (name); | |
4690 | copy = bfd_alloc (abfd, len); | |
4691 | if (copy == NULL) | |
4692 | return (struct elf_link_hash_entry *) 0 - 1; | |
4693 | ||
4694 | first = p - name + 1; | |
4695 | memcpy (copy, name, first); | |
4696 | memcpy (copy + first, name + first + 1, len - first); | |
4697 | ||
4698 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE); | |
4699 | if (h == NULL) | |
4700 | { | |
4701 | /* We also need to check references to the symbol without the | |
4702 | version. */ | |
4703 | copy[first - 1] = '\0'; | |
4704 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
4705 | FALSE, FALSE, FALSE); | |
4706 | } | |
4707 | ||
4708 | bfd_release (abfd, copy); | |
4709 | return h; | |
4710 | } | |
4711 | ||
0ad989f9 L |
4712 | /* Add symbols from an ELF archive file to the linker hash table. We |
4713 | don't use _bfd_generic_link_add_archive_symbols because of a | |
4714 | problem which arises on UnixWare. The UnixWare libc.so is an | |
4715 | archive which includes an entry libc.so.1 which defines a bunch of | |
4716 | symbols. The libc.so archive also includes a number of other | |
4717 | object files, which also define symbols, some of which are the same | |
4718 | as those defined in libc.so.1. Correct linking requires that we | |
4719 | consider each object file in turn, and include it if it defines any | |
4720 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
4721 | this; it looks through the list of undefined symbols, and includes | |
4722 | any object file which defines them. When this algorithm is used on | |
4723 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
4724 | bunch of symbols. This means that some of the other objects in the | |
4725 | archive are not included in the link, which is incorrect since they | |
4726 | precede libc.so.1 in the archive. | |
4727 | ||
4728 | Fortunately, ELF archive handling is simpler than that done by | |
4729 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4730 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4731 | symbol is currently undefined, we know that we must pull in that | |
4732 | object file. | |
4733 | ||
4734 | Unfortunately, we do have to make multiple passes over the symbol | |
4735 | table until nothing further is resolved. */ | |
4736 | ||
4ad4eba5 AM |
4737 | static bfd_boolean |
4738 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
4739 | { |
4740 | symindex c; | |
4741 | bfd_boolean *defined = NULL; | |
4742 | bfd_boolean *included = NULL; | |
4743 | carsym *symdefs; | |
4744 | bfd_boolean loop; | |
4745 | bfd_size_type amt; | |
8387904d AM |
4746 | const struct elf_backend_data *bed; |
4747 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
4748 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
4749 | |
4750 | if (! bfd_has_map (abfd)) | |
4751 | { | |
4752 | /* An empty archive is a special case. */ | |
4753 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
4754 | return TRUE; | |
4755 | bfd_set_error (bfd_error_no_armap); | |
4756 | return FALSE; | |
4757 | } | |
4758 | ||
4759 | /* Keep track of all symbols we know to be already defined, and all | |
4760 | files we know to be already included. This is to speed up the | |
4761 | second and subsequent passes. */ | |
4762 | c = bfd_ardata (abfd)->symdef_count; | |
4763 | if (c == 0) | |
4764 | return TRUE; | |
4765 | amt = c; | |
4766 | amt *= sizeof (bfd_boolean); | |
4767 | defined = bfd_zmalloc (amt); | |
4768 | included = bfd_zmalloc (amt); | |
4769 | if (defined == NULL || included == NULL) | |
4770 | goto error_return; | |
4771 | ||
4772 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
4773 | bed = get_elf_backend_data (abfd); |
4774 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
4775 | |
4776 | do | |
4777 | { | |
4778 | file_ptr last; | |
4779 | symindex i; | |
4780 | carsym *symdef; | |
4781 | carsym *symdefend; | |
4782 | ||
4783 | loop = FALSE; | |
4784 | last = -1; | |
4785 | ||
4786 | symdef = symdefs; | |
4787 | symdefend = symdef + c; | |
4788 | for (i = 0; symdef < symdefend; symdef++, i++) | |
4789 | { | |
4790 | struct elf_link_hash_entry *h; | |
4791 | bfd *element; | |
4792 | struct bfd_link_hash_entry *undefs_tail; | |
4793 | symindex mark; | |
4794 | ||
4795 | if (defined[i] || included[i]) | |
4796 | continue; | |
4797 | if (symdef->file_offset == last) | |
4798 | { | |
4799 | included[i] = TRUE; | |
4800 | continue; | |
4801 | } | |
4802 | ||
8387904d AM |
4803 | h = archive_symbol_lookup (abfd, info, symdef->name); |
4804 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
4805 | goto error_return; | |
0ad989f9 L |
4806 | |
4807 | if (h == NULL) | |
4808 | continue; | |
4809 | ||
4810 | if (h->root.type == bfd_link_hash_common) | |
4811 | { | |
4812 | /* We currently have a common symbol. The archive map contains | |
4813 | a reference to this symbol, so we may want to include it. We | |
4814 | only want to include it however, if this archive element | |
4815 | contains a definition of the symbol, not just another common | |
4816 | declaration of it. | |
4817 | ||
4818 | Unfortunately some archivers (including GNU ar) will put | |
4819 | declarations of common symbols into their archive maps, as | |
4820 | well as real definitions, so we cannot just go by the archive | |
4821 | map alone. Instead we must read in the element's symbol | |
4822 | table and check that to see what kind of symbol definition | |
4823 | this is. */ | |
4824 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
4825 | continue; | |
4826 | } | |
4827 | else if (h->root.type != bfd_link_hash_undefined) | |
4828 | { | |
4829 | if (h->root.type != bfd_link_hash_undefweak) | |
4830 | defined[i] = TRUE; | |
4831 | continue; | |
4832 | } | |
4833 | ||
4834 | /* We need to include this archive member. */ | |
4835 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
4836 | if (element == NULL) | |
4837 | goto error_return; | |
4838 | ||
4839 | if (! bfd_check_format (element, bfd_object)) | |
4840 | goto error_return; | |
4841 | ||
4842 | /* Doublecheck that we have not included this object | |
4843 | already--it should be impossible, but there may be | |
4844 | something wrong with the archive. */ | |
4845 | if (element->archive_pass != 0) | |
4846 | { | |
4847 | bfd_set_error (bfd_error_bad_value); | |
4848 | goto error_return; | |
4849 | } | |
4850 | element->archive_pass = 1; | |
4851 | ||
4852 | undefs_tail = info->hash->undefs_tail; | |
4853 | ||
4854 | if (! (*info->callbacks->add_archive_element) (info, element, | |
4855 | symdef->name)) | |
4856 | goto error_return; | |
4857 | if (! bfd_link_add_symbols (element, info)) | |
4858 | goto error_return; | |
4859 | ||
4860 | /* If there are any new undefined symbols, we need to make | |
4861 | another pass through the archive in order to see whether | |
4862 | they can be defined. FIXME: This isn't perfect, because | |
4863 | common symbols wind up on undefs_tail and because an | |
4864 | undefined symbol which is defined later on in this pass | |
4865 | does not require another pass. This isn't a bug, but it | |
4866 | does make the code less efficient than it could be. */ | |
4867 | if (undefs_tail != info->hash->undefs_tail) | |
4868 | loop = TRUE; | |
4869 | ||
4870 | /* Look backward to mark all symbols from this object file | |
4871 | which we have already seen in this pass. */ | |
4872 | mark = i; | |
4873 | do | |
4874 | { | |
4875 | included[mark] = TRUE; | |
4876 | if (mark == 0) | |
4877 | break; | |
4878 | --mark; | |
4879 | } | |
4880 | while (symdefs[mark].file_offset == symdef->file_offset); | |
4881 | ||
4882 | /* We mark subsequent symbols from this object file as we go | |
4883 | on through the loop. */ | |
4884 | last = symdef->file_offset; | |
4885 | } | |
4886 | } | |
4887 | while (loop); | |
4888 | ||
4889 | free (defined); | |
4890 | free (included); | |
4891 | ||
4892 | return TRUE; | |
4893 | ||
4894 | error_return: | |
4895 | if (defined != NULL) | |
4896 | free (defined); | |
4897 | if (included != NULL) | |
4898 | free (included); | |
4899 | return FALSE; | |
4900 | } | |
4ad4eba5 AM |
4901 | |
4902 | /* Given an ELF BFD, add symbols to the global hash table as | |
4903 | appropriate. */ | |
4904 | ||
4905 | bfd_boolean | |
4906 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
4907 | { | |
4908 | switch (bfd_get_format (abfd)) | |
4909 | { | |
4910 | case bfd_object: | |
4911 | return elf_link_add_object_symbols (abfd, info); | |
4912 | case bfd_archive: | |
4913 | return elf_link_add_archive_symbols (abfd, info); | |
4914 | default: | |
4915 | bfd_set_error (bfd_error_wrong_format); | |
4916 | return FALSE; | |
4917 | } | |
4918 | } | |
5a580b3a AM |
4919 | \f |
4920 | /* This function will be called though elf_link_hash_traverse to store | |
4921 | all hash value of the exported symbols in an array. */ | |
4922 | ||
4923 | static bfd_boolean | |
4924 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
4925 | { | |
4926 | unsigned long **valuep = data; | |
4927 | const char *name; | |
4928 | char *p; | |
4929 | unsigned long ha; | |
4930 | char *alc = NULL; | |
4931 | ||
4932 | if (h->root.type == bfd_link_hash_warning) | |
4933 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4934 | ||
4935 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
4936 | if (h->dynindx == -1) | |
4937 | return TRUE; | |
4938 | ||
4939 | name = h->root.root.string; | |
4940 | p = strchr (name, ELF_VER_CHR); | |
4941 | if (p != NULL) | |
4942 | { | |
4943 | alc = bfd_malloc (p - name + 1); | |
4944 | memcpy (alc, name, p - name); | |
4945 | alc[p - name] = '\0'; | |
4946 | name = alc; | |
4947 | } | |
4948 | ||
4949 | /* Compute the hash value. */ | |
4950 | ha = bfd_elf_hash (name); | |
4951 | ||
4952 | /* Store the found hash value in the array given as the argument. */ | |
4953 | *(*valuep)++ = ha; | |
4954 | ||
4955 | /* And store it in the struct so that we can put it in the hash table | |
4956 | later. */ | |
f6e332e6 | 4957 | h->u.elf_hash_value = ha; |
5a580b3a AM |
4958 | |
4959 | if (alc != NULL) | |
4960 | free (alc); | |
4961 | ||
4962 | return TRUE; | |
4963 | } | |
4964 | ||
fdc90cb4 JJ |
4965 | struct collect_gnu_hash_codes |
4966 | { | |
4967 | bfd *output_bfd; | |
4968 | const struct elf_backend_data *bed; | |
4969 | unsigned long int nsyms; | |
4970 | unsigned long int maskbits; | |
4971 | unsigned long int *hashcodes; | |
4972 | unsigned long int *hashval; | |
4973 | unsigned long int *indx; | |
4974 | unsigned long int *counts; | |
4975 | bfd_vma *bitmask; | |
4976 | bfd_byte *contents; | |
4977 | long int min_dynindx; | |
4978 | unsigned long int bucketcount; | |
4979 | unsigned long int symindx; | |
4980 | long int local_indx; | |
4981 | long int shift1, shift2; | |
4982 | unsigned long int mask; | |
4983 | }; | |
4984 | ||
4985 | /* This function will be called though elf_link_hash_traverse to store | |
4986 | all hash value of the exported symbols in an array. */ | |
4987 | ||
4988 | static bfd_boolean | |
4989 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
4990 | { | |
4991 | struct collect_gnu_hash_codes *s = data; | |
4992 | const char *name; | |
4993 | char *p; | |
4994 | unsigned long ha; | |
4995 | char *alc = NULL; | |
4996 | ||
4997 | if (h->root.type == bfd_link_hash_warning) | |
4998 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4999 | ||
5000 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
5001 | if (h->dynindx == -1) | |
5002 | return TRUE; | |
5003 | ||
5004 | /* Ignore also local symbols and undefined symbols. */ | |
5005 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5006 | return TRUE; | |
5007 | ||
5008 | name = h->root.root.string; | |
5009 | p = strchr (name, ELF_VER_CHR); | |
5010 | if (p != NULL) | |
5011 | { | |
5012 | alc = bfd_malloc (p - name + 1); | |
5013 | memcpy (alc, name, p - name); | |
5014 | alc[p - name] = '\0'; | |
5015 | name = alc; | |
5016 | } | |
5017 | ||
5018 | /* Compute the hash value. */ | |
5019 | ha = bfd_elf_gnu_hash (name); | |
5020 | ||
5021 | /* Store the found hash value in the array for compute_bucket_count, | |
5022 | and also for .dynsym reordering purposes. */ | |
5023 | s->hashcodes[s->nsyms] = ha; | |
5024 | s->hashval[h->dynindx] = ha; | |
5025 | ++s->nsyms; | |
5026 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5027 | s->min_dynindx = h->dynindx; | |
5028 | ||
5029 | if (alc != NULL) | |
5030 | free (alc); | |
5031 | ||
5032 | return TRUE; | |
5033 | } | |
5034 | ||
5035 | /* This function will be called though elf_link_hash_traverse to do | |
5036 | final dynaminc symbol renumbering. */ | |
5037 | ||
5038 | static bfd_boolean | |
5039 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5040 | { | |
5041 | struct collect_gnu_hash_codes *s = data; | |
5042 | unsigned long int bucket; | |
5043 | unsigned long int val; | |
5044 | ||
5045 | if (h->root.type == bfd_link_hash_warning) | |
5046 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5047 | ||
5048 | /* Ignore indirect symbols. */ | |
5049 | if (h->dynindx == -1) | |
5050 | return TRUE; | |
5051 | ||
5052 | /* Ignore also local symbols and undefined symbols. */ | |
5053 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5054 | { | |
5055 | if (h->dynindx >= s->min_dynindx) | |
5056 | h->dynindx = s->local_indx++; | |
5057 | return TRUE; | |
5058 | } | |
5059 | ||
5060 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5061 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5062 | & ((s->maskbits >> s->shift1) - 1); | |
5063 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5064 | s->bitmask[val] | |
5065 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5066 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5067 | if (s->counts[bucket] == 1) | |
5068 | /* Last element terminates the chain. */ | |
5069 | val |= 1; | |
5070 | bfd_put_32 (s->output_bfd, val, | |
5071 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5072 | --s->counts[bucket]; | |
5073 | h->dynindx = s->indx[bucket]++; | |
5074 | return TRUE; | |
5075 | } | |
5076 | ||
5077 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5078 | ||
5079 | bfd_boolean | |
5080 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5081 | { | |
5082 | return !(h->forced_local | |
5083 | || h->root.type == bfd_link_hash_undefined | |
5084 | || h->root.type == bfd_link_hash_undefweak | |
5085 | || ((h->root.type == bfd_link_hash_defined | |
5086 | || h->root.type == bfd_link_hash_defweak) | |
5087 | && h->root.u.def.section->output_section == NULL)); | |
5088 | } | |
5089 | ||
5a580b3a AM |
5090 | /* Array used to determine the number of hash table buckets to use |
5091 | based on the number of symbols there are. If there are fewer than | |
5092 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5093 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5094 | than 32771 buckets. */ | |
5095 | ||
5096 | static const size_t elf_buckets[] = | |
5097 | { | |
5098 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5099 | 16411, 32771, 0 | |
5100 | }; | |
5101 | ||
5102 | /* Compute bucket count for hashing table. We do not use a static set | |
5103 | of possible tables sizes anymore. Instead we determine for all | |
5104 | possible reasonable sizes of the table the outcome (i.e., the | |
5105 | number of collisions etc) and choose the best solution. The | |
5106 | weighting functions are not too simple to allow the table to grow | |
5107 | without bounds. Instead one of the weighting factors is the size. | |
5108 | Therefore the result is always a good payoff between few collisions | |
5109 | (= short chain lengths) and table size. */ | |
5110 | static size_t | |
fdc90cb4 JJ |
5111 | compute_bucket_count (struct bfd_link_info *info, unsigned long int *hashcodes, |
5112 | unsigned long int nsyms, int gnu_hash) | |
5a580b3a AM |
5113 | { |
5114 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; | |
5115 | size_t best_size = 0; | |
5a580b3a AM |
5116 | unsigned long int i; |
5117 | bfd_size_type amt; | |
5118 | ||
5a580b3a AM |
5119 | /* We have a problem here. The following code to optimize the table |
5120 | size requires an integer type with more the 32 bits. If | |
5121 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5122 | #ifdef BFD_HOST_U_64_BIT | |
5123 | if (info->optimize) | |
5124 | { | |
5a580b3a AM |
5125 | size_t minsize; |
5126 | size_t maxsize; | |
5127 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a AM |
5128 | bfd *dynobj = elf_hash_table (info)->dynobj; |
5129 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); | |
fdc90cb4 | 5130 | unsigned long int *counts; |
5a580b3a AM |
5131 | |
5132 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5133 | that the hashing table must at least have NSYMS/4 and at most | |
5134 | 2*NSYMS buckets. */ | |
5135 | minsize = nsyms / 4; | |
5136 | if (minsize == 0) | |
5137 | minsize = 1; | |
5138 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5139 | if (gnu_hash) |
5140 | { | |
5141 | if (minsize < 2) | |
5142 | minsize = 2; | |
5143 | if ((best_size & 31) == 0) | |
5144 | ++best_size; | |
5145 | } | |
5a580b3a AM |
5146 | |
5147 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5148 | since the size could be large. */ | |
5149 | amt = maxsize; | |
5150 | amt *= sizeof (unsigned long int); | |
5151 | counts = bfd_malloc (amt); | |
5152 | if (counts == NULL) | |
fdc90cb4 | 5153 | return 0; |
5a580b3a AM |
5154 | |
5155 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5156 | minimal chain length. The minor criteria is (of course) the size | |
5157 | of the table. */ | |
5158 | for (i = minsize; i < maxsize; ++i) | |
5159 | { | |
5160 | /* Walk through the array of hashcodes and count the collisions. */ | |
5161 | BFD_HOST_U_64_BIT max; | |
5162 | unsigned long int j; | |
5163 | unsigned long int fact; | |
5164 | ||
fdc90cb4 JJ |
5165 | if (gnu_hash && (i & 31) == 0) |
5166 | continue; | |
5167 | ||
5a580b3a AM |
5168 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5169 | ||
5170 | /* Determine how often each hash bucket is used. */ | |
5171 | for (j = 0; j < nsyms; ++j) | |
5172 | ++counts[hashcodes[j] % i]; | |
5173 | ||
5174 | /* For the weight function we need some information about the | |
5175 | pagesize on the target. This is information need not be 100% | |
5176 | accurate. Since this information is not available (so far) we | |
5177 | define it here to a reasonable default value. If it is crucial | |
5178 | to have a better value some day simply define this value. */ | |
5179 | # ifndef BFD_TARGET_PAGESIZE | |
5180 | # define BFD_TARGET_PAGESIZE (4096) | |
5181 | # endif | |
5182 | ||
fdc90cb4 JJ |
5183 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5184 | and the chains. */ | |
5185 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
5186 | |
5187 | # if 1 | |
5188 | /* Variant 1: optimize for short chains. We add the squares | |
5189 | of all the chain lengths (which favors many small chain | |
5190 | over a few long chains). */ | |
5191 | for (j = 0; j < i; ++j) | |
5192 | max += counts[j] * counts[j]; | |
5193 | ||
5194 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 5195 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5196 | max *= fact * fact; |
5197 | # else | |
5198 | /* Variant 2: Optimize a lot more for small table. Here we | |
5199 | also add squares of the size but we also add penalties for | |
5200 | empty slots (the +1 term). */ | |
5201 | for (j = 0; j < i; ++j) | |
5202 | max += (1 + counts[j]) * (1 + counts[j]); | |
5203 | ||
5204 | /* The overall size of the table is considered, but not as | |
5205 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 5206 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5207 | max *= fact; |
5208 | # endif | |
5209 | ||
5210 | /* Compare with current best results. */ | |
5211 | if (max < best_chlen) | |
5212 | { | |
5213 | best_chlen = max; | |
5214 | best_size = i; | |
5215 | } | |
5216 | } | |
5217 | ||
5218 | free (counts); | |
5219 | } | |
5220 | else | |
5221 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
5222 | { | |
5223 | /* This is the fallback solution if no 64bit type is available or if we | |
5224 | are not supposed to spend much time on optimizations. We select the | |
5225 | bucket count using a fixed set of numbers. */ | |
5226 | for (i = 0; elf_buckets[i] != 0; i++) | |
5227 | { | |
5228 | best_size = elf_buckets[i]; | |
fdc90cb4 | 5229 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
5230 | break; |
5231 | } | |
fdc90cb4 JJ |
5232 | if (gnu_hash && best_size < 2) |
5233 | best_size = 2; | |
5a580b3a AM |
5234 | } |
5235 | ||
5a580b3a AM |
5236 | return best_size; |
5237 | } | |
5238 | ||
5239 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
5240 | called by the ELF linker emulation before_allocation routine. We | |
5241 | must set the sizes of the sections before the linker sets the | |
5242 | addresses of the various sections. */ | |
5243 | ||
5244 | bfd_boolean | |
5245 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
5246 | const char *soname, | |
5247 | const char *rpath, | |
5248 | const char *filter_shlib, | |
5249 | const char * const *auxiliary_filters, | |
5250 | struct bfd_link_info *info, | |
5251 | asection **sinterpptr, | |
5252 | struct bfd_elf_version_tree *verdefs) | |
5253 | { | |
5254 | bfd_size_type soname_indx; | |
5255 | bfd *dynobj; | |
5256 | const struct elf_backend_data *bed; | |
5257 | struct elf_assign_sym_version_info asvinfo; | |
5258 | ||
5259 | *sinterpptr = NULL; | |
5260 | ||
5261 | soname_indx = (bfd_size_type) -1; | |
5262 | ||
5263 | if (!is_elf_hash_table (info->hash)) | |
5264 | return TRUE; | |
5265 | ||
6bfdb61b | 5266 | bed = get_elf_backend_data (output_bfd); |
8c37241b | 5267 | elf_tdata (output_bfd)->relro = info->relro; |
5a580b3a AM |
5268 | if (info->execstack) |
5269 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
5270 | else if (info->noexecstack) | |
5271 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
5272 | else | |
5273 | { | |
5274 | bfd *inputobj; | |
5275 | asection *notesec = NULL; | |
5276 | int exec = 0; | |
5277 | ||
5278 | for (inputobj = info->input_bfds; | |
5279 | inputobj; | |
5280 | inputobj = inputobj->link_next) | |
5281 | { | |
5282 | asection *s; | |
5283 | ||
d457dcf6 | 5284 | if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED)) |
5a580b3a AM |
5285 | continue; |
5286 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
5287 | if (s) | |
5288 | { | |
5289 | if (s->flags & SEC_CODE) | |
5290 | exec = PF_X; | |
5291 | notesec = s; | |
5292 | } | |
6bfdb61b | 5293 | else if (bed->default_execstack) |
5a580b3a AM |
5294 | exec = PF_X; |
5295 | } | |
5296 | if (notesec) | |
5297 | { | |
5298 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
5299 | if (exec && info->relocatable | |
5300 | && notesec->output_section != bfd_abs_section_ptr) | |
5301 | notesec->output_section->flags |= SEC_CODE; | |
5302 | } | |
5303 | } | |
5304 | ||
5305 | /* Any syms created from now on start with -1 in | |
5306 | got.refcount/offset and plt.refcount/offset. */ | |
a6aa5195 AM |
5307 | elf_hash_table (info)->init_got_refcount |
5308 | = elf_hash_table (info)->init_got_offset; | |
5309 | elf_hash_table (info)->init_plt_refcount | |
5310 | = elf_hash_table (info)->init_plt_offset; | |
5a580b3a AM |
5311 | |
5312 | /* The backend may have to create some sections regardless of whether | |
5313 | we're dynamic or not. */ | |
5a580b3a AM |
5314 | if (bed->elf_backend_always_size_sections |
5315 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
5316 | return FALSE; | |
5317 | ||
eb3d5f3b JB |
5318 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) |
5319 | return FALSE; | |
5320 | ||
5a580b3a AM |
5321 | dynobj = elf_hash_table (info)->dynobj; |
5322 | ||
5323 | /* If there were no dynamic objects in the link, there is nothing to | |
5324 | do here. */ | |
5325 | if (dynobj == NULL) | |
5326 | return TRUE; | |
5327 | ||
5a580b3a AM |
5328 | if (elf_hash_table (info)->dynamic_sections_created) |
5329 | { | |
5330 | struct elf_info_failed eif; | |
5331 | struct elf_link_hash_entry *h; | |
5332 | asection *dynstr; | |
5333 | struct bfd_elf_version_tree *t; | |
5334 | struct bfd_elf_version_expr *d; | |
046183de | 5335 | asection *s; |
5a580b3a AM |
5336 | bfd_boolean all_defined; |
5337 | ||
5338 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
5339 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
5340 | ||
5341 | if (soname != NULL) | |
5342 | { | |
5343 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5344 | soname, TRUE); | |
5345 | if (soname_indx == (bfd_size_type) -1 | |
5346 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
5347 | return FALSE; | |
5348 | } | |
5349 | ||
5350 | if (info->symbolic) | |
5351 | { | |
5352 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
5353 | return FALSE; | |
5354 | info->flags |= DF_SYMBOLIC; | |
5355 | } | |
5356 | ||
5357 | if (rpath != NULL) | |
5358 | { | |
5359 | bfd_size_type indx; | |
5360 | ||
5361 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
5362 | TRUE); | |
5363 | if (indx == (bfd_size_type) -1 | |
5364 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
5365 | return FALSE; | |
5366 | ||
5367 | if (info->new_dtags) | |
5368 | { | |
5369 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
5370 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
5371 | return FALSE; | |
5372 | } | |
5373 | } | |
5374 | ||
5375 | if (filter_shlib != NULL) | |
5376 | { | |
5377 | bfd_size_type indx; | |
5378 | ||
5379 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5380 | filter_shlib, TRUE); | |
5381 | if (indx == (bfd_size_type) -1 | |
5382 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
5383 | return FALSE; | |
5384 | } | |
5385 | ||
5386 | if (auxiliary_filters != NULL) | |
5387 | { | |
5388 | const char * const *p; | |
5389 | ||
5390 | for (p = auxiliary_filters; *p != NULL; p++) | |
5391 | { | |
5392 | bfd_size_type indx; | |
5393 | ||
5394 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5395 | *p, TRUE); | |
5396 | if (indx == (bfd_size_type) -1 | |
5397 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
5398 | return FALSE; | |
5399 | } | |
5400 | } | |
5401 | ||
5402 | eif.info = info; | |
5403 | eif.verdefs = verdefs; | |
5404 | eif.failed = FALSE; | |
5405 | ||
5406 | /* If we are supposed to export all symbols into the dynamic symbol | |
5407 | table (this is not the normal case), then do so. */ | |
55255dae L |
5408 | if (info->export_dynamic |
5409 | || (info->executable && info->dynamic)) | |
5a580b3a AM |
5410 | { |
5411 | elf_link_hash_traverse (elf_hash_table (info), | |
5412 | _bfd_elf_export_symbol, | |
5413 | &eif); | |
5414 | if (eif.failed) | |
5415 | return FALSE; | |
5416 | } | |
5417 | ||
5418 | /* Make all global versions with definition. */ | |
5419 | for (t = verdefs; t != NULL; t = t->next) | |
5420 | for (d = t->globals.list; d != NULL; d = d->next) | |
5421 | if (!d->symver && d->symbol) | |
5422 | { | |
5423 | const char *verstr, *name; | |
5424 | size_t namelen, verlen, newlen; | |
5425 | char *newname, *p; | |
5426 | struct elf_link_hash_entry *newh; | |
5427 | ||
5428 | name = d->symbol; | |
5429 | namelen = strlen (name); | |
5430 | verstr = t->name; | |
5431 | verlen = strlen (verstr); | |
5432 | newlen = namelen + verlen + 3; | |
5433 | ||
5434 | newname = bfd_malloc (newlen); | |
5435 | if (newname == NULL) | |
5436 | return FALSE; | |
5437 | memcpy (newname, name, namelen); | |
5438 | ||
5439 | /* Check the hidden versioned definition. */ | |
5440 | p = newname + namelen; | |
5441 | *p++ = ELF_VER_CHR; | |
5442 | memcpy (p, verstr, verlen + 1); | |
5443 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5444 | newname, FALSE, FALSE, | |
5445 | FALSE); | |
5446 | if (newh == NULL | |
5447 | || (newh->root.type != bfd_link_hash_defined | |
5448 | && newh->root.type != bfd_link_hash_defweak)) | |
5449 | { | |
5450 | /* Check the default versioned definition. */ | |
5451 | *p++ = ELF_VER_CHR; | |
5452 | memcpy (p, verstr, verlen + 1); | |
5453 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5454 | newname, FALSE, FALSE, | |
5455 | FALSE); | |
5456 | } | |
5457 | free (newname); | |
5458 | ||
5459 | /* Mark this version if there is a definition and it is | |
5460 | not defined in a shared object. */ | |
5461 | if (newh != NULL | |
f5385ebf | 5462 | && !newh->def_dynamic |
5a580b3a AM |
5463 | && (newh->root.type == bfd_link_hash_defined |
5464 | || newh->root.type == bfd_link_hash_defweak)) | |
5465 | d->symver = 1; | |
5466 | } | |
5467 | ||
5468 | /* Attach all the symbols to their version information. */ | |
5469 | asvinfo.output_bfd = output_bfd; | |
5470 | asvinfo.info = info; | |
5471 | asvinfo.verdefs = verdefs; | |
5472 | asvinfo.failed = FALSE; | |
5473 | ||
5474 | elf_link_hash_traverse (elf_hash_table (info), | |
5475 | _bfd_elf_link_assign_sym_version, | |
5476 | &asvinfo); | |
5477 | if (asvinfo.failed) | |
5478 | return FALSE; | |
5479 | ||
5480 | if (!info->allow_undefined_version) | |
5481 | { | |
5482 | /* Check if all global versions have a definition. */ | |
5483 | all_defined = TRUE; | |
5484 | for (t = verdefs; t != NULL; t = t->next) | |
5485 | for (d = t->globals.list; d != NULL; d = d->next) | |
5486 | if (!d->symver && !d->script) | |
5487 | { | |
5488 | (*_bfd_error_handler) | |
5489 | (_("%s: undefined version: %s"), | |
5490 | d->pattern, t->name); | |
5491 | all_defined = FALSE; | |
5492 | } | |
5493 | ||
5494 | if (!all_defined) | |
5495 | { | |
5496 | bfd_set_error (bfd_error_bad_value); | |
5497 | return FALSE; | |
5498 | } | |
5499 | } | |
5500 | ||
5501 | /* Find all symbols which were defined in a dynamic object and make | |
5502 | the backend pick a reasonable value for them. */ | |
5503 | elf_link_hash_traverse (elf_hash_table (info), | |
5504 | _bfd_elf_adjust_dynamic_symbol, | |
5505 | &eif); | |
5506 | if (eif.failed) | |
5507 | return FALSE; | |
5508 | ||
5509 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 5510 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
5511 | now so that we know the final size of the .dynamic section. */ |
5512 | ||
5513 | /* If there are initialization and/or finalization functions to | |
5514 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
5515 | h = (info->init_function | |
5516 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5517 | info->init_function, FALSE, | |
5518 | FALSE, FALSE) | |
5519 | : NULL); | |
5520 | if (h != NULL | |
f5385ebf AM |
5521 | && (h->ref_regular |
5522 | || h->def_regular)) | |
5a580b3a AM |
5523 | { |
5524 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
5525 | return FALSE; | |
5526 | } | |
5527 | h = (info->fini_function | |
5528 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5529 | info->fini_function, FALSE, | |
5530 | FALSE, FALSE) | |
5531 | : NULL); | |
5532 | if (h != NULL | |
f5385ebf AM |
5533 | && (h->ref_regular |
5534 | || h->def_regular)) | |
5a580b3a AM |
5535 | { |
5536 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
5537 | return FALSE; | |
5538 | } | |
5539 | ||
046183de AM |
5540 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); |
5541 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5542 | { |
5543 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
5544 | if (! info->executable) | |
5545 | { | |
5546 | bfd *sub; | |
5547 | asection *o; | |
5548 | ||
5549 | for (sub = info->input_bfds; sub != NULL; | |
5550 | sub = sub->link_next) | |
3fcd97f1 JJ |
5551 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour) |
5552 | for (o = sub->sections; o != NULL; o = o->next) | |
5553 | if (elf_section_data (o)->this_hdr.sh_type | |
5554 | == SHT_PREINIT_ARRAY) | |
5555 | { | |
5556 | (*_bfd_error_handler) | |
5557 | (_("%B: .preinit_array section is not allowed in DSO"), | |
5558 | sub); | |
5559 | break; | |
5560 | } | |
5a580b3a AM |
5561 | |
5562 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5563 | return FALSE; | |
5564 | } | |
5565 | ||
5566 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
5567 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
5568 | return FALSE; | |
5569 | } | |
046183de AM |
5570 | s = bfd_get_section_by_name (output_bfd, ".init_array"); |
5571 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5572 | { |
5573 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
5574 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
5575 | return FALSE; | |
5576 | } | |
046183de AM |
5577 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); |
5578 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5579 | { |
5580 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
5581 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
5582 | return FALSE; | |
5583 | } | |
5584 | ||
5585 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5586 | /* If .dynstr is excluded from the link, we don't want any of | |
5587 | these tags. Strictly, we should be checking each section | |
5588 | individually; This quick check covers for the case where | |
5589 | someone does a /DISCARD/ : { *(*) }. */ | |
5590 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
5591 | { | |
5592 | bfd_size_type strsize; | |
5593 | ||
5594 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
fdc90cb4 JJ |
5595 | if ((info->emit_hash |
5596 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
5597 | || (info->emit_gnu_hash | |
5598 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
5a580b3a AM |
5599 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) |
5600 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
5601 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
5602 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
5603 | bed->s->sizeof_sym)) | |
5604 | return FALSE; | |
5605 | } | |
5606 | } | |
5607 | ||
5608 | /* The backend must work out the sizes of all the other dynamic | |
5609 | sections. */ | |
5610 | if (bed->elf_backend_size_dynamic_sections | |
5611 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
5612 | return FALSE; | |
5613 | ||
5614 | if (elf_hash_table (info)->dynamic_sections_created) | |
5615 | { | |
554220db | 5616 | unsigned long section_sym_count; |
5a580b3a | 5617 | asection *s; |
5a580b3a AM |
5618 | |
5619 | /* Set up the version definition section. */ | |
5620 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
5621 | BFD_ASSERT (s != NULL); | |
5622 | ||
5623 | /* We may have created additional version definitions if we are | |
5624 | just linking a regular application. */ | |
5625 | verdefs = asvinfo.verdefs; | |
5626 | ||
5627 | /* Skip anonymous version tag. */ | |
5628 | if (verdefs != NULL && verdefs->vernum == 0) | |
5629 | verdefs = verdefs->next; | |
5630 | ||
3e3b46e5 | 5631 | if (verdefs == NULL && !info->create_default_symver) |
8423293d | 5632 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5633 | else |
5634 | { | |
5635 | unsigned int cdefs; | |
5636 | bfd_size_type size; | |
5637 | struct bfd_elf_version_tree *t; | |
5638 | bfd_byte *p; | |
5639 | Elf_Internal_Verdef def; | |
5640 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
5641 | struct bfd_link_hash_entry *bh; |
5642 | struct elf_link_hash_entry *h; | |
5643 | const char *name; | |
5a580b3a AM |
5644 | |
5645 | cdefs = 0; | |
5646 | size = 0; | |
5647 | ||
5648 | /* Make space for the base version. */ | |
5649 | size += sizeof (Elf_External_Verdef); | |
5650 | size += sizeof (Elf_External_Verdaux); | |
5651 | ++cdefs; | |
5652 | ||
3e3b46e5 PB |
5653 | /* Make space for the default version. */ |
5654 | if (info->create_default_symver) | |
5655 | { | |
5656 | size += sizeof (Elf_External_Verdef); | |
5657 | ++cdefs; | |
5658 | } | |
5659 | ||
5a580b3a AM |
5660 | for (t = verdefs; t != NULL; t = t->next) |
5661 | { | |
5662 | struct bfd_elf_version_deps *n; | |
5663 | ||
5664 | size += sizeof (Elf_External_Verdef); | |
5665 | size += sizeof (Elf_External_Verdaux); | |
5666 | ++cdefs; | |
5667 | ||
5668 | for (n = t->deps; n != NULL; n = n->next) | |
5669 | size += sizeof (Elf_External_Verdaux); | |
5670 | } | |
5671 | ||
eea6121a AM |
5672 | s->size = size; |
5673 | s->contents = bfd_alloc (output_bfd, s->size); | |
5674 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5675 | return FALSE; |
5676 | ||
5677 | /* Fill in the version definition section. */ | |
5678 | ||
5679 | p = s->contents; | |
5680 | ||
5681 | def.vd_version = VER_DEF_CURRENT; | |
5682 | def.vd_flags = VER_FLG_BASE; | |
5683 | def.vd_ndx = 1; | |
5684 | def.vd_cnt = 1; | |
3e3b46e5 PB |
5685 | if (info->create_default_symver) |
5686 | { | |
5687 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
5688 | def.vd_next = sizeof (Elf_External_Verdef); | |
5689 | } | |
5690 | else | |
5691 | { | |
5692 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5693 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5694 | + sizeof (Elf_External_Verdaux)); | |
5695 | } | |
5a580b3a AM |
5696 | |
5697 | if (soname_indx != (bfd_size_type) -1) | |
5698 | { | |
5699 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5700 | soname_indx); | |
5701 | def.vd_hash = bfd_elf_hash (soname); | |
5702 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 5703 | name = soname; |
5a580b3a AM |
5704 | } |
5705 | else | |
5706 | { | |
5a580b3a AM |
5707 | bfd_size_type indx; |
5708 | ||
06084812 | 5709 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
5710 | def.vd_hash = bfd_elf_hash (name); |
5711 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5712 | name, FALSE); | |
5713 | if (indx == (bfd_size_type) -1) | |
5714 | return FALSE; | |
5715 | defaux.vda_name = indx; | |
5716 | } | |
5717 | defaux.vda_next = 0; | |
5718 | ||
5719 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5720 | (Elf_External_Verdef *) p); | |
5721 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
5722 | if (info->create_default_symver) |
5723 | { | |
5724 | /* Add a symbol representing this version. */ | |
5725 | bh = NULL; | |
5726 | if (! (_bfd_generic_link_add_one_symbol | |
5727 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5728 | 0, NULL, FALSE, | |
5729 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5730 | return FALSE; | |
5731 | h = (struct elf_link_hash_entry *) bh; | |
5732 | h->non_elf = 0; | |
5733 | h->def_regular = 1; | |
5734 | h->type = STT_OBJECT; | |
5735 | h->verinfo.vertree = NULL; | |
5736 | ||
5737 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
5738 | return FALSE; | |
5739 | ||
5740 | /* Create a duplicate of the base version with the same | |
5741 | aux block, but different flags. */ | |
5742 | def.vd_flags = 0; | |
5743 | def.vd_ndx = 2; | |
5744 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5745 | if (verdefs) | |
5746 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5747 | + sizeof (Elf_External_Verdaux)); | |
5748 | else | |
5749 | def.vd_next = 0; | |
5750 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5751 | (Elf_External_Verdef *) p); | |
5752 | p += sizeof (Elf_External_Verdef); | |
5753 | } | |
5a580b3a AM |
5754 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
5755 | (Elf_External_Verdaux *) p); | |
5756 | p += sizeof (Elf_External_Verdaux); | |
5757 | ||
5758 | for (t = verdefs; t != NULL; t = t->next) | |
5759 | { | |
5760 | unsigned int cdeps; | |
5761 | struct bfd_elf_version_deps *n; | |
5a580b3a AM |
5762 | |
5763 | cdeps = 0; | |
5764 | for (n = t->deps; n != NULL; n = n->next) | |
5765 | ++cdeps; | |
5766 | ||
5767 | /* Add a symbol representing this version. */ | |
5768 | bh = NULL; | |
5769 | if (! (_bfd_generic_link_add_one_symbol | |
5770 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5771 | 0, NULL, FALSE, | |
5772 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5773 | return FALSE; | |
5774 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
5775 | h->non_elf = 0; |
5776 | h->def_regular = 1; | |
5a580b3a AM |
5777 | h->type = STT_OBJECT; |
5778 | h->verinfo.vertree = t; | |
5779 | ||
c152c796 | 5780 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
5781 | return FALSE; |
5782 | ||
5783 | def.vd_version = VER_DEF_CURRENT; | |
5784 | def.vd_flags = 0; | |
5785 | if (t->globals.list == NULL | |
5786 | && t->locals.list == NULL | |
5787 | && ! t->used) | |
5788 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 5789 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
5790 | def.vd_cnt = cdeps + 1; |
5791 | def.vd_hash = bfd_elf_hash (t->name); | |
5792 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5793 | def.vd_next = 0; | |
5794 | if (t->next != NULL) | |
5795 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5796 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
5797 | ||
5798 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5799 | (Elf_External_Verdef *) p); | |
5800 | p += sizeof (Elf_External_Verdef); | |
5801 | ||
5802 | defaux.vda_name = h->dynstr_index; | |
5803 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5804 | h->dynstr_index); | |
5805 | defaux.vda_next = 0; | |
5806 | if (t->deps != NULL) | |
5807 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5808 | t->name_indx = defaux.vda_name; | |
5809 | ||
5810 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5811 | (Elf_External_Verdaux *) p); | |
5812 | p += sizeof (Elf_External_Verdaux); | |
5813 | ||
5814 | for (n = t->deps; n != NULL; n = n->next) | |
5815 | { | |
5816 | if (n->version_needed == NULL) | |
5817 | { | |
5818 | /* This can happen if there was an error in the | |
5819 | version script. */ | |
5820 | defaux.vda_name = 0; | |
5821 | } | |
5822 | else | |
5823 | { | |
5824 | defaux.vda_name = n->version_needed->name_indx; | |
5825 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5826 | defaux.vda_name); | |
5827 | } | |
5828 | if (n->next == NULL) | |
5829 | defaux.vda_next = 0; | |
5830 | else | |
5831 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5832 | ||
5833 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5834 | (Elf_External_Verdaux *) p); | |
5835 | p += sizeof (Elf_External_Verdaux); | |
5836 | } | |
5837 | } | |
5838 | ||
5839 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
5840 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
5841 | return FALSE; | |
5842 | ||
5843 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
5844 | } | |
5845 | ||
5846 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
5847 | { | |
5848 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
5849 | return FALSE; | |
5850 | } | |
5851 | else if (info->flags & DF_BIND_NOW) | |
5852 | { | |
5853 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
5854 | return FALSE; | |
5855 | } | |
5856 | ||
5857 | if (info->flags_1) | |
5858 | { | |
5859 | if (info->executable) | |
5860 | info->flags_1 &= ~ (DF_1_INITFIRST | |
5861 | | DF_1_NODELETE | |
5862 | | DF_1_NOOPEN); | |
5863 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
5864 | return FALSE; | |
5865 | } | |
5866 | ||
5867 | /* Work out the size of the version reference section. */ | |
5868 | ||
5869 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
5870 | BFD_ASSERT (s != NULL); | |
5871 | { | |
5872 | struct elf_find_verdep_info sinfo; | |
5873 | ||
5874 | sinfo.output_bfd = output_bfd; | |
5875 | sinfo.info = info; | |
5876 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
5877 | if (sinfo.vers == 0) | |
5878 | sinfo.vers = 1; | |
5879 | sinfo.failed = FALSE; | |
5880 | ||
5881 | elf_link_hash_traverse (elf_hash_table (info), | |
5882 | _bfd_elf_link_find_version_dependencies, | |
5883 | &sinfo); | |
5884 | ||
5885 | if (elf_tdata (output_bfd)->verref == NULL) | |
8423293d | 5886 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5887 | else |
5888 | { | |
5889 | Elf_Internal_Verneed *t; | |
5890 | unsigned int size; | |
5891 | unsigned int crefs; | |
5892 | bfd_byte *p; | |
5893 | ||
5894 | /* Build the version definition section. */ | |
5895 | size = 0; | |
5896 | crefs = 0; | |
5897 | for (t = elf_tdata (output_bfd)->verref; | |
5898 | t != NULL; | |
5899 | t = t->vn_nextref) | |
5900 | { | |
5901 | Elf_Internal_Vernaux *a; | |
5902 | ||
5903 | size += sizeof (Elf_External_Verneed); | |
5904 | ++crefs; | |
5905 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5906 | size += sizeof (Elf_External_Vernaux); | |
5907 | } | |
5908 | ||
eea6121a AM |
5909 | s->size = size; |
5910 | s->contents = bfd_alloc (output_bfd, s->size); | |
5a580b3a AM |
5911 | if (s->contents == NULL) |
5912 | return FALSE; | |
5913 | ||
5914 | p = s->contents; | |
5915 | for (t = elf_tdata (output_bfd)->verref; | |
5916 | t != NULL; | |
5917 | t = t->vn_nextref) | |
5918 | { | |
5919 | unsigned int caux; | |
5920 | Elf_Internal_Vernaux *a; | |
5921 | bfd_size_type indx; | |
5922 | ||
5923 | caux = 0; | |
5924 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5925 | ++caux; | |
5926 | ||
5927 | t->vn_version = VER_NEED_CURRENT; | |
5928 | t->vn_cnt = caux; | |
5929 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5930 | elf_dt_name (t->vn_bfd) != NULL | |
5931 | ? elf_dt_name (t->vn_bfd) | |
06084812 | 5932 | : lbasename (t->vn_bfd->filename), |
5a580b3a AM |
5933 | FALSE); |
5934 | if (indx == (bfd_size_type) -1) | |
5935 | return FALSE; | |
5936 | t->vn_file = indx; | |
5937 | t->vn_aux = sizeof (Elf_External_Verneed); | |
5938 | if (t->vn_nextref == NULL) | |
5939 | t->vn_next = 0; | |
5940 | else | |
5941 | t->vn_next = (sizeof (Elf_External_Verneed) | |
5942 | + caux * sizeof (Elf_External_Vernaux)); | |
5943 | ||
5944 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
5945 | (Elf_External_Verneed *) p); | |
5946 | p += sizeof (Elf_External_Verneed); | |
5947 | ||
5948 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5949 | { | |
5950 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
5951 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5952 | a->vna_nodename, FALSE); | |
5953 | if (indx == (bfd_size_type) -1) | |
5954 | return FALSE; | |
5955 | a->vna_name = indx; | |
5956 | if (a->vna_nextptr == NULL) | |
5957 | a->vna_next = 0; | |
5958 | else | |
5959 | a->vna_next = sizeof (Elf_External_Vernaux); | |
5960 | ||
5961 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
5962 | (Elf_External_Vernaux *) p); | |
5963 | p += sizeof (Elf_External_Vernaux); | |
5964 | } | |
5965 | } | |
5966 | ||
5967 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
5968 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
5969 | return FALSE; | |
5970 | ||
5971 | elf_tdata (output_bfd)->cverrefs = crefs; | |
5972 | } | |
5973 | } | |
5974 | ||
8423293d AM |
5975 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
5976 | && elf_tdata (output_bfd)->cverdefs == 0) | |
5977 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
5978 | §ion_sym_count) == 0) | |
5979 | { | |
5980 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
5981 | s->flags |= SEC_EXCLUDE; | |
5982 | } | |
5983 | } | |
5984 | return TRUE; | |
5985 | } | |
5986 | ||
74541ad4 AM |
5987 | /* Find the first non-excluded output section. We'll use its |
5988 | section symbol for some emitted relocs. */ | |
5989 | void | |
5990 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
5991 | { | |
5992 | asection *s; | |
5993 | ||
5994 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
5995 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
5996 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
5997 | { | |
5998 | elf_hash_table (info)->text_index_section = s; | |
5999 | break; | |
6000 | } | |
6001 | } | |
6002 | ||
6003 | /* Find two non-excluded output sections, one for code, one for data. | |
6004 | We'll use their section symbols for some emitted relocs. */ | |
6005 | void | |
6006 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
6007 | { | |
6008 | asection *s; | |
6009 | ||
6010 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6011 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) | |
6012 | == (SEC_ALLOC | SEC_READONLY)) | |
6013 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6014 | { | |
6015 | elf_hash_table (info)->text_index_section = s; | |
6016 | break; | |
6017 | } | |
6018 | ||
6019 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6020 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) | |
6021 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6022 | { | |
6023 | elf_hash_table (info)->data_index_section = s; | |
6024 | break; | |
6025 | } | |
6026 | ||
6027 | if (elf_hash_table (info)->text_index_section == NULL) | |
6028 | elf_hash_table (info)->text_index_section | |
6029 | = elf_hash_table (info)->data_index_section; | |
6030 | } | |
6031 | ||
8423293d AM |
6032 | bfd_boolean |
6033 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
6034 | { | |
74541ad4 AM |
6035 | const struct elf_backend_data *bed; |
6036 | ||
8423293d AM |
6037 | if (!is_elf_hash_table (info->hash)) |
6038 | return TRUE; | |
6039 | ||
74541ad4 AM |
6040 | bed = get_elf_backend_data (output_bfd); |
6041 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
6042 | ||
8423293d AM |
6043 | if (elf_hash_table (info)->dynamic_sections_created) |
6044 | { | |
6045 | bfd *dynobj; | |
8423293d AM |
6046 | asection *s; |
6047 | bfd_size_type dynsymcount; | |
6048 | unsigned long section_sym_count; | |
8423293d AM |
6049 | unsigned int dtagcount; |
6050 | ||
6051 | dynobj = elf_hash_table (info)->dynobj; | |
6052 | ||
5a580b3a AM |
6053 | /* Assign dynsym indicies. In a shared library we generate a |
6054 | section symbol for each output section, which come first. | |
6055 | Next come all of the back-end allocated local dynamic syms, | |
6056 | followed by the rest of the global symbols. */ | |
6057 | ||
554220db AM |
6058 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, |
6059 | §ion_sym_count); | |
5a580b3a AM |
6060 | |
6061 | /* Work out the size of the symbol version section. */ | |
6062 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
6063 | BFD_ASSERT (s != NULL); | |
8423293d AM |
6064 | if (dynsymcount != 0 |
6065 | && (s->flags & SEC_EXCLUDE) == 0) | |
5a580b3a | 6066 | { |
eea6121a AM |
6067 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
6068 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
6069 | if (s->contents == NULL) |
6070 | return FALSE; | |
6071 | ||
6072 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
6073 | return FALSE; | |
6074 | } | |
6075 | ||
6076 | /* Set the size of the .dynsym and .hash sections. We counted | |
6077 | the number of dynamic symbols in elf_link_add_object_symbols. | |
6078 | We will build the contents of .dynsym and .hash when we build | |
6079 | the final symbol table, because until then we do not know the | |
6080 | correct value to give the symbols. We built the .dynstr | |
6081 | section as we went along in elf_link_add_object_symbols. */ | |
6082 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
6083 | BFD_ASSERT (s != NULL); | |
eea6121a | 6084 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a AM |
6085 | |
6086 | if (dynsymcount != 0) | |
6087 | { | |
554220db AM |
6088 | s->contents = bfd_alloc (output_bfd, s->size); |
6089 | if (s->contents == NULL) | |
6090 | return FALSE; | |
5a580b3a | 6091 | |
554220db AM |
6092 | /* The first entry in .dynsym is a dummy symbol. |
6093 | Clear all the section syms, in case we don't output them all. */ | |
6094 | ++section_sym_count; | |
6095 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a AM |
6096 | } |
6097 | ||
fdc90cb4 JJ |
6098 | elf_hash_table (info)->bucketcount = 0; |
6099 | ||
5a580b3a AM |
6100 | /* Compute the size of the hashing table. As a side effect this |
6101 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
6102 | if (info->emit_hash) |
6103 | { | |
6104 | unsigned long int *hashcodes; | |
6105 | unsigned long int *hashcodesp; | |
6106 | bfd_size_type amt; | |
6107 | unsigned long int nsyms; | |
6108 | size_t bucketcount; | |
6109 | size_t hash_entry_size; | |
6110 | ||
6111 | /* Compute the hash values for all exported symbols. At the same | |
6112 | time store the values in an array so that we could use them for | |
6113 | optimizations. */ | |
6114 | amt = dynsymcount * sizeof (unsigned long int); | |
6115 | hashcodes = bfd_malloc (amt); | |
6116 | if (hashcodes == NULL) | |
6117 | return FALSE; | |
6118 | hashcodesp = hashcodes; | |
5a580b3a | 6119 | |
fdc90cb4 JJ |
6120 | /* Put all hash values in HASHCODES. */ |
6121 | elf_link_hash_traverse (elf_hash_table (info), | |
6122 | elf_collect_hash_codes, &hashcodesp); | |
5a580b3a | 6123 | |
fdc90cb4 JJ |
6124 | nsyms = hashcodesp - hashcodes; |
6125 | bucketcount | |
6126 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
6127 | free (hashcodes); | |
6128 | ||
6129 | if (bucketcount == 0) | |
6130 | return FALSE; | |
5a580b3a | 6131 | |
fdc90cb4 JJ |
6132 | elf_hash_table (info)->bucketcount = bucketcount; |
6133 | ||
6134 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
6135 | BFD_ASSERT (s != NULL); | |
6136 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
6137 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
6138 | s->contents = bfd_zalloc (output_bfd, s->size); | |
6139 | if (s->contents == NULL) | |
6140 | return FALSE; | |
6141 | ||
6142 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
6143 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
6144 | s->contents + hash_entry_size); | |
6145 | } | |
6146 | ||
6147 | if (info->emit_gnu_hash) | |
6148 | { | |
6149 | size_t i, cnt; | |
6150 | unsigned char *contents; | |
6151 | struct collect_gnu_hash_codes cinfo; | |
6152 | bfd_size_type amt; | |
6153 | size_t bucketcount; | |
6154 | ||
6155 | memset (&cinfo, 0, sizeof (cinfo)); | |
6156 | ||
6157 | /* Compute the hash values for all exported symbols. At the same | |
6158 | time store the values in an array so that we could use them for | |
6159 | optimizations. */ | |
6160 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
6161 | cinfo.hashcodes = bfd_malloc (amt); | |
6162 | if (cinfo.hashcodes == NULL) | |
6163 | return FALSE; | |
6164 | ||
6165 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
6166 | cinfo.min_dynindx = -1; | |
6167 | cinfo.output_bfd = output_bfd; | |
6168 | cinfo.bed = bed; | |
6169 | ||
6170 | /* Put all hash values in HASHCODES. */ | |
6171 | elf_link_hash_traverse (elf_hash_table (info), | |
6172 | elf_collect_gnu_hash_codes, &cinfo); | |
6173 | ||
6174 | bucketcount | |
6175 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
6176 | ||
6177 | if (bucketcount == 0) | |
6178 | { | |
6179 | free (cinfo.hashcodes); | |
6180 | return FALSE; | |
6181 | } | |
6182 | ||
6183 | s = bfd_get_section_by_name (dynobj, ".gnu.hash"); | |
6184 | BFD_ASSERT (s != NULL); | |
6185 | ||
6186 | if (cinfo.nsyms == 0) | |
6187 | { | |
6188 | /* Empty .gnu.hash section is special. */ | |
6189 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
6190 | free (cinfo.hashcodes); | |
6191 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
6192 | contents = bfd_zalloc (output_bfd, s->size); | |
6193 | if (contents == NULL) | |
6194 | return FALSE; | |
6195 | s->contents = contents; | |
6196 | /* 1 empty bucket. */ | |
6197 | bfd_put_32 (output_bfd, 1, contents); | |
6198 | /* SYMIDX above the special symbol 0. */ | |
6199 | bfd_put_32 (output_bfd, 1, contents + 4); | |
6200 | /* Just one word for bitmask. */ | |
6201 | bfd_put_32 (output_bfd, 1, contents + 8); | |
6202 | /* Only hash fn bloom filter. */ | |
6203 | bfd_put_32 (output_bfd, 0, contents + 12); | |
6204 | /* No hashes are valid - empty bitmask. */ | |
6205 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
6206 | /* No hashes in the only bucket. */ | |
6207 | bfd_put_32 (output_bfd, 0, | |
6208 | contents + 16 + bed->s->arch_size / 8); | |
6209 | } | |
6210 | else | |
6211 | { | |
fdc90cb4 | 6212 | unsigned long int maskwords, maskbitslog2; |
0b33793d | 6213 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 JJ |
6214 | |
6215 | maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1; | |
6216 | if (maskbitslog2 < 3) | |
6217 | maskbitslog2 = 5; | |
6218 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
6219 | maskbitslog2 = maskbitslog2 + 3; | |
6220 | else | |
6221 | maskbitslog2 = maskbitslog2 + 2; | |
6222 | if (bed->s->arch_size == 64) | |
6223 | { | |
6224 | if (maskbitslog2 == 5) | |
6225 | maskbitslog2 = 6; | |
6226 | cinfo.shift1 = 6; | |
6227 | } | |
6228 | else | |
6229 | cinfo.shift1 = 5; | |
6230 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 6231 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
6232 | cinfo.maskbits = 1 << maskbitslog2; |
6233 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
6234 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
6235 | amt += maskwords * sizeof (bfd_vma); | |
6236 | cinfo.bitmask = bfd_malloc (amt); | |
6237 | if (cinfo.bitmask == NULL) | |
6238 | { | |
6239 | free (cinfo.hashcodes); | |
6240 | return FALSE; | |
6241 | } | |
6242 | ||
6243 | cinfo.counts = (void *) (cinfo.bitmask + maskwords); | |
6244 | cinfo.indx = cinfo.counts + bucketcount; | |
6245 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
6246 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
6247 | ||
6248 | /* Determine how often each hash bucket is used. */ | |
6249 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
6250 | for (i = 0; i < cinfo.nsyms; ++i) | |
6251 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
6252 | ||
6253 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
6254 | if (cinfo.counts[i] != 0) | |
6255 | { | |
6256 | cinfo.indx[i] = cnt; | |
6257 | cnt += cinfo.counts[i]; | |
6258 | } | |
6259 | BFD_ASSERT (cnt == dynsymcount); | |
6260 | cinfo.bucketcount = bucketcount; | |
6261 | cinfo.local_indx = cinfo.min_dynindx; | |
6262 | ||
6263 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
6264 | s->size += cinfo.maskbits / 8; | |
6265 | contents = bfd_zalloc (output_bfd, s->size); | |
6266 | if (contents == NULL) | |
6267 | { | |
6268 | free (cinfo.bitmask); | |
6269 | free (cinfo.hashcodes); | |
6270 | return FALSE; | |
6271 | } | |
6272 | ||
6273 | s->contents = contents; | |
6274 | bfd_put_32 (output_bfd, bucketcount, contents); | |
6275 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
6276 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
6277 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
6278 | contents += 16 + cinfo.maskbits / 8; | |
6279 | ||
6280 | for (i = 0; i < bucketcount; ++i) | |
6281 | { | |
6282 | if (cinfo.counts[i] == 0) | |
6283 | bfd_put_32 (output_bfd, 0, contents); | |
6284 | else | |
6285 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
6286 | contents += 4; | |
6287 | } | |
6288 | ||
6289 | cinfo.contents = contents; | |
6290 | ||
6291 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
6292 | elf_link_hash_traverse (elf_hash_table (info), | |
6293 | elf_renumber_gnu_hash_syms, &cinfo); | |
6294 | ||
6295 | contents = s->contents + 16; | |
6296 | for (i = 0; i < maskwords; ++i) | |
6297 | { | |
6298 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
6299 | contents); | |
6300 | contents += bed->s->arch_size / 8; | |
6301 | } | |
6302 | ||
6303 | free (cinfo.bitmask); | |
6304 | free (cinfo.hashcodes); | |
6305 | } | |
6306 | } | |
5a580b3a AM |
6307 | |
6308 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
6309 | BFD_ASSERT (s != NULL); | |
6310 | ||
4ad4eba5 | 6311 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 6312 | |
eea6121a | 6313 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
6314 | |
6315 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
6316 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
6317 | return FALSE; | |
6318 | } | |
6319 | ||
6320 | return TRUE; | |
6321 | } | |
c152c796 AM |
6322 | |
6323 | /* Final phase of ELF linker. */ | |
6324 | ||
6325 | /* A structure we use to avoid passing large numbers of arguments. */ | |
6326 | ||
6327 | struct elf_final_link_info | |
6328 | { | |
6329 | /* General link information. */ | |
6330 | struct bfd_link_info *info; | |
6331 | /* Output BFD. */ | |
6332 | bfd *output_bfd; | |
6333 | /* Symbol string table. */ | |
6334 | struct bfd_strtab_hash *symstrtab; | |
6335 | /* .dynsym section. */ | |
6336 | asection *dynsym_sec; | |
6337 | /* .hash section. */ | |
6338 | asection *hash_sec; | |
6339 | /* symbol version section (.gnu.version). */ | |
6340 | asection *symver_sec; | |
6341 | /* Buffer large enough to hold contents of any section. */ | |
6342 | bfd_byte *contents; | |
6343 | /* Buffer large enough to hold external relocs of any section. */ | |
6344 | void *external_relocs; | |
6345 | /* Buffer large enough to hold internal relocs of any section. */ | |
6346 | Elf_Internal_Rela *internal_relocs; | |
6347 | /* Buffer large enough to hold external local symbols of any input | |
6348 | BFD. */ | |
6349 | bfd_byte *external_syms; | |
6350 | /* And a buffer for symbol section indices. */ | |
6351 | Elf_External_Sym_Shndx *locsym_shndx; | |
6352 | /* Buffer large enough to hold internal local symbols of any input | |
6353 | BFD. */ | |
6354 | Elf_Internal_Sym *internal_syms; | |
6355 | /* Array large enough to hold a symbol index for each local symbol | |
6356 | of any input BFD. */ | |
6357 | long *indices; | |
6358 | /* Array large enough to hold a section pointer for each local | |
6359 | symbol of any input BFD. */ | |
6360 | asection **sections; | |
6361 | /* Buffer to hold swapped out symbols. */ | |
6362 | bfd_byte *symbuf; | |
6363 | /* And one for symbol section indices. */ | |
6364 | Elf_External_Sym_Shndx *symshndxbuf; | |
6365 | /* Number of swapped out symbols in buffer. */ | |
6366 | size_t symbuf_count; | |
6367 | /* Number of symbols which fit in symbuf. */ | |
6368 | size_t symbuf_size; | |
6369 | /* And same for symshndxbuf. */ | |
6370 | size_t shndxbuf_size; | |
6371 | }; | |
6372 | ||
6373 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
6374 | ||
6375 | struct elf_outext_info | |
6376 | { | |
6377 | bfd_boolean failed; | |
6378 | bfd_boolean localsyms; | |
6379 | struct elf_final_link_info *finfo; | |
6380 | }; | |
6381 | ||
d9352518 DB |
6382 | |
6383 | /* Support for evaluating a complex relocation. | |
6384 | ||
6385 | Complex relocations are generalized, self-describing relocations. The | |
6386 | implementation of them consists of two parts: complex symbols, and the | |
6387 | relocations themselves. | |
6388 | ||
6389 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
6390 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
6391 | information (start bit, end bit, word width, etc) into the addend. This | |
6392 | information is extracted from CGEN-generated operand tables within gas. | |
6393 | ||
6394 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
6395 | internal) representing prefix-notation expressions, including but not | |
6396 | limited to those sorts of expressions normally encoded as addends in the | |
6397 | addend field. The symbol mangling format is: | |
6398 | ||
6399 | <node> := <literal> | |
6400 | | <unary-operator> ':' <node> | |
6401 | | <binary-operator> ':' <node> ':' <node> | |
6402 | ; | |
6403 | ||
6404 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
6405 | | 'S' <digits=N> ':' <N character section name> | |
6406 | | '#' <hexdigits> | |
6407 | ; | |
6408 | ||
6409 | <binary-operator> := as in C | |
6410 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
6411 | ||
6412 | static void | |
6413 | set_symbol_value (bfd * bfd_with_globals, | |
6414 | struct elf_final_link_info * finfo, | |
6415 | int symidx, | |
6416 | bfd_vma val) | |
6417 | { | |
6418 | bfd_boolean is_local; | |
6419 | Elf_Internal_Sym * sym; | |
6420 | struct elf_link_hash_entry ** sym_hashes; | |
6421 | struct elf_link_hash_entry * h; | |
6422 | ||
6423 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
6424 | sym = finfo->internal_syms + symidx; | |
6425 | is_local = ELF_ST_BIND(sym->st_info) == STB_LOCAL; | |
6426 | ||
6427 | if (is_local) | |
6428 | { | |
6429 | /* It is a local symbol: move it to the | |
6430 | "absolute" section and give it a value. */ | |
6431 | sym->st_shndx = SHN_ABS; | |
6432 | sym->st_value = val; | |
6433 | } | |
6434 | else | |
6435 | { | |
6436 | /* It is a global symbol: set its link type | |
6437 | to "defined" and give it a value. */ | |
6438 | h = sym_hashes [symidx]; | |
6439 | while (h->root.type == bfd_link_hash_indirect | |
6440 | || h->root.type == bfd_link_hash_warning) | |
6441 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6442 | h->root.type = bfd_link_hash_defined; | |
6443 | h->root.u.def.value = val; | |
6444 | h->root.u.def.section = bfd_abs_section_ptr; | |
6445 | } | |
6446 | } | |
6447 | ||
6448 | static bfd_boolean | |
6449 | resolve_symbol (const char * name, | |
6450 | bfd * input_bfd, | |
6451 | struct elf_final_link_info * finfo, | |
6452 | bfd_vma * result, | |
6453 | size_t locsymcount) | |
6454 | { | |
6455 | Elf_Internal_Sym * sym; | |
6456 | struct bfd_link_hash_entry * global_entry; | |
6457 | const char * candidate = NULL; | |
6458 | Elf_Internal_Shdr * symtab_hdr; | |
6459 | asection * sec = NULL; | |
6460 | size_t i; | |
6461 | ||
6462 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; | |
6463 | ||
6464 | for (i = 0; i < locsymcount; ++ i) | |
6465 | { | |
6466 | sym = finfo->internal_syms + i; | |
6467 | sec = finfo->sections [i]; | |
6468 | ||
6469 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
6470 | continue; | |
6471 | ||
6472 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
6473 | symtab_hdr->sh_link, | |
6474 | sym->st_name); | |
6475 | #ifdef DEBUG | |
6476 | printf ("Comparing string: '%s' vs. '%s' = 0x%x\n", | |
6477 | name, candidate, (unsigned int)sym->st_value); | |
6478 | #endif | |
6479 | if (candidate && strcmp (candidate, name) == 0) | |
6480 | { | |
6481 | * result = sym->st_value; | |
6482 | ||
6483 | if (sym->st_shndx > SHN_UNDEF && | |
6484 | sym->st_shndx < SHN_LORESERVE) | |
6485 | { | |
6486 | #ifdef DEBUG | |
6487 | printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n", | |
6488 | sec->output_section->name, | |
6489 | (unsigned int)sec->output_section->vma, | |
6490 | (unsigned int)sec->output_offset); | |
6491 | #endif | |
6492 | * result += sec->output_offset + sec->output_section->vma; | |
6493 | } | |
6494 | #ifdef DEBUG | |
6495 | printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result); | |
6496 | #endif | |
6497 | return TRUE; | |
6498 | } | |
6499 | } | |
6500 | ||
6501 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
6502 | global_entry = bfd_link_hash_lookup (finfo->info->hash, name, FALSE, FALSE, TRUE); | |
6503 | if (!global_entry) | |
6504 | return FALSE; | |
6505 | ||
6506 | if (global_entry->type == bfd_link_hash_defined | |
6507 | || global_entry->type == bfd_link_hash_defweak) | |
6508 | { | |
6509 | * result = global_entry->u.def.value | |
6510 | + global_entry->u.def.section->output_section->vma | |
6511 | + global_entry->u.def.section->output_offset; | |
6512 | #ifdef DEBUG | |
6513 | printf ("Found GLOBAL symbol '%s' with value %8.8x\n", | |
6514 | global_entry->root.string, (unsigned int)*result); | |
6515 | #endif | |
6516 | return TRUE; | |
6517 | } | |
6518 | ||
6519 | if (global_entry->type == bfd_link_hash_common) | |
6520 | { | |
6521 | *result = global_entry->u.def.value + | |
6522 | bfd_com_section_ptr->output_section->vma + | |
6523 | bfd_com_section_ptr->output_offset; | |
6524 | #ifdef DEBUG | |
6525 | printf ("Found COMMON symbol '%s' with value %8.8x\n", | |
6526 | global_entry->root.string, (unsigned int)*result); | |
6527 | #endif | |
6528 | return TRUE; | |
6529 | } | |
6530 | ||
6531 | return FALSE; | |
6532 | } | |
6533 | ||
6534 | static bfd_boolean | |
6535 | resolve_section (const char * name, | |
6536 | asection * sections, | |
6537 | bfd_vma * result) | |
6538 | { | |
6539 | asection * curr; | |
6540 | unsigned int len; | |
6541 | ||
6542 | for (curr = sections; curr; curr = curr->next) | |
6543 | if (strcmp (curr->name, name) == 0) | |
6544 | { | |
6545 | *result = curr->vma; | |
6546 | return TRUE; | |
6547 | } | |
6548 | ||
6549 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
6550 | for (curr = sections; curr; curr = curr->next) | |
6551 | { | |
6552 | len = strlen (curr->name); | |
6553 | if (len > strlen (name)) | |
6554 | continue; | |
6555 | ||
6556 | if (strncmp (curr->name, name, len) == 0) | |
6557 | { | |
6558 | if (strncmp (".end", name + len, 4) == 0) | |
6559 | { | |
6560 | *result = curr->vma + curr->size; | |
6561 | return TRUE; | |
6562 | } | |
6563 | ||
6564 | /* Insert more pseudo-section names here, if you like. */ | |
6565 | } | |
6566 | } | |
6567 | ||
6568 | return FALSE; | |
6569 | } | |
6570 | ||
6571 | static void | |
6572 | undefined_reference (const char * reftype, | |
6573 | const char * name) | |
6574 | { | |
6575 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype, name); | |
6576 | } | |
6577 | ||
6578 | static bfd_boolean | |
6579 | eval_symbol (bfd_vma * result, | |
6580 | char * sym, | |
6581 | char ** advanced, | |
6582 | bfd * input_bfd, | |
6583 | struct elf_final_link_info * finfo, | |
6584 | bfd_vma addr, | |
6585 | bfd_vma section_offset, | |
6586 | size_t locsymcount, | |
6587 | int signed_p) | |
6588 | { | |
6589 | int len; | |
6590 | int symlen; | |
6591 | bfd_vma a; | |
6592 | bfd_vma b; | |
6593 | const int bufsz = 4096; | |
6594 | char symbuf [bufsz]; | |
6595 | const char * symend; | |
6596 | bfd_boolean symbol_is_section = FALSE; | |
6597 | ||
6598 | len = strlen (sym); | |
6599 | symend = sym + len; | |
6600 | ||
6601 | if (len < 1 || len > bufsz) | |
6602 | { | |
6603 | bfd_set_error (bfd_error_invalid_operation); | |
6604 | return FALSE; | |
6605 | } | |
6606 | ||
6607 | switch (* sym) | |
6608 | { | |
6609 | case '.': | |
6610 | * result = addr + section_offset; | |
6611 | * advanced = sym + 1; | |
6612 | return TRUE; | |
6613 | ||
6614 | case '#': | |
6615 | ++ sym; | |
6616 | * result = strtoul (sym, advanced, 16); | |
6617 | return TRUE; | |
6618 | ||
6619 | case 'S': | |
6620 | symbol_is_section = TRUE; | |
6621 | case 's': | |
6622 | ++ sym; | |
6623 | symlen = strtol (sym, &sym, 10); | |
6624 | ++ sym; /* Skip the trailing ':'. */ | |
6625 | ||
6626 | if ((symend < sym) || ((symlen + 1) > bufsz)) | |
6627 | { | |
6628 | bfd_set_error (bfd_error_invalid_operation); | |
6629 | return FALSE; | |
6630 | } | |
6631 | ||
6632 | memcpy (symbuf, sym, symlen); | |
6633 | symbuf [symlen] = '\0'; | |
6634 | * advanced = sym + symlen; | |
6635 | ||
6636 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
6637 | the symbol as a section, or vice-versa. so we're pretty liberal in our | |
6638 | interpretation here; section means "try section first", not "must be a | |
6639 | section", and likewise with symbol. */ | |
6640 | ||
6641 | if (symbol_is_section) | |
6642 | { | |
6643 | if ((resolve_section (symbuf, finfo->output_bfd->sections, result) != TRUE) | |
6644 | && (resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE)) | |
6645 | { | |
6646 | undefined_reference ("section", symbuf); | |
6647 | return FALSE; | |
6648 | } | |
6649 | } | |
6650 | else | |
6651 | { | |
6652 | if ((resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE) | |
6653 | && (resolve_section (symbuf, finfo->output_bfd->sections, | |
6654 | result) != TRUE)) | |
6655 | { | |
6656 | undefined_reference ("symbol", symbuf); | |
6657 | return FALSE; | |
6658 | } | |
6659 | } | |
6660 | ||
6661 | return TRUE; | |
6662 | ||
6663 | /* All that remains are operators. */ | |
6664 | ||
6665 | #define UNARY_OP(op) \ | |
6666 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
6667 | { \ | |
6668 | sym += strlen (#op); \ | |
6669 | if (* sym == ':') \ | |
6670 | ++ sym; \ | |
6671 | if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \ | |
6672 | section_offset, locsymcount, signed_p) \ | |
6673 | != TRUE) \ | |
6674 | return FALSE; \ | |
6675 | if (signed_p) \ | |
6676 | * result = op ((signed)a); \ | |
6677 | else \ | |
6678 | * result = op a; \ | |
6679 | * advanced = sym; \ | |
6680 | return TRUE; \ | |
6681 | } | |
6682 | ||
6683 | #define BINARY_OP(op) \ | |
6684 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
6685 | { \ | |
6686 | sym += strlen (#op); \ | |
6687 | if (* sym == ':') \ | |
6688 | ++ sym; \ | |
6689 | if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \ | |
6690 | section_offset, locsymcount, signed_p) \ | |
6691 | != TRUE) \ | |
6692 | return FALSE; \ | |
6693 | ++ sym; \ | |
6694 | if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \ | |
6695 | section_offset, locsymcount, signed_p) \ | |
6696 | != TRUE) \ | |
6697 | return FALSE; \ | |
6698 | if (signed_p) \ | |
6699 | * result = ((signed) a) op ((signed) b); \ | |
6700 | else \ | |
6701 | * result = a op b; \ | |
6702 | * advanced = sym; \ | |
6703 | return TRUE; \ | |
6704 | } | |
6705 | ||
6706 | default: | |
6707 | UNARY_OP (0-); | |
6708 | BINARY_OP (<<); | |
6709 | BINARY_OP (>>); | |
6710 | BINARY_OP (==); | |
6711 | BINARY_OP (!=); | |
6712 | BINARY_OP (<=); | |
6713 | BINARY_OP (>=); | |
6714 | BINARY_OP (&&); | |
6715 | BINARY_OP (||); | |
6716 | UNARY_OP (~); | |
6717 | UNARY_OP (!); | |
6718 | BINARY_OP (*); | |
6719 | BINARY_OP (/); | |
6720 | BINARY_OP (%); | |
6721 | BINARY_OP (^); | |
6722 | BINARY_OP (|); | |
6723 | BINARY_OP (&); | |
6724 | BINARY_OP (+); | |
6725 | BINARY_OP (-); | |
6726 | BINARY_OP (<); | |
6727 | BINARY_OP (>); | |
6728 | #undef UNARY_OP | |
6729 | #undef BINARY_OP | |
6730 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
6731 | bfd_set_error (bfd_error_invalid_operation); | |
6732 | return FALSE; | |
6733 | } | |
6734 | } | |
6735 | ||
6736 | /* Entry point to evaluator, called from elf_link_input_bfd. */ | |
6737 | ||
6738 | static bfd_boolean | |
6739 | evaluate_complex_relocation_symbols (bfd * input_bfd, | |
6740 | struct elf_final_link_info * finfo, | |
6741 | size_t locsymcount) | |
6742 | { | |
6743 | const struct elf_backend_data * bed; | |
6744 | Elf_Internal_Shdr * symtab_hdr; | |
6745 | struct elf_link_hash_entry ** sym_hashes; | |
6746 | asection * reloc_sec; | |
6747 | bfd_boolean result = TRUE; | |
6748 | ||
6749 | /* For each section, we're going to check and see if it has any | |
6750 | complex relocations, and we're going to evaluate any of them | |
6751 | we can. */ | |
6752 | ||
6753 | if (finfo->info->relocatable) | |
6754 | return TRUE; | |
6755 | ||
6756 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; | |
6757 | sym_hashes = elf_sym_hashes (input_bfd); | |
6758 | bed = get_elf_backend_data (input_bfd); | |
6759 | ||
6760 | for (reloc_sec = input_bfd->sections; reloc_sec; reloc_sec = reloc_sec->next) | |
6761 | { | |
6762 | Elf_Internal_Rela * internal_relocs; | |
6763 | unsigned long i; | |
6764 | ||
6765 | /* This section was omitted from the link. */ | |
6766 | if (! reloc_sec->linker_mark) | |
6767 | continue; | |
6768 | ||
6769 | /* Only process sections containing relocs. */ | |
6770 | if ((reloc_sec->flags & SEC_RELOC) == 0) | |
6771 | continue; | |
6772 | ||
6773 | if (reloc_sec->reloc_count == 0) | |
6774 | continue; | |
6775 | ||
6776 | /* Read in the relocs for this section. */ | |
6777 | internal_relocs | |
6778 | = _bfd_elf_link_read_relocs (input_bfd, reloc_sec, NULL, | |
6779 | (Elf_Internal_Rela *) NULL, | |
6780 | FALSE); | |
6781 | if (internal_relocs == NULL) | |
6782 | continue; | |
6783 | ||
6784 | for (i = reloc_sec->reloc_count; i--;) | |
6785 | { | |
6786 | Elf_Internal_Rela * rel; | |
6787 | char * sym_name; | |
947844a3 | 6788 | bfd_vma index; |
d9352518 DB |
6789 | Elf_Internal_Sym * sym; |
6790 | bfd_vma result; | |
6791 | bfd_vma section_offset; | |
6792 | bfd_vma addr; | |
6793 | int signed_p = 0; | |
6794 | ||
6795 | rel = internal_relocs + i; | |
6796 | section_offset = reloc_sec->output_section->vma | |
6797 | + reloc_sec->output_offset; | |
6798 | addr = rel->r_offset; | |
6799 | ||
6800 | index = ELF32_R_SYM (rel->r_info); | |
6801 | if (bed->s->arch_size == 64) | |
6802 | index >>= 24; | |
4aac632e AM |
6803 | |
6804 | if (index == STN_UNDEF) | |
6805 | continue; | |
6806 | ||
d9352518 DB |
6807 | if (index < locsymcount) |
6808 | { | |
6809 | /* The symbol is local. */ | |
6810 | sym = finfo->internal_syms + index; | |
6811 | ||
6812 | /* We're only processing STT_RELC or STT_SRELC type symbols. */ | |
6813 | if ((ELF_ST_TYPE (sym->st_info) != STT_RELC) && | |
6814 | (ELF_ST_TYPE (sym->st_info) != STT_SRELC)) | |
6815 | continue; | |
6816 | ||
6817 | sym_name = bfd_elf_string_from_elf_section | |
6818 | (input_bfd, symtab_hdr->sh_link, sym->st_name); | |
6819 | ||
6820 | signed_p = (ELF_ST_TYPE (sym->st_info) == STT_SRELC); | |
6821 | } | |
6822 | else | |
6823 | { | |
6824 | /* The symbol is global. */ | |
6825 | struct elf_link_hash_entry * h; | |
6826 | ||
6827 | if (elf_bad_symtab (input_bfd)) | |
6828 | continue; | |
6829 | ||
6830 | h = sym_hashes [index - locsymcount]; | |
6831 | while ( h->root.type == bfd_link_hash_indirect | |
6832 | || h->root.type == bfd_link_hash_warning) | |
6833 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6834 | ||
6835 | if (h->type != STT_RELC && h->type != STT_SRELC) | |
6836 | continue; | |
6837 | ||
6838 | signed_p = (h->type == STT_SRELC); | |
6839 | sym_name = (char *) h->root.root.string; | |
6840 | } | |
6841 | #ifdef DEBUG | |
6842 | printf ("Encountered a complex symbol!"); | |
6843 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
6844 | input_bfd->filename, reloc_sec->name, i); | |
6845 | printf (" symbol: idx %8.8lx, name %s\n", | |
6846 | index, sym_name); | |
6847 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
6848 | rel->r_info, addr); | |
6849 | printf (" Evaluating '%s' ...\n ", sym_name); | |
6850 | #endif | |
6851 | if (eval_symbol (& result, sym_name, & sym_name, input_bfd, | |
6852 | finfo, addr, section_offset, locsymcount, | |
6853 | signed_p)) | |
6854 | /* Symbol evaluated OK. Update to absolute value. */ | |
6855 | set_symbol_value (input_bfd, finfo, index, result); | |
6856 | ||
6857 | else | |
6858 | result = FALSE; | |
6859 | } | |
6860 | ||
6861 | if (internal_relocs != elf_section_data (reloc_sec)->relocs) | |
6862 | free (internal_relocs); | |
6863 | } | |
6864 | ||
6865 | /* If nothing went wrong, then we adjusted | |
6866 | everything we wanted to adjust. */ | |
6867 | return result; | |
6868 | } | |
6869 | ||
6870 | static void | |
6871 | put_value (bfd_vma size, | |
6872 | unsigned long chunksz, | |
6873 | bfd * input_bfd, | |
6874 | bfd_vma x, | |
6875 | bfd_byte * location) | |
6876 | { | |
6877 | location += (size - chunksz); | |
6878 | ||
6879 | for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8)) | |
6880 | { | |
6881 | switch (chunksz) | |
6882 | { | |
6883 | default: | |
6884 | case 0: | |
6885 | abort (); | |
6886 | case 1: | |
6887 | bfd_put_8 (input_bfd, x, location); | |
6888 | break; | |
6889 | case 2: | |
6890 | bfd_put_16 (input_bfd, x, location); | |
6891 | break; | |
6892 | case 4: | |
6893 | bfd_put_32 (input_bfd, x, location); | |
6894 | break; | |
6895 | case 8: | |
6896 | #ifdef BFD64 | |
6897 | bfd_put_64 (input_bfd, x, location); | |
6898 | #else | |
6899 | abort (); | |
6900 | #endif | |
6901 | break; | |
6902 | } | |
6903 | } | |
6904 | } | |
6905 | ||
6906 | static bfd_vma | |
6907 | get_value (bfd_vma size, | |
6908 | unsigned long chunksz, | |
6909 | bfd * input_bfd, | |
6910 | bfd_byte * location) | |
6911 | { | |
6912 | bfd_vma x = 0; | |
6913 | ||
6914 | for (; size; size -= chunksz, location += chunksz) | |
6915 | { | |
6916 | switch (chunksz) | |
6917 | { | |
6918 | default: | |
6919 | case 0: | |
6920 | abort (); | |
6921 | case 1: | |
6922 | x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location); | |
6923 | break; | |
6924 | case 2: | |
6925 | x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location); | |
6926 | break; | |
6927 | case 4: | |
6928 | x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location); | |
6929 | break; | |
6930 | case 8: | |
6931 | #ifdef BFD64 | |
6932 | x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location); | |
6933 | #else | |
6934 | abort (); | |
6935 | #endif | |
6936 | break; | |
6937 | } | |
6938 | } | |
6939 | return x; | |
6940 | } | |
6941 | ||
6942 | static void | |
6943 | decode_complex_addend | |
6944 | (unsigned long * start, /* in bits */ | |
6945 | unsigned long * oplen, /* in bits */ | |
6946 | unsigned long * len, /* in bits */ | |
6947 | unsigned long * wordsz, /* in bytes */ | |
6948 | unsigned long * chunksz, /* in bytes */ | |
6949 | unsigned long * lsb0_p, | |
6950 | unsigned long * signed_p, | |
6951 | unsigned long * trunc_p, | |
6952 | unsigned long encoded) | |
6953 | { | |
6954 | * start = encoded & 0x3F; | |
6955 | * len = (encoded >> 6) & 0x3F; | |
6956 | * oplen = (encoded >> 12) & 0x3F; | |
6957 | * wordsz = (encoded >> 18) & 0xF; | |
6958 | * chunksz = (encoded >> 22) & 0xF; | |
6959 | * lsb0_p = (encoded >> 27) & 1; | |
6960 | * signed_p = (encoded >> 28) & 1; | |
6961 | * trunc_p = (encoded >> 29) & 1; | |
6962 | } | |
6963 | ||
6964 | void | |
6965 | bfd_elf_perform_complex_relocation | |
6966 | (bfd * output_bfd ATTRIBUTE_UNUSED, | |
6967 | struct bfd_link_info * info, | |
6968 | bfd * input_bfd, | |
6969 | asection * input_section, | |
6970 | bfd_byte * contents, | |
6971 | Elf_Internal_Rela * rel, | |
6972 | Elf_Internal_Sym * local_syms, | |
6973 | asection ** local_sections) | |
6974 | { | |
6975 | const struct elf_backend_data * bed; | |
6976 | Elf_Internal_Shdr * symtab_hdr; | |
6977 | asection * sec; | |
6978 | bfd_vma relocation = 0, shift, x; | |
947844a3 | 6979 | bfd_vma r_symndx; |
d9352518 DB |
6980 | bfd_vma mask; |
6981 | unsigned long start, oplen, len, wordsz, | |
6982 | chunksz, lsb0_p, signed_p, trunc_p; | |
6983 | ||
6984 | /* Perform this reloc, since it is complex. | |
6985 | (this is not to say that it necessarily refers to a complex | |
6986 | symbol; merely that it is a self-describing CGEN based reloc. | |
6987 | i.e. the addend has the complete reloc information (bit start, end, | |
6988 | word size, etc) encoded within it.). */ | |
6989 | r_symndx = ELF32_R_SYM (rel->r_info); | |
6990 | bed = get_elf_backend_data (input_bfd); | |
6991 | if (bed->s->arch_size == 64) | |
6992 | r_symndx >>= 24; | |
6993 | ||
6994 | #ifdef DEBUG | |
6995 | printf ("Performing complex relocation %ld...\n", r_symndx); | |
6996 | #endif | |
6997 | ||
6998 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; | |
6999 | if (r_symndx < symtab_hdr->sh_info) | |
7000 | { | |
7001 | /* The symbol is local. */ | |
7002 | Elf_Internal_Sym * sym; | |
7003 | ||
7004 | sym = local_syms + r_symndx; | |
7005 | sec = local_sections [r_symndx]; | |
7006 | relocation = sym->st_value; | |
7007 | if (sym->st_shndx > SHN_UNDEF && | |
7008 | sym->st_shndx < SHN_LORESERVE) | |
7009 | relocation += (sec->output_offset + | |
7010 | sec->output_section->vma); | |
7011 | } | |
7012 | else | |
7013 | { | |
7014 | /* The symbol is global. */ | |
7015 | struct elf_link_hash_entry **sym_hashes; | |
7016 | struct elf_link_hash_entry * h; | |
7017 | ||
7018 | sym_hashes = elf_sym_hashes (input_bfd); | |
7019 | h = sym_hashes [r_symndx]; | |
7020 | ||
7021 | while (h->root.type == bfd_link_hash_indirect | |
7022 | || h->root.type == bfd_link_hash_warning) | |
7023 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7024 | ||
7025 | if (h->root.type == bfd_link_hash_defined | |
7026 | || h->root.type == bfd_link_hash_defweak) | |
7027 | { | |
7028 | sec = h->root.u.def.section; | |
7029 | relocation = h->root.u.def.value; | |
7030 | ||
7031 | if (! bfd_is_abs_section (sec)) | |
7032 | relocation += (sec->output_section->vma | |
7033 | + sec->output_offset); | |
7034 | } | |
7035 | if (h->root.type == bfd_link_hash_undefined | |
7036 | && !((*info->callbacks->undefined_symbol) | |
7037 | (info, h->root.root.string, input_bfd, | |
7038 | input_section, rel->r_offset, | |
7039 | info->unresolved_syms_in_objects == RM_GENERATE_ERROR | |
7040 | || ELF_ST_VISIBILITY (h->other)))) | |
7041 | return; | |
7042 | } | |
7043 | ||
7044 | decode_complex_addend (& start, & oplen, & len, & wordsz, | |
7045 | & chunksz, & lsb0_p, & signed_p, | |
7046 | & trunc_p, rel->r_addend); | |
7047 | ||
7048 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
7049 | ||
7050 | if (lsb0_p) | |
7051 | shift = (start + 1) - len; | |
7052 | else | |
7053 | shift = (8 * wordsz) - (start + len); | |
7054 | ||
7055 | x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset); | |
7056 | ||
7057 | #ifdef DEBUG | |
7058 | printf ("Doing complex reloc: " | |
7059 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
7060 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
7061 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
7062 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
7063 | oplen, x, mask, relocation); | |
7064 | #endif | |
7065 | ||
7066 | if (! trunc_p) | |
7067 | { | |
7068 | /* Now do an overflow check. */ | |
7069 | if (bfd_check_overflow ((signed_p ? | |
7070 | complain_overflow_signed : | |
7071 | complain_overflow_unsigned), | |
7072 | len, 0, (8 * wordsz), | |
7073 | relocation) == bfd_reloc_overflow) | |
7074 | (*_bfd_error_handler) | |
7075 | ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit " | |
7076 | "within 0x%lx", | |
7077 | input_bfd->filename, input_section->name, rel->r_offset, | |
7078 | relocation, (signed_p ? "(signed) " : ""), mask); | |
7079 | } | |
7080 | ||
7081 | /* Do the deed. */ | |
7082 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
7083 | ||
7084 | #ifdef DEBUG | |
7085 | printf (" relocation: %8.8lx\n" | |
7086 | " shifted mask: %8.8lx\n" | |
7087 | " shifted/masked reloc: %8.8lx\n" | |
7088 | " result: %8.8lx\n", | |
7089 | relocation, (mask << shift), | |
7090 | ((relocation & mask) << shift), x); | |
7091 | #endif | |
7092 | put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset); | |
7093 | } | |
7094 | ||
c152c796 AM |
7095 | /* When performing a relocatable link, the input relocations are |
7096 | preserved. But, if they reference global symbols, the indices | |
7097 | referenced must be updated. Update all the relocations in | |
7098 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ | |
7099 | ||
7100 | static void | |
7101 | elf_link_adjust_relocs (bfd *abfd, | |
7102 | Elf_Internal_Shdr *rel_hdr, | |
7103 | unsigned int count, | |
7104 | struct elf_link_hash_entry **rel_hash) | |
7105 | { | |
7106 | unsigned int i; | |
7107 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7108 | bfd_byte *erela; | |
7109 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
7110 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
7111 | bfd_vma r_type_mask; | |
7112 | int r_sym_shift; | |
7113 | ||
7114 | if (rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
7115 | { | |
7116 | swap_in = bed->s->swap_reloc_in; | |
7117 | swap_out = bed->s->swap_reloc_out; | |
7118 | } | |
7119 | else if (rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
7120 | { | |
7121 | swap_in = bed->s->swap_reloca_in; | |
7122 | swap_out = bed->s->swap_reloca_out; | |
7123 | } | |
7124 | else | |
7125 | abort (); | |
7126 | ||
7127 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
7128 | abort (); | |
7129 | ||
7130 | if (bed->s->arch_size == 32) | |
7131 | { | |
7132 | r_type_mask = 0xff; | |
7133 | r_sym_shift = 8; | |
7134 | } | |
7135 | else | |
7136 | { | |
7137 | r_type_mask = 0xffffffff; | |
7138 | r_sym_shift = 32; | |
7139 | } | |
7140 | ||
7141 | erela = rel_hdr->contents; | |
7142 | for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) | |
7143 | { | |
7144 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
7145 | unsigned int j; | |
7146 | ||
7147 | if (*rel_hash == NULL) | |
7148 | continue; | |
7149 | ||
7150 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
7151 | ||
7152 | (*swap_in) (abfd, erela, irela); | |
7153 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
7154 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
7155 | | (irela[j].r_info & r_type_mask)); | |
7156 | (*swap_out) (abfd, irela, erela); | |
7157 | } | |
7158 | } | |
7159 | ||
7160 | struct elf_link_sort_rela | |
7161 | { | |
7162 | union { | |
7163 | bfd_vma offset; | |
7164 | bfd_vma sym_mask; | |
7165 | } u; | |
7166 | enum elf_reloc_type_class type; | |
7167 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
7168 | Elf_Internal_Rela rela[1]; | |
7169 | }; | |
7170 | ||
7171 | static int | |
7172 | elf_link_sort_cmp1 (const void *A, const void *B) | |
7173 | { | |
7174 | const struct elf_link_sort_rela *a = A; | |
7175 | const struct elf_link_sort_rela *b = B; | |
7176 | int relativea, relativeb; | |
7177 | ||
7178 | relativea = a->type == reloc_class_relative; | |
7179 | relativeb = b->type == reloc_class_relative; | |
7180 | ||
7181 | if (relativea < relativeb) | |
7182 | return 1; | |
7183 | if (relativea > relativeb) | |
7184 | return -1; | |
7185 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
7186 | return -1; | |
7187 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
7188 | return 1; | |
7189 | if (a->rela->r_offset < b->rela->r_offset) | |
7190 | return -1; | |
7191 | if (a->rela->r_offset > b->rela->r_offset) | |
7192 | return 1; | |
7193 | return 0; | |
7194 | } | |
7195 | ||
7196 | static int | |
7197 | elf_link_sort_cmp2 (const void *A, const void *B) | |
7198 | { | |
7199 | const struct elf_link_sort_rela *a = A; | |
7200 | const struct elf_link_sort_rela *b = B; | |
7201 | int copya, copyb; | |
7202 | ||
7203 | if (a->u.offset < b->u.offset) | |
7204 | return -1; | |
7205 | if (a->u.offset > b->u.offset) | |
7206 | return 1; | |
7207 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); | |
7208 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
7209 | if (copya < copyb) | |
7210 | return -1; | |
7211 | if (copya > copyb) | |
7212 | return 1; | |
7213 | if (a->rela->r_offset < b->rela->r_offset) | |
7214 | return -1; | |
7215 | if (a->rela->r_offset > b->rela->r_offset) | |
7216 | return 1; | |
7217 | return 0; | |
7218 | } | |
7219 | ||
7220 | static size_t | |
7221 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
7222 | { | |
3410fea8 | 7223 | asection *dynamic_relocs; |
fc66a176 L |
7224 | asection *rela_dyn; |
7225 | asection *rel_dyn; | |
c152c796 AM |
7226 | bfd_size_type count, size; |
7227 | size_t i, ret, sort_elt, ext_size; | |
7228 | bfd_byte *sort, *s_non_relative, *p; | |
7229 | struct elf_link_sort_rela *sq; | |
7230 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7231 | int i2e = bed->s->int_rels_per_ext_rel; | |
7232 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
7233 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
7234 | struct bfd_link_order *lo; | |
7235 | bfd_vma r_sym_mask; | |
3410fea8 | 7236 | bfd_boolean use_rela; |
c152c796 | 7237 | |
3410fea8 NC |
7238 | /* Find a dynamic reloc section. */ |
7239 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
7240 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
7241 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
7242 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 7243 | { |
3410fea8 NC |
7244 | bfd_boolean use_rela_initialised = FALSE; |
7245 | ||
7246 | /* This is just here to stop gcc from complaining. | |
7247 | It's initialization checking code is not perfect. */ | |
7248 | use_rela = TRUE; | |
7249 | ||
7250 | /* Both sections are present. Examine the sizes | |
7251 | of the indirect sections to help us choose. */ | |
7252 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
7253 | if (lo->type == bfd_indirect_link_order) | |
7254 | { | |
7255 | asection *o = lo->u.indirect.section; | |
7256 | ||
7257 | if ((o->size % bed->s->sizeof_rela) == 0) | |
7258 | { | |
7259 | if ((o->size % bed->s->sizeof_rel) == 0) | |
7260 | /* Section size is divisible by both rel and rela sizes. | |
7261 | It is of no help to us. */ | |
7262 | ; | |
7263 | else | |
7264 | { | |
7265 | /* Section size is only divisible by rela. */ | |
7266 | if (use_rela_initialised && (use_rela == FALSE)) | |
7267 | { | |
7268 | _bfd_error_handler | |
7269 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7270 | bfd_set_error (bfd_error_invalid_operation); | |
7271 | return 0; | |
7272 | } | |
7273 | else | |
7274 | { | |
7275 | use_rela = TRUE; | |
7276 | use_rela_initialised = TRUE; | |
7277 | } | |
7278 | } | |
7279 | } | |
7280 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
7281 | { | |
7282 | /* Section size is only divisible by rel. */ | |
7283 | if (use_rela_initialised && (use_rela == TRUE)) | |
7284 | { | |
7285 | _bfd_error_handler | |
7286 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7287 | bfd_set_error (bfd_error_invalid_operation); | |
7288 | return 0; | |
7289 | } | |
7290 | else | |
7291 | { | |
7292 | use_rela = FALSE; | |
7293 | use_rela_initialised = TRUE; | |
7294 | } | |
7295 | } | |
7296 | else | |
7297 | { | |
7298 | /* The section size is not divisible by either - something is wrong. */ | |
7299 | _bfd_error_handler | |
7300 | (_("%B: Unable to sort relocs - they are of an unknown size"), abfd); | |
7301 | bfd_set_error (bfd_error_invalid_operation); | |
7302 | return 0; | |
7303 | } | |
7304 | } | |
7305 | ||
7306 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
7307 | if (lo->type == bfd_indirect_link_order) | |
7308 | { | |
7309 | asection *o = lo->u.indirect.section; | |
7310 | ||
7311 | if ((o->size % bed->s->sizeof_rela) == 0) | |
7312 | { | |
7313 | if ((o->size % bed->s->sizeof_rel) == 0) | |
7314 | /* Section size is divisible by both rel and rela sizes. | |
7315 | It is of no help to us. */ | |
7316 | ; | |
7317 | else | |
7318 | { | |
7319 | /* Section size is only divisible by rela. */ | |
7320 | if (use_rela_initialised && (use_rela == FALSE)) | |
7321 | { | |
7322 | _bfd_error_handler | |
7323 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7324 | bfd_set_error (bfd_error_invalid_operation); | |
7325 | return 0; | |
7326 | } | |
7327 | else | |
7328 | { | |
7329 | use_rela = TRUE; | |
7330 | use_rela_initialised = TRUE; | |
7331 | } | |
7332 | } | |
7333 | } | |
7334 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
7335 | { | |
7336 | /* Section size is only divisible by rel. */ | |
7337 | if (use_rela_initialised && (use_rela == TRUE)) | |
7338 | { | |
7339 | _bfd_error_handler | |
7340 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7341 | bfd_set_error (bfd_error_invalid_operation); | |
7342 | return 0; | |
7343 | } | |
7344 | else | |
7345 | { | |
7346 | use_rela = FALSE; | |
7347 | use_rela_initialised = TRUE; | |
7348 | } | |
7349 | } | |
7350 | else | |
7351 | { | |
7352 | /* The section size is not divisible by either - something is wrong. */ | |
7353 | _bfd_error_handler | |
7354 | (_("%B: Unable to sort relocs - they are of an unknown size"), abfd); | |
7355 | bfd_set_error (bfd_error_invalid_operation); | |
7356 | return 0; | |
7357 | } | |
7358 | } | |
7359 | ||
7360 | if (! use_rela_initialised) | |
7361 | /* Make a guess. */ | |
7362 | use_rela = TRUE; | |
c152c796 | 7363 | } |
fc66a176 L |
7364 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
7365 | use_rela = TRUE; | |
7366 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 7367 | use_rela = FALSE; |
c152c796 | 7368 | else |
fc66a176 | 7369 | return 0; |
3410fea8 NC |
7370 | |
7371 | if (use_rela) | |
c152c796 | 7372 | { |
3410fea8 | 7373 | dynamic_relocs = rela_dyn; |
c152c796 AM |
7374 | ext_size = bed->s->sizeof_rela; |
7375 | swap_in = bed->s->swap_reloca_in; | |
7376 | swap_out = bed->s->swap_reloca_out; | |
7377 | } | |
3410fea8 NC |
7378 | else |
7379 | { | |
7380 | dynamic_relocs = rel_dyn; | |
7381 | ext_size = bed->s->sizeof_rel; | |
7382 | swap_in = bed->s->swap_reloc_in; | |
7383 | swap_out = bed->s->swap_reloc_out; | |
7384 | } | |
c152c796 AM |
7385 | |
7386 | size = 0; | |
3410fea8 | 7387 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 7388 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 7389 | size += lo->u.indirect.section->size; |
c152c796 | 7390 | |
3410fea8 | 7391 | if (size != dynamic_relocs->size) |
c152c796 AM |
7392 | return 0; |
7393 | ||
7394 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
7395 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
7396 | |
7397 | count = dynamic_relocs->size / ext_size; | |
c152c796 | 7398 | sort = bfd_zmalloc (sort_elt * count); |
3410fea8 | 7399 | |
c152c796 AM |
7400 | if (sort == NULL) |
7401 | { | |
7402 | (*info->callbacks->warning) | |
7403 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
7404 | return 0; | |
7405 | } | |
7406 | ||
7407 | if (bed->s->arch_size == 32) | |
7408 | r_sym_mask = ~(bfd_vma) 0xff; | |
7409 | else | |
7410 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
7411 | ||
3410fea8 | 7412 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
7413 | if (lo->type == bfd_indirect_link_order) |
7414 | { | |
7415 | bfd_byte *erel, *erelend; | |
7416 | asection *o = lo->u.indirect.section; | |
7417 | ||
1da212d6 AM |
7418 | if (o->contents == NULL && o->size != 0) |
7419 | { | |
7420 | /* This is a reloc section that is being handled as a normal | |
7421 | section. See bfd_section_from_shdr. We can't combine | |
7422 | relocs in this case. */ | |
7423 | free (sort); | |
7424 | return 0; | |
7425 | } | |
c152c796 | 7426 | erel = o->contents; |
eea6121a | 7427 | erelend = o->contents + o->size; |
c152c796 | 7428 | p = sort + o->output_offset / ext_size * sort_elt; |
3410fea8 | 7429 | |
c152c796 AM |
7430 | while (erel < erelend) |
7431 | { | |
7432 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 7433 | |
c152c796 AM |
7434 | (*swap_in) (abfd, erel, s->rela); |
7435 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
7436 | s->u.sym_mask = r_sym_mask; | |
7437 | p += sort_elt; | |
7438 | erel += ext_size; | |
7439 | } | |
7440 | } | |
7441 | ||
7442 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
7443 | ||
7444 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
7445 | { | |
7446 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
7447 | if (s->type != reloc_class_relative) | |
7448 | break; | |
7449 | } | |
7450 | ret = i; | |
7451 | s_non_relative = p; | |
7452 | ||
7453 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
7454 | for (; i < count; i++, p += sort_elt) | |
7455 | { | |
7456 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
7457 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
7458 | sq = sp; | |
7459 | sp->u.offset = sq->rela->r_offset; | |
7460 | } | |
7461 | ||
7462 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
7463 | ||
3410fea8 | 7464 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
7465 | if (lo->type == bfd_indirect_link_order) |
7466 | { | |
7467 | bfd_byte *erel, *erelend; | |
7468 | asection *o = lo->u.indirect.section; | |
7469 | ||
7470 | erel = o->contents; | |
eea6121a | 7471 | erelend = o->contents + o->size; |
c152c796 AM |
7472 | p = sort + o->output_offset / ext_size * sort_elt; |
7473 | while (erel < erelend) | |
7474 | { | |
7475 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
7476 | (*swap_out) (abfd, s->rela, erel); | |
7477 | p += sort_elt; | |
7478 | erel += ext_size; | |
7479 | } | |
7480 | } | |
7481 | ||
7482 | free (sort); | |
3410fea8 | 7483 | *psec = dynamic_relocs; |
c152c796 AM |
7484 | return ret; |
7485 | } | |
7486 | ||
7487 | /* Flush the output symbols to the file. */ | |
7488 | ||
7489 | static bfd_boolean | |
7490 | elf_link_flush_output_syms (struct elf_final_link_info *finfo, | |
7491 | const struct elf_backend_data *bed) | |
7492 | { | |
7493 | if (finfo->symbuf_count > 0) | |
7494 | { | |
7495 | Elf_Internal_Shdr *hdr; | |
7496 | file_ptr pos; | |
7497 | bfd_size_type amt; | |
7498 | ||
7499 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
7500 | pos = hdr->sh_offset + hdr->sh_size; | |
7501 | amt = finfo->symbuf_count * bed->s->sizeof_sym; | |
7502 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
7503 | || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) | |
7504 | return FALSE; | |
7505 | ||
7506 | hdr->sh_size += amt; | |
7507 | finfo->symbuf_count = 0; | |
7508 | } | |
7509 | ||
7510 | return TRUE; | |
7511 | } | |
7512 | ||
7513 | /* Add a symbol to the output symbol table. */ | |
7514 | ||
7515 | static bfd_boolean | |
7516 | elf_link_output_sym (struct elf_final_link_info *finfo, | |
7517 | const char *name, | |
7518 | Elf_Internal_Sym *elfsym, | |
7519 | asection *input_sec, | |
7520 | struct elf_link_hash_entry *h) | |
7521 | { | |
7522 | bfd_byte *dest; | |
7523 | Elf_External_Sym_Shndx *destshndx; | |
7524 | bfd_boolean (*output_symbol_hook) | |
7525 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, | |
7526 | struct elf_link_hash_entry *); | |
7527 | const struct elf_backend_data *bed; | |
7528 | ||
7529 | bed = get_elf_backend_data (finfo->output_bfd); | |
7530 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; | |
7531 | if (output_symbol_hook != NULL) | |
7532 | { | |
7533 | if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h)) | |
7534 | return FALSE; | |
7535 | } | |
7536 | ||
7537 | if (name == NULL || *name == '\0') | |
7538 | elfsym->st_name = 0; | |
7539 | else if (input_sec->flags & SEC_EXCLUDE) | |
7540 | elfsym->st_name = 0; | |
7541 | else | |
7542 | { | |
7543 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
7544 | name, TRUE, FALSE); | |
7545 | if (elfsym->st_name == (unsigned long) -1) | |
7546 | return FALSE; | |
7547 | } | |
7548 | ||
7549 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
7550 | { | |
7551 | if (! elf_link_flush_output_syms (finfo, bed)) | |
7552 | return FALSE; | |
7553 | } | |
7554 | ||
7555 | dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; | |
7556 | destshndx = finfo->symshndxbuf; | |
7557 | if (destshndx != NULL) | |
7558 | { | |
7559 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
7560 | { | |
7561 | bfd_size_type amt; | |
7562 | ||
7563 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); | |
7564 | finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2); | |
7565 | if (destshndx == NULL) | |
7566 | return FALSE; | |
7567 | memset ((char *) destshndx + amt, 0, amt); | |
7568 | finfo->shndxbuf_size *= 2; | |
7569 | } | |
7570 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
7571 | } | |
7572 | ||
7573 | bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); | |
7574 | finfo->symbuf_count += 1; | |
7575 | bfd_get_symcount (finfo->output_bfd) += 1; | |
7576 | ||
7577 | return TRUE; | |
7578 | } | |
7579 | ||
c0d5a53d L |
7580 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
7581 | ||
7582 | static bfd_boolean | |
7583 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
7584 | { | |
7585 | if (sym->st_shndx > SHN_HIRESERVE) | |
7586 | { | |
7587 | /* The gABI doesn't support dynamic symbols in output sections | |
7588 | beyond 64k. */ | |
7589 | (*_bfd_error_handler) | |
7590 | (_("%B: Too many sections: %d (>= %d)"), | |
7591 | abfd, bfd_count_sections (abfd), SHN_LORESERVE); | |
7592 | bfd_set_error (bfd_error_nonrepresentable_section); | |
7593 | return FALSE; | |
7594 | } | |
7595 | return TRUE; | |
7596 | } | |
7597 | ||
c152c796 AM |
7598 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
7599 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
7600 | versioned symbol that would normally require an explicit version. | |
7601 | We also handle the case that a DSO references a hidden symbol | |
7602 | which may be satisfied by a versioned symbol in another DSO. */ | |
7603 | ||
7604 | static bfd_boolean | |
7605 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
7606 | const struct elf_backend_data *bed, | |
7607 | struct elf_link_hash_entry *h) | |
7608 | { | |
7609 | bfd *abfd; | |
7610 | struct elf_link_loaded_list *loaded; | |
7611 | ||
7612 | if (!is_elf_hash_table (info->hash)) | |
7613 | return FALSE; | |
7614 | ||
7615 | switch (h->root.type) | |
7616 | { | |
7617 | default: | |
7618 | abfd = NULL; | |
7619 | break; | |
7620 | ||
7621 | case bfd_link_hash_undefined: | |
7622 | case bfd_link_hash_undefweak: | |
7623 | abfd = h->root.u.undef.abfd; | |
7624 | if ((abfd->flags & DYNAMIC) == 0 | |
e56f61be | 7625 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
7626 | return FALSE; |
7627 | break; | |
7628 | ||
7629 | case bfd_link_hash_defined: | |
7630 | case bfd_link_hash_defweak: | |
7631 | abfd = h->root.u.def.section->owner; | |
7632 | break; | |
7633 | ||
7634 | case bfd_link_hash_common: | |
7635 | abfd = h->root.u.c.p->section->owner; | |
7636 | break; | |
7637 | } | |
7638 | BFD_ASSERT (abfd != NULL); | |
7639 | ||
7640 | for (loaded = elf_hash_table (info)->loaded; | |
7641 | loaded != NULL; | |
7642 | loaded = loaded->next) | |
7643 | { | |
7644 | bfd *input; | |
7645 | Elf_Internal_Shdr *hdr; | |
7646 | bfd_size_type symcount; | |
7647 | bfd_size_type extsymcount; | |
7648 | bfd_size_type extsymoff; | |
7649 | Elf_Internal_Shdr *versymhdr; | |
7650 | Elf_Internal_Sym *isym; | |
7651 | Elf_Internal_Sym *isymend; | |
7652 | Elf_Internal_Sym *isymbuf; | |
7653 | Elf_External_Versym *ever; | |
7654 | Elf_External_Versym *extversym; | |
7655 | ||
7656 | input = loaded->abfd; | |
7657 | ||
7658 | /* We check each DSO for a possible hidden versioned definition. */ | |
7659 | if (input == abfd | |
7660 | || (input->flags & DYNAMIC) == 0 | |
7661 | || elf_dynversym (input) == 0) | |
7662 | continue; | |
7663 | ||
7664 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
7665 | ||
7666 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
7667 | if (elf_bad_symtab (input)) | |
7668 | { | |
7669 | extsymcount = symcount; | |
7670 | extsymoff = 0; | |
7671 | } | |
7672 | else | |
7673 | { | |
7674 | extsymcount = symcount - hdr->sh_info; | |
7675 | extsymoff = hdr->sh_info; | |
7676 | } | |
7677 | ||
7678 | if (extsymcount == 0) | |
7679 | continue; | |
7680 | ||
7681 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
7682 | NULL, NULL, NULL); | |
7683 | if (isymbuf == NULL) | |
7684 | return FALSE; | |
7685 | ||
7686 | /* Read in any version definitions. */ | |
7687 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
7688 | extversym = bfd_malloc (versymhdr->sh_size); | |
7689 | if (extversym == NULL) | |
7690 | goto error_ret; | |
7691 | ||
7692 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
7693 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
7694 | != versymhdr->sh_size)) | |
7695 | { | |
7696 | free (extversym); | |
7697 | error_ret: | |
7698 | free (isymbuf); | |
7699 | return FALSE; | |
7700 | } | |
7701 | ||
7702 | ever = extversym + extsymoff; | |
7703 | isymend = isymbuf + extsymcount; | |
7704 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
7705 | { | |
7706 | const char *name; | |
7707 | Elf_Internal_Versym iver; | |
7708 | unsigned short version_index; | |
7709 | ||
7710 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
7711 | || isym->st_shndx == SHN_UNDEF) | |
7712 | continue; | |
7713 | ||
7714 | name = bfd_elf_string_from_elf_section (input, | |
7715 | hdr->sh_link, | |
7716 | isym->st_name); | |
7717 | if (strcmp (name, h->root.root.string) != 0) | |
7718 | continue; | |
7719 | ||
7720 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
7721 | ||
7722 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
7723 | { | |
7724 | /* If we have a non-hidden versioned sym, then it should | |
7725 | have provided a definition for the undefined sym. */ | |
7726 | abort (); | |
7727 | } | |
7728 | ||
7729 | version_index = iver.vs_vers & VERSYM_VERSION; | |
7730 | if (version_index == 1 || version_index == 2) | |
7731 | { | |
7732 | /* This is the base or first version. We can use it. */ | |
7733 | free (extversym); | |
7734 | free (isymbuf); | |
7735 | return TRUE; | |
7736 | } | |
7737 | } | |
7738 | ||
7739 | free (extversym); | |
7740 | free (isymbuf); | |
7741 | } | |
7742 | ||
7743 | return FALSE; | |
7744 | } | |
7745 | ||
7746 | /* Add an external symbol to the symbol table. This is called from | |
7747 | the hash table traversal routine. When generating a shared object, | |
7748 | we go through the symbol table twice. The first time we output | |
7749 | anything that might have been forced to local scope in a version | |
7750 | script. The second time we output the symbols that are still | |
7751 | global symbols. */ | |
7752 | ||
7753 | static bfd_boolean | |
7754 | elf_link_output_extsym (struct elf_link_hash_entry *h, void *data) | |
7755 | { | |
7756 | struct elf_outext_info *eoinfo = data; | |
7757 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
7758 | bfd_boolean strip; | |
7759 | Elf_Internal_Sym sym; | |
7760 | asection *input_sec; | |
7761 | const struct elf_backend_data *bed; | |
7762 | ||
7763 | if (h->root.type == bfd_link_hash_warning) | |
7764 | { | |
7765 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7766 | if (h->root.type == bfd_link_hash_new) | |
7767 | return TRUE; | |
7768 | } | |
7769 | ||
7770 | /* Decide whether to output this symbol in this pass. */ | |
7771 | if (eoinfo->localsyms) | |
7772 | { | |
f5385ebf | 7773 | if (!h->forced_local) |
c152c796 AM |
7774 | return TRUE; |
7775 | } | |
7776 | else | |
7777 | { | |
f5385ebf | 7778 | if (h->forced_local) |
c152c796 AM |
7779 | return TRUE; |
7780 | } | |
7781 | ||
7782 | bed = get_elf_backend_data (finfo->output_bfd); | |
7783 | ||
12ac1cf5 | 7784 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 7785 | { |
12ac1cf5 NC |
7786 | /* If we have an undefined symbol reference here then it must have |
7787 | come from a shared library that is being linked in. (Undefined | |
7788 | references in regular files have already been handled). */ | |
7789 | bfd_boolean ignore_undef = FALSE; | |
7790 | ||
7791 | /* Some symbols may be special in that the fact that they're | |
7792 | undefined can be safely ignored - let backend determine that. */ | |
7793 | if (bed->elf_backend_ignore_undef_symbol) | |
7794 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
7795 | ||
7796 | /* If we are reporting errors for this situation then do so now. */ | |
7797 | if (ignore_undef == FALSE | |
7798 | && h->ref_dynamic | |
7799 | && ! h->ref_regular | |
7800 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h) | |
7801 | && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
c152c796 | 7802 | { |
12ac1cf5 NC |
7803 | if (! (finfo->info->callbacks->undefined_symbol |
7804 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
7805 | NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) | |
7806 | { | |
7807 | eoinfo->failed = TRUE; | |
7808 | return FALSE; | |
7809 | } | |
c152c796 AM |
7810 | } |
7811 | } | |
7812 | ||
7813 | /* We should also warn if a forced local symbol is referenced from | |
7814 | shared libraries. */ | |
7815 | if (! finfo->info->relocatable | |
7816 | && (! finfo->info->shared) | |
f5385ebf AM |
7817 | && h->forced_local |
7818 | && h->ref_dynamic | |
7819 | && !h->dynamic_def | |
7820 | && !h->dynamic_weak | |
c152c796 AM |
7821 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) |
7822 | { | |
7823 | (*_bfd_error_handler) | |
d003868e | 7824 | (_("%B: %s symbol `%s' in %B is referenced by DSO"), |
cfca085c L |
7825 | finfo->output_bfd, |
7826 | h->root.u.def.section == bfd_abs_section_ptr | |
7827 | ? finfo->output_bfd : h->root.u.def.section->owner, | |
c152c796 AM |
7828 | ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
7829 | ? "internal" | |
7830 | : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
d003868e AM |
7831 | ? "hidden" : "local", |
7832 | h->root.root.string); | |
c152c796 AM |
7833 | eoinfo->failed = TRUE; |
7834 | return FALSE; | |
7835 | } | |
7836 | ||
7837 | /* We don't want to output symbols that have never been mentioned by | |
7838 | a regular file, or that we have been told to strip. However, if | |
7839 | h->indx is set to -2, the symbol is used by a reloc and we must | |
7840 | output it. */ | |
7841 | if (h->indx == -2) | |
7842 | strip = FALSE; | |
f5385ebf | 7843 | else if ((h->def_dynamic |
77cfaee6 AM |
7844 | || h->ref_dynamic |
7845 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
7846 | && !h->def_regular |
7847 | && !h->ref_regular) | |
c152c796 AM |
7848 | strip = TRUE; |
7849 | else if (finfo->info->strip == strip_all) | |
7850 | strip = TRUE; | |
7851 | else if (finfo->info->strip == strip_some | |
7852 | && bfd_hash_lookup (finfo->info->keep_hash, | |
7853 | h->root.root.string, FALSE, FALSE) == NULL) | |
7854 | strip = TRUE; | |
7855 | else if (finfo->info->strip_discarded | |
7856 | && (h->root.type == bfd_link_hash_defined | |
7857 | || h->root.type == bfd_link_hash_defweak) | |
7858 | && elf_discarded_section (h->root.u.def.section)) | |
7859 | strip = TRUE; | |
7860 | else | |
7861 | strip = FALSE; | |
7862 | ||
7863 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
7864 | nothing else to do unless it is a forced local symbol. */ | |
7865 | if (strip | |
7866 | && h->dynindx == -1 | |
f5385ebf | 7867 | && !h->forced_local) |
c152c796 AM |
7868 | return TRUE; |
7869 | ||
7870 | sym.st_value = 0; | |
7871 | sym.st_size = h->size; | |
7872 | sym.st_other = h->other; | |
f5385ebf | 7873 | if (h->forced_local) |
c152c796 AM |
7874 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); |
7875 | else if (h->root.type == bfd_link_hash_undefweak | |
7876 | || h->root.type == bfd_link_hash_defweak) | |
7877 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
7878 | else | |
7879 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
7880 | ||
7881 | switch (h->root.type) | |
7882 | { | |
7883 | default: | |
7884 | case bfd_link_hash_new: | |
7885 | case bfd_link_hash_warning: | |
7886 | abort (); | |
7887 | return FALSE; | |
7888 | ||
7889 | case bfd_link_hash_undefined: | |
7890 | case bfd_link_hash_undefweak: | |
7891 | input_sec = bfd_und_section_ptr; | |
7892 | sym.st_shndx = SHN_UNDEF; | |
7893 | break; | |
7894 | ||
7895 | case bfd_link_hash_defined: | |
7896 | case bfd_link_hash_defweak: | |
7897 | { | |
7898 | input_sec = h->root.u.def.section; | |
7899 | if (input_sec->output_section != NULL) | |
7900 | { | |
7901 | sym.st_shndx = | |
7902 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
7903 | input_sec->output_section); | |
7904 | if (sym.st_shndx == SHN_BAD) | |
7905 | { | |
7906 | (*_bfd_error_handler) | |
d003868e AM |
7907 | (_("%B: could not find output section %A for input section %A"), |
7908 | finfo->output_bfd, input_sec->output_section, input_sec); | |
c152c796 AM |
7909 | eoinfo->failed = TRUE; |
7910 | return FALSE; | |
7911 | } | |
7912 | ||
7913 | /* ELF symbols in relocatable files are section relative, | |
7914 | but in nonrelocatable files they are virtual | |
7915 | addresses. */ | |
7916 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
7917 | if (! finfo->info->relocatable) | |
7918 | { | |
7919 | sym.st_value += input_sec->output_section->vma; | |
7920 | if (h->type == STT_TLS) | |
7921 | { | |
7922 | /* STT_TLS symbols are relative to PT_TLS segment | |
7923 | base. */ | |
7924 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
7925 | sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
7926 | } | |
7927 | } | |
7928 | } | |
7929 | else | |
7930 | { | |
7931 | BFD_ASSERT (input_sec->owner == NULL | |
7932 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
7933 | sym.st_shndx = SHN_UNDEF; | |
7934 | input_sec = bfd_und_section_ptr; | |
7935 | } | |
7936 | } | |
7937 | break; | |
7938 | ||
7939 | case bfd_link_hash_common: | |
7940 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 7941 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
7942 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
7943 | break; | |
7944 | ||
7945 | case bfd_link_hash_indirect: | |
7946 | /* These symbols are created by symbol versioning. They point | |
7947 | to the decorated version of the name. For example, if the | |
7948 | symbol foo@@GNU_1.2 is the default, which should be used when | |
7949 | foo is used with no version, then we add an indirect symbol | |
7950 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
7951 | since the indirected symbol is already in the hash table. */ | |
7952 | return TRUE; | |
7953 | } | |
7954 | ||
7955 | /* Give the processor backend a chance to tweak the symbol value, | |
7956 | and also to finish up anything that needs to be done for this | |
7957 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
7958 | forced local syms when non-shared is due to a historical quirk. */ | |
7959 | if ((h->dynindx != -1 | |
f5385ebf | 7960 | || h->forced_local) |
c152c796 AM |
7961 | && ((finfo->info->shared |
7962 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
7963 | || h->root.type != bfd_link_hash_undefweak)) | |
f5385ebf | 7964 | || !h->forced_local) |
c152c796 AM |
7965 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
7966 | { | |
7967 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
7968 | (finfo->output_bfd, finfo->info, h, &sym))) | |
7969 | { | |
7970 | eoinfo->failed = TRUE; | |
7971 | return FALSE; | |
7972 | } | |
7973 | } | |
7974 | ||
7975 | /* If we are marking the symbol as undefined, and there are no | |
7976 | non-weak references to this symbol from a regular object, then | |
7977 | mark the symbol as weak undefined; if there are non-weak | |
7978 | references, mark the symbol as strong. We can't do this earlier, | |
7979 | because it might not be marked as undefined until the | |
7980 | finish_dynamic_symbol routine gets through with it. */ | |
7981 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 7982 | && h->ref_regular |
c152c796 AM |
7983 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
7984 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
7985 | { | |
7986 | int bindtype; | |
7987 | ||
f5385ebf | 7988 | if (h->ref_regular_nonweak) |
c152c796 AM |
7989 | bindtype = STB_GLOBAL; |
7990 | else | |
7991 | bindtype = STB_WEAK; | |
7992 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); | |
7993 | } | |
7994 | ||
7995 | /* If a non-weak symbol with non-default visibility is not defined | |
7996 | locally, it is a fatal error. */ | |
7997 | if (! finfo->info->relocatable | |
7998 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT | |
7999 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
8000 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 8001 | && !h->def_regular) |
c152c796 AM |
8002 | { |
8003 | (*_bfd_error_handler) | |
d003868e AM |
8004 | (_("%B: %s symbol `%s' isn't defined"), |
8005 | finfo->output_bfd, | |
8006 | ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED | |
8007 | ? "protected" | |
8008 | : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL | |
8009 | ? "internal" : "hidden", | |
8010 | h->root.root.string); | |
c152c796 AM |
8011 | eoinfo->failed = TRUE; |
8012 | return FALSE; | |
8013 | } | |
8014 | ||
8015 | /* If this symbol should be put in the .dynsym section, then put it | |
8016 | there now. We already know the symbol index. We also fill in | |
8017 | the entry in the .hash section. */ | |
8018 | if (h->dynindx != -1 | |
8019 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
8020 | { | |
c152c796 AM |
8021 | bfd_byte *esym; |
8022 | ||
8023 | sym.st_name = h->dynstr_index; | |
8024 | esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; | |
c0d5a53d L |
8025 | if (! check_dynsym (finfo->output_bfd, &sym)) |
8026 | { | |
8027 | eoinfo->failed = TRUE; | |
8028 | return FALSE; | |
8029 | } | |
c152c796 AM |
8030 | bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); |
8031 | ||
fdc90cb4 JJ |
8032 | if (finfo->hash_sec != NULL) |
8033 | { | |
8034 | size_t hash_entry_size; | |
8035 | bfd_byte *bucketpos; | |
8036 | bfd_vma chain; | |
41198d0c L |
8037 | size_t bucketcount; |
8038 | size_t bucket; | |
8039 | ||
8040 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
8041 | bucket = h->u.elf_hash_value % bucketcount; | |
fdc90cb4 JJ |
8042 | |
8043 | hash_entry_size | |
8044 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; | |
8045 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents | |
8046 | + (bucket + 2) * hash_entry_size); | |
8047 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
8048 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); | |
8049 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, | |
8050 | ((bfd_byte *) finfo->hash_sec->contents | |
8051 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
8052 | } | |
c152c796 AM |
8053 | |
8054 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
8055 | { | |
8056 | Elf_Internal_Versym iversym; | |
8057 | Elf_External_Versym *eversym; | |
8058 | ||
f5385ebf | 8059 | if (!h->def_regular) |
c152c796 AM |
8060 | { |
8061 | if (h->verinfo.verdef == NULL) | |
8062 | iversym.vs_vers = 0; | |
8063 | else | |
8064 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
8065 | } | |
8066 | else | |
8067 | { | |
8068 | if (h->verinfo.vertree == NULL) | |
8069 | iversym.vs_vers = 1; | |
8070 | else | |
8071 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
3e3b46e5 PB |
8072 | if (finfo->info->create_default_symver) |
8073 | iversym.vs_vers++; | |
c152c796 AM |
8074 | } |
8075 | ||
f5385ebf | 8076 | if (h->hidden) |
c152c796 AM |
8077 | iversym.vs_vers |= VERSYM_HIDDEN; |
8078 | ||
8079 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; | |
8080 | eversym += h->dynindx; | |
8081 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
8082 | } | |
8083 | } | |
8084 | ||
8085 | /* If we're stripping it, then it was just a dynamic symbol, and | |
8086 | there's nothing else to do. */ | |
8087 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) | |
8088 | return TRUE; | |
8089 | ||
8090 | h->indx = bfd_get_symcount (finfo->output_bfd); | |
8091 | ||
8092 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h)) | |
8093 | { | |
8094 | eoinfo->failed = TRUE; | |
8095 | return FALSE; | |
8096 | } | |
8097 | ||
8098 | return TRUE; | |
8099 | } | |
8100 | ||
cdd3575c AM |
8101 | /* Return TRUE if special handling is done for relocs in SEC against |
8102 | symbols defined in discarded sections. */ | |
8103 | ||
c152c796 AM |
8104 | static bfd_boolean |
8105 | elf_section_ignore_discarded_relocs (asection *sec) | |
8106 | { | |
8107 | const struct elf_backend_data *bed; | |
8108 | ||
cdd3575c AM |
8109 | switch (sec->sec_info_type) |
8110 | { | |
8111 | case ELF_INFO_TYPE_STABS: | |
8112 | case ELF_INFO_TYPE_EH_FRAME: | |
8113 | return TRUE; | |
8114 | default: | |
8115 | break; | |
8116 | } | |
c152c796 AM |
8117 | |
8118 | bed = get_elf_backend_data (sec->owner); | |
8119 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
8120 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
8121 | return TRUE; | |
8122 | ||
8123 | return FALSE; | |
8124 | } | |
8125 | ||
9e66c942 AM |
8126 | /* Return a mask saying how ld should treat relocations in SEC against |
8127 | symbols defined in discarded sections. If this function returns | |
8128 | COMPLAIN set, ld will issue a warning message. If this function | |
8129 | returns PRETEND set, and the discarded section was link-once and the | |
8130 | same size as the kept link-once section, ld will pretend that the | |
8131 | symbol was actually defined in the kept section. Otherwise ld will | |
8132 | zero the reloc (at least that is the intent, but some cooperation by | |
8133 | the target dependent code is needed, particularly for REL targets). */ | |
8134 | ||
8a696751 AM |
8135 | unsigned int |
8136 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 8137 | { |
9e66c942 | 8138 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 8139 | return PRETEND; |
cdd3575c AM |
8140 | |
8141 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 8142 | return 0; |
cdd3575c AM |
8143 | |
8144 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 8145 | return 0; |
cdd3575c | 8146 | |
9e66c942 | 8147 | return COMPLAIN | PRETEND; |
cdd3575c AM |
8148 | } |
8149 | ||
3d7f7666 L |
8150 | /* Find a match between a section and a member of a section group. */ |
8151 | ||
8152 | static asection * | |
c0f00686 L |
8153 | match_group_member (asection *sec, asection *group, |
8154 | struct bfd_link_info *info) | |
3d7f7666 L |
8155 | { |
8156 | asection *first = elf_next_in_group (group); | |
8157 | asection *s = first; | |
8158 | ||
8159 | while (s != NULL) | |
8160 | { | |
c0f00686 | 8161 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
8162 | return s; |
8163 | ||
83180ade | 8164 | s = elf_next_in_group (s); |
3d7f7666 L |
8165 | if (s == first) |
8166 | break; | |
8167 | } | |
8168 | ||
8169 | return NULL; | |
8170 | } | |
8171 | ||
01b3c8ab | 8172 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
8173 | to replace it. Return the replacement if it is OK. Otherwise return |
8174 | NULL. */ | |
01b3c8ab L |
8175 | |
8176 | asection * | |
c0f00686 | 8177 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
8178 | { |
8179 | asection *kept; | |
8180 | ||
8181 | kept = sec->kept_section; | |
8182 | if (kept != NULL) | |
8183 | { | |
c2370991 | 8184 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 8185 | kept = match_group_member (sec, kept, info); |
01b3c8ab L |
8186 | if (kept != NULL && sec->size != kept->size) |
8187 | kept = NULL; | |
c2370991 | 8188 | sec->kept_section = kept; |
01b3c8ab L |
8189 | } |
8190 | return kept; | |
8191 | } | |
8192 | ||
c152c796 AM |
8193 | /* Link an input file into the linker output file. This function |
8194 | handles all the sections and relocations of the input file at once. | |
8195 | This is so that we only have to read the local symbols once, and | |
8196 | don't have to keep them in memory. */ | |
8197 | ||
8198 | static bfd_boolean | |
8199 | elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) | |
8200 | { | |
8201 | bfd_boolean (*relocate_section) | |
8202 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
8203 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
8204 | bfd *output_bfd; | |
8205 | Elf_Internal_Shdr *symtab_hdr; | |
8206 | size_t locsymcount; | |
8207 | size_t extsymoff; | |
8208 | Elf_Internal_Sym *isymbuf; | |
8209 | Elf_Internal_Sym *isym; | |
8210 | Elf_Internal_Sym *isymend; | |
8211 | long *pindex; | |
8212 | asection **ppsection; | |
8213 | asection *o; | |
8214 | const struct elf_backend_data *bed; | |
8215 | bfd_boolean emit_relocs; | |
8216 | struct elf_link_hash_entry **sym_hashes; | |
8217 | ||
8218 | output_bfd = finfo->output_bfd; | |
8219 | bed = get_elf_backend_data (output_bfd); | |
8220 | relocate_section = bed->elf_backend_relocate_section; | |
8221 | ||
8222 | /* If this is a dynamic object, we don't want to do anything here: | |
8223 | we don't want the local symbols, and we don't want the section | |
8224 | contents. */ | |
8225 | if ((input_bfd->flags & DYNAMIC) != 0) | |
8226 | return TRUE; | |
8227 | ||
8228 | emit_relocs = (finfo->info->relocatable | |
eac338cf | 8229 | || finfo->info->emitrelocations); |
c152c796 AM |
8230 | |
8231 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
8232 | if (elf_bad_symtab (input_bfd)) | |
8233 | { | |
8234 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
8235 | extsymoff = 0; | |
8236 | } | |
8237 | else | |
8238 | { | |
8239 | locsymcount = symtab_hdr->sh_info; | |
8240 | extsymoff = symtab_hdr->sh_info; | |
8241 | } | |
8242 | ||
8243 | /* Read the local symbols. */ | |
8244 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
8245 | if (isymbuf == NULL && locsymcount != 0) | |
8246 | { | |
8247 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8248 | finfo->internal_syms, | |
8249 | finfo->external_syms, | |
8250 | finfo->locsym_shndx); | |
8251 | if (isymbuf == NULL) | |
8252 | return FALSE; | |
8253 | } | |
b31847af DB |
8254 | /* evaluate_complex_relocation_symbols looks for symbols in |
8255 | finfo->internal_syms. */ | |
8256 | else if (isymbuf != NULL && locsymcount != 0) | |
8257 | { | |
8258 | bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8259 | finfo->internal_syms, | |
8260 | finfo->external_syms, | |
8261 | finfo->locsym_shndx); | |
8262 | } | |
c152c796 AM |
8263 | |
8264 | /* Find local symbol sections and adjust values of symbols in | |
8265 | SEC_MERGE sections. Write out those local symbols we know are | |
8266 | going into the output file. */ | |
8267 | isymend = isymbuf + locsymcount; | |
8268 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
8269 | isym < isymend; | |
8270 | isym++, pindex++, ppsection++) | |
8271 | { | |
8272 | asection *isec; | |
8273 | const char *name; | |
8274 | Elf_Internal_Sym osym; | |
8275 | ||
8276 | *pindex = -1; | |
8277 | ||
8278 | if (elf_bad_symtab (input_bfd)) | |
8279 | { | |
8280 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
8281 | { | |
8282 | *ppsection = NULL; | |
8283 | continue; | |
8284 | } | |
8285 | } | |
8286 | ||
8287 | if (isym->st_shndx == SHN_UNDEF) | |
8288 | isec = bfd_und_section_ptr; | |
8289 | else if (isym->st_shndx < SHN_LORESERVE | |
8290 | || isym->st_shndx > SHN_HIRESERVE) | |
8291 | { | |
8292 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); | |
8293 | if (isec | |
8294 | && isec->sec_info_type == ELF_INFO_TYPE_MERGE | |
8295 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) | |
8296 | isym->st_value = | |
8297 | _bfd_merged_section_offset (output_bfd, &isec, | |
8298 | elf_section_data (isec)->sec_info, | |
753731ee | 8299 | isym->st_value); |
c152c796 AM |
8300 | } |
8301 | else if (isym->st_shndx == SHN_ABS) | |
8302 | isec = bfd_abs_section_ptr; | |
8303 | else if (isym->st_shndx == SHN_COMMON) | |
8304 | isec = bfd_com_section_ptr; | |
8305 | else | |
8306 | { | |
f02571c5 AM |
8307 | /* Don't attempt to output symbols with st_shnx in the |
8308 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
8309 | *ppsection = NULL; | |
8310 | continue; | |
c152c796 AM |
8311 | } |
8312 | ||
8313 | *ppsection = isec; | |
8314 | ||
8315 | /* Don't output the first, undefined, symbol. */ | |
8316 | if (ppsection == finfo->sections) | |
8317 | continue; | |
8318 | ||
8319 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
8320 | { | |
8321 | /* We never output section symbols. Instead, we use the | |
8322 | section symbol of the corresponding section in the output | |
8323 | file. */ | |
8324 | continue; | |
8325 | } | |
8326 | ||
8327 | /* If we are stripping all symbols, we don't want to output this | |
8328 | one. */ | |
8329 | if (finfo->info->strip == strip_all) | |
8330 | continue; | |
8331 | ||
8332 | /* If we are discarding all local symbols, we don't want to | |
8333 | output this one. If we are generating a relocatable output | |
8334 | file, then some of the local symbols may be required by | |
8335 | relocs; we output them below as we discover that they are | |
8336 | needed. */ | |
8337 | if (finfo->info->discard == discard_all) | |
8338 | continue; | |
8339 | ||
8340 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
8341 | discarding, we don't need to keep it. */ |
8342 | if (isym->st_shndx != SHN_UNDEF | |
8343 | && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
ccf5f610 | 8344 | && (isec == NULL |
f02571c5 AM |
8345 | || bfd_section_removed_from_list (output_bfd, |
8346 | isec->output_section))) | |
e75a280b L |
8347 | continue; |
8348 | ||
c152c796 AM |
8349 | /* Get the name of the symbol. */ |
8350 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
8351 | isym->st_name); | |
8352 | if (name == NULL) | |
8353 | return FALSE; | |
8354 | ||
8355 | /* See if we are discarding symbols with this name. */ | |
8356 | if ((finfo->info->strip == strip_some | |
8357 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) | |
8358 | == NULL)) | |
8359 | || (((finfo->info->discard == discard_sec_merge | |
8360 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) | |
8361 | || finfo->info->discard == discard_l) | |
8362 | && bfd_is_local_label_name (input_bfd, name))) | |
8363 | continue; | |
8364 | ||
8365 | /* If we get here, we are going to output this symbol. */ | |
8366 | ||
8367 | osym = *isym; | |
8368 | ||
8369 | /* Adjust the section index for the output file. */ | |
8370 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
8371 | isec->output_section); | |
8372 | if (osym.st_shndx == SHN_BAD) | |
8373 | return FALSE; | |
8374 | ||
8375 | *pindex = bfd_get_symcount (output_bfd); | |
8376 | ||
8377 | /* ELF symbols in relocatable files are section relative, but | |
8378 | in executable files they are virtual addresses. Note that | |
8379 | this code assumes that all ELF sections have an associated | |
8380 | BFD section with a reasonable value for output_offset; below | |
8381 | we assume that they also have a reasonable value for | |
8382 | output_section. Any special sections must be set up to meet | |
8383 | these requirements. */ | |
8384 | osym.st_value += isec->output_offset; | |
8385 | if (! finfo->info->relocatable) | |
8386 | { | |
8387 | osym.st_value += isec->output_section->vma; | |
8388 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
8389 | { | |
8390 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
8391 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
8392 | osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
8393 | } | |
8394 | } | |
8395 | ||
8396 | if (! elf_link_output_sym (finfo, name, &osym, isec, NULL)) | |
8397 | return FALSE; | |
8398 | } | |
8399 | ||
d9352518 DB |
8400 | if (! evaluate_complex_relocation_symbols (input_bfd, finfo, locsymcount)) |
8401 | return FALSE; | |
8402 | ||
c152c796 AM |
8403 | /* Relocate the contents of each section. */ |
8404 | sym_hashes = elf_sym_hashes (input_bfd); | |
8405 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
8406 | { | |
8407 | bfd_byte *contents; | |
8408 | ||
8409 | if (! o->linker_mark) | |
8410 | { | |
8411 | /* This section was omitted from the link. */ | |
8412 | continue; | |
8413 | } | |
8414 | ||
8415 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 8416 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
8417 | continue; |
8418 | ||
8419 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
8420 | { | |
8421 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
8422 | or somesuch. */ | |
8423 | continue; | |
8424 | } | |
8425 | ||
8426 | /* Get the contents of the section. They have been cached by a | |
8427 | relaxation routine. Note that o is a section in an input | |
8428 | file, so the contents field will not have been set by any of | |
8429 | the routines which work on output files. */ | |
8430 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
8431 | contents = elf_section_data (o)->this_hdr.contents; | |
8432 | else | |
8433 | { | |
eea6121a AM |
8434 | bfd_size_type amt = o->rawsize ? o->rawsize : o->size; |
8435 | ||
c152c796 | 8436 | contents = finfo->contents; |
eea6121a | 8437 | if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt)) |
c152c796 AM |
8438 | return FALSE; |
8439 | } | |
8440 | ||
8441 | if ((o->flags & SEC_RELOC) != 0) | |
8442 | { | |
8443 | Elf_Internal_Rela *internal_relocs; | |
8444 | bfd_vma r_type_mask; | |
8445 | int r_sym_shift; | |
8446 | ||
8447 | /* Get the swapped relocs. */ | |
8448 | internal_relocs | |
8449 | = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, | |
8450 | finfo->internal_relocs, FALSE); | |
8451 | if (internal_relocs == NULL | |
8452 | && o->reloc_count > 0) | |
8453 | return FALSE; | |
8454 | ||
8455 | if (bed->s->arch_size == 32) | |
8456 | { | |
8457 | r_type_mask = 0xff; | |
8458 | r_sym_shift = 8; | |
8459 | } | |
8460 | else | |
8461 | { | |
8462 | r_type_mask = 0xffffffff; | |
8463 | r_sym_shift = 32; | |
8464 | } | |
8465 | ||
8466 | /* Run through the relocs looking for any against symbols | |
8467 | from discarded sections and section symbols from | |
8468 | removed link-once sections. Complain about relocs | |
8469 | against discarded sections. Zero relocs against removed | |
e0ae6d6f | 8470 | link-once sections. */ |
c152c796 AM |
8471 | if (!elf_section_ignore_discarded_relocs (o)) |
8472 | { | |
8473 | Elf_Internal_Rela *rel, *relend; | |
8a696751 | 8474 | unsigned int action = (*bed->action_discarded) (o); |
c152c796 AM |
8475 | |
8476 | rel = internal_relocs; | |
8477 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
8478 | for ( ; rel < relend; rel++) | |
8479 | { | |
8480 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
cdd3575c AM |
8481 | asection **ps, *sec; |
8482 | struct elf_link_hash_entry *h = NULL; | |
8483 | const char *sym_name; | |
c152c796 | 8484 | |
ee75fd95 AM |
8485 | if (r_symndx == STN_UNDEF) |
8486 | continue; | |
8487 | ||
c152c796 AM |
8488 | if (r_symndx >= locsymcount |
8489 | || (elf_bad_symtab (input_bfd) | |
8490 | && finfo->sections[r_symndx] == NULL)) | |
8491 | { | |
c152c796 | 8492 | h = sym_hashes[r_symndx - extsymoff]; |
dce669a1 | 8493 | |
8c19749a NC |
8494 | /* Badly formatted input files can contain relocs that |
8495 | reference non-existant symbols. Check here so that | |
8496 | we do not seg fault. */ | |
8497 | if (h == NULL) | |
8498 | { | |
8499 | char buffer [32]; | |
8500 | ||
8501 | sprintf_vma (buffer, rel->r_info); | |
8502 | (*_bfd_error_handler) | |
8503 | (_("error: %B contains a reloc (0x%s) for section %A " | |
8504 | "that references a non-existent global symbol"), | |
8505 | input_bfd, o, buffer); | |
8506 | bfd_set_error (bfd_error_bad_value); | |
8507 | return FALSE; | |
8508 | } | |
3b36f7e6 | 8509 | |
c152c796 AM |
8510 | while (h->root.type == bfd_link_hash_indirect |
8511 | || h->root.type == bfd_link_hash_warning) | |
8512 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8513 | ||
cdd3575c AM |
8514 | if (h->root.type != bfd_link_hash_defined |
8515 | && h->root.type != bfd_link_hash_defweak) | |
8516 | continue; | |
8517 | ||
8518 | ps = &h->root.u.def.section; | |
8519 | sym_name = h->root.root.string; | |
c152c796 AM |
8520 | } |
8521 | else | |
8522 | { | |
cdd3575c AM |
8523 | Elf_Internal_Sym *sym = isymbuf + r_symndx; |
8524 | ps = &finfo->sections[r_symndx]; | |
26c61ae5 L |
8525 | sym_name = bfd_elf_sym_name (input_bfd, |
8526 | symtab_hdr, | |
8527 | sym, *ps); | |
cdd3575c | 8528 | } |
c152c796 | 8529 | |
cdd3575c AM |
8530 | /* Complain if the definition comes from a |
8531 | discarded section. */ | |
8532 | if ((sec = *ps) != NULL && elf_discarded_section (sec)) | |
8533 | { | |
87e5235d | 8534 | BFD_ASSERT (r_symndx != 0); |
9e66c942 | 8535 | if (action & COMPLAIN) |
e1fffbe6 AM |
8536 | (*finfo->info->callbacks->einfo) |
8537 | (_("%X`%s' referenced in section `%A' of %B: " | |
58ac56d0 | 8538 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 8539 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 8540 | |
87e5235d | 8541 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 8542 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
8543 | really defined in the kept linkonce section. |
8544 | FIXME: This is quite broken. Modifying the | |
8545 | symbol here means we will be changing all later | |
e0ae6d6f | 8546 | uses of the symbol, not just in this section. */ |
01b3c8ab | 8547 | if (action & PRETEND) |
87e5235d | 8548 | { |
01b3c8ab L |
8549 | asection *kept; |
8550 | ||
c0f00686 L |
8551 | kept = _bfd_elf_check_kept_section (sec, |
8552 | finfo->info); | |
01b3c8ab | 8553 | if (kept != NULL) |
87e5235d AM |
8554 | { |
8555 | *ps = kept; | |
8556 | continue; | |
8557 | } | |
8558 | } | |
c152c796 AM |
8559 | } |
8560 | } | |
8561 | } | |
8562 | ||
8563 | /* Relocate the section by invoking a back end routine. | |
8564 | ||
8565 | The back end routine is responsible for adjusting the | |
8566 | section contents as necessary, and (if using Rela relocs | |
8567 | and generating a relocatable output file) adjusting the | |
8568 | reloc addend as necessary. | |
8569 | ||
8570 | The back end routine does not have to worry about setting | |
8571 | the reloc address or the reloc symbol index. | |
8572 | ||
8573 | The back end routine is given a pointer to the swapped in | |
8574 | internal symbols, and can access the hash table entries | |
8575 | for the external symbols via elf_sym_hashes (input_bfd). | |
8576 | ||
8577 | When generating relocatable output, the back end routine | |
8578 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
8579 | output symbol is going to be a section symbol | |
8580 | corresponding to the output section, which will require | |
8581 | the addend to be adjusted. */ | |
8582 | ||
8583 | if (! (*relocate_section) (output_bfd, finfo->info, | |
8584 | input_bfd, o, contents, | |
8585 | internal_relocs, | |
8586 | isymbuf, | |
8587 | finfo->sections)) | |
8588 | return FALSE; | |
8589 | ||
8590 | if (emit_relocs) | |
8591 | { | |
8592 | Elf_Internal_Rela *irela; | |
8593 | Elf_Internal_Rela *irelaend; | |
8594 | bfd_vma last_offset; | |
8595 | struct elf_link_hash_entry **rel_hash; | |
eac338cf | 8596 | struct elf_link_hash_entry **rel_hash_list; |
c152c796 AM |
8597 | Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2; |
8598 | unsigned int next_erel; | |
c152c796 AM |
8599 | bfd_boolean rela_normal; |
8600 | ||
8601 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
8602 | rela_normal = (bed->rela_normal | |
8603 | && (input_rel_hdr->sh_entsize | |
8604 | == bed->s->sizeof_rela)); | |
8605 | ||
8606 | /* Adjust the reloc addresses and symbol indices. */ | |
8607 | ||
8608 | irela = internal_relocs; | |
8609 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
8610 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
8611 | + elf_section_data (o->output_section)->rel_count | |
8612 | + elf_section_data (o->output_section)->rel_count2); | |
eac338cf | 8613 | rel_hash_list = rel_hash; |
c152c796 AM |
8614 | last_offset = o->output_offset; |
8615 | if (!finfo->info->relocatable) | |
8616 | last_offset += o->output_section->vma; | |
8617 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
8618 | { | |
8619 | unsigned long r_symndx; | |
8620 | asection *sec; | |
8621 | Elf_Internal_Sym sym; | |
8622 | ||
8623 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
8624 | { | |
8625 | rel_hash++; | |
8626 | next_erel = 0; | |
8627 | } | |
8628 | ||
8629 | irela->r_offset = _bfd_elf_section_offset (output_bfd, | |
8630 | finfo->info, o, | |
8631 | irela->r_offset); | |
8632 | if (irela->r_offset >= (bfd_vma) -2) | |
8633 | { | |
8634 | /* This is a reloc for a deleted entry or somesuch. | |
8635 | Turn it into an R_*_NONE reloc, at the same | |
8636 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 8637 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
8638 | being ordered. */ |
8639 | irela->r_offset = last_offset; | |
8640 | irela->r_info = 0; | |
8641 | irela->r_addend = 0; | |
8642 | continue; | |
8643 | } | |
8644 | ||
8645 | irela->r_offset += o->output_offset; | |
8646 | ||
8647 | /* Relocs in an executable have to be virtual addresses. */ | |
8648 | if (!finfo->info->relocatable) | |
8649 | irela->r_offset += o->output_section->vma; | |
8650 | ||
8651 | last_offset = irela->r_offset; | |
8652 | ||
8653 | r_symndx = irela->r_info >> r_sym_shift; | |
8654 | if (r_symndx == STN_UNDEF) | |
8655 | continue; | |
8656 | ||
8657 | if (r_symndx >= locsymcount | |
8658 | || (elf_bad_symtab (input_bfd) | |
8659 | && finfo->sections[r_symndx] == NULL)) | |
8660 | { | |
8661 | struct elf_link_hash_entry *rh; | |
8662 | unsigned long indx; | |
8663 | ||
8664 | /* This is a reloc against a global symbol. We | |
8665 | have not yet output all the local symbols, so | |
8666 | we do not know the symbol index of any global | |
8667 | symbol. We set the rel_hash entry for this | |
8668 | reloc to point to the global hash table entry | |
8669 | for this symbol. The symbol index is then | |
ee75fd95 | 8670 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
8671 | indx = r_symndx - extsymoff; |
8672 | rh = elf_sym_hashes (input_bfd)[indx]; | |
8673 | while (rh->root.type == bfd_link_hash_indirect | |
8674 | || rh->root.type == bfd_link_hash_warning) | |
8675 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
8676 | ||
8677 | /* Setting the index to -2 tells | |
8678 | elf_link_output_extsym that this symbol is | |
8679 | used by a reloc. */ | |
8680 | BFD_ASSERT (rh->indx < 0); | |
8681 | rh->indx = -2; | |
8682 | ||
8683 | *rel_hash = rh; | |
8684 | ||
8685 | continue; | |
8686 | } | |
8687 | ||
8688 | /* This is a reloc against a local symbol. */ | |
8689 | ||
8690 | *rel_hash = NULL; | |
8691 | sym = isymbuf[r_symndx]; | |
8692 | sec = finfo->sections[r_symndx]; | |
8693 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) | |
8694 | { | |
8695 | /* I suppose the backend ought to fill in the | |
8696 | section of any STT_SECTION symbol against a | |
6a8d1586 AM |
8697 | processor specific section. */ |
8698 | r_symndx = 0; | |
8699 | if (bfd_is_abs_section (sec)) | |
8700 | ; | |
c152c796 AM |
8701 | else if (sec == NULL || sec->owner == NULL) |
8702 | { | |
8703 | bfd_set_error (bfd_error_bad_value); | |
8704 | return FALSE; | |
8705 | } | |
8706 | else | |
8707 | { | |
6a8d1586 AM |
8708 | asection *osec = sec->output_section; |
8709 | ||
8710 | /* If we have discarded a section, the output | |
8711 | section will be the absolute section. In | |
ab96bf03 AM |
8712 | case of discarded SEC_MERGE sections, use |
8713 | the kept section. relocate_section should | |
8714 | have already handled discarded linkonce | |
8715 | sections. */ | |
6a8d1586 AM |
8716 | if (bfd_is_abs_section (osec) |
8717 | && sec->kept_section != NULL | |
8718 | && sec->kept_section->output_section != NULL) | |
8719 | { | |
8720 | osec = sec->kept_section->output_section; | |
8721 | irela->r_addend -= osec->vma; | |
8722 | } | |
8723 | ||
8724 | if (!bfd_is_abs_section (osec)) | |
8725 | { | |
8726 | r_symndx = osec->target_index; | |
74541ad4 AM |
8727 | if (r_symndx == 0) |
8728 | { | |
8729 | struct elf_link_hash_table *htab; | |
8730 | asection *oi; | |
8731 | ||
8732 | htab = elf_hash_table (finfo->info); | |
8733 | oi = htab->text_index_section; | |
8734 | if ((osec->flags & SEC_READONLY) == 0 | |
8735 | && htab->data_index_section != NULL) | |
8736 | oi = htab->data_index_section; | |
8737 | ||
8738 | if (oi != NULL) | |
8739 | { | |
8740 | irela->r_addend += osec->vma - oi->vma; | |
8741 | r_symndx = oi->target_index; | |
8742 | } | |
8743 | } | |
8744 | ||
6a8d1586 AM |
8745 | BFD_ASSERT (r_symndx != 0); |
8746 | } | |
c152c796 AM |
8747 | } |
8748 | ||
8749 | /* Adjust the addend according to where the | |
8750 | section winds up in the output section. */ | |
8751 | if (rela_normal) | |
8752 | irela->r_addend += sec->output_offset; | |
8753 | } | |
8754 | else | |
8755 | { | |
8756 | if (finfo->indices[r_symndx] == -1) | |
8757 | { | |
8758 | unsigned long shlink; | |
8759 | const char *name; | |
8760 | asection *osec; | |
8761 | ||
8762 | if (finfo->info->strip == strip_all) | |
8763 | { | |
8764 | /* You can't do ld -r -s. */ | |
8765 | bfd_set_error (bfd_error_invalid_operation); | |
8766 | return FALSE; | |
8767 | } | |
8768 | ||
8769 | /* This symbol was skipped earlier, but | |
8770 | since it is needed by a reloc, we | |
8771 | must output it now. */ | |
8772 | shlink = symtab_hdr->sh_link; | |
8773 | name = (bfd_elf_string_from_elf_section | |
8774 | (input_bfd, shlink, sym.st_name)); | |
8775 | if (name == NULL) | |
8776 | return FALSE; | |
8777 | ||
8778 | osec = sec->output_section; | |
8779 | sym.st_shndx = | |
8780 | _bfd_elf_section_from_bfd_section (output_bfd, | |
8781 | osec); | |
8782 | if (sym.st_shndx == SHN_BAD) | |
8783 | return FALSE; | |
8784 | ||
8785 | sym.st_value += sec->output_offset; | |
8786 | if (! finfo->info->relocatable) | |
8787 | { | |
8788 | sym.st_value += osec->vma; | |
8789 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
8790 | { | |
8791 | /* STT_TLS symbols are relative to PT_TLS | |
8792 | segment base. */ | |
8793 | BFD_ASSERT (elf_hash_table (finfo->info) | |
8794 | ->tls_sec != NULL); | |
8795 | sym.st_value -= (elf_hash_table (finfo->info) | |
8796 | ->tls_sec->vma); | |
8797 | } | |
8798 | } | |
8799 | ||
8800 | finfo->indices[r_symndx] | |
8801 | = bfd_get_symcount (output_bfd); | |
8802 | ||
8803 | if (! elf_link_output_sym (finfo, name, &sym, sec, | |
8804 | NULL)) | |
8805 | return FALSE; | |
8806 | } | |
8807 | ||
8808 | r_symndx = finfo->indices[r_symndx]; | |
8809 | } | |
8810 | ||
8811 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
8812 | | (irela->r_info & r_type_mask)); | |
8813 | } | |
8814 | ||
8815 | /* Swap out the relocs. */ | |
c152c796 | 8816 | if (input_rel_hdr->sh_size != 0 |
eac338cf PB |
8817 | && !bed->elf_backend_emit_relocs (output_bfd, o, |
8818 | input_rel_hdr, | |
8819 | internal_relocs, | |
8820 | rel_hash_list)) | |
c152c796 AM |
8821 | return FALSE; |
8822 | ||
8823 | input_rel_hdr2 = elf_section_data (o)->rel_hdr2; | |
8824 | if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0) | |
8825 | { | |
8826 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) | |
8827 | * bed->s->int_rels_per_ext_rel); | |
eac338cf PB |
8828 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
8829 | if (!bed->elf_backend_emit_relocs (output_bfd, o, | |
8830 | input_rel_hdr2, | |
8831 | internal_relocs, | |
8832 | rel_hash_list)) | |
c152c796 AM |
8833 | return FALSE; |
8834 | } | |
8835 | } | |
8836 | } | |
8837 | ||
8838 | /* Write out the modified section contents. */ | |
8839 | if (bed->elf_backend_write_section | |
c7b8f16e JB |
8840 | && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o, |
8841 | contents)) | |
c152c796 AM |
8842 | { |
8843 | /* Section written out. */ | |
8844 | } | |
8845 | else switch (o->sec_info_type) | |
8846 | { | |
8847 | case ELF_INFO_TYPE_STABS: | |
8848 | if (! (_bfd_write_section_stabs | |
8849 | (output_bfd, | |
8850 | &elf_hash_table (finfo->info)->stab_info, | |
8851 | o, &elf_section_data (o)->sec_info, contents))) | |
8852 | return FALSE; | |
8853 | break; | |
8854 | case ELF_INFO_TYPE_MERGE: | |
8855 | if (! _bfd_write_merged_section (output_bfd, o, | |
8856 | elf_section_data (o)->sec_info)) | |
8857 | return FALSE; | |
8858 | break; | |
8859 | case ELF_INFO_TYPE_EH_FRAME: | |
8860 | { | |
8861 | if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, | |
8862 | o, contents)) | |
8863 | return FALSE; | |
8864 | } | |
8865 | break; | |
8866 | default: | |
8867 | { | |
c152c796 AM |
8868 | if (! (o->flags & SEC_EXCLUDE) |
8869 | && ! bfd_set_section_contents (output_bfd, o->output_section, | |
8870 | contents, | |
8871 | (file_ptr) o->output_offset, | |
eea6121a | 8872 | o->size)) |
c152c796 AM |
8873 | return FALSE; |
8874 | } | |
8875 | break; | |
8876 | } | |
8877 | } | |
8878 | ||
8879 | return TRUE; | |
8880 | } | |
8881 | ||
8882 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 8883 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
8884 | is used to build constructor and destructor tables when linking |
8885 | with -Ur. */ | |
8886 | ||
8887 | static bfd_boolean | |
8888 | elf_reloc_link_order (bfd *output_bfd, | |
8889 | struct bfd_link_info *info, | |
8890 | asection *output_section, | |
8891 | struct bfd_link_order *link_order) | |
8892 | { | |
8893 | reloc_howto_type *howto; | |
8894 | long indx; | |
8895 | bfd_vma offset; | |
8896 | bfd_vma addend; | |
8897 | struct elf_link_hash_entry **rel_hash_ptr; | |
8898 | Elf_Internal_Shdr *rel_hdr; | |
8899 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
8900 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
8901 | bfd_byte *erel; | |
8902 | unsigned int i; | |
8903 | ||
8904 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
8905 | if (howto == NULL) | |
8906 | { | |
8907 | bfd_set_error (bfd_error_bad_value); | |
8908 | return FALSE; | |
8909 | } | |
8910 | ||
8911 | addend = link_order->u.reloc.p->addend; | |
8912 | ||
8913 | /* Figure out the symbol index. */ | |
8914 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
8915 | + elf_section_data (output_section)->rel_count | |
8916 | + elf_section_data (output_section)->rel_count2); | |
8917 | if (link_order->type == bfd_section_reloc_link_order) | |
8918 | { | |
8919 | indx = link_order->u.reloc.p->u.section->target_index; | |
8920 | BFD_ASSERT (indx != 0); | |
8921 | *rel_hash_ptr = NULL; | |
8922 | } | |
8923 | else | |
8924 | { | |
8925 | struct elf_link_hash_entry *h; | |
8926 | ||
8927 | /* Treat a reloc against a defined symbol as though it were | |
8928 | actually against the section. */ | |
8929 | h = ((struct elf_link_hash_entry *) | |
8930 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
8931 | link_order->u.reloc.p->u.name, | |
8932 | FALSE, FALSE, TRUE)); | |
8933 | if (h != NULL | |
8934 | && (h->root.type == bfd_link_hash_defined | |
8935 | || h->root.type == bfd_link_hash_defweak)) | |
8936 | { | |
8937 | asection *section; | |
8938 | ||
8939 | section = h->root.u.def.section; | |
8940 | indx = section->output_section->target_index; | |
8941 | *rel_hash_ptr = NULL; | |
8942 | /* It seems that we ought to add the symbol value to the | |
8943 | addend here, but in practice it has already been added | |
8944 | because it was passed to constructor_callback. */ | |
8945 | addend += section->output_section->vma + section->output_offset; | |
8946 | } | |
8947 | else if (h != NULL) | |
8948 | { | |
8949 | /* Setting the index to -2 tells elf_link_output_extsym that | |
8950 | this symbol is used by a reloc. */ | |
8951 | h->indx = -2; | |
8952 | *rel_hash_ptr = h; | |
8953 | indx = 0; | |
8954 | } | |
8955 | else | |
8956 | { | |
8957 | if (! ((*info->callbacks->unattached_reloc) | |
8958 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) | |
8959 | return FALSE; | |
8960 | indx = 0; | |
8961 | } | |
8962 | } | |
8963 | ||
8964 | /* If this is an inplace reloc, we must write the addend into the | |
8965 | object file. */ | |
8966 | if (howto->partial_inplace && addend != 0) | |
8967 | { | |
8968 | bfd_size_type size; | |
8969 | bfd_reloc_status_type rstat; | |
8970 | bfd_byte *buf; | |
8971 | bfd_boolean ok; | |
8972 | const char *sym_name; | |
8973 | ||
8974 | size = bfd_get_reloc_size (howto); | |
8975 | buf = bfd_zmalloc (size); | |
8976 | if (buf == NULL) | |
8977 | return FALSE; | |
8978 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
8979 | switch (rstat) | |
8980 | { | |
8981 | case bfd_reloc_ok: | |
8982 | break; | |
8983 | ||
8984 | default: | |
8985 | case bfd_reloc_outofrange: | |
8986 | abort (); | |
8987 | ||
8988 | case bfd_reloc_overflow: | |
8989 | if (link_order->type == bfd_section_reloc_link_order) | |
8990 | sym_name = bfd_section_name (output_bfd, | |
8991 | link_order->u.reloc.p->u.section); | |
8992 | else | |
8993 | sym_name = link_order->u.reloc.p->u.name; | |
8994 | if (! ((*info->callbacks->reloc_overflow) | |
dfeffb9f L |
8995 | (info, NULL, sym_name, howto->name, addend, NULL, |
8996 | NULL, (bfd_vma) 0))) | |
c152c796 AM |
8997 | { |
8998 | free (buf); | |
8999 | return FALSE; | |
9000 | } | |
9001 | break; | |
9002 | } | |
9003 | ok = bfd_set_section_contents (output_bfd, output_section, buf, | |
9004 | link_order->offset, size); | |
9005 | free (buf); | |
9006 | if (! ok) | |
9007 | return FALSE; | |
9008 | } | |
9009 | ||
9010 | /* The address of a reloc is relative to the section in a | |
9011 | relocatable file, and is a virtual address in an executable | |
9012 | file. */ | |
9013 | offset = link_order->offset; | |
9014 | if (! info->relocatable) | |
9015 | offset += output_section->vma; | |
9016 | ||
9017 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
9018 | { | |
9019 | irel[i].r_offset = offset; | |
9020 | irel[i].r_info = 0; | |
9021 | irel[i].r_addend = 0; | |
9022 | } | |
9023 | if (bed->s->arch_size == 32) | |
9024 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
9025 | else | |
9026 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
9027 | ||
9028 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
9029 | erel = rel_hdr->contents; | |
9030 | if (rel_hdr->sh_type == SHT_REL) | |
9031 | { | |
9032 | erel += (elf_section_data (output_section)->rel_count | |
9033 | * bed->s->sizeof_rel); | |
9034 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); | |
9035 | } | |
9036 | else | |
9037 | { | |
9038 | irel[0].r_addend = addend; | |
9039 | erel += (elf_section_data (output_section)->rel_count | |
9040 | * bed->s->sizeof_rela); | |
9041 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); | |
9042 | } | |
9043 | ||
9044 | ++elf_section_data (output_section)->rel_count; | |
9045 | ||
9046 | return TRUE; | |
9047 | } | |
9048 | ||
0b52efa6 PB |
9049 | |
9050 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
9051 | ||
9052 | static bfd_vma | |
9053 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
9054 | { | |
9055 | Elf_Internal_Shdr **elf_shdrp; | |
9056 | asection *s; | |
9057 | int elfsec; | |
9058 | ||
9059 | s = p->u.indirect.section; | |
9060 | elf_shdrp = elf_elfsections (s->owner); | |
9061 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
9062 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
9063 | /* PR 290: |
9064 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 9065 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
9066 | sh_info fields. Hence we could get the situation |
9067 | where elfsec is 0. */ | |
9068 | if (elfsec == 0) | |
9069 | { | |
9070 | const struct elf_backend_data *bed | |
9071 | = get_elf_backend_data (s->owner); | |
9072 | if (bed->link_order_error_handler) | |
d003868e AM |
9073 | bed->link_order_error_handler |
9074 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); | |
185d09ad L |
9075 | return 0; |
9076 | } | |
9077 | else | |
9078 | { | |
9079 | s = elf_shdrp[elfsec]->bfd_section; | |
9080 | return s->output_section->vma + s->output_offset; | |
9081 | } | |
0b52efa6 PB |
9082 | } |
9083 | ||
9084 | ||
9085 | /* Compare two sections based on the locations of the sections they are | |
9086 | linked to. Used by elf_fixup_link_order. */ | |
9087 | ||
9088 | static int | |
9089 | compare_link_order (const void * a, const void * b) | |
9090 | { | |
9091 | bfd_vma apos; | |
9092 | bfd_vma bpos; | |
9093 | ||
9094 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
9095 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
9096 | if (apos < bpos) | |
9097 | return -1; | |
9098 | return apos > bpos; | |
9099 | } | |
9100 | ||
9101 | ||
9102 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
9103 | order as their linked sections. Returns false if this could not be done | |
9104 | because an output section includes both ordered and unordered | |
9105 | sections. Ideally we'd do this in the linker proper. */ | |
9106 | ||
9107 | static bfd_boolean | |
9108 | elf_fixup_link_order (bfd *abfd, asection *o) | |
9109 | { | |
9110 | int seen_linkorder; | |
9111 | int seen_other; | |
9112 | int n; | |
9113 | struct bfd_link_order *p; | |
9114 | bfd *sub; | |
9115 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 9116 | unsigned elfsec; |
0b52efa6 | 9117 | struct bfd_link_order **sections; |
d33cdfe3 | 9118 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 9119 | bfd_vma offset; |
3b36f7e6 | 9120 | |
d33cdfe3 L |
9121 | other_sec = NULL; |
9122 | linkorder_sec = NULL; | |
0b52efa6 PB |
9123 | seen_other = 0; |
9124 | seen_linkorder = 0; | |
8423293d | 9125 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 9126 | { |
d33cdfe3 | 9127 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
9128 | { |
9129 | s = p->u.indirect.section; | |
d33cdfe3 L |
9130 | sub = s->owner; |
9131 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
9132 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
9133 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
9134 | && elfsec < elf_numsections (sub) | |
0b52efa6 | 9135 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER) |
d33cdfe3 L |
9136 | { |
9137 | seen_linkorder++; | |
9138 | linkorder_sec = s; | |
9139 | } | |
0b52efa6 | 9140 | else |
d33cdfe3 L |
9141 | { |
9142 | seen_other++; | |
9143 | other_sec = s; | |
9144 | } | |
0b52efa6 PB |
9145 | } |
9146 | else | |
9147 | seen_other++; | |
d33cdfe3 L |
9148 | |
9149 | if (seen_other && seen_linkorder) | |
9150 | { | |
9151 | if (other_sec && linkorder_sec) | |
9152 | (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"), | |
9153 | o, linkorder_sec, | |
9154 | linkorder_sec->owner, other_sec, | |
9155 | other_sec->owner); | |
9156 | else | |
9157 | (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), | |
9158 | o); | |
9159 | bfd_set_error (bfd_error_bad_value); | |
9160 | return FALSE; | |
9161 | } | |
0b52efa6 PB |
9162 | } |
9163 | ||
9164 | if (!seen_linkorder) | |
9165 | return TRUE; | |
9166 | ||
0b52efa6 PB |
9167 | sections = (struct bfd_link_order **) |
9168 | xmalloc (seen_linkorder * sizeof (struct bfd_link_order *)); | |
9169 | seen_linkorder = 0; | |
3b36f7e6 | 9170 | |
8423293d | 9171 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
9172 | { |
9173 | sections[seen_linkorder++] = p; | |
9174 | } | |
9175 | /* Sort the input sections in the order of their linked section. */ | |
9176 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
9177 | compare_link_order); | |
9178 | ||
9179 | /* Change the offsets of the sections. */ | |
9180 | offset = 0; | |
9181 | for (n = 0; n < seen_linkorder; n++) | |
9182 | { | |
9183 | s = sections[n]->u.indirect.section; | |
9184 | offset &= ~(bfd_vma)((1 << s->alignment_power) - 1); | |
9185 | s->output_offset = offset; | |
9186 | sections[n]->offset = offset; | |
9187 | offset += sections[n]->size; | |
9188 | } | |
9189 | ||
9190 | return TRUE; | |
9191 | } | |
9192 | ||
9193 | ||
c152c796 AM |
9194 | /* Do the final step of an ELF link. */ |
9195 | ||
9196 | bfd_boolean | |
9197 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
9198 | { | |
9199 | bfd_boolean dynamic; | |
9200 | bfd_boolean emit_relocs; | |
9201 | bfd *dynobj; | |
9202 | struct elf_final_link_info finfo; | |
9203 | register asection *o; | |
9204 | register struct bfd_link_order *p; | |
9205 | register bfd *sub; | |
9206 | bfd_size_type max_contents_size; | |
9207 | bfd_size_type max_external_reloc_size; | |
9208 | bfd_size_type max_internal_reloc_count; | |
9209 | bfd_size_type max_sym_count; | |
9210 | bfd_size_type max_sym_shndx_count; | |
9211 | file_ptr off; | |
9212 | Elf_Internal_Sym elfsym; | |
9213 | unsigned int i; | |
9214 | Elf_Internal_Shdr *symtab_hdr; | |
9215 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
9216 | Elf_Internal_Shdr *symstrtab_hdr; | |
9217 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9218 | struct elf_outext_info eoinfo; | |
9219 | bfd_boolean merged; | |
9220 | size_t relativecount = 0; | |
9221 | asection *reldyn = 0; | |
9222 | bfd_size_type amt; | |
9223 | ||
9224 | if (! is_elf_hash_table (info->hash)) | |
9225 | return FALSE; | |
9226 | ||
9227 | if (info->shared) | |
9228 | abfd->flags |= DYNAMIC; | |
9229 | ||
9230 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
9231 | dynobj = elf_hash_table (info)->dynobj; | |
9232 | ||
9233 | emit_relocs = (info->relocatable | |
a4676736 | 9234 | || info->emitrelocations); |
c152c796 AM |
9235 | |
9236 | finfo.info = info; | |
9237 | finfo.output_bfd = abfd; | |
9238 | finfo.symstrtab = _bfd_elf_stringtab_init (); | |
9239 | if (finfo.symstrtab == NULL) | |
9240 | return FALSE; | |
9241 | ||
9242 | if (! dynamic) | |
9243 | { | |
9244 | finfo.dynsym_sec = NULL; | |
9245 | finfo.hash_sec = NULL; | |
9246 | finfo.symver_sec = NULL; | |
9247 | } | |
9248 | else | |
9249 | { | |
9250 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
9251 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
fdc90cb4 | 9252 | BFD_ASSERT (finfo.dynsym_sec != NULL); |
c152c796 AM |
9253 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); |
9254 | /* Note that it is OK if symver_sec is NULL. */ | |
9255 | } | |
9256 | ||
9257 | finfo.contents = NULL; | |
9258 | finfo.external_relocs = NULL; | |
9259 | finfo.internal_relocs = NULL; | |
9260 | finfo.external_syms = NULL; | |
9261 | finfo.locsym_shndx = NULL; | |
9262 | finfo.internal_syms = NULL; | |
9263 | finfo.indices = NULL; | |
9264 | finfo.sections = NULL; | |
9265 | finfo.symbuf = NULL; | |
9266 | finfo.symshndxbuf = NULL; | |
9267 | finfo.symbuf_count = 0; | |
9268 | finfo.shndxbuf_size = 0; | |
9269 | ||
9270 | /* Count up the number of relocations we will output for each output | |
9271 | section, so that we know the sizes of the reloc sections. We | |
9272 | also figure out some maximum sizes. */ | |
9273 | max_contents_size = 0; | |
9274 | max_external_reloc_size = 0; | |
9275 | max_internal_reloc_count = 0; | |
9276 | max_sym_count = 0; | |
9277 | max_sym_shndx_count = 0; | |
9278 | merged = FALSE; | |
9279 | for (o = abfd->sections; o != NULL; o = o->next) | |
9280 | { | |
9281 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
9282 | o->reloc_count = 0; | |
9283 | ||
8423293d | 9284 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
9285 | { |
9286 | unsigned int reloc_count = 0; | |
9287 | struct bfd_elf_section_data *esdi = NULL; | |
9288 | unsigned int *rel_count1; | |
9289 | ||
9290 | if (p->type == bfd_section_reloc_link_order | |
9291 | || p->type == bfd_symbol_reloc_link_order) | |
9292 | reloc_count = 1; | |
9293 | else if (p->type == bfd_indirect_link_order) | |
9294 | { | |
9295 | asection *sec; | |
9296 | ||
9297 | sec = p->u.indirect.section; | |
9298 | esdi = elf_section_data (sec); | |
9299 | ||
9300 | /* Mark all sections which are to be included in the | |
9301 | link. This will normally be every section. We need | |
9302 | to do this so that we can identify any sections which | |
9303 | the linker has decided to not include. */ | |
9304 | sec->linker_mark = TRUE; | |
9305 | ||
9306 | if (sec->flags & SEC_MERGE) | |
9307 | merged = TRUE; | |
9308 | ||
9309 | if (info->relocatable || info->emitrelocations) | |
9310 | reloc_count = sec->reloc_count; | |
9311 | else if (bed->elf_backend_count_relocs) | |
9312 | { | |
9313 | Elf_Internal_Rela * relocs; | |
9314 | ||
1182cb93 AM |
9315 | relocs = _bfd_elf_link_read_relocs (sec->owner, sec, |
9316 | NULL, NULL, | |
c152c796 AM |
9317 | info->keep_memory); |
9318 | ||
1182cb93 AM |
9319 | if (relocs != NULL) |
9320 | { | |
9321 | reloc_count | |
9322 | = (*bed->elf_backend_count_relocs) (sec, relocs); | |
c152c796 | 9323 | |
1182cb93 AM |
9324 | if (elf_section_data (sec)->relocs != relocs) |
9325 | free (relocs); | |
9326 | } | |
c152c796 AM |
9327 | } |
9328 | ||
eea6121a AM |
9329 | if (sec->rawsize > max_contents_size) |
9330 | max_contents_size = sec->rawsize; | |
9331 | if (sec->size > max_contents_size) | |
9332 | max_contents_size = sec->size; | |
c152c796 AM |
9333 | |
9334 | /* We are interested in just local symbols, not all | |
9335 | symbols. */ | |
9336 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
9337 | && (sec->owner->flags & DYNAMIC) == 0) | |
9338 | { | |
9339 | size_t sym_count; | |
9340 | ||
9341 | if (elf_bad_symtab (sec->owner)) | |
9342 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
9343 | / bed->s->sizeof_sym); | |
9344 | else | |
9345 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
9346 | ||
9347 | if (sym_count > max_sym_count) | |
9348 | max_sym_count = sym_count; | |
9349 | ||
9350 | if (sym_count > max_sym_shndx_count | |
9351 | && elf_symtab_shndx (sec->owner) != 0) | |
9352 | max_sym_shndx_count = sym_count; | |
9353 | ||
9354 | if ((sec->flags & SEC_RELOC) != 0) | |
9355 | { | |
9356 | size_t ext_size; | |
9357 | ||
9358 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
9359 | if (ext_size > max_external_reloc_size) | |
9360 | max_external_reloc_size = ext_size; | |
9361 | if (sec->reloc_count > max_internal_reloc_count) | |
9362 | max_internal_reloc_count = sec->reloc_count; | |
9363 | } | |
9364 | } | |
9365 | } | |
9366 | ||
9367 | if (reloc_count == 0) | |
9368 | continue; | |
9369 | ||
9370 | o->reloc_count += reloc_count; | |
9371 | ||
9372 | /* MIPS may have a mix of REL and RELA relocs on sections. | |
9373 | To support this curious ABI we keep reloc counts in | |
9374 | elf_section_data too. We must be careful to add the | |
9375 | relocations from the input section to the right output | |
9376 | count. FIXME: Get rid of one count. We have | |
9377 | o->reloc_count == esdo->rel_count + esdo->rel_count2. */ | |
9378 | rel_count1 = &esdo->rel_count; | |
9379 | if (esdi != NULL) | |
9380 | { | |
9381 | bfd_boolean same_size; | |
9382 | bfd_size_type entsize1; | |
9383 | ||
9384 | entsize1 = esdi->rel_hdr.sh_entsize; | |
9385 | BFD_ASSERT (entsize1 == bed->s->sizeof_rel | |
9386 | || entsize1 == bed->s->sizeof_rela); | |
9387 | same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel); | |
9388 | ||
9389 | if (!same_size) | |
9390 | rel_count1 = &esdo->rel_count2; | |
9391 | ||
9392 | if (esdi->rel_hdr2 != NULL) | |
9393 | { | |
9394 | bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize; | |
9395 | unsigned int alt_count; | |
9396 | unsigned int *rel_count2; | |
9397 | ||
9398 | BFD_ASSERT (entsize2 != entsize1 | |
9399 | && (entsize2 == bed->s->sizeof_rel | |
9400 | || entsize2 == bed->s->sizeof_rela)); | |
9401 | ||
9402 | rel_count2 = &esdo->rel_count2; | |
9403 | if (!same_size) | |
9404 | rel_count2 = &esdo->rel_count; | |
9405 | ||
9406 | /* The following is probably too simplistic if the | |
9407 | backend counts output relocs unusually. */ | |
9408 | BFD_ASSERT (bed->elf_backend_count_relocs == NULL); | |
9409 | alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2); | |
9410 | *rel_count2 += alt_count; | |
9411 | reloc_count -= alt_count; | |
9412 | } | |
9413 | } | |
9414 | *rel_count1 += reloc_count; | |
9415 | } | |
9416 | ||
9417 | if (o->reloc_count > 0) | |
9418 | o->flags |= SEC_RELOC; | |
9419 | else | |
9420 | { | |
9421 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
9422 | set it (this is probably a bug) and if it is set | |
9423 | assign_section_numbers will create a reloc section. */ | |
9424 | o->flags &=~ SEC_RELOC; | |
9425 | } | |
9426 | ||
9427 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
9428 | zero. This is done in elf_fake_sections as well, but forcing | |
9429 | the VMA to 0 here will ensure that relocs against these | |
9430 | sections are handled correctly. */ | |
9431 | if ((o->flags & SEC_ALLOC) == 0 | |
9432 | && ! o->user_set_vma) | |
9433 | o->vma = 0; | |
9434 | } | |
9435 | ||
9436 | if (! info->relocatable && merged) | |
9437 | elf_link_hash_traverse (elf_hash_table (info), | |
9438 | _bfd_elf_link_sec_merge_syms, abfd); | |
9439 | ||
9440 | /* Figure out the file positions for everything but the symbol table | |
9441 | and the relocs. We set symcount to force assign_section_numbers | |
9442 | to create a symbol table. */ | |
9443 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; | |
9444 | BFD_ASSERT (! abfd->output_has_begun); | |
9445 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
9446 | goto error_return; | |
9447 | ||
ee75fd95 | 9448 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
9449 | for (o = abfd->sections; o != NULL; o = o->next) |
9450 | { | |
9451 | if ((o->flags & SEC_RELOC) != 0) | |
9452 | { | |
9453 | if (!(_bfd_elf_link_size_reloc_section | |
9454 | (abfd, &elf_section_data (o)->rel_hdr, o))) | |
9455 | goto error_return; | |
9456 | ||
9457 | if (elf_section_data (o)->rel_hdr2 | |
9458 | && !(_bfd_elf_link_size_reloc_section | |
9459 | (abfd, elf_section_data (o)->rel_hdr2, o))) | |
9460 | goto error_return; | |
9461 | } | |
9462 | ||
9463 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
9464 | to count upwards while actually outputting the relocations. */ | |
9465 | elf_section_data (o)->rel_count = 0; | |
9466 | elf_section_data (o)->rel_count2 = 0; | |
9467 | } | |
9468 | ||
9469 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
9470 | ||
9471 | /* We have now assigned file positions for all the sections except | |
9472 | .symtab and .strtab. We start the .symtab section at the current | |
9473 | file position, and write directly to it. We build the .strtab | |
9474 | section in memory. */ | |
9475 | bfd_get_symcount (abfd) = 0; | |
9476 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
9477 | /* sh_name is set in prep_headers. */ | |
9478 | symtab_hdr->sh_type = SHT_SYMTAB; | |
9479 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
9480 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
9481 | /* sh_link is set in assign_section_numbers. */ | |
9482 | /* sh_info is set below. */ | |
9483 | /* sh_offset is set just below. */ | |
9484 | symtab_hdr->sh_addralign = 1 << bed->s->log_file_align; | |
9485 | ||
9486 | off = elf_tdata (abfd)->next_file_pos; | |
9487 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
9488 | ||
9489 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
9490 | incorrect. We do not yet know the size of the .symtab section. | |
9491 | We correct next_file_pos below, after we do know the size. */ | |
9492 | ||
9493 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
9494 | continuously seeking to the right position in the file. */ | |
9495 | if (! info->keep_memory || max_sym_count < 20) | |
9496 | finfo.symbuf_size = 20; | |
9497 | else | |
9498 | finfo.symbuf_size = max_sym_count; | |
9499 | amt = finfo.symbuf_size; | |
9500 | amt *= bed->s->sizeof_sym; | |
9501 | finfo.symbuf = bfd_malloc (amt); | |
9502 | if (finfo.symbuf == NULL) | |
9503 | goto error_return; | |
9504 | if (elf_numsections (abfd) > SHN_LORESERVE) | |
9505 | { | |
9506 | /* Wild guess at number of output symbols. realloc'd as needed. */ | |
9507 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
9508 | finfo.shndxbuf_size = amt; | |
9509 | amt *= sizeof (Elf_External_Sym_Shndx); | |
9510 | finfo.symshndxbuf = bfd_zmalloc (amt); | |
9511 | if (finfo.symshndxbuf == NULL) | |
9512 | goto error_return; | |
9513 | } | |
9514 | ||
9515 | /* Start writing out the symbol table. The first symbol is always a | |
9516 | dummy symbol. */ | |
9517 | if (info->strip != strip_all | |
9518 | || emit_relocs) | |
9519 | { | |
9520 | elfsym.st_value = 0; | |
9521 | elfsym.st_size = 0; | |
9522 | elfsym.st_info = 0; | |
9523 | elfsym.st_other = 0; | |
9524 | elfsym.st_shndx = SHN_UNDEF; | |
9525 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr, | |
9526 | NULL)) | |
9527 | goto error_return; | |
9528 | } | |
9529 | ||
c152c796 AM |
9530 | /* Output a symbol for each section. We output these even if we are |
9531 | discarding local symbols, since they are used for relocs. These | |
9532 | symbols have no names. We store the index of each one in the | |
9533 | index field of the section, so that we can find it again when | |
9534 | outputting relocs. */ | |
9535 | if (info->strip != strip_all | |
9536 | || emit_relocs) | |
9537 | { | |
9538 | elfsym.st_size = 0; | |
9539 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
9540 | elfsym.st_other = 0; | |
f0b5bb34 | 9541 | elfsym.st_value = 0; |
c152c796 AM |
9542 | for (i = 1; i < elf_numsections (abfd); i++) |
9543 | { | |
9544 | o = bfd_section_from_elf_index (abfd, i); | |
9545 | if (o != NULL) | |
f0b5bb34 AM |
9546 | { |
9547 | o->target_index = bfd_get_symcount (abfd); | |
9548 | elfsym.st_shndx = i; | |
9549 | if (!info->relocatable) | |
9550 | elfsym.st_value = o->vma; | |
9551 | if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL)) | |
9552 | goto error_return; | |
9553 | } | |
c152c796 AM |
9554 | if (i == SHN_LORESERVE - 1) |
9555 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
9556 | } | |
9557 | } | |
9558 | ||
9559 | /* Allocate some memory to hold information read in from the input | |
9560 | files. */ | |
9561 | if (max_contents_size != 0) | |
9562 | { | |
9563 | finfo.contents = bfd_malloc (max_contents_size); | |
9564 | if (finfo.contents == NULL) | |
9565 | goto error_return; | |
9566 | } | |
9567 | ||
9568 | if (max_external_reloc_size != 0) | |
9569 | { | |
9570 | finfo.external_relocs = bfd_malloc (max_external_reloc_size); | |
9571 | if (finfo.external_relocs == NULL) | |
9572 | goto error_return; | |
9573 | } | |
9574 | ||
9575 | if (max_internal_reloc_count != 0) | |
9576 | { | |
9577 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
9578 | amt *= sizeof (Elf_Internal_Rela); | |
9579 | finfo.internal_relocs = bfd_malloc (amt); | |
9580 | if (finfo.internal_relocs == NULL) | |
9581 | goto error_return; | |
9582 | } | |
9583 | ||
9584 | if (max_sym_count != 0) | |
9585 | { | |
9586 | amt = max_sym_count * bed->s->sizeof_sym; | |
9587 | finfo.external_syms = bfd_malloc (amt); | |
9588 | if (finfo.external_syms == NULL) | |
9589 | goto error_return; | |
9590 | ||
9591 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
9592 | finfo.internal_syms = bfd_malloc (amt); | |
9593 | if (finfo.internal_syms == NULL) | |
9594 | goto error_return; | |
9595 | ||
9596 | amt = max_sym_count * sizeof (long); | |
9597 | finfo.indices = bfd_malloc (amt); | |
9598 | if (finfo.indices == NULL) | |
9599 | goto error_return; | |
9600 | ||
9601 | amt = max_sym_count * sizeof (asection *); | |
9602 | finfo.sections = bfd_malloc (amt); | |
9603 | if (finfo.sections == NULL) | |
9604 | goto error_return; | |
9605 | } | |
9606 | ||
9607 | if (max_sym_shndx_count != 0) | |
9608 | { | |
9609 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
9610 | finfo.locsym_shndx = bfd_malloc (amt); | |
9611 | if (finfo.locsym_shndx == NULL) | |
9612 | goto error_return; | |
9613 | } | |
9614 | ||
9615 | if (elf_hash_table (info)->tls_sec) | |
9616 | { | |
9617 | bfd_vma base, end = 0; | |
9618 | asection *sec; | |
9619 | ||
9620 | for (sec = elf_hash_table (info)->tls_sec; | |
9621 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
9622 | sec = sec->next) | |
9623 | { | |
3a800eb9 | 9624 | bfd_size_type size = sec->size; |
c152c796 | 9625 | |
3a800eb9 AM |
9626 | if (size == 0 |
9627 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 9628 | { |
3a800eb9 AM |
9629 | struct bfd_link_order *o = sec->map_tail.link_order; |
9630 | if (o != NULL) | |
9631 | size = o->offset + o->size; | |
c152c796 AM |
9632 | } |
9633 | end = sec->vma + size; | |
9634 | } | |
9635 | base = elf_hash_table (info)->tls_sec->vma; | |
9636 | end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power); | |
9637 | elf_hash_table (info)->tls_size = end - base; | |
9638 | } | |
9639 | ||
0b52efa6 PB |
9640 | /* Reorder SHF_LINK_ORDER sections. */ |
9641 | for (o = abfd->sections; o != NULL; o = o->next) | |
9642 | { | |
9643 | if (!elf_fixup_link_order (abfd, o)) | |
9644 | return FALSE; | |
9645 | } | |
9646 | ||
c152c796 AM |
9647 | /* Since ELF permits relocations to be against local symbols, we |
9648 | must have the local symbols available when we do the relocations. | |
9649 | Since we would rather only read the local symbols once, and we | |
9650 | would rather not keep them in memory, we handle all the | |
9651 | relocations for a single input file at the same time. | |
9652 | ||
9653 | Unfortunately, there is no way to know the total number of local | |
9654 | symbols until we have seen all of them, and the local symbol | |
9655 | indices precede the global symbol indices. This means that when | |
9656 | we are generating relocatable output, and we see a reloc against | |
9657 | a global symbol, we can not know the symbol index until we have | |
9658 | finished examining all the local symbols to see which ones we are | |
9659 | going to output. To deal with this, we keep the relocations in | |
9660 | memory, and don't output them until the end of the link. This is | |
9661 | an unfortunate waste of memory, but I don't see a good way around | |
9662 | it. Fortunately, it only happens when performing a relocatable | |
9663 | link, which is not the common case. FIXME: If keep_memory is set | |
9664 | we could write the relocs out and then read them again; I don't | |
9665 | know how bad the memory loss will be. */ | |
9666 | ||
9667 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
9668 | sub->output_has_begun = FALSE; | |
9669 | for (o = abfd->sections; o != NULL; o = o->next) | |
9670 | { | |
8423293d | 9671 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
9672 | { |
9673 | if (p->type == bfd_indirect_link_order | |
9674 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
9675 | == bfd_target_elf_flavour) | |
9676 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
9677 | { | |
9678 | if (! sub->output_has_begun) | |
9679 | { | |
9680 | if (! elf_link_input_bfd (&finfo, sub)) | |
9681 | goto error_return; | |
9682 | sub->output_has_begun = TRUE; | |
9683 | } | |
9684 | } | |
9685 | else if (p->type == bfd_section_reloc_link_order | |
9686 | || p->type == bfd_symbol_reloc_link_order) | |
9687 | { | |
9688 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
9689 | goto error_return; | |
9690 | } | |
9691 | else | |
9692 | { | |
9693 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
9694 | goto error_return; | |
9695 | } | |
9696 | } | |
9697 | } | |
9698 | ||
c0f00686 L |
9699 | /* Free symbol buffer if needed. */ |
9700 | if (!info->reduce_memory_overheads) | |
9701 | { | |
9702 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
3fcd97f1 JJ |
9703 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
9704 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
9705 | { |
9706 | free (elf_tdata (sub)->symbuf); | |
9707 | elf_tdata (sub)->symbuf = NULL; | |
9708 | } | |
9709 | } | |
9710 | ||
c152c796 AM |
9711 | /* Output any global symbols that got converted to local in a |
9712 | version script or due to symbol visibility. We do this in a | |
9713 | separate step since ELF requires all local symbols to appear | |
9714 | prior to any global symbols. FIXME: We should only do this if | |
9715 | some global symbols were, in fact, converted to become local. | |
9716 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
9717 | eoinfo.failed = FALSE; | |
9718 | eoinfo.finfo = &finfo; | |
9719 | eoinfo.localsyms = TRUE; | |
9720 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
9721 | &eoinfo); | |
9722 | if (eoinfo.failed) | |
9723 | return FALSE; | |
9724 | ||
4e617b1e PB |
9725 | /* If backend needs to output some local symbols not present in the hash |
9726 | table, do it now. */ | |
9727 | if (bed->elf_backend_output_arch_local_syms) | |
9728 | { | |
9729 | typedef bfd_boolean (*out_sym_func) | |
9730 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
9731 | struct elf_link_hash_entry *); | |
9732 | ||
9733 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
9734 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
9735 | return FALSE; | |
9736 | } | |
9737 | ||
c152c796 AM |
9738 | /* That wrote out all the local symbols. Finish up the symbol table |
9739 | with the global symbols. Even if we want to strip everything we | |
9740 | can, we still need to deal with those global symbols that got | |
9741 | converted to local in a version script. */ | |
9742 | ||
9743 | /* The sh_info field records the index of the first non local symbol. */ | |
9744 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
9745 | ||
9746 | if (dynamic | |
9747 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
9748 | { | |
9749 | Elf_Internal_Sym sym; | |
9750 | bfd_byte *dynsym = finfo.dynsym_sec->contents; | |
9751 | long last_local = 0; | |
9752 | ||
9753 | /* Write out the section symbols for the output sections. */ | |
67687978 | 9754 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
c152c796 AM |
9755 | { |
9756 | asection *s; | |
9757 | ||
9758 | sym.st_size = 0; | |
9759 | sym.st_name = 0; | |
9760 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
9761 | sym.st_other = 0; | |
9762 | ||
9763 | for (s = abfd->sections; s != NULL; s = s->next) | |
9764 | { | |
9765 | int indx; | |
9766 | bfd_byte *dest; | |
9767 | long dynindx; | |
9768 | ||
c152c796 | 9769 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
9770 | if (dynindx <= 0) |
9771 | continue; | |
9772 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
9773 | BFD_ASSERT (indx > 0); |
9774 | sym.st_shndx = indx; | |
c0d5a53d L |
9775 | if (! check_dynsym (abfd, &sym)) |
9776 | return FALSE; | |
c152c796 AM |
9777 | sym.st_value = s->vma; |
9778 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
8c37241b JJ |
9779 | if (last_local < dynindx) |
9780 | last_local = dynindx; | |
c152c796 AM |
9781 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
9782 | } | |
c152c796 AM |
9783 | } |
9784 | ||
9785 | /* Write out the local dynsyms. */ | |
9786 | if (elf_hash_table (info)->dynlocal) | |
9787 | { | |
9788 | struct elf_link_local_dynamic_entry *e; | |
9789 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
9790 | { | |
9791 | asection *s; | |
9792 | bfd_byte *dest; | |
9793 | ||
9794 | sym.st_size = e->isym.st_size; | |
9795 | sym.st_other = e->isym.st_other; | |
9796 | ||
9797 | /* Copy the internal symbol as is. | |
9798 | Note that we saved a word of storage and overwrote | |
9799 | the original st_name with the dynstr_index. */ | |
9800 | sym = e->isym; | |
9801 | ||
9802 | if (e->isym.st_shndx != SHN_UNDEF | |
9803 | && (e->isym.st_shndx < SHN_LORESERVE | |
9804 | || e->isym.st_shndx > SHN_HIRESERVE)) | |
9805 | { | |
9806 | s = bfd_section_from_elf_index (e->input_bfd, | |
9807 | e->isym.st_shndx); | |
9808 | ||
9809 | sym.st_shndx = | |
9810 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
9811 | if (! check_dynsym (abfd, &sym)) |
9812 | return FALSE; | |
c152c796 AM |
9813 | sym.st_value = (s->output_section->vma |
9814 | + s->output_offset | |
9815 | + e->isym.st_value); | |
9816 | } | |
9817 | ||
9818 | if (last_local < e->dynindx) | |
9819 | last_local = e->dynindx; | |
9820 | ||
9821 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
9822 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
9823 | } | |
9824 | } | |
9825 | ||
9826 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = | |
9827 | last_local + 1; | |
9828 | } | |
9829 | ||
9830 | /* We get the global symbols from the hash table. */ | |
9831 | eoinfo.failed = FALSE; | |
9832 | eoinfo.localsyms = FALSE; | |
9833 | eoinfo.finfo = &finfo; | |
9834 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
9835 | &eoinfo); | |
9836 | if (eoinfo.failed) | |
9837 | return FALSE; | |
9838 | ||
9839 | /* If backend needs to output some symbols not present in the hash | |
9840 | table, do it now. */ | |
9841 | if (bed->elf_backend_output_arch_syms) | |
9842 | { | |
9843 | typedef bfd_boolean (*out_sym_func) | |
9844 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
9845 | struct elf_link_hash_entry *); | |
9846 | ||
9847 | if (! ((*bed->elf_backend_output_arch_syms) | |
9848 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
9849 | return FALSE; | |
9850 | } | |
9851 | ||
9852 | /* Flush all symbols to the file. */ | |
9853 | if (! elf_link_flush_output_syms (&finfo, bed)) | |
9854 | return FALSE; | |
9855 | ||
9856 | /* Now we know the size of the symtab section. */ | |
9857 | off += symtab_hdr->sh_size; | |
9858 | ||
9859 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
9860 | if (symtab_shndx_hdr->sh_name != 0) | |
9861 | { | |
9862 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
9863 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
9864 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
9865 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
9866 | symtab_shndx_hdr->sh_size = amt; | |
9867 | ||
9868 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
9869 | off, TRUE); | |
9870 | ||
9871 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
9872 | || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) | |
9873 | return FALSE; | |
9874 | } | |
9875 | ||
9876 | ||
9877 | /* Finish up and write out the symbol string table (.strtab) | |
9878 | section. */ | |
9879 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
9880 | /* sh_name was set in prep_headers. */ | |
9881 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
9882 | symstrtab_hdr->sh_flags = 0; | |
9883 | symstrtab_hdr->sh_addr = 0; | |
9884 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
9885 | symstrtab_hdr->sh_entsize = 0; | |
9886 | symstrtab_hdr->sh_link = 0; | |
9887 | symstrtab_hdr->sh_info = 0; | |
9888 | /* sh_offset is set just below. */ | |
9889 | symstrtab_hdr->sh_addralign = 1; | |
9890 | ||
9891 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); | |
9892 | elf_tdata (abfd)->next_file_pos = off; | |
9893 | ||
9894 | if (bfd_get_symcount (abfd) > 0) | |
9895 | { | |
9896 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
9897 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
9898 | return FALSE; | |
9899 | } | |
9900 | ||
9901 | /* Adjust the relocs to have the correct symbol indices. */ | |
9902 | for (o = abfd->sections; o != NULL; o = o->next) | |
9903 | { | |
9904 | if ((o->flags & SEC_RELOC) == 0) | |
9905 | continue; | |
9906 | ||
9907 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, | |
9908 | elf_section_data (o)->rel_count, | |
9909 | elf_section_data (o)->rel_hashes); | |
9910 | if (elf_section_data (o)->rel_hdr2 != NULL) | |
9911 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, | |
9912 | elf_section_data (o)->rel_count2, | |
9913 | (elf_section_data (o)->rel_hashes | |
9914 | + elf_section_data (o)->rel_count)); | |
9915 | ||
9916 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
9917 | trying to swap the relocs out itself. */ | |
9918 | o->reloc_count = 0; | |
9919 | } | |
9920 | ||
9921 | if (dynamic && info->combreloc && dynobj != NULL) | |
9922 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
9923 | ||
9924 | /* If we are linking against a dynamic object, or generating a | |
9925 | shared library, finish up the dynamic linking information. */ | |
9926 | if (dynamic) | |
9927 | { | |
9928 | bfd_byte *dyncon, *dynconend; | |
9929 | ||
9930 | /* Fix up .dynamic entries. */ | |
9931 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
9932 | BFD_ASSERT (o != NULL); | |
9933 | ||
9934 | dyncon = o->contents; | |
eea6121a | 9935 | dynconend = o->contents + o->size; |
c152c796 AM |
9936 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
9937 | { | |
9938 | Elf_Internal_Dyn dyn; | |
9939 | const char *name; | |
9940 | unsigned int type; | |
9941 | ||
9942 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
9943 | ||
9944 | switch (dyn.d_tag) | |
9945 | { | |
9946 | default: | |
9947 | continue; | |
9948 | case DT_NULL: | |
9949 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
9950 | { | |
9951 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
9952 | { | |
9953 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
9954 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
9955 | default: continue; | |
9956 | } | |
9957 | dyn.d_un.d_val = relativecount; | |
9958 | relativecount = 0; | |
9959 | break; | |
9960 | } | |
9961 | continue; | |
9962 | ||
9963 | case DT_INIT: | |
9964 | name = info->init_function; | |
9965 | goto get_sym; | |
9966 | case DT_FINI: | |
9967 | name = info->fini_function; | |
9968 | get_sym: | |
9969 | { | |
9970 | struct elf_link_hash_entry *h; | |
9971 | ||
9972 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
9973 | FALSE, FALSE, TRUE); | |
9974 | if (h != NULL | |
9975 | && (h->root.type == bfd_link_hash_defined | |
9976 | || h->root.type == bfd_link_hash_defweak)) | |
9977 | { | |
9978 | dyn.d_un.d_val = h->root.u.def.value; | |
9979 | o = h->root.u.def.section; | |
9980 | if (o->output_section != NULL) | |
9981 | dyn.d_un.d_val += (o->output_section->vma | |
9982 | + o->output_offset); | |
9983 | else | |
9984 | { | |
9985 | /* The symbol is imported from another shared | |
9986 | library and does not apply to this one. */ | |
9987 | dyn.d_un.d_val = 0; | |
9988 | } | |
9989 | break; | |
9990 | } | |
9991 | } | |
9992 | continue; | |
9993 | ||
9994 | case DT_PREINIT_ARRAYSZ: | |
9995 | name = ".preinit_array"; | |
9996 | goto get_size; | |
9997 | case DT_INIT_ARRAYSZ: | |
9998 | name = ".init_array"; | |
9999 | goto get_size; | |
10000 | case DT_FINI_ARRAYSZ: | |
10001 | name = ".fini_array"; | |
10002 | get_size: | |
10003 | o = bfd_get_section_by_name (abfd, name); | |
10004 | if (o == NULL) | |
10005 | { | |
10006 | (*_bfd_error_handler) | |
d003868e | 10007 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
10008 | goto error_return; |
10009 | } | |
eea6121a | 10010 | if (o->size == 0) |
c152c796 AM |
10011 | (*_bfd_error_handler) |
10012 | (_("warning: %s section has zero size"), name); | |
eea6121a | 10013 | dyn.d_un.d_val = o->size; |
c152c796 AM |
10014 | break; |
10015 | ||
10016 | case DT_PREINIT_ARRAY: | |
10017 | name = ".preinit_array"; | |
10018 | goto get_vma; | |
10019 | case DT_INIT_ARRAY: | |
10020 | name = ".init_array"; | |
10021 | goto get_vma; | |
10022 | case DT_FINI_ARRAY: | |
10023 | name = ".fini_array"; | |
10024 | goto get_vma; | |
10025 | ||
10026 | case DT_HASH: | |
10027 | name = ".hash"; | |
10028 | goto get_vma; | |
fdc90cb4 JJ |
10029 | case DT_GNU_HASH: |
10030 | name = ".gnu.hash"; | |
10031 | goto get_vma; | |
c152c796 AM |
10032 | case DT_STRTAB: |
10033 | name = ".dynstr"; | |
10034 | goto get_vma; | |
10035 | case DT_SYMTAB: | |
10036 | name = ".dynsym"; | |
10037 | goto get_vma; | |
10038 | case DT_VERDEF: | |
10039 | name = ".gnu.version_d"; | |
10040 | goto get_vma; | |
10041 | case DT_VERNEED: | |
10042 | name = ".gnu.version_r"; | |
10043 | goto get_vma; | |
10044 | case DT_VERSYM: | |
10045 | name = ".gnu.version"; | |
10046 | get_vma: | |
10047 | o = bfd_get_section_by_name (abfd, name); | |
10048 | if (o == NULL) | |
10049 | { | |
10050 | (*_bfd_error_handler) | |
d003868e | 10051 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
10052 | goto error_return; |
10053 | } | |
10054 | dyn.d_un.d_ptr = o->vma; | |
10055 | break; | |
10056 | ||
10057 | case DT_REL: | |
10058 | case DT_RELA: | |
10059 | case DT_RELSZ: | |
10060 | case DT_RELASZ: | |
10061 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
10062 | type = SHT_REL; | |
10063 | else | |
10064 | type = SHT_RELA; | |
10065 | dyn.d_un.d_val = 0; | |
10066 | for (i = 1; i < elf_numsections (abfd); i++) | |
10067 | { | |
10068 | Elf_Internal_Shdr *hdr; | |
10069 | ||
10070 | hdr = elf_elfsections (abfd)[i]; | |
10071 | if (hdr->sh_type == type | |
10072 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
10073 | { | |
10074 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
10075 | dyn.d_un.d_val += hdr->sh_size; | |
10076 | else | |
10077 | { | |
10078 | if (dyn.d_un.d_val == 0 | |
10079 | || hdr->sh_addr < dyn.d_un.d_val) | |
10080 | dyn.d_un.d_val = hdr->sh_addr; | |
10081 | } | |
10082 | } | |
10083 | } | |
10084 | break; | |
10085 | } | |
10086 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
10087 | } | |
10088 | } | |
10089 | ||
10090 | /* If we have created any dynamic sections, then output them. */ | |
10091 | if (dynobj != NULL) | |
10092 | { | |
10093 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
10094 | goto error_return; | |
10095 | ||
943284cc DJ |
10096 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
10097 | if (info->warn_shared_textrel && info->shared) | |
10098 | { | |
10099 | bfd_byte *dyncon, *dynconend; | |
10100 | ||
10101 | /* Fix up .dynamic entries. */ | |
10102 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
10103 | BFD_ASSERT (o != NULL); | |
10104 | ||
10105 | dyncon = o->contents; | |
10106 | dynconend = o->contents + o->size; | |
10107 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
10108 | { | |
10109 | Elf_Internal_Dyn dyn; | |
10110 | ||
10111 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
10112 | ||
10113 | if (dyn.d_tag == DT_TEXTREL) | |
10114 | { | |
9267588c L |
10115 | info->callbacks->einfo |
10116 | (_("%P: warning: creating a DT_TEXTREL in a shared object.\n")); | |
943284cc DJ |
10117 | break; |
10118 | } | |
10119 | } | |
10120 | } | |
10121 | ||
c152c796 AM |
10122 | for (o = dynobj->sections; o != NULL; o = o->next) |
10123 | { | |
10124 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 10125 | || o->size == 0 |
c152c796 AM |
10126 | || o->output_section == bfd_abs_section_ptr) |
10127 | continue; | |
10128 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
10129 | { | |
10130 | /* At this point, we are only interested in sections | |
10131 | created by _bfd_elf_link_create_dynamic_sections. */ | |
10132 | continue; | |
10133 | } | |
3722b82f AM |
10134 | if (elf_hash_table (info)->stab_info.stabstr == o) |
10135 | continue; | |
eea6121a AM |
10136 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
10137 | continue; | |
c152c796 AM |
10138 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
10139 | != SHT_STRTAB) | |
10140 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
10141 | { | |
10142 | if (! bfd_set_section_contents (abfd, o->output_section, | |
10143 | o->contents, | |
10144 | (file_ptr) o->output_offset, | |
eea6121a | 10145 | o->size)) |
c152c796 AM |
10146 | goto error_return; |
10147 | } | |
10148 | else | |
10149 | { | |
10150 | /* The contents of the .dynstr section are actually in a | |
10151 | stringtab. */ | |
10152 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
10153 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
10154 | || ! _bfd_elf_strtab_emit (abfd, | |
10155 | elf_hash_table (info)->dynstr)) | |
10156 | goto error_return; | |
10157 | } | |
10158 | } | |
10159 | } | |
10160 | ||
10161 | if (info->relocatable) | |
10162 | { | |
10163 | bfd_boolean failed = FALSE; | |
10164 | ||
10165 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
10166 | if (failed) | |
10167 | goto error_return; | |
10168 | } | |
10169 | ||
10170 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 10171 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
10172 | { |
10173 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
10174 | goto error_return; | |
10175 | } | |
10176 | ||
10177 | if (info->eh_frame_hdr) | |
10178 | { | |
10179 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) | |
10180 | goto error_return; | |
10181 | } | |
10182 | ||
10183 | if (finfo.symstrtab != NULL) | |
10184 | _bfd_stringtab_free (finfo.symstrtab); | |
10185 | if (finfo.contents != NULL) | |
10186 | free (finfo.contents); | |
10187 | if (finfo.external_relocs != NULL) | |
10188 | free (finfo.external_relocs); | |
10189 | if (finfo.internal_relocs != NULL) | |
10190 | free (finfo.internal_relocs); | |
10191 | if (finfo.external_syms != NULL) | |
10192 | free (finfo.external_syms); | |
10193 | if (finfo.locsym_shndx != NULL) | |
10194 | free (finfo.locsym_shndx); | |
10195 | if (finfo.internal_syms != NULL) | |
10196 | free (finfo.internal_syms); | |
10197 | if (finfo.indices != NULL) | |
10198 | free (finfo.indices); | |
10199 | if (finfo.sections != NULL) | |
10200 | free (finfo.sections); | |
10201 | if (finfo.symbuf != NULL) | |
10202 | free (finfo.symbuf); | |
10203 | if (finfo.symshndxbuf != NULL) | |
10204 | free (finfo.symshndxbuf); | |
10205 | for (o = abfd->sections; o != NULL; o = o->next) | |
10206 | { | |
10207 | if ((o->flags & SEC_RELOC) != 0 | |
10208 | && elf_section_data (o)->rel_hashes != NULL) | |
10209 | free (elf_section_data (o)->rel_hashes); | |
10210 | } | |
10211 | ||
10212 | elf_tdata (abfd)->linker = TRUE; | |
10213 | ||
10214 | return TRUE; | |
10215 | ||
10216 | error_return: | |
10217 | if (finfo.symstrtab != NULL) | |
10218 | _bfd_stringtab_free (finfo.symstrtab); | |
10219 | if (finfo.contents != NULL) | |
10220 | free (finfo.contents); | |
10221 | if (finfo.external_relocs != NULL) | |
10222 | free (finfo.external_relocs); | |
10223 | if (finfo.internal_relocs != NULL) | |
10224 | free (finfo.internal_relocs); | |
10225 | if (finfo.external_syms != NULL) | |
10226 | free (finfo.external_syms); | |
10227 | if (finfo.locsym_shndx != NULL) | |
10228 | free (finfo.locsym_shndx); | |
10229 | if (finfo.internal_syms != NULL) | |
10230 | free (finfo.internal_syms); | |
10231 | if (finfo.indices != NULL) | |
10232 | free (finfo.indices); | |
10233 | if (finfo.sections != NULL) | |
10234 | free (finfo.sections); | |
10235 | if (finfo.symbuf != NULL) | |
10236 | free (finfo.symbuf); | |
10237 | if (finfo.symshndxbuf != NULL) | |
10238 | free (finfo.symshndxbuf); | |
10239 | for (o = abfd->sections; o != NULL; o = o->next) | |
10240 | { | |
10241 | if ((o->flags & SEC_RELOC) != 0 | |
10242 | && elf_section_data (o)->rel_hashes != NULL) | |
10243 | free (elf_section_data (o)->rel_hashes); | |
10244 | } | |
10245 | ||
10246 | return FALSE; | |
10247 | } | |
10248 | \f | |
10249 | /* Garbage collect unused sections. */ | |
10250 | ||
07adf181 AM |
10251 | /* Default gc_mark_hook. */ |
10252 | ||
10253 | asection * | |
10254 | _bfd_elf_gc_mark_hook (asection *sec, | |
10255 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
10256 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
10257 | struct elf_link_hash_entry *h, | |
10258 | Elf_Internal_Sym *sym) | |
10259 | { | |
10260 | if (h != NULL) | |
10261 | { | |
10262 | switch (h->root.type) | |
10263 | { | |
10264 | case bfd_link_hash_defined: | |
10265 | case bfd_link_hash_defweak: | |
10266 | return h->root.u.def.section; | |
10267 | ||
10268 | case bfd_link_hash_common: | |
10269 | return h->root.u.c.p->section; | |
10270 | ||
10271 | default: | |
10272 | break; | |
10273 | } | |
10274 | } | |
10275 | else | |
10276 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
10277 | ||
10278 | return NULL; | |
10279 | } | |
10280 | ||
10281 | /* The mark phase of garbage collection. For a given section, mark | |
10282 | it and any sections in this section's group, and all the sections | |
10283 | which define symbols to which it refers. */ | |
10284 | ||
ccfa59ea AM |
10285 | bfd_boolean |
10286 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
10287 | asection *sec, | |
6a5bb875 | 10288 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
10289 | { |
10290 | bfd_boolean ret; | |
39c2f51b | 10291 | bfd_boolean is_eh; |
c152c796 AM |
10292 | asection *group_sec; |
10293 | ||
10294 | sec->gc_mark = 1; | |
10295 | ||
10296 | /* Mark all the sections in the group. */ | |
10297 | group_sec = elf_section_data (sec)->next_in_group; | |
10298 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 10299 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
10300 | return FALSE; |
10301 | ||
10302 | /* Look through the section relocs. */ | |
10303 | ret = TRUE; | |
39c2f51b | 10304 | is_eh = strcmp (sec->name, ".eh_frame") == 0; |
c152c796 AM |
10305 | if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0) |
10306 | { | |
10307 | Elf_Internal_Rela *relstart, *rel, *relend; | |
10308 | Elf_Internal_Shdr *symtab_hdr; | |
10309 | struct elf_link_hash_entry **sym_hashes; | |
10310 | size_t nlocsyms; | |
10311 | size_t extsymoff; | |
10312 | bfd *input_bfd = sec->owner; | |
10313 | const struct elf_backend_data *bed = get_elf_backend_data (input_bfd); | |
10314 | Elf_Internal_Sym *isym = NULL; | |
10315 | int r_sym_shift; | |
10316 | ||
10317 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
10318 | sym_hashes = elf_sym_hashes (input_bfd); | |
10319 | ||
10320 | /* Read the local symbols. */ | |
10321 | if (elf_bad_symtab (input_bfd)) | |
10322 | { | |
10323 | nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10324 | extsymoff = 0; | |
10325 | } | |
10326 | else | |
10327 | extsymoff = nlocsyms = symtab_hdr->sh_info; | |
10328 | ||
10329 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
10330 | if (isym == NULL && nlocsyms != 0) | |
10331 | { | |
10332 | isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0, | |
10333 | NULL, NULL, NULL); | |
10334 | if (isym == NULL) | |
10335 | return FALSE; | |
10336 | } | |
10337 | ||
10338 | /* Read the relocations. */ | |
10339 | relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL, | |
10340 | info->keep_memory); | |
10341 | if (relstart == NULL) | |
10342 | { | |
10343 | ret = FALSE; | |
10344 | goto out1; | |
10345 | } | |
10346 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
10347 | ||
10348 | if (bed->s->arch_size == 32) | |
10349 | r_sym_shift = 8; | |
10350 | else | |
10351 | r_sym_shift = 32; | |
10352 | ||
10353 | for (rel = relstart; rel < relend; rel++) | |
10354 | { | |
10355 | unsigned long r_symndx; | |
10356 | asection *rsec; | |
10357 | struct elf_link_hash_entry *h; | |
10358 | ||
10359 | r_symndx = rel->r_info >> r_sym_shift; | |
10360 | if (r_symndx == 0) | |
10361 | continue; | |
10362 | ||
10363 | if (r_symndx >= nlocsyms | |
10364 | || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL) | |
10365 | { | |
10366 | h = sym_hashes[r_symndx - extsymoff]; | |
20f0a1ad AM |
10367 | while (h->root.type == bfd_link_hash_indirect |
10368 | || h->root.type == bfd_link_hash_warning) | |
10369 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 AM |
10370 | rsec = (*gc_mark_hook) (sec, info, rel, h, NULL); |
10371 | } | |
10372 | else | |
10373 | { | |
10374 | rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]); | |
10375 | } | |
10376 | ||
10377 | if (rsec && !rsec->gc_mark) | |
10378 | { | |
10379 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
10380 | rsec->gc_mark = 1; | |
39c2f51b AM |
10381 | else if (is_eh) |
10382 | rsec->gc_mark_from_eh = 1; | |
ccfa59ea | 10383 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) |
c152c796 AM |
10384 | { |
10385 | ret = FALSE; | |
10386 | goto out2; | |
10387 | } | |
10388 | } | |
10389 | } | |
10390 | ||
10391 | out2: | |
10392 | if (elf_section_data (sec)->relocs != relstart) | |
10393 | free (relstart); | |
10394 | out1: | |
10395 | if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym) | |
10396 | { | |
10397 | if (! info->keep_memory) | |
10398 | free (isym); | |
10399 | else | |
10400 | symtab_hdr->contents = (unsigned char *) isym; | |
10401 | } | |
10402 | } | |
10403 | ||
10404 | return ret; | |
10405 | } | |
10406 | ||
10407 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ | |
10408 | ||
c17d87de NC |
10409 | struct elf_gc_sweep_symbol_info |
10410 | { | |
ccabcbe5 AM |
10411 | struct bfd_link_info *info; |
10412 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
10413 | bfd_boolean); | |
10414 | }; | |
10415 | ||
c152c796 | 10416 | static bfd_boolean |
ccabcbe5 | 10417 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) |
c152c796 | 10418 | { |
c152c796 AM |
10419 | if (h->root.type == bfd_link_hash_warning) |
10420 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10421 | ||
ccabcbe5 AM |
10422 | if ((h->root.type == bfd_link_hash_defined |
10423 | || h->root.type == bfd_link_hash_defweak) | |
10424 | && !h->root.u.def.section->gc_mark | |
10425 | && !(h->root.u.def.section->owner->flags & DYNAMIC)) | |
10426 | { | |
10427 | struct elf_gc_sweep_symbol_info *inf = data; | |
10428 | (*inf->hide_symbol) (inf->info, h, TRUE); | |
10429 | } | |
c152c796 AM |
10430 | |
10431 | return TRUE; | |
10432 | } | |
10433 | ||
10434 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
10435 | ||
10436 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
10437 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
10438 | ||
10439 | static bfd_boolean | |
ccabcbe5 | 10440 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
10441 | { |
10442 | bfd *sub; | |
ccabcbe5 AM |
10443 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10444 | gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; | |
10445 | unsigned long section_sym_count; | |
10446 | struct elf_gc_sweep_symbol_info sweep_info; | |
c152c796 AM |
10447 | |
10448 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
10449 | { | |
10450 | asection *o; | |
10451 | ||
10452 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
10453 | continue; | |
10454 | ||
10455 | for (o = sub->sections; o != NULL; o = o->next) | |
10456 | { | |
7c2c8505 AM |
10457 | /* Keep debug and special sections. */ |
10458 | if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 | |
dea5f36a | 10459 | || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) |
c152c796 AM |
10460 | o->gc_mark = 1; |
10461 | ||
10462 | if (o->gc_mark) | |
10463 | continue; | |
10464 | ||
10465 | /* Skip sweeping sections already excluded. */ | |
10466 | if (o->flags & SEC_EXCLUDE) | |
10467 | continue; | |
10468 | ||
10469 | /* Since this is early in the link process, it is simple | |
10470 | to remove a section from the output. */ | |
10471 | o->flags |= SEC_EXCLUDE; | |
10472 | ||
c17d87de NC |
10473 | if (info->print_gc_sections == TRUE) |
10474 | _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name); | |
10475 | ||
c152c796 AM |
10476 | /* But we also have to update some of the relocation |
10477 | info we collected before. */ | |
10478 | if (gc_sweep_hook | |
e8aaee2a AM |
10479 | && (o->flags & SEC_RELOC) != 0 |
10480 | && o->reloc_count > 0 | |
10481 | && !bfd_is_abs_section (o->output_section)) | |
c152c796 AM |
10482 | { |
10483 | Elf_Internal_Rela *internal_relocs; | |
10484 | bfd_boolean r; | |
10485 | ||
10486 | internal_relocs | |
10487 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
10488 | info->keep_memory); | |
10489 | if (internal_relocs == NULL) | |
10490 | return FALSE; | |
10491 | ||
10492 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
10493 | ||
10494 | if (elf_section_data (o)->relocs != internal_relocs) | |
10495 | free (internal_relocs); | |
10496 | ||
10497 | if (!r) | |
10498 | return FALSE; | |
10499 | } | |
10500 | } | |
10501 | } | |
10502 | ||
10503 | /* Remove the symbols that were in the swept sections from the dynamic | |
10504 | symbol table. GCFIXME: Anyone know how to get them out of the | |
10505 | static symbol table as well? */ | |
ccabcbe5 AM |
10506 | sweep_info.info = info; |
10507 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
10508 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
10509 | &sweep_info); | |
c152c796 | 10510 | |
ccabcbe5 | 10511 | _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); |
c152c796 AM |
10512 | return TRUE; |
10513 | } | |
10514 | ||
10515 | /* Propagate collected vtable information. This is called through | |
10516 | elf_link_hash_traverse. */ | |
10517 | ||
10518 | static bfd_boolean | |
10519 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
10520 | { | |
10521 | if (h->root.type == bfd_link_hash_warning) | |
10522 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10523 | ||
10524 | /* Those that are not vtables. */ | |
f6e332e6 | 10525 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
10526 | return TRUE; |
10527 | ||
10528 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 10529 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
10530 | return TRUE; |
10531 | ||
10532 | /* If we've already been done, exit. */ | |
f6e332e6 | 10533 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
10534 | return TRUE; |
10535 | ||
10536 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 10537 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 10538 | |
f6e332e6 | 10539 | if (h->vtable->used == NULL) |
c152c796 AM |
10540 | { |
10541 | /* None of this table's entries were referenced. Re-use the | |
10542 | parent's table. */ | |
f6e332e6 AM |
10543 | h->vtable->used = h->vtable->parent->vtable->used; |
10544 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
10545 | } |
10546 | else | |
10547 | { | |
10548 | size_t n; | |
10549 | bfd_boolean *cu, *pu; | |
10550 | ||
10551 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 10552 | cu = h->vtable->used; |
c152c796 | 10553 | cu[-1] = TRUE; |
f6e332e6 | 10554 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
10555 | if (pu != NULL) |
10556 | { | |
10557 | const struct elf_backend_data *bed; | |
10558 | unsigned int log_file_align; | |
10559 | ||
10560 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
10561 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 10562 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
10563 | while (n--) |
10564 | { | |
10565 | if (*pu) | |
10566 | *cu = TRUE; | |
10567 | pu++; | |
10568 | cu++; | |
10569 | } | |
10570 | } | |
10571 | } | |
10572 | ||
10573 | return TRUE; | |
10574 | } | |
10575 | ||
10576 | static bfd_boolean | |
10577 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
10578 | { | |
10579 | asection *sec; | |
10580 | bfd_vma hstart, hend; | |
10581 | Elf_Internal_Rela *relstart, *relend, *rel; | |
10582 | const struct elf_backend_data *bed; | |
10583 | unsigned int log_file_align; | |
10584 | ||
10585 | if (h->root.type == bfd_link_hash_warning) | |
10586 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10587 | ||
10588 | /* Take care of both those symbols that do not describe vtables as | |
10589 | well as those that are not loaded. */ | |
f6e332e6 | 10590 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
10591 | return TRUE; |
10592 | ||
10593 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
10594 | || h->root.type == bfd_link_hash_defweak); | |
10595 | ||
10596 | sec = h->root.u.def.section; | |
10597 | hstart = h->root.u.def.value; | |
10598 | hend = hstart + h->size; | |
10599 | ||
10600 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
10601 | if (!relstart) | |
10602 | return *(bfd_boolean *) okp = FALSE; | |
10603 | bed = get_elf_backend_data (sec->owner); | |
10604 | log_file_align = bed->s->log_file_align; | |
10605 | ||
10606 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
10607 | ||
10608 | for (rel = relstart; rel < relend; ++rel) | |
10609 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
10610 | { | |
10611 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
10612 | if (h->vtable->used |
10613 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
10614 | { |
10615 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 10616 | if (h->vtable->used[entry]) |
c152c796 AM |
10617 | continue; |
10618 | } | |
10619 | /* Otherwise, kill it. */ | |
10620 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
10621 | } | |
10622 | ||
10623 | return TRUE; | |
10624 | } | |
10625 | ||
87538722 AM |
10626 | /* Mark sections containing dynamically referenced symbols. When |
10627 | building shared libraries, we must assume that any visible symbol is | |
10628 | referenced. */ | |
715df9b8 | 10629 | |
64d03ab5 AM |
10630 | bfd_boolean |
10631 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 10632 | { |
87538722 AM |
10633 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
10634 | ||
715df9b8 EB |
10635 | if (h->root.type == bfd_link_hash_warning) |
10636 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10637 | ||
10638 | if ((h->root.type == bfd_link_hash_defined | |
10639 | || h->root.type == bfd_link_hash_defweak) | |
87538722 | 10640 | && (h->ref_dynamic |
5adcfd8b | 10641 | || (!info->executable |
87538722 AM |
10642 | && h->def_regular |
10643 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL | |
10644 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN))) | |
715df9b8 EB |
10645 | h->root.u.def.section->flags |= SEC_KEEP; |
10646 | ||
10647 | return TRUE; | |
10648 | } | |
3b36f7e6 | 10649 | |
c152c796 AM |
10650 | /* Do mark and sweep of unused sections. */ |
10651 | ||
10652 | bfd_boolean | |
10653 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
10654 | { | |
10655 | bfd_boolean ok = TRUE; | |
10656 | bfd *sub; | |
6a5bb875 | 10657 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 10658 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 10659 | |
64d03ab5 | 10660 | if (!bed->can_gc_sections |
c152c796 AM |
10661 | || info->relocatable |
10662 | || info->emitrelocations | |
715df9b8 | 10663 | || !is_elf_hash_table (info->hash)) |
c152c796 AM |
10664 | { |
10665 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
10666 | return TRUE; | |
10667 | } | |
10668 | ||
10669 | /* Apply transitive closure to the vtable entry usage info. */ | |
10670 | elf_link_hash_traverse (elf_hash_table (info), | |
10671 | elf_gc_propagate_vtable_entries_used, | |
10672 | &ok); | |
10673 | if (!ok) | |
10674 | return FALSE; | |
10675 | ||
10676 | /* Kill the vtable relocations that were not used. */ | |
10677 | elf_link_hash_traverse (elf_hash_table (info), | |
10678 | elf_gc_smash_unused_vtentry_relocs, | |
10679 | &ok); | |
10680 | if (!ok) | |
10681 | return FALSE; | |
10682 | ||
715df9b8 EB |
10683 | /* Mark dynamically referenced symbols. */ |
10684 | if (elf_hash_table (info)->dynamic_sections_created) | |
10685 | elf_link_hash_traverse (elf_hash_table (info), | |
64d03ab5 | 10686 | bed->gc_mark_dynamic_ref, |
87538722 | 10687 | info); |
c152c796 | 10688 | |
715df9b8 | 10689 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 10690 | gc_mark_hook = bed->gc_mark_hook; |
c152c796 AM |
10691 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) |
10692 | { | |
10693 | asection *o; | |
10694 | ||
10695 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
10696 | continue; | |
10697 | ||
10698 | for (o = sub->sections; o != NULL; o = o->next) | |
a14a5de3 | 10699 | if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark) |
39c2f51b AM |
10700 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) |
10701 | return FALSE; | |
c152c796 AM |
10702 | } |
10703 | ||
6a5bb875 PB |
10704 | /* Allow the backend to mark additional target specific sections. */ |
10705 | if (bed->gc_mark_extra_sections) | |
10706 | bed->gc_mark_extra_sections(info, gc_mark_hook); | |
10707 | ||
9e8cc8b4 AM |
10708 | /* ... again for sections marked from eh_frame. */ |
10709 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
10710 | { | |
10711 | asection *o; | |
10712 | ||
10713 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
10714 | continue; | |
10715 | ||
a48710b3 EB |
10716 | /* Keep .gcc_except_table.* if the associated .text.* (or the |
10717 | associated .gnu.linkonce.t.* if .text.* doesn't exist) is | |
9e8cc8b4 | 10718 | marked. This isn't very nice, but the proper solution, |
2de92251 | 10719 | splitting .eh_frame up and using comdat doesn't pan out |
9e8cc8b4 AM |
10720 | easily due to needing special relocs to handle the |
10721 | difference of two symbols in separate sections. | |
10722 | Don't keep code sections referenced by .eh_frame. */ | |
ea9986ff | 10723 | #define TEXT_PREFIX ".text." |
a48710b3 | 10724 | #define TEXT_PREFIX2 ".gnu.linkonce.t." |
ea9986ff | 10725 | #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table." |
9e8cc8b4 AM |
10726 | for (o = sub->sections; o != NULL; o = o->next) |
10727 | if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0) | |
10728 | { | |
ea9986ff | 10729 | if (CONST_STRNEQ (o->name, GCC_EXCEPT_TABLE_PREFIX)) |
9e8cc8b4 | 10730 | { |
9e8cc8b4 | 10731 | char *fn_name; |
ea9986ff | 10732 | const char *sec_name; |
9e8cc8b4 | 10733 | asection *fn_text; |
a48710b3 | 10734 | unsigned o_name_prefix_len , fn_name_prefix_len, tmp; |
9e8cc8b4 | 10735 | |
a48710b3 | 10736 | o_name_prefix_len = strlen (GCC_EXCEPT_TABLE_PREFIX); |
ea9986ff | 10737 | sec_name = o->name + o_name_prefix_len; |
a48710b3 EB |
10738 | fn_name_prefix_len = strlen (TEXT_PREFIX); |
10739 | tmp = strlen (TEXT_PREFIX2); | |
10740 | if (tmp > fn_name_prefix_len) | |
10741 | fn_name_prefix_len = tmp; | |
10742 | fn_name | |
10743 | = bfd_malloc (fn_name_prefix_len + strlen (sec_name) + 1); | |
9e8cc8b4 AM |
10744 | if (fn_name == NULL) |
10745 | return FALSE; | |
a48710b3 EB |
10746 | |
10747 | /* Try the first prefix. */ | |
ea9986ff | 10748 | sprintf (fn_name, "%s%s", TEXT_PREFIX, sec_name); |
9e8cc8b4 | 10749 | fn_text = bfd_get_section_by_name (sub, fn_name); |
a48710b3 EB |
10750 | |
10751 | /* Try the second prefix. */ | |
10752 | if (fn_text == NULL) | |
10753 | { | |
10754 | sprintf (fn_name, "%s%s", TEXT_PREFIX2, sec_name); | |
10755 | fn_text = bfd_get_section_by_name (sub, fn_name); | |
10756 | } | |
10757 | ||
9e8cc8b4 AM |
10758 | free (fn_name); |
10759 | if (fn_text == NULL || !fn_text->gc_mark) | |
10760 | continue; | |
10761 | } | |
10762 | ||
10763 | /* If not using specially named exception table section, | |
10764 | then keep whatever we are using. */ | |
10765 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
10766 | return FALSE; | |
10767 | } | |
10768 | } | |
10769 | ||
c152c796 | 10770 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 10771 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
10772 | } |
10773 | \f | |
10774 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
10775 | ||
10776 | bfd_boolean | |
10777 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
10778 | asection *sec, | |
10779 | struct elf_link_hash_entry *h, | |
10780 | bfd_vma offset) | |
10781 | { | |
10782 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
10783 | struct elf_link_hash_entry **search, *child; | |
10784 | bfd_size_type extsymcount; | |
10785 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10786 | ||
10787 | /* The sh_info field of the symtab header tells us where the | |
10788 | external symbols start. We don't care about the local symbols at | |
10789 | this point. */ | |
10790 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
10791 | if (!elf_bad_symtab (abfd)) | |
10792 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
10793 | ||
10794 | sym_hashes = elf_sym_hashes (abfd); | |
10795 | sym_hashes_end = sym_hashes + extsymcount; | |
10796 | ||
10797 | /* Hunt down the child symbol, which is in this section at the same | |
10798 | offset as the relocation. */ | |
10799 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
10800 | { | |
10801 | if ((child = *search) != NULL | |
10802 | && (child->root.type == bfd_link_hash_defined | |
10803 | || child->root.type == bfd_link_hash_defweak) | |
10804 | && child->root.u.def.section == sec | |
10805 | && child->root.u.def.value == offset) | |
10806 | goto win; | |
10807 | } | |
10808 | ||
d003868e AM |
10809 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
10810 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
10811 | bfd_set_error (bfd_error_invalid_operation); |
10812 | return FALSE; | |
10813 | ||
10814 | win: | |
f6e332e6 AM |
10815 | if (!child->vtable) |
10816 | { | |
10817 | child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable)); | |
10818 | if (!child->vtable) | |
10819 | return FALSE; | |
10820 | } | |
c152c796 AM |
10821 | if (!h) |
10822 | { | |
10823 | /* This *should* only be the absolute section. It could potentially | |
10824 | be that someone has defined a non-global vtable though, which | |
10825 | would be bad. It isn't worth paging in the local symbols to be | |
10826 | sure though; that case should simply be handled by the assembler. */ | |
10827 | ||
f6e332e6 | 10828 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
10829 | } |
10830 | else | |
f6e332e6 | 10831 | child->vtable->parent = h; |
c152c796 AM |
10832 | |
10833 | return TRUE; | |
10834 | } | |
10835 | ||
10836 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
10837 | ||
10838 | bfd_boolean | |
10839 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
10840 | asection *sec ATTRIBUTE_UNUSED, | |
10841 | struct elf_link_hash_entry *h, | |
10842 | bfd_vma addend) | |
10843 | { | |
10844 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10845 | unsigned int log_file_align = bed->s->log_file_align; | |
10846 | ||
f6e332e6 AM |
10847 | if (!h->vtable) |
10848 | { | |
10849 | h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable)); | |
10850 | if (!h->vtable) | |
10851 | return FALSE; | |
10852 | } | |
10853 | ||
10854 | if (addend >= h->vtable->size) | |
c152c796 AM |
10855 | { |
10856 | size_t size, bytes, file_align; | |
f6e332e6 | 10857 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
10858 | |
10859 | /* While the symbol is undefined, we have to be prepared to handle | |
10860 | a zero size. */ | |
10861 | file_align = 1 << log_file_align; | |
10862 | if (h->root.type == bfd_link_hash_undefined) | |
10863 | size = addend + file_align; | |
10864 | else | |
10865 | { | |
10866 | size = h->size; | |
10867 | if (addend >= size) | |
10868 | { | |
10869 | /* Oops! We've got a reference past the defined end of | |
10870 | the table. This is probably a bug -- shall we warn? */ | |
10871 | size = addend + file_align; | |
10872 | } | |
10873 | } | |
10874 | size = (size + file_align - 1) & -file_align; | |
10875 | ||
10876 | /* Allocate one extra entry for use as a "done" flag for the | |
10877 | consolidation pass. */ | |
10878 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
10879 | ||
10880 | if (ptr) | |
10881 | { | |
10882 | ptr = bfd_realloc (ptr - 1, bytes); | |
10883 | ||
10884 | if (ptr != NULL) | |
10885 | { | |
10886 | size_t oldbytes; | |
10887 | ||
f6e332e6 | 10888 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
10889 | * sizeof (bfd_boolean)); |
10890 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
10891 | } | |
10892 | } | |
10893 | else | |
10894 | ptr = bfd_zmalloc (bytes); | |
10895 | ||
10896 | if (ptr == NULL) | |
10897 | return FALSE; | |
10898 | ||
10899 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
10900 | h->vtable->used = ptr + 1; |
10901 | h->vtable->size = size; | |
c152c796 AM |
10902 | } |
10903 | ||
f6e332e6 | 10904 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
10905 | |
10906 | return TRUE; | |
10907 | } | |
10908 | ||
10909 | struct alloc_got_off_arg { | |
10910 | bfd_vma gotoff; | |
10911 | unsigned int got_elt_size; | |
10912 | }; | |
10913 | ||
10914 | /* We need a special top-level link routine to convert got reference counts | |
10915 | to real got offsets. */ | |
10916 | ||
10917 | static bfd_boolean | |
10918 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
10919 | { | |
10920 | struct alloc_got_off_arg *gofarg = arg; | |
10921 | ||
10922 | if (h->root.type == bfd_link_hash_warning) | |
10923 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10924 | ||
10925 | if (h->got.refcount > 0) | |
10926 | { | |
10927 | h->got.offset = gofarg->gotoff; | |
10928 | gofarg->gotoff += gofarg->got_elt_size; | |
10929 | } | |
10930 | else | |
10931 | h->got.offset = (bfd_vma) -1; | |
10932 | ||
10933 | return TRUE; | |
10934 | } | |
10935 | ||
10936 | /* And an accompanying bit to work out final got entry offsets once | |
10937 | we're done. Should be called from final_link. */ | |
10938 | ||
10939 | bfd_boolean | |
10940 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
10941 | struct bfd_link_info *info) | |
10942 | { | |
10943 | bfd *i; | |
10944 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10945 | bfd_vma gotoff; | |
10946 | unsigned int got_elt_size = bed->s->arch_size / 8; | |
10947 | struct alloc_got_off_arg gofarg; | |
10948 | ||
10949 | if (! is_elf_hash_table (info->hash)) | |
10950 | return FALSE; | |
10951 | ||
10952 | /* The GOT offset is relative to the .got section, but the GOT header is | |
10953 | put into the .got.plt section, if the backend uses it. */ | |
10954 | if (bed->want_got_plt) | |
10955 | gotoff = 0; | |
10956 | else | |
10957 | gotoff = bed->got_header_size; | |
10958 | ||
10959 | /* Do the local .got entries first. */ | |
10960 | for (i = info->input_bfds; i; i = i->link_next) | |
10961 | { | |
10962 | bfd_signed_vma *local_got; | |
10963 | bfd_size_type j, locsymcount; | |
10964 | Elf_Internal_Shdr *symtab_hdr; | |
10965 | ||
10966 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
10967 | continue; | |
10968 | ||
10969 | local_got = elf_local_got_refcounts (i); | |
10970 | if (!local_got) | |
10971 | continue; | |
10972 | ||
10973 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
10974 | if (elf_bad_symtab (i)) | |
10975 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10976 | else | |
10977 | locsymcount = symtab_hdr->sh_info; | |
10978 | ||
10979 | for (j = 0; j < locsymcount; ++j) | |
10980 | { | |
10981 | if (local_got[j] > 0) | |
10982 | { | |
10983 | local_got[j] = gotoff; | |
10984 | gotoff += got_elt_size; | |
10985 | } | |
10986 | else | |
10987 | local_got[j] = (bfd_vma) -1; | |
10988 | } | |
10989 | } | |
10990 | ||
10991 | /* Then the global .got entries. .plt refcounts are handled by | |
10992 | adjust_dynamic_symbol */ | |
10993 | gofarg.gotoff = gotoff; | |
10994 | gofarg.got_elt_size = got_elt_size; | |
10995 | elf_link_hash_traverse (elf_hash_table (info), | |
10996 | elf_gc_allocate_got_offsets, | |
10997 | &gofarg); | |
10998 | return TRUE; | |
10999 | } | |
11000 | ||
11001 | /* Many folk need no more in the way of final link than this, once | |
11002 | got entry reference counting is enabled. */ | |
11003 | ||
11004 | bfd_boolean | |
11005 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
11006 | { | |
11007 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
11008 | return FALSE; | |
11009 | ||
11010 | /* Invoke the regular ELF backend linker to do all the work. */ | |
11011 | return bfd_elf_final_link (abfd, info); | |
11012 | } | |
11013 | ||
11014 | bfd_boolean | |
11015 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
11016 | { | |
11017 | struct elf_reloc_cookie *rcookie = cookie; | |
11018 | ||
11019 | if (rcookie->bad_symtab) | |
11020 | rcookie->rel = rcookie->rels; | |
11021 | ||
11022 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
11023 | { | |
11024 | unsigned long r_symndx; | |
11025 | ||
11026 | if (! rcookie->bad_symtab) | |
11027 | if (rcookie->rel->r_offset > offset) | |
11028 | return FALSE; | |
11029 | if (rcookie->rel->r_offset != offset) | |
11030 | continue; | |
11031 | ||
11032 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
11033 | if (r_symndx == SHN_UNDEF) | |
11034 | return TRUE; | |
11035 | ||
11036 | if (r_symndx >= rcookie->locsymcount | |
11037 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
11038 | { | |
11039 | struct elf_link_hash_entry *h; | |
11040 | ||
11041 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
11042 | ||
11043 | while (h->root.type == bfd_link_hash_indirect | |
11044 | || h->root.type == bfd_link_hash_warning) | |
11045 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11046 | ||
11047 | if ((h->root.type == bfd_link_hash_defined | |
11048 | || h->root.type == bfd_link_hash_defweak) | |
11049 | && elf_discarded_section (h->root.u.def.section)) | |
11050 | return TRUE; | |
11051 | else | |
11052 | return FALSE; | |
11053 | } | |
11054 | else | |
11055 | { | |
11056 | /* It's not a relocation against a global symbol, | |
11057 | but it could be a relocation against a local | |
11058 | symbol for a discarded section. */ | |
11059 | asection *isec; | |
11060 | Elf_Internal_Sym *isym; | |
11061 | ||
11062 | /* Need to: get the symbol; get the section. */ | |
11063 | isym = &rcookie->locsyms[r_symndx]; | |
11064 | if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
11065 | { | |
11066 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); | |
11067 | if (isec != NULL && elf_discarded_section (isec)) | |
11068 | return TRUE; | |
11069 | } | |
11070 | } | |
11071 | return FALSE; | |
11072 | } | |
11073 | return FALSE; | |
11074 | } | |
11075 | ||
11076 | /* Discard unneeded references to discarded sections. | |
11077 | Returns TRUE if any section's size was changed. */ | |
11078 | /* This function assumes that the relocations are in sorted order, | |
11079 | which is true for all known assemblers. */ | |
11080 | ||
11081 | bfd_boolean | |
11082 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) | |
11083 | { | |
11084 | struct elf_reloc_cookie cookie; | |
11085 | asection *stab, *eh; | |
11086 | Elf_Internal_Shdr *symtab_hdr; | |
11087 | const struct elf_backend_data *bed; | |
11088 | bfd *abfd; | |
11089 | unsigned int count; | |
11090 | bfd_boolean ret = FALSE; | |
11091 | ||
11092 | if (info->traditional_format | |
11093 | || !is_elf_hash_table (info->hash)) | |
11094 | return FALSE; | |
11095 | ||
11096 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) | |
11097 | { | |
11098 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
11099 | continue; | |
11100 | ||
11101 | bed = get_elf_backend_data (abfd); | |
11102 | ||
11103 | if ((abfd->flags & DYNAMIC) != 0) | |
11104 | continue; | |
11105 | ||
8da3dbc5 AM |
11106 | eh = NULL; |
11107 | if (!info->relocatable) | |
11108 | { | |
11109 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); | |
11110 | if (eh != NULL | |
eea6121a | 11111 | && (eh->size == 0 |
8da3dbc5 AM |
11112 | || bfd_is_abs_section (eh->output_section))) |
11113 | eh = NULL; | |
11114 | } | |
c152c796 AM |
11115 | |
11116 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
11117 | if (stab != NULL | |
eea6121a | 11118 | && (stab->size == 0 |
c152c796 AM |
11119 | || bfd_is_abs_section (stab->output_section) |
11120 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) | |
11121 | stab = NULL; | |
11122 | ||
11123 | if (stab == NULL | |
11124 | && eh == NULL | |
11125 | && bed->elf_backend_discard_info == NULL) | |
11126 | continue; | |
11127 | ||
11128 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11129 | cookie.abfd = abfd; | |
11130 | cookie.sym_hashes = elf_sym_hashes (abfd); | |
11131 | cookie.bad_symtab = elf_bad_symtab (abfd); | |
11132 | if (cookie.bad_symtab) | |
11133 | { | |
11134 | cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
11135 | cookie.extsymoff = 0; | |
11136 | } | |
11137 | else | |
11138 | { | |
11139 | cookie.locsymcount = symtab_hdr->sh_info; | |
11140 | cookie.extsymoff = symtab_hdr->sh_info; | |
11141 | } | |
11142 | ||
11143 | if (bed->s->arch_size == 32) | |
11144 | cookie.r_sym_shift = 8; | |
11145 | else | |
11146 | cookie.r_sym_shift = 32; | |
11147 | ||
11148 | cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
11149 | if (cookie.locsyms == NULL && cookie.locsymcount != 0) | |
11150 | { | |
11151 | cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
11152 | cookie.locsymcount, 0, | |
11153 | NULL, NULL, NULL); | |
11154 | if (cookie.locsyms == NULL) | |
1182cb93 AM |
11155 | { |
11156 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
11157 | return FALSE; | |
11158 | } | |
c152c796 AM |
11159 | } |
11160 | ||
11161 | if (stab != NULL) | |
11162 | { | |
11163 | cookie.rels = NULL; | |
11164 | count = stab->reloc_count; | |
11165 | if (count != 0) | |
11166 | cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL, | |
11167 | info->keep_memory); | |
11168 | if (cookie.rels != NULL) | |
11169 | { | |
11170 | cookie.rel = cookie.rels; | |
11171 | cookie.relend = cookie.rels; | |
11172 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
11173 | if (_bfd_discard_section_stabs (abfd, stab, | |
11174 | elf_section_data (stab)->sec_info, | |
11175 | bfd_elf_reloc_symbol_deleted_p, | |
11176 | &cookie)) | |
11177 | ret = TRUE; | |
11178 | if (elf_section_data (stab)->relocs != cookie.rels) | |
11179 | free (cookie.rels); | |
11180 | } | |
11181 | } | |
11182 | ||
11183 | if (eh != NULL) | |
11184 | { | |
11185 | cookie.rels = NULL; | |
11186 | count = eh->reloc_count; | |
11187 | if (count != 0) | |
11188 | cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL, | |
11189 | info->keep_memory); | |
11190 | cookie.rel = cookie.rels; | |
11191 | cookie.relend = cookie.rels; | |
11192 | if (cookie.rels != NULL) | |
11193 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
11194 | ||
11195 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, | |
11196 | bfd_elf_reloc_symbol_deleted_p, | |
11197 | &cookie)) | |
11198 | ret = TRUE; | |
11199 | ||
11200 | if (cookie.rels != NULL | |
11201 | && elf_section_data (eh)->relocs != cookie.rels) | |
11202 | free (cookie.rels); | |
11203 | } | |
11204 | ||
11205 | if (bed->elf_backend_discard_info != NULL | |
11206 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
11207 | ret = TRUE; | |
11208 | ||
11209 | if (cookie.locsyms != NULL | |
11210 | && symtab_hdr->contents != (unsigned char *) cookie.locsyms) | |
11211 | { | |
11212 | if (! info->keep_memory) | |
11213 | free (cookie.locsyms); | |
11214 | else | |
11215 | symtab_hdr->contents = (unsigned char *) cookie.locsyms; | |
11216 | } | |
11217 | } | |
11218 | ||
11219 | if (info->eh_frame_hdr | |
11220 | && !info->relocatable | |
11221 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
11222 | ret = TRUE; | |
11223 | ||
11224 | return ret; | |
11225 | } | |
082b7297 L |
11226 | |
11227 | void | |
c0f00686 L |
11228 | _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec, |
11229 | struct bfd_link_info *info) | |
082b7297 L |
11230 | { |
11231 | flagword flags; | |
6d2cd210 | 11232 | const char *name, *p; |
082b7297 L |
11233 | struct bfd_section_already_linked *l; |
11234 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
3d7f7666 | 11235 | |
3d7f7666 L |
11236 | if (sec->output_section == bfd_abs_section_ptr) |
11237 | return; | |
082b7297 L |
11238 | |
11239 | flags = sec->flags; | |
3d7f7666 | 11240 | |
c2370991 AM |
11241 | /* Return if it isn't a linkonce section. A comdat group section |
11242 | also has SEC_LINK_ONCE set. */ | |
11243 | if ((flags & SEC_LINK_ONCE) == 0) | |
082b7297 L |
11244 | return; |
11245 | ||
c2370991 AM |
11246 | /* Don't put group member sections on our list of already linked |
11247 | sections. They are handled as a group via their group section. */ | |
11248 | if (elf_sec_group (sec) != NULL) | |
11249 | return; | |
3d7f7666 | 11250 | |
082b7297 L |
11251 | /* FIXME: When doing a relocatable link, we may have trouble |
11252 | copying relocations in other sections that refer to local symbols | |
11253 | in the section being discarded. Those relocations will have to | |
11254 | be converted somehow; as of this writing I'm not sure that any of | |
11255 | the backends handle that correctly. | |
11256 | ||
11257 | It is tempting to instead not discard link once sections when | |
11258 | doing a relocatable link (technically, they should be discarded | |
11259 | whenever we are building constructors). However, that fails, | |
11260 | because the linker winds up combining all the link once sections | |
11261 | into a single large link once section, which defeats the purpose | |
11262 | of having link once sections in the first place. | |
11263 | ||
11264 | Also, not merging link once sections in a relocatable link | |
11265 | causes trouble for MIPS ELF, which relies on link once semantics | |
11266 | to handle the .reginfo section correctly. */ | |
11267 | ||
11268 | name = bfd_get_section_name (abfd, sec); | |
11269 | ||
0112cd26 | 11270 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
6d2cd210 JJ |
11271 | && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
11272 | p++; | |
11273 | else | |
11274 | p = name; | |
11275 | ||
11276 | already_linked_list = bfd_section_already_linked_table_lookup (p); | |
082b7297 L |
11277 | |
11278 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
11279 | { | |
c2370991 AM |
11280 | /* We may have 2 different types of sections on the list: group |
11281 | sections and linkonce sections. Match like sections. */ | |
3d7f7666 | 11282 | if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) |
6d2cd210 | 11283 | && strcmp (name, l->sec->name) == 0 |
082b7297 L |
11284 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL) |
11285 | { | |
11286 | /* The section has already been linked. See if we should | |
6d2cd210 | 11287 | issue a warning. */ |
082b7297 L |
11288 | switch (flags & SEC_LINK_DUPLICATES) |
11289 | { | |
11290 | default: | |
11291 | abort (); | |
11292 | ||
11293 | case SEC_LINK_DUPLICATES_DISCARD: | |
11294 | break; | |
11295 | ||
11296 | case SEC_LINK_DUPLICATES_ONE_ONLY: | |
11297 | (*_bfd_error_handler) | |
c93625e2 | 11298 | (_("%B: ignoring duplicate section `%A'"), |
d003868e | 11299 | abfd, sec); |
082b7297 L |
11300 | break; |
11301 | ||
11302 | case SEC_LINK_DUPLICATES_SAME_SIZE: | |
11303 | if (sec->size != l->sec->size) | |
11304 | (*_bfd_error_handler) | |
c93625e2 | 11305 | (_("%B: duplicate section `%A' has different size"), |
d003868e | 11306 | abfd, sec); |
082b7297 | 11307 | break; |
ea5158d8 DJ |
11308 | |
11309 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: | |
11310 | if (sec->size != l->sec->size) | |
11311 | (*_bfd_error_handler) | |
c93625e2 | 11312 | (_("%B: duplicate section `%A' has different size"), |
ea5158d8 DJ |
11313 | abfd, sec); |
11314 | else if (sec->size != 0) | |
11315 | { | |
11316 | bfd_byte *sec_contents, *l_sec_contents; | |
11317 | ||
11318 | if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents)) | |
11319 | (*_bfd_error_handler) | |
c93625e2 | 11320 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
11321 | abfd, sec); |
11322 | else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec, | |
11323 | &l_sec_contents)) | |
11324 | (*_bfd_error_handler) | |
c93625e2 | 11325 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
11326 | l->sec->owner, l->sec); |
11327 | else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) | |
11328 | (*_bfd_error_handler) | |
c93625e2 | 11329 | (_("%B: warning: duplicate section `%A' has different contents"), |
ea5158d8 DJ |
11330 | abfd, sec); |
11331 | ||
11332 | if (sec_contents) | |
11333 | free (sec_contents); | |
11334 | if (l_sec_contents) | |
11335 | free (l_sec_contents); | |
11336 | } | |
11337 | break; | |
082b7297 L |
11338 | } |
11339 | ||
11340 | /* Set the output_section field so that lang_add_section | |
11341 | does not create a lang_input_section structure for this | |
11342 | section. Since there might be a symbol in the section | |
11343 | being discarded, we must retain a pointer to the section | |
11344 | which we are really going to use. */ | |
11345 | sec->output_section = bfd_abs_section_ptr; | |
11346 | sec->kept_section = l->sec; | |
3b36f7e6 | 11347 | |
082b7297 | 11348 | if (flags & SEC_GROUP) |
3d7f7666 L |
11349 | { |
11350 | asection *first = elf_next_in_group (sec); | |
11351 | asection *s = first; | |
11352 | ||
11353 | while (s != NULL) | |
11354 | { | |
11355 | s->output_section = bfd_abs_section_ptr; | |
11356 | /* Record which group discards it. */ | |
11357 | s->kept_section = l->sec; | |
11358 | s = elf_next_in_group (s); | |
11359 | /* These lists are circular. */ | |
11360 | if (s == first) | |
11361 | break; | |
11362 | } | |
11363 | } | |
082b7297 L |
11364 | |
11365 | return; | |
11366 | } | |
11367 | } | |
11368 | ||
c2370991 AM |
11369 | /* A single member comdat group section may be discarded by a |
11370 | linkonce section and vice versa. */ | |
11371 | ||
11372 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 11373 | { |
c2370991 AM |
11374 | asection *first = elf_next_in_group (sec); |
11375 | ||
11376 | if (first != NULL && elf_next_in_group (first) == first) | |
11377 | /* Check this single member group against linkonce sections. */ | |
11378 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
11379 | if ((l->sec->flags & SEC_GROUP) == 0 | |
11380 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL | |
11381 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
11382 | { | |
11383 | first->output_section = bfd_abs_section_ptr; | |
11384 | first->kept_section = l->sec; | |
11385 | sec->output_section = bfd_abs_section_ptr; | |
11386 | break; | |
11387 | } | |
3d7f7666 L |
11388 | } |
11389 | else | |
c2370991 | 11390 | /* Check this linkonce section against single member groups. */ |
6d2cd210 JJ |
11391 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
11392 | if (l->sec->flags & SEC_GROUP) | |
11393 | { | |
11394 | asection *first = elf_next_in_group (l->sec); | |
11395 | ||
11396 | if (first != NULL | |
11397 | && elf_next_in_group (first) == first | |
c0f00686 | 11398 | && bfd_elf_match_symbols_in_sections (first, sec, info)) |
6d2cd210 JJ |
11399 | { |
11400 | sec->output_section = bfd_abs_section_ptr; | |
c2370991 | 11401 | sec->kept_section = first; |
6d2cd210 JJ |
11402 | break; |
11403 | } | |
11404 | } | |
11405 | ||
082b7297 L |
11406 | /* This is the first section with this name. Record it. */ |
11407 | bfd_section_already_linked_table_insert (already_linked_list, sec); | |
11408 | } | |
81e1b023 | 11409 | |
a4d8e49b L |
11410 | bfd_boolean |
11411 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
11412 | { | |
11413 | return sym->st_shndx == SHN_COMMON; | |
11414 | } | |
11415 | ||
11416 | unsigned int | |
11417 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
11418 | { | |
11419 | return SHN_COMMON; | |
11420 | } | |
11421 | ||
11422 | asection * | |
11423 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
11424 | { | |
11425 | return bfd_com_section_ptr; | |
11426 | } |