]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
64d03ab5 | 2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
74f0fb50 | 3 | 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
252b5132 | 4 | |
8fdd7217 | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
8fdd7217 NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
8fdd7217 | 10 | (at your option) any later version. |
252b5132 | 11 | |
8fdd7217 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
8fdd7217 NC |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
252b5132 | 21 | |
252b5132 | 22 | #include "sysdep.h" |
3db64b00 | 23 | #include "bfd.h" |
252b5132 RH |
24 | #include "bfdlink.h" |
25 | #include "libbfd.h" | |
26 | #define ARCH_SIZE 0 | |
27 | #include "elf-bfd.h" | |
4ad4eba5 | 28 | #include "safe-ctype.h" |
ccf2f652 | 29 | #include "libiberty.h" |
66eb6687 | 30 | #include "objalloc.h" |
252b5132 | 31 | |
d98685ac AM |
32 | /* Define a symbol in a dynamic linkage section. */ |
33 | ||
34 | struct elf_link_hash_entry * | |
35 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
36 | struct bfd_link_info *info, | |
37 | asection *sec, | |
38 | const char *name) | |
39 | { | |
40 | struct elf_link_hash_entry *h; | |
41 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 42 | const struct elf_backend_data *bed; |
d98685ac AM |
43 | |
44 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
45 | if (h != NULL) | |
46 | { | |
47 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
48 | This is a symptom of a larger problem: Absolute symbols | |
49 | defined in shared libraries can't be overridden, because we | |
50 | lose the link to the bfd which is via the symbol section. */ | |
51 | h->root.type = bfd_link_hash_new; | |
52 | } | |
53 | ||
54 | bh = &h->root; | |
55 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, | |
56 | sec, 0, NULL, FALSE, | |
57 | get_elf_backend_data (abfd)->collect, | |
58 | &bh)) | |
59 | return NULL; | |
60 | h = (struct elf_link_hash_entry *) bh; | |
61 | h->def_regular = 1; | |
62 | h->type = STT_OBJECT; | |
63 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
64 | ||
ccabcbe5 AM |
65 | bed = get_elf_backend_data (abfd); |
66 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
d98685ac AM |
67 | return h; |
68 | } | |
69 | ||
b34976b6 | 70 | bfd_boolean |
268b6b39 | 71 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
72 | { |
73 | flagword flags; | |
aad5d350 | 74 | asection *s; |
252b5132 | 75 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 76 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
77 | int ptralign; |
78 | ||
79 | /* This function may be called more than once. */ | |
aad5d350 AM |
80 | s = bfd_get_section_by_name (abfd, ".got"); |
81 | if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0) | |
b34976b6 | 82 | return TRUE; |
252b5132 RH |
83 | |
84 | switch (bed->s->arch_size) | |
85 | { | |
bb0deeff AO |
86 | case 32: |
87 | ptralign = 2; | |
88 | break; | |
89 | ||
90 | case 64: | |
91 | ptralign = 3; | |
92 | break; | |
93 | ||
94 | default: | |
95 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 96 | return FALSE; |
252b5132 RH |
97 | } |
98 | ||
e5a52504 | 99 | flags = bed->dynamic_sec_flags; |
252b5132 | 100 | |
3496cb2a | 101 | s = bfd_make_section_with_flags (abfd, ".got", flags); |
252b5132 | 102 | if (s == NULL |
252b5132 | 103 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 104 | return FALSE; |
252b5132 RH |
105 | |
106 | if (bed->want_got_plt) | |
107 | { | |
3496cb2a | 108 | s = bfd_make_section_with_flags (abfd, ".got.plt", flags); |
252b5132 | 109 | if (s == NULL |
252b5132 | 110 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 111 | return FALSE; |
252b5132 RH |
112 | } |
113 | ||
2517a57f AM |
114 | if (bed->want_got_sym) |
115 | { | |
116 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
117 | (or .got.plt) section. We don't do this in the linker script | |
118 | because we don't want to define the symbol if we are not creating | |
119 | a global offset table. */ | |
d98685ac | 120 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_"); |
2517a57f | 121 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
122 | if (h == NULL) |
123 | return FALSE; | |
2517a57f | 124 | } |
252b5132 RH |
125 | |
126 | /* The first bit of the global offset table is the header. */ | |
3b36f7e6 | 127 | s->size += bed->got_header_size; |
252b5132 | 128 | |
b34976b6 | 129 | return TRUE; |
252b5132 RH |
130 | } |
131 | \f | |
7e9f0867 AM |
132 | /* Create a strtab to hold the dynamic symbol names. */ |
133 | static bfd_boolean | |
134 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
135 | { | |
136 | struct elf_link_hash_table *hash_table; | |
137 | ||
138 | hash_table = elf_hash_table (info); | |
139 | if (hash_table->dynobj == NULL) | |
140 | hash_table->dynobj = abfd; | |
141 | ||
142 | if (hash_table->dynstr == NULL) | |
143 | { | |
144 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
145 | if (hash_table->dynstr == NULL) | |
146 | return FALSE; | |
147 | } | |
148 | return TRUE; | |
149 | } | |
150 | ||
45d6a902 AM |
151 | /* Create some sections which will be filled in with dynamic linking |
152 | information. ABFD is an input file which requires dynamic sections | |
153 | to be created. The dynamic sections take up virtual memory space | |
154 | when the final executable is run, so we need to create them before | |
155 | addresses are assigned to the output sections. We work out the | |
156 | actual contents and size of these sections later. */ | |
252b5132 | 157 | |
b34976b6 | 158 | bfd_boolean |
268b6b39 | 159 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 160 | { |
45d6a902 AM |
161 | flagword flags; |
162 | register asection *s; | |
9c5bfbb7 | 163 | const struct elf_backend_data *bed; |
252b5132 | 164 | |
0eddce27 | 165 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
166 | return FALSE; |
167 | ||
168 | if (elf_hash_table (info)->dynamic_sections_created) | |
169 | return TRUE; | |
170 | ||
7e9f0867 AM |
171 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
172 | return FALSE; | |
45d6a902 | 173 | |
7e9f0867 | 174 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
175 | bed = get_elf_backend_data (abfd); |
176 | ||
177 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
178 | |
179 | /* A dynamically linked executable has a .interp section, but a | |
180 | shared library does not. */ | |
36af4a4e | 181 | if (info->executable) |
252b5132 | 182 | { |
3496cb2a L |
183 | s = bfd_make_section_with_flags (abfd, ".interp", |
184 | flags | SEC_READONLY); | |
185 | if (s == NULL) | |
45d6a902 AM |
186 | return FALSE; |
187 | } | |
bb0deeff | 188 | |
45d6a902 AM |
189 | /* Create sections to hold version informations. These are removed |
190 | if they are not needed. */ | |
3496cb2a L |
191 | s = bfd_make_section_with_flags (abfd, ".gnu.version_d", |
192 | flags | SEC_READONLY); | |
45d6a902 | 193 | if (s == NULL |
45d6a902 AM |
194 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
195 | return FALSE; | |
196 | ||
3496cb2a L |
197 | s = bfd_make_section_with_flags (abfd, ".gnu.version", |
198 | flags | SEC_READONLY); | |
45d6a902 | 199 | if (s == NULL |
45d6a902 AM |
200 | || ! bfd_set_section_alignment (abfd, s, 1)) |
201 | return FALSE; | |
202 | ||
3496cb2a L |
203 | s = bfd_make_section_with_flags (abfd, ".gnu.version_r", |
204 | flags | SEC_READONLY); | |
45d6a902 | 205 | if (s == NULL |
45d6a902 AM |
206 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
207 | return FALSE; | |
208 | ||
3496cb2a L |
209 | s = bfd_make_section_with_flags (abfd, ".dynsym", |
210 | flags | SEC_READONLY); | |
45d6a902 | 211 | if (s == NULL |
45d6a902 AM |
212 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
213 | return FALSE; | |
214 | ||
3496cb2a L |
215 | s = bfd_make_section_with_flags (abfd, ".dynstr", |
216 | flags | SEC_READONLY); | |
217 | if (s == NULL) | |
45d6a902 AM |
218 | return FALSE; |
219 | ||
3496cb2a | 220 | s = bfd_make_section_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 221 | if (s == NULL |
45d6a902 AM |
222 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
223 | return FALSE; | |
224 | ||
225 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
226 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
227 | only want to define it if we are, in fact, creating a .dynamic | |
228 | section. We don't want to define it if there is no .dynamic | |
229 | section, since on some ELF platforms the start up code examines it | |
230 | to decide how to initialize the process. */ | |
d98685ac | 231 | if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC")) |
45d6a902 AM |
232 | return FALSE; |
233 | ||
fdc90cb4 JJ |
234 | if (info->emit_hash) |
235 | { | |
236 | s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY); | |
237 | if (s == NULL | |
238 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
239 | return FALSE; | |
240 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
241 | } | |
242 | ||
243 | if (info->emit_gnu_hash) | |
244 | { | |
245 | s = bfd_make_section_with_flags (abfd, ".gnu.hash", | |
246 | flags | SEC_READONLY); | |
247 | if (s == NULL | |
248 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
249 | return FALSE; | |
250 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: | |
251 | 4 32-bit words followed by variable count of 64-bit words, then | |
252 | variable count of 32-bit words. */ | |
253 | if (bed->s->arch_size == 64) | |
254 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
255 | else | |
256 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
257 | } | |
45d6a902 AM |
258 | |
259 | /* Let the backend create the rest of the sections. This lets the | |
260 | backend set the right flags. The backend will normally create | |
261 | the .got and .plt sections. */ | |
262 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
263 | return FALSE; | |
264 | ||
265 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
266 | ||
267 | return TRUE; | |
268 | } | |
269 | ||
270 | /* Create dynamic sections when linking against a dynamic object. */ | |
271 | ||
272 | bfd_boolean | |
268b6b39 | 273 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
274 | { |
275 | flagword flags, pltflags; | |
7325306f | 276 | struct elf_link_hash_entry *h; |
45d6a902 | 277 | asection *s; |
9c5bfbb7 | 278 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 279 | |
252b5132 RH |
280 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
281 | .rel[a].bss sections. */ | |
e5a52504 | 282 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
283 | |
284 | pltflags = flags; | |
252b5132 | 285 | if (bed->plt_not_loaded) |
6df4d94c MM |
286 | /* We do not clear SEC_ALLOC here because we still want the OS to |
287 | allocate space for the section; it's just that there's nothing | |
288 | to read in from the object file. */ | |
5d1634d7 | 289 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
290 | else |
291 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
292 | if (bed->plt_readonly) |
293 | pltflags |= SEC_READONLY; | |
294 | ||
3496cb2a | 295 | s = bfd_make_section_with_flags (abfd, ".plt", pltflags); |
252b5132 | 296 | if (s == NULL |
252b5132 | 297 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 298 | return FALSE; |
252b5132 | 299 | |
d98685ac AM |
300 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
301 | .plt section. */ | |
7325306f RS |
302 | if (bed->want_plt_sym) |
303 | { | |
304 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
305 | "_PROCEDURE_LINKAGE_TABLE_"); | |
306 | elf_hash_table (info)->hplt = h; | |
307 | if (h == NULL) | |
308 | return FALSE; | |
309 | } | |
252b5132 | 310 | |
3496cb2a | 311 | s = bfd_make_section_with_flags (abfd, |
d35fd659 | 312 | (bed->rela_plts_and_copies_p |
3496cb2a L |
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 | 349 | s = bfd_make_section_with_flags (abfd, |
d35fd659 | 350 | (bed->rela_plts_and_copies_p |
3496cb2a L |
351 | ? ".rela.bss" : ".rel.bss"), |
352 | flags | SEC_READONLY); | |
3018b441 | 353 | if (s == NULL |
45d6a902 | 354 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 355 | return FALSE; |
3018b441 | 356 | } |
252b5132 RH |
357 | } |
358 | ||
b34976b6 | 359 | return TRUE; |
252b5132 RH |
360 | } |
361 | \f | |
252b5132 RH |
362 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
363 | read the input files, since we need to have a list of all of them | |
364 | before we can determine the final sizes of the output sections. | |
365 | Note that we may actually call this function even though we are not | |
366 | going to output any dynamic symbols; in some cases we know that a | |
367 | symbol should be in the dynamic symbol table, but only if there is | |
368 | one. */ | |
369 | ||
b34976b6 | 370 | bfd_boolean |
c152c796 AM |
371 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
372 | struct elf_link_hash_entry *h) | |
252b5132 RH |
373 | { |
374 | if (h->dynindx == -1) | |
375 | { | |
2b0f7ef9 | 376 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 377 | char *p; |
252b5132 | 378 | const char *name; |
252b5132 RH |
379 | bfd_size_type indx; |
380 | ||
7a13edea NC |
381 | /* XXX: The ABI draft says the linker must turn hidden and |
382 | internal symbols into STB_LOCAL symbols when producing the | |
383 | DSO. However, if ld.so honors st_other in the dynamic table, | |
384 | this would not be necessary. */ | |
385 | switch (ELF_ST_VISIBILITY (h->other)) | |
386 | { | |
387 | case STV_INTERNAL: | |
388 | case STV_HIDDEN: | |
9d6eee78 L |
389 | if (h->root.type != bfd_link_hash_undefined |
390 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 391 | { |
f5385ebf | 392 | h->forced_local = 1; |
67687978 PB |
393 | if (!elf_hash_table (info)->is_relocatable_executable) |
394 | return TRUE; | |
7a13edea | 395 | } |
0444bdd4 | 396 | |
7a13edea NC |
397 | default: |
398 | break; | |
399 | } | |
400 | ||
252b5132 RH |
401 | h->dynindx = elf_hash_table (info)->dynsymcount; |
402 | ++elf_hash_table (info)->dynsymcount; | |
403 | ||
404 | dynstr = elf_hash_table (info)->dynstr; | |
405 | if (dynstr == NULL) | |
406 | { | |
407 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 408 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 409 | if (dynstr == NULL) |
b34976b6 | 410 | return FALSE; |
252b5132 RH |
411 | } |
412 | ||
413 | /* We don't put any version information in the dynamic string | |
aad5d350 | 414 | table. */ |
252b5132 RH |
415 | name = h->root.root.string; |
416 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
417 | if (p != NULL) |
418 | /* We know that the p points into writable memory. In fact, | |
419 | there are only a few symbols that have read-only names, being | |
420 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
421 | by the backends. Most symbols will have names pointing into | |
422 | an ELF string table read from a file, or to objalloc memory. */ | |
423 | *p = 0; | |
424 | ||
425 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
426 | ||
427 | if (p != NULL) | |
428 | *p = ELF_VER_CHR; | |
252b5132 RH |
429 | |
430 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 431 | return FALSE; |
252b5132 RH |
432 | h->dynstr_index = indx; |
433 | } | |
434 | ||
b34976b6 | 435 | return TRUE; |
252b5132 | 436 | } |
45d6a902 | 437 | \f |
55255dae L |
438 | /* Mark a symbol dynamic. */ |
439 | ||
440 | void | |
441 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, | |
40b36307 L |
442 | struct elf_link_hash_entry *h, |
443 | Elf_Internal_Sym *sym) | |
55255dae | 444 | { |
40b36307 | 445 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
55255dae | 446 | |
40b36307 L |
447 | /* It may be called more than once on the same H. */ |
448 | if(h->dynamic || info->relocatable) | |
55255dae L |
449 | return; |
450 | ||
40b36307 L |
451 | if ((info->dynamic_data |
452 | && (h->type == STT_OBJECT | |
453 | || (sym != NULL | |
454 | && ELF_ST_TYPE (sym->st_info) == STT_OBJECT))) | |
a0c8462f | 455 | || (d != NULL |
40b36307 L |
456 | && h->root.type == bfd_link_hash_new |
457 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
55255dae L |
458 | h->dynamic = 1; |
459 | } | |
460 | ||
45d6a902 AM |
461 | /* Record an assignment to a symbol made by a linker script. We need |
462 | this in case some dynamic object refers to this symbol. */ | |
463 | ||
464 | bfd_boolean | |
fe21a8fc L |
465 | bfd_elf_record_link_assignment (bfd *output_bfd, |
466 | struct bfd_link_info *info, | |
268b6b39 | 467 | const char *name, |
fe21a8fc L |
468 | bfd_boolean provide, |
469 | bfd_boolean hidden) | |
45d6a902 | 470 | { |
00cbee0a | 471 | struct elf_link_hash_entry *h, *hv; |
4ea42fb7 | 472 | struct elf_link_hash_table *htab; |
00cbee0a | 473 | const struct elf_backend_data *bed; |
45d6a902 | 474 | |
0eddce27 | 475 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
476 | return TRUE; |
477 | ||
4ea42fb7 AM |
478 | htab = elf_hash_table (info); |
479 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 480 | if (h == NULL) |
4ea42fb7 | 481 | return provide; |
45d6a902 | 482 | |
00cbee0a | 483 | switch (h->root.type) |
77cfaee6 | 484 | { |
00cbee0a L |
485 | case bfd_link_hash_defined: |
486 | case bfd_link_hash_defweak: | |
487 | case bfd_link_hash_common: | |
488 | break; | |
489 | case bfd_link_hash_undefweak: | |
490 | case bfd_link_hash_undefined: | |
491 | /* Since we're defining the symbol, don't let it seem to have not | |
492 | been defined. record_dynamic_symbol and size_dynamic_sections | |
493 | may depend on this. */ | |
4ea42fb7 | 494 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
495 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
496 | bfd_link_repair_undef_list (&htab->root); | |
00cbee0a L |
497 | break; |
498 | case bfd_link_hash_new: | |
40b36307 | 499 | bfd_elf_link_mark_dynamic_symbol (info, h, NULL); |
55255dae | 500 | h->non_elf = 0; |
00cbee0a L |
501 | break; |
502 | case bfd_link_hash_indirect: | |
503 | /* We had a versioned symbol in a dynamic library. We make the | |
a0c8462f | 504 | the versioned symbol point to this one. */ |
00cbee0a L |
505 | bed = get_elf_backend_data (output_bfd); |
506 | hv = h; | |
507 | while (hv->root.type == bfd_link_hash_indirect | |
508 | || hv->root.type == bfd_link_hash_warning) | |
509 | hv = (struct elf_link_hash_entry *) hv->root.u.i.link; | |
510 | /* We don't need to update h->root.u since linker will set them | |
511 | later. */ | |
512 | h->root.type = bfd_link_hash_undefined; | |
513 | hv->root.type = bfd_link_hash_indirect; | |
514 | hv->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
515 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); | |
516 | break; | |
517 | case bfd_link_hash_warning: | |
518 | abort (); | |
519 | break; | |
55255dae | 520 | } |
45d6a902 AM |
521 | |
522 | /* If this symbol is being provided by the linker script, and it is | |
523 | currently defined by a dynamic object, but not by a regular | |
524 | object, then mark it as undefined so that the generic linker will | |
525 | force the correct value. */ | |
526 | if (provide | |
f5385ebf AM |
527 | && h->def_dynamic |
528 | && !h->def_regular) | |
45d6a902 AM |
529 | h->root.type = bfd_link_hash_undefined; |
530 | ||
531 | /* If this symbol is not being provided by the linker script, and it is | |
532 | currently defined by a dynamic object, but not by a regular object, | |
533 | then clear out any version information because the symbol will not be | |
534 | associated with the dynamic object any more. */ | |
535 | if (!provide | |
f5385ebf AM |
536 | && h->def_dynamic |
537 | && !h->def_regular) | |
45d6a902 AM |
538 | h->verinfo.verdef = NULL; |
539 | ||
f5385ebf | 540 | h->def_regular = 1; |
45d6a902 | 541 | |
fe21a8fc L |
542 | if (provide && hidden) |
543 | { | |
544 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
545 | ||
546 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
547 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
548 | } | |
549 | ||
6fa3860b PB |
550 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
551 | and executables. */ | |
552 | if (!info->relocatable | |
553 | && h->dynindx != -1 | |
554 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
555 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
556 | h->forced_local = 1; | |
557 | ||
f5385ebf AM |
558 | if ((h->def_dynamic |
559 | || h->ref_dynamic | |
67687978 PB |
560 | || info->shared |
561 | || (info->executable && elf_hash_table (info)->is_relocatable_executable)) | |
45d6a902 AM |
562 | && h->dynindx == -1) |
563 | { | |
c152c796 | 564 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
565 | return FALSE; |
566 | ||
567 | /* If this is a weak defined symbol, and we know a corresponding | |
568 | real symbol from the same dynamic object, make sure the real | |
569 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
570 | if (h->u.weakdef != NULL |
571 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 572 | { |
f6e332e6 | 573 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
574 | return FALSE; |
575 | } | |
576 | } | |
577 | ||
578 | return TRUE; | |
579 | } | |
42751cf3 | 580 | |
8c58d23b AM |
581 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
582 | success, and 2 on a failure caused by attempting to record a symbol | |
583 | in a discarded section, eg. a discarded link-once section symbol. */ | |
584 | ||
585 | int | |
c152c796 AM |
586 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
587 | bfd *input_bfd, | |
588 | long input_indx) | |
8c58d23b AM |
589 | { |
590 | bfd_size_type amt; | |
591 | struct elf_link_local_dynamic_entry *entry; | |
592 | struct elf_link_hash_table *eht; | |
593 | struct elf_strtab_hash *dynstr; | |
594 | unsigned long dynstr_index; | |
595 | char *name; | |
596 | Elf_External_Sym_Shndx eshndx; | |
597 | char esym[sizeof (Elf64_External_Sym)]; | |
598 | ||
0eddce27 | 599 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
600 | return 0; |
601 | ||
602 | /* See if the entry exists already. */ | |
603 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
604 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
605 | return 1; | |
606 | ||
607 | amt = sizeof (*entry); | |
268b6b39 | 608 | entry = bfd_alloc (input_bfd, amt); |
8c58d23b AM |
609 | if (entry == NULL) |
610 | return 0; | |
611 | ||
612 | /* Go find the symbol, so that we can find it's name. */ | |
613 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 614 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
615 | { |
616 | bfd_release (input_bfd, entry); | |
617 | return 0; | |
618 | } | |
619 | ||
620 | if (entry->isym.st_shndx != SHN_UNDEF | |
4fbb74a6 | 621 | && entry->isym.st_shndx < SHN_LORESERVE) |
8c58d23b AM |
622 | { |
623 | asection *s; | |
624 | ||
625 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
626 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
627 | { | |
628 | /* We can still bfd_release here as nothing has done another | |
629 | bfd_alloc. We can't do this later in this function. */ | |
630 | bfd_release (input_bfd, entry); | |
631 | return 2; | |
632 | } | |
633 | } | |
634 | ||
635 | name = (bfd_elf_string_from_elf_section | |
636 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
637 | entry->isym.st_name)); | |
638 | ||
639 | dynstr = elf_hash_table (info)->dynstr; | |
640 | if (dynstr == NULL) | |
641 | { | |
642 | /* Create a strtab to hold the dynamic symbol names. */ | |
643 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
644 | if (dynstr == NULL) | |
645 | return 0; | |
646 | } | |
647 | ||
b34976b6 | 648 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
649 | if (dynstr_index == (unsigned long) -1) |
650 | return 0; | |
651 | entry->isym.st_name = dynstr_index; | |
652 | ||
653 | eht = elf_hash_table (info); | |
654 | ||
655 | entry->next = eht->dynlocal; | |
656 | eht->dynlocal = entry; | |
657 | entry->input_bfd = input_bfd; | |
658 | entry->input_indx = input_indx; | |
659 | eht->dynsymcount++; | |
660 | ||
661 | /* Whatever binding the symbol had before, it's now local. */ | |
662 | entry->isym.st_info | |
663 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
664 | ||
665 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
666 | ||
667 | return 1; | |
668 | } | |
669 | ||
30b30c21 | 670 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 671 | |
30b30c21 | 672 | long |
268b6b39 AM |
673 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
674 | bfd *input_bfd, | |
675 | long input_indx) | |
30b30c21 RH |
676 | { |
677 | struct elf_link_local_dynamic_entry *e; | |
678 | ||
679 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
680 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
681 | return e->dynindx; | |
682 | return -1; | |
683 | } | |
684 | ||
685 | /* This function is used to renumber the dynamic symbols, if some of | |
686 | them are removed because they are marked as local. This is called | |
687 | via elf_link_hash_traverse. */ | |
688 | ||
b34976b6 | 689 | static bfd_boolean |
268b6b39 AM |
690 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
691 | void *data) | |
42751cf3 | 692 | { |
268b6b39 | 693 | size_t *count = data; |
30b30c21 | 694 | |
e92d460e AM |
695 | if (h->root.type == bfd_link_hash_warning) |
696 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
697 | ||
6fa3860b PB |
698 | if (h->forced_local) |
699 | return TRUE; | |
700 | ||
701 | if (h->dynindx != -1) | |
702 | h->dynindx = ++(*count); | |
703 | ||
704 | return TRUE; | |
705 | } | |
706 | ||
707 | ||
708 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
709 | STB_LOCAL binding. */ | |
710 | ||
711 | static bfd_boolean | |
712 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
713 | void *data) | |
714 | { | |
715 | size_t *count = data; | |
716 | ||
717 | if (h->root.type == bfd_link_hash_warning) | |
718 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
719 | ||
720 | if (!h->forced_local) | |
721 | return TRUE; | |
722 | ||
42751cf3 | 723 | if (h->dynindx != -1) |
30b30c21 RH |
724 | h->dynindx = ++(*count); |
725 | ||
b34976b6 | 726 | return TRUE; |
42751cf3 | 727 | } |
30b30c21 | 728 | |
aee6f5b4 AO |
729 | /* Return true if the dynamic symbol for a given section should be |
730 | omitted when creating a shared library. */ | |
731 | bfd_boolean | |
732 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
733 | struct bfd_link_info *info, | |
734 | asection *p) | |
735 | { | |
74541ad4 AM |
736 | struct elf_link_hash_table *htab; |
737 | ||
aee6f5b4 AO |
738 | switch (elf_section_data (p)->this_hdr.sh_type) |
739 | { | |
740 | case SHT_PROGBITS: | |
741 | case SHT_NOBITS: | |
742 | /* If sh_type is yet undecided, assume it could be | |
743 | SHT_PROGBITS/SHT_NOBITS. */ | |
744 | case SHT_NULL: | |
74541ad4 AM |
745 | htab = elf_hash_table (info); |
746 | if (p == htab->tls_sec) | |
747 | return FALSE; | |
748 | ||
749 | if (htab->text_index_section != NULL) | |
750 | return p != htab->text_index_section && p != htab->data_index_section; | |
751 | ||
aee6f5b4 AO |
752 | if (strcmp (p->name, ".got") == 0 |
753 | || strcmp (p->name, ".got.plt") == 0 | |
754 | || strcmp (p->name, ".plt") == 0) | |
755 | { | |
756 | asection *ip; | |
aee6f5b4 | 757 | |
74541ad4 AM |
758 | if (htab->dynobj != NULL |
759 | && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL | |
aee6f5b4 AO |
760 | && (ip->flags & SEC_LINKER_CREATED) |
761 | && ip->output_section == p) | |
762 | return TRUE; | |
763 | } | |
764 | return FALSE; | |
765 | ||
766 | /* There shouldn't be section relative relocations | |
767 | against any other section. */ | |
768 | default: | |
769 | return TRUE; | |
770 | } | |
771 | } | |
772 | ||
062e2358 | 773 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
774 | symbol for each output section, which come first. Next come symbols |
775 | which have been forced to local binding. Then all of the back-end | |
776 | allocated local dynamic syms, followed by the rest of the global | |
777 | symbols. */ | |
30b30c21 | 778 | |
554220db AM |
779 | static unsigned long |
780 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
781 | struct bfd_link_info *info, | |
782 | unsigned long *section_sym_count) | |
30b30c21 RH |
783 | { |
784 | unsigned long dynsymcount = 0; | |
785 | ||
67687978 | 786 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
30b30c21 | 787 | { |
aee6f5b4 | 788 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
789 | asection *p; |
790 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 791 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
792 | && (p->flags & SEC_ALLOC) != 0 |
793 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
794 | elf_section_data (p)->dynindx = ++dynsymcount; | |
74541ad4 AM |
795 | else |
796 | elf_section_data (p)->dynindx = 0; | |
30b30c21 | 797 | } |
554220db | 798 | *section_sym_count = dynsymcount; |
30b30c21 | 799 | |
6fa3860b PB |
800 | elf_link_hash_traverse (elf_hash_table (info), |
801 | elf_link_renumber_local_hash_table_dynsyms, | |
802 | &dynsymcount); | |
803 | ||
30b30c21 RH |
804 | if (elf_hash_table (info)->dynlocal) |
805 | { | |
806 | struct elf_link_local_dynamic_entry *p; | |
807 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
808 | p->dynindx = ++dynsymcount; | |
809 | } | |
810 | ||
811 | elf_link_hash_traverse (elf_hash_table (info), | |
812 | elf_link_renumber_hash_table_dynsyms, | |
813 | &dynsymcount); | |
814 | ||
815 | /* There is an unused NULL entry at the head of the table which | |
816 | we must account for in our count. Unless there weren't any | |
817 | symbols, which means we'll have no table at all. */ | |
818 | if (dynsymcount != 0) | |
819 | ++dynsymcount; | |
820 | ||
ccabcbe5 AM |
821 | elf_hash_table (info)->dynsymcount = dynsymcount; |
822 | return dynsymcount; | |
30b30c21 | 823 | } |
252b5132 | 824 | |
45d6a902 AM |
825 | /* This function is called when we want to define a new symbol. It |
826 | handles the various cases which arise when we find a definition in | |
827 | a dynamic object, or when there is already a definition in a | |
828 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
829 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
830 | OVERRIDE if the old symbol is overriding a new definition. We set | |
831 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
832 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
833 | change, we mean that we shouldn't warn if the type or size does | |
af44c138 L |
834 | change. We set POLD_ALIGNMENT if an old common symbol in a dynamic |
835 | object is overridden by a regular object. */ | |
45d6a902 AM |
836 | |
837 | bfd_boolean | |
268b6b39 AM |
838 | _bfd_elf_merge_symbol (bfd *abfd, |
839 | struct bfd_link_info *info, | |
840 | const char *name, | |
841 | Elf_Internal_Sym *sym, | |
842 | asection **psec, | |
843 | bfd_vma *pvalue, | |
af44c138 | 844 | unsigned int *pold_alignment, |
268b6b39 AM |
845 | struct elf_link_hash_entry **sym_hash, |
846 | bfd_boolean *skip, | |
847 | bfd_boolean *override, | |
848 | bfd_boolean *type_change_ok, | |
0f8a2703 | 849 | bfd_boolean *size_change_ok) |
252b5132 | 850 | { |
7479dfd4 | 851 | asection *sec, *oldsec; |
45d6a902 AM |
852 | struct elf_link_hash_entry *h; |
853 | struct elf_link_hash_entry *flip; | |
854 | int bind; | |
855 | bfd *oldbfd; | |
856 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
0a36a439 | 857 | bfd_boolean newweak, oldweak, newfunc, oldfunc; |
a4d8e49b | 858 | const struct elf_backend_data *bed; |
45d6a902 AM |
859 | |
860 | *skip = FALSE; | |
861 | *override = FALSE; | |
862 | ||
863 | sec = *psec; | |
864 | bind = ELF_ST_BIND (sym->st_info); | |
865 | ||
cd7be95b KH |
866 | /* Silently discard TLS symbols from --just-syms. There's no way to |
867 | combine a static TLS block with a new TLS block for this executable. */ | |
868 | if (ELF_ST_TYPE (sym->st_info) == STT_TLS | |
869 | && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
870 | { | |
871 | *skip = TRUE; | |
872 | return TRUE; | |
873 | } | |
874 | ||
45d6a902 AM |
875 | if (! bfd_is_und_section (sec)) |
876 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
877 | else | |
878 | h = ((struct elf_link_hash_entry *) | |
879 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
880 | if (h == NULL) | |
881 | return FALSE; | |
882 | *sym_hash = h; | |
252b5132 | 883 | |
88ba32a0 L |
884 | bed = get_elf_backend_data (abfd); |
885 | ||
45d6a902 AM |
886 | /* This code is for coping with dynamic objects, and is only useful |
887 | if we are doing an ELF link. */ | |
88ba32a0 | 888 | if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) |
45d6a902 | 889 | return TRUE; |
252b5132 | 890 | |
45d6a902 AM |
891 | /* For merging, we only care about real symbols. */ |
892 | ||
893 | while (h->root.type == bfd_link_hash_indirect | |
894 | || h->root.type == bfd_link_hash_warning) | |
895 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
896 | ||
40b36307 L |
897 | /* We have to check it for every instance since the first few may be |
898 | refereences and not all compilers emit symbol type for undefined | |
899 | symbols. */ | |
900 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
901 | ||
45d6a902 AM |
902 | /* If we just created the symbol, mark it as being an ELF symbol. |
903 | Other than that, there is nothing to do--there is no merge issue | |
904 | with a newly defined symbol--so we just return. */ | |
905 | ||
906 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 907 | { |
f5385ebf | 908 | h->non_elf = 0; |
45d6a902 AM |
909 | return TRUE; |
910 | } | |
252b5132 | 911 | |
7479dfd4 L |
912 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
913 | existing symbol. */ | |
252b5132 | 914 | |
45d6a902 AM |
915 | switch (h->root.type) |
916 | { | |
917 | default: | |
918 | oldbfd = NULL; | |
7479dfd4 | 919 | oldsec = NULL; |
45d6a902 | 920 | break; |
252b5132 | 921 | |
45d6a902 AM |
922 | case bfd_link_hash_undefined: |
923 | case bfd_link_hash_undefweak: | |
924 | oldbfd = h->root.u.undef.abfd; | |
7479dfd4 | 925 | oldsec = NULL; |
45d6a902 AM |
926 | break; |
927 | ||
928 | case bfd_link_hash_defined: | |
929 | case bfd_link_hash_defweak: | |
930 | oldbfd = h->root.u.def.section->owner; | |
7479dfd4 | 931 | oldsec = h->root.u.def.section; |
45d6a902 AM |
932 | break; |
933 | ||
934 | case bfd_link_hash_common: | |
935 | oldbfd = h->root.u.c.p->section->owner; | |
7479dfd4 | 936 | oldsec = h->root.u.c.p->section; |
45d6a902 AM |
937 | break; |
938 | } | |
939 | ||
940 | /* In cases involving weak versioned symbols, we may wind up trying | |
941 | to merge a symbol with itself. Catch that here, to avoid the | |
942 | confusion that results if we try to override a symbol with | |
943 | itself. The additional tests catch cases like | |
944 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
945 | dynamic object, which we do want to handle here. */ | |
946 | if (abfd == oldbfd | |
947 | && ((abfd->flags & DYNAMIC) == 0 | |
f5385ebf | 948 | || !h->def_regular)) |
45d6a902 AM |
949 | return TRUE; |
950 | ||
951 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
952 | respectively, is from a dynamic object. */ | |
953 | ||
707bba77 | 954 | newdyn = (abfd->flags & DYNAMIC) != 0; |
45d6a902 | 955 | |
707bba77 | 956 | olddyn = FALSE; |
45d6a902 AM |
957 | if (oldbfd != NULL) |
958 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 959 | else if (oldsec != NULL) |
45d6a902 | 960 | { |
707bba77 | 961 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 962 | indices used by MIPS ELF. */ |
707bba77 | 963 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 964 | } |
252b5132 | 965 | |
45d6a902 AM |
966 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
967 | respectively, appear to be a definition rather than reference. */ | |
968 | ||
707bba77 | 969 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 970 | |
707bba77 AM |
971 | olddef = (h->root.type != bfd_link_hash_undefined |
972 | && h->root.type != bfd_link_hash_undefweak | |
973 | && h->root.type != bfd_link_hash_common); | |
45d6a902 | 974 | |
0a36a439 L |
975 | /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, |
976 | respectively, appear to be a function. */ | |
977 | ||
978 | newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
979 | && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); | |
980 | ||
981 | oldfunc = (h->type != STT_NOTYPE | |
982 | && bed->is_function_type (h->type)); | |
983 | ||
580a2b6e L |
984 | /* When we try to create a default indirect symbol from the dynamic |
985 | definition with the default version, we skip it if its type and | |
986 | the type of existing regular definition mismatch. We only do it | |
987 | if the existing regular definition won't be dynamic. */ | |
988 | if (pold_alignment == NULL | |
989 | && !info->shared | |
990 | && !info->export_dynamic | |
991 | && !h->ref_dynamic | |
992 | && newdyn | |
993 | && newdef | |
994 | && !olddyn | |
995 | && (olddef || h->root.type == bfd_link_hash_common) | |
996 | && ELF_ST_TYPE (sym->st_info) != h->type | |
997 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
fcb93ecf | 998 | && h->type != STT_NOTYPE |
0a36a439 | 999 | && !(newfunc && oldfunc)) |
580a2b6e L |
1000 | { |
1001 | *skip = TRUE; | |
1002 | return TRUE; | |
1003 | } | |
1004 | ||
68f49ba3 L |
1005 | /* Check TLS symbol. We don't check undefined symbol introduced by |
1006 | "ld -u". */ | |
7479dfd4 | 1007 | if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS) |
68f49ba3 L |
1008 | && ELF_ST_TYPE (sym->st_info) != h->type |
1009 | && oldbfd != NULL) | |
7479dfd4 L |
1010 | { |
1011 | bfd *ntbfd, *tbfd; | |
1012 | bfd_boolean ntdef, tdef; | |
1013 | asection *ntsec, *tsec; | |
1014 | ||
1015 | if (h->type == STT_TLS) | |
1016 | { | |
3b36f7e6 | 1017 | ntbfd = abfd; |
7479dfd4 L |
1018 | ntsec = sec; |
1019 | ntdef = newdef; | |
1020 | tbfd = oldbfd; | |
1021 | tsec = oldsec; | |
1022 | tdef = olddef; | |
1023 | } | |
1024 | else | |
1025 | { | |
1026 | ntbfd = oldbfd; | |
1027 | ntsec = oldsec; | |
1028 | ntdef = olddef; | |
1029 | tbfd = abfd; | |
1030 | tsec = sec; | |
1031 | tdef = newdef; | |
1032 | } | |
1033 | ||
1034 | if (tdef && ntdef) | |
1035 | (*_bfd_error_handler) | |
1036 | (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), | |
1037 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); | |
1038 | else if (!tdef && !ntdef) | |
1039 | (*_bfd_error_handler) | |
1040 | (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), | |
1041 | tbfd, ntbfd, h->root.root.string); | |
1042 | else if (tdef) | |
1043 | (*_bfd_error_handler) | |
1044 | (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), | |
1045 | tbfd, tsec, ntbfd, h->root.root.string); | |
1046 | else | |
1047 | (*_bfd_error_handler) | |
1048 | (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), | |
1049 | tbfd, ntbfd, ntsec, h->root.root.string); | |
1050 | ||
1051 | bfd_set_error (bfd_error_bad_value); | |
1052 | return FALSE; | |
1053 | } | |
1054 | ||
4cc11e76 | 1055 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
1056 | object or is weak in all dynamic objects. Internal and hidden |
1057 | visibility will make it unavailable to dynamic objects. */ | |
f5385ebf | 1058 | if (newdyn && !h->dynamic_def) |
45d6a902 AM |
1059 | { |
1060 | if (!bfd_is_und_section (sec)) | |
f5385ebf | 1061 | h->dynamic_def = 1; |
45d6a902 | 1062 | else |
252b5132 | 1063 | { |
45d6a902 AM |
1064 | /* Check if this symbol is weak in all dynamic objects. If it |
1065 | is the first time we see it in a dynamic object, we mark | |
1066 | if it is weak. Otherwise, we clear it. */ | |
f5385ebf | 1067 | if (!h->ref_dynamic) |
79349b09 | 1068 | { |
45d6a902 | 1069 | if (bind == STB_WEAK) |
f5385ebf | 1070 | h->dynamic_weak = 1; |
252b5132 | 1071 | } |
45d6a902 | 1072 | else if (bind != STB_WEAK) |
f5385ebf | 1073 | h->dynamic_weak = 0; |
252b5132 | 1074 | } |
45d6a902 | 1075 | } |
252b5132 | 1076 | |
45d6a902 AM |
1077 | /* If the old symbol has non-default visibility, we ignore the new |
1078 | definition from a dynamic object. */ | |
1079 | if (newdyn | |
9c7a29a3 | 1080 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1081 | && !bfd_is_und_section (sec)) |
1082 | { | |
1083 | *skip = TRUE; | |
1084 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1085 | h->ref_dynamic = 1; |
45d6a902 AM |
1086 | /* A protected symbol has external availability. Make sure it is |
1087 | recorded as dynamic. | |
1088 | ||
1089 | FIXME: Should we check type and size for protected symbol? */ | |
1090 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1091 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1092 | else |
1093 | return TRUE; | |
1094 | } | |
1095 | else if (!newdyn | |
9c7a29a3 | 1096 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1097 | && h->def_dynamic) |
45d6a902 AM |
1098 | { |
1099 | /* If the new symbol with non-default visibility comes from a | |
1100 | relocatable file and the old definition comes from a dynamic | |
1101 | object, we remove the old definition. */ | |
1102 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
d2dee3b2 L |
1103 | { |
1104 | /* Handle the case where the old dynamic definition is | |
1105 | default versioned. We need to copy the symbol info from | |
1106 | the symbol with default version to the normal one if it | |
1107 | was referenced before. */ | |
1108 | if (h->ref_regular) | |
1109 | { | |
1110 | const struct elf_backend_data *bed | |
1111 | = get_elf_backend_data (abfd); | |
1112 | struct elf_link_hash_entry *vh = *sym_hash; | |
1113 | vh->root.type = h->root.type; | |
1114 | h->root.type = bfd_link_hash_indirect; | |
1115 | (*bed->elf_backend_copy_indirect_symbol) (info, vh, h); | |
1116 | /* Protected symbols will override the dynamic definition | |
1117 | with default version. */ | |
1118 | if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED) | |
1119 | { | |
1120 | h->root.u.i.link = (struct bfd_link_hash_entry *) vh; | |
1121 | vh->dynamic_def = 1; | |
1122 | vh->ref_dynamic = 1; | |
1123 | } | |
1124 | else | |
1125 | { | |
1126 | h->root.type = vh->root.type; | |
1127 | vh->ref_dynamic = 0; | |
1128 | /* We have to hide it here since it was made dynamic | |
1129 | global with extra bits when the symbol info was | |
1130 | copied from the old dynamic definition. */ | |
1131 | (*bed->elf_backend_hide_symbol) (info, vh, TRUE); | |
1132 | } | |
1133 | h = vh; | |
1134 | } | |
1135 | else | |
1136 | h = *sym_hash; | |
1137 | } | |
1de1a317 | 1138 | |
f6e332e6 | 1139 | if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root) |
1de1a317 L |
1140 | && bfd_is_und_section (sec)) |
1141 | { | |
1142 | /* If the new symbol is undefined and the old symbol was | |
1143 | also undefined before, we need to make sure | |
1144 | _bfd_generic_link_add_one_symbol doesn't mess | |
f6e332e6 | 1145 | up the linker hash table undefs list. Since the old |
1de1a317 L |
1146 | definition came from a dynamic object, it is still on the |
1147 | undefs list. */ | |
1148 | h->root.type = bfd_link_hash_undefined; | |
1de1a317 L |
1149 | h->root.u.undef.abfd = abfd; |
1150 | } | |
1151 | else | |
1152 | { | |
1153 | h->root.type = bfd_link_hash_new; | |
1154 | h->root.u.undef.abfd = NULL; | |
1155 | } | |
1156 | ||
f5385ebf | 1157 | if (h->def_dynamic) |
252b5132 | 1158 | { |
f5385ebf AM |
1159 | h->def_dynamic = 0; |
1160 | h->ref_dynamic = 1; | |
1161 | h->dynamic_def = 1; | |
45d6a902 AM |
1162 | } |
1163 | /* FIXME: Should we check type and size for protected symbol? */ | |
1164 | h->size = 0; | |
1165 | h->type = 0; | |
1166 | return TRUE; | |
1167 | } | |
14a793b2 | 1168 | |
79349b09 AM |
1169 | /* Differentiate strong and weak symbols. */ |
1170 | newweak = bind == STB_WEAK; | |
1171 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1172 | || h->root.type == bfd_link_hash_undefweak); | |
14a793b2 | 1173 | |
15b43f48 AM |
1174 | /* If a new weak symbol definition comes from a regular file and the |
1175 | old symbol comes from a dynamic library, we treat the new one as | |
1176 | strong. Similarly, an old weak symbol definition from a regular | |
1177 | file is treated as strong when the new symbol comes from a dynamic | |
1178 | library. Further, an old weak symbol from a dynamic library is | |
1179 | treated as strong if the new symbol is from a dynamic library. | |
1180 | This reflects the way glibc's ld.so works. | |
1181 | ||
1182 | Do this before setting *type_change_ok or *size_change_ok so that | |
1183 | we warn properly when dynamic library symbols are overridden. */ | |
1184 | ||
1185 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1186 | newweak = FALSE; |
15b43f48 | 1187 | if (olddef && newdyn) |
0f8a2703 AM |
1188 | oldweak = FALSE; |
1189 | ||
fcb93ecf | 1190 | /* Allow changes between different types of funciton symbol. */ |
0a36a439 | 1191 | if (newfunc && oldfunc) |
fcb93ecf PB |
1192 | *type_change_ok = TRUE; |
1193 | ||
79349b09 AM |
1194 | /* It's OK to change the type if either the existing symbol or the |
1195 | new symbol is weak. A type change is also OK if the old symbol | |
1196 | is undefined and the new symbol is defined. */ | |
252b5132 | 1197 | |
79349b09 AM |
1198 | if (oldweak |
1199 | || newweak | |
1200 | || (newdef | |
1201 | && h->root.type == bfd_link_hash_undefined)) | |
1202 | *type_change_ok = TRUE; | |
1203 | ||
1204 | /* It's OK to change the size if either the existing symbol or the | |
1205 | new symbol is weak, or if the old symbol is undefined. */ | |
1206 | ||
1207 | if (*type_change_ok | |
1208 | || h->root.type == bfd_link_hash_undefined) | |
1209 | *size_change_ok = TRUE; | |
45d6a902 | 1210 | |
45d6a902 AM |
1211 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1212 | symbol, respectively, appears to be a common symbol in a dynamic | |
1213 | object. If a symbol appears in an uninitialized section, and is | |
1214 | not weak, and is not a function, then it may be a common symbol | |
1215 | which was resolved when the dynamic object was created. We want | |
1216 | to treat such symbols specially, because they raise special | |
1217 | considerations when setting the symbol size: if the symbol | |
1218 | appears as a common symbol in a regular object, and the size in | |
1219 | the regular object is larger, we must make sure that we use the | |
1220 | larger size. This problematic case can always be avoided in C, | |
1221 | but it must be handled correctly when using Fortran shared | |
1222 | libraries. | |
1223 | ||
1224 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1225 | likewise for OLDDYNCOMMON and OLDDEF. | |
1226 | ||
1227 | Note that this test is just a heuristic, and that it is quite | |
1228 | possible to have an uninitialized symbol in a shared object which | |
1229 | is really a definition, rather than a common symbol. This could | |
1230 | lead to some minor confusion when the symbol really is a common | |
1231 | symbol in some regular object. However, I think it will be | |
1232 | harmless. */ | |
1233 | ||
1234 | if (newdyn | |
1235 | && newdef | |
79349b09 | 1236 | && !newweak |
45d6a902 AM |
1237 | && (sec->flags & SEC_ALLOC) != 0 |
1238 | && (sec->flags & SEC_LOAD) == 0 | |
1239 | && sym->st_size > 0 | |
0a36a439 | 1240 | && !newfunc) |
45d6a902 AM |
1241 | newdyncommon = TRUE; |
1242 | else | |
1243 | newdyncommon = FALSE; | |
1244 | ||
1245 | if (olddyn | |
1246 | && olddef | |
1247 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1248 | && h->def_dynamic |
45d6a902 AM |
1249 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1250 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1251 | && h->size > 0 | |
0a36a439 | 1252 | && !oldfunc) |
45d6a902 AM |
1253 | olddyncommon = TRUE; |
1254 | else | |
1255 | olddyncommon = FALSE; | |
1256 | ||
a4d8e49b L |
1257 | /* We now know everything about the old and new symbols. We ask the |
1258 | backend to check if we can merge them. */ | |
a4d8e49b L |
1259 | if (bed->merge_symbol |
1260 | && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue, | |
1261 | pold_alignment, skip, override, | |
1262 | type_change_ok, size_change_ok, | |
1263 | &newdyn, &newdef, &newdyncommon, &newweak, | |
1264 | abfd, &sec, | |
1265 | &olddyn, &olddef, &olddyncommon, &oldweak, | |
1266 | oldbfd, &oldsec)) | |
1267 | return FALSE; | |
1268 | ||
45d6a902 AM |
1269 | /* If both the old and the new symbols look like common symbols in a |
1270 | dynamic object, set the size of the symbol to the larger of the | |
1271 | two. */ | |
1272 | ||
1273 | if (olddyncommon | |
1274 | && newdyncommon | |
1275 | && sym->st_size != h->size) | |
1276 | { | |
1277 | /* Since we think we have two common symbols, issue a multiple | |
1278 | common warning if desired. Note that we only warn if the | |
1279 | size is different. If the size is the same, we simply let | |
1280 | the old symbol override the new one as normally happens with | |
1281 | symbols defined in dynamic objects. */ | |
1282 | ||
1283 | if (! ((*info->callbacks->multiple_common) | |
1284 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1285 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1286 | return FALSE; | |
252b5132 | 1287 | |
45d6a902 AM |
1288 | if (sym->st_size > h->size) |
1289 | h->size = sym->st_size; | |
252b5132 | 1290 | |
45d6a902 | 1291 | *size_change_ok = TRUE; |
252b5132 RH |
1292 | } |
1293 | ||
45d6a902 AM |
1294 | /* If we are looking at a dynamic object, and we have found a |
1295 | definition, we need to see if the symbol was already defined by | |
1296 | some other object. If so, we want to use the existing | |
1297 | definition, and we do not want to report a multiple symbol | |
1298 | definition error; we do this by clobbering *PSEC to be | |
1299 | bfd_und_section_ptr. | |
1300 | ||
1301 | We treat a common symbol as a definition if the symbol in the | |
1302 | shared library is a function, since common symbols always | |
1303 | represent variables; this can cause confusion in principle, but | |
1304 | any such confusion would seem to indicate an erroneous program or | |
1305 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1306 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1307 | |
1308 | if (newdyn | |
1309 | && newdef | |
77cfaee6 | 1310 | && (olddef |
45d6a902 | 1311 | || (h->root.type == bfd_link_hash_common |
0a36a439 | 1312 | && (newweak || newfunc)))) |
45d6a902 AM |
1313 | { |
1314 | *override = TRUE; | |
1315 | newdef = FALSE; | |
1316 | newdyncommon = FALSE; | |
252b5132 | 1317 | |
45d6a902 AM |
1318 | *psec = sec = bfd_und_section_ptr; |
1319 | *size_change_ok = TRUE; | |
252b5132 | 1320 | |
45d6a902 AM |
1321 | /* If we get here when the old symbol is a common symbol, then |
1322 | we are explicitly letting it override a weak symbol or | |
1323 | function in a dynamic object, and we don't want to warn about | |
1324 | a type change. If the old symbol is a defined symbol, a type | |
1325 | change warning may still be appropriate. */ | |
252b5132 | 1326 | |
45d6a902 AM |
1327 | if (h->root.type == bfd_link_hash_common) |
1328 | *type_change_ok = TRUE; | |
1329 | } | |
1330 | ||
1331 | /* Handle the special case of an old common symbol merging with a | |
1332 | new symbol which looks like a common symbol in a shared object. | |
1333 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1334 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1335 | right thing. */ | |
45d6a902 AM |
1336 | |
1337 | if (newdyncommon | |
1338 | && h->root.type == bfd_link_hash_common) | |
1339 | { | |
1340 | *override = TRUE; | |
1341 | newdef = FALSE; | |
1342 | newdyncommon = FALSE; | |
1343 | *pvalue = sym->st_size; | |
a4d8e49b | 1344 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1345 | *size_change_ok = TRUE; |
1346 | } | |
1347 | ||
c5e2cead | 1348 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1349 | if (newdef && olddef && newweak) |
c5e2cead L |
1350 | *skip = TRUE; |
1351 | ||
45d6a902 AM |
1352 | /* If the old symbol is from a dynamic object, and the new symbol is |
1353 | a definition which is not from a dynamic object, then the new | |
1354 | symbol overrides the old symbol. Symbols from regular files | |
1355 | always take precedence over symbols from dynamic objects, even if | |
1356 | they are defined after the dynamic object in the link. | |
1357 | ||
1358 | As above, we again permit a common symbol in a regular object to | |
1359 | override a definition in a shared object if the shared object | |
0f8a2703 | 1360 | symbol is a function or is weak. */ |
45d6a902 AM |
1361 | |
1362 | flip = NULL; | |
77cfaee6 | 1363 | if (!newdyn |
45d6a902 AM |
1364 | && (newdef |
1365 | || (bfd_is_com_section (sec) | |
0a36a439 | 1366 | && (oldweak || oldfunc))) |
45d6a902 AM |
1367 | && olddyn |
1368 | && olddef | |
f5385ebf | 1369 | && h->def_dynamic) |
45d6a902 AM |
1370 | { |
1371 | /* Change the hash table entry to undefined, and let | |
1372 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1373 | new definition. */ | |
1374 | ||
1375 | h->root.type = bfd_link_hash_undefined; | |
1376 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1377 | *size_change_ok = TRUE; | |
1378 | ||
1379 | olddef = FALSE; | |
1380 | olddyncommon = FALSE; | |
1381 | ||
1382 | /* We again permit a type change when a common symbol may be | |
1383 | overriding a function. */ | |
1384 | ||
1385 | if (bfd_is_com_section (sec)) | |
0a36a439 L |
1386 | { |
1387 | if (oldfunc) | |
1388 | { | |
1389 | /* If a common symbol overrides a function, make sure | |
1390 | that it isn't defined dynamically nor has type | |
1391 | function. */ | |
1392 | h->def_dynamic = 0; | |
1393 | h->type = STT_NOTYPE; | |
1394 | } | |
1395 | *type_change_ok = TRUE; | |
1396 | } | |
45d6a902 AM |
1397 | |
1398 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1399 | flip = *sym_hash; | |
1400 | else | |
1401 | /* This union may have been set to be non-NULL when this symbol | |
1402 | was seen in a dynamic object. We must force the union to be | |
1403 | NULL, so that it is correct for a regular symbol. */ | |
1404 | h->verinfo.vertree = NULL; | |
1405 | } | |
1406 | ||
1407 | /* Handle the special case of a new common symbol merging with an | |
1408 | old symbol that looks like it might be a common symbol defined in | |
1409 | a shared object. Note that we have already handled the case in | |
1410 | which a new common symbol should simply override the definition | |
1411 | in the shared library. */ | |
1412 | ||
1413 | if (! newdyn | |
1414 | && bfd_is_com_section (sec) | |
1415 | && olddyncommon) | |
1416 | { | |
1417 | /* It would be best if we could set the hash table entry to a | |
1418 | common symbol, but we don't know what to use for the section | |
1419 | or the alignment. */ | |
1420 | if (! ((*info->callbacks->multiple_common) | |
1421 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1422 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1423 | return FALSE; | |
1424 | ||
4cc11e76 | 1425 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1426 | larger, pretend that the new symbol has its size. */ |
1427 | ||
1428 | if (h->size > *pvalue) | |
1429 | *pvalue = h->size; | |
1430 | ||
af44c138 L |
1431 | /* We need to remember the alignment required by the symbol |
1432 | in the dynamic object. */ | |
1433 | BFD_ASSERT (pold_alignment); | |
1434 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1435 | |
1436 | olddef = FALSE; | |
1437 | olddyncommon = FALSE; | |
1438 | ||
1439 | h->root.type = bfd_link_hash_undefined; | |
1440 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1441 | ||
1442 | *size_change_ok = TRUE; | |
1443 | *type_change_ok = TRUE; | |
1444 | ||
1445 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1446 | flip = *sym_hash; | |
1447 | else | |
1448 | h->verinfo.vertree = NULL; | |
1449 | } | |
1450 | ||
1451 | if (flip != NULL) | |
1452 | { | |
1453 | /* Handle the case where we had a versioned symbol in a dynamic | |
1454 | library and now find a definition in a normal object. In this | |
1455 | case, we make the versioned symbol point to the normal one. */ | |
9c5bfbb7 | 1456 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 1457 | flip->root.type = h->root.type; |
00cbee0a | 1458 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1459 | h->root.type = bfd_link_hash_indirect; |
1460 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1461 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1462 | if (h->def_dynamic) |
45d6a902 | 1463 | { |
f5385ebf AM |
1464 | h->def_dynamic = 0; |
1465 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1466 | } |
1467 | } | |
1468 | ||
45d6a902 AM |
1469 | return TRUE; |
1470 | } | |
1471 | ||
1472 | /* This function is called to create an indirect symbol from the | |
1473 | default for the symbol with the default version if needed. The | |
1474 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1475 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 AM |
1476 | |
1477 | bfd_boolean | |
268b6b39 AM |
1478 | _bfd_elf_add_default_symbol (bfd *abfd, |
1479 | struct bfd_link_info *info, | |
1480 | struct elf_link_hash_entry *h, | |
1481 | const char *name, | |
1482 | Elf_Internal_Sym *sym, | |
1483 | asection **psec, | |
1484 | bfd_vma *value, | |
1485 | bfd_boolean *dynsym, | |
0f8a2703 | 1486 | bfd_boolean override) |
45d6a902 AM |
1487 | { |
1488 | bfd_boolean type_change_ok; | |
1489 | bfd_boolean size_change_ok; | |
1490 | bfd_boolean skip; | |
1491 | char *shortname; | |
1492 | struct elf_link_hash_entry *hi; | |
1493 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1494 | const struct elf_backend_data *bed; |
45d6a902 AM |
1495 | bfd_boolean collect; |
1496 | bfd_boolean dynamic; | |
1497 | char *p; | |
1498 | size_t len, shortlen; | |
1499 | asection *sec; | |
1500 | ||
1501 | /* If this symbol has a version, and it is the default version, we | |
1502 | create an indirect symbol from the default name to the fully | |
1503 | decorated name. This will cause external references which do not | |
1504 | specify a version to be bound to this version of the symbol. */ | |
1505 | p = strchr (name, ELF_VER_CHR); | |
1506 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1507 | return TRUE; | |
1508 | ||
1509 | if (override) | |
1510 | { | |
4cc11e76 | 1511 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1512 | need to create the indirect symbol from the default name. */ |
1513 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1514 | FALSE, FALSE); | |
1515 | BFD_ASSERT (hi != NULL); | |
1516 | if (hi == h) | |
1517 | return TRUE; | |
1518 | while (hi->root.type == bfd_link_hash_indirect | |
1519 | || hi->root.type == bfd_link_hash_warning) | |
1520 | { | |
1521 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1522 | if (hi == h) | |
1523 | return TRUE; | |
1524 | } | |
1525 | } | |
1526 | ||
1527 | bed = get_elf_backend_data (abfd); | |
1528 | collect = bed->collect; | |
1529 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1530 | ||
1531 | shortlen = p - name; | |
1532 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1533 | if (shortname == NULL) | |
1534 | return FALSE; | |
1535 | memcpy (shortname, name, shortlen); | |
1536 | shortname[shortlen] = '\0'; | |
1537 | ||
1538 | /* We are going to create a new symbol. Merge it with any existing | |
1539 | symbol with this name. For the purposes of the merge, act as | |
1540 | though we were defining the symbol we just defined, although we | |
1541 | actually going to define an indirect symbol. */ | |
1542 | type_change_ok = FALSE; | |
1543 | size_change_ok = FALSE; | |
1544 | sec = *psec; | |
1545 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1546 | NULL, &hi, &skip, &override, |
1547 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1548 | return FALSE; |
1549 | ||
1550 | if (skip) | |
1551 | goto nondefault; | |
1552 | ||
1553 | if (! override) | |
1554 | { | |
1555 | bh = &hi->root; | |
1556 | if (! (_bfd_generic_link_add_one_symbol | |
1557 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1558 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1559 | return FALSE; |
1560 | hi = (struct elf_link_hash_entry *) bh; | |
1561 | } | |
1562 | else | |
1563 | { | |
1564 | /* In this case the symbol named SHORTNAME is overriding the | |
1565 | indirect symbol we want to add. We were planning on making | |
1566 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1567 | is the name without a version. NAME is the fully versioned | |
1568 | name, and it is the default version. | |
1569 | ||
1570 | Overriding means that we already saw a definition for the | |
1571 | symbol SHORTNAME in a regular object, and it is overriding | |
1572 | the symbol defined in the dynamic object. | |
1573 | ||
1574 | When this happens, we actually want to change NAME, the | |
1575 | symbol we just added, to refer to SHORTNAME. This will cause | |
1576 | references to NAME in the shared object to become references | |
1577 | to SHORTNAME in the regular object. This is what we expect | |
1578 | when we override a function in a shared object: that the | |
1579 | references in the shared object will be mapped to the | |
1580 | definition in the regular object. */ | |
1581 | ||
1582 | while (hi->root.type == bfd_link_hash_indirect | |
1583 | || hi->root.type == bfd_link_hash_warning) | |
1584 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1585 | ||
1586 | h->root.type = bfd_link_hash_indirect; | |
1587 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1588 | if (h->def_dynamic) |
45d6a902 | 1589 | { |
f5385ebf AM |
1590 | h->def_dynamic = 0; |
1591 | hi->ref_dynamic = 1; | |
1592 | if (hi->ref_regular | |
1593 | || hi->def_regular) | |
45d6a902 | 1594 | { |
c152c796 | 1595 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1596 | return FALSE; |
1597 | } | |
1598 | } | |
1599 | ||
1600 | /* Now set HI to H, so that the following code will set the | |
1601 | other fields correctly. */ | |
1602 | hi = h; | |
1603 | } | |
1604 | ||
fab4a87f L |
1605 | /* Check if HI is a warning symbol. */ |
1606 | if (hi->root.type == bfd_link_hash_warning) | |
1607 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1608 | ||
45d6a902 AM |
1609 | /* If there is a duplicate definition somewhere, then HI may not |
1610 | point to an indirect symbol. We will have reported an error to | |
1611 | the user in that case. */ | |
1612 | ||
1613 | if (hi->root.type == bfd_link_hash_indirect) | |
1614 | { | |
1615 | struct elf_link_hash_entry *ht; | |
1616 | ||
45d6a902 | 1617 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 1618 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 AM |
1619 | |
1620 | /* See if the new flags lead us to realize that the symbol must | |
1621 | be dynamic. */ | |
1622 | if (! *dynsym) | |
1623 | { | |
1624 | if (! dynamic) | |
1625 | { | |
1626 | if (info->shared | |
f5385ebf | 1627 | || hi->ref_dynamic) |
45d6a902 AM |
1628 | *dynsym = TRUE; |
1629 | } | |
1630 | else | |
1631 | { | |
f5385ebf | 1632 | if (hi->ref_regular) |
45d6a902 AM |
1633 | *dynsym = TRUE; |
1634 | } | |
1635 | } | |
1636 | } | |
1637 | ||
1638 | /* We also need to define an indirection from the nondefault version | |
1639 | of the symbol. */ | |
1640 | ||
1641 | nondefault: | |
1642 | len = strlen (name); | |
1643 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1644 | if (shortname == NULL) | |
1645 | return FALSE; | |
1646 | memcpy (shortname, name, shortlen); | |
1647 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1648 | ||
1649 | /* Once again, merge with any existing symbol. */ | |
1650 | type_change_ok = FALSE; | |
1651 | size_change_ok = FALSE; | |
1652 | sec = *psec; | |
1653 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1654 | NULL, &hi, &skip, &override, |
1655 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1656 | return FALSE; |
1657 | ||
1658 | if (skip) | |
1659 | return TRUE; | |
1660 | ||
1661 | if (override) | |
1662 | { | |
1663 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1664 | the type of override we do in the case above unless it is | |
4cc11e76 | 1665 | overridden by a versioned definition. */ |
45d6a902 AM |
1666 | if (hi->root.type != bfd_link_hash_defined |
1667 | && hi->root.type != bfd_link_hash_defweak) | |
1668 | (*_bfd_error_handler) | |
d003868e AM |
1669 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1670 | abfd, shortname); | |
45d6a902 AM |
1671 | } |
1672 | else | |
1673 | { | |
1674 | bh = &hi->root; | |
1675 | if (! (_bfd_generic_link_add_one_symbol | |
1676 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1677 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1678 | return FALSE; |
1679 | hi = (struct elf_link_hash_entry *) bh; | |
1680 | ||
1681 | /* If there is a duplicate definition somewhere, then HI may not | |
1682 | point to an indirect symbol. We will have reported an error | |
1683 | to the user in that case. */ | |
1684 | ||
1685 | if (hi->root.type == bfd_link_hash_indirect) | |
1686 | { | |
fcfa13d2 | 1687 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
45d6a902 AM |
1688 | |
1689 | /* See if the new flags lead us to realize that the symbol | |
1690 | must be dynamic. */ | |
1691 | if (! *dynsym) | |
1692 | { | |
1693 | if (! dynamic) | |
1694 | { | |
1695 | if (info->shared | |
f5385ebf | 1696 | || hi->ref_dynamic) |
45d6a902 AM |
1697 | *dynsym = TRUE; |
1698 | } | |
1699 | else | |
1700 | { | |
f5385ebf | 1701 | if (hi->ref_regular) |
45d6a902 AM |
1702 | *dynsym = TRUE; |
1703 | } | |
1704 | } | |
1705 | } | |
1706 | } | |
1707 | ||
1708 | return TRUE; | |
1709 | } | |
1710 | \f | |
1711 | /* This routine is used to export all defined symbols into the dynamic | |
1712 | symbol table. It is called via elf_link_hash_traverse. */ | |
1713 | ||
1714 | bfd_boolean | |
268b6b39 | 1715 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1716 | { |
268b6b39 | 1717 | struct elf_info_failed *eif = data; |
45d6a902 | 1718 | |
55255dae L |
1719 | /* Ignore this if we won't export it. */ |
1720 | if (!eif->info->export_dynamic && !h->dynamic) | |
1721 | return TRUE; | |
1722 | ||
45d6a902 AM |
1723 | /* Ignore indirect symbols. These are added by the versioning code. */ |
1724 | if (h->root.type == bfd_link_hash_indirect) | |
1725 | return TRUE; | |
1726 | ||
1727 | if (h->root.type == bfd_link_hash_warning) | |
1728 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1729 | ||
1730 | if (h->dynindx == -1 | |
f5385ebf AM |
1731 | && (h->def_regular |
1732 | || h->ref_regular)) | |
45d6a902 AM |
1733 | { |
1734 | struct bfd_elf_version_tree *t; | |
1735 | struct bfd_elf_version_expr *d; | |
1736 | ||
1737 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1738 | { | |
108ba305 | 1739 | if (t->globals.list != NULL) |
45d6a902 | 1740 | { |
108ba305 JJ |
1741 | d = (*t->match) (&t->globals, NULL, h->root.root.string); |
1742 | if (d != NULL) | |
1743 | goto doit; | |
45d6a902 AM |
1744 | } |
1745 | ||
108ba305 | 1746 | if (t->locals.list != NULL) |
45d6a902 | 1747 | { |
108ba305 JJ |
1748 | d = (*t->match) (&t->locals, NULL, h->root.root.string); |
1749 | if (d != NULL) | |
1750 | return TRUE; | |
45d6a902 AM |
1751 | } |
1752 | } | |
1753 | ||
1754 | if (!eif->verdefs) | |
1755 | { | |
1756 | doit: | |
c152c796 | 1757 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
1758 | { |
1759 | eif->failed = TRUE; | |
1760 | return FALSE; | |
1761 | } | |
1762 | } | |
1763 | } | |
1764 | ||
1765 | return TRUE; | |
1766 | } | |
1767 | \f | |
1768 | /* Look through the symbols which are defined in other shared | |
1769 | libraries and referenced here. Update the list of version | |
1770 | dependencies. This will be put into the .gnu.version_r section. | |
1771 | This function is called via elf_link_hash_traverse. */ | |
1772 | ||
1773 | bfd_boolean | |
268b6b39 AM |
1774 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1775 | void *data) | |
45d6a902 | 1776 | { |
268b6b39 | 1777 | struct elf_find_verdep_info *rinfo = data; |
45d6a902 AM |
1778 | Elf_Internal_Verneed *t; |
1779 | Elf_Internal_Vernaux *a; | |
1780 | bfd_size_type amt; | |
1781 | ||
1782 | if (h->root.type == bfd_link_hash_warning) | |
1783 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1784 | ||
1785 | /* We only care about symbols defined in shared objects with version | |
1786 | information. */ | |
f5385ebf AM |
1787 | if (!h->def_dynamic |
1788 | || h->def_regular | |
45d6a902 AM |
1789 | || h->dynindx == -1 |
1790 | || h->verinfo.verdef == NULL) | |
1791 | return TRUE; | |
1792 | ||
1793 | /* See if we already know about this version. */ | |
1794 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1795 | { | |
1796 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1797 | continue; | |
1798 | ||
1799 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1800 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1801 | return TRUE; | |
1802 | ||
1803 | break; | |
1804 | } | |
1805 | ||
1806 | /* This is a new version. Add it to tree we are building. */ | |
1807 | ||
1808 | if (t == NULL) | |
1809 | { | |
1810 | amt = sizeof *t; | |
268b6b39 | 1811 | t = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1812 | if (t == NULL) |
1813 | { | |
1814 | rinfo->failed = TRUE; | |
1815 | return FALSE; | |
1816 | } | |
1817 | ||
1818 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1819 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1820 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1821 | } | |
1822 | ||
1823 | amt = sizeof *a; | |
268b6b39 | 1824 | a = bfd_zalloc (rinfo->output_bfd, amt); |
14b1c01e AM |
1825 | if (a == NULL) |
1826 | { | |
1827 | rinfo->failed = TRUE; | |
1828 | return FALSE; | |
1829 | } | |
45d6a902 AM |
1830 | |
1831 | /* Note that we are copying a string pointer here, and testing it | |
1832 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1833 | discard the string data when low in memory, this will have to be | |
1834 | fixed. */ | |
1835 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1836 | ||
1837 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1838 | a->vna_nextptr = t->vn_auxptr; | |
1839 | ||
1840 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1841 | ++rinfo->vers; | |
1842 | ||
1843 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1844 | ||
1845 | t->vn_auxptr = a; | |
1846 | ||
1847 | return TRUE; | |
1848 | } | |
1849 | ||
1850 | /* Figure out appropriate versions for all the symbols. We may not | |
1851 | have the version number script until we have read all of the input | |
1852 | files, so until that point we don't know which symbols should be | |
1853 | local. This function is called via elf_link_hash_traverse. */ | |
1854 | ||
1855 | bfd_boolean | |
268b6b39 | 1856 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
1857 | { |
1858 | struct elf_assign_sym_version_info *sinfo; | |
1859 | struct bfd_link_info *info; | |
9c5bfbb7 | 1860 | const struct elf_backend_data *bed; |
45d6a902 AM |
1861 | struct elf_info_failed eif; |
1862 | char *p; | |
1863 | bfd_size_type amt; | |
1864 | ||
268b6b39 | 1865 | sinfo = data; |
45d6a902 AM |
1866 | info = sinfo->info; |
1867 | ||
1868 | if (h->root.type == bfd_link_hash_warning) | |
1869 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1870 | ||
1871 | /* Fix the symbol flags. */ | |
1872 | eif.failed = FALSE; | |
1873 | eif.info = info; | |
1874 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1875 | { | |
1876 | if (eif.failed) | |
1877 | sinfo->failed = TRUE; | |
1878 | return FALSE; | |
1879 | } | |
1880 | ||
1881 | /* We only need version numbers for symbols defined in regular | |
1882 | objects. */ | |
f5385ebf | 1883 | if (!h->def_regular) |
45d6a902 AM |
1884 | return TRUE; |
1885 | ||
1886 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1887 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1888 | if (p != NULL && h->verinfo.vertree == NULL) | |
1889 | { | |
1890 | struct bfd_elf_version_tree *t; | |
1891 | bfd_boolean hidden; | |
1892 | ||
1893 | hidden = TRUE; | |
1894 | ||
1895 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1896 | not a hidden symbol. */ | |
1897 | ++p; | |
1898 | if (*p == ELF_VER_CHR) | |
1899 | { | |
1900 | hidden = FALSE; | |
1901 | ++p; | |
1902 | } | |
1903 | ||
1904 | /* If there is no version string, we can just return out. */ | |
1905 | if (*p == '\0') | |
1906 | { | |
1907 | if (hidden) | |
f5385ebf | 1908 | h->hidden = 1; |
45d6a902 AM |
1909 | return TRUE; |
1910 | } | |
1911 | ||
1912 | /* Look for the version. If we find it, it is no longer weak. */ | |
1913 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1914 | { | |
1915 | if (strcmp (t->name, p) == 0) | |
1916 | { | |
1917 | size_t len; | |
1918 | char *alc; | |
1919 | struct bfd_elf_version_expr *d; | |
1920 | ||
1921 | len = p - h->root.root.string; | |
268b6b39 | 1922 | alc = bfd_malloc (len); |
45d6a902 | 1923 | if (alc == NULL) |
14b1c01e AM |
1924 | { |
1925 | sinfo->failed = TRUE; | |
1926 | return FALSE; | |
1927 | } | |
45d6a902 AM |
1928 | memcpy (alc, h->root.root.string, len - 1); |
1929 | alc[len - 1] = '\0'; | |
1930 | if (alc[len - 2] == ELF_VER_CHR) | |
1931 | alc[len - 2] = '\0'; | |
1932 | ||
1933 | h->verinfo.vertree = t; | |
1934 | t->used = TRUE; | |
1935 | d = NULL; | |
1936 | ||
108ba305 JJ |
1937 | if (t->globals.list != NULL) |
1938 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
1939 | |
1940 | /* See if there is anything to force this symbol to | |
1941 | local scope. */ | |
108ba305 | 1942 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 1943 | { |
108ba305 JJ |
1944 | d = (*t->match) (&t->locals, NULL, alc); |
1945 | if (d != NULL | |
1946 | && h->dynindx != -1 | |
108ba305 JJ |
1947 | && ! info->export_dynamic) |
1948 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
1949 | } |
1950 | ||
1951 | free (alc); | |
1952 | break; | |
1953 | } | |
1954 | } | |
1955 | ||
1956 | /* If we are building an application, we need to create a | |
1957 | version node for this version. */ | |
36af4a4e | 1958 | if (t == NULL && info->executable) |
45d6a902 AM |
1959 | { |
1960 | struct bfd_elf_version_tree **pp; | |
1961 | int version_index; | |
1962 | ||
1963 | /* If we aren't going to export this symbol, we don't need | |
1964 | to worry about it. */ | |
1965 | if (h->dynindx == -1) | |
1966 | return TRUE; | |
1967 | ||
1968 | amt = sizeof *t; | |
108ba305 | 1969 | t = bfd_zalloc (sinfo->output_bfd, amt); |
45d6a902 AM |
1970 | if (t == NULL) |
1971 | { | |
1972 | sinfo->failed = TRUE; | |
1973 | return FALSE; | |
1974 | } | |
1975 | ||
45d6a902 | 1976 | t->name = p; |
45d6a902 AM |
1977 | t->name_indx = (unsigned int) -1; |
1978 | t->used = TRUE; | |
1979 | ||
1980 | version_index = 1; | |
1981 | /* Don't count anonymous version tag. */ | |
1982 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1983 | version_index = 0; | |
1984 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1985 | ++version_index; | |
1986 | t->vernum = version_index; | |
1987 | ||
1988 | *pp = t; | |
1989 | ||
1990 | h->verinfo.vertree = t; | |
1991 | } | |
1992 | else if (t == NULL) | |
1993 | { | |
1994 | /* We could not find the version for a symbol when | |
1995 | generating a shared archive. Return an error. */ | |
1996 | (*_bfd_error_handler) | |
c55fe096 | 1997 | (_("%B: version node not found for symbol %s"), |
d003868e | 1998 | sinfo->output_bfd, h->root.root.string); |
45d6a902 AM |
1999 | bfd_set_error (bfd_error_bad_value); |
2000 | sinfo->failed = TRUE; | |
2001 | return FALSE; | |
2002 | } | |
2003 | ||
2004 | if (hidden) | |
f5385ebf | 2005 | h->hidden = 1; |
45d6a902 AM |
2006 | } |
2007 | ||
2008 | /* If we don't have a version for this symbol, see if we can find | |
2009 | something. */ | |
2010 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
2011 | { | |
2012 | struct bfd_elf_version_tree *t; | |
ae5a3597 | 2013 | struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver; |
45d6a902 AM |
2014 | struct bfd_elf_version_expr *d; |
2015 | ||
2016 | /* See if can find what version this symbol is in. If the | |
2017 | symbol is supposed to be local, then don't actually register | |
2018 | it. */ | |
2019 | local_ver = NULL; | |
ae5a3597 AM |
2020 | global_ver = NULL; |
2021 | exist_ver = NULL; | |
45d6a902 AM |
2022 | for (t = sinfo->verdefs; t != NULL; t = t->next) |
2023 | { | |
108ba305 | 2024 | if (t->globals.list != NULL) |
45d6a902 | 2025 | { |
108ba305 JJ |
2026 | d = NULL; |
2027 | while ((d = (*t->match) (&t->globals, d, | |
2028 | h->root.root.string)) != NULL) | |
ae5a3597 AM |
2029 | { |
2030 | global_ver = t; | |
2031 | local_ver = NULL; | |
2032 | if (d->symver) | |
2033 | exist_ver = t; | |
2034 | d->script = 1; | |
2035 | /* If the match is a wildcard pattern, keep looking for | |
2036 | a more explicit, perhaps even local, match. */ | |
2037 | if (d->literal) | |
108ba305 | 2038 | break; |
ae5a3597 AM |
2039 | } |
2040 | ||
45d6a902 AM |
2041 | if (d != NULL) |
2042 | break; | |
45d6a902 AM |
2043 | } |
2044 | ||
108ba305 | 2045 | if (t->locals.list != NULL) |
45d6a902 | 2046 | { |
108ba305 JJ |
2047 | d = NULL; |
2048 | while ((d = (*t->match) (&t->locals, d, | |
2049 | h->root.root.string)) != NULL) | |
45d6a902 | 2050 | { |
108ba305 | 2051 | local_ver = t; |
ae5a3597 AM |
2052 | /* If the match is a wildcard pattern, keep looking for |
2053 | a more explicit, perhaps even global, match. */ | |
2054 | if (d->literal) | |
2055 | { | |
2056 | /* An exact match overrides a global wildcard. */ | |
2057 | global_ver = NULL; | |
2058 | break; | |
2059 | } | |
45d6a902 AM |
2060 | } |
2061 | ||
2062 | if (d != NULL) | |
2063 | break; | |
2064 | } | |
2065 | } | |
2066 | ||
ae5a3597 AM |
2067 | if (global_ver != NULL) |
2068 | { | |
2069 | h->verinfo.vertree = global_ver; | |
2070 | /* If we already have a versioned symbol that matches the | |
2071 | node for this symbol, then we don't want to create a | |
2072 | duplicate from the unversioned symbol. Instead hide the | |
2073 | unversioned symbol. */ | |
2074 | if (exist_ver == global_ver) | |
2075 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2076 | } | |
2077 | else if (local_ver != NULL) | |
45d6a902 AM |
2078 | { |
2079 | h->verinfo.vertree = local_ver; | |
ae5a3597 AM |
2080 | if (!info->export_dynamic |
2081 | || exist_ver == local_ver) | |
2082 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2083 | } |
2084 | } | |
2085 | ||
2086 | return TRUE; | |
2087 | } | |
2088 | \f | |
45d6a902 AM |
2089 | /* Read and swap the relocs from the section indicated by SHDR. This |
2090 | may be either a REL or a RELA section. The relocations are | |
2091 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2092 | which should have already been allocated to contain enough space. | |
2093 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2094 | relocations should be stored. | |
2095 | ||
2096 | Returns FALSE if something goes wrong. */ | |
2097 | ||
2098 | static bfd_boolean | |
268b6b39 | 2099 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2100 | asection *sec, |
268b6b39 AM |
2101 | Elf_Internal_Shdr *shdr, |
2102 | void *external_relocs, | |
2103 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2104 | { |
9c5bfbb7 | 2105 | const struct elf_backend_data *bed; |
268b6b39 | 2106 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2107 | const bfd_byte *erela; |
2108 | const bfd_byte *erelaend; | |
2109 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2110 | Elf_Internal_Shdr *symtab_hdr; |
2111 | size_t nsyms; | |
45d6a902 | 2112 | |
45d6a902 AM |
2113 | /* Position ourselves at the start of the section. */ |
2114 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2115 | return FALSE; | |
2116 | ||
2117 | /* Read the relocations. */ | |
2118 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2119 | return FALSE; | |
2120 | ||
243ef1e0 L |
2121 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2122 | nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
2123 | ||
45d6a902 AM |
2124 | bed = get_elf_backend_data (abfd); |
2125 | ||
2126 | /* Convert the external relocations to the internal format. */ | |
2127 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2128 | swap_in = bed->s->swap_reloc_in; | |
2129 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2130 | swap_in = bed->s->swap_reloca_in; | |
2131 | else | |
2132 | { | |
2133 | bfd_set_error (bfd_error_wrong_format); | |
2134 | return FALSE; | |
2135 | } | |
2136 | ||
2137 | erela = external_relocs; | |
51992aec | 2138 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2139 | irela = internal_relocs; |
2140 | while (erela < erelaend) | |
2141 | { | |
243ef1e0 L |
2142 | bfd_vma r_symndx; |
2143 | ||
45d6a902 | 2144 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2145 | r_symndx = ELF32_R_SYM (irela->r_info); |
2146 | if (bed->s->arch_size == 64) | |
2147 | r_symndx >>= 24; | |
2148 | if ((size_t) r_symndx >= nsyms) | |
2149 | { | |
2150 | (*_bfd_error_handler) | |
d003868e AM |
2151 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" |
2152 | " for offset 0x%lx in section `%A'"), | |
2153 | abfd, sec, | |
2154 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
2155 | bfd_set_error (bfd_error_bad_value); |
2156 | return FALSE; | |
2157 | } | |
45d6a902 AM |
2158 | irela += bed->s->int_rels_per_ext_rel; |
2159 | erela += shdr->sh_entsize; | |
2160 | } | |
2161 | ||
2162 | return TRUE; | |
2163 | } | |
2164 | ||
2165 | /* Read and swap the relocs for a section O. They may have been | |
2166 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2167 | not NULL, they are used as buffers to read into. They are known to | |
2168 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2169 | the return value is allocated using either malloc or bfd_alloc, | |
2170 | according to the KEEP_MEMORY argument. If O has two relocation | |
2171 | sections (both REL and RELA relocations), then the REL_HDR | |
2172 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
2173 | REL_HDR2 relocations. */ | |
2174 | ||
2175 | Elf_Internal_Rela * | |
268b6b39 AM |
2176 | _bfd_elf_link_read_relocs (bfd *abfd, |
2177 | asection *o, | |
2178 | void *external_relocs, | |
2179 | Elf_Internal_Rela *internal_relocs, | |
2180 | bfd_boolean keep_memory) | |
45d6a902 AM |
2181 | { |
2182 | Elf_Internal_Shdr *rel_hdr; | |
268b6b39 | 2183 | void *alloc1 = NULL; |
45d6a902 | 2184 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2185 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
2186 | |
2187 | if (elf_section_data (o)->relocs != NULL) | |
2188 | return elf_section_data (o)->relocs; | |
2189 | ||
2190 | if (o->reloc_count == 0) | |
2191 | return NULL; | |
2192 | ||
2193 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
2194 | ||
2195 | if (internal_relocs == NULL) | |
2196 | { | |
2197 | bfd_size_type size; | |
2198 | ||
2199 | size = o->reloc_count; | |
2200 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
2201 | if (keep_memory) | |
4dd07732 | 2202 | internal_relocs = alloc2 = bfd_alloc (abfd, size); |
45d6a902 | 2203 | else |
268b6b39 | 2204 | internal_relocs = alloc2 = bfd_malloc (size); |
45d6a902 AM |
2205 | if (internal_relocs == NULL) |
2206 | goto error_return; | |
2207 | } | |
2208 | ||
2209 | if (external_relocs == NULL) | |
2210 | { | |
2211 | bfd_size_type size = rel_hdr->sh_size; | |
2212 | ||
2213 | if (elf_section_data (o)->rel_hdr2) | |
2214 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
268b6b39 | 2215 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2216 | if (alloc1 == NULL) |
2217 | goto error_return; | |
2218 | external_relocs = alloc1; | |
2219 | } | |
2220 | ||
243ef1e0 | 2221 | if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, |
45d6a902 AM |
2222 | external_relocs, |
2223 | internal_relocs)) | |
2224 | goto error_return; | |
51992aec AM |
2225 | if (elf_section_data (o)->rel_hdr2 |
2226 | && (!elf_link_read_relocs_from_section | |
2227 | (abfd, o, | |
2228 | elf_section_data (o)->rel_hdr2, | |
2229 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
2230 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
2231 | * bed->s->int_rels_per_ext_rel)))) | |
45d6a902 AM |
2232 | goto error_return; |
2233 | ||
2234 | /* Cache the results for next time, if we can. */ | |
2235 | if (keep_memory) | |
2236 | elf_section_data (o)->relocs = internal_relocs; | |
2237 | ||
2238 | if (alloc1 != NULL) | |
2239 | free (alloc1); | |
2240 | ||
2241 | /* Don't free alloc2, since if it was allocated we are passing it | |
2242 | back (under the name of internal_relocs). */ | |
2243 | ||
2244 | return internal_relocs; | |
2245 | ||
2246 | error_return: | |
2247 | if (alloc1 != NULL) | |
2248 | free (alloc1); | |
2249 | if (alloc2 != NULL) | |
4dd07732 AM |
2250 | { |
2251 | if (keep_memory) | |
2252 | bfd_release (abfd, alloc2); | |
2253 | else | |
2254 | free (alloc2); | |
2255 | } | |
45d6a902 AM |
2256 | return NULL; |
2257 | } | |
2258 | ||
2259 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2260 | section header for a section containing relocations for O. */ | |
2261 | ||
2262 | bfd_boolean | |
268b6b39 AM |
2263 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2264 | Elf_Internal_Shdr *rel_hdr, | |
2265 | asection *o) | |
45d6a902 AM |
2266 | { |
2267 | bfd_size_type reloc_count; | |
2268 | bfd_size_type num_rel_hashes; | |
2269 | ||
2270 | /* Figure out how many relocations there will be. */ | |
2271 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
2272 | reloc_count = elf_section_data (o)->rel_count; | |
2273 | else | |
2274 | reloc_count = elf_section_data (o)->rel_count2; | |
2275 | ||
2276 | num_rel_hashes = o->reloc_count; | |
2277 | if (num_rel_hashes < reloc_count) | |
2278 | num_rel_hashes = reloc_count; | |
2279 | ||
2280 | /* That allows us to calculate the size of the section. */ | |
2281 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
2282 | ||
2283 | /* The contents field must last into write_object_contents, so we | |
2284 | allocate it with bfd_alloc rather than malloc. Also since we | |
2285 | cannot be sure that the contents will actually be filled in, | |
2286 | we zero the allocated space. */ | |
268b6b39 | 2287 | rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2288 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2289 | return FALSE; | |
2290 | ||
2291 | /* We only allocate one set of hash entries, so we only do it the | |
2292 | first time we are called. */ | |
2293 | if (elf_section_data (o)->rel_hashes == NULL | |
2294 | && num_rel_hashes) | |
2295 | { | |
2296 | struct elf_link_hash_entry **p; | |
2297 | ||
268b6b39 | 2298 | p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
2299 | if (p == NULL) |
2300 | return FALSE; | |
2301 | ||
2302 | elf_section_data (o)->rel_hashes = p; | |
2303 | } | |
2304 | ||
2305 | return TRUE; | |
2306 | } | |
2307 | ||
2308 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2309 | originated from the section given by INPUT_REL_HDR) to the | |
2310 | OUTPUT_BFD. */ | |
2311 | ||
2312 | bfd_boolean | |
268b6b39 AM |
2313 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2314 | asection *input_section, | |
2315 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2316 | Elf_Internal_Rela *internal_relocs, |
2317 | struct elf_link_hash_entry **rel_hash | |
2318 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2319 | { |
2320 | Elf_Internal_Rela *irela; | |
2321 | Elf_Internal_Rela *irelaend; | |
2322 | bfd_byte *erel; | |
2323 | Elf_Internal_Shdr *output_rel_hdr; | |
2324 | asection *output_section; | |
2325 | unsigned int *rel_countp = NULL; | |
9c5bfbb7 | 2326 | const struct elf_backend_data *bed; |
268b6b39 | 2327 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
45d6a902 AM |
2328 | |
2329 | output_section = input_section->output_section; | |
2330 | output_rel_hdr = NULL; | |
2331 | ||
2332 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
2333 | == input_rel_hdr->sh_entsize) | |
2334 | { | |
2335 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
2336 | rel_countp = &elf_section_data (output_section)->rel_count; | |
2337 | } | |
2338 | else if (elf_section_data (output_section)->rel_hdr2 | |
2339 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
2340 | == input_rel_hdr->sh_entsize)) | |
2341 | { | |
2342 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2343 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2344 | } | |
2345 | else | |
2346 | { | |
2347 | (*_bfd_error_handler) | |
d003868e AM |
2348 | (_("%B: relocation size mismatch in %B section %A"), |
2349 | output_bfd, input_section->owner, input_section); | |
297d8443 | 2350 | bfd_set_error (bfd_error_wrong_format); |
45d6a902 AM |
2351 | return FALSE; |
2352 | } | |
2353 | ||
2354 | bed = get_elf_backend_data (output_bfd); | |
2355 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2356 | swap_out = bed->s->swap_reloc_out; | |
2357 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2358 | swap_out = bed->s->swap_reloca_out; | |
2359 | else | |
2360 | abort (); | |
2361 | ||
2362 | erel = output_rel_hdr->contents; | |
2363 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2364 | irela = internal_relocs; | |
2365 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2366 | * bed->s->int_rels_per_ext_rel); | |
2367 | while (irela < irelaend) | |
2368 | { | |
2369 | (*swap_out) (output_bfd, irela, erel); | |
2370 | irela += bed->s->int_rels_per_ext_rel; | |
2371 | erel += input_rel_hdr->sh_entsize; | |
2372 | } | |
2373 | ||
2374 | /* Bump the counter, so that we know where to add the next set of | |
2375 | relocations. */ | |
2376 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2377 | ||
2378 | return TRUE; | |
2379 | } | |
2380 | \f | |
508c3946 L |
2381 | /* Make weak undefined symbols in PIE dynamic. */ |
2382 | ||
2383 | bfd_boolean | |
2384 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2385 | struct elf_link_hash_entry *h) | |
2386 | { | |
2387 | if (info->pie | |
2388 | && h->dynindx == -1 | |
2389 | && h->root.type == bfd_link_hash_undefweak) | |
2390 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2391 | ||
2392 | return TRUE; | |
2393 | } | |
2394 | ||
45d6a902 AM |
2395 | /* Fix up the flags for a symbol. This handles various cases which |
2396 | can only be fixed after all the input files are seen. This is | |
2397 | currently called by both adjust_dynamic_symbol and | |
2398 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2399 | the face of future changes. */ | |
2400 | ||
2401 | bfd_boolean | |
268b6b39 AM |
2402 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2403 | struct elf_info_failed *eif) | |
45d6a902 | 2404 | { |
33774f08 | 2405 | const struct elf_backend_data *bed; |
508c3946 | 2406 | |
45d6a902 AM |
2407 | /* If this symbol was mentioned in a non-ELF file, try to set |
2408 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2409 | permit a non-ELF file to correctly refer to a symbol defined in | |
2410 | an ELF dynamic object. */ | |
f5385ebf | 2411 | if (h->non_elf) |
45d6a902 AM |
2412 | { |
2413 | while (h->root.type == bfd_link_hash_indirect) | |
2414 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2415 | ||
2416 | if (h->root.type != bfd_link_hash_defined | |
2417 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2418 | { |
2419 | h->ref_regular = 1; | |
2420 | h->ref_regular_nonweak = 1; | |
2421 | } | |
45d6a902 AM |
2422 | else |
2423 | { | |
2424 | if (h->root.u.def.section->owner != NULL | |
2425 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2426 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2427 | { |
2428 | h->ref_regular = 1; | |
2429 | h->ref_regular_nonweak = 1; | |
2430 | } | |
45d6a902 | 2431 | else |
f5385ebf | 2432 | h->def_regular = 1; |
45d6a902 AM |
2433 | } |
2434 | ||
2435 | if (h->dynindx == -1 | |
f5385ebf AM |
2436 | && (h->def_dynamic |
2437 | || h->ref_dynamic)) | |
45d6a902 | 2438 | { |
c152c796 | 2439 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2440 | { |
2441 | eif->failed = TRUE; | |
2442 | return FALSE; | |
2443 | } | |
2444 | } | |
2445 | } | |
2446 | else | |
2447 | { | |
f5385ebf | 2448 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2449 | was first seen in a non-ELF file. Fortunately, if the symbol |
2450 | was first seen in an ELF file, we're probably OK unless the | |
2451 | symbol was defined in a non-ELF file. Catch that case here. | |
2452 | FIXME: We're still in trouble if the symbol was first seen in | |
2453 | a dynamic object, and then later in a non-ELF regular object. */ | |
2454 | if ((h->root.type == bfd_link_hash_defined | |
2455 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2456 | && !h->def_regular |
45d6a902 AM |
2457 | && (h->root.u.def.section->owner != NULL |
2458 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2459 | != bfd_target_elf_flavour) | |
2460 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2461 | && !h->def_dynamic))) |
2462 | h->def_regular = 1; | |
45d6a902 AM |
2463 | } |
2464 | ||
508c3946 | 2465 | /* Backend specific symbol fixup. */ |
33774f08 AM |
2466 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2467 | if (bed->elf_backend_fixup_symbol | |
2468 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2469 | return FALSE; | |
508c3946 | 2470 | |
45d6a902 AM |
2471 | /* If this is a final link, and the symbol was defined as a common |
2472 | symbol in a regular object file, and there was no definition in | |
2473 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2474 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2475 | flag will not have been set. */ |
2476 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2477 | && !h->def_regular |
2478 | && h->ref_regular | |
2479 | && !h->def_dynamic | |
45d6a902 | 2480 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) |
f5385ebf | 2481 | h->def_regular = 1; |
45d6a902 AM |
2482 | |
2483 | /* If -Bsymbolic was used (which means to bind references to global | |
2484 | symbols to the definition within the shared object), and this | |
2485 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2486 | need a PLT entry. Likewise, if the symbol has non-default |
2487 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2488 | will force it local. */ |
f5385ebf | 2489 | if (h->needs_plt |
45d6a902 | 2490 | && eif->info->shared |
0eddce27 | 2491 | && is_elf_hash_table (eif->info->hash) |
55255dae | 2492 | && (SYMBOLIC_BIND (eif->info, h) |
c1be741f | 2493 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2494 | && h->def_regular) |
45d6a902 | 2495 | { |
45d6a902 AM |
2496 | bfd_boolean force_local; |
2497 | ||
45d6a902 AM |
2498 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2499 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2500 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2501 | } | |
2502 | ||
2503 | /* If a weak undefined symbol has non-default visibility, we also | |
2504 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2505 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 | 2506 | && h->root.type == bfd_link_hash_undefweak) |
33774f08 | 2507 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
45d6a902 AM |
2508 | |
2509 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2510 | the real definition in the dynamic object, copy interesting flags | |
2511 | over to the real definition. */ | |
f6e332e6 | 2512 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2513 | { |
2514 | struct elf_link_hash_entry *weakdef; | |
2515 | ||
f6e332e6 | 2516 | weakdef = h->u.weakdef; |
45d6a902 AM |
2517 | if (h->root.type == bfd_link_hash_indirect) |
2518 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2519 | ||
2520 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2521 | || h->root.type == bfd_link_hash_defweak); | |
f5385ebf | 2522 | BFD_ASSERT (weakdef->def_dynamic); |
45d6a902 AM |
2523 | |
2524 | /* If the real definition is defined by a regular object file, | |
2525 | don't do anything special. See the longer description in | |
2526 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
f5385ebf | 2527 | if (weakdef->def_regular) |
f6e332e6 | 2528 | h->u.weakdef = NULL; |
45d6a902 | 2529 | else |
a26587ba RS |
2530 | { |
2531 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2532 | || weakdef->root.type == bfd_link_hash_defweak); | |
2533 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h); | |
2534 | } | |
45d6a902 AM |
2535 | } |
2536 | ||
2537 | return TRUE; | |
2538 | } | |
2539 | ||
2540 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2541 | called via elf_link_hash_traverse, and also calls itself | |
2542 | recursively. */ | |
2543 | ||
2544 | bfd_boolean | |
268b6b39 | 2545 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2546 | { |
268b6b39 | 2547 | struct elf_info_failed *eif = data; |
45d6a902 | 2548 | bfd *dynobj; |
9c5bfbb7 | 2549 | const struct elf_backend_data *bed; |
45d6a902 | 2550 | |
0eddce27 | 2551 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2552 | return FALSE; |
2553 | ||
2554 | if (h->root.type == bfd_link_hash_warning) | |
2555 | { | |
a6aa5195 AM |
2556 | h->got = elf_hash_table (eif->info)->init_got_offset; |
2557 | h->plt = elf_hash_table (eif->info)->init_plt_offset; | |
45d6a902 AM |
2558 | |
2559 | /* When warning symbols are created, they **replace** the "real" | |
2560 | entry in the hash table, thus we never get to see the real | |
2561 | symbol in a hash traversal. So look at it now. */ | |
2562 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2563 | } | |
2564 | ||
2565 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2566 | if (h->root.type == bfd_link_hash_indirect) | |
2567 | return TRUE; | |
2568 | ||
2569 | /* Fix the symbol flags. */ | |
2570 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2571 | return FALSE; | |
2572 | ||
2573 | /* If this symbol does not require a PLT entry, and it is not | |
2574 | defined by a dynamic object, or is not referenced by a regular | |
2575 | object, ignore it. We do have to handle a weak defined symbol, | |
2576 | even if no regular object refers to it, if we decided to add it | |
2577 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2578 | about symbols which are defined by one dynamic object and | |
2579 | referenced by another one? */ | |
f5385ebf AM |
2580 | if (!h->needs_plt |
2581 | && (h->def_regular | |
2582 | || !h->def_dynamic | |
2583 | || (!h->ref_regular | |
f6e332e6 | 2584 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 | 2585 | { |
a6aa5195 | 2586 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2587 | return TRUE; |
2588 | } | |
2589 | ||
2590 | /* If we've already adjusted this symbol, don't do it again. This | |
2591 | can happen via a recursive call. */ | |
f5385ebf | 2592 | if (h->dynamic_adjusted) |
45d6a902 AM |
2593 | return TRUE; |
2594 | ||
2595 | /* Don't look at this symbol again. Note that we must set this | |
2596 | after checking the above conditions, because we may look at a | |
2597 | symbol once, decide not to do anything, and then get called | |
2598 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2599 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2600 | |
2601 | /* If this is a weak definition, and we know a real definition, and | |
2602 | the real symbol is not itself defined by a regular object file, | |
2603 | then get a good value for the real definition. We handle the | |
2604 | real symbol first, for the convenience of the backend routine. | |
2605 | ||
2606 | Note that there is a confusing case here. If the real definition | |
2607 | is defined by a regular object file, we don't get the real symbol | |
2608 | from the dynamic object, but we do get the weak symbol. If the | |
2609 | processor backend uses a COPY reloc, then if some routine in the | |
2610 | dynamic object changes the real symbol, we will not see that | |
2611 | change in the corresponding weak symbol. This is the way other | |
2612 | ELF linkers work as well, and seems to be a result of the shared | |
2613 | library model. | |
2614 | ||
2615 | I will clarify this issue. Most SVR4 shared libraries define the | |
2616 | variable _timezone and define timezone as a weak synonym. The | |
2617 | tzset call changes _timezone. If you write | |
2618 | extern int timezone; | |
2619 | int _timezone = 5; | |
2620 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2621 | you might expect that, since timezone is a synonym for _timezone, | |
2622 | the same number will print both times. However, if the processor | |
2623 | backend uses a COPY reloc, then actually timezone will be copied | |
2624 | into your process image, and, since you define _timezone | |
2625 | yourself, _timezone will not. Thus timezone and _timezone will | |
2626 | wind up at different memory locations. The tzset call will set | |
2627 | _timezone, leaving timezone unchanged. */ | |
2628 | ||
f6e332e6 | 2629 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2630 | { |
2631 | /* If we get to this point, we know there is an implicit | |
2632 | reference by a regular object file via the weak symbol H. | |
2633 | FIXME: Is this really true? What if the traversal finds | |
f6e332e6 AM |
2634 | H->U.WEAKDEF before it finds H? */ |
2635 | h->u.weakdef->ref_regular = 1; | |
45d6a902 | 2636 | |
f6e332e6 | 2637 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2638 | return FALSE; |
2639 | } | |
2640 | ||
2641 | /* If a symbol has no type and no size and does not require a PLT | |
2642 | entry, then we are probably about to do the wrong thing here: we | |
2643 | are probably going to create a COPY reloc for an empty object. | |
2644 | This case can arise when a shared object is built with assembly | |
2645 | code, and the assembly code fails to set the symbol type. */ | |
2646 | if (h->size == 0 | |
2647 | && h->type == STT_NOTYPE | |
f5385ebf | 2648 | && !h->needs_plt) |
45d6a902 AM |
2649 | (*_bfd_error_handler) |
2650 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2651 | h->root.root.string); | |
2652 | ||
2653 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2654 | bed = get_elf_backend_data (dynobj); | |
2655 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2656 | { | |
2657 | eif->failed = TRUE; | |
2658 | return FALSE; | |
2659 | } | |
2660 | ||
2661 | return TRUE; | |
2662 | } | |
2663 | ||
027297b7 L |
2664 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
2665 | DYNBSS. */ | |
2666 | ||
2667 | bfd_boolean | |
2668 | _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h, | |
2669 | asection *dynbss) | |
2670 | { | |
91ac5911 | 2671 | unsigned int power_of_two; |
027297b7 L |
2672 | bfd_vma mask; |
2673 | asection *sec = h->root.u.def.section; | |
2674 | ||
2675 | /* The section aligment of definition is the maximum alignment | |
91ac5911 L |
2676 | requirement of symbols defined in the section. Since we don't |
2677 | know the symbol alignment requirement, we start with the | |
2678 | maximum alignment and check low bits of the symbol address | |
2679 | for the minimum alignment. */ | |
2680 | power_of_two = bfd_get_section_alignment (sec->owner, sec); | |
2681 | mask = ((bfd_vma) 1 << power_of_two) - 1; | |
2682 | while ((h->root.u.def.value & mask) != 0) | |
2683 | { | |
2684 | mask >>= 1; | |
2685 | --power_of_two; | |
2686 | } | |
027297b7 | 2687 | |
91ac5911 L |
2688 | if (power_of_two > bfd_get_section_alignment (dynbss->owner, |
2689 | dynbss)) | |
027297b7 L |
2690 | { |
2691 | /* Adjust the section alignment if needed. */ | |
2692 | if (! bfd_set_section_alignment (dynbss->owner, dynbss, | |
91ac5911 | 2693 | power_of_two)) |
027297b7 L |
2694 | return FALSE; |
2695 | } | |
2696 | ||
91ac5911 | 2697 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
2698 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
2699 | ||
2700 | /* Define the symbol as being at this point in DYNBSS. */ | |
2701 | h->root.u.def.section = dynbss; | |
2702 | h->root.u.def.value = dynbss->size; | |
2703 | ||
2704 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
2705 | dynbss->size += h->size; | |
2706 | ||
2707 | return TRUE; | |
2708 | } | |
2709 | ||
45d6a902 AM |
2710 | /* Adjust all external symbols pointing into SEC_MERGE sections |
2711 | to reflect the object merging within the sections. */ | |
2712 | ||
2713 | bfd_boolean | |
268b6b39 | 2714 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2715 | { |
2716 | asection *sec; | |
2717 | ||
2718 | if (h->root.type == bfd_link_hash_warning) | |
2719 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2720 | ||
2721 | if ((h->root.type == bfd_link_hash_defined | |
2722 | || h->root.type == bfd_link_hash_defweak) | |
2723 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2724 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2725 | { | |
268b6b39 | 2726 | bfd *output_bfd = data; |
45d6a902 AM |
2727 | |
2728 | h->root.u.def.value = | |
2729 | _bfd_merged_section_offset (output_bfd, | |
2730 | &h->root.u.def.section, | |
2731 | elf_section_data (sec)->sec_info, | |
753731ee | 2732 | h->root.u.def.value); |
45d6a902 AM |
2733 | } |
2734 | ||
2735 | return TRUE; | |
2736 | } | |
986a241f RH |
2737 | |
2738 | /* Returns false if the symbol referred to by H should be considered | |
2739 | to resolve local to the current module, and true if it should be | |
2740 | considered to bind dynamically. */ | |
2741 | ||
2742 | bfd_boolean | |
268b6b39 AM |
2743 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2744 | struct bfd_link_info *info, | |
2745 | bfd_boolean ignore_protected) | |
986a241f RH |
2746 | { |
2747 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
2748 | const struct elf_backend_data *bed; |
2749 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
2750 | |
2751 | if (h == NULL) | |
2752 | return FALSE; | |
2753 | ||
2754 | while (h->root.type == bfd_link_hash_indirect | |
2755 | || h->root.type == bfd_link_hash_warning) | |
2756 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2757 | ||
2758 | /* If it was forced local, then clearly it's not dynamic. */ | |
2759 | if (h->dynindx == -1) | |
2760 | return FALSE; | |
f5385ebf | 2761 | if (h->forced_local) |
986a241f RH |
2762 | return FALSE; |
2763 | ||
2764 | /* Identify the cases where name binding rules say that a | |
2765 | visible symbol resolves locally. */ | |
55255dae | 2766 | binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h); |
986a241f RH |
2767 | |
2768 | switch (ELF_ST_VISIBILITY (h->other)) | |
2769 | { | |
2770 | case STV_INTERNAL: | |
2771 | case STV_HIDDEN: | |
2772 | return FALSE; | |
2773 | ||
2774 | case STV_PROTECTED: | |
fcb93ecf PB |
2775 | hash_table = elf_hash_table (info); |
2776 | if (!is_elf_hash_table (hash_table)) | |
2777 | return FALSE; | |
2778 | ||
2779 | bed = get_elf_backend_data (hash_table->dynobj); | |
2780 | ||
986a241f RH |
2781 | /* Proper resolution for function pointer equality may require |
2782 | that these symbols perhaps be resolved dynamically, even though | |
2783 | we should be resolving them to the current module. */ | |
fcb93ecf | 2784 | if (!ignore_protected || !bed->is_function_type (h->type)) |
986a241f RH |
2785 | binding_stays_local_p = TRUE; |
2786 | break; | |
2787 | ||
2788 | default: | |
986a241f RH |
2789 | break; |
2790 | } | |
2791 | ||
aa37626c | 2792 | /* If it isn't defined locally, then clearly it's dynamic. */ |
f5385ebf | 2793 | if (!h->def_regular) |
aa37626c L |
2794 | return TRUE; |
2795 | ||
986a241f RH |
2796 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2797 | us that it remains local. */ | |
2798 | return !binding_stays_local_p; | |
2799 | } | |
f6c52c13 AM |
2800 | |
2801 | /* Return true if the symbol referred to by H should be considered | |
2802 | to resolve local to the current module, and false otherwise. Differs | |
2803 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2804 | undefined symbols and weak symbols. */ | |
2805 | ||
2806 | bfd_boolean | |
268b6b39 AM |
2807 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2808 | struct bfd_link_info *info, | |
2809 | bfd_boolean local_protected) | |
f6c52c13 | 2810 | { |
fcb93ecf PB |
2811 | const struct elf_backend_data *bed; |
2812 | struct elf_link_hash_table *hash_table; | |
2813 | ||
f6c52c13 AM |
2814 | /* If it's a local sym, of course we resolve locally. */ |
2815 | if (h == NULL) | |
2816 | return TRUE; | |
2817 | ||
d95edcac L |
2818 | /* STV_HIDDEN or STV_INTERNAL ones must be local. */ |
2819 | if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
2820 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
2821 | return TRUE; | |
2822 | ||
7e2294f9 AO |
2823 | /* Common symbols that become definitions don't get the DEF_REGULAR |
2824 | flag set, so test it first, and don't bail out. */ | |
2825 | if (ELF_COMMON_DEF_P (h)) | |
2826 | /* Do nothing. */; | |
f6c52c13 | 2827 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
2828 | resolve locally. The sym is either undefined or dynamic. */ |
2829 | else if (!h->def_regular) | |
f6c52c13 AM |
2830 | return FALSE; |
2831 | ||
2832 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 2833 | if (h->forced_local) |
f6c52c13 AM |
2834 | return TRUE; |
2835 | ||
2836 | /* As do non-dynamic symbols. */ | |
2837 | if (h->dynindx == -1) | |
2838 | return TRUE; | |
2839 | ||
2840 | /* At this point, we know the symbol is defined and dynamic. In an | |
2841 | executable it must resolve locally, likewise when building symbolic | |
2842 | shared libraries. */ | |
55255dae | 2843 | if (info->executable || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
2844 | return TRUE; |
2845 | ||
2846 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2847 | with default visibility might not resolve locally. */ | |
2848 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2849 | return FALSE; | |
2850 | ||
fcb93ecf PB |
2851 | hash_table = elf_hash_table (info); |
2852 | if (!is_elf_hash_table (hash_table)) | |
2853 | return TRUE; | |
2854 | ||
2855 | bed = get_elf_backend_data (hash_table->dynobj); | |
2856 | ||
1c16dfa5 | 2857 | /* STV_PROTECTED non-function symbols are local. */ |
fcb93ecf | 2858 | if (!bed->is_function_type (h->type)) |
1c16dfa5 L |
2859 | return TRUE; |
2860 | ||
f6c52c13 AM |
2861 | /* Function pointer equality tests may require that STV_PROTECTED |
2862 | symbols be treated as dynamic symbols, even when we know that the | |
2863 | dynamic linker will resolve them locally. */ | |
2864 | return local_protected; | |
2865 | } | |
e1918d23 AM |
2866 | |
2867 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2868 | aligned. Returns the first TLS output section. */ | |
2869 | ||
2870 | struct bfd_section * | |
2871 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2872 | { | |
2873 | struct bfd_section *sec, *tls; | |
2874 | unsigned int align = 0; | |
2875 | ||
2876 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2877 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2878 | break; | |
2879 | tls = sec; | |
2880 | ||
2881 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2882 | if (sec->alignment_power > align) | |
2883 | align = sec->alignment_power; | |
2884 | ||
2885 | elf_hash_table (info)->tls_sec = tls; | |
2886 | ||
2887 | /* Ensure the alignment of the first section is the largest alignment, | |
2888 | so that the tls segment starts aligned. */ | |
2889 | if (tls != NULL) | |
2890 | tls->alignment_power = align; | |
2891 | ||
2892 | return tls; | |
2893 | } | |
0ad989f9 L |
2894 | |
2895 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2896 | static bfd_boolean | |
2897 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2898 | Elf_Internal_Sym *sym) | |
2899 | { | |
a4d8e49b L |
2900 | const struct elf_backend_data *bed; |
2901 | ||
0ad989f9 L |
2902 | /* Local symbols do not count, but target specific ones might. */ |
2903 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2904 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2905 | return FALSE; | |
2906 | ||
fcb93ecf | 2907 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 2908 | /* Function symbols do not count. */ |
fcb93ecf | 2909 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
2910 | return FALSE; |
2911 | ||
2912 | /* If the section is undefined, then so is the symbol. */ | |
2913 | if (sym->st_shndx == SHN_UNDEF) | |
2914 | return FALSE; | |
2915 | ||
2916 | /* If the symbol is defined in the common section, then | |
2917 | it is a common definition and so does not count. */ | |
a4d8e49b | 2918 | if (bed->common_definition (sym)) |
0ad989f9 L |
2919 | return FALSE; |
2920 | ||
2921 | /* If the symbol is in a target specific section then we | |
2922 | must rely upon the backend to tell us what it is. */ | |
2923 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2924 | /* FIXME - this function is not coded yet: | |
2925 | ||
2926 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2927 | ||
2928 | Instead for now assume that the definition is not global, | |
2929 | Even if this is wrong, at least the linker will behave | |
2930 | in the same way that it used to do. */ | |
2931 | return FALSE; | |
2932 | ||
2933 | return TRUE; | |
2934 | } | |
2935 | ||
2936 | /* Search the symbol table of the archive element of the archive ABFD | |
2937 | whose archive map contains a mention of SYMDEF, and determine if | |
2938 | the symbol is defined in this element. */ | |
2939 | static bfd_boolean | |
2940 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2941 | { | |
2942 | Elf_Internal_Shdr * hdr; | |
2943 | bfd_size_type symcount; | |
2944 | bfd_size_type extsymcount; | |
2945 | bfd_size_type extsymoff; | |
2946 | Elf_Internal_Sym *isymbuf; | |
2947 | Elf_Internal_Sym *isym; | |
2948 | Elf_Internal_Sym *isymend; | |
2949 | bfd_boolean result; | |
2950 | ||
2951 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2952 | if (abfd == NULL) | |
2953 | return FALSE; | |
2954 | ||
2955 | if (! bfd_check_format (abfd, bfd_object)) | |
2956 | return FALSE; | |
2957 | ||
2958 | /* If we have already included the element containing this symbol in the | |
2959 | link then we do not need to include it again. Just claim that any symbol | |
2960 | it contains is not a definition, so that our caller will not decide to | |
2961 | (re)include this element. */ | |
2962 | if (abfd->archive_pass) | |
2963 | return FALSE; | |
2964 | ||
2965 | /* Select the appropriate symbol table. */ | |
2966 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2967 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2968 | else | |
2969 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2970 | ||
2971 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2972 | ||
2973 | /* The sh_info field of the symtab header tells us where the | |
2974 | external symbols start. We don't care about the local symbols. */ | |
2975 | if (elf_bad_symtab (abfd)) | |
2976 | { | |
2977 | extsymcount = symcount; | |
2978 | extsymoff = 0; | |
2979 | } | |
2980 | else | |
2981 | { | |
2982 | extsymcount = symcount - hdr->sh_info; | |
2983 | extsymoff = hdr->sh_info; | |
2984 | } | |
2985 | ||
2986 | if (extsymcount == 0) | |
2987 | return FALSE; | |
2988 | ||
2989 | /* Read in the symbol table. */ | |
2990 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2991 | NULL, NULL, NULL); | |
2992 | if (isymbuf == NULL) | |
2993 | return FALSE; | |
2994 | ||
2995 | /* Scan the symbol table looking for SYMDEF. */ | |
2996 | result = FALSE; | |
2997 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2998 | { | |
2999 | const char *name; | |
3000 | ||
3001 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3002 | isym->st_name); | |
3003 | if (name == NULL) | |
3004 | break; | |
3005 | ||
3006 | if (strcmp (name, symdef->name) == 0) | |
3007 | { | |
3008 | result = is_global_data_symbol_definition (abfd, isym); | |
3009 | break; | |
3010 | } | |
3011 | } | |
3012 | ||
3013 | free (isymbuf); | |
3014 | ||
3015 | return result; | |
3016 | } | |
3017 | \f | |
5a580b3a AM |
3018 | /* Add an entry to the .dynamic table. */ |
3019 | ||
3020 | bfd_boolean | |
3021 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
3022 | bfd_vma tag, | |
3023 | bfd_vma val) | |
3024 | { | |
3025 | struct elf_link_hash_table *hash_table; | |
3026 | const struct elf_backend_data *bed; | |
3027 | asection *s; | |
3028 | bfd_size_type newsize; | |
3029 | bfd_byte *newcontents; | |
3030 | Elf_Internal_Dyn dyn; | |
3031 | ||
3032 | hash_table = elf_hash_table (info); | |
3033 | if (! is_elf_hash_table (hash_table)) | |
3034 | return FALSE; | |
3035 | ||
3036 | bed = get_elf_backend_data (hash_table->dynobj); | |
3037 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
3038 | BFD_ASSERT (s != NULL); | |
3039 | ||
eea6121a | 3040 | newsize = s->size + bed->s->sizeof_dyn; |
5a580b3a AM |
3041 | newcontents = bfd_realloc (s->contents, newsize); |
3042 | if (newcontents == NULL) | |
3043 | return FALSE; | |
3044 | ||
3045 | dyn.d_tag = tag; | |
3046 | dyn.d_un.d_val = val; | |
eea6121a | 3047 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3048 | |
eea6121a | 3049 | s->size = newsize; |
5a580b3a AM |
3050 | s->contents = newcontents; |
3051 | ||
3052 | return TRUE; | |
3053 | } | |
3054 | ||
3055 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
3056 | otherwise just check whether one already exists. Returns -1 on error, | |
3057 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
3058 | ||
4ad4eba5 | 3059 | static int |
7e9f0867 AM |
3060 | elf_add_dt_needed_tag (bfd *abfd, |
3061 | struct bfd_link_info *info, | |
4ad4eba5 AM |
3062 | const char *soname, |
3063 | bfd_boolean do_it) | |
5a580b3a AM |
3064 | { |
3065 | struct elf_link_hash_table *hash_table; | |
3066 | bfd_size_type oldsize; | |
3067 | bfd_size_type strindex; | |
3068 | ||
7e9f0867 AM |
3069 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3070 | return -1; | |
3071 | ||
5a580b3a AM |
3072 | hash_table = elf_hash_table (info); |
3073 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
3074 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
3075 | if (strindex == (bfd_size_type) -1) | |
3076 | return -1; | |
3077 | ||
3078 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
3079 | { | |
3080 | asection *sdyn; | |
3081 | const struct elf_backend_data *bed; | |
3082 | bfd_byte *extdyn; | |
3083 | ||
3084 | bed = get_elf_backend_data (hash_table->dynobj); | |
3085 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
7e9f0867 AM |
3086 | if (sdyn != NULL) |
3087 | for (extdyn = sdyn->contents; | |
3088 | extdyn < sdyn->contents + sdyn->size; | |
3089 | extdyn += bed->s->sizeof_dyn) | |
3090 | { | |
3091 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3092 | |
7e9f0867 AM |
3093 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3094 | if (dyn.d_tag == DT_NEEDED | |
3095 | && dyn.d_un.d_val == strindex) | |
3096 | { | |
3097 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3098 | return 1; | |
3099 | } | |
3100 | } | |
5a580b3a AM |
3101 | } |
3102 | ||
3103 | if (do_it) | |
3104 | { | |
7e9f0867 AM |
3105 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3106 | return -1; | |
3107 | ||
5a580b3a AM |
3108 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3109 | return -1; | |
3110 | } | |
3111 | else | |
3112 | /* We were just checking for existence of the tag. */ | |
3113 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3114 | ||
3115 | return 0; | |
3116 | } | |
3117 | ||
3118 | /* Sort symbol by value and section. */ | |
4ad4eba5 AM |
3119 | static int |
3120 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3121 | { |
3122 | const struct elf_link_hash_entry *h1; | |
3123 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3124 | bfd_signed_vma vdiff; |
5a580b3a AM |
3125 | |
3126 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3127 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3128 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3129 | if (vdiff != 0) | |
3130 | return vdiff > 0 ? 1 : -1; | |
3131 | else | |
3132 | { | |
3133 | long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
3134 | if (sdiff != 0) | |
3135 | return sdiff > 0 ? 1 : -1; | |
3136 | } | |
5a580b3a AM |
3137 | return 0; |
3138 | } | |
4ad4eba5 | 3139 | |
5a580b3a AM |
3140 | /* This function is used to adjust offsets into .dynstr for |
3141 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3142 | ||
3143 | static bfd_boolean | |
3144 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3145 | { | |
3146 | struct elf_strtab_hash *dynstr = data; | |
3147 | ||
3148 | if (h->root.type == bfd_link_hash_warning) | |
3149 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3150 | ||
3151 | if (h->dynindx != -1) | |
3152 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3153 | return TRUE; | |
3154 | } | |
3155 | ||
3156 | /* Assign string offsets in .dynstr, update all structures referencing | |
3157 | them. */ | |
3158 | ||
4ad4eba5 AM |
3159 | static bfd_boolean |
3160 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3161 | { |
3162 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3163 | struct elf_link_local_dynamic_entry *entry; | |
3164 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3165 | bfd *dynobj = hash_table->dynobj; | |
3166 | asection *sdyn; | |
3167 | bfd_size_type size; | |
3168 | const struct elf_backend_data *bed; | |
3169 | bfd_byte *extdyn; | |
3170 | ||
3171 | _bfd_elf_strtab_finalize (dynstr); | |
3172 | size = _bfd_elf_strtab_size (dynstr); | |
3173 | ||
3174 | bed = get_elf_backend_data (dynobj); | |
3175 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
3176 | BFD_ASSERT (sdyn != NULL); | |
3177 | ||
3178 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3179 | for (extdyn = sdyn->contents; | |
eea6121a | 3180 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3181 | extdyn += bed->s->sizeof_dyn) |
3182 | { | |
3183 | Elf_Internal_Dyn dyn; | |
3184 | ||
3185 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3186 | switch (dyn.d_tag) | |
3187 | { | |
3188 | case DT_STRSZ: | |
3189 | dyn.d_un.d_val = size; | |
3190 | break; | |
3191 | case DT_NEEDED: | |
3192 | case DT_SONAME: | |
3193 | case DT_RPATH: | |
3194 | case DT_RUNPATH: | |
3195 | case DT_FILTER: | |
3196 | case DT_AUXILIARY: | |
3197 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
3198 | break; | |
3199 | default: | |
3200 | continue; | |
3201 | } | |
3202 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3203 | } | |
3204 | ||
3205 | /* Now update local dynamic symbols. */ | |
3206 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3207 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3208 | entry->isym.st_name); | |
3209 | ||
3210 | /* And the rest of dynamic symbols. */ | |
3211 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3212 | ||
3213 | /* Adjust version definitions. */ | |
3214 | if (elf_tdata (output_bfd)->cverdefs) | |
3215 | { | |
3216 | asection *s; | |
3217 | bfd_byte *p; | |
3218 | bfd_size_type i; | |
3219 | Elf_Internal_Verdef def; | |
3220 | Elf_Internal_Verdaux defaux; | |
3221 | ||
3222 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
3223 | p = s->contents; | |
3224 | do | |
3225 | { | |
3226 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3227 | &def); | |
3228 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3229 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3230 | continue; | |
5a580b3a AM |
3231 | for (i = 0; i < def.vd_cnt; ++i) |
3232 | { | |
3233 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3234 | (Elf_External_Verdaux *) p, &defaux); | |
3235 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3236 | defaux.vda_name); | |
3237 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3238 | &defaux, (Elf_External_Verdaux *) p); | |
3239 | p += sizeof (Elf_External_Verdaux); | |
3240 | } | |
3241 | } | |
3242 | while (def.vd_next); | |
3243 | } | |
3244 | ||
3245 | /* Adjust version references. */ | |
3246 | if (elf_tdata (output_bfd)->verref) | |
3247 | { | |
3248 | asection *s; | |
3249 | bfd_byte *p; | |
3250 | bfd_size_type i; | |
3251 | Elf_Internal_Verneed need; | |
3252 | Elf_Internal_Vernaux needaux; | |
3253 | ||
3254 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
3255 | p = s->contents; | |
3256 | do | |
3257 | { | |
3258 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3259 | &need); | |
3260 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3261 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3262 | (Elf_External_Verneed *) p); | |
3263 | p += sizeof (Elf_External_Verneed); | |
3264 | for (i = 0; i < need.vn_cnt; ++i) | |
3265 | { | |
3266 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3267 | (Elf_External_Vernaux *) p, &needaux); | |
3268 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3269 | needaux.vna_name); | |
3270 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3271 | &needaux, | |
3272 | (Elf_External_Vernaux *) p); | |
3273 | p += sizeof (Elf_External_Vernaux); | |
3274 | } | |
3275 | } | |
3276 | while (need.vn_next); | |
3277 | } | |
3278 | ||
3279 | return TRUE; | |
3280 | } | |
3281 | \f | |
13285a1b AM |
3282 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
3283 | The default is to only match when the INPUT and OUTPUT are exactly | |
3284 | the same target. */ | |
3285 | ||
3286 | bfd_boolean | |
3287 | _bfd_elf_default_relocs_compatible (const bfd_target *input, | |
3288 | const bfd_target *output) | |
3289 | { | |
3290 | return input == output; | |
3291 | } | |
3292 | ||
3293 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
3294 | This version is used when different targets for the same architecture | |
3295 | are virtually identical. */ | |
3296 | ||
3297 | bfd_boolean | |
3298 | _bfd_elf_relocs_compatible (const bfd_target *input, | |
3299 | const bfd_target *output) | |
3300 | { | |
3301 | const struct elf_backend_data *obed, *ibed; | |
3302 | ||
3303 | if (input == output) | |
3304 | return TRUE; | |
3305 | ||
3306 | ibed = xvec_get_elf_backend_data (input); | |
3307 | obed = xvec_get_elf_backend_data (output); | |
3308 | ||
3309 | if (ibed->arch != obed->arch) | |
3310 | return FALSE; | |
3311 | ||
3312 | /* If both backends are using this function, deem them compatible. */ | |
3313 | return ibed->relocs_compatible == obed->relocs_compatible; | |
3314 | } | |
3315 | ||
4ad4eba5 AM |
3316 | /* Add symbols from an ELF object file to the linker hash table. */ |
3317 | ||
3318 | static bfd_boolean | |
3319 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3320 | { | |
4ad4eba5 AM |
3321 | Elf_Internal_Shdr *hdr; |
3322 | bfd_size_type symcount; | |
3323 | bfd_size_type extsymcount; | |
3324 | bfd_size_type extsymoff; | |
3325 | struct elf_link_hash_entry **sym_hash; | |
3326 | bfd_boolean dynamic; | |
3327 | Elf_External_Versym *extversym = NULL; | |
3328 | Elf_External_Versym *ever; | |
3329 | struct elf_link_hash_entry *weaks; | |
3330 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
3331 | bfd_size_type nondeflt_vers_cnt = 0; | |
3332 | Elf_Internal_Sym *isymbuf = NULL; | |
3333 | Elf_Internal_Sym *isym; | |
3334 | Elf_Internal_Sym *isymend; | |
3335 | const struct elf_backend_data *bed; | |
3336 | bfd_boolean add_needed; | |
66eb6687 | 3337 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3338 | bfd_size_type amt; |
66eb6687 | 3339 | void *alloc_mark = NULL; |
4f87808c AM |
3340 | struct bfd_hash_entry **old_table = NULL; |
3341 | unsigned int old_size = 0; | |
3342 | unsigned int old_count = 0; | |
66eb6687 AM |
3343 | void *old_tab = NULL; |
3344 | void *old_hash; | |
3345 | void *old_ent; | |
3346 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3347 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
3348 | long old_dynsymcount = 0; | |
3349 | size_t tabsize = 0; | |
3350 | size_t hashsize = 0; | |
4ad4eba5 | 3351 | |
66eb6687 | 3352 | htab = elf_hash_table (info); |
4ad4eba5 | 3353 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3354 | |
3355 | if ((abfd->flags & DYNAMIC) == 0) | |
3356 | dynamic = FALSE; | |
3357 | else | |
3358 | { | |
3359 | dynamic = TRUE; | |
3360 | ||
3361 | /* You can't use -r against a dynamic object. Also, there's no | |
3362 | hope of using a dynamic object which does not exactly match | |
3363 | the format of the output file. */ | |
3364 | if (info->relocatable | |
66eb6687 | 3365 | || !is_elf_hash_table (htab) |
f13a99db | 3366 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 3367 | { |
9a0789ec NC |
3368 | if (info->relocatable) |
3369 | bfd_set_error (bfd_error_invalid_operation); | |
3370 | else | |
3371 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3372 | goto error_return; |
3373 | } | |
3374 | } | |
3375 | ||
3376 | /* As a GNU extension, any input sections which are named | |
3377 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3378 | symbol. This differs from .gnu.warning sections, which generate | |
3379 | warnings when they are included in an output file. */ | |
3380 | if (info->executable) | |
3381 | { | |
3382 | asection *s; | |
3383 | ||
3384 | for (s = abfd->sections; s != NULL; s = s->next) | |
3385 | { | |
3386 | const char *name; | |
3387 | ||
3388 | name = bfd_get_section_name (abfd, s); | |
0112cd26 | 3389 | if (CONST_STRNEQ (name, ".gnu.warning.")) |
4ad4eba5 AM |
3390 | { |
3391 | char *msg; | |
3392 | bfd_size_type sz; | |
4ad4eba5 AM |
3393 | |
3394 | name += sizeof ".gnu.warning." - 1; | |
3395 | ||
3396 | /* If this is a shared object, then look up the symbol | |
3397 | in the hash table. If it is there, and it is already | |
3398 | been defined, then we will not be using the entry | |
3399 | from this shared object, so we don't need to warn. | |
3400 | FIXME: If we see the definition in a regular object | |
3401 | later on, we will warn, but we shouldn't. The only | |
3402 | fix is to keep track of what warnings we are supposed | |
3403 | to emit, and then handle them all at the end of the | |
3404 | link. */ | |
3405 | if (dynamic) | |
3406 | { | |
3407 | struct elf_link_hash_entry *h; | |
3408 | ||
66eb6687 | 3409 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
4ad4eba5 AM |
3410 | |
3411 | /* FIXME: What about bfd_link_hash_common? */ | |
3412 | if (h != NULL | |
3413 | && (h->root.type == bfd_link_hash_defined | |
3414 | || h->root.type == bfd_link_hash_defweak)) | |
3415 | { | |
3416 | /* We don't want to issue this warning. Clobber | |
3417 | the section size so that the warning does not | |
3418 | get copied into the output file. */ | |
eea6121a | 3419 | s->size = 0; |
4ad4eba5 AM |
3420 | continue; |
3421 | } | |
3422 | } | |
3423 | ||
eea6121a | 3424 | sz = s->size; |
370a0e1b | 3425 | msg = bfd_alloc (abfd, sz + 1); |
4ad4eba5 AM |
3426 | if (msg == NULL) |
3427 | goto error_return; | |
3428 | ||
370a0e1b | 3429 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
4ad4eba5 AM |
3430 | goto error_return; |
3431 | ||
370a0e1b | 3432 | msg[sz] = '\0'; |
4ad4eba5 AM |
3433 | |
3434 | if (! (_bfd_generic_link_add_one_symbol | |
3435 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
66eb6687 | 3436 | FALSE, bed->collect, NULL))) |
4ad4eba5 AM |
3437 | goto error_return; |
3438 | ||
3439 | if (! info->relocatable) | |
3440 | { | |
3441 | /* Clobber the section size so that the warning does | |
3442 | not get copied into the output file. */ | |
eea6121a | 3443 | s->size = 0; |
11d2f718 AM |
3444 | |
3445 | /* Also set SEC_EXCLUDE, so that symbols defined in | |
3446 | the warning section don't get copied to the output. */ | |
3447 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3448 | } |
3449 | } | |
3450 | } | |
3451 | } | |
3452 | ||
3453 | add_needed = TRUE; | |
3454 | if (! dynamic) | |
3455 | { | |
3456 | /* If we are creating a shared library, create all the dynamic | |
3457 | sections immediately. We need to attach them to something, | |
3458 | so we attach them to this BFD, provided it is the right | |
3459 | format. FIXME: If there are no input BFD's of the same | |
3460 | format as the output, we can't make a shared library. */ | |
3461 | if (info->shared | |
66eb6687 | 3462 | && is_elf_hash_table (htab) |
f13a99db | 3463 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 3464 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
3465 | { |
3466 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3467 | goto error_return; | |
3468 | } | |
3469 | } | |
66eb6687 | 3470 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
3471 | goto error_return; |
3472 | else | |
3473 | { | |
3474 | asection *s; | |
3475 | const char *soname = NULL; | |
3476 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; | |
3477 | int ret; | |
3478 | ||
3479 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3480 | ld shouldn't allow it. */ |
4ad4eba5 AM |
3481 | if ((s = abfd->sections) != NULL |
3482 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
92fd189d | 3483 | abort (); |
4ad4eba5 AM |
3484 | |
3485 | /* If this dynamic lib was specified on the command line with | |
3486 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3487 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3488 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3489 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3490 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3491 | all. */ | |
3492 | add_needed = (elf_dyn_lib_class (abfd) | |
3493 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3494 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3495 | |
3496 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3497 | if (s != NULL) | |
3498 | { | |
3499 | bfd_byte *dynbuf; | |
3500 | bfd_byte *extdyn; | |
cb33740c | 3501 | unsigned int elfsec; |
4ad4eba5 AM |
3502 | unsigned long shlink; |
3503 | ||
eea6121a | 3504 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
4ad4eba5 AM |
3505 | goto error_free_dyn; |
3506 | ||
3507 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 3508 | if (elfsec == SHN_BAD) |
4ad4eba5 AM |
3509 | goto error_free_dyn; |
3510 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3511 | ||
3512 | for (extdyn = dynbuf; | |
eea6121a | 3513 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3514 | extdyn += bed->s->sizeof_dyn) |
3515 | { | |
3516 | Elf_Internal_Dyn dyn; | |
3517 | ||
3518 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3519 | if (dyn.d_tag == DT_SONAME) | |
3520 | { | |
3521 | unsigned int tagv = dyn.d_un.d_val; | |
3522 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3523 | if (soname == NULL) | |
3524 | goto error_free_dyn; | |
3525 | } | |
3526 | if (dyn.d_tag == DT_NEEDED) | |
3527 | { | |
3528 | struct bfd_link_needed_list *n, **pn; | |
3529 | char *fnm, *anm; | |
3530 | unsigned int tagv = dyn.d_un.d_val; | |
3531 | ||
3532 | amt = sizeof (struct bfd_link_needed_list); | |
3533 | n = bfd_alloc (abfd, amt); | |
3534 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3535 | if (n == NULL || fnm == NULL) | |
3536 | goto error_free_dyn; | |
3537 | amt = strlen (fnm) + 1; | |
3538 | anm = bfd_alloc (abfd, amt); | |
3539 | if (anm == NULL) | |
3540 | goto error_free_dyn; | |
3541 | memcpy (anm, fnm, amt); | |
3542 | n->name = anm; | |
3543 | n->by = abfd; | |
3544 | n->next = NULL; | |
66eb6687 | 3545 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3546 | ; |
3547 | *pn = n; | |
3548 | } | |
3549 | if (dyn.d_tag == DT_RUNPATH) | |
3550 | { | |
3551 | struct bfd_link_needed_list *n, **pn; | |
3552 | char *fnm, *anm; | |
3553 | unsigned int tagv = dyn.d_un.d_val; | |
3554 | ||
3555 | amt = sizeof (struct bfd_link_needed_list); | |
3556 | n = bfd_alloc (abfd, amt); | |
3557 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3558 | if (n == NULL || fnm == NULL) | |
3559 | goto error_free_dyn; | |
3560 | amt = strlen (fnm) + 1; | |
3561 | anm = bfd_alloc (abfd, amt); | |
3562 | if (anm == NULL) | |
3563 | goto error_free_dyn; | |
3564 | memcpy (anm, fnm, amt); | |
3565 | n->name = anm; | |
3566 | n->by = abfd; | |
3567 | n->next = NULL; | |
3568 | for (pn = & runpath; | |
3569 | *pn != NULL; | |
3570 | pn = &(*pn)->next) | |
3571 | ; | |
3572 | *pn = n; | |
3573 | } | |
3574 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3575 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3576 | { | |
3577 | struct bfd_link_needed_list *n, **pn; | |
3578 | char *fnm, *anm; | |
3579 | unsigned int tagv = dyn.d_un.d_val; | |
3580 | ||
3581 | amt = sizeof (struct bfd_link_needed_list); | |
3582 | n = bfd_alloc (abfd, amt); | |
3583 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3584 | if (n == NULL || fnm == NULL) | |
3585 | goto error_free_dyn; | |
3586 | amt = strlen (fnm) + 1; | |
3587 | anm = bfd_alloc (abfd, amt); | |
3588 | if (anm == NULL) | |
3589 | { | |
3590 | error_free_dyn: | |
3591 | free (dynbuf); | |
3592 | goto error_return; | |
3593 | } | |
3594 | memcpy (anm, fnm, amt); | |
3595 | n->name = anm; | |
3596 | n->by = abfd; | |
3597 | n->next = NULL; | |
3598 | for (pn = & rpath; | |
3599 | *pn != NULL; | |
3600 | pn = &(*pn)->next) | |
3601 | ; | |
3602 | *pn = n; | |
3603 | } | |
3604 | } | |
3605 | ||
3606 | free (dynbuf); | |
3607 | } | |
3608 | ||
3609 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3610 | frees all more recently bfd_alloc'd blocks as well. */ | |
3611 | if (runpath) | |
3612 | rpath = runpath; | |
3613 | ||
3614 | if (rpath) | |
3615 | { | |
3616 | struct bfd_link_needed_list **pn; | |
66eb6687 | 3617 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3618 | ; |
3619 | *pn = rpath; | |
3620 | } | |
3621 | ||
3622 | /* We do not want to include any of the sections in a dynamic | |
3623 | object in the output file. We hack by simply clobbering the | |
3624 | list of sections in the BFD. This could be handled more | |
3625 | cleanly by, say, a new section flag; the existing | |
3626 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3627 | still implies that the section takes up space in the output | |
3628 | file. */ | |
3629 | bfd_section_list_clear (abfd); | |
3630 | ||
4ad4eba5 AM |
3631 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3632 | object. If the object has a DT_SONAME entry, we use it. | |
3633 | Otherwise, if the generic linker stuck something in | |
3634 | elf_dt_name, we use that. Otherwise, we just use the file | |
3635 | name. */ | |
3636 | if (soname == NULL || *soname == '\0') | |
3637 | { | |
3638 | soname = elf_dt_name (abfd); | |
3639 | if (soname == NULL || *soname == '\0') | |
3640 | soname = bfd_get_filename (abfd); | |
3641 | } | |
3642 | ||
3643 | /* Save the SONAME because sometimes the linker emulation code | |
3644 | will need to know it. */ | |
3645 | elf_dt_name (abfd) = soname; | |
3646 | ||
7e9f0867 | 3647 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3648 | if (ret < 0) |
3649 | goto error_return; | |
3650 | ||
3651 | /* If we have already included this dynamic object in the | |
3652 | link, just ignore it. There is no reason to include a | |
3653 | particular dynamic object more than once. */ | |
3654 | if (ret > 0) | |
3655 | return TRUE; | |
3656 | } | |
3657 | ||
3658 | /* If this is a dynamic object, we always link against the .dynsym | |
3659 | symbol table, not the .symtab symbol table. The dynamic linker | |
3660 | will only see the .dynsym symbol table, so there is no reason to | |
3661 | look at .symtab for a dynamic object. */ | |
3662 | ||
3663 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3664 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3665 | else | |
3666 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3667 | ||
3668 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3669 | ||
3670 | /* The sh_info field of the symtab header tells us where the | |
3671 | external symbols start. We don't care about the local symbols at | |
3672 | this point. */ | |
3673 | if (elf_bad_symtab (abfd)) | |
3674 | { | |
3675 | extsymcount = symcount; | |
3676 | extsymoff = 0; | |
3677 | } | |
3678 | else | |
3679 | { | |
3680 | extsymcount = symcount - hdr->sh_info; | |
3681 | extsymoff = hdr->sh_info; | |
3682 | } | |
3683 | ||
3684 | sym_hash = NULL; | |
3685 | if (extsymcount != 0) | |
3686 | { | |
3687 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3688 | NULL, NULL, NULL); | |
3689 | if (isymbuf == NULL) | |
3690 | goto error_return; | |
3691 | ||
3692 | /* We store a pointer to the hash table entry for each external | |
3693 | symbol. */ | |
3694 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3695 | sym_hash = bfd_alloc (abfd, amt); | |
3696 | if (sym_hash == NULL) | |
3697 | goto error_free_sym; | |
3698 | elf_sym_hashes (abfd) = sym_hash; | |
3699 | } | |
3700 | ||
3701 | if (dynamic) | |
3702 | { | |
3703 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
3704 | if (!_bfd_elf_slurp_version_tables (abfd, |
3705 | info->default_imported_symver)) | |
4ad4eba5 AM |
3706 | goto error_free_sym; |
3707 | ||
3708 | /* Read in the symbol versions, but don't bother to convert them | |
3709 | to internal format. */ | |
3710 | if (elf_dynversym (abfd) != 0) | |
3711 | { | |
3712 | Elf_Internal_Shdr *versymhdr; | |
3713 | ||
3714 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
3715 | extversym = bfd_malloc (versymhdr->sh_size); | |
3716 | if (extversym == NULL) | |
3717 | goto error_free_sym; | |
3718 | amt = versymhdr->sh_size; | |
3719 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3720 | || bfd_bread (extversym, amt, abfd) != amt) | |
3721 | goto error_free_vers; | |
3722 | } | |
3723 | } | |
3724 | ||
66eb6687 AM |
3725 | /* If we are loading an as-needed shared lib, save the symbol table |
3726 | state before we start adding symbols. If the lib turns out | |
3727 | to be unneeded, restore the state. */ | |
3728 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
3729 | { | |
3730 | unsigned int i; | |
3731 | size_t entsize; | |
3732 | ||
3733 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
3734 | { | |
3735 | struct bfd_hash_entry *p; | |
2de92251 | 3736 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3737 | |
3738 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
3739 | { |
3740 | h = (struct elf_link_hash_entry *) p; | |
3741 | entsize += htab->root.table.entsize; | |
3742 | if (h->root.type == bfd_link_hash_warning) | |
3743 | entsize += htab->root.table.entsize; | |
3744 | } | |
66eb6687 AM |
3745 | } |
3746 | ||
3747 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
3748 | hashsize = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3749 | old_tab = bfd_malloc (tabsize + entsize + hashsize); | |
3750 | if (old_tab == NULL) | |
3751 | goto error_free_vers; | |
3752 | ||
3753 | /* Remember the current objalloc pointer, so that all mem for | |
3754 | symbols added can later be reclaimed. */ | |
3755 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
3756 | if (alloc_mark == NULL) | |
3757 | goto error_free_vers; | |
3758 | ||
5061a885 AM |
3759 | /* Make a special call to the linker "notice" function to |
3760 | tell it that we are about to handle an as-needed lib. */ | |
3761 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, | |
3762 | notice_as_needed)) | |
9af2a943 | 3763 | goto error_free_vers; |
5061a885 | 3764 | |
66eb6687 AM |
3765 | /* Clone the symbol table and sym hashes. Remember some |
3766 | pointers into the symbol table, and dynamic symbol count. */ | |
3767 | old_hash = (char *) old_tab + tabsize; | |
3768 | old_ent = (char *) old_hash + hashsize; | |
3769 | memcpy (old_tab, htab->root.table.table, tabsize); | |
3770 | memcpy (old_hash, sym_hash, hashsize); | |
3771 | old_undefs = htab->root.undefs; | |
3772 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
3773 | old_table = htab->root.table.table; |
3774 | old_size = htab->root.table.size; | |
3775 | old_count = htab->root.table.count; | |
66eb6687 AM |
3776 | old_dynsymcount = htab->dynsymcount; |
3777 | ||
3778 | for (i = 0; i < htab->root.table.size; i++) | |
3779 | { | |
3780 | struct bfd_hash_entry *p; | |
2de92251 | 3781 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3782 | |
3783 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
3784 | { | |
3785 | memcpy (old_ent, p, htab->root.table.entsize); | |
3786 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
3787 | h = (struct elf_link_hash_entry *) p; |
3788 | if (h->root.type == bfd_link_hash_warning) | |
3789 | { | |
3790 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
3791 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
3792 | } | |
66eb6687 AM |
3793 | } |
3794 | } | |
3795 | } | |
4ad4eba5 | 3796 | |
66eb6687 | 3797 | weaks = NULL; |
4ad4eba5 AM |
3798 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
3799 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3800 | isym < isymend; | |
3801 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3802 | { | |
3803 | int bind; | |
3804 | bfd_vma value; | |
af44c138 | 3805 | asection *sec, *new_sec; |
4ad4eba5 AM |
3806 | flagword flags; |
3807 | const char *name; | |
3808 | struct elf_link_hash_entry *h; | |
3809 | bfd_boolean definition; | |
3810 | bfd_boolean size_change_ok; | |
3811 | bfd_boolean type_change_ok; | |
3812 | bfd_boolean new_weakdef; | |
3813 | bfd_boolean override; | |
a4d8e49b | 3814 | bfd_boolean common; |
4ad4eba5 AM |
3815 | unsigned int old_alignment; |
3816 | bfd *old_bfd; | |
3817 | ||
3818 | override = FALSE; | |
3819 | ||
3820 | flags = BSF_NO_FLAGS; | |
3821 | sec = NULL; | |
3822 | value = isym->st_value; | |
3823 | *sym_hash = NULL; | |
a4d8e49b | 3824 | common = bed->common_definition (isym); |
4ad4eba5 AM |
3825 | |
3826 | bind = ELF_ST_BIND (isym->st_info); | |
3827 | if (bind == STB_LOCAL) | |
3828 | { | |
3829 | /* This should be impossible, since ELF requires that all | |
3830 | global symbols follow all local symbols, and that sh_info | |
3831 | point to the first global symbol. Unfortunately, Irix 5 | |
3832 | screws this up. */ | |
3833 | continue; | |
3834 | } | |
3835 | else if (bind == STB_GLOBAL) | |
3836 | { | |
a4d8e49b | 3837 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 AM |
3838 | flags = BSF_GLOBAL; |
3839 | } | |
3840 | else if (bind == STB_WEAK) | |
3841 | flags = BSF_WEAK; | |
3842 | else | |
3843 | { | |
3844 | /* Leave it up to the processor backend. */ | |
3845 | } | |
3846 | ||
3847 | if (isym->st_shndx == SHN_UNDEF) | |
3848 | sec = bfd_und_section_ptr; | |
cb33740c AM |
3849 | else if (isym->st_shndx == SHN_ABS) |
3850 | sec = bfd_abs_section_ptr; | |
3851 | else if (isym->st_shndx == SHN_COMMON) | |
3852 | { | |
3853 | sec = bfd_com_section_ptr; | |
3854 | /* What ELF calls the size we call the value. What ELF | |
3855 | calls the value we call the alignment. */ | |
3856 | value = isym->st_size; | |
3857 | } | |
3858 | else | |
4ad4eba5 AM |
3859 | { |
3860 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3861 | if (sec == NULL) | |
3862 | sec = bfd_abs_section_ptr; | |
529fcb95 PB |
3863 | else if (sec->kept_section) |
3864 | { | |
e5d08002 L |
3865 | /* Symbols from discarded section are undefined. We keep |
3866 | its visibility. */ | |
529fcb95 PB |
3867 | sec = bfd_und_section_ptr; |
3868 | isym->st_shndx = SHN_UNDEF; | |
3869 | } | |
4ad4eba5 AM |
3870 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
3871 | value -= sec->vma; | |
3872 | } | |
4ad4eba5 AM |
3873 | |
3874 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3875 | isym->st_name); | |
3876 | if (name == NULL) | |
3877 | goto error_free_vers; | |
3878 | ||
3879 | if (isym->st_shndx == SHN_COMMON | |
6a4a0940 JJ |
3880 | && ELF_ST_TYPE (isym->st_info) == STT_TLS |
3881 | && !info->relocatable) | |
4ad4eba5 AM |
3882 | { |
3883 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3884 | ||
3885 | if (tcomm == NULL) | |
3886 | { | |
3496cb2a L |
3887 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", |
3888 | (SEC_ALLOC | |
3889 | | SEC_IS_COMMON | |
3890 | | SEC_LINKER_CREATED | |
3891 | | SEC_THREAD_LOCAL)); | |
3892 | if (tcomm == NULL) | |
4ad4eba5 AM |
3893 | goto error_free_vers; |
3894 | } | |
3895 | sec = tcomm; | |
3896 | } | |
66eb6687 | 3897 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 3898 | { |
66eb6687 AM |
3899 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
3900 | &sec, &value)) | |
4ad4eba5 AM |
3901 | goto error_free_vers; |
3902 | ||
3903 | /* The hook function sets the name to NULL if this symbol | |
3904 | should be skipped for some reason. */ | |
3905 | if (name == NULL) | |
3906 | continue; | |
3907 | } | |
3908 | ||
3909 | /* Sanity check that all possibilities were handled. */ | |
3910 | if (sec == NULL) | |
3911 | { | |
3912 | bfd_set_error (bfd_error_bad_value); | |
3913 | goto error_free_vers; | |
3914 | } | |
3915 | ||
3916 | if (bfd_is_und_section (sec) | |
3917 | || bfd_is_com_section (sec)) | |
3918 | definition = FALSE; | |
3919 | else | |
3920 | definition = TRUE; | |
3921 | ||
3922 | size_change_ok = FALSE; | |
66eb6687 | 3923 | type_change_ok = bed->type_change_ok; |
4ad4eba5 AM |
3924 | old_alignment = 0; |
3925 | old_bfd = NULL; | |
af44c138 | 3926 | new_sec = sec; |
4ad4eba5 | 3927 | |
66eb6687 | 3928 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
3929 | { |
3930 | Elf_Internal_Versym iver; | |
3931 | unsigned int vernum = 0; | |
3932 | bfd_boolean skip; | |
3933 | ||
fc0e6df6 | 3934 | if (ever == NULL) |
4ad4eba5 | 3935 | { |
fc0e6df6 PB |
3936 | if (info->default_imported_symver) |
3937 | /* Use the default symbol version created earlier. */ | |
3938 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
3939 | else | |
3940 | iver.vs_vers = 0; | |
3941 | } | |
3942 | else | |
3943 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
3944 | ||
3945 | vernum = iver.vs_vers & VERSYM_VERSION; | |
3946 | ||
3947 | /* If this is a hidden symbol, or if it is not version | |
3948 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
3949 | However, we do not modify a non-hidden absolute symbol |
3950 | if it is not a function, because it might be the version | |
3951 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 3952 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
3953 | || (vernum > 1 |
3954 | && (!bfd_is_abs_section (sec) | |
3955 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
3956 | { |
3957 | const char *verstr; | |
3958 | size_t namelen, verlen, newlen; | |
3959 | char *newname, *p; | |
3960 | ||
3961 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 3962 | { |
fc0e6df6 PB |
3963 | if (vernum > elf_tdata (abfd)->cverdefs) |
3964 | verstr = NULL; | |
3965 | else if (vernum > 1) | |
3966 | verstr = | |
3967 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
3968 | else | |
3969 | verstr = ""; | |
4ad4eba5 | 3970 | |
fc0e6df6 | 3971 | if (verstr == NULL) |
4ad4eba5 | 3972 | { |
fc0e6df6 PB |
3973 | (*_bfd_error_handler) |
3974 | (_("%B: %s: invalid version %u (max %d)"), | |
3975 | abfd, name, vernum, | |
3976 | elf_tdata (abfd)->cverdefs); | |
3977 | bfd_set_error (bfd_error_bad_value); | |
3978 | goto error_free_vers; | |
4ad4eba5 | 3979 | } |
fc0e6df6 PB |
3980 | } |
3981 | else | |
3982 | { | |
3983 | /* We cannot simply test for the number of | |
3984 | entries in the VERNEED section since the | |
3985 | numbers for the needed versions do not start | |
3986 | at 0. */ | |
3987 | Elf_Internal_Verneed *t; | |
3988 | ||
3989 | verstr = NULL; | |
3990 | for (t = elf_tdata (abfd)->verref; | |
3991 | t != NULL; | |
3992 | t = t->vn_nextref) | |
4ad4eba5 | 3993 | { |
fc0e6df6 | 3994 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 3995 | |
fc0e6df6 PB |
3996 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
3997 | { | |
3998 | if (a->vna_other == vernum) | |
4ad4eba5 | 3999 | { |
fc0e6df6 PB |
4000 | verstr = a->vna_nodename; |
4001 | break; | |
4ad4eba5 | 4002 | } |
4ad4eba5 | 4003 | } |
fc0e6df6 PB |
4004 | if (a != NULL) |
4005 | break; | |
4006 | } | |
4007 | if (verstr == NULL) | |
4008 | { | |
4009 | (*_bfd_error_handler) | |
4010 | (_("%B: %s: invalid needed version %d"), | |
4011 | abfd, name, vernum); | |
4012 | bfd_set_error (bfd_error_bad_value); | |
4013 | goto error_free_vers; | |
4ad4eba5 | 4014 | } |
4ad4eba5 | 4015 | } |
fc0e6df6 PB |
4016 | |
4017 | namelen = strlen (name); | |
4018 | verlen = strlen (verstr); | |
4019 | newlen = namelen + verlen + 2; | |
4020 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4021 | && isym->st_shndx != SHN_UNDEF) | |
4022 | ++newlen; | |
4023 | ||
66eb6687 | 4024 | newname = bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
4025 | if (newname == NULL) |
4026 | goto error_free_vers; | |
4027 | memcpy (newname, name, namelen); | |
4028 | p = newname + namelen; | |
4029 | *p++ = ELF_VER_CHR; | |
4030 | /* If this is a defined non-hidden version symbol, | |
4031 | we add another @ to the name. This indicates the | |
4032 | default version of the symbol. */ | |
4033 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4034 | && isym->st_shndx != SHN_UNDEF) | |
4035 | *p++ = ELF_VER_CHR; | |
4036 | memcpy (p, verstr, verlen + 1); | |
4037 | ||
4038 | name = newname; | |
4ad4eba5 AM |
4039 | } |
4040 | ||
af44c138 L |
4041 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, |
4042 | &value, &old_alignment, | |
4ad4eba5 AM |
4043 | sym_hash, &skip, &override, |
4044 | &type_change_ok, &size_change_ok)) | |
4045 | goto error_free_vers; | |
4046 | ||
4047 | if (skip) | |
4048 | continue; | |
4049 | ||
4050 | if (override) | |
4051 | definition = FALSE; | |
4052 | ||
4053 | h = *sym_hash; | |
4054 | while (h->root.type == bfd_link_hash_indirect | |
4055 | || h->root.type == bfd_link_hash_warning) | |
4056 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4057 | ||
4058 | /* Remember the old alignment if this is a common symbol, so | |
4059 | that we don't reduce the alignment later on. We can't | |
4060 | check later, because _bfd_generic_link_add_one_symbol | |
4061 | will set a default for the alignment which we want to | |
4062 | override. We also remember the old bfd where the existing | |
4063 | definition comes from. */ | |
4064 | switch (h->root.type) | |
4065 | { | |
4066 | default: | |
4067 | break; | |
4068 | ||
4069 | case bfd_link_hash_defined: | |
4070 | case bfd_link_hash_defweak: | |
4071 | old_bfd = h->root.u.def.section->owner; | |
4072 | break; | |
4073 | ||
4074 | case bfd_link_hash_common: | |
4075 | old_bfd = h->root.u.c.p->section->owner; | |
4076 | old_alignment = h->root.u.c.p->alignment_power; | |
4077 | break; | |
4078 | } | |
4079 | ||
4080 | if (elf_tdata (abfd)->verdef != NULL | |
4081 | && ! override | |
4082 | && vernum > 1 | |
4083 | && definition) | |
4084 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4085 | } | |
4086 | ||
4087 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4088 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4089 | (struct bfd_link_hash_entry **) sym_hash))) |
4090 | goto error_free_vers; | |
4091 | ||
4092 | h = *sym_hash; | |
4093 | while (h->root.type == bfd_link_hash_indirect | |
4094 | || h->root.type == bfd_link_hash_warning) | |
4095 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4096 | *sym_hash = h; | |
4097 | ||
4098 | new_weakdef = FALSE; | |
4099 | if (dynamic | |
4100 | && definition | |
4101 | && (flags & BSF_WEAK) != 0 | |
fcb93ecf | 4102 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4103 | && is_elf_hash_table (htab) |
f6e332e6 | 4104 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
4105 | { |
4106 | /* Keep a list of all weak defined non function symbols from | |
4107 | a dynamic object, using the weakdef field. Later in this | |
4108 | function we will set the weakdef field to the correct | |
4109 | value. We only put non-function symbols from dynamic | |
4110 | objects on this list, because that happens to be the only | |
4111 | time we need to know the normal symbol corresponding to a | |
4112 | weak symbol, and the information is time consuming to | |
4113 | figure out. If the weakdef field is not already NULL, | |
4114 | then this symbol was already defined by some previous | |
4115 | dynamic object, and we will be using that previous | |
4116 | definition anyhow. */ | |
4117 | ||
f6e332e6 | 4118 | h->u.weakdef = weaks; |
4ad4eba5 AM |
4119 | weaks = h; |
4120 | new_weakdef = TRUE; | |
4121 | } | |
4122 | ||
4123 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4124 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4125 | && h->root.type == bfd_link_hash_common) |
4126 | { | |
4127 | unsigned int align; | |
4128 | ||
a4d8e49b | 4129 | if (common) |
af44c138 L |
4130 | align = bfd_log2 (isym->st_value); |
4131 | else | |
4132 | { | |
4133 | /* The new symbol is a common symbol in a shared object. | |
4134 | We need to get the alignment from the section. */ | |
4135 | align = new_sec->alignment_power; | |
4136 | } | |
4ad4eba5 AM |
4137 | if (align > old_alignment |
4138 | /* Permit an alignment power of zero if an alignment of one | |
4139 | is specified and no other alignments have been specified. */ | |
4140 | || (isym->st_value == 1 && old_alignment == 0)) | |
4141 | h->root.u.c.p->alignment_power = align; | |
4142 | else | |
4143 | h->root.u.c.p->alignment_power = old_alignment; | |
4144 | } | |
4145 | ||
66eb6687 | 4146 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4147 | { |
4ad4eba5 | 4148 | bfd_boolean dynsym; |
4ad4eba5 AM |
4149 | |
4150 | /* Check the alignment when a common symbol is involved. This | |
4151 | can change when a common symbol is overridden by a normal | |
4152 | definition or a common symbol is ignored due to the old | |
4153 | normal definition. We need to make sure the maximum | |
4154 | alignment is maintained. */ | |
a4d8e49b | 4155 | if ((old_alignment || common) |
4ad4eba5 AM |
4156 | && h->root.type != bfd_link_hash_common) |
4157 | { | |
4158 | unsigned int common_align; | |
4159 | unsigned int normal_align; | |
4160 | unsigned int symbol_align; | |
4161 | bfd *normal_bfd; | |
4162 | bfd *common_bfd; | |
4163 | ||
4164 | symbol_align = ffs (h->root.u.def.value) - 1; | |
4165 | if (h->root.u.def.section->owner != NULL | |
4166 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
4167 | { | |
4168 | normal_align = h->root.u.def.section->alignment_power; | |
4169 | if (normal_align > symbol_align) | |
4170 | normal_align = symbol_align; | |
4171 | } | |
4172 | else | |
4173 | normal_align = symbol_align; | |
4174 | ||
4175 | if (old_alignment) | |
4176 | { | |
4177 | common_align = old_alignment; | |
4178 | common_bfd = old_bfd; | |
4179 | normal_bfd = abfd; | |
4180 | } | |
4181 | else | |
4182 | { | |
4183 | common_align = bfd_log2 (isym->st_value); | |
4184 | common_bfd = abfd; | |
4185 | normal_bfd = old_bfd; | |
4186 | } | |
4187 | ||
4188 | if (normal_align < common_align) | |
d07676f8 NC |
4189 | { |
4190 | /* PR binutils/2735 */ | |
4191 | if (normal_bfd == NULL) | |
4192 | (*_bfd_error_handler) | |
4193 | (_("Warning: alignment %u of common symbol `%s' in %B" | |
4194 | " is greater than the alignment (%u) of its section %A"), | |
4195 | common_bfd, h->root.u.def.section, | |
4196 | 1 << common_align, name, 1 << normal_align); | |
4197 | else | |
4198 | (*_bfd_error_handler) | |
4199 | (_("Warning: alignment %u of symbol `%s' in %B" | |
4200 | " is smaller than %u in %B"), | |
4201 | normal_bfd, common_bfd, | |
4202 | 1 << normal_align, name, 1 << common_align); | |
4203 | } | |
4ad4eba5 AM |
4204 | } |
4205 | ||
83ad0046 L |
4206 | /* Remember the symbol size if it isn't undefined. */ |
4207 | if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 AM |
4208 | && (definition || h->size == 0)) |
4209 | { | |
83ad0046 L |
4210 | if (h->size != 0 |
4211 | && h->size != isym->st_size | |
4212 | && ! size_change_ok) | |
4ad4eba5 | 4213 | (*_bfd_error_handler) |
d003868e AM |
4214 | (_("Warning: size of symbol `%s' changed" |
4215 | " from %lu in %B to %lu in %B"), | |
4216 | old_bfd, abfd, | |
4ad4eba5 | 4217 | name, (unsigned long) h->size, |
d003868e | 4218 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
4219 | |
4220 | h->size = isym->st_size; | |
4221 | } | |
4222 | ||
4223 | /* If this is a common symbol, then we always want H->SIZE | |
4224 | to be the size of the common symbol. The code just above | |
4225 | won't fix the size if a common symbol becomes larger. We | |
4226 | don't warn about a size change here, because that is | |
fcb93ecf PB |
4227 | covered by --warn-common. Allow changed between different |
4228 | function types. */ | |
4ad4eba5 AM |
4229 | if (h->root.type == bfd_link_hash_common) |
4230 | h->size = h->root.u.c.size; | |
4231 | ||
4232 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
4233 | && (definition || h->type == STT_NOTYPE)) | |
4234 | { | |
4235 | if (h->type != STT_NOTYPE | |
4236 | && h->type != ELF_ST_TYPE (isym->st_info) | |
4237 | && ! type_change_ok) | |
4238 | (*_bfd_error_handler) | |
d003868e AM |
4239 | (_("Warning: type of symbol `%s' changed" |
4240 | " from %d to %d in %B"), | |
4241 | abfd, name, h->type, ELF_ST_TYPE (isym->st_info)); | |
4ad4eba5 AM |
4242 | |
4243 | h->type = ELF_ST_TYPE (isym->st_info); | |
4244 | } | |
4245 | ||
4246 | /* If st_other has a processor-specific meaning, specific | |
4247 | code might be needed here. We never merge the visibility | |
4248 | attribute with the one from a dynamic object. */ | |
4249 | if (bed->elf_backend_merge_symbol_attribute) | |
4250 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
4251 | dynamic); | |
4252 | ||
b58f81ae DJ |
4253 | /* If this symbol has default visibility and the user has requested |
4254 | we not re-export it, then mark it as hidden. */ | |
4255 | if (definition && !dynamic | |
4256 | && (abfd->no_export | |
4257 | || (abfd->my_archive && abfd->my_archive->no_export)) | |
4258 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) | |
66eb6687 AM |
4259 | isym->st_other = (STV_HIDDEN |
4260 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
b58f81ae | 4261 | |
8992f0d7 | 4262 | if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic) |
4ad4eba5 AM |
4263 | { |
4264 | unsigned char hvis, symvis, other, nvis; | |
4265 | ||
8992f0d7 TS |
4266 | /* Only merge the visibility. Leave the remainder of the |
4267 | st_other field to elf_backend_merge_symbol_attribute. */ | |
4268 | other = h->other & ~ELF_ST_VISIBILITY (-1); | |
4ad4eba5 AM |
4269 | |
4270 | /* Combine visibilities, using the most constraining one. */ | |
4271 | hvis = ELF_ST_VISIBILITY (h->other); | |
4272 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
4273 | if (! hvis) | |
4274 | nvis = symvis; | |
4275 | else if (! symvis) | |
4276 | nvis = hvis; | |
4277 | else | |
4278 | nvis = hvis < symvis ? hvis : symvis; | |
4279 | ||
4280 | h->other = other | nvis; | |
4281 | } | |
4282 | ||
4283 | /* Set a flag in the hash table entry indicating the type of | |
4284 | reference or definition we just found. Keep a count of | |
4285 | the number of dynamic symbols we find. A dynamic symbol | |
4286 | is one which is referenced or defined by both a regular | |
4287 | object and a shared object. */ | |
4ad4eba5 AM |
4288 | dynsym = FALSE; |
4289 | if (! dynamic) | |
4290 | { | |
4291 | if (! definition) | |
4292 | { | |
f5385ebf | 4293 | h->ref_regular = 1; |
4ad4eba5 | 4294 | if (bind != STB_WEAK) |
f5385ebf | 4295 | h->ref_regular_nonweak = 1; |
4ad4eba5 AM |
4296 | } |
4297 | else | |
f5385ebf | 4298 | h->def_regular = 1; |
4ad4eba5 | 4299 | if (! info->executable |
f5385ebf AM |
4300 | || h->def_dynamic |
4301 | || h->ref_dynamic) | |
4ad4eba5 AM |
4302 | dynsym = TRUE; |
4303 | } | |
4304 | else | |
4305 | { | |
4306 | if (! definition) | |
f5385ebf | 4307 | h->ref_dynamic = 1; |
4ad4eba5 | 4308 | else |
f5385ebf AM |
4309 | h->def_dynamic = 1; |
4310 | if (h->def_regular | |
4311 | || h->ref_regular | |
f6e332e6 | 4312 | || (h->u.weakdef != NULL |
4ad4eba5 | 4313 | && ! new_weakdef |
f6e332e6 | 4314 | && h->u.weakdef->dynindx != -1)) |
4ad4eba5 AM |
4315 | dynsym = TRUE; |
4316 | } | |
4317 | ||
b2064611 | 4318 | if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable) |
92b7c7b6 L |
4319 | { |
4320 | /* We don't want to make debug symbol dynamic. */ | |
4321 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4322 | dynsym = FALSE; | |
4323 | } | |
4324 | ||
4ad4eba5 AM |
4325 | /* Check to see if we need to add an indirect symbol for |
4326 | the default name. */ | |
4327 | if (definition || h->root.type == bfd_link_hash_common) | |
4328 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4329 | &sec, &value, &dynsym, | |
4330 | override)) | |
4331 | goto error_free_vers; | |
4332 | ||
4333 | if (definition && !dynamic) | |
4334 | { | |
4335 | char *p = strchr (name, ELF_VER_CHR); | |
4336 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4337 | { | |
4338 | /* Queue non-default versions so that .symver x, x@FOO | |
4339 | aliases can be checked. */ | |
66eb6687 | 4340 | if (!nondeflt_vers) |
4ad4eba5 | 4341 | { |
66eb6687 AM |
4342 | amt = ((isymend - isym + 1) |
4343 | * sizeof (struct elf_link_hash_entry *)); | |
4ad4eba5 | 4344 | nondeflt_vers = bfd_malloc (amt); |
14b1c01e AM |
4345 | if (!nondeflt_vers) |
4346 | goto error_free_vers; | |
4ad4eba5 | 4347 | } |
66eb6687 | 4348 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4349 | } |
4350 | } | |
4351 | ||
4352 | if (dynsym && h->dynindx == -1) | |
4353 | { | |
c152c796 | 4354 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4355 | goto error_free_vers; |
f6e332e6 | 4356 | if (h->u.weakdef != NULL |
4ad4eba5 | 4357 | && ! new_weakdef |
f6e332e6 | 4358 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 4359 | { |
66eb6687 | 4360 | if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
4361 | goto error_free_vers; |
4362 | } | |
4363 | } | |
4364 | else if (dynsym && h->dynindx != -1) | |
4365 | /* If the symbol already has a dynamic index, but | |
4366 | visibility says it should not be visible, turn it into | |
4367 | a local symbol. */ | |
4368 | switch (ELF_ST_VISIBILITY (h->other)) | |
4369 | { | |
4370 | case STV_INTERNAL: | |
4371 | case STV_HIDDEN: | |
4372 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4373 | dynsym = FALSE; | |
4374 | break; | |
4375 | } | |
4376 | ||
4377 | if (!add_needed | |
4378 | && definition | |
4379 | && dynsym | |
f5385ebf | 4380 | && h->ref_regular) |
4ad4eba5 AM |
4381 | { |
4382 | int ret; | |
4383 | const char *soname = elf_dt_name (abfd); | |
4384 | ||
4385 | /* A symbol from a library loaded via DT_NEEDED of some | |
4386 | other library is referenced by a regular object. | |
e56f61be L |
4387 | Add a DT_NEEDED entry for it. Issue an error if |
4388 | --no-add-needed is used. */ | |
4389 | if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) | |
4390 | { | |
4391 | (*_bfd_error_handler) | |
4392 | (_("%s: invalid DSO for symbol `%s' definition"), | |
d003868e | 4393 | abfd, name); |
e56f61be L |
4394 | bfd_set_error (bfd_error_bad_value); |
4395 | goto error_free_vers; | |
4396 | } | |
4397 | ||
a5db907e AM |
4398 | elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED; |
4399 | ||
4ad4eba5 | 4400 | add_needed = TRUE; |
7e9f0867 | 4401 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4402 | if (ret < 0) |
4403 | goto error_free_vers; | |
4404 | ||
4405 | BFD_ASSERT (ret == 0); | |
4406 | } | |
4407 | } | |
4408 | } | |
4409 | ||
66eb6687 AM |
4410 | if (extversym != NULL) |
4411 | { | |
4412 | free (extversym); | |
4413 | extversym = NULL; | |
4414 | } | |
4415 | ||
4416 | if (isymbuf != NULL) | |
4417 | { | |
4418 | free (isymbuf); | |
4419 | isymbuf = NULL; | |
4420 | } | |
4421 | ||
4422 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4423 | { | |
4424 | unsigned int i; | |
4425 | ||
4426 | /* Restore the symbol table. */ | |
97fed1c9 JJ |
4427 | if (bed->as_needed_cleanup) |
4428 | (*bed->as_needed_cleanup) (abfd, info); | |
66eb6687 AM |
4429 | old_hash = (char *) old_tab + tabsize; |
4430 | old_ent = (char *) old_hash + hashsize; | |
4431 | sym_hash = elf_sym_hashes (abfd); | |
4f87808c AM |
4432 | htab->root.table.table = old_table; |
4433 | htab->root.table.size = old_size; | |
4434 | htab->root.table.count = old_count; | |
66eb6687 AM |
4435 | memcpy (htab->root.table.table, old_tab, tabsize); |
4436 | memcpy (sym_hash, old_hash, hashsize); | |
4437 | htab->root.undefs = old_undefs; | |
4438 | htab->root.undefs_tail = old_undefs_tail; | |
4439 | for (i = 0; i < htab->root.table.size; i++) | |
4440 | { | |
4441 | struct bfd_hash_entry *p; | |
4442 | struct elf_link_hash_entry *h; | |
4443 | ||
4444 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4445 | { | |
4446 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
4447 | if (h->root.type == bfd_link_hash_warning) |
4448 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
66eb6687 AM |
4449 | if (h->dynindx >= old_dynsymcount) |
4450 | _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index); | |
2de92251 | 4451 | |
66eb6687 AM |
4452 | memcpy (p, old_ent, htab->root.table.entsize); |
4453 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4454 | h = (struct elf_link_hash_entry *) p; |
4455 | if (h->root.type == bfd_link_hash_warning) | |
4456 | { | |
4457 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
4458 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4459 | } | |
66eb6687 AM |
4460 | } |
4461 | } | |
4462 | ||
5061a885 AM |
4463 | /* Make a special call to the linker "notice" function to |
4464 | tell it that symbols added for crefs may need to be removed. */ | |
4465 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, | |
4466 | notice_not_needed)) | |
9af2a943 | 4467 | goto error_free_vers; |
5061a885 | 4468 | |
66eb6687 AM |
4469 | free (old_tab); |
4470 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
4471 | alloc_mark); | |
4472 | if (nondeflt_vers != NULL) | |
4473 | free (nondeflt_vers); | |
4474 | return TRUE; | |
4475 | } | |
2de92251 | 4476 | |
66eb6687 AM |
4477 | if (old_tab != NULL) |
4478 | { | |
5061a885 AM |
4479 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, |
4480 | notice_needed)) | |
9af2a943 | 4481 | goto error_free_vers; |
66eb6687 AM |
4482 | free (old_tab); |
4483 | old_tab = NULL; | |
4484 | } | |
4485 | ||
4ad4eba5 AM |
4486 | /* Now that all the symbols from this input file are created, handle |
4487 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
4488 | if (nondeflt_vers != NULL) | |
4489 | { | |
4490 | bfd_size_type cnt, symidx; | |
4491 | ||
4492 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
4493 | { | |
4494 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
4495 | char *shortname, *p; | |
4496 | ||
4497 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
4498 | if (p == NULL | |
4499 | || (h->root.type != bfd_link_hash_defined | |
4500 | && h->root.type != bfd_link_hash_defweak)) | |
4501 | continue; | |
4502 | ||
4503 | amt = p - h->root.root.string; | |
4504 | shortname = bfd_malloc (amt + 1); | |
14b1c01e AM |
4505 | if (!shortname) |
4506 | goto error_free_vers; | |
4ad4eba5 AM |
4507 | memcpy (shortname, h->root.root.string, amt); |
4508 | shortname[amt] = '\0'; | |
4509 | ||
4510 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 4511 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
4512 | FALSE, FALSE, FALSE); |
4513 | if (hi != NULL | |
4514 | && hi->root.type == h->root.type | |
4515 | && hi->root.u.def.value == h->root.u.def.value | |
4516 | && hi->root.u.def.section == h->root.u.def.section) | |
4517 | { | |
4518 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4519 | hi->root.type = bfd_link_hash_indirect; | |
4520 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 4521 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
4522 | sym_hash = elf_sym_hashes (abfd); |
4523 | if (sym_hash) | |
4524 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4525 | if (sym_hash[symidx] == hi) | |
4526 | { | |
4527 | sym_hash[symidx] = h; | |
4528 | break; | |
4529 | } | |
4530 | } | |
4531 | free (shortname); | |
4532 | } | |
4533 | free (nondeflt_vers); | |
4534 | nondeflt_vers = NULL; | |
4535 | } | |
4536 | ||
4ad4eba5 AM |
4537 | /* Now set the weakdefs field correctly for all the weak defined |
4538 | symbols we found. The only way to do this is to search all the | |
4539 | symbols. Since we only need the information for non functions in | |
4540 | dynamic objects, that's the only time we actually put anything on | |
4541 | the list WEAKS. We need this information so that if a regular | |
4542 | object refers to a symbol defined weakly in a dynamic object, the | |
4543 | real symbol in the dynamic object is also put in the dynamic | |
4544 | symbols; we also must arrange for both symbols to point to the | |
4545 | same memory location. We could handle the general case of symbol | |
4546 | aliasing, but a general symbol alias can only be generated in | |
4547 | assembler code, handling it correctly would be very time | |
4548 | consuming, and other ELF linkers don't handle general aliasing | |
4549 | either. */ | |
4550 | if (weaks != NULL) | |
4551 | { | |
4552 | struct elf_link_hash_entry **hpp; | |
4553 | struct elf_link_hash_entry **hppend; | |
4554 | struct elf_link_hash_entry **sorted_sym_hash; | |
4555 | struct elf_link_hash_entry *h; | |
4556 | size_t sym_count; | |
4557 | ||
4558 | /* Since we have to search the whole symbol list for each weak | |
4559 | defined symbol, search time for N weak defined symbols will be | |
4560 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
4561 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
4562 | sorted_sym_hash = bfd_malloc (amt); | |
4563 | if (sorted_sym_hash == NULL) | |
4564 | goto error_return; | |
4565 | sym_hash = sorted_sym_hash; | |
4566 | hpp = elf_sym_hashes (abfd); | |
4567 | hppend = hpp + extsymcount; | |
4568 | sym_count = 0; | |
4569 | for (; hpp < hppend; hpp++) | |
4570 | { | |
4571 | h = *hpp; | |
4572 | if (h != NULL | |
4573 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 4574 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
4575 | { |
4576 | *sym_hash = h; | |
4577 | sym_hash++; | |
4578 | sym_count++; | |
4579 | } | |
4580 | } | |
4581 | ||
4582 | qsort (sorted_sym_hash, sym_count, | |
4583 | sizeof (struct elf_link_hash_entry *), | |
4584 | elf_sort_symbol); | |
4585 | ||
4586 | while (weaks != NULL) | |
4587 | { | |
4588 | struct elf_link_hash_entry *hlook; | |
4589 | asection *slook; | |
4590 | bfd_vma vlook; | |
4591 | long ilook; | |
4592 | size_t i, j, idx; | |
4593 | ||
4594 | hlook = weaks; | |
f6e332e6 AM |
4595 | weaks = hlook->u.weakdef; |
4596 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
4597 | |
4598 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
4599 | || hlook->root.type == bfd_link_hash_defweak | |
4600 | || hlook->root.type == bfd_link_hash_common | |
4601 | || hlook->root.type == bfd_link_hash_indirect); | |
4602 | slook = hlook->root.u.def.section; | |
4603 | vlook = hlook->root.u.def.value; | |
4604 | ||
4605 | ilook = -1; | |
4606 | i = 0; | |
4607 | j = sym_count; | |
4608 | while (i < j) | |
4609 | { | |
4610 | bfd_signed_vma vdiff; | |
4611 | idx = (i + j) / 2; | |
4612 | h = sorted_sym_hash [idx]; | |
4613 | vdiff = vlook - h->root.u.def.value; | |
4614 | if (vdiff < 0) | |
4615 | j = idx; | |
4616 | else if (vdiff > 0) | |
4617 | i = idx + 1; | |
4618 | else | |
4619 | { | |
a9b881be | 4620 | long sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
4621 | if (sdiff < 0) |
4622 | j = idx; | |
4623 | else if (sdiff > 0) | |
4624 | i = idx + 1; | |
4625 | else | |
4626 | { | |
4627 | ilook = idx; | |
4628 | break; | |
4629 | } | |
4630 | } | |
4631 | } | |
4632 | ||
4633 | /* We didn't find a value/section match. */ | |
4634 | if (ilook == -1) | |
4635 | continue; | |
4636 | ||
4637 | for (i = ilook; i < sym_count; i++) | |
4638 | { | |
4639 | h = sorted_sym_hash [i]; | |
4640 | ||
4641 | /* Stop if value or section doesn't match. */ | |
4642 | if (h->root.u.def.value != vlook | |
4643 | || h->root.u.def.section != slook) | |
4644 | break; | |
4645 | else if (h != hlook) | |
4646 | { | |
f6e332e6 | 4647 | hlook->u.weakdef = h; |
4ad4eba5 AM |
4648 | |
4649 | /* If the weak definition is in the list of dynamic | |
4650 | symbols, make sure the real definition is put | |
4651 | there as well. */ | |
4652 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
4653 | { | |
c152c796 | 4654 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4dd07732 AM |
4655 | { |
4656 | err_free_sym_hash: | |
4657 | free (sorted_sym_hash); | |
4658 | goto error_return; | |
4659 | } | |
4ad4eba5 AM |
4660 | } |
4661 | ||
4662 | /* If the real definition is in the list of dynamic | |
4663 | symbols, make sure the weak definition is put | |
4664 | there as well. If we don't do this, then the | |
4665 | dynamic loader might not merge the entries for the | |
4666 | real definition and the weak definition. */ | |
4667 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
4668 | { | |
c152c796 | 4669 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4dd07732 | 4670 | goto err_free_sym_hash; |
4ad4eba5 AM |
4671 | } |
4672 | break; | |
4673 | } | |
4674 | } | |
4675 | } | |
4676 | ||
4677 | free (sorted_sym_hash); | |
4678 | } | |
4679 | ||
33177bb1 AM |
4680 | if (bed->check_directives |
4681 | && !(*bed->check_directives) (abfd, info)) | |
4682 | return FALSE; | |
85fbca6a | 4683 | |
4ad4eba5 AM |
4684 | /* If this object is the same format as the output object, and it is |
4685 | not a shared library, then let the backend look through the | |
4686 | relocs. | |
4687 | ||
4688 | This is required to build global offset table entries and to | |
4689 | arrange for dynamic relocs. It is not required for the | |
4690 | particular common case of linking non PIC code, even when linking | |
4691 | against shared libraries, but unfortunately there is no way of | |
4692 | knowing whether an object file has been compiled PIC or not. | |
4693 | Looking through the relocs is not particularly time consuming. | |
4694 | The problem is that we must either (1) keep the relocs in memory, | |
4695 | which causes the linker to require additional runtime memory or | |
4696 | (2) read the relocs twice from the input file, which wastes time. | |
4697 | This would be a good case for using mmap. | |
4698 | ||
4699 | I have no idea how to handle linking PIC code into a file of a | |
4700 | different format. It probably can't be done. */ | |
4ad4eba5 | 4701 | if (! dynamic |
66eb6687 | 4702 | && is_elf_hash_table (htab) |
13285a1b | 4703 | && bed->check_relocs != NULL |
f13a99db | 4704 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) |
4ad4eba5 AM |
4705 | { |
4706 | asection *o; | |
4707 | ||
4708 | for (o = abfd->sections; o != NULL; o = o->next) | |
4709 | { | |
4710 | Elf_Internal_Rela *internal_relocs; | |
4711 | bfd_boolean ok; | |
4712 | ||
4713 | if ((o->flags & SEC_RELOC) == 0 | |
4714 | || o->reloc_count == 0 | |
4715 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4716 | && (o->flags & SEC_DEBUGGING) != 0) | |
4717 | || bfd_is_abs_section (o->output_section)) | |
4718 | continue; | |
4719 | ||
4720 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4721 | info->keep_memory); | |
4722 | if (internal_relocs == NULL) | |
4723 | goto error_return; | |
4724 | ||
66eb6687 | 4725 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); |
4ad4eba5 AM |
4726 | |
4727 | if (elf_section_data (o)->relocs != internal_relocs) | |
4728 | free (internal_relocs); | |
4729 | ||
4730 | if (! ok) | |
4731 | goto error_return; | |
4732 | } | |
4733 | } | |
4734 | ||
4735 | /* If this is a non-traditional link, try to optimize the handling | |
4736 | of the .stab/.stabstr sections. */ | |
4737 | if (! dynamic | |
4738 | && ! info->traditional_format | |
66eb6687 | 4739 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
4740 | && (info->strip != strip_all && info->strip != strip_debugger)) |
4741 | { | |
4742 | asection *stabstr; | |
4743 | ||
4744 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4745 | if (stabstr != NULL) | |
4746 | { | |
4747 | bfd_size_type string_offset = 0; | |
4748 | asection *stab; | |
4749 | ||
4750 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 4751 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
4752 | && (!stab->name[5] || |
4753 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4754 | && (stab->flags & SEC_MERGE) == 0 | |
4755 | && !bfd_is_abs_section (stab->output_section)) | |
4756 | { | |
4757 | struct bfd_elf_section_data *secdata; | |
4758 | ||
4759 | secdata = elf_section_data (stab); | |
66eb6687 AM |
4760 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
4761 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
4762 | &string_offset)) |
4763 | goto error_return; | |
4764 | if (secdata->sec_info) | |
4765 | stab->sec_info_type = ELF_INFO_TYPE_STABS; | |
4766 | } | |
4767 | } | |
4768 | } | |
4769 | ||
66eb6687 | 4770 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
4771 | { |
4772 | /* Add this bfd to the loaded list. */ | |
4773 | struct elf_link_loaded_list *n; | |
4774 | ||
4775 | n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); | |
4776 | if (n == NULL) | |
4777 | goto error_return; | |
4778 | n->abfd = abfd; | |
66eb6687 AM |
4779 | n->next = htab->loaded; |
4780 | htab->loaded = n; | |
4ad4eba5 AM |
4781 | } |
4782 | ||
4783 | return TRUE; | |
4784 | ||
4785 | error_free_vers: | |
66eb6687 AM |
4786 | if (old_tab != NULL) |
4787 | free (old_tab); | |
4ad4eba5 AM |
4788 | if (nondeflt_vers != NULL) |
4789 | free (nondeflt_vers); | |
4790 | if (extversym != NULL) | |
4791 | free (extversym); | |
4792 | error_free_sym: | |
4793 | if (isymbuf != NULL) | |
4794 | free (isymbuf); | |
4795 | error_return: | |
4796 | return FALSE; | |
4797 | } | |
4798 | ||
8387904d AM |
4799 | /* Return the linker hash table entry of a symbol that might be |
4800 | satisfied by an archive symbol. Return -1 on error. */ | |
4801 | ||
4802 | struct elf_link_hash_entry * | |
4803 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
4804 | struct bfd_link_info *info, | |
4805 | const char *name) | |
4806 | { | |
4807 | struct elf_link_hash_entry *h; | |
4808 | char *p, *copy; | |
4809 | size_t len, first; | |
4810 | ||
4811 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
4812 | if (h != NULL) | |
4813 | return h; | |
4814 | ||
4815 | /* If this is a default version (the name contains @@), look up the | |
4816 | symbol again with only one `@' as well as without the version. | |
4817 | The effect is that references to the symbol with and without the | |
4818 | version will be matched by the default symbol in the archive. */ | |
4819 | ||
4820 | p = strchr (name, ELF_VER_CHR); | |
4821 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4822 | return h; | |
4823 | ||
4824 | /* First check with only one `@'. */ | |
4825 | len = strlen (name); | |
4826 | copy = bfd_alloc (abfd, len); | |
4827 | if (copy == NULL) | |
4828 | return (struct elf_link_hash_entry *) 0 - 1; | |
4829 | ||
4830 | first = p - name + 1; | |
4831 | memcpy (copy, name, first); | |
4832 | memcpy (copy + first, name + first + 1, len - first); | |
4833 | ||
4834 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE); | |
4835 | if (h == NULL) | |
4836 | { | |
4837 | /* We also need to check references to the symbol without the | |
4838 | version. */ | |
4839 | copy[first - 1] = '\0'; | |
4840 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
4841 | FALSE, FALSE, FALSE); | |
4842 | } | |
4843 | ||
4844 | bfd_release (abfd, copy); | |
4845 | return h; | |
4846 | } | |
4847 | ||
0ad989f9 L |
4848 | /* Add symbols from an ELF archive file to the linker hash table. We |
4849 | don't use _bfd_generic_link_add_archive_symbols because of a | |
4850 | problem which arises on UnixWare. The UnixWare libc.so is an | |
4851 | archive which includes an entry libc.so.1 which defines a bunch of | |
4852 | symbols. The libc.so archive also includes a number of other | |
4853 | object files, which also define symbols, some of which are the same | |
4854 | as those defined in libc.so.1. Correct linking requires that we | |
4855 | consider each object file in turn, and include it if it defines any | |
4856 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
4857 | this; it looks through the list of undefined symbols, and includes | |
4858 | any object file which defines them. When this algorithm is used on | |
4859 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
4860 | bunch of symbols. This means that some of the other objects in the | |
4861 | archive are not included in the link, which is incorrect since they | |
4862 | precede libc.so.1 in the archive. | |
4863 | ||
4864 | Fortunately, ELF archive handling is simpler than that done by | |
4865 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4866 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4867 | symbol is currently undefined, we know that we must pull in that | |
4868 | object file. | |
4869 | ||
4870 | Unfortunately, we do have to make multiple passes over the symbol | |
4871 | table until nothing further is resolved. */ | |
4872 | ||
4ad4eba5 AM |
4873 | static bfd_boolean |
4874 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
4875 | { |
4876 | symindex c; | |
4877 | bfd_boolean *defined = NULL; | |
4878 | bfd_boolean *included = NULL; | |
4879 | carsym *symdefs; | |
4880 | bfd_boolean loop; | |
4881 | bfd_size_type amt; | |
8387904d AM |
4882 | const struct elf_backend_data *bed; |
4883 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
4884 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
4885 | |
4886 | if (! bfd_has_map (abfd)) | |
4887 | { | |
4888 | /* An empty archive is a special case. */ | |
4889 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
4890 | return TRUE; | |
4891 | bfd_set_error (bfd_error_no_armap); | |
4892 | return FALSE; | |
4893 | } | |
4894 | ||
4895 | /* Keep track of all symbols we know to be already defined, and all | |
4896 | files we know to be already included. This is to speed up the | |
4897 | second and subsequent passes. */ | |
4898 | c = bfd_ardata (abfd)->symdef_count; | |
4899 | if (c == 0) | |
4900 | return TRUE; | |
4901 | amt = c; | |
4902 | amt *= sizeof (bfd_boolean); | |
4903 | defined = bfd_zmalloc (amt); | |
4904 | included = bfd_zmalloc (amt); | |
4905 | if (defined == NULL || included == NULL) | |
4906 | goto error_return; | |
4907 | ||
4908 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
4909 | bed = get_elf_backend_data (abfd); |
4910 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
4911 | |
4912 | do | |
4913 | { | |
4914 | file_ptr last; | |
4915 | symindex i; | |
4916 | carsym *symdef; | |
4917 | carsym *symdefend; | |
4918 | ||
4919 | loop = FALSE; | |
4920 | last = -1; | |
4921 | ||
4922 | symdef = symdefs; | |
4923 | symdefend = symdef + c; | |
4924 | for (i = 0; symdef < symdefend; symdef++, i++) | |
4925 | { | |
4926 | struct elf_link_hash_entry *h; | |
4927 | bfd *element; | |
4928 | struct bfd_link_hash_entry *undefs_tail; | |
4929 | symindex mark; | |
4930 | ||
4931 | if (defined[i] || included[i]) | |
4932 | continue; | |
4933 | if (symdef->file_offset == last) | |
4934 | { | |
4935 | included[i] = TRUE; | |
4936 | continue; | |
4937 | } | |
4938 | ||
8387904d AM |
4939 | h = archive_symbol_lookup (abfd, info, symdef->name); |
4940 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
4941 | goto error_return; | |
0ad989f9 L |
4942 | |
4943 | if (h == NULL) | |
4944 | continue; | |
4945 | ||
4946 | if (h->root.type == bfd_link_hash_common) | |
4947 | { | |
4948 | /* We currently have a common symbol. The archive map contains | |
4949 | a reference to this symbol, so we may want to include it. We | |
4950 | only want to include it however, if this archive element | |
4951 | contains a definition of the symbol, not just another common | |
4952 | declaration of it. | |
4953 | ||
4954 | Unfortunately some archivers (including GNU ar) will put | |
4955 | declarations of common symbols into their archive maps, as | |
4956 | well as real definitions, so we cannot just go by the archive | |
4957 | map alone. Instead we must read in the element's symbol | |
4958 | table and check that to see what kind of symbol definition | |
4959 | this is. */ | |
4960 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
4961 | continue; | |
4962 | } | |
4963 | else if (h->root.type != bfd_link_hash_undefined) | |
4964 | { | |
4965 | if (h->root.type != bfd_link_hash_undefweak) | |
4966 | defined[i] = TRUE; | |
4967 | continue; | |
4968 | } | |
4969 | ||
4970 | /* We need to include this archive member. */ | |
4971 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
4972 | if (element == NULL) | |
4973 | goto error_return; | |
4974 | ||
4975 | if (! bfd_check_format (element, bfd_object)) | |
4976 | goto error_return; | |
4977 | ||
4978 | /* Doublecheck that we have not included this object | |
4979 | already--it should be impossible, but there may be | |
4980 | something wrong with the archive. */ | |
4981 | if (element->archive_pass != 0) | |
4982 | { | |
4983 | bfd_set_error (bfd_error_bad_value); | |
4984 | goto error_return; | |
4985 | } | |
4986 | element->archive_pass = 1; | |
4987 | ||
4988 | undefs_tail = info->hash->undefs_tail; | |
4989 | ||
4990 | if (! (*info->callbacks->add_archive_element) (info, element, | |
4991 | symdef->name)) | |
4992 | goto error_return; | |
4993 | if (! bfd_link_add_symbols (element, info)) | |
4994 | goto error_return; | |
4995 | ||
4996 | /* If there are any new undefined symbols, we need to make | |
4997 | another pass through the archive in order to see whether | |
4998 | they can be defined. FIXME: This isn't perfect, because | |
4999 | common symbols wind up on undefs_tail and because an | |
5000 | undefined symbol which is defined later on in this pass | |
5001 | does not require another pass. This isn't a bug, but it | |
5002 | does make the code less efficient than it could be. */ | |
5003 | if (undefs_tail != info->hash->undefs_tail) | |
5004 | loop = TRUE; | |
5005 | ||
5006 | /* Look backward to mark all symbols from this object file | |
5007 | which we have already seen in this pass. */ | |
5008 | mark = i; | |
5009 | do | |
5010 | { | |
5011 | included[mark] = TRUE; | |
5012 | if (mark == 0) | |
5013 | break; | |
5014 | --mark; | |
5015 | } | |
5016 | while (symdefs[mark].file_offset == symdef->file_offset); | |
5017 | ||
5018 | /* We mark subsequent symbols from this object file as we go | |
5019 | on through the loop. */ | |
5020 | last = symdef->file_offset; | |
5021 | } | |
5022 | } | |
5023 | while (loop); | |
5024 | ||
5025 | free (defined); | |
5026 | free (included); | |
5027 | ||
5028 | return TRUE; | |
5029 | ||
5030 | error_return: | |
5031 | if (defined != NULL) | |
5032 | free (defined); | |
5033 | if (included != NULL) | |
5034 | free (included); | |
5035 | return FALSE; | |
5036 | } | |
4ad4eba5 AM |
5037 | |
5038 | /* Given an ELF BFD, add symbols to the global hash table as | |
5039 | appropriate. */ | |
5040 | ||
5041 | bfd_boolean | |
5042 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
5043 | { | |
5044 | switch (bfd_get_format (abfd)) | |
5045 | { | |
5046 | case bfd_object: | |
5047 | return elf_link_add_object_symbols (abfd, info); | |
5048 | case bfd_archive: | |
5049 | return elf_link_add_archive_symbols (abfd, info); | |
5050 | default: | |
5051 | bfd_set_error (bfd_error_wrong_format); | |
5052 | return FALSE; | |
5053 | } | |
5054 | } | |
5a580b3a | 5055 | \f |
14b1c01e AM |
5056 | struct hash_codes_info |
5057 | { | |
5058 | unsigned long *hashcodes; | |
5059 | bfd_boolean error; | |
5060 | }; | |
a0c8462f | 5061 | |
5a580b3a AM |
5062 | /* This function will be called though elf_link_hash_traverse to store |
5063 | all hash value of the exported symbols in an array. */ | |
5064 | ||
5065 | static bfd_boolean | |
5066 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5067 | { | |
14b1c01e | 5068 | struct hash_codes_info *inf = data; |
5a580b3a AM |
5069 | const char *name; |
5070 | char *p; | |
5071 | unsigned long ha; | |
5072 | char *alc = NULL; | |
5073 | ||
5074 | if (h->root.type == bfd_link_hash_warning) | |
5075 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5076 | ||
5077 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
5078 | if (h->dynindx == -1) | |
5079 | return TRUE; | |
5080 | ||
5081 | name = h->root.root.string; | |
5082 | p = strchr (name, ELF_VER_CHR); | |
5083 | if (p != NULL) | |
5084 | { | |
5085 | alc = bfd_malloc (p - name + 1); | |
14b1c01e AM |
5086 | if (alc == NULL) |
5087 | { | |
5088 | inf->error = TRUE; | |
5089 | return FALSE; | |
5090 | } | |
5a580b3a AM |
5091 | memcpy (alc, name, p - name); |
5092 | alc[p - name] = '\0'; | |
5093 | name = alc; | |
5094 | } | |
5095 | ||
5096 | /* Compute the hash value. */ | |
5097 | ha = bfd_elf_hash (name); | |
5098 | ||
5099 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 5100 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
5101 | |
5102 | /* And store it in the struct so that we can put it in the hash table | |
5103 | later. */ | |
f6e332e6 | 5104 | h->u.elf_hash_value = ha; |
5a580b3a AM |
5105 | |
5106 | if (alc != NULL) | |
5107 | free (alc); | |
5108 | ||
5109 | return TRUE; | |
5110 | } | |
5111 | ||
fdc90cb4 JJ |
5112 | struct collect_gnu_hash_codes |
5113 | { | |
5114 | bfd *output_bfd; | |
5115 | const struct elf_backend_data *bed; | |
5116 | unsigned long int nsyms; | |
5117 | unsigned long int maskbits; | |
5118 | unsigned long int *hashcodes; | |
5119 | unsigned long int *hashval; | |
5120 | unsigned long int *indx; | |
5121 | unsigned long int *counts; | |
5122 | bfd_vma *bitmask; | |
5123 | bfd_byte *contents; | |
5124 | long int min_dynindx; | |
5125 | unsigned long int bucketcount; | |
5126 | unsigned long int symindx; | |
5127 | long int local_indx; | |
5128 | long int shift1, shift2; | |
5129 | unsigned long int mask; | |
14b1c01e | 5130 | bfd_boolean error; |
fdc90cb4 JJ |
5131 | }; |
5132 | ||
5133 | /* This function will be called though elf_link_hash_traverse to store | |
5134 | all hash value of the exported symbols in an array. */ | |
5135 | ||
5136 | static bfd_boolean | |
5137 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5138 | { | |
5139 | struct collect_gnu_hash_codes *s = data; | |
5140 | const char *name; | |
5141 | char *p; | |
5142 | unsigned long ha; | |
5143 | char *alc = NULL; | |
5144 | ||
5145 | if (h->root.type == bfd_link_hash_warning) | |
5146 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5147 | ||
5148 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
5149 | if (h->dynindx == -1) | |
5150 | return TRUE; | |
5151 | ||
5152 | /* Ignore also local symbols and undefined symbols. */ | |
5153 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5154 | return TRUE; | |
5155 | ||
5156 | name = h->root.root.string; | |
5157 | p = strchr (name, ELF_VER_CHR); | |
5158 | if (p != NULL) | |
5159 | { | |
5160 | alc = bfd_malloc (p - name + 1); | |
14b1c01e AM |
5161 | if (alc == NULL) |
5162 | { | |
5163 | s->error = TRUE; | |
5164 | return FALSE; | |
5165 | } | |
fdc90cb4 JJ |
5166 | memcpy (alc, name, p - name); |
5167 | alc[p - name] = '\0'; | |
5168 | name = alc; | |
5169 | } | |
5170 | ||
5171 | /* Compute the hash value. */ | |
5172 | ha = bfd_elf_gnu_hash (name); | |
5173 | ||
5174 | /* Store the found hash value in the array for compute_bucket_count, | |
5175 | and also for .dynsym reordering purposes. */ | |
5176 | s->hashcodes[s->nsyms] = ha; | |
5177 | s->hashval[h->dynindx] = ha; | |
5178 | ++s->nsyms; | |
5179 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5180 | s->min_dynindx = h->dynindx; | |
5181 | ||
5182 | if (alc != NULL) | |
5183 | free (alc); | |
5184 | ||
5185 | return TRUE; | |
5186 | } | |
5187 | ||
5188 | /* This function will be called though elf_link_hash_traverse to do | |
5189 | final dynaminc symbol renumbering. */ | |
5190 | ||
5191 | static bfd_boolean | |
5192 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5193 | { | |
5194 | struct collect_gnu_hash_codes *s = data; | |
5195 | unsigned long int bucket; | |
5196 | unsigned long int val; | |
5197 | ||
5198 | if (h->root.type == bfd_link_hash_warning) | |
5199 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5200 | ||
5201 | /* Ignore indirect symbols. */ | |
5202 | if (h->dynindx == -1) | |
5203 | return TRUE; | |
5204 | ||
5205 | /* Ignore also local symbols and undefined symbols. */ | |
5206 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5207 | { | |
5208 | if (h->dynindx >= s->min_dynindx) | |
5209 | h->dynindx = s->local_indx++; | |
5210 | return TRUE; | |
5211 | } | |
5212 | ||
5213 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5214 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5215 | & ((s->maskbits >> s->shift1) - 1); | |
5216 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5217 | s->bitmask[val] | |
5218 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5219 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5220 | if (s->counts[bucket] == 1) | |
5221 | /* Last element terminates the chain. */ | |
5222 | val |= 1; | |
5223 | bfd_put_32 (s->output_bfd, val, | |
5224 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5225 | --s->counts[bucket]; | |
5226 | h->dynindx = s->indx[bucket]++; | |
5227 | return TRUE; | |
5228 | } | |
5229 | ||
5230 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5231 | ||
5232 | bfd_boolean | |
5233 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5234 | { | |
5235 | return !(h->forced_local | |
5236 | || h->root.type == bfd_link_hash_undefined | |
5237 | || h->root.type == bfd_link_hash_undefweak | |
5238 | || ((h->root.type == bfd_link_hash_defined | |
5239 | || h->root.type == bfd_link_hash_defweak) | |
5240 | && h->root.u.def.section->output_section == NULL)); | |
5241 | } | |
5242 | ||
5a580b3a AM |
5243 | /* Array used to determine the number of hash table buckets to use |
5244 | based on the number of symbols there are. If there are fewer than | |
5245 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5246 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5247 | than 32771 buckets. */ | |
5248 | ||
5249 | static const size_t elf_buckets[] = | |
5250 | { | |
5251 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5252 | 16411, 32771, 0 | |
5253 | }; | |
5254 | ||
5255 | /* Compute bucket count for hashing table. We do not use a static set | |
5256 | of possible tables sizes anymore. Instead we determine for all | |
5257 | possible reasonable sizes of the table the outcome (i.e., the | |
5258 | number of collisions etc) and choose the best solution. The | |
5259 | weighting functions are not too simple to allow the table to grow | |
5260 | without bounds. Instead one of the weighting factors is the size. | |
5261 | Therefore the result is always a good payoff between few collisions | |
5262 | (= short chain lengths) and table size. */ | |
5263 | static size_t | |
d40f3da9 AM |
5264 | compute_bucket_count (struct bfd_link_info *info, |
5265 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, | |
5266 | unsigned long int nsyms, | |
5267 | int gnu_hash) | |
5a580b3a | 5268 | { |
5a580b3a | 5269 | size_t best_size = 0; |
5a580b3a | 5270 | unsigned long int i; |
5a580b3a | 5271 | |
5a580b3a AM |
5272 | /* We have a problem here. The following code to optimize the table |
5273 | size requires an integer type with more the 32 bits. If | |
5274 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5275 | #ifdef BFD_HOST_U_64_BIT | |
5276 | if (info->optimize) | |
5277 | { | |
5a580b3a AM |
5278 | size_t minsize; |
5279 | size_t maxsize; | |
5280 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a | 5281 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 5282 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 5283 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 5284 | unsigned long int *counts; |
d40f3da9 | 5285 | bfd_size_type amt; |
5a580b3a AM |
5286 | |
5287 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5288 | that the hashing table must at least have NSYMS/4 and at most | |
5289 | 2*NSYMS buckets. */ | |
5290 | minsize = nsyms / 4; | |
5291 | if (minsize == 0) | |
5292 | minsize = 1; | |
5293 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5294 | if (gnu_hash) |
5295 | { | |
5296 | if (minsize < 2) | |
5297 | minsize = 2; | |
5298 | if ((best_size & 31) == 0) | |
5299 | ++best_size; | |
5300 | } | |
5a580b3a AM |
5301 | |
5302 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5303 | since the size could be large. */ | |
5304 | amt = maxsize; | |
5305 | amt *= sizeof (unsigned long int); | |
5306 | counts = bfd_malloc (amt); | |
5307 | if (counts == NULL) | |
fdc90cb4 | 5308 | return 0; |
5a580b3a AM |
5309 | |
5310 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5311 | minimal chain length. The minor criteria is (of course) the size | |
5312 | of the table. */ | |
5313 | for (i = minsize; i < maxsize; ++i) | |
5314 | { | |
5315 | /* Walk through the array of hashcodes and count the collisions. */ | |
5316 | BFD_HOST_U_64_BIT max; | |
5317 | unsigned long int j; | |
5318 | unsigned long int fact; | |
5319 | ||
fdc90cb4 JJ |
5320 | if (gnu_hash && (i & 31) == 0) |
5321 | continue; | |
5322 | ||
5a580b3a AM |
5323 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5324 | ||
5325 | /* Determine how often each hash bucket is used. */ | |
5326 | for (j = 0; j < nsyms; ++j) | |
5327 | ++counts[hashcodes[j] % i]; | |
5328 | ||
5329 | /* For the weight function we need some information about the | |
5330 | pagesize on the target. This is information need not be 100% | |
5331 | accurate. Since this information is not available (so far) we | |
5332 | define it here to a reasonable default value. If it is crucial | |
5333 | to have a better value some day simply define this value. */ | |
5334 | # ifndef BFD_TARGET_PAGESIZE | |
5335 | # define BFD_TARGET_PAGESIZE (4096) | |
5336 | # endif | |
5337 | ||
fdc90cb4 JJ |
5338 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5339 | and the chains. */ | |
5340 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
5341 | |
5342 | # if 1 | |
5343 | /* Variant 1: optimize for short chains. We add the squares | |
5344 | of all the chain lengths (which favors many small chain | |
5345 | over a few long chains). */ | |
5346 | for (j = 0; j < i; ++j) | |
5347 | max += counts[j] * counts[j]; | |
5348 | ||
5349 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 5350 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5351 | max *= fact * fact; |
5352 | # else | |
5353 | /* Variant 2: Optimize a lot more for small table. Here we | |
5354 | also add squares of the size but we also add penalties for | |
5355 | empty slots (the +1 term). */ | |
5356 | for (j = 0; j < i; ++j) | |
5357 | max += (1 + counts[j]) * (1 + counts[j]); | |
5358 | ||
5359 | /* The overall size of the table is considered, but not as | |
5360 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 5361 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5362 | max *= fact; |
5363 | # endif | |
5364 | ||
5365 | /* Compare with current best results. */ | |
5366 | if (max < best_chlen) | |
5367 | { | |
5368 | best_chlen = max; | |
5369 | best_size = i; | |
5370 | } | |
5371 | } | |
5372 | ||
5373 | free (counts); | |
5374 | } | |
5375 | else | |
5376 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
5377 | { | |
5378 | /* This is the fallback solution if no 64bit type is available or if we | |
5379 | are not supposed to spend much time on optimizations. We select the | |
5380 | bucket count using a fixed set of numbers. */ | |
5381 | for (i = 0; elf_buckets[i] != 0; i++) | |
5382 | { | |
5383 | best_size = elf_buckets[i]; | |
fdc90cb4 | 5384 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
5385 | break; |
5386 | } | |
fdc90cb4 JJ |
5387 | if (gnu_hash && best_size < 2) |
5388 | best_size = 2; | |
5a580b3a AM |
5389 | } |
5390 | ||
5a580b3a AM |
5391 | return best_size; |
5392 | } | |
5393 | ||
5394 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
5395 | called by the ELF linker emulation before_allocation routine. We | |
5396 | must set the sizes of the sections before the linker sets the | |
5397 | addresses of the various sections. */ | |
5398 | ||
5399 | bfd_boolean | |
5400 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
5401 | const char *soname, | |
5402 | const char *rpath, | |
5403 | const char *filter_shlib, | |
5404 | const char * const *auxiliary_filters, | |
5405 | struct bfd_link_info *info, | |
5406 | asection **sinterpptr, | |
5407 | struct bfd_elf_version_tree *verdefs) | |
5408 | { | |
5409 | bfd_size_type soname_indx; | |
5410 | bfd *dynobj; | |
5411 | const struct elf_backend_data *bed; | |
5412 | struct elf_assign_sym_version_info asvinfo; | |
5413 | ||
5414 | *sinterpptr = NULL; | |
5415 | ||
5416 | soname_indx = (bfd_size_type) -1; | |
5417 | ||
5418 | if (!is_elf_hash_table (info->hash)) | |
5419 | return TRUE; | |
5420 | ||
6bfdb61b | 5421 | bed = get_elf_backend_data (output_bfd); |
5a580b3a AM |
5422 | if (info->execstack) |
5423 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
5424 | else if (info->noexecstack) | |
5425 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
5426 | else | |
5427 | { | |
5428 | bfd *inputobj; | |
5429 | asection *notesec = NULL; | |
5430 | int exec = 0; | |
5431 | ||
5432 | for (inputobj = info->input_bfds; | |
5433 | inputobj; | |
5434 | inputobj = inputobj->link_next) | |
5435 | { | |
5436 | asection *s; | |
5437 | ||
a94b9d2d | 5438 | if (inputobj->flags & (DYNAMIC | EXEC_P | BFD_LINKER_CREATED)) |
5a580b3a AM |
5439 | continue; |
5440 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
5441 | if (s) | |
5442 | { | |
5443 | if (s->flags & SEC_CODE) | |
5444 | exec = PF_X; | |
5445 | notesec = s; | |
5446 | } | |
6bfdb61b | 5447 | else if (bed->default_execstack) |
5a580b3a AM |
5448 | exec = PF_X; |
5449 | } | |
5450 | if (notesec) | |
5451 | { | |
5452 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
5453 | if (exec && info->relocatable | |
5454 | && notesec->output_section != bfd_abs_section_ptr) | |
5455 | notesec->output_section->flags |= SEC_CODE; | |
5456 | } | |
5457 | } | |
5458 | ||
5459 | /* Any syms created from now on start with -1 in | |
5460 | got.refcount/offset and plt.refcount/offset. */ | |
a6aa5195 AM |
5461 | elf_hash_table (info)->init_got_refcount |
5462 | = elf_hash_table (info)->init_got_offset; | |
5463 | elf_hash_table (info)->init_plt_refcount | |
5464 | = elf_hash_table (info)->init_plt_offset; | |
5a580b3a AM |
5465 | |
5466 | /* The backend may have to create some sections regardless of whether | |
5467 | we're dynamic or not. */ | |
5a580b3a AM |
5468 | if (bed->elf_backend_always_size_sections |
5469 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
5470 | return FALSE; | |
5471 | ||
eb3d5f3b JB |
5472 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) |
5473 | return FALSE; | |
5474 | ||
5a580b3a AM |
5475 | dynobj = elf_hash_table (info)->dynobj; |
5476 | ||
5477 | /* If there were no dynamic objects in the link, there is nothing to | |
5478 | do here. */ | |
5479 | if (dynobj == NULL) | |
5480 | return TRUE; | |
5481 | ||
5a580b3a AM |
5482 | if (elf_hash_table (info)->dynamic_sections_created) |
5483 | { | |
5484 | struct elf_info_failed eif; | |
5485 | struct elf_link_hash_entry *h; | |
5486 | asection *dynstr; | |
5487 | struct bfd_elf_version_tree *t; | |
5488 | struct bfd_elf_version_expr *d; | |
046183de | 5489 | asection *s; |
5a580b3a AM |
5490 | bfd_boolean all_defined; |
5491 | ||
5492 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
5493 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
5494 | ||
5495 | if (soname != NULL) | |
5496 | { | |
5497 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5498 | soname, TRUE); | |
5499 | if (soname_indx == (bfd_size_type) -1 | |
5500 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
5501 | return FALSE; | |
5502 | } | |
5503 | ||
5504 | if (info->symbolic) | |
5505 | { | |
5506 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
5507 | return FALSE; | |
5508 | info->flags |= DF_SYMBOLIC; | |
5509 | } | |
5510 | ||
5511 | if (rpath != NULL) | |
5512 | { | |
5513 | bfd_size_type indx; | |
5514 | ||
5515 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
5516 | TRUE); | |
5517 | if (indx == (bfd_size_type) -1 | |
5518 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
5519 | return FALSE; | |
5520 | ||
5521 | if (info->new_dtags) | |
5522 | { | |
5523 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
5524 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
5525 | return FALSE; | |
5526 | } | |
5527 | } | |
5528 | ||
5529 | if (filter_shlib != NULL) | |
5530 | { | |
5531 | bfd_size_type indx; | |
5532 | ||
5533 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5534 | filter_shlib, TRUE); | |
5535 | if (indx == (bfd_size_type) -1 | |
5536 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
5537 | return FALSE; | |
5538 | } | |
5539 | ||
5540 | if (auxiliary_filters != NULL) | |
5541 | { | |
5542 | const char * const *p; | |
5543 | ||
5544 | for (p = auxiliary_filters; *p != NULL; p++) | |
5545 | { | |
5546 | bfd_size_type indx; | |
5547 | ||
5548 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5549 | *p, TRUE); | |
5550 | if (indx == (bfd_size_type) -1 | |
5551 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
5552 | return FALSE; | |
5553 | } | |
5554 | } | |
5555 | ||
5556 | eif.info = info; | |
5557 | eif.verdefs = verdefs; | |
5558 | eif.failed = FALSE; | |
5559 | ||
5560 | /* If we are supposed to export all symbols into the dynamic symbol | |
5561 | table (this is not the normal case), then do so. */ | |
55255dae L |
5562 | if (info->export_dynamic |
5563 | || (info->executable && info->dynamic)) | |
5a580b3a AM |
5564 | { |
5565 | elf_link_hash_traverse (elf_hash_table (info), | |
5566 | _bfd_elf_export_symbol, | |
5567 | &eif); | |
5568 | if (eif.failed) | |
5569 | return FALSE; | |
5570 | } | |
5571 | ||
5572 | /* Make all global versions with definition. */ | |
5573 | for (t = verdefs; t != NULL; t = t->next) | |
5574 | for (d = t->globals.list; d != NULL; d = d->next) | |
ae5a3597 | 5575 | if (!d->symver && d->literal) |
5a580b3a AM |
5576 | { |
5577 | const char *verstr, *name; | |
5578 | size_t namelen, verlen, newlen; | |
5579 | char *newname, *p; | |
5580 | struct elf_link_hash_entry *newh; | |
5581 | ||
ae5a3597 | 5582 | name = d->pattern; |
5a580b3a AM |
5583 | namelen = strlen (name); |
5584 | verstr = t->name; | |
5585 | verlen = strlen (verstr); | |
5586 | newlen = namelen + verlen + 3; | |
5587 | ||
5588 | newname = bfd_malloc (newlen); | |
5589 | if (newname == NULL) | |
5590 | return FALSE; | |
5591 | memcpy (newname, name, namelen); | |
5592 | ||
5593 | /* Check the hidden versioned definition. */ | |
5594 | p = newname + namelen; | |
5595 | *p++ = ELF_VER_CHR; | |
5596 | memcpy (p, verstr, verlen + 1); | |
5597 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5598 | newname, FALSE, FALSE, | |
5599 | FALSE); | |
5600 | if (newh == NULL | |
5601 | || (newh->root.type != bfd_link_hash_defined | |
5602 | && newh->root.type != bfd_link_hash_defweak)) | |
5603 | { | |
5604 | /* Check the default versioned definition. */ | |
5605 | *p++ = ELF_VER_CHR; | |
5606 | memcpy (p, verstr, verlen + 1); | |
5607 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5608 | newname, FALSE, FALSE, | |
5609 | FALSE); | |
5610 | } | |
5611 | free (newname); | |
5612 | ||
5613 | /* Mark this version if there is a definition and it is | |
5614 | not defined in a shared object. */ | |
5615 | if (newh != NULL | |
f5385ebf | 5616 | && !newh->def_dynamic |
5a580b3a AM |
5617 | && (newh->root.type == bfd_link_hash_defined |
5618 | || newh->root.type == bfd_link_hash_defweak)) | |
5619 | d->symver = 1; | |
5620 | } | |
5621 | ||
5622 | /* Attach all the symbols to their version information. */ | |
5623 | asvinfo.output_bfd = output_bfd; | |
5624 | asvinfo.info = info; | |
5625 | asvinfo.verdefs = verdefs; | |
5626 | asvinfo.failed = FALSE; | |
5627 | ||
5628 | elf_link_hash_traverse (elf_hash_table (info), | |
5629 | _bfd_elf_link_assign_sym_version, | |
5630 | &asvinfo); | |
5631 | if (asvinfo.failed) | |
5632 | return FALSE; | |
5633 | ||
5634 | if (!info->allow_undefined_version) | |
5635 | { | |
5636 | /* Check if all global versions have a definition. */ | |
5637 | all_defined = TRUE; | |
5638 | for (t = verdefs; t != NULL; t = t->next) | |
5639 | for (d = t->globals.list; d != NULL; d = d->next) | |
ae5a3597 | 5640 | if (d->literal && !d->symver && !d->script) |
5a580b3a AM |
5641 | { |
5642 | (*_bfd_error_handler) | |
5643 | (_("%s: undefined version: %s"), | |
5644 | d->pattern, t->name); | |
5645 | all_defined = FALSE; | |
5646 | } | |
5647 | ||
5648 | if (!all_defined) | |
5649 | { | |
5650 | bfd_set_error (bfd_error_bad_value); | |
5651 | return FALSE; | |
5652 | } | |
5653 | } | |
5654 | ||
5655 | /* Find all symbols which were defined in a dynamic object and make | |
5656 | the backend pick a reasonable value for them. */ | |
5657 | elf_link_hash_traverse (elf_hash_table (info), | |
5658 | _bfd_elf_adjust_dynamic_symbol, | |
5659 | &eif); | |
5660 | if (eif.failed) | |
5661 | return FALSE; | |
5662 | ||
5663 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 5664 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
5665 | now so that we know the final size of the .dynamic section. */ |
5666 | ||
5667 | /* If there are initialization and/or finalization functions to | |
5668 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
5669 | h = (info->init_function | |
5670 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5671 | info->init_function, FALSE, | |
5672 | FALSE, FALSE) | |
5673 | : NULL); | |
5674 | if (h != NULL | |
f5385ebf AM |
5675 | && (h->ref_regular |
5676 | || h->def_regular)) | |
5a580b3a AM |
5677 | { |
5678 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
5679 | return FALSE; | |
5680 | } | |
5681 | h = (info->fini_function | |
5682 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5683 | info->fini_function, FALSE, | |
5684 | FALSE, FALSE) | |
5685 | : NULL); | |
5686 | if (h != NULL | |
f5385ebf AM |
5687 | && (h->ref_regular |
5688 | || h->def_regular)) | |
5a580b3a AM |
5689 | { |
5690 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
5691 | return FALSE; | |
5692 | } | |
5693 | ||
046183de AM |
5694 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); |
5695 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5696 | { |
5697 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
5698 | if (! info->executable) | |
5699 | { | |
5700 | bfd *sub; | |
5701 | asection *o; | |
5702 | ||
5703 | for (sub = info->input_bfds; sub != NULL; | |
5704 | sub = sub->link_next) | |
3fcd97f1 JJ |
5705 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour) |
5706 | for (o = sub->sections; o != NULL; o = o->next) | |
5707 | if (elf_section_data (o)->this_hdr.sh_type | |
5708 | == SHT_PREINIT_ARRAY) | |
5709 | { | |
5710 | (*_bfd_error_handler) | |
5711 | (_("%B: .preinit_array section is not allowed in DSO"), | |
5712 | sub); | |
5713 | break; | |
5714 | } | |
5a580b3a AM |
5715 | |
5716 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5717 | return FALSE; | |
5718 | } | |
5719 | ||
5720 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
5721 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
5722 | return FALSE; | |
5723 | } | |
046183de AM |
5724 | s = bfd_get_section_by_name (output_bfd, ".init_array"); |
5725 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5726 | { |
5727 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
5728 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
5729 | return FALSE; | |
5730 | } | |
046183de AM |
5731 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); |
5732 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5733 | { |
5734 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
5735 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
5736 | return FALSE; | |
5737 | } | |
5738 | ||
5739 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5740 | /* If .dynstr is excluded from the link, we don't want any of | |
5741 | these tags. Strictly, we should be checking each section | |
5742 | individually; This quick check covers for the case where | |
5743 | someone does a /DISCARD/ : { *(*) }. */ | |
5744 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
5745 | { | |
5746 | bfd_size_type strsize; | |
5747 | ||
5748 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
fdc90cb4 JJ |
5749 | if ((info->emit_hash |
5750 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
5751 | || (info->emit_gnu_hash | |
5752 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
5a580b3a AM |
5753 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) |
5754 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
5755 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
5756 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
5757 | bed->s->sizeof_sym)) | |
5758 | return FALSE; | |
5759 | } | |
5760 | } | |
5761 | ||
5762 | /* The backend must work out the sizes of all the other dynamic | |
5763 | sections. */ | |
5764 | if (bed->elf_backend_size_dynamic_sections | |
5765 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
5766 | return FALSE; | |
5767 | ||
5768 | if (elf_hash_table (info)->dynamic_sections_created) | |
5769 | { | |
554220db | 5770 | unsigned long section_sym_count; |
5a580b3a | 5771 | asection *s; |
5a580b3a AM |
5772 | |
5773 | /* Set up the version definition section. */ | |
5774 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
5775 | BFD_ASSERT (s != NULL); | |
5776 | ||
5777 | /* We may have created additional version definitions if we are | |
5778 | just linking a regular application. */ | |
5779 | verdefs = asvinfo.verdefs; | |
5780 | ||
5781 | /* Skip anonymous version tag. */ | |
5782 | if (verdefs != NULL && verdefs->vernum == 0) | |
5783 | verdefs = verdefs->next; | |
5784 | ||
3e3b46e5 | 5785 | if (verdefs == NULL && !info->create_default_symver) |
8423293d | 5786 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5787 | else |
5788 | { | |
5789 | unsigned int cdefs; | |
5790 | bfd_size_type size; | |
5791 | struct bfd_elf_version_tree *t; | |
5792 | bfd_byte *p; | |
5793 | Elf_Internal_Verdef def; | |
5794 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
5795 | struct bfd_link_hash_entry *bh; |
5796 | struct elf_link_hash_entry *h; | |
5797 | const char *name; | |
5a580b3a AM |
5798 | |
5799 | cdefs = 0; | |
5800 | size = 0; | |
5801 | ||
5802 | /* Make space for the base version. */ | |
5803 | size += sizeof (Elf_External_Verdef); | |
5804 | size += sizeof (Elf_External_Verdaux); | |
5805 | ++cdefs; | |
5806 | ||
3e3b46e5 PB |
5807 | /* Make space for the default version. */ |
5808 | if (info->create_default_symver) | |
5809 | { | |
5810 | size += sizeof (Elf_External_Verdef); | |
5811 | ++cdefs; | |
5812 | } | |
5813 | ||
5a580b3a AM |
5814 | for (t = verdefs; t != NULL; t = t->next) |
5815 | { | |
5816 | struct bfd_elf_version_deps *n; | |
5817 | ||
5818 | size += sizeof (Elf_External_Verdef); | |
5819 | size += sizeof (Elf_External_Verdaux); | |
5820 | ++cdefs; | |
5821 | ||
5822 | for (n = t->deps; n != NULL; n = n->next) | |
5823 | size += sizeof (Elf_External_Verdaux); | |
5824 | } | |
5825 | ||
eea6121a AM |
5826 | s->size = size; |
5827 | s->contents = bfd_alloc (output_bfd, s->size); | |
5828 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5829 | return FALSE; |
5830 | ||
5831 | /* Fill in the version definition section. */ | |
5832 | ||
5833 | p = s->contents; | |
5834 | ||
5835 | def.vd_version = VER_DEF_CURRENT; | |
5836 | def.vd_flags = VER_FLG_BASE; | |
5837 | def.vd_ndx = 1; | |
5838 | def.vd_cnt = 1; | |
3e3b46e5 PB |
5839 | if (info->create_default_symver) |
5840 | { | |
5841 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
5842 | def.vd_next = sizeof (Elf_External_Verdef); | |
5843 | } | |
5844 | else | |
5845 | { | |
5846 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5847 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5848 | + sizeof (Elf_External_Verdaux)); | |
5849 | } | |
5a580b3a AM |
5850 | |
5851 | if (soname_indx != (bfd_size_type) -1) | |
5852 | { | |
5853 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5854 | soname_indx); | |
5855 | def.vd_hash = bfd_elf_hash (soname); | |
5856 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 5857 | name = soname; |
5a580b3a AM |
5858 | } |
5859 | else | |
5860 | { | |
5a580b3a AM |
5861 | bfd_size_type indx; |
5862 | ||
06084812 | 5863 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
5864 | def.vd_hash = bfd_elf_hash (name); |
5865 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5866 | name, FALSE); | |
5867 | if (indx == (bfd_size_type) -1) | |
5868 | return FALSE; | |
5869 | defaux.vda_name = indx; | |
5870 | } | |
5871 | defaux.vda_next = 0; | |
5872 | ||
5873 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5874 | (Elf_External_Verdef *) p); | |
5875 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
5876 | if (info->create_default_symver) |
5877 | { | |
5878 | /* Add a symbol representing this version. */ | |
5879 | bh = NULL; | |
5880 | if (! (_bfd_generic_link_add_one_symbol | |
5881 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5882 | 0, NULL, FALSE, | |
5883 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5884 | return FALSE; | |
5885 | h = (struct elf_link_hash_entry *) bh; | |
5886 | h->non_elf = 0; | |
5887 | h->def_regular = 1; | |
5888 | h->type = STT_OBJECT; | |
5889 | h->verinfo.vertree = NULL; | |
5890 | ||
5891 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
5892 | return FALSE; | |
5893 | ||
5894 | /* Create a duplicate of the base version with the same | |
5895 | aux block, but different flags. */ | |
5896 | def.vd_flags = 0; | |
5897 | def.vd_ndx = 2; | |
5898 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5899 | if (verdefs) | |
5900 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5901 | + sizeof (Elf_External_Verdaux)); | |
5902 | else | |
5903 | def.vd_next = 0; | |
5904 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5905 | (Elf_External_Verdef *) p); | |
5906 | p += sizeof (Elf_External_Verdef); | |
5907 | } | |
5a580b3a AM |
5908 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
5909 | (Elf_External_Verdaux *) p); | |
5910 | p += sizeof (Elf_External_Verdaux); | |
5911 | ||
5912 | for (t = verdefs; t != NULL; t = t->next) | |
5913 | { | |
5914 | unsigned int cdeps; | |
5915 | struct bfd_elf_version_deps *n; | |
5a580b3a AM |
5916 | |
5917 | cdeps = 0; | |
5918 | for (n = t->deps; n != NULL; n = n->next) | |
5919 | ++cdeps; | |
5920 | ||
5921 | /* Add a symbol representing this version. */ | |
5922 | bh = NULL; | |
5923 | if (! (_bfd_generic_link_add_one_symbol | |
5924 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5925 | 0, NULL, FALSE, | |
5926 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5927 | return FALSE; | |
5928 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
5929 | h->non_elf = 0; |
5930 | h->def_regular = 1; | |
5a580b3a AM |
5931 | h->type = STT_OBJECT; |
5932 | h->verinfo.vertree = t; | |
5933 | ||
c152c796 | 5934 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
5935 | return FALSE; |
5936 | ||
5937 | def.vd_version = VER_DEF_CURRENT; | |
5938 | def.vd_flags = 0; | |
5939 | if (t->globals.list == NULL | |
5940 | && t->locals.list == NULL | |
5941 | && ! t->used) | |
5942 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 5943 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
5944 | def.vd_cnt = cdeps + 1; |
5945 | def.vd_hash = bfd_elf_hash (t->name); | |
5946 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5947 | def.vd_next = 0; | |
5948 | if (t->next != NULL) | |
5949 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5950 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
5951 | ||
5952 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5953 | (Elf_External_Verdef *) p); | |
5954 | p += sizeof (Elf_External_Verdef); | |
5955 | ||
5956 | defaux.vda_name = h->dynstr_index; | |
5957 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5958 | h->dynstr_index); | |
5959 | defaux.vda_next = 0; | |
5960 | if (t->deps != NULL) | |
5961 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5962 | t->name_indx = defaux.vda_name; | |
5963 | ||
5964 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5965 | (Elf_External_Verdaux *) p); | |
5966 | p += sizeof (Elf_External_Verdaux); | |
5967 | ||
5968 | for (n = t->deps; n != NULL; n = n->next) | |
5969 | { | |
5970 | if (n->version_needed == NULL) | |
5971 | { | |
5972 | /* This can happen if there was an error in the | |
5973 | version script. */ | |
5974 | defaux.vda_name = 0; | |
5975 | } | |
5976 | else | |
5977 | { | |
5978 | defaux.vda_name = n->version_needed->name_indx; | |
5979 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5980 | defaux.vda_name); | |
5981 | } | |
5982 | if (n->next == NULL) | |
5983 | defaux.vda_next = 0; | |
5984 | else | |
5985 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5986 | ||
5987 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5988 | (Elf_External_Verdaux *) p); | |
5989 | p += sizeof (Elf_External_Verdaux); | |
5990 | } | |
5991 | } | |
5992 | ||
5993 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
5994 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
5995 | return FALSE; | |
5996 | ||
5997 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
5998 | } | |
5999 | ||
6000 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
6001 | { | |
6002 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
6003 | return FALSE; | |
6004 | } | |
6005 | else if (info->flags & DF_BIND_NOW) | |
6006 | { | |
6007 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
6008 | return FALSE; | |
6009 | } | |
6010 | ||
6011 | if (info->flags_1) | |
6012 | { | |
6013 | if (info->executable) | |
6014 | info->flags_1 &= ~ (DF_1_INITFIRST | |
6015 | | DF_1_NODELETE | |
6016 | | DF_1_NOOPEN); | |
6017 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
6018 | return FALSE; | |
6019 | } | |
6020 | ||
6021 | /* Work out the size of the version reference section. */ | |
6022 | ||
6023 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
6024 | BFD_ASSERT (s != NULL); | |
6025 | { | |
6026 | struct elf_find_verdep_info sinfo; | |
6027 | ||
6028 | sinfo.output_bfd = output_bfd; | |
6029 | sinfo.info = info; | |
6030 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
6031 | if (sinfo.vers == 0) | |
6032 | sinfo.vers = 1; | |
6033 | sinfo.failed = FALSE; | |
6034 | ||
6035 | elf_link_hash_traverse (elf_hash_table (info), | |
6036 | _bfd_elf_link_find_version_dependencies, | |
6037 | &sinfo); | |
14b1c01e AM |
6038 | if (sinfo.failed) |
6039 | return FALSE; | |
5a580b3a AM |
6040 | |
6041 | if (elf_tdata (output_bfd)->verref == NULL) | |
8423293d | 6042 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
6043 | else |
6044 | { | |
6045 | Elf_Internal_Verneed *t; | |
6046 | unsigned int size; | |
6047 | unsigned int crefs; | |
6048 | bfd_byte *p; | |
6049 | ||
6050 | /* Build the version definition section. */ | |
6051 | size = 0; | |
6052 | crefs = 0; | |
6053 | for (t = elf_tdata (output_bfd)->verref; | |
6054 | t != NULL; | |
6055 | t = t->vn_nextref) | |
6056 | { | |
6057 | Elf_Internal_Vernaux *a; | |
6058 | ||
6059 | size += sizeof (Elf_External_Verneed); | |
6060 | ++crefs; | |
6061 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6062 | size += sizeof (Elf_External_Vernaux); | |
6063 | } | |
6064 | ||
eea6121a AM |
6065 | s->size = size; |
6066 | s->contents = bfd_alloc (output_bfd, s->size); | |
5a580b3a AM |
6067 | if (s->contents == NULL) |
6068 | return FALSE; | |
6069 | ||
6070 | p = s->contents; | |
6071 | for (t = elf_tdata (output_bfd)->verref; | |
6072 | t != NULL; | |
6073 | t = t->vn_nextref) | |
6074 | { | |
6075 | unsigned int caux; | |
6076 | Elf_Internal_Vernaux *a; | |
6077 | bfd_size_type indx; | |
6078 | ||
6079 | caux = 0; | |
6080 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6081 | ++caux; | |
6082 | ||
6083 | t->vn_version = VER_NEED_CURRENT; | |
6084 | t->vn_cnt = caux; | |
6085 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6086 | elf_dt_name (t->vn_bfd) != NULL | |
6087 | ? elf_dt_name (t->vn_bfd) | |
06084812 | 6088 | : lbasename (t->vn_bfd->filename), |
5a580b3a AM |
6089 | FALSE); |
6090 | if (indx == (bfd_size_type) -1) | |
6091 | return FALSE; | |
6092 | t->vn_file = indx; | |
6093 | t->vn_aux = sizeof (Elf_External_Verneed); | |
6094 | if (t->vn_nextref == NULL) | |
6095 | t->vn_next = 0; | |
6096 | else | |
6097 | t->vn_next = (sizeof (Elf_External_Verneed) | |
6098 | + caux * sizeof (Elf_External_Vernaux)); | |
6099 | ||
6100 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
6101 | (Elf_External_Verneed *) p); | |
6102 | p += sizeof (Elf_External_Verneed); | |
6103 | ||
6104 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6105 | { | |
6106 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6107 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6108 | a->vna_nodename, FALSE); | |
6109 | if (indx == (bfd_size_type) -1) | |
6110 | return FALSE; | |
6111 | a->vna_name = indx; | |
6112 | if (a->vna_nextptr == NULL) | |
6113 | a->vna_next = 0; | |
6114 | else | |
6115 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6116 | ||
6117 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6118 | (Elf_External_Vernaux *) p); | |
6119 | p += sizeof (Elf_External_Vernaux); | |
6120 | } | |
6121 | } | |
6122 | ||
6123 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
6124 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
6125 | return FALSE; | |
6126 | ||
6127 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6128 | } | |
6129 | } | |
6130 | ||
8423293d AM |
6131 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
6132 | && elf_tdata (output_bfd)->cverdefs == 0) | |
6133 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
6134 | §ion_sym_count) == 0) | |
6135 | { | |
6136 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
6137 | s->flags |= SEC_EXCLUDE; | |
6138 | } | |
6139 | } | |
6140 | return TRUE; | |
6141 | } | |
6142 | ||
74541ad4 AM |
6143 | /* Find the first non-excluded output section. We'll use its |
6144 | section symbol for some emitted relocs. */ | |
6145 | void | |
6146 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
6147 | { | |
6148 | asection *s; | |
6149 | ||
6150 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6151 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
6152 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6153 | { | |
6154 | elf_hash_table (info)->text_index_section = s; | |
6155 | break; | |
6156 | } | |
6157 | } | |
6158 | ||
6159 | /* Find two non-excluded output sections, one for code, one for data. | |
6160 | We'll use their section symbols for some emitted relocs. */ | |
6161 | void | |
6162 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
6163 | { | |
6164 | asection *s; | |
6165 | ||
266b05cf DJ |
6166 | /* Data first, since setting text_index_section changes |
6167 | _bfd_elf_link_omit_section_dynsym. */ | |
74541ad4 | 6168 | for (s = output_bfd->sections; s != NULL; s = s->next) |
266b05cf | 6169 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) |
74541ad4 AM |
6170 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6171 | { | |
266b05cf | 6172 | elf_hash_table (info)->data_index_section = s; |
74541ad4 AM |
6173 | break; |
6174 | } | |
6175 | ||
6176 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
266b05cf DJ |
6177 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) |
6178 | == (SEC_ALLOC | SEC_READONLY)) | |
74541ad4 AM |
6179 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6180 | { | |
266b05cf | 6181 | elf_hash_table (info)->text_index_section = s; |
74541ad4 AM |
6182 | break; |
6183 | } | |
6184 | ||
6185 | if (elf_hash_table (info)->text_index_section == NULL) | |
6186 | elf_hash_table (info)->text_index_section | |
6187 | = elf_hash_table (info)->data_index_section; | |
6188 | } | |
6189 | ||
8423293d AM |
6190 | bfd_boolean |
6191 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
6192 | { | |
74541ad4 AM |
6193 | const struct elf_backend_data *bed; |
6194 | ||
8423293d AM |
6195 | if (!is_elf_hash_table (info->hash)) |
6196 | return TRUE; | |
6197 | ||
74541ad4 AM |
6198 | bed = get_elf_backend_data (output_bfd); |
6199 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
6200 | ||
8423293d AM |
6201 | if (elf_hash_table (info)->dynamic_sections_created) |
6202 | { | |
6203 | bfd *dynobj; | |
8423293d AM |
6204 | asection *s; |
6205 | bfd_size_type dynsymcount; | |
6206 | unsigned long section_sym_count; | |
8423293d AM |
6207 | unsigned int dtagcount; |
6208 | ||
6209 | dynobj = elf_hash_table (info)->dynobj; | |
6210 | ||
5a580b3a AM |
6211 | /* Assign dynsym indicies. In a shared library we generate a |
6212 | section symbol for each output section, which come first. | |
6213 | Next come all of the back-end allocated local dynamic syms, | |
6214 | followed by the rest of the global symbols. */ | |
6215 | ||
554220db AM |
6216 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, |
6217 | §ion_sym_count); | |
5a580b3a AM |
6218 | |
6219 | /* Work out the size of the symbol version section. */ | |
6220 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
6221 | BFD_ASSERT (s != NULL); | |
8423293d AM |
6222 | if (dynsymcount != 0 |
6223 | && (s->flags & SEC_EXCLUDE) == 0) | |
5a580b3a | 6224 | { |
eea6121a AM |
6225 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
6226 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
6227 | if (s->contents == NULL) |
6228 | return FALSE; | |
6229 | ||
6230 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
6231 | return FALSE; | |
6232 | } | |
6233 | ||
6234 | /* Set the size of the .dynsym and .hash sections. We counted | |
6235 | the number of dynamic symbols in elf_link_add_object_symbols. | |
6236 | We will build the contents of .dynsym and .hash when we build | |
6237 | the final symbol table, because until then we do not know the | |
6238 | correct value to give the symbols. We built the .dynstr | |
6239 | section as we went along in elf_link_add_object_symbols. */ | |
6240 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
6241 | BFD_ASSERT (s != NULL); | |
eea6121a | 6242 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a AM |
6243 | |
6244 | if (dynsymcount != 0) | |
6245 | { | |
554220db AM |
6246 | s->contents = bfd_alloc (output_bfd, s->size); |
6247 | if (s->contents == NULL) | |
6248 | return FALSE; | |
5a580b3a | 6249 | |
554220db AM |
6250 | /* The first entry in .dynsym is a dummy symbol. |
6251 | Clear all the section syms, in case we don't output them all. */ | |
6252 | ++section_sym_count; | |
6253 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a AM |
6254 | } |
6255 | ||
fdc90cb4 JJ |
6256 | elf_hash_table (info)->bucketcount = 0; |
6257 | ||
5a580b3a AM |
6258 | /* Compute the size of the hashing table. As a side effect this |
6259 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
6260 | if (info->emit_hash) |
6261 | { | |
6262 | unsigned long int *hashcodes; | |
14b1c01e | 6263 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
6264 | bfd_size_type amt; |
6265 | unsigned long int nsyms; | |
6266 | size_t bucketcount; | |
6267 | size_t hash_entry_size; | |
6268 | ||
6269 | /* Compute the hash values for all exported symbols. At the same | |
6270 | time store the values in an array so that we could use them for | |
6271 | optimizations. */ | |
6272 | amt = dynsymcount * sizeof (unsigned long int); | |
6273 | hashcodes = bfd_malloc (amt); | |
6274 | if (hashcodes == NULL) | |
6275 | return FALSE; | |
14b1c01e AM |
6276 | hashinf.hashcodes = hashcodes; |
6277 | hashinf.error = FALSE; | |
5a580b3a | 6278 | |
fdc90cb4 JJ |
6279 | /* Put all hash values in HASHCODES. */ |
6280 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
6281 | elf_collect_hash_codes, &hashinf); |
6282 | if (hashinf.error) | |
4dd07732 AM |
6283 | { |
6284 | free (hashcodes); | |
6285 | return FALSE; | |
6286 | } | |
5a580b3a | 6287 | |
14b1c01e | 6288 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
6289 | bucketcount |
6290 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
6291 | free (hashcodes); | |
6292 | ||
6293 | if (bucketcount == 0) | |
6294 | return FALSE; | |
5a580b3a | 6295 | |
fdc90cb4 JJ |
6296 | elf_hash_table (info)->bucketcount = bucketcount; |
6297 | ||
6298 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
6299 | BFD_ASSERT (s != NULL); | |
6300 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
6301 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
6302 | s->contents = bfd_zalloc (output_bfd, s->size); | |
6303 | if (s->contents == NULL) | |
6304 | return FALSE; | |
6305 | ||
6306 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
6307 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
6308 | s->contents + hash_entry_size); | |
6309 | } | |
6310 | ||
6311 | if (info->emit_gnu_hash) | |
6312 | { | |
6313 | size_t i, cnt; | |
6314 | unsigned char *contents; | |
6315 | struct collect_gnu_hash_codes cinfo; | |
6316 | bfd_size_type amt; | |
6317 | size_t bucketcount; | |
6318 | ||
6319 | memset (&cinfo, 0, sizeof (cinfo)); | |
6320 | ||
6321 | /* Compute the hash values for all exported symbols. At the same | |
6322 | time store the values in an array so that we could use them for | |
6323 | optimizations. */ | |
6324 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
6325 | cinfo.hashcodes = bfd_malloc (amt); | |
6326 | if (cinfo.hashcodes == NULL) | |
6327 | return FALSE; | |
6328 | ||
6329 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
6330 | cinfo.min_dynindx = -1; | |
6331 | cinfo.output_bfd = output_bfd; | |
6332 | cinfo.bed = bed; | |
6333 | ||
6334 | /* Put all hash values in HASHCODES. */ | |
6335 | elf_link_hash_traverse (elf_hash_table (info), | |
6336 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e | 6337 | if (cinfo.error) |
4dd07732 AM |
6338 | { |
6339 | free (cinfo.hashcodes); | |
6340 | return FALSE; | |
6341 | } | |
fdc90cb4 JJ |
6342 | |
6343 | bucketcount | |
6344 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
6345 | ||
6346 | if (bucketcount == 0) | |
6347 | { | |
6348 | free (cinfo.hashcodes); | |
6349 | return FALSE; | |
6350 | } | |
6351 | ||
6352 | s = bfd_get_section_by_name (dynobj, ".gnu.hash"); | |
6353 | BFD_ASSERT (s != NULL); | |
6354 | ||
6355 | if (cinfo.nsyms == 0) | |
6356 | { | |
6357 | /* Empty .gnu.hash section is special. */ | |
6358 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
6359 | free (cinfo.hashcodes); | |
6360 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
6361 | contents = bfd_zalloc (output_bfd, s->size); | |
6362 | if (contents == NULL) | |
6363 | return FALSE; | |
6364 | s->contents = contents; | |
6365 | /* 1 empty bucket. */ | |
6366 | bfd_put_32 (output_bfd, 1, contents); | |
6367 | /* SYMIDX above the special symbol 0. */ | |
6368 | bfd_put_32 (output_bfd, 1, contents + 4); | |
6369 | /* Just one word for bitmask. */ | |
6370 | bfd_put_32 (output_bfd, 1, contents + 8); | |
6371 | /* Only hash fn bloom filter. */ | |
6372 | bfd_put_32 (output_bfd, 0, contents + 12); | |
6373 | /* No hashes are valid - empty bitmask. */ | |
6374 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
6375 | /* No hashes in the only bucket. */ | |
6376 | bfd_put_32 (output_bfd, 0, | |
6377 | contents + 16 + bed->s->arch_size / 8); | |
6378 | } | |
6379 | else | |
6380 | { | |
fdc90cb4 | 6381 | unsigned long int maskwords, maskbitslog2; |
0b33793d | 6382 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 JJ |
6383 | |
6384 | maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1; | |
6385 | if (maskbitslog2 < 3) | |
6386 | maskbitslog2 = 5; | |
6387 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
6388 | maskbitslog2 = maskbitslog2 + 3; | |
6389 | else | |
6390 | maskbitslog2 = maskbitslog2 + 2; | |
6391 | if (bed->s->arch_size == 64) | |
6392 | { | |
6393 | if (maskbitslog2 == 5) | |
6394 | maskbitslog2 = 6; | |
6395 | cinfo.shift1 = 6; | |
6396 | } | |
6397 | else | |
6398 | cinfo.shift1 = 5; | |
6399 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 6400 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
6401 | cinfo.maskbits = 1 << maskbitslog2; |
6402 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
6403 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
6404 | amt += maskwords * sizeof (bfd_vma); | |
6405 | cinfo.bitmask = bfd_malloc (amt); | |
6406 | if (cinfo.bitmask == NULL) | |
6407 | { | |
6408 | free (cinfo.hashcodes); | |
6409 | return FALSE; | |
6410 | } | |
6411 | ||
6412 | cinfo.counts = (void *) (cinfo.bitmask + maskwords); | |
6413 | cinfo.indx = cinfo.counts + bucketcount; | |
6414 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
6415 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
6416 | ||
6417 | /* Determine how often each hash bucket is used. */ | |
6418 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
6419 | for (i = 0; i < cinfo.nsyms; ++i) | |
6420 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
6421 | ||
6422 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
6423 | if (cinfo.counts[i] != 0) | |
6424 | { | |
6425 | cinfo.indx[i] = cnt; | |
6426 | cnt += cinfo.counts[i]; | |
6427 | } | |
6428 | BFD_ASSERT (cnt == dynsymcount); | |
6429 | cinfo.bucketcount = bucketcount; | |
6430 | cinfo.local_indx = cinfo.min_dynindx; | |
6431 | ||
6432 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
6433 | s->size += cinfo.maskbits / 8; | |
6434 | contents = bfd_zalloc (output_bfd, s->size); | |
6435 | if (contents == NULL) | |
6436 | { | |
6437 | free (cinfo.bitmask); | |
6438 | free (cinfo.hashcodes); | |
6439 | return FALSE; | |
6440 | } | |
6441 | ||
6442 | s->contents = contents; | |
6443 | bfd_put_32 (output_bfd, bucketcount, contents); | |
6444 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
6445 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
6446 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
6447 | contents += 16 + cinfo.maskbits / 8; | |
6448 | ||
6449 | for (i = 0; i < bucketcount; ++i) | |
6450 | { | |
6451 | if (cinfo.counts[i] == 0) | |
6452 | bfd_put_32 (output_bfd, 0, contents); | |
6453 | else | |
6454 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
6455 | contents += 4; | |
6456 | } | |
6457 | ||
6458 | cinfo.contents = contents; | |
6459 | ||
6460 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
6461 | elf_link_hash_traverse (elf_hash_table (info), | |
6462 | elf_renumber_gnu_hash_syms, &cinfo); | |
6463 | ||
6464 | contents = s->contents + 16; | |
6465 | for (i = 0; i < maskwords; ++i) | |
6466 | { | |
6467 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
6468 | contents); | |
6469 | contents += bed->s->arch_size / 8; | |
6470 | } | |
6471 | ||
6472 | free (cinfo.bitmask); | |
6473 | free (cinfo.hashcodes); | |
6474 | } | |
6475 | } | |
5a580b3a AM |
6476 | |
6477 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
6478 | BFD_ASSERT (s != NULL); | |
6479 | ||
4ad4eba5 | 6480 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 6481 | |
eea6121a | 6482 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
6483 | |
6484 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
6485 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
6486 | return FALSE; | |
6487 | } | |
6488 | ||
6489 | return TRUE; | |
6490 | } | |
4d269e42 AM |
6491 | \f |
6492 | /* Indicate that we are only retrieving symbol values from this | |
6493 | section. */ | |
6494 | ||
6495 | void | |
6496 | _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info) | |
6497 | { | |
6498 | if (is_elf_hash_table (info->hash)) | |
6499 | sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS; | |
6500 | _bfd_generic_link_just_syms (sec, info); | |
6501 | } | |
6502 | ||
6503 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ | |
6504 | ||
6505 | static void | |
6506 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
6507 | asection *sec) | |
6508 | { | |
6509 | BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE); | |
6510 | sec->sec_info_type = ELF_INFO_TYPE_NONE; | |
6511 | } | |
6512 | ||
6513 | /* Finish SHF_MERGE section merging. */ | |
6514 | ||
6515 | bfd_boolean | |
6516 | _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info) | |
6517 | { | |
6518 | bfd *ibfd; | |
6519 | asection *sec; | |
6520 | ||
6521 | if (!is_elf_hash_table (info->hash)) | |
6522 | return FALSE; | |
6523 | ||
6524 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
6525 | if ((ibfd->flags & DYNAMIC) == 0) | |
6526 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) | |
6527 | if ((sec->flags & SEC_MERGE) != 0 | |
6528 | && !bfd_is_abs_section (sec->output_section)) | |
6529 | { | |
6530 | struct bfd_elf_section_data *secdata; | |
6531 | ||
6532 | secdata = elf_section_data (sec); | |
6533 | if (! _bfd_add_merge_section (abfd, | |
6534 | &elf_hash_table (info)->merge_info, | |
6535 | sec, &secdata->sec_info)) | |
6536 | return FALSE; | |
6537 | else if (secdata->sec_info) | |
6538 | sec->sec_info_type = ELF_INFO_TYPE_MERGE; | |
6539 | } | |
6540 | ||
6541 | if (elf_hash_table (info)->merge_info != NULL) | |
6542 | _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info, | |
6543 | merge_sections_remove_hook); | |
6544 | return TRUE; | |
6545 | } | |
6546 | ||
6547 | /* Create an entry in an ELF linker hash table. */ | |
6548 | ||
6549 | struct bfd_hash_entry * | |
6550 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
6551 | struct bfd_hash_table *table, | |
6552 | const char *string) | |
6553 | { | |
6554 | /* Allocate the structure if it has not already been allocated by a | |
6555 | subclass. */ | |
6556 | if (entry == NULL) | |
6557 | { | |
6558 | entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); | |
6559 | if (entry == NULL) | |
6560 | return entry; | |
6561 | } | |
6562 | ||
6563 | /* Call the allocation method of the superclass. */ | |
6564 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
6565 | if (entry != NULL) | |
6566 | { | |
6567 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
6568 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
6569 | ||
6570 | /* Set local fields. */ | |
6571 | ret->indx = -1; | |
6572 | ret->dynindx = -1; | |
6573 | ret->got = htab->init_got_refcount; | |
6574 | ret->plt = htab->init_plt_refcount; | |
6575 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
6576 | - offsetof (struct elf_link_hash_entry, size))); | |
6577 | /* Assume that we have been called by a non-ELF symbol reader. | |
6578 | This flag is then reset by the code which reads an ELF input | |
6579 | file. This ensures that a symbol created by a non-ELF symbol | |
6580 | reader will have the flag set correctly. */ | |
6581 | ret->non_elf = 1; | |
6582 | } | |
6583 | ||
6584 | return entry; | |
6585 | } | |
6586 | ||
6587 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
6588 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
6589 | ||
6590 | void | |
6591 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
6592 | struct elf_link_hash_entry *dir, | |
6593 | struct elf_link_hash_entry *ind) | |
6594 | { | |
6595 | struct elf_link_hash_table *htab; | |
6596 | ||
6597 | /* Copy down any references that we may have already seen to the | |
6598 | symbol which just became indirect. */ | |
6599 | ||
6600 | dir->ref_dynamic |= ind->ref_dynamic; | |
6601 | dir->ref_regular |= ind->ref_regular; | |
6602 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
6603 | dir->non_got_ref |= ind->non_got_ref; | |
6604 | dir->needs_plt |= ind->needs_plt; | |
6605 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
6606 | ||
6607 | if (ind->root.type != bfd_link_hash_indirect) | |
6608 | return; | |
6609 | ||
6610 | /* Copy over the global and procedure linkage table refcount entries. | |
6611 | These may have been already set up by a check_relocs routine. */ | |
6612 | htab = elf_hash_table (info); | |
6613 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
6614 | { | |
6615 | if (dir->got.refcount < 0) | |
6616 | dir->got.refcount = 0; | |
6617 | dir->got.refcount += ind->got.refcount; | |
6618 | ind->got.refcount = htab->init_got_refcount.refcount; | |
6619 | } | |
6620 | ||
6621 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
6622 | { | |
6623 | if (dir->plt.refcount < 0) | |
6624 | dir->plt.refcount = 0; | |
6625 | dir->plt.refcount += ind->plt.refcount; | |
6626 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
6627 | } | |
6628 | ||
6629 | if (ind->dynindx != -1) | |
6630 | { | |
6631 | if (dir->dynindx != -1) | |
6632 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
6633 | dir->dynindx = ind->dynindx; | |
6634 | dir->dynstr_index = ind->dynstr_index; | |
6635 | ind->dynindx = -1; | |
6636 | ind->dynstr_index = 0; | |
6637 | } | |
6638 | } | |
6639 | ||
6640 | void | |
6641 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
6642 | struct elf_link_hash_entry *h, | |
6643 | bfd_boolean force_local) | |
6644 | { | |
6645 | h->plt = elf_hash_table (info)->init_plt_offset; | |
6646 | h->needs_plt = 0; | |
6647 | if (force_local) | |
6648 | { | |
6649 | h->forced_local = 1; | |
6650 | if (h->dynindx != -1) | |
6651 | { | |
6652 | h->dynindx = -1; | |
6653 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, | |
6654 | h->dynstr_index); | |
6655 | } | |
6656 | } | |
6657 | } | |
6658 | ||
6659 | /* Initialize an ELF linker hash table. */ | |
6660 | ||
6661 | bfd_boolean | |
6662 | _bfd_elf_link_hash_table_init | |
6663 | (struct elf_link_hash_table *table, | |
6664 | bfd *abfd, | |
6665 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
6666 | struct bfd_hash_table *, | |
6667 | const char *), | |
6668 | unsigned int entsize) | |
6669 | { | |
6670 | bfd_boolean ret; | |
6671 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; | |
6672 | ||
6673 | memset (table, 0, sizeof * table); | |
6674 | table->init_got_refcount.refcount = can_refcount - 1; | |
6675 | table->init_plt_refcount.refcount = can_refcount - 1; | |
6676 | table->init_got_offset.offset = -(bfd_vma) 1; | |
6677 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
6678 | /* The first dynamic symbol is a dummy. */ | |
6679 | table->dynsymcount = 1; | |
6680 | ||
6681 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
6682 | table->root.type = bfd_link_elf_hash_table; | |
6683 | ||
6684 | return ret; | |
6685 | } | |
6686 | ||
6687 | /* Create an ELF linker hash table. */ | |
6688 | ||
6689 | struct bfd_link_hash_table * | |
6690 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
6691 | { | |
6692 | struct elf_link_hash_table *ret; | |
6693 | bfd_size_type amt = sizeof (struct elf_link_hash_table); | |
6694 | ||
6695 | ret = bfd_malloc (amt); | |
6696 | if (ret == NULL) | |
6697 | return NULL; | |
6698 | ||
6699 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
6700 | sizeof (struct elf_link_hash_entry))) | |
6701 | { | |
6702 | free (ret); | |
6703 | return NULL; | |
6704 | } | |
6705 | ||
6706 | return &ret->root; | |
6707 | } | |
6708 | ||
6709 | /* This is a hook for the ELF emulation code in the generic linker to | |
6710 | tell the backend linker what file name to use for the DT_NEEDED | |
6711 | entry for a dynamic object. */ | |
6712 | ||
6713 | void | |
6714 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
6715 | { | |
6716 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6717 | && bfd_get_format (abfd) == bfd_object) | |
6718 | elf_dt_name (abfd) = name; | |
6719 | } | |
6720 | ||
6721 | int | |
6722 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
6723 | { | |
6724 | int lib_class; | |
6725 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6726 | && bfd_get_format (abfd) == bfd_object) | |
6727 | lib_class = elf_dyn_lib_class (abfd); | |
6728 | else | |
6729 | lib_class = 0; | |
6730 | return lib_class; | |
6731 | } | |
6732 | ||
6733 | void | |
6734 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
6735 | { | |
6736 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6737 | && bfd_get_format (abfd) == bfd_object) | |
6738 | elf_dyn_lib_class (abfd) = lib_class; | |
6739 | } | |
6740 | ||
6741 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
6742 | the linker ELF emulation code. */ | |
6743 | ||
6744 | struct bfd_link_needed_list * | |
6745 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
6746 | struct bfd_link_info *info) | |
6747 | { | |
6748 | if (! is_elf_hash_table (info->hash)) | |
6749 | return NULL; | |
6750 | return elf_hash_table (info)->needed; | |
6751 | } | |
6752 | ||
6753 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
6754 | hook for the linker ELF emulation code. */ | |
6755 | ||
6756 | struct bfd_link_needed_list * | |
6757 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
6758 | struct bfd_link_info *info) | |
6759 | { | |
6760 | if (! is_elf_hash_table (info->hash)) | |
6761 | return NULL; | |
6762 | return elf_hash_table (info)->runpath; | |
6763 | } | |
6764 | ||
6765 | /* Get the name actually used for a dynamic object for a link. This | |
6766 | is the SONAME entry if there is one. Otherwise, it is the string | |
6767 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
6768 | ||
6769 | const char * | |
6770 | bfd_elf_get_dt_soname (bfd *abfd) | |
6771 | { | |
6772 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6773 | && bfd_get_format (abfd) == bfd_object) | |
6774 | return elf_dt_name (abfd); | |
6775 | return NULL; | |
6776 | } | |
6777 | ||
6778 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
6779 | the ELF linker emulation code. */ | |
6780 | ||
6781 | bfd_boolean | |
6782 | bfd_elf_get_bfd_needed_list (bfd *abfd, | |
6783 | struct bfd_link_needed_list **pneeded) | |
6784 | { | |
6785 | asection *s; | |
6786 | bfd_byte *dynbuf = NULL; | |
cb33740c | 6787 | unsigned int elfsec; |
4d269e42 AM |
6788 | unsigned long shlink; |
6789 | bfd_byte *extdyn, *extdynend; | |
6790 | size_t extdynsize; | |
6791 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
6792 | ||
6793 | *pneeded = NULL; | |
6794 | ||
6795 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
6796 | || bfd_get_format (abfd) != bfd_object) | |
6797 | return TRUE; | |
6798 | ||
6799 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
6800 | if (s == NULL || s->size == 0) | |
6801 | return TRUE; | |
6802 | ||
6803 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
6804 | goto error_return; | |
6805 | ||
6806 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 6807 | if (elfsec == SHN_BAD) |
4d269e42 AM |
6808 | goto error_return; |
6809 | ||
6810 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 6811 | |
4d269e42 AM |
6812 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
6813 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
6814 | ||
6815 | extdyn = dynbuf; | |
6816 | extdynend = extdyn + s->size; | |
6817 | for (; extdyn < extdynend; extdyn += extdynsize) | |
6818 | { | |
6819 | Elf_Internal_Dyn dyn; | |
6820 | ||
6821 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
6822 | ||
6823 | if (dyn.d_tag == DT_NULL) | |
6824 | break; | |
6825 | ||
6826 | if (dyn.d_tag == DT_NEEDED) | |
6827 | { | |
6828 | const char *string; | |
6829 | struct bfd_link_needed_list *l; | |
6830 | unsigned int tagv = dyn.d_un.d_val; | |
6831 | bfd_size_type amt; | |
6832 | ||
6833 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
6834 | if (string == NULL) | |
6835 | goto error_return; | |
6836 | ||
6837 | amt = sizeof *l; | |
6838 | l = bfd_alloc (abfd, amt); | |
6839 | if (l == NULL) | |
6840 | goto error_return; | |
6841 | ||
6842 | l->by = abfd; | |
6843 | l->name = string; | |
6844 | l->next = *pneeded; | |
6845 | *pneeded = l; | |
6846 | } | |
6847 | } | |
6848 | ||
6849 | free (dynbuf); | |
6850 | ||
6851 | return TRUE; | |
6852 | ||
6853 | error_return: | |
6854 | if (dynbuf != NULL) | |
6855 | free (dynbuf); | |
6856 | return FALSE; | |
6857 | } | |
6858 | ||
6859 | struct elf_symbuf_symbol | |
6860 | { | |
6861 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
6862 | unsigned char st_info; /* Type and binding attributes */ | |
6863 | unsigned char st_other; /* Visibilty, and target specific */ | |
6864 | }; | |
6865 | ||
6866 | struct elf_symbuf_head | |
6867 | { | |
6868 | struct elf_symbuf_symbol *ssym; | |
6869 | bfd_size_type count; | |
6870 | unsigned int st_shndx; | |
6871 | }; | |
6872 | ||
6873 | struct elf_symbol | |
6874 | { | |
6875 | union | |
6876 | { | |
6877 | Elf_Internal_Sym *isym; | |
6878 | struct elf_symbuf_symbol *ssym; | |
6879 | } u; | |
6880 | const char *name; | |
6881 | }; | |
6882 | ||
6883 | /* Sort references to symbols by ascending section number. */ | |
6884 | ||
6885 | static int | |
6886 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
6887 | { | |
6888 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
6889 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
6890 | ||
6891 | return s1->st_shndx - s2->st_shndx; | |
6892 | } | |
6893 | ||
6894 | static int | |
6895 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
6896 | { | |
6897 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
6898 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
6899 | return strcmp (s1->name, s2->name); | |
6900 | } | |
6901 | ||
6902 | static struct elf_symbuf_head * | |
6903 | elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf) | |
6904 | { | |
14b1c01e | 6905 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
6906 | struct elf_symbuf_symbol *ssym; |
6907 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
3ae181ee | 6908 | bfd_size_type i, shndx_count, total_size; |
4d269e42 | 6909 | |
14b1c01e | 6910 | indbuf = bfd_malloc2 (symcount, sizeof (*indbuf)); |
4d269e42 AM |
6911 | if (indbuf == NULL) |
6912 | return NULL; | |
6913 | ||
6914 | for (ind = indbuf, i = 0; i < symcount; i++) | |
6915 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
6916 | *ind++ = &isymbuf[i]; | |
6917 | indbufend = ind; | |
6918 | ||
6919 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
6920 | elf_sort_elf_symbol); | |
6921 | ||
6922 | shndx_count = 0; | |
6923 | if (indbufend > indbuf) | |
6924 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
6925 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
6926 | shndx_count++; | |
6927 | ||
3ae181ee L |
6928 | total_size = ((shndx_count + 1) * sizeof (*ssymbuf) |
6929 | + (indbufend - indbuf) * sizeof (*ssym)); | |
6930 | ssymbuf = bfd_malloc (total_size); | |
4d269e42 AM |
6931 | if (ssymbuf == NULL) |
6932 | { | |
6933 | free (indbuf); | |
6934 | return NULL; | |
6935 | } | |
6936 | ||
3ae181ee | 6937 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); |
4d269e42 AM |
6938 | ssymbuf->ssym = NULL; |
6939 | ssymbuf->count = shndx_count; | |
6940 | ssymbuf->st_shndx = 0; | |
6941 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
6942 | { | |
6943 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
6944 | { | |
6945 | ssymhead++; | |
6946 | ssymhead->ssym = ssym; | |
6947 | ssymhead->count = 0; | |
6948 | ssymhead->st_shndx = (*ind)->st_shndx; | |
6949 | } | |
6950 | ssym->st_name = (*ind)->st_name; | |
6951 | ssym->st_info = (*ind)->st_info; | |
6952 | ssym->st_other = (*ind)->st_other; | |
6953 | ssymhead->count++; | |
6954 | } | |
3ae181ee L |
6955 | BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count |
6956 | && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) | |
6957 | == total_size)); | |
4d269e42 AM |
6958 | |
6959 | free (indbuf); | |
6960 | return ssymbuf; | |
6961 | } | |
6962 | ||
6963 | /* Check if 2 sections define the same set of local and global | |
6964 | symbols. */ | |
6965 | ||
8f317e31 | 6966 | static bfd_boolean |
4d269e42 AM |
6967 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
6968 | struct bfd_link_info *info) | |
6969 | { | |
6970 | bfd *bfd1, *bfd2; | |
6971 | const struct elf_backend_data *bed1, *bed2; | |
6972 | Elf_Internal_Shdr *hdr1, *hdr2; | |
6973 | bfd_size_type symcount1, symcount2; | |
6974 | Elf_Internal_Sym *isymbuf1, *isymbuf2; | |
6975 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
6976 | Elf_Internal_Sym *isym, *isymend; | |
6977 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
6978 | bfd_size_type count1, count2, i; | |
cb33740c | 6979 | unsigned int shndx1, shndx2; |
4d269e42 AM |
6980 | bfd_boolean result; |
6981 | ||
6982 | bfd1 = sec1->owner; | |
6983 | bfd2 = sec2->owner; | |
6984 | ||
4d269e42 AM |
6985 | /* Both sections have to be in ELF. */ |
6986 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
6987 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
6988 | return FALSE; | |
6989 | ||
6990 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
6991 | return FALSE; | |
6992 | ||
4d269e42 AM |
6993 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
6994 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
cb33740c | 6995 | if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) |
4d269e42 AM |
6996 | return FALSE; |
6997 | ||
6998 | bed1 = get_elf_backend_data (bfd1); | |
6999 | bed2 = get_elf_backend_data (bfd2); | |
7000 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
7001 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
7002 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
7003 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
7004 | ||
7005 | if (symcount1 == 0 || symcount2 == 0) | |
7006 | return FALSE; | |
7007 | ||
7008 | result = FALSE; | |
7009 | isymbuf1 = NULL; | |
7010 | isymbuf2 = NULL; | |
7011 | ssymbuf1 = elf_tdata (bfd1)->symbuf; | |
7012 | ssymbuf2 = elf_tdata (bfd2)->symbuf; | |
7013 | ||
7014 | if (ssymbuf1 == NULL) | |
7015 | { | |
7016 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
7017 | NULL, NULL, NULL); | |
7018 | if (isymbuf1 == NULL) | |
7019 | goto done; | |
7020 | ||
7021 | if (!info->reduce_memory_overheads) | |
7022 | elf_tdata (bfd1)->symbuf = ssymbuf1 | |
7023 | = elf_create_symbuf (symcount1, isymbuf1); | |
7024 | } | |
7025 | ||
7026 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
7027 | { | |
7028 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
7029 | NULL, NULL, NULL); | |
7030 | if (isymbuf2 == NULL) | |
7031 | goto done; | |
7032 | ||
7033 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
7034 | elf_tdata (bfd2)->symbuf = ssymbuf2 | |
7035 | = elf_create_symbuf (symcount2, isymbuf2); | |
7036 | } | |
7037 | ||
7038 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
7039 | { | |
7040 | /* Optimized faster version. */ | |
7041 | bfd_size_type lo, hi, mid; | |
7042 | struct elf_symbol *symp; | |
7043 | struct elf_symbuf_symbol *ssym, *ssymend; | |
7044 | ||
7045 | lo = 0; | |
7046 | hi = ssymbuf1->count; | |
7047 | ssymbuf1++; | |
7048 | count1 = 0; | |
7049 | while (lo < hi) | |
7050 | { | |
7051 | mid = (lo + hi) / 2; | |
cb33740c | 7052 | if (shndx1 < ssymbuf1[mid].st_shndx) |
4d269e42 | 7053 | hi = mid; |
cb33740c | 7054 | else if (shndx1 > ssymbuf1[mid].st_shndx) |
4d269e42 AM |
7055 | lo = mid + 1; |
7056 | else | |
7057 | { | |
7058 | count1 = ssymbuf1[mid].count; | |
7059 | ssymbuf1 += mid; | |
7060 | break; | |
7061 | } | |
7062 | } | |
7063 | ||
7064 | lo = 0; | |
7065 | hi = ssymbuf2->count; | |
7066 | ssymbuf2++; | |
7067 | count2 = 0; | |
7068 | while (lo < hi) | |
7069 | { | |
7070 | mid = (lo + hi) / 2; | |
cb33740c | 7071 | if (shndx2 < ssymbuf2[mid].st_shndx) |
4d269e42 | 7072 | hi = mid; |
cb33740c | 7073 | else if (shndx2 > ssymbuf2[mid].st_shndx) |
4d269e42 AM |
7074 | lo = mid + 1; |
7075 | else | |
7076 | { | |
7077 | count2 = ssymbuf2[mid].count; | |
7078 | ssymbuf2 += mid; | |
7079 | break; | |
7080 | } | |
7081 | } | |
7082 | ||
7083 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7084 | goto done; | |
7085 | ||
7086 | symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol)); | |
7087 | symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol)); | |
7088 | if (symtable1 == NULL || symtable2 == NULL) | |
7089 | goto done; | |
7090 | ||
7091 | symp = symtable1; | |
7092 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
7093 | ssym < ssymend; ssym++, symp++) | |
7094 | { | |
7095 | symp->u.ssym = ssym; | |
7096 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
7097 | hdr1->sh_link, | |
7098 | ssym->st_name); | |
7099 | } | |
7100 | ||
7101 | symp = symtable2; | |
7102 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
7103 | ssym < ssymend; ssym++, symp++) | |
7104 | { | |
7105 | symp->u.ssym = ssym; | |
7106 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
7107 | hdr2->sh_link, | |
7108 | ssym->st_name); | |
7109 | } | |
7110 | ||
7111 | /* Sort symbol by name. */ | |
7112 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7113 | elf_sym_name_compare); | |
7114 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7115 | elf_sym_name_compare); | |
7116 | ||
7117 | for (i = 0; i < count1; i++) | |
7118 | /* Two symbols must have the same binding, type and name. */ | |
7119 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
7120 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
7121 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7122 | goto done; | |
7123 | ||
7124 | result = TRUE; | |
7125 | goto done; | |
7126 | } | |
7127 | ||
7128 | symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
7129 | symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
7130 | if (symtable1 == NULL || symtable2 == NULL) | |
7131 | goto done; | |
7132 | ||
7133 | /* Count definitions in the section. */ | |
7134 | count1 = 0; | |
7135 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
cb33740c | 7136 | if (isym->st_shndx == shndx1) |
4d269e42 AM |
7137 | symtable1[count1++].u.isym = isym; |
7138 | ||
7139 | count2 = 0; | |
7140 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
cb33740c | 7141 | if (isym->st_shndx == shndx2) |
4d269e42 AM |
7142 | symtable2[count2++].u.isym = isym; |
7143 | ||
7144 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7145 | goto done; | |
7146 | ||
7147 | for (i = 0; i < count1; i++) | |
7148 | symtable1[i].name | |
7149 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
7150 | symtable1[i].u.isym->st_name); | |
7151 | ||
7152 | for (i = 0; i < count2; i++) | |
7153 | symtable2[i].name | |
7154 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
7155 | symtable2[i].u.isym->st_name); | |
7156 | ||
7157 | /* Sort symbol by name. */ | |
7158 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7159 | elf_sym_name_compare); | |
7160 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7161 | elf_sym_name_compare); | |
7162 | ||
7163 | for (i = 0; i < count1; i++) | |
7164 | /* Two symbols must have the same binding, type and name. */ | |
7165 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
7166 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
7167 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7168 | goto done; | |
7169 | ||
7170 | result = TRUE; | |
7171 | ||
7172 | done: | |
7173 | if (symtable1) | |
7174 | free (symtable1); | |
7175 | if (symtable2) | |
7176 | free (symtable2); | |
7177 | if (isymbuf1) | |
7178 | free (isymbuf1); | |
7179 | if (isymbuf2) | |
7180 | free (isymbuf2); | |
7181 | ||
7182 | return result; | |
7183 | } | |
7184 | ||
7185 | /* Return TRUE if 2 section types are compatible. */ | |
7186 | ||
7187 | bfd_boolean | |
7188 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
7189 | bfd *bbfd, const asection *bsec) | |
7190 | { | |
7191 | if (asec == NULL | |
7192 | || bsec == NULL | |
7193 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
7194 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
7195 | return TRUE; | |
7196 | ||
7197 | return elf_section_type (asec) == elf_section_type (bsec); | |
7198 | } | |
7199 | \f | |
c152c796 AM |
7200 | /* Final phase of ELF linker. */ |
7201 | ||
7202 | /* A structure we use to avoid passing large numbers of arguments. */ | |
7203 | ||
7204 | struct elf_final_link_info | |
7205 | { | |
7206 | /* General link information. */ | |
7207 | struct bfd_link_info *info; | |
7208 | /* Output BFD. */ | |
7209 | bfd *output_bfd; | |
7210 | /* Symbol string table. */ | |
7211 | struct bfd_strtab_hash *symstrtab; | |
7212 | /* .dynsym section. */ | |
7213 | asection *dynsym_sec; | |
7214 | /* .hash section. */ | |
7215 | asection *hash_sec; | |
7216 | /* symbol version section (.gnu.version). */ | |
7217 | asection *symver_sec; | |
7218 | /* Buffer large enough to hold contents of any section. */ | |
7219 | bfd_byte *contents; | |
7220 | /* Buffer large enough to hold external relocs of any section. */ | |
7221 | void *external_relocs; | |
7222 | /* Buffer large enough to hold internal relocs of any section. */ | |
7223 | Elf_Internal_Rela *internal_relocs; | |
7224 | /* Buffer large enough to hold external local symbols of any input | |
7225 | BFD. */ | |
7226 | bfd_byte *external_syms; | |
7227 | /* And a buffer for symbol section indices. */ | |
7228 | Elf_External_Sym_Shndx *locsym_shndx; | |
7229 | /* Buffer large enough to hold internal local symbols of any input | |
7230 | BFD. */ | |
7231 | Elf_Internal_Sym *internal_syms; | |
7232 | /* Array large enough to hold a symbol index for each local symbol | |
7233 | of any input BFD. */ | |
7234 | long *indices; | |
7235 | /* Array large enough to hold a section pointer for each local | |
7236 | symbol of any input BFD. */ | |
7237 | asection **sections; | |
7238 | /* Buffer to hold swapped out symbols. */ | |
7239 | bfd_byte *symbuf; | |
7240 | /* And one for symbol section indices. */ | |
7241 | Elf_External_Sym_Shndx *symshndxbuf; | |
7242 | /* Number of swapped out symbols in buffer. */ | |
7243 | size_t symbuf_count; | |
7244 | /* Number of symbols which fit in symbuf. */ | |
7245 | size_t symbuf_size; | |
7246 | /* And same for symshndxbuf. */ | |
7247 | size_t shndxbuf_size; | |
7248 | }; | |
7249 | ||
7250 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
7251 | ||
7252 | struct elf_outext_info | |
7253 | { | |
7254 | bfd_boolean failed; | |
7255 | bfd_boolean localsyms; | |
7256 | struct elf_final_link_info *finfo; | |
7257 | }; | |
7258 | ||
d9352518 DB |
7259 | |
7260 | /* Support for evaluating a complex relocation. | |
7261 | ||
7262 | Complex relocations are generalized, self-describing relocations. The | |
7263 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 7264 | relocations themselves. |
d9352518 DB |
7265 | |
7266 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
7267 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
7268 | information (start bit, end bit, word width, etc) into the addend. This | |
7269 | information is extracted from CGEN-generated operand tables within gas. | |
7270 | ||
7271 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
7272 | internal) representing prefix-notation expressions, including but not | |
7273 | limited to those sorts of expressions normally encoded as addends in the | |
7274 | addend field. The symbol mangling format is: | |
7275 | ||
7276 | <node> := <literal> | |
7277 | | <unary-operator> ':' <node> | |
7278 | | <binary-operator> ':' <node> ':' <node> | |
7279 | ; | |
7280 | ||
7281 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
7282 | | 'S' <digits=N> ':' <N character section name> | |
7283 | | '#' <hexdigits> | |
7284 | ; | |
7285 | ||
7286 | <binary-operator> := as in C | |
7287 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
7288 | ||
7289 | static void | |
a0c8462f AM |
7290 | set_symbol_value (bfd *bfd_with_globals, |
7291 | Elf_Internal_Sym *isymbuf, | |
7292 | size_t locsymcount, | |
7293 | size_t symidx, | |
7294 | bfd_vma val) | |
d9352518 | 7295 | { |
8977835c AM |
7296 | struct elf_link_hash_entry **sym_hashes; |
7297 | struct elf_link_hash_entry *h; | |
7298 | size_t extsymoff = locsymcount; | |
d9352518 | 7299 | |
8977835c | 7300 | if (symidx < locsymcount) |
d9352518 | 7301 | { |
8977835c AM |
7302 | Elf_Internal_Sym *sym; |
7303 | ||
7304 | sym = isymbuf + symidx; | |
7305 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
7306 | { | |
7307 | /* It is a local symbol: move it to the | |
7308 | "absolute" section and give it a value. */ | |
7309 | sym->st_shndx = SHN_ABS; | |
7310 | sym->st_value = val; | |
7311 | return; | |
7312 | } | |
7313 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
7314 | extsymoff = 0; | |
d9352518 | 7315 | } |
8977835c AM |
7316 | |
7317 | /* It is a global symbol: set its link type | |
7318 | to "defined" and give it a value. */ | |
7319 | ||
7320 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
7321 | h = sym_hashes [symidx - extsymoff]; | |
7322 | while (h->root.type == bfd_link_hash_indirect | |
7323 | || h->root.type == bfd_link_hash_warning) | |
7324 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7325 | h->root.type = bfd_link_hash_defined; | |
7326 | h->root.u.def.value = val; | |
7327 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
7328 | } |
7329 | ||
a0c8462f AM |
7330 | static bfd_boolean |
7331 | resolve_symbol (const char *name, | |
7332 | bfd *input_bfd, | |
7333 | struct elf_final_link_info *finfo, | |
7334 | bfd_vma *result, | |
7335 | Elf_Internal_Sym *isymbuf, | |
7336 | size_t locsymcount) | |
d9352518 | 7337 | { |
a0c8462f AM |
7338 | Elf_Internal_Sym *sym; |
7339 | struct bfd_link_hash_entry *global_entry; | |
7340 | const char *candidate = NULL; | |
7341 | Elf_Internal_Shdr *symtab_hdr; | |
7342 | size_t i; | |
7343 | ||
d9352518 DB |
7344 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
7345 | ||
7346 | for (i = 0; i < locsymcount; ++ i) | |
7347 | { | |
8977835c | 7348 | sym = isymbuf + i; |
d9352518 DB |
7349 | |
7350 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
7351 | continue; | |
7352 | ||
7353 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
7354 | symtab_hdr->sh_link, | |
7355 | sym->st_name); | |
7356 | #ifdef DEBUG | |
0f02bbd9 AM |
7357 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
7358 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
7359 | #endif |
7360 | if (candidate && strcmp (candidate, name) == 0) | |
7361 | { | |
0f02bbd9 | 7362 | asection *sec = finfo->sections [i]; |
d9352518 | 7363 | |
0f02bbd9 AM |
7364 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
7365 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 7366 | #ifdef DEBUG |
0f02bbd9 AM |
7367 | printf ("Found symbol with value %8.8lx\n", |
7368 | (unsigned long) *result); | |
d9352518 DB |
7369 | #endif |
7370 | return TRUE; | |
7371 | } | |
7372 | } | |
7373 | ||
7374 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
a0c8462f AM |
7375 | global_entry = bfd_link_hash_lookup (finfo->info->hash, name, |
7376 | FALSE, FALSE, TRUE); | |
d9352518 DB |
7377 | if (!global_entry) |
7378 | return FALSE; | |
a0c8462f | 7379 | |
d9352518 DB |
7380 | if (global_entry->type == bfd_link_hash_defined |
7381 | || global_entry->type == bfd_link_hash_defweak) | |
7382 | { | |
a0c8462f AM |
7383 | *result = (global_entry->u.def.value |
7384 | + global_entry->u.def.section->output_section->vma | |
7385 | + global_entry->u.def.section->output_offset); | |
d9352518 | 7386 | #ifdef DEBUG |
0f02bbd9 AM |
7387 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
7388 | global_entry->root.string, (unsigned long) *result); | |
d9352518 DB |
7389 | #endif |
7390 | return TRUE; | |
a0c8462f | 7391 | } |
d9352518 | 7392 | |
d9352518 DB |
7393 | return FALSE; |
7394 | } | |
7395 | ||
7396 | static bfd_boolean | |
a0c8462f AM |
7397 | resolve_section (const char *name, |
7398 | asection *sections, | |
7399 | bfd_vma *result) | |
d9352518 | 7400 | { |
a0c8462f AM |
7401 | asection *curr; |
7402 | unsigned int len; | |
d9352518 | 7403 | |
a0c8462f | 7404 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
7405 | if (strcmp (curr->name, name) == 0) |
7406 | { | |
7407 | *result = curr->vma; | |
7408 | return TRUE; | |
7409 | } | |
7410 | ||
7411 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
a0c8462f | 7412 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
7413 | { |
7414 | len = strlen (curr->name); | |
a0c8462f | 7415 | if (len > strlen (name)) |
d9352518 DB |
7416 | continue; |
7417 | ||
7418 | if (strncmp (curr->name, name, len) == 0) | |
7419 | { | |
7420 | if (strncmp (".end", name + len, 4) == 0) | |
7421 | { | |
7422 | *result = curr->vma + curr->size; | |
7423 | return TRUE; | |
7424 | } | |
7425 | ||
7426 | /* Insert more pseudo-section names here, if you like. */ | |
7427 | } | |
7428 | } | |
a0c8462f | 7429 | |
d9352518 DB |
7430 | return FALSE; |
7431 | } | |
7432 | ||
7433 | static void | |
a0c8462f | 7434 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 7435 | { |
a0c8462f AM |
7436 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
7437 | reftype, name); | |
d9352518 DB |
7438 | } |
7439 | ||
7440 | static bfd_boolean | |
a0c8462f AM |
7441 | eval_symbol (bfd_vma *result, |
7442 | const char **symp, | |
7443 | bfd *input_bfd, | |
7444 | struct elf_final_link_info *finfo, | |
7445 | bfd_vma dot, | |
7446 | Elf_Internal_Sym *isymbuf, | |
7447 | size_t locsymcount, | |
7448 | int signed_p) | |
d9352518 | 7449 | { |
4b93929b NC |
7450 | size_t len; |
7451 | size_t symlen; | |
a0c8462f AM |
7452 | bfd_vma a; |
7453 | bfd_vma b; | |
4b93929b | 7454 | char symbuf[4096]; |
0f02bbd9 | 7455 | const char *sym = *symp; |
a0c8462f AM |
7456 | const char *symend; |
7457 | bfd_boolean symbol_is_section = FALSE; | |
d9352518 DB |
7458 | |
7459 | len = strlen (sym); | |
7460 | symend = sym + len; | |
7461 | ||
4b93929b | 7462 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
7463 | { |
7464 | bfd_set_error (bfd_error_invalid_operation); | |
7465 | return FALSE; | |
7466 | } | |
a0c8462f | 7467 | |
d9352518 DB |
7468 | switch (* sym) |
7469 | { | |
7470 | case '.': | |
0f02bbd9 AM |
7471 | *result = dot; |
7472 | *symp = sym + 1; | |
d9352518 DB |
7473 | return TRUE; |
7474 | ||
7475 | case '#': | |
0f02bbd9 AM |
7476 | ++sym; |
7477 | *result = strtoul (sym, (char **) symp, 16); | |
d9352518 DB |
7478 | return TRUE; |
7479 | ||
7480 | case 'S': | |
7481 | symbol_is_section = TRUE; | |
a0c8462f | 7482 | case 's': |
0f02bbd9 AM |
7483 | ++sym; |
7484 | symlen = strtol (sym, (char **) symp, 10); | |
7485 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 7486 | |
4b93929b | 7487 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
7488 | { |
7489 | bfd_set_error (bfd_error_invalid_operation); | |
7490 | return FALSE; | |
7491 | } | |
7492 | ||
7493 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 7494 | symbuf[symlen] = '\0'; |
0f02bbd9 | 7495 | *symp = sym + symlen; |
a0c8462f AM |
7496 | |
7497 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
7498 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
7499 | interpretation here; section means "try section first", not "must be a | |
7500 | section", and likewise with symbol. */ | |
7501 | ||
a0c8462f | 7502 | if (symbol_is_section) |
d9352518 | 7503 | { |
8977835c AM |
7504 | if (!resolve_section (symbuf, finfo->output_bfd->sections, result) |
7505 | && !resolve_symbol (symbuf, input_bfd, finfo, result, | |
7506 | isymbuf, locsymcount)) | |
d9352518 DB |
7507 | { |
7508 | undefined_reference ("section", symbuf); | |
7509 | return FALSE; | |
7510 | } | |
a0c8462f AM |
7511 | } |
7512 | else | |
d9352518 | 7513 | { |
8977835c AM |
7514 | if (!resolve_symbol (symbuf, input_bfd, finfo, result, |
7515 | isymbuf, locsymcount) | |
7516 | && !resolve_section (symbuf, finfo->output_bfd->sections, | |
7517 | result)) | |
d9352518 DB |
7518 | { |
7519 | undefined_reference ("symbol", symbuf); | |
7520 | return FALSE; | |
7521 | } | |
7522 | } | |
7523 | ||
7524 | return TRUE; | |
a0c8462f | 7525 | |
d9352518 DB |
7526 | /* All that remains are operators. */ |
7527 | ||
7528 | #define UNARY_OP(op) \ | |
7529 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
7530 | { \ | |
7531 | sym += strlen (#op); \ | |
a0c8462f AM |
7532 | if (*sym == ':') \ |
7533 | ++sym; \ | |
0f02bbd9 AM |
7534 | *symp = sym; \ |
7535 | if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \ | |
7536 | isymbuf, locsymcount, signed_p)) \ | |
a0c8462f AM |
7537 | return FALSE; \ |
7538 | if (signed_p) \ | |
0f02bbd9 | 7539 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
7540 | else \ |
7541 | *result = op a; \ | |
d9352518 DB |
7542 | return TRUE; \ |
7543 | } | |
7544 | ||
7545 | #define BINARY_OP(op) \ | |
7546 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
7547 | { \ | |
7548 | sym += strlen (#op); \ | |
a0c8462f AM |
7549 | if (*sym == ':') \ |
7550 | ++sym; \ | |
0f02bbd9 AM |
7551 | *symp = sym; \ |
7552 | if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \ | |
7553 | isymbuf, locsymcount, signed_p)) \ | |
a0c8462f | 7554 | return FALSE; \ |
0f02bbd9 AM |
7555 | ++*symp; \ |
7556 | if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \ | |
7557 | isymbuf, locsymcount, signed_p)) \ | |
a0c8462f AM |
7558 | return FALSE; \ |
7559 | if (signed_p) \ | |
0f02bbd9 | 7560 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
7561 | else \ |
7562 | *result = a op b; \ | |
d9352518 DB |
7563 | return TRUE; \ |
7564 | } | |
7565 | ||
7566 | default: | |
7567 | UNARY_OP (0-); | |
7568 | BINARY_OP (<<); | |
7569 | BINARY_OP (>>); | |
7570 | BINARY_OP (==); | |
7571 | BINARY_OP (!=); | |
7572 | BINARY_OP (<=); | |
7573 | BINARY_OP (>=); | |
7574 | BINARY_OP (&&); | |
7575 | BINARY_OP (||); | |
7576 | UNARY_OP (~); | |
7577 | UNARY_OP (!); | |
7578 | BINARY_OP (*); | |
7579 | BINARY_OP (/); | |
7580 | BINARY_OP (%); | |
7581 | BINARY_OP (^); | |
7582 | BINARY_OP (|); | |
7583 | BINARY_OP (&); | |
7584 | BINARY_OP (+); | |
7585 | BINARY_OP (-); | |
7586 | BINARY_OP (<); | |
7587 | BINARY_OP (>); | |
7588 | #undef UNARY_OP | |
7589 | #undef BINARY_OP | |
7590 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
7591 | bfd_set_error (bfd_error_invalid_operation); | |
7592 | return FALSE; | |
7593 | } | |
7594 | } | |
7595 | ||
d9352518 | 7596 | static void |
a0c8462f AM |
7597 | put_value (bfd_vma size, |
7598 | unsigned long chunksz, | |
7599 | bfd *input_bfd, | |
7600 | bfd_vma x, | |
7601 | bfd_byte *location) | |
d9352518 DB |
7602 | { |
7603 | location += (size - chunksz); | |
7604 | ||
a0c8462f | 7605 | for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8)) |
d9352518 DB |
7606 | { |
7607 | switch (chunksz) | |
7608 | { | |
7609 | default: | |
7610 | case 0: | |
7611 | abort (); | |
7612 | case 1: | |
7613 | bfd_put_8 (input_bfd, x, location); | |
7614 | break; | |
7615 | case 2: | |
7616 | bfd_put_16 (input_bfd, x, location); | |
7617 | break; | |
7618 | case 4: | |
7619 | bfd_put_32 (input_bfd, x, location); | |
7620 | break; | |
7621 | case 8: | |
7622 | #ifdef BFD64 | |
7623 | bfd_put_64 (input_bfd, x, location); | |
7624 | #else | |
7625 | abort (); | |
7626 | #endif | |
7627 | break; | |
7628 | } | |
7629 | } | |
7630 | } | |
7631 | ||
a0c8462f AM |
7632 | static bfd_vma |
7633 | get_value (bfd_vma size, | |
7634 | unsigned long chunksz, | |
7635 | bfd *input_bfd, | |
7636 | bfd_byte *location) | |
d9352518 DB |
7637 | { |
7638 | bfd_vma x = 0; | |
7639 | ||
a0c8462f | 7640 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
7641 | { |
7642 | switch (chunksz) | |
7643 | { | |
7644 | default: | |
7645 | case 0: | |
7646 | abort (); | |
7647 | case 1: | |
7648 | x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location); | |
7649 | break; | |
7650 | case 2: | |
7651 | x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location); | |
7652 | break; | |
7653 | case 4: | |
7654 | x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location); | |
7655 | break; | |
7656 | case 8: | |
7657 | #ifdef BFD64 | |
7658 | x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location); | |
7659 | #else | |
7660 | abort (); | |
7661 | #endif | |
7662 | break; | |
7663 | } | |
7664 | } | |
7665 | return x; | |
7666 | } | |
7667 | ||
a0c8462f AM |
7668 | static void |
7669 | decode_complex_addend (unsigned long *start, /* in bits */ | |
7670 | unsigned long *oplen, /* in bits */ | |
7671 | unsigned long *len, /* in bits */ | |
7672 | unsigned long *wordsz, /* in bytes */ | |
7673 | unsigned long *chunksz, /* in bytes */ | |
7674 | unsigned long *lsb0_p, | |
7675 | unsigned long *signed_p, | |
7676 | unsigned long *trunc_p, | |
7677 | unsigned long encoded) | |
d9352518 DB |
7678 | { |
7679 | * start = encoded & 0x3F; | |
7680 | * len = (encoded >> 6) & 0x3F; | |
7681 | * oplen = (encoded >> 12) & 0x3F; | |
7682 | * wordsz = (encoded >> 18) & 0xF; | |
7683 | * chunksz = (encoded >> 22) & 0xF; | |
7684 | * lsb0_p = (encoded >> 27) & 1; | |
7685 | * signed_p = (encoded >> 28) & 1; | |
7686 | * trunc_p = (encoded >> 29) & 1; | |
7687 | } | |
7688 | ||
cdfeee4f | 7689 | bfd_reloc_status_type |
0f02bbd9 | 7690 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
cdfeee4f | 7691 | asection *input_section ATTRIBUTE_UNUSED, |
0f02bbd9 AM |
7692 | bfd_byte *contents, |
7693 | Elf_Internal_Rela *rel, | |
7694 | bfd_vma relocation) | |
d9352518 | 7695 | { |
0f02bbd9 AM |
7696 | bfd_vma shift, x, mask; |
7697 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 7698 | bfd_reloc_status_type r; |
d9352518 DB |
7699 | |
7700 | /* Perform this reloc, since it is complex. | |
7701 | (this is not to say that it necessarily refers to a complex | |
7702 | symbol; merely that it is a self-describing CGEN based reloc. | |
7703 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 7704 | word size, etc) encoded within it.). */ |
d9352518 | 7705 | |
a0c8462f AM |
7706 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
7707 | &chunksz, &lsb0_p, &signed_p, | |
7708 | &trunc_p, rel->r_addend); | |
d9352518 DB |
7709 | |
7710 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
7711 | ||
7712 | if (lsb0_p) | |
7713 | shift = (start + 1) - len; | |
7714 | else | |
7715 | shift = (8 * wordsz) - (start + len); | |
7716 | ||
a0c8462f | 7717 | x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset); |
d9352518 DB |
7718 | |
7719 | #ifdef DEBUG | |
7720 | printf ("Doing complex reloc: " | |
7721 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
7722 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
7723 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
7724 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
7725 | oplen, x, mask, relocation); | |
7726 | #endif | |
7727 | ||
cdfeee4f | 7728 | r = bfd_reloc_ok; |
d9352518 | 7729 | if (! trunc_p) |
cdfeee4f AM |
7730 | /* Now do an overflow check. */ |
7731 | r = bfd_check_overflow ((signed_p | |
7732 | ? complain_overflow_signed | |
7733 | : complain_overflow_unsigned), | |
7734 | len, 0, (8 * wordsz), | |
7735 | relocation); | |
a0c8462f | 7736 | |
d9352518 DB |
7737 | /* Do the deed. */ |
7738 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
7739 | ||
7740 | #ifdef DEBUG | |
7741 | printf (" relocation: %8.8lx\n" | |
7742 | " shifted mask: %8.8lx\n" | |
7743 | " shifted/masked reloc: %8.8lx\n" | |
7744 | " result: %8.8lx\n", | |
a0c8462f | 7745 | relocation, (mask << shift), |
d9352518 DB |
7746 | ((relocation & mask) << shift), x); |
7747 | #endif | |
7748 | put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset); | |
cdfeee4f | 7749 | return r; |
d9352518 DB |
7750 | } |
7751 | ||
c152c796 AM |
7752 | /* When performing a relocatable link, the input relocations are |
7753 | preserved. But, if they reference global symbols, the indices | |
7754 | referenced must be updated. Update all the relocations in | |
7755 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ | |
7756 | ||
7757 | static void | |
7758 | elf_link_adjust_relocs (bfd *abfd, | |
7759 | Elf_Internal_Shdr *rel_hdr, | |
7760 | unsigned int count, | |
7761 | struct elf_link_hash_entry **rel_hash) | |
7762 | { | |
7763 | unsigned int i; | |
7764 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7765 | bfd_byte *erela; | |
7766 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
7767 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
7768 | bfd_vma r_type_mask; | |
7769 | int r_sym_shift; | |
7770 | ||
7771 | if (rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
7772 | { | |
7773 | swap_in = bed->s->swap_reloc_in; | |
7774 | swap_out = bed->s->swap_reloc_out; | |
7775 | } | |
7776 | else if (rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
7777 | { | |
7778 | swap_in = bed->s->swap_reloca_in; | |
7779 | swap_out = bed->s->swap_reloca_out; | |
7780 | } | |
7781 | else | |
7782 | abort (); | |
7783 | ||
7784 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
7785 | abort (); | |
7786 | ||
7787 | if (bed->s->arch_size == 32) | |
7788 | { | |
7789 | r_type_mask = 0xff; | |
7790 | r_sym_shift = 8; | |
7791 | } | |
7792 | else | |
7793 | { | |
7794 | r_type_mask = 0xffffffff; | |
7795 | r_sym_shift = 32; | |
7796 | } | |
7797 | ||
7798 | erela = rel_hdr->contents; | |
7799 | for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) | |
7800 | { | |
7801 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
7802 | unsigned int j; | |
7803 | ||
7804 | if (*rel_hash == NULL) | |
7805 | continue; | |
7806 | ||
7807 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
7808 | ||
7809 | (*swap_in) (abfd, erela, irela); | |
7810 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
7811 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
7812 | | (irela[j].r_info & r_type_mask)); | |
7813 | (*swap_out) (abfd, irela, erela); | |
7814 | } | |
7815 | } | |
7816 | ||
7817 | struct elf_link_sort_rela | |
7818 | { | |
7819 | union { | |
7820 | bfd_vma offset; | |
7821 | bfd_vma sym_mask; | |
7822 | } u; | |
7823 | enum elf_reloc_type_class type; | |
7824 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
7825 | Elf_Internal_Rela rela[1]; | |
7826 | }; | |
7827 | ||
7828 | static int | |
7829 | elf_link_sort_cmp1 (const void *A, const void *B) | |
7830 | { | |
7831 | const struct elf_link_sort_rela *a = A; | |
7832 | const struct elf_link_sort_rela *b = B; | |
7833 | int relativea, relativeb; | |
7834 | ||
7835 | relativea = a->type == reloc_class_relative; | |
7836 | relativeb = b->type == reloc_class_relative; | |
7837 | ||
7838 | if (relativea < relativeb) | |
7839 | return 1; | |
7840 | if (relativea > relativeb) | |
7841 | return -1; | |
7842 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
7843 | return -1; | |
7844 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
7845 | return 1; | |
7846 | if (a->rela->r_offset < b->rela->r_offset) | |
7847 | return -1; | |
7848 | if (a->rela->r_offset > b->rela->r_offset) | |
7849 | return 1; | |
7850 | return 0; | |
7851 | } | |
7852 | ||
7853 | static int | |
7854 | elf_link_sort_cmp2 (const void *A, const void *B) | |
7855 | { | |
7856 | const struct elf_link_sort_rela *a = A; | |
7857 | const struct elf_link_sort_rela *b = B; | |
7858 | int copya, copyb; | |
7859 | ||
7860 | if (a->u.offset < b->u.offset) | |
7861 | return -1; | |
7862 | if (a->u.offset > b->u.offset) | |
7863 | return 1; | |
7864 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); | |
7865 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
7866 | if (copya < copyb) | |
7867 | return -1; | |
7868 | if (copya > copyb) | |
7869 | return 1; | |
7870 | if (a->rela->r_offset < b->rela->r_offset) | |
7871 | return -1; | |
7872 | if (a->rela->r_offset > b->rela->r_offset) | |
7873 | return 1; | |
7874 | return 0; | |
7875 | } | |
7876 | ||
7877 | static size_t | |
7878 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
7879 | { | |
3410fea8 | 7880 | asection *dynamic_relocs; |
fc66a176 L |
7881 | asection *rela_dyn; |
7882 | asection *rel_dyn; | |
c152c796 AM |
7883 | bfd_size_type count, size; |
7884 | size_t i, ret, sort_elt, ext_size; | |
7885 | bfd_byte *sort, *s_non_relative, *p; | |
7886 | struct elf_link_sort_rela *sq; | |
7887 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7888 | int i2e = bed->s->int_rels_per_ext_rel; | |
7889 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
7890 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
7891 | struct bfd_link_order *lo; | |
7892 | bfd_vma r_sym_mask; | |
3410fea8 | 7893 | bfd_boolean use_rela; |
c152c796 | 7894 | |
3410fea8 NC |
7895 | /* Find a dynamic reloc section. */ |
7896 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
7897 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
7898 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
7899 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 7900 | { |
3410fea8 NC |
7901 | bfd_boolean use_rela_initialised = FALSE; |
7902 | ||
7903 | /* This is just here to stop gcc from complaining. | |
7904 | It's initialization checking code is not perfect. */ | |
7905 | use_rela = TRUE; | |
7906 | ||
7907 | /* Both sections are present. Examine the sizes | |
7908 | of the indirect sections to help us choose. */ | |
7909 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
7910 | if (lo->type == bfd_indirect_link_order) | |
7911 | { | |
7912 | asection *o = lo->u.indirect.section; | |
7913 | ||
7914 | if ((o->size % bed->s->sizeof_rela) == 0) | |
7915 | { | |
7916 | if ((o->size % bed->s->sizeof_rel) == 0) | |
7917 | /* Section size is divisible by both rel and rela sizes. | |
7918 | It is of no help to us. */ | |
7919 | ; | |
7920 | else | |
7921 | { | |
7922 | /* Section size is only divisible by rela. */ | |
7923 | if (use_rela_initialised && (use_rela == FALSE)) | |
7924 | { | |
7925 | _bfd_error_handler | |
7926 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7927 | bfd_set_error (bfd_error_invalid_operation); | |
7928 | return 0; | |
7929 | } | |
7930 | else | |
7931 | { | |
7932 | use_rela = TRUE; | |
7933 | use_rela_initialised = TRUE; | |
7934 | } | |
7935 | } | |
7936 | } | |
7937 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
7938 | { | |
7939 | /* Section size is only divisible by rel. */ | |
7940 | if (use_rela_initialised && (use_rela == TRUE)) | |
7941 | { | |
7942 | _bfd_error_handler | |
7943 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7944 | bfd_set_error (bfd_error_invalid_operation); | |
7945 | return 0; | |
7946 | } | |
7947 | else | |
7948 | { | |
7949 | use_rela = FALSE; | |
7950 | use_rela_initialised = TRUE; | |
7951 | } | |
7952 | } | |
7953 | else | |
7954 | { | |
7955 | /* The section size is not divisible by either - something is wrong. */ | |
7956 | _bfd_error_handler | |
7957 | (_("%B: Unable to sort relocs - they are of an unknown size"), abfd); | |
7958 | bfd_set_error (bfd_error_invalid_operation); | |
7959 | return 0; | |
7960 | } | |
7961 | } | |
7962 | ||
7963 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
7964 | if (lo->type == bfd_indirect_link_order) | |
7965 | { | |
7966 | asection *o = lo->u.indirect.section; | |
7967 | ||
7968 | if ((o->size % bed->s->sizeof_rela) == 0) | |
7969 | { | |
7970 | if ((o->size % bed->s->sizeof_rel) == 0) | |
7971 | /* Section size is divisible by both rel and rela sizes. | |
7972 | It is of no help to us. */ | |
7973 | ; | |
7974 | else | |
7975 | { | |
7976 | /* Section size is only divisible by rela. */ | |
7977 | if (use_rela_initialised && (use_rela == FALSE)) | |
7978 | { | |
7979 | _bfd_error_handler | |
7980 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7981 | bfd_set_error (bfd_error_invalid_operation); | |
7982 | return 0; | |
7983 | } | |
7984 | else | |
7985 | { | |
7986 | use_rela = TRUE; | |
7987 | use_rela_initialised = TRUE; | |
7988 | } | |
7989 | } | |
7990 | } | |
7991 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
7992 | { | |
7993 | /* Section size is only divisible by rel. */ | |
7994 | if (use_rela_initialised && (use_rela == TRUE)) | |
7995 | { | |
7996 | _bfd_error_handler | |
7997 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
7998 | bfd_set_error (bfd_error_invalid_operation); | |
7999 | return 0; | |
8000 | } | |
8001 | else | |
8002 | { | |
8003 | use_rela = FALSE; | |
8004 | use_rela_initialised = TRUE; | |
8005 | } | |
8006 | } | |
8007 | else | |
8008 | { | |
8009 | /* The section size is not divisible by either - something is wrong. */ | |
8010 | _bfd_error_handler | |
8011 | (_("%B: Unable to sort relocs - they are of an unknown size"), abfd); | |
8012 | bfd_set_error (bfd_error_invalid_operation); | |
8013 | return 0; | |
8014 | } | |
8015 | } | |
8016 | ||
8017 | if (! use_rela_initialised) | |
8018 | /* Make a guess. */ | |
8019 | use_rela = TRUE; | |
c152c796 | 8020 | } |
fc66a176 L |
8021 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
8022 | use_rela = TRUE; | |
8023 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 8024 | use_rela = FALSE; |
c152c796 | 8025 | else |
fc66a176 | 8026 | return 0; |
3410fea8 NC |
8027 | |
8028 | if (use_rela) | |
c152c796 | 8029 | { |
3410fea8 | 8030 | dynamic_relocs = rela_dyn; |
c152c796 AM |
8031 | ext_size = bed->s->sizeof_rela; |
8032 | swap_in = bed->s->swap_reloca_in; | |
8033 | swap_out = bed->s->swap_reloca_out; | |
8034 | } | |
3410fea8 NC |
8035 | else |
8036 | { | |
8037 | dynamic_relocs = rel_dyn; | |
8038 | ext_size = bed->s->sizeof_rel; | |
8039 | swap_in = bed->s->swap_reloc_in; | |
8040 | swap_out = bed->s->swap_reloc_out; | |
8041 | } | |
c152c796 AM |
8042 | |
8043 | size = 0; | |
3410fea8 | 8044 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 8045 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 8046 | size += lo->u.indirect.section->size; |
c152c796 | 8047 | |
3410fea8 | 8048 | if (size != dynamic_relocs->size) |
c152c796 AM |
8049 | return 0; |
8050 | ||
8051 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
8052 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
8053 | |
8054 | count = dynamic_relocs->size / ext_size; | |
c152c796 | 8055 | sort = bfd_zmalloc (sort_elt * count); |
3410fea8 | 8056 | |
c152c796 AM |
8057 | if (sort == NULL) |
8058 | { | |
8059 | (*info->callbacks->warning) | |
8060 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
8061 | return 0; | |
8062 | } | |
8063 | ||
8064 | if (bed->s->arch_size == 32) | |
8065 | r_sym_mask = ~(bfd_vma) 0xff; | |
8066 | else | |
8067 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
8068 | ||
3410fea8 | 8069 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
8070 | if (lo->type == bfd_indirect_link_order) |
8071 | { | |
8072 | bfd_byte *erel, *erelend; | |
8073 | asection *o = lo->u.indirect.section; | |
8074 | ||
1da212d6 AM |
8075 | if (o->contents == NULL && o->size != 0) |
8076 | { | |
8077 | /* This is a reloc section that is being handled as a normal | |
8078 | section. See bfd_section_from_shdr. We can't combine | |
8079 | relocs in this case. */ | |
8080 | free (sort); | |
8081 | return 0; | |
8082 | } | |
c152c796 | 8083 | erel = o->contents; |
eea6121a | 8084 | erelend = o->contents + o->size; |
c152c796 | 8085 | p = sort + o->output_offset / ext_size * sort_elt; |
3410fea8 | 8086 | |
c152c796 AM |
8087 | while (erel < erelend) |
8088 | { | |
8089 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 8090 | |
c152c796 AM |
8091 | (*swap_in) (abfd, erel, s->rela); |
8092 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
8093 | s->u.sym_mask = r_sym_mask; | |
8094 | p += sort_elt; | |
8095 | erel += ext_size; | |
8096 | } | |
8097 | } | |
8098 | ||
8099 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
8100 | ||
8101 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
8102 | { | |
8103 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
8104 | if (s->type != reloc_class_relative) | |
8105 | break; | |
8106 | } | |
8107 | ret = i; | |
8108 | s_non_relative = p; | |
8109 | ||
8110 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
8111 | for (; i < count; i++, p += sort_elt) | |
8112 | { | |
8113 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
8114 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
8115 | sq = sp; | |
8116 | sp->u.offset = sq->rela->r_offset; | |
8117 | } | |
8118 | ||
8119 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
8120 | ||
3410fea8 | 8121 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
8122 | if (lo->type == bfd_indirect_link_order) |
8123 | { | |
8124 | bfd_byte *erel, *erelend; | |
8125 | asection *o = lo->u.indirect.section; | |
8126 | ||
8127 | erel = o->contents; | |
eea6121a | 8128 | erelend = o->contents + o->size; |
c152c796 AM |
8129 | p = sort + o->output_offset / ext_size * sort_elt; |
8130 | while (erel < erelend) | |
8131 | { | |
8132 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
8133 | (*swap_out) (abfd, s->rela, erel); | |
8134 | p += sort_elt; | |
8135 | erel += ext_size; | |
8136 | } | |
8137 | } | |
8138 | ||
8139 | free (sort); | |
3410fea8 | 8140 | *psec = dynamic_relocs; |
c152c796 AM |
8141 | return ret; |
8142 | } | |
8143 | ||
8144 | /* Flush the output symbols to the file. */ | |
8145 | ||
8146 | static bfd_boolean | |
8147 | elf_link_flush_output_syms (struct elf_final_link_info *finfo, | |
8148 | const struct elf_backend_data *bed) | |
8149 | { | |
8150 | if (finfo->symbuf_count > 0) | |
8151 | { | |
8152 | Elf_Internal_Shdr *hdr; | |
8153 | file_ptr pos; | |
8154 | bfd_size_type amt; | |
8155 | ||
8156 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
8157 | pos = hdr->sh_offset + hdr->sh_size; | |
8158 | amt = finfo->symbuf_count * bed->s->sizeof_sym; | |
8159 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
8160 | || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) | |
8161 | return FALSE; | |
8162 | ||
8163 | hdr->sh_size += amt; | |
8164 | finfo->symbuf_count = 0; | |
8165 | } | |
8166 | ||
8167 | return TRUE; | |
8168 | } | |
8169 | ||
8170 | /* Add a symbol to the output symbol table. */ | |
8171 | ||
8172 | static bfd_boolean | |
8173 | elf_link_output_sym (struct elf_final_link_info *finfo, | |
8174 | const char *name, | |
8175 | Elf_Internal_Sym *elfsym, | |
8176 | asection *input_sec, | |
8177 | struct elf_link_hash_entry *h) | |
8178 | { | |
8179 | bfd_byte *dest; | |
8180 | Elf_External_Sym_Shndx *destshndx; | |
8181 | bfd_boolean (*output_symbol_hook) | |
8182 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, | |
8183 | struct elf_link_hash_entry *); | |
8184 | const struct elf_backend_data *bed; | |
8185 | ||
8186 | bed = get_elf_backend_data (finfo->output_bfd); | |
8187 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; | |
8188 | if (output_symbol_hook != NULL) | |
8189 | { | |
8190 | if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h)) | |
8191 | return FALSE; | |
8192 | } | |
8193 | ||
8194 | if (name == NULL || *name == '\0') | |
8195 | elfsym->st_name = 0; | |
8196 | else if (input_sec->flags & SEC_EXCLUDE) | |
8197 | elfsym->st_name = 0; | |
8198 | else | |
8199 | { | |
8200 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
8201 | name, TRUE, FALSE); | |
8202 | if (elfsym->st_name == (unsigned long) -1) | |
8203 | return FALSE; | |
8204 | } | |
8205 | ||
8206 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
8207 | { | |
8208 | if (! elf_link_flush_output_syms (finfo, bed)) | |
8209 | return FALSE; | |
8210 | } | |
8211 | ||
8212 | dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; | |
8213 | destshndx = finfo->symshndxbuf; | |
8214 | if (destshndx != NULL) | |
8215 | { | |
8216 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
8217 | { | |
8218 | bfd_size_type amt; | |
8219 | ||
8220 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); | |
515ef31d | 8221 | destshndx = bfd_realloc (destshndx, amt * 2); |
c152c796 AM |
8222 | if (destshndx == NULL) |
8223 | return FALSE; | |
515ef31d | 8224 | finfo->symshndxbuf = destshndx; |
c152c796 AM |
8225 | memset ((char *) destshndx + amt, 0, amt); |
8226 | finfo->shndxbuf_size *= 2; | |
8227 | } | |
8228 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
8229 | } | |
8230 | ||
8231 | bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); | |
8232 | finfo->symbuf_count += 1; | |
8233 | bfd_get_symcount (finfo->output_bfd) += 1; | |
8234 | ||
8235 | return TRUE; | |
8236 | } | |
8237 | ||
c0d5a53d L |
8238 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
8239 | ||
8240 | static bfd_boolean | |
8241 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
8242 | { | |
4fbb74a6 AM |
8243 | if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) |
8244 | && sym->st_shndx < SHN_LORESERVE) | |
c0d5a53d L |
8245 | { |
8246 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 8247 | beyond 64k. */ |
c0d5a53d L |
8248 | (*_bfd_error_handler) |
8249 | (_("%B: Too many sections: %d (>= %d)"), | |
4fbb74a6 | 8250 | abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); |
c0d5a53d L |
8251 | bfd_set_error (bfd_error_nonrepresentable_section); |
8252 | return FALSE; | |
8253 | } | |
8254 | return TRUE; | |
8255 | } | |
8256 | ||
c152c796 AM |
8257 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
8258 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
8259 | versioned symbol that would normally require an explicit version. | |
8260 | We also handle the case that a DSO references a hidden symbol | |
8261 | which may be satisfied by a versioned symbol in another DSO. */ | |
8262 | ||
8263 | static bfd_boolean | |
8264 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
8265 | const struct elf_backend_data *bed, | |
8266 | struct elf_link_hash_entry *h) | |
8267 | { | |
8268 | bfd *abfd; | |
8269 | struct elf_link_loaded_list *loaded; | |
8270 | ||
8271 | if (!is_elf_hash_table (info->hash)) | |
8272 | return FALSE; | |
8273 | ||
8274 | switch (h->root.type) | |
8275 | { | |
8276 | default: | |
8277 | abfd = NULL; | |
8278 | break; | |
8279 | ||
8280 | case bfd_link_hash_undefined: | |
8281 | case bfd_link_hash_undefweak: | |
8282 | abfd = h->root.u.undef.abfd; | |
8283 | if ((abfd->flags & DYNAMIC) == 0 | |
e56f61be | 8284 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
8285 | return FALSE; |
8286 | break; | |
8287 | ||
8288 | case bfd_link_hash_defined: | |
8289 | case bfd_link_hash_defweak: | |
8290 | abfd = h->root.u.def.section->owner; | |
8291 | break; | |
8292 | ||
8293 | case bfd_link_hash_common: | |
8294 | abfd = h->root.u.c.p->section->owner; | |
8295 | break; | |
8296 | } | |
8297 | BFD_ASSERT (abfd != NULL); | |
8298 | ||
8299 | for (loaded = elf_hash_table (info)->loaded; | |
8300 | loaded != NULL; | |
8301 | loaded = loaded->next) | |
8302 | { | |
8303 | bfd *input; | |
8304 | Elf_Internal_Shdr *hdr; | |
8305 | bfd_size_type symcount; | |
8306 | bfd_size_type extsymcount; | |
8307 | bfd_size_type extsymoff; | |
8308 | Elf_Internal_Shdr *versymhdr; | |
8309 | Elf_Internal_Sym *isym; | |
8310 | Elf_Internal_Sym *isymend; | |
8311 | Elf_Internal_Sym *isymbuf; | |
8312 | Elf_External_Versym *ever; | |
8313 | Elf_External_Versym *extversym; | |
8314 | ||
8315 | input = loaded->abfd; | |
8316 | ||
8317 | /* We check each DSO for a possible hidden versioned definition. */ | |
8318 | if (input == abfd | |
8319 | || (input->flags & DYNAMIC) == 0 | |
8320 | || elf_dynversym (input) == 0) | |
8321 | continue; | |
8322 | ||
8323 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
8324 | ||
8325 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
8326 | if (elf_bad_symtab (input)) | |
8327 | { | |
8328 | extsymcount = symcount; | |
8329 | extsymoff = 0; | |
8330 | } | |
8331 | else | |
8332 | { | |
8333 | extsymcount = symcount - hdr->sh_info; | |
8334 | extsymoff = hdr->sh_info; | |
8335 | } | |
8336 | ||
8337 | if (extsymcount == 0) | |
8338 | continue; | |
8339 | ||
8340 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
8341 | NULL, NULL, NULL); | |
8342 | if (isymbuf == NULL) | |
8343 | return FALSE; | |
8344 | ||
8345 | /* Read in any version definitions. */ | |
8346 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
8347 | extversym = bfd_malloc (versymhdr->sh_size); | |
8348 | if (extversym == NULL) | |
8349 | goto error_ret; | |
8350 | ||
8351 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
8352 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
8353 | != versymhdr->sh_size)) | |
8354 | { | |
8355 | free (extversym); | |
8356 | error_ret: | |
8357 | free (isymbuf); | |
8358 | return FALSE; | |
8359 | } | |
8360 | ||
8361 | ever = extversym + extsymoff; | |
8362 | isymend = isymbuf + extsymcount; | |
8363 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
8364 | { | |
8365 | const char *name; | |
8366 | Elf_Internal_Versym iver; | |
8367 | unsigned short version_index; | |
8368 | ||
8369 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
8370 | || isym->st_shndx == SHN_UNDEF) | |
8371 | continue; | |
8372 | ||
8373 | name = bfd_elf_string_from_elf_section (input, | |
8374 | hdr->sh_link, | |
8375 | isym->st_name); | |
8376 | if (strcmp (name, h->root.root.string) != 0) | |
8377 | continue; | |
8378 | ||
8379 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
8380 | ||
8381 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
8382 | { | |
8383 | /* If we have a non-hidden versioned sym, then it should | |
8384 | have provided a definition for the undefined sym. */ | |
8385 | abort (); | |
8386 | } | |
8387 | ||
8388 | version_index = iver.vs_vers & VERSYM_VERSION; | |
8389 | if (version_index == 1 || version_index == 2) | |
8390 | { | |
8391 | /* This is the base or first version. We can use it. */ | |
8392 | free (extversym); | |
8393 | free (isymbuf); | |
8394 | return TRUE; | |
8395 | } | |
8396 | } | |
8397 | ||
8398 | free (extversym); | |
8399 | free (isymbuf); | |
8400 | } | |
8401 | ||
8402 | return FALSE; | |
8403 | } | |
8404 | ||
8405 | /* Add an external symbol to the symbol table. This is called from | |
8406 | the hash table traversal routine. When generating a shared object, | |
8407 | we go through the symbol table twice. The first time we output | |
8408 | anything that might have been forced to local scope in a version | |
8409 | script. The second time we output the symbols that are still | |
8410 | global symbols. */ | |
8411 | ||
8412 | static bfd_boolean | |
8413 | elf_link_output_extsym (struct elf_link_hash_entry *h, void *data) | |
8414 | { | |
8415 | struct elf_outext_info *eoinfo = data; | |
8416 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
8417 | bfd_boolean strip; | |
8418 | Elf_Internal_Sym sym; | |
8419 | asection *input_sec; | |
8420 | const struct elf_backend_data *bed; | |
8421 | ||
8422 | if (h->root.type == bfd_link_hash_warning) | |
8423 | { | |
8424 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8425 | if (h->root.type == bfd_link_hash_new) | |
8426 | return TRUE; | |
8427 | } | |
8428 | ||
8429 | /* Decide whether to output this symbol in this pass. */ | |
8430 | if (eoinfo->localsyms) | |
8431 | { | |
f5385ebf | 8432 | if (!h->forced_local) |
c152c796 AM |
8433 | return TRUE; |
8434 | } | |
8435 | else | |
8436 | { | |
f5385ebf | 8437 | if (h->forced_local) |
c152c796 AM |
8438 | return TRUE; |
8439 | } | |
8440 | ||
8441 | bed = get_elf_backend_data (finfo->output_bfd); | |
8442 | ||
12ac1cf5 | 8443 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 8444 | { |
12ac1cf5 NC |
8445 | /* If we have an undefined symbol reference here then it must have |
8446 | come from a shared library that is being linked in. (Undefined | |
8447 | references in regular files have already been handled). */ | |
8448 | bfd_boolean ignore_undef = FALSE; | |
8449 | ||
8450 | /* Some symbols may be special in that the fact that they're | |
8451 | undefined can be safely ignored - let backend determine that. */ | |
8452 | if (bed->elf_backend_ignore_undef_symbol) | |
8453 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
8454 | ||
8455 | /* If we are reporting errors for this situation then do so now. */ | |
8456 | if (ignore_undef == FALSE | |
8457 | && h->ref_dynamic | |
8458 | && ! h->ref_regular | |
8459 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h) | |
8460 | && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
c152c796 | 8461 | { |
12ac1cf5 NC |
8462 | if (! (finfo->info->callbacks->undefined_symbol |
8463 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
8464 | NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) | |
8465 | { | |
8466 | eoinfo->failed = TRUE; | |
8467 | return FALSE; | |
8468 | } | |
c152c796 AM |
8469 | } |
8470 | } | |
8471 | ||
8472 | /* We should also warn if a forced local symbol is referenced from | |
8473 | shared libraries. */ | |
8474 | if (! finfo->info->relocatable | |
8475 | && (! finfo->info->shared) | |
f5385ebf AM |
8476 | && h->forced_local |
8477 | && h->ref_dynamic | |
8478 | && !h->dynamic_def | |
8479 | && !h->dynamic_weak | |
c152c796 AM |
8480 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) |
8481 | { | |
8482 | (*_bfd_error_handler) | |
d003868e | 8483 | (_("%B: %s symbol `%s' in %B is referenced by DSO"), |
cfca085c L |
8484 | finfo->output_bfd, |
8485 | h->root.u.def.section == bfd_abs_section_ptr | |
8486 | ? finfo->output_bfd : h->root.u.def.section->owner, | |
c152c796 AM |
8487 | ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
8488 | ? "internal" | |
8489 | : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
d003868e AM |
8490 | ? "hidden" : "local", |
8491 | h->root.root.string); | |
c152c796 AM |
8492 | eoinfo->failed = TRUE; |
8493 | return FALSE; | |
8494 | } | |
8495 | ||
8496 | /* We don't want to output symbols that have never been mentioned by | |
8497 | a regular file, or that we have been told to strip. However, if | |
8498 | h->indx is set to -2, the symbol is used by a reloc and we must | |
8499 | output it. */ | |
8500 | if (h->indx == -2) | |
8501 | strip = FALSE; | |
f5385ebf | 8502 | else if ((h->def_dynamic |
77cfaee6 AM |
8503 | || h->ref_dynamic |
8504 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
8505 | && !h->def_regular |
8506 | && !h->ref_regular) | |
c152c796 AM |
8507 | strip = TRUE; |
8508 | else if (finfo->info->strip == strip_all) | |
8509 | strip = TRUE; | |
8510 | else if (finfo->info->strip == strip_some | |
8511 | && bfd_hash_lookup (finfo->info->keep_hash, | |
8512 | h->root.root.string, FALSE, FALSE) == NULL) | |
8513 | strip = TRUE; | |
8514 | else if (finfo->info->strip_discarded | |
8515 | && (h->root.type == bfd_link_hash_defined | |
8516 | || h->root.type == bfd_link_hash_defweak) | |
8517 | && elf_discarded_section (h->root.u.def.section)) | |
8518 | strip = TRUE; | |
8519 | else | |
8520 | strip = FALSE; | |
8521 | ||
8522 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
8523 | nothing else to do unless it is a forced local symbol. */ | |
8524 | if (strip | |
8525 | && h->dynindx == -1 | |
f5385ebf | 8526 | && !h->forced_local) |
c152c796 AM |
8527 | return TRUE; |
8528 | ||
8529 | sym.st_value = 0; | |
8530 | sym.st_size = h->size; | |
8531 | sym.st_other = h->other; | |
f5385ebf | 8532 | if (h->forced_local) |
c152c796 AM |
8533 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); |
8534 | else if (h->root.type == bfd_link_hash_undefweak | |
8535 | || h->root.type == bfd_link_hash_defweak) | |
8536 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
8537 | else | |
8538 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
8539 | ||
8540 | switch (h->root.type) | |
8541 | { | |
8542 | default: | |
8543 | case bfd_link_hash_new: | |
8544 | case bfd_link_hash_warning: | |
8545 | abort (); | |
8546 | return FALSE; | |
8547 | ||
8548 | case bfd_link_hash_undefined: | |
8549 | case bfd_link_hash_undefweak: | |
8550 | input_sec = bfd_und_section_ptr; | |
8551 | sym.st_shndx = SHN_UNDEF; | |
8552 | break; | |
8553 | ||
8554 | case bfd_link_hash_defined: | |
8555 | case bfd_link_hash_defweak: | |
8556 | { | |
8557 | input_sec = h->root.u.def.section; | |
8558 | if (input_sec->output_section != NULL) | |
8559 | { | |
8560 | sym.st_shndx = | |
8561 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
8562 | input_sec->output_section); | |
8563 | if (sym.st_shndx == SHN_BAD) | |
8564 | { | |
8565 | (*_bfd_error_handler) | |
d003868e AM |
8566 | (_("%B: could not find output section %A for input section %A"), |
8567 | finfo->output_bfd, input_sec->output_section, input_sec); | |
c152c796 AM |
8568 | eoinfo->failed = TRUE; |
8569 | return FALSE; | |
8570 | } | |
8571 | ||
8572 | /* ELF symbols in relocatable files are section relative, | |
8573 | but in nonrelocatable files they are virtual | |
8574 | addresses. */ | |
8575 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
8576 | if (! finfo->info->relocatable) | |
8577 | { | |
8578 | sym.st_value += input_sec->output_section->vma; | |
8579 | if (h->type == STT_TLS) | |
8580 | { | |
430a16a5 NC |
8581 | asection *tls_sec = elf_hash_table (finfo->info)->tls_sec; |
8582 | if (tls_sec != NULL) | |
8583 | sym.st_value -= tls_sec->vma; | |
8584 | else | |
8585 | { | |
8586 | /* The TLS section may have been garbage collected. */ | |
8587 | BFD_ASSERT (finfo->info->gc_sections | |
8588 | && !input_sec->gc_mark); | |
8589 | } | |
c152c796 AM |
8590 | } |
8591 | } | |
8592 | } | |
8593 | else | |
8594 | { | |
8595 | BFD_ASSERT (input_sec->owner == NULL | |
8596 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
8597 | sym.st_shndx = SHN_UNDEF; | |
8598 | input_sec = bfd_und_section_ptr; | |
8599 | } | |
8600 | } | |
8601 | break; | |
8602 | ||
8603 | case bfd_link_hash_common: | |
8604 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 8605 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
8606 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
8607 | break; | |
8608 | ||
8609 | case bfd_link_hash_indirect: | |
8610 | /* These symbols are created by symbol versioning. They point | |
8611 | to the decorated version of the name. For example, if the | |
8612 | symbol foo@@GNU_1.2 is the default, which should be used when | |
8613 | foo is used with no version, then we add an indirect symbol | |
8614 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
8615 | since the indirected symbol is already in the hash table. */ | |
8616 | return TRUE; | |
8617 | } | |
8618 | ||
8619 | /* Give the processor backend a chance to tweak the symbol value, | |
8620 | and also to finish up anything that needs to be done for this | |
8621 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
8622 | forced local syms when non-shared is due to a historical quirk. */ | |
8623 | if ((h->dynindx != -1 | |
f5385ebf | 8624 | || h->forced_local) |
c152c796 AM |
8625 | && ((finfo->info->shared |
8626 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
8627 | || h->root.type != bfd_link_hash_undefweak)) | |
f5385ebf | 8628 | || !h->forced_local) |
c152c796 AM |
8629 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
8630 | { | |
8631 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8632 | (finfo->output_bfd, finfo->info, h, &sym))) | |
8633 | { | |
8634 | eoinfo->failed = TRUE; | |
8635 | return FALSE; | |
8636 | } | |
8637 | } | |
8638 | ||
8639 | /* If we are marking the symbol as undefined, and there are no | |
8640 | non-weak references to this symbol from a regular object, then | |
8641 | mark the symbol as weak undefined; if there are non-weak | |
8642 | references, mark the symbol as strong. We can't do this earlier, | |
8643 | because it might not be marked as undefined until the | |
8644 | finish_dynamic_symbol routine gets through with it. */ | |
8645 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 8646 | && h->ref_regular |
c152c796 AM |
8647 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
8648 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
8649 | { | |
8650 | int bindtype; | |
8651 | ||
f5385ebf | 8652 | if (h->ref_regular_nonweak) |
c152c796 AM |
8653 | bindtype = STB_GLOBAL; |
8654 | else | |
8655 | bindtype = STB_WEAK; | |
8656 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); | |
8657 | } | |
8658 | ||
bda987c2 CD |
8659 | /* If this is a symbol defined in a dynamic library, don't use the |
8660 | symbol size from the dynamic library. Relinking an executable | |
8661 | against a new library may introduce gratuitous changes in the | |
8662 | executable's symbols if we keep the size. */ | |
8663 | if (sym.st_shndx == SHN_UNDEF | |
8664 | && !h->def_regular | |
8665 | && h->def_dynamic) | |
8666 | sym.st_size = 0; | |
8667 | ||
c152c796 AM |
8668 | /* If a non-weak symbol with non-default visibility is not defined |
8669 | locally, it is a fatal error. */ | |
8670 | if (! finfo->info->relocatable | |
8671 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT | |
8672 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
8673 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 8674 | && !h->def_regular) |
c152c796 AM |
8675 | { |
8676 | (*_bfd_error_handler) | |
d003868e AM |
8677 | (_("%B: %s symbol `%s' isn't defined"), |
8678 | finfo->output_bfd, | |
8679 | ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED | |
8680 | ? "protected" | |
8681 | : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL | |
8682 | ? "internal" : "hidden", | |
8683 | h->root.root.string); | |
c152c796 AM |
8684 | eoinfo->failed = TRUE; |
8685 | return FALSE; | |
8686 | } | |
8687 | ||
8688 | /* If this symbol should be put in the .dynsym section, then put it | |
8689 | there now. We already know the symbol index. We also fill in | |
8690 | the entry in the .hash section. */ | |
8691 | if (h->dynindx != -1 | |
8692 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
8693 | { | |
c152c796 AM |
8694 | bfd_byte *esym; |
8695 | ||
8696 | sym.st_name = h->dynstr_index; | |
8697 | esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; | |
c0d5a53d L |
8698 | if (! check_dynsym (finfo->output_bfd, &sym)) |
8699 | { | |
8700 | eoinfo->failed = TRUE; | |
8701 | return FALSE; | |
8702 | } | |
c152c796 AM |
8703 | bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); |
8704 | ||
fdc90cb4 JJ |
8705 | if (finfo->hash_sec != NULL) |
8706 | { | |
8707 | size_t hash_entry_size; | |
8708 | bfd_byte *bucketpos; | |
8709 | bfd_vma chain; | |
41198d0c L |
8710 | size_t bucketcount; |
8711 | size_t bucket; | |
8712 | ||
8713 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
8714 | bucket = h->u.elf_hash_value % bucketcount; | |
fdc90cb4 JJ |
8715 | |
8716 | hash_entry_size | |
8717 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; | |
8718 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents | |
8719 | + (bucket + 2) * hash_entry_size); | |
8720 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
8721 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); | |
8722 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, | |
8723 | ((bfd_byte *) finfo->hash_sec->contents | |
8724 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
8725 | } | |
c152c796 AM |
8726 | |
8727 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
8728 | { | |
8729 | Elf_Internal_Versym iversym; | |
8730 | Elf_External_Versym *eversym; | |
8731 | ||
f5385ebf | 8732 | if (!h->def_regular) |
c152c796 AM |
8733 | { |
8734 | if (h->verinfo.verdef == NULL) | |
8735 | iversym.vs_vers = 0; | |
8736 | else | |
8737 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
8738 | } | |
8739 | else | |
8740 | { | |
8741 | if (h->verinfo.vertree == NULL) | |
8742 | iversym.vs_vers = 1; | |
8743 | else | |
8744 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
3e3b46e5 PB |
8745 | if (finfo->info->create_default_symver) |
8746 | iversym.vs_vers++; | |
c152c796 AM |
8747 | } |
8748 | ||
f5385ebf | 8749 | if (h->hidden) |
c152c796 AM |
8750 | iversym.vs_vers |= VERSYM_HIDDEN; |
8751 | ||
8752 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; | |
8753 | eversym += h->dynindx; | |
8754 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
8755 | } | |
8756 | } | |
8757 | ||
8758 | /* If we're stripping it, then it was just a dynamic symbol, and | |
8759 | there's nothing else to do. */ | |
8760 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) | |
8761 | return TRUE; | |
8762 | ||
8763 | h->indx = bfd_get_symcount (finfo->output_bfd); | |
8764 | ||
8765 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h)) | |
8766 | { | |
8767 | eoinfo->failed = TRUE; | |
8768 | return FALSE; | |
8769 | } | |
8770 | ||
8771 | return TRUE; | |
8772 | } | |
8773 | ||
cdd3575c AM |
8774 | /* Return TRUE if special handling is done for relocs in SEC against |
8775 | symbols defined in discarded sections. */ | |
8776 | ||
c152c796 AM |
8777 | static bfd_boolean |
8778 | elf_section_ignore_discarded_relocs (asection *sec) | |
8779 | { | |
8780 | const struct elf_backend_data *bed; | |
8781 | ||
cdd3575c AM |
8782 | switch (sec->sec_info_type) |
8783 | { | |
8784 | case ELF_INFO_TYPE_STABS: | |
8785 | case ELF_INFO_TYPE_EH_FRAME: | |
8786 | return TRUE; | |
8787 | default: | |
8788 | break; | |
8789 | } | |
c152c796 AM |
8790 | |
8791 | bed = get_elf_backend_data (sec->owner); | |
8792 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
8793 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
8794 | return TRUE; | |
8795 | ||
8796 | return FALSE; | |
8797 | } | |
8798 | ||
9e66c942 AM |
8799 | /* Return a mask saying how ld should treat relocations in SEC against |
8800 | symbols defined in discarded sections. If this function returns | |
8801 | COMPLAIN set, ld will issue a warning message. If this function | |
8802 | returns PRETEND set, and the discarded section was link-once and the | |
8803 | same size as the kept link-once section, ld will pretend that the | |
8804 | symbol was actually defined in the kept section. Otherwise ld will | |
8805 | zero the reloc (at least that is the intent, but some cooperation by | |
8806 | the target dependent code is needed, particularly for REL targets). */ | |
8807 | ||
8a696751 AM |
8808 | unsigned int |
8809 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 8810 | { |
9e66c942 | 8811 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 8812 | return PRETEND; |
cdd3575c AM |
8813 | |
8814 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 8815 | return 0; |
cdd3575c AM |
8816 | |
8817 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 8818 | return 0; |
cdd3575c | 8819 | |
9e66c942 | 8820 | return COMPLAIN | PRETEND; |
cdd3575c AM |
8821 | } |
8822 | ||
3d7f7666 L |
8823 | /* Find a match between a section and a member of a section group. */ |
8824 | ||
8825 | static asection * | |
c0f00686 L |
8826 | match_group_member (asection *sec, asection *group, |
8827 | struct bfd_link_info *info) | |
3d7f7666 L |
8828 | { |
8829 | asection *first = elf_next_in_group (group); | |
8830 | asection *s = first; | |
8831 | ||
8832 | while (s != NULL) | |
8833 | { | |
c0f00686 | 8834 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
8835 | return s; |
8836 | ||
83180ade | 8837 | s = elf_next_in_group (s); |
3d7f7666 L |
8838 | if (s == first) |
8839 | break; | |
8840 | } | |
8841 | ||
8842 | return NULL; | |
8843 | } | |
8844 | ||
01b3c8ab | 8845 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
8846 | to replace it. Return the replacement if it is OK. Otherwise return |
8847 | NULL. */ | |
01b3c8ab L |
8848 | |
8849 | asection * | |
c0f00686 | 8850 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
8851 | { |
8852 | asection *kept; | |
8853 | ||
8854 | kept = sec->kept_section; | |
8855 | if (kept != NULL) | |
8856 | { | |
c2370991 | 8857 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 8858 | kept = match_group_member (sec, kept, info); |
1dd2625f BW |
8859 | if (kept != NULL |
8860 | && ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
8861 | != (kept->rawsize != 0 ? kept->rawsize : kept->size))) | |
01b3c8ab | 8862 | kept = NULL; |
c2370991 | 8863 | sec->kept_section = kept; |
01b3c8ab L |
8864 | } |
8865 | return kept; | |
8866 | } | |
8867 | ||
c152c796 AM |
8868 | /* Link an input file into the linker output file. This function |
8869 | handles all the sections and relocations of the input file at once. | |
8870 | This is so that we only have to read the local symbols once, and | |
8871 | don't have to keep them in memory. */ | |
8872 | ||
8873 | static bfd_boolean | |
8874 | elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) | |
8875 | { | |
ece5ef60 | 8876 | int (*relocate_section) |
c152c796 AM |
8877 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
8878 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
8879 | bfd *output_bfd; | |
8880 | Elf_Internal_Shdr *symtab_hdr; | |
8881 | size_t locsymcount; | |
8882 | size_t extsymoff; | |
8883 | Elf_Internal_Sym *isymbuf; | |
8884 | Elf_Internal_Sym *isym; | |
8885 | Elf_Internal_Sym *isymend; | |
8886 | long *pindex; | |
8887 | asection **ppsection; | |
8888 | asection *o; | |
8889 | const struct elf_backend_data *bed; | |
c152c796 AM |
8890 | struct elf_link_hash_entry **sym_hashes; |
8891 | ||
8892 | output_bfd = finfo->output_bfd; | |
8893 | bed = get_elf_backend_data (output_bfd); | |
8894 | relocate_section = bed->elf_backend_relocate_section; | |
8895 | ||
8896 | /* If this is a dynamic object, we don't want to do anything here: | |
8897 | we don't want the local symbols, and we don't want the section | |
8898 | contents. */ | |
8899 | if ((input_bfd->flags & DYNAMIC) != 0) | |
8900 | return TRUE; | |
8901 | ||
c152c796 AM |
8902 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
8903 | if (elf_bad_symtab (input_bfd)) | |
8904 | { | |
8905 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
8906 | extsymoff = 0; | |
8907 | } | |
8908 | else | |
8909 | { | |
8910 | locsymcount = symtab_hdr->sh_info; | |
8911 | extsymoff = symtab_hdr->sh_info; | |
8912 | } | |
8913 | ||
8914 | /* Read the local symbols. */ | |
8915 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
8916 | if (isymbuf == NULL && locsymcount != 0) | |
8917 | { | |
8918 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8919 | finfo->internal_syms, | |
8920 | finfo->external_syms, | |
8921 | finfo->locsym_shndx); | |
8922 | if (isymbuf == NULL) | |
8923 | return FALSE; | |
8924 | } | |
8925 | ||
8926 | /* Find local symbol sections and adjust values of symbols in | |
8927 | SEC_MERGE sections. Write out those local symbols we know are | |
8928 | going into the output file. */ | |
8929 | isymend = isymbuf + locsymcount; | |
8930 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
8931 | isym < isymend; | |
8932 | isym++, pindex++, ppsection++) | |
8933 | { | |
8934 | asection *isec; | |
8935 | const char *name; | |
8936 | Elf_Internal_Sym osym; | |
8937 | ||
8938 | *pindex = -1; | |
8939 | ||
8940 | if (elf_bad_symtab (input_bfd)) | |
8941 | { | |
8942 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
8943 | { | |
8944 | *ppsection = NULL; | |
8945 | continue; | |
8946 | } | |
8947 | } | |
8948 | ||
8949 | if (isym->st_shndx == SHN_UNDEF) | |
8950 | isec = bfd_und_section_ptr; | |
c152c796 AM |
8951 | else if (isym->st_shndx == SHN_ABS) |
8952 | isec = bfd_abs_section_ptr; | |
8953 | else if (isym->st_shndx == SHN_COMMON) | |
8954 | isec = bfd_com_section_ptr; | |
8955 | else | |
8956 | { | |
cb33740c AM |
8957 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
8958 | if (isec == NULL) | |
8959 | { | |
8960 | /* Don't attempt to output symbols with st_shnx in the | |
8961 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
8962 | *ppsection = NULL; | |
8963 | continue; | |
8964 | } | |
8965 | else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE | |
8966 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) | |
8967 | isym->st_value = | |
8968 | _bfd_merged_section_offset (output_bfd, &isec, | |
8969 | elf_section_data (isec)->sec_info, | |
8970 | isym->st_value); | |
c152c796 AM |
8971 | } |
8972 | ||
8973 | *ppsection = isec; | |
8974 | ||
8975 | /* Don't output the first, undefined, symbol. */ | |
8976 | if (ppsection == finfo->sections) | |
8977 | continue; | |
8978 | ||
8979 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
8980 | { | |
8981 | /* We never output section symbols. Instead, we use the | |
8982 | section symbol of the corresponding section in the output | |
8983 | file. */ | |
8984 | continue; | |
8985 | } | |
8986 | ||
8987 | /* If we are stripping all symbols, we don't want to output this | |
8988 | one. */ | |
8989 | if (finfo->info->strip == strip_all) | |
8990 | continue; | |
8991 | ||
8992 | /* If we are discarding all local symbols, we don't want to | |
8993 | output this one. If we are generating a relocatable output | |
8994 | file, then some of the local symbols may be required by | |
8995 | relocs; we output them below as we discover that they are | |
8996 | needed. */ | |
8997 | if (finfo->info->discard == discard_all) | |
8998 | continue; | |
8999 | ||
9000 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
9001 | discarding, we don't need to keep it. */ |
9002 | if (isym->st_shndx != SHN_UNDEF | |
4fbb74a6 AM |
9003 | && isym->st_shndx < SHN_LORESERVE |
9004 | && bfd_section_removed_from_list (output_bfd, | |
9005 | isec->output_section)) | |
e75a280b L |
9006 | continue; |
9007 | ||
c152c796 AM |
9008 | /* Get the name of the symbol. */ |
9009 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
9010 | isym->st_name); | |
9011 | if (name == NULL) | |
9012 | return FALSE; | |
9013 | ||
9014 | /* See if we are discarding symbols with this name. */ | |
9015 | if ((finfo->info->strip == strip_some | |
9016 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) | |
9017 | == NULL)) | |
9018 | || (((finfo->info->discard == discard_sec_merge | |
9019 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) | |
9020 | || finfo->info->discard == discard_l) | |
9021 | && bfd_is_local_label_name (input_bfd, name))) | |
9022 | continue; | |
9023 | ||
9024 | /* If we get here, we are going to output this symbol. */ | |
9025 | ||
9026 | osym = *isym; | |
9027 | ||
9028 | /* Adjust the section index for the output file. */ | |
9029 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
9030 | isec->output_section); | |
9031 | if (osym.st_shndx == SHN_BAD) | |
9032 | return FALSE; | |
9033 | ||
9034 | *pindex = bfd_get_symcount (output_bfd); | |
9035 | ||
9036 | /* ELF symbols in relocatable files are section relative, but | |
9037 | in executable files they are virtual addresses. Note that | |
9038 | this code assumes that all ELF sections have an associated | |
9039 | BFD section with a reasonable value for output_offset; below | |
9040 | we assume that they also have a reasonable value for | |
9041 | output_section. Any special sections must be set up to meet | |
9042 | these requirements. */ | |
9043 | osym.st_value += isec->output_offset; | |
9044 | if (! finfo->info->relocatable) | |
9045 | { | |
9046 | osym.st_value += isec->output_section->vma; | |
9047 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
9048 | { | |
9049 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
9050 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
9051 | osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
9052 | } | |
9053 | } | |
9054 | ||
9055 | if (! elf_link_output_sym (finfo, name, &osym, isec, NULL)) | |
9056 | return FALSE; | |
9057 | } | |
9058 | ||
9059 | /* Relocate the contents of each section. */ | |
9060 | sym_hashes = elf_sym_hashes (input_bfd); | |
9061 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
9062 | { | |
9063 | bfd_byte *contents; | |
9064 | ||
9065 | if (! o->linker_mark) | |
9066 | { | |
9067 | /* This section was omitted from the link. */ | |
9068 | continue; | |
9069 | } | |
9070 | ||
bcacc0f5 AM |
9071 | if (finfo->info->relocatable |
9072 | && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) | |
9073 | { | |
9074 | /* Deal with the group signature symbol. */ | |
9075 | struct bfd_elf_section_data *sec_data = elf_section_data (o); | |
9076 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
9077 | asection *osec = o->output_section; | |
9078 | ||
9079 | if (symndx >= locsymcount | |
9080 | || (elf_bad_symtab (input_bfd) | |
9081 | && finfo->sections[symndx] == NULL)) | |
9082 | { | |
9083 | struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; | |
9084 | while (h->root.type == bfd_link_hash_indirect | |
9085 | || h->root.type == bfd_link_hash_warning) | |
9086 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9087 | /* Arrange for symbol to be output. */ | |
9088 | h->indx = -2; | |
9089 | elf_section_data (osec)->this_hdr.sh_info = -2; | |
9090 | } | |
9091 | else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) | |
9092 | { | |
9093 | /* We'll use the output section target_index. */ | |
9094 | asection *sec = finfo->sections[symndx]->output_section; | |
9095 | elf_section_data (osec)->this_hdr.sh_info = sec->target_index; | |
9096 | } | |
9097 | else | |
9098 | { | |
9099 | if (finfo->indices[symndx] == -1) | |
9100 | { | |
9101 | /* Otherwise output the local symbol now. */ | |
9102 | Elf_Internal_Sym sym = isymbuf[symndx]; | |
9103 | asection *sec = finfo->sections[symndx]->output_section; | |
9104 | const char *name; | |
9105 | ||
9106 | name = bfd_elf_string_from_elf_section (input_bfd, | |
9107 | symtab_hdr->sh_link, | |
9108 | sym.st_name); | |
9109 | if (name == NULL) | |
9110 | return FALSE; | |
9111 | ||
9112 | sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
9113 | sec); | |
9114 | if (sym.st_shndx == SHN_BAD) | |
9115 | return FALSE; | |
9116 | ||
9117 | sym.st_value += o->output_offset; | |
9118 | ||
9119 | finfo->indices[symndx] = bfd_get_symcount (output_bfd); | |
9120 | if (! elf_link_output_sym (finfo, name, &sym, o, NULL)) | |
9121 | return FALSE; | |
9122 | } | |
9123 | elf_section_data (osec)->this_hdr.sh_info | |
9124 | = finfo->indices[symndx]; | |
9125 | } | |
9126 | } | |
9127 | ||
c152c796 | 9128 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 9129 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
9130 | continue; |
9131 | ||
9132 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
9133 | { | |
9134 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
9135 | or somesuch. */ | |
9136 | continue; | |
9137 | } | |
9138 | ||
9139 | /* Get the contents of the section. They have been cached by a | |
9140 | relaxation routine. Note that o is a section in an input | |
9141 | file, so the contents field will not have been set by any of | |
9142 | the routines which work on output files. */ | |
9143 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
9144 | contents = elf_section_data (o)->this_hdr.contents; | |
9145 | else | |
9146 | { | |
eea6121a AM |
9147 | bfd_size_type amt = o->rawsize ? o->rawsize : o->size; |
9148 | ||
c152c796 | 9149 | contents = finfo->contents; |
eea6121a | 9150 | if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt)) |
c152c796 AM |
9151 | return FALSE; |
9152 | } | |
9153 | ||
9154 | if ((o->flags & SEC_RELOC) != 0) | |
9155 | { | |
9156 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 9157 | Elf_Internal_Rela *rel, *relend; |
c152c796 AM |
9158 | bfd_vma r_type_mask; |
9159 | int r_sym_shift; | |
0f02bbd9 | 9160 | int action_discarded; |
ece5ef60 | 9161 | int ret; |
c152c796 AM |
9162 | |
9163 | /* Get the swapped relocs. */ | |
9164 | internal_relocs | |
9165 | = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, | |
9166 | finfo->internal_relocs, FALSE); | |
9167 | if (internal_relocs == NULL | |
9168 | && o->reloc_count > 0) | |
9169 | return FALSE; | |
9170 | ||
9171 | if (bed->s->arch_size == 32) | |
9172 | { | |
9173 | r_type_mask = 0xff; | |
9174 | r_sym_shift = 8; | |
9175 | } | |
9176 | else | |
9177 | { | |
9178 | r_type_mask = 0xffffffff; | |
9179 | r_sym_shift = 32; | |
9180 | } | |
9181 | ||
0f02bbd9 | 9182 | action_discarded = -1; |
c152c796 | 9183 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
9184 | action_discarded = (*bed->action_discarded) (o); |
9185 | ||
9186 | /* Run through the relocs evaluating complex reloc symbols and | |
9187 | looking for relocs against symbols from discarded sections | |
9188 | or section symbols from removed link-once sections. | |
9189 | Complain about relocs against discarded sections. Zero | |
9190 | relocs against removed link-once sections. */ | |
9191 | ||
9192 | rel = internal_relocs; | |
9193 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
9194 | for ( ; rel < relend; rel++) | |
c152c796 | 9195 | { |
0f02bbd9 AM |
9196 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
9197 | unsigned int s_type; | |
9198 | asection **ps, *sec; | |
9199 | struct elf_link_hash_entry *h = NULL; | |
9200 | const char *sym_name; | |
c152c796 | 9201 | |
0f02bbd9 AM |
9202 | if (r_symndx == STN_UNDEF) |
9203 | continue; | |
c152c796 | 9204 | |
0f02bbd9 AM |
9205 | if (r_symndx >= locsymcount |
9206 | || (elf_bad_symtab (input_bfd) | |
9207 | && finfo->sections[r_symndx] == NULL)) | |
9208 | { | |
9209 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 9210 | |
0f02bbd9 AM |
9211 | /* Badly formatted input files can contain relocs that |
9212 | reference non-existant symbols. Check here so that | |
9213 | we do not seg fault. */ | |
9214 | if (h == NULL) | |
c152c796 | 9215 | { |
0f02bbd9 | 9216 | char buffer [32]; |
dce669a1 | 9217 | |
0f02bbd9 AM |
9218 | sprintf_vma (buffer, rel->r_info); |
9219 | (*_bfd_error_handler) | |
9220 | (_("error: %B contains a reloc (0x%s) for section %A " | |
9221 | "that references a non-existent global symbol"), | |
9222 | input_bfd, o, buffer); | |
9223 | bfd_set_error (bfd_error_bad_value); | |
9224 | return FALSE; | |
9225 | } | |
3b36f7e6 | 9226 | |
0f02bbd9 AM |
9227 | while (h->root.type == bfd_link_hash_indirect |
9228 | || h->root.type == bfd_link_hash_warning) | |
9229 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 9230 | |
0f02bbd9 | 9231 | s_type = h->type; |
cdd3575c | 9232 | |
0f02bbd9 AM |
9233 | ps = NULL; |
9234 | if (h->root.type == bfd_link_hash_defined | |
9235 | || h->root.type == bfd_link_hash_defweak) | |
9236 | ps = &h->root.u.def.section; | |
9237 | ||
9238 | sym_name = h->root.root.string; | |
9239 | } | |
9240 | else | |
9241 | { | |
9242 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
9243 | ||
9244 | s_type = ELF_ST_TYPE (sym->st_info); | |
9245 | ps = &finfo->sections[r_symndx]; | |
9246 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, | |
9247 | sym, *ps); | |
9248 | } | |
c152c796 | 9249 | |
0f02bbd9 AM |
9250 | if (s_type == STT_RELC || s_type == STT_SRELC) |
9251 | { | |
9252 | bfd_vma val; | |
9253 | bfd_vma dot = (rel->r_offset | |
9254 | + o->output_offset + o->output_section->vma); | |
9255 | #ifdef DEBUG | |
9256 | printf ("Encountered a complex symbol!"); | |
9257 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
9258 | input_bfd->filename, o->name, rel - internal_relocs); | |
9259 | printf (" symbol: idx %8.8lx, name %s\n", | |
9260 | r_symndx, sym_name); | |
9261 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
9262 | (unsigned long) rel->r_info, | |
9263 | (unsigned long) rel->r_offset); | |
9264 | #endif | |
9265 | if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot, | |
9266 | isymbuf, locsymcount, s_type == STT_SRELC)) | |
9267 | return FALSE; | |
9268 | ||
9269 | /* Symbol evaluated OK. Update to absolute value. */ | |
9270 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
9271 | r_symndx, val); | |
9272 | continue; | |
9273 | } | |
9274 | ||
9275 | if (action_discarded != -1 && ps != NULL) | |
9276 | { | |
cdd3575c AM |
9277 | /* Complain if the definition comes from a |
9278 | discarded section. */ | |
9279 | if ((sec = *ps) != NULL && elf_discarded_section (sec)) | |
9280 | { | |
87e5235d | 9281 | BFD_ASSERT (r_symndx != 0); |
0f02bbd9 | 9282 | if (action_discarded & COMPLAIN) |
e1fffbe6 AM |
9283 | (*finfo->info->callbacks->einfo) |
9284 | (_("%X`%s' referenced in section `%A' of %B: " | |
58ac56d0 | 9285 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 9286 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 9287 | |
87e5235d | 9288 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 9289 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
9290 | really defined in the kept linkonce section. |
9291 | FIXME: This is quite broken. Modifying the | |
9292 | symbol here means we will be changing all later | |
e0ae6d6f | 9293 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 9294 | if (action_discarded & PRETEND) |
87e5235d | 9295 | { |
01b3c8ab L |
9296 | asection *kept; |
9297 | ||
c0f00686 L |
9298 | kept = _bfd_elf_check_kept_section (sec, |
9299 | finfo->info); | |
01b3c8ab | 9300 | if (kept != NULL) |
87e5235d AM |
9301 | { |
9302 | *ps = kept; | |
9303 | continue; | |
9304 | } | |
9305 | } | |
c152c796 AM |
9306 | } |
9307 | } | |
9308 | } | |
9309 | ||
9310 | /* Relocate the section by invoking a back end routine. | |
9311 | ||
9312 | The back end routine is responsible for adjusting the | |
9313 | section contents as necessary, and (if using Rela relocs | |
9314 | and generating a relocatable output file) adjusting the | |
9315 | reloc addend as necessary. | |
9316 | ||
9317 | The back end routine does not have to worry about setting | |
9318 | the reloc address or the reloc symbol index. | |
9319 | ||
9320 | The back end routine is given a pointer to the swapped in | |
9321 | internal symbols, and can access the hash table entries | |
9322 | for the external symbols via elf_sym_hashes (input_bfd). | |
9323 | ||
9324 | When generating relocatable output, the back end routine | |
9325 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
9326 | output symbol is going to be a section symbol | |
9327 | corresponding to the output section, which will require | |
9328 | the addend to be adjusted. */ | |
9329 | ||
ece5ef60 | 9330 | ret = (*relocate_section) (output_bfd, finfo->info, |
c152c796 AM |
9331 | input_bfd, o, contents, |
9332 | internal_relocs, | |
9333 | isymbuf, | |
ece5ef60 AM |
9334 | finfo->sections); |
9335 | if (!ret) | |
c152c796 AM |
9336 | return FALSE; |
9337 | ||
ece5ef60 AM |
9338 | if (ret == 2 |
9339 | || finfo->info->relocatable | |
9340 | || finfo->info->emitrelocations) | |
c152c796 AM |
9341 | { |
9342 | Elf_Internal_Rela *irela; | |
9343 | Elf_Internal_Rela *irelaend; | |
9344 | bfd_vma last_offset; | |
9345 | struct elf_link_hash_entry **rel_hash; | |
eac338cf | 9346 | struct elf_link_hash_entry **rel_hash_list; |
c152c796 AM |
9347 | Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2; |
9348 | unsigned int next_erel; | |
c152c796 AM |
9349 | bfd_boolean rela_normal; |
9350 | ||
9351 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
9352 | rela_normal = (bed->rela_normal | |
9353 | && (input_rel_hdr->sh_entsize | |
9354 | == bed->s->sizeof_rela)); | |
9355 | ||
9356 | /* Adjust the reloc addresses and symbol indices. */ | |
9357 | ||
9358 | irela = internal_relocs; | |
9359 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
9360 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
9361 | + elf_section_data (o->output_section)->rel_count | |
9362 | + elf_section_data (o->output_section)->rel_count2); | |
eac338cf | 9363 | rel_hash_list = rel_hash; |
c152c796 AM |
9364 | last_offset = o->output_offset; |
9365 | if (!finfo->info->relocatable) | |
9366 | last_offset += o->output_section->vma; | |
9367 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
9368 | { | |
9369 | unsigned long r_symndx; | |
9370 | asection *sec; | |
9371 | Elf_Internal_Sym sym; | |
9372 | ||
9373 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
9374 | { | |
9375 | rel_hash++; | |
9376 | next_erel = 0; | |
9377 | } | |
9378 | ||
9379 | irela->r_offset = _bfd_elf_section_offset (output_bfd, | |
9380 | finfo->info, o, | |
9381 | irela->r_offset); | |
9382 | if (irela->r_offset >= (bfd_vma) -2) | |
9383 | { | |
9384 | /* This is a reloc for a deleted entry or somesuch. | |
9385 | Turn it into an R_*_NONE reloc, at the same | |
9386 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 9387 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
9388 | being ordered. */ |
9389 | irela->r_offset = last_offset; | |
9390 | irela->r_info = 0; | |
9391 | irela->r_addend = 0; | |
9392 | continue; | |
9393 | } | |
9394 | ||
9395 | irela->r_offset += o->output_offset; | |
9396 | ||
9397 | /* Relocs in an executable have to be virtual addresses. */ | |
9398 | if (!finfo->info->relocatable) | |
9399 | irela->r_offset += o->output_section->vma; | |
9400 | ||
9401 | last_offset = irela->r_offset; | |
9402 | ||
9403 | r_symndx = irela->r_info >> r_sym_shift; | |
9404 | if (r_symndx == STN_UNDEF) | |
9405 | continue; | |
9406 | ||
9407 | if (r_symndx >= locsymcount | |
9408 | || (elf_bad_symtab (input_bfd) | |
9409 | && finfo->sections[r_symndx] == NULL)) | |
9410 | { | |
9411 | struct elf_link_hash_entry *rh; | |
9412 | unsigned long indx; | |
9413 | ||
9414 | /* This is a reloc against a global symbol. We | |
9415 | have not yet output all the local symbols, so | |
9416 | we do not know the symbol index of any global | |
9417 | symbol. We set the rel_hash entry for this | |
9418 | reloc to point to the global hash table entry | |
9419 | for this symbol. The symbol index is then | |
ee75fd95 | 9420 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
9421 | indx = r_symndx - extsymoff; |
9422 | rh = elf_sym_hashes (input_bfd)[indx]; | |
9423 | while (rh->root.type == bfd_link_hash_indirect | |
9424 | || rh->root.type == bfd_link_hash_warning) | |
9425 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
9426 | ||
9427 | /* Setting the index to -2 tells | |
9428 | elf_link_output_extsym that this symbol is | |
9429 | used by a reloc. */ | |
9430 | BFD_ASSERT (rh->indx < 0); | |
9431 | rh->indx = -2; | |
9432 | ||
9433 | *rel_hash = rh; | |
9434 | ||
9435 | continue; | |
9436 | } | |
9437 | ||
9438 | /* This is a reloc against a local symbol. */ | |
9439 | ||
9440 | *rel_hash = NULL; | |
9441 | sym = isymbuf[r_symndx]; | |
9442 | sec = finfo->sections[r_symndx]; | |
9443 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) | |
9444 | { | |
9445 | /* I suppose the backend ought to fill in the | |
9446 | section of any STT_SECTION symbol against a | |
6a8d1586 AM |
9447 | processor specific section. */ |
9448 | r_symndx = 0; | |
9449 | if (bfd_is_abs_section (sec)) | |
9450 | ; | |
c152c796 AM |
9451 | else if (sec == NULL || sec->owner == NULL) |
9452 | { | |
9453 | bfd_set_error (bfd_error_bad_value); | |
9454 | return FALSE; | |
9455 | } | |
9456 | else | |
9457 | { | |
6a8d1586 AM |
9458 | asection *osec = sec->output_section; |
9459 | ||
9460 | /* If we have discarded a section, the output | |
9461 | section will be the absolute section. In | |
ab96bf03 AM |
9462 | case of discarded SEC_MERGE sections, use |
9463 | the kept section. relocate_section should | |
9464 | have already handled discarded linkonce | |
9465 | sections. */ | |
6a8d1586 AM |
9466 | if (bfd_is_abs_section (osec) |
9467 | && sec->kept_section != NULL | |
9468 | && sec->kept_section->output_section != NULL) | |
9469 | { | |
9470 | osec = sec->kept_section->output_section; | |
9471 | irela->r_addend -= osec->vma; | |
9472 | } | |
9473 | ||
9474 | if (!bfd_is_abs_section (osec)) | |
9475 | { | |
9476 | r_symndx = osec->target_index; | |
74541ad4 AM |
9477 | if (r_symndx == 0) |
9478 | { | |
9479 | struct elf_link_hash_table *htab; | |
9480 | asection *oi; | |
9481 | ||
9482 | htab = elf_hash_table (finfo->info); | |
9483 | oi = htab->text_index_section; | |
9484 | if ((osec->flags & SEC_READONLY) == 0 | |
9485 | && htab->data_index_section != NULL) | |
9486 | oi = htab->data_index_section; | |
9487 | ||
9488 | if (oi != NULL) | |
9489 | { | |
9490 | irela->r_addend += osec->vma - oi->vma; | |
9491 | r_symndx = oi->target_index; | |
9492 | } | |
9493 | } | |
9494 | ||
6a8d1586 AM |
9495 | BFD_ASSERT (r_symndx != 0); |
9496 | } | |
c152c796 AM |
9497 | } |
9498 | ||
9499 | /* Adjust the addend according to where the | |
9500 | section winds up in the output section. */ | |
9501 | if (rela_normal) | |
9502 | irela->r_addend += sec->output_offset; | |
9503 | } | |
9504 | else | |
9505 | { | |
9506 | if (finfo->indices[r_symndx] == -1) | |
9507 | { | |
9508 | unsigned long shlink; | |
9509 | const char *name; | |
9510 | asection *osec; | |
9511 | ||
9512 | if (finfo->info->strip == strip_all) | |
9513 | { | |
9514 | /* You can't do ld -r -s. */ | |
9515 | bfd_set_error (bfd_error_invalid_operation); | |
9516 | return FALSE; | |
9517 | } | |
9518 | ||
9519 | /* This symbol was skipped earlier, but | |
9520 | since it is needed by a reloc, we | |
9521 | must output it now. */ | |
9522 | shlink = symtab_hdr->sh_link; | |
9523 | name = (bfd_elf_string_from_elf_section | |
9524 | (input_bfd, shlink, sym.st_name)); | |
9525 | if (name == NULL) | |
9526 | return FALSE; | |
9527 | ||
9528 | osec = sec->output_section; | |
9529 | sym.st_shndx = | |
9530 | _bfd_elf_section_from_bfd_section (output_bfd, | |
9531 | osec); | |
9532 | if (sym.st_shndx == SHN_BAD) | |
9533 | return FALSE; | |
9534 | ||
9535 | sym.st_value += sec->output_offset; | |
9536 | if (! finfo->info->relocatable) | |
9537 | { | |
9538 | sym.st_value += osec->vma; | |
9539 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
9540 | { | |
9541 | /* STT_TLS symbols are relative to PT_TLS | |
9542 | segment base. */ | |
9543 | BFD_ASSERT (elf_hash_table (finfo->info) | |
9544 | ->tls_sec != NULL); | |
9545 | sym.st_value -= (elf_hash_table (finfo->info) | |
9546 | ->tls_sec->vma); | |
9547 | } | |
9548 | } | |
9549 | ||
9550 | finfo->indices[r_symndx] | |
9551 | = bfd_get_symcount (output_bfd); | |
9552 | ||
9553 | if (! elf_link_output_sym (finfo, name, &sym, sec, | |
9554 | NULL)) | |
9555 | return FALSE; | |
9556 | } | |
9557 | ||
9558 | r_symndx = finfo->indices[r_symndx]; | |
9559 | } | |
9560 | ||
9561 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
9562 | | (irela->r_info & r_type_mask)); | |
9563 | } | |
9564 | ||
9565 | /* Swap out the relocs. */ | |
c152c796 | 9566 | if (input_rel_hdr->sh_size != 0 |
eac338cf PB |
9567 | && !bed->elf_backend_emit_relocs (output_bfd, o, |
9568 | input_rel_hdr, | |
9569 | internal_relocs, | |
9570 | rel_hash_list)) | |
c152c796 AM |
9571 | return FALSE; |
9572 | ||
9573 | input_rel_hdr2 = elf_section_data (o)->rel_hdr2; | |
9574 | if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0) | |
9575 | { | |
9576 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) | |
9577 | * bed->s->int_rels_per_ext_rel); | |
eac338cf PB |
9578 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
9579 | if (!bed->elf_backend_emit_relocs (output_bfd, o, | |
9580 | input_rel_hdr2, | |
9581 | internal_relocs, | |
9582 | rel_hash_list)) | |
c152c796 AM |
9583 | return FALSE; |
9584 | } | |
9585 | } | |
9586 | } | |
9587 | ||
9588 | /* Write out the modified section contents. */ | |
9589 | if (bed->elf_backend_write_section | |
c7b8f16e JB |
9590 | && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o, |
9591 | contents)) | |
c152c796 AM |
9592 | { |
9593 | /* Section written out. */ | |
9594 | } | |
9595 | else switch (o->sec_info_type) | |
9596 | { | |
9597 | case ELF_INFO_TYPE_STABS: | |
9598 | if (! (_bfd_write_section_stabs | |
9599 | (output_bfd, | |
9600 | &elf_hash_table (finfo->info)->stab_info, | |
9601 | o, &elf_section_data (o)->sec_info, contents))) | |
9602 | return FALSE; | |
9603 | break; | |
9604 | case ELF_INFO_TYPE_MERGE: | |
9605 | if (! _bfd_write_merged_section (output_bfd, o, | |
9606 | elf_section_data (o)->sec_info)) | |
9607 | return FALSE; | |
9608 | break; | |
9609 | case ELF_INFO_TYPE_EH_FRAME: | |
9610 | { | |
9611 | if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, | |
9612 | o, contents)) | |
9613 | return FALSE; | |
9614 | } | |
9615 | break; | |
9616 | default: | |
9617 | { | |
c152c796 | 9618 | if (! (o->flags & SEC_EXCLUDE) |
ace79388 | 9619 | && ! (o->output_section->flags & SEC_NEVER_LOAD) |
c152c796 AM |
9620 | && ! bfd_set_section_contents (output_bfd, o->output_section, |
9621 | contents, | |
9622 | (file_ptr) o->output_offset, | |
eea6121a | 9623 | o->size)) |
c152c796 AM |
9624 | return FALSE; |
9625 | } | |
9626 | break; | |
9627 | } | |
9628 | } | |
9629 | ||
9630 | return TRUE; | |
9631 | } | |
9632 | ||
9633 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 9634 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
9635 | is used to build constructor and destructor tables when linking |
9636 | with -Ur. */ | |
9637 | ||
9638 | static bfd_boolean | |
9639 | elf_reloc_link_order (bfd *output_bfd, | |
9640 | struct bfd_link_info *info, | |
9641 | asection *output_section, | |
9642 | struct bfd_link_order *link_order) | |
9643 | { | |
9644 | reloc_howto_type *howto; | |
9645 | long indx; | |
9646 | bfd_vma offset; | |
9647 | bfd_vma addend; | |
9648 | struct elf_link_hash_entry **rel_hash_ptr; | |
9649 | Elf_Internal_Shdr *rel_hdr; | |
9650 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
9651 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
9652 | bfd_byte *erel; | |
9653 | unsigned int i; | |
9654 | ||
9655 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
9656 | if (howto == NULL) | |
9657 | { | |
9658 | bfd_set_error (bfd_error_bad_value); | |
9659 | return FALSE; | |
9660 | } | |
9661 | ||
9662 | addend = link_order->u.reloc.p->addend; | |
9663 | ||
9664 | /* Figure out the symbol index. */ | |
9665 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
9666 | + elf_section_data (output_section)->rel_count | |
9667 | + elf_section_data (output_section)->rel_count2); | |
9668 | if (link_order->type == bfd_section_reloc_link_order) | |
9669 | { | |
9670 | indx = link_order->u.reloc.p->u.section->target_index; | |
9671 | BFD_ASSERT (indx != 0); | |
9672 | *rel_hash_ptr = NULL; | |
9673 | } | |
9674 | else | |
9675 | { | |
9676 | struct elf_link_hash_entry *h; | |
9677 | ||
9678 | /* Treat a reloc against a defined symbol as though it were | |
9679 | actually against the section. */ | |
9680 | h = ((struct elf_link_hash_entry *) | |
9681 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
9682 | link_order->u.reloc.p->u.name, | |
9683 | FALSE, FALSE, TRUE)); | |
9684 | if (h != NULL | |
9685 | && (h->root.type == bfd_link_hash_defined | |
9686 | || h->root.type == bfd_link_hash_defweak)) | |
9687 | { | |
9688 | asection *section; | |
9689 | ||
9690 | section = h->root.u.def.section; | |
9691 | indx = section->output_section->target_index; | |
9692 | *rel_hash_ptr = NULL; | |
9693 | /* It seems that we ought to add the symbol value to the | |
9694 | addend here, but in practice it has already been added | |
9695 | because it was passed to constructor_callback. */ | |
9696 | addend += section->output_section->vma + section->output_offset; | |
9697 | } | |
9698 | else if (h != NULL) | |
9699 | { | |
9700 | /* Setting the index to -2 tells elf_link_output_extsym that | |
9701 | this symbol is used by a reloc. */ | |
9702 | h->indx = -2; | |
9703 | *rel_hash_ptr = h; | |
9704 | indx = 0; | |
9705 | } | |
9706 | else | |
9707 | { | |
9708 | if (! ((*info->callbacks->unattached_reloc) | |
9709 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) | |
9710 | return FALSE; | |
9711 | indx = 0; | |
9712 | } | |
9713 | } | |
9714 | ||
9715 | /* If this is an inplace reloc, we must write the addend into the | |
9716 | object file. */ | |
9717 | if (howto->partial_inplace && addend != 0) | |
9718 | { | |
9719 | bfd_size_type size; | |
9720 | bfd_reloc_status_type rstat; | |
9721 | bfd_byte *buf; | |
9722 | bfd_boolean ok; | |
9723 | const char *sym_name; | |
9724 | ||
9725 | size = bfd_get_reloc_size (howto); | |
9726 | buf = bfd_zmalloc (size); | |
9727 | if (buf == NULL) | |
9728 | return FALSE; | |
9729 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
9730 | switch (rstat) | |
9731 | { | |
9732 | case bfd_reloc_ok: | |
9733 | break; | |
9734 | ||
9735 | default: | |
9736 | case bfd_reloc_outofrange: | |
9737 | abort (); | |
9738 | ||
9739 | case bfd_reloc_overflow: | |
9740 | if (link_order->type == bfd_section_reloc_link_order) | |
9741 | sym_name = bfd_section_name (output_bfd, | |
9742 | link_order->u.reloc.p->u.section); | |
9743 | else | |
9744 | sym_name = link_order->u.reloc.p->u.name; | |
9745 | if (! ((*info->callbacks->reloc_overflow) | |
dfeffb9f L |
9746 | (info, NULL, sym_name, howto->name, addend, NULL, |
9747 | NULL, (bfd_vma) 0))) | |
c152c796 AM |
9748 | { |
9749 | free (buf); | |
9750 | return FALSE; | |
9751 | } | |
9752 | break; | |
9753 | } | |
9754 | ok = bfd_set_section_contents (output_bfd, output_section, buf, | |
9755 | link_order->offset, size); | |
9756 | free (buf); | |
9757 | if (! ok) | |
9758 | return FALSE; | |
9759 | } | |
9760 | ||
9761 | /* The address of a reloc is relative to the section in a | |
9762 | relocatable file, and is a virtual address in an executable | |
9763 | file. */ | |
9764 | offset = link_order->offset; | |
9765 | if (! info->relocatable) | |
9766 | offset += output_section->vma; | |
9767 | ||
9768 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
9769 | { | |
9770 | irel[i].r_offset = offset; | |
9771 | irel[i].r_info = 0; | |
9772 | irel[i].r_addend = 0; | |
9773 | } | |
9774 | if (bed->s->arch_size == 32) | |
9775 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
9776 | else | |
9777 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
9778 | ||
9779 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
9780 | erel = rel_hdr->contents; | |
9781 | if (rel_hdr->sh_type == SHT_REL) | |
9782 | { | |
9783 | erel += (elf_section_data (output_section)->rel_count | |
9784 | * bed->s->sizeof_rel); | |
9785 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); | |
9786 | } | |
9787 | else | |
9788 | { | |
9789 | irel[0].r_addend = addend; | |
9790 | erel += (elf_section_data (output_section)->rel_count | |
9791 | * bed->s->sizeof_rela); | |
9792 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); | |
9793 | } | |
9794 | ||
9795 | ++elf_section_data (output_section)->rel_count; | |
9796 | ||
9797 | return TRUE; | |
9798 | } | |
9799 | ||
0b52efa6 PB |
9800 | |
9801 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
9802 | ||
9803 | static bfd_vma | |
9804 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
9805 | { | |
9806 | Elf_Internal_Shdr **elf_shdrp; | |
9807 | asection *s; | |
9808 | int elfsec; | |
9809 | ||
9810 | s = p->u.indirect.section; | |
9811 | elf_shdrp = elf_elfsections (s->owner); | |
9812 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
9813 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
9814 | /* PR 290: |
9815 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 9816 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
9817 | sh_info fields. Hence we could get the situation |
9818 | where elfsec is 0. */ | |
9819 | if (elfsec == 0) | |
9820 | { | |
9821 | const struct elf_backend_data *bed | |
9822 | = get_elf_backend_data (s->owner); | |
9823 | if (bed->link_order_error_handler) | |
d003868e AM |
9824 | bed->link_order_error_handler |
9825 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); | |
185d09ad L |
9826 | return 0; |
9827 | } | |
9828 | else | |
9829 | { | |
9830 | s = elf_shdrp[elfsec]->bfd_section; | |
9831 | return s->output_section->vma + s->output_offset; | |
9832 | } | |
0b52efa6 PB |
9833 | } |
9834 | ||
9835 | ||
9836 | /* Compare two sections based on the locations of the sections they are | |
9837 | linked to. Used by elf_fixup_link_order. */ | |
9838 | ||
9839 | static int | |
9840 | compare_link_order (const void * a, const void * b) | |
9841 | { | |
9842 | bfd_vma apos; | |
9843 | bfd_vma bpos; | |
9844 | ||
9845 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
9846 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
9847 | if (apos < bpos) | |
9848 | return -1; | |
9849 | return apos > bpos; | |
9850 | } | |
9851 | ||
9852 | ||
9853 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
9854 | order as their linked sections. Returns false if this could not be done | |
9855 | because an output section includes both ordered and unordered | |
9856 | sections. Ideally we'd do this in the linker proper. */ | |
9857 | ||
9858 | static bfd_boolean | |
9859 | elf_fixup_link_order (bfd *abfd, asection *o) | |
9860 | { | |
9861 | int seen_linkorder; | |
9862 | int seen_other; | |
9863 | int n; | |
9864 | struct bfd_link_order *p; | |
9865 | bfd *sub; | |
9866 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 9867 | unsigned elfsec; |
0b52efa6 | 9868 | struct bfd_link_order **sections; |
d33cdfe3 | 9869 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 9870 | bfd_vma offset; |
3b36f7e6 | 9871 | |
d33cdfe3 L |
9872 | other_sec = NULL; |
9873 | linkorder_sec = NULL; | |
0b52efa6 PB |
9874 | seen_other = 0; |
9875 | seen_linkorder = 0; | |
8423293d | 9876 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 9877 | { |
d33cdfe3 | 9878 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
9879 | { |
9880 | s = p->u.indirect.section; | |
d33cdfe3 L |
9881 | sub = s->owner; |
9882 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
9883 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
9884 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
9885 | && elfsec < elf_numsections (sub) | |
4fbb74a6 AM |
9886 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER |
9887 | && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) | |
d33cdfe3 L |
9888 | { |
9889 | seen_linkorder++; | |
9890 | linkorder_sec = s; | |
9891 | } | |
0b52efa6 | 9892 | else |
d33cdfe3 L |
9893 | { |
9894 | seen_other++; | |
9895 | other_sec = s; | |
9896 | } | |
0b52efa6 PB |
9897 | } |
9898 | else | |
9899 | seen_other++; | |
d33cdfe3 L |
9900 | |
9901 | if (seen_other && seen_linkorder) | |
9902 | { | |
9903 | if (other_sec && linkorder_sec) | |
9904 | (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"), | |
9905 | o, linkorder_sec, | |
9906 | linkorder_sec->owner, other_sec, | |
9907 | other_sec->owner); | |
9908 | else | |
9909 | (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), | |
9910 | o); | |
9911 | bfd_set_error (bfd_error_bad_value); | |
9912 | return FALSE; | |
9913 | } | |
0b52efa6 PB |
9914 | } |
9915 | ||
9916 | if (!seen_linkorder) | |
9917 | return TRUE; | |
9918 | ||
0b52efa6 | 9919 | sections = (struct bfd_link_order **) |
14b1c01e AM |
9920 | bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); |
9921 | if (sections == NULL) | |
9922 | return FALSE; | |
0b52efa6 | 9923 | seen_linkorder = 0; |
3b36f7e6 | 9924 | |
8423293d | 9925 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
9926 | { |
9927 | sections[seen_linkorder++] = p; | |
9928 | } | |
9929 | /* Sort the input sections in the order of their linked section. */ | |
9930 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
9931 | compare_link_order); | |
9932 | ||
9933 | /* Change the offsets of the sections. */ | |
9934 | offset = 0; | |
9935 | for (n = 0; n < seen_linkorder; n++) | |
9936 | { | |
9937 | s = sections[n]->u.indirect.section; | |
461686a3 | 9938 | offset &= ~(bfd_vma) 0 << s->alignment_power; |
0b52efa6 PB |
9939 | s->output_offset = offset; |
9940 | sections[n]->offset = offset; | |
9941 | offset += sections[n]->size; | |
9942 | } | |
9943 | ||
4dd07732 | 9944 | free (sections); |
0b52efa6 PB |
9945 | return TRUE; |
9946 | } | |
9947 | ||
9948 | ||
c152c796 AM |
9949 | /* Do the final step of an ELF link. */ |
9950 | ||
9951 | bfd_boolean | |
9952 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
9953 | { | |
9954 | bfd_boolean dynamic; | |
9955 | bfd_boolean emit_relocs; | |
9956 | bfd *dynobj; | |
9957 | struct elf_final_link_info finfo; | |
9958 | register asection *o; | |
9959 | register struct bfd_link_order *p; | |
9960 | register bfd *sub; | |
9961 | bfd_size_type max_contents_size; | |
9962 | bfd_size_type max_external_reloc_size; | |
9963 | bfd_size_type max_internal_reloc_count; | |
9964 | bfd_size_type max_sym_count; | |
9965 | bfd_size_type max_sym_shndx_count; | |
9966 | file_ptr off; | |
9967 | Elf_Internal_Sym elfsym; | |
9968 | unsigned int i; | |
9969 | Elf_Internal_Shdr *symtab_hdr; | |
9970 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
9971 | Elf_Internal_Shdr *symstrtab_hdr; | |
9972 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9973 | struct elf_outext_info eoinfo; | |
9974 | bfd_boolean merged; | |
9975 | size_t relativecount = 0; | |
9976 | asection *reldyn = 0; | |
9977 | bfd_size_type amt; | |
104d59d1 JM |
9978 | asection *attr_section = NULL; |
9979 | bfd_vma attr_size = 0; | |
9980 | const char *std_attrs_section; | |
c152c796 AM |
9981 | |
9982 | if (! is_elf_hash_table (info->hash)) | |
9983 | return FALSE; | |
9984 | ||
9985 | if (info->shared) | |
9986 | abfd->flags |= DYNAMIC; | |
9987 | ||
9988 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
9989 | dynobj = elf_hash_table (info)->dynobj; | |
9990 | ||
9991 | emit_relocs = (info->relocatable | |
a4676736 | 9992 | || info->emitrelocations); |
c152c796 AM |
9993 | |
9994 | finfo.info = info; | |
9995 | finfo.output_bfd = abfd; | |
9996 | finfo.symstrtab = _bfd_elf_stringtab_init (); | |
9997 | if (finfo.symstrtab == NULL) | |
9998 | return FALSE; | |
9999 | ||
10000 | if (! dynamic) | |
10001 | { | |
10002 | finfo.dynsym_sec = NULL; | |
10003 | finfo.hash_sec = NULL; | |
10004 | finfo.symver_sec = NULL; | |
10005 | } | |
10006 | else | |
10007 | { | |
10008 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
10009 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
fdc90cb4 | 10010 | BFD_ASSERT (finfo.dynsym_sec != NULL); |
c152c796 AM |
10011 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); |
10012 | /* Note that it is OK if symver_sec is NULL. */ | |
10013 | } | |
10014 | ||
10015 | finfo.contents = NULL; | |
10016 | finfo.external_relocs = NULL; | |
10017 | finfo.internal_relocs = NULL; | |
10018 | finfo.external_syms = NULL; | |
10019 | finfo.locsym_shndx = NULL; | |
10020 | finfo.internal_syms = NULL; | |
10021 | finfo.indices = NULL; | |
10022 | finfo.sections = NULL; | |
10023 | finfo.symbuf = NULL; | |
10024 | finfo.symshndxbuf = NULL; | |
10025 | finfo.symbuf_count = 0; | |
10026 | finfo.shndxbuf_size = 0; | |
10027 | ||
104d59d1 JM |
10028 | /* The object attributes have been merged. Remove the input |
10029 | sections from the link, and set the contents of the output | |
10030 | secton. */ | |
10031 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; | |
10032 | for (o = abfd->sections; o != NULL; o = o->next) | |
10033 | { | |
10034 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) | |
10035 | || strcmp (o->name, ".gnu.attributes") == 0) | |
10036 | { | |
10037 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
10038 | { | |
10039 | asection *input_section; | |
10040 | ||
10041 | if (p->type != bfd_indirect_link_order) | |
10042 | continue; | |
10043 | input_section = p->u.indirect.section; | |
10044 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
10045 | elf_link_input_bfd ignores this section. */ | |
10046 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
10047 | } | |
a0c8462f | 10048 | |
104d59d1 JM |
10049 | attr_size = bfd_elf_obj_attr_size (abfd); |
10050 | if (attr_size) | |
10051 | { | |
10052 | bfd_set_section_size (abfd, o, attr_size); | |
10053 | attr_section = o; | |
10054 | /* Skip this section later on. */ | |
10055 | o->map_head.link_order = NULL; | |
10056 | } | |
10057 | else | |
10058 | o->flags |= SEC_EXCLUDE; | |
10059 | } | |
10060 | } | |
10061 | ||
c152c796 AM |
10062 | /* Count up the number of relocations we will output for each output |
10063 | section, so that we know the sizes of the reloc sections. We | |
10064 | also figure out some maximum sizes. */ | |
10065 | max_contents_size = 0; | |
10066 | max_external_reloc_size = 0; | |
10067 | max_internal_reloc_count = 0; | |
10068 | max_sym_count = 0; | |
10069 | max_sym_shndx_count = 0; | |
10070 | merged = FALSE; | |
10071 | for (o = abfd->sections; o != NULL; o = o->next) | |
10072 | { | |
10073 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
10074 | o->reloc_count = 0; | |
10075 | ||
8423293d | 10076 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
10077 | { |
10078 | unsigned int reloc_count = 0; | |
10079 | struct bfd_elf_section_data *esdi = NULL; | |
10080 | unsigned int *rel_count1; | |
10081 | ||
10082 | if (p->type == bfd_section_reloc_link_order | |
10083 | || p->type == bfd_symbol_reloc_link_order) | |
10084 | reloc_count = 1; | |
10085 | else if (p->type == bfd_indirect_link_order) | |
10086 | { | |
10087 | asection *sec; | |
10088 | ||
10089 | sec = p->u.indirect.section; | |
10090 | esdi = elf_section_data (sec); | |
10091 | ||
10092 | /* Mark all sections which are to be included in the | |
10093 | link. This will normally be every section. We need | |
10094 | to do this so that we can identify any sections which | |
10095 | the linker has decided to not include. */ | |
10096 | sec->linker_mark = TRUE; | |
10097 | ||
10098 | if (sec->flags & SEC_MERGE) | |
10099 | merged = TRUE; | |
10100 | ||
10101 | if (info->relocatable || info->emitrelocations) | |
10102 | reloc_count = sec->reloc_count; | |
10103 | else if (bed->elf_backend_count_relocs) | |
58217f29 | 10104 | reloc_count = (*bed->elf_backend_count_relocs) (info, sec); |
c152c796 | 10105 | |
eea6121a AM |
10106 | if (sec->rawsize > max_contents_size) |
10107 | max_contents_size = sec->rawsize; | |
10108 | if (sec->size > max_contents_size) | |
10109 | max_contents_size = sec->size; | |
c152c796 AM |
10110 | |
10111 | /* We are interested in just local symbols, not all | |
10112 | symbols. */ | |
10113 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
10114 | && (sec->owner->flags & DYNAMIC) == 0) | |
10115 | { | |
10116 | size_t sym_count; | |
10117 | ||
10118 | if (elf_bad_symtab (sec->owner)) | |
10119 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
10120 | / bed->s->sizeof_sym); | |
10121 | else | |
10122 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
10123 | ||
10124 | if (sym_count > max_sym_count) | |
10125 | max_sym_count = sym_count; | |
10126 | ||
10127 | if (sym_count > max_sym_shndx_count | |
10128 | && elf_symtab_shndx (sec->owner) != 0) | |
10129 | max_sym_shndx_count = sym_count; | |
10130 | ||
10131 | if ((sec->flags & SEC_RELOC) != 0) | |
10132 | { | |
10133 | size_t ext_size; | |
10134 | ||
10135 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
10136 | if (ext_size > max_external_reloc_size) | |
10137 | max_external_reloc_size = ext_size; | |
10138 | if (sec->reloc_count > max_internal_reloc_count) | |
10139 | max_internal_reloc_count = sec->reloc_count; | |
10140 | } | |
10141 | } | |
10142 | } | |
10143 | ||
10144 | if (reloc_count == 0) | |
10145 | continue; | |
10146 | ||
10147 | o->reloc_count += reloc_count; | |
10148 | ||
10149 | /* MIPS may have a mix of REL and RELA relocs on sections. | |
10150 | To support this curious ABI we keep reloc counts in | |
10151 | elf_section_data too. We must be careful to add the | |
10152 | relocations from the input section to the right output | |
10153 | count. FIXME: Get rid of one count. We have | |
10154 | o->reloc_count == esdo->rel_count + esdo->rel_count2. */ | |
10155 | rel_count1 = &esdo->rel_count; | |
10156 | if (esdi != NULL) | |
10157 | { | |
10158 | bfd_boolean same_size; | |
10159 | bfd_size_type entsize1; | |
10160 | ||
10161 | entsize1 = esdi->rel_hdr.sh_entsize; | |
10162 | BFD_ASSERT (entsize1 == bed->s->sizeof_rel | |
10163 | || entsize1 == bed->s->sizeof_rela); | |
10164 | same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel); | |
10165 | ||
10166 | if (!same_size) | |
10167 | rel_count1 = &esdo->rel_count2; | |
10168 | ||
10169 | if (esdi->rel_hdr2 != NULL) | |
10170 | { | |
10171 | bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize; | |
10172 | unsigned int alt_count; | |
10173 | unsigned int *rel_count2; | |
10174 | ||
10175 | BFD_ASSERT (entsize2 != entsize1 | |
10176 | && (entsize2 == bed->s->sizeof_rel | |
10177 | || entsize2 == bed->s->sizeof_rela)); | |
10178 | ||
10179 | rel_count2 = &esdo->rel_count2; | |
10180 | if (!same_size) | |
10181 | rel_count2 = &esdo->rel_count; | |
10182 | ||
10183 | /* The following is probably too simplistic if the | |
10184 | backend counts output relocs unusually. */ | |
10185 | BFD_ASSERT (bed->elf_backend_count_relocs == NULL); | |
10186 | alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2); | |
10187 | *rel_count2 += alt_count; | |
10188 | reloc_count -= alt_count; | |
10189 | } | |
10190 | } | |
10191 | *rel_count1 += reloc_count; | |
10192 | } | |
10193 | ||
10194 | if (o->reloc_count > 0) | |
10195 | o->flags |= SEC_RELOC; | |
10196 | else | |
10197 | { | |
10198 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
10199 | set it (this is probably a bug) and if it is set | |
10200 | assign_section_numbers will create a reloc section. */ | |
10201 | o->flags &=~ SEC_RELOC; | |
10202 | } | |
10203 | ||
10204 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
10205 | zero. This is done in elf_fake_sections as well, but forcing | |
10206 | the VMA to 0 here will ensure that relocs against these | |
10207 | sections are handled correctly. */ | |
10208 | if ((o->flags & SEC_ALLOC) == 0 | |
10209 | && ! o->user_set_vma) | |
10210 | o->vma = 0; | |
10211 | } | |
10212 | ||
10213 | if (! info->relocatable && merged) | |
10214 | elf_link_hash_traverse (elf_hash_table (info), | |
10215 | _bfd_elf_link_sec_merge_syms, abfd); | |
10216 | ||
10217 | /* Figure out the file positions for everything but the symbol table | |
10218 | and the relocs. We set symcount to force assign_section_numbers | |
10219 | to create a symbol table. */ | |
10220 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; | |
10221 | BFD_ASSERT (! abfd->output_has_begun); | |
10222 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
10223 | goto error_return; | |
10224 | ||
ee75fd95 | 10225 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
10226 | for (o = abfd->sections; o != NULL; o = o->next) |
10227 | { | |
10228 | if ((o->flags & SEC_RELOC) != 0) | |
10229 | { | |
10230 | if (!(_bfd_elf_link_size_reloc_section | |
10231 | (abfd, &elf_section_data (o)->rel_hdr, o))) | |
10232 | goto error_return; | |
10233 | ||
10234 | if (elf_section_data (o)->rel_hdr2 | |
10235 | && !(_bfd_elf_link_size_reloc_section | |
10236 | (abfd, elf_section_data (o)->rel_hdr2, o))) | |
10237 | goto error_return; | |
10238 | } | |
10239 | ||
10240 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
10241 | to count upwards while actually outputting the relocations. */ | |
10242 | elf_section_data (o)->rel_count = 0; | |
10243 | elf_section_data (o)->rel_count2 = 0; | |
10244 | } | |
10245 | ||
10246 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
10247 | ||
10248 | /* We have now assigned file positions for all the sections except | |
10249 | .symtab and .strtab. We start the .symtab section at the current | |
10250 | file position, and write directly to it. We build the .strtab | |
10251 | section in memory. */ | |
10252 | bfd_get_symcount (abfd) = 0; | |
10253 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
10254 | /* sh_name is set in prep_headers. */ | |
10255 | symtab_hdr->sh_type = SHT_SYMTAB; | |
10256 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
10257 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
10258 | /* sh_link is set in assign_section_numbers. */ | |
10259 | /* sh_info is set below. */ | |
10260 | /* sh_offset is set just below. */ | |
72de5009 | 10261 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
c152c796 AM |
10262 | |
10263 | off = elf_tdata (abfd)->next_file_pos; | |
10264 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
10265 | ||
10266 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
10267 | incorrect. We do not yet know the size of the .symtab section. | |
10268 | We correct next_file_pos below, after we do know the size. */ | |
10269 | ||
10270 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
10271 | continuously seeking to the right position in the file. */ | |
10272 | if (! info->keep_memory || max_sym_count < 20) | |
10273 | finfo.symbuf_size = 20; | |
10274 | else | |
10275 | finfo.symbuf_size = max_sym_count; | |
10276 | amt = finfo.symbuf_size; | |
10277 | amt *= bed->s->sizeof_sym; | |
10278 | finfo.symbuf = bfd_malloc (amt); | |
10279 | if (finfo.symbuf == NULL) | |
10280 | goto error_return; | |
4fbb74a6 | 10281 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
c152c796 AM |
10282 | { |
10283 | /* Wild guess at number of output symbols. realloc'd as needed. */ | |
10284 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
10285 | finfo.shndxbuf_size = amt; | |
10286 | amt *= sizeof (Elf_External_Sym_Shndx); | |
10287 | finfo.symshndxbuf = bfd_zmalloc (amt); | |
10288 | if (finfo.symshndxbuf == NULL) | |
10289 | goto error_return; | |
10290 | } | |
10291 | ||
10292 | /* Start writing out the symbol table. The first symbol is always a | |
10293 | dummy symbol. */ | |
10294 | if (info->strip != strip_all | |
10295 | || emit_relocs) | |
10296 | { | |
10297 | elfsym.st_value = 0; | |
10298 | elfsym.st_size = 0; | |
10299 | elfsym.st_info = 0; | |
10300 | elfsym.st_other = 0; | |
10301 | elfsym.st_shndx = SHN_UNDEF; | |
10302 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr, | |
10303 | NULL)) | |
10304 | goto error_return; | |
10305 | } | |
10306 | ||
c152c796 AM |
10307 | /* Output a symbol for each section. We output these even if we are |
10308 | discarding local symbols, since they are used for relocs. These | |
10309 | symbols have no names. We store the index of each one in the | |
10310 | index field of the section, so that we can find it again when | |
10311 | outputting relocs. */ | |
10312 | if (info->strip != strip_all | |
10313 | || emit_relocs) | |
10314 | { | |
10315 | elfsym.st_size = 0; | |
10316 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
10317 | elfsym.st_other = 0; | |
f0b5bb34 | 10318 | elfsym.st_value = 0; |
c152c796 AM |
10319 | for (i = 1; i < elf_numsections (abfd); i++) |
10320 | { | |
10321 | o = bfd_section_from_elf_index (abfd, i); | |
10322 | if (o != NULL) | |
f0b5bb34 AM |
10323 | { |
10324 | o->target_index = bfd_get_symcount (abfd); | |
10325 | elfsym.st_shndx = i; | |
10326 | if (!info->relocatable) | |
10327 | elfsym.st_value = o->vma; | |
10328 | if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL)) | |
10329 | goto error_return; | |
10330 | } | |
c152c796 AM |
10331 | } |
10332 | } | |
10333 | ||
10334 | /* Allocate some memory to hold information read in from the input | |
10335 | files. */ | |
10336 | if (max_contents_size != 0) | |
10337 | { | |
10338 | finfo.contents = bfd_malloc (max_contents_size); | |
10339 | if (finfo.contents == NULL) | |
10340 | goto error_return; | |
10341 | } | |
10342 | ||
10343 | if (max_external_reloc_size != 0) | |
10344 | { | |
10345 | finfo.external_relocs = bfd_malloc (max_external_reloc_size); | |
10346 | if (finfo.external_relocs == NULL) | |
10347 | goto error_return; | |
10348 | } | |
10349 | ||
10350 | if (max_internal_reloc_count != 0) | |
10351 | { | |
10352 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
10353 | amt *= sizeof (Elf_Internal_Rela); | |
10354 | finfo.internal_relocs = bfd_malloc (amt); | |
10355 | if (finfo.internal_relocs == NULL) | |
10356 | goto error_return; | |
10357 | } | |
10358 | ||
10359 | if (max_sym_count != 0) | |
10360 | { | |
10361 | amt = max_sym_count * bed->s->sizeof_sym; | |
10362 | finfo.external_syms = bfd_malloc (amt); | |
10363 | if (finfo.external_syms == NULL) | |
10364 | goto error_return; | |
10365 | ||
10366 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
10367 | finfo.internal_syms = bfd_malloc (amt); | |
10368 | if (finfo.internal_syms == NULL) | |
10369 | goto error_return; | |
10370 | ||
10371 | amt = max_sym_count * sizeof (long); | |
10372 | finfo.indices = bfd_malloc (amt); | |
10373 | if (finfo.indices == NULL) | |
10374 | goto error_return; | |
10375 | ||
10376 | amt = max_sym_count * sizeof (asection *); | |
10377 | finfo.sections = bfd_malloc (amt); | |
10378 | if (finfo.sections == NULL) | |
10379 | goto error_return; | |
10380 | } | |
10381 | ||
10382 | if (max_sym_shndx_count != 0) | |
10383 | { | |
10384 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
10385 | finfo.locsym_shndx = bfd_malloc (amt); | |
10386 | if (finfo.locsym_shndx == NULL) | |
10387 | goto error_return; | |
10388 | } | |
10389 | ||
10390 | if (elf_hash_table (info)->tls_sec) | |
10391 | { | |
10392 | bfd_vma base, end = 0; | |
10393 | asection *sec; | |
10394 | ||
10395 | for (sec = elf_hash_table (info)->tls_sec; | |
10396 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
10397 | sec = sec->next) | |
10398 | { | |
3a800eb9 | 10399 | bfd_size_type size = sec->size; |
c152c796 | 10400 | |
3a800eb9 AM |
10401 | if (size == 0 |
10402 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 10403 | { |
3a800eb9 AM |
10404 | struct bfd_link_order *o = sec->map_tail.link_order; |
10405 | if (o != NULL) | |
10406 | size = o->offset + o->size; | |
c152c796 AM |
10407 | } |
10408 | end = sec->vma + size; | |
10409 | } | |
10410 | base = elf_hash_table (info)->tls_sec->vma; | |
10411 | end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power); | |
10412 | elf_hash_table (info)->tls_size = end - base; | |
10413 | } | |
10414 | ||
0b52efa6 PB |
10415 | /* Reorder SHF_LINK_ORDER sections. */ |
10416 | for (o = abfd->sections; o != NULL; o = o->next) | |
10417 | { | |
10418 | if (!elf_fixup_link_order (abfd, o)) | |
10419 | return FALSE; | |
10420 | } | |
10421 | ||
c152c796 AM |
10422 | /* Since ELF permits relocations to be against local symbols, we |
10423 | must have the local symbols available when we do the relocations. | |
10424 | Since we would rather only read the local symbols once, and we | |
10425 | would rather not keep them in memory, we handle all the | |
10426 | relocations for a single input file at the same time. | |
10427 | ||
10428 | Unfortunately, there is no way to know the total number of local | |
10429 | symbols until we have seen all of them, and the local symbol | |
10430 | indices precede the global symbol indices. This means that when | |
10431 | we are generating relocatable output, and we see a reloc against | |
10432 | a global symbol, we can not know the symbol index until we have | |
10433 | finished examining all the local symbols to see which ones we are | |
10434 | going to output. To deal with this, we keep the relocations in | |
10435 | memory, and don't output them until the end of the link. This is | |
10436 | an unfortunate waste of memory, but I don't see a good way around | |
10437 | it. Fortunately, it only happens when performing a relocatable | |
10438 | link, which is not the common case. FIXME: If keep_memory is set | |
10439 | we could write the relocs out and then read them again; I don't | |
10440 | know how bad the memory loss will be. */ | |
10441 | ||
10442 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
10443 | sub->output_has_begun = FALSE; | |
10444 | for (o = abfd->sections; o != NULL; o = o->next) | |
10445 | { | |
8423293d | 10446 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
10447 | { |
10448 | if (p->type == bfd_indirect_link_order | |
10449 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
10450 | == bfd_target_elf_flavour) | |
10451 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
10452 | { | |
10453 | if (! sub->output_has_begun) | |
10454 | { | |
10455 | if (! elf_link_input_bfd (&finfo, sub)) | |
10456 | goto error_return; | |
10457 | sub->output_has_begun = TRUE; | |
10458 | } | |
10459 | } | |
10460 | else if (p->type == bfd_section_reloc_link_order | |
10461 | || p->type == bfd_symbol_reloc_link_order) | |
10462 | { | |
10463 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
10464 | goto error_return; | |
10465 | } | |
10466 | else | |
10467 | { | |
10468 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
10469 | goto error_return; | |
10470 | } | |
10471 | } | |
10472 | } | |
10473 | ||
c0f00686 L |
10474 | /* Free symbol buffer if needed. */ |
10475 | if (!info->reduce_memory_overheads) | |
10476 | { | |
10477 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
3fcd97f1 JJ |
10478 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
10479 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
10480 | { |
10481 | free (elf_tdata (sub)->symbuf); | |
10482 | elf_tdata (sub)->symbuf = NULL; | |
10483 | } | |
10484 | } | |
10485 | ||
c152c796 AM |
10486 | /* Output any global symbols that got converted to local in a |
10487 | version script or due to symbol visibility. We do this in a | |
10488 | separate step since ELF requires all local symbols to appear | |
10489 | prior to any global symbols. FIXME: We should only do this if | |
10490 | some global symbols were, in fact, converted to become local. | |
10491 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
10492 | eoinfo.failed = FALSE; | |
10493 | eoinfo.finfo = &finfo; | |
10494 | eoinfo.localsyms = TRUE; | |
10495 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
10496 | &eoinfo); | |
10497 | if (eoinfo.failed) | |
10498 | return FALSE; | |
10499 | ||
4e617b1e PB |
10500 | /* If backend needs to output some local symbols not present in the hash |
10501 | table, do it now. */ | |
10502 | if (bed->elf_backend_output_arch_local_syms) | |
10503 | { | |
10504 | typedef bfd_boolean (*out_sym_func) | |
10505 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
10506 | struct elf_link_hash_entry *); | |
10507 | ||
10508 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
10509 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
10510 | return FALSE; | |
10511 | } | |
10512 | ||
c152c796 AM |
10513 | /* That wrote out all the local symbols. Finish up the symbol table |
10514 | with the global symbols. Even if we want to strip everything we | |
10515 | can, we still need to deal with those global symbols that got | |
10516 | converted to local in a version script. */ | |
10517 | ||
10518 | /* The sh_info field records the index of the first non local symbol. */ | |
10519 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
10520 | ||
10521 | if (dynamic | |
10522 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
10523 | { | |
10524 | Elf_Internal_Sym sym; | |
10525 | bfd_byte *dynsym = finfo.dynsym_sec->contents; | |
10526 | long last_local = 0; | |
10527 | ||
10528 | /* Write out the section symbols for the output sections. */ | |
67687978 | 10529 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
c152c796 AM |
10530 | { |
10531 | asection *s; | |
10532 | ||
10533 | sym.st_size = 0; | |
10534 | sym.st_name = 0; | |
10535 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
10536 | sym.st_other = 0; | |
10537 | ||
10538 | for (s = abfd->sections; s != NULL; s = s->next) | |
10539 | { | |
10540 | int indx; | |
10541 | bfd_byte *dest; | |
10542 | long dynindx; | |
10543 | ||
c152c796 | 10544 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
10545 | if (dynindx <= 0) |
10546 | continue; | |
10547 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
10548 | BFD_ASSERT (indx > 0); |
10549 | sym.st_shndx = indx; | |
c0d5a53d L |
10550 | if (! check_dynsym (abfd, &sym)) |
10551 | return FALSE; | |
c152c796 AM |
10552 | sym.st_value = s->vma; |
10553 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
8c37241b JJ |
10554 | if (last_local < dynindx) |
10555 | last_local = dynindx; | |
c152c796 AM |
10556 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
10557 | } | |
c152c796 AM |
10558 | } |
10559 | ||
10560 | /* Write out the local dynsyms. */ | |
10561 | if (elf_hash_table (info)->dynlocal) | |
10562 | { | |
10563 | struct elf_link_local_dynamic_entry *e; | |
10564 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
10565 | { | |
10566 | asection *s; | |
10567 | bfd_byte *dest; | |
10568 | ||
10569 | sym.st_size = e->isym.st_size; | |
10570 | sym.st_other = e->isym.st_other; | |
10571 | ||
10572 | /* Copy the internal symbol as is. | |
10573 | Note that we saved a word of storage and overwrote | |
10574 | the original st_name with the dynstr_index. */ | |
10575 | sym = e->isym; | |
10576 | ||
cb33740c AM |
10577 | s = bfd_section_from_elf_index (e->input_bfd, |
10578 | e->isym.st_shndx); | |
10579 | if (s != NULL) | |
c152c796 | 10580 | { |
c152c796 AM |
10581 | sym.st_shndx = |
10582 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
10583 | if (! check_dynsym (abfd, &sym)) |
10584 | return FALSE; | |
c152c796 AM |
10585 | sym.st_value = (s->output_section->vma |
10586 | + s->output_offset | |
10587 | + e->isym.st_value); | |
10588 | } | |
10589 | ||
10590 | if (last_local < e->dynindx) | |
10591 | last_local = e->dynindx; | |
10592 | ||
10593 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
10594 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
10595 | } | |
10596 | } | |
10597 | ||
10598 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = | |
10599 | last_local + 1; | |
10600 | } | |
10601 | ||
10602 | /* We get the global symbols from the hash table. */ | |
10603 | eoinfo.failed = FALSE; | |
10604 | eoinfo.localsyms = FALSE; | |
10605 | eoinfo.finfo = &finfo; | |
10606 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
10607 | &eoinfo); | |
10608 | if (eoinfo.failed) | |
10609 | return FALSE; | |
10610 | ||
10611 | /* If backend needs to output some symbols not present in the hash | |
10612 | table, do it now. */ | |
10613 | if (bed->elf_backend_output_arch_syms) | |
10614 | { | |
10615 | typedef bfd_boolean (*out_sym_func) | |
10616 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
10617 | struct elf_link_hash_entry *); | |
10618 | ||
10619 | if (! ((*bed->elf_backend_output_arch_syms) | |
10620 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
10621 | return FALSE; | |
10622 | } | |
10623 | ||
10624 | /* Flush all symbols to the file. */ | |
10625 | if (! elf_link_flush_output_syms (&finfo, bed)) | |
10626 | return FALSE; | |
10627 | ||
10628 | /* Now we know the size of the symtab section. */ | |
10629 | off += symtab_hdr->sh_size; | |
10630 | ||
10631 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
10632 | if (symtab_shndx_hdr->sh_name != 0) | |
10633 | { | |
10634 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
10635 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
10636 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
10637 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
10638 | symtab_shndx_hdr->sh_size = amt; | |
10639 | ||
10640 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
10641 | off, TRUE); | |
10642 | ||
10643 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
10644 | || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) | |
10645 | return FALSE; | |
10646 | } | |
10647 | ||
10648 | ||
10649 | /* Finish up and write out the symbol string table (.strtab) | |
10650 | section. */ | |
10651 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
10652 | /* sh_name was set in prep_headers. */ | |
10653 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
10654 | symstrtab_hdr->sh_flags = 0; | |
10655 | symstrtab_hdr->sh_addr = 0; | |
10656 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
10657 | symstrtab_hdr->sh_entsize = 0; | |
10658 | symstrtab_hdr->sh_link = 0; | |
10659 | symstrtab_hdr->sh_info = 0; | |
10660 | /* sh_offset is set just below. */ | |
10661 | symstrtab_hdr->sh_addralign = 1; | |
10662 | ||
10663 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); | |
10664 | elf_tdata (abfd)->next_file_pos = off; | |
10665 | ||
10666 | if (bfd_get_symcount (abfd) > 0) | |
10667 | { | |
10668 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
10669 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
10670 | return FALSE; | |
10671 | } | |
10672 | ||
10673 | /* Adjust the relocs to have the correct symbol indices. */ | |
10674 | for (o = abfd->sections; o != NULL; o = o->next) | |
10675 | { | |
10676 | if ((o->flags & SEC_RELOC) == 0) | |
10677 | continue; | |
10678 | ||
10679 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, | |
10680 | elf_section_data (o)->rel_count, | |
10681 | elf_section_data (o)->rel_hashes); | |
10682 | if (elf_section_data (o)->rel_hdr2 != NULL) | |
10683 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, | |
10684 | elf_section_data (o)->rel_count2, | |
10685 | (elf_section_data (o)->rel_hashes | |
10686 | + elf_section_data (o)->rel_count)); | |
10687 | ||
10688 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
10689 | trying to swap the relocs out itself. */ | |
10690 | o->reloc_count = 0; | |
10691 | } | |
10692 | ||
10693 | if (dynamic && info->combreloc && dynobj != NULL) | |
10694 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
10695 | ||
10696 | /* If we are linking against a dynamic object, or generating a | |
10697 | shared library, finish up the dynamic linking information. */ | |
10698 | if (dynamic) | |
10699 | { | |
10700 | bfd_byte *dyncon, *dynconend; | |
10701 | ||
10702 | /* Fix up .dynamic entries. */ | |
10703 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
10704 | BFD_ASSERT (o != NULL); | |
10705 | ||
10706 | dyncon = o->contents; | |
eea6121a | 10707 | dynconend = o->contents + o->size; |
c152c796 AM |
10708 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
10709 | { | |
10710 | Elf_Internal_Dyn dyn; | |
10711 | const char *name; | |
10712 | unsigned int type; | |
10713 | ||
10714 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
10715 | ||
10716 | switch (dyn.d_tag) | |
10717 | { | |
10718 | default: | |
10719 | continue; | |
10720 | case DT_NULL: | |
10721 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
10722 | { | |
10723 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
10724 | { | |
10725 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
10726 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
10727 | default: continue; | |
10728 | } | |
10729 | dyn.d_un.d_val = relativecount; | |
10730 | relativecount = 0; | |
10731 | break; | |
10732 | } | |
10733 | continue; | |
10734 | ||
10735 | case DT_INIT: | |
10736 | name = info->init_function; | |
10737 | goto get_sym; | |
10738 | case DT_FINI: | |
10739 | name = info->fini_function; | |
10740 | get_sym: | |
10741 | { | |
10742 | struct elf_link_hash_entry *h; | |
10743 | ||
10744 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
10745 | FALSE, FALSE, TRUE); | |
10746 | if (h != NULL | |
10747 | && (h->root.type == bfd_link_hash_defined | |
10748 | || h->root.type == bfd_link_hash_defweak)) | |
10749 | { | |
bef26483 | 10750 | dyn.d_un.d_ptr = h->root.u.def.value; |
c152c796 AM |
10751 | o = h->root.u.def.section; |
10752 | if (o->output_section != NULL) | |
bef26483 | 10753 | dyn.d_un.d_ptr += (o->output_section->vma |
c152c796 AM |
10754 | + o->output_offset); |
10755 | else | |
10756 | { | |
10757 | /* The symbol is imported from another shared | |
10758 | library and does not apply to this one. */ | |
bef26483 | 10759 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
10760 | } |
10761 | break; | |
10762 | } | |
10763 | } | |
10764 | continue; | |
10765 | ||
10766 | case DT_PREINIT_ARRAYSZ: | |
10767 | name = ".preinit_array"; | |
10768 | goto get_size; | |
10769 | case DT_INIT_ARRAYSZ: | |
10770 | name = ".init_array"; | |
10771 | goto get_size; | |
10772 | case DT_FINI_ARRAYSZ: | |
10773 | name = ".fini_array"; | |
10774 | get_size: | |
10775 | o = bfd_get_section_by_name (abfd, name); | |
10776 | if (o == NULL) | |
10777 | { | |
10778 | (*_bfd_error_handler) | |
d003868e | 10779 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
10780 | goto error_return; |
10781 | } | |
eea6121a | 10782 | if (o->size == 0) |
c152c796 AM |
10783 | (*_bfd_error_handler) |
10784 | (_("warning: %s section has zero size"), name); | |
eea6121a | 10785 | dyn.d_un.d_val = o->size; |
c152c796 AM |
10786 | break; |
10787 | ||
10788 | case DT_PREINIT_ARRAY: | |
10789 | name = ".preinit_array"; | |
10790 | goto get_vma; | |
10791 | case DT_INIT_ARRAY: | |
10792 | name = ".init_array"; | |
10793 | goto get_vma; | |
10794 | case DT_FINI_ARRAY: | |
10795 | name = ".fini_array"; | |
10796 | goto get_vma; | |
10797 | ||
10798 | case DT_HASH: | |
10799 | name = ".hash"; | |
10800 | goto get_vma; | |
fdc90cb4 JJ |
10801 | case DT_GNU_HASH: |
10802 | name = ".gnu.hash"; | |
10803 | goto get_vma; | |
c152c796 AM |
10804 | case DT_STRTAB: |
10805 | name = ".dynstr"; | |
10806 | goto get_vma; | |
10807 | case DT_SYMTAB: | |
10808 | name = ".dynsym"; | |
10809 | goto get_vma; | |
10810 | case DT_VERDEF: | |
10811 | name = ".gnu.version_d"; | |
10812 | goto get_vma; | |
10813 | case DT_VERNEED: | |
10814 | name = ".gnu.version_r"; | |
10815 | goto get_vma; | |
10816 | case DT_VERSYM: | |
10817 | name = ".gnu.version"; | |
10818 | get_vma: | |
10819 | o = bfd_get_section_by_name (abfd, name); | |
10820 | if (o == NULL) | |
10821 | { | |
10822 | (*_bfd_error_handler) | |
d003868e | 10823 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
10824 | goto error_return; |
10825 | } | |
10826 | dyn.d_un.d_ptr = o->vma; | |
10827 | break; | |
10828 | ||
10829 | case DT_REL: | |
10830 | case DT_RELA: | |
10831 | case DT_RELSZ: | |
10832 | case DT_RELASZ: | |
10833 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
10834 | type = SHT_REL; | |
10835 | else | |
10836 | type = SHT_RELA; | |
10837 | dyn.d_un.d_val = 0; | |
bef26483 | 10838 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
10839 | for (i = 1; i < elf_numsections (abfd); i++) |
10840 | { | |
10841 | Elf_Internal_Shdr *hdr; | |
10842 | ||
10843 | hdr = elf_elfsections (abfd)[i]; | |
10844 | if (hdr->sh_type == type | |
10845 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
10846 | { | |
10847 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
10848 | dyn.d_un.d_val += hdr->sh_size; | |
10849 | else | |
10850 | { | |
bef26483 AM |
10851 | if (dyn.d_un.d_ptr == 0 |
10852 | || hdr->sh_addr < dyn.d_un.d_ptr) | |
10853 | dyn.d_un.d_ptr = hdr->sh_addr; | |
c152c796 AM |
10854 | } |
10855 | } | |
10856 | } | |
10857 | break; | |
10858 | } | |
10859 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
10860 | } | |
10861 | } | |
10862 | ||
10863 | /* If we have created any dynamic sections, then output them. */ | |
10864 | if (dynobj != NULL) | |
10865 | { | |
10866 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
10867 | goto error_return; | |
10868 | ||
943284cc DJ |
10869 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
10870 | if (info->warn_shared_textrel && info->shared) | |
10871 | { | |
10872 | bfd_byte *dyncon, *dynconend; | |
10873 | ||
10874 | /* Fix up .dynamic entries. */ | |
10875 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
10876 | BFD_ASSERT (o != NULL); | |
10877 | ||
10878 | dyncon = o->contents; | |
10879 | dynconend = o->contents + o->size; | |
10880 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
10881 | { | |
10882 | Elf_Internal_Dyn dyn; | |
10883 | ||
10884 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
10885 | ||
10886 | if (dyn.d_tag == DT_TEXTREL) | |
10887 | { | |
a0c8462f | 10888 | info->callbacks->einfo |
9267588c | 10889 | (_("%P: warning: creating a DT_TEXTREL in a shared object.\n")); |
943284cc DJ |
10890 | break; |
10891 | } | |
10892 | } | |
10893 | } | |
10894 | ||
c152c796 AM |
10895 | for (o = dynobj->sections; o != NULL; o = o->next) |
10896 | { | |
10897 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 10898 | || o->size == 0 |
c152c796 AM |
10899 | || o->output_section == bfd_abs_section_ptr) |
10900 | continue; | |
10901 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
10902 | { | |
10903 | /* At this point, we are only interested in sections | |
10904 | created by _bfd_elf_link_create_dynamic_sections. */ | |
10905 | continue; | |
10906 | } | |
3722b82f AM |
10907 | if (elf_hash_table (info)->stab_info.stabstr == o) |
10908 | continue; | |
eea6121a AM |
10909 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
10910 | continue; | |
c152c796 AM |
10911 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
10912 | != SHT_STRTAB) | |
10913 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
10914 | { | |
10915 | if (! bfd_set_section_contents (abfd, o->output_section, | |
10916 | o->contents, | |
10917 | (file_ptr) o->output_offset, | |
eea6121a | 10918 | o->size)) |
c152c796 AM |
10919 | goto error_return; |
10920 | } | |
10921 | else | |
10922 | { | |
10923 | /* The contents of the .dynstr section are actually in a | |
10924 | stringtab. */ | |
10925 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
10926 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
10927 | || ! _bfd_elf_strtab_emit (abfd, | |
10928 | elf_hash_table (info)->dynstr)) | |
10929 | goto error_return; | |
10930 | } | |
10931 | } | |
10932 | } | |
10933 | ||
10934 | if (info->relocatable) | |
10935 | { | |
10936 | bfd_boolean failed = FALSE; | |
10937 | ||
10938 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
10939 | if (failed) | |
10940 | goto error_return; | |
10941 | } | |
10942 | ||
10943 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 10944 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
10945 | { |
10946 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
10947 | goto error_return; | |
10948 | } | |
10949 | ||
10950 | if (info->eh_frame_hdr) | |
10951 | { | |
10952 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) | |
10953 | goto error_return; | |
10954 | } | |
10955 | ||
10956 | if (finfo.symstrtab != NULL) | |
10957 | _bfd_stringtab_free (finfo.symstrtab); | |
10958 | if (finfo.contents != NULL) | |
10959 | free (finfo.contents); | |
10960 | if (finfo.external_relocs != NULL) | |
10961 | free (finfo.external_relocs); | |
10962 | if (finfo.internal_relocs != NULL) | |
10963 | free (finfo.internal_relocs); | |
10964 | if (finfo.external_syms != NULL) | |
10965 | free (finfo.external_syms); | |
10966 | if (finfo.locsym_shndx != NULL) | |
10967 | free (finfo.locsym_shndx); | |
10968 | if (finfo.internal_syms != NULL) | |
10969 | free (finfo.internal_syms); | |
10970 | if (finfo.indices != NULL) | |
10971 | free (finfo.indices); | |
10972 | if (finfo.sections != NULL) | |
10973 | free (finfo.sections); | |
10974 | if (finfo.symbuf != NULL) | |
10975 | free (finfo.symbuf); | |
10976 | if (finfo.symshndxbuf != NULL) | |
10977 | free (finfo.symshndxbuf); | |
10978 | for (o = abfd->sections; o != NULL; o = o->next) | |
10979 | { | |
10980 | if ((o->flags & SEC_RELOC) != 0 | |
10981 | && elf_section_data (o)->rel_hashes != NULL) | |
10982 | free (elf_section_data (o)->rel_hashes); | |
10983 | } | |
10984 | ||
10985 | elf_tdata (abfd)->linker = TRUE; | |
10986 | ||
104d59d1 JM |
10987 | if (attr_section) |
10988 | { | |
10989 | bfd_byte *contents = bfd_malloc (attr_size); | |
10990 | if (contents == NULL) | |
d0f16d5e | 10991 | return FALSE; /* Bail out and fail. */ |
104d59d1 JM |
10992 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
10993 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
10994 | free (contents); | |
10995 | } | |
10996 | ||
c152c796 AM |
10997 | return TRUE; |
10998 | ||
10999 | error_return: | |
11000 | if (finfo.symstrtab != NULL) | |
11001 | _bfd_stringtab_free (finfo.symstrtab); | |
11002 | if (finfo.contents != NULL) | |
11003 | free (finfo.contents); | |
11004 | if (finfo.external_relocs != NULL) | |
11005 | free (finfo.external_relocs); | |
11006 | if (finfo.internal_relocs != NULL) | |
11007 | free (finfo.internal_relocs); | |
11008 | if (finfo.external_syms != NULL) | |
11009 | free (finfo.external_syms); | |
11010 | if (finfo.locsym_shndx != NULL) | |
11011 | free (finfo.locsym_shndx); | |
11012 | if (finfo.internal_syms != NULL) | |
11013 | free (finfo.internal_syms); | |
11014 | if (finfo.indices != NULL) | |
11015 | free (finfo.indices); | |
11016 | if (finfo.sections != NULL) | |
11017 | free (finfo.sections); | |
11018 | if (finfo.symbuf != NULL) | |
11019 | free (finfo.symbuf); | |
11020 | if (finfo.symshndxbuf != NULL) | |
11021 | free (finfo.symshndxbuf); | |
11022 | for (o = abfd->sections; o != NULL; o = o->next) | |
11023 | { | |
11024 | if ((o->flags & SEC_RELOC) != 0 | |
11025 | && elf_section_data (o)->rel_hashes != NULL) | |
11026 | free (elf_section_data (o)->rel_hashes); | |
11027 | } | |
11028 | ||
11029 | return FALSE; | |
11030 | } | |
11031 | \f | |
5241d853 RS |
11032 | /* Initialize COOKIE for input bfd ABFD. */ |
11033 | ||
11034 | static bfd_boolean | |
11035 | init_reloc_cookie (struct elf_reloc_cookie *cookie, | |
11036 | struct bfd_link_info *info, bfd *abfd) | |
11037 | { | |
11038 | Elf_Internal_Shdr *symtab_hdr; | |
11039 | const struct elf_backend_data *bed; | |
11040 | ||
11041 | bed = get_elf_backend_data (abfd); | |
11042 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11043 | ||
11044 | cookie->abfd = abfd; | |
11045 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
11046 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
11047 | if (cookie->bad_symtab) | |
11048 | { | |
11049 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
11050 | cookie->extsymoff = 0; | |
11051 | } | |
11052 | else | |
11053 | { | |
11054 | cookie->locsymcount = symtab_hdr->sh_info; | |
11055 | cookie->extsymoff = symtab_hdr->sh_info; | |
11056 | } | |
11057 | ||
11058 | if (bed->s->arch_size == 32) | |
11059 | cookie->r_sym_shift = 8; | |
11060 | else | |
11061 | cookie->r_sym_shift = 32; | |
11062 | ||
11063 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
11064 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
11065 | { | |
11066 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
11067 | cookie->locsymcount, 0, | |
11068 | NULL, NULL, NULL); | |
11069 | if (cookie->locsyms == NULL) | |
11070 | { | |
11071 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
11072 | return FALSE; | |
11073 | } | |
11074 | if (info->keep_memory) | |
11075 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
11076 | } | |
11077 | return TRUE; | |
11078 | } | |
11079 | ||
11080 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
11081 | ||
11082 | static void | |
11083 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
11084 | { | |
11085 | Elf_Internal_Shdr *symtab_hdr; | |
11086 | ||
11087 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11088 | if (cookie->locsyms != NULL | |
11089 | && symtab_hdr->contents != (unsigned char *) cookie->locsyms) | |
11090 | free (cookie->locsyms); | |
11091 | } | |
11092 | ||
11093 | /* Initialize the relocation information in COOKIE for input section SEC | |
11094 | of input bfd ABFD. */ | |
11095 | ||
11096 | static bfd_boolean | |
11097 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
11098 | struct bfd_link_info *info, bfd *abfd, | |
11099 | asection *sec) | |
11100 | { | |
11101 | const struct elf_backend_data *bed; | |
11102 | ||
11103 | if (sec->reloc_count == 0) | |
11104 | { | |
11105 | cookie->rels = NULL; | |
11106 | cookie->relend = NULL; | |
11107 | } | |
11108 | else | |
11109 | { | |
11110 | bed = get_elf_backend_data (abfd); | |
11111 | ||
11112 | cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
11113 | info->keep_memory); | |
11114 | if (cookie->rels == NULL) | |
11115 | return FALSE; | |
11116 | cookie->rel = cookie->rels; | |
11117 | cookie->relend = (cookie->rels | |
11118 | + sec->reloc_count * bed->s->int_rels_per_ext_rel); | |
11119 | } | |
11120 | cookie->rel = cookie->rels; | |
11121 | return TRUE; | |
11122 | } | |
11123 | ||
11124 | /* Free the memory allocated by init_reloc_cookie_rels, | |
11125 | if appropriate. */ | |
11126 | ||
11127 | static void | |
11128 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
11129 | asection *sec) | |
11130 | { | |
11131 | if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) | |
11132 | free (cookie->rels); | |
11133 | } | |
11134 | ||
11135 | /* Initialize the whole of COOKIE for input section SEC. */ | |
11136 | ||
11137 | static bfd_boolean | |
11138 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
11139 | struct bfd_link_info *info, | |
11140 | asection *sec) | |
11141 | { | |
11142 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
11143 | goto error1; | |
11144 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
11145 | goto error2; | |
11146 | return TRUE; | |
11147 | ||
11148 | error2: | |
11149 | fini_reloc_cookie (cookie, sec->owner); | |
11150 | error1: | |
11151 | return FALSE; | |
11152 | } | |
11153 | ||
11154 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
11155 | if appropriate. */ | |
11156 | ||
11157 | static void | |
11158 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
11159 | asection *sec) | |
11160 | { | |
11161 | fini_reloc_cookie_rels (cookie, sec); | |
11162 | fini_reloc_cookie (cookie, sec->owner); | |
11163 | } | |
11164 | \f | |
c152c796 AM |
11165 | /* Garbage collect unused sections. */ |
11166 | ||
07adf181 AM |
11167 | /* Default gc_mark_hook. */ |
11168 | ||
11169 | asection * | |
11170 | _bfd_elf_gc_mark_hook (asection *sec, | |
11171 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
11172 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
11173 | struct elf_link_hash_entry *h, | |
11174 | Elf_Internal_Sym *sym) | |
11175 | { | |
11176 | if (h != NULL) | |
11177 | { | |
11178 | switch (h->root.type) | |
11179 | { | |
11180 | case bfd_link_hash_defined: | |
11181 | case bfd_link_hash_defweak: | |
11182 | return h->root.u.def.section; | |
11183 | ||
11184 | case bfd_link_hash_common: | |
11185 | return h->root.u.c.p->section; | |
11186 | ||
11187 | default: | |
11188 | break; | |
11189 | } | |
11190 | } | |
11191 | else | |
11192 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
11193 | ||
11194 | return NULL; | |
11195 | } | |
11196 | ||
5241d853 RS |
11197 | /* COOKIE->rel describes a relocation against section SEC, which is |
11198 | a section we've decided to keep. Return the section that contains | |
11199 | the relocation symbol, or NULL if no section contains it. */ | |
11200 | ||
11201 | asection * | |
11202 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
11203 | elf_gc_mark_hook_fn gc_mark_hook, | |
11204 | struct elf_reloc_cookie *cookie) | |
11205 | { | |
11206 | unsigned long r_symndx; | |
11207 | struct elf_link_hash_entry *h; | |
11208 | ||
11209 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
11210 | if (r_symndx == 0) | |
11211 | return NULL; | |
11212 | ||
11213 | if (r_symndx >= cookie->locsymcount | |
11214 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
11215 | { | |
11216 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
11217 | while (h->root.type == bfd_link_hash_indirect | |
11218 | || h->root.type == bfd_link_hash_warning) | |
11219 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11220 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); | |
11221 | } | |
11222 | ||
11223 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
11224 | &cookie->locsyms[r_symndx]); | |
11225 | } | |
11226 | ||
11227 | /* COOKIE->rel describes a relocation against section SEC, which is | |
11228 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 11229 | the relocation symbol. */ |
5241d853 RS |
11230 | |
11231 | bfd_boolean | |
11232 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, | |
11233 | asection *sec, | |
11234 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 11235 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
11236 | { |
11237 | asection *rsec; | |
11238 | ||
11239 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie); | |
11240 | if (rsec && !rsec->gc_mark) | |
11241 | { | |
11242 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
11243 | rsec->gc_mark = 1; | |
5241d853 RS |
11244 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) |
11245 | return FALSE; | |
11246 | } | |
11247 | return TRUE; | |
11248 | } | |
11249 | ||
07adf181 AM |
11250 | /* The mark phase of garbage collection. For a given section, mark |
11251 | it and any sections in this section's group, and all the sections | |
11252 | which define symbols to which it refers. */ | |
11253 | ||
ccfa59ea AM |
11254 | bfd_boolean |
11255 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
11256 | asection *sec, | |
6a5bb875 | 11257 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
11258 | { |
11259 | bfd_boolean ret; | |
9d0a14d3 | 11260 | asection *group_sec, *eh_frame; |
c152c796 AM |
11261 | |
11262 | sec->gc_mark = 1; | |
11263 | ||
11264 | /* Mark all the sections in the group. */ | |
11265 | group_sec = elf_section_data (sec)->next_in_group; | |
11266 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 11267 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
11268 | return FALSE; |
11269 | ||
11270 | /* Look through the section relocs. */ | |
11271 | ret = TRUE; | |
9d0a14d3 RS |
11272 | eh_frame = elf_eh_frame_section (sec->owner); |
11273 | if ((sec->flags & SEC_RELOC) != 0 | |
11274 | && sec->reloc_count > 0 | |
11275 | && sec != eh_frame) | |
c152c796 | 11276 | { |
5241d853 | 11277 | struct elf_reloc_cookie cookie; |
c152c796 | 11278 | |
5241d853 RS |
11279 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
11280 | ret = FALSE; | |
c152c796 | 11281 | else |
c152c796 | 11282 | { |
5241d853 | 11283 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 11284 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 RS |
11285 | { |
11286 | ret = FALSE; | |
11287 | break; | |
11288 | } | |
11289 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
11290 | } |
11291 | } | |
9d0a14d3 RS |
11292 | |
11293 | if (ret && eh_frame && elf_fde_list (sec)) | |
11294 | { | |
11295 | struct elf_reloc_cookie cookie; | |
11296 | ||
11297 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
11298 | ret = FALSE; | |
11299 | else | |
11300 | { | |
11301 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
11302 | gc_mark_hook, &cookie)) | |
11303 | ret = FALSE; | |
11304 | fini_reloc_cookie_for_section (&cookie, eh_frame); | |
11305 | } | |
11306 | } | |
11307 | ||
c152c796 AM |
11308 | return ret; |
11309 | } | |
11310 | ||
11311 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ | |
11312 | ||
c17d87de NC |
11313 | struct elf_gc_sweep_symbol_info |
11314 | { | |
ccabcbe5 AM |
11315 | struct bfd_link_info *info; |
11316 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
11317 | bfd_boolean); | |
11318 | }; | |
11319 | ||
c152c796 | 11320 | static bfd_boolean |
ccabcbe5 | 11321 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) |
c152c796 | 11322 | { |
c152c796 AM |
11323 | if (h->root.type == bfd_link_hash_warning) |
11324 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11325 | ||
ccabcbe5 AM |
11326 | if ((h->root.type == bfd_link_hash_defined |
11327 | || h->root.type == bfd_link_hash_defweak) | |
11328 | && !h->root.u.def.section->gc_mark | |
11329 | && !(h->root.u.def.section->owner->flags & DYNAMIC)) | |
11330 | { | |
11331 | struct elf_gc_sweep_symbol_info *inf = data; | |
11332 | (*inf->hide_symbol) (inf->info, h, TRUE); | |
11333 | } | |
c152c796 AM |
11334 | |
11335 | return TRUE; | |
11336 | } | |
11337 | ||
11338 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
11339 | ||
11340 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
11341 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
11342 | ||
11343 | static bfd_boolean | |
ccabcbe5 | 11344 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
11345 | { |
11346 | bfd *sub; | |
ccabcbe5 AM |
11347 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11348 | gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; | |
11349 | unsigned long section_sym_count; | |
11350 | struct elf_gc_sweep_symbol_info sweep_info; | |
c152c796 AM |
11351 | |
11352 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
11353 | { | |
11354 | asection *o; | |
11355 | ||
11356 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
11357 | continue; | |
11358 | ||
11359 | for (o = sub->sections; o != NULL; o = o->next) | |
11360 | { | |
7c2c8505 AM |
11361 | /* Keep debug and special sections. */ |
11362 | if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 | |
dea5f36a | 11363 | || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) |
c152c796 AM |
11364 | o->gc_mark = 1; |
11365 | ||
11366 | if (o->gc_mark) | |
11367 | continue; | |
11368 | ||
11369 | /* Skip sweeping sections already excluded. */ | |
11370 | if (o->flags & SEC_EXCLUDE) | |
11371 | continue; | |
11372 | ||
11373 | /* Since this is early in the link process, it is simple | |
11374 | to remove a section from the output. */ | |
11375 | o->flags |= SEC_EXCLUDE; | |
11376 | ||
c55fe096 | 11377 | if (info->print_gc_sections && o->size != 0) |
c17d87de NC |
11378 | _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name); |
11379 | ||
c152c796 AM |
11380 | /* But we also have to update some of the relocation |
11381 | info we collected before. */ | |
11382 | if (gc_sweep_hook | |
e8aaee2a AM |
11383 | && (o->flags & SEC_RELOC) != 0 |
11384 | && o->reloc_count > 0 | |
11385 | && !bfd_is_abs_section (o->output_section)) | |
c152c796 AM |
11386 | { |
11387 | Elf_Internal_Rela *internal_relocs; | |
11388 | bfd_boolean r; | |
11389 | ||
11390 | internal_relocs | |
11391 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
11392 | info->keep_memory); | |
11393 | if (internal_relocs == NULL) | |
11394 | return FALSE; | |
11395 | ||
11396 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
11397 | ||
11398 | if (elf_section_data (o)->relocs != internal_relocs) | |
11399 | free (internal_relocs); | |
11400 | ||
11401 | if (!r) | |
11402 | return FALSE; | |
11403 | } | |
11404 | } | |
11405 | } | |
11406 | ||
11407 | /* Remove the symbols that were in the swept sections from the dynamic | |
11408 | symbol table. GCFIXME: Anyone know how to get them out of the | |
11409 | static symbol table as well? */ | |
ccabcbe5 AM |
11410 | sweep_info.info = info; |
11411 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
11412 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
11413 | &sweep_info); | |
c152c796 | 11414 | |
ccabcbe5 | 11415 | _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); |
c152c796 AM |
11416 | return TRUE; |
11417 | } | |
11418 | ||
11419 | /* Propagate collected vtable information. This is called through | |
11420 | elf_link_hash_traverse. */ | |
11421 | ||
11422 | static bfd_boolean | |
11423 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
11424 | { | |
11425 | if (h->root.type == bfd_link_hash_warning) | |
11426 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11427 | ||
11428 | /* Those that are not vtables. */ | |
f6e332e6 | 11429 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
11430 | return TRUE; |
11431 | ||
11432 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 11433 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
11434 | return TRUE; |
11435 | ||
11436 | /* If we've already been done, exit. */ | |
f6e332e6 | 11437 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
11438 | return TRUE; |
11439 | ||
11440 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 11441 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 11442 | |
f6e332e6 | 11443 | if (h->vtable->used == NULL) |
c152c796 AM |
11444 | { |
11445 | /* None of this table's entries were referenced. Re-use the | |
11446 | parent's table. */ | |
f6e332e6 AM |
11447 | h->vtable->used = h->vtable->parent->vtable->used; |
11448 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
11449 | } |
11450 | else | |
11451 | { | |
11452 | size_t n; | |
11453 | bfd_boolean *cu, *pu; | |
11454 | ||
11455 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 11456 | cu = h->vtable->used; |
c152c796 | 11457 | cu[-1] = TRUE; |
f6e332e6 | 11458 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
11459 | if (pu != NULL) |
11460 | { | |
11461 | const struct elf_backend_data *bed; | |
11462 | unsigned int log_file_align; | |
11463 | ||
11464 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
11465 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 11466 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
11467 | while (n--) |
11468 | { | |
11469 | if (*pu) | |
11470 | *cu = TRUE; | |
11471 | pu++; | |
11472 | cu++; | |
11473 | } | |
11474 | } | |
11475 | } | |
11476 | ||
11477 | return TRUE; | |
11478 | } | |
11479 | ||
11480 | static bfd_boolean | |
11481 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
11482 | { | |
11483 | asection *sec; | |
11484 | bfd_vma hstart, hend; | |
11485 | Elf_Internal_Rela *relstart, *relend, *rel; | |
11486 | const struct elf_backend_data *bed; | |
11487 | unsigned int log_file_align; | |
11488 | ||
11489 | if (h->root.type == bfd_link_hash_warning) | |
11490 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11491 | ||
11492 | /* Take care of both those symbols that do not describe vtables as | |
11493 | well as those that are not loaded. */ | |
f6e332e6 | 11494 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
11495 | return TRUE; |
11496 | ||
11497 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
11498 | || h->root.type == bfd_link_hash_defweak); | |
11499 | ||
11500 | sec = h->root.u.def.section; | |
11501 | hstart = h->root.u.def.value; | |
11502 | hend = hstart + h->size; | |
11503 | ||
11504 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
11505 | if (!relstart) | |
11506 | return *(bfd_boolean *) okp = FALSE; | |
11507 | bed = get_elf_backend_data (sec->owner); | |
11508 | log_file_align = bed->s->log_file_align; | |
11509 | ||
11510 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
11511 | ||
11512 | for (rel = relstart; rel < relend; ++rel) | |
11513 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
11514 | { | |
11515 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
11516 | if (h->vtable->used |
11517 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
11518 | { |
11519 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 11520 | if (h->vtable->used[entry]) |
c152c796 AM |
11521 | continue; |
11522 | } | |
11523 | /* Otherwise, kill it. */ | |
11524 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
11525 | } | |
11526 | ||
11527 | return TRUE; | |
11528 | } | |
11529 | ||
87538722 AM |
11530 | /* Mark sections containing dynamically referenced symbols. When |
11531 | building shared libraries, we must assume that any visible symbol is | |
11532 | referenced. */ | |
715df9b8 | 11533 | |
64d03ab5 AM |
11534 | bfd_boolean |
11535 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 11536 | { |
87538722 AM |
11537 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
11538 | ||
715df9b8 EB |
11539 | if (h->root.type == bfd_link_hash_warning) |
11540 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11541 | ||
11542 | if ((h->root.type == bfd_link_hash_defined | |
11543 | || h->root.type == bfd_link_hash_defweak) | |
87538722 | 11544 | && (h->ref_dynamic |
5adcfd8b | 11545 | || (!info->executable |
87538722 AM |
11546 | && h->def_regular |
11547 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL | |
11548 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN))) | |
715df9b8 EB |
11549 | h->root.u.def.section->flags |= SEC_KEEP; |
11550 | ||
11551 | return TRUE; | |
11552 | } | |
3b36f7e6 | 11553 | |
74f0fb50 AM |
11554 | /* Keep all sections containing symbols undefined on the command-line, |
11555 | and the section containing the entry symbol. */ | |
11556 | ||
11557 | void | |
11558 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
11559 | { | |
11560 | struct bfd_sym_chain *sym; | |
11561 | ||
11562 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
11563 | { | |
11564 | struct elf_link_hash_entry *h; | |
11565 | ||
11566 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
11567 | FALSE, FALSE, FALSE); | |
11568 | ||
11569 | if (h != NULL | |
11570 | && (h->root.type == bfd_link_hash_defined | |
11571 | || h->root.type == bfd_link_hash_defweak) | |
11572 | && !bfd_is_abs_section (h->root.u.def.section)) | |
11573 | h->root.u.def.section->flags |= SEC_KEEP; | |
11574 | } | |
11575 | } | |
11576 | ||
c152c796 AM |
11577 | /* Do mark and sweep of unused sections. */ |
11578 | ||
11579 | bfd_boolean | |
11580 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
11581 | { | |
11582 | bfd_boolean ok = TRUE; | |
11583 | bfd *sub; | |
6a5bb875 | 11584 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 11585 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 11586 | |
64d03ab5 | 11587 | if (!bed->can_gc_sections |
715df9b8 | 11588 | || !is_elf_hash_table (info->hash)) |
c152c796 AM |
11589 | { |
11590 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
11591 | return TRUE; | |
11592 | } | |
11593 | ||
74f0fb50 AM |
11594 | bed->gc_keep (info); |
11595 | ||
9d0a14d3 RS |
11596 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
11597 | at the .eh_frame section if we can mark the FDEs individually. */ | |
11598 | _bfd_elf_begin_eh_frame_parsing (info); | |
11599 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
11600 | { | |
11601 | asection *sec; | |
11602 | struct elf_reloc_cookie cookie; | |
11603 | ||
11604 | sec = bfd_get_section_by_name (sub, ".eh_frame"); | |
11605 | if (sec && init_reloc_cookie_for_section (&cookie, info, sec)) | |
11606 | { | |
11607 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
11608 | if (elf_section_data (sec)->sec_info) | |
11609 | elf_eh_frame_section (sub) = sec; | |
11610 | fini_reloc_cookie_for_section (&cookie, sec); | |
11611 | } | |
11612 | } | |
11613 | _bfd_elf_end_eh_frame_parsing (info); | |
11614 | ||
c152c796 AM |
11615 | /* Apply transitive closure to the vtable entry usage info. */ |
11616 | elf_link_hash_traverse (elf_hash_table (info), | |
11617 | elf_gc_propagate_vtable_entries_used, | |
11618 | &ok); | |
11619 | if (!ok) | |
11620 | return FALSE; | |
11621 | ||
11622 | /* Kill the vtable relocations that were not used. */ | |
11623 | elf_link_hash_traverse (elf_hash_table (info), | |
11624 | elf_gc_smash_unused_vtentry_relocs, | |
11625 | &ok); | |
11626 | if (!ok) | |
11627 | return FALSE; | |
11628 | ||
715df9b8 EB |
11629 | /* Mark dynamically referenced symbols. */ |
11630 | if (elf_hash_table (info)->dynamic_sections_created) | |
11631 | elf_link_hash_traverse (elf_hash_table (info), | |
64d03ab5 | 11632 | bed->gc_mark_dynamic_ref, |
87538722 | 11633 | info); |
c152c796 | 11634 | |
715df9b8 | 11635 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 11636 | gc_mark_hook = bed->gc_mark_hook; |
c152c796 AM |
11637 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) |
11638 | { | |
11639 | asection *o; | |
11640 | ||
11641 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
11642 | continue; | |
11643 | ||
11644 | for (o = sub->sections; o != NULL; o = o->next) | |
a14a5de3 | 11645 | if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark) |
39c2f51b AM |
11646 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) |
11647 | return FALSE; | |
c152c796 AM |
11648 | } |
11649 | ||
6a5bb875 PB |
11650 | /* Allow the backend to mark additional target specific sections. */ |
11651 | if (bed->gc_mark_extra_sections) | |
74f0fb50 | 11652 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 11653 | |
c152c796 | 11654 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 11655 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
11656 | } |
11657 | \f | |
11658 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
11659 | ||
11660 | bfd_boolean | |
11661 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
11662 | asection *sec, | |
11663 | struct elf_link_hash_entry *h, | |
11664 | bfd_vma offset) | |
11665 | { | |
11666 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
11667 | struct elf_link_hash_entry **search, *child; | |
11668 | bfd_size_type extsymcount; | |
11669 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11670 | ||
11671 | /* The sh_info field of the symtab header tells us where the | |
11672 | external symbols start. We don't care about the local symbols at | |
11673 | this point. */ | |
11674 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
11675 | if (!elf_bad_symtab (abfd)) | |
11676 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
11677 | ||
11678 | sym_hashes = elf_sym_hashes (abfd); | |
11679 | sym_hashes_end = sym_hashes + extsymcount; | |
11680 | ||
11681 | /* Hunt down the child symbol, which is in this section at the same | |
11682 | offset as the relocation. */ | |
11683 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
11684 | { | |
11685 | if ((child = *search) != NULL | |
11686 | && (child->root.type == bfd_link_hash_defined | |
11687 | || child->root.type == bfd_link_hash_defweak) | |
11688 | && child->root.u.def.section == sec | |
11689 | && child->root.u.def.value == offset) | |
11690 | goto win; | |
11691 | } | |
11692 | ||
d003868e AM |
11693 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
11694 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
11695 | bfd_set_error (bfd_error_invalid_operation); |
11696 | return FALSE; | |
11697 | ||
11698 | win: | |
f6e332e6 AM |
11699 | if (!child->vtable) |
11700 | { | |
11701 | child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable)); | |
11702 | if (!child->vtable) | |
11703 | return FALSE; | |
11704 | } | |
c152c796 AM |
11705 | if (!h) |
11706 | { | |
11707 | /* This *should* only be the absolute section. It could potentially | |
11708 | be that someone has defined a non-global vtable though, which | |
11709 | would be bad. It isn't worth paging in the local symbols to be | |
11710 | sure though; that case should simply be handled by the assembler. */ | |
11711 | ||
f6e332e6 | 11712 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
11713 | } |
11714 | else | |
f6e332e6 | 11715 | child->vtable->parent = h; |
c152c796 AM |
11716 | |
11717 | return TRUE; | |
11718 | } | |
11719 | ||
11720 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
11721 | ||
11722 | bfd_boolean | |
11723 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
11724 | asection *sec ATTRIBUTE_UNUSED, | |
11725 | struct elf_link_hash_entry *h, | |
11726 | bfd_vma addend) | |
11727 | { | |
11728 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11729 | unsigned int log_file_align = bed->s->log_file_align; | |
11730 | ||
f6e332e6 AM |
11731 | if (!h->vtable) |
11732 | { | |
11733 | h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable)); | |
11734 | if (!h->vtable) | |
11735 | return FALSE; | |
11736 | } | |
11737 | ||
11738 | if (addend >= h->vtable->size) | |
c152c796 AM |
11739 | { |
11740 | size_t size, bytes, file_align; | |
f6e332e6 | 11741 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
11742 | |
11743 | /* While the symbol is undefined, we have to be prepared to handle | |
11744 | a zero size. */ | |
11745 | file_align = 1 << log_file_align; | |
11746 | if (h->root.type == bfd_link_hash_undefined) | |
11747 | size = addend + file_align; | |
11748 | else | |
11749 | { | |
11750 | size = h->size; | |
11751 | if (addend >= size) | |
11752 | { | |
11753 | /* Oops! We've got a reference past the defined end of | |
11754 | the table. This is probably a bug -- shall we warn? */ | |
11755 | size = addend + file_align; | |
11756 | } | |
11757 | } | |
11758 | size = (size + file_align - 1) & -file_align; | |
11759 | ||
11760 | /* Allocate one extra entry for use as a "done" flag for the | |
11761 | consolidation pass. */ | |
11762 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
11763 | ||
11764 | if (ptr) | |
11765 | { | |
11766 | ptr = bfd_realloc (ptr - 1, bytes); | |
11767 | ||
11768 | if (ptr != NULL) | |
11769 | { | |
11770 | size_t oldbytes; | |
11771 | ||
f6e332e6 | 11772 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
11773 | * sizeof (bfd_boolean)); |
11774 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
11775 | } | |
11776 | } | |
11777 | else | |
11778 | ptr = bfd_zmalloc (bytes); | |
11779 | ||
11780 | if (ptr == NULL) | |
11781 | return FALSE; | |
11782 | ||
11783 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
11784 | h->vtable->used = ptr + 1; |
11785 | h->vtable->size = size; | |
c152c796 AM |
11786 | } |
11787 | ||
f6e332e6 | 11788 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
11789 | |
11790 | return TRUE; | |
11791 | } | |
11792 | ||
11793 | struct alloc_got_off_arg { | |
11794 | bfd_vma gotoff; | |
10455f89 | 11795 | struct bfd_link_info *info; |
c152c796 AM |
11796 | }; |
11797 | ||
11798 | /* We need a special top-level link routine to convert got reference counts | |
11799 | to real got offsets. */ | |
11800 | ||
11801 | static bfd_boolean | |
11802 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
11803 | { | |
11804 | struct alloc_got_off_arg *gofarg = arg; | |
10455f89 HPN |
11805 | bfd *obfd = gofarg->info->output_bfd; |
11806 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
c152c796 AM |
11807 | |
11808 | if (h->root.type == bfd_link_hash_warning) | |
11809 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11810 | ||
11811 | if (h->got.refcount > 0) | |
11812 | { | |
11813 | h->got.offset = gofarg->gotoff; | |
10455f89 | 11814 | gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); |
c152c796 AM |
11815 | } |
11816 | else | |
11817 | h->got.offset = (bfd_vma) -1; | |
11818 | ||
11819 | return TRUE; | |
11820 | } | |
11821 | ||
11822 | /* And an accompanying bit to work out final got entry offsets once | |
11823 | we're done. Should be called from final_link. */ | |
11824 | ||
11825 | bfd_boolean | |
11826 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
11827 | struct bfd_link_info *info) | |
11828 | { | |
11829 | bfd *i; | |
11830 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11831 | bfd_vma gotoff; | |
c152c796 AM |
11832 | struct alloc_got_off_arg gofarg; |
11833 | ||
10455f89 HPN |
11834 | BFD_ASSERT (abfd == info->output_bfd); |
11835 | ||
c152c796 AM |
11836 | if (! is_elf_hash_table (info->hash)) |
11837 | return FALSE; | |
11838 | ||
11839 | /* The GOT offset is relative to the .got section, but the GOT header is | |
11840 | put into the .got.plt section, if the backend uses it. */ | |
11841 | if (bed->want_got_plt) | |
11842 | gotoff = 0; | |
11843 | else | |
11844 | gotoff = bed->got_header_size; | |
11845 | ||
11846 | /* Do the local .got entries first. */ | |
11847 | for (i = info->input_bfds; i; i = i->link_next) | |
11848 | { | |
11849 | bfd_signed_vma *local_got; | |
11850 | bfd_size_type j, locsymcount; | |
11851 | Elf_Internal_Shdr *symtab_hdr; | |
11852 | ||
11853 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
11854 | continue; | |
11855 | ||
11856 | local_got = elf_local_got_refcounts (i); | |
11857 | if (!local_got) | |
11858 | continue; | |
11859 | ||
11860 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
11861 | if (elf_bad_symtab (i)) | |
11862 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
11863 | else | |
11864 | locsymcount = symtab_hdr->sh_info; | |
11865 | ||
11866 | for (j = 0; j < locsymcount; ++j) | |
11867 | { | |
11868 | if (local_got[j] > 0) | |
11869 | { | |
11870 | local_got[j] = gotoff; | |
10455f89 | 11871 | gotoff += bed->got_elt_size (abfd, info, NULL, i, j); |
c152c796 AM |
11872 | } |
11873 | else | |
11874 | local_got[j] = (bfd_vma) -1; | |
11875 | } | |
11876 | } | |
11877 | ||
11878 | /* Then the global .got entries. .plt refcounts are handled by | |
11879 | adjust_dynamic_symbol */ | |
11880 | gofarg.gotoff = gotoff; | |
10455f89 | 11881 | gofarg.info = info; |
c152c796 AM |
11882 | elf_link_hash_traverse (elf_hash_table (info), |
11883 | elf_gc_allocate_got_offsets, | |
11884 | &gofarg); | |
11885 | return TRUE; | |
11886 | } | |
11887 | ||
11888 | /* Many folk need no more in the way of final link than this, once | |
11889 | got entry reference counting is enabled. */ | |
11890 | ||
11891 | bfd_boolean | |
11892 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
11893 | { | |
11894 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
11895 | return FALSE; | |
11896 | ||
11897 | /* Invoke the regular ELF backend linker to do all the work. */ | |
11898 | return bfd_elf_final_link (abfd, info); | |
11899 | } | |
11900 | ||
11901 | bfd_boolean | |
11902 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
11903 | { | |
11904 | struct elf_reloc_cookie *rcookie = cookie; | |
11905 | ||
11906 | if (rcookie->bad_symtab) | |
11907 | rcookie->rel = rcookie->rels; | |
11908 | ||
11909 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
11910 | { | |
11911 | unsigned long r_symndx; | |
11912 | ||
11913 | if (! rcookie->bad_symtab) | |
11914 | if (rcookie->rel->r_offset > offset) | |
11915 | return FALSE; | |
11916 | if (rcookie->rel->r_offset != offset) | |
11917 | continue; | |
11918 | ||
11919 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
11920 | if (r_symndx == SHN_UNDEF) | |
11921 | return TRUE; | |
11922 | ||
11923 | if (r_symndx >= rcookie->locsymcount | |
11924 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
11925 | { | |
11926 | struct elf_link_hash_entry *h; | |
11927 | ||
11928 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
11929 | ||
11930 | while (h->root.type == bfd_link_hash_indirect | |
11931 | || h->root.type == bfd_link_hash_warning) | |
11932 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11933 | ||
11934 | if ((h->root.type == bfd_link_hash_defined | |
11935 | || h->root.type == bfd_link_hash_defweak) | |
11936 | && elf_discarded_section (h->root.u.def.section)) | |
11937 | return TRUE; | |
11938 | else | |
11939 | return FALSE; | |
11940 | } | |
11941 | else | |
11942 | { | |
11943 | /* It's not a relocation against a global symbol, | |
11944 | but it could be a relocation against a local | |
11945 | symbol for a discarded section. */ | |
11946 | asection *isec; | |
11947 | Elf_Internal_Sym *isym; | |
11948 | ||
11949 | /* Need to: get the symbol; get the section. */ | |
11950 | isym = &rcookie->locsyms[r_symndx]; | |
cb33740c AM |
11951 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); |
11952 | if (isec != NULL && elf_discarded_section (isec)) | |
11953 | return TRUE; | |
c152c796 AM |
11954 | } |
11955 | return FALSE; | |
11956 | } | |
11957 | return FALSE; | |
11958 | } | |
11959 | ||
11960 | /* Discard unneeded references to discarded sections. | |
11961 | Returns TRUE if any section's size was changed. */ | |
11962 | /* This function assumes that the relocations are in sorted order, | |
11963 | which is true for all known assemblers. */ | |
11964 | ||
11965 | bfd_boolean | |
11966 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) | |
11967 | { | |
11968 | struct elf_reloc_cookie cookie; | |
11969 | asection *stab, *eh; | |
c152c796 AM |
11970 | const struct elf_backend_data *bed; |
11971 | bfd *abfd; | |
c152c796 AM |
11972 | bfd_boolean ret = FALSE; |
11973 | ||
11974 | if (info->traditional_format | |
11975 | || !is_elf_hash_table (info->hash)) | |
11976 | return FALSE; | |
11977 | ||
ca92cecb | 11978 | _bfd_elf_begin_eh_frame_parsing (info); |
c152c796 AM |
11979 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) |
11980 | { | |
11981 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
11982 | continue; | |
11983 | ||
11984 | bed = get_elf_backend_data (abfd); | |
11985 | ||
11986 | if ((abfd->flags & DYNAMIC) != 0) | |
11987 | continue; | |
11988 | ||
8da3dbc5 AM |
11989 | eh = NULL; |
11990 | if (!info->relocatable) | |
11991 | { | |
11992 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); | |
11993 | if (eh != NULL | |
eea6121a | 11994 | && (eh->size == 0 |
8da3dbc5 AM |
11995 | || bfd_is_abs_section (eh->output_section))) |
11996 | eh = NULL; | |
11997 | } | |
c152c796 AM |
11998 | |
11999 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
12000 | if (stab != NULL | |
eea6121a | 12001 | && (stab->size == 0 |
c152c796 AM |
12002 | || bfd_is_abs_section (stab->output_section) |
12003 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) | |
12004 | stab = NULL; | |
12005 | ||
12006 | if (stab == NULL | |
12007 | && eh == NULL | |
12008 | && bed->elf_backend_discard_info == NULL) | |
12009 | continue; | |
12010 | ||
5241d853 RS |
12011 | if (!init_reloc_cookie (&cookie, info, abfd)) |
12012 | return FALSE; | |
c152c796 | 12013 | |
5241d853 RS |
12014 | if (stab != NULL |
12015 | && stab->reloc_count > 0 | |
12016 | && init_reloc_cookie_rels (&cookie, info, abfd, stab)) | |
c152c796 | 12017 | { |
5241d853 RS |
12018 | if (_bfd_discard_section_stabs (abfd, stab, |
12019 | elf_section_data (stab)->sec_info, | |
12020 | bfd_elf_reloc_symbol_deleted_p, | |
12021 | &cookie)) | |
12022 | ret = TRUE; | |
12023 | fini_reloc_cookie_rels (&cookie, stab); | |
c152c796 AM |
12024 | } |
12025 | ||
5241d853 RS |
12026 | if (eh != NULL |
12027 | && init_reloc_cookie_rels (&cookie, info, abfd, eh)) | |
c152c796 | 12028 | { |
ca92cecb | 12029 | _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie); |
c152c796 AM |
12030 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, |
12031 | bfd_elf_reloc_symbol_deleted_p, | |
12032 | &cookie)) | |
12033 | ret = TRUE; | |
5241d853 | 12034 | fini_reloc_cookie_rels (&cookie, eh); |
c152c796 AM |
12035 | } |
12036 | ||
12037 | if (bed->elf_backend_discard_info != NULL | |
12038 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
12039 | ret = TRUE; | |
12040 | ||
5241d853 | 12041 | fini_reloc_cookie (&cookie, abfd); |
c152c796 | 12042 | } |
ca92cecb | 12043 | _bfd_elf_end_eh_frame_parsing (info); |
c152c796 AM |
12044 | |
12045 | if (info->eh_frame_hdr | |
12046 | && !info->relocatable | |
12047 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
12048 | ret = TRUE; | |
12049 | ||
12050 | return ret; | |
12051 | } | |
082b7297 | 12052 | |
9659de1c AM |
12053 | /* For a SHT_GROUP section, return the group signature. For other |
12054 | sections, return the normal section name. */ | |
12055 | ||
12056 | static const char * | |
12057 | section_signature (asection *sec) | |
12058 | { | |
12059 | if ((sec->flags & SEC_GROUP) != 0 | |
12060 | && elf_next_in_group (sec) != NULL | |
12061 | && elf_group_name (elf_next_in_group (sec)) != NULL) | |
12062 | return elf_group_name (elf_next_in_group (sec)); | |
12063 | return sec->name; | |
12064 | } | |
12065 | ||
082b7297 | 12066 | void |
9659de1c | 12067 | _bfd_elf_section_already_linked (bfd *abfd, asection *sec, |
c0f00686 | 12068 | struct bfd_link_info *info) |
082b7297 L |
12069 | { |
12070 | flagword flags; | |
6d2cd210 | 12071 | const char *name, *p; |
082b7297 L |
12072 | struct bfd_section_already_linked *l; |
12073 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
3d7f7666 | 12074 | |
3d7f7666 L |
12075 | if (sec->output_section == bfd_abs_section_ptr) |
12076 | return; | |
082b7297 L |
12077 | |
12078 | flags = sec->flags; | |
3d7f7666 | 12079 | |
c2370991 AM |
12080 | /* Return if it isn't a linkonce section. A comdat group section |
12081 | also has SEC_LINK_ONCE set. */ | |
12082 | if ((flags & SEC_LINK_ONCE) == 0) | |
082b7297 L |
12083 | return; |
12084 | ||
c2370991 AM |
12085 | /* Don't put group member sections on our list of already linked |
12086 | sections. They are handled as a group via their group section. */ | |
12087 | if (elf_sec_group (sec) != NULL) | |
12088 | return; | |
3d7f7666 | 12089 | |
082b7297 L |
12090 | /* FIXME: When doing a relocatable link, we may have trouble |
12091 | copying relocations in other sections that refer to local symbols | |
12092 | in the section being discarded. Those relocations will have to | |
12093 | be converted somehow; as of this writing I'm not sure that any of | |
12094 | the backends handle that correctly. | |
12095 | ||
12096 | It is tempting to instead not discard link once sections when | |
12097 | doing a relocatable link (technically, they should be discarded | |
12098 | whenever we are building constructors). However, that fails, | |
12099 | because the linker winds up combining all the link once sections | |
12100 | into a single large link once section, which defeats the purpose | |
12101 | of having link once sections in the first place. | |
12102 | ||
12103 | Also, not merging link once sections in a relocatable link | |
12104 | causes trouble for MIPS ELF, which relies on link once semantics | |
12105 | to handle the .reginfo section correctly. */ | |
12106 | ||
9659de1c | 12107 | name = section_signature (sec); |
082b7297 | 12108 | |
0112cd26 | 12109 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
6d2cd210 JJ |
12110 | && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
12111 | p++; | |
12112 | else | |
12113 | p = name; | |
12114 | ||
12115 | already_linked_list = bfd_section_already_linked_table_lookup (p); | |
082b7297 L |
12116 | |
12117 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
12118 | { | |
c2370991 AM |
12119 | /* We may have 2 different types of sections on the list: group |
12120 | sections and linkonce sections. Match like sections. */ | |
3d7f7666 | 12121 | if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) |
9659de1c | 12122 | && strcmp (name, section_signature (l->sec)) == 0 |
082b7297 L |
12123 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL) |
12124 | { | |
12125 | /* The section has already been linked. See if we should | |
6d2cd210 | 12126 | issue a warning. */ |
082b7297 L |
12127 | switch (flags & SEC_LINK_DUPLICATES) |
12128 | { | |
12129 | default: | |
12130 | abort (); | |
12131 | ||
12132 | case SEC_LINK_DUPLICATES_DISCARD: | |
12133 | break; | |
12134 | ||
12135 | case SEC_LINK_DUPLICATES_ONE_ONLY: | |
12136 | (*_bfd_error_handler) | |
c93625e2 | 12137 | (_("%B: ignoring duplicate section `%A'"), |
d003868e | 12138 | abfd, sec); |
082b7297 L |
12139 | break; |
12140 | ||
12141 | case SEC_LINK_DUPLICATES_SAME_SIZE: | |
12142 | if (sec->size != l->sec->size) | |
12143 | (*_bfd_error_handler) | |
c93625e2 | 12144 | (_("%B: duplicate section `%A' has different size"), |
d003868e | 12145 | abfd, sec); |
082b7297 | 12146 | break; |
ea5158d8 DJ |
12147 | |
12148 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: | |
12149 | if (sec->size != l->sec->size) | |
12150 | (*_bfd_error_handler) | |
c93625e2 | 12151 | (_("%B: duplicate section `%A' has different size"), |
ea5158d8 DJ |
12152 | abfd, sec); |
12153 | else if (sec->size != 0) | |
12154 | { | |
12155 | bfd_byte *sec_contents, *l_sec_contents; | |
12156 | ||
12157 | if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents)) | |
12158 | (*_bfd_error_handler) | |
c93625e2 | 12159 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
12160 | abfd, sec); |
12161 | else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec, | |
12162 | &l_sec_contents)) | |
12163 | (*_bfd_error_handler) | |
c93625e2 | 12164 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
12165 | l->sec->owner, l->sec); |
12166 | else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) | |
12167 | (*_bfd_error_handler) | |
c93625e2 | 12168 | (_("%B: warning: duplicate section `%A' has different contents"), |
ea5158d8 DJ |
12169 | abfd, sec); |
12170 | ||
12171 | if (sec_contents) | |
12172 | free (sec_contents); | |
12173 | if (l_sec_contents) | |
12174 | free (l_sec_contents); | |
12175 | } | |
12176 | break; | |
082b7297 L |
12177 | } |
12178 | ||
12179 | /* Set the output_section field so that lang_add_section | |
12180 | does not create a lang_input_section structure for this | |
12181 | section. Since there might be a symbol in the section | |
12182 | being discarded, we must retain a pointer to the section | |
12183 | which we are really going to use. */ | |
12184 | sec->output_section = bfd_abs_section_ptr; | |
12185 | sec->kept_section = l->sec; | |
3b36f7e6 | 12186 | |
082b7297 | 12187 | if (flags & SEC_GROUP) |
3d7f7666 L |
12188 | { |
12189 | asection *first = elf_next_in_group (sec); | |
12190 | asection *s = first; | |
12191 | ||
12192 | while (s != NULL) | |
12193 | { | |
12194 | s->output_section = bfd_abs_section_ptr; | |
12195 | /* Record which group discards it. */ | |
12196 | s->kept_section = l->sec; | |
12197 | s = elf_next_in_group (s); | |
12198 | /* These lists are circular. */ | |
12199 | if (s == first) | |
12200 | break; | |
12201 | } | |
12202 | } | |
082b7297 L |
12203 | |
12204 | return; | |
12205 | } | |
12206 | } | |
12207 | ||
c2370991 AM |
12208 | /* A single member comdat group section may be discarded by a |
12209 | linkonce section and vice versa. */ | |
12210 | ||
12211 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 12212 | { |
c2370991 AM |
12213 | asection *first = elf_next_in_group (sec); |
12214 | ||
12215 | if (first != NULL && elf_next_in_group (first) == first) | |
12216 | /* Check this single member group against linkonce sections. */ | |
12217 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
12218 | if ((l->sec->flags & SEC_GROUP) == 0 | |
12219 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL | |
12220 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
12221 | { | |
12222 | first->output_section = bfd_abs_section_ptr; | |
12223 | first->kept_section = l->sec; | |
12224 | sec->output_section = bfd_abs_section_ptr; | |
12225 | break; | |
12226 | } | |
3d7f7666 L |
12227 | } |
12228 | else | |
c2370991 | 12229 | /* Check this linkonce section against single member groups. */ |
6d2cd210 JJ |
12230 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
12231 | if (l->sec->flags & SEC_GROUP) | |
12232 | { | |
12233 | asection *first = elf_next_in_group (l->sec); | |
12234 | ||
12235 | if (first != NULL | |
12236 | && elf_next_in_group (first) == first | |
c0f00686 | 12237 | && bfd_elf_match_symbols_in_sections (first, sec, info)) |
6d2cd210 JJ |
12238 | { |
12239 | sec->output_section = bfd_abs_section_ptr; | |
c2370991 | 12240 | sec->kept_section = first; |
6d2cd210 JJ |
12241 | break; |
12242 | } | |
12243 | } | |
12244 | ||
082b7297 | 12245 | /* This is the first section with this name. Record it. */ |
a6626e8c MS |
12246 | if (! bfd_section_already_linked_table_insert (already_linked_list, sec)) |
12247 | info->callbacks->einfo (_("%F%P: already_linked_table: %E")); | |
082b7297 | 12248 | } |
81e1b023 | 12249 | |
a4d8e49b L |
12250 | bfd_boolean |
12251 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
12252 | { | |
12253 | return sym->st_shndx == SHN_COMMON; | |
12254 | } | |
12255 | ||
12256 | unsigned int | |
12257 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
12258 | { | |
12259 | return SHN_COMMON; | |
12260 | } | |
12261 | ||
12262 | asection * | |
12263 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
12264 | { | |
12265 | return bfd_com_section_ptr; | |
12266 | } | |
10455f89 HPN |
12267 | |
12268 | bfd_vma | |
12269 | _bfd_elf_default_got_elt_size (bfd *abfd, | |
12270 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12271 | struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, | |
12272 | bfd *ibfd ATTRIBUTE_UNUSED, | |
12273 | unsigned long symndx ATTRIBUTE_UNUSED) | |
12274 | { | |
12275 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
12276 | return bed->s->arch_size / 8; | |
12277 | } | |
83bac4b0 NC |
12278 | |
12279 | /* Routines to support the creation of dynamic relocs. */ | |
12280 | ||
12281 | /* Return true if NAME is a name of a relocation | |
12282 | section associated with section S. */ | |
12283 | ||
12284 | static bfd_boolean | |
12285 | is_reloc_section (bfd_boolean rela, const char * name, asection * s) | |
12286 | { | |
12287 | if (rela) | |
12288 | return CONST_STRNEQ (name, ".rela") | |
12289 | && strcmp (bfd_get_section_name (NULL, s), name + 5) == 0; | |
12290 | ||
12291 | return CONST_STRNEQ (name, ".rel") | |
12292 | && strcmp (bfd_get_section_name (NULL, s), name + 4) == 0; | |
12293 | } | |
12294 | ||
12295 | /* Returns the name of the dynamic reloc section associated with SEC. */ | |
12296 | ||
12297 | static const char * | |
12298 | get_dynamic_reloc_section_name (bfd * abfd, | |
12299 | asection * sec, | |
12300 | bfd_boolean is_rela) | |
12301 | { | |
12302 | const char * name; | |
12303 | unsigned int strndx = elf_elfheader (abfd)->e_shstrndx; | |
12304 | unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name; | |
12305 | ||
12306 | name = bfd_elf_string_from_elf_section (abfd, strndx, shnam); | |
12307 | if (name == NULL) | |
12308 | return NULL; | |
12309 | ||
12310 | if (! is_reloc_section (is_rela, name, sec)) | |
12311 | { | |
12312 | static bfd_boolean complained = FALSE; | |
12313 | ||
12314 | if (! complained) | |
12315 | { | |
12316 | (*_bfd_error_handler) | |
12317 | (_("%B: bad relocation section name `%s\'"), abfd, name); | |
12318 | complained = TRUE; | |
12319 | } | |
12320 | name = NULL; | |
12321 | } | |
12322 | ||
12323 | return name; | |
12324 | } | |
12325 | ||
12326 | /* Returns the dynamic reloc section associated with SEC. | |
12327 | If necessary compute the name of the dynamic reloc section based | |
12328 | on SEC's name (looked up in ABFD's string table) and the setting | |
12329 | of IS_RELA. */ | |
12330 | ||
12331 | asection * | |
12332 | _bfd_elf_get_dynamic_reloc_section (bfd * abfd, | |
12333 | asection * sec, | |
12334 | bfd_boolean is_rela) | |
12335 | { | |
12336 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
12337 | ||
12338 | if (reloc_sec == NULL) | |
12339 | { | |
12340 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
12341 | ||
12342 | if (name != NULL) | |
12343 | { | |
12344 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
12345 | ||
12346 | if (reloc_sec != NULL) | |
12347 | elf_section_data (sec)->sreloc = reloc_sec; | |
12348 | } | |
12349 | } | |
12350 | ||
12351 | return reloc_sec; | |
12352 | } | |
12353 | ||
12354 | /* Returns the dynamic reloc section associated with SEC. If the | |
12355 | section does not exist it is created and attached to the DYNOBJ | |
12356 | bfd and stored in the SRELOC field of SEC's elf_section_data | |
12357 | structure. | |
12358 | ||
12359 | ALIGNMENT is the alignment for the newly created section and | |
12360 | IS_RELA defines whether the name should be .rela.<SEC's name> | |
12361 | or .rel.<SEC's name>. The section name is looked up in the | |
12362 | string table associated with ABFD. */ | |
12363 | ||
12364 | asection * | |
12365 | _bfd_elf_make_dynamic_reloc_section (asection * sec, | |
12366 | bfd * dynobj, | |
12367 | unsigned int alignment, | |
12368 | bfd * abfd, | |
12369 | bfd_boolean is_rela) | |
12370 | { | |
12371 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
12372 | ||
12373 | if (reloc_sec == NULL) | |
12374 | { | |
12375 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
12376 | ||
12377 | if (name == NULL) | |
12378 | return NULL; | |
12379 | ||
12380 | reloc_sec = bfd_get_section_by_name (dynobj, name); | |
12381 | ||
12382 | if (reloc_sec == NULL) | |
12383 | { | |
12384 | flagword flags; | |
12385 | ||
12386 | flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
12387 | if ((sec->flags & SEC_ALLOC) != 0) | |
12388 | flags |= SEC_ALLOC | SEC_LOAD; | |
12389 | ||
12390 | reloc_sec = bfd_make_section_with_flags (dynobj, name, flags); | |
12391 | if (reloc_sec != NULL) | |
12392 | { | |
12393 | if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) | |
12394 | reloc_sec = NULL; | |
12395 | } | |
12396 | } | |
12397 | ||
12398 | elf_section_data (sec)->sreloc = reloc_sec; | |
12399 | } | |
12400 | ||
12401 | return reloc_sec; | |
12402 | } |