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 | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
8fdd7217 | 10 | (at your option) any later version. |
252b5132 | 11 | |
8fdd7217 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
8fdd7217 NC |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
252b5132 | 21 | |
252b5132 | 22 | #include "sysdep.h" |
3db64b00 | 23 | #include "bfd.h" |
252b5132 RH |
24 | #include "bfdlink.h" |
25 | #include "libbfd.h" | |
26 | #define ARCH_SIZE 0 | |
27 | #include "elf-bfd.h" | |
4ad4eba5 | 28 | #include "safe-ctype.h" |
ccf2f652 | 29 | #include "libiberty.h" |
66eb6687 | 30 | #include "objalloc.h" |
252b5132 | 31 | |
d98685ac AM |
32 | /* Define a symbol in a dynamic linkage section. */ |
33 | ||
34 | struct elf_link_hash_entry * | |
35 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
36 | struct bfd_link_info *info, | |
37 | asection *sec, | |
38 | const char *name) | |
39 | { | |
40 | struct elf_link_hash_entry *h; | |
41 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 42 | const struct elf_backend_data *bed; |
d98685ac AM |
43 | |
44 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
45 | if (h != NULL) | |
46 | { | |
47 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
48 | This is a symptom of a larger problem: Absolute symbols | |
49 | defined in shared libraries can't be overridden, because we | |
50 | lose the link to the bfd which is via the symbol section. */ | |
51 | h->root.type = bfd_link_hash_new; | |
52 | } | |
53 | ||
54 | bh = &h->root; | |
55 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, | |
56 | sec, 0, NULL, FALSE, | |
57 | get_elf_backend_data (abfd)->collect, | |
58 | &bh)) | |
59 | return NULL; | |
60 | h = (struct elf_link_hash_entry *) bh; | |
61 | h->def_regular = 1; | |
62 | h->type = STT_OBJECT; | |
63 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
64 | ||
ccabcbe5 AM |
65 | bed = get_elf_backend_data (abfd); |
66 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
d98685ac AM |
67 | return h; |
68 | } | |
69 | ||
b34976b6 | 70 | bfd_boolean |
268b6b39 | 71 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
72 | { |
73 | flagword flags; | |
aad5d350 | 74 | asection *s; |
252b5132 | 75 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 76 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
77 | int ptralign; |
78 | ||
79 | /* This function may be called more than once. */ | |
aad5d350 AM |
80 | s = bfd_get_section_by_name (abfd, ".got"); |
81 | if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0) | |
b34976b6 | 82 | return TRUE; |
252b5132 RH |
83 | |
84 | switch (bed->s->arch_size) | |
85 | { | |
bb0deeff AO |
86 | case 32: |
87 | ptralign = 2; | |
88 | break; | |
89 | ||
90 | case 64: | |
91 | ptralign = 3; | |
92 | break; | |
93 | ||
94 | default: | |
95 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 96 | return FALSE; |
252b5132 RH |
97 | } |
98 | ||
e5a52504 | 99 | flags = bed->dynamic_sec_flags; |
252b5132 | 100 | |
3496cb2a | 101 | s = bfd_make_section_with_flags (abfd, ".got", flags); |
252b5132 | 102 | if (s == NULL |
252b5132 | 103 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 104 | return FALSE; |
252b5132 RH |
105 | |
106 | if (bed->want_got_plt) | |
107 | { | |
3496cb2a | 108 | s = bfd_make_section_with_flags (abfd, ".got.plt", flags); |
252b5132 | 109 | if (s == NULL |
252b5132 | 110 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 111 | return FALSE; |
252b5132 RH |
112 | } |
113 | ||
2517a57f AM |
114 | if (bed->want_got_sym) |
115 | { | |
116 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
117 | (or .got.plt) section. We don't do this in the linker script | |
118 | because we don't want to define the symbol if we are not creating | |
119 | a global offset table. */ | |
d98685ac | 120 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_"); |
2517a57f | 121 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
122 | if (h == NULL) |
123 | return FALSE; | |
2517a57f | 124 | } |
252b5132 RH |
125 | |
126 | /* The first bit of the global offset table is the header. */ | |
3b36f7e6 | 127 | s->size += bed->got_header_size; |
252b5132 | 128 | |
b34976b6 | 129 | return TRUE; |
252b5132 RH |
130 | } |
131 | \f | |
7e9f0867 AM |
132 | /* Create a strtab to hold the dynamic symbol names. */ |
133 | static bfd_boolean | |
134 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
135 | { | |
136 | struct elf_link_hash_table *hash_table; | |
137 | ||
138 | hash_table = elf_hash_table (info); | |
139 | if (hash_table->dynobj == NULL) | |
140 | hash_table->dynobj = abfd; | |
141 | ||
142 | if (hash_table->dynstr == NULL) | |
143 | { | |
144 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
145 | if (hash_table->dynstr == NULL) | |
146 | return FALSE; | |
147 | } | |
148 | return TRUE; | |
149 | } | |
150 | ||
45d6a902 AM |
151 | /* Create some sections which will be filled in with dynamic linking |
152 | information. ABFD is an input file which requires dynamic sections | |
153 | to be created. The dynamic sections take up virtual memory space | |
154 | when the final executable is run, so we need to create them before | |
155 | addresses are assigned to the output sections. We work out the | |
156 | actual contents and size of these sections later. */ | |
252b5132 | 157 | |
b34976b6 | 158 | bfd_boolean |
268b6b39 | 159 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 160 | { |
45d6a902 AM |
161 | flagword flags; |
162 | register asection *s; | |
9c5bfbb7 | 163 | const struct elf_backend_data *bed; |
252b5132 | 164 | |
0eddce27 | 165 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
166 | return FALSE; |
167 | ||
168 | if (elf_hash_table (info)->dynamic_sections_created) | |
169 | return TRUE; | |
170 | ||
7e9f0867 AM |
171 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
172 | return FALSE; | |
45d6a902 | 173 | |
7e9f0867 | 174 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
175 | bed = get_elf_backend_data (abfd); |
176 | ||
177 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
178 | |
179 | /* A dynamically linked executable has a .interp section, but a | |
180 | shared library does not. */ | |
36af4a4e | 181 | if (info->executable) |
252b5132 | 182 | { |
3496cb2a L |
183 | s = bfd_make_section_with_flags (abfd, ".interp", |
184 | flags | SEC_READONLY); | |
185 | if (s == NULL) | |
45d6a902 AM |
186 | return FALSE; |
187 | } | |
bb0deeff | 188 | |
45d6a902 AM |
189 | /* Create sections to hold version informations. These are removed |
190 | if they are not needed. */ | |
3496cb2a L |
191 | s = bfd_make_section_with_flags (abfd, ".gnu.version_d", |
192 | flags | SEC_READONLY); | |
45d6a902 | 193 | if (s == NULL |
45d6a902 AM |
194 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
195 | return FALSE; | |
196 | ||
3496cb2a L |
197 | s = bfd_make_section_with_flags (abfd, ".gnu.version", |
198 | flags | SEC_READONLY); | |
45d6a902 | 199 | if (s == NULL |
45d6a902 AM |
200 | || ! bfd_set_section_alignment (abfd, s, 1)) |
201 | return FALSE; | |
202 | ||
3496cb2a L |
203 | s = bfd_make_section_with_flags (abfd, ".gnu.version_r", |
204 | flags | SEC_READONLY); | |
45d6a902 | 205 | if (s == NULL |
45d6a902 AM |
206 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
207 | return FALSE; | |
208 | ||
3496cb2a L |
209 | s = bfd_make_section_with_flags (abfd, ".dynsym", |
210 | flags | SEC_READONLY); | |
45d6a902 | 211 | if (s == NULL |
45d6a902 AM |
212 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
213 | return FALSE; | |
214 | ||
3496cb2a L |
215 | s = bfd_make_section_with_flags (abfd, ".dynstr", |
216 | flags | SEC_READONLY); | |
217 | if (s == NULL) | |
45d6a902 AM |
218 | return FALSE; |
219 | ||
3496cb2a | 220 | s = bfd_make_section_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 221 | if (s == NULL |
45d6a902 AM |
222 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
223 | return FALSE; | |
224 | ||
225 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
226 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
227 | only want to define it if we are, in fact, creating a .dynamic | |
228 | section. We don't want to define it if there is no .dynamic | |
229 | section, since on some ELF platforms the start up code examines it | |
230 | to decide how to initialize the process. */ | |
d98685ac | 231 | if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC")) |
45d6a902 AM |
232 | return FALSE; |
233 | ||
fdc90cb4 JJ |
234 | if (info->emit_hash) |
235 | { | |
236 | s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY); | |
237 | if (s == NULL | |
238 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
239 | return FALSE; | |
240 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
241 | } | |
242 | ||
243 | if (info->emit_gnu_hash) | |
244 | { | |
245 | s = bfd_make_section_with_flags (abfd, ".gnu.hash", | |
246 | flags | SEC_READONLY); | |
247 | if (s == NULL | |
248 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
249 | return FALSE; | |
250 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: | |
251 | 4 32-bit words followed by variable count of 64-bit words, then | |
252 | variable count of 32-bit words. */ | |
253 | if (bed->s->arch_size == 64) | |
254 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
255 | else | |
256 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
257 | } | |
45d6a902 AM |
258 | |
259 | /* Let the backend create the rest of the sections. This lets the | |
260 | backend set the right flags. The backend will normally create | |
261 | the .got and .plt sections. */ | |
262 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
263 | return FALSE; | |
264 | ||
265 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
266 | ||
267 | return TRUE; | |
268 | } | |
269 | ||
270 | /* Create dynamic sections when linking against a dynamic object. */ | |
271 | ||
272 | bfd_boolean | |
268b6b39 | 273 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
274 | { |
275 | flagword flags, pltflags; | |
7325306f | 276 | struct elf_link_hash_entry *h; |
45d6a902 | 277 | asection *s; |
9c5bfbb7 | 278 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 279 | |
252b5132 RH |
280 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
281 | .rel[a].bss sections. */ | |
e5a52504 | 282 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
283 | |
284 | pltflags = flags; | |
252b5132 | 285 | if (bed->plt_not_loaded) |
6df4d94c MM |
286 | /* We do not clear SEC_ALLOC here because we still want the OS to |
287 | allocate space for the section; it's just that there's nothing | |
288 | to read in from the object file. */ | |
5d1634d7 | 289 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
290 | else |
291 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
292 | if (bed->plt_readonly) |
293 | pltflags |= SEC_READONLY; | |
294 | ||
3496cb2a | 295 | s = bfd_make_section_with_flags (abfd, ".plt", pltflags); |
252b5132 | 296 | if (s == NULL |
252b5132 | 297 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 298 | return FALSE; |
252b5132 | 299 | |
d98685ac AM |
300 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
301 | .plt section. */ | |
7325306f RS |
302 | if (bed->want_plt_sym) |
303 | { | |
304 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
305 | "_PROCEDURE_LINKAGE_TABLE_"); | |
306 | elf_hash_table (info)->hplt = h; | |
307 | if (h == NULL) | |
308 | return FALSE; | |
309 | } | |
252b5132 | 310 | |
3496cb2a L |
311 | s = bfd_make_section_with_flags (abfd, |
312 | (bed->default_use_rela_p | |
313 | ? ".rela.plt" : ".rel.plt"), | |
314 | flags | SEC_READONLY); | |
252b5132 | 315 | if (s == NULL |
45d6a902 | 316 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 317 | return FALSE; |
252b5132 RH |
318 | |
319 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 320 | return FALSE; |
252b5132 | 321 | |
3018b441 RH |
322 | if (bed->want_dynbss) |
323 | { | |
324 | /* The .dynbss section is a place to put symbols which are defined | |
325 | by dynamic objects, are referenced by regular objects, and are | |
326 | not functions. We must allocate space for them in the process | |
327 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
328 | initialize them at run time. The linker script puts the .dynbss | |
329 | section into the .bss section of the final image. */ | |
3496cb2a L |
330 | s = bfd_make_section_with_flags (abfd, ".dynbss", |
331 | (SEC_ALLOC | |
332 | | SEC_LINKER_CREATED)); | |
333 | if (s == NULL) | |
b34976b6 | 334 | return FALSE; |
252b5132 | 335 | |
3018b441 | 336 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
337 | normally needed. We need to create it here, though, so that the |
338 | linker will map it to an output section. We can't just create it | |
339 | only if we need it, because we will not know whether we need it | |
340 | until we have seen all the input files, and the first time the | |
341 | main linker code calls BFD after examining all the input files | |
342 | (size_dynamic_sections) the input sections have already been | |
343 | mapped to the output sections. If the section turns out not to | |
344 | be needed, we can discard it later. We will never need this | |
345 | section when generating a shared object, since they do not use | |
346 | copy relocs. */ | |
3018b441 RH |
347 | if (! info->shared) |
348 | { | |
3496cb2a L |
349 | s = bfd_make_section_with_flags (abfd, |
350 | (bed->default_use_rela_p | |
351 | ? ".rela.bss" : ".rel.bss"), | |
352 | flags | SEC_READONLY); | |
3018b441 | 353 | if (s == NULL |
45d6a902 | 354 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 355 | return FALSE; |
3018b441 | 356 | } |
252b5132 RH |
357 | } |
358 | ||
b34976b6 | 359 | return TRUE; |
252b5132 RH |
360 | } |
361 | \f | |
252b5132 RH |
362 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
363 | read the input files, since we need to have a list of all of them | |
364 | before we can determine the final sizes of the output sections. | |
365 | Note that we may actually call this function even though we are not | |
366 | going to output any dynamic symbols; in some cases we know that a | |
367 | symbol should be in the dynamic symbol table, but only if there is | |
368 | one. */ | |
369 | ||
b34976b6 | 370 | bfd_boolean |
c152c796 AM |
371 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
372 | struct elf_link_hash_entry *h) | |
252b5132 RH |
373 | { |
374 | if (h->dynindx == -1) | |
375 | { | |
2b0f7ef9 | 376 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 377 | char *p; |
252b5132 | 378 | const char *name; |
252b5132 RH |
379 | bfd_size_type indx; |
380 | ||
7a13edea NC |
381 | /* XXX: The ABI draft says the linker must turn hidden and |
382 | internal symbols into STB_LOCAL symbols when producing the | |
383 | DSO. However, if ld.so honors st_other in the dynamic table, | |
384 | this would not be necessary. */ | |
385 | switch (ELF_ST_VISIBILITY (h->other)) | |
386 | { | |
387 | case STV_INTERNAL: | |
388 | case STV_HIDDEN: | |
9d6eee78 L |
389 | if (h->root.type != bfd_link_hash_undefined |
390 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 391 | { |
f5385ebf | 392 | h->forced_local = 1; |
67687978 PB |
393 | if (!elf_hash_table (info)->is_relocatable_executable) |
394 | return TRUE; | |
7a13edea | 395 | } |
0444bdd4 | 396 | |
7a13edea NC |
397 | default: |
398 | break; | |
399 | } | |
400 | ||
252b5132 RH |
401 | h->dynindx = elf_hash_table (info)->dynsymcount; |
402 | ++elf_hash_table (info)->dynsymcount; | |
403 | ||
404 | dynstr = elf_hash_table (info)->dynstr; | |
405 | if (dynstr == NULL) | |
406 | { | |
407 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 408 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 409 | if (dynstr == NULL) |
b34976b6 | 410 | return FALSE; |
252b5132 RH |
411 | } |
412 | ||
413 | /* We don't put any version information in the dynamic string | |
aad5d350 | 414 | table. */ |
252b5132 RH |
415 | name = h->root.root.string; |
416 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
417 | if (p != NULL) |
418 | /* We know that the p points into writable memory. In fact, | |
419 | there are only a few symbols that have read-only names, being | |
420 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
421 | by the backends. Most symbols will have names pointing into | |
422 | an ELF string table read from a file, or to objalloc memory. */ | |
423 | *p = 0; | |
424 | ||
425 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
426 | ||
427 | if (p != NULL) | |
428 | *p = ELF_VER_CHR; | |
252b5132 RH |
429 | |
430 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 431 | return FALSE; |
252b5132 RH |
432 | h->dynstr_index = indx; |
433 | } | |
434 | ||
b34976b6 | 435 | return TRUE; |
252b5132 | 436 | } |
45d6a902 | 437 | \f |
55255dae L |
438 | /* Mark a symbol dynamic. */ |
439 | ||
440 | void | |
441 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, | |
40b36307 L |
442 | struct elf_link_hash_entry *h, |
443 | Elf_Internal_Sym *sym) | |
55255dae | 444 | { |
40b36307 | 445 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
55255dae | 446 | |
40b36307 L |
447 | /* It may be called more than once on the same H. */ |
448 | if(h->dynamic || info->relocatable) | |
55255dae L |
449 | return; |
450 | ||
40b36307 L |
451 | if ((info->dynamic_data |
452 | && (h->type == STT_OBJECT | |
453 | || (sym != NULL | |
454 | && ELF_ST_TYPE (sym->st_info) == STT_OBJECT))) | |
455 | || (d != NULL | |
456 | && h->root.type == bfd_link_hash_new | |
457 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
55255dae L |
458 | h->dynamic = 1; |
459 | } | |
460 | ||
45d6a902 AM |
461 | /* Record an assignment to a symbol made by a linker script. We need |
462 | this in case some dynamic object refers to this symbol. */ | |
463 | ||
464 | bfd_boolean | |
fe21a8fc L |
465 | bfd_elf_record_link_assignment (bfd *output_bfd, |
466 | struct bfd_link_info *info, | |
268b6b39 | 467 | const char *name, |
fe21a8fc L |
468 | bfd_boolean provide, |
469 | bfd_boolean hidden) | |
45d6a902 | 470 | { |
00cbee0a | 471 | struct elf_link_hash_entry *h, *hv; |
4ea42fb7 | 472 | struct elf_link_hash_table *htab; |
00cbee0a | 473 | const struct elf_backend_data *bed; |
45d6a902 | 474 | |
0eddce27 | 475 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
476 | return TRUE; |
477 | ||
4ea42fb7 AM |
478 | htab = elf_hash_table (info); |
479 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 480 | if (h == NULL) |
4ea42fb7 | 481 | return provide; |
45d6a902 | 482 | |
00cbee0a | 483 | switch (h->root.type) |
77cfaee6 | 484 | { |
00cbee0a L |
485 | case bfd_link_hash_defined: |
486 | case bfd_link_hash_defweak: | |
487 | case bfd_link_hash_common: | |
488 | break; | |
489 | case bfd_link_hash_undefweak: | |
490 | case bfd_link_hash_undefined: | |
491 | /* Since we're defining the symbol, don't let it seem to have not | |
492 | been defined. record_dynamic_symbol and size_dynamic_sections | |
493 | may depend on this. */ | |
4ea42fb7 | 494 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
495 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
496 | bfd_link_repair_undef_list (&htab->root); | |
00cbee0a L |
497 | break; |
498 | case bfd_link_hash_new: | |
40b36307 | 499 | bfd_elf_link_mark_dynamic_symbol (info, h, NULL); |
55255dae | 500 | h->non_elf = 0; |
00cbee0a L |
501 | break; |
502 | case bfd_link_hash_indirect: | |
503 | /* We had a versioned symbol in a dynamic library. We make the | |
504 | the versioned symbol point to this one. */ | |
505 | bed = get_elf_backend_data (output_bfd); | |
506 | hv = h; | |
507 | while (hv->root.type == bfd_link_hash_indirect | |
508 | || hv->root.type == bfd_link_hash_warning) | |
509 | hv = (struct elf_link_hash_entry *) hv->root.u.i.link; | |
510 | /* We don't need to update h->root.u since linker will set them | |
511 | later. */ | |
512 | h->root.type = bfd_link_hash_undefined; | |
513 | hv->root.type = bfd_link_hash_indirect; | |
514 | hv->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
515 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); | |
516 | break; | |
517 | case bfd_link_hash_warning: | |
518 | abort (); | |
519 | break; | |
55255dae | 520 | } |
45d6a902 AM |
521 | |
522 | /* If this symbol is being provided by the linker script, and it is | |
523 | currently defined by a dynamic object, but not by a regular | |
524 | object, then mark it as undefined so that the generic linker will | |
525 | force the correct value. */ | |
526 | if (provide | |
f5385ebf AM |
527 | && h->def_dynamic |
528 | && !h->def_regular) | |
45d6a902 AM |
529 | h->root.type = bfd_link_hash_undefined; |
530 | ||
531 | /* If this symbol is not being provided by the linker script, and it is | |
532 | currently defined by a dynamic object, but not by a regular object, | |
533 | then clear out any version information because the symbol will not be | |
534 | associated with the dynamic object any more. */ | |
535 | if (!provide | |
f5385ebf AM |
536 | && h->def_dynamic |
537 | && !h->def_regular) | |
45d6a902 AM |
538 | h->verinfo.verdef = NULL; |
539 | ||
f5385ebf | 540 | h->def_regular = 1; |
45d6a902 | 541 | |
fe21a8fc L |
542 | if (provide && hidden) |
543 | { | |
544 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
545 | ||
546 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
547 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
548 | } | |
549 | ||
6fa3860b PB |
550 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
551 | and executables. */ | |
552 | if (!info->relocatable | |
553 | && h->dynindx != -1 | |
554 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
555 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
556 | h->forced_local = 1; | |
557 | ||
f5385ebf AM |
558 | if ((h->def_dynamic |
559 | || h->ref_dynamic | |
67687978 PB |
560 | || info->shared |
561 | || (info->executable && elf_hash_table (info)->is_relocatable_executable)) | |
45d6a902 AM |
562 | && h->dynindx == -1) |
563 | { | |
c152c796 | 564 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
565 | return FALSE; |
566 | ||
567 | /* If this is a weak defined symbol, and we know a corresponding | |
568 | real symbol from the same dynamic object, make sure the real | |
569 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
570 | if (h->u.weakdef != NULL |
571 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 572 | { |
f6e332e6 | 573 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
574 | return FALSE; |
575 | } | |
576 | } | |
577 | ||
578 | return TRUE; | |
579 | } | |
42751cf3 | 580 | |
8c58d23b AM |
581 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
582 | success, and 2 on a failure caused by attempting to record a symbol | |
583 | in a discarded section, eg. a discarded link-once section symbol. */ | |
584 | ||
585 | int | |
c152c796 AM |
586 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
587 | bfd *input_bfd, | |
588 | long input_indx) | |
8c58d23b AM |
589 | { |
590 | bfd_size_type amt; | |
591 | struct elf_link_local_dynamic_entry *entry; | |
592 | struct elf_link_hash_table *eht; | |
593 | struct elf_strtab_hash *dynstr; | |
594 | unsigned long dynstr_index; | |
595 | char *name; | |
596 | Elf_External_Sym_Shndx eshndx; | |
597 | char esym[sizeof (Elf64_External_Sym)]; | |
598 | ||
0eddce27 | 599 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
600 | return 0; |
601 | ||
602 | /* See if the entry exists already. */ | |
603 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
604 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
605 | return 1; | |
606 | ||
607 | amt = sizeof (*entry); | |
268b6b39 | 608 | entry = bfd_alloc (input_bfd, amt); |
8c58d23b AM |
609 | if (entry == NULL) |
610 | return 0; | |
611 | ||
612 | /* Go find the symbol, so that we can find it's name. */ | |
613 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 614 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
615 | { |
616 | bfd_release (input_bfd, entry); | |
617 | return 0; | |
618 | } | |
619 | ||
620 | if (entry->isym.st_shndx != SHN_UNDEF | |
621 | && (entry->isym.st_shndx < SHN_LORESERVE | |
622 | || entry->isym.st_shndx > SHN_HIRESERVE)) | |
623 | { | |
624 | asection *s; | |
625 | ||
626 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
627 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
628 | { | |
629 | /* We can still bfd_release here as nothing has done another | |
630 | bfd_alloc. We can't do this later in this function. */ | |
631 | bfd_release (input_bfd, entry); | |
632 | return 2; | |
633 | } | |
634 | } | |
635 | ||
636 | name = (bfd_elf_string_from_elf_section | |
637 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
638 | entry->isym.st_name)); | |
639 | ||
640 | dynstr = elf_hash_table (info)->dynstr; | |
641 | if (dynstr == NULL) | |
642 | { | |
643 | /* Create a strtab to hold the dynamic symbol names. */ | |
644 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
645 | if (dynstr == NULL) | |
646 | return 0; | |
647 | } | |
648 | ||
b34976b6 | 649 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
650 | if (dynstr_index == (unsigned long) -1) |
651 | return 0; | |
652 | entry->isym.st_name = dynstr_index; | |
653 | ||
654 | eht = elf_hash_table (info); | |
655 | ||
656 | entry->next = eht->dynlocal; | |
657 | eht->dynlocal = entry; | |
658 | entry->input_bfd = input_bfd; | |
659 | entry->input_indx = input_indx; | |
660 | eht->dynsymcount++; | |
661 | ||
662 | /* Whatever binding the symbol had before, it's now local. */ | |
663 | entry->isym.st_info | |
664 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
665 | ||
666 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
667 | ||
668 | return 1; | |
669 | } | |
670 | ||
30b30c21 | 671 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 672 | |
30b30c21 | 673 | long |
268b6b39 AM |
674 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
675 | bfd *input_bfd, | |
676 | long input_indx) | |
30b30c21 RH |
677 | { |
678 | struct elf_link_local_dynamic_entry *e; | |
679 | ||
680 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
681 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
682 | return e->dynindx; | |
683 | return -1; | |
684 | } | |
685 | ||
686 | /* This function is used to renumber the dynamic symbols, if some of | |
687 | them are removed because they are marked as local. This is called | |
688 | via elf_link_hash_traverse. */ | |
689 | ||
b34976b6 | 690 | static bfd_boolean |
268b6b39 AM |
691 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
692 | void *data) | |
42751cf3 | 693 | { |
268b6b39 | 694 | size_t *count = data; |
30b30c21 | 695 | |
e92d460e AM |
696 | if (h->root.type == bfd_link_hash_warning) |
697 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
698 | ||
6fa3860b PB |
699 | if (h->forced_local) |
700 | return TRUE; | |
701 | ||
702 | if (h->dynindx != -1) | |
703 | h->dynindx = ++(*count); | |
704 | ||
705 | return TRUE; | |
706 | } | |
707 | ||
708 | ||
709 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
710 | STB_LOCAL binding. */ | |
711 | ||
712 | static bfd_boolean | |
713 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
714 | void *data) | |
715 | { | |
716 | size_t *count = data; | |
717 | ||
718 | if (h->root.type == bfd_link_hash_warning) | |
719 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
720 | ||
721 | if (!h->forced_local) | |
722 | return TRUE; | |
723 | ||
42751cf3 | 724 | if (h->dynindx != -1) |
30b30c21 RH |
725 | h->dynindx = ++(*count); |
726 | ||
b34976b6 | 727 | return TRUE; |
42751cf3 | 728 | } |
30b30c21 | 729 | |
aee6f5b4 AO |
730 | /* Return true if the dynamic symbol for a given section should be |
731 | omitted when creating a shared library. */ | |
732 | bfd_boolean | |
733 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
734 | struct bfd_link_info *info, | |
735 | asection *p) | |
736 | { | |
74541ad4 AM |
737 | struct elf_link_hash_table *htab; |
738 | ||
aee6f5b4 AO |
739 | switch (elf_section_data (p)->this_hdr.sh_type) |
740 | { | |
741 | case SHT_PROGBITS: | |
742 | case SHT_NOBITS: | |
743 | /* If sh_type is yet undecided, assume it could be | |
744 | SHT_PROGBITS/SHT_NOBITS. */ | |
745 | case SHT_NULL: | |
74541ad4 AM |
746 | htab = elf_hash_table (info); |
747 | if (p == htab->tls_sec) | |
748 | return FALSE; | |
749 | ||
750 | if (htab->text_index_section != NULL) | |
751 | return p != htab->text_index_section && p != htab->data_index_section; | |
752 | ||
aee6f5b4 AO |
753 | if (strcmp (p->name, ".got") == 0 |
754 | || strcmp (p->name, ".got.plt") == 0 | |
755 | || strcmp (p->name, ".plt") == 0) | |
756 | { | |
757 | asection *ip; | |
aee6f5b4 | 758 | |
74541ad4 AM |
759 | if (htab->dynobj != NULL |
760 | && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL | |
aee6f5b4 AO |
761 | && (ip->flags & SEC_LINKER_CREATED) |
762 | && ip->output_section == p) | |
763 | return TRUE; | |
764 | } | |
765 | return FALSE; | |
766 | ||
767 | /* There shouldn't be section relative relocations | |
768 | against any other section. */ | |
769 | default: | |
770 | return TRUE; | |
771 | } | |
772 | } | |
773 | ||
062e2358 | 774 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
775 | symbol for each output section, which come first. Next come symbols |
776 | which have been forced to local binding. Then all of the back-end | |
777 | allocated local dynamic syms, followed by the rest of the global | |
778 | symbols. */ | |
30b30c21 | 779 | |
554220db AM |
780 | static unsigned long |
781 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
782 | struct bfd_link_info *info, | |
783 | unsigned long *section_sym_count) | |
30b30c21 RH |
784 | { |
785 | unsigned long dynsymcount = 0; | |
786 | ||
67687978 | 787 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
30b30c21 | 788 | { |
aee6f5b4 | 789 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
790 | asection *p; |
791 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 792 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
793 | && (p->flags & SEC_ALLOC) != 0 |
794 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
795 | elf_section_data (p)->dynindx = ++dynsymcount; | |
74541ad4 AM |
796 | else |
797 | elf_section_data (p)->dynindx = 0; | |
30b30c21 | 798 | } |
554220db | 799 | *section_sym_count = dynsymcount; |
30b30c21 | 800 | |
6fa3860b PB |
801 | elf_link_hash_traverse (elf_hash_table (info), |
802 | elf_link_renumber_local_hash_table_dynsyms, | |
803 | &dynsymcount); | |
804 | ||
30b30c21 RH |
805 | if (elf_hash_table (info)->dynlocal) |
806 | { | |
807 | struct elf_link_local_dynamic_entry *p; | |
808 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
809 | p->dynindx = ++dynsymcount; | |
810 | } | |
811 | ||
812 | elf_link_hash_traverse (elf_hash_table (info), | |
813 | elf_link_renumber_hash_table_dynsyms, | |
814 | &dynsymcount); | |
815 | ||
816 | /* There is an unused NULL entry at the head of the table which | |
817 | we must account for in our count. Unless there weren't any | |
818 | symbols, which means we'll have no table at all. */ | |
819 | if (dynsymcount != 0) | |
820 | ++dynsymcount; | |
821 | ||
ccabcbe5 AM |
822 | elf_hash_table (info)->dynsymcount = dynsymcount; |
823 | return dynsymcount; | |
30b30c21 | 824 | } |
252b5132 | 825 | |
45d6a902 AM |
826 | /* This function is called when we want to define a new symbol. It |
827 | handles the various cases which arise when we find a definition in | |
828 | a dynamic object, or when there is already a definition in a | |
829 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
830 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
831 | OVERRIDE if the old symbol is overriding a new definition. We set | |
832 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
833 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
834 | change, we mean that we shouldn't warn if the type or size does | |
af44c138 L |
835 | change. We set POLD_ALIGNMENT if an old common symbol in a dynamic |
836 | object is overridden by a regular object. */ | |
45d6a902 AM |
837 | |
838 | bfd_boolean | |
268b6b39 AM |
839 | _bfd_elf_merge_symbol (bfd *abfd, |
840 | struct bfd_link_info *info, | |
841 | const char *name, | |
842 | Elf_Internal_Sym *sym, | |
843 | asection **psec, | |
844 | bfd_vma *pvalue, | |
af44c138 | 845 | unsigned int *pold_alignment, |
268b6b39 AM |
846 | struct elf_link_hash_entry **sym_hash, |
847 | bfd_boolean *skip, | |
848 | bfd_boolean *override, | |
849 | bfd_boolean *type_change_ok, | |
0f8a2703 | 850 | bfd_boolean *size_change_ok) |
252b5132 | 851 | { |
7479dfd4 | 852 | asection *sec, *oldsec; |
45d6a902 AM |
853 | struct elf_link_hash_entry *h; |
854 | struct elf_link_hash_entry *flip; | |
855 | int bind; | |
856 | bfd *oldbfd; | |
857 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
77cfaee6 | 858 | bfd_boolean newweak, oldweak; |
a4d8e49b | 859 | const struct elf_backend_data *bed; |
45d6a902 AM |
860 | |
861 | *skip = FALSE; | |
862 | *override = FALSE; | |
863 | ||
864 | sec = *psec; | |
865 | bind = ELF_ST_BIND (sym->st_info); | |
866 | ||
cd7be95b KH |
867 | /* Silently discard TLS symbols from --just-syms. There's no way to |
868 | combine a static TLS block with a new TLS block for this executable. */ | |
869 | if (ELF_ST_TYPE (sym->st_info) == STT_TLS | |
870 | && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
871 | { | |
872 | *skip = TRUE; | |
873 | return TRUE; | |
874 | } | |
875 | ||
45d6a902 AM |
876 | if (! bfd_is_und_section (sec)) |
877 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
878 | else | |
879 | h = ((struct elf_link_hash_entry *) | |
880 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
881 | if (h == NULL) | |
882 | return FALSE; | |
883 | *sym_hash = h; | |
252b5132 | 884 | |
45d6a902 AM |
885 | /* This code is for coping with dynamic objects, and is only useful |
886 | if we are doing an ELF link. */ | |
887 | if (info->hash->creator != abfd->xvec) | |
888 | return TRUE; | |
252b5132 | 889 | |
45d6a902 AM |
890 | /* For merging, we only care about real symbols. */ |
891 | ||
892 | while (h->root.type == bfd_link_hash_indirect | |
893 | || h->root.type == bfd_link_hash_warning) | |
894 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
895 | ||
40b36307 L |
896 | /* We have to check it for every instance since the first few may be |
897 | refereences and not all compilers emit symbol type for undefined | |
898 | symbols. */ | |
899 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
900 | ||
45d6a902 AM |
901 | /* If we just created the symbol, mark it as being an ELF symbol. |
902 | Other than that, there is nothing to do--there is no merge issue | |
903 | with a newly defined symbol--so we just return. */ | |
904 | ||
905 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 906 | { |
f5385ebf | 907 | h->non_elf = 0; |
45d6a902 AM |
908 | return TRUE; |
909 | } | |
252b5132 | 910 | |
7479dfd4 L |
911 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
912 | existing symbol. */ | |
252b5132 | 913 | |
45d6a902 AM |
914 | switch (h->root.type) |
915 | { | |
916 | default: | |
917 | oldbfd = NULL; | |
7479dfd4 | 918 | oldsec = NULL; |
45d6a902 | 919 | break; |
252b5132 | 920 | |
45d6a902 AM |
921 | case bfd_link_hash_undefined: |
922 | case bfd_link_hash_undefweak: | |
923 | oldbfd = h->root.u.undef.abfd; | |
7479dfd4 | 924 | oldsec = NULL; |
45d6a902 AM |
925 | break; |
926 | ||
927 | case bfd_link_hash_defined: | |
928 | case bfd_link_hash_defweak: | |
929 | oldbfd = h->root.u.def.section->owner; | |
7479dfd4 | 930 | oldsec = h->root.u.def.section; |
45d6a902 AM |
931 | break; |
932 | ||
933 | case bfd_link_hash_common: | |
934 | oldbfd = h->root.u.c.p->section->owner; | |
7479dfd4 | 935 | oldsec = h->root.u.c.p->section; |
45d6a902 AM |
936 | break; |
937 | } | |
938 | ||
939 | /* In cases involving weak versioned symbols, we may wind up trying | |
940 | to merge a symbol with itself. Catch that here, to avoid the | |
941 | confusion that results if we try to override a symbol with | |
942 | itself. The additional tests catch cases like | |
943 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
944 | dynamic object, which we do want to handle here. */ | |
945 | if (abfd == oldbfd | |
946 | && ((abfd->flags & DYNAMIC) == 0 | |
f5385ebf | 947 | || !h->def_regular)) |
45d6a902 AM |
948 | return TRUE; |
949 | ||
950 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
951 | respectively, is from a dynamic object. */ | |
952 | ||
707bba77 | 953 | newdyn = (abfd->flags & DYNAMIC) != 0; |
45d6a902 | 954 | |
707bba77 | 955 | olddyn = FALSE; |
45d6a902 AM |
956 | if (oldbfd != NULL) |
957 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 958 | else if (oldsec != NULL) |
45d6a902 | 959 | { |
707bba77 | 960 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 961 | indices used by MIPS ELF. */ |
707bba77 | 962 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 963 | } |
252b5132 | 964 | |
45d6a902 AM |
965 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
966 | respectively, appear to be a definition rather than reference. */ | |
967 | ||
707bba77 | 968 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 969 | |
707bba77 AM |
970 | olddef = (h->root.type != bfd_link_hash_undefined |
971 | && h->root.type != bfd_link_hash_undefweak | |
972 | && h->root.type != bfd_link_hash_common); | |
45d6a902 | 973 | |
fcb93ecf | 974 | bed = get_elf_backend_data (abfd); |
580a2b6e L |
975 | /* When we try to create a default indirect symbol from the dynamic |
976 | definition with the default version, we skip it if its type and | |
977 | the type of existing regular definition mismatch. We only do it | |
978 | if the existing regular definition won't be dynamic. */ | |
979 | if (pold_alignment == NULL | |
980 | && !info->shared | |
981 | && !info->export_dynamic | |
982 | && !h->ref_dynamic | |
983 | && newdyn | |
984 | && newdef | |
985 | && !olddyn | |
986 | && (olddef || h->root.type == bfd_link_hash_common) | |
987 | && ELF_ST_TYPE (sym->st_info) != h->type | |
988 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
fcb93ecf PB |
989 | && h->type != STT_NOTYPE |
990 | && !(bed->is_function_type (ELF_ST_TYPE (sym->st_info)) | |
991 | && bed->is_function_type (h->type))) | |
580a2b6e L |
992 | { |
993 | *skip = TRUE; | |
994 | return TRUE; | |
995 | } | |
996 | ||
68f49ba3 L |
997 | /* Check TLS symbol. We don't check undefined symbol introduced by |
998 | "ld -u". */ | |
7479dfd4 | 999 | if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS) |
68f49ba3 L |
1000 | && ELF_ST_TYPE (sym->st_info) != h->type |
1001 | && oldbfd != NULL) | |
7479dfd4 L |
1002 | { |
1003 | bfd *ntbfd, *tbfd; | |
1004 | bfd_boolean ntdef, tdef; | |
1005 | asection *ntsec, *tsec; | |
1006 | ||
1007 | if (h->type == STT_TLS) | |
1008 | { | |
3b36f7e6 | 1009 | ntbfd = abfd; |
7479dfd4 L |
1010 | ntsec = sec; |
1011 | ntdef = newdef; | |
1012 | tbfd = oldbfd; | |
1013 | tsec = oldsec; | |
1014 | tdef = olddef; | |
1015 | } | |
1016 | else | |
1017 | { | |
1018 | ntbfd = oldbfd; | |
1019 | ntsec = oldsec; | |
1020 | ntdef = olddef; | |
1021 | tbfd = abfd; | |
1022 | tsec = sec; | |
1023 | tdef = newdef; | |
1024 | } | |
1025 | ||
1026 | if (tdef && ntdef) | |
1027 | (*_bfd_error_handler) | |
1028 | (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), | |
1029 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); | |
1030 | else if (!tdef && !ntdef) | |
1031 | (*_bfd_error_handler) | |
1032 | (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), | |
1033 | tbfd, ntbfd, h->root.root.string); | |
1034 | else if (tdef) | |
1035 | (*_bfd_error_handler) | |
1036 | (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), | |
1037 | tbfd, tsec, ntbfd, h->root.root.string); | |
1038 | else | |
1039 | (*_bfd_error_handler) | |
1040 | (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), | |
1041 | tbfd, ntbfd, ntsec, h->root.root.string); | |
1042 | ||
1043 | bfd_set_error (bfd_error_bad_value); | |
1044 | return FALSE; | |
1045 | } | |
1046 | ||
4cc11e76 | 1047 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
1048 | object or is weak in all dynamic objects. Internal and hidden |
1049 | visibility will make it unavailable to dynamic objects. */ | |
f5385ebf | 1050 | if (newdyn && !h->dynamic_def) |
45d6a902 AM |
1051 | { |
1052 | if (!bfd_is_und_section (sec)) | |
f5385ebf | 1053 | h->dynamic_def = 1; |
45d6a902 | 1054 | else |
252b5132 | 1055 | { |
45d6a902 AM |
1056 | /* Check if this symbol is weak in all dynamic objects. If it |
1057 | is the first time we see it in a dynamic object, we mark | |
1058 | if it is weak. Otherwise, we clear it. */ | |
f5385ebf | 1059 | if (!h->ref_dynamic) |
79349b09 | 1060 | { |
45d6a902 | 1061 | if (bind == STB_WEAK) |
f5385ebf | 1062 | h->dynamic_weak = 1; |
252b5132 | 1063 | } |
45d6a902 | 1064 | else if (bind != STB_WEAK) |
f5385ebf | 1065 | h->dynamic_weak = 0; |
252b5132 | 1066 | } |
45d6a902 | 1067 | } |
252b5132 | 1068 | |
45d6a902 AM |
1069 | /* If the old symbol has non-default visibility, we ignore the new |
1070 | definition from a dynamic object. */ | |
1071 | if (newdyn | |
9c7a29a3 | 1072 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1073 | && !bfd_is_und_section (sec)) |
1074 | { | |
1075 | *skip = TRUE; | |
1076 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1077 | h->ref_dynamic = 1; |
45d6a902 AM |
1078 | /* A protected symbol has external availability. Make sure it is |
1079 | recorded as dynamic. | |
1080 | ||
1081 | FIXME: Should we check type and size for protected symbol? */ | |
1082 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1083 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1084 | else |
1085 | return TRUE; | |
1086 | } | |
1087 | else if (!newdyn | |
9c7a29a3 | 1088 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1089 | && h->def_dynamic) |
45d6a902 AM |
1090 | { |
1091 | /* If the new symbol with non-default visibility comes from a | |
1092 | relocatable file and the old definition comes from a dynamic | |
1093 | object, we remove the old definition. */ | |
1094 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
d2dee3b2 L |
1095 | { |
1096 | /* Handle the case where the old dynamic definition is | |
1097 | default versioned. We need to copy the symbol info from | |
1098 | the symbol with default version to the normal one if it | |
1099 | was referenced before. */ | |
1100 | if (h->ref_regular) | |
1101 | { | |
1102 | const struct elf_backend_data *bed | |
1103 | = get_elf_backend_data (abfd); | |
1104 | struct elf_link_hash_entry *vh = *sym_hash; | |
1105 | vh->root.type = h->root.type; | |
1106 | h->root.type = bfd_link_hash_indirect; | |
1107 | (*bed->elf_backend_copy_indirect_symbol) (info, vh, h); | |
1108 | /* Protected symbols will override the dynamic definition | |
1109 | with default version. */ | |
1110 | if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED) | |
1111 | { | |
1112 | h->root.u.i.link = (struct bfd_link_hash_entry *) vh; | |
1113 | vh->dynamic_def = 1; | |
1114 | vh->ref_dynamic = 1; | |
1115 | } | |
1116 | else | |
1117 | { | |
1118 | h->root.type = vh->root.type; | |
1119 | vh->ref_dynamic = 0; | |
1120 | /* We have to hide it here since it was made dynamic | |
1121 | global with extra bits when the symbol info was | |
1122 | copied from the old dynamic definition. */ | |
1123 | (*bed->elf_backend_hide_symbol) (info, vh, TRUE); | |
1124 | } | |
1125 | h = vh; | |
1126 | } | |
1127 | else | |
1128 | h = *sym_hash; | |
1129 | } | |
1de1a317 | 1130 | |
f6e332e6 | 1131 | if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root) |
1de1a317 L |
1132 | && bfd_is_und_section (sec)) |
1133 | { | |
1134 | /* If the new symbol is undefined and the old symbol was | |
1135 | also undefined before, we need to make sure | |
1136 | _bfd_generic_link_add_one_symbol doesn't mess | |
f6e332e6 | 1137 | up the linker hash table undefs list. Since the old |
1de1a317 L |
1138 | definition came from a dynamic object, it is still on the |
1139 | undefs list. */ | |
1140 | h->root.type = bfd_link_hash_undefined; | |
1de1a317 L |
1141 | h->root.u.undef.abfd = abfd; |
1142 | } | |
1143 | else | |
1144 | { | |
1145 | h->root.type = bfd_link_hash_new; | |
1146 | h->root.u.undef.abfd = NULL; | |
1147 | } | |
1148 | ||
f5385ebf | 1149 | if (h->def_dynamic) |
252b5132 | 1150 | { |
f5385ebf AM |
1151 | h->def_dynamic = 0; |
1152 | h->ref_dynamic = 1; | |
1153 | h->dynamic_def = 1; | |
45d6a902 AM |
1154 | } |
1155 | /* FIXME: Should we check type and size for protected symbol? */ | |
1156 | h->size = 0; | |
1157 | h->type = 0; | |
1158 | return TRUE; | |
1159 | } | |
14a793b2 | 1160 | |
79349b09 AM |
1161 | /* Differentiate strong and weak symbols. */ |
1162 | newweak = bind == STB_WEAK; | |
1163 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1164 | || h->root.type == bfd_link_hash_undefweak); | |
14a793b2 | 1165 | |
15b43f48 AM |
1166 | /* If a new weak symbol definition comes from a regular file and the |
1167 | old symbol comes from a dynamic library, we treat the new one as | |
1168 | strong. Similarly, an old weak symbol definition from a regular | |
1169 | file is treated as strong when the new symbol comes from a dynamic | |
1170 | library. Further, an old weak symbol from a dynamic library is | |
1171 | treated as strong if the new symbol is from a dynamic library. | |
1172 | This reflects the way glibc's ld.so works. | |
1173 | ||
1174 | Do this before setting *type_change_ok or *size_change_ok so that | |
1175 | we warn properly when dynamic library symbols are overridden. */ | |
1176 | ||
1177 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1178 | newweak = FALSE; |
15b43f48 | 1179 | if (olddef && newdyn) |
0f8a2703 AM |
1180 | oldweak = FALSE; |
1181 | ||
fcb93ecf PB |
1182 | /* Allow changes between different types of funciton symbol. */ |
1183 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)) | |
1184 | && bed->is_function_type (h->type)) | |
1185 | *type_change_ok = TRUE; | |
1186 | ||
79349b09 AM |
1187 | /* It's OK to change the type if either the existing symbol or the |
1188 | new symbol is weak. A type change is also OK if the old symbol | |
1189 | is undefined and the new symbol is defined. */ | |
252b5132 | 1190 | |
79349b09 AM |
1191 | if (oldweak |
1192 | || newweak | |
1193 | || (newdef | |
1194 | && h->root.type == bfd_link_hash_undefined)) | |
1195 | *type_change_ok = TRUE; | |
1196 | ||
1197 | /* It's OK to change the size if either the existing symbol or the | |
1198 | new symbol is weak, or if the old symbol is undefined. */ | |
1199 | ||
1200 | if (*type_change_ok | |
1201 | || h->root.type == bfd_link_hash_undefined) | |
1202 | *size_change_ok = TRUE; | |
45d6a902 | 1203 | |
45d6a902 AM |
1204 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1205 | symbol, respectively, appears to be a common symbol in a dynamic | |
1206 | object. If a symbol appears in an uninitialized section, and is | |
1207 | not weak, and is not a function, then it may be a common symbol | |
1208 | which was resolved when the dynamic object was created. We want | |
1209 | to treat such symbols specially, because they raise special | |
1210 | considerations when setting the symbol size: if the symbol | |
1211 | appears as a common symbol in a regular object, and the size in | |
1212 | the regular object is larger, we must make sure that we use the | |
1213 | larger size. This problematic case can always be avoided in C, | |
1214 | but it must be handled correctly when using Fortran shared | |
1215 | libraries. | |
1216 | ||
1217 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1218 | likewise for OLDDYNCOMMON and OLDDEF. | |
1219 | ||
1220 | Note that this test is just a heuristic, and that it is quite | |
1221 | possible to have an uninitialized symbol in a shared object which | |
1222 | is really a definition, rather than a common symbol. This could | |
1223 | lead to some minor confusion when the symbol really is a common | |
1224 | symbol in some regular object. However, I think it will be | |
1225 | harmless. */ | |
1226 | ||
1227 | if (newdyn | |
1228 | && newdef | |
79349b09 | 1229 | && !newweak |
45d6a902 AM |
1230 | && (sec->flags & SEC_ALLOC) != 0 |
1231 | && (sec->flags & SEC_LOAD) == 0 | |
1232 | && sym->st_size > 0 | |
fcb93ecf | 1233 | && !bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
45d6a902 AM |
1234 | newdyncommon = TRUE; |
1235 | else | |
1236 | newdyncommon = FALSE; | |
1237 | ||
1238 | if (olddyn | |
1239 | && olddef | |
1240 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1241 | && h->def_dynamic |
45d6a902 AM |
1242 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1243 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1244 | && h->size > 0 | |
fcb93ecf | 1245 | && !bed->is_function_type (h->type)) |
45d6a902 AM |
1246 | olddyncommon = TRUE; |
1247 | else | |
1248 | olddyncommon = FALSE; | |
1249 | ||
a4d8e49b L |
1250 | /* We now know everything about the old and new symbols. We ask the |
1251 | backend to check if we can merge them. */ | |
a4d8e49b L |
1252 | if (bed->merge_symbol |
1253 | && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue, | |
1254 | pold_alignment, skip, override, | |
1255 | type_change_ok, size_change_ok, | |
1256 | &newdyn, &newdef, &newdyncommon, &newweak, | |
1257 | abfd, &sec, | |
1258 | &olddyn, &olddef, &olddyncommon, &oldweak, | |
1259 | oldbfd, &oldsec)) | |
1260 | return FALSE; | |
1261 | ||
45d6a902 AM |
1262 | /* If both the old and the new symbols look like common symbols in a |
1263 | dynamic object, set the size of the symbol to the larger of the | |
1264 | two. */ | |
1265 | ||
1266 | if (olddyncommon | |
1267 | && newdyncommon | |
1268 | && sym->st_size != h->size) | |
1269 | { | |
1270 | /* Since we think we have two common symbols, issue a multiple | |
1271 | common warning if desired. Note that we only warn if the | |
1272 | size is different. If the size is the same, we simply let | |
1273 | the old symbol override the new one as normally happens with | |
1274 | symbols defined in dynamic objects. */ | |
1275 | ||
1276 | if (! ((*info->callbacks->multiple_common) | |
1277 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1278 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1279 | return FALSE; | |
252b5132 | 1280 | |
45d6a902 AM |
1281 | if (sym->st_size > h->size) |
1282 | h->size = sym->st_size; | |
252b5132 | 1283 | |
45d6a902 | 1284 | *size_change_ok = TRUE; |
252b5132 RH |
1285 | } |
1286 | ||
45d6a902 AM |
1287 | /* If we are looking at a dynamic object, and we have found a |
1288 | definition, we need to see if the symbol was already defined by | |
1289 | some other object. If so, we want to use the existing | |
1290 | definition, and we do not want to report a multiple symbol | |
1291 | definition error; we do this by clobbering *PSEC to be | |
1292 | bfd_und_section_ptr. | |
1293 | ||
1294 | We treat a common symbol as a definition if the symbol in the | |
1295 | shared library is a function, since common symbols always | |
1296 | represent variables; this can cause confusion in principle, but | |
1297 | any such confusion would seem to indicate an erroneous program or | |
1298 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1299 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1300 | |
1301 | if (newdyn | |
1302 | && newdef | |
77cfaee6 | 1303 | && (olddef |
45d6a902 | 1304 | || (h->root.type == bfd_link_hash_common |
79349b09 | 1305 | && (newweak |
fcb93ecf | 1306 | || bed->is_function_type (ELF_ST_TYPE (sym->st_info)))))) |
45d6a902 AM |
1307 | { |
1308 | *override = TRUE; | |
1309 | newdef = FALSE; | |
1310 | newdyncommon = FALSE; | |
252b5132 | 1311 | |
45d6a902 AM |
1312 | *psec = sec = bfd_und_section_ptr; |
1313 | *size_change_ok = TRUE; | |
252b5132 | 1314 | |
45d6a902 AM |
1315 | /* If we get here when the old symbol is a common symbol, then |
1316 | we are explicitly letting it override a weak symbol or | |
1317 | function in a dynamic object, and we don't want to warn about | |
1318 | a type change. If the old symbol is a defined symbol, a type | |
1319 | change warning may still be appropriate. */ | |
252b5132 | 1320 | |
45d6a902 AM |
1321 | if (h->root.type == bfd_link_hash_common) |
1322 | *type_change_ok = TRUE; | |
1323 | } | |
1324 | ||
1325 | /* Handle the special case of an old common symbol merging with a | |
1326 | new symbol which looks like a common symbol in a shared object. | |
1327 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1328 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1329 | right thing. */ | |
45d6a902 AM |
1330 | |
1331 | if (newdyncommon | |
1332 | && h->root.type == bfd_link_hash_common) | |
1333 | { | |
1334 | *override = TRUE; | |
1335 | newdef = FALSE; | |
1336 | newdyncommon = FALSE; | |
1337 | *pvalue = sym->st_size; | |
a4d8e49b | 1338 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1339 | *size_change_ok = TRUE; |
1340 | } | |
1341 | ||
c5e2cead | 1342 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1343 | if (newdef && olddef && newweak) |
c5e2cead L |
1344 | *skip = TRUE; |
1345 | ||
45d6a902 AM |
1346 | /* If the old symbol is from a dynamic object, and the new symbol is |
1347 | a definition which is not from a dynamic object, then the new | |
1348 | symbol overrides the old symbol. Symbols from regular files | |
1349 | always take precedence over symbols from dynamic objects, even if | |
1350 | they are defined after the dynamic object in the link. | |
1351 | ||
1352 | As above, we again permit a common symbol in a regular object to | |
1353 | override a definition in a shared object if the shared object | |
0f8a2703 | 1354 | symbol is a function or is weak. */ |
45d6a902 AM |
1355 | |
1356 | flip = NULL; | |
77cfaee6 | 1357 | if (!newdyn |
45d6a902 AM |
1358 | && (newdef |
1359 | || (bfd_is_com_section (sec) | |
79349b09 | 1360 | && (oldweak |
fcb93ecf | 1361 | || bed->is_function_type (h->type)))) |
45d6a902 AM |
1362 | && olddyn |
1363 | && olddef | |
f5385ebf | 1364 | && h->def_dynamic) |
45d6a902 AM |
1365 | { |
1366 | /* Change the hash table entry to undefined, and let | |
1367 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1368 | new definition. */ | |
1369 | ||
1370 | h->root.type = bfd_link_hash_undefined; | |
1371 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1372 | *size_change_ok = TRUE; | |
1373 | ||
1374 | olddef = FALSE; | |
1375 | olddyncommon = FALSE; | |
1376 | ||
1377 | /* We again permit a type change when a common symbol may be | |
1378 | overriding a function. */ | |
1379 | ||
1380 | if (bfd_is_com_section (sec)) | |
1381 | *type_change_ok = TRUE; | |
1382 | ||
1383 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1384 | flip = *sym_hash; | |
1385 | else | |
1386 | /* This union may have been set to be non-NULL when this symbol | |
1387 | was seen in a dynamic object. We must force the union to be | |
1388 | NULL, so that it is correct for a regular symbol. */ | |
1389 | h->verinfo.vertree = NULL; | |
1390 | } | |
1391 | ||
1392 | /* Handle the special case of a new common symbol merging with an | |
1393 | old symbol that looks like it might be a common symbol defined in | |
1394 | a shared object. Note that we have already handled the case in | |
1395 | which a new common symbol should simply override the definition | |
1396 | in the shared library. */ | |
1397 | ||
1398 | if (! newdyn | |
1399 | && bfd_is_com_section (sec) | |
1400 | && olddyncommon) | |
1401 | { | |
1402 | /* It would be best if we could set the hash table entry to a | |
1403 | common symbol, but we don't know what to use for the section | |
1404 | or the alignment. */ | |
1405 | if (! ((*info->callbacks->multiple_common) | |
1406 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1407 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1408 | return FALSE; | |
1409 | ||
4cc11e76 | 1410 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1411 | larger, pretend that the new symbol has its size. */ |
1412 | ||
1413 | if (h->size > *pvalue) | |
1414 | *pvalue = h->size; | |
1415 | ||
af44c138 L |
1416 | /* We need to remember the alignment required by the symbol |
1417 | in the dynamic object. */ | |
1418 | BFD_ASSERT (pold_alignment); | |
1419 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1420 | |
1421 | olddef = FALSE; | |
1422 | olddyncommon = FALSE; | |
1423 | ||
1424 | h->root.type = bfd_link_hash_undefined; | |
1425 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1426 | ||
1427 | *size_change_ok = TRUE; | |
1428 | *type_change_ok = TRUE; | |
1429 | ||
1430 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1431 | flip = *sym_hash; | |
1432 | else | |
1433 | h->verinfo.vertree = NULL; | |
1434 | } | |
1435 | ||
1436 | if (flip != NULL) | |
1437 | { | |
1438 | /* Handle the case where we had a versioned symbol in a dynamic | |
1439 | library and now find a definition in a normal object. In this | |
1440 | case, we make the versioned symbol point to the normal one. */ | |
9c5bfbb7 | 1441 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 1442 | flip->root.type = h->root.type; |
00cbee0a | 1443 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1444 | h->root.type = bfd_link_hash_indirect; |
1445 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1446 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1447 | if (h->def_dynamic) |
45d6a902 | 1448 | { |
f5385ebf AM |
1449 | h->def_dynamic = 0; |
1450 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1451 | } |
1452 | } | |
1453 | ||
45d6a902 AM |
1454 | return TRUE; |
1455 | } | |
1456 | ||
1457 | /* This function is called to create an indirect symbol from the | |
1458 | default for the symbol with the default version if needed. The | |
1459 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1460 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 AM |
1461 | |
1462 | bfd_boolean | |
268b6b39 AM |
1463 | _bfd_elf_add_default_symbol (bfd *abfd, |
1464 | struct bfd_link_info *info, | |
1465 | struct elf_link_hash_entry *h, | |
1466 | const char *name, | |
1467 | Elf_Internal_Sym *sym, | |
1468 | asection **psec, | |
1469 | bfd_vma *value, | |
1470 | bfd_boolean *dynsym, | |
0f8a2703 | 1471 | bfd_boolean override) |
45d6a902 AM |
1472 | { |
1473 | bfd_boolean type_change_ok; | |
1474 | bfd_boolean size_change_ok; | |
1475 | bfd_boolean skip; | |
1476 | char *shortname; | |
1477 | struct elf_link_hash_entry *hi; | |
1478 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1479 | const struct elf_backend_data *bed; |
45d6a902 AM |
1480 | bfd_boolean collect; |
1481 | bfd_boolean dynamic; | |
1482 | char *p; | |
1483 | size_t len, shortlen; | |
1484 | asection *sec; | |
1485 | ||
1486 | /* If this symbol has a version, and it is the default version, we | |
1487 | create an indirect symbol from the default name to the fully | |
1488 | decorated name. This will cause external references which do not | |
1489 | specify a version to be bound to this version of the symbol. */ | |
1490 | p = strchr (name, ELF_VER_CHR); | |
1491 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1492 | return TRUE; | |
1493 | ||
1494 | if (override) | |
1495 | { | |
4cc11e76 | 1496 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1497 | need to create the indirect symbol from the default name. */ |
1498 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1499 | FALSE, FALSE); | |
1500 | BFD_ASSERT (hi != NULL); | |
1501 | if (hi == h) | |
1502 | return TRUE; | |
1503 | while (hi->root.type == bfd_link_hash_indirect | |
1504 | || hi->root.type == bfd_link_hash_warning) | |
1505 | { | |
1506 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1507 | if (hi == h) | |
1508 | return TRUE; | |
1509 | } | |
1510 | } | |
1511 | ||
1512 | bed = get_elf_backend_data (abfd); | |
1513 | collect = bed->collect; | |
1514 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1515 | ||
1516 | shortlen = p - name; | |
1517 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1518 | if (shortname == NULL) | |
1519 | return FALSE; | |
1520 | memcpy (shortname, name, shortlen); | |
1521 | shortname[shortlen] = '\0'; | |
1522 | ||
1523 | /* We are going to create a new symbol. Merge it with any existing | |
1524 | symbol with this name. For the purposes of the merge, act as | |
1525 | though we were defining the symbol we just defined, although we | |
1526 | actually going to define an indirect symbol. */ | |
1527 | type_change_ok = FALSE; | |
1528 | size_change_ok = FALSE; | |
1529 | sec = *psec; | |
1530 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1531 | NULL, &hi, &skip, &override, |
1532 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1533 | return FALSE; |
1534 | ||
1535 | if (skip) | |
1536 | goto nondefault; | |
1537 | ||
1538 | if (! override) | |
1539 | { | |
1540 | bh = &hi->root; | |
1541 | if (! (_bfd_generic_link_add_one_symbol | |
1542 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1543 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1544 | return FALSE; |
1545 | hi = (struct elf_link_hash_entry *) bh; | |
1546 | } | |
1547 | else | |
1548 | { | |
1549 | /* In this case the symbol named SHORTNAME is overriding the | |
1550 | indirect symbol we want to add. We were planning on making | |
1551 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1552 | is the name without a version. NAME is the fully versioned | |
1553 | name, and it is the default version. | |
1554 | ||
1555 | Overriding means that we already saw a definition for the | |
1556 | symbol SHORTNAME in a regular object, and it is overriding | |
1557 | the symbol defined in the dynamic object. | |
1558 | ||
1559 | When this happens, we actually want to change NAME, the | |
1560 | symbol we just added, to refer to SHORTNAME. This will cause | |
1561 | references to NAME in the shared object to become references | |
1562 | to SHORTNAME in the regular object. This is what we expect | |
1563 | when we override a function in a shared object: that the | |
1564 | references in the shared object will be mapped to the | |
1565 | definition in the regular object. */ | |
1566 | ||
1567 | while (hi->root.type == bfd_link_hash_indirect | |
1568 | || hi->root.type == bfd_link_hash_warning) | |
1569 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1570 | ||
1571 | h->root.type = bfd_link_hash_indirect; | |
1572 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1573 | if (h->def_dynamic) |
45d6a902 | 1574 | { |
f5385ebf AM |
1575 | h->def_dynamic = 0; |
1576 | hi->ref_dynamic = 1; | |
1577 | if (hi->ref_regular | |
1578 | || hi->def_regular) | |
45d6a902 | 1579 | { |
c152c796 | 1580 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1581 | return FALSE; |
1582 | } | |
1583 | } | |
1584 | ||
1585 | /* Now set HI to H, so that the following code will set the | |
1586 | other fields correctly. */ | |
1587 | hi = h; | |
1588 | } | |
1589 | ||
fab4a87f L |
1590 | /* Check if HI is a warning symbol. */ |
1591 | if (hi->root.type == bfd_link_hash_warning) | |
1592 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1593 | ||
45d6a902 AM |
1594 | /* If there is a duplicate definition somewhere, then HI may not |
1595 | point to an indirect symbol. We will have reported an error to | |
1596 | the user in that case. */ | |
1597 | ||
1598 | if (hi->root.type == bfd_link_hash_indirect) | |
1599 | { | |
1600 | struct elf_link_hash_entry *ht; | |
1601 | ||
45d6a902 | 1602 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 1603 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 AM |
1604 | |
1605 | /* See if the new flags lead us to realize that the symbol must | |
1606 | be dynamic. */ | |
1607 | if (! *dynsym) | |
1608 | { | |
1609 | if (! dynamic) | |
1610 | { | |
1611 | if (info->shared | |
f5385ebf | 1612 | || hi->ref_dynamic) |
45d6a902 AM |
1613 | *dynsym = TRUE; |
1614 | } | |
1615 | else | |
1616 | { | |
f5385ebf | 1617 | if (hi->ref_regular) |
45d6a902 AM |
1618 | *dynsym = TRUE; |
1619 | } | |
1620 | } | |
1621 | } | |
1622 | ||
1623 | /* We also need to define an indirection from the nondefault version | |
1624 | of the symbol. */ | |
1625 | ||
1626 | nondefault: | |
1627 | len = strlen (name); | |
1628 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1629 | if (shortname == NULL) | |
1630 | return FALSE; | |
1631 | memcpy (shortname, name, shortlen); | |
1632 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1633 | ||
1634 | /* Once again, merge with any existing symbol. */ | |
1635 | type_change_ok = FALSE; | |
1636 | size_change_ok = FALSE; | |
1637 | sec = *psec; | |
1638 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1639 | NULL, &hi, &skip, &override, |
1640 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1641 | return FALSE; |
1642 | ||
1643 | if (skip) | |
1644 | return TRUE; | |
1645 | ||
1646 | if (override) | |
1647 | { | |
1648 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1649 | the type of override we do in the case above unless it is | |
4cc11e76 | 1650 | overridden by a versioned definition. */ |
45d6a902 AM |
1651 | if (hi->root.type != bfd_link_hash_defined |
1652 | && hi->root.type != bfd_link_hash_defweak) | |
1653 | (*_bfd_error_handler) | |
d003868e AM |
1654 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1655 | abfd, shortname); | |
45d6a902 AM |
1656 | } |
1657 | else | |
1658 | { | |
1659 | bh = &hi->root; | |
1660 | if (! (_bfd_generic_link_add_one_symbol | |
1661 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1662 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1663 | return FALSE; |
1664 | hi = (struct elf_link_hash_entry *) bh; | |
1665 | ||
1666 | /* If there is a duplicate definition somewhere, then HI may not | |
1667 | point to an indirect symbol. We will have reported an error | |
1668 | to the user in that case. */ | |
1669 | ||
1670 | if (hi->root.type == bfd_link_hash_indirect) | |
1671 | { | |
fcfa13d2 | 1672 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
45d6a902 AM |
1673 | |
1674 | /* See if the new flags lead us to realize that the symbol | |
1675 | must be dynamic. */ | |
1676 | if (! *dynsym) | |
1677 | { | |
1678 | if (! dynamic) | |
1679 | { | |
1680 | if (info->shared | |
f5385ebf | 1681 | || hi->ref_dynamic) |
45d6a902 AM |
1682 | *dynsym = TRUE; |
1683 | } | |
1684 | else | |
1685 | { | |
f5385ebf | 1686 | if (hi->ref_regular) |
45d6a902 AM |
1687 | *dynsym = TRUE; |
1688 | } | |
1689 | } | |
1690 | } | |
1691 | } | |
1692 | ||
1693 | return TRUE; | |
1694 | } | |
1695 | \f | |
1696 | /* This routine is used to export all defined symbols into the dynamic | |
1697 | symbol table. It is called via elf_link_hash_traverse. */ | |
1698 | ||
1699 | bfd_boolean | |
268b6b39 | 1700 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1701 | { |
268b6b39 | 1702 | struct elf_info_failed *eif = data; |
45d6a902 | 1703 | |
55255dae L |
1704 | /* Ignore this if we won't export it. */ |
1705 | if (!eif->info->export_dynamic && !h->dynamic) | |
1706 | return TRUE; | |
1707 | ||
45d6a902 AM |
1708 | /* Ignore indirect symbols. These are added by the versioning code. */ |
1709 | if (h->root.type == bfd_link_hash_indirect) | |
1710 | return TRUE; | |
1711 | ||
1712 | if (h->root.type == bfd_link_hash_warning) | |
1713 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1714 | ||
1715 | if (h->dynindx == -1 | |
f5385ebf AM |
1716 | && (h->def_regular |
1717 | || h->ref_regular)) | |
45d6a902 AM |
1718 | { |
1719 | struct bfd_elf_version_tree *t; | |
1720 | struct bfd_elf_version_expr *d; | |
1721 | ||
1722 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1723 | { | |
108ba305 | 1724 | if (t->globals.list != NULL) |
45d6a902 | 1725 | { |
108ba305 JJ |
1726 | d = (*t->match) (&t->globals, NULL, h->root.root.string); |
1727 | if (d != NULL) | |
1728 | goto doit; | |
45d6a902 AM |
1729 | } |
1730 | ||
108ba305 | 1731 | if (t->locals.list != NULL) |
45d6a902 | 1732 | { |
108ba305 JJ |
1733 | d = (*t->match) (&t->locals, NULL, h->root.root.string); |
1734 | if (d != NULL) | |
1735 | return TRUE; | |
45d6a902 AM |
1736 | } |
1737 | } | |
1738 | ||
1739 | if (!eif->verdefs) | |
1740 | { | |
1741 | doit: | |
c152c796 | 1742 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
1743 | { |
1744 | eif->failed = TRUE; | |
1745 | return FALSE; | |
1746 | } | |
1747 | } | |
1748 | } | |
1749 | ||
1750 | return TRUE; | |
1751 | } | |
1752 | \f | |
1753 | /* Look through the symbols which are defined in other shared | |
1754 | libraries and referenced here. Update the list of version | |
1755 | dependencies. This will be put into the .gnu.version_r section. | |
1756 | This function is called via elf_link_hash_traverse. */ | |
1757 | ||
1758 | bfd_boolean | |
268b6b39 AM |
1759 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1760 | void *data) | |
45d6a902 | 1761 | { |
268b6b39 | 1762 | struct elf_find_verdep_info *rinfo = data; |
45d6a902 AM |
1763 | Elf_Internal_Verneed *t; |
1764 | Elf_Internal_Vernaux *a; | |
1765 | bfd_size_type amt; | |
1766 | ||
1767 | if (h->root.type == bfd_link_hash_warning) | |
1768 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1769 | ||
1770 | /* We only care about symbols defined in shared objects with version | |
1771 | information. */ | |
f5385ebf AM |
1772 | if (!h->def_dynamic |
1773 | || h->def_regular | |
45d6a902 AM |
1774 | || h->dynindx == -1 |
1775 | || h->verinfo.verdef == NULL) | |
1776 | return TRUE; | |
1777 | ||
1778 | /* See if we already know about this version. */ | |
1779 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1780 | { | |
1781 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1782 | continue; | |
1783 | ||
1784 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1785 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1786 | return TRUE; | |
1787 | ||
1788 | break; | |
1789 | } | |
1790 | ||
1791 | /* This is a new version. Add it to tree we are building. */ | |
1792 | ||
1793 | if (t == NULL) | |
1794 | { | |
1795 | amt = sizeof *t; | |
268b6b39 | 1796 | t = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1797 | if (t == NULL) |
1798 | { | |
1799 | rinfo->failed = TRUE; | |
1800 | return FALSE; | |
1801 | } | |
1802 | ||
1803 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1804 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1805 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1806 | } | |
1807 | ||
1808 | amt = sizeof *a; | |
268b6b39 | 1809 | a = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1810 | |
1811 | /* Note that we are copying a string pointer here, and testing it | |
1812 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1813 | discard the string data when low in memory, this will have to be | |
1814 | fixed. */ | |
1815 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1816 | ||
1817 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1818 | a->vna_nextptr = t->vn_auxptr; | |
1819 | ||
1820 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1821 | ++rinfo->vers; | |
1822 | ||
1823 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1824 | ||
1825 | t->vn_auxptr = a; | |
1826 | ||
1827 | return TRUE; | |
1828 | } | |
1829 | ||
1830 | /* Figure out appropriate versions for all the symbols. We may not | |
1831 | have the version number script until we have read all of the input | |
1832 | files, so until that point we don't know which symbols should be | |
1833 | local. This function is called via elf_link_hash_traverse. */ | |
1834 | ||
1835 | bfd_boolean | |
268b6b39 | 1836 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
1837 | { |
1838 | struct elf_assign_sym_version_info *sinfo; | |
1839 | struct bfd_link_info *info; | |
9c5bfbb7 | 1840 | const struct elf_backend_data *bed; |
45d6a902 AM |
1841 | struct elf_info_failed eif; |
1842 | char *p; | |
1843 | bfd_size_type amt; | |
1844 | ||
268b6b39 | 1845 | sinfo = data; |
45d6a902 AM |
1846 | info = sinfo->info; |
1847 | ||
1848 | if (h->root.type == bfd_link_hash_warning) | |
1849 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1850 | ||
1851 | /* Fix the symbol flags. */ | |
1852 | eif.failed = FALSE; | |
1853 | eif.info = info; | |
1854 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1855 | { | |
1856 | if (eif.failed) | |
1857 | sinfo->failed = TRUE; | |
1858 | return FALSE; | |
1859 | } | |
1860 | ||
1861 | /* We only need version numbers for symbols defined in regular | |
1862 | objects. */ | |
f5385ebf | 1863 | if (!h->def_regular) |
45d6a902 AM |
1864 | return TRUE; |
1865 | ||
1866 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1867 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1868 | if (p != NULL && h->verinfo.vertree == NULL) | |
1869 | { | |
1870 | struct bfd_elf_version_tree *t; | |
1871 | bfd_boolean hidden; | |
1872 | ||
1873 | hidden = TRUE; | |
1874 | ||
1875 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1876 | not a hidden symbol. */ | |
1877 | ++p; | |
1878 | if (*p == ELF_VER_CHR) | |
1879 | { | |
1880 | hidden = FALSE; | |
1881 | ++p; | |
1882 | } | |
1883 | ||
1884 | /* If there is no version string, we can just return out. */ | |
1885 | if (*p == '\0') | |
1886 | { | |
1887 | if (hidden) | |
f5385ebf | 1888 | h->hidden = 1; |
45d6a902 AM |
1889 | return TRUE; |
1890 | } | |
1891 | ||
1892 | /* Look for the version. If we find it, it is no longer weak. */ | |
1893 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1894 | { | |
1895 | if (strcmp (t->name, p) == 0) | |
1896 | { | |
1897 | size_t len; | |
1898 | char *alc; | |
1899 | struct bfd_elf_version_expr *d; | |
1900 | ||
1901 | len = p - h->root.root.string; | |
268b6b39 | 1902 | alc = bfd_malloc (len); |
45d6a902 AM |
1903 | if (alc == NULL) |
1904 | return FALSE; | |
1905 | memcpy (alc, h->root.root.string, len - 1); | |
1906 | alc[len - 1] = '\0'; | |
1907 | if (alc[len - 2] == ELF_VER_CHR) | |
1908 | alc[len - 2] = '\0'; | |
1909 | ||
1910 | h->verinfo.vertree = t; | |
1911 | t->used = TRUE; | |
1912 | d = NULL; | |
1913 | ||
108ba305 JJ |
1914 | if (t->globals.list != NULL) |
1915 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
1916 | |
1917 | /* See if there is anything to force this symbol to | |
1918 | local scope. */ | |
108ba305 | 1919 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 1920 | { |
108ba305 JJ |
1921 | d = (*t->match) (&t->locals, NULL, alc); |
1922 | if (d != NULL | |
1923 | && h->dynindx != -1 | |
108ba305 JJ |
1924 | && ! info->export_dynamic) |
1925 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
1926 | } |
1927 | ||
1928 | free (alc); | |
1929 | break; | |
1930 | } | |
1931 | } | |
1932 | ||
1933 | /* If we are building an application, we need to create a | |
1934 | version node for this version. */ | |
36af4a4e | 1935 | if (t == NULL && info->executable) |
45d6a902 AM |
1936 | { |
1937 | struct bfd_elf_version_tree **pp; | |
1938 | int version_index; | |
1939 | ||
1940 | /* If we aren't going to export this symbol, we don't need | |
1941 | to worry about it. */ | |
1942 | if (h->dynindx == -1) | |
1943 | return TRUE; | |
1944 | ||
1945 | amt = sizeof *t; | |
108ba305 | 1946 | t = bfd_zalloc (sinfo->output_bfd, amt); |
45d6a902 AM |
1947 | if (t == NULL) |
1948 | { | |
1949 | sinfo->failed = TRUE; | |
1950 | return FALSE; | |
1951 | } | |
1952 | ||
45d6a902 | 1953 | t->name = p; |
45d6a902 AM |
1954 | t->name_indx = (unsigned int) -1; |
1955 | t->used = TRUE; | |
1956 | ||
1957 | version_index = 1; | |
1958 | /* Don't count anonymous version tag. */ | |
1959 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1960 | version_index = 0; | |
1961 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1962 | ++version_index; | |
1963 | t->vernum = version_index; | |
1964 | ||
1965 | *pp = t; | |
1966 | ||
1967 | h->verinfo.vertree = t; | |
1968 | } | |
1969 | else if (t == NULL) | |
1970 | { | |
1971 | /* We could not find the version for a symbol when | |
1972 | generating a shared archive. Return an error. */ | |
1973 | (*_bfd_error_handler) | |
c55fe096 | 1974 | (_("%B: version node not found for symbol %s"), |
d003868e | 1975 | sinfo->output_bfd, h->root.root.string); |
45d6a902 AM |
1976 | bfd_set_error (bfd_error_bad_value); |
1977 | sinfo->failed = TRUE; | |
1978 | return FALSE; | |
1979 | } | |
1980 | ||
1981 | if (hidden) | |
f5385ebf | 1982 | h->hidden = 1; |
45d6a902 AM |
1983 | } |
1984 | ||
1985 | /* If we don't have a version for this symbol, see if we can find | |
1986 | something. */ | |
1987 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
1988 | { | |
1989 | struct bfd_elf_version_tree *t; | |
1990 | struct bfd_elf_version_tree *local_ver; | |
1991 | struct bfd_elf_version_expr *d; | |
1992 | ||
1993 | /* See if can find what version this symbol is in. If the | |
1994 | symbol is supposed to be local, then don't actually register | |
1995 | it. */ | |
1996 | local_ver = NULL; | |
1997 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1998 | { | |
108ba305 | 1999 | if (t->globals.list != NULL) |
45d6a902 AM |
2000 | { |
2001 | bfd_boolean matched; | |
2002 | ||
2003 | matched = FALSE; | |
108ba305 JJ |
2004 | d = NULL; |
2005 | while ((d = (*t->match) (&t->globals, d, | |
2006 | h->root.root.string)) != NULL) | |
2007 | if (d->symver) | |
2008 | matched = TRUE; | |
2009 | else | |
2010 | { | |
2011 | /* There is a version without definition. Make | |
2012 | the symbol the default definition for this | |
2013 | version. */ | |
2014 | h->verinfo.vertree = t; | |
2015 | local_ver = NULL; | |
2016 | d->script = 1; | |
2017 | break; | |
2018 | } | |
45d6a902 AM |
2019 | if (d != NULL) |
2020 | break; | |
2021 | else if (matched) | |
2022 | /* There is no undefined version for this symbol. Hide the | |
2023 | default one. */ | |
2024 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2025 | } | |
2026 | ||
108ba305 | 2027 | if (t->locals.list != NULL) |
45d6a902 | 2028 | { |
108ba305 JJ |
2029 | d = NULL; |
2030 | while ((d = (*t->match) (&t->locals, d, | |
2031 | h->root.root.string)) != NULL) | |
45d6a902 | 2032 | { |
108ba305 | 2033 | local_ver = t; |
45d6a902 | 2034 | /* If the match is "*", keep looking for a more |
108ba305 JJ |
2035 | explicit, perhaps even global, match. |
2036 | XXX: Shouldn't this be !d->wildcard instead? */ | |
2037 | if (d->pattern[0] != '*' || d->pattern[1] != '\0') | |
2038 | break; | |
45d6a902 AM |
2039 | } |
2040 | ||
2041 | if (d != NULL) | |
2042 | break; | |
2043 | } | |
2044 | } | |
2045 | ||
2046 | if (local_ver != NULL) | |
2047 | { | |
2048 | h->verinfo.vertree = local_ver; | |
2049 | if (h->dynindx != -1 | |
45d6a902 AM |
2050 | && ! info->export_dynamic) |
2051 | { | |
2052 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2053 | } | |
2054 | } | |
2055 | } | |
2056 | ||
2057 | return TRUE; | |
2058 | } | |
2059 | \f | |
45d6a902 AM |
2060 | /* Read and swap the relocs from the section indicated by SHDR. This |
2061 | may be either a REL or a RELA section. The relocations are | |
2062 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2063 | which should have already been allocated to contain enough space. | |
2064 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2065 | relocations should be stored. | |
2066 | ||
2067 | Returns FALSE if something goes wrong. */ | |
2068 | ||
2069 | static bfd_boolean | |
268b6b39 | 2070 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2071 | asection *sec, |
268b6b39 AM |
2072 | Elf_Internal_Shdr *shdr, |
2073 | void *external_relocs, | |
2074 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2075 | { |
9c5bfbb7 | 2076 | const struct elf_backend_data *bed; |
268b6b39 | 2077 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2078 | const bfd_byte *erela; |
2079 | const bfd_byte *erelaend; | |
2080 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2081 | Elf_Internal_Shdr *symtab_hdr; |
2082 | size_t nsyms; | |
45d6a902 | 2083 | |
45d6a902 AM |
2084 | /* Position ourselves at the start of the section. */ |
2085 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2086 | return FALSE; | |
2087 | ||
2088 | /* Read the relocations. */ | |
2089 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2090 | return FALSE; | |
2091 | ||
243ef1e0 L |
2092 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2093 | nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
2094 | ||
45d6a902 AM |
2095 | bed = get_elf_backend_data (abfd); |
2096 | ||
2097 | /* Convert the external relocations to the internal format. */ | |
2098 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2099 | swap_in = bed->s->swap_reloc_in; | |
2100 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2101 | swap_in = bed->s->swap_reloca_in; | |
2102 | else | |
2103 | { | |
2104 | bfd_set_error (bfd_error_wrong_format); | |
2105 | return FALSE; | |
2106 | } | |
2107 | ||
2108 | erela = external_relocs; | |
51992aec | 2109 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2110 | irela = internal_relocs; |
2111 | while (erela < erelaend) | |
2112 | { | |
243ef1e0 L |
2113 | bfd_vma r_symndx; |
2114 | ||
45d6a902 | 2115 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2116 | r_symndx = ELF32_R_SYM (irela->r_info); |
2117 | if (bed->s->arch_size == 64) | |
2118 | r_symndx >>= 24; | |
2119 | if ((size_t) r_symndx >= nsyms) | |
2120 | { | |
2121 | (*_bfd_error_handler) | |
d003868e AM |
2122 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" |
2123 | " for offset 0x%lx in section `%A'"), | |
2124 | abfd, sec, | |
2125 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
2126 | bfd_set_error (bfd_error_bad_value); |
2127 | return FALSE; | |
2128 | } | |
45d6a902 AM |
2129 | irela += bed->s->int_rels_per_ext_rel; |
2130 | erela += shdr->sh_entsize; | |
2131 | } | |
2132 | ||
2133 | return TRUE; | |
2134 | } | |
2135 | ||
2136 | /* Read and swap the relocs for a section O. They may have been | |
2137 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2138 | not NULL, they are used as buffers to read into. They are known to | |
2139 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2140 | the return value is allocated using either malloc or bfd_alloc, | |
2141 | according to the KEEP_MEMORY argument. If O has two relocation | |
2142 | sections (both REL and RELA relocations), then the REL_HDR | |
2143 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
2144 | REL_HDR2 relocations. */ | |
2145 | ||
2146 | Elf_Internal_Rela * | |
268b6b39 AM |
2147 | _bfd_elf_link_read_relocs (bfd *abfd, |
2148 | asection *o, | |
2149 | void *external_relocs, | |
2150 | Elf_Internal_Rela *internal_relocs, | |
2151 | bfd_boolean keep_memory) | |
45d6a902 AM |
2152 | { |
2153 | Elf_Internal_Shdr *rel_hdr; | |
268b6b39 | 2154 | void *alloc1 = NULL; |
45d6a902 | 2155 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2156 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
2157 | |
2158 | if (elf_section_data (o)->relocs != NULL) | |
2159 | return elf_section_data (o)->relocs; | |
2160 | ||
2161 | if (o->reloc_count == 0) | |
2162 | return NULL; | |
2163 | ||
2164 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
2165 | ||
2166 | if (internal_relocs == NULL) | |
2167 | { | |
2168 | bfd_size_type size; | |
2169 | ||
2170 | size = o->reloc_count; | |
2171 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
2172 | if (keep_memory) | |
268b6b39 | 2173 | internal_relocs = bfd_alloc (abfd, size); |
45d6a902 | 2174 | else |
268b6b39 | 2175 | internal_relocs = alloc2 = bfd_malloc (size); |
45d6a902 AM |
2176 | if (internal_relocs == NULL) |
2177 | goto error_return; | |
2178 | } | |
2179 | ||
2180 | if (external_relocs == NULL) | |
2181 | { | |
2182 | bfd_size_type size = rel_hdr->sh_size; | |
2183 | ||
2184 | if (elf_section_data (o)->rel_hdr2) | |
2185 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
268b6b39 | 2186 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2187 | if (alloc1 == NULL) |
2188 | goto error_return; | |
2189 | external_relocs = alloc1; | |
2190 | } | |
2191 | ||
243ef1e0 | 2192 | if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, |
45d6a902 AM |
2193 | external_relocs, |
2194 | internal_relocs)) | |
2195 | goto error_return; | |
51992aec AM |
2196 | if (elf_section_data (o)->rel_hdr2 |
2197 | && (!elf_link_read_relocs_from_section | |
2198 | (abfd, o, | |
2199 | elf_section_data (o)->rel_hdr2, | |
2200 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
2201 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
2202 | * bed->s->int_rels_per_ext_rel)))) | |
45d6a902 AM |
2203 | goto error_return; |
2204 | ||
2205 | /* Cache the results for next time, if we can. */ | |
2206 | if (keep_memory) | |
2207 | elf_section_data (o)->relocs = internal_relocs; | |
2208 | ||
2209 | if (alloc1 != NULL) | |
2210 | free (alloc1); | |
2211 | ||
2212 | /* Don't free alloc2, since if it was allocated we are passing it | |
2213 | back (under the name of internal_relocs). */ | |
2214 | ||
2215 | return internal_relocs; | |
2216 | ||
2217 | error_return: | |
2218 | if (alloc1 != NULL) | |
2219 | free (alloc1); | |
2220 | if (alloc2 != NULL) | |
2221 | free (alloc2); | |
2222 | return NULL; | |
2223 | } | |
2224 | ||
2225 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2226 | section header for a section containing relocations for O. */ | |
2227 | ||
2228 | bfd_boolean | |
268b6b39 AM |
2229 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2230 | Elf_Internal_Shdr *rel_hdr, | |
2231 | asection *o) | |
45d6a902 AM |
2232 | { |
2233 | bfd_size_type reloc_count; | |
2234 | bfd_size_type num_rel_hashes; | |
2235 | ||
2236 | /* Figure out how many relocations there will be. */ | |
2237 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
2238 | reloc_count = elf_section_data (o)->rel_count; | |
2239 | else | |
2240 | reloc_count = elf_section_data (o)->rel_count2; | |
2241 | ||
2242 | num_rel_hashes = o->reloc_count; | |
2243 | if (num_rel_hashes < reloc_count) | |
2244 | num_rel_hashes = reloc_count; | |
2245 | ||
2246 | /* That allows us to calculate the size of the section. */ | |
2247 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
2248 | ||
2249 | /* The contents field must last into write_object_contents, so we | |
2250 | allocate it with bfd_alloc rather than malloc. Also since we | |
2251 | cannot be sure that the contents will actually be filled in, | |
2252 | we zero the allocated space. */ | |
268b6b39 | 2253 | rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2254 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2255 | return FALSE; | |
2256 | ||
2257 | /* We only allocate one set of hash entries, so we only do it the | |
2258 | first time we are called. */ | |
2259 | if (elf_section_data (o)->rel_hashes == NULL | |
2260 | && num_rel_hashes) | |
2261 | { | |
2262 | struct elf_link_hash_entry **p; | |
2263 | ||
268b6b39 | 2264 | p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
2265 | if (p == NULL) |
2266 | return FALSE; | |
2267 | ||
2268 | elf_section_data (o)->rel_hashes = p; | |
2269 | } | |
2270 | ||
2271 | return TRUE; | |
2272 | } | |
2273 | ||
2274 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2275 | originated from the section given by INPUT_REL_HDR) to the | |
2276 | OUTPUT_BFD. */ | |
2277 | ||
2278 | bfd_boolean | |
268b6b39 AM |
2279 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2280 | asection *input_section, | |
2281 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2282 | Elf_Internal_Rela *internal_relocs, |
2283 | struct elf_link_hash_entry **rel_hash | |
2284 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2285 | { |
2286 | Elf_Internal_Rela *irela; | |
2287 | Elf_Internal_Rela *irelaend; | |
2288 | bfd_byte *erel; | |
2289 | Elf_Internal_Shdr *output_rel_hdr; | |
2290 | asection *output_section; | |
2291 | unsigned int *rel_countp = NULL; | |
9c5bfbb7 | 2292 | const struct elf_backend_data *bed; |
268b6b39 | 2293 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
45d6a902 AM |
2294 | |
2295 | output_section = input_section->output_section; | |
2296 | output_rel_hdr = NULL; | |
2297 | ||
2298 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
2299 | == input_rel_hdr->sh_entsize) | |
2300 | { | |
2301 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
2302 | rel_countp = &elf_section_data (output_section)->rel_count; | |
2303 | } | |
2304 | else if (elf_section_data (output_section)->rel_hdr2 | |
2305 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
2306 | == input_rel_hdr->sh_entsize)) | |
2307 | { | |
2308 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2309 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2310 | } | |
2311 | else | |
2312 | { | |
2313 | (*_bfd_error_handler) | |
d003868e AM |
2314 | (_("%B: relocation size mismatch in %B section %A"), |
2315 | output_bfd, input_section->owner, input_section); | |
45d6a902 AM |
2316 | bfd_set_error (bfd_error_wrong_object_format); |
2317 | return FALSE; | |
2318 | } | |
2319 | ||
2320 | bed = get_elf_backend_data (output_bfd); | |
2321 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2322 | swap_out = bed->s->swap_reloc_out; | |
2323 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2324 | swap_out = bed->s->swap_reloca_out; | |
2325 | else | |
2326 | abort (); | |
2327 | ||
2328 | erel = output_rel_hdr->contents; | |
2329 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2330 | irela = internal_relocs; | |
2331 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2332 | * bed->s->int_rels_per_ext_rel); | |
2333 | while (irela < irelaend) | |
2334 | { | |
2335 | (*swap_out) (output_bfd, irela, erel); | |
2336 | irela += bed->s->int_rels_per_ext_rel; | |
2337 | erel += input_rel_hdr->sh_entsize; | |
2338 | } | |
2339 | ||
2340 | /* Bump the counter, so that we know where to add the next set of | |
2341 | relocations. */ | |
2342 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2343 | ||
2344 | return TRUE; | |
2345 | } | |
2346 | \f | |
508c3946 L |
2347 | /* Make weak undefined symbols in PIE dynamic. */ |
2348 | ||
2349 | bfd_boolean | |
2350 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2351 | struct elf_link_hash_entry *h) | |
2352 | { | |
2353 | if (info->pie | |
2354 | && h->dynindx == -1 | |
2355 | && h->root.type == bfd_link_hash_undefweak) | |
2356 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2357 | ||
2358 | return TRUE; | |
2359 | } | |
2360 | ||
45d6a902 AM |
2361 | /* Fix up the flags for a symbol. This handles various cases which |
2362 | can only be fixed after all the input files are seen. This is | |
2363 | currently called by both adjust_dynamic_symbol and | |
2364 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2365 | the face of future changes. */ | |
2366 | ||
2367 | bfd_boolean | |
268b6b39 AM |
2368 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2369 | struct elf_info_failed *eif) | |
45d6a902 | 2370 | { |
508c3946 L |
2371 | const struct elf_backend_data *bed = NULL; |
2372 | ||
45d6a902 AM |
2373 | /* If this symbol was mentioned in a non-ELF file, try to set |
2374 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2375 | permit a non-ELF file to correctly refer to a symbol defined in | |
2376 | an ELF dynamic object. */ | |
f5385ebf | 2377 | if (h->non_elf) |
45d6a902 AM |
2378 | { |
2379 | while (h->root.type == bfd_link_hash_indirect) | |
2380 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2381 | ||
2382 | if (h->root.type != bfd_link_hash_defined | |
2383 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2384 | { |
2385 | h->ref_regular = 1; | |
2386 | h->ref_regular_nonweak = 1; | |
2387 | } | |
45d6a902 AM |
2388 | else |
2389 | { | |
2390 | if (h->root.u.def.section->owner != NULL | |
2391 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2392 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2393 | { |
2394 | h->ref_regular = 1; | |
2395 | h->ref_regular_nonweak = 1; | |
2396 | } | |
45d6a902 | 2397 | else |
f5385ebf | 2398 | h->def_regular = 1; |
45d6a902 AM |
2399 | } |
2400 | ||
2401 | if (h->dynindx == -1 | |
f5385ebf AM |
2402 | && (h->def_dynamic |
2403 | || h->ref_dynamic)) | |
45d6a902 | 2404 | { |
c152c796 | 2405 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2406 | { |
2407 | eif->failed = TRUE; | |
2408 | return FALSE; | |
2409 | } | |
2410 | } | |
2411 | } | |
2412 | else | |
2413 | { | |
f5385ebf | 2414 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2415 | was first seen in a non-ELF file. Fortunately, if the symbol |
2416 | was first seen in an ELF file, we're probably OK unless the | |
2417 | symbol was defined in a non-ELF file. Catch that case here. | |
2418 | FIXME: We're still in trouble if the symbol was first seen in | |
2419 | a dynamic object, and then later in a non-ELF regular object. */ | |
2420 | if ((h->root.type == bfd_link_hash_defined | |
2421 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2422 | && !h->def_regular |
45d6a902 AM |
2423 | && (h->root.u.def.section->owner != NULL |
2424 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2425 | != bfd_target_elf_flavour) | |
2426 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2427 | && !h->def_dynamic))) |
2428 | h->def_regular = 1; | |
45d6a902 AM |
2429 | } |
2430 | ||
508c3946 L |
2431 | /* Backend specific symbol fixup. */ |
2432 | if (elf_hash_table (eif->info)->dynobj) | |
2433 | { | |
2434 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2435 | if (bed->elf_backend_fixup_symbol | |
2436 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2437 | return FALSE; | |
2438 | } | |
2439 | ||
45d6a902 AM |
2440 | /* If this is a final link, and the symbol was defined as a common |
2441 | symbol in a regular object file, and there was no definition in | |
2442 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2443 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2444 | flag will not have been set. */ |
2445 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2446 | && !h->def_regular |
2447 | && h->ref_regular | |
2448 | && !h->def_dynamic | |
45d6a902 | 2449 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) |
f5385ebf | 2450 | h->def_regular = 1; |
45d6a902 AM |
2451 | |
2452 | /* If -Bsymbolic was used (which means to bind references to global | |
2453 | symbols to the definition within the shared object), and this | |
2454 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2455 | need a PLT entry. Likewise, if the symbol has non-default |
2456 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2457 | will force it local. */ |
f5385ebf | 2458 | if (h->needs_plt |
45d6a902 | 2459 | && eif->info->shared |
0eddce27 | 2460 | && is_elf_hash_table (eif->info->hash) |
55255dae | 2461 | && (SYMBOLIC_BIND (eif->info, h) |
c1be741f | 2462 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2463 | && h->def_regular) |
45d6a902 | 2464 | { |
45d6a902 AM |
2465 | bfd_boolean force_local; |
2466 | ||
45d6a902 AM |
2467 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2468 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2469 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2470 | } | |
2471 | ||
2472 | /* If a weak undefined symbol has non-default visibility, we also | |
2473 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2474 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
2475 | && h->root.type == bfd_link_hash_undefweak) |
2476 | { | |
9c5bfbb7 | 2477 | const struct elf_backend_data *bed; |
45d6a902 AM |
2478 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2479 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2480 | } | |
2481 | ||
2482 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2483 | the real definition in the dynamic object, copy interesting flags | |
2484 | over to the real definition. */ | |
f6e332e6 | 2485 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2486 | { |
2487 | struct elf_link_hash_entry *weakdef; | |
2488 | ||
f6e332e6 | 2489 | weakdef = h->u.weakdef; |
45d6a902 AM |
2490 | if (h->root.type == bfd_link_hash_indirect) |
2491 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2492 | ||
2493 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2494 | || h->root.type == bfd_link_hash_defweak); | |
2495 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2496 | || weakdef->root.type == bfd_link_hash_defweak); | |
f5385ebf | 2497 | BFD_ASSERT (weakdef->def_dynamic); |
45d6a902 AM |
2498 | |
2499 | /* If the real definition is defined by a regular object file, | |
2500 | don't do anything special. See the longer description in | |
2501 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
f5385ebf | 2502 | if (weakdef->def_regular) |
f6e332e6 | 2503 | h->u.weakdef = NULL; |
45d6a902 | 2504 | else |
508c3946 L |
2505 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, |
2506 | h); | |
45d6a902 AM |
2507 | } |
2508 | ||
2509 | return TRUE; | |
2510 | } | |
2511 | ||
2512 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2513 | called via elf_link_hash_traverse, and also calls itself | |
2514 | recursively. */ | |
2515 | ||
2516 | bfd_boolean | |
268b6b39 | 2517 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2518 | { |
268b6b39 | 2519 | struct elf_info_failed *eif = data; |
45d6a902 | 2520 | bfd *dynobj; |
9c5bfbb7 | 2521 | const struct elf_backend_data *bed; |
45d6a902 | 2522 | |
0eddce27 | 2523 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2524 | return FALSE; |
2525 | ||
2526 | if (h->root.type == bfd_link_hash_warning) | |
2527 | { | |
a6aa5195 AM |
2528 | h->got = elf_hash_table (eif->info)->init_got_offset; |
2529 | h->plt = elf_hash_table (eif->info)->init_plt_offset; | |
45d6a902 AM |
2530 | |
2531 | /* When warning symbols are created, they **replace** the "real" | |
2532 | entry in the hash table, thus we never get to see the real | |
2533 | symbol in a hash traversal. So look at it now. */ | |
2534 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2535 | } | |
2536 | ||
2537 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2538 | if (h->root.type == bfd_link_hash_indirect) | |
2539 | return TRUE; | |
2540 | ||
2541 | /* Fix the symbol flags. */ | |
2542 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2543 | return FALSE; | |
2544 | ||
2545 | /* If this symbol does not require a PLT entry, and it is not | |
2546 | defined by a dynamic object, or is not referenced by a regular | |
2547 | object, ignore it. We do have to handle a weak defined symbol, | |
2548 | even if no regular object refers to it, if we decided to add it | |
2549 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2550 | about symbols which are defined by one dynamic object and | |
2551 | referenced by another one? */ | |
f5385ebf AM |
2552 | if (!h->needs_plt |
2553 | && (h->def_regular | |
2554 | || !h->def_dynamic | |
2555 | || (!h->ref_regular | |
f6e332e6 | 2556 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 | 2557 | { |
a6aa5195 | 2558 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2559 | return TRUE; |
2560 | } | |
2561 | ||
2562 | /* If we've already adjusted this symbol, don't do it again. This | |
2563 | can happen via a recursive call. */ | |
f5385ebf | 2564 | if (h->dynamic_adjusted) |
45d6a902 AM |
2565 | return TRUE; |
2566 | ||
2567 | /* Don't look at this symbol again. Note that we must set this | |
2568 | after checking the above conditions, because we may look at a | |
2569 | symbol once, decide not to do anything, and then get called | |
2570 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2571 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2572 | |
2573 | /* If this is a weak definition, and we know a real definition, and | |
2574 | the real symbol is not itself defined by a regular object file, | |
2575 | then get a good value for the real definition. We handle the | |
2576 | real symbol first, for the convenience of the backend routine. | |
2577 | ||
2578 | Note that there is a confusing case here. If the real definition | |
2579 | is defined by a regular object file, we don't get the real symbol | |
2580 | from the dynamic object, but we do get the weak symbol. If the | |
2581 | processor backend uses a COPY reloc, then if some routine in the | |
2582 | dynamic object changes the real symbol, we will not see that | |
2583 | change in the corresponding weak symbol. This is the way other | |
2584 | ELF linkers work as well, and seems to be a result of the shared | |
2585 | library model. | |
2586 | ||
2587 | I will clarify this issue. Most SVR4 shared libraries define the | |
2588 | variable _timezone and define timezone as a weak synonym. The | |
2589 | tzset call changes _timezone. If you write | |
2590 | extern int timezone; | |
2591 | int _timezone = 5; | |
2592 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2593 | you might expect that, since timezone is a synonym for _timezone, | |
2594 | the same number will print both times. However, if the processor | |
2595 | backend uses a COPY reloc, then actually timezone will be copied | |
2596 | into your process image, and, since you define _timezone | |
2597 | yourself, _timezone will not. Thus timezone and _timezone will | |
2598 | wind up at different memory locations. The tzset call will set | |
2599 | _timezone, leaving timezone unchanged. */ | |
2600 | ||
f6e332e6 | 2601 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2602 | { |
2603 | /* If we get to this point, we know there is an implicit | |
2604 | reference by a regular object file via the weak symbol H. | |
2605 | FIXME: Is this really true? What if the traversal finds | |
f6e332e6 AM |
2606 | H->U.WEAKDEF before it finds H? */ |
2607 | h->u.weakdef->ref_regular = 1; | |
45d6a902 | 2608 | |
f6e332e6 | 2609 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2610 | return FALSE; |
2611 | } | |
2612 | ||
2613 | /* If a symbol has no type and no size and does not require a PLT | |
2614 | entry, then we are probably about to do the wrong thing here: we | |
2615 | are probably going to create a COPY reloc for an empty object. | |
2616 | This case can arise when a shared object is built with assembly | |
2617 | code, and the assembly code fails to set the symbol type. */ | |
2618 | if (h->size == 0 | |
2619 | && h->type == STT_NOTYPE | |
f5385ebf | 2620 | && !h->needs_plt) |
45d6a902 AM |
2621 | (*_bfd_error_handler) |
2622 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2623 | h->root.root.string); | |
2624 | ||
2625 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2626 | bed = get_elf_backend_data (dynobj); | |
2627 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2628 | { | |
2629 | eif->failed = TRUE; | |
2630 | return FALSE; | |
2631 | } | |
2632 | ||
2633 | return TRUE; | |
2634 | } | |
2635 | ||
027297b7 L |
2636 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
2637 | DYNBSS. */ | |
2638 | ||
2639 | bfd_boolean | |
2640 | _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h, | |
2641 | asection *dynbss) | |
2642 | { | |
91ac5911 | 2643 | unsigned int power_of_two; |
027297b7 L |
2644 | bfd_vma mask; |
2645 | asection *sec = h->root.u.def.section; | |
2646 | ||
2647 | /* The section aligment of definition is the maximum alignment | |
91ac5911 L |
2648 | requirement of symbols defined in the section. Since we don't |
2649 | know the symbol alignment requirement, we start with the | |
2650 | maximum alignment and check low bits of the symbol address | |
2651 | for the minimum alignment. */ | |
2652 | power_of_two = bfd_get_section_alignment (sec->owner, sec); | |
2653 | mask = ((bfd_vma) 1 << power_of_two) - 1; | |
2654 | while ((h->root.u.def.value & mask) != 0) | |
2655 | { | |
2656 | mask >>= 1; | |
2657 | --power_of_two; | |
2658 | } | |
027297b7 | 2659 | |
91ac5911 L |
2660 | if (power_of_two > bfd_get_section_alignment (dynbss->owner, |
2661 | dynbss)) | |
027297b7 L |
2662 | { |
2663 | /* Adjust the section alignment if needed. */ | |
2664 | if (! bfd_set_section_alignment (dynbss->owner, dynbss, | |
91ac5911 | 2665 | power_of_two)) |
027297b7 L |
2666 | return FALSE; |
2667 | } | |
2668 | ||
91ac5911 | 2669 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
2670 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
2671 | ||
2672 | /* Define the symbol as being at this point in DYNBSS. */ | |
2673 | h->root.u.def.section = dynbss; | |
2674 | h->root.u.def.value = dynbss->size; | |
2675 | ||
2676 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
2677 | dynbss->size += h->size; | |
2678 | ||
2679 | return TRUE; | |
2680 | } | |
2681 | ||
45d6a902 AM |
2682 | /* Adjust all external symbols pointing into SEC_MERGE sections |
2683 | to reflect the object merging within the sections. */ | |
2684 | ||
2685 | bfd_boolean | |
268b6b39 | 2686 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2687 | { |
2688 | asection *sec; | |
2689 | ||
2690 | if (h->root.type == bfd_link_hash_warning) | |
2691 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2692 | ||
2693 | if ((h->root.type == bfd_link_hash_defined | |
2694 | || h->root.type == bfd_link_hash_defweak) | |
2695 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2696 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2697 | { | |
268b6b39 | 2698 | bfd *output_bfd = data; |
45d6a902 AM |
2699 | |
2700 | h->root.u.def.value = | |
2701 | _bfd_merged_section_offset (output_bfd, | |
2702 | &h->root.u.def.section, | |
2703 | elf_section_data (sec)->sec_info, | |
753731ee | 2704 | h->root.u.def.value); |
45d6a902 AM |
2705 | } |
2706 | ||
2707 | return TRUE; | |
2708 | } | |
986a241f RH |
2709 | |
2710 | /* Returns false if the symbol referred to by H should be considered | |
2711 | to resolve local to the current module, and true if it should be | |
2712 | considered to bind dynamically. */ | |
2713 | ||
2714 | bfd_boolean | |
268b6b39 AM |
2715 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2716 | struct bfd_link_info *info, | |
2717 | bfd_boolean ignore_protected) | |
986a241f RH |
2718 | { |
2719 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
2720 | const struct elf_backend_data *bed; |
2721 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
2722 | |
2723 | if (h == NULL) | |
2724 | return FALSE; | |
2725 | ||
2726 | while (h->root.type == bfd_link_hash_indirect | |
2727 | || h->root.type == bfd_link_hash_warning) | |
2728 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2729 | ||
2730 | /* If it was forced local, then clearly it's not dynamic. */ | |
2731 | if (h->dynindx == -1) | |
2732 | return FALSE; | |
f5385ebf | 2733 | if (h->forced_local) |
986a241f RH |
2734 | return FALSE; |
2735 | ||
2736 | /* Identify the cases where name binding rules say that a | |
2737 | visible symbol resolves locally. */ | |
55255dae | 2738 | binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h); |
986a241f RH |
2739 | |
2740 | switch (ELF_ST_VISIBILITY (h->other)) | |
2741 | { | |
2742 | case STV_INTERNAL: | |
2743 | case STV_HIDDEN: | |
2744 | return FALSE; | |
2745 | ||
2746 | case STV_PROTECTED: | |
fcb93ecf PB |
2747 | hash_table = elf_hash_table (info); |
2748 | if (!is_elf_hash_table (hash_table)) | |
2749 | return FALSE; | |
2750 | ||
2751 | bed = get_elf_backend_data (hash_table->dynobj); | |
2752 | ||
986a241f RH |
2753 | /* Proper resolution for function pointer equality may require |
2754 | that these symbols perhaps be resolved dynamically, even though | |
2755 | we should be resolving them to the current module. */ | |
fcb93ecf | 2756 | if (!ignore_protected || !bed->is_function_type (h->type)) |
986a241f RH |
2757 | binding_stays_local_p = TRUE; |
2758 | break; | |
2759 | ||
2760 | default: | |
986a241f RH |
2761 | break; |
2762 | } | |
2763 | ||
aa37626c | 2764 | /* If it isn't defined locally, then clearly it's dynamic. */ |
f5385ebf | 2765 | if (!h->def_regular) |
aa37626c L |
2766 | return TRUE; |
2767 | ||
986a241f RH |
2768 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2769 | us that it remains local. */ | |
2770 | return !binding_stays_local_p; | |
2771 | } | |
f6c52c13 AM |
2772 | |
2773 | /* Return true if the symbol referred to by H should be considered | |
2774 | to resolve local to the current module, and false otherwise. Differs | |
2775 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2776 | undefined symbols and weak symbols. */ | |
2777 | ||
2778 | bfd_boolean | |
268b6b39 AM |
2779 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2780 | struct bfd_link_info *info, | |
2781 | bfd_boolean local_protected) | |
f6c52c13 | 2782 | { |
fcb93ecf PB |
2783 | const struct elf_backend_data *bed; |
2784 | struct elf_link_hash_table *hash_table; | |
2785 | ||
f6c52c13 AM |
2786 | /* If it's a local sym, of course we resolve locally. */ |
2787 | if (h == NULL) | |
2788 | return TRUE; | |
2789 | ||
7e2294f9 AO |
2790 | /* Common symbols that become definitions don't get the DEF_REGULAR |
2791 | flag set, so test it first, and don't bail out. */ | |
2792 | if (ELF_COMMON_DEF_P (h)) | |
2793 | /* Do nothing. */; | |
f6c52c13 | 2794 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
2795 | resolve locally. The sym is either undefined or dynamic. */ |
2796 | else if (!h->def_regular) | |
f6c52c13 AM |
2797 | return FALSE; |
2798 | ||
2799 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 2800 | if (h->forced_local) |
f6c52c13 AM |
2801 | return TRUE; |
2802 | ||
2803 | /* As do non-dynamic symbols. */ | |
2804 | if (h->dynindx == -1) | |
2805 | return TRUE; | |
2806 | ||
2807 | /* At this point, we know the symbol is defined and dynamic. In an | |
2808 | executable it must resolve locally, likewise when building symbolic | |
2809 | shared libraries. */ | |
55255dae | 2810 | if (info->executable || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
2811 | return TRUE; |
2812 | ||
2813 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2814 | with default visibility might not resolve locally. */ | |
2815 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2816 | return FALSE; | |
2817 | ||
2818 | /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */ | |
2819 | if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED) | |
2820 | return TRUE; | |
2821 | ||
fcb93ecf PB |
2822 | hash_table = elf_hash_table (info); |
2823 | if (!is_elf_hash_table (hash_table)) | |
2824 | return TRUE; | |
2825 | ||
2826 | bed = get_elf_backend_data (hash_table->dynobj); | |
2827 | ||
1c16dfa5 | 2828 | /* STV_PROTECTED non-function symbols are local. */ |
fcb93ecf | 2829 | if (!bed->is_function_type (h->type)) |
1c16dfa5 L |
2830 | return TRUE; |
2831 | ||
f6c52c13 AM |
2832 | /* Function pointer equality tests may require that STV_PROTECTED |
2833 | symbols be treated as dynamic symbols, even when we know that the | |
2834 | dynamic linker will resolve them locally. */ | |
2835 | return local_protected; | |
2836 | } | |
e1918d23 AM |
2837 | |
2838 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2839 | aligned. Returns the first TLS output section. */ | |
2840 | ||
2841 | struct bfd_section * | |
2842 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2843 | { | |
2844 | struct bfd_section *sec, *tls; | |
2845 | unsigned int align = 0; | |
2846 | ||
2847 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2848 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2849 | break; | |
2850 | tls = sec; | |
2851 | ||
2852 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2853 | if (sec->alignment_power > align) | |
2854 | align = sec->alignment_power; | |
2855 | ||
2856 | elf_hash_table (info)->tls_sec = tls; | |
2857 | ||
2858 | /* Ensure the alignment of the first section is the largest alignment, | |
2859 | so that the tls segment starts aligned. */ | |
2860 | if (tls != NULL) | |
2861 | tls->alignment_power = align; | |
2862 | ||
2863 | return tls; | |
2864 | } | |
0ad989f9 L |
2865 | |
2866 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2867 | static bfd_boolean | |
2868 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2869 | Elf_Internal_Sym *sym) | |
2870 | { | |
a4d8e49b L |
2871 | const struct elf_backend_data *bed; |
2872 | ||
0ad989f9 L |
2873 | /* Local symbols do not count, but target specific ones might. */ |
2874 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2875 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2876 | return FALSE; | |
2877 | ||
fcb93ecf | 2878 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 2879 | /* Function symbols do not count. */ |
fcb93ecf | 2880 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
2881 | return FALSE; |
2882 | ||
2883 | /* If the section is undefined, then so is the symbol. */ | |
2884 | if (sym->st_shndx == SHN_UNDEF) | |
2885 | return FALSE; | |
2886 | ||
2887 | /* If the symbol is defined in the common section, then | |
2888 | it is a common definition and so does not count. */ | |
a4d8e49b | 2889 | if (bed->common_definition (sym)) |
0ad989f9 L |
2890 | return FALSE; |
2891 | ||
2892 | /* If the symbol is in a target specific section then we | |
2893 | must rely upon the backend to tell us what it is. */ | |
2894 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2895 | /* FIXME - this function is not coded yet: | |
2896 | ||
2897 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2898 | ||
2899 | Instead for now assume that the definition is not global, | |
2900 | Even if this is wrong, at least the linker will behave | |
2901 | in the same way that it used to do. */ | |
2902 | return FALSE; | |
2903 | ||
2904 | return TRUE; | |
2905 | } | |
2906 | ||
2907 | /* Search the symbol table of the archive element of the archive ABFD | |
2908 | whose archive map contains a mention of SYMDEF, and determine if | |
2909 | the symbol is defined in this element. */ | |
2910 | static bfd_boolean | |
2911 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2912 | { | |
2913 | Elf_Internal_Shdr * hdr; | |
2914 | bfd_size_type symcount; | |
2915 | bfd_size_type extsymcount; | |
2916 | bfd_size_type extsymoff; | |
2917 | Elf_Internal_Sym *isymbuf; | |
2918 | Elf_Internal_Sym *isym; | |
2919 | Elf_Internal_Sym *isymend; | |
2920 | bfd_boolean result; | |
2921 | ||
2922 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2923 | if (abfd == NULL) | |
2924 | return FALSE; | |
2925 | ||
2926 | if (! bfd_check_format (abfd, bfd_object)) | |
2927 | return FALSE; | |
2928 | ||
2929 | /* If we have already included the element containing this symbol in the | |
2930 | link then we do not need to include it again. Just claim that any symbol | |
2931 | it contains is not a definition, so that our caller will not decide to | |
2932 | (re)include this element. */ | |
2933 | if (abfd->archive_pass) | |
2934 | return FALSE; | |
2935 | ||
2936 | /* Select the appropriate symbol table. */ | |
2937 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2938 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2939 | else | |
2940 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2941 | ||
2942 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2943 | ||
2944 | /* The sh_info field of the symtab header tells us where the | |
2945 | external symbols start. We don't care about the local symbols. */ | |
2946 | if (elf_bad_symtab (abfd)) | |
2947 | { | |
2948 | extsymcount = symcount; | |
2949 | extsymoff = 0; | |
2950 | } | |
2951 | else | |
2952 | { | |
2953 | extsymcount = symcount - hdr->sh_info; | |
2954 | extsymoff = hdr->sh_info; | |
2955 | } | |
2956 | ||
2957 | if (extsymcount == 0) | |
2958 | return FALSE; | |
2959 | ||
2960 | /* Read in the symbol table. */ | |
2961 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2962 | NULL, NULL, NULL); | |
2963 | if (isymbuf == NULL) | |
2964 | return FALSE; | |
2965 | ||
2966 | /* Scan the symbol table looking for SYMDEF. */ | |
2967 | result = FALSE; | |
2968 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2969 | { | |
2970 | const char *name; | |
2971 | ||
2972 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
2973 | isym->st_name); | |
2974 | if (name == NULL) | |
2975 | break; | |
2976 | ||
2977 | if (strcmp (name, symdef->name) == 0) | |
2978 | { | |
2979 | result = is_global_data_symbol_definition (abfd, isym); | |
2980 | break; | |
2981 | } | |
2982 | } | |
2983 | ||
2984 | free (isymbuf); | |
2985 | ||
2986 | return result; | |
2987 | } | |
2988 | \f | |
5a580b3a AM |
2989 | /* Add an entry to the .dynamic table. */ |
2990 | ||
2991 | bfd_boolean | |
2992 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
2993 | bfd_vma tag, | |
2994 | bfd_vma val) | |
2995 | { | |
2996 | struct elf_link_hash_table *hash_table; | |
2997 | const struct elf_backend_data *bed; | |
2998 | asection *s; | |
2999 | bfd_size_type newsize; | |
3000 | bfd_byte *newcontents; | |
3001 | Elf_Internal_Dyn dyn; | |
3002 | ||
3003 | hash_table = elf_hash_table (info); | |
3004 | if (! is_elf_hash_table (hash_table)) | |
3005 | return FALSE; | |
3006 | ||
3007 | bed = get_elf_backend_data (hash_table->dynobj); | |
3008 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
3009 | BFD_ASSERT (s != NULL); | |
3010 | ||
eea6121a | 3011 | newsize = s->size + bed->s->sizeof_dyn; |
5a580b3a AM |
3012 | newcontents = bfd_realloc (s->contents, newsize); |
3013 | if (newcontents == NULL) | |
3014 | return FALSE; | |
3015 | ||
3016 | dyn.d_tag = tag; | |
3017 | dyn.d_un.d_val = val; | |
eea6121a | 3018 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3019 | |
eea6121a | 3020 | s->size = newsize; |
5a580b3a AM |
3021 | s->contents = newcontents; |
3022 | ||
3023 | return TRUE; | |
3024 | } | |
3025 | ||
3026 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
3027 | otherwise just check whether one already exists. Returns -1 on error, | |
3028 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
3029 | ||
4ad4eba5 | 3030 | static int |
7e9f0867 AM |
3031 | elf_add_dt_needed_tag (bfd *abfd, |
3032 | struct bfd_link_info *info, | |
4ad4eba5 AM |
3033 | const char *soname, |
3034 | bfd_boolean do_it) | |
5a580b3a AM |
3035 | { |
3036 | struct elf_link_hash_table *hash_table; | |
3037 | bfd_size_type oldsize; | |
3038 | bfd_size_type strindex; | |
3039 | ||
7e9f0867 AM |
3040 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3041 | return -1; | |
3042 | ||
5a580b3a AM |
3043 | hash_table = elf_hash_table (info); |
3044 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
3045 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
3046 | if (strindex == (bfd_size_type) -1) | |
3047 | return -1; | |
3048 | ||
3049 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
3050 | { | |
3051 | asection *sdyn; | |
3052 | const struct elf_backend_data *bed; | |
3053 | bfd_byte *extdyn; | |
3054 | ||
3055 | bed = get_elf_backend_data (hash_table->dynobj); | |
3056 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
7e9f0867 AM |
3057 | if (sdyn != NULL) |
3058 | for (extdyn = sdyn->contents; | |
3059 | extdyn < sdyn->contents + sdyn->size; | |
3060 | extdyn += bed->s->sizeof_dyn) | |
3061 | { | |
3062 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3063 | |
7e9f0867 AM |
3064 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3065 | if (dyn.d_tag == DT_NEEDED | |
3066 | && dyn.d_un.d_val == strindex) | |
3067 | { | |
3068 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3069 | return 1; | |
3070 | } | |
3071 | } | |
5a580b3a AM |
3072 | } |
3073 | ||
3074 | if (do_it) | |
3075 | { | |
7e9f0867 AM |
3076 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3077 | return -1; | |
3078 | ||
5a580b3a AM |
3079 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3080 | return -1; | |
3081 | } | |
3082 | else | |
3083 | /* We were just checking for existence of the tag. */ | |
3084 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3085 | ||
3086 | return 0; | |
3087 | } | |
3088 | ||
3089 | /* Sort symbol by value and section. */ | |
4ad4eba5 AM |
3090 | static int |
3091 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3092 | { |
3093 | const struct elf_link_hash_entry *h1; | |
3094 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3095 | bfd_signed_vma vdiff; |
5a580b3a AM |
3096 | |
3097 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3098 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3099 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3100 | if (vdiff != 0) | |
3101 | return vdiff > 0 ? 1 : -1; | |
3102 | else | |
3103 | { | |
3104 | long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
3105 | if (sdiff != 0) | |
3106 | return sdiff > 0 ? 1 : -1; | |
3107 | } | |
5a580b3a AM |
3108 | return 0; |
3109 | } | |
4ad4eba5 | 3110 | |
5a580b3a AM |
3111 | /* This function is used to adjust offsets into .dynstr for |
3112 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3113 | ||
3114 | static bfd_boolean | |
3115 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3116 | { | |
3117 | struct elf_strtab_hash *dynstr = data; | |
3118 | ||
3119 | if (h->root.type == bfd_link_hash_warning) | |
3120 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3121 | ||
3122 | if (h->dynindx != -1) | |
3123 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3124 | return TRUE; | |
3125 | } | |
3126 | ||
3127 | /* Assign string offsets in .dynstr, update all structures referencing | |
3128 | them. */ | |
3129 | ||
4ad4eba5 AM |
3130 | static bfd_boolean |
3131 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3132 | { |
3133 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3134 | struct elf_link_local_dynamic_entry *entry; | |
3135 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3136 | bfd *dynobj = hash_table->dynobj; | |
3137 | asection *sdyn; | |
3138 | bfd_size_type size; | |
3139 | const struct elf_backend_data *bed; | |
3140 | bfd_byte *extdyn; | |
3141 | ||
3142 | _bfd_elf_strtab_finalize (dynstr); | |
3143 | size = _bfd_elf_strtab_size (dynstr); | |
3144 | ||
3145 | bed = get_elf_backend_data (dynobj); | |
3146 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
3147 | BFD_ASSERT (sdyn != NULL); | |
3148 | ||
3149 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3150 | for (extdyn = sdyn->contents; | |
eea6121a | 3151 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3152 | extdyn += bed->s->sizeof_dyn) |
3153 | { | |
3154 | Elf_Internal_Dyn dyn; | |
3155 | ||
3156 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3157 | switch (dyn.d_tag) | |
3158 | { | |
3159 | case DT_STRSZ: | |
3160 | dyn.d_un.d_val = size; | |
3161 | break; | |
3162 | case DT_NEEDED: | |
3163 | case DT_SONAME: | |
3164 | case DT_RPATH: | |
3165 | case DT_RUNPATH: | |
3166 | case DT_FILTER: | |
3167 | case DT_AUXILIARY: | |
3168 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
3169 | break; | |
3170 | default: | |
3171 | continue; | |
3172 | } | |
3173 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3174 | } | |
3175 | ||
3176 | /* Now update local dynamic symbols. */ | |
3177 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3178 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3179 | entry->isym.st_name); | |
3180 | ||
3181 | /* And the rest of dynamic symbols. */ | |
3182 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3183 | ||
3184 | /* Adjust version definitions. */ | |
3185 | if (elf_tdata (output_bfd)->cverdefs) | |
3186 | { | |
3187 | asection *s; | |
3188 | bfd_byte *p; | |
3189 | bfd_size_type i; | |
3190 | Elf_Internal_Verdef def; | |
3191 | Elf_Internal_Verdaux defaux; | |
3192 | ||
3193 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
3194 | p = s->contents; | |
3195 | do | |
3196 | { | |
3197 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3198 | &def); | |
3199 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3200 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3201 | continue; | |
5a580b3a AM |
3202 | for (i = 0; i < def.vd_cnt; ++i) |
3203 | { | |
3204 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3205 | (Elf_External_Verdaux *) p, &defaux); | |
3206 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3207 | defaux.vda_name); | |
3208 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3209 | &defaux, (Elf_External_Verdaux *) p); | |
3210 | p += sizeof (Elf_External_Verdaux); | |
3211 | } | |
3212 | } | |
3213 | while (def.vd_next); | |
3214 | } | |
3215 | ||
3216 | /* Adjust version references. */ | |
3217 | if (elf_tdata (output_bfd)->verref) | |
3218 | { | |
3219 | asection *s; | |
3220 | bfd_byte *p; | |
3221 | bfd_size_type i; | |
3222 | Elf_Internal_Verneed need; | |
3223 | Elf_Internal_Vernaux needaux; | |
3224 | ||
3225 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
3226 | p = s->contents; | |
3227 | do | |
3228 | { | |
3229 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3230 | &need); | |
3231 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3232 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3233 | (Elf_External_Verneed *) p); | |
3234 | p += sizeof (Elf_External_Verneed); | |
3235 | for (i = 0; i < need.vn_cnt; ++i) | |
3236 | { | |
3237 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3238 | (Elf_External_Vernaux *) p, &needaux); | |
3239 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3240 | needaux.vna_name); | |
3241 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3242 | &needaux, | |
3243 | (Elf_External_Vernaux *) p); | |
3244 | p += sizeof (Elf_External_Vernaux); | |
3245 | } | |
3246 | } | |
3247 | while (need.vn_next); | |
3248 | } | |
3249 | ||
3250 | return TRUE; | |
3251 | } | |
3252 | \f | |
4ad4eba5 AM |
3253 | /* Add symbols from an ELF object file to the linker hash table. */ |
3254 | ||
3255 | static bfd_boolean | |
3256 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3257 | { | |
4ad4eba5 AM |
3258 | Elf_Internal_Shdr *hdr; |
3259 | bfd_size_type symcount; | |
3260 | bfd_size_type extsymcount; | |
3261 | bfd_size_type extsymoff; | |
3262 | struct elf_link_hash_entry **sym_hash; | |
3263 | bfd_boolean dynamic; | |
3264 | Elf_External_Versym *extversym = NULL; | |
3265 | Elf_External_Versym *ever; | |
3266 | struct elf_link_hash_entry *weaks; | |
3267 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
3268 | bfd_size_type nondeflt_vers_cnt = 0; | |
3269 | Elf_Internal_Sym *isymbuf = NULL; | |
3270 | Elf_Internal_Sym *isym; | |
3271 | Elf_Internal_Sym *isymend; | |
3272 | const struct elf_backend_data *bed; | |
3273 | bfd_boolean add_needed; | |
66eb6687 | 3274 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3275 | bfd_size_type amt; |
66eb6687 | 3276 | void *alloc_mark = NULL; |
4f87808c AM |
3277 | struct bfd_hash_entry **old_table = NULL; |
3278 | unsigned int old_size = 0; | |
3279 | unsigned int old_count = 0; | |
66eb6687 AM |
3280 | void *old_tab = NULL; |
3281 | void *old_hash; | |
3282 | void *old_ent; | |
3283 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3284 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
3285 | long old_dynsymcount = 0; | |
3286 | size_t tabsize = 0; | |
3287 | size_t hashsize = 0; | |
4ad4eba5 | 3288 | |
66eb6687 | 3289 | htab = elf_hash_table (info); |
4ad4eba5 | 3290 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3291 | |
3292 | if ((abfd->flags & DYNAMIC) == 0) | |
3293 | dynamic = FALSE; | |
3294 | else | |
3295 | { | |
3296 | dynamic = TRUE; | |
3297 | ||
3298 | /* You can't use -r against a dynamic object. Also, there's no | |
3299 | hope of using a dynamic object which does not exactly match | |
3300 | the format of the output file. */ | |
3301 | if (info->relocatable | |
66eb6687 AM |
3302 | || !is_elf_hash_table (htab) |
3303 | || htab->root.creator != abfd->xvec) | |
4ad4eba5 | 3304 | { |
9a0789ec NC |
3305 | if (info->relocatable) |
3306 | bfd_set_error (bfd_error_invalid_operation); | |
3307 | else | |
3308 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3309 | goto error_return; |
3310 | } | |
3311 | } | |
3312 | ||
3313 | /* As a GNU extension, any input sections which are named | |
3314 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3315 | symbol. This differs from .gnu.warning sections, which generate | |
3316 | warnings when they are included in an output file. */ | |
3317 | if (info->executable) | |
3318 | { | |
3319 | asection *s; | |
3320 | ||
3321 | for (s = abfd->sections; s != NULL; s = s->next) | |
3322 | { | |
3323 | const char *name; | |
3324 | ||
3325 | name = bfd_get_section_name (abfd, s); | |
0112cd26 | 3326 | if (CONST_STRNEQ (name, ".gnu.warning.")) |
4ad4eba5 AM |
3327 | { |
3328 | char *msg; | |
3329 | bfd_size_type sz; | |
4ad4eba5 AM |
3330 | |
3331 | name += sizeof ".gnu.warning." - 1; | |
3332 | ||
3333 | /* If this is a shared object, then look up the symbol | |
3334 | in the hash table. If it is there, and it is already | |
3335 | been defined, then we will not be using the entry | |
3336 | from this shared object, so we don't need to warn. | |
3337 | FIXME: If we see the definition in a regular object | |
3338 | later on, we will warn, but we shouldn't. The only | |
3339 | fix is to keep track of what warnings we are supposed | |
3340 | to emit, and then handle them all at the end of the | |
3341 | link. */ | |
3342 | if (dynamic) | |
3343 | { | |
3344 | struct elf_link_hash_entry *h; | |
3345 | ||
66eb6687 | 3346 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
4ad4eba5 AM |
3347 | |
3348 | /* FIXME: What about bfd_link_hash_common? */ | |
3349 | if (h != NULL | |
3350 | && (h->root.type == bfd_link_hash_defined | |
3351 | || h->root.type == bfd_link_hash_defweak)) | |
3352 | { | |
3353 | /* We don't want to issue this warning. Clobber | |
3354 | the section size so that the warning does not | |
3355 | get copied into the output file. */ | |
eea6121a | 3356 | s->size = 0; |
4ad4eba5 AM |
3357 | continue; |
3358 | } | |
3359 | } | |
3360 | ||
eea6121a | 3361 | sz = s->size; |
370a0e1b | 3362 | msg = bfd_alloc (abfd, sz + 1); |
4ad4eba5 AM |
3363 | if (msg == NULL) |
3364 | goto error_return; | |
3365 | ||
370a0e1b | 3366 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
4ad4eba5 AM |
3367 | goto error_return; |
3368 | ||
370a0e1b | 3369 | msg[sz] = '\0'; |
4ad4eba5 AM |
3370 | |
3371 | if (! (_bfd_generic_link_add_one_symbol | |
3372 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
66eb6687 | 3373 | FALSE, bed->collect, NULL))) |
4ad4eba5 AM |
3374 | goto error_return; |
3375 | ||
3376 | if (! info->relocatable) | |
3377 | { | |
3378 | /* Clobber the section size so that the warning does | |
3379 | not get copied into the output file. */ | |
eea6121a | 3380 | s->size = 0; |
11d2f718 AM |
3381 | |
3382 | /* Also set SEC_EXCLUDE, so that symbols defined in | |
3383 | the warning section don't get copied to the output. */ | |
3384 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3385 | } |
3386 | } | |
3387 | } | |
3388 | } | |
3389 | ||
3390 | add_needed = TRUE; | |
3391 | if (! dynamic) | |
3392 | { | |
3393 | /* If we are creating a shared library, create all the dynamic | |
3394 | sections immediately. We need to attach them to something, | |
3395 | so we attach them to this BFD, provided it is the right | |
3396 | format. FIXME: If there are no input BFD's of the same | |
3397 | format as the output, we can't make a shared library. */ | |
3398 | if (info->shared | |
66eb6687 AM |
3399 | && is_elf_hash_table (htab) |
3400 | && htab->root.creator == abfd->xvec | |
3401 | && !htab->dynamic_sections_created) | |
4ad4eba5 AM |
3402 | { |
3403 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3404 | goto error_return; | |
3405 | } | |
3406 | } | |
66eb6687 | 3407 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
3408 | goto error_return; |
3409 | else | |
3410 | { | |
3411 | asection *s; | |
3412 | const char *soname = NULL; | |
3413 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; | |
3414 | int ret; | |
3415 | ||
3416 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3417 | ld shouldn't allow it. */ |
4ad4eba5 AM |
3418 | if ((s = abfd->sections) != NULL |
3419 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
92fd189d | 3420 | abort (); |
4ad4eba5 AM |
3421 | |
3422 | /* If this dynamic lib was specified on the command line with | |
3423 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3424 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3425 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3426 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3427 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3428 | all. */ | |
3429 | add_needed = (elf_dyn_lib_class (abfd) | |
3430 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3431 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3432 | |
3433 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3434 | if (s != NULL) | |
3435 | { | |
3436 | bfd_byte *dynbuf; | |
3437 | bfd_byte *extdyn; | |
3438 | int elfsec; | |
3439 | unsigned long shlink; | |
3440 | ||
eea6121a | 3441 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
4ad4eba5 AM |
3442 | goto error_free_dyn; |
3443 | ||
3444 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
3445 | if (elfsec == -1) | |
3446 | goto error_free_dyn; | |
3447 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3448 | ||
3449 | for (extdyn = dynbuf; | |
eea6121a | 3450 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3451 | extdyn += bed->s->sizeof_dyn) |
3452 | { | |
3453 | Elf_Internal_Dyn dyn; | |
3454 | ||
3455 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3456 | if (dyn.d_tag == DT_SONAME) | |
3457 | { | |
3458 | unsigned int tagv = dyn.d_un.d_val; | |
3459 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3460 | if (soname == NULL) | |
3461 | goto error_free_dyn; | |
3462 | } | |
3463 | if (dyn.d_tag == DT_NEEDED) | |
3464 | { | |
3465 | struct bfd_link_needed_list *n, **pn; | |
3466 | char *fnm, *anm; | |
3467 | unsigned int tagv = dyn.d_un.d_val; | |
3468 | ||
3469 | amt = sizeof (struct bfd_link_needed_list); | |
3470 | n = bfd_alloc (abfd, amt); | |
3471 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3472 | if (n == NULL || fnm == NULL) | |
3473 | goto error_free_dyn; | |
3474 | amt = strlen (fnm) + 1; | |
3475 | anm = bfd_alloc (abfd, amt); | |
3476 | if (anm == NULL) | |
3477 | goto error_free_dyn; | |
3478 | memcpy (anm, fnm, amt); | |
3479 | n->name = anm; | |
3480 | n->by = abfd; | |
3481 | n->next = NULL; | |
66eb6687 | 3482 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3483 | ; |
3484 | *pn = n; | |
3485 | } | |
3486 | if (dyn.d_tag == DT_RUNPATH) | |
3487 | { | |
3488 | struct bfd_link_needed_list *n, **pn; | |
3489 | char *fnm, *anm; | |
3490 | unsigned int tagv = dyn.d_un.d_val; | |
3491 | ||
3492 | amt = sizeof (struct bfd_link_needed_list); | |
3493 | n = bfd_alloc (abfd, amt); | |
3494 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3495 | if (n == NULL || fnm == NULL) | |
3496 | goto error_free_dyn; | |
3497 | amt = strlen (fnm) + 1; | |
3498 | anm = bfd_alloc (abfd, amt); | |
3499 | if (anm == NULL) | |
3500 | goto error_free_dyn; | |
3501 | memcpy (anm, fnm, amt); | |
3502 | n->name = anm; | |
3503 | n->by = abfd; | |
3504 | n->next = NULL; | |
3505 | for (pn = & runpath; | |
3506 | *pn != NULL; | |
3507 | pn = &(*pn)->next) | |
3508 | ; | |
3509 | *pn = n; | |
3510 | } | |
3511 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3512 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3513 | { | |
3514 | struct bfd_link_needed_list *n, **pn; | |
3515 | char *fnm, *anm; | |
3516 | unsigned int tagv = dyn.d_un.d_val; | |
3517 | ||
3518 | amt = sizeof (struct bfd_link_needed_list); | |
3519 | n = bfd_alloc (abfd, amt); | |
3520 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3521 | if (n == NULL || fnm == NULL) | |
3522 | goto error_free_dyn; | |
3523 | amt = strlen (fnm) + 1; | |
3524 | anm = bfd_alloc (abfd, amt); | |
3525 | if (anm == NULL) | |
3526 | { | |
3527 | error_free_dyn: | |
3528 | free (dynbuf); | |
3529 | goto error_return; | |
3530 | } | |
3531 | memcpy (anm, fnm, amt); | |
3532 | n->name = anm; | |
3533 | n->by = abfd; | |
3534 | n->next = NULL; | |
3535 | for (pn = & rpath; | |
3536 | *pn != NULL; | |
3537 | pn = &(*pn)->next) | |
3538 | ; | |
3539 | *pn = n; | |
3540 | } | |
3541 | } | |
3542 | ||
3543 | free (dynbuf); | |
3544 | } | |
3545 | ||
3546 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3547 | frees all more recently bfd_alloc'd blocks as well. */ | |
3548 | if (runpath) | |
3549 | rpath = runpath; | |
3550 | ||
3551 | if (rpath) | |
3552 | { | |
3553 | struct bfd_link_needed_list **pn; | |
66eb6687 | 3554 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3555 | ; |
3556 | *pn = rpath; | |
3557 | } | |
3558 | ||
3559 | /* We do not want to include any of the sections in a dynamic | |
3560 | object in the output file. We hack by simply clobbering the | |
3561 | list of sections in the BFD. This could be handled more | |
3562 | cleanly by, say, a new section flag; the existing | |
3563 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3564 | still implies that the section takes up space in the output | |
3565 | file. */ | |
3566 | bfd_section_list_clear (abfd); | |
3567 | ||
4ad4eba5 AM |
3568 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3569 | object. If the object has a DT_SONAME entry, we use it. | |
3570 | Otherwise, if the generic linker stuck something in | |
3571 | elf_dt_name, we use that. Otherwise, we just use the file | |
3572 | name. */ | |
3573 | if (soname == NULL || *soname == '\0') | |
3574 | { | |
3575 | soname = elf_dt_name (abfd); | |
3576 | if (soname == NULL || *soname == '\0') | |
3577 | soname = bfd_get_filename (abfd); | |
3578 | } | |
3579 | ||
3580 | /* Save the SONAME because sometimes the linker emulation code | |
3581 | will need to know it. */ | |
3582 | elf_dt_name (abfd) = soname; | |
3583 | ||
7e9f0867 | 3584 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3585 | if (ret < 0) |
3586 | goto error_return; | |
3587 | ||
3588 | /* If we have already included this dynamic object in the | |
3589 | link, just ignore it. There is no reason to include a | |
3590 | particular dynamic object more than once. */ | |
3591 | if (ret > 0) | |
3592 | return TRUE; | |
3593 | } | |
3594 | ||
3595 | /* If this is a dynamic object, we always link against the .dynsym | |
3596 | symbol table, not the .symtab symbol table. The dynamic linker | |
3597 | will only see the .dynsym symbol table, so there is no reason to | |
3598 | look at .symtab for a dynamic object. */ | |
3599 | ||
3600 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3601 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3602 | else | |
3603 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3604 | ||
3605 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3606 | ||
3607 | /* The sh_info field of the symtab header tells us where the | |
3608 | external symbols start. We don't care about the local symbols at | |
3609 | this point. */ | |
3610 | if (elf_bad_symtab (abfd)) | |
3611 | { | |
3612 | extsymcount = symcount; | |
3613 | extsymoff = 0; | |
3614 | } | |
3615 | else | |
3616 | { | |
3617 | extsymcount = symcount - hdr->sh_info; | |
3618 | extsymoff = hdr->sh_info; | |
3619 | } | |
3620 | ||
3621 | sym_hash = NULL; | |
3622 | if (extsymcount != 0) | |
3623 | { | |
3624 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3625 | NULL, NULL, NULL); | |
3626 | if (isymbuf == NULL) | |
3627 | goto error_return; | |
3628 | ||
3629 | /* We store a pointer to the hash table entry for each external | |
3630 | symbol. */ | |
3631 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3632 | sym_hash = bfd_alloc (abfd, amt); | |
3633 | if (sym_hash == NULL) | |
3634 | goto error_free_sym; | |
3635 | elf_sym_hashes (abfd) = sym_hash; | |
3636 | } | |
3637 | ||
3638 | if (dynamic) | |
3639 | { | |
3640 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
3641 | if (!_bfd_elf_slurp_version_tables (abfd, |
3642 | info->default_imported_symver)) | |
4ad4eba5 AM |
3643 | goto error_free_sym; |
3644 | ||
3645 | /* Read in the symbol versions, but don't bother to convert them | |
3646 | to internal format. */ | |
3647 | if (elf_dynversym (abfd) != 0) | |
3648 | { | |
3649 | Elf_Internal_Shdr *versymhdr; | |
3650 | ||
3651 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
3652 | extversym = bfd_malloc (versymhdr->sh_size); | |
3653 | if (extversym == NULL) | |
3654 | goto error_free_sym; | |
3655 | amt = versymhdr->sh_size; | |
3656 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3657 | || bfd_bread (extversym, amt, abfd) != amt) | |
3658 | goto error_free_vers; | |
3659 | } | |
3660 | } | |
3661 | ||
66eb6687 AM |
3662 | /* If we are loading an as-needed shared lib, save the symbol table |
3663 | state before we start adding symbols. If the lib turns out | |
3664 | to be unneeded, restore the state. */ | |
3665 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
3666 | { | |
3667 | unsigned int i; | |
3668 | size_t entsize; | |
3669 | ||
3670 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
3671 | { | |
3672 | struct bfd_hash_entry *p; | |
2de92251 | 3673 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3674 | |
3675 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
3676 | { |
3677 | h = (struct elf_link_hash_entry *) p; | |
3678 | entsize += htab->root.table.entsize; | |
3679 | if (h->root.type == bfd_link_hash_warning) | |
3680 | entsize += htab->root.table.entsize; | |
3681 | } | |
66eb6687 AM |
3682 | } |
3683 | ||
3684 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
3685 | hashsize = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3686 | old_tab = bfd_malloc (tabsize + entsize + hashsize); | |
3687 | if (old_tab == NULL) | |
3688 | goto error_free_vers; | |
3689 | ||
3690 | /* Remember the current objalloc pointer, so that all mem for | |
3691 | symbols added can later be reclaimed. */ | |
3692 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
3693 | if (alloc_mark == NULL) | |
3694 | goto error_free_vers; | |
3695 | ||
5061a885 AM |
3696 | /* Make a special call to the linker "notice" function to |
3697 | tell it that we are about to handle an as-needed lib. */ | |
3698 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, | |
3699 | notice_as_needed)) | |
9af2a943 | 3700 | goto error_free_vers; |
5061a885 | 3701 | |
66eb6687 AM |
3702 | /* Clone the symbol table and sym hashes. Remember some |
3703 | pointers into the symbol table, and dynamic symbol count. */ | |
3704 | old_hash = (char *) old_tab + tabsize; | |
3705 | old_ent = (char *) old_hash + hashsize; | |
3706 | memcpy (old_tab, htab->root.table.table, tabsize); | |
3707 | memcpy (old_hash, sym_hash, hashsize); | |
3708 | old_undefs = htab->root.undefs; | |
3709 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
3710 | old_table = htab->root.table.table; |
3711 | old_size = htab->root.table.size; | |
3712 | old_count = htab->root.table.count; | |
66eb6687 AM |
3713 | old_dynsymcount = htab->dynsymcount; |
3714 | ||
3715 | for (i = 0; i < htab->root.table.size; i++) | |
3716 | { | |
3717 | struct bfd_hash_entry *p; | |
2de92251 | 3718 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3719 | |
3720 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
3721 | { | |
3722 | memcpy (old_ent, p, htab->root.table.entsize); | |
3723 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
3724 | h = (struct elf_link_hash_entry *) p; |
3725 | if (h->root.type == bfd_link_hash_warning) | |
3726 | { | |
3727 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
3728 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
3729 | } | |
66eb6687 AM |
3730 | } |
3731 | } | |
3732 | } | |
4ad4eba5 | 3733 | |
66eb6687 | 3734 | weaks = NULL; |
4ad4eba5 AM |
3735 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
3736 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3737 | isym < isymend; | |
3738 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3739 | { | |
3740 | int bind; | |
3741 | bfd_vma value; | |
af44c138 | 3742 | asection *sec, *new_sec; |
4ad4eba5 AM |
3743 | flagword flags; |
3744 | const char *name; | |
3745 | struct elf_link_hash_entry *h; | |
3746 | bfd_boolean definition; | |
3747 | bfd_boolean size_change_ok; | |
3748 | bfd_boolean type_change_ok; | |
3749 | bfd_boolean new_weakdef; | |
3750 | bfd_boolean override; | |
a4d8e49b | 3751 | bfd_boolean common; |
4ad4eba5 AM |
3752 | unsigned int old_alignment; |
3753 | bfd *old_bfd; | |
3754 | ||
3755 | override = FALSE; | |
3756 | ||
3757 | flags = BSF_NO_FLAGS; | |
3758 | sec = NULL; | |
3759 | value = isym->st_value; | |
3760 | *sym_hash = NULL; | |
a4d8e49b | 3761 | common = bed->common_definition (isym); |
4ad4eba5 AM |
3762 | |
3763 | bind = ELF_ST_BIND (isym->st_info); | |
3764 | if (bind == STB_LOCAL) | |
3765 | { | |
3766 | /* This should be impossible, since ELF requires that all | |
3767 | global symbols follow all local symbols, and that sh_info | |
3768 | point to the first global symbol. Unfortunately, Irix 5 | |
3769 | screws this up. */ | |
3770 | continue; | |
3771 | } | |
3772 | else if (bind == STB_GLOBAL) | |
3773 | { | |
a4d8e49b | 3774 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 AM |
3775 | flags = BSF_GLOBAL; |
3776 | } | |
3777 | else if (bind == STB_WEAK) | |
3778 | flags = BSF_WEAK; | |
3779 | else | |
3780 | { | |
3781 | /* Leave it up to the processor backend. */ | |
3782 | } | |
3783 | ||
3784 | if (isym->st_shndx == SHN_UNDEF) | |
3785 | sec = bfd_und_section_ptr; | |
66eb6687 AM |
3786 | else if (isym->st_shndx < SHN_LORESERVE |
3787 | || isym->st_shndx > SHN_HIRESERVE) | |
4ad4eba5 AM |
3788 | { |
3789 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3790 | if (sec == NULL) | |
3791 | sec = bfd_abs_section_ptr; | |
529fcb95 PB |
3792 | else if (sec->kept_section) |
3793 | { | |
e5d08002 L |
3794 | /* Symbols from discarded section are undefined. We keep |
3795 | its visibility. */ | |
529fcb95 PB |
3796 | sec = bfd_und_section_ptr; |
3797 | isym->st_shndx = SHN_UNDEF; | |
3798 | } | |
4ad4eba5 AM |
3799 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
3800 | value -= sec->vma; | |
3801 | } | |
3802 | else if (isym->st_shndx == SHN_ABS) | |
3803 | sec = bfd_abs_section_ptr; | |
3804 | else if (isym->st_shndx == SHN_COMMON) | |
3805 | { | |
3806 | sec = bfd_com_section_ptr; | |
3807 | /* What ELF calls the size we call the value. What ELF | |
3808 | calls the value we call the alignment. */ | |
3809 | value = isym->st_size; | |
3810 | } | |
3811 | else | |
3812 | { | |
3813 | /* Leave it up to the processor backend. */ | |
3814 | } | |
3815 | ||
3816 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3817 | isym->st_name); | |
3818 | if (name == NULL) | |
3819 | goto error_free_vers; | |
3820 | ||
3821 | if (isym->st_shndx == SHN_COMMON | |
6a4a0940 JJ |
3822 | && ELF_ST_TYPE (isym->st_info) == STT_TLS |
3823 | && !info->relocatable) | |
4ad4eba5 AM |
3824 | { |
3825 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3826 | ||
3827 | if (tcomm == NULL) | |
3828 | { | |
3496cb2a L |
3829 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", |
3830 | (SEC_ALLOC | |
3831 | | SEC_IS_COMMON | |
3832 | | SEC_LINKER_CREATED | |
3833 | | SEC_THREAD_LOCAL)); | |
3834 | if (tcomm == NULL) | |
4ad4eba5 AM |
3835 | goto error_free_vers; |
3836 | } | |
3837 | sec = tcomm; | |
3838 | } | |
66eb6687 | 3839 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 3840 | { |
66eb6687 AM |
3841 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
3842 | &sec, &value)) | |
4ad4eba5 AM |
3843 | goto error_free_vers; |
3844 | ||
3845 | /* The hook function sets the name to NULL if this symbol | |
3846 | should be skipped for some reason. */ | |
3847 | if (name == NULL) | |
3848 | continue; | |
3849 | } | |
3850 | ||
3851 | /* Sanity check that all possibilities were handled. */ | |
3852 | if (sec == NULL) | |
3853 | { | |
3854 | bfd_set_error (bfd_error_bad_value); | |
3855 | goto error_free_vers; | |
3856 | } | |
3857 | ||
3858 | if (bfd_is_und_section (sec) | |
3859 | || bfd_is_com_section (sec)) | |
3860 | definition = FALSE; | |
3861 | else | |
3862 | definition = TRUE; | |
3863 | ||
3864 | size_change_ok = FALSE; | |
66eb6687 | 3865 | type_change_ok = bed->type_change_ok; |
4ad4eba5 AM |
3866 | old_alignment = 0; |
3867 | old_bfd = NULL; | |
af44c138 | 3868 | new_sec = sec; |
4ad4eba5 | 3869 | |
66eb6687 | 3870 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
3871 | { |
3872 | Elf_Internal_Versym iver; | |
3873 | unsigned int vernum = 0; | |
3874 | bfd_boolean skip; | |
3875 | ||
fc0e6df6 | 3876 | if (ever == NULL) |
4ad4eba5 | 3877 | { |
fc0e6df6 PB |
3878 | if (info->default_imported_symver) |
3879 | /* Use the default symbol version created earlier. */ | |
3880 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
3881 | else | |
3882 | iver.vs_vers = 0; | |
3883 | } | |
3884 | else | |
3885 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
3886 | ||
3887 | vernum = iver.vs_vers & VERSYM_VERSION; | |
3888 | ||
3889 | /* If this is a hidden symbol, or if it is not version | |
3890 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
3891 | However, we do not modify a non-hidden absolute symbol |
3892 | if it is not a function, because it might be the version | |
3893 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 3894 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
3895 | || (vernum > 1 |
3896 | && (!bfd_is_abs_section (sec) | |
3897 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
3898 | { |
3899 | const char *verstr; | |
3900 | size_t namelen, verlen, newlen; | |
3901 | char *newname, *p; | |
3902 | ||
3903 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 3904 | { |
fc0e6df6 PB |
3905 | if (vernum > elf_tdata (abfd)->cverdefs) |
3906 | verstr = NULL; | |
3907 | else if (vernum > 1) | |
3908 | verstr = | |
3909 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
3910 | else | |
3911 | verstr = ""; | |
4ad4eba5 | 3912 | |
fc0e6df6 | 3913 | if (verstr == NULL) |
4ad4eba5 | 3914 | { |
fc0e6df6 PB |
3915 | (*_bfd_error_handler) |
3916 | (_("%B: %s: invalid version %u (max %d)"), | |
3917 | abfd, name, vernum, | |
3918 | elf_tdata (abfd)->cverdefs); | |
3919 | bfd_set_error (bfd_error_bad_value); | |
3920 | goto error_free_vers; | |
4ad4eba5 | 3921 | } |
fc0e6df6 PB |
3922 | } |
3923 | else | |
3924 | { | |
3925 | /* We cannot simply test for the number of | |
3926 | entries in the VERNEED section since the | |
3927 | numbers for the needed versions do not start | |
3928 | at 0. */ | |
3929 | Elf_Internal_Verneed *t; | |
3930 | ||
3931 | verstr = NULL; | |
3932 | for (t = elf_tdata (abfd)->verref; | |
3933 | t != NULL; | |
3934 | t = t->vn_nextref) | |
4ad4eba5 | 3935 | { |
fc0e6df6 | 3936 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 3937 | |
fc0e6df6 PB |
3938 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
3939 | { | |
3940 | if (a->vna_other == vernum) | |
4ad4eba5 | 3941 | { |
fc0e6df6 PB |
3942 | verstr = a->vna_nodename; |
3943 | break; | |
4ad4eba5 | 3944 | } |
4ad4eba5 | 3945 | } |
fc0e6df6 PB |
3946 | if (a != NULL) |
3947 | break; | |
3948 | } | |
3949 | if (verstr == NULL) | |
3950 | { | |
3951 | (*_bfd_error_handler) | |
3952 | (_("%B: %s: invalid needed version %d"), | |
3953 | abfd, name, vernum); | |
3954 | bfd_set_error (bfd_error_bad_value); | |
3955 | goto error_free_vers; | |
4ad4eba5 | 3956 | } |
4ad4eba5 | 3957 | } |
fc0e6df6 PB |
3958 | |
3959 | namelen = strlen (name); | |
3960 | verlen = strlen (verstr); | |
3961 | newlen = namelen + verlen + 2; | |
3962 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3963 | && isym->st_shndx != SHN_UNDEF) | |
3964 | ++newlen; | |
3965 | ||
66eb6687 | 3966 | newname = bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
3967 | if (newname == NULL) |
3968 | goto error_free_vers; | |
3969 | memcpy (newname, name, namelen); | |
3970 | p = newname + namelen; | |
3971 | *p++ = ELF_VER_CHR; | |
3972 | /* If this is a defined non-hidden version symbol, | |
3973 | we add another @ to the name. This indicates the | |
3974 | default version of the symbol. */ | |
3975 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3976 | && isym->st_shndx != SHN_UNDEF) | |
3977 | *p++ = ELF_VER_CHR; | |
3978 | memcpy (p, verstr, verlen + 1); | |
3979 | ||
3980 | name = newname; | |
4ad4eba5 AM |
3981 | } |
3982 | ||
af44c138 L |
3983 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, |
3984 | &value, &old_alignment, | |
4ad4eba5 AM |
3985 | sym_hash, &skip, &override, |
3986 | &type_change_ok, &size_change_ok)) | |
3987 | goto error_free_vers; | |
3988 | ||
3989 | if (skip) | |
3990 | continue; | |
3991 | ||
3992 | if (override) | |
3993 | definition = FALSE; | |
3994 | ||
3995 | h = *sym_hash; | |
3996 | while (h->root.type == bfd_link_hash_indirect | |
3997 | || h->root.type == bfd_link_hash_warning) | |
3998 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3999 | ||
4000 | /* Remember the old alignment if this is a common symbol, so | |
4001 | that we don't reduce the alignment later on. We can't | |
4002 | check later, because _bfd_generic_link_add_one_symbol | |
4003 | will set a default for the alignment which we want to | |
4004 | override. We also remember the old bfd where the existing | |
4005 | definition comes from. */ | |
4006 | switch (h->root.type) | |
4007 | { | |
4008 | default: | |
4009 | break; | |
4010 | ||
4011 | case bfd_link_hash_defined: | |
4012 | case bfd_link_hash_defweak: | |
4013 | old_bfd = h->root.u.def.section->owner; | |
4014 | break; | |
4015 | ||
4016 | case bfd_link_hash_common: | |
4017 | old_bfd = h->root.u.c.p->section->owner; | |
4018 | old_alignment = h->root.u.c.p->alignment_power; | |
4019 | break; | |
4020 | } | |
4021 | ||
4022 | if (elf_tdata (abfd)->verdef != NULL | |
4023 | && ! override | |
4024 | && vernum > 1 | |
4025 | && definition) | |
4026 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4027 | } | |
4028 | ||
4029 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4030 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4031 | (struct bfd_link_hash_entry **) sym_hash))) |
4032 | goto error_free_vers; | |
4033 | ||
4034 | h = *sym_hash; | |
4035 | while (h->root.type == bfd_link_hash_indirect | |
4036 | || h->root.type == bfd_link_hash_warning) | |
4037 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4038 | *sym_hash = h; | |
4039 | ||
4040 | new_weakdef = FALSE; | |
4041 | if (dynamic | |
4042 | && definition | |
4043 | && (flags & BSF_WEAK) != 0 | |
fcb93ecf | 4044 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4045 | && is_elf_hash_table (htab) |
f6e332e6 | 4046 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
4047 | { |
4048 | /* Keep a list of all weak defined non function symbols from | |
4049 | a dynamic object, using the weakdef field. Later in this | |
4050 | function we will set the weakdef field to the correct | |
4051 | value. We only put non-function symbols from dynamic | |
4052 | objects on this list, because that happens to be the only | |
4053 | time we need to know the normal symbol corresponding to a | |
4054 | weak symbol, and the information is time consuming to | |
4055 | figure out. If the weakdef field is not already NULL, | |
4056 | then this symbol was already defined by some previous | |
4057 | dynamic object, and we will be using that previous | |
4058 | definition anyhow. */ | |
4059 | ||
f6e332e6 | 4060 | h->u.weakdef = weaks; |
4ad4eba5 AM |
4061 | weaks = h; |
4062 | new_weakdef = TRUE; | |
4063 | } | |
4064 | ||
4065 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4066 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4067 | && h->root.type == bfd_link_hash_common) |
4068 | { | |
4069 | unsigned int align; | |
4070 | ||
a4d8e49b | 4071 | if (common) |
af44c138 L |
4072 | align = bfd_log2 (isym->st_value); |
4073 | else | |
4074 | { | |
4075 | /* The new symbol is a common symbol in a shared object. | |
4076 | We need to get the alignment from the section. */ | |
4077 | align = new_sec->alignment_power; | |
4078 | } | |
4ad4eba5 AM |
4079 | if (align > old_alignment |
4080 | /* Permit an alignment power of zero if an alignment of one | |
4081 | is specified and no other alignments have been specified. */ | |
4082 | || (isym->st_value == 1 && old_alignment == 0)) | |
4083 | h->root.u.c.p->alignment_power = align; | |
4084 | else | |
4085 | h->root.u.c.p->alignment_power = old_alignment; | |
4086 | } | |
4087 | ||
66eb6687 | 4088 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4089 | { |
4ad4eba5 | 4090 | bfd_boolean dynsym; |
4ad4eba5 AM |
4091 | |
4092 | /* Check the alignment when a common symbol is involved. This | |
4093 | can change when a common symbol is overridden by a normal | |
4094 | definition or a common symbol is ignored due to the old | |
4095 | normal definition. We need to make sure the maximum | |
4096 | alignment is maintained. */ | |
a4d8e49b | 4097 | if ((old_alignment || common) |
4ad4eba5 AM |
4098 | && h->root.type != bfd_link_hash_common) |
4099 | { | |
4100 | unsigned int common_align; | |
4101 | unsigned int normal_align; | |
4102 | unsigned int symbol_align; | |
4103 | bfd *normal_bfd; | |
4104 | bfd *common_bfd; | |
4105 | ||
4106 | symbol_align = ffs (h->root.u.def.value) - 1; | |
4107 | if (h->root.u.def.section->owner != NULL | |
4108 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
4109 | { | |
4110 | normal_align = h->root.u.def.section->alignment_power; | |
4111 | if (normal_align > symbol_align) | |
4112 | normal_align = symbol_align; | |
4113 | } | |
4114 | else | |
4115 | normal_align = symbol_align; | |
4116 | ||
4117 | if (old_alignment) | |
4118 | { | |
4119 | common_align = old_alignment; | |
4120 | common_bfd = old_bfd; | |
4121 | normal_bfd = abfd; | |
4122 | } | |
4123 | else | |
4124 | { | |
4125 | common_align = bfd_log2 (isym->st_value); | |
4126 | common_bfd = abfd; | |
4127 | normal_bfd = old_bfd; | |
4128 | } | |
4129 | ||
4130 | if (normal_align < common_align) | |
d07676f8 NC |
4131 | { |
4132 | /* PR binutils/2735 */ | |
4133 | if (normal_bfd == NULL) | |
4134 | (*_bfd_error_handler) | |
4135 | (_("Warning: alignment %u of common symbol `%s' in %B" | |
4136 | " is greater than the alignment (%u) of its section %A"), | |
4137 | common_bfd, h->root.u.def.section, | |
4138 | 1 << common_align, name, 1 << normal_align); | |
4139 | else | |
4140 | (*_bfd_error_handler) | |
4141 | (_("Warning: alignment %u of symbol `%s' in %B" | |
4142 | " is smaller than %u in %B"), | |
4143 | normal_bfd, common_bfd, | |
4144 | 1 << normal_align, name, 1 << common_align); | |
4145 | } | |
4ad4eba5 AM |
4146 | } |
4147 | ||
83ad0046 L |
4148 | /* Remember the symbol size if it isn't undefined. */ |
4149 | if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 AM |
4150 | && (definition || h->size == 0)) |
4151 | { | |
83ad0046 L |
4152 | if (h->size != 0 |
4153 | && h->size != isym->st_size | |
4154 | && ! size_change_ok) | |
4ad4eba5 | 4155 | (*_bfd_error_handler) |
d003868e AM |
4156 | (_("Warning: size of symbol `%s' changed" |
4157 | " from %lu in %B to %lu in %B"), | |
4158 | old_bfd, abfd, | |
4ad4eba5 | 4159 | name, (unsigned long) h->size, |
d003868e | 4160 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
4161 | |
4162 | h->size = isym->st_size; | |
4163 | } | |
4164 | ||
4165 | /* If this is a common symbol, then we always want H->SIZE | |
4166 | to be the size of the common symbol. The code just above | |
4167 | won't fix the size if a common symbol becomes larger. We | |
4168 | don't warn about a size change here, because that is | |
fcb93ecf PB |
4169 | covered by --warn-common. Allow changed between different |
4170 | function types. */ | |
4ad4eba5 AM |
4171 | if (h->root.type == bfd_link_hash_common) |
4172 | h->size = h->root.u.c.size; | |
4173 | ||
4174 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
4175 | && (definition || h->type == STT_NOTYPE)) | |
4176 | { | |
4177 | if (h->type != STT_NOTYPE | |
4178 | && h->type != ELF_ST_TYPE (isym->st_info) | |
4179 | && ! type_change_ok) | |
4180 | (*_bfd_error_handler) | |
d003868e AM |
4181 | (_("Warning: type of symbol `%s' changed" |
4182 | " from %d to %d in %B"), | |
4183 | abfd, name, h->type, ELF_ST_TYPE (isym->st_info)); | |
4ad4eba5 AM |
4184 | |
4185 | h->type = ELF_ST_TYPE (isym->st_info); | |
4186 | } | |
4187 | ||
4188 | /* If st_other has a processor-specific meaning, specific | |
4189 | code might be needed here. We never merge the visibility | |
4190 | attribute with the one from a dynamic object. */ | |
4191 | if (bed->elf_backend_merge_symbol_attribute) | |
4192 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
4193 | dynamic); | |
4194 | ||
b58f81ae DJ |
4195 | /* If this symbol has default visibility and the user has requested |
4196 | we not re-export it, then mark it as hidden. */ | |
4197 | if (definition && !dynamic | |
4198 | && (abfd->no_export | |
4199 | || (abfd->my_archive && abfd->my_archive->no_export)) | |
4200 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) | |
66eb6687 AM |
4201 | isym->st_other = (STV_HIDDEN |
4202 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
b58f81ae | 4203 | |
8992f0d7 | 4204 | if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic) |
4ad4eba5 AM |
4205 | { |
4206 | unsigned char hvis, symvis, other, nvis; | |
4207 | ||
8992f0d7 TS |
4208 | /* Only merge the visibility. Leave the remainder of the |
4209 | st_other field to elf_backend_merge_symbol_attribute. */ | |
4210 | other = h->other & ~ELF_ST_VISIBILITY (-1); | |
4ad4eba5 AM |
4211 | |
4212 | /* Combine visibilities, using the most constraining one. */ | |
4213 | hvis = ELF_ST_VISIBILITY (h->other); | |
4214 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
4215 | if (! hvis) | |
4216 | nvis = symvis; | |
4217 | else if (! symvis) | |
4218 | nvis = hvis; | |
4219 | else | |
4220 | nvis = hvis < symvis ? hvis : symvis; | |
4221 | ||
4222 | h->other = other | nvis; | |
4223 | } | |
4224 | ||
4225 | /* Set a flag in the hash table entry indicating the type of | |
4226 | reference or definition we just found. Keep a count of | |
4227 | the number of dynamic symbols we find. A dynamic symbol | |
4228 | is one which is referenced or defined by both a regular | |
4229 | object and a shared object. */ | |
4ad4eba5 AM |
4230 | dynsym = FALSE; |
4231 | if (! dynamic) | |
4232 | { | |
4233 | if (! definition) | |
4234 | { | |
f5385ebf | 4235 | h->ref_regular = 1; |
4ad4eba5 | 4236 | if (bind != STB_WEAK) |
f5385ebf | 4237 | h->ref_regular_nonweak = 1; |
4ad4eba5 AM |
4238 | } |
4239 | else | |
f5385ebf | 4240 | h->def_regular = 1; |
4ad4eba5 | 4241 | if (! info->executable |
f5385ebf AM |
4242 | || h->def_dynamic |
4243 | || h->ref_dynamic) | |
4ad4eba5 AM |
4244 | dynsym = TRUE; |
4245 | } | |
4246 | else | |
4247 | { | |
4248 | if (! definition) | |
f5385ebf | 4249 | h->ref_dynamic = 1; |
4ad4eba5 | 4250 | else |
f5385ebf AM |
4251 | h->def_dynamic = 1; |
4252 | if (h->def_regular | |
4253 | || h->ref_regular | |
f6e332e6 | 4254 | || (h->u.weakdef != NULL |
4ad4eba5 | 4255 | && ! new_weakdef |
f6e332e6 | 4256 | && h->u.weakdef->dynindx != -1)) |
4ad4eba5 AM |
4257 | dynsym = TRUE; |
4258 | } | |
4259 | ||
92b7c7b6 L |
4260 | if (definition && (sec->flags & SEC_DEBUGGING)) |
4261 | { | |
4262 | /* We don't want to make debug symbol dynamic. */ | |
4263 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4264 | dynsym = FALSE; | |
4265 | } | |
4266 | ||
4ad4eba5 AM |
4267 | /* Check to see if we need to add an indirect symbol for |
4268 | the default name. */ | |
4269 | if (definition || h->root.type == bfd_link_hash_common) | |
4270 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4271 | &sec, &value, &dynsym, | |
4272 | override)) | |
4273 | goto error_free_vers; | |
4274 | ||
4275 | if (definition && !dynamic) | |
4276 | { | |
4277 | char *p = strchr (name, ELF_VER_CHR); | |
4278 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4279 | { | |
4280 | /* Queue non-default versions so that .symver x, x@FOO | |
4281 | aliases can be checked. */ | |
66eb6687 | 4282 | if (!nondeflt_vers) |
4ad4eba5 | 4283 | { |
66eb6687 AM |
4284 | amt = ((isymend - isym + 1) |
4285 | * sizeof (struct elf_link_hash_entry *)); | |
4ad4eba5 AM |
4286 | nondeflt_vers = bfd_malloc (amt); |
4287 | } | |
66eb6687 | 4288 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4289 | } |
4290 | } | |
4291 | ||
4292 | if (dynsym && h->dynindx == -1) | |
4293 | { | |
c152c796 | 4294 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4295 | goto error_free_vers; |
f6e332e6 | 4296 | if (h->u.weakdef != NULL |
4ad4eba5 | 4297 | && ! new_weakdef |
f6e332e6 | 4298 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 4299 | { |
66eb6687 | 4300 | if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
4301 | goto error_free_vers; |
4302 | } | |
4303 | } | |
4304 | else if (dynsym && h->dynindx != -1) | |
4305 | /* If the symbol already has a dynamic index, but | |
4306 | visibility says it should not be visible, turn it into | |
4307 | a local symbol. */ | |
4308 | switch (ELF_ST_VISIBILITY (h->other)) | |
4309 | { | |
4310 | case STV_INTERNAL: | |
4311 | case STV_HIDDEN: | |
4312 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4313 | dynsym = FALSE; | |
4314 | break; | |
4315 | } | |
4316 | ||
4317 | if (!add_needed | |
4318 | && definition | |
4319 | && dynsym | |
f5385ebf | 4320 | && h->ref_regular) |
4ad4eba5 AM |
4321 | { |
4322 | int ret; | |
4323 | const char *soname = elf_dt_name (abfd); | |
4324 | ||
4325 | /* A symbol from a library loaded via DT_NEEDED of some | |
4326 | other library is referenced by a regular object. | |
e56f61be L |
4327 | Add a DT_NEEDED entry for it. Issue an error if |
4328 | --no-add-needed is used. */ | |
4329 | if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) | |
4330 | { | |
4331 | (*_bfd_error_handler) | |
4332 | (_("%s: invalid DSO for symbol `%s' definition"), | |
d003868e | 4333 | abfd, name); |
e56f61be L |
4334 | bfd_set_error (bfd_error_bad_value); |
4335 | goto error_free_vers; | |
4336 | } | |
4337 | ||
a5db907e AM |
4338 | elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED; |
4339 | ||
4ad4eba5 | 4340 | add_needed = TRUE; |
7e9f0867 | 4341 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4342 | if (ret < 0) |
4343 | goto error_free_vers; | |
4344 | ||
4345 | BFD_ASSERT (ret == 0); | |
4346 | } | |
4347 | } | |
4348 | } | |
4349 | ||
66eb6687 AM |
4350 | if (extversym != NULL) |
4351 | { | |
4352 | free (extversym); | |
4353 | extversym = NULL; | |
4354 | } | |
4355 | ||
4356 | if (isymbuf != NULL) | |
4357 | { | |
4358 | free (isymbuf); | |
4359 | isymbuf = NULL; | |
4360 | } | |
4361 | ||
4362 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4363 | { | |
4364 | unsigned int i; | |
4365 | ||
4366 | /* Restore the symbol table. */ | |
97fed1c9 JJ |
4367 | if (bed->as_needed_cleanup) |
4368 | (*bed->as_needed_cleanup) (abfd, info); | |
66eb6687 AM |
4369 | old_hash = (char *) old_tab + tabsize; |
4370 | old_ent = (char *) old_hash + hashsize; | |
4371 | sym_hash = elf_sym_hashes (abfd); | |
4f87808c AM |
4372 | htab->root.table.table = old_table; |
4373 | htab->root.table.size = old_size; | |
4374 | htab->root.table.count = old_count; | |
66eb6687 AM |
4375 | memcpy (htab->root.table.table, old_tab, tabsize); |
4376 | memcpy (sym_hash, old_hash, hashsize); | |
4377 | htab->root.undefs = old_undefs; | |
4378 | htab->root.undefs_tail = old_undefs_tail; | |
4379 | for (i = 0; i < htab->root.table.size; i++) | |
4380 | { | |
4381 | struct bfd_hash_entry *p; | |
4382 | struct elf_link_hash_entry *h; | |
4383 | ||
4384 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4385 | { | |
4386 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
4387 | if (h->root.type == bfd_link_hash_warning) |
4388 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
66eb6687 AM |
4389 | if (h->dynindx >= old_dynsymcount) |
4390 | _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index); | |
2de92251 | 4391 | |
66eb6687 AM |
4392 | memcpy (p, old_ent, htab->root.table.entsize); |
4393 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4394 | h = (struct elf_link_hash_entry *) p; |
4395 | if (h->root.type == bfd_link_hash_warning) | |
4396 | { | |
4397 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
4398 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4399 | } | |
66eb6687 AM |
4400 | } |
4401 | } | |
4402 | ||
5061a885 AM |
4403 | /* Make a special call to the linker "notice" function to |
4404 | tell it that symbols added for crefs may need to be removed. */ | |
4405 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, | |
4406 | notice_not_needed)) | |
9af2a943 | 4407 | goto error_free_vers; |
5061a885 | 4408 | |
66eb6687 AM |
4409 | free (old_tab); |
4410 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
4411 | alloc_mark); | |
4412 | if (nondeflt_vers != NULL) | |
4413 | free (nondeflt_vers); | |
4414 | return TRUE; | |
4415 | } | |
2de92251 | 4416 | |
66eb6687 AM |
4417 | if (old_tab != NULL) |
4418 | { | |
5061a885 AM |
4419 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, |
4420 | notice_needed)) | |
9af2a943 | 4421 | goto error_free_vers; |
66eb6687 AM |
4422 | free (old_tab); |
4423 | old_tab = NULL; | |
4424 | } | |
4425 | ||
4ad4eba5 AM |
4426 | /* Now that all the symbols from this input file are created, handle |
4427 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
4428 | if (nondeflt_vers != NULL) | |
4429 | { | |
4430 | bfd_size_type cnt, symidx; | |
4431 | ||
4432 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
4433 | { | |
4434 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
4435 | char *shortname, *p; | |
4436 | ||
4437 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
4438 | if (p == NULL | |
4439 | || (h->root.type != bfd_link_hash_defined | |
4440 | && h->root.type != bfd_link_hash_defweak)) | |
4441 | continue; | |
4442 | ||
4443 | amt = p - h->root.root.string; | |
4444 | shortname = bfd_malloc (amt + 1); | |
4445 | memcpy (shortname, h->root.root.string, amt); | |
4446 | shortname[amt] = '\0'; | |
4447 | ||
4448 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 4449 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
4450 | FALSE, FALSE, FALSE); |
4451 | if (hi != NULL | |
4452 | && hi->root.type == h->root.type | |
4453 | && hi->root.u.def.value == h->root.u.def.value | |
4454 | && hi->root.u.def.section == h->root.u.def.section) | |
4455 | { | |
4456 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4457 | hi->root.type = bfd_link_hash_indirect; | |
4458 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 4459 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
4460 | sym_hash = elf_sym_hashes (abfd); |
4461 | if (sym_hash) | |
4462 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4463 | if (sym_hash[symidx] == hi) | |
4464 | { | |
4465 | sym_hash[symidx] = h; | |
4466 | break; | |
4467 | } | |
4468 | } | |
4469 | free (shortname); | |
4470 | } | |
4471 | free (nondeflt_vers); | |
4472 | nondeflt_vers = NULL; | |
4473 | } | |
4474 | ||
4ad4eba5 AM |
4475 | /* Now set the weakdefs field correctly for all the weak defined |
4476 | symbols we found. The only way to do this is to search all the | |
4477 | symbols. Since we only need the information for non functions in | |
4478 | dynamic objects, that's the only time we actually put anything on | |
4479 | the list WEAKS. We need this information so that if a regular | |
4480 | object refers to a symbol defined weakly in a dynamic object, the | |
4481 | real symbol in the dynamic object is also put in the dynamic | |
4482 | symbols; we also must arrange for both symbols to point to the | |
4483 | same memory location. We could handle the general case of symbol | |
4484 | aliasing, but a general symbol alias can only be generated in | |
4485 | assembler code, handling it correctly would be very time | |
4486 | consuming, and other ELF linkers don't handle general aliasing | |
4487 | either. */ | |
4488 | if (weaks != NULL) | |
4489 | { | |
4490 | struct elf_link_hash_entry **hpp; | |
4491 | struct elf_link_hash_entry **hppend; | |
4492 | struct elf_link_hash_entry **sorted_sym_hash; | |
4493 | struct elf_link_hash_entry *h; | |
4494 | size_t sym_count; | |
4495 | ||
4496 | /* Since we have to search the whole symbol list for each weak | |
4497 | defined symbol, search time for N weak defined symbols will be | |
4498 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
4499 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
4500 | sorted_sym_hash = bfd_malloc (amt); | |
4501 | if (sorted_sym_hash == NULL) | |
4502 | goto error_return; | |
4503 | sym_hash = sorted_sym_hash; | |
4504 | hpp = elf_sym_hashes (abfd); | |
4505 | hppend = hpp + extsymcount; | |
4506 | sym_count = 0; | |
4507 | for (; hpp < hppend; hpp++) | |
4508 | { | |
4509 | h = *hpp; | |
4510 | if (h != NULL | |
4511 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 4512 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
4513 | { |
4514 | *sym_hash = h; | |
4515 | sym_hash++; | |
4516 | sym_count++; | |
4517 | } | |
4518 | } | |
4519 | ||
4520 | qsort (sorted_sym_hash, sym_count, | |
4521 | sizeof (struct elf_link_hash_entry *), | |
4522 | elf_sort_symbol); | |
4523 | ||
4524 | while (weaks != NULL) | |
4525 | { | |
4526 | struct elf_link_hash_entry *hlook; | |
4527 | asection *slook; | |
4528 | bfd_vma vlook; | |
4529 | long ilook; | |
4530 | size_t i, j, idx; | |
4531 | ||
4532 | hlook = weaks; | |
f6e332e6 AM |
4533 | weaks = hlook->u.weakdef; |
4534 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
4535 | |
4536 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
4537 | || hlook->root.type == bfd_link_hash_defweak | |
4538 | || hlook->root.type == bfd_link_hash_common | |
4539 | || hlook->root.type == bfd_link_hash_indirect); | |
4540 | slook = hlook->root.u.def.section; | |
4541 | vlook = hlook->root.u.def.value; | |
4542 | ||
4543 | ilook = -1; | |
4544 | i = 0; | |
4545 | j = sym_count; | |
4546 | while (i < j) | |
4547 | { | |
4548 | bfd_signed_vma vdiff; | |
4549 | idx = (i + j) / 2; | |
4550 | h = sorted_sym_hash [idx]; | |
4551 | vdiff = vlook - h->root.u.def.value; | |
4552 | if (vdiff < 0) | |
4553 | j = idx; | |
4554 | else if (vdiff > 0) | |
4555 | i = idx + 1; | |
4556 | else | |
4557 | { | |
a9b881be | 4558 | long sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
4559 | if (sdiff < 0) |
4560 | j = idx; | |
4561 | else if (sdiff > 0) | |
4562 | i = idx + 1; | |
4563 | else | |
4564 | { | |
4565 | ilook = idx; | |
4566 | break; | |
4567 | } | |
4568 | } | |
4569 | } | |
4570 | ||
4571 | /* We didn't find a value/section match. */ | |
4572 | if (ilook == -1) | |
4573 | continue; | |
4574 | ||
4575 | for (i = ilook; i < sym_count; i++) | |
4576 | { | |
4577 | h = sorted_sym_hash [i]; | |
4578 | ||
4579 | /* Stop if value or section doesn't match. */ | |
4580 | if (h->root.u.def.value != vlook | |
4581 | || h->root.u.def.section != slook) | |
4582 | break; | |
4583 | else if (h != hlook) | |
4584 | { | |
f6e332e6 | 4585 | hlook->u.weakdef = h; |
4ad4eba5 AM |
4586 | |
4587 | /* If the weak definition is in the list of dynamic | |
4588 | symbols, make sure the real definition is put | |
4589 | there as well. */ | |
4590 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
4591 | { | |
c152c796 | 4592 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 AM |
4593 | goto error_return; |
4594 | } | |
4595 | ||
4596 | /* If the real definition is in the list of dynamic | |
4597 | symbols, make sure the weak definition is put | |
4598 | there as well. If we don't do this, then the | |
4599 | dynamic loader might not merge the entries for the | |
4600 | real definition and the weak definition. */ | |
4601 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
4602 | { | |
c152c796 | 4603 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4ad4eba5 AM |
4604 | goto error_return; |
4605 | } | |
4606 | break; | |
4607 | } | |
4608 | } | |
4609 | } | |
4610 | ||
4611 | free (sorted_sym_hash); | |
4612 | } | |
4613 | ||
66eb6687 AM |
4614 | if (bed->check_directives) |
4615 | (*bed->check_directives) (abfd, info); | |
85fbca6a | 4616 | |
4ad4eba5 AM |
4617 | /* If this object is the same format as the output object, and it is |
4618 | not a shared library, then let the backend look through the | |
4619 | relocs. | |
4620 | ||
4621 | This is required to build global offset table entries and to | |
4622 | arrange for dynamic relocs. It is not required for the | |
4623 | particular common case of linking non PIC code, even when linking | |
4624 | against shared libraries, but unfortunately there is no way of | |
4625 | knowing whether an object file has been compiled PIC or not. | |
4626 | Looking through the relocs is not particularly time consuming. | |
4627 | The problem is that we must either (1) keep the relocs in memory, | |
4628 | which causes the linker to require additional runtime memory or | |
4629 | (2) read the relocs twice from the input file, which wastes time. | |
4630 | This would be a good case for using mmap. | |
4631 | ||
4632 | I have no idea how to handle linking PIC code into a file of a | |
4633 | different format. It probably can't be done. */ | |
4ad4eba5 | 4634 | if (! dynamic |
66eb6687 AM |
4635 | && is_elf_hash_table (htab) |
4636 | && htab->root.creator == abfd->xvec | |
4637 | && bed->check_relocs != NULL) | |
4ad4eba5 AM |
4638 | { |
4639 | asection *o; | |
4640 | ||
4641 | for (o = abfd->sections; o != NULL; o = o->next) | |
4642 | { | |
4643 | Elf_Internal_Rela *internal_relocs; | |
4644 | bfd_boolean ok; | |
4645 | ||
4646 | if ((o->flags & SEC_RELOC) == 0 | |
4647 | || o->reloc_count == 0 | |
4648 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4649 | && (o->flags & SEC_DEBUGGING) != 0) | |
4650 | || bfd_is_abs_section (o->output_section)) | |
4651 | continue; | |
4652 | ||
4653 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4654 | info->keep_memory); | |
4655 | if (internal_relocs == NULL) | |
4656 | goto error_return; | |
4657 | ||
66eb6687 | 4658 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); |
4ad4eba5 AM |
4659 | |
4660 | if (elf_section_data (o)->relocs != internal_relocs) | |
4661 | free (internal_relocs); | |
4662 | ||
4663 | if (! ok) | |
4664 | goto error_return; | |
4665 | } | |
4666 | } | |
4667 | ||
4668 | /* If this is a non-traditional link, try to optimize the handling | |
4669 | of the .stab/.stabstr sections. */ | |
4670 | if (! dynamic | |
4671 | && ! info->traditional_format | |
66eb6687 | 4672 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
4673 | && (info->strip != strip_all && info->strip != strip_debugger)) |
4674 | { | |
4675 | asection *stabstr; | |
4676 | ||
4677 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4678 | if (stabstr != NULL) | |
4679 | { | |
4680 | bfd_size_type string_offset = 0; | |
4681 | asection *stab; | |
4682 | ||
4683 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 4684 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
4685 | && (!stab->name[5] || |
4686 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4687 | && (stab->flags & SEC_MERGE) == 0 | |
4688 | && !bfd_is_abs_section (stab->output_section)) | |
4689 | { | |
4690 | struct bfd_elf_section_data *secdata; | |
4691 | ||
4692 | secdata = elf_section_data (stab); | |
66eb6687 AM |
4693 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
4694 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
4695 | &string_offset)) |
4696 | goto error_return; | |
4697 | if (secdata->sec_info) | |
4698 | stab->sec_info_type = ELF_INFO_TYPE_STABS; | |
4699 | } | |
4700 | } | |
4701 | } | |
4702 | ||
66eb6687 | 4703 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
4704 | { |
4705 | /* Add this bfd to the loaded list. */ | |
4706 | struct elf_link_loaded_list *n; | |
4707 | ||
4708 | n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); | |
4709 | if (n == NULL) | |
4710 | goto error_return; | |
4711 | n->abfd = abfd; | |
66eb6687 AM |
4712 | n->next = htab->loaded; |
4713 | htab->loaded = n; | |
4ad4eba5 AM |
4714 | } |
4715 | ||
4716 | return TRUE; | |
4717 | ||
4718 | error_free_vers: | |
66eb6687 AM |
4719 | if (old_tab != NULL) |
4720 | free (old_tab); | |
4ad4eba5 AM |
4721 | if (nondeflt_vers != NULL) |
4722 | free (nondeflt_vers); | |
4723 | if (extversym != NULL) | |
4724 | free (extversym); | |
4725 | error_free_sym: | |
4726 | if (isymbuf != NULL) | |
4727 | free (isymbuf); | |
4728 | error_return: | |
4729 | return FALSE; | |
4730 | } | |
4731 | ||
8387904d AM |
4732 | /* Return the linker hash table entry of a symbol that might be |
4733 | satisfied by an archive symbol. Return -1 on error. */ | |
4734 | ||
4735 | struct elf_link_hash_entry * | |
4736 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
4737 | struct bfd_link_info *info, | |
4738 | const char *name) | |
4739 | { | |
4740 | struct elf_link_hash_entry *h; | |
4741 | char *p, *copy; | |
4742 | size_t len, first; | |
4743 | ||
4744 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
4745 | if (h != NULL) | |
4746 | return h; | |
4747 | ||
4748 | /* If this is a default version (the name contains @@), look up the | |
4749 | symbol again with only one `@' as well as without the version. | |
4750 | The effect is that references to the symbol with and without the | |
4751 | version will be matched by the default symbol in the archive. */ | |
4752 | ||
4753 | p = strchr (name, ELF_VER_CHR); | |
4754 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4755 | return h; | |
4756 | ||
4757 | /* First check with only one `@'. */ | |
4758 | len = strlen (name); | |
4759 | copy = bfd_alloc (abfd, len); | |
4760 | if (copy == NULL) | |
4761 | return (struct elf_link_hash_entry *) 0 - 1; | |
4762 | ||
4763 | first = p - name + 1; | |
4764 | memcpy (copy, name, first); | |
4765 | memcpy (copy + first, name + first + 1, len - first); | |
4766 | ||
4767 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE); | |
4768 | if (h == NULL) | |
4769 | { | |
4770 | /* We also need to check references to the symbol without the | |
4771 | version. */ | |
4772 | copy[first - 1] = '\0'; | |
4773 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
4774 | FALSE, FALSE, FALSE); | |
4775 | } | |
4776 | ||
4777 | bfd_release (abfd, copy); | |
4778 | return h; | |
4779 | } | |
4780 | ||
0ad989f9 L |
4781 | /* Add symbols from an ELF archive file to the linker hash table. We |
4782 | don't use _bfd_generic_link_add_archive_symbols because of a | |
4783 | problem which arises on UnixWare. The UnixWare libc.so is an | |
4784 | archive which includes an entry libc.so.1 which defines a bunch of | |
4785 | symbols. The libc.so archive also includes a number of other | |
4786 | object files, which also define symbols, some of which are the same | |
4787 | as those defined in libc.so.1. Correct linking requires that we | |
4788 | consider each object file in turn, and include it if it defines any | |
4789 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
4790 | this; it looks through the list of undefined symbols, and includes | |
4791 | any object file which defines them. When this algorithm is used on | |
4792 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
4793 | bunch of symbols. This means that some of the other objects in the | |
4794 | archive are not included in the link, which is incorrect since they | |
4795 | precede libc.so.1 in the archive. | |
4796 | ||
4797 | Fortunately, ELF archive handling is simpler than that done by | |
4798 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4799 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4800 | symbol is currently undefined, we know that we must pull in that | |
4801 | object file. | |
4802 | ||
4803 | Unfortunately, we do have to make multiple passes over the symbol | |
4804 | table until nothing further is resolved. */ | |
4805 | ||
4ad4eba5 AM |
4806 | static bfd_boolean |
4807 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
4808 | { |
4809 | symindex c; | |
4810 | bfd_boolean *defined = NULL; | |
4811 | bfd_boolean *included = NULL; | |
4812 | carsym *symdefs; | |
4813 | bfd_boolean loop; | |
4814 | bfd_size_type amt; | |
8387904d AM |
4815 | const struct elf_backend_data *bed; |
4816 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
4817 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
4818 | |
4819 | if (! bfd_has_map (abfd)) | |
4820 | { | |
4821 | /* An empty archive is a special case. */ | |
4822 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
4823 | return TRUE; | |
4824 | bfd_set_error (bfd_error_no_armap); | |
4825 | return FALSE; | |
4826 | } | |
4827 | ||
4828 | /* Keep track of all symbols we know to be already defined, and all | |
4829 | files we know to be already included. This is to speed up the | |
4830 | second and subsequent passes. */ | |
4831 | c = bfd_ardata (abfd)->symdef_count; | |
4832 | if (c == 0) | |
4833 | return TRUE; | |
4834 | amt = c; | |
4835 | amt *= sizeof (bfd_boolean); | |
4836 | defined = bfd_zmalloc (amt); | |
4837 | included = bfd_zmalloc (amt); | |
4838 | if (defined == NULL || included == NULL) | |
4839 | goto error_return; | |
4840 | ||
4841 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
4842 | bed = get_elf_backend_data (abfd); |
4843 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
4844 | |
4845 | do | |
4846 | { | |
4847 | file_ptr last; | |
4848 | symindex i; | |
4849 | carsym *symdef; | |
4850 | carsym *symdefend; | |
4851 | ||
4852 | loop = FALSE; | |
4853 | last = -1; | |
4854 | ||
4855 | symdef = symdefs; | |
4856 | symdefend = symdef + c; | |
4857 | for (i = 0; symdef < symdefend; symdef++, i++) | |
4858 | { | |
4859 | struct elf_link_hash_entry *h; | |
4860 | bfd *element; | |
4861 | struct bfd_link_hash_entry *undefs_tail; | |
4862 | symindex mark; | |
4863 | ||
4864 | if (defined[i] || included[i]) | |
4865 | continue; | |
4866 | if (symdef->file_offset == last) | |
4867 | { | |
4868 | included[i] = TRUE; | |
4869 | continue; | |
4870 | } | |
4871 | ||
8387904d AM |
4872 | h = archive_symbol_lookup (abfd, info, symdef->name); |
4873 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
4874 | goto error_return; | |
0ad989f9 L |
4875 | |
4876 | if (h == NULL) | |
4877 | continue; | |
4878 | ||
4879 | if (h->root.type == bfd_link_hash_common) | |
4880 | { | |
4881 | /* We currently have a common symbol. The archive map contains | |
4882 | a reference to this symbol, so we may want to include it. We | |
4883 | only want to include it however, if this archive element | |
4884 | contains a definition of the symbol, not just another common | |
4885 | declaration of it. | |
4886 | ||
4887 | Unfortunately some archivers (including GNU ar) will put | |
4888 | declarations of common symbols into their archive maps, as | |
4889 | well as real definitions, so we cannot just go by the archive | |
4890 | map alone. Instead we must read in the element's symbol | |
4891 | table and check that to see what kind of symbol definition | |
4892 | this is. */ | |
4893 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
4894 | continue; | |
4895 | } | |
4896 | else if (h->root.type != bfd_link_hash_undefined) | |
4897 | { | |
4898 | if (h->root.type != bfd_link_hash_undefweak) | |
4899 | defined[i] = TRUE; | |
4900 | continue; | |
4901 | } | |
4902 | ||
4903 | /* We need to include this archive member. */ | |
4904 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
4905 | if (element == NULL) | |
4906 | goto error_return; | |
4907 | ||
4908 | if (! bfd_check_format (element, bfd_object)) | |
4909 | goto error_return; | |
4910 | ||
4911 | /* Doublecheck that we have not included this object | |
4912 | already--it should be impossible, but there may be | |
4913 | something wrong with the archive. */ | |
4914 | if (element->archive_pass != 0) | |
4915 | { | |
4916 | bfd_set_error (bfd_error_bad_value); | |
4917 | goto error_return; | |
4918 | } | |
4919 | element->archive_pass = 1; | |
4920 | ||
4921 | undefs_tail = info->hash->undefs_tail; | |
4922 | ||
4923 | if (! (*info->callbacks->add_archive_element) (info, element, | |
4924 | symdef->name)) | |
4925 | goto error_return; | |
4926 | if (! bfd_link_add_symbols (element, info)) | |
4927 | goto error_return; | |
4928 | ||
4929 | /* If there are any new undefined symbols, we need to make | |
4930 | another pass through the archive in order to see whether | |
4931 | they can be defined. FIXME: This isn't perfect, because | |
4932 | common symbols wind up on undefs_tail and because an | |
4933 | undefined symbol which is defined later on in this pass | |
4934 | does not require another pass. This isn't a bug, but it | |
4935 | does make the code less efficient than it could be. */ | |
4936 | if (undefs_tail != info->hash->undefs_tail) | |
4937 | loop = TRUE; | |
4938 | ||
4939 | /* Look backward to mark all symbols from this object file | |
4940 | which we have already seen in this pass. */ | |
4941 | mark = i; | |
4942 | do | |
4943 | { | |
4944 | included[mark] = TRUE; | |
4945 | if (mark == 0) | |
4946 | break; | |
4947 | --mark; | |
4948 | } | |
4949 | while (symdefs[mark].file_offset == symdef->file_offset); | |
4950 | ||
4951 | /* We mark subsequent symbols from this object file as we go | |
4952 | on through the loop. */ | |
4953 | last = symdef->file_offset; | |
4954 | } | |
4955 | } | |
4956 | while (loop); | |
4957 | ||
4958 | free (defined); | |
4959 | free (included); | |
4960 | ||
4961 | return TRUE; | |
4962 | ||
4963 | error_return: | |
4964 | if (defined != NULL) | |
4965 | free (defined); | |
4966 | if (included != NULL) | |
4967 | free (included); | |
4968 | return FALSE; | |
4969 | } | |
4ad4eba5 AM |
4970 | |
4971 | /* Given an ELF BFD, add symbols to the global hash table as | |
4972 | appropriate. */ | |
4973 | ||
4974 | bfd_boolean | |
4975 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
4976 | { | |
4977 | switch (bfd_get_format (abfd)) | |
4978 | { | |
4979 | case bfd_object: | |
4980 | return elf_link_add_object_symbols (abfd, info); | |
4981 | case bfd_archive: | |
4982 | return elf_link_add_archive_symbols (abfd, info); | |
4983 | default: | |
4984 | bfd_set_error (bfd_error_wrong_format); | |
4985 | return FALSE; | |
4986 | } | |
4987 | } | |
5a580b3a AM |
4988 | \f |
4989 | /* This function will be called though elf_link_hash_traverse to store | |
4990 | all hash value of the exported symbols in an array. */ | |
4991 | ||
4992 | static bfd_boolean | |
4993 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
4994 | { | |
4995 | unsigned long **valuep = data; | |
4996 | const char *name; | |
4997 | char *p; | |
4998 | unsigned long ha; | |
4999 | char *alc = NULL; | |
5000 | ||
5001 | if (h->root.type == bfd_link_hash_warning) | |
5002 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5003 | ||
5004 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
5005 | if (h->dynindx == -1) | |
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_hash (name); | |
5020 | ||
5021 | /* Store the found hash value in the array given as the argument. */ | |
5022 | *(*valuep)++ = ha; | |
5023 | ||
5024 | /* And store it in the struct so that we can put it in the hash table | |
5025 | later. */ | |
f6e332e6 | 5026 | h->u.elf_hash_value = ha; |
5a580b3a AM |
5027 | |
5028 | if (alc != NULL) | |
5029 | free (alc); | |
5030 | ||
5031 | return TRUE; | |
5032 | } | |
5033 | ||
fdc90cb4 JJ |
5034 | struct collect_gnu_hash_codes |
5035 | { | |
5036 | bfd *output_bfd; | |
5037 | const struct elf_backend_data *bed; | |
5038 | unsigned long int nsyms; | |
5039 | unsigned long int maskbits; | |
5040 | unsigned long int *hashcodes; | |
5041 | unsigned long int *hashval; | |
5042 | unsigned long int *indx; | |
5043 | unsigned long int *counts; | |
5044 | bfd_vma *bitmask; | |
5045 | bfd_byte *contents; | |
5046 | long int min_dynindx; | |
5047 | unsigned long int bucketcount; | |
5048 | unsigned long int symindx; | |
5049 | long int local_indx; | |
5050 | long int shift1, shift2; | |
5051 | unsigned long int mask; | |
5052 | }; | |
5053 | ||
5054 | /* This function will be called though elf_link_hash_traverse to store | |
5055 | all hash value of the exported symbols in an array. */ | |
5056 | ||
5057 | static bfd_boolean | |
5058 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5059 | { | |
5060 | struct collect_gnu_hash_codes *s = data; | |
5061 | const char *name; | |
5062 | char *p; | |
5063 | unsigned long ha; | |
5064 | char *alc = NULL; | |
5065 | ||
5066 | if (h->root.type == bfd_link_hash_warning) | |
5067 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5068 | ||
5069 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
5070 | if (h->dynindx == -1) | |
5071 | return TRUE; | |
5072 | ||
5073 | /* Ignore also local symbols and undefined symbols. */ | |
5074 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5075 | return TRUE; | |
5076 | ||
5077 | name = h->root.root.string; | |
5078 | p = strchr (name, ELF_VER_CHR); | |
5079 | if (p != NULL) | |
5080 | { | |
5081 | alc = bfd_malloc (p - name + 1); | |
5082 | memcpy (alc, name, p - name); | |
5083 | alc[p - name] = '\0'; | |
5084 | name = alc; | |
5085 | } | |
5086 | ||
5087 | /* Compute the hash value. */ | |
5088 | ha = bfd_elf_gnu_hash (name); | |
5089 | ||
5090 | /* Store the found hash value in the array for compute_bucket_count, | |
5091 | and also for .dynsym reordering purposes. */ | |
5092 | s->hashcodes[s->nsyms] = ha; | |
5093 | s->hashval[h->dynindx] = ha; | |
5094 | ++s->nsyms; | |
5095 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5096 | s->min_dynindx = h->dynindx; | |
5097 | ||
5098 | if (alc != NULL) | |
5099 | free (alc); | |
5100 | ||
5101 | return TRUE; | |
5102 | } | |
5103 | ||
5104 | /* This function will be called though elf_link_hash_traverse to do | |
5105 | final dynaminc symbol renumbering. */ | |
5106 | ||
5107 | static bfd_boolean | |
5108 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5109 | { | |
5110 | struct collect_gnu_hash_codes *s = data; | |
5111 | unsigned long int bucket; | |
5112 | unsigned long int val; | |
5113 | ||
5114 | if (h->root.type == bfd_link_hash_warning) | |
5115 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5116 | ||
5117 | /* Ignore indirect symbols. */ | |
5118 | if (h->dynindx == -1) | |
5119 | return TRUE; | |
5120 | ||
5121 | /* Ignore also local symbols and undefined symbols. */ | |
5122 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5123 | { | |
5124 | if (h->dynindx >= s->min_dynindx) | |
5125 | h->dynindx = s->local_indx++; | |
5126 | return TRUE; | |
5127 | } | |
5128 | ||
5129 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5130 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5131 | & ((s->maskbits >> s->shift1) - 1); | |
5132 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5133 | s->bitmask[val] | |
5134 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5135 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5136 | if (s->counts[bucket] == 1) | |
5137 | /* Last element terminates the chain. */ | |
5138 | val |= 1; | |
5139 | bfd_put_32 (s->output_bfd, val, | |
5140 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5141 | --s->counts[bucket]; | |
5142 | h->dynindx = s->indx[bucket]++; | |
5143 | return TRUE; | |
5144 | } | |
5145 | ||
5146 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5147 | ||
5148 | bfd_boolean | |
5149 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5150 | { | |
5151 | return !(h->forced_local | |
5152 | || h->root.type == bfd_link_hash_undefined | |
5153 | || h->root.type == bfd_link_hash_undefweak | |
5154 | || ((h->root.type == bfd_link_hash_defined | |
5155 | || h->root.type == bfd_link_hash_defweak) | |
5156 | && h->root.u.def.section->output_section == NULL)); | |
5157 | } | |
5158 | ||
5a580b3a AM |
5159 | /* Array used to determine the number of hash table buckets to use |
5160 | based on the number of symbols there are. If there are fewer than | |
5161 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5162 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5163 | than 32771 buckets. */ | |
5164 | ||
5165 | static const size_t elf_buckets[] = | |
5166 | { | |
5167 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5168 | 16411, 32771, 0 | |
5169 | }; | |
5170 | ||
5171 | /* Compute bucket count for hashing table. We do not use a static set | |
5172 | of possible tables sizes anymore. Instead we determine for all | |
5173 | possible reasonable sizes of the table the outcome (i.e., the | |
5174 | number of collisions etc) and choose the best solution. The | |
5175 | weighting functions are not too simple to allow the table to grow | |
5176 | without bounds. Instead one of the weighting factors is the size. | |
5177 | Therefore the result is always a good payoff between few collisions | |
5178 | (= short chain lengths) and table size. */ | |
5179 | static size_t | |
fdc90cb4 JJ |
5180 | compute_bucket_count (struct bfd_link_info *info, unsigned long int *hashcodes, |
5181 | unsigned long int nsyms, int gnu_hash) | |
5a580b3a AM |
5182 | { |
5183 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; | |
5184 | size_t best_size = 0; | |
5a580b3a AM |
5185 | unsigned long int i; |
5186 | bfd_size_type amt; | |
5187 | ||
5a580b3a AM |
5188 | /* We have a problem here. The following code to optimize the table |
5189 | size requires an integer type with more the 32 bits. If | |
5190 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5191 | #ifdef BFD_HOST_U_64_BIT | |
5192 | if (info->optimize) | |
5193 | { | |
5a580b3a AM |
5194 | size_t minsize; |
5195 | size_t maxsize; | |
5196 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a AM |
5197 | bfd *dynobj = elf_hash_table (info)->dynobj; |
5198 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); | |
fdc90cb4 | 5199 | unsigned long int *counts; |
5a580b3a AM |
5200 | |
5201 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5202 | that the hashing table must at least have NSYMS/4 and at most | |
5203 | 2*NSYMS buckets. */ | |
5204 | minsize = nsyms / 4; | |
5205 | if (minsize == 0) | |
5206 | minsize = 1; | |
5207 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5208 | if (gnu_hash) |
5209 | { | |
5210 | if (minsize < 2) | |
5211 | minsize = 2; | |
5212 | if ((best_size & 31) == 0) | |
5213 | ++best_size; | |
5214 | } | |
5a580b3a AM |
5215 | |
5216 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5217 | since the size could be large. */ | |
5218 | amt = maxsize; | |
5219 | amt *= sizeof (unsigned long int); | |
5220 | counts = bfd_malloc (amt); | |
5221 | if (counts == NULL) | |
fdc90cb4 | 5222 | return 0; |
5a580b3a AM |
5223 | |
5224 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5225 | minimal chain length. The minor criteria is (of course) the size | |
5226 | of the table. */ | |
5227 | for (i = minsize; i < maxsize; ++i) | |
5228 | { | |
5229 | /* Walk through the array of hashcodes and count the collisions. */ | |
5230 | BFD_HOST_U_64_BIT max; | |
5231 | unsigned long int j; | |
5232 | unsigned long int fact; | |
5233 | ||
fdc90cb4 JJ |
5234 | if (gnu_hash && (i & 31) == 0) |
5235 | continue; | |
5236 | ||
5a580b3a AM |
5237 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5238 | ||
5239 | /* Determine how often each hash bucket is used. */ | |
5240 | for (j = 0; j < nsyms; ++j) | |
5241 | ++counts[hashcodes[j] % i]; | |
5242 | ||
5243 | /* For the weight function we need some information about the | |
5244 | pagesize on the target. This is information need not be 100% | |
5245 | accurate. Since this information is not available (so far) we | |
5246 | define it here to a reasonable default value. If it is crucial | |
5247 | to have a better value some day simply define this value. */ | |
5248 | # ifndef BFD_TARGET_PAGESIZE | |
5249 | # define BFD_TARGET_PAGESIZE (4096) | |
5250 | # endif | |
5251 | ||
fdc90cb4 JJ |
5252 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5253 | and the chains. */ | |
5254 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
5255 | |
5256 | # if 1 | |
5257 | /* Variant 1: optimize for short chains. We add the squares | |
5258 | of all the chain lengths (which favors many small chain | |
5259 | over a few long chains). */ | |
5260 | for (j = 0; j < i; ++j) | |
5261 | max += counts[j] * counts[j]; | |
5262 | ||
5263 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 5264 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5265 | max *= fact * fact; |
5266 | # else | |
5267 | /* Variant 2: Optimize a lot more for small table. Here we | |
5268 | also add squares of the size but we also add penalties for | |
5269 | empty slots (the +1 term). */ | |
5270 | for (j = 0; j < i; ++j) | |
5271 | max += (1 + counts[j]) * (1 + counts[j]); | |
5272 | ||
5273 | /* The overall size of the table is considered, but not as | |
5274 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 5275 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5276 | max *= fact; |
5277 | # endif | |
5278 | ||
5279 | /* Compare with current best results. */ | |
5280 | if (max < best_chlen) | |
5281 | { | |
5282 | best_chlen = max; | |
5283 | best_size = i; | |
5284 | } | |
5285 | } | |
5286 | ||
5287 | free (counts); | |
5288 | } | |
5289 | else | |
5290 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
5291 | { | |
5292 | /* This is the fallback solution if no 64bit type is available or if we | |
5293 | are not supposed to spend much time on optimizations. We select the | |
5294 | bucket count using a fixed set of numbers. */ | |
5295 | for (i = 0; elf_buckets[i] != 0; i++) | |
5296 | { | |
5297 | best_size = elf_buckets[i]; | |
fdc90cb4 | 5298 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
5299 | break; |
5300 | } | |
fdc90cb4 JJ |
5301 | if (gnu_hash && best_size < 2) |
5302 | best_size = 2; | |
5a580b3a AM |
5303 | } |
5304 | ||
5a580b3a AM |
5305 | return best_size; |
5306 | } | |
5307 | ||
5308 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
5309 | called by the ELF linker emulation before_allocation routine. We | |
5310 | must set the sizes of the sections before the linker sets the | |
5311 | addresses of the various sections. */ | |
5312 | ||
5313 | bfd_boolean | |
5314 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
5315 | const char *soname, | |
5316 | const char *rpath, | |
5317 | const char *filter_shlib, | |
5318 | const char * const *auxiliary_filters, | |
5319 | struct bfd_link_info *info, | |
5320 | asection **sinterpptr, | |
5321 | struct bfd_elf_version_tree *verdefs) | |
5322 | { | |
5323 | bfd_size_type soname_indx; | |
5324 | bfd *dynobj; | |
5325 | const struct elf_backend_data *bed; | |
5326 | struct elf_assign_sym_version_info asvinfo; | |
5327 | ||
5328 | *sinterpptr = NULL; | |
5329 | ||
5330 | soname_indx = (bfd_size_type) -1; | |
5331 | ||
5332 | if (!is_elf_hash_table (info->hash)) | |
5333 | return TRUE; | |
5334 | ||
6bfdb61b | 5335 | bed = get_elf_backend_data (output_bfd); |
8c37241b | 5336 | elf_tdata (output_bfd)->relro = info->relro; |
5a580b3a AM |
5337 | if (info->execstack) |
5338 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
5339 | else if (info->noexecstack) | |
5340 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
5341 | else | |
5342 | { | |
5343 | bfd *inputobj; | |
5344 | asection *notesec = NULL; | |
5345 | int exec = 0; | |
5346 | ||
5347 | for (inputobj = info->input_bfds; | |
5348 | inputobj; | |
5349 | inputobj = inputobj->link_next) | |
5350 | { | |
5351 | asection *s; | |
5352 | ||
d457dcf6 | 5353 | if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED)) |
5a580b3a AM |
5354 | continue; |
5355 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
5356 | if (s) | |
5357 | { | |
5358 | if (s->flags & SEC_CODE) | |
5359 | exec = PF_X; | |
5360 | notesec = s; | |
5361 | } | |
6bfdb61b | 5362 | else if (bed->default_execstack) |
5a580b3a AM |
5363 | exec = PF_X; |
5364 | } | |
5365 | if (notesec) | |
5366 | { | |
5367 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
5368 | if (exec && info->relocatable | |
5369 | && notesec->output_section != bfd_abs_section_ptr) | |
5370 | notesec->output_section->flags |= SEC_CODE; | |
5371 | } | |
5372 | } | |
5373 | ||
5374 | /* Any syms created from now on start with -1 in | |
5375 | got.refcount/offset and plt.refcount/offset. */ | |
a6aa5195 AM |
5376 | elf_hash_table (info)->init_got_refcount |
5377 | = elf_hash_table (info)->init_got_offset; | |
5378 | elf_hash_table (info)->init_plt_refcount | |
5379 | = elf_hash_table (info)->init_plt_offset; | |
5a580b3a AM |
5380 | |
5381 | /* The backend may have to create some sections regardless of whether | |
5382 | we're dynamic or not. */ | |
5a580b3a AM |
5383 | if (bed->elf_backend_always_size_sections |
5384 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
5385 | return FALSE; | |
5386 | ||
eb3d5f3b JB |
5387 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) |
5388 | return FALSE; | |
5389 | ||
5a580b3a AM |
5390 | dynobj = elf_hash_table (info)->dynobj; |
5391 | ||
5392 | /* If there were no dynamic objects in the link, there is nothing to | |
5393 | do here. */ | |
5394 | if (dynobj == NULL) | |
5395 | return TRUE; | |
5396 | ||
5a580b3a AM |
5397 | if (elf_hash_table (info)->dynamic_sections_created) |
5398 | { | |
5399 | struct elf_info_failed eif; | |
5400 | struct elf_link_hash_entry *h; | |
5401 | asection *dynstr; | |
5402 | struct bfd_elf_version_tree *t; | |
5403 | struct bfd_elf_version_expr *d; | |
046183de | 5404 | asection *s; |
5a580b3a AM |
5405 | bfd_boolean all_defined; |
5406 | ||
5407 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
5408 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
5409 | ||
5410 | if (soname != NULL) | |
5411 | { | |
5412 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5413 | soname, TRUE); | |
5414 | if (soname_indx == (bfd_size_type) -1 | |
5415 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
5416 | return FALSE; | |
5417 | } | |
5418 | ||
5419 | if (info->symbolic) | |
5420 | { | |
5421 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
5422 | return FALSE; | |
5423 | info->flags |= DF_SYMBOLIC; | |
5424 | } | |
5425 | ||
5426 | if (rpath != NULL) | |
5427 | { | |
5428 | bfd_size_type indx; | |
5429 | ||
5430 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
5431 | TRUE); | |
5432 | if (indx == (bfd_size_type) -1 | |
5433 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
5434 | return FALSE; | |
5435 | ||
5436 | if (info->new_dtags) | |
5437 | { | |
5438 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
5439 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
5440 | return FALSE; | |
5441 | } | |
5442 | } | |
5443 | ||
5444 | if (filter_shlib != NULL) | |
5445 | { | |
5446 | bfd_size_type indx; | |
5447 | ||
5448 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5449 | filter_shlib, TRUE); | |
5450 | if (indx == (bfd_size_type) -1 | |
5451 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
5452 | return FALSE; | |
5453 | } | |
5454 | ||
5455 | if (auxiliary_filters != NULL) | |
5456 | { | |
5457 | const char * const *p; | |
5458 | ||
5459 | for (p = auxiliary_filters; *p != NULL; p++) | |
5460 | { | |
5461 | bfd_size_type indx; | |
5462 | ||
5463 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5464 | *p, TRUE); | |
5465 | if (indx == (bfd_size_type) -1 | |
5466 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
5467 | return FALSE; | |
5468 | } | |
5469 | } | |
5470 | ||
5471 | eif.info = info; | |
5472 | eif.verdefs = verdefs; | |
5473 | eif.failed = FALSE; | |
5474 | ||
5475 | /* If we are supposed to export all symbols into the dynamic symbol | |
5476 | table (this is not the normal case), then do so. */ | |
55255dae L |
5477 | if (info->export_dynamic |
5478 | || (info->executable && info->dynamic)) | |
5a580b3a AM |
5479 | { |
5480 | elf_link_hash_traverse (elf_hash_table (info), | |
5481 | _bfd_elf_export_symbol, | |
5482 | &eif); | |
5483 | if (eif.failed) | |
5484 | return FALSE; | |
5485 | } | |
5486 | ||
5487 | /* Make all global versions with definition. */ | |
5488 | for (t = verdefs; t != NULL; t = t->next) | |
5489 | for (d = t->globals.list; d != NULL; d = d->next) | |
5490 | if (!d->symver && d->symbol) | |
5491 | { | |
5492 | const char *verstr, *name; | |
5493 | size_t namelen, verlen, newlen; | |
5494 | char *newname, *p; | |
5495 | struct elf_link_hash_entry *newh; | |
5496 | ||
5497 | name = d->symbol; | |
5498 | namelen = strlen (name); | |
5499 | verstr = t->name; | |
5500 | verlen = strlen (verstr); | |
5501 | newlen = namelen + verlen + 3; | |
5502 | ||
5503 | newname = bfd_malloc (newlen); | |
5504 | if (newname == NULL) | |
5505 | return FALSE; | |
5506 | memcpy (newname, name, namelen); | |
5507 | ||
5508 | /* Check the hidden versioned definition. */ | |
5509 | p = newname + namelen; | |
5510 | *p++ = ELF_VER_CHR; | |
5511 | memcpy (p, verstr, verlen + 1); | |
5512 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5513 | newname, FALSE, FALSE, | |
5514 | FALSE); | |
5515 | if (newh == NULL | |
5516 | || (newh->root.type != bfd_link_hash_defined | |
5517 | && newh->root.type != bfd_link_hash_defweak)) | |
5518 | { | |
5519 | /* Check the default versioned definition. */ | |
5520 | *p++ = ELF_VER_CHR; | |
5521 | memcpy (p, verstr, verlen + 1); | |
5522 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5523 | newname, FALSE, FALSE, | |
5524 | FALSE); | |
5525 | } | |
5526 | free (newname); | |
5527 | ||
5528 | /* Mark this version if there is a definition and it is | |
5529 | not defined in a shared object. */ | |
5530 | if (newh != NULL | |
f5385ebf | 5531 | && !newh->def_dynamic |
5a580b3a AM |
5532 | && (newh->root.type == bfd_link_hash_defined |
5533 | || newh->root.type == bfd_link_hash_defweak)) | |
5534 | d->symver = 1; | |
5535 | } | |
5536 | ||
5537 | /* Attach all the symbols to their version information. */ | |
5538 | asvinfo.output_bfd = output_bfd; | |
5539 | asvinfo.info = info; | |
5540 | asvinfo.verdefs = verdefs; | |
5541 | asvinfo.failed = FALSE; | |
5542 | ||
5543 | elf_link_hash_traverse (elf_hash_table (info), | |
5544 | _bfd_elf_link_assign_sym_version, | |
5545 | &asvinfo); | |
5546 | if (asvinfo.failed) | |
5547 | return FALSE; | |
5548 | ||
5549 | if (!info->allow_undefined_version) | |
5550 | { | |
5551 | /* Check if all global versions have a definition. */ | |
5552 | all_defined = TRUE; | |
5553 | for (t = verdefs; t != NULL; t = t->next) | |
5554 | for (d = t->globals.list; d != NULL; d = d->next) | |
5555 | if (!d->symver && !d->script) | |
5556 | { | |
5557 | (*_bfd_error_handler) | |
5558 | (_("%s: undefined version: %s"), | |
5559 | d->pattern, t->name); | |
5560 | all_defined = FALSE; | |
5561 | } | |
5562 | ||
5563 | if (!all_defined) | |
5564 | { | |
5565 | bfd_set_error (bfd_error_bad_value); | |
5566 | return FALSE; | |
5567 | } | |
5568 | } | |
5569 | ||
5570 | /* Find all symbols which were defined in a dynamic object and make | |
5571 | the backend pick a reasonable value for them. */ | |
5572 | elf_link_hash_traverse (elf_hash_table (info), | |
5573 | _bfd_elf_adjust_dynamic_symbol, | |
5574 | &eif); | |
5575 | if (eif.failed) | |
5576 | return FALSE; | |
5577 | ||
5578 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 5579 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
5580 | now so that we know the final size of the .dynamic section. */ |
5581 | ||
5582 | /* If there are initialization and/or finalization functions to | |
5583 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
5584 | h = (info->init_function | |
5585 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5586 | info->init_function, FALSE, | |
5587 | FALSE, FALSE) | |
5588 | : NULL); | |
5589 | if (h != NULL | |
f5385ebf AM |
5590 | && (h->ref_regular |
5591 | || h->def_regular)) | |
5a580b3a AM |
5592 | { |
5593 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
5594 | return FALSE; | |
5595 | } | |
5596 | h = (info->fini_function | |
5597 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5598 | info->fini_function, FALSE, | |
5599 | FALSE, FALSE) | |
5600 | : NULL); | |
5601 | if (h != NULL | |
f5385ebf AM |
5602 | && (h->ref_regular |
5603 | || h->def_regular)) | |
5a580b3a AM |
5604 | { |
5605 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
5606 | return FALSE; | |
5607 | } | |
5608 | ||
046183de AM |
5609 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); |
5610 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5611 | { |
5612 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
5613 | if (! info->executable) | |
5614 | { | |
5615 | bfd *sub; | |
5616 | asection *o; | |
5617 | ||
5618 | for (sub = info->input_bfds; sub != NULL; | |
5619 | sub = sub->link_next) | |
3fcd97f1 JJ |
5620 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour) |
5621 | for (o = sub->sections; o != NULL; o = o->next) | |
5622 | if (elf_section_data (o)->this_hdr.sh_type | |
5623 | == SHT_PREINIT_ARRAY) | |
5624 | { | |
5625 | (*_bfd_error_handler) | |
5626 | (_("%B: .preinit_array section is not allowed in DSO"), | |
5627 | sub); | |
5628 | break; | |
5629 | } | |
5a580b3a AM |
5630 | |
5631 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5632 | return FALSE; | |
5633 | } | |
5634 | ||
5635 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
5636 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
5637 | return FALSE; | |
5638 | } | |
046183de AM |
5639 | s = bfd_get_section_by_name (output_bfd, ".init_array"); |
5640 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5641 | { |
5642 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
5643 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
5644 | return FALSE; | |
5645 | } | |
046183de AM |
5646 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); |
5647 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5648 | { |
5649 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
5650 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
5651 | return FALSE; | |
5652 | } | |
5653 | ||
5654 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5655 | /* If .dynstr is excluded from the link, we don't want any of | |
5656 | these tags. Strictly, we should be checking each section | |
5657 | individually; This quick check covers for the case where | |
5658 | someone does a /DISCARD/ : { *(*) }. */ | |
5659 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
5660 | { | |
5661 | bfd_size_type strsize; | |
5662 | ||
5663 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
fdc90cb4 JJ |
5664 | if ((info->emit_hash |
5665 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
5666 | || (info->emit_gnu_hash | |
5667 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
5a580b3a AM |
5668 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) |
5669 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
5670 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
5671 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
5672 | bed->s->sizeof_sym)) | |
5673 | return FALSE; | |
5674 | } | |
5675 | } | |
5676 | ||
5677 | /* The backend must work out the sizes of all the other dynamic | |
5678 | sections. */ | |
5679 | if (bed->elf_backend_size_dynamic_sections | |
5680 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
5681 | return FALSE; | |
5682 | ||
5683 | if (elf_hash_table (info)->dynamic_sections_created) | |
5684 | { | |
554220db | 5685 | unsigned long section_sym_count; |
5a580b3a | 5686 | asection *s; |
5a580b3a AM |
5687 | |
5688 | /* Set up the version definition section. */ | |
5689 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
5690 | BFD_ASSERT (s != NULL); | |
5691 | ||
5692 | /* We may have created additional version definitions if we are | |
5693 | just linking a regular application. */ | |
5694 | verdefs = asvinfo.verdefs; | |
5695 | ||
5696 | /* Skip anonymous version tag. */ | |
5697 | if (verdefs != NULL && verdefs->vernum == 0) | |
5698 | verdefs = verdefs->next; | |
5699 | ||
3e3b46e5 | 5700 | if (verdefs == NULL && !info->create_default_symver) |
8423293d | 5701 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5702 | else |
5703 | { | |
5704 | unsigned int cdefs; | |
5705 | bfd_size_type size; | |
5706 | struct bfd_elf_version_tree *t; | |
5707 | bfd_byte *p; | |
5708 | Elf_Internal_Verdef def; | |
5709 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
5710 | struct bfd_link_hash_entry *bh; |
5711 | struct elf_link_hash_entry *h; | |
5712 | const char *name; | |
5a580b3a AM |
5713 | |
5714 | cdefs = 0; | |
5715 | size = 0; | |
5716 | ||
5717 | /* Make space for the base version. */ | |
5718 | size += sizeof (Elf_External_Verdef); | |
5719 | size += sizeof (Elf_External_Verdaux); | |
5720 | ++cdefs; | |
5721 | ||
3e3b46e5 PB |
5722 | /* Make space for the default version. */ |
5723 | if (info->create_default_symver) | |
5724 | { | |
5725 | size += sizeof (Elf_External_Verdef); | |
5726 | ++cdefs; | |
5727 | } | |
5728 | ||
5a580b3a AM |
5729 | for (t = verdefs; t != NULL; t = t->next) |
5730 | { | |
5731 | struct bfd_elf_version_deps *n; | |
5732 | ||
5733 | size += sizeof (Elf_External_Verdef); | |
5734 | size += sizeof (Elf_External_Verdaux); | |
5735 | ++cdefs; | |
5736 | ||
5737 | for (n = t->deps; n != NULL; n = n->next) | |
5738 | size += sizeof (Elf_External_Verdaux); | |
5739 | } | |
5740 | ||
eea6121a AM |
5741 | s->size = size; |
5742 | s->contents = bfd_alloc (output_bfd, s->size); | |
5743 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5744 | return FALSE; |
5745 | ||
5746 | /* Fill in the version definition section. */ | |
5747 | ||
5748 | p = s->contents; | |
5749 | ||
5750 | def.vd_version = VER_DEF_CURRENT; | |
5751 | def.vd_flags = VER_FLG_BASE; | |
5752 | def.vd_ndx = 1; | |
5753 | def.vd_cnt = 1; | |
3e3b46e5 PB |
5754 | if (info->create_default_symver) |
5755 | { | |
5756 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
5757 | def.vd_next = sizeof (Elf_External_Verdef); | |
5758 | } | |
5759 | else | |
5760 | { | |
5761 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5762 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5763 | + sizeof (Elf_External_Verdaux)); | |
5764 | } | |
5a580b3a AM |
5765 | |
5766 | if (soname_indx != (bfd_size_type) -1) | |
5767 | { | |
5768 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5769 | soname_indx); | |
5770 | def.vd_hash = bfd_elf_hash (soname); | |
5771 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 5772 | name = soname; |
5a580b3a AM |
5773 | } |
5774 | else | |
5775 | { | |
5a580b3a AM |
5776 | bfd_size_type indx; |
5777 | ||
06084812 | 5778 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
5779 | def.vd_hash = bfd_elf_hash (name); |
5780 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5781 | name, FALSE); | |
5782 | if (indx == (bfd_size_type) -1) | |
5783 | return FALSE; | |
5784 | defaux.vda_name = indx; | |
5785 | } | |
5786 | defaux.vda_next = 0; | |
5787 | ||
5788 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5789 | (Elf_External_Verdef *) p); | |
5790 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
5791 | if (info->create_default_symver) |
5792 | { | |
5793 | /* Add a symbol representing this version. */ | |
5794 | bh = NULL; | |
5795 | if (! (_bfd_generic_link_add_one_symbol | |
5796 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5797 | 0, NULL, FALSE, | |
5798 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5799 | return FALSE; | |
5800 | h = (struct elf_link_hash_entry *) bh; | |
5801 | h->non_elf = 0; | |
5802 | h->def_regular = 1; | |
5803 | h->type = STT_OBJECT; | |
5804 | h->verinfo.vertree = NULL; | |
5805 | ||
5806 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
5807 | return FALSE; | |
5808 | ||
5809 | /* Create a duplicate of the base version with the same | |
5810 | aux block, but different flags. */ | |
5811 | def.vd_flags = 0; | |
5812 | def.vd_ndx = 2; | |
5813 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5814 | if (verdefs) | |
5815 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5816 | + sizeof (Elf_External_Verdaux)); | |
5817 | else | |
5818 | def.vd_next = 0; | |
5819 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5820 | (Elf_External_Verdef *) p); | |
5821 | p += sizeof (Elf_External_Verdef); | |
5822 | } | |
5a580b3a AM |
5823 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
5824 | (Elf_External_Verdaux *) p); | |
5825 | p += sizeof (Elf_External_Verdaux); | |
5826 | ||
5827 | for (t = verdefs; t != NULL; t = t->next) | |
5828 | { | |
5829 | unsigned int cdeps; | |
5830 | struct bfd_elf_version_deps *n; | |
5a580b3a AM |
5831 | |
5832 | cdeps = 0; | |
5833 | for (n = t->deps; n != NULL; n = n->next) | |
5834 | ++cdeps; | |
5835 | ||
5836 | /* Add a symbol representing this version. */ | |
5837 | bh = NULL; | |
5838 | if (! (_bfd_generic_link_add_one_symbol | |
5839 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5840 | 0, NULL, FALSE, | |
5841 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5842 | return FALSE; | |
5843 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
5844 | h->non_elf = 0; |
5845 | h->def_regular = 1; | |
5a580b3a AM |
5846 | h->type = STT_OBJECT; |
5847 | h->verinfo.vertree = t; | |
5848 | ||
c152c796 | 5849 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
5850 | return FALSE; |
5851 | ||
5852 | def.vd_version = VER_DEF_CURRENT; | |
5853 | def.vd_flags = 0; | |
5854 | if (t->globals.list == NULL | |
5855 | && t->locals.list == NULL | |
5856 | && ! t->used) | |
5857 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 5858 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
5859 | def.vd_cnt = cdeps + 1; |
5860 | def.vd_hash = bfd_elf_hash (t->name); | |
5861 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5862 | def.vd_next = 0; | |
5863 | if (t->next != NULL) | |
5864 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5865 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
5866 | ||
5867 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5868 | (Elf_External_Verdef *) p); | |
5869 | p += sizeof (Elf_External_Verdef); | |
5870 | ||
5871 | defaux.vda_name = h->dynstr_index; | |
5872 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5873 | h->dynstr_index); | |
5874 | defaux.vda_next = 0; | |
5875 | if (t->deps != NULL) | |
5876 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5877 | t->name_indx = defaux.vda_name; | |
5878 | ||
5879 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5880 | (Elf_External_Verdaux *) p); | |
5881 | p += sizeof (Elf_External_Verdaux); | |
5882 | ||
5883 | for (n = t->deps; n != NULL; n = n->next) | |
5884 | { | |
5885 | if (n->version_needed == NULL) | |
5886 | { | |
5887 | /* This can happen if there was an error in the | |
5888 | version script. */ | |
5889 | defaux.vda_name = 0; | |
5890 | } | |
5891 | else | |
5892 | { | |
5893 | defaux.vda_name = n->version_needed->name_indx; | |
5894 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5895 | defaux.vda_name); | |
5896 | } | |
5897 | if (n->next == NULL) | |
5898 | defaux.vda_next = 0; | |
5899 | else | |
5900 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5901 | ||
5902 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5903 | (Elf_External_Verdaux *) p); | |
5904 | p += sizeof (Elf_External_Verdaux); | |
5905 | } | |
5906 | } | |
5907 | ||
5908 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
5909 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
5910 | return FALSE; | |
5911 | ||
5912 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
5913 | } | |
5914 | ||
5915 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
5916 | { | |
5917 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
5918 | return FALSE; | |
5919 | } | |
5920 | else if (info->flags & DF_BIND_NOW) | |
5921 | { | |
5922 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
5923 | return FALSE; | |
5924 | } | |
5925 | ||
5926 | if (info->flags_1) | |
5927 | { | |
5928 | if (info->executable) | |
5929 | info->flags_1 &= ~ (DF_1_INITFIRST | |
5930 | | DF_1_NODELETE | |
5931 | | DF_1_NOOPEN); | |
5932 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
5933 | return FALSE; | |
5934 | } | |
5935 | ||
5936 | /* Work out the size of the version reference section. */ | |
5937 | ||
5938 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
5939 | BFD_ASSERT (s != NULL); | |
5940 | { | |
5941 | struct elf_find_verdep_info sinfo; | |
5942 | ||
5943 | sinfo.output_bfd = output_bfd; | |
5944 | sinfo.info = info; | |
5945 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
5946 | if (sinfo.vers == 0) | |
5947 | sinfo.vers = 1; | |
5948 | sinfo.failed = FALSE; | |
5949 | ||
5950 | elf_link_hash_traverse (elf_hash_table (info), | |
5951 | _bfd_elf_link_find_version_dependencies, | |
5952 | &sinfo); | |
5953 | ||
5954 | if (elf_tdata (output_bfd)->verref == NULL) | |
8423293d | 5955 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5956 | else |
5957 | { | |
5958 | Elf_Internal_Verneed *t; | |
5959 | unsigned int size; | |
5960 | unsigned int crefs; | |
5961 | bfd_byte *p; | |
5962 | ||
5963 | /* Build the version definition section. */ | |
5964 | size = 0; | |
5965 | crefs = 0; | |
5966 | for (t = elf_tdata (output_bfd)->verref; | |
5967 | t != NULL; | |
5968 | t = t->vn_nextref) | |
5969 | { | |
5970 | Elf_Internal_Vernaux *a; | |
5971 | ||
5972 | size += sizeof (Elf_External_Verneed); | |
5973 | ++crefs; | |
5974 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5975 | size += sizeof (Elf_External_Vernaux); | |
5976 | } | |
5977 | ||
eea6121a AM |
5978 | s->size = size; |
5979 | s->contents = bfd_alloc (output_bfd, s->size); | |
5a580b3a AM |
5980 | if (s->contents == NULL) |
5981 | return FALSE; | |
5982 | ||
5983 | p = s->contents; | |
5984 | for (t = elf_tdata (output_bfd)->verref; | |
5985 | t != NULL; | |
5986 | t = t->vn_nextref) | |
5987 | { | |
5988 | unsigned int caux; | |
5989 | Elf_Internal_Vernaux *a; | |
5990 | bfd_size_type indx; | |
5991 | ||
5992 | caux = 0; | |
5993 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5994 | ++caux; | |
5995 | ||
5996 | t->vn_version = VER_NEED_CURRENT; | |
5997 | t->vn_cnt = caux; | |
5998 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5999 | elf_dt_name (t->vn_bfd) != NULL | |
6000 | ? elf_dt_name (t->vn_bfd) | |
06084812 | 6001 | : lbasename (t->vn_bfd->filename), |
5a580b3a AM |
6002 | FALSE); |
6003 | if (indx == (bfd_size_type) -1) | |
6004 | return FALSE; | |
6005 | t->vn_file = indx; | |
6006 | t->vn_aux = sizeof (Elf_External_Verneed); | |
6007 | if (t->vn_nextref == NULL) | |
6008 | t->vn_next = 0; | |
6009 | else | |
6010 | t->vn_next = (sizeof (Elf_External_Verneed) | |
6011 | + caux * sizeof (Elf_External_Vernaux)); | |
6012 | ||
6013 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
6014 | (Elf_External_Verneed *) p); | |
6015 | p += sizeof (Elf_External_Verneed); | |
6016 | ||
6017 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6018 | { | |
6019 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6020 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6021 | a->vna_nodename, FALSE); | |
6022 | if (indx == (bfd_size_type) -1) | |
6023 | return FALSE; | |
6024 | a->vna_name = indx; | |
6025 | if (a->vna_nextptr == NULL) | |
6026 | a->vna_next = 0; | |
6027 | else | |
6028 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6029 | ||
6030 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6031 | (Elf_External_Vernaux *) p); | |
6032 | p += sizeof (Elf_External_Vernaux); | |
6033 | } | |
6034 | } | |
6035 | ||
6036 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
6037 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
6038 | return FALSE; | |
6039 | ||
6040 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6041 | } | |
6042 | } | |
6043 | ||
8423293d AM |
6044 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
6045 | && elf_tdata (output_bfd)->cverdefs == 0) | |
6046 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
6047 | §ion_sym_count) == 0) | |
6048 | { | |
6049 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
6050 | s->flags |= SEC_EXCLUDE; | |
6051 | } | |
6052 | } | |
6053 | return TRUE; | |
6054 | } | |
6055 | ||
74541ad4 AM |
6056 | /* Find the first non-excluded output section. We'll use its |
6057 | section symbol for some emitted relocs. */ | |
6058 | void | |
6059 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
6060 | { | |
6061 | asection *s; | |
6062 | ||
6063 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6064 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
6065 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6066 | { | |
6067 | elf_hash_table (info)->text_index_section = s; | |
6068 | break; | |
6069 | } | |
6070 | } | |
6071 | ||
6072 | /* Find two non-excluded output sections, one for code, one for data. | |
6073 | We'll use their section symbols for some emitted relocs. */ | |
6074 | void | |
6075 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
6076 | { | |
6077 | asection *s; | |
6078 | ||
6079 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6080 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) | |
6081 | == (SEC_ALLOC | SEC_READONLY)) | |
6082 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6083 | { | |
6084 | elf_hash_table (info)->text_index_section = s; | |
6085 | break; | |
6086 | } | |
6087 | ||
6088 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6089 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) | |
6090 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6091 | { | |
6092 | elf_hash_table (info)->data_index_section = s; | |
6093 | break; | |
6094 | } | |
6095 | ||
6096 | if (elf_hash_table (info)->text_index_section == NULL) | |
6097 | elf_hash_table (info)->text_index_section | |
6098 | = elf_hash_table (info)->data_index_section; | |
6099 | } | |
6100 | ||
8423293d AM |
6101 | bfd_boolean |
6102 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
6103 | { | |
74541ad4 AM |
6104 | const struct elf_backend_data *bed; |
6105 | ||
8423293d AM |
6106 | if (!is_elf_hash_table (info->hash)) |
6107 | return TRUE; | |
6108 | ||
74541ad4 AM |
6109 | bed = get_elf_backend_data (output_bfd); |
6110 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
6111 | ||
8423293d AM |
6112 | if (elf_hash_table (info)->dynamic_sections_created) |
6113 | { | |
6114 | bfd *dynobj; | |
8423293d AM |
6115 | asection *s; |
6116 | bfd_size_type dynsymcount; | |
6117 | unsigned long section_sym_count; | |
8423293d AM |
6118 | unsigned int dtagcount; |
6119 | ||
6120 | dynobj = elf_hash_table (info)->dynobj; | |
6121 | ||
5a580b3a AM |
6122 | /* Assign dynsym indicies. In a shared library we generate a |
6123 | section symbol for each output section, which come first. | |
6124 | Next come all of the back-end allocated local dynamic syms, | |
6125 | followed by the rest of the global symbols. */ | |
6126 | ||
554220db AM |
6127 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, |
6128 | §ion_sym_count); | |
5a580b3a AM |
6129 | |
6130 | /* Work out the size of the symbol version section. */ | |
6131 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
6132 | BFD_ASSERT (s != NULL); | |
8423293d AM |
6133 | if (dynsymcount != 0 |
6134 | && (s->flags & SEC_EXCLUDE) == 0) | |
5a580b3a | 6135 | { |
eea6121a AM |
6136 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
6137 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
6138 | if (s->contents == NULL) |
6139 | return FALSE; | |
6140 | ||
6141 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
6142 | return FALSE; | |
6143 | } | |
6144 | ||
6145 | /* Set the size of the .dynsym and .hash sections. We counted | |
6146 | the number of dynamic symbols in elf_link_add_object_symbols. | |
6147 | We will build the contents of .dynsym and .hash when we build | |
6148 | the final symbol table, because until then we do not know the | |
6149 | correct value to give the symbols. We built the .dynstr | |
6150 | section as we went along in elf_link_add_object_symbols. */ | |
6151 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
6152 | BFD_ASSERT (s != NULL); | |
eea6121a | 6153 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a AM |
6154 | |
6155 | if (dynsymcount != 0) | |
6156 | { | |
554220db AM |
6157 | s->contents = bfd_alloc (output_bfd, s->size); |
6158 | if (s->contents == NULL) | |
6159 | return FALSE; | |
5a580b3a | 6160 | |
554220db AM |
6161 | /* The first entry in .dynsym is a dummy symbol. |
6162 | Clear all the section syms, in case we don't output them all. */ | |
6163 | ++section_sym_count; | |
6164 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a AM |
6165 | } |
6166 | ||
fdc90cb4 JJ |
6167 | elf_hash_table (info)->bucketcount = 0; |
6168 | ||
5a580b3a AM |
6169 | /* Compute the size of the hashing table. As a side effect this |
6170 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
6171 | if (info->emit_hash) |
6172 | { | |
6173 | unsigned long int *hashcodes; | |
6174 | unsigned long int *hashcodesp; | |
6175 | bfd_size_type amt; | |
6176 | unsigned long int nsyms; | |
6177 | size_t bucketcount; | |
6178 | size_t hash_entry_size; | |
6179 | ||
6180 | /* Compute the hash values for all exported symbols. At the same | |
6181 | time store the values in an array so that we could use them for | |
6182 | optimizations. */ | |
6183 | amt = dynsymcount * sizeof (unsigned long int); | |
6184 | hashcodes = bfd_malloc (amt); | |
6185 | if (hashcodes == NULL) | |
6186 | return FALSE; | |
6187 | hashcodesp = hashcodes; | |
5a580b3a | 6188 | |
fdc90cb4 JJ |
6189 | /* Put all hash values in HASHCODES. */ |
6190 | elf_link_hash_traverse (elf_hash_table (info), | |
6191 | elf_collect_hash_codes, &hashcodesp); | |
5a580b3a | 6192 | |
fdc90cb4 JJ |
6193 | nsyms = hashcodesp - hashcodes; |
6194 | bucketcount | |
6195 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
6196 | free (hashcodes); | |
6197 | ||
6198 | if (bucketcount == 0) | |
6199 | return FALSE; | |
5a580b3a | 6200 | |
fdc90cb4 JJ |
6201 | elf_hash_table (info)->bucketcount = bucketcount; |
6202 | ||
6203 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
6204 | BFD_ASSERT (s != NULL); | |
6205 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
6206 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
6207 | s->contents = bfd_zalloc (output_bfd, s->size); | |
6208 | if (s->contents == NULL) | |
6209 | return FALSE; | |
6210 | ||
6211 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
6212 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
6213 | s->contents + hash_entry_size); | |
6214 | } | |
6215 | ||
6216 | if (info->emit_gnu_hash) | |
6217 | { | |
6218 | size_t i, cnt; | |
6219 | unsigned char *contents; | |
6220 | struct collect_gnu_hash_codes cinfo; | |
6221 | bfd_size_type amt; | |
6222 | size_t bucketcount; | |
6223 | ||
6224 | memset (&cinfo, 0, sizeof (cinfo)); | |
6225 | ||
6226 | /* Compute the hash values for all exported symbols. At the same | |
6227 | time store the values in an array so that we could use them for | |
6228 | optimizations. */ | |
6229 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
6230 | cinfo.hashcodes = bfd_malloc (amt); | |
6231 | if (cinfo.hashcodes == NULL) | |
6232 | return FALSE; | |
6233 | ||
6234 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
6235 | cinfo.min_dynindx = -1; | |
6236 | cinfo.output_bfd = output_bfd; | |
6237 | cinfo.bed = bed; | |
6238 | ||
6239 | /* Put all hash values in HASHCODES. */ | |
6240 | elf_link_hash_traverse (elf_hash_table (info), | |
6241 | elf_collect_gnu_hash_codes, &cinfo); | |
6242 | ||
6243 | bucketcount | |
6244 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
6245 | ||
6246 | if (bucketcount == 0) | |
6247 | { | |
6248 | free (cinfo.hashcodes); | |
6249 | return FALSE; | |
6250 | } | |
6251 | ||
6252 | s = bfd_get_section_by_name (dynobj, ".gnu.hash"); | |
6253 | BFD_ASSERT (s != NULL); | |
6254 | ||
6255 | if (cinfo.nsyms == 0) | |
6256 | { | |
6257 | /* Empty .gnu.hash section is special. */ | |
6258 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
6259 | free (cinfo.hashcodes); | |
6260 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
6261 | contents = bfd_zalloc (output_bfd, s->size); | |
6262 | if (contents == NULL) | |
6263 | return FALSE; | |
6264 | s->contents = contents; | |
6265 | /* 1 empty bucket. */ | |
6266 | bfd_put_32 (output_bfd, 1, contents); | |
6267 | /* SYMIDX above the special symbol 0. */ | |
6268 | bfd_put_32 (output_bfd, 1, contents + 4); | |
6269 | /* Just one word for bitmask. */ | |
6270 | bfd_put_32 (output_bfd, 1, contents + 8); | |
6271 | /* Only hash fn bloom filter. */ | |
6272 | bfd_put_32 (output_bfd, 0, contents + 12); | |
6273 | /* No hashes are valid - empty bitmask. */ | |
6274 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
6275 | /* No hashes in the only bucket. */ | |
6276 | bfd_put_32 (output_bfd, 0, | |
6277 | contents + 16 + bed->s->arch_size / 8); | |
6278 | } | |
6279 | else | |
6280 | { | |
fdc90cb4 | 6281 | unsigned long int maskwords, maskbitslog2; |
0b33793d | 6282 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 JJ |
6283 | |
6284 | maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1; | |
6285 | if (maskbitslog2 < 3) | |
6286 | maskbitslog2 = 5; | |
6287 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
6288 | maskbitslog2 = maskbitslog2 + 3; | |
6289 | else | |
6290 | maskbitslog2 = maskbitslog2 + 2; | |
6291 | if (bed->s->arch_size == 64) | |
6292 | { | |
6293 | if (maskbitslog2 == 5) | |
6294 | maskbitslog2 = 6; | |
6295 | cinfo.shift1 = 6; | |
6296 | } | |
6297 | else | |
6298 | cinfo.shift1 = 5; | |
6299 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 6300 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
6301 | cinfo.maskbits = 1 << maskbitslog2; |
6302 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
6303 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
6304 | amt += maskwords * sizeof (bfd_vma); | |
6305 | cinfo.bitmask = bfd_malloc (amt); | |
6306 | if (cinfo.bitmask == NULL) | |
6307 | { | |
6308 | free (cinfo.hashcodes); | |
6309 | return FALSE; | |
6310 | } | |
6311 | ||
6312 | cinfo.counts = (void *) (cinfo.bitmask + maskwords); | |
6313 | cinfo.indx = cinfo.counts + bucketcount; | |
6314 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
6315 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
6316 | ||
6317 | /* Determine how often each hash bucket is used. */ | |
6318 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
6319 | for (i = 0; i < cinfo.nsyms; ++i) | |
6320 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
6321 | ||
6322 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
6323 | if (cinfo.counts[i] != 0) | |
6324 | { | |
6325 | cinfo.indx[i] = cnt; | |
6326 | cnt += cinfo.counts[i]; | |
6327 | } | |
6328 | BFD_ASSERT (cnt == dynsymcount); | |
6329 | cinfo.bucketcount = bucketcount; | |
6330 | cinfo.local_indx = cinfo.min_dynindx; | |
6331 | ||
6332 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
6333 | s->size += cinfo.maskbits / 8; | |
6334 | contents = bfd_zalloc (output_bfd, s->size); | |
6335 | if (contents == NULL) | |
6336 | { | |
6337 | free (cinfo.bitmask); | |
6338 | free (cinfo.hashcodes); | |
6339 | return FALSE; | |
6340 | } | |
6341 | ||
6342 | s->contents = contents; | |
6343 | bfd_put_32 (output_bfd, bucketcount, contents); | |
6344 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
6345 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
6346 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
6347 | contents += 16 + cinfo.maskbits / 8; | |
6348 | ||
6349 | for (i = 0; i < bucketcount; ++i) | |
6350 | { | |
6351 | if (cinfo.counts[i] == 0) | |
6352 | bfd_put_32 (output_bfd, 0, contents); | |
6353 | else | |
6354 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
6355 | contents += 4; | |
6356 | } | |
6357 | ||
6358 | cinfo.contents = contents; | |
6359 | ||
6360 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
6361 | elf_link_hash_traverse (elf_hash_table (info), | |
6362 | elf_renumber_gnu_hash_syms, &cinfo); | |
6363 | ||
6364 | contents = s->contents + 16; | |
6365 | for (i = 0; i < maskwords; ++i) | |
6366 | { | |
6367 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
6368 | contents); | |
6369 | contents += bed->s->arch_size / 8; | |
6370 | } | |
6371 | ||
6372 | free (cinfo.bitmask); | |
6373 | free (cinfo.hashcodes); | |
6374 | } | |
6375 | } | |
5a580b3a AM |
6376 | |
6377 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
6378 | BFD_ASSERT (s != NULL); | |
6379 | ||
4ad4eba5 | 6380 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 6381 | |
eea6121a | 6382 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
6383 | |
6384 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
6385 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
6386 | return FALSE; | |
6387 | } | |
6388 | ||
6389 | return TRUE; | |
6390 | } | |
c152c796 AM |
6391 | |
6392 | /* Final phase of ELF linker. */ | |
6393 | ||
6394 | /* A structure we use to avoid passing large numbers of arguments. */ | |
6395 | ||
6396 | struct elf_final_link_info | |
6397 | { | |
6398 | /* General link information. */ | |
6399 | struct bfd_link_info *info; | |
6400 | /* Output BFD. */ | |
6401 | bfd *output_bfd; | |
6402 | /* Symbol string table. */ | |
6403 | struct bfd_strtab_hash *symstrtab; | |
6404 | /* .dynsym section. */ | |
6405 | asection *dynsym_sec; | |
6406 | /* .hash section. */ | |
6407 | asection *hash_sec; | |
6408 | /* symbol version section (.gnu.version). */ | |
6409 | asection *symver_sec; | |
6410 | /* Buffer large enough to hold contents of any section. */ | |
6411 | bfd_byte *contents; | |
6412 | /* Buffer large enough to hold external relocs of any section. */ | |
6413 | void *external_relocs; | |
6414 | /* Buffer large enough to hold internal relocs of any section. */ | |
6415 | Elf_Internal_Rela *internal_relocs; | |
6416 | /* Buffer large enough to hold external local symbols of any input | |
6417 | BFD. */ | |
6418 | bfd_byte *external_syms; | |
6419 | /* And a buffer for symbol section indices. */ | |
6420 | Elf_External_Sym_Shndx *locsym_shndx; | |
6421 | /* Buffer large enough to hold internal local symbols of any input | |
6422 | BFD. */ | |
6423 | Elf_Internal_Sym *internal_syms; | |
6424 | /* Array large enough to hold a symbol index for each local symbol | |
6425 | of any input BFD. */ | |
6426 | long *indices; | |
6427 | /* Array large enough to hold a section pointer for each local | |
6428 | symbol of any input BFD. */ | |
6429 | asection **sections; | |
6430 | /* Buffer to hold swapped out symbols. */ | |
6431 | bfd_byte *symbuf; | |
6432 | /* And one for symbol section indices. */ | |
6433 | Elf_External_Sym_Shndx *symshndxbuf; | |
6434 | /* Number of swapped out symbols in buffer. */ | |
6435 | size_t symbuf_count; | |
6436 | /* Number of symbols which fit in symbuf. */ | |
6437 | size_t symbuf_size; | |
6438 | /* And same for symshndxbuf. */ | |
6439 | size_t shndxbuf_size; | |
6440 | }; | |
6441 | ||
6442 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
6443 | ||
6444 | struct elf_outext_info | |
6445 | { | |
6446 | bfd_boolean failed; | |
6447 | bfd_boolean localsyms; | |
6448 | struct elf_final_link_info *finfo; | |
6449 | }; | |
6450 | ||
d9352518 DB |
6451 | |
6452 | /* Support for evaluating a complex relocation. | |
6453 | ||
6454 | Complex relocations are generalized, self-describing relocations. The | |
6455 | implementation of them consists of two parts: complex symbols, and the | |
6456 | relocations themselves. | |
6457 | ||
6458 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
6459 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
6460 | information (start bit, end bit, word width, etc) into the addend. This | |
6461 | information is extracted from CGEN-generated operand tables within gas. | |
6462 | ||
6463 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
6464 | internal) representing prefix-notation expressions, including but not | |
6465 | limited to those sorts of expressions normally encoded as addends in the | |
6466 | addend field. The symbol mangling format is: | |
6467 | ||
6468 | <node> := <literal> | |
6469 | | <unary-operator> ':' <node> | |
6470 | | <binary-operator> ':' <node> ':' <node> | |
6471 | ; | |
6472 | ||
6473 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
6474 | | 'S' <digits=N> ':' <N character section name> | |
6475 | | '#' <hexdigits> | |
6476 | ; | |
6477 | ||
6478 | <binary-operator> := as in C | |
6479 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
6480 | ||
6481 | static void | |
6482 | set_symbol_value (bfd * bfd_with_globals, | |
6483 | struct elf_final_link_info * finfo, | |
6484 | int symidx, | |
6485 | bfd_vma val) | |
6486 | { | |
6487 | bfd_boolean is_local; | |
6488 | Elf_Internal_Sym * sym; | |
6489 | struct elf_link_hash_entry ** sym_hashes; | |
6490 | struct elf_link_hash_entry * h; | |
6491 | ||
6492 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
6493 | sym = finfo->internal_syms + symidx; | |
6494 | is_local = ELF_ST_BIND(sym->st_info) == STB_LOCAL; | |
6495 | ||
6496 | if (is_local) | |
6497 | { | |
6498 | /* It is a local symbol: move it to the | |
6499 | "absolute" section and give it a value. */ | |
6500 | sym->st_shndx = SHN_ABS; | |
6501 | sym->st_value = val; | |
6502 | } | |
6503 | else | |
6504 | { | |
6505 | /* It is a global symbol: set its link type | |
6506 | to "defined" and give it a value. */ | |
6507 | h = sym_hashes [symidx]; | |
6508 | while (h->root.type == bfd_link_hash_indirect | |
6509 | || h->root.type == bfd_link_hash_warning) | |
6510 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6511 | h->root.type = bfd_link_hash_defined; | |
6512 | h->root.u.def.value = val; | |
6513 | h->root.u.def.section = bfd_abs_section_ptr; | |
6514 | } | |
6515 | } | |
6516 | ||
6517 | static bfd_boolean | |
6518 | resolve_symbol (const char * name, | |
6519 | bfd * input_bfd, | |
6520 | struct elf_final_link_info * finfo, | |
6521 | bfd_vma * result, | |
6522 | size_t locsymcount) | |
6523 | { | |
6524 | Elf_Internal_Sym * sym; | |
6525 | struct bfd_link_hash_entry * global_entry; | |
6526 | const char * candidate = NULL; | |
6527 | Elf_Internal_Shdr * symtab_hdr; | |
6528 | asection * sec = NULL; | |
6529 | size_t i; | |
6530 | ||
6531 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; | |
6532 | ||
6533 | for (i = 0; i < locsymcount; ++ i) | |
6534 | { | |
6535 | sym = finfo->internal_syms + i; | |
6536 | sec = finfo->sections [i]; | |
6537 | ||
6538 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
6539 | continue; | |
6540 | ||
6541 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
6542 | symtab_hdr->sh_link, | |
6543 | sym->st_name); | |
6544 | #ifdef DEBUG | |
6545 | printf ("Comparing string: '%s' vs. '%s' = 0x%x\n", | |
6546 | name, candidate, (unsigned int)sym->st_value); | |
6547 | #endif | |
6548 | if (candidate && strcmp (candidate, name) == 0) | |
6549 | { | |
6550 | * result = sym->st_value; | |
6551 | ||
6552 | if (sym->st_shndx > SHN_UNDEF && | |
6553 | sym->st_shndx < SHN_LORESERVE) | |
6554 | { | |
6555 | #ifdef DEBUG | |
6556 | printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n", | |
6557 | sec->output_section->name, | |
6558 | (unsigned int)sec->output_section->vma, | |
6559 | (unsigned int)sec->output_offset); | |
6560 | #endif | |
6561 | * result += sec->output_offset + sec->output_section->vma; | |
6562 | } | |
6563 | #ifdef DEBUG | |
6564 | printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result); | |
6565 | #endif | |
6566 | return TRUE; | |
6567 | } | |
6568 | } | |
6569 | ||
6570 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
6571 | global_entry = bfd_link_hash_lookup (finfo->info->hash, name, FALSE, FALSE, TRUE); | |
6572 | if (!global_entry) | |
6573 | return FALSE; | |
6574 | ||
6575 | if (global_entry->type == bfd_link_hash_defined | |
6576 | || global_entry->type == bfd_link_hash_defweak) | |
6577 | { | |
6578 | * result = global_entry->u.def.value | |
6579 | + global_entry->u.def.section->output_section->vma | |
6580 | + global_entry->u.def.section->output_offset; | |
6581 | #ifdef DEBUG | |
6582 | printf ("Found GLOBAL symbol '%s' with value %8.8x\n", | |
6583 | global_entry->root.string, (unsigned int)*result); | |
6584 | #endif | |
6585 | return TRUE; | |
6586 | } | |
6587 | ||
6588 | if (global_entry->type == bfd_link_hash_common) | |
6589 | { | |
6590 | *result = global_entry->u.def.value + | |
6591 | bfd_com_section_ptr->output_section->vma + | |
6592 | bfd_com_section_ptr->output_offset; | |
6593 | #ifdef DEBUG | |
6594 | printf ("Found COMMON symbol '%s' with value %8.8x\n", | |
6595 | global_entry->root.string, (unsigned int)*result); | |
6596 | #endif | |
6597 | return TRUE; | |
6598 | } | |
6599 | ||
6600 | return FALSE; | |
6601 | } | |
6602 | ||
6603 | static bfd_boolean | |
6604 | resolve_section (const char * name, | |
6605 | asection * sections, | |
6606 | bfd_vma * result) | |
6607 | { | |
6608 | asection * curr; | |
6609 | unsigned int len; | |
6610 | ||
6611 | for (curr = sections; curr; curr = curr->next) | |
6612 | if (strcmp (curr->name, name) == 0) | |
6613 | { | |
6614 | *result = curr->vma; | |
6615 | return TRUE; | |
6616 | } | |
6617 | ||
6618 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
6619 | for (curr = sections; curr; curr = curr->next) | |
6620 | { | |
6621 | len = strlen (curr->name); | |
6622 | if (len > strlen (name)) | |
6623 | continue; | |
6624 | ||
6625 | if (strncmp (curr->name, name, len) == 0) | |
6626 | { | |
6627 | if (strncmp (".end", name + len, 4) == 0) | |
6628 | { | |
6629 | *result = curr->vma + curr->size; | |
6630 | return TRUE; | |
6631 | } | |
6632 | ||
6633 | /* Insert more pseudo-section names here, if you like. */ | |
6634 | } | |
6635 | } | |
6636 | ||
6637 | return FALSE; | |
6638 | } | |
6639 | ||
6640 | static void | |
6641 | undefined_reference (const char * reftype, | |
6642 | const char * name) | |
6643 | { | |
6644 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype, name); | |
6645 | } | |
6646 | ||
6647 | static bfd_boolean | |
6648 | eval_symbol (bfd_vma * result, | |
6649 | char * sym, | |
6650 | char ** advanced, | |
6651 | bfd * input_bfd, | |
6652 | struct elf_final_link_info * finfo, | |
6653 | bfd_vma addr, | |
6654 | bfd_vma section_offset, | |
6655 | size_t locsymcount, | |
6656 | int signed_p) | |
6657 | { | |
6658 | int len; | |
6659 | int symlen; | |
6660 | bfd_vma a; | |
6661 | bfd_vma b; | |
6662 | const int bufsz = 4096; | |
6663 | char symbuf [bufsz]; | |
6664 | const char * symend; | |
6665 | bfd_boolean symbol_is_section = FALSE; | |
6666 | ||
6667 | len = strlen (sym); | |
6668 | symend = sym + len; | |
6669 | ||
6670 | if (len < 1 || len > bufsz) | |
6671 | { | |
6672 | bfd_set_error (bfd_error_invalid_operation); | |
6673 | return FALSE; | |
6674 | } | |
6675 | ||
6676 | switch (* sym) | |
6677 | { | |
6678 | case '.': | |
6679 | * result = addr + section_offset; | |
6680 | * advanced = sym + 1; | |
6681 | return TRUE; | |
6682 | ||
6683 | case '#': | |
6684 | ++ sym; | |
6685 | * result = strtoul (sym, advanced, 16); | |
6686 | return TRUE; | |
6687 | ||
6688 | case 'S': | |
6689 | symbol_is_section = TRUE; | |
6690 | case 's': | |
6691 | ++ sym; | |
6692 | symlen = strtol (sym, &sym, 10); | |
6693 | ++ sym; /* Skip the trailing ':'. */ | |
6694 | ||
6695 | if ((symend < sym) || ((symlen + 1) > bufsz)) | |
6696 | { | |
6697 | bfd_set_error (bfd_error_invalid_operation); | |
6698 | return FALSE; | |
6699 | } | |
6700 | ||
6701 | memcpy (symbuf, sym, symlen); | |
6702 | symbuf [symlen] = '\0'; | |
6703 | * advanced = sym + symlen; | |
6704 | ||
6705 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
6706 | the symbol as a section, or vice-versa. so we're pretty liberal in our | |
6707 | interpretation here; section means "try section first", not "must be a | |
6708 | section", and likewise with symbol. */ | |
6709 | ||
6710 | if (symbol_is_section) | |
6711 | { | |
6712 | if ((resolve_section (symbuf, finfo->output_bfd->sections, result) != TRUE) | |
6713 | && (resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE)) | |
6714 | { | |
6715 | undefined_reference ("section", symbuf); | |
6716 | return FALSE; | |
6717 | } | |
6718 | } | |
6719 | else | |
6720 | { | |
6721 | if ((resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE) | |
6722 | && (resolve_section (symbuf, finfo->output_bfd->sections, | |
6723 | result) != TRUE)) | |
6724 | { | |
6725 | undefined_reference ("symbol", symbuf); | |
6726 | return FALSE; | |
6727 | } | |
6728 | } | |
6729 | ||
6730 | return TRUE; | |
6731 | ||
6732 | /* All that remains are operators. */ | |
6733 | ||
6734 | #define UNARY_OP(op) \ | |
6735 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
6736 | { \ | |
6737 | sym += strlen (#op); \ | |
6738 | if (* sym == ':') \ | |
6739 | ++ sym; \ | |
6740 | if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \ | |
6741 | section_offset, locsymcount, signed_p) \ | |
6742 | != TRUE) \ | |
6743 | return FALSE; \ | |
6744 | if (signed_p) \ | |
6745 | * result = op ((signed)a); \ | |
6746 | else \ | |
6747 | * result = op a; \ | |
6748 | * advanced = sym; \ | |
6749 | return TRUE; \ | |
6750 | } | |
6751 | ||
6752 | #define BINARY_OP(op) \ | |
6753 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
6754 | { \ | |
6755 | sym += strlen (#op); \ | |
6756 | if (* sym == ':') \ | |
6757 | ++ sym; \ | |
6758 | if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \ | |
6759 | section_offset, locsymcount, signed_p) \ | |
6760 | != TRUE) \ | |
6761 | return FALSE; \ | |
6762 | ++ sym; \ | |
6763 | if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \ | |
6764 | section_offset, locsymcount, signed_p) \ | |
6765 | != TRUE) \ | |
6766 | return FALSE; \ | |
6767 | if (signed_p) \ | |
6768 | * result = ((signed) a) op ((signed) b); \ | |
6769 | else \ | |
6770 | * result = a op b; \ | |
6771 | * advanced = sym; \ | |
6772 | return TRUE; \ | |
6773 | } | |
6774 | ||
6775 | default: | |
6776 | UNARY_OP (0-); | |
6777 | BINARY_OP (<<); | |
6778 | BINARY_OP (>>); | |
6779 | BINARY_OP (==); | |
6780 | BINARY_OP (!=); | |
6781 | BINARY_OP (<=); | |
6782 | BINARY_OP (>=); | |
6783 | BINARY_OP (&&); | |
6784 | BINARY_OP (||); | |
6785 | UNARY_OP (~); | |
6786 | UNARY_OP (!); | |
6787 | BINARY_OP (*); | |
6788 | BINARY_OP (/); | |
6789 | BINARY_OP (%); | |
6790 | BINARY_OP (^); | |
6791 | BINARY_OP (|); | |
6792 | BINARY_OP (&); | |
6793 | BINARY_OP (+); | |
6794 | BINARY_OP (-); | |
6795 | BINARY_OP (<); | |
6796 | BINARY_OP (>); | |
6797 | #undef UNARY_OP | |
6798 | #undef BINARY_OP | |
6799 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
6800 | bfd_set_error (bfd_error_invalid_operation); | |
6801 | return FALSE; | |
6802 | } | |
6803 | } | |
6804 | ||
6805 | /* Entry point to evaluator, called from elf_link_input_bfd. */ | |
6806 | ||
6807 | static bfd_boolean | |
6808 | evaluate_complex_relocation_symbols (bfd * input_bfd, | |
6809 | struct elf_final_link_info * finfo, | |
6810 | size_t locsymcount) | |
6811 | { | |
6812 | const struct elf_backend_data * bed; | |
6813 | Elf_Internal_Shdr * symtab_hdr; | |
6814 | struct elf_link_hash_entry ** sym_hashes; | |
6815 | asection * reloc_sec; | |
6816 | bfd_boolean result = TRUE; | |
6817 | ||
6818 | /* For each section, we're going to check and see if it has any | |
6819 | complex relocations, and we're going to evaluate any of them | |
6820 | we can. */ | |
6821 | ||
6822 | if (finfo->info->relocatable) | |
6823 | return TRUE; | |
6824 | ||
6825 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; | |
6826 | sym_hashes = elf_sym_hashes (input_bfd); | |
6827 | bed = get_elf_backend_data (input_bfd); | |
6828 | ||
6829 | for (reloc_sec = input_bfd->sections; reloc_sec; reloc_sec = reloc_sec->next) | |
6830 | { | |
6831 | Elf_Internal_Rela * internal_relocs; | |
6832 | unsigned long i; | |
6833 | ||
6834 | /* This section was omitted from the link. */ | |
6835 | if (! reloc_sec->linker_mark) | |
6836 | continue; | |
6837 | ||
6838 | /* Only process sections containing relocs. */ | |
6839 | if ((reloc_sec->flags & SEC_RELOC) == 0) | |
6840 | continue; | |
6841 | ||
6842 | if (reloc_sec->reloc_count == 0) | |
6843 | continue; | |
6844 | ||
6845 | /* Read in the relocs for this section. */ | |
6846 | internal_relocs | |
6847 | = _bfd_elf_link_read_relocs (input_bfd, reloc_sec, NULL, | |
6848 | (Elf_Internal_Rela *) NULL, | |
6849 | FALSE); | |
6850 | if (internal_relocs == NULL) | |
6851 | continue; | |
6852 | ||
6853 | for (i = reloc_sec->reloc_count; i--;) | |
6854 | { | |
6855 | Elf_Internal_Rela * rel; | |
6856 | char * sym_name; | |
947844a3 | 6857 | bfd_vma index; |
d9352518 DB |
6858 | Elf_Internal_Sym * sym; |
6859 | bfd_vma result; | |
6860 | bfd_vma section_offset; | |
6861 | bfd_vma addr; | |
6862 | int signed_p = 0; | |
6863 | ||
6864 | rel = internal_relocs + i; | |
6865 | section_offset = reloc_sec->output_section->vma | |
6866 | + reloc_sec->output_offset; | |
6867 | addr = rel->r_offset; | |
6868 | ||
6869 | index = ELF32_R_SYM (rel->r_info); | |
6870 | if (bed->s->arch_size == 64) | |
6871 | index >>= 24; | |
4aac632e AM |
6872 | |
6873 | if (index == STN_UNDEF) | |
6874 | continue; | |
6875 | ||
d9352518 DB |
6876 | if (index < locsymcount) |
6877 | { | |
6878 | /* The symbol is local. */ | |
6879 | sym = finfo->internal_syms + index; | |
6880 | ||
6881 | /* We're only processing STT_RELC or STT_SRELC type symbols. */ | |
6882 | if ((ELF_ST_TYPE (sym->st_info) != STT_RELC) && | |
6883 | (ELF_ST_TYPE (sym->st_info) != STT_SRELC)) | |
6884 | continue; | |
6885 | ||
6886 | sym_name = bfd_elf_string_from_elf_section | |
6887 | (input_bfd, symtab_hdr->sh_link, sym->st_name); | |
6888 | ||
6889 | signed_p = (ELF_ST_TYPE (sym->st_info) == STT_SRELC); | |
6890 | } | |
6891 | else | |
6892 | { | |
6893 | /* The symbol is global. */ | |
6894 | struct elf_link_hash_entry * h; | |
6895 | ||
6896 | if (elf_bad_symtab (input_bfd)) | |
6897 | continue; | |
6898 | ||
6899 | h = sym_hashes [index - locsymcount]; | |
6900 | while ( h->root.type == bfd_link_hash_indirect | |
6901 | || h->root.type == bfd_link_hash_warning) | |
6902 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6903 | ||
6904 | if (h->type != STT_RELC && h->type != STT_SRELC) | |
6905 | continue; | |
6906 | ||
6907 | signed_p = (h->type == STT_SRELC); | |
6908 | sym_name = (char *) h->root.root.string; | |
6909 | } | |
6910 | #ifdef DEBUG | |
6911 | printf ("Encountered a complex symbol!"); | |
6912 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
6913 | input_bfd->filename, reloc_sec->name, i); | |
6914 | printf (" symbol: idx %8.8lx, name %s\n", | |
6915 | index, sym_name); | |
6916 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
6917 | rel->r_info, addr); | |
6918 | printf (" Evaluating '%s' ...\n ", sym_name); | |
6919 | #endif | |
6920 | if (eval_symbol (& result, sym_name, & sym_name, input_bfd, | |
6921 | finfo, addr, section_offset, locsymcount, | |
6922 | signed_p)) | |
6923 | /* Symbol evaluated OK. Update to absolute value. */ | |
6924 | set_symbol_value (input_bfd, finfo, index, result); | |
6925 | ||
6926 | else | |
6927 | result = FALSE; | |
6928 | } | |
6929 | ||
6930 | if (internal_relocs != elf_section_data (reloc_sec)->relocs) | |
6931 | free (internal_relocs); | |
6932 | } | |
6933 | ||
6934 | /* If nothing went wrong, then we adjusted | |
6935 | everything we wanted to adjust. */ | |
6936 | return result; | |
6937 | } | |
6938 | ||
6939 | static void | |
6940 | put_value (bfd_vma size, | |
6941 | unsigned long chunksz, | |
6942 | bfd * input_bfd, | |
6943 | bfd_vma x, | |
6944 | bfd_byte * location) | |
6945 | { | |
6946 | location += (size - chunksz); | |
6947 | ||
6948 | for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8)) | |
6949 | { | |
6950 | switch (chunksz) | |
6951 | { | |
6952 | default: | |
6953 | case 0: | |
6954 | abort (); | |
6955 | case 1: | |
6956 | bfd_put_8 (input_bfd, x, location); | |
6957 | break; | |
6958 | case 2: | |
6959 | bfd_put_16 (input_bfd, x, location); | |
6960 | break; | |
6961 | case 4: | |
6962 | bfd_put_32 (input_bfd, x, location); | |
6963 | break; | |
6964 | case 8: | |
6965 | #ifdef BFD64 | |
6966 | bfd_put_64 (input_bfd, x, location); | |
6967 | #else | |
6968 | abort (); | |
6969 | #endif | |
6970 | break; | |
6971 | } | |
6972 | } | |
6973 | } | |
6974 | ||
6975 | static bfd_vma | |
6976 | get_value (bfd_vma size, | |
6977 | unsigned long chunksz, | |
6978 | bfd * input_bfd, | |
6979 | bfd_byte * location) | |
6980 | { | |
6981 | bfd_vma x = 0; | |
6982 | ||
6983 | for (; size; size -= chunksz, location += chunksz) | |
6984 | { | |
6985 | switch (chunksz) | |
6986 | { | |
6987 | default: | |
6988 | case 0: | |
6989 | abort (); | |
6990 | case 1: | |
6991 | x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location); | |
6992 | break; | |
6993 | case 2: | |
6994 | x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location); | |
6995 | break; | |
6996 | case 4: | |
6997 | x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location); | |
6998 | break; | |
6999 | case 8: | |
7000 | #ifdef BFD64 | |
7001 | x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location); | |
7002 | #else | |
7003 | abort (); | |
7004 | #endif | |
7005 | break; | |
7006 | } | |
7007 | } | |
7008 | return x; | |
7009 | } | |
7010 | ||
7011 | static void | |
7012 | decode_complex_addend | |
7013 | (unsigned long * start, /* in bits */ | |
7014 | unsigned long * oplen, /* in bits */ | |
7015 | unsigned long * len, /* in bits */ | |
7016 | unsigned long * wordsz, /* in bytes */ | |
7017 | unsigned long * chunksz, /* in bytes */ | |
7018 | unsigned long * lsb0_p, | |
7019 | unsigned long * signed_p, | |
7020 | unsigned long * trunc_p, | |
7021 | unsigned long encoded) | |
7022 | { | |
7023 | * start = encoded & 0x3F; | |
7024 | * len = (encoded >> 6) & 0x3F; | |
7025 | * oplen = (encoded >> 12) & 0x3F; | |
7026 | * wordsz = (encoded >> 18) & 0xF; | |
7027 | * chunksz = (encoded >> 22) & 0xF; | |
7028 | * lsb0_p = (encoded >> 27) & 1; | |
7029 | * signed_p = (encoded >> 28) & 1; | |
7030 | * trunc_p = (encoded >> 29) & 1; | |
7031 | } | |
7032 | ||
7033 | void | |
7034 | bfd_elf_perform_complex_relocation | |
7035 | (bfd * output_bfd ATTRIBUTE_UNUSED, | |
7036 | struct bfd_link_info * info, | |
7037 | bfd * input_bfd, | |
7038 | asection * input_section, | |
7039 | bfd_byte * contents, | |
7040 | Elf_Internal_Rela * rel, | |
7041 | Elf_Internal_Sym * local_syms, | |
7042 | asection ** local_sections) | |
7043 | { | |
7044 | const struct elf_backend_data * bed; | |
7045 | Elf_Internal_Shdr * symtab_hdr; | |
7046 | asection * sec; | |
7047 | bfd_vma relocation = 0, shift, x; | |
947844a3 | 7048 | bfd_vma r_symndx; |
d9352518 DB |
7049 | bfd_vma mask; |
7050 | unsigned long start, oplen, len, wordsz, | |
7051 | chunksz, lsb0_p, signed_p, trunc_p; | |
7052 | ||
7053 | /* Perform this reloc, since it is complex. | |
7054 | (this is not to say that it necessarily refers to a complex | |
7055 | symbol; merely that it is a self-describing CGEN based reloc. | |
7056 | i.e. the addend has the complete reloc information (bit start, end, | |
7057 | word size, etc) encoded within it.). */ | |
7058 | r_symndx = ELF32_R_SYM (rel->r_info); | |
7059 | bed = get_elf_backend_data (input_bfd); | |
7060 | if (bed->s->arch_size == 64) | |
7061 | r_symndx >>= 24; | |
7062 | ||
7063 | #ifdef DEBUG | |
7064 | printf ("Performing complex relocation %ld...\n", r_symndx); | |
7065 | #endif | |
7066 | ||
7067 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; | |
7068 | if (r_symndx < symtab_hdr->sh_info) | |
7069 | { | |
7070 | /* The symbol is local. */ | |
7071 | Elf_Internal_Sym * sym; | |
7072 | ||
7073 | sym = local_syms + r_symndx; | |
7074 | sec = local_sections [r_symndx]; | |
7075 | relocation = sym->st_value; | |
7076 | if (sym->st_shndx > SHN_UNDEF && | |
7077 | sym->st_shndx < SHN_LORESERVE) | |
7078 | relocation += (sec->output_offset + | |
7079 | sec->output_section->vma); | |
7080 | } | |
7081 | else | |
7082 | { | |
7083 | /* The symbol is global. */ | |
7084 | struct elf_link_hash_entry **sym_hashes; | |
7085 | struct elf_link_hash_entry * h; | |
7086 | ||
7087 | sym_hashes = elf_sym_hashes (input_bfd); | |
7088 | h = sym_hashes [r_symndx]; | |
7089 | ||
7090 | while (h->root.type == bfd_link_hash_indirect | |
7091 | || h->root.type == bfd_link_hash_warning) | |
7092 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7093 | ||
7094 | if (h->root.type == bfd_link_hash_defined | |
7095 | || h->root.type == bfd_link_hash_defweak) | |
7096 | { | |
7097 | sec = h->root.u.def.section; | |
7098 | relocation = h->root.u.def.value; | |
7099 | ||
7100 | if (! bfd_is_abs_section (sec)) | |
7101 | relocation += (sec->output_section->vma | |
7102 | + sec->output_offset); | |
7103 | } | |
7104 | if (h->root.type == bfd_link_hash_undefined | |
7105 | && !((*info->callbacks->undefined_symbol) | |
7106 | (info, h->root.root.string, input_bfd, | |
7107 | input_section, rel->r_offset, | |
7108 | info->unresolved_syms_in_objects == RM_GENERATE_ERROR | |
7109 | || ELF_ST_VISIBILITY (h->other)))) | |
7110 | return; | |
7111 | } | |
7112 | ||
7113 | decode_complex_addend (& start, & oplen, & len, & wordsz, | |
7114 | & chunksz, & lsb0_p, & signed_p, | |
7115 | & trunc_p, rel->r_addend); | |
7116 | ||
7117 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
7118 | ||
7119 | if (lsb0_p) | |
7120 | shift = (start + 1) - len; | |
7121 | else | |
7122 | shift = (8 * wordsz) - (start + len); | |
7123 | ||
7124 | x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset); | |
7125 | ||
7126 | #ifdef DEBUG | |
7127 | printf ("Doing complex reloc: " | |
7128 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
7129 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
7130 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
7131 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
7132 | oplen, x, mask, relocation); | |
7133 | #endif | |
7134 | ||
7135 | if (! trunc_p) | |
7136 | { | |
7137 | /* Now do an overflow check. */ | |
7138 | if (bfd_check_overflow ((signed_p ? | |
7139 | complain_overflow_signed : | |
7140 | complain_overflow_unsigned), | |
7141 | len, 0, (8 * wordsz), | |
7142 | relocation) == bfd_reloc_overflow) | |
7143 | (*_bfd_error_handler) | |
7144 | ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit " | |
7145 | "within 0x%lx", | |
7146 | input_bfd->filename, input_section->name, rel->r_offset, | |
7147 | relocation, (signed_p ? "(signed) " : ""), mask); | |
7148 | } | |
7149 | ||
7150 | /* Do the deed. */ | |
7151 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
7152 | ||
7153 | #ifdef DEBUG | |
7154 | printf (" relocation: %8.8lx\n" | |
7155 | " shifted mask: %8.8lx\n" | |
7156 | " shifted/masked reloc: %8.8lx\n" | |
7157 | " result: %8.8lx\n", | |
7158 | relocation, (mask << shift), | |
7159 | ((relocation & mask) << shift), x); | |
7160 | #endif | |
7161 | put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset); | |
7162 | } | |
7163 | ||
c152c796 AM |
7164 | /* When performing a relocatable link, the input relocations are |
7165 | preserved. But, if they reference global symbols, the indices | |
7166 | referenced must be updated. Update all the relocations in | |
7167 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ | |
7168 | ||
7169 | static void | |
7170 | elf_link_adjust_relocs (bfd *abfd, | |
7171 | Elf_Internal_Shdr *rel_hdr, | |
7172 | unsigned int count, | |
7173 | struct elf_link_hash_entry **rel_hash) | |
7174 | { | |
7175 | unsigned int i; | |
7176 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7177 | bfd_byte *erela; | |
7178 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
7179 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
7180 | bfd_vma r_type_mask; | |
7181 | int r_sym_shift; | |
7182 | ||
7183 | if (rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
7184 | { | |
7185 | swap_in = bed->s->swap_reloc_in; | |
7186 | swap_out = bed->s->swap_reloc_out; | |
7187 | } | |
7188 | else if (rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
7189 | { | |
7190 | swap_in = bed->s->swap_reloca_in; | |
7191 | swap_out = bed->s->swap_reloca_out; | |
7192 | } | |
7193 | else | |
7194 | abort (); | |
7195 | ||
7196 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
7197 | abort (); | |
7198 | ||
7199 | if (bed->s->arch_size == 32) | |
7200 | { | |
7201 | r_type_mask = 0xff; | |
7202 | r_sym_shift = 8; | |
7203 | } | |
7204 | else | |
7205 | { | |
7206 | r_type_mask = 0xffffffff; | |
7207 | r_sym_shift = 32; | |
7208 | } | |
7209 | ||
7210 | erela = rel_hdr->contents; | |
7211 | for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) | |
7212 | { | |
7213 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
7214 | unsigned int j; | |
7215 | ||
7216 | if (*rel_hash == NULL) | |
7217 | continue; | |
7218 | ||
7219 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
7220 | ||
7221 | (*swap_in) (abfd, erela, irela); | |
7222 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
7223 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
7224 | | (irela[j].r_info & r_type_mask)); | |
7225 | (*swap_out) (abfd, irela, erela); | |
7226 | } | |
7227 | } | |
7228 | ||
7229 | struct elf_link_sort_rela | |
7230 | { | |
7231 | union { | |
7232 | bfd_vma offset; | |
7233 | bfd_vma sym_mask; | |
7234 | } u; | |
7235 | enum elf_reloc_type_class type; | |
7236 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
7237 | Elf_Internal_Rela rela[1]; | |
7238 | }; | |
7239 | ||
7240 | static int | |
7241 | elf_link_sort_cmp1 (const void *A, const void *B) | |
7242 | { | |
7243 | const struct elf_link_sort_rela *a = A; | |
7244 | const struct elf_link_sort_rela *b = B; | |
7245 | int relativea, relativeb; | |
7246 | ||
7247 | relativea = a->type == reloc_class_relative; | |
7248 | relativeb = b->type == reloc_class_relative; | |
7249 | ||
7250 | if (relativea < relativeb) | |
7251 | return 1; | |
7252 | if (relativea > relativeb) | |
7253 | return -1; | |
7254 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
7255 | return -1; | |
7256 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
7257 | return 1; | |
7258 | if (a->rela->r_offset < b->rela->r_offset) | |
7259 | return -1; | |
7260 | if (a->rela->r_offset > b->rela->r_offset) | |
7261 | return 1; | |
7262 | return 0; | |
7263 | } | |
7264 | ||
7265 | static int | |
7266 | elf_link_sort_cmp2 (const void *A, const void *B) | |
7267 | { | |
7268 | const struct elf_link_sort_rela *a = A; | |
7269 | const struct elf_link_sort_rela *b = B; | |
7270 | int copya, copyb; | |
7271 | ||
7272 | if (a->u.offset < b->u.offset) | |
7273 | return -1; | |
7274 | if (a->u.offset > b->u.offset) | |
7275 | return 1; | |
7276 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); | |
7277 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
7278 | if (copya < copyb) | |
7279 | return -1; | |
7280 | if (copya > copyb) | |
7281 | return 1; | |
7282 | if (a->rela->r_offset < b->rela->r_offset) | |
7283 | return -1; | |
7284 | if (a->rela->r_offset > b->rela->r_offset) | |
7285 | return 1; | |
7286 | return 0; | |
7287 | } | |
7288 | ||
7289 | static size_t | |
7290 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
7291 | { | |
3410fea8 | 7292 | asection *dynamic_relocs; |
fc66a176 L |
7293 | asection *rela_dyn; |
7294 | asection *rel_dyn; | |
c152c796 AM |
7295 | bfd_size_type count, size; |
7296 | size_t i, ret, sort_elt, ext_size; | |
7297 | bfd_byte *sort, *s_non_relative, *p; | |
7298 | struct elf_link_sort_rela *sq; | |
7299 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7300 | int i2e = bed->s->int_rels_per_ext_rel; | |
7301 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
7302 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
7303 | struct bfd_link_order *lo; | |
7304 | bfd_vma r_sym_mask; | |
3410fea8 | 7305 | bfd_boolean use_rela; |
c152c796 | 7306 | |
3410fea8 NC |
7307 | /* Find a dynamic reloc section. */ |
7308 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
7309 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
7310 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
7311 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 7312 | { |
3410fea8 NC |
7313 | bfd_boolean use_rela_initialised = FALSE; |
7314 | ||
7315 | /* This is just here to stop gcc from complaining. | |
7316 | It's initialization checking code is not perfect. */ | |
7317 | use_rela = TRUE; | |
7318 | ||
7319 | /* Both sections are present. Examine the sizes | |
7320 | of the indirect sections to help us choose. */ | |
7321 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
7322 | if (lo->type == bfd_indirect_link_order) | |
7323 | { | |
7324 | asection *o = lo->u.indirect.section; | |
7325 | ||
7326 | if ((o->size % bed->s->sizeof_rela) == 0) | |
7327 | { | |
7328 | if ((o->size % bed->s->sizeof_rel) == 0) | |
7329 | /* Section size is divisible by both rel and rela sizes. | |
7330 | It is of no help to us. */ | |
7331 | ; | |
7332 | else | |
7333 | { | |
7334 | /* Section size is only divisible by rela. */ | |
7335 | if (use_rela_initialised && (use_rela == FALSE)) | |
7336 | { | |
7337 | _bfd_error_handler | |
7338 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7339 | bfd_set_error (bfd_error_invalid_operation); | |
7340 | return 0; | |
7341 | } | |
7342 | else | |
7343 | { | |
7344 | use_rela = TRUE; | |
7345 | use_rela_initialised = TRUE; | |
7346 | } | |
7347 | } | |
7348 | } | |
7349 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
7350 | { | |
7351 | /* Section size is only divisible by rel. */ | |
7352 | if (use_rela_initialised && (use_rela == TRUE)) | |
7353 | { | |
7354 | _bfd_error_handler | |
7355 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7356 | bfd_set_error (bfd_error_invalid_operation); | |
7357 | return 0; | |
7358 | } | |
7359 | else | |
7360 | { | |
7361 | use_rela = FALSE; | |
7362 | use_rela_initialised = TRUE; | |
7363 | } | |
7364 | } | |
7365 | else | |
7366 | { | |
7367 | /* The section size is not divisible by either - something is wrong. */ | |
7368 | _bfd_error_handler | |
7369 | (_("%B: Unable to sort relocs - they are of an unknown size"), abfd); | |
7370 | bfd_set_error (bfd_error_invalid_operation); | |
7371 | return 0; | |
7372 | } | |
7373 | } | |
7374 | ||
7375 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
7376 | if (lo->type == bfd_indirect_link_order) | |
7377 | { | |
7378 | asection *o = lo->u.indirect.section; | |
7379 | ||
7380 | if ((o->size % bed->s->sizeof_rela) == 0) | |
7381 | { | |
7382 | if ((o->size % bed->s->sizeof_rel) == 0) | |
7383 | /* Section size is divisible by both rel and rela sizes. | |
7384 | It is of no help to us. */ | |
7385 | ; | |
7386 | else | |
7387 | { | |
7388 | /* Section size is only divisible by rela. */ | |
7389 | if (use_rela_initialised && (use_rela == FALSE)) | |
7390 | { | |
7391 | _bfd_error_handler | |
7392 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7393 | bfd_set_error (bfd_error_invalid_operation); | |
7394 | return 0; | |
7395 | } | |
7396 | else | |
7397 | { | |
7398 | use_rela = TRUE; | |
7399 | use_rela_initialised = TRUE; | |
7400 | } | |
7401 | } | |
7402 | } | |
7403 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
7404 | { | |
7405 | /* Section size is only divisible by rel. */ | |
7406 | if (use_rela_initialised && (use_rela == TRUE)) | |
7407 | { | |
7408 | _bfd_error_handler | |
7409 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7410 | bfd_set_error (bfd_error_invalid_operation); | |
7411 | return 0; | |
7412 | } | |
7413 | else | |
7414 | { | |
7415 | use_rela = FALSE; | |
7416 | use_rela_initialised = TRUE; | |
7417 | } | |
7418 | } | |
7419 | else | |
7420 | { | |
7421 | /* The section size is not divisible by either - something is wrong. */ | |
7422 | _bfd_error_handler | |
7423 | (_("%B: Unable to sort relocs - they are of an unknown size"), abfd); | |
7424 | bfd_set_error (bfd_error_invalid_operation); | |
7425 | return 0; | |
7426 | } | |
7427 | } | |
7428 | ||
7429 | if (! use_rela_initialised) | |
7430 | /* Make a guess. */ | |
7431 | use_rela = TRUE; | |
c152c796 | 7432 | } |
fc66a176 L |
7433 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
7434 | use_rela = TRUE; | |
7435 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 7436 | use_rela = FALSE; |
c152c796 | 7437 | else |
fc66a176 | 7438 | return 0; |
3410fea8 NC |
7439 | |
7440 | if (use_rela) | |
c152c796 | 7441 | { |
3410fea8 | 7442 | dynamic_relocs = rela_dyn; |
c152c796 AM |
7443 | ext_size = bed->s->sizeof_rela; |
7444 | swap_in = bed->s->swap_reloca_in; | |
7445 | swap_out = bed->s->swap_reloca_out; | |
7446 | } | |
3410fea8 NC |
7447 | else |
7448 | { | |
7449 | dynamic_relocs = rel_dyn; | |
7450 | ext_size = bed->s->sizeof_rel; | |
7451 | swap_in = bed->s->swap_reloc_in; | |
7452 | swap_out = bed->s->swap_reloc_out; | |
7453 | } | |
c152c796 AM |
7454 | |
7455 | size = 0; | |
3410fea8 | 7456 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 7457 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 7458 | size += lo->u.indirect.section->size; |
c152c796 | 7459 | |
3410fea8 | 7460 | if (size != dynamic_relocs->size) |
c152c796 AM |
7461 | return 0; |
7462 | ||
7463 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
7464 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
7465 | |
7466 | count = dynamic_relocs->size / ext_size; | |
c152c796 | 7467 | sort = bfd_zmalloc (sort_elt * count); |
3410fea8 | 7468 | |
c152c796 AM |
7469 | if (sort == NULL) |
7470 | { | |
7471 | (*info->callbacks->warning) | |
7472 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
7473 | return 0; | |
7474 | } | |
7475 | ||
7476 | if (bed->s->arch_size == 32) | |
7477 | r_sym_mask = ~(bfd_vma) 0xff; | |
7478 | else | |
7479 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
7480 | ||
3410fea8 | 7481 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
7482 | if (lo->type == bfd_indirect_link_order) |
7483 | { | |
7484 | bfd_byte *erel, *erelend; | |
7485 | asection *o = lo->u.indirect.section; | |
7486 | ||
1da212d6 AM |
7487 | if (o->contents == NULL && o->size != 0) |
7488 | { | |
7489 | /* This is a reloc section that is being handled as a normal | |
7490 | section. See bfd_section_from_shdr. We can't combine | |
7491 | relocs in this case. */ | |
7492 | free (sort); | |
7493 | return 0; | |
7494 | } | |
c152c796 | 7495 | erel = o->contents; |
eea6121a | 7496 | erelend = o->contents + o->size; |
c152c796 | 7497 | p = sort + o->output_offset / ext_size * sort_elt; |
3410fea8 | 7498 | |
c152c796 AM |
7499 | while (erel < erelend) |
7500 | { | |
7501 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 7502 | |
c152c796 AM |
7503 | (*swap_in) (abfd, erel, s->rela); |
7504 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
7505 | s->u.sym_mask = r_sym_mask; | |
7506 | p += sort_elt; | |
7507 | erel += ext_size; | |
7508 | } | |
7509 | } | |
7510 | ||
7511 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
7512 | ||
7513 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
7514 | { | |
7515 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
7516 | if (s->type != reloc_class_relative) | |
7517 | break; | |
7518 | } | |
7519 | ret = i; | |
7520 | s_non_relative = p; | |
7521 | ||
7522 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
7523 | for (; i < count; i++, p += sort_elt) | |
7524 | { | |
7525 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
7526 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
7527 | sq = sp; | |
7528 | sp->u.offset = sq->rela->r_offset; | |
7529 | } | |
7530 | ||
7531 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
7532 | ||
3410fea8 | 7533 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
7534 | if (lo->type == bfd_indirect_link_order) |
7535 | { | |
7536 | bfd_byte *erel, *erelend; | |
7537 | asection *o = lo->u.indirect.section; | |
7538 | ||
7539 | erel = o->contents; | |
eea6121a | 7540 | erelend = o->contents + o->size; |
c152c796 AM |
7541 | p = sort + o->output_offset / ext_size * sort_elt; |
7542 | while (erel < erelend) | |
7543 | { | |
7544 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
7545 | (*swap_out) (abfd, s->rela, erel); | |
7546 | p += sort_elt; | |
7547 | erel += ext_size; | |
7548 | } | |
7549 | } | |
7550 | ||
7551 | free (sort); | |
3410fea8 | 7552 | *psec = dynamic_relocs; |
c152c796 AM |
7553 | return ret; |
7554 | } | |
7555 | ||
7556 | /* Flush the output symbols to the file. */ | |
7557 | ||
7558 | static bfd_boolean | |
7559 | elf_link_flush_output_syms (struct elf_final_link_info *finfo, | |
7560 | const struct elf_backend_data *bed) | |
7561 | { | |
7562 | if (finfo->symbuf_count > 0) | |
7563 | { | |
7564 | Elf_Internal_Shdr *hdr; | |
7565 | file_ptr pos; | |
7566 | bfd_size_type amt; | |
7567 | ||
7568 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
7569 | pos = hdr->sh_offset + hdr->sh_size; | |
7570 | amt = finfo->symbuf_count * bed->s->sizeof_sym; | |
7571 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
7572 | || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) | |
7573 | return FALSE; | |
7574 | ||
7575 | hdr->sh_size += amt; | |
7576 | finfo->symbuf_count = 0; | |
7577 | } | |
7578 | ||
7579 | return TRUE; | |
7580 | } | |
7581 | ||
7582 | /* Add a symbol to the output symbol table. */ | |
7583 | ||
7584 | static bfd_boolean | |
7585 | elf_link_output_sym (struct elf_final_link_info *finfo, | |
7586 | const char *name, | |
7587 | Elf_Internal_Sym *elfsym, | |
7588 | asection *input_sec, | |
7589 | struct elf_link_hash_entry *h) | |
7590 | { | |
7591 | bfd_byte *dest; | |
7592 | Elf_External_Sym_Shndx *destshndx; | |
7593 | bfd_boolean (*output_symbol_hook) | |
7594 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, | |
7595 | struct elf_link_hash_entry *); | |
7596 | const struct elf_backend_data *bed; | |
7597 | ||
7598 | bed = get_elf_backend_data (finfo->output_bfd); | |
7599 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; | |
7600 | if (output_symbol_hook != NULL) | |
7601 | { | |
7602 | if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h)) | |
7603 | return FALSE; | |
7604 | } | |
7605 | ||
7606 | if (name == NULL || *name == '\0') | |
7607 | elfsym->st_name = 0; | |
7608 | else if (input_sec->flags & SEC_EXCLUDE) | |
7609 | elfsym->st_name = 0; | |
7610 | else | |
7611 | { | |
7612 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
7613 | name, TRUE, FALSE); | |
7614 | if (elfsym->st_name == (unsigned long) -1) | |
7615 | return FALSE; | |
7616 | } | |
7617 | ||
7618 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
7619 | { | |
7620 | if (! elf_link_flush_output_syms (finfo, bed)) | |
7621 | return FALSE; | |
7622 | } | |
7623 | ||
7624 | dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; | |
7625 | destshndx = finfo->symshndxbuf; | |
7626 | if (destshndx != NULL) | |
7627 | { | |
7628 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
7629 | { | |
7630 | bfd_size_type amt; | |
7631 | ||
7632 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); | |
7633 | finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2); | |
7634 | if (destshndx == NULL) | |
7635 | return FALSE; | |
7636 | memset ((char *) destshndx + amt, 0, amt); | |
7637 | finfo->shndxbuf_size *= 2; | |
7638 | } | |
7639 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
7640 | } | |
7641 | ||
7642 | bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); | |
7643 | finfo->symbuf_count += 1; | |
7644 | bfd_get_symcount (finfo->output_bfd) += 1; | |
7645 | ||
7646 | return TRUE; | |
7647 | } | |
7648 | ||
c0d5a53d L |
7649 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
7650 | ||
7651 | static bfd_boolean | |
7652 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
7653 | { | |
7654 | if (sym->st_shndx > SHN_HIRESERVE) | |
7655 | { | |
7656 | /* The gABI doesn't support dynamic symbols in output sections | |
7657 | beyond 64k. */ | |
7658 | (*_bfd_error_handler) | |
7659 | (_("%B: Too many sections: %d (>= %d)"), | |
7660 | abfd, bfd_count_sections (abfd), SHN_LORESERVE); | |
7661 | bfd_set_error (bfd_error_nonrepresentable_section); | |
7662 | return FALSE; | |
7663 | } | |
7664 | return TRUE; | |
7665 | } | |
7666 | ||
c152c796 AM |
7667 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
7668 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
7669 | versioned symbol that would normally require an explicit version. | |
7670 | We also handle the case that a DSO references a hidden symbol | |
7671 | which may be satisfied by a versioned symbol in another DSO. */ | |
7672 | ||
7673 | static bfd_boolean | |
7674 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
7675 | const struct elf_backend_data *bed, | |
7676 | struct elf_link_hash_entry *h) | |
7677 | { | |
7678 | bfd *abfd; | |
7679 | struct elf_link_loaded_list *loaded; | |
7680 | ||
7681 | if (!is_elf_hash_table (info->hash)) | |
7682 | return FALSE; | |
7683 | ||
7684 | switch (h->root.type) | |
7685 | { | |
7686 | default: | |
7687 | abfd = NULL; | |
7688 | break; | |
7689 | ||
7690 | case bfd_link_hash_undefined: | |
7691 | case bfd_link_hash_undefweak: | |
7692 | abfd = h->root.u.undef.abfd; | |
7693 | if ((abfd->flags & DYNAMIC) == 0 | |
e56f61be | 7694 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
7695 | return FALSE; |
7696 | break; | |
7697 | ||
7698 | case bfd_link_hash_defined: | |
7699 | case bfd_link_hash_defweak: | |
7700 | abfd = h->root.u.def.section->owner; | |
7701 | break; | |
7702 | ||
7703 | case bfd_link_hash_common: | |
7704 | abfd = h->root.u.c.p->section->owner; | |
7705 | break; | |
7706 | } | |
7707 | BFD_ASSERT (abfd != NULL); | |
7708 | ||
7709 | for (loaded = elf_hash_table (info)->loaded; | |
7710 | loaded != NULL; | |
7711 | loaded = loaded->next) | |
7712 | { | |
7713 | bfd *input; | |
7714 | Elf_Internal_Shdr *hdr; | |
7715 | bfd_size_type symcount; | |
7716 | bfd_size_type extsymcount; | |
7717 | bfd_size_type extsymoff; | |
7718 | Elf_Internal_Shdr *versymhdr; | |
7719 | Elf_Internal_Sym *isym; | |
7720 | Elf_Internal_Sym *isymend; | |
7721 | Elf_Internal_Sym *isymbuf; | |
7722 | Elf_External_Versym *ever; | |
7723 | Elf_External_Versym *extversym; | |
7724 | ||
7725 | input = loaded->abfd; | |
7726 | ||
7727 | /* We check each DSO for a possible hidden versioned definition. */ | |
7728 | if (input == abfd | |
7729 | || (input->flags & DYNAMIC) == 0 | |
7730 | || elf_dynversym (input) == 0) | |
7731 | continue; | |
7732 | ||
7733 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
7734 | ||
7735 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
7736 | if (elf_bad_symtab (input)) | |
7737 | { | |
7738 | extsymcount = symcount; | |
7739 | extsymoff = 0; | |
7740 | } | |
7741 | else | |
7742 | { | |
7743 | extsymcount = symcount - hdr->sh_info; | |
7744 | extsymoff = hdr->sh_info; | |
7745 | } | |
7746 | ||
7747 | if (extsymcount == 0) | |
7748 | continue; | |
7749 | ||
7750 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
7751 | NULL, NULL, NULL); | |
7752 | if (isymbuf == NULL) | |
7753 | return FALSE; | |
7754 | ||
7755 | /* Read in any version definitions. */ | |
7756 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
7757 | extversym = bfd_malloc (versymhdr->sh_size); | |
7758 | if (extversym == NULL) | |
7759 | goto error_ret; | |
7760 | ||
7761 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
7762 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
7763 | != versymhdr->sh_size)) | |
7764 | { | |
7765 | free (extversym); | |
7766 | error_ret: | |
7767 | free (isymbuf); | |
7768 | return FALSE; | |
7769 | } | |
7770 | ||
7771 | ever = extversym + extsymoff; | |
7772 | isymend = isymbuf + extsymcount; | |
7773 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
7774 | { | |
7775 | const char *name; | |
7776 | Elf_Internal_Versym iver; | |
7777 | unsigned short version_index; | |
7778 | ||
7779 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
7780 | || isym->st_shndx == SHN_UNDEF) | |
7781 | continue; | |
7782 | ||
7783 | name = bfd_elf_string_from_elf_section (input, | |
7784 | hdr->sh_link, | |
7785 | isym->st_name); | |
7786 | if (strcmp (name, h->root.root.string) != 0) | |
7787 | continue; | |
7788 | ||
7789 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
7790 | ||
7791 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
7792 | { | |
7793 | /* If we have a non-hidden versioned sym, then it should | |
7794 | have provided a definition for the undefined sym. */ | |
7795 | abort (); | |
7796 | } | |
7797 | ||
7798 | version_index = iver.vs_vers & VERSYM_VERSION; | |
7799 | if (version_index == 1 || version_index == 2) | |
7800 | { | |
7801 | /* This is the base or first version. We can use it. */ | |
7802 | free (extversym); | |
7803 | free (isymbuf); | |
7804 | return TRUE; | |
7805 | } | |
7806 | } | |
7807 | ||
7808 | free (extversym); | |
7809 | free (isymbuf); | |
7810 | } | |
7811 | ||
7812 | return FALSE; | |
7813 | } | |
7814 | ||
7815 | /* Add an external symbol to the symbol table. This is called from | |
7816 | the hash table traversal routine. When generating a shared object, | |
7817 | we go through the symbol table twice. The first time we output | |
7818 | anything that might have been forced to local scope in a version | |
7819 | script. The second time we output the symbols that are still | |
7820 | global symbols. */ | |
7821 | ||
7822 | static bfd_boolean | |
7823 | elf_link_output_extsym (struct elf_link_hash_entry *h, void *data) | |
7824 | { | |
7825 | struct elf_outext_info *eoinfo = data; | |
7826 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
7827 | bfd_boolean strip; | |
7828 | Elf_Internal_Sym sym; | |
7829 | asection *input_sec; | |
7830 | const struct elf_backend_data *bed; | |
7831 | ||
7832 | if (h->root.type == bfd_link_hash_warning) | |
7833 | { | |
7834 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7835 | if (h->root.type == bfd_link_hash_new) | |
7836 | return TRUE; | |
7837 | } | |
7838 | ||
7839 | /* Decide whether to output this symbol in this pass. */ | |
7840 | if (eoinfo->localsyms) | |
7841 | { | |
f5385ebf | 7842 | if (!h->forced_local) |
c152c796 AM |
7843 | return TRUE; |
7844 | } | |
7845 | else | |
7846 | { | |
f5385ebf | 7847 | if (h->forced_local) |
c152c796 AM |
7848 | return TRUE; |
7849 | } | |
7850 | ||
7851 | bed = get_elf_backend_data (finfo->output_bfd); | |
7852 | ||
12ac1cf5 | 7853 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 7854 | { |
12ac1cf5 NC |
7855 | /* If we have an undefined symbol reference here then it must have |
7856 | come from a shared library that is being linked in. (Undefined | |
7857 | references in regular files have already been handled). */ | |
7858 | bfd_boolean ignore_undef = FALSE; | |
7859 | ||
7860 | /* Some symbols may be special in that the fact that they're | |
7861 | undefined can be safely ignored - let backend determine that. */ | |
7862 | if (bed->elf_backend_ignore_undef_symbol) | |
7863 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
7864 | ||
7865 | /* If we are reporting errors for this situation then do so now. */ | |
7866 | if (ignore_undef == FALSE | |
7867 | && h->ref_dynamic | |
7868 | && ! h->ref_regular | |
7869 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h) | |
7870 | && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
c152c796 | 7871 | { |
12ac1cf5 NC |
7872 | if (! (finfo->info->callbacks->undefined_symbol |
7873 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
7874 | NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) | |
7875 | { | |
7876 | eoinfo->failed = TRUE; | |
7877 | return FALSE; | |
7878 | } | |
c152c796 AM |
7879 | } |
7880 | } | |
7881 | ||
7882 | /* We should also warn if a forced local symbol is referenced from | |
7883 | shared libraries. */ | |
7884 | if (! finfo->info->relocatable | |
7885 | && (! finfo->info->shared) | |
f5385ebf AM |
7886 | && h->forced_local |
7887 | && h->ref_dynamic | |
7888 | && !h->dynamic_def | |
7889 | && !h->dynamic_weak | |
c152c796 AM |
7890 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) |
7891 | { | |
7892 | (*_bfd_error_handler) | |
d003868e | 7893 | (_("%B: %s symbol `%s' in %B is referenced by DSO"), |
cfca085c L |
7894 | finfo->output_bfd, |
7895 | h->root.u.def.section == bfd_abs_section_ptr | |
7896 | ? finfo->output_bfd : h->root.u.def.section->owner, | |
c152c796 AM |
7897 | ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
7898 | ? "internal" | |
7899 | : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
d003868e AM |
7900 | ? "hidden" : "local", |
7901 | h->root.root.string); | |
c152c796 AM |
7902 | eoinfo->failed = TRUE; |
7903 | return FALSE; | |
7904 | } | |
7905 | ||
7906 | /* We don't want to output symbols that have never been mentioned by | |
7907 | a regular file, or that we have been told to strip. However, if | |
7908 | h->indx is set to -2, the symbol is used by a reloc and we must | |
7909 | output it. */ | |
7910 | if (h->indx == -2) | |
7911 | strip = FALSE; | |
f5385ebf | 7912 | else if ((h->def_dynamic |
77cfaee6 AM |
7913 | || h->ref_dynamic |
7914 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
7915 | && !h->def_regular |
7916 | && !h->ref_regular) | |
c152c796 AM |
7917 | strip = TRUE; |
7918 | else if (finfo->info->strip == strip_all) | |
7919 | strip = TRUE; | |
7920 | else if (finfo->info->strip == strip_some | |
7921 | && bfd_hash_lookup (finfo->info->keep_hash, | |
7922 | h->root.root.string, FALSE, FALSE) == NULL) | |
7923 | strip = TRUE; | |
7924 | else if (finfo->info->strip_discarded | |
7925 | && (h->root.type == bfd_link_hash_defined | |
7926 | || h->root.type == bfd_link_hash_defweak) | |
7927 | && elf_discarded_section (h->root.u.def.section)) | |
7928 | strip = TRUE; | |
7929 | else | |
7930 | strip = FALSE; | |
7931 | ||
7932 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
7933 | nothing else to do unless it is a forced local symbol. */ | |
7934 | if (strip | |
7935 | && h->dynindx == -1 | |
f5385ebf | 7936 | && !h->forced_local) |
c152c796 AM |
7937 | return TRUE; |
7938 | ||
7939 | sym.st_value = 0; | |
7940 | sym.st_size = h->size; | |
7941 | sym.st_other = h->other; | |
f5385ebf | 7942 | if (h->forced_local) |
c152c796 AM |
7943 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); |
7944 | else if (h->root.type == bfd_link_hash_undefweak | |
7945 | || h->root.type == bfd_link_hash_defweak) | |
7946 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
7947 | else | |
7948 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
7949 | ||
7950 | switch (h->root.type) | |
7951 | { | |
7952 | default: | |
7953 | case bfd_link_hash_new: | |
7954 | case bfd_link_hash_warning: | |
7955 | abort (); | |
7956 | return FALSE; | |
7957 | ||
7958 | case bfd_link_hash_undefined: | |
7959 | case bfd_link_hash_undefweak: | |
7960 | input_sec = bfd_und_section_ptr; | |
7961 | sym.st_shndx = SHN_UNDEF; | |
7962 | break; | |
7963 | ||
7964 | case bfd_link_hash_defined: | |
7965 | case bfd_link_hash_defweak: | |
7966 | { | |
7967 | input_sec = h->root.u.def.section; | |
7968 | if (input_sec->output_section != NULL) | |
7969 | { | |
7970 | sym.st_shndx = | |
7971 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
7972 | input_sec->output_section); | |
7973 | if (sym.st_shndx == SHN_BAD) | |
7974 | { | |
7975 | (*_bfd_error_handler) | |
d003868e AM |
7976 | (_("%B: could not find output section %A for input section %A"), |
7977 | finfo->output_bfd, input_sec->output_section, input_sec); | |
c152c796 AM |
7978 | eoinfo->failed = TRUE; |
7979 | return FALSE; | |
7980 | } | |
7981 | ||
7982 | /* ELF symbols in relocatable files are section relative, | |
7983 | but in nonrelocatable files they are virtual | |
7984 | addresses. */ | |
7985 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
7986 | if (! finfo->info->relocatable) | |
7987 | { | |
7988 | sym.st_value += input_sec->output_section->vma; | |
7989 | if (h->type == STT_TLS) | |
7990 | { | |
7991 | /* STT_TLS symbols are relative to PT_TLS segment | |
7992 | base. */ | |
7993 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
7994 | sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
7995 | } | |
7996 | } | |
7997 | } | |
7998 | else | |
7999 | { | |
8000 | BFD_ASSERT (input_sec->owner == NULL | |
8001 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
8002 | sym.st_shndx = SHN_UNDEF; | |
8003 | input_sec = bfd_und_section_ptr; | |
8004 | } | |
8005 | } | |
8006 | break; | |
8007 | ||
8008 | case bfd_link_hash_common: | |
8009 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 8010 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
8011 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
8012 | break; | |
8013 | ||
8014 | case bfd_link_hash_indirect: | |
8015 | /* These symbols are created by symbol versioning. They point | |
8016 | to the decorated version of the name. For example, if the | |
8017 | symbol foo@@GNU_1.2 is the default, which should be used when | |
8018 | foo is used with no version, then we add an indirect symbol | |
8019 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
8020 | since the indirected symbol is already in the hash table. */ | |
8021 | return TRUE; | |
8022 | } | |
8023 | ||
8024 | /* Give the processor backend a chance to tweak the symbol value, | |
8025 | and also to finish up anything that needs to be done for this | |
8026 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
8027 | forced local syms when non-shared is due to a historical quirk. */ | |
8028 | if ((h->dynindx != -1 | |
f5385ebf | 8029 | || h->forced_local) |
c152c796 AM |
8030 | && ((finfo->info->shared |
8031 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
8032 | || h->root.type != bfd_link_hash_undefweak)) | |
f5385ebf | 8033 | || !h->forced_local) |
c152c796 AM |
8034 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
8035 | { | |
8036 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8037 | (finfo->output_bfd, finfo->info, h, &sym))) | |
8038 | { | |
8039 | eoinfo->failed = TRUE; | |
8040 | return FALSE; | |
8041 | } | |
8042 | } | |
8043 | ||
8044 | /* If we are marking the symbol as undefined, and there are no | |
8045 | non-weak references to this symbol from a regular object, then | |
8046 | mark the symbol as weak undefined; if there are non-weak | |
8047 | references, mark the symbol as strong. We can't do this earlier, | |
8048 | because it might not be marked as undefined until the | |
8049 | finish_dynamic_symbol routine gets through with it. */ | |
8050 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 8051 | && h->ref_regular |
c152c796 AM |
8052 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
8053 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
8054 | { | |
8055 | int bindtype; | |
8056 | ||
f5385ebf | 8057 | if (h->ref_regular_nonweak) |
c152c796 AM |
8058 | bindtype = STB_GLOBAL; |
8059 | else | |
8060 | bindtype = STB_WEAK; | |
8061 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); | |
8062 | } | |
8063 | ||
8064 | /* If a non-weak symbol with non-default visibility is not defined | |
8065 | locally, it is a fatal error. */ | |
8066 | if (! finfo->info->relocatable | |
8067 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT | |
8068 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
8069 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 8070 | && !h->def_regular) |
c152c796 AM |
8071 | { |
8072 | (*_bfd_error_handler) | |
d003868e AM |
8073 | (_("%B: %s symbol `%s' isn't defined"), |
8074 | finfo->output_bfd, | |
8075 | ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED | |
8076 | ? "protected" | |
8077 | : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL | |
8078 | ? "internal" : "hidden", | |
8079 | h->root.root.string); | |
c152c796 AM |
8080 | eoinfo->failed = TRUE; |
8081 | return FALSE; | |
8082 | } | |
8083 | ||
8084 | /* If this symbol should be put in the .dynsym section, then put it | |
8085 | there now. We already know the symbol index. We also fill in | |
8086 | the entry in the .hash section. */ | |
8087 | if (h->dynindx != -1 | |
8088 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
8089 | { | |
c152c796 AM |
8090 | bfd_byte *esym; |
8091 | ||
8092 | sym.st_name = h->dynstr_index; | |
8093 | esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; | |
c0d5a53d L |
8094 | if (! check_dynsym (finfo->output_bfd, &sym)) |
8095 | { | |
8096 | eoinfo->failed = TRUE; | |
8097 | return FALSE; | |
8098 | } | |
c152c796 AM |
8099 | bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); |
8100 | ||
fdc90cb4 JJ |
8101 | if (finfo->hash_sec != NULL) |
8102 | { | |
8103 | size_t hash_entry_size; | |
8104 | bfd_byte *bucketpos; | |
8105 | bfd_vma chain; | |
41198d0c L |
8106 | size_t bucketcount; |
8107 | size_t bucket; | |
8108 | ||
8109 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
8110 | bucket = h->u.elf_hash_value % bucketcount; | |
fdc90cb4 JJ |
8111 | |
8112 | hash_entry_size | |
8113 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; | |
8114 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents | |
8115 | + (bucket + 2) * hash_entry_size); | |
8116 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
8117 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); | |
8118 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, | |
8119 | ((bfd_byte *) finfo->hash_sec->contents | |
8120 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
8121 | } | |
c152c796 AM |
8122 | |
8123 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
8124 | { | |
8125 | Elf_Internal_Versym iversym; | |
8126 | Elf_External_Versym *eversym; | |
8127 | ||
f5385ebf | 8128 | if (!h->def_regular) |
c152c796 AM |
8129 | { |
8130 | if (h->verinfo.verdef == NULL) | |
8131 | iversym.vs_vers = 0; | |
8132 | else | |
8133 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
8134 | } | |
8135 | else | |
8136 | { | |
8137 | if (h->verinfo.vertree == NULL) | |
8138 | iversym.vs_vers = 1; | |
8139 | else | |
8140 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
3e3b46e5 PB |
8141 | if (finfo->info->create_default_symver) |
8142 | iversym.vs_vers++; | |
c152c796 AM |
8143 | } |
8144 | ||
f5385ebf | 8145 | if (h->hidden) |
c152c796 AM |
8146 | iversym.vs_vers |= VERSYM_HIDDEN; |
8147 | ||
8148 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; | |
8149 | eversym += h->dynindx; | |
8150 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
8151 | } | |
8152 | } | |
8153 | ||
8154 | /* If we're stripping it, then it was just a dynamic symbol, and | |
8155 | there's nothing else to do. */ | |
8156 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) | |
8157 | return TRUE; | |
8158 | ||
8159 | h->indx = bfd_get_symcount (finfo->output_bfd); | |
8160 | ||
8161 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h)) | |
8162 | { | |
8163 | eoinfo->failed = TRUE; | |
8164 | return FALSE; | |
8165 | } | |
8166 | ||
8167 | return TRUE; | |
8168 | } | |
8169 | ||
cdd3575c AM |
8170 | /* Return TRUE if special handling is done for relocs in SEC against |
8171 | symbols defined in discarded sections. */ | |
8172 | ||
c152c796 AM |
8173 | static bfd_boolean |
8174 | elf_section_ignore_discarded_relocs (asection *sec) | |
8175 | { | |
8176 | const struct elf_backend_data *bed; | |
8177 | ||
cdd3575c AM |
8178 | switch (sec->sec_info_type) |
8179 | { | |
8180 | case ELF_INFO_TYPE_STABS: | |
8181 | case ELF_INFO_TYPE_EH_FRAME: | |
8182 | return TRUE; | |
8183 | default: | |
8184 | break; | |
8185 | } | |
c152c796 AM |
8186 | |
8187 | bed = get_elf_backend_data (sec->owner); | |
8188 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
8189 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
8190 | return TRUE; | |
8191 | ||
8192 | return FALSE; | |
8193 | } | |
8194 | ||
9e66c942 AM |
8195 | /* Return a mask saying how ld should treat relocations in SEC against |
8196 | symbols defined in discarded sections. If this function returns | |
8197 | COMPLAIN set, ld will issue a warning message. If this function | |
8198 | returns PRETEND set, and the discarded section was link-once and the | |
8199 | same size as the kept link-once section, ld will pretend that the | |
8200 | symbol was actually defined in the kept section. Otherwise ld will | |
8201 | zero the reloc (at least that is the intent, but some cooperation by | |
8202 | the target dependent code is needed, particularly for REL targets). */ | |
8203 | ||
8a696751 AM |
8204 | unsigned int |
8205 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 8206 | { |
9e66c942 | 8207 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 8208 | return PRETEND; |
cdd3575c AM |
8209 | |
8210 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 8211 | return 0; |
cdd3575c AM |
8212 | |
8213 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 8214 | return 0; |
cdd3575c | 8215 | |
9e66c942 | 8216 | return COMPLAIN | PRETEND; |
cdd3575c AM |
8217 | } |
8218 | ||
3d7f7666 L |
8219 | /* Find a match between a section and a member of a section group. */ |
8220 | ||
8221 | static asection * | |
c0f00686 L |
8222 | match_group_member (asection *sec, asection *group, |
8223 | struct bfd_link_info *info) | |
3d7f7666 L |
8224 | { |
8225 | asection *first = elf_next_in_group (group); | |
8226 | asection *s = first; | |
8227 | ||
8228 | while (s != NULL) | |
8229 | { | |
c0f00686 | 8230 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
8231 | return s; |
8232 | ||
83180ade | 8233 | s = elf_next_in_group (s); |
3d7f7666 L |
8234 | if (s == first) |
8235 | break; | |
8236 | } | |
8237 | ||
8238 | return NULL; | |
8239 | } | |
8240 | ||
01b3c8ab | 8241 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
8242 | to replace it. Return the replacement if it is OK. Otherwise return |
8243 | NULL. */ | |
01b3c8ab L |
8244 | |
8245 | asection * | |
c0f00686 | 8246 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
8247 | { |
8248 | asection *kept; | |
8249 | ||
8250 | kept = sec->kept_section; | |
8251 | if (kept != NULL) | |
8252 | { | |
c2370991 | 8253 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 8254 | kept = match_group_member (sec, kept, info); |
01b3c8ab L |
8255 | if (kept != NULL && sec->size != kept->size) |
8256 | kept = NULL; | |
c2370991 | 8257 | sec->kept_section = kept; |
01b3c8ab L |
8258 | } |
8259 | return kept; | |
8260 | } | |
8261 | ||
c152c796 AM |
8262 | /* Link an input file into the linker output file. This function |
8263 | handles all the sections and relocations of the input file at once. | |
8264 | This is so that we only have to read the local symbols once, and | |
8265 | don't have to keep them in memory. */ | |
8266 | ||
8267 | static bfd_boolean | |
8268 | elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) | |
8269 | { | |
ece5ef60 | 8270 | int (*relocate_section) |
c152c796 AM |
8271 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
8272 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
8273 | bfd *output_bfd; | |
8274 | Elf_Internal_Shdr *symtab_hdr; | |
8275 | size_t locsymcount; | |
8276 | size_t extsymoff; | |
8277 | Elf_Internal_Sym *isymbuf; | |
8278 | Elf_Internal_Sym *isym; | |
8279 | Elf_Internal_Sym *isymend; | |
8280 | long *pindex; | |
8281 | asection **ppsection; | |
8282 | asection *o; | |
8283 | const struct elf_backend_data *bed; | |
c152c796 AM |
8284 | struct elf_link_hash_entry **sym_hashes; |
8285 | ||
8286 | output_bfd = finfo->output_bfd; | |
8287 | bed = get_elf_backend_data (output_bfd); | |
8288 | relocate_section = bed->elf_backend_relocate_section; | |
8289 | ||
8290 | /* If this is a dynamic object, we don't want to do anything here: | |
8291 | we don't want the local symbols, and we don't want the section | |
8292 | contents. */ | |
8293 | if ((input_bfd->flags & DYNAMIC) != 0) | |
8294 | return TRUE; | |
8295 | ||
c152c796 AM |
8296 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
8297 | if (elf_bad_symtab (input_bfd)) | |
8298 | { | |
8299 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
8300 | extsymoff = 0; | |
8301 | } | |
8302 | else | |
8303 | { | |
8304 | locsymcount = symtab_hdr->sh_info; | |
8305 | extsymoff = symtab_hdr->sh_info; | |
8306 | } | |
8307 | ||
8308 | /* Read the local symbols. */ | |
8309 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
8310 | if (isymbuf == NULL && locsymcount != 0) | |
8311 | { | |
8312 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8313 | finfo->internal_syms, | |
8314 | finfo->external_syms, | |
8315 | finfo->locsym_shndx); | |
8316 | if (isymbuf == NULL) | |
8317 | return FALSE; | |
8318 | } | |
b31847af DB |
8319 | /* evaluate_complex_relocation_symbols looks for symbols in |
8320 | finfo->internal_syms. */ | |
8321 | else if (isymbuf != NULL && locsymcount != 0) | |
8322 | { | |
8323 | bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8324 | finfo->internal_syms, | |
8325 | finfo->external_syms, | |
8326 | finfo->locsym_shndx); | |
8327 | } | |
c152c796 AM |
8328 | |
8329 | /* Find local symbol sections and adjust values of symbols in | |
8330 | SEC_MERGE sections. Write out those local symbols we know are | |
8331 | going into the output file. */ | |
8332 | isymend = isymbuf + locsymcount; | |
8333 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
8334 | isym < isymend; | |
8335 | isym++, pindex++, ppsection++) | |
8336 | { | |
8337 | asection *isec; | |
8338 | const char *name; | |
8339 | Elf_Internal_Sym osym; | |
8340 | ||
8341 | *pindex = -1; | |
8342 | ||
8343 | if (elf_bad_symtab (input_bfd)) | |
8344 | { | |
8345 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
8346 | { | |
8347 | *ppsection = NULL; | |
8348 | continue; | |
8349 | } | |
8350 | } | |
8351 | ||
8352 | if (isym->st_shndx == SHN_UNDEF) | |
8353 | isec = bfd_und_section_ptr; | |
8354 | else if (isym->st_shndx < SHN_LORESERVE | |
8355 | || isym->st_shndx > SHN_HIRESERVE) | |
8356 | { | |
8357 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); | |
8358 | if (isec | |
8359 | && isec->sec_info_type == ELF_INFO_TYPE_MERGE | |
8360 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) | |
8361 | isym->st_value = | |
8362 | _bfd_merged_section_offset (output_bfd, &isec, | |
8363 | elf_section_data (isec)->sec_info, | |
753731ee | 8364 | isym->st_value); |
c152c796 AM |
8365 | } |
8366 | else if (isym->st_shndx == SHN_ABS) | |
8367 | isec = bfd_abs_section_ptr; | |
8368 | else if (isym->st_shndx == SHN_COMMON) | |
8369 | isec = bfd_com_section_ptr; | |
8370 | else | |
8371 | { | |
f02571c5 AM |
8372 | /* Don't attempt to output symbols with st_shnx in the |
8373 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
8374 | *ppsection = NULL; | |
8375 | continue; | |
c152c796 AM |
8376 | } |
8377 | ||
8378 | *ppsection = isec; | |
8379 | ||
8380 | /* Don't output the first, undefined, symbol. */ | |
8381 | if (ppsection == finfo->sections) | |
8382 | continue; | |
8383 | ||
8384 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
8385 | { | |
8386 | /* We never output section symbols. Instead, we use the | |
8387 | section symbol of the corresponding section in the output | |
8388 | file. */ | |
8389 | continue; | |
8390 | } | |
8391 | ||
8392 | /* If we are stripping all symbols, we don't want to output this | |
8393 | one. */ | |
8394 | if (finfo->info->strip == strip_all) | |
8395 | continue; | |
8396 | ||
8397 | /* If we are discarding all local symbols, we don't want to | |
8398 | output this one. If we are generating a relocatable output | |
8399 | file, then some of the local symbols may be required by | |
8400 | relocs; we output them below as we discover that they are | |
8401 | needed. */ | |
8402 | if (finfo->info->discard == discard_all) | |
8403 | continue; | |
8404 | ||
8405 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
8406 | discarding, we don't need to keep it. */ |
8407 | if (isym->st_shndx != SHN_UNDEF | |
8408 | && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
ccf5f610 | 8409 | && (isec == NULL |
f02571c5 AM |
8410 | || bfd_section_removed_from_list (output_bfd, |
8411 | isec->output_section))) | |
e75a280b L |
8412 | continue; |
8413 | ||
c152c796 AM |
8414 | /* Get the name of the symbol. */ |
8415 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
8416 | isym->st_name); | |
8417 | if (name == NULL) | |
8418 | return FALSE; | |
8419 | ||
8420 | /* See if we are discarding symbols with this name. */ | |
8421 | if ((finfo->info->strip == strip_some | |
8422 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) | |
8423 | == NULL)) | |
8424 | || (((finfo->info->discard == discard_sec_merge | |
8425 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) | |
8426 | || finfo->info->discard == discard_l) | |
8427 | && bfd_is_local_label_name (input_bfd, name))) | |
8428 | continue; | |
8429 | ||
8430 | /* If we get here, we are going to output this symbol. */ | |
8431 | ||
8432 | osym = *isym; | |
8433 | ||
8434 | /* Adjust the section index for the output file. */ | |
8435 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
8436 | isec->output_section); | |
8437 | if (osym.st_shndx == SHN_BAD) | |
8438 | return FALSE; | |
8439 | ||
8440 | *pindex = bfd_get_symcount (output_bfd); | |
8441 | ||
8442 | /* ELF symbols in relocatable files are section relative, but | |
8443 | in executable files they are virtual addresses. Note that | |
8444 | this code assumes that all ELF sections have an associated | |
8445 | BFD section with a reasonable value for output_offset; below | |
8446 | we assume that they also have a reasonable value for | |
8447 | output_section. Any special sections must be set up to meet | |
8448 | these requirements. */ | |
8449 | osym.st_value += isec->output_offset; | |
8450 | if (! finfo->info->relocatable) | |
8451 | { | |
8452 | osym.st_value += isec->output_section->vma; | |
8453 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
8454 | { | |
8455 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
8456 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
8457 | osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
8458 | } | |
8459 | } | |
8460 | ||
8461 | if (! elf_link_output_sym (finfo, name, &osym, isec, NULL)) | |
8462 | return FALSE; | |
8463 | } | |
8464 | ||
d9352518 DB |
8465 | if (! evaluate_complex_relocation_symbols (input_bfd, finfo, locsymcount)) |
8466 | return FALSE; | |
8467 | ||
c152c796 AM |
8468 | /* Relocate the contents of each section. */ |
8469 | sym_hashes = elf_sym_hashes (input_bfd); | |
8470 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
8471 | { | |
8472 | bfd_byte *contents; | |
8473 | ||
8474 | if (! o->linker_mark) | |
8475 | { | |
8476 | /* This section was omitted from the link. */ | |
8477 | continue; | |
8478 | } | |
8479 | ||
8480 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 8481 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
8482 | continue; |
8483 | ||
8484 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
8485 | { | |
8486 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
8487 | or somesuch. */ | |
8488 | continue; | |
8489 | } | |
8490 | ||
8491 | /* Get the contents of the section. They have been cached by a | |
8492 | relaxation routine. Note that o is a section in an input | |
8493 | file, so the contents field will not have been set by any of | |
8494 | the routines which work on output files. */ | |
8495 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
8496 | contents = elf_section_data (o)->this_hdr.contents; | |
8497 | else | |
8498 | { | |
eea6121a AM |
8499 | bfd_size_type amt = o->rawsize ? o->rawsize : o->size; |
8500 | ||
c152c796 | 8501 | contents = finfo->contents; |
eea6121a | 8502 | if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt)) |
c152c796 AM |
8503 | return FALSE; |
8504 | } | |
8505 | ||
8506 | if ((o->flags & SEC_RELOC) != 0) | |
8507 | { | |
8508 | Elf_Internal_Rela *internal_relocs; | |
8509 | bfd_vma r_type_mask; | |
8510 | int r_sym_shift; | |
ece5ef60 | 8511 | int ret; |
c152c796 AM |
8512 | |
8513 | /* Get the swapped relocs. */ | |
8514 | internal_relocs | |
8515 | = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, | |
8516 | finfo->internal_relocs, FALSE); | |
8517 | if (internal_relocs == NULL | |
8518 | && o->reloc_count > 0) | |
8519 | return FALSE; | |
8520 | ||
8521 | if (bed->s->arch_size == 32) | |
8522 | { | |
8523 | r_type_mask = 0xff; | |
8524 | r_sym_shift = 8; | |
8525 | } | |
8526 | else | |
8527 | { | |
8528 | r_type_mask = 0xffffffff; | |
8529 | r_sym_shift = 32; | |
8530 | } | |
8531 | ||
8532 | /* Run through the relocs looking for any against symbols | |
8533 | from discarded sections and section symbols from | |
8534 | removed link-once sections. Complain about relocs | |
8535 | against discarded sections. Zero relocs against removed | |
e0ae6d6f | 8536 | link-once sections. */ |
c152c796 AM |
8537 | if (!elf_section_ignore_discarded_relocs (o)) |
8538 | { | |
8539 | Elf_Internal_Rela *rel, *relend; | |
8a696751 | 8540 | unsigned int action = (*bed->action_discarded) (o); |
c152c796 AM |
8541 | |
8542 | rel = internal_relocs; | |
8543 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
8544 | for ( ; rel < relend; rel++) | |
8545 | { | |
8546 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
cdd3575c AM |
8547 | asection **ps, *sec; |
8548 | struct elf_link_hash_entry *h = NULL; | |
8549 | const char *sym_name; | |
c152c796 | 8550 | |
ee75fd95 AM |
8551 | if (r_symndx == STN_UNDEF) |
8552 | continue; | |
8553 | ||
c152c796 AM |
8554 | if (r_symndx >= locsymcount |
8555 | || (elf_bad_symtab (input_bfd) | |
8556 | && finfo->sections[r_symndx] == NULL)) | |
8557 | { | |
c152c796 | 8558 | h = sym_hashes[r_symndx - extsymoff]; |
dce669a1 | 8559 | |
8c19749a NC |
8560 | /* Badly formatted input files can contain relocs that |
8561 | reference non-existant symbols. Check here so that | |
8562 | we do not seg fault. */ | |
8563 | if (h == NULL) | |
8564 | { | |
8565 | char buffer [32]; | |
8566 | ||
8567 | sprintf_vma (buffer, rel->r_info); | |
8568 | (*_bfd_error_handler) | |
8569 | (_("error: %B contains a reloc (0x%s) for section %A " | |
8570 | "that references a non-existent global symbol"), | |
8571 | input_bfd, o, buffer); | |
8572 | bfd_set_error (bfd_error_bad_value); | |
8573 | return FALSE; | |
8574 | } | |
3b36f7e6 | 8575 | |
c152c796 AM |
8576 | while (h->root.type == bfd_link_hash_indirect |
8577 | || h->root.type == bfd_link_hash_warning) | |
8578 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8579 | ||
cdd3575c AM |
8580 | if (h->root.type != bfd_link_hash_defined |
8581 | && h->root.type != bfd_link_hash_defweak) | |
8582 | continue; | |
8583 | ||
8584 | ps = &h->root.u.def.section; | |
8585 | sym_name = h->root.root.string; | |
c152c796 AM |
8586 | } |
8587 | else | |
8588 | { | |
cdd3575c AM |
8589 | Elf_Internal_Sym *sym = isymbuf + r_symndx; |
8590 | ps = &finfo->sections[r_symndx]; | |
26c61ae5 L |
8591 | sym_name = bfd_elf_sym_name (input_bfd, |
8592 | symtab_hdr, | |
8593 | sym, *ps); | |
cdd3575c | 8594 | } |
c152c796 | 8595 | |
cdd3575c AM |
8596 | /* Complain if the definition comes from a |
8597 | discarded section. */ | |
8598 | if ((sec = *ps) != NULL && elf_discarded_section (sec)) | |
8599 | { | |
87e5235d | 8600 | BFD_ASSERT (r_symndx != 0); |
9e66c942 | 8601 | if (action & COMPLAIN) |
e1fffbe6 AM |
8602 | (*finfo->info->callbacks->einfo) |
8603 | (_("%X`%s' referenced in section `%A' of %B: " | |
58ac56d0 | 8604 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 8605 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 8606 | |
87e5235d | 8607 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 8608 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
8609 | really defined in the kept linkonce section. |
8610 | FIXME: This is quite broken. Modifying the | |
8611 | symbol here means we will be changing all later | |
e0ae6d6f | 8612 | uses of the symbol, not just in this section. */ |
01b3c8ab | 8613 | if (action & PRETEND) |
87e5235d | 8614 | { |
01b3c8ab L |
8615 | asection *kept; |
8616 | ||
c0f00686 L |
8617 | kept = _bfd_elf_check_kept_section (sec, |
8618 | finfo->info); | |
01b3c8ab | 8619 | if (kept != NULL) |
87e5235d AM |
8620 | { |
8621 | *ps = kept; | |
8622 | continue; | |
8623 | } | |
8624 | } | |
c152c796 AM |
8625 | } |
8626 | } | |
8627 | } | |
8628 | ||
8629 | /* Relocate the section by invoking a back end routine. | |
8630 | ||
8631 | The back end routine is responsible for adjusting the | |
8632 | section contents as necessary, and (if using Rela relocs | |
8633 | and generating a relocatable output file) adjusting the | |
8634 | reloc addend as necessary. | |
8635 | ||
8636 | The back end routine does not have to worry about setting | |
8637 | the reloc address or the reloc symbol index. | |
8638 | ||
8639 | The back end routine is given a pointer to the swapped in | |
8640 | internal symbols, and can access the hash table entries | |
8641 | for the external symbols via elf_sym_hashes (input_bfd). | |
8642 | ||
8643 | When generating relocatable output, the back end routine | |
8644 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
8645 | output symbol is going to be a section symbol | |
8646 | corresponding to the output section, which will require | |
8647 | the addend to be adjusted. */ | |
8648 | ||
ece5ef60 | 8649 | ret = (*relocate_section) (output_bfd, finfo->info, |
c152c796 AM |
8650 | input_bfd, o, contents, |
8651 | internal_relocs, | |
8652 | isymbuf, | |
ece5ef60 AM |
8653 | finfo->sections); |
8654 | if (!ret) | |
c152c796 AM |
8655 | return FALSE; |
8656 | ||
ece5ef60 AM |
8657 | if (ret == 2 |
8658 | || finfo->info->relocatable | |
8659 | || finfo->info->emitrelocations) | |
c152c796 AM |
8660 | { |
8661 | Elf_Internal_Rela *irela; | |
8662 | Elf_Internal_Rela *irelaend; | |
8663 | bfd_vma last_offset; | |
8664 | struct elf_link_hash_entry **rel_hash; | |
eac338cf | 8665 | struct elf_link_hash_entry **rel_hash_list; |
c152c796 AM |
8666 | Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2; |
8667 | unsigned int next_erel; | |
c152c796 AM |
8668 | bfd_boolean rela_normal; |
8669 | ||
8670 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
8671 | rela_normal = (bed->rela_normal | |
8672 | && (input_rel_hdr->sh_entsize | |
8673 | == bed->s->sizeof_rela)); | |
8674 | ||
8675 | /* Adjust the reloc addresses and symbol indices. */ | |
8676 | ||
8677 | irela = internal_relocs; | |
8678 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
8679 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
8680 | + elf_section_data (o->output_section)->rel_count | |
8681 | + elf_section_data (o->output_section)->rel_count2); | |
eac338cf | 8682 | rel_hash_list = rel_hash; |
c152c796 AM |
8683 | last_offset = o->output_offset; |
8684 | if (!finfo->info->relocatable) | |
8685 | last_offset += o->output_section->vma; | |
8686 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
8687 | { | |
8688 | unsigned long r_symndx; | |
8689 | asection *sec; | |
8690 | Elf_Internal_Sym sym; | |
8691 | ||
8692 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
8693 | { | |
8694 | rel_hash++; | |
8695 | next_erel = 0; | |
8696 | } | |
8697 | ||
8698 | irela->r_offset = _bfd_elf_section_offset (output_bfd, | |
8699 | finfo->info, o, | |
8700 | irela->r_offset); | |
8701 | if (irela->r_offset >= (bfd_vma) -2) | |
8702 | { | |
8703 | /* This is a reloc for a deleted entry or somesuch. | |
8704 | Turn it into an R_*_NONE reloc, at the same | |
8705 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 8706 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
8707 | being ordered. */ |
8708 | irela->r_offset = last_offset; | |
8709 | irela->r_info = 0; | |
8710 | irela->r_addend = 0; | |
8711 | continue; | |
8712 | } | |
8713 | ||
8714 | irela->r_offset += o->output_offset; | |
8715 | ||
8716 | /* Relocs in an executable have to be virtual addresses. */ | |
8717 | if (!finfo->info->relocatable) | |
8718 | irela->r_offset += o->output_section->vma; | |
8719 | ||
8720 | last_offset = irela->r_offset; | |
8721 | ||
8722 | r_symndx = irela->r_info >> r_sym_shift; | |
8723 | if (r_symndx == STN_UNDEF) | |
8724 | continue; | |
8725 | ||
8726 | if (r_symndx >= locsymcount | |
8727 | || (elf_bad_symtab (input_bfd) | |
8728 | && finfo->sections[r_symndx] == NULL)) | |
8729 | { | |
8730 | struct elf_link_hash_entry *rh; | |
8731 | unsigned long indx; | |
8732 | ||
8733 | /* This is a reloc against a global symbol. We | |
8734 | have not yet output all the local symbols, so | |
8735 | we do not know the symbol index of any global | |
8736 | symbol. We set the rel_hash entry for this | |
8737 | reloc to point to the global hash table entry | |
8738 | for this symbol. The symbol index is then | |
ee75fd95 | 8739 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
8740 | indx = r_symndx - extsymoff; |
8741 | rh = elf_sym_hashes (input_bfd)[indx]; | |
8742 | while (rh->root.type == bfd_link_hash_indirect | |
8743 | || rh->root.type == bfd_link_hash_warning) | |
8744 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
8745 | ||
8746 | /* Setting the index to -2 tells | |
8747 | elf_link_output_extsym that this symbol is | |
8748 | used by a reloc. */ | |
8749 | BFD_ASSERT (rh->indx < 0); | |
8750 | rh->indx = -2; | |
8751 | ||
8752 | *rel_hash = rh; | |
8753 | ||
8754 | continue; | |
8755 | } | |
8756 | ||
8757 | /* This is a reloc against a local symbol. */ | |
8758 | ||
8759 | *rel_hash = NULL; | |
8760 | sym = isymbuf[r_symndx]; | |
8761 | sec = finfo->sections[r_symndx]; | |
8762 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) | |
8763 | { | |
8764 | /* I suppose the backend ought to fill in the | |
8765 | section of any STT_SECTION symbol against a | |
6a8d1586 AM |
8766 | processor specific section. */ |
8767 | r_symndx = 0; | |
8768 | if (bfd_is_abs_section (sec)) | |
8769 | ; | |
c152c796 AM |
8770 | else if (sec == NULL || sec->owner == NULL) |
8771 | { | |
8772 | bfd_set_error (bfd_error_bad_value); | |
8773 | return FALSE; | |
8774 | } | |
8775 | else | |
8776 | { | |
6a8d1586 AM |
8777 | asection *osec = sec->output_section; |
8778 | ||
8779 | /* If we have discarded a section, the output | |
8780 | section will be the absolute section. In | |
ab96bf03 AM |
8781 | case of discarded SEC_MERGE sections, use |
8782 | the kept section. relocate_section should | |
8783 | have already handled discarded linkonce | |
8784 | sections. */ | |
6a8d1586 AM |
8785 | if (bfd_is_abs_section (osec) |
8786 | && sec->kept_section != NULL | |
8787 | && sec->kept_section->output_section != NULL) | |
8788 | { | |
8789 | osec = sec->kept_section->output_section; | |
8790 | irela->r_addend -= osec->vma; | |
8791 | } | |
8792 | ||
8793 | if (!bfd_is_abs_section (osec)) | |
8794 | { | |
8795 | r_symndx = osec->target_index; | |
74541ad4 AM |
8796 | if (r_symndx == 0) |
8797 | { | |
8798 | struct elf_link_hash_table *htab; | |
8799 | asection *oi; | |
8800 | ||
8801 | htab = elf_hash_table (finfo->info); | |
8802 | oi = htab->text_index_section; | |
8803 | if ((osec->flags & SEC_READONLY) == 0 | |
8804 | && htab->data_index_section != NULL) | |
8805 | oi = htab->data_index_section; | |
8806 | ||
8807 | if (oi != NULL) | |
8808 | { | |
8809 | irela->r_addend += osec->vma - oi->vma; | |
8810 | r_symndx = oi->target_index; | |
8811 | } | |
8812 | } | |
8813 | ||
6a8d1586 AM |
8814 | BFD_ASSERT (r_symndx != 0); |
8815 | } | |
c152c796 AM |
8816 | } |
8817 | ||
8818 | /* Adjust the addend according to where the | |
8819 | section winds up in the output section. */ | |
8820 | if (rela_normal) | |
8821 | irela->r_addend += sec->output_offset; | |
8822 | } | |
8823 | else | |
8824 | { | |
8825 | if (finfo->indices[r_symndx] == -1) | |
8826 | { | |
8827 | unsigned long shlink; | |
8828 | const char *name; | |
8829 | asection *osec; | |
8830 | ||
8831 | if (finfo->info->strip == strip_all) | |
8832 | { | |
8833 | /* You can't do ld -r -s. */ | |
8834 | bfd_set_error (bfd_error_invalid_operation); | |
8835 | return FALSE; | |
8836 | } | |
8837 | ||
8838 | /* This symbol was skipped earlier, but | |
8839 | since it is needed by a reloc, we | |
8840 | must output it now. */ | |
8841 | shlink = symtab_hdr->sh_link; | |
8842 | name = (bfd_elf_string_from_elf_section | |
8843 | (input_bfd, shlink, sym.st_name)); | |
8844 | if (name == NULL) | |
8845 | return FALSE; | |
8846 | ||
8847 | osec = sec->output_section; | |
8848 | sym.st_shndx = | |
8849 | _bfd_elf_section_from_bfd_section (output_bfd, | |
8850 | osec); | |
8851 | if (sym.st_shndx == SHN_BAD) | |
8852 | return FALSE; | |
8853 | ||
8854 | sym.st_value += sec->output_offset; | |
8855 | if (! finfo->info->relocatable) | |
8856 | { | |
8857 | sym.st_value += osec->vma; | |
8858 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
8859 | { | |
8860 | /* STT_TLS symbols are relative to PT_TLS | |
8861 | segment base. */ | |
8862 | BFD_ASSERT (elf_hash_table (finfo->info) | |
8863 | ->tls_sec != NULL); | |
8864 | sym.st_value -= (elf_hash_table (finfo->info) | |
8865 | ->tls_sec->vma); | |
8866 | } | |
8867 | } | |
8868 | ||
8869 | finfo->indices[r_symndx] | |
8870 | = bfd_get_symcount (output_bfd); | |
8871 | ||
8872 | if (! elf_link_output_sym (finfo, name, &sym, sec, | |
8873 | NULL)) | |
8874 | return FALSE; | |
8875 | } | |
8876 | ||
8877 | r_symndx = finfo->indices[r_symndx]; | |
8878 | } | |
8879 | ||
8880 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
8881 | | (irela->r_info & r_type_mask)); | |
8882 | } | |
8883 | ||
8884 | /* Swap out the relocs. */ | |
c152c796 | 8885 | if (input_rel_hdr->sh_size != 0 |
eac338cf PB |
8886 | && !bed->elf_backend_emit_relocs (output_bfd, o, |
8887 | input_rel_hdr, | |
8888 | internal_relocs, | |
8889 | rel_hash_list)) | |
c152c796 AM |
8890 | return FALSE; |
8891 | ||
8892 | input_rel_hdr2 = elf_section_data (o)->rel_hdr2; | |
8893 | if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0) | |
8894 | { | |
8895 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) | |
8896 | * bed->s->int_rels_per_ext_rel); | |
eac338cf PB |
8897 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
8898 | if (!bed->elf_backend_emit_relocs (output_bfd, o, | |
8899 | input_rel_hdr2, | |
8900 | internal_relocs, | |
8901 | rel_hash_list)) | |
c152c796 AM |
8902 | return FALSE; |
8903 | } | |
8904 | } | |
8905 | } | |
8906 | ||
8907 | /* Write out the modified section contents. */ | |
8908 | if (bed->elf_backend_write_section | |
c7b8f16e JB |
8909 | && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o, |
8910 | contents)) | |
c152c796 AM |
8911 | { |
8912 | /* Section written out. */ | |
8913 | } | |
8914 | else switch (o->sec_info_type) | |
8915 | { | |
8916 | case ELF_INFO_TYPE_STABS: | |
8917 | if (! (_bfd_write_section_stabs | |
8918 | (output_bfd, | |
8919 | &elf_hash_table (finfo->info)->stab_info, | |
8920 | o, &elf_section_data (o)->sec_info, contents))) | |
8921 | return FALSE; | |
8922 | break; | |
8923 | case ELF_INFO_TYPE_MERGE: | |
8924 | if (! _bfd_write_merged_section (output_bfd, o, | |
8925 | elf_section_data (o)->sec_info)) | |
8926 | return FALSE; | |
8927 | break; | |
8928 | case ELF_INFO_TYPE_EH_FRAME: | |
8929 | { | |
8930 | if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, | |
8931 | o, contents)) | |
8932 | return FALSE; | |
8933 | } | |
8934 | break; | |
8935 | default: | |
8936 | { | |
c152c796 AM |
8937 | if (! (o->flags & SEC_EXCLUDE) |
8938 | && ! bfd_set_section_contents (output_bfd, o->output_section, | |
8939 | contents, | |
8940 | (file_ptr) o->output_offset, | |
eea6121a | 8941 | o->size)) |
c152c796 AM |
8942 | return FALSE; |
8943 | } | |
8944 | break; | |
8945 | } | |
8946 | } | |
8947 | ||
8948 | return TRUE; | |
8949 | } | |
8950 | ||
8951 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 8952 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
8953 | is used to build constructor and destructor tables when linking |
8954 | with -Ur. */ | |
8955 | ||
8956 | static bfd_boolean | |
8957 | elf_reloc_link_order (bfd *output_bfd, | |
8958 | struct bfd_link_info *info, | |
8959 | asection *output_section, | |
8960 | struct bfd_link_order *link_order) | |
8961 | { | |
8962 | reloc_howto_type *howto; | |
8963 | long indx; | |
8964 | bfd_vma offset; | |
8965 | bfd_vma addend; | |
8966 | struct elf_link_hash_entry **rel_hash_ptr; | |
8967 | Elf_Internal_Shdr *rel_hdr; | |
8968 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
8969 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
8970 | bfd_byte *erel; | |
8971 | unsigned int i; | |
8972 | ||
8973 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
8974 | if (howto == NULL) | |
8975 | { | |
8976 | bfd_set_error (bfd_error_bad_value); | |
8977 | return FALSE; | |
8978 | } | |
8979 | ||
8980 | addend = link_order->u.reloc.p->addend; | |
8981 | ||
8982 | /* Figure out the symbol index. */ | |
8983 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
8984 | + elf_section_data (output_section)->rel_count | |
8985 | + elf_section_data (output_section)->rel_count2); | |
8986 | if (link_order->type == bfd_section_reloc_link_order) | |
8987 | { | |
8988 | indx = link_order->u.reloc.p->u.section->target_index; | |
8989 | BFD_ASSERT (indx != 0); | |
8990 | *rel_hash_ptr = NULL; | |
8991 | } | |
8992 | else | |
8993 | { | |
8994 | struct elf_link_hash_entry *h; | |
8995 | ||
8996 | /* Treat a reloc against a defined symbol as though it were | |
8997 | actually against the section. */ | |
8998 | h = ((struct elf_link_hash_entry *) | |
8999 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
9000 | link_order->u.reloc.p->u.name, | |
9001 | FALSE, FALSE, TRUE)); | |
9002 | if (h != NULL | |
9003 | && (h->root.type == bfd_link_hash_defined | |
9004 | || h->root.type == bfd_link_hash_defweak)) | |
9005 | { | |
9006 | asection *section; | |
9007 | ||
9008 | section = h->root.u.def.section; | |
9009 | indx = section->output_section->target_index; | |
9010 | *rel_hash_ptr = NULL; | |
9011 | /* It seems that we ought to add the symbol value to the | |
9012 | addend here, but in practice it has already been added | |
9013 | because it was passed to constructor_callback. */ | |
9014 | addend += section->output_section->vma + section->output_offset; | |
9015 | } | |
9016 | else if (h != NULL) | |
9017 | { | |
9018 | /* Setting the index to -2 tells elf_link_output_extsym that | |
9019 | this symbol is used by a reloc. */ | |
9020 | h->indx = -2; | |
9021 | *rel_hash_ptr = h; | |
9022 | indx = 0; | |
9023 | } | |
9024 | else | |
9025 | { | |
9026 | if (! ((*info->callbacks->unattached_reloc) | |
9027 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) | |
9028 | return FALSE; | |
9029 | indx = 0; | |
9030 | } | |
9031 | } | |
9032 | ||
9033 | /* If this is an inplace reloc, we must write the addend into the | |
9034 | object file. */ | |
9035 | if (howto->partial_inplace && addend != 0) | |
9036 | { | |
9037 | bfd_size_type size; | |
9038 | bfd_reloc_status_type rstat; | |
9039 | bfd_byte *buf; | |
9040 | bfd_boolean ok; | |
9041 | const char *sym_name; | |
9042 | ||
9043 | size = bfd_get_reloc_size (howto); | |
9044 | buf = bfd_zmalloc (size); | |
9045 | if (buf == NULL) | |
9046 | return FALSE; | |
9047 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
9048 | switch (rstat) | |
9049 | { | |
9050 | case bfd_reloc_ok: | |
9051 | break; | |
9052 | ||
9053 | default: | |
9054 | case bfd_reloc_outofrange: | |
9055 | abort (); | |
9056 | ||
9057 | case bfd_reloc_overflow: | |
9058 | if (link_order->type == bfd_section_reloc_link_order) | |
9059 | sym_name = bfd_section_name (output_bfd, | |
9060 | link_order->u.reloc.p->u.section); | |
9061 | else | |
9062 | sym_name = link_order->u.reloc.p->u.name; | |
9063 | if (! ((*info->callbacks->reloc_overflow) | |
dfeffb9f L |
9064 | (info, NULL, sym_name, howto->name, addend, NULL, |
9065 | NULL, (bfd_vma) 0))) | |
c152c796 AM |
9066 | { |
9067 | free (buf); | |
9068 | return FALSE; | |
9069 | } | |
9070 | break; | |
9071 | } | |
9072 | ok = bfd_set_section_contents (output_bfd, output_section, buf, | |
9073 | link_order->offset, size); | |
9074 | free (buf); | |
9075 | if (! ok) | |
9076 | return FALSE; | |
9077 | } | |
9078 | ||
9079 | /* The address of a reloc is relative to the section in a | |
9080 | relocatable file, and is a virtual address in an executable | |
9081 | file. */ | |
9082 | offset = link_order->offset; | |
9083 | if (! info->relocatable) | |
9084 | offset += output_section->vma; | |
9085 | ||
9086 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
9087 | { | |
9088 | irel[i].r_offset = offset; | |
9089 | irel[i].r_info = 0; | |
9090 | irel[i].r_addend = 0; | |
9091 | } | |
9092 | if (bed->s->arch_size == 32) | |
9093 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
9094 | else | |
9095 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
9096 | ||
9097 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
9098 | erel = rel_hdr->contents; | |
9099 | if (rel_hdr->sh_type == SHT_REL) | |
9100 | { | |
9101 | erel += (elf_section_data (output_section)->rel_count | |
9102 | * bed->s->sizeof_rel); | |
9103 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); | |
9104 | } | |
9105 | else | |
9106 | { | |
9107 | irel[0].r_addend = addend; | |
9108 | erel += (elf_section_data (output_section)->rel_count | |
9109 | * bed->s->sizeof_rela); | |
9110 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); | |
9111 | } | |
9112 | ||
9113 | ++elf_section_data (output_section)->rel_count; | |
9114 | ||
9115 | return TRUE; | |
9116 | } | |
9117 | ||
0b52efa6 PB |
9118 | |
9119 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
9120 | ||
9121 | static bfd_vma | |
9122 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
9123 | { | |
9124 | Elf_Internal_Shdr **elf_shdrp; | |
9125 | asection *s; | |
9126 | int elfsec; | |
9127 | ||
9128 | s = p->u.indirect.section; | |
9129 | elf_shdrp = elf_elfsections (s->owner); | |
9130 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
9131 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
9132 | /* PR 290: |
9133 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 9134 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
9135 | sh_info fields. Hence we could get the situation |
9136 | where elfsec is 0. */ | |
9137 | if (elfsec == 0) | |
9138 | { | |
9139 | const struct elf_backend_data *bed | |
9140 | = get_elf_backend_data (s->owner); | |
9141 | if (bed->link_order_error_handler) | |
d003868e AM |
9142 | bed->link_order_error_handler |
9143 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); | |
185d09ad L |
9144 | return 0; |
9145 | } | |
9146 | else | |
9147 | { | |
9148 | s = elf_shdrp[elfsec]->bfd_section; | |
9149 | return s->output_section->vma + s->output_offset; | |
9150 | } | |
0b52efa6 PB |
9151 | } |
9152 | ||
9153 | ||
9154 | /* Compare two sections based on the locations of the sections they are | |
9155 | linked to. Used by elf_fixup_link_order. */ | |
9156 | ||
9157 | static int | |
9158 | compare_link_order (const void * a, const void * b) | |
9159 | { | |
9160 | bfd_vma apos; | |
9161 | bfd_vma bpos; | |
9162 | ||
9163 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
9164 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
9165 | if (apos < bpos) | |
9166 | return -1; | |
9167 | return apos > bpos; | |
9168 | } | |
9169 | ||
9170 | ||
9171 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
9172 | order as their linked sections. Returns false if this could not be done | |
9173 | because an output section includes both ordered and unordered | |
9174 | sections. Ideally we'd do this in the linker proper. */ | |
9175 | ||
9176 | static bfd_boolean | |
9177 | elf_fixup_link_order (bfd *abfd, asection *o) | |
9178 | { | |
9179 | int seen_linkorder; | |
9180 | int seen_other; | |
9181 | int n; | |
9182 | struct bfd_link_order *p; | |
9183 | bfd *sub; | |
9184 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 9185 | unsigned elfsec; |
0b52efa6 | 9186 | struct bfd_link_order **sections; |
d33cdfe3 | 9187 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 9188 | bfd_vma offset; |
3b36f7e6 | 9189 | |
d33cdfe3 L |
9190 | other_sec = NULL; |
9191 | linkorder_sec = NULL; | |
0b52efa6 PB |
9192 | seen_other = 0; |
9193 | seen_linkorder = 0; | |
8423293d | 9194 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 9195 | { |
d33cdfe3 | 9196 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
9197 | { |
9198 | s = p->u.indirect.section; | |
d33cdfe3 L |
9199 | sub = s->owner; |
9200 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
9201 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
9202 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
9203 | && elfsec < elf_numsections (sub) | |
0b52efa6 | 9204 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER) |
d33cdfe3 L |
9205 | { |
9206 | seen_linkorder++; | |
9207 | linkorder_sec = s; | |
9208 | } | |
0b52efa6 | 9209 | else |
d33cdfe3 L |
9210 | { |
9211 | seen_other++; | |
9212 | other_sec = s; | |
9213 | } | |
0b52efa6 PB |
9214 | } |
9215 | else | |
9216 | seen_other++; | |
d33cdfe3 L |
9217 | |
9218 | if (seen_other && seen_linkorder) | |
9219 | { | |
9220 | if (other_sec && linkorder_sec) | |
9221 | (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"), | |
9222 | o, linkorder_sec, | |
9223 | linkorder_sec->owner, other_sec, | |
9224 | other_sec->owner); | |
9225 | else | |
9226 | (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), | |
9227 | o); | |
9228 | bfd_set_error (bfd_error_bad_value); | |
9229 | return FALSE; | |
9230 | } | |
0b52efa6 PB |
9231 | } |
9232 | ||
9233 | if (!seen_linkorder) | |
9234 | return TRUE; | |
9235 | ||
0b52efa6 PB |
9236 | sections = (struct bfd_link_order **) |
9237 | xmalloc (seen_linkorder * sizeof (struct bfd_link_order *)); | |
9238 | seen_linkorder = 0; | |
3b36f7e6 | 9239 | |
8423293d | 9240 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
9241 | { |
9242 | sections[seen_linkorder++] = p; | |
9243 | } | |
9244 | /* Sort the input sections in the order of their linked section. */ | |
9245 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
9246 | compare_link_order); | |
9247 | ||
9248 | /* Change the offsets of the sections. */ | |
9249 | offset = 0; | |
9250 | for (n = 0; n < seen_linkorder; n++) | |
9251 | { | |
9252 | s = sections[n]->u.indirect.section; | |
9253 | offset &= ~(bfd_vma)((1 << s->alignment_power) - 1); | |
9254 | s->output_offset = offset; | |
9255 | sections[n]->offset = offset; | |
9256 | offset += sections[n]->size; | |
9257 | } | |
9258 | ||
9259 | return TRUE; | |
9260 | } | |
9261 | ||
9262 | ||
c152c796 AM |
9263 | /* Do the final step of an ELF link. */ |
9264 | ||
9265 | bfd_boolean | |
9266 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
9267 | { | |
9268 | bfd_boolean dynamic; | |
9269 | bfd_boolean emit_relocs; | |
9270 | bfd *dynobj; | |
9271 | struct elf_final_link_info finfo; | |
9272 | register asection *o; | |
9273 | register struct bfd_link_order *p; | |
9274 | register bfd *sub; | |
9275 | bfd_size_type max_contents_size; | |
9276 | bfd_size_type max_external_reloc_size; | |
9277 | bfd_size_type max_internal_reloc_count; | |
9278 | bfd_size_type max_sym_count; | |
9279 | bfd_size_type max_sym_shndx_count; | |
9280 | file_ptr off; | |
9281 | Elf_Internal_Sym elfsym; | |
9282 | unsigned int i; | |
9283 | Elf_Internal_Shdr *symtab_hdr; | |
9284 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
9285 | Elf_Internal_Shdr *symstrtab_hdr; | |
9286 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9287 | struct elf_outext_info eoinfo; | |
9288 | bfd_boolean merged; | |
9289 | size_t relativecount = 0; | |
9290 | asection *reldyn = 0; | |
9291 | bfd_size_type amt; | |
104d59d1 JM |
9292 | asection *attr_section = NULL; |
9293 | bfd_vma attr_size = 0; | |
9294 | const char *std_attrs_section; | |
c152c796 AM |
9295 | |
9296 | if (! is_elf_hash_table (info->hash)) | |
9297 | return FALSE; | |
9298 | ||
9299 | if (info->shared) | |
9300 | abfd->flags |= DYNAMIC; | |
9301 | ||
9302 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
9303 | dynobj = elf_hash_table (info)->dynobj; | |
9304 | ||
9305 | emit_relocs = (info->relocatable | |
a4676736 | 9306 | || info->emitrelocations); |
c152c796 AM |
9307 | |
9308 | finfo.info = info; | |
9309 | finfo.output_bfd = abfd; | |
9310 | finfo.symstrtab = _bfd_elf_stringtab_init (); | |
9311 | if (finfo.symstrtab == NULL) | |
9312 | return FALSE; | |
9313 | ||
9314 | if (! dynamic) | |
9315 | { | |
9316 | finfo.dynsym_sec = NULL; | |
9317 | finfo.hash_sec = NULL; | |
9318 | finfo.symver_sec = NULL; | |
9319 | } | |
9320 | else | |
9321 | { | |
9322 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
9323 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
fdc90cb4 | 9324 | BFD_ASSERT (finfo.dynsym_sec != NULL); |
c152c796 AM |
9325 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); |
9326 | /* Note that it is OK if symver_sec is NULL. */ | |
9327 | } | |
9328 | ||
9329 | finfo.contents = NULL; | |
9330 | finfo.external_relocs = NULL; | |
9331 | finfo.internal_relocs = NULL; | |
9332 | finfo.external_syms = NULL; | |
9333 | finfo.locsym_shndx = NULL; | |
9334 | finfo.internal_syms = NULL; | |
9335 | finfo.indices = NULL; | |
9336 | finfo.sections = NULL; | |
9337 | finfo.symbuf = NULL; | |
9338 | finfo.symshndxbuf = NULL; | |
9339 | finfo.symbuf_count = 0; | |
9340 | finfo.shndxbuf_size = 0; | |
9341 | ||
104d59d1 JM |
9342 | /* The object attributes have been merged. Remove the input |
9343 | sections from the link, and set the contents of the output | |
9344 | secton. */ | |
9345 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; | |
9346 | for (o = abfd->sections; o != NULL; o = o->next) | |
9347 | { | |
9348 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) | |
9349 | || strcmp (o->name, ".gnu.attributes") == 0) | |
9350 | { | |
9351 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
9352 | { | |
9353 | asection *input_section; | |
9354 | ||
9355 | if (p->type != bfd_indirect_link_order) | |
9356 | continue; | |
9357 | input_section = p->u.indirect.section; | |
9358 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
9359 | elf_link_input_bfd ignores this section. */ | |
9360 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
9361 | } | |
9362 | ||
9363 | attr_size = bfd_elf_obj_attr_size (abfd); | |
9364 | if (attr_size) | |
9365 | { | |
9366 | bfd_set_section_size (abfd, o, attr_size); | |
9367 | attr_section = o; | |
9368 | /* Skip this section later on. */ | |
9369 | o->map_head.link_order = NULL; | |
9370 | } | |
9371 | else | |
9372 | o->flags |= SEC_EXCLUDE; | |
9373 | } | |
9374 | } | |
9375 | ||
c152c796 AM |
9376 | /* Count up the number of relocations we will output for each output |
9377 | section, so that we know the sizes of the reloc sections. We | |
9378 | also figure out some maximum sizes. */ | |
9379 | max_contents_size = 0; | |
9380 | max_external_reloc_size = 0; | |
9381 | max_internal_reloc_count = 0; | |
9382 | max_sym_count = 0; | |
9383 | max_sym_shndx_count = 0; | |
9384 | merged = FALSE; | |
9385 | for (o = abfd->sections; o != NULL; o = o->next) | |
9386 | { | |
9387 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
9388 | o->reloc_count = 0; | |
9389 | ||
8423293d | 9390 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
9391 | { |
9392 | unsigned int reloc_count = 0; | |
9393 | struct bfd_elf_section_data *esdi = NULL; | |
9394 | unsigned int *rel_count1; | |
9395 | ||
9396 | if (p->type == bfd_section_reloc_link_order | |
9397 | || p->type == bfd_symbol_reloc_link_order) | |
9398 | reloc_count = 1; | |
9399 | else if (p->type == bfd_indirect_link_order) | |
9400 | { | |
9401 | asection *sec; | |
9402 | ||
9403 | sec = p->u.indirect.section; | |
9404 | esdi = elf_section_data (sec); | |
9405 | ||
9406 | /* Mark all sections which are to be included in the | |
9407 | link. This will normally be every section. We need | |
9408 | to do this so that we can identify any sections which | |
9409 | the linker has decided to not include. */ | |
9410 | sec->linker_mark = TRUE; | |
9411 | ||
9412 | if (sec->flags & SEC_MERGE) | |
9413 | merged = TRUE; | |
9414 | ||
9415 | if (info->relocatable || info->emitrelocations) | |
9416 | reloc_count = sec->reloc_count; | |
9417 | else if (bed->elf_backend_count_relocs) | |
9418 | { | |
9419 | Elf_Internal_Rela * relocs; | |
9420 | ||
1182cb93 AM |
9421 | relocs = _bfd_elf_link_read_relocs (sec->owner, sec, |
9422 | NULL, NULL, | |
c152c796 AM |
9423 | info->keep_memory); |
9424 | ||
1182cb93 AM |
9425 | if (relocs != NULL) |
9426 | { | |
9427 | reloc_count | |
9428 | = (*bed->elf_backend_count_relocs) (sec, relocs); | |
c152c796 | 9429 | |
1182cb93 AM |
9430 | if (elf_section_data (sec)->relocs != relocs) |
9431 | free (relocs); | |
9432 | } | |
c152c796 AM |
9433 | } |
9434 | ||
eea6121a AM |
9435 | if (sec->rawsize > max_contents_size) |
9436 | max_contents_size = sec->rawsize; | |
9437 | if (sec->size > max_contents_size) | |
9438 | max_contents_size = sec->size; | |
c152c796 AM |
9439 | |
9440 | /* We are interested in just local symbols, not all | |
9441 | symbols. */ | |
9442 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
9443 | && (sec->owner->flags & DYNAMIC) == 0) | |
9444 | { | |
9445 | size_t sym_count; | |
9446 | ||
9447 | if (elf_bad_symtab (sec->owner)) | |
9448 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
9449 | / bed->s->sizeof_sym); | |
9450 | else | |
9451 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
9452 | ||
9453 | if (sym_count > max_sym_count) | |
9454 | max_sym_count = sym_count; | |
9455 | ||
9456 | if (sym_count > max_sym_shndx_count | |
9457 | && elf_symtab_shndx (sec->owner) != 0) | |
9458 | max_sym_shndx_count = sym_count; | |
9459 | ||
9460 | if ((sec->flags & SEC_RELOC) != 0) | |
9461 | { | |
9462 | size_t ext_size; | |
9463 | ||
9464 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
9465 | if (ext_size > max_external_reloc_size) | |
9466 | max_external_reloc_size = ext_size; | |
9467 | if (sec->reloc_count > max_internal_reloc_count) | |
9468 | max_internal_reloc_count = sec->reloc_count; | |
9469 | } | |
9470 | } | |
9471 | } | |
9472 | ||
9473 | if (reloc_count == 0) | |
9474 | continue; | |
9475 | ||
9476 | o->reloc_count += reloc_count; | |
9477 | ||
9478 | /* MIPS may have a mix of REL and RELA relocs on sections. | |
9479 | To support this curious ABI we keep reloc counts in | |
9480 | elf_section_data too. We must be careful to add the | |
9481 | relocations from the input section to the right output | |
9482 | count. FIXME: Get rid of one count. We have | |
9483 | o->reloc_count == esdo->rel_count + esdo->rel_count2. */ | |
9484 | rel_count1 = &esdo->rel_count; | |
9485 | if (esdi != NULL) | |
9486 | { | |
9487 | bfd_boolean same_size; | |
9488 | bfd_size_type entsize1; | |
9489 | ||
9490 | entsize1 = esdi->rel_hdr.sh_entsize; | |
9491 | BFD_ASSERT (entsize1 == bed->s->sizeof_rel | |
9492 | || entsize1 == bed->s->sizeof_rela); | |
9493 | same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel); | |
9494 | ||
9495 | if (!same_size) | |
9496 | rel_count1 = &esdo->rel_count2; | |
9497 | ||
9498 | if (esdi->rel_hdr2 != NULL) | |
9499 | { | |
9500 | bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize; | |
9501 | unsigned int alt_count; | |
9502 | unsigned int *rel_count2; | |
9503 | ||
9504 | BFD_ASSERT (entsize2 != entsize1 | |
9505 | && (entsize2 == bed->s->sizeof_rel | |
9506 | || entsize2 == bed->s->sizeof_rela)); | |
9507 | ||
9508 | rel_count2 = &esdo->rel_count2; | |
9509 | if (!same_size) | |
9510 | rel_count2 = &esdo->rel_count; | |
9511 | ||
9512 | /* The following is probably too simplistic if the | |
9513 | backend counts output relocs unusually. */ | |
9514 | BFD_ASSERT (bed->elf_backend_count_relocs == NULL); | |
9515 | alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2); | |
9516 | *rel_count2 += alt_count; | |
9517 | reloc_count -= alt_count; | |
9518 | } | |
9519 | } | |
9520 | *rel_count1 += reloc_count; | |
9521 | } | |
9522 | ||
9523 | if (o->reloc_count > 0) | |
9524 | o->flags |= SEC_RELOC; | |
9525 | else | |
9526 | { | |
9527 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
9528 | set it (this is probably a bug) and if it is set | |
9529 | assign_section_numbers will create a reloc section. */ | |
9530 | o->flags &=~ SEC_RELOC; | |
9531 | } | |
9532 | ||
9533 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
9534 | zero. This is done in elf_fake_sections as well, but forcing | |
9535 | the VMA to 0 here will ensure that relocs against these | |
9536 | sections are handled correctly. */ | |
9537 | if ((o->flags & SEC_ALLOC) == 0 | |
9538 | && ! o->user_set_vma) | |
9539 | o->vma = 0; | |
9540 | } | |
9541 | ||
9542 | if (! info->relocatable && merged) | |
9543 | elf_link_hash_traverse (elf_hash_table (info), | |
9544 | _bfd_elf_link_sec_merge_syms, abfd); | |
9545 | ||
9546 | /* Figure out the file positions for everything but the symbol table | |
9547 | and the relocs. We set symcount to force assign_section_numbers | |
9548 | to create a symbol table. */ | |
9549 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; | |
9550 | BFD_ASSERT (! abfd->output_has_begun); | |
9551 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
9552 | goto error_return; | |
9553 | ||
ee75fd95 | 9554 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
9555 | for (o = abfd->sections; o != NULL; o = o->next) |
9556 | { | |
9557 | if ((o->flags & SEC_RELOC) != 0) | |
9558 | { | |
9559 | if (!(_bfd_elf_link_size_reloc_section | |
9560 | (abfd, &elf_section_data (o)->rel_hdr, o))) | |
9561 | goto error_return; | |
9562 | ||
9563 | if (elf_section_data (o)->rel_hdr2 | |
9564 | && !(_bfd_elf_link_size_reloc_section | |
9565 | (abfd, elf_section_data (o)->rel_hdr2, o))) | |
9566 | goto error_return; | |
9567 | } | |
9568 | ||
9569 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
9570 | to count upwards while actually outputting the relocations. */ | |
9571 | elf_section_data (o)->rel_count = 0; | |
9572 | elf_section_data (o)->rel_count2 = 0; | |
9573 | } | |
9574 | ||
9575 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
9576 | ||
9577 | /* We have now assigned file positions for all the sections except | |
9578 | .symtab and .strtab. We start the .symtab section at the current | |
9579 | file position, and write directly to it. We build the .strtab | |
9580 | section in memory. */ | |
9581 | bfd_get_symcount (abfd) = 0; | |
9582 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
9583 | /* sh_name is set in prep_headers. */ | |
9584 | symtab_hdr->sh_type = SHT_SYMTAB; | |
9585 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
9586 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
9587 | /* sh_link is set in assign_section_numbers. */ | |
9588 | /* sh_info is set below. */ | |
9589 | /* sh_offset is set just below. */ | |
9590 | symtab_hdr->sh_addralign = 1 << bed->s->log_file_align; | |
9591 | ||
9592 | off = elf_tdata (abfd)->next_file_pos; | |
9593 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
9594 | ||
9595 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
9596 | incorrect. We do not yet know the size of the .symtab section. | |
9597 | We correct next_file_pos below, after we do know the size. */ | |
9598 | ||
9599 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
9600 | continuously seeking to the right position in the file. */ | |
9601 | if (! info->keep_memory || max_sym_count < 20) | |
9602 | finfo.symbuf_size = 20; | |
9603 | else | |
9604 | finfo.symbuf_size = max_sym_count; | |
9605 | amt = finfo.symbuf_size; | |
9606 | amt *= bed->s->sizeof_sym; | |
9607 | finfo.symbuf = bfd_malloc (amt); | |
9608 | if (finfo.symbuf == NULL) | |
9609 | goto error_return; | |
9610 | if (elf_numsections (abfd) > SHN_LORESERVE) | |
9611 | { | |
9612 | /* Wild guess at number of output symbols. realloc'd as needed. */ | |
9613 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
9614 | finfo.shndxbuf_size = amt; | |
9615 | amt *= sizeof (Elf_External_Sym_Shndx); | |
9616 | finfo.symshndxbuf = bfd_zmalloc (amt); | |
9617 | if (finfo.symshndxbuf == NULL) | |
9618 | goto error_return; | |
9619 | } | |
9620 | ||
9621 | /* Start writing out the symbol table. The first symbol is always a | |
9622 | dummy symbol. */ | |
9623 | if (info->strip != strip_all | |
9624 | || emit_relocs) | |
9625 | { | |
9626 | elfsym.st_value = 0; | |
9627 | elfsym.st_size = 0; | |
9628 | elfsym.st_info = 0; | |
9629 | elfsym.st_other = 0; | |
9630 | elfsym.st_shndx = SHN_UNDEF; | |
9631 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr, | |
9632 | NULL)) | |
9633 | goto error_return; | |
9634 | } | |
9635 | ||
c152c796 AM |
9636 | /* Output a symbol for each section. We output these even if we are |
9637 | discarding local symbols, since they are used for relocs. These | |
9638 | symbols have no names. We store the index of each one in the | |
9639 | index field of the section, so that we can find it again when | |
9640 | outputting relocs. */ | |
9641 | if (info->strip != strip_all | |
9642 | || emit_relocs) | |
9643 | { | |
9644 | elfsym.st_size = 0; | |
9645 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
9646 | elfsym.st_other = 0; | |
f0b5bb34 | 9647 | elfsym.st_value = 0; |
c152c796 AM |
9648 | for (i = 1; i < elf_numsections (abfd); i++) |
9649 | { | |
9650 | o = bfd_section_from_elf_index (abfd, i); | |
9651 | if (o != NULL) | |
f0b5bb34 AM |
9652 | { |
9653 | o->target_index = bfd_get_symcount (abfd); | |
9654 | elfsym.st_shndx = i; | |
9655 | if (!info->relocatable) | |
9656 | elfsym.st_value = o->vma; | |
9657 | if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL)) | |
9658 | goto error_return; | |
9659 | } | |
c152c796 AM |
9660 | if (i == SHN_LORESERVE - 1) |
9661 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
9662 | } | |
9663 | } | |
9664 | ||
9665 | /* Allocate some memory to hold information read in from the input | |
9666 | files. */ | |
9667 | if (max_contents_size != 0) | |
9668 | { | |
9669 | finfo.contents = bfd_malloc (max_contents_size); | |
9670 | if (finfo.contents == NULL) | |
9671 | goto error_return; | |
9672 | } | |
9673 | ||
9674 | if (max_external_reloc_size != 0) | |
9675 | { | |
9676 | finfo.external_relocs = bfd_malloc (max_external_reloc_size); | |
9677 | if (finfo.external_relocs == NULL) | |
9678 | goto error_return; | |
9679 | } | |
9680 | ||
9681 | if (max_internal_reloc_count != 0) | |
9682 | { | |
9683 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
9684 | amt *= sizeof (Elf_Internal_Rela); | |
9685 | finfo.internal_relocs = bfd_malloc (amt); | |
9686 | if (finfo.internal_relocs == NULL) | |
9687 | goto error_return; | |
9688 | } | |
9689 | ||
9690 | if (max_sym_count != 0) | |
9691 | { | |
9692 | amt = max_sym_count * bed->s->sizeof_sym; | |
9693 | finfo.external_syms = bfd_malloc (amt); | |
9694 | if (finfo.external_syms == NULL) | |
9695 | goto error_return; | |
9696 | ||
9697 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
9698 | finfo.internal_syms = bfd_malloc (amt); | |
9699 | if (finfo.internal_syms == NULL) | |
9700 | goto error_return; | |
9701 | ||
9702 | amt = max_sym_count * sizeof (long); | |
9703 | finfo.indices = bfd_malloc (amt); | |
9704 | if (finfo.indices == NULL) | |
9705 | goto error_return; | |
9706 | ||
9707 | amt = max_sym_count * sizeof (asection *); | |
9708 | finfo.sections = bfd_malloc (amt); | |
9709 | if (finfo.sections == NULL) | |
9710 | goto error_return; | |
9711 | } | |
9712 | ||
9713 | if (max_sym_shndx_count != 0) | |
9714 | { | |
9715 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
9716 | finfo.locsym_shndx = bfd_malloc (amt); | |
9717 | if (finfo.locsym_shndx == NULL) | |
9718 | goto error_return; | |
9719 | } | |
9720 | ||
9721 | if (elf_hash_table (info)->tls_sec) | |
9722 | { | |
9723 | bfd_vma base, end = 0; | |
9724 | asection *sec; | |
9725 | ||
9726 | for (sec = elf_hash_table (info)->tls_sec; | |
9727 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
9728 | sec = sec->next) | |
9729 | { | |
3a800eb9 | 9730 | bfd_size_type size = sec->size; |
c152c796 | 9731 | |
3a800eb9 AM |
9732 | if (size == 0 |
9733 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 9734 | { |
3a800eb9 AM |
9735 | struct bfd_link_order *o = sec->map_tail.link_order; |
9736 | if (o != NULL) | |
9737 | size = o->offset + o->size; | |
c152c796 AM |
9738 | } |
9739 | end = sec->vma + size; | |
9740 | } | |
9741 | base = elf_hash_table (info)->tls_sec->vma; | |
9742 | end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power); | |
9743 | elf_hash_table (info)->tls_size = end - base; | |
9744 | } | |
9745 | ||
0b52efa6 PB |
9746 | /* Reorder SHF_LINK_ORDER sections. */ |
9747 | for (o = abfd->sections; o != NULL; o = o->next) | |
9748 | { | |
9749 | if (!elf_fixup_link_order (abfd, o)) | |
9750 | return FALSE; | |
9751 | } | |
9752 | ||
c152c796 AM |
9753 | /* Since ELF permits relocations to be against local symbols, we |
9754 | must have the local symbols available when we do the relocations. | |
9755 | Since we would rather only read the local symbols once, and we | |
9756 | would rather not keep them in memory, we handle all the | |
9757 | relocations for a single input file at the same time. | |
9758 | ||
9759 | Unfortunately, there is no way to know the total number of local | |
9760 | symbols until we have seen all of them, and the local symbol | |
9761 | indices precede the global symbol indices. This means that when | |
9762 | we are generating relocatable output, and we see a reloc against | |
9763 | a global symbol, we can not know the symbol index until we have | |
9764 | finished examining all the local symbols to see which ones we are | |
9765 | going to output. To deal with this, we keep the relocations in | |
9766 | memory, and don't output them until the end of the link. This is | |
9767 | an unfortunate waste of memory, but I don't see a good way around | |
9768 | it. Fortunately, it only happens when performing a relocatable | |
9769 | link, which is not the common case. FIXME: If keep_memory is set | |
9770 | we could write the relocs out and then read them again; I don't | |
9771 | know how bad the memory loss will be. */ | |
9772 | ||
9773 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
9774 | sub->output_has_begun = FALSE; | |
9775 | for (o = abfd->sections; o != NULL; o = o->next) | |
9776 | { | |
8423293d | 9777 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
9778 | { |
9779 | if (p->type == bfd_indirect_link_order | |
9780 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
9781 | == bfd_target_elf_flavour) | |
9782 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
9783 | { | |
9784 | if (! sub->output_has_begun) | |
9785 | { | |
9786 | if (! elf_link_input_bfd (&finfo, sub)) | |
9787 | goto error_return; | |
9788 | sub->output_has_begun = TRUE; | |
9789 | } | |
9790 | } | |
9791 | else if (p->type == bfd_section_reloc_link_order | |
9792 | || p->type == bfd_symbol_reloc_link_order) | |
9793 | { | |
9794 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
9795 | goto error_return; | |
9796 | } | |
9797 | else | |
9798 | { | |
9799 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
9800 | goto error_return; | |
9801 | } | |
9802 | } | |
9803 | } | |
9804 | ||
c0f00686 L |
9805 | /* Free symbol buffer if needed. */ |
9806 | if (!info->reduce_memory_overheads) | |
9807 | { | |
9808 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
3fcd97f1 JJ |
9809 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
9810 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
9811 | { |
9812 | free (elf_tdata (sub)->symbuf); | |
9813 | elf_tdata (sub)->symbuf = NULL; | |
9814 | } | |
9815 | } | |
9816 | ||
c152c796 AM |
9817 | /* Output any global symbols that got converted to local in a |
9818 | version script or due to symbol visibility. We do this in a | |
9819 | separate step since ELF requires all local symbols to appear | |
9820 | prior to any global symbols. FIXME: We should only do this if | |
9821 | some global symbols were, in fact, converted to become local. | |
9822 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
9823 | eoinfo.failed = FALSE; | |
9824 | eoinfo.finfo = &finfo; | |
9825 | eoinfo.localsyms = TRUE; | |
9826 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
9827 | &eoinfo); | |
9828 | if (eoinfo.failed) | |
9829 | return FALSE; | |
9830 | ||
4e617b1e PB |
9831 | /* If backend needs to output some local symbols not present in the hash |
9832 | table, do it now. */ | |
9833 | if (bed->elf_backend_output_arch_local_syms) | |
9834 | { | |
9835 | typedef bfd_boolean (*out_sym_func) | |
9836 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
9837 | struct elf_link_hash_entry *); | |
9838 | ||
9839 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
9840 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
9841 | return FALSE; | |
9842 | } | |
9843 | ||
c152c796 AM |
9844 | /* That wrote out all the local symbols. Finish up the symbol table |
9845 | with the global symbols. Even if we want to strip everything we | |
9846 | can, we still need to deal with those global symbols that got | |
9847 | converted to local in a version script. */ | |
9848 | ||
9849 | /* The sh_info field records the index of the first non local symbol. */ | |
9850 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
9851 | ||
9852 | if (dynamic | |
9853 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
9854 | { | |
9855 | Elf_Internal_Sym sym; | |
9856 | bfd_byte *dynsym = finfo.dynsym_sec->contents; | |
9857 | long last_local = 0; | |
9858 | ||
9859 | /* Write out the section symbols for the output sections. */ | |
67687978 | 9860 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
c152c796 AM |
9861 | { |
9862 | asection *s; | |
9863 | ||
9864 | sym.st_size = 0; | |
9865 | sym.st_name = 0; | |
9866 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
9867 | sym.st_other = 0; | |
9868 | ||
9869 | for (s = abfd->sections; s != NULL; s = s->next) | |
9870 | { | |
9871 | int indx; | |
9872 | bfd_byte *dest; | |
9873 | long dynindx; | |
9874 | ||
c152c796 | 9875 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
9876 | if (dynindx <= 0) |
9877 | continue; | |
9878 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
9879 | BFD_ASSERT (indx > 0); |
9880 | sym.st_shndx = indx; | |
c0d5a53d L |
9881 | if (! check_dynsym (abfd, &sym)) |
9882 | return FALSE; | |
c152c796 AM |
9883 | sym.st_value = s->vma; |
9884 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
8c37241b JJ |
9885 | if (last_local < dynindx) |
9886 | last_local = dynindx; | |
c152c796 AM |
9887 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
9888 | } | |
c152c796 AM |
9889 | } |
9890 | ||
9891 | /* Write out the local dynsyms. */ | |
9892 | if (elf_hash_table (info)->dynlocal) | |
9893 | { | |
9894 | struct elf_link_local_dynamic_entry *e; | |
9895 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
9896 | { | |
9897 | asection *s; | |
9898 | bfd_byte *dest; | |
9899 | ||
9900 | sym.st_size = e->isym.st_size; | |
9901 | sym.st_other = e->isym.st_other; | |
9902 | ||
9903 | /* Copy the internal symbol as is. | |
9904 | Note that we saved a word of storage and overwrote | |
9905 | the original st_name with the dynstr_index. */ | |
9906 | sym = e->isym; | |
9907 | ||
9908 | if (e->isym.st_shndx != SHN_UNDEF | |
9909 | && (e->isym.st_shndx < SHN_LORESERVE | |
9910 | || e->isym.st_shndx > SHN_HIRESERVE)) | |
9911 | { | |
9912 | s = bfd_section_from_elf_index (e->input_bfd, | |
9913 | e->isym.st_shndx); | |
9914 | ||
9915 | sym.st_shndx = | |
9916 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
9917 | if (! check_dynsym (abfd, &sym)) |
9918 | return FALSE; | |
c152c796 AM |
9919 | sym.st_value = (s->output_section->vma |
9920 | + s->output_offset | |
9921 | + e->isym.st_value); | |
9922 | } | |
9923 | ||
9924 | if (last_local < e->dynindx) | |
9925 | last_local = e->dynindx; | |
9926 | ||
9927 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
9928 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
9929 | } | |
9930 | } | |
9931 | ||
9932 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = | |
9933 | last_local + 1; | |
9934 | } | |
9935 | ||
9936 | /* We get the global symbols from the hash table. */ | |
9937 | eoinfo.failed = FALSE; | |
9938 | eoinfo.localsyms = FALSE; | |
9939 | eoinfo.finfo = &finfo; | |
9940 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
9941 | &eoinfo); | |
9942 | if (eoinfo.failed) | |
9943 | return FALSE; | |
9944 | ||
9945 | /* If backend needs to output some symbols not present in the hash | |
9946 | table, do it now. */ | |
9947 | if (bed->elf_backend_output_arch_syms) | |
9948 | { | |
9949 | typedef bfd_boolean (*out_sym_func) | |
9950 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
9951 | struct elf_link_hash_entry *); | |
9952 | ||
9953 | if (! ((*bed->elf_backend_output_arch_syms) | |
9954 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
9955 | return FALSE; | |
9956 | } | |
9957 | ||
9958 | /* Flush all symbols to the file. */ | |
9959 | if (! elf_link_flush_output_syms (&finfo, bed)) | |
9960 | return FALSE; | |
9961 | ||
9962 | /* Now we know the size of the symtab section. */ | |
9963 | off += symtab_hdr->sh_size; | |
9964 | ||
9965 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
9966 | if (symtab_shndx_hdr->sh_name != 0) | |
9967 | { | |
9968 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
9969 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
9970 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
9971 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
9972 | symtab_shndx_hdr->sh_size = amt; | |
9973 | ||
9974 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
9975 | off, TRUE); | |
9976 | ||
9977 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
9978 | || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) | |
9979 | return FALSE; | |
9980 | } | |
9981 | ||
9982 | ||
9983 | /* Finish up and write out the symbol string table (.strtab) | |
9984 | section. */ | |
9985 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
9986 | /* sh_name was set in prep_headers. */ | |
9987 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
9988 | symstrtab_hdr->sh_flags = 0; | |
9989 | symstrtab_hdr->sh_addr = 0; | |
9990 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
9991 | symstrtab_hdr->sh_entsize = 0; | |
9992 | symstrtab_hdr->sh_link = 0; | |
9993 | symstrtab_hdr->sh_info = 0; | |
9994 | /* sh_offset is set just below. */ | |
9995 | symstrtab_hdr->sh_addralign = 1; | |
9996 | ||
9997 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); | |
9998 | elf_tdata (abfd)->next_file_pos = off; | |
9999 | ||
10000 | if (bfd_get_symcount (abfd) > 0) | |
10001 | { | |
10002 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
10003 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
10004 | return FALSE; | |
10005 | } | |
10006 | ||
10007 | /* Adjust the relocs to have the correct symbol indices. */ | |
10008 | for (o = abfd->sections; o != NULL; o = o->next) | |
10009 | { | |
10010 | if ((o->flags & SEC_RELOC) == 0) | |
10011 | continue; | |
10012 | ||
10013 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, | |
10014 | elf_section_data (o)->rel_count, | |
10015 | elf_section_data (o)->rel_hashes); | |
10016 | if (elf_section_data (o)->rel_hdr2 != NULL) | |
10017 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, | |
10018 | elf_section_data (o)->rel_count2, | |
10019 | (elf_section_data (o)->rel_hashes | |
10020 | + elf_section_data (o)->rel_count)); | |
10021 | ||
10022 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
10023 | trying to swap the relocs out itself. */ | |
10024 | o->reloc_count = 0; | |
10025 | } | |
10026 | ||
10027 | if (dynamic && info->combreloc && dynobj != NULL) | |
10028 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
10029 | ||
10030 | /* If we are linking against a dynamic object, or generating a | |
10031 | shared library, finish up the dynamic linking information. */ | |
10032 | if (dynamic) | |
10033 | { | |
10034 | bfd_byte *dyncon, *dynconend; | |
10035 | ||
10036 | /* Fix up .dynamic entries. */ | |
10037 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
10038 | BFD_ASSERT (o != NULL); | |
10039 | ||
10040 | dyncon = o->contents; | |
eea6121a | 10041 | dynconend = o->contents + o->size; |
c152c796 AM |
10042 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
10043 | { | |
10044 | Elf_Internal_Dyn dyn; | |
10045 | const char *name; | |
10046 | unsigned int type; | |
10047 | ||
10048 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
10049 | ||
10050 | switch (dyn.d_tag) | |
10051 | { | |
10052 | default: | |
10053 | continue; | |
10054 | case DT_NULL: | |
10055 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
10056 | { | |
10057 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
10058 | { | |
10059 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
10060 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
10061 | default: continue; | |
10062 | } | |
10063 | dyn.d_un.d_val = relativecount; | |
10064 | relativecount = 0; | |
10065 | break; | |
10066 | } | |
10067 | continue; | |
10068 | ||
10069 | case DT_INIT: | |
10070 | name = info->init_function; | |
10071 | goto get_sym; | |
10072 | case DT_FINI: | |
10073 | name = info->fini_function; | |
10074 | get_sym: | |
10075 | { | |
10076 | struct elf_link_hash_entry *h; | |
10077 | ||
10078 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
10079 | FALSE, FALSE, TRUE); | |
10080 | if (h != NULL | |
10081 | && (h->root.type == bfd_link_hash_defined | |
10082 | || h->root.type == bfd_link_hash_defweak)) | |
10083 | { | |
10084 | dyn.d_un.d_val = h->root.u.def.value; | |
10085 | o = h->root.u.def.section; | |
10086 | if (o->output_section != NULL) | |
10087 | dyn.d_un.d_val += (o->output_section->vma | |
10088 | + o->output_offset); | |
10089 | else | |
10090 | { | |
10091 | /* The symbol is imported from another shared | |
10092 | library and does not apply to this one. */ | |
10093 | dyn.d_un.d_val = 0; | |
10094 | } | |
10095 | break; | |
10096 | } | |
10097 | } | |
10098 | continue; | |
10099 | ||
10100 | case DT_PREINIT_ARRAYSZ: | |
10101 | name = ".preinit_array"; | |
10102 | goto get_size; | |
10103 | case DT_INIT_ARRAYSZ: | |
10104 | name = ".init_array"; | |
10105 | goto get_size; | |
10106 | case DT_FINI_ARRAYSZ: | |
10107 | name = ".fini_array"; | |
10108 | get_size: | |
10109 | o = bfd_get_section_by_name (abfd, name); | |
10110 | if (o == NULL) | |
10111 | { | |
10112 | (*_bfd_error_handler) | |
d003868e | 10113 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
10114 | goto error_return; |
10115 | } | |
eea6121a | 10116 | if (o->size == 0) |
c152c796 AM |
10117 | (*_bfd_error_handler) |
10118 | (_("warning: %s section has zero size"), name); | |
eea6121a | 10119 | dyn.d_un.d_val = o->size; |
c152c796 AM |
10120 | break; |
10121 | ||
10122 | case DT_PREINIT_ARRAY: | |
10123 | name = ".preinit_array"; | |
10124 | goto get_vma; | |
10125 | case DT_INIT_ARRAY: | |
10126 | name = ".init_array"; | |
10127 | goto get_vma; | |
10128 | case DT_FINI_ARRAY: | |
10129 | name = ".fini_array"; | |
10130 | goto get_vma; | |
10131 | ||
10132 | case DT_HASH: | |
10133 | name = ".hash"; | |
10134 | goto get_vma; | |
fdc90cb4 JJ |
10135 | case DT_GNU_HASH: |
10136 | name = ".gnu.hash"; | |
10137 | goto get_vma; | |
c152c796 AM |
10138 | case DT_STRTAB: |
10139 | name = ".dynstr"; | |
10140 | goto get_vma; | |
10141 | case DT_SYMTAB: | |
10142 | name = ".dynsym"; | |
10143 | goto get_vma; | |
10144 | case DT_VERDEF: | |
10145 | name = ".gnu.version_d"; | |
10146 | goto get_vma; | |
10147 | case DT_VERNEED: | |
10148 | name = ".gnu.version_r"; | |
10149 | goto get_vma; | |
10150 | case DT_VERSYM: | |
10151 | name = ".gnu.version"; | |
10152 | get_vma: | |
10153 | o = bfd_get_section_by_name (abfd, name); | |
10154 | if (o == NULL) | |
10155 | { | |
10156 | (*_bfd_error_handler) | |
d003868e | 10157 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
10158 | goto error_return; |
10159 | } | |
10160 | dyn.d_un.d_ptr = o->vma; | |
10161 | break; | |
10162 | ||
10163 | case DT_REL: | |
10164 | case DT_RELA: | |
10165 | case DT_RELSZ: | |
10166 | case DT_RELASZ: | |
10167 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
10168 | type = SHT_REL; | |
10169 | else | |
10170 | type = SHT_RELA; | |
10171 | dyn.d_un.d_val = 0; | |
10172 | for (i = 1; i < elf_numsections (abfd); i++) | |
10173 | { | |
10174 | Elf_Internal_Shdr *hdr; | |
10175 | ||
10176 | hdr = elf_elfsections (abfd)[i]; | |
10177 | if (hdr->sh_type == type | |
10178 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
10179 | { | |
10180 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
10181 | dyn.d_un.d_val += hdr->sh_size; | |
10182 | else | |
10183 | { | |
10184 | if (dyn.d_un.d_val == 0 | |
10185 | || hdr->sh_addr < dyn.d_un.d_val) | |
10186 | dyn.d_un.d_val = hdr->sh_addr; | |
10187 | } | |
10188 | } | |
10189 | } | |
10190 | break; | |
10191 | } | |
10192 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
10193 | } | |
10194 | } | |
10195 | ||
10196 | /* If we have created any dynamic sections, then output them. */ | |
10197 | if (dynobj != NULL) | |
10198 | { | |
10199 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
10200 | goto error_return; | |
10201 | ||
943284cc DJ |
10202 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
10203 | if (info->warn_shared_textrel && info->shared) | |
10204 | { | |
10205 | bfd_byte *dyncon, *dynconend; | |
10206 | ||
10207 | /* Fix up .dynamic entries. */ | |
10208 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
10209 | BFD_ASSERT (o != NULL); | |
10210 | ||
10211 | dyncon = o->contents; | |
10212 | dynconend = o->contents + o->size; | |
10213 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
10214 | { | |
10215 | Elf_Internal_Dyn dyn; | |
10216 | ||
10217 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
10218 | ||
10219 | if (dyn.d_tag == DT_TEXTREL) | |
10220 | { | |
9267588c L |
10221 | info->callbacks->einfo |
10222 | (_("%P: warning: creating a DT_TEXTREL in a shared object.\n")); | |
943284cc DJ |
10223 | break; |
10224 | } | |
10225 | } | |
10226 | } | |
10227 | ||
c152c796 AM |
10228 | for (o = dynobj->sections; o != NULL; o = o->next) |
10229 | { | |
10230 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 10231 | || o->size == 0 |
c152c796 AM |
10232 | || o->output_section == bfd_abs_section_ptr) |
10233 | continue; | |
10234 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
10235 | { | |
10236 | /* At this point, we are only interested in sections | |
10237 | created by _bfd_elf_link_create_dynamic_sections. */ | |
10238 | continue; | |
10239 | } | |
3722b82f AM |
10240 | if (elf_hash_table (info)->stab_info.stabstr == o) |
10241 | continue; | |
eea6121a AM |
10242 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
10243 | continue; | |
c152c796 AM |
10244 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
10245 | != SHT_STRTAB) | |
10246 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
10247 | { | |
10248 | if (! bfd_set_section_contents (abfd, o->output_section, | |
10249 | o->contents, | |
10250 | (file_ptr) o->output_offset, | |
eea6121a | 10251 | o->size)) |
c152c796 AM |
10252 | goto error_return; |
10253 | } | |
10254 | else | |
10255 | { | |
10256 | /* The contents of the .dynstr section are actually in a | |
10257 | stringtab. */ | |
10258 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
10259 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
10260 | || ! _bfd_elf_strtab_emit (abfd, | |
10261 | elf_hash_table (info)->dynstr)) | |
10262 | goto error_return; | |
10263 | } | |
10264 | } | |
10265 | } | |
10266 | ||
10267 | if (info->relocatable) | |
10268 | { | |
10269 | bfd_boolean failed = FALSE; | |
10270 | ||
10271 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
10272 | if (failed) | |
10273 | goto error_return; | |
10274 | } | |
10275 | ||
10276 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 10277 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
10278 | { |
10279 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
10280 | goto error_return; | |
10281 | } | |
10282 | ||
10283 | if (info->eh_frame_hdr) | |
10284 | { | |
10285 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) | |
10286 | goto error_return; | |
10287 | } | |
10288 | ||
10289 | if (finfo.symstrtab != NULL) | |
10290 | _bfd_stringtab_free (finfo.symstrtab); | |
10291 | if (finfo.contents != NULL) | |
10292 | free (finfo.contents); | |
10293 | if (finfo.external_relocs != NULL) | |
10294 | free (finfo.external_relocs); | |
10295 | if (finfo.internal_relocs != NULL) | |
10296 | free (finfo.internal_relocs); | |
10297 | if (finfo.external_syms != NULL) | |
10298 | free (finfo.external_syms); | |
10299 | if (finfo.locsym_shndx != NULL) | |
10300 | free (finfo.locsym_shndx); | |
10301 | if (finfo.internal_syms != NULL) | |
10302 | free (finfo.internal_syms); | |
10303 | if (finfo.indices != NULL) | |
10304 | free (finfo.indices); | |
10305 | if (finfo.sections != NULL) | |
10306 | free (finfo.sections); | |
10307 | if (finfo.symbuf != NULL) | |
10308 | free (finfo.symbuf); | |
10309 | if (finfo.symshndxbuf != NULL) | |
10310 | free (finfo.symshndxbuf); | |
10311 | for (o = abfd->sections; o != NULL; o = o->next) | |
10312 | { | |
10313 | if ((o->flags & SEC_RELOC) != 0 | |
10314 | && elf_section_data (o)->rel_hashes != NULL) | |
10315 | free (elf_section_data (o)->rel_hashes); | |
10316 | } | |
10317 | ||
10318 | elf_tdata (abfd)->linker = TRUE; | |
10319 | ||
104d59d1 JM |
10320 | if (attr_section) |
10321 | { | |
10322 | bfd_byte *contents = bfd_malloc (attr_size); | |
10323 | if (contents == NULL) | |
10324 | goto error_return; | |
10325 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); | |
10326 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
10327 | free (contents); | |
10328 | } | |
10329 | ||
c152c796 AM |
10330 | return TRUE; |
10331 | ||
10332 | error_return: | |
10333 | if (finfo.symstrtab != NULL) | |
10334 | _bfd_stringtab_free (finfo.symstrtab); | |
10335 | if (finfo.contents != NULL) | |
10336 | free (finfo.contents); | |
10337 | if (finfo.external_relocs != NULL) | |
10338 | free (finfo.external_relocs); | |
10339 | if (finfo.internal_relocs != NULL) | |
10340 | free (finfo.internal_relocs); | |
10341 | if (finfo.external_syms != NULL) | |
10342 | free (finfo.external_syms); | |
10343 | if (finfo.locsym_shndx != NULL) | |
10344 | free (finfo.locsym_shndx); | |
10345 | if (finfo.internal_syms != NULL) | |
10346 | free (finfo.internal_syms); | |
10347 | if (finfo.indices != NULL) | |
10348 | free (finfo.indices); | |
10349 | if (finfo.sections != NULL) | |
10350 | free (finfo.sections); | |
10351 | if (finfo.symbuf != NULL) | |
10352 | free (finfo.symbuf); | |
10353 | if (finfo.symshndxbuf != NULL) | |
10354 | free (finfo.symshndxbuf); | |
10355 | for (o = abfd->sections; o != NULL; o = o->next) | |
10356 | { | |
10357 | if ((o->flags & SEC_RELOC) != 0 | |
10358 | && elf_section_data (o)->rel_hashes != NULL) | |
10359 | free (elf_section_data (o)->rel_hashes); | |
10360 | } | |
10361 | ||
10362 | return FALSE; | |
10363 | } | |
10364 | \f | |
10365 | /* Garbage collect unused sections. */ | |
10366 | ||
07adf181 AM |
10367 | /* Default gc_mark_hook. */ |
10368 | ||
10369 | asection * | |
10370 | _bfd_elf_gc_mark_hook (asection *sec, | |
10371 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
10372 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
10373 | struct elf_link_hash_entry *h, | |
10374 | Elf_Internal_Sym *sym) | |
10375 | { | |
10376 | if (h != NULL) | |
10377 | { | |
10378 | switch (h->root.type) | |
10379 | { | |
10380 | case bfd_link_hash_defined: | |
10381 | case bfd_link_hash_defweak: | |
10382 | return h->root.u.def.section; | |
10383 | ||
10384 | case bfd_link_hash_common: | |
10385 | return h->root.u.c.p->section; | |
10386 | ||
10387 | default: | |
10388 | break; | |
10389 | } | |
10390 | } | |
10391 | else | |
10392 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
10393 | ||
10394 | return NULL; | |
10395 | } | |
10396 | ||
10397 | /* The mark phase of garbage collection. For a given section, mark | |
10398 | it and any sections in this section's group, and all the sections | |
10399 | which define symbols to which it refers. */ | |
10400 | ||
ccfa59ea AM |
10401 | bfd_boolean |
10402 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
10403 | asection *sec, | |
6a5bb875 | 10404 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
10405 | { |
10406 | bfd_boolean ret; | |
39c2f51b | 10407 | bfd_boolean is_eh; |
c152c796 AM |
10408 | asection *group_sec; |
10409 | ||
10410 | sec->gc_mark = 1; | |
10411 | ||
10412 | /* Mark all the sections in the group. */ | |
10413 | group_sec = elf_section_data (sec)->next_in_group; | |
10414 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 10415 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
10416 | return FALSE; |
10417 | ||
10418 | /* Look through the section relocs. */ | |
10419 | ret = TRUE; | |
39c2f51b | 10420 | is_eh = strcmp (sec->name, ".eh_frame") == 0; |
c152c796 AM |
10421 | if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0) |
10422 | { | |
10423 | Elf_Internal_Rela *relstart, *rel, *relend; | |
10424 | Elf_Internal_Shdr *symtab_hdr; | |
10425 | struct elf_link_hash_entry **sym_hashes; | |
10426 | size_t nlocsyms; | |
10427 | size_t extsymoff; | |
10428 | bfd *input_bfd = sec->owner; | |
10429 | const struct elf_backend_data *bed = get_elf_backend_data (input_bfd); | |
10430 | Elf_Internal_Sym *isym = NULL; | |
10431 | int r_sym_shift; | |
10432 | ||
10433 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
10434 | sym_hashes = elf_sym_hashes (input_bfd); | |
10435 | ||
10436 | /* Read the local symbols. */ | |
10437 | if (elf_bad_symtab (input_bfd)) | |
10438 | { | |
10439 | nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10440 | extsymoff = 0; | |
10441 | } | |
10442 | else | |
10443 | extsymoff = nlocsyms = symtab_hdr->sh_info; | |
10444 | ||
10445 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
10446 | if (isym == NULL && nlocsyms != 0) | |
10447 | { | |
10448 | isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0, | |
10449 | NULL, NULL, NULL); | |
10450 | if (isym == NULL) | |
10451 | return FALSE; | |
10452 | } | |
10453 | ||
10454 | /* Read the relocations. */ | |
10455 | relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL, | |
10456 | info->keep_memory); | |
10457 | if (relstart == NULL) | |
10458 | { | |
10459 | ret = FALSE; | |
10460 | goto out1; | |
10461 | } | |
10462 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
10463 | ||
10464 | if (bed->s->arch_size == 32) | |
10465 | r_sym_shift = 8; | |
10466 | else | |
10467 | r_sym_shift = 32; | |
10468 | ||
10469 | for (rel = relstart; rel < relend; rel++) | |
10470 | { | |
10471 | unsigned long r_symndx; | |
10472 | asection *rsec; | |
10473 | struct elf_link_hash_entry *h; | |
10474 | ||
10475 | r_symndx = rel->r_info >> r_sym_shift; | |
10476 | if (r_symndx == 0) | |
10477 | continue; | |
10478 | ||
10479 | if (r_symndx >= nlocsyms | |
10480 | || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL) | |
10481 | { | |
10482 | h = sym_hashes[r_symndx - extsymoff]; | |
20f0a1ad AM |
10483 | while (h->root.type == bfd_link_hash_indirect |
10484 | || h->root.type == bfd_link_hash_warning) | |
10485 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 AM |
10486 | rsec = (*gc_mark_hook) (sec, info, rel, h, NULL); |
10487 | } | |
10488 | else | |
10489 | { | |
10490 | rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]); | |
10491 | } | |
10492 | ||
10493 | if (rsec && !rsec->gc_mark) | |
10494 | { | |
10495 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
10496 | rsec->gc_mark = 1; | |
39c2f51b AM |
10497 | else if (is_eh) |
10498 | rsec->gc_mark_from_eh = 1; | |
ccfa59ea | 10499 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) |
c152c796 AM |
10500 | { |
10501 | ret = FALSE; | |
10502 | goto out2; | |
10503 | } | |
10504 | } | |
10505 | } | |
10506 | ||
10507 | out2: | |
10508 | if (elf_section_data (sec)->relocs != relstart) | |
10509 | free (relstart); | |
10510 | out1: | |
10511 | if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym) | |
10512 | { | |
10513 | if (! info->keep_memory) | |
10514 | free (isym); | |
10515 | else | |
10516 | symtab_hdr->contents = (unsigned char *) isym; | |
10517 | } | |
10518 | } | |
10519 | ||
10520 | return ret; | |
10521 | } | |
10522 | ||
10523 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ | |
10524 | ||
c17d87de NC |
10525 | struct elf_gc_sweep_symbol_info |
10526 | { | |
ccabcbe5 AM |
10527 | struct bfd_link_info *info; |
10528 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
10529 | bfd_boolean); | |
10530 | }; | |
10531 | ||
c152c796 | 10532 | static bfd_boolean |
ccabcbe5 | 10533 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) |
c152c796 | 10534 | { |
c152c796 AM |
10535 | if (h->root.type == bfd_link_hash_warning) |
10536 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10537 | ||
ccabcbe5 AM |
10538 | if ((h->root.type == bfd_link_hash_defined |
10539 | || h->root.type == bfd_link_hash_defweak) | |
10540 | && !h->root.u.def.section->gc_mark | |
10541 | && !(h->root.u.def.section->owner->flags & DYNAMIC)) | |
10542 | { | |
10543 | struct elf_gc_sweep_symbol_info *inf = data; | |
10544 | (*inf->hide_symbol) (inf->info, h, TRUE); | |
10545 | } | |
c152c796 AM |
10546 | |
10547 | return TRUE; | |
10548 | } | |
10549 | ||
10550 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
10551 | ||
10552 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
10553 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
10554 | ||
10555 | static bfd_boolean | |
ccabcbe5 | 10556 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
10557 | { |
10558 | bfd *sub; | |
ccabcbe5 AM |
10559 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10560 | gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; | |
10561 | unsigned long section_sym_count; | |
10562 | struct elf_gc_sweep_symbol_info sweep_info; | |
c152c796 AM |
10563 | |
10564 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
10565 | { | |
10566 | asection *o; | |
10567 | ||
10568 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
10569 | continue; | |
10570 | ||
10571 | for (o = sub->sections; o != NULL; o = o->next) | |
10572 | { | |
7c2c8505 AM |
10573 | /* Keep debug and special sections. */ |
10574 | if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 | |
dea5f36a | 10575 | || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) |
c152c796 AM |
10576 | o->gc_mark = 1; |
10577 | ||
10578 | if (o->gc_mark) | |
10579 | continue; | |
10580 | ||
10581 | /* Skip sweeping sections already excluded. */ | |
10582 | if (o->flags & SEC_EXCLUDE) | |
10583 | continue; | |
10584 | ||
10585 | /* Since this is early in the link process, it is simple | |
10586 | to remove a section from the output. */ | |
10587 | o->flags |= SEC_EXCLUDE; | |
10588 | ||
c55fe096 | 10589 | if (info->print_gc_sections && o->size != 0) |
c17d87de NC |
10590 | _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name); |
10591 | ||
c152c796 AM |
10592 | /* But we also have to update some of the relocation |
10593 | info we collected before. */ | |
10594 | if (gc_sweep_hook | |
e8aaee2a AM |
10595 | && (o->flags & SEC_RELOC) != 0 |
10596 | && o->reloc_count > 0 | |
10597 | && !bfd_is_abs_section (o->output_section)) | |
c152c796 AM |
10598 | { |
10599 | Elf_Internal_Rela *internal_relocs; | |
10600 | bfd_boolean r; | |
10601 | ||
10602 | internal_relocs | |
10603 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
10604 | info->keep_memory); | |
10605 | if (internal_relocs == NULL) | |
10606 | return FALSE; | |
10607 | ||
10608 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
10609 | ||
10610 | if (elf_section_data (o)->relocs != internal_relocs) | |
10611 | free (internal_relocs); | |
10612 | ||
10613 | if (!r) | |
10614 | return FALSE; | |
10615 | } | |
10616 | } | |
10617 | } | |
10618 | ||
10619 | /* Remove the symbols that were in the swept sections from the dynamic | |
10620 | symbol table. GCFIXME: Anyone know how to get them out of the | |
10621 | static symbol table as well? */ | |
ccabcbe5 AM |
10622 | sweep_info.info = info; |
10623 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
10624 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
10625 | &sweep_info); | |
c152c796 | 10626 | |
ccabcbe5 | 10627 | _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); |
c152c796 AM |
10628 | return TRUE; |
10629 | } | |
10630 | ||
10631 | /* Propagate collected vtable information. This is called through | |
10632 | elf_link_hash_traverse. */ | |
10633 | ||
10634 | static bfd_boolean | |
10635 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
10636 | { | |
10637 | if (h->root.type == bfd_link_hash_warning) | |
10638 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10639 | ||
10640 | /* Those that are not vtables. */ | |
f6e332e6 | 10641 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
10642 | return TRUE; |
10643 | ||
10644 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 10645 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
10646 | return TRUE; |
10647 | ||
10648 | /* If we've already been done, exit. */ | |
f6e332e6 | 10649 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
10650 | return TRUE; |
10651 | ||
10652 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 10653 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 10654 | |
f6e332e6 | 10655 | if (h->vtable->used == NULL) |
c152c796 AM |
10656 | { |
10657 | /* None of this table's entries were referenced. Re-use the | |
10658 | parent's table. */ | |
f6e332e6 AM |
10659 | h->vtable->used = h->vtable->parent->vtable->used; |
10660 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
10661 | } |
10662 | else | |
10663 | { | |
10664 | size_t n; | |
10665 | bfd_boolean *cu, *pu; | |
10666 | ||
10667 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 10668 | cu = h->vtable->used; |
c152c796 | 10669 | cu[-1] = TRUE; |
f6e332e6 | 10670 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
10671 | if (pu != NULL) |
10672 | { | |
10673 | const struct elf_backend_data *bed; | |
10674 | unsigned int log_file_align; | |
10675 | ||
10676 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
10677 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 10678 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
10679 | while (n--) |
10680 | { | |
10681 | if (*pu) | |
10682 | *cu = TRUE; | |
10683 | pu++; | |
10684 | cu++; | |
10685 | } | |
10686 | } | |
10687 | } | |
10688 | ||
10689 | return TRUE; | |
10690 | } | |
10691 | ||
10692 | static bfd_boolean | |
10693 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
10694 | { | |
10695 | asection *sec; | |
10696 | bfd_vma hstart, hend; | |
10697 | Elf_Internal_Rela *relstart, *relend, *rel; | |
10698 | const struct elf_backend_data *bed; | |
10699 | unsigned int log_file_align; | |
10700 | ||
10701 | if (h->root.type == bfd_link_hash_warning) | |
10702 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10703 | ||
10704 | /* Take care of both those symbols that do not describe vtables as | |
10705 | well as those that are not loaded. */ | |
f6e332e6 | 10706 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
10707 | return TRUE; |
10708 | ||
10709 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
10710 | || h->root.type == bfd_link_hash_defweak); | |
10711 | ||
10712 | sec = h->root.u.def.section; | |
10713 | hstart = h->root.u.def.value; | |
10714 | hend = hstart + h->size; | |
10715 | ||
10716 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
10717 | if (!relstart) | |
10718 | return *(bfd_boolean *) okp = FALSE; | |
10719 | bed = get_elf_backend_data (sec->owner); | |
10720 | log_file_align = bed->s->log_file_align; | |
10721 | ||
10722 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
10723 | ||
10724 | for (rel = relstart; rel < relend; ++rel) | |
10725 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
10726 | { | |
10727 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
10728 | if (h->vtable->used |
10729 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
10730 | { |
10731 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 10732 | if (h->vtable->used[entry]) |
c152c796 AM |
10733 | continue; |
10734 | } | |
10735 | /* Otherwise, kill it. */ | |
10736 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
10737 | } | |
10738 | ||
10739 | return TRUE; | |
10740 | } | |
10741 | ||
87538722 AM |
10742 | /* Mark sections containing dynamically referenced symbols. When |
10743 | building shared libraries, we must assume that any visible symbol is | |
10744 | referenced. */ | |
715df9b8 | 10745 | |
64d03ab5 AM |
10746 | bfd_boolean |
10747 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 10748 | { |
87538722 AM |
10749 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
10750 | ||
715df9b8 EB |
10751 | if (h->root.type == bfd_link_hash_warning) |
10752 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10753 | ||
10754 | if ((h->root.type == bfd_link_hash_defined | |
10755 | || h->root.type == bfd_link_hash_defweak) | |
87538722 | 10756 | && (h->ref_dynamic |
5adcfd8b | 10757 | || (!info->executable |
87538722 AM |
10758 | && h->def_regular |
10759 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL | |
10760 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN))) | |
715df9b8 EB |
10761 | h->root.u.def.section->flags |= SEC_KEEP; |
10762 | ||
10763 | return TRUE; | |
10764 | } | |
3b36f7e6 | 10765 | |
c152c796 AM |
10766 | /* Do mark and sweep of unused sections. */ |
10767 | ||
10768 | bfd_boolean | |
10769 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
10770 | { | |
10771 | bfd_boolean ok = TRUE; | |
10772 | bfd *sub; | |
6a5bb875 | 10773 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 10774 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 10775 | |
64d03ab5 | 10776 | if (!bed->can_gc_sections |
c152c796 AM |
10777 | || info->relocatable |
10778 | || info->emitrelocations | |
715df9b8 | 10779 | || !is_elf_hash_table (info->hash)) |
c152c796 AM |
10780 | { |
10781 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
10782 | return TRUE; | |
10783 | } | |
10784 | ||
10785 | /* Apply transitive closure to the vtable entry usage info. */ | |
10786 | elf_link_hash_traverse (elf_hash_table (info), | |
10787 | elf_gc_propagate_vtable_entries_used, | |
10788 | &ok); | |
10789 | if (!ok) | |
10790 | return FALSE; | |
10791 | ||
10792 | /* Kill the vtable relocations that were not used. */ | |
10793 | elf_link_hash_traverse (elf_hash_table (info), | |
10794 | elf_gc_smash_unused_vtentry_relocs, | |
10795 | &ok); | |
10796 | if (!ok) | |
10797 | return FALSE; | |
10798 | ||
715df9b8 EB |
10799 | /* Mark dynamically referenced symbols. */ |
10800 | if (elf_hash_table (info)->dynamic_sections_created) | |
10801 | elf_link_hash_traverse (elf_hash_table (info), | |
64d03ab5 | 10802 | bed->gc_mark_dynamic_ref, |
87538722 | 10803 | info); |
c152c796 | 10804 | |
715df9b8 | 10805 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 10806 | gc_mark_hook = bed->gc_mark_hook; |
c152c796 AM |
10807 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) |
10808 | { | |
10809 | asection *o; | |
10810 | ||
10811 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
10812 | continue; | |
10813 | ||
10814 | for (o = sub->sections; o != NULL; o = o->next) | |
a14a5de3 | 10815 | if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark) |
39c2f51b AM |
10816 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) |
10817 | return FALSE; | |
c152c796 AM |
10818 | } |
10819 | ||
6a5bb875 PB |
10820 | /* Allow the backend to mark additional target specific sections. */ |
10821 | if (bed->gc_mark_extra_sections) | |
10822 | bed->gc_mark_extra_sections(info, gc_mark_hook); | |
10823 | ||
9e8cc8b4 AM |
10824 | /* ... again for sections marked from eh_frame. */ |
10825 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
10826 | { | |
10827 | asection *o; | |
10828 | ||
10829 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
10830 | continue; | |
10831 | ||
a48710b3 EB |
10832 | /* Keep .gcc_except_table.* if the associated .text.* (or the |
10833 | associated .gnu.linkonce.t.* if .text.* doesn't exist) is | |
9e8cc8b4 | 10834 | marked. This isn't very nice, but the proper solution, |
2de92251 | 10835 | splitting .eh_frame up and using comdat doesn't pan out |
9e8cc8b4 AM |
10836 | easily due to needing special relocs to handle the |
10837 | difference of two symbols in separate sections. | |
10838 | Don't keep code sections referenced by .eh_frame. */ | |
ea9986ff | 10839 | #define TEXT_PREFIX ".text." |
a48710b3 | 10840 | #define TEXT_PREFIX2 ".gnu.linkonce.t." |
ea9986ff | 10841 | #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table." |
9e8cc8b4 AM |
10842 | for (o = sub->sections; o != NULL; o = o->next) |
10843 | if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0) | |
10844 | { | |
ea9986ff | 10845 | if (CONST_STRNEQ (o->name, GCC_EXCEPT_TABLE_PREFIX)) |
9e8cc8b4 | 10846 | { |
9e8cc8b4 | 10847 | char *fn_name; |
ea9986ff | 10848 | const char *sec_name; |
9e8cc8b4 | 10849 | asection *fn_text; |
a48710b3 | 10850 | unsigned o_name_prefix_len , fn_name_prefix_len, tmp; |
9e8cc8b4 | 10851 | |
a48710b3 | 10852 | o_name_prefix_len = strlen (GCC_EXCEPT_TABLE_PREFIX); |
ea9986ff | 10853 | sec_name = o->name + o_name_prefix_len; |
a48710b3 EB |
10854 | fn_name_prefix_len = strlen (TEXT_PREFIX); |
10855 | tmp = strlen (TEXT_PREFIX2); | |
10856 | if (tmp > fn_name_prefix_len) | |
10857 | fn_name_prefix_len = tmp; | |
10858 | fn_name | |
10859 | = bfd_malloc (fn_name_prefix_len + strlen (sec_name) + 1); | |
9e8cc8b4 AM |
10860 | if (fn_name == NULL) |
10861 | return FALSE; | |
a48710b3 EB |
10862 | |
10863 | /* Try the first prefix. */ | |
ea9986ff | 10864 | sprintf (fn_name, "%s%s", TEXT_PREFIX, sec_name); |
9e8cc8b4 | 10865 | fn_text = bfd_get_section_by_name (sub, fn_name); |
a48710b3 EB |
10866 | |
10867 | /* Try the second prefix. */ | |
10868 | if (fn_text == NULL) | |
10869 | { | |
10870 | sprintf (fn_name, "%s%s", TEXT_PREFIX2, sec_name); | |
10871 | fn_text = bfd_get_section_by_name (sub, fn_name); | |
10872 | } | |
10873 | ||
9e8cc8b4 AM |
10874 | free (fn_name); |
10875 | if (fn_text == NULL || !fn_text->gc_mark) | |
10876 | continue; | |
10877 | } | |
10878 | ||
10879 | /* If not using specially named exception table section, | |
10880 | then keep whatever we are using. */ | |
10881 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
10882 | return FALSE; | |
10883 | } | |
10884 | } | |
10885 | ||
c152c796 | 10886 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 10887 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
10888 | } |
10889 | \f | |
10890 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
10891 | ||
10892 | bfd_boolean | |
10893 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
10894 | asection *sec, | |
10895 | struct elf_link_hash_entry *h, | |
10896 | bfd_vma offset) | |
10897 | { | |
10898 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
10899 | struct elf_link_hash_entry **search, *child; | |
10900 | bfd_size_type extsymcount; | |
10901 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10902 | ||
10903 | /* The sh_info field of the symtab header tells us where the | |
10904 | external symbols start. We don't care about the local symbols at | |
10905 | this point. */ | |
10906 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
10907 | if (!elf_bad_symtab (abfd)) | |
10908 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
10909 | ||
10910 | sym_hashes = elf_sym_hashes (abfd); | |
10911 | sym_hashes_end = sym_hashes + extsymcount; | |
10912 | ||
10913 | /* Hunt down the child symbol, which is in this section at the same | |
10914 | offset as the relocation. */ | |
10915 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
10916 | { | |
10917 | if ((child = *search) != NULL | |
10918 | && (child->root.type == bfd_link_hash_defined | |
10919 | || child->root.type == bfd_link_hash_defweak) | |
10920 | && child->root.u.def.section == sec | |
10921 | && child->root.u.def.value == offset) | |
10922 | goto win; | |
10923 | } | |
10924 | ||
d003868e AM |
10925 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
10926 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
10927 | bfd_set_error (bfd_error_invalid_operation); |
10928 | return FALSE; | |
10929 | ||
10930 | win: | |
f6e332e6 AM |
10931 | if (!child->vtable) |
10932 | { | |
10933 | child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable)); | |
10934 | if (!child->vtable) | |
10935 | return FALSE; | |
10936 | } | |
c152c796 AM |
10937 | if (!h) |
10938 | { | |
10939 | /* This *should* only be the absolute section. It could potentially | |
10940 | be that someone has defined a non-global vtable though, which | |
10941 | would be bad. It isn't worth paging in the local symbols to be | |
10942 | sure though; that case should simply be handled by the assembler. */ | |
10943 | ||
f6e332e6 | 10944 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
10945 | } |
10946 | else | |
f6e332e6 | 10947 | child->vtable->parent = h; |
c152c796 AM |
10948 | |
10949 | return TRUE; | |
10950 | } | |
10951 | ||
10952 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
10953 | ||
10954 | bfd_boolean | |
10955 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
10956 | asection *sec ATTRIBUTE_UNUSED, | |
10957 | struct elf_link_hash_entry *h, | |
10958 | bfd_vma addend) | |
10959 | { | |
10960 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10961 | unsigned int log_file_align = bed->s->log_file_align; | |
10962 | ||
f6e332e6 AM |
10963 | if (!h->vtable) |
10964 | { | |
10965 | h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable)); | |
10966 | if (!h->vtable) | |
10967 | return FALSE; | |
10968 | } | |
10969 | ||
10970 | if (addend >= h->vtable->size) | |
c152c796 AM |
10971 | { |
10972 | size_t size, bytes, file_align; | |
f6e332e6 | 10973 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
10974 | |
10975 | /* While the symbol is undefined, we have to be prepared to handle | |
10976 | a zero size. */ | |
10977 | file_align = 1 << log_file_align; | |
10978 | if (h->root.type == bfd_link_hash_undefined) | |
10979 | size = addend + file_align; | |
10980 | else | |
10981 | { | |
10982 | size = h->size; | |
10983 | if (addend >= size) | |
10984 | { | |
10985 | /* Oops! We've got a reference past the defined end of | |
10986 | the table. This is probably a bug -- shall we warn? */ | |
10987 | size = addend + file_align; | |
10988 | } | |
10989 | } | |
10990 | size = (size + file_align - 1) & -file_align; | |
10991 | ||
10992 | /* Allocate one extra entry for use as a "done" flag for the | |
10993 | consolidation pass. */ | |
10994 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
10995 | ||
10996 | if (ptr) | |
10997 | { | |
10998 | ptr = bfd_realloc (ptr - 1, bytes); | |
10999 | ||
11000 | if (ptr != NULL) | |
11001 | { | |
11002 | size_t oldbytes; | |
11003 | ||
f6e332e6 | 11004 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
11005 | * sizeof (bfd_boolean)); |
11006 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
11007 | } | |
11008 | } | |
11009 | else | |
11010 | ptr = bfd_zmalloc (bytes); | |
11011 | ||
11012 | if (ptr == NULL) | |
11013 | return FALSE; | |
11014 | ||
11015 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
11016 | h->vtable->used = ptr + 1; |
11017 | h->vtable->size = size; | |
c152c796 AM |
11018 | } |
11019 | ||
f6e332e6 | 11020 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
11021 | |
11022 | return TRUE; | |
11023 | } | |
11024 | ||
11025 | struct alloc_got_off_arg { | |
11026 | bfd_vma gotoff; | |
11027 | unsigned int got_elt_size; | |
11028 | }; | |
11029 | ||
11030 | /* We need a special top-level link routine to convert got reference counts | |
11031 | to real got offsets. */ | |
11032 | ||
11033 | static bfd_boolean | |
11034 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
11035 | { | |
11036 | struct alloc_got_off_arg *gofarg = arg; | |
11037 | ||
11038 | if (h->root.type == bfd_link_hash_warning) | |
11039 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11040 | ||
11041 | if (h->got.refcount > 0) | |
11042 | { | |
11043 | h->got.offset = gofarg->gotoff; | |
11044 | gofarg->gotoff += gofarg->got_elt_size; | |
11045 | } | |
11046 | else | |
11047 | h->got.offset = (bfd_vma) -1; | |
11048 | ||
11049 | return TRUE; | |
11050 | } | |
11051 | ||
11052 | /* And an accompanying bit to work out final got entry offsets once | |
11053 | we're done. Should be called from final_link. */ | |
11054 | ||
11055 | bfd_boolean | |
11056 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
11057 | struct bfd_link_info *info) | |
11058 | { | |
11059 | bfd *i; | |
11060 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11061 | bfd_vma gotoff; | |
11062 | unsigned int got_elt_size = bed->s->arch_size / 8; | |
11063 | struct alloc_got_off_arg gofarg; | |
11064 | ||
11065 | if (! is_elf_hash_table (info->hash)) | |
11066 | return FALSE; | |
11067 | ||
11068 | /* The GOT offset is relative to the .got section, but the GOT header is | |
11069 | put into the .got.plt section, if the backend uses it. */ | |
11070 | if (bed->want_got_plt) | |
11071 | gotoff = 0; | |
11072 | else | |
11073 | gotoff = bed->got_header_size; | |
11074 | ||
11075 | /* Do the local .got entries first. */ | |
11076 | for (i = info->input_bfds; i; i = i->link_next) | |
11077 | { | |
11078 | bfd_signed_vma *local_got; | |
11079 | bfd_size_type j, locsymcount; | |
11080 | Elf_Internal_Shdr *symtab_hdr; | |
11081 | ||
11082 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
11083 | continue; | |
11084 | ||
11085 | local_got = elf_local_got_refcounts (i); | |
11086 | if (!local_got) | |
11087 | continue; | |
11088 | ||
11089 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
11090 | if (elf_bad_symtab (i)) | |
11091 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
11092 | else | |
11093 | locsymcount = symtab_hdr->sh_info; | |
11094 | ||
11095 | for (j = 0; j < locsymcount; ++j) | |
11096 | { | |
11097 | if (local_got[j] > 0) | |
11098 | { | |
11099 | local_got[j] = gotoff; | |
11100 | gotoff += got_elt_size; | |
11101 | } | |
11102 | else | |
11103 | local_got[j] = (bfd_vma) -1; | |
11104 | } | |
11105 | } | |
11106 | ||
11107 | /* Then the global .got entries. .plt refcounts are handled by | |
11108 | adjust_dynamic_symbol */ | |
11109 | gofarg.gotoff = gotoff; | |
11110 | gofarg.got_elt_size = got_elt_size; | |
11111 | elf_link_hash_traverse (elf_hash_table (info), | |
11112 | elf_gc_allocate_got_offsets, | |
11113 | &gofarg); | |
11114 | return TRUE; | |
11115 | } | |
11116 | ||
11117 | /* Many folk need no more in the way of final link than this, once | |
11118 | got entry reference counting is enabled. */ | |
11119 | ||
11120 | bfd_boolean | |
11121 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
11122 | { | |
11123 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
11124 | return FALSE; | |
11125 | ||
11126 | /* Invoke the regular ELF backend linker to do all the work. */ | |
11127 | return bfd_elf_final_link (abfd, info); | |
11128 | } | |
11129 | ||
11130 | bfd_boolean | |
11131 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
11132 | { | |
11133 | struct elf_reloc_cookie *rcookie = cookie; | |
11134 | ||
11135 | if (rcookie->bad_symtab) | |
11136 | rcookie->rel = rcookie->rels; | |
11137 | ||
11138 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
11139 | { | |
11140 | unsigned long r_symndx; | |
11141 | ||
11142 | if (! rcookie->bad_symtab) | |
11143 | if (rcookie->rel->r_offset > offset) | |
11144 | return FALSE; | |
11145 | if (rcookie->rel->r_offset != offset) | |
11146 | continue; | |
11147 | ||
11148 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
11149 | if (r_symndx == SHN_UNDEF) | |
11150 | return TRUE; | |
11151 | ||
11152 | if (r_symndx >= rcookie->locsymcount | |
11153 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
11154 | { | |
11155 | struct elf_link_hash_entry *h; | |
11156 | ||
11157 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
11158 | ||
11159 | while (h->root.type == bfd_link_hash_indirect | |
11160 | || h->root.type == bfd_link_hash_warning) | |
11161 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11162 | ||
11163 | if ((h->root.type == bfd_link_hash_defined | |
11164 | || h->root.type == bfd_link_hash_defweak) | |
11165 | && elf_discarded_section (h->root.u.def.section)) | |
11166 | return TRUE; | |
11167 | else | |
11168 | return FALSE; | |
11169 | } | |
11170 | else | |
11171 | { | |
11172 | /* It's not a relocation against a global symbol, | |
11173 | but it could be a relocation against a local | |
11174 | symbol for a discarded section. */ | |
11175 | asection *isec; | |
11176 | Elf_Internal_Sym *isym; | |
11177 | ||
11178 | /* Need to: get the symbol; get the section. */ | |
11179 | isym = &rcookie->locsyms[r_symndx]; | |
11180 | if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
11181 | { | |
11182 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); | |
11183 | if (isec != NULL && elf_discarded_section (isec)) | |
11184 | return TRUE; | |
11185 | } | |
11186 | } | |
11187 | return FALSE; | |
11188 | } | |
11189 | return FALSE; | |
11190 | } | |
11191 | ||
11192 | /* Discard unneeded references to discarded sections. | |
11193 | Returns TRUE if any section's size was changed. */ | |
11194 | /* This function assumes that the relocations are in sorted order, | |
11195 | which is true for all known assemblers. */ | |
11196 | ||
11197 | bfd_boolean | |
11198 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) | |
11199 | { | |
11200 | struct elf_reloc_cookie cookie; | |
11201 | asection *stab, *eh; | |
11202 | Elf_Internal_Shdr *symtab_hdr; | |
11203 | const struct elf_backend_data *bed; | |
11204 | bfd *abfd; | |
11205 | unsigned int count; | |
11206 | bfd_boolean ret = FALSE; | |
11207 | ||
11208 | if (info->traditional_format | |
11209 | || !is_elf_hash_table (info->hash)) | |
11210 | return FALSE; | |
11211 | ||
11212 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) | |
11213 | { | |
11214 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
11215 | continue; | |
11216 | ||
11217 | bed = get_elf_backend_data (abfd); | |
11218 | ||
11219 | if ((abfd->flags & DYNAMIC) != 0) | |
11220 | continue; | |
11221 | ||
8da3dbc5 AM |
11222 | eh = NULL; |
11223 | if (!info->relocatable) | |
11224 | { | |
11225 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); | |
11226 | if (eh != NULL | |
eea6121a | 11227 | && (eh->size == 0 |
8da3dbc5 AM |
11228 | || bfd_is_abs_section (eh->output_section))) |
11229 | eh = NULL; | |
11230 | } | |
c152c796 AM |
11231 | |
11232 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
11233 | if (stab != NULL | |
eea6121a | 11234 | && (stab->size == 0 |
c152c796 AM |
11235 | || bfd_is_abs_section (stab->output_section) |
11236 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) | |
11237 | stab = NULL; | |
11238 | ||
11239 | if (stab == NULL | |
11240 | && eh == NULL | |
11241 | && bed->elf_backend_discard_info == NULL) | |
11242 | continue; | |
11243 | ||
11244 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11245 | cookie.abfd = abfd; | |
11246 | cookie.sym_hashes = elf_sym_hashes (abfd); | |
11247 | cookie.bad_symtab = elf_bad_symtab (abfd); | |
11248 | if (cookie.bad_symtab) | |
11249 | { | |
11250 | cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
11251 | cookie.extsymoff = 0; | |
11252 | } | |
11253 | else | |
11254 | { | |
11255 | cookie.locsymcount = symtab_hdr->sh_info; | |
11256 | cookie.extsymoff = symtab_hdr->sh_info; | |
11257 | } | |
11258 | ||
11259 | if (bed->s->arch_size == 32) | |
11260 | cookie.r_sym_shift = 8; | |
11261 | else | |
11262 | cookie.r_sym_shift = 32; | |
11263 | ||
11264 | cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
11265 | if (cookie.locsyms == NULL && cookie.locsymcount != 0) | |
11266 | { | |
11267 | cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
11268 | cookie.locsymcount, 0, | |
11269 | NULL, NULL, NULL); | |
11270 | if (cookie.locsyms == NULL) | |
1182cb93 AM |
11271 | { |
11272 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
11273 | return FALSE; | |
11274 | } | |
c152c796 AM |
11275 | } |
11276 | ||
11277 | if (stab != NULL) | |
11278 | { | |
11279 | cookie.rels = NULL; | |
11280 | count = stab->reloc_count; | |
11281 | if (count != 0) | |
11282 | cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL, | |
11283 | info->keep_memory); | |
11284 | if (cookie.rels != NULL) | |
11285 | { | |
11286 | cookie.rel = cookie.rels; | |
11287 | cookie.relend = cookie.rels; | |
11288 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
11289 | if (_bfd_discard_section_stabs (abfd, stab, | |
11290 | elf_section_data (stab)->sec_info, | |
11291 | bfd_elf_reloc_symbol_deleted_p, | |
11292 | &cookie)) | |
11293 | ret = TRUE; | |
11294 | if (elf_section_data (stab)->relocs != cookie.rels) | |
11295 | free (cookie.rels); | |
11296 | } | |
11297 | } | |
11298 | ||
11299 | if (eh != NULL) | |
11300 | { | |
11301 | cookie.rels = NULL; | |
11302 | count = eh->reloc_count; | |
11303 | if (count != 0) | |
11304 | cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL, | |
11305 | info->keep_memory); | |
11306 | cookie.rel = cookie.rels; | |
11307 | cookie.relend = cookie.rels; | |
11308 | if (cookie.rels != NULL) | |
11309 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
11310 | ||
11311 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, | |
11312 | bfd_elf_reloc_symbol_deleted_p, | |
11313 | &cookie)) | |
11314 | ret = TRUE; | |
11315 | ||
11316 | if (cookie.rels != NULL | |
11317 | && elf_section_data (eh)->relocs != cookie.rels) | |
11318 | free (cookie.rels); | |
11319 | } | |
11320 | ||
11321 | if (bed->elf_backend_discard_info != NULL | |
11322 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
11323 | ret = TRUE; | |
11324 | ||
11325 | if (cookie.locsyms != NULL | |
11326 | && symtab_hdr->contents != (unsigned char *) cookie.locsyms) | |
11327 | { | |
11328 | if (! info->keep_memory) | |
11329 | free (cookie.locsyms); | |
11330 | else | |
11331 | symtab_hdr->contents = (unsigned char *) cookie.locsyms; | |
11332 | } | |
11333 | } | |
11334 | ||
11335 | if (info->eh_frame_hdr | |
11336 | && !info->relocatable | |
11337 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
11338 | ret = TRUE; | |
11339 | ||
11340 | return ret; | |
11341 | } | |
082b7297 L |
11342 | |
11343 | void | |
c0f00686 L |
11344 | _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec, |
11345 | struct bfd_link_info *info) | |
082b7297 L |
11346 | { |
11347 | flagword flags; | |
6d2cd210 | 11348 | const char *name, *p; |
082b7297 L |
11349 | struct bfd_section_already_linked *l; |
11350 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
3d7f7666 | 11351 | |
3d7f7666 L |
11352 | if (sec->output_section == bfd_abs_section_ptr) |
11353 | return; | |
082b7297 L |
11354 | |
11355 | flags = sec->flags; | |
3d7f7666 | 11356 | |
c2370991 AM |
11357 | /* Return if it isn't a linkonce section. A comdat group section |
11358 | also has SEC_LINK_ONCE set. */ | |
11359 | if ((flags & SEC_LINK_ONCE) == 0) | |
082b7297 L |
11360 | return; |
11361 | ||
c2370991 AM |
11362 | /* Don't put group member sections on our list of already linked |
11363 | sections. They are handled as a group via their group section. */ | |
11364 | if (elf_sec_group (sec) != NULL) | |
11365 | return; | |
3d7f7666 | 11366 | |
082b7297 L |
11367 | /* FIXME: When doing a relocatable link, we may have trouble |
11368 | copying relocations in other sections that refer to local symbols | |
11369 | in the section being discarded. Those relocations will have to | |
11370 | be converted somehow; as of this writing I'm not sure that any of | |
11371 | the backends handle that correctly. | |
11372 | ||
11373 | It is tempting to instead not discard link once sections when | |
11374 | doing a relocatable link (technically, they should be discarded | |
11375 | whenever we are building constructors). However, that fails, | |
11376 | because the linker winds up combining all the link once sections | |
11377 | into a single large link once section, which defeats the purpose | |
11378 | of having link once sections in the first place. | |
11379 | ||
11380 | Also, not merging link once sections in a relocatable link | |
11381 | causes trouble for MIPS ELF, which relies on link once semantics | |
11382 | to handle the .reginfo section correctly. */ | |
11383 | ||
11384 | name = bfd_get_section_name (abfd, sec); | |
11385 | ||
0112cd26 | 11386 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
6d2cd210 JJ |
11387 | && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
11388 | p++; | |
11389 | else | |
11390 | p = name; | |
11391 | ||
11392 | already_linked_list = bfd_section_already_linked_table_lookup (p); | |
082b7297 L |
11393 | |
11394 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
11395 | { | |
c2370991 AM |
11396 | /* We may have 2 different types of sections on the list: group |
11397 | sections and linkonce sections. Match like sections. */ | |
3d7f7666 | 11398 | if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) |
6d2cd210 | 11399 | && strcmp (name, l->sec->name) == 0 |
082b7297 L |
11400 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL) |
11401 | { | |
11402 | /* The section has already been linked. See if we should | |
6d2cd210 | 11403 | issue a warning. */ |
082b7297 L |
11404 | switch (flags & SEC_LINK_DUPLICATES) |
11405 | { | |
11406 | default: | |
11407 | abort (); | |
11408 | ||
11409 | case SEC_LINK_DUPLICATES_DISCARD: | |
11410 | break; | |
11411 | ||
11412 | case SEC_LINK_DUPLICATES_ONE_ONLY: | |
11413 | (*_bfd_error_handler) | |
c93625e2 | 11414 | (_("%B: ignoring duplicate section `%A'"), |
d003868e | 11415 | abfd, sec); |
082b7297 L |
11416 | break; |
11417 | ||
11418 | case SEC_LINK_DUPLICATES_SAME_SIZE: | |
11419 | if (sec->size != l->sec->size) | |
11420 | (*_bfd_error_handler) | |
c93625e2 | 11421 | (_("%B: duplicate section `%A' has different size"), |
d003868e | 11422 | abfd, sec); |
082b7297 | 11423 | break; |
ea5158d8 DJ |
11424 | |
11425 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: | |
11426 | if (sec->size != l->sec->size) | |
11427 | (*_bfd_error_handler) | |
c93625e2 | 11428 | (_("%B: duplicate section `%A' has different size"), |
ea5158d8 DJ |
11429 | abfd, sec); |
11430 | else if (sec->size != 0) | |
11431 | { | |
11432 | bfd_byte *sec_contents, *l_sec_contents; | |
11433 | ||
11434 | if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents)) | |
11435 | (*_bfd_error_handler) | |
c93625e2 | 11436 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
11437 | abfd, sec); |
11438 | else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec, | |
11439 | &l_sec_contents)) | |
11440 | (*_bfd_error_handler) | |
c93625e2 | 11441 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
11442 | l->sec->owner, l->sec); |
11443 | else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) | |
11444 | (*_bfd_error_handler) | |
c93625e2 | 11445 | (_("%B: warning: duplicate section `%A' has different contents"), |
ea5158d8 DJ |
11446 | abfd, sec); |
11447 | ||
11448 | if (sec_contents) | |
11449 | free (sec_contents); | |
11450 | if (l_sec_contents) | |
11451 | free (l_sec_contents); | |
11452 | } | |
11453 | break; | |
082b7297 L |
11454 | } |
11455 | ||
11456 | /* Set the output_section field so that lang_add_section | |
11457 | does not create a lang_input_section structure for this | |
11458 | section. Since there might be a symbol in the section | |
11459 | being discarded, we must retain a pointer to the section | |
11460 | which we are really going to use. */ | |
11461 | sec->output_section = bfd_abs_section_ptr; | |
11462 | sec->kept_section = l->sec; | |
3b36f7e6 | 11463 | |
082b7297 | 11464 | if (flags & SEC_GROUP) |
3d7f7666 L |
11465 | { |
11466 | asection *first = elf_next_in_group (sec); | |
11467 | asection *s = first; | |
11468 | ||
11469 | while (s != NULL) | |
11470 | { | |
11471 | s->output_section = bfd_abs_section_ptr; | |
11472 | /* Record which group discards it. */ | |
11473 | s->kept_section = l->sec; | |
11474 | s = elf_next_in_group (s); | |
11475 | /* These lists are circular. */ | |
11476 | if (s == first) | |
11477 | break; | |
11478 | } | |
11479 | } | |
082b7297 L |
11480 | |
11481 | return; | |
11482 | } | |
11483 | } | |
11484 | ||
c2370991 AM |
11485 | /* A single member comdat group section may be discarded by a |
11486 | linkonce section and vice versa. */ | |
11487 | ||
11488 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 11489 | { |
c2370991 AM |
11490 | asection *first = elf_next_in_group (sec); |
11491 | ||
11492 | if (first != NULL && elf_next_in_group (first) == first) | |
11493 | /* Check this single member group against linkonce sections. */ | |
11494 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
11495 | if ((l->sec->flags & SEC_GROUP) == 0 | |
11496 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL | |
11497 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
11498 | { | |
11499 | first->output_section = bfd_abs_section_ptr; | |
11500 | first->kept_section = l->sec; | |
11501 | sec->output_section = bfd_abs_section_ptr; | |
11502 | break; | |
11503 | } | |
3d7f7666 L |
11504 | } |
11505 | else | |
c2370991 | 11506 | /* Check this linkonce section against single member groups. */ |
6d2cd210 JJ |
11507 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
11508 | if (l->sec->flags & SEC_GROUP) | |
11509 | { | |
11510 | asection *first = elf_next_in_group (l->sec); | |
11511 | ||
11512 | if (first != NULL | |
11513 | && elf_next_in_group (first) == first | |
c0f00686 | 11514 | && bfd_elf_match_symbols_in_sections (first, sec, info)) |
6d2cd210 JJ |
11515 | { |
11516 | sec->output_section = bfd_abs_section_ptr; | |
c2370991 | 11517 | sec->kept_section = first; |
6d2cd210 JJ |
11518 | break; |
11519 | } | |
11520 | } | |
11521 | ||
082b7297 L |
11522 | /* This is the first section with this name. Record it. */ |
11523 | bfd_section_already_linked_table_insert (already_linked_list, sec); | |
11524 | } | |
81e1b023 | 11525 | |
a4d8e49b L |
11526 | bfd_boolean |
11527 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
11528 | { | |
11529 | return sym->st_shndx == SHN_COMMON; | |
11530 | } | |
11531 | ||
11532 | unsigned int | |
11533 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
11534 | { | |
11535 | return SHN_COMMON; | |
11536 | } | |
11537 | ||
11538 | asection * | |
11539 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
11540 | { | |
11541 | return bfd_com_section_ptr; | |
11542 | } |