Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
7e9f0867 | 2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 |
7898deda | 3 | Free Software Foundation, Inc. |
252b5132 | 4 | |
8fdd7217 | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
8fdd7217 NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
252b5132 | 11 | |
8fdd7217 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
8fdd7217 NC |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
3e110533 | 19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
252b5132 RH |
20 | |
21 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
23 | #include "bfdlink.h" | |
24 | #include "libbfd.h" | |
25 | #define ARCH_SIZE 0 | |
26 | #include "elf-bfd.h" | |
4ad4eba5 | 27 | #include "safe-ctype.h" |
ccf2f652 | 28 | #include "libiberty.h" |
252b5132 | 29 | |
b34976b6 | 30 | bfd_boolean |
268b6b39 | 31 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
32 | { |
33 | flagword flags; | |
aad5d350 | 34 | asection *s; |
252b5132 | 35 | struct elf_link_hash_entry *h; |
14a793b2 | 36 | struct bfd_link_hash_entry *bh; |
9c5bfbb7 | 37 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
38 | int ptralign; |
39 | ||
40 | /* This function may be called more than once. */ | |
aad5d350 AM |
41 | s = bfd_get_section_by_name (abfd, ".got"); |
42 | if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0) | |
b34976b6 | 43 | return TRUE; |
252b5132 RH |
44 | |
45 | switch (bed->s->arch_size) | |
46 | { | |
bb0deeff AO |
47 | case 32: |
48 | ptralign = 2; | |
49 | break; | |
50 | ||
51 | case 64: | |
52 | ptralign = 3; | |
53 | break; | |
54 | ||
55 | default: | |
56 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 57 | return FALSE; |
252b5132 RH |
58 | } |
59 | ||
e5a52504 | 60 | flags = bed->dynamic_sec_flags; |
252b5132 | 61 | |
3496cb2a | 62 | s = bfd_make_section_with_flags (abfd, ".got", flags); |
252b5132 | 63 | if (s == NULL |
252b5132 | 64 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 65 | return FALSE; |
252b5132 RH |
66 | |
67 | if (bed->want_got_plt) | |
68 | { | |
3496cb2a | 69 | s = bfd_make_section_with_flags (abfd, ".got.plt", flags); |
252b5132 | 70 | if (s == NULL |
252b5132 | 71 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 72 | return FALSE; |
252b5132 RH |
73 | } |
74 | ||
2517a57f AM |
75 | if (bed->want_got_sym) |
76 | { | |
77 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
78 | (or .got.plt) section. We don't do this in the linker script | |
79 | because we don't want to define the symbol if we are not creating | |
80 | a global offset table. */ | |
14a793b2 | 81 | bh = NULL; |
2517a57f AM |
82 | if (!(_bfd_generic_link_add_one_symbol |
83 | (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, | |
3b36f7e6 | 84 | 0, NULL, FALSE, bed->collect, &bh))) |
b34976b6 | 85 | return FALSE; |
14a793b2 | 86 | h = (struct elf_link_hash_entry *) bh; |
f5385ebf | 87 | h->def_regular = 1; |
2517a57f | 88 | h->type = STT_OBJECT; |
e6857c0c | 89 | h->other = STV_HIDDEN; |
252b5132 | 90 | |
36af4a4e | 91 | if (! info->executable |
c152c796 | 92 | && ! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 93 | return FALSE; |
252b5132 | 94 | |
2517a57f AM |
95 | elf_hash_table (info)->hgot = h; |
96 | } | |
252b5132 RH |
97 | |
98 | /* The first bit of the global offset table is the header. */ | |
3b36f7e6 | 99 | s->size += bed->got_header_size; |
252b5132 | 100 | |
b34976b6 | 101 | return TRUE; |
252b5132 RH |
102 | } |
103 | \f | |
7e9f0867 AM |
104 | /* Create a strtab to hold the dynamic symbol names. */ |
105 | static bfd_boolean | |
106 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
107 | { | |
108 | struct elf_link_hash_table *hash_table; | |
109 | ||
110 | hash_table = elf_hash_table (info); | |
111 | if (hash_table->dynobj == NULL) | |
112 | hash_table->dynobj = abfd; | |
113 | ||
114 | if (hash_table->dynstr == NULL) | |
115 | { | |
116 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
117 | if (hash_table->dynstr == NULL) | |
118 | return FALSE; | |
119 | } | |
120 | return TRUE; | |
121 | } | |
122 | ||
45d6a902 AM |
123 | /* Create some sections which will be filled in with dynamic linking |
124 | information. ABFD is an input file which requires dynamic sections | |
125 | to be created. The dynamic sections take up virtual memory space | |
126 | when the final executable is run, so we need to create them before | |
127 | addresses are assigned to the output sections. We work out the | |
128 | actual contents and size of these sections later. */ | |
252b5132 | 129 | |
b34976b6 | 130 | bfd_boolean |
268b6b39 | 131 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 132 | { |
45d6a902 AM |
133 | flagword flags; |
134 | register asection *s; | |
135 | struct elf_link_hash_entry *h; | |
136 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 137 | const struct elf_backend_data *bed; |
252b5132 | 138 | |
0eddce27 | 139 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
140 | return FALSE; |
141 | ||
142 | if (elf_hash_table (info)->dynamic_sections_created) | |
143 | return TRUE; | |
144 | ||
7e9f0867 AM |
145 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
146 | return FALSE; | |
45d6a902 | 147 | |
7e9f0867 | 148 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
149 | bed = get_elf_backend_data (abfd); |
150 | ||
151 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
152 | |
153 | /* A dynamically linked executable has a .interp section, but a | |
154 | shared library does not. */ | |
36af4a4e | 155 | if (info->executable) |
252b5132 | 156 | { |
3496cb2a L |
157 | s = bfd_make_section_with_flags (abfd, ".interp", |
158 | flags | SEC_READONLY); | |
159 | if (s == NULL) | |
45d6a902 AM |
160 | return FALSE; |
161 | } | |
bb0deeff | 162 | |
0eddce27 | 163 | if (! info->traditional_format) |
45d6a902 | 164 | { |
3496cb2a L |
165 | s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr", |
166 | flags | SEC_READONLY); | |
45d6a902 | 167 | if (s == NULL |
45d6a902 AM |
168 | || ! bfd_set_section_alignment (abfd, s, 2)) |
169 | return FALSE; | |
170 | elf_hash_table (info)->eh_info.hdr_sec = s; | |
171 | } | |
bb0deeff | 172 | |
45d6a902 AM |
173 | /* Create sections to hold version informations. These are removed |
174 | if they are not needed. */ | |
3496cb2a L |
175 | s = bfd_make_section_with_flags (abfd, ".gnu.version_d", |
176 | flags | SEC_READONLY); | |
45d6a902 | 177 | if (s == NULL |
45d6a902 AM |
178 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
179 | return FALSE; | |
180 | ||
3496cb2a L |
181 | s = bfd_make_section_with_flags (abfd, ".gnu.version", |
182 | flags | SEC_READONLY); | |
45d6a902 | 183 | if (s == NULL |
45d6a902 AM |
184 | || ! bfd_set_section_alignment (abfd, s, 1)) |
185 | return FALSE; | |
186 | ||
3496cb2a L |
187 | s = bfd_make_section_with_flags (abfd, ".gnu.version_r", |
188 | flags | SEC_READONLY); | |
45d6a902 | 189 | if (s == NULL |
45d6a902 AM |
190 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
191 | return FALSE; | |
192 | ||
3496cb2a L |
193 | s = bfd_make_section_with_flags (abfd, ".dynsym", |
194 | flags | SEC_READONLY); | |
45d6a902 | 195 | if (s == NULL |
45d6a902 AM |
196 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
197 | return FALSE; | |
198 | ||
3496cb2a L |
199 | s = bfd_make_section_with_flags (abfd, ".dynstr", |
200 | flags | SEC_READONLY); | |
201 | if (s == NULL) | |
45d6a902 AM |
202 | return FALSE; |
203 | ||
3496cb2a | 204 | s = bfd_make_section_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 205 | if (s == NULL |
45d6a902 AM |
206 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
207 | return FALSE; | |
208 | ||
209 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
210 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
211 | only want to define it if we are, in fact, creating a .dynamic | |
212 | section. We don't want to define it if there is no .dynamic | |
213 | section, since on some ELF platforms the start up code examines it | |
214 | to decide how to initialize the process. */ | |
215 | h = elf_link_hash_lookup (elf_hash_table (info), "_DYNAMIC", | |
216 | FALSE, FALSE, FALSE); | |
217 | if (h != NULL) | |
218 | { | |
219 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
220 | This is a symptom of a larger problem: Absolute symbols | |
221 | defined in shared libraries can't be overridden, because we | |
222 | lose the link to the bfd which is via the symbol section. */ | |
223 | h->root.type = bfd_link_hash_new; | |
224 | } | |
225 | bh = &h->root; | |
45d6a902 | 226 | if (! (_bfd_generic_link_add_one_symbol |
268b6b39 AM |
227 | (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE, |
228 | get_elf_backend_data (abfd)->collect, &bh))) | |
45d6a902 AM |
229 | return FALSE; |
230 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf | 231 | h->def_regular = 1; |
45d6a902 AM |
232 | h->type = STT_OBJECT; |
233 | ||
36af4a4e | 234 | if (! info->executable |
c152c796 | 235 | && ! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
236 | return FALSE; |
237 | ||
3496cb2a L |
238 | s = bfd_make_section_with_flags (abfd, ".hash", |
239 | flags | SEC_READONLY); | |
45d6a902 | 240 | if (s == NULL |
45d6a902 AM |
241 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
242 | return FALSE; | |
243 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
244 | ||
245 | /* Let the backend create the rest of the sections. This lets the | |
246 | backend set the right flags. The backend will normally create | |
247 | the .got and .plt sections. */ | |
248 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
249 | return FALSE; | |
250 | ||
251 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
252 | ||
253 | return TRUE; | |
254 | } | |
255 | ||
256 | /* Create dynamic sections when linking against a dynamic object. */ | |
257 | ||
258 | bfd_boolean | |
268b6b39 | 259 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
260 | { |
261 | flagword flags, pltflags; | |
262 | asection *s; | |
9c5bfbb7 | 263 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 264 | |
252b5132 RH |
265 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
266 | .rel[a].bss sections. */ | |
e5a52504 | 267 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
268 | |
269 | pltflags = flags; | |
252b5132 | 270 | if (bed->plt_not_loaded) |
6df4d94c MM |
271 | /* We do not clear SEC_ALLOC here because we still want the OS to |
272 | allocate space for the section; it's just that there's nothing | |
273 | to read in from the object file. */ | |
5d1634d7 | 274 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
275 | else |
276 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
277 | if (bed->plt_readonly) |
278 | pltflags |= SEC_READONLY; | |
279 | ||
3496cb2a | 280 | s = bfd_make_section_with_flags (abfd, ".plt", pltflags); |
252b5132 | 281 | if (s == NULL |
252b5132 | 282 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 283 | return FALSE; |
252b5132 RH |
284 | |
285 | if (bed->want_plt_sym) | |
286 | { | |
287 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the | |
288 | .plt section. */ | |
14a793b2 AM |
289 | struct elf_link_hash_entry *h; |
290 | struct bfd_link_hash_entry *bh = NULL; | |
291 | ||
252b5132 | 292 | if (! (_bfd_generic_link_add_one_symbol |
268b6b39 AM |
293 | (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL, |
294 | FALSE, get_elf_backend_data (abfd)->collect, &bh))) | |
b34976b6 | 295 | return FALSE; |
14a793b2 | 296 | h = (struct elf_link_hash_entry *) bh; |
f5385ebf | 297 | h->def_regular = 1; |
252b5132 RH |
298 | h->type = STT_OBJECT; |
299 | ||
36af4a4e | 300 | if (! info->executable |
c152c796 | 301 | && ! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 302 | return FALSE; |
252b5132 RH |
303 | } |
304 | ||
3496cb2a L |
305 | s = bfd_make_section_with_flags (abfd, |
306 | (bed->default_use_rela_p | |
307 | ? ".rela.plt" : ".rel.plt"), | |
308 | flags | SEC_READONLY); | |
252b5132 | 309 | if (s == NULL |
45d6a902 | 310 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 311 | return FALSE; |
252b5132 RH |
312 | |
313 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 314 | return FALSE; |
252b5132 | 315 | |
3018b441 RH |
316 | if (bed->want_dynbss) |
317 | { | |
318 | /* The .dynbss section is a place to put symbols which are defined | |
319 | by dynamic objects, are referenced by regular objects, and are | |
320 | not functions. We must allocate space for them in the process | |
321 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
322 | initialize them at run time. The linker script puts the .dynbss | |
323 | section into the .bss section of the final image. */ | |
3496cb2a L |
324 | s = bfd_make_section_with_flags (abfd, ".dynbss", |
325 | (SEC_ALLOC | |
326 | | SEC_LINKER_CREATED)); | |
327 | if (s == NULL) | |
b34976b6 | 328 | return FALSE; |
252b5132 | 329 | |
3018b441 | 330 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
331 | normally needed. We need to create it here, though, so that the |
332 | linker will map it to an output section. We can't just create it | |
333 | only if we need it, because we will not know whether we need it | |
334 | until we have seen all the input files, and the first time the | |
335 | main linker code calls BFD after examining all the input files | |
336 | (size_dynamic_sections) the input sections have already been | |
337 | mapped to the output sections. If the section turns out not to | |
338 | be needed, we can discard it later. We will never need this | |
339 | section when generating a shared object, since they do not use | |
340 | copy relocs. */ | |
3018b441 RH |
341 | if (! info->shared) |
342 | { | |
3496cb2a L |
343 | s = bfd_make_section_with_flags (abfd, |
344 | (bed->default_use_rela_p | |
345 | ? ".rela.bss" : ".rel.bss"), | |
346 | flags | SEC_READONLY); | |
3018b441 | 347 | if (s == NULL |
45d6a902 | 348 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 349 | return FALSE; |
3018b441 | 350 | } |
252b5132 RH |
351 | } |
352 | ||
b34976b6 | 353 | return TRUE; |
252b5132 RH |
354 | } |
355 | \f | |
252b5132 RH |
356 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
357 | read the input files, since we need to have a list of all of them | |
358 | before we can determine the final sizes of the output sections. | |
359 | Note that we may actually call this function even though we are not | |
360 | going to output any dynamic symbols; in some cases we know that a | |
361 | symbol should be in the dynamic symbol table, but only if there is | |
362 | one. */ | |
363 | ||
b34976b6 | 364 | bfd_boolean |
c152c796 AM |
365 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
366 | struct elf_link_hash_entry *h) | |
252b5132 RH |
367 | { |
368 | if (h->dynindx == -1) | |
369 | { | |
2b0f7ef9 | 370 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 371 | char *p; |
252b5132 | 372 | const char *name; |
252b5132 RH |
373 | bfd_size_type indx; |
374 | ||
7a13edea NC |
375 | /* XXX: The ABI draft says the linker must turn hidden and |
376 | internal symbols into STB_LOCAL symbols when producing the | |
377 | DSO. However, if ld.so honors st_other in the dynamic table, | |
378 | this would not be necessary. */ | |
379 | switch (ELF_ST_VISIBILITY (h->other)) | |
380 | { | |
381 | case STV_INTERNAL: | |
382 | case STV_HIDDEN: | |
9d6eee78 L |
383 | if (h->root.type != bfd_link_hash_undefined |
384 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 385 | { |
f5385ebf | 386 | h->forced_local = 1; |
67687978 PB |
387 | if (!elf_hash_table (info)->is_relocatable_executable) |
388 | return TRUE; | |
7a13edea | 389 | } |
0444bdd4 | 390 | |
7a13edea NC |
391 | default: |
392 | break; | |
393 | } | |
394 | ||
252b5132 RH |
395 | h->dynindx = elf_hash_table (info)->dynsymcount; |
396 | ++elf_hash_table (info)->dynsymcount; | |
397 | ||
398 | dynstr = elf_hash_table (info)->dynstr; | |
399 | if (dynstr == NULL) | |
400 | { | |
401 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 402 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 403 | if (dynstr == NULL) |
b34976b6 | 404 | return FALSE; |
252b5132 RH |
405 | } |
406 | ||
407 | /* We don't put any version information in the dynamic string | |
aad5d350 | 408 | table. */ |
252b5132 RH |
409 | name = h->root.root.string; |
410 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
411 | if (p != NULL) |
412 | /* We know that the p points into writable memory. In fact, | |
413 | there are only a few symbols that have read-only names, being | |
414 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
415 | by the backends. Most symbols will have names pointing into | |
416 | an ELF string table read from a file, or to objalloc memory. */ | |
417 | *p = 0; | |
418 | ||
419 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
420 | ||
421 | if (p != NULL) | |
422 | *p = ELF_VER_CHR; | |
252b5132 RH |
423 | |
424 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 425 | return FALSE; |
252b5132 RH |
426 | h->dynstr_index = indx; |
427 | } | |
428 | ||
b34976b6 | 429 | return TRUE; |
252b5132 | 430 | } |
45d6a902 AM |
431 | \f |
432 | /* Record an assignment to a symbol made by a linker script. We need | |
433 | this in case some dynamic object refers to this symbol. */ | |
434 | ||
435 | bfd_boolean | |
268b6b39 AM |
436 | bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED, |
437 | struct bfd_link_info *info, | |
438 | const char *name, | |
439 | bfd_boolean provide) | |
45d6a902 AM |
440 | { |
441 | struct elf_link_hash_entry *h; | |
4ea42fb7 | 442 | struct elf_link_hash_table *htab; |
45d6a902 | 443 | |
0eddce27 | 444 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
445 | return TRUE; |
446 | ||
4ea42fb7 AM |
447 | htab = elf_hash_table (info); |
448 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 449 | if (h == NULL) |
4ea42fb7 | 450 | return provide; |
45d6a902 | 451 | |
02bb6eae AO |
452 | /* Since we're defining the symbol, don't let it seem to have not |
453 | been defined. record_dynamic_symbol and size_dynamic_sections | |
77cfaee6 | 454 | may depend on this. */ |
02bb6eae AO |
455 | if (h->root.type == bfd_link_hash_undefweak |
456 | || h->root.type == bfd_link_hash_undefined) | |
77cfaee6 | 457 | { |
4ea42fb7 | 458 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
459 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
460 | bfd_link_repair_undef_list (&htab->root); | |
77cfaee6 | 461 | } |
02bb6eae | 462 | |
45d6a902 | 463 | if (h->root.type == bfd_link_hash_new) |
f5385ebf | 464 | h->non_elf = 0; |
45d6a902 AM |
465 | |
466 | /* If this symbol is being provided by the linker script, and it is | |
467 | currently defined by a dynamic object, but not by a regular | |
468 | object, then mark it as undefined so that the generic linker will | |
469 | force the correct value. */ | |
470 | if (provide | |
f5385ebf AM |
471 | && h->def_dynamic |
472 | && !h->def_regular) | |
45d6a902 AM |
473 | h->root.type = bfd_link_hash_undefined; |
474 | ||
475 | /* If this symbol is not being provided by the linker script, and it is | |
476 | currently defined by a dynamic object, but not by a regular object, | |
477 | then clear out any version information because the symbol will not be | |
478 | associated with the dynamic object any more. */ | |
479 | if (!provide | |
f5385ebf AM |
480 | && h->def_dynamic |
481 | && !h->def_regular) | |
45d6a902 AM |
482 | h->verinfo.verdef = NULL; |
483 | ||
f5385ebf | 484 | h->def_regular = 1; |
45d6a902 | 485 | |
6fa3860b PB |
486 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
487 | and executables. */ | |
488 | if (!info->relocatable | |
489 | && h->dynindx != -1 | |
490 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
491 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
492 | h->forced_local = 1; | |
493 | ||
f5385ebf AM |
494 | if ((h->def_dynamic |
495 | || h->ref_dynamic | |
67687978 PB |
496 | || info->shared |
497 | || (info->executable && elf_hash_table (info)->is_relocatable_executable)) | |
45d6a902 AM |
498 | && h->dynindx == -1) |
499 | { | |
c152c796 | 500 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
501 | return FALSE; |
502 | ||
503 | /* If this is a weak defined symbol, and we know a corresponding | |
504 | real symbol from the same dynamic object, make sure the real | |
505 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
506 | if (h->u.weakdef != NULL |
507 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 508 | { |
f6e332e6 | 509 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
510 | return FALSE; |
511 | } | |
512 | } | |
513 | ||
514 | return TRUE; | |
515 | } | |
42751cf3 | 516 | |
8c58d23b AM |
517 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
518 | success, and 2 on a failure caused by attempting to record a symbol | |
519 | in a discarded section, eg. a discarded link-once section symbol. */ | |
520 | ||
521 | int | |
c152c796 AM |
522 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
523 | bfd *input_bfd, | |
524 | long input_indx) | |
8c58d23b AM |
525 | { |
526 | bfd_size_type amt; | |
527 | struct elf_link_local_dynamic_entry *entry; | |
528 | struct elf_link_hash_table *eht; | |
529 | struct elf_strtab_hash *dynstr; | |
530 | unsigned long dynstr_index; | |
531 | char *name; | |
532 | Elf_External_Sym_Shndx eshndx; | |
533 | char esym[sizeof (Elf64_External_Sym)]; | |
534 | ||
0eddce27 | 535 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
536 | return 0; |
537 | ||
538 | /* See if the entry exists already. */ | |
539 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
540 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
541 | return 1; | |
542 | ||
543 | amt = sizeof (*entry); | |
268b6b39 | 544 | entry = bfd_alloc (input_bfd, amt); |
8c58d23b AM |
545 | if (entry == NULL) |
546 | return 0; | |
547 | ||
548 | /* Go find the symbol, so that we can find it's name. */ | |
549 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 550 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
551 | { |
552 | bfd_release (input_bfd, entry); | |
553 | return 0; | |
554 | } | |
555 | ||
556 | if (entry->isym.st_shndx != SHN_UNDEF | |
557 | && (entry->isym.st_shndx < SHN_LORESERVE | |
558 | || entry->isym.st_shndx > SHN_HIRESERVE)) | |
559 | { | |
560 | asection *s; | |
561 | ||
562 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
563 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
564 | { | |
565 | /* We can still bfd_release here as nothing has done another | |
566 | bfd_alloc. We can't do this later in this function. */ | |
567 | bfd_release (input_bfd, entry); | |
568 | return 2; | |
569 | } | |
570 | } | |
571 | ||
572 | name = (bfd_elf_string_from_elf_section | |
573 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
574 | entry->isym.st_name)); | |
575 | ||
576 | dynstr = elf_hash_table (info)->dynstr; | |
577 | if (dynstr == NULL) | |
578 | { | |
579 | /* Create a strtab to hold the dynamic symbol names. */ | |
580 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
581 | if (dynstr == NULL) | |
582 | return 0; | |
583 | } | |
584 | ||
b34976b6 | 585 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
586 | if (dynstr_index == (unsigned long) -1) |
587 | return 0; | |
588 | entry->isym.st_name = dynstr_index; | |
589 | ||
590 | eht = elf_hash_table (info); | |
591 | ||
592 | entry->next = eht->dynlocal; | |
593 | eht->dynlocal = entry; | |
594 | entry->input_bfd = input_bfd; | |
595 | entry->input_indx = input_indx; | |
596 | eht->dynsymcount++; | |
597 | ||
598 | /* Whatever binding the symbol had before, it's now local. */ | |
599 | entry->isym.st_info | |
600 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
601 | ||
602 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
603 | ||
604 | return 1; | |
605 | } | |
606 | ||
30b30c21 | 607 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 608 | |
30b30c21 | 609 | long |
268b6b39 AM |
610 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
611 | bfd *input_bfd, | |
612 | long input_indx) | |
30b30c21 RH |
613 | { |
614 | struct elf_link_local_dynamic_entry *e; | |
615 | ||
616 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
617 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
618 | return e->dynindx; | |
619 | return -1; | |
620 | } | |
621 | ||
622 | /* This function is used to renumber the dynamic symbols, if some of | |
623 | them are removed because they are marked as local. This is called | |
624 | via elf_link_hash_traverse. */ | |
625 | ||
b34976b6 | 626 | static bfd_boolean |
268b6b39 AM |
627 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
628 | void *data) | |
42751cf3 | 629 | { |
268b6b39 | 630 | size_t *count = data; |
30b30c21 | 631 | |
e92d460e AM |
632 | if (h->root.type == bfd_link_hash_warning) |
633 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
634 | ||
6fa3860b PB |
635 | if (h->forced_local) |
636 | return TRUE; | |
637 | ||
638 | if (h->dynindx != -1) | |
639 | h->dynindx = ++(*count); | |
640 | ||
641 | return TRUE; | |
642 | } | |
643 | ||
644 | ||
645 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
646 | STB_LOCAL binding. */ | |
647 | ||
648 | static bfd_boolean | |
649 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
650 | void *data) | |
651 | { | |
652 | size_t *count = data; | |
653 | ||
654 | if (h->root.type == bfd_link_hash_warning) | |
655 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
656 | ||
657 | if (!h->forced_local) | |
658 | return TRUE; | |
659 | ||
42751cf3 | 660 | if (h->dynindx != -1) |
30b30c21 RH |
661 | h->dynindx = ++(*count); |
662 | ||
b34976b6 | 663 | return TRUE; |
42751cf3 | 664 | } |
30b30c21 | 665 | |
aee6f5b4 AO |
666 | /* Return true if the dynamic symbol for a given section should be |
667 | omitted when creating a shared library. */ | |
668 | bfd_boolean | |
669 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
670 | struct bfd_link_info *info, | |
671 | asection *p) | |
672 | { | |
673 | switch (elf_section_data (p)->this_hdr.sh_type) | |
674 | { | |
675 | case SHT_PROGBITS: | |
676 | case SHT_NOBITS: | |
677 | /* If sh_type is yet undecided, assume it could be | |
678 | SHT_PROGBITS/SHT_NOBITS. */ | |
679 | case SHT_NULL: | |
680 | if (strcmp (p->name, ".got") == 0 | |
681 | || strcmp (p->name, ".got.plt") == 0 | |
682 | || strcmp (p->name, ".plt") == 0) | |
683 | { | |
684 | asection *ip; | |
685 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
686 | ||
687 | if (dynobj != NULL | |
1da212d6 | 688 | && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL |
aee6f5b4 AO |
689 | && (ip->flags & SEC_LINKER_CREATED) |
690 | && ip->output_section == p) | |
691 | return TRUE; | |
692 | } | |
693 | return FALSE; | |
694 | ||
695 | /* There shouldn't be section relative relocations | |
696 | against any other section. */ | |
697 | default: | |
698 | return TRUE; | |
699 | } | |
700 | } | |
701 | ||
062e2358 | 702 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
703 | symbol for each output section, which come first. Next come symbols |
704 | which have been forced to local binding. Then all of the back-end | |
705 | allocated local dynamic syms, followed by the rest of the global | |
706 | symbols. */ | |
30b30c21 | 707 | |
554220db AM |
708 | static unsigned long |
709 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
710 | struct bfd_link_info *info, | |
711 | unsigned long *section_sym_count) | |
30b30c21 RH |
712 | { |
713 | unsigned long dynsymcount = 0; | |
714 | ||
67687978 | 715 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
30b30c21 | 716 | { |
aee6f5b4 | 717 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
718 | asection *p; |
719 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 720 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
721 | && (p->flags & SEC_ALLOC) != 0 |
722 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
723 | elf_section_data (p)->dynindx = ++dynsymcount; | |
30b30c21 | 724 | } |
554220db | 725 | *section_sym_count = dynsymcount; |
30b30c21 | 726 | |
6fa3860b PB |
727 | elf_link_hash_traverse (elf_hash_table (info), |
728 | elf_link_renumber_local_hash_table_dynsyms, | |
729 | &dynsymcount); | |
730 | ||
30b30c21 RH |
731 | if (elf_hash_table (info)->dynlocal) |
732 | { | |
733 | struct elf_link_local_dynamic_entry *p; | |
734 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
735 | p->dynindx = ++dynsymcount; | |
736 | } | |
737 | ||
738 | elf_link_hash_traverse (elf_hash_table (info), | |
739 | elf_link_renumber_hash_table_dynsyms, | |
740 | &dynsymcount); | |
741 | ||
742 | /* There is an unused NULL entry at the head of the table which | |
743 | we must account for in our count. Unless there weren't any | |
744 | symbols, which means we'll have no table at all. */ | |
745 | if (dynsymcount != 0) | |
746 | ++dynsymcount; | |
747 | ||
748 | return elf_hash_table (info)->dynsymcount = dynsymcount; | |
749 | } | |
252b5132 | 750 | |
45d6a902 AM |
751 | /* This function is called when we want to define a new symbol. It |
752 | handles the various cases which arise when we find a definition in | |
753 | a dynamic object, or when there is already a definition in a | |
754 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
755 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
756 | OVERRIDE if the old symbol is overriding a new definition. We set | |
757 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
758 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
759 | change, we mean that we shouldn't warn if the type or size does | |
af44c138 L |
760 | change. We set POLD_ALIGNMENT if an old common symbol in a dynamic |
761 | object is overridden by a regular object. */ | |
45d6a902 AM |
762 | |
763 | bfd_boolean | |
268b6b39 AM |
764 | _bfd_elf_merge_symbol (bfd *abfd, |
765 | struct bfd_link_info *info, | |
766 | const char *name, | |
767 | Elf_Internal_Sym *sym, | |
768 | asection **psec, | |
769 | bfd_vma *pvalue, | |
af44c138 | 770 | unsigned int *pold_alignment, |
268b6b39 AM |
771 | struct elf_link_hash_entry **sym_hash, |
772 | bfd_boolean *skip, | |
773 | bfd_boolean *override, | |
774 | bfd_boolean *type_change_ok, | |
0f8a2703 | 775 | bfd_boolean *size_change_ok) |
252b5132 | 776 | { |
7479dfd4 | 777 | asection *sec, *oldsec; |
45d6a902 AM |
778 | struct elf_link_hash_entry *h; |
779 | struct elf_link_hash_entry *flip; | |
780 | int bind; | |
781 | bfd *oldbfd; | |
782 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
77cfaee6 | 783 | bfd_boolean newweak, oldweak; |
45d6a902 AM |
784 | |
785 | *skip = FALSE; | |
786 | *override = FALSE; | |
787 | ||
788 | sec = *psec; | |
789 | bind = ELF_ST_BIND (sym->st_info); | |
790 | ||
791 | if (! bfd_is_und_section (sec)) | |
792 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
793 | else | |
794 | h = ((struct elf_link_hash_entry *) | |
795 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
796 | if (h == NULL) | |
797 | return FALSE; | |
798 | *sym_hash = h; | |
252b5132 | 799 | |
45d6a902 AM |
800 | /* This code is for coping with dynamic objects, and is only useful |
801 | if we are doing an ELF link. */ | |
802 | if (info->hash->creator != abfd->xvec) | |
803 | return TRUE; | |
252b5132 | 804 | |
45d6a902 AM |
805 | /* For merging, we only care about real symbols. */ |
806 | ||
807 | while (h->root.type == bfd_link_hash_indirect | |
808 | || h->root.type == bfd_link_hash_warning) | |
809 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
810 | ||
811 | /* If we just created the symbol, mark it as being an ELF symbol. | |
812 | Other than that, there is nothing to do--there is no merge issue | |
813 | with a newly defined symbol--so we just return. */ | |
814 | ||
815 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 816 | { |
f5385ebf | 817 | h->non_elf = 0; |
45d6a902 AM |
818 | return TRUE; |
819 | } | |
252b5132 | 820 | |
7479dfd4 L |
821 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
822 | existing symbol. */ | |
252b5132 | 823 | |
45d6a902 AM |
824 | switch (h->root.type) |
825 | { | |
826 | default: | |
827 | oldbfd = NULL; | |
7479dfd4 | 828 | oldsec = NULL; |
45d6a902 | 829 | break; |
252b5132 | 830 | |
45d6a902 AM |
831 | case bfd_link_hash_undefined: |
832 | case bfd_link_hash_undefweak: | |
833 | oldbfd = h->root.u.undef.abfd; | |
7479dfd4 | 834 | oldsec = NULL; |
45d6a902 AM |
835 | break; |
836 | ||
837 | case bfd_link_hash_defined: | |
838 | case bfd_link_hash_defweak: | |
839 | oldbfd = h->root.u.def.section->owner; | |
7479dfd4 | 840 | oldsec = h->root.u.def.section; |
45d6a902 AM |
841 | break; |
842 | ||
843 | case bfd_link_hash_common: | |
844 | oldbfd = h->root.u.c.p->section->owner; | |
7479dfd4 | 845 | oldsec = h->root.u.c.p->section; |
45d6a902 AM |
846 | break; |
847 | } | |
848 | ||
849 | /* In cases involving weak versioned symbols, we may wind up trying | |
850 | to merge a symbol with itself. Catch that here, to avoid the | |
851 | confusion that results if we try to override a symbol with | |
852 | itself. The additional tests catch cases like | |
853 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
854 | dynamic object, which we do want to handle here. */ | |
855 | if (abfd == oldbfd | |
856 | && ((abfd->flags & DYNAMIC) == 0 | |
f5385ebf | 857 | || !h->def_regular)) |
45d6a902 AM |
858 | return TRUE; |
859 | ||
860 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
861 | respectively, is from a dynamic object. */ | |
862 | ||
863 | if ((abfd->flags & DYNAMIC) != 0) | |
864 | newdyn = TRUE; | |
865 | else | |
866 | newdyn = FALSE; | |
867 | ||
868 | if (oldbfd != NULL) | |
869 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
870 | else | |
871 | { | |
872 | asection *hsec; | |
873 | ||
874 | /* This code handles the special SHN_MIPS_{TEXT,DATA} section | |
875 | indices used by MIPS ELF. */ | |
876 | switch (h->root.type) | |
252b5132 | 877 | { |
45d6a902 AM |
878 | default: |
879 | hsec = NULL; | |
880 | break; | |
252b5132 | 881 | |
45d6a902 AM |
882 | case bfd_link_hash_defined: |
883 | case bfd_link_hash_defweak: | |
884 | hsec = h->root.u.def.section; | |
885 | break; | |
252b5132 | 886 | |
45d6a902 AM |
887 | case bfd_link_hash_common: |
888 | hsec = h->root.u.c.p->section; | |
889 | break; | |
252b5132 | 890 | } |
252b5132 | 891 | |
45d6a902 AM |
892 | if (hsec == NULL) |
893 | olddyn = FALSE; | |
894 | else | |
895 | olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0; | |
896 | } | |
252b5132 | 897 | |
45d6a902 AM |
898 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
899 | respectively, appear to be a definition rather than reference. */ | |
900 | ||
901 | if (bfd_is_und_section (sec) || bfd_is_com_section (sec)) | |
902 | newdef = FALSE; | |
903 | else | |
904 | newdef = TRUE; | |
905 | ||
906 | if (h->root.type == bfd_link_hash_undefined | |
907 | || h->root.type == bfd_link_hash_undefweak | |
908 | || h->root.type == bfd_link_hash_common) | |
909 | olddef = FALSE; | |
910 | else | |
911 | olddef = TRUE; | |
912 | ||
7479dfd4 L |
913 | /* Check TLS symbol. */ |
914 | if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS) | |
915 | && ELF_ST_TYPE (sym->st_info) != h->type) | |
916 | { | |
917 | bfd *ntbfd, *tbfd; | |
918 | bfd_boolean ntdef, tdef; | |
919 | asection *ntsec, *tsec; | |
920 | ||
921 | if (h->type == STT_TLS) | |
922 | { | |
3b36f7e6 | 923 | ntbfd = abfd; |
7479dfd4 L |
924 | ntsec = sec; |
925 | ntdef = newdef; | |
926 | tbfd = oldbfd; | |
927 | tsec = oldsec; | |
928 | tdef = olddef; | |
929 | } | |
930 | else | |
931 | { | |
932 | ntbfd = oldbfd; | |
933 | ntsec = oldsec; | |
934 | ntdef = olddef; | |
935 | tbfd = abfd; | |
936 | tsec = sec; | |
937 | tdef = newdef; | |
938 | } | |
939 | ||
940 | if (tdef && ntdef) | |
941 | (*_bfd_error_handler) | |
942 | (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), | |
943 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); | |
944 | else if (!tdef && !ntdef) | |
945 | (*_bfd_error_handler) | |
946 | (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), | |
947 | tbfd, ntbfd, h->root.root.string); | |
948 | else if (tdef) | |
949 | (*_bfd_error_handler) | |
950 | (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), | |
951 | tbfd, tsec, ntbfd, h->root.root.string); | |
952 | else | |
953 | (*_bfd_error_handler) | |
954 | (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), | |
955 | tbfd, ntbfd, ntsec, h->root.root.string); | |
956 | ||
957 | bfd_set_error (bfd_error_bad_value); | |
958 | return FALSE; | |
959 | } | |
960 | ||
4cc11e76 | 961 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
962 | object or is weak in all dynamic objects. Internal and hidden |
963 | visibility will make it unavailable to dynamic objects. */ | |
f5385ebf | 964 | if (newdyn && !h->dynamic_def) |
45d6a902 AM |
965 | { |
966 | if (!bfd_is_und_section (sec)) | |
f5385ebf | 967 | h->dynamic_def = 1; |
45d6a902 | 968 | else |
252b5132 | 969 | { |
45d6a902 AM |
970 | /* Check if this symbol is weak in all dynamic objects. If it |
971 | is the first time we see it in a dynamic object, we mark | |
972 | if it is weak. Otherwise, we clear it. */ | |
f5385ebf | 973 | if (!h->ref_dynamic) |
79349b09 | 974 | { |
45d6a902 | 975 | if (bind == STB_WEAK) |
f5385ebf | 976 | h->dynamic_weak = 1; |
252b5132 | 977 | } |
45d6a902 | 978 | else if (bind != STB_WEAK) |
f5385ebf | 979 | h->dynamic_weak = 0; |
252b5132 | 980 | } |
45d6a902 | 981 | } |
252b5132 | 982 | |
45d6a902 AM |
983 | /* If the old symbol has non-default visibility, we ignore the new |
984 | definition from a dynamic object. */ | |
985 | if (newdyn | |
9c7a29a3 | 986 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
987 | && !bfd_is_und_section (sec)) |
988 | { | |
989 | *skip = TRUE; | |
990 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 991 | h->ref_dynamic = 1; |
45d6a902 AM |
992 | /* A protected symbol has external availability. Make sure it is |
993 | recorded as dynamic. | |
994 | ||
995 | FIXME: Should we check type and size for protected symbol? */ | |
996 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 997 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
998 | else |
999 | return TRUE; | |
1000 | } | |
1001 | else if (!newdyn | |
9c7a29a3 | 1002 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1003 | && h->def_dynamic) |
45d6a902 AM |
1004 | { |
1005 | /* If the new symbol with non-default visibility comes from a | |
1006 | relocatable file and the old definition comes from a dynamic | |
1007 | object, we remove the old definition. */ | |
1008 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1009 | h = *sym_hash; | |
1de1a317 | 1010 | |
f6e332e6 | 1011 | if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root) |
1de1a317 L |
1012 | && bfd_is_und_section (sec)) |
1013 | { | |
1014 | /* If the new symbol is undefined and the old symbol was | |
1015 | also undefined before, we need to make sure | |
1016 | _bfd_generic_link_add_one_symbol doesn't mess | |
f6e332e6 | 1017 | up the linker hash table undefs list. Since the old |
1de1a317 L |
1018 | definition came from a dynamic object, it is still on the |
1019 | undefs list. */ | |
1020 | h->root.type = bfd_link_hash_undefined; | |
1de1a317 L |
1021 | h->root.u.undef.abfd = abfd; |
1022 | } | |
1023 | else | |
1024 | { | |
1025 | h->root.type = bfd_link_hash_new; | |
1026 | h->root.u.undef.abfd = NULL; | |
1027 | } | |
1028 | ||
f5385ebf | 1029 | if (h->def_dynamic) |
252b5132 | 1030 | { |
f5385ebf AM |
1031 | h->def_dynamic = 0; |
1032 | h->ref_dynamic = 1; | |
1033 | h->dynamic_def = 1; | |
45d6a902 AM |
1034 | } |
1035 | /* FIXME: Should we check type and size for protected symbol? */ | |
1036 | h->size = 0; | |
1037 | h->type = 0; | |
1038 | return TRUE; | |
1039 | } | |
14a793b2 | 1040 | |
79349b09 AM |
1041 | /* Differentiate strong and weak symbols. */ |
1042 | newweak = bind == STB_WEAK; | |
1043 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1044 | || h->root.type == bfd_link_hash_undefweak); | |
14a793b2 | 1045 | |
15b43f48 AM |
1046 | /* If a new weak symbol definition comes from a regular file and the |
1047 | old symbol comes from a dynamic library, we treat the new one as | |
1048 | strong. Similarly, an old weak symbol definition from a regular | |
1049 | file is treated as strong when the new symbol comes from a dynamic | |
1050 | library. Further, an old weak symbol from a dynamic library is | |
1051 | treated as strong if the new symbol is from a dynamic library. | |
1052 | This reflects the way glibc's ld.so works. | |
1053 | ||
1054 | Do this before setting *type_change_ok or *size_change_ok so that | |
1055 | we warn properly when dynamic library symbols are overridden. */ | |
1056 | ||
1057 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1058 | newweak = FALSE; |
15b43f48 | 1059 | if (olddef && newdyn) |
0f8a2703 AM |
1060 | oldweak = FALSE; |
1061 | ||
79349b09 AM |
1062 | /* It's OK to change the type if either the existing symbol or the |
1063 | new symbol is weak. A type change is also OK if the old symbol | |
1064 | is undefined and the new symbol is defined. */ | |
252b5132 | 1065 | |
79349b09 AM |
1066 | if (oldweak |
1067 | || newweak | |
1068 | || (newdef | |
1069 | && h->root.type == bfd_link_hash_undefined)) | |
1070 | *type_change_ok = TRUE; | |
1071 | ||
1072 | /* It's OK to change the size if either the existing symbol or the | |
1073 | new symbol is weak, or if the old symbol is undefined. */ | |
1074 | ||
1075 | if (*type_change_ok | |
1076 | || h->root.type == bfd_link_hash_undefined) | |
1077 | *size_change_ok = TRUE; | |
45d6a902 | 1078 | |
45d6a902 AM |
1079 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1080 | symbol, respectively, appears to be a common symbol in a dynamic | |
1081 | object. If a symbol appears in an uninitialized section, and is | |
1082 | not weak, and is not a function, then it may be a common symbol | |
1083 | which was resolved when the dynamic object was created. We want | |
1084 | to treat such symbols specially, because they raise special | |
1085 | considerations when setting the symbol size: if the symbol | |
1086 | appears as a common symbol in a regular object, and the size in | |
1087 | the regular object is larger, we must make sure that we use the | |
1088 | larger size. This problematic case can always be avoided in C, | |
1089 | but it must be handled correctly when using Fortran shared | |
1090 | libraries. | |
1091 | ||
1092 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1093 | likewise for OLDDYNCOMMON and OLDDEF. | |
1094 | ||
1095 | Note that this test is just a heuristic, and that it is quite | |
1096 | possible to have an uninitialized symbol in a shared object which | |
1097 | is really a definition, rather than a common symbol. This could | |
1098 | lead to some minor confusion when the symbol really is a common | |
1099 | symbol in some regular object. However, I think it will be | |
1100 | harmless. */ | |
1101 | ||
1102 | if (newdyn | |
1103 | && newdef | |
79349b09 | 1104 | && !newweak |
45d6a902 AM |
1105 | && (sec->flags & SEC_ALLOC) != 0 |
1106 | && (sec->flags & SEC_LOAD) == 0 | |
1107 | && sym->st_size > 0 | |
45d6a902 AM |
1108 | && ELF_ST_TYPE (sym->st_info) != STT_FUNC) |
1109 | newdyncommon = TRUE; | |
1110 | else | |
1111 | newdyncommon = FALSE; | |
1112 | ||
1113 | if (olddyn | |
1114 | && olddef | |
1115 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1116 | && h->def_dynamic |
45d6a902 AM |
1117 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1118 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1119 | && h->size > 0 | |
1120 | && h->type != STT_FUNC) | |
1121 | olddyncommon = TRUE; | |
1122 | else | |
1123 | olddyncommon = FALSE; | |
1124 | ||
45d6a902 AM |
1125 | /* If both the old and the new symbols look like common symbols in a |
1126 | dynamic object, set the size of the symbol to the larger of the | |
1127 | two. */ | |
1128 | ||
1129 | if (olddyncommon | |
1130 | && newdyncommon | |
1131 | && sym->st_size != h->size) | |
1132 | { | |
1133 | /* Since we think we have two common symbols, issue a multiple | |
1134 | common warning if desired. Note that we only warn if the | |
1135 | size is different. If the size is the same, we simply let | |
1136 | the old symbol override the new one as normally happens with | |
1137 | symbols defined in dynamic objects. */ | |
1138 | ||
1139 | if (! ((*info->callbacks->multiple_common) | |
1140 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1141 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1142 | return FALSE; | |
252b5132 | 1143 | |
45d6a902 AM |
1144 | if (sym->st_size > h->size) |
1145 | h->size = sym->st_size; | |
252b5132 | 1146 | |
45d6a902 | 1147 | *size_change_ok = TRUE; |
252b5132 RH |
1148 | } |
1149 | ||
45d6a902 AM |
1150 | /* If we are looking at a dynamic object, and we have found a |
1151 | definition, we need to see if the symbol was already defined by | |
1152 | some other object. If so, we want to use the existing | |
1153 | definition, and we do not want to report a multiple symbol | |
1154 | definition error; we do this by clobbering *PSEC to be | |
1155 | bfd_und_section_ptr. | |
1156 | ||
1157 | We treat a common symbol as a definition if the symbol in the | |
1158 | shared library is a function, since common symbols always | |
1159 | represent variables; this can cause confusion in principle, but | |
1160 | any such confusion would seem to indicate an erroneous program or | |
1161 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1162 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1163 | |
1164 | if (newdyn | |
1165 | && newdef | |
77cfaee6 | 1166 | && (olddef |
45d6a902 | 1167 | || (h->root.type == bfd_link_hash_common |
79349b09 | 1168 | && (newweak |
0f8a2703 | 1169 | || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))) |
45d6a902 AM |
1170 | { |
1171 | *override = TRUE; | |
1172 | newdef = FALSE; | |
1173 | newdyncommon = FALSE; | |
252b5132 | 1174 | |
45d6a902 AM |
1175 | *psec = sec = bfd_und_section_ptr; |
1176 | *size_change_ok = TRUE; | |
252b5132 | 1177 | |
45d6a902 AM |
1178 | /* If we get here when the old symbol is a common symbol, then |
1179 | we are explicitly letting it override a weak symbol or | |
1180 | function in a dynamic object, and we don't want to warn about | |
1181 | a type change. If the old symbol is a defined symbol, a type | |
1182 | change warning may still be appropriate. */ | |
252b5132 | 1183 | |
45d6a902 AM |
1184 | if (h->root.type == bfd_link_hash_common) |
1185 | *type_change_ok = TRUE; | |
1186 | } | |
1187 | ||
1188 | /* Handle the special case of an old common symbol merging with a | |
1189 | new symbol which looks like a common symbol in a shared object. | |
1190 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1191 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1192 | right thing. */ | |
45d6a902 AM |
1193 | |
1194 | if (newdyncommon | |
1195 | && h->root.type == bfd_link_hash_common) | |
1196 | { | |
1197 | *override = TRUE; | |
1198 | newdef = FALSE; | |
1199 | newdyncommon = FALSE; | |
1200 | *pvalue = sym->st_size; | |
1201 | *psec = sec = bfd_com_section_ptr; | |
1202 | *size_change_ok = TRUE; | |
1203 | } | |
1204 | ||
c5e2cead L |
1205 | /* Skip weak definitions of symbols that are already defined. */ |
1206 | if (newdef && olddef && newweak && !oldweak) | |
1207 | *skip = TRUE; | |
1208 | ||
45d6a902 AM |
1209 | /* If the old symbol is from a dynamic object, and the new symbol is |
1210 | a definition which is not from a dynamic object, then the new | |
1211 | symbol overrides the old symbol. Symbols from regular files | |
1212 | always take precedence over symbols from dynamic objects, even if | |
1213 | they are defined after the dynamic object in the link. | |
1214 | ||
1215 | As above, we again permit a common symbol in a regular object to | |
1216 | override a definition in a shared object if the shared object | |
0f8a2703 | 1217 | symbol is a function or is weak. */ |
45d6a902 AM |
1218 | |
1219 | flip = NULL; | |
77cfaee6 | 1220 | if (!newdyn |
45d6a902 AM |
1221 | && (newdef |
1222 | || (bfd_is_com_section (sec) | |
79349b09 AM |
1223 | && (oldweak |
1224 | || h->type == STT_FUNC))) | |
45d6a902 AM |
1225 | && olddyn |
1226 | && olddef | |
f5385ebf | 1227 | && h->def_dynamic) |
45d6a902 AM |
1228 | { |
1229 | /* Change the hash table entry to undefined, and let | |
1230 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1231 | new definition. */ | |
1232 | ||
1233 | h->root.type = bfd_link_hash_undefined; | |
1234 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1235 | *size_change_ok = TRUE; | |
1236 | ||
1237 | olddef = FALSE; | |
1238 | olddyncommon = FALSE; | |
1239 | ||
1240 | /* We again permit a type change when a common symbol may be | |
1241 | overriding a function. */ | |
1242 | ||
1243 | if (bfd_is_com_section (sec)) | |
1244 | *type_change_ok = TRUE; | |
1245 | ||
1246 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1247 | flip = *sym_hash; | |
1248 | else | |
1249 | /* This union may have been set to be non-NULL when this symbol | |
1250 | was seen in a dynamic object. We must force the union to be | |
1251 | NULL, so that it is correct for a regular symbol. */ | |
1252 | h->verinfo.vertree = NULL; | |
1253 | } | |
1254 | ||
1255 | /* Handle the special case of a new common symbol merging with an | |
1256 | old symbol that looks like it might be a common symbol defined in | |
1257 | a shared object. Note that we have already handled the case in | |
1258 | which a new common symbol should simply override the definition | |
1259 | in the shared library. */ | |
1260 | ||
1261 | if (! newdyn | |
1262 | && bfd_is_com_section (sec) | |
1263 | && olddyncommon) | |
1264 | { | |
1265 | /* It would be best if we could set the hash table entry to a | |
1266 | common symbol, but we don't know what to use for the section | |
1267 | or the alignment. */ | |
1268 | if (! ((*info->callbacks->multiple_common) | |
1269 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1270 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1271 | return FALSE; | |
1272 | ||
4cc11e76 | 1273 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1274 | larger, pretend that the new symbol has its size. */ |
1275 | ||
1276 | if (h->size > *pvalue) | |
1277 | *pvalue = h->size; | |
1278 | ||
af44c138 L |
1279 | /* We need to remember the alignment required by the symbol |
1280 | in the dynamic object. */ | |
1281 | BFD_ASSERT (pold_alignment); | |
1282 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1283 | |
1284 | olddef = FALSE; | |
1285 | olddyncommon = FALSE; | |
1286 | ||
1287 | h->root.type = bfd_link_hash_undefined; | |
1288 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1289 | ||
1290 | *size_change_ok = TRUE; | |
1291 | *type_change_ok = TRUE; | |
1292 | ||
1293 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1294 | flip = *sym_hash; | |
1295 | else | |
1296 | h->verinfo.vertree = NULL; | |
1297 | } | |
1298 | ||
1299 | if (flip != NULL) | |
1300 | { | |
1301 | /* Handle the case where we had a versioned symbol in a dynamic | |
1302 | library and now find a definition in a normal object. In this | |
1303 | case, we make the versioned symbol point to the normal one. */ | |
9c5bfbb7 | 1304 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1305 | flip->root.type = h->root.type; |
1306 | h->root.type = bfd_link_hash_indirect; | |
1307 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
1308 | (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h); | |
1309 | flip->root.u.undef.abfd = h->root.u.undef.abfd; | |
f5385ebf | 1310 | if (h->def_dynamic) |
45d6a902 | 1311 | { |
f5385ebf AM |
1312 | h->def_dynamic = 0; |
1313 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1314 | } |
1315 | } | |
1316 | ||
45d6a902 AM |
1317 | return TRUE; |
1318 | } | |
1319 | ||
1320 | /* This function is called to create an indirect symbol from the | |
1321 | default for the symbol with the default version if needed. The | |
1322 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1323 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 AM |
1324 | |
1325 | bfd_boolean | |
268b6b39 AM |
1326 | _bfd_elf_add_default_symbol (bfd *abfd, |
1327 | struct bfd_link_info *info, | |
1328 | struct elf_link_hash_entry *h, | |
1329 | const char *name, | |
1330 | Elf_Internal_Sym *sym, | |
1331 | asection **psec, | |
1332 | bfd_vma *value, | |
1333 | bfd_boolean *dynsym, | |
0f8a2703 | 1334 | bfd_boolean override) |
45d6a902 AM |
1335 | { |
1336 | bfd_boolean type_change_ok; | |
1337 | bfd_boolean size_change_ok; | |
1338 | bfd_boolean skip; | |
1339 | char *shortname; | |
1340 | struct elf_link_hash_entry *hi; | |
1341 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1342 | const struct elf_backend_data *bed; |
45d6a902 AM |
1343 | bfd_boolean collect; |
1344 | bfd_boolean dynamic; | |
1345 | char *p; | |
1346 | size_t len, shortlen; | |
1347 | asection *sec; | |
1348 | ||
1349 | /* If this symbol has a version, and it is the default version, we | |
1350 | create an indirect symbol from the default name to the fully | |
1351 | decorated name. This will cause external references which do not | |
1352 | specify a version to be bound to this version of the symbol. */ | |
1353 | p = strchr (name, ELF_VER_CHR); | |
1354 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1355 | return TRUE; | |
1356 | ||
1357 | if (override) | |
1358 | { | |
4cc11e76 | 1359 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1360 | need to create the indirect symbol from the default name. */ |
1361 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1362 | FALSE, FALSE); | |
1363 | BFD_ASSERT (hi != NULL); | |
1364 | if (hi == h) | |
1365 | return TRUE; | |
1366 | while (hi->root.type == bfd_link_hash_indirect | |
1367 | || hi->root.type == bfd_link_hash_warning) | |
1368 | { | |
1369 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1370 | if (hi == h) | |
1371 | return TRUE; | |
1372 | } | |
1373 | } | |
1374 | ||
1375 | bed = get_elf_backend_data (abfd); | |
1376 | collect = bed->collect; | |
1377 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1378 | ||
1379 | shortlen = p - name; | |
1380 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1381 | if (shortname == NULL) | |
1382 | return FALSE; | |
1383 | memcpy (shortname, name, shortlen); | |
1384 | shortname[shortlen] = '\0'; | |
1385 | ||
1386 | /* We are going to create a new symbol. Merge it with any existing | |
1387 | symbol with this name. For the purposes of the merge, act as | |
1388 | though we were defining the symbol we just defined, although we | |
1389 | actually going to define an indirect symbol. */ | |
1390 | type_change_ok = FALSE; | |
1391 | size_change_ok = FALSE; | |
1392 | sec = *psec; | |
1393 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1394 | NULL, &hi, &skip, &override, |
1395 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1396 | return FALSE; |
1397 | ||
1398 | if (skip) | |
1399 | goto nondefault; | |
1400 | ||
1401 | if (! override) | |
1402 | { | |
1403 | bh = &hi->root; | |
1404 | if (! (_bfd_generic_link_add_one_symbol | |
1405 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1406 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1407 | return FALSE; |
1408 | hi = (struct elf_link_hash_entry *) bh; | |
1409 | } | |
1410 | else | |
1411 | { | |
1412 | /* In this case the symbol named SHORTNAME is overriding the | |
1413 | indirect symbol we want to add. We were planning on making | |
1414 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1415 | is the name without a version. NAME is the fully versioned | |
1416 | name, and it is the default version. | |
1417 | ||
1418 | Overriding means that we already saw a definition for the | |
1419 | symbol SHORTNAME in a regular object, and it is overriding | |
1420 | the symbol defined in the dynamic object. | |
1421 | ||
1422 | When this happens, we actually want to change NAME, the | |
1423 | symbol we just added, to refer to SHORTNAME. This will cause | |
1424 | references to NAME in the shared object to become references | |
1425 | to SHORTNAME in the regular object. This is what we expect | |
1426 | when we override a function in a shared object: that the | |
1427 | references in the shared object will be mapped to the | |
1428 | definition in the regular object. */ | |
1429 | ||
1430 | while (hi->root.type == bfd_link_hash_indirect | |
1431 | || hi->root.type == bfd_link_hash_warning) | |
1432 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1433 | ||
1434 | h->root.type = bfd_link_hash_indirect; | |
1435 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1436 | if (h->def_dynamic) |
45d6a902 | 1437 | { |
f5385ebf AM |
1438 | h->def_dynamic = 0; |
1439 | hi->ref_dynamic = 1; | |
1440 | if (hi->ref_regular | |
1441 | || hi->def_regular) | |
45d6a902 | 1442 | { |
c152c796 | 1443 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1444 | return FALSE; |
1445 | } | |
1446 | } | |
1447 | ||
1448 | /* Now set HI to H, so that the following code will set the | |
1449 | other fields correctly. */ | |
1450 | hi = h; | |
1451 | } | |
1452 | ||
1453 | /* If there is a duplicate definition somewhere, then HI may not | |
1454 | point to an indirect symbol. We will have reported an error to | |
1455 | the user in that case. */ | |
1456 | ||
1457 | if (hi->root.type == bfd_link_hash_indirect) | |
1458 | { | |
1459 | struct elf_link_hash_entry *ht; | |
1460 | ||
45d6a902 AM |
1461 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
1462 | (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi); | |
1463 | ||
1464 | /* See if the new flags lead us to realize that the symbol must | |
1465 | be dynamic. */ | |
1466 | if (! *dynsym) | |
1467 | { | |
1468 | if (! dynamic) | |
1469 | { | |
1470 | if (info->shared | |
f5385ebf | 1471 | || hi->ref_dynamic) |
45d6a902 AM |
1472 | *dynsym = TRUE; |
1473 | } | |
1474 | else | |
1475 | { | |
f5385ebf | 1476 | if (hi->ref_regular) |
45d6a902 AM |
1477 | *dynsym = TRUE; |
1478 | } | |
1479 | } | |
1480 | } | |
1481 | ||
1482 | /* We also need to define an indirection from the nondefault version | |
1483 | of the symbol. */ | |
1484 | ||
1485 | nondefault: | |
1486 | len = strlen (name); | |
1487 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1488 | if (shortname == NULL) | |
1489 | return FALSE; | |
1490 | memcpy (shortname, name, shortlen); | |
1491 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1492 | ||
1493 | /* Once again, merge with any existing symbol. */ | |
1494 | type_change_ok = FALSE; | |
1495 | size_change_ok = FALSE; | |
1496 | sec = *psec; | |
1497 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1498 | NULL, &hi, &skip, &override, |
1499 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1500 | return FALSE; |
1501 | ||
1502 | if (skip) | |
1503 | return TRUE; | |
1504 | ||
1505 | if (override) | |
1506 | { | |
1507 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1508 | the type of override we do in the case above unless it is | |
4cc11e76 | 1509 | overridden by a versioned definition. */ |
45d6a902 AM |
1510 | if (hi->root.type != bfd_link_hash_defined |
1511 | && hi->root.type != bfd_link_hash_defweak) | |
1512 | (*_bfd_error_handler) | |
d003868e AM |
1513 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1514 | abfd, shortname); | |
45d6a902 AM |
1515 | } |
1516 | else | |
1517 | { | |
1518 | bh = &hi->root; | |
1519 | if (! (_bfd_generic_link_add_one_symbol | |
1520 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1521 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1522 | return FALSE; |
1523 | hi = (struct elf_link_hash_entry *) bh; | |
1524 | ||
1525 | /* If there is a duplicate definition somewhere, then HI may not | |
1526 | point to an indirect symbol. We will have reported an error | |
1527 | to the user in that case. */ | |
1528 | ||
1529 | if (hi->root.type == bfd_link_hash_indirect) | |
1530 | { | |
45d6a902 AM |
1531 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); |
1532 | ||
1533 | /* See if the new flags lead us to realize that the symbol | |
1534 | must be dynamic. */ | |
1535 | if (! *dynsym) | |
1536 | { | |
1537 | if (! dynamic) | |
1538 | { | |
1539 | if (info->shared | |
f5385ebf | 1540 | || hi->ref_dynamic) |
45d6a902 AM |
1541 | *dynsym = TRUE; |
1542 | } | |
1543 | else | |
1544 | { | |
f5385ebf | 1545 | if (hi->ref_regular) |
45d6a902 AM |
1546 | *dynsym = TRUE; |
1547 | } | |
1548 | } | |
1549 | } | |
1550 | } | |
1551 | ||
1552 | return TRUE; | |
1553 | } | |
1554 | \f | |
1555 | /* This routine is used to export all defined symbols into the dynamic | |
1556 | symbol table. It is called via elf_link_hash_traverse. */ | |
1557 | ||
1558 | bfd_boolean | |
268b6b39 | 1559 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1560 | { |
268b6b39 | 1561 | struct elf_info_failed *eif = data; |
45d6a902 AM |
1562 | |
1563 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
1564 | if (h->root.type == bfd_link_hash_indirect) | |
1565 | return TRUE; | |
1566 | ||
1567 | if (h->root.type == bfd_link_hash_warning) | |
1568 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1569 | ||
1570 | if (h->dynindx == -1 | |
f5385ebf AM |
1571 | && (h->def_regular |
1572 | || h->ref_regular)) | |
45d6a902 AM |
1573 | { |
1574 | struct bfd_elf_version_tree *t; | |
1575 | struct bfd_elf_version_expr *d; | |
1576 | ||
1577 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1578 | { | |
108ba305 | 1579 | if (t->globals.list != NULL) |
45d6a902 | 1580 | { |
108ba305 JJ |
1581 | d = (*t->match) (&t->globals, NULL, h->root.root.string); |
1582 | if (d != NULL) | |
1583 | goto doit; | |
45d6a902 AM |
1584 | } |
1585 | ||
108ba305 | 1586 | if (t->locals.list != NULL) |
45d6a902 | 1587 | { |
108ba305 JJ |
1588 | d = (*t->match) (&t->locals, NULL, h->root.root.string); |
1589 | if (d != NULL) | |
1590 | return TRUE; | |
45d6a902 AM |
1591 | } |
1592 | } | |
1593 | ||
1594 | if (!eif->verdefs) | |
1595 | { | |
1596 | doit: | |
c152c796 | 1597 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
1598 | { |
1599 | eif->failed = TRUE; | |
1600 | return FALSE; | |
1601 | } | |
1602 | } | |
1603 | } | |
1604 | ||
1605 | return TRUE; | |
1606 | } | |
1607 | \f | |
1608 | /* Look through the symbols which are defined in other shared | |
1609 | libraries and referenced here. Update the list of version | |
1610 | dependencies. This will be put into the .gnu.version_r section. | |
1611 | This function is called via elf_link_hash_traverse. */ | |
1612 | ||
1613 | bfd_boolean | |
268b6b39 AM |
1614 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1615 | void *data) | |
45d6a902 | 1616 | { |
268b6b39 | 1617 | struct elf_find_verdep_info *rinfo = data; |
45d6a902 AM |
1618 | Elf_Internal_Verneed *t; |
1619 | Elf_Internal_Vernaux *a; | |
1620 | bfd_size_type amt; | |
1621 | ||
1622 | if (h->root.type == bfd_link_hash_warning) | |
1623 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1624 | ||
1625 | /* We only care about symbols defined in shared objects with version | |
1626 | information. */ | |
f5385ebf AM |
1627 | if (!h->def_dynamic |
1628 | || h->def_regular | |
45d6a902 AM |
1629 | || h->dynindx == -1 |
1630 | || h->verinfo.verdef == NULL) | |
1631 | return TRUE; | |
1632 | ||
1633 | /* See if we already know about this version. */ | |
1634 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1635 | { | |
1636 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1637 | continue; | |
1638 | ||
1639 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1640 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1641 | return TRUE; | |
1642 | ||
1643 | break; | |
1644 | } | |
1645 | ||
1646 | /* This is a new version. Add it to tree we are building. */ | |
1647 | ||
1648 | if (t == NULL) | |
1649 | { | |
1650 | amt = sizeof *t; | |
268b6b39 | 1651 | t = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1652 | if (t == NULL) |
1653 | { | |
1654 | rinfo->failed = TRUE; | |
1655 | return FALSE; | |
1656 | } | |
1657 | ||
1658 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1659 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1660 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1661 | } | |
1662 | ||
1663 | amt = sizeof *a; | |
268b6b39 | 1664 | a = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1665 | |
1666 | /* Note that we are copying a string pointer here, and testing it | |
1667 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1668 | discard the string data when low in memory, this will have to be | |
1669 | fixed. */ | |
1670 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1671 | ||
1672 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1673 | a->vna_nextptr = t->vn_auxptr; | |
1674 | ||
1675 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1676 | ++rinfo->vers; | |
1677 | ||
1678 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1679 | ||
1680 | t->vn_auxptr = a; | |
1681 | ||
1682 | return TRUE; | |
1683 | } | |
1684 | ||
1685 | /* Figure out appropriate versions for all the symbols. We may not | |
1686 | have the version number script until we have read all of the input | |
1687 | files, so until that point we don't know which symbols should be | |
1688 | local. This function is called via elf_link_hash_traverse. */ | |
1689 | ||
1690 | bfd_boolean | |
268b6b39 | 1691 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
1692 | { |
1693 | struct elf_assign_sym_version_info *sinfo; | |
1694 | struct bfd_link_info *info; | |
9c5bfbb7 | 1695 | const struct elf_backend_data *bed; |
45d6a902 AM |
1696 | struct elf_info_failed eif; |
1697 | char *p; | |
1698 | bfd_size_type amt; | |
1699 | ||
268b6b39 | 1700 | sinfo = data; |
45d6a902 AM |
1701 | info = sinfo->info; |
1702 | ||
1703 | if (h->root.type == bfd_link_hash_warning) | |
1704 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1705 | ||
1706 | /* Fix the symbol flags. */ | |
1707 | eif.failed = FALSE; | |
1708 | eif.info = info; | |
1709 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1710 | { | |
1711 | if (eif.failed) | |
1712 | sinfo->failed = TRUE; | |
1713 | return FALSE; | |
1714 | } | |
1715 | ||
1716 | /* We only need version numbers for symbols defined in regular | |
1717 | objects. */ | |
f5385ebf | 1718 | if (!h->def_regular) |
45d6a902 AM |
1719 | return TRUE; |
1720 | ||
1721 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1722 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1723 | if (p != NULL && h->verinfo.vertree == NULL) | |
1724 | { | |
1725 | struct bfd_elf_version_tree *t; | |
1726 | bfd_boolean hidden; | |
1727 | ||
1728 | hidden = TRUE; | |
1729 | ||
1730 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1731 | not a hidden symbol. */ | |
1732 | ++p; | |
1733 | if (*p == ELF_VER_CHR) | |
1734 | { | |
1735 | hidden = FALSE; | |
1736 | ++p; | |
1737 | } | |
1738 | ||
1739 | /* If there is no version string, we can just return out. */ | |
1740 | if (*p == '\0') | |
1741 | { | |
1742 | if (hidden) | |
f5385ebf | 1743 | h->hidden = 1; |
45d6a902 AM |
1744 | return TRUE; |
1745 | } | |
1746 | ||
1747 | /* Look for the version. If we find it, it is no longer weak. */ | |
1748 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1749 | { | |
1750 | if (strcmp (t->name, p) == 0) | |
1751 | { | |
1752 | size_t len; | |
1753 | char *alc; | |
1754 | struct bfd_elf_version_expr *d; | |
1755 | ||
1756 | len = p - h->root.root.string; | |
268b6b39 | 1757 | alc = bfd_malloc (len); |
45d6a902 AM |
1758 | if (alc == NULL) |
1759 | return FALSE; | |
1760 | memcpy (alc, h->root.root.string, len - 1); | |
1761 | alc[len - 1] = '\0'; | |
1762 | if (alc[len - 2] == ELF_VER_CHR) | |
1763 | alc[len - 2] = '\0'; | |
1764 | ||
1765 | h->verinfo.vertree = t; | |
1766 | t->used = TRUE; | |
1767 | d = NULL; | |
1768 | ||
108ba305 JJ |
1769 | if (t->globals.list != NULL) |
1770 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
1771 | |
1772 | /* See if there is anything to force this symbol to | |
1773 | local scope. */ | |
108ba305 | 1774 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 1775 | { |
108ba305 JJ |
1776 | d = (*t->match) (&t->locals, NULL, alc); |
1777 | if (d != NULL | |
1778 | && h->dynindx != -1 | |
108ba305 JJ |
1779 | && ! info->export_dynamic) |
1780 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
1781 | } |
1782 | ||
1783 | free (alc); | |
1784 | break; | |
1785 | } | |
1786 | } | |
1787 | ||
1788 | /* If we are building an application, we need to create a | |
1789 | version node for this version. */ | |
36af4a4e | 1790 | if (t == NULL && info->executable) |
45d6a902 AM |
1791 | { |
1792 | struct bfd_elf_version_tree **pp; | |
1793 | int version_index; | |
1794 | ||
1795 | /* If we aren't going to export this symbol, we don't need | |
1796 | to worry about it. */ | |
1797 | if (h->dynindx == -1) | |
1798 | return TRUE; | |
1799 | ||
1800 | amt = sizeof *t; | |
108ba305 | 1801 | t = bfd_zalloc (sinfo->output_bfd, amt); |
45d6a902 AM |
1802 | if (t == NULL) |
1803 | { | |
1804 | sinfo->failed = TRUE; | |
1805 | return FALSE; | |
1806 | } | |
1807 | ||
45d6a902 | 1808 | t->name = p; |
45d6a902 AM |
1809 | t->name_indx = (unsigned int) -1; |
1810 | t->used = TRUE; | |
1811 | ||
1812 | version_index = 1; | |
1813 | /* Don't count anonymous version tag. */ | |
1814 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1815 | version_index = 0; | |
1816 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1817 | ++version_index; | |
1818 | t->vernum = version_index; | |
1819 | ||
1820 | *pp = t; | |
1821 | ||
1822 | h->verinfo.vertree = t; | |
1823 | } | |
1824 | else if (t == NULL) | |
1825 | { | |
1826 | /* We could not find the version for a symbol when | |
1827 | generating a shared archive. Return an error. */ | |
1828 | (*_bfd_error_handler) | |
d003868e AM |
1829 | (_("%B: undefined versioned symbol name %s"), |
1830 | sinfo->output_bfd, h->root.root.string); | |
45d6a902 AM |
1831 | bfd_set_error (bfd_error_bad_value); |
1832 | sinfo->failed = TRUE; | |
1833 | return FALSE; | |
1834 | } | |
1835 | ||
1836 | if (hidden) | |
f5385ebf | 1837 | h->hidden = 1; |
45d6a902 AM |
1838 | } |
1839 | ||
1840 | /* If we don't have a version for this symbol, see if we can find | |
1841 | something. */ | |
1842 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
1843 | { | |
1844 | struct bfd_elf_version_tree *t; | |
1845 | struct bfd_elf_version_tree *local_ver; | |
1846 | struct bfd_elf_version_expr *d; | |
1847 | ||
1848 | /* See if can find what version this symbol is in. If the | |
1849 | symbol is supposed to be local, then don't actually register | |
1850 | it. */ | |
1851 | local_ver = NULL; | |
1852 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1853 | { | |
108ba305 | 1854 | if (t->globals.list != NULL) |
45d6a902 AM |
1855 | { |
1856 | bfd_boolean matched; | |
1857 | ||
1858 | matched = FALSE; | |
108ba305 JJ |
1859 | d = NULL; |
1860 | while ((d = (*t->match) (&t->globals, d, | |
1861 | h->root.root.string)) != NULL) | |
1862 | if (d->symver) | |
1863 | matched = TRUE; | |
1864 | else | |
1865 | { | |
1866 | /* There is a version without definition. Make | |
1867 | the symbol the default definition for this | |
1868 | version. */ | |
1869 | h->verinfo.vertree = t; | |
1870 | local_ver = NULL; | |
1871 | d->script = 1; | |
1872 | break; | |
1873 | } | |
45d6a902 AM |
1874 | if (d != NULL) |
1875 | break; | |
1876 | else if (matched) | |
1877 | /* There is no undefined version for this symbol. Hide the | |
1878 | default one. */ | |
1879 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1880 | } | |
1881 | ||
108ba305 | 1882 | if (t->locals.list != NULL) |
45d6a902 | 1883 | { |
108ba305 JJ |
1884 | d = NULL; |
1885 | while ((d = (*t->match) (&t->locals, d, | |
1886 | h->root.root.string)) != NULL) | |
45d6a902 | 1887 | { |
108ba305 | 1888 | local_ver = t; |
45d6a902 | 1889 | /* If the match is "*", keep looking for a more |
108ba305 JJ |
1890 | explicit, perhaps even global, match. |
1891 | XXX: Shouldn't this be !d->wildcard instead? */ | |
1892 | if (d->pattern[0] != '*' || d->pattern[1] != '\0') | |
1893 | break; | |
45d6a902 AM |
1894 | } |
1895 | ||
1896 | if (d != NULL) | |
1897 | break; | |
1898 | } | |
1899 | } | |
1900 | ||
1901 | if (local_ver != NULL) | |
1902 | { | |
1903 | h->verinfo.vertree = local_ver; | |
1904 | if (h->dynindx != -1 | |
45d6a902 AM |
1905 | && ! info->export_dynamic) |
1906 | { | |
1907 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1908 | } | |
1909 | } | |
1910 | } | |
1911 | ||
1912 | return TRUE; | |
1913 | } | |
1914 | \f | |
45d6a902 AM |
1915 | /* Read and swap the relocs from the section indicated by SHDR. This |
1916 | may be either a REL or a RELA section. The relocations are | |
1917 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
1918 | which should have already been allocated to contain enough space. | |
1919 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
1920 | relocations should be stored. | |
1921 | ||
1922 | Returns FALSE if something goes wrong. */ | |
1923 | ||
1924 | static bfd_boolean | |
268b6b39 | 1925 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 1926 | asection *sec, |
268b6b39 AM |
1927 | Elf_Internal_Shdr *shdr, |
1928 | void *external_relocs, | |
1929 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 1930 | { |
9c5bfbb7 | 1931 | const struct elf_backend_data *bed; |
268b6b39 | 1932 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
1933 | const bfd_byte *erela; |
1934 | const bfd_byte *erelaend; | |
1935 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
1936 | Elf_Internal_Shdr *symtab_hdr; |
1937 | size_t nsyms; | |
45d6a902 | 1938 | |
45d6a902 AM |
1939 | /* Position ourselves at the start of the section. */ |
1940 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
1941 | return FALSE; | |
1942 | ||
1943 | /* Read the relocations. */ | |
1944 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
1945 | return FALSE; | |
1946 | ||
243ef1e0 L |
1947 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1948 | nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
1949 | ||
45d6a902 AM |
1950 | bed = get_elf_backend_data (abfd); |
1951 | ||
1952 | /* Convert the external relocations to the internal format. */ | |
1953 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
1954 | swap_in = bed->s->swap_reloc_in; | |
1955 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
1956 | swap_in = bed->s->swap_reloca_in; | |
1957 | else | |
1958 | { | |
1959 | bfd_set_error (bfd_error_wrong_format); | |
1960 | return FALSE; | |
1961 | } | |
1962 | ||
1963 | erela = external_relocs; | |
51992aec | 1964 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
1965 | irela = internal_relocs; |
1966 | while (erela < erelaend) | |
1967 | { | |
243ef1e0 L |
1968 | bfd_vma r_symndx; |
1969 | ||
45d6a902 | 1970 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
1971 | r_symndx = ELF32_R_SYM (irela->r_info); |
1972 | if (bed->s->arch_size == 64) | |
1973 | r_symndx >>= 24; | |
1974 | if ((size_t) r_symndx >= nsyms) | |
1975 | { | |
1976 | (*_bfd_error_handler) | |
d003868e AM |
1977 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" |
1978 | " for offset 0x%lx in section `%A'"), | |
1979 | abfd, sec, | |
1980 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
1981 | bfd_set_error (bfd_error_bad_value); |
1982 | return FALSE; | |
1983 | } | |
45d6a902 AM |
1984 | irela += bed->s->int_rels_per_ext_rel; |
1985 | erela += shdr->sh_entsize; | |
1986 | } | |
1987 | ||
1988 | return TRUE; | |
1989 | } | |
1990 | ||
1991 | /* Read and swap the relocs for a section O. They may have been | |
1992 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
1993 | not NULL, they are used as buffers to read into. They are known to | |
1994 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
1995 | the return value is allocated using either malloc or bfd_alloc, | |
1996 | according to the KEEP_MEMORY argument. If O has two relocation | |
1997 | sections (both REL and RELA relocations), then the REL_HDR | |
1998 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
1999 | REL_HDR2 relocations. */ | |
2000 | ||
2001 | Elf_Internal_Rela * | |
268b6b39 AM |
2002 | _bfd_elf_link_read_relocs (bfd *abfd, |
2003 | asection *o, | |
2004 | void *external_relocs, | |
2005 | Elf_Internal_Rela *internal_relocs, | |
2006 | bfd_boolean keep_memory) | |
45d6a902 AM |
2007 | { |
2008 | Elf_Internal_Shdr *rel_hdr; | |
268b6b39 | 2009 | void *alloc1 = NULL; |
45d6a902 | 2010 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2011 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
2012 | |
2013 | if (elf_section_data (o)->relocs != NULL) | |
2014 | return elf_section_data (o)->relocs; | |
2015 | ||
2016 | if (o->reloc_count == 0) | |
2017 | return NULL; | |
2018 | ||
2019 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
2020 | ||
2021 | if (internal_relocs == NULL) | |
2022 | { | |
2023 | bfd_size_type size; | |
2024 | ||
2025 | size = o->reloc_count; | |
2026 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
2027 | if (keep_memory) | |
268b6b39 | 2028 | internal_relocs = bfd_alloc (abfd, size); |
45d6a902 | 2029 | else |
268b6b39 | 2030 | internal_relocs = alloc2 = bfd_malloc (size); |
45d6a902 AM |
2031 | if (internal_relocs == NULL) |
2032 | goto error_return; | |
2033 | } | |
2034 | ||
2035 | if (external_relocs == NULL) | |
2036 | { | |
2037 | bfd_size_type size = rel_hdr->sh_size; | |
2038 | ||
2039 | if (elf_section_data (o)->rel_hdr2) | |
2040 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
268b6b39 | 2041 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2042 | if (alloc1 == NULL) |
2043 | goto error_return; | |
2044 | external_relocs = alloc1; | |
2045 | } | |
2046 | ||
243ef1e0 | 2047 | if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, |
45d6a902 AM |
2048 | external_relocs, |
2049 | internal_relocs)) | |
2050 | goto error_return; | |
51992aec AM |
2051 | if (elf_section_data (o)->rel_hdr2 |
2052 | && (!elf_link_read_relocs_from_section | |
2053 | (abfd, o, | |
2054 | elf_section_data (o)->rel_hdr2, | |
2055 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
2056 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
2057 | * bed->s->int_rels_per_ext_rel)))) | |
45d6a902 AM |
2058 | goto error_return; |
2059 | ||
2060 | /* Cache the results for next time, if we can. */ | |
2061 | if (keep_memory) | |
2062 | elf_section_data (o)->relocs = internal_relocs; | |
2063 | ||
2064 | if (alloc1 != NULL) | |
2065 | free (alloc1); | |
2066 | ||
2067 | /* Don't free alloc2, since if it was allocated we are passing it | |
2068 | back (under the name of internal_relocs). */ | |
2069 | ||
2070 | return internal_relocs; | |
2071 | ||
2072 | error_return: | |
2073 | if (alloc1 != NULL) | |
2074 | free (alloc1); | |
2075 | if (alloc2 != NULL) | |
2076 | free (alloc2); | |
2077 | return NULL; | |
2078 | } | |
2079 | ||
2080 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2081 | section header for a section containing relocations for O. */ | |
2082 | ||
2083 | bfd_boolean | |
268b6b39 AM |
2084 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2085 | Elf_Internal_Shdr *rel_hdr, | |
2086 | asection *o) | |
45d6a902 AM |
2087 | { |
2088 | bfd_size_type reloc_count; | |
2089 | bfd_size_type num_rel_hashes; | |
2090 | ||
2091 | /* Figure out how many relocations there will be. */ | |
2092 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
2093 | reloc_count = elf_section_data (o)->rel_count; | |
2094 | else | |
2095 | reloc_count = elf_section_data (o)->rel_count2; | |
2096 | ||
2097 | num_rel_hashes = o->reloc_count; | |
2098 | if (num_rel_hashes < reloc_count) | |
2099 | num_rel_hashes = reloc_count; | |
2100 | ||
2101 | /* That allows us to calculate the size of the section. */ | |
2102 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
2103 | ||
2104 | /* The contents field must last into write_object_contents, so we | |
2105 | allocate it with bfd_alloc rather than malloc. Also since we | |
2106 | cannot be sure that the contents will actually be filled in, | |
2107 | we zero the allocated space. */ | |
268b6b39 | 2108 | rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2109 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2110 | return FALSE; | |
2111 | ||
2112 | /* We only allocate one set of hash entries, so we only do it the | |
2113 | first time we are called. */ | |
2114 | if (elf_section_data (o)->rel_hashes == NULL | |
2115 | && num_rel_hashes) | |
2116 | { | |
2117 | struct elf_link_hash_entry **p; | |
2118 | ||
268b6b39 | 2119 | p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
2120 | if (p == NULL) |
2121 | return FALSE; | |
2122 | ||
2123 | elf_section_data (o)->rel_hashes = p; | |
2124 | } | |
2125 | ||
2126 | return TRUE; | |
2127 | } | |
2128 | ||
2129 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2130 | originated from the section given by INPUT_REL_HDR) to the | |
2131 | OUTPUT_BFD. */ | |
2132 | ||
2133 | bfd_boolean | |
268b6b39 AM |
2134 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2135 | asection *input_section, | |
2136 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2137 | Elf_Internal_Rela *internal_relocs, |
2138 | struct elf_link_hash_entry **rel_hash | |
2139 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2140 | { |
2141 | Elf_Internal_Rela *irela; | |
2142 | Elf_Internal_Rela *irelaend; | |
2143 | bfd_byte *erel; | |
2144 | Elf_Internal_Shdr *output_rel_hdr; | |
2145 | asection *output_section; | |
2146 | unsigned int *rel_countp = NULL; | |
9c5bfbb7 | 2147 | const struct elf_backend_data *bed; |
268b6b39 | 2148 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
45d6a902 AM |
2149 | |
2150 | output_section = input_section->output_section; | |
2151 | output_rel_hdr = NULL; | |
2152 | ||
2153 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
2154 | == input_rel_hdr->sh_entsize) | |
2155 | { | |
2156 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
2157 | rel_countp = &elf_section_data (output_section)->rel_count; | |
2158 | } | |
2159 | else if (elf_section_data (output_section)->rel_hdr2 | |
2160 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
2161 | == input_rel_hdr->sh_entsize)) | |
2162 | { | |
2163 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2164 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2165 | } | |
2166 | else | |
2167 | { | |
2168 | (*_bfd_error_handler) | |
d003868e AM |
2169 | (_("%B: relocation size mismatch in %B section %A"), |
2170 | output_bfd, input_section->owner, input_section); | |
45d6a902 AM |
2171 | bfd_set_error (bfd_error_wrong_object_format); |
2172 | return FALSE; | |
2173 | } | |
2174 | ||
2175 | bed = get_elf_backend_data (output_bfd); | |
2176 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2177 | swap_out = bed->s->swap_reloc_out; | |
2178 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2179 | swap_out = bed->s->swap_reloca_out; | |
2180 | else | |
2181 | abort (); | |
2182 | ||
2183 | erel = output_rel_hdr->contents; | |
2184 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2185 | irela = internal_relocs; | |
2186 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2187 | * bed->s->int_rels_per_ext_rel); | |
2188 | while (irela < irelaend) | |
2189 | { | |
2190 | (*swap_out) (output_bfd, irela, erel); | |
2191 | irela += bed->s->int_rels_per_ext_rel; | |
2192 | erel += input_rel_hdr->sh_entsize; | |
2193 | } | |
2194 | ||
2195 | /* Bump the counter, so that we know where to add the next set of | |
2196 | relocations. */ | |
2197 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2198 | ||
2199 | return TRUE; | |
2200 | } | |
2201 | \f | |
2202 | /* Fix up the flags for a symbol. This handles various cases which | |
2203 | can only be fixed after all the input files are seen. This is | |
2204 | currently called by both adjust_dynamic_symbol and | |
2205 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2206 | the face of future changes. */ | |
2207 | ||
2208 | bfd_boolean | |
268b6b39 AM |
2209 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2210 | struct elf_info_failed *eif) | |
45d6a902 AM |
2211 | { |
2212 | /* If this symbol was mentioned in a non-ELF file, try to set | |
2213 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2214 | permit a non-ELF file to correctly refer to a symbol defined in | |
2215 | an ELF dynamic object. */ | |
f5385ebf | 2216 | if (h->non_elf) |
45d6a902 AM |
2217 | { |
2218 | while (h->root.type == bfd_link_hash_indirect) | |
2219 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2220 | ||
2221 | if (h->root.type != bfd_link_hash_defined | |
2222 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2223 | { |
2224 | h->ref_regular = 1; | |
2225 | h->ref_regular_nonweak = 1; | |
2226 | } | |
45d6a902 AM |
2227 | else |
2228 | { | |
2229 | if (h->root.u.def.section->owner != NULL | |
2230 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2231 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2232 | { |
2233 | h->ref_regular = 1; | |
2234 | h->ref_regular_nonweak = 1; | |
2235 | } | |
45d6a902 | 2236 | else |
f5385ebf | 2237 | h->def_regular = 1; |
45d6a902 AM |
2238 | } |
2239 | ||
2240 | if (h->dynindx == -1 | |
f5385ebf AM |
2241 | && (h->def_dynamic |
2242 | || h->ref_dynamic)) | |
45d6a902 | 2243 | { |
c152c796 | 2244 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2245 | { |
2246 | eif->failed = TRUE; | |
2247 | return FALSE; | |
2248 | } | |
2249 | } | |
2250 | } | |
2251 | else | |
2252 | { | |
f5385ebf | 2253 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2254 | was first seen in a non-ELF file. Fortunately, if the symbol |
2255 | was first seen in an ELF file, we're probably OK unless the | |
2256 | symbol was defined in a non-ELF file. Catch that case here. | |
2257 | FIXME: We're still in trouble if the symbol was first seen in | |
2258 | a dynamic object, and then later in a non-ELF regular object. */ | |
2259 | if ((h->root.type == bfd_link_hash_defined | |
2260 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2261 | && !h->def_regular |
45d6a902 AM |
2262 | && (h->root.u.def.section->owner != NULL |
2263 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2264 | != bfd_target_elf_flavour) | |
2265 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2266 | && !h->def_dynamic))) |
2267 | h->def_regular = 1; | |
45d6a902 AM |
2268 | } |
2269 | ||
2270 | /* If this is a final link, and the symbol was defined as a common | |
2271 | symbol in a regular object file, and there was no definition in | |
2272 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2273 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2274 | flag will not have been set. */ |
2275 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2276 | && !h->def_regular |
2277 | && h->ref_regular | |
2278 | && !h->def_dynamic | |
45d6a902 | 2279 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) |
f5385ebf | 2280 | h->def_regular = 1; |
45d6a902 AM |
2281 | |
2282 | /* If -Bsymbolic was used (which means to bind references to global | |
2283 | symbols to the definition within the shared object), and this | |
2284 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2285 | need a PLT entry. Likewise, if the symbol has non-default |
2286 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2287 | will force it local. */ |
f5385ebf | 2288 | if (h->needs_plt |
45d6a902 | 2289 | && eif->info->shared |
0eddce27 | 2290 | && is_elf_hash_table (eif->info->hash) |
45d6a902 | 2291 | && (eif->info->symbolic |
c1be741f | 2292 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2293 | && h->def_regular) |
45d6a902 | 2294 | { |
9c5bfbb7 | 2295 | const struct elf_backend_data *bed; |
45d6a902 AM |
2296 | bfd_boolean force_local; |
2297 | ||
2298 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2299 | ||
2300 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL | |
2301 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2302 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2303 | } | |
2304 | ||
2305 | /* If a weak undefined symbol has non-default visibility, we also | |
2306 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2307 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
2308 | && h->root.type == bfd_link_hash_undefweak) |
2309 | { | |
9c5bfbb7 | 2310 | const struct elf_backend_data *bed; |
45d6a902 AM |
2311 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2312 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2313 | } | |
2314 | ||
2315 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2316 | the real definition in the dynamic object, copy interesting flags | |
2317 | over to the real definition. */ | |
f6e332e6 | 2318 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2319 | { |
2320 | struct elf_link_hash_entry *weakdef; | |
2321 | ||
f6e332e6 | 2322 | weakdef = h->u.weakdef; |
45d6a902 AM |
2323 | if (h->root.type == bfd_link_hash_indirect) |
2324 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2325 | ||
2326 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2327 | || h->root.type == bfd_link_hash_defweak); | |
2328 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2329 | || weakdef->root.type == bfd_link_hash_defweak); | |
f5385ebf | 2330 | BFD_ASSERT (weakdef->def_dynamic); |
45d6a902 AM |
2331 | |
2332 | /* If the real definition is defined by a regular object file, | |
2333 | don't do anything special. See the longer description in | |
2334 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
f5385ebf | 2335 | if (weakdef->def_regular) |
f6e332e6 | 2336 | h->u.weakdef = NULL; |
45d6a902 AM |
2337 | else |
2338 | { | |
9c5bfbb7 | 2339 | const struct elf_backend_data *bed; |
45d6a902 AM |
2340 | |
2341 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2342 | (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h); | |
2343 | } | |
2344 | } | |
2345 | ||
2346 | return TRUE; | |
2347 | } | |
2348 | ||
2349 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2350 | called via elf_link_hash_traverse, and also calls itself | |
2351 | recursively. */ | |
2352 | ||
2353 | bfd_boolean | |
268b6b39 | 2354 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2355 | { |
268b6b39 | 2356 | struct elf_info_failed *eif = data; |
45d6a902 | 2357 | bfd *dynobj; |
9c5bfbb7 | 2358 | const struct elf_backend_data *bed; |
45d6a902 | 2359 | |
0eddce27 | 2360 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2361 | return FALSE; |
2362 | ||
2363 | if (h->root.type == bfd_link_hash_warning) | |
2364 | { | |
a6aa5195 AM |
2365 | h->got = elf_hash_table (eif->info)->init_got_offset; |
2366 | h->plt = elf_hash_table (eif->info)->init_plt_offset; | |
45d6a902 AM |
2367 | |
2368 | /* When warning symbols are created, they **replace** the "real" | |
2369 | entry in the hash table, thus we never get to see the real | |
2370 | symbol in a hash traversal. So look at it now. */ | |
2371 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2372 | } | |
2373 | ||
2374 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2375 | if (h->root.type == bfd_link_hash_indirect) | |
2376 | return TRUE; | |
2377 | ||
2378 | /* Fix the symbol flags. */ | |
2379 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2380 | return FALSE; | |
2381 | ||
2382 | /* If this symbol does not require a PLT entry, and it is not | |
2383 | defined by a dynamic object, or is not referenced by a regular | |
2384 | object, ignore it. We do have to handle a weak defined symbol, | |
2385 | even if no regular object refers to it, if we decided to add it | |
2386 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2387 | about symbols which are defined by one dynamic object and | |
2388 | referenced by another one? */ | |
f5385ebf AM |
2389 | if (!h->needs_plt |
2390 | && (h->def_regular | |
2391 | || !h->def_dynamic | |
2392 | || (!h->ref_regular | |
f6e332e6 | 2393 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 | 2394 | { |
a6aa5195 | 2395 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2396 | return TRUE; |
2397 | } | |
2398 | ||
2399 | /* If we've already adjusted this symbol, don't do it again. This | |
2400 | can happen via a recursive call. */ | |
f5385ebf | 2401 | if (h->dynamic_adjusted) |
45d6a902 AM |
2402 | return TRUE; |
2403 | ||
2404 | /* Don't look at this symbol again. Note that we must set this | |
2405 | after checking the above conditions, because we may look at a | |
2406 | symbol once, decide not to do anything, and then get called | |
2407 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2408 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2409 | |
2410 | /* If this is a weak definition, and we know a real definition, and | |
2411 | the real symbol is not itself defined by a regular object file, | |
2412 | then get a good value for the real definition. We handle the | |
2413 | real symbol first, for the convenience of the backend routine. | |
2414 | ||
2415 | Note that there is a confusing case here. If the real definition | |
2416 | is defined by a regular object file, we don't get the real symbol | |
2417 | from the dynamic object, but we do get the weak symbol. If the | |
2418 | processor backend uses a COPY reloc, then if some routine in the | |
2419 | dynamic object changes the real symbol, we will not see that | |
2420 | change in the corresponding weak symbol. This is the way other | |
2421 | ELF linkers work as well, and seems to be a result of the shared | |
2422 | library model. | |
2423 | ||
2424 | I will clarify this issue. Most SVR4 shared libraries define the | |
2425 | variable _timezone and define timezone as a weak synonym. The | |
2426 | tzset call changes _timezone. If you write | |
2427 | extern int timezone; | |
2428 | int _timezone = 5; | |
2429 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2430 | you might expect that, since timezone is a synonym for _timezone, | |
2431 | the same number will print both times. However, if the processor | |
2432 | backend uses a COPY reloc, then actually timezone will be copied | |
2433 | into your process image, and, since you define _timezone | |
2434 | yourself, _timezone will not. Thus timezone and _timezone will | |
2435 | wind up at different memory locations. The tzset call will set | |
2436 | _timezone, leaving timezone unchanged. */ | |
2437 | ||
f6e332e6 | 2438 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2439 | { |
2440 | /* If we get to this point, we know there is an implicit | |
2441 | reference by a regular object file via the weak symbol H. | |
2442 | FIXME: Is this really true? What if the traversal finds | |
f6e332e6 AM |
2443 | H->U.WEAKDEF before it finds H? */ |
2444 | h->u.weakdef->ref_regular = 1; | |
45d6a902 | 2445 | |
f6e332e6 | 2446 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2447 | return FALSE; |
2448 | } | |
2449 | ||
2450 | /* If a symbol has no type and no size and does not require a PLT | |
2451 | entry, then we are probably about to do the wrong thing here: we | |
2452 | are probably going to create a COPY reloc for an empty object. | |
2453 | This case can arise when a shared object is built with assembly | |
2454 | code, and the assembly code fails to set the symbol type. */ | |
2455 | if (h->size == 0 | |
2456 | && h->type == STT_NOTYPE | |
f5385ebf | 2457 | && !h->needs_plt) |
45d6a902 AM |
2458 | (*_bfd_error_handler) |
2459 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2460 | h->root.root.string); | |
2461 | ||
2462 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2463 | bed = get_elf_backend_data (dynobj); | |
2464 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2465 | { | |
2466 | eif->failed = TRUE; | |
2467 | return FALSE; | |
2468 | } | |
2469 | ||
2470 | return TRUE; | |
2471 | } | |
2472 | ||
2473 | /* Adjust all external symbols pointing into SEC_MERGE sections | |
2474 | to reflect the object merging within the sections. */ | |
2475 | ||
2476 | bfd_boolean | |
268b6b39 | 2477 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2478 | { |
2479 | asection *sec; | |
2480 | ||
2481 | if (h->root.type == bfd_link_hash_warning) | |
2482 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2483 | ||
2484 | if ((h->root.type == bfd_link_hash_defined | |
2485 | || h->root.type == bfd_link_hash_defweak) | |
2486 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2487 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2488 | { | |
268b6b39 | 2489 | bfd *output_bfd = data; |
45d6a902 AM |
2490 | |
2491 | h->root.u.def.value = | |
2492 | _bfd_merged_section_offset (output_bfd, | |
2493 | &h->root.u.def.section, | |
2494 | elf_section_data (sec)->sec_info, | |
753731ee | 2495 | h->root.u.def.value); |
45d6a902 AM |
2496 | } |
2497 | ||
2498 | return TRUE; | |
2499 | } | |
986a241f RH |
2500 | |
2501 | /* Returns false if the symbol referred to by H should be considered | |
2502 | to resolve local to the current module, and true if it should be | |
2503 | considered to bind dynamically. */ | |
2504 | ||
2505 | bfd_boolean | |
268b6b39 AM |
2506 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2507 | struct bfd_link_info *info, | |
2508 | bfd_boolean ignore_protected) | |
986a241f RH |
2509 | { |
2510 | bfd_boolean binding_stays_local_p; | |
2511 | ||
2512 | if (h == NULL) | |
2513 | return FALSE; | |
2514 | ||
2515 | while (h->root.type == bfd_link_hash_indirect | |
2516 | || h->root.type == bfd_link_hash_warning) | |
2517 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2518 | ||
2519 | /* If it was forced local, then clearly it's not dynamic. */ | |
2520 | if (h->dynindx == -1) | |
2521 | return FALSE; | |
f5385ebf | 2522 | if (h->forced_local) |
986a241f RH |
2523 | return FALSE; |
2524 | ||
2525 | /* Identify the cases where name binding rules say that a | |
2526 | visible symbol resolves locally. */ | |
2527 | binding_stays_local_p = info->executable || info->symbolic; | |
2528 | ||
2529 | switch (ELF_ST_VISIBILITY (h->other)) | |
2530 | { | |
2531 | case STV_INTERNAL: | |
2532 | case STV_HIDDEN: | |
2533 | return FALSE; | |
2534 | ||
2535 | case STV_PROTECTED: | |
2536 | /* Proper resolution for function pointer equality may require | |
2537 | that these symbols perhaps be resolved dynamically, even though | |
2538 | we should be resolving them to the current module. */ | |
1c16dfa5 | 2539 | if (!ignore_protected || h->type != STT_FUNC) |
986a241f RH |
2540 | binding_stays_local_p = TRUE; |
2541 | break; | |
2542 | ||
2543 | default: | |
986a241f RH |
2544 | break; |
2545 | } | |
2546 | ||
aa37626c | 2547 | /* If it isn't defined locally, then clearly it's dynamic. */ |
f5385ebf | 2548 | if (!h->def_regular) |
aa37626c L |
2549 | return TRUE; |
2550 | ||
986a241f RH |
2551 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2552 | us that it remains local. */ | |
2553 | return !binding_stays_local_p; | |
2554 | } | |
f6c52c13 AM |
2555 | |
2556 | /* Return true if the symbol referred to by H should be considered | |
2557 | to resolve local to the current module, and false otherwise. Differs | |
2558 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2559 | undefined symbols and weak symbols. */ | |
2560 | ||
2561 | bfd_boolean | |
268b6b39 AM |
2562 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2563 | struct bfd_link_info *info, | |
2564 | bfd_boolean local_protected) | |
f6c52c13 AM |
2565 | { |
2566 | /* If it's a local sym, of course we resolve locally. */ | |
2567 | if (h == NULL) | |
2568 | return TRUE; | |
2569 | ||
7e2294f9 AO |
2570 | /* Common symbols that become definitions don't get the DEF_REGULAR |
2571 | flag set, so test it first, and don't bail out. */ | |
2572 | if (ELF_COMMON_DEF_P (h)) | |
2573 | /* Do nothing. */; | |
f6c52c13 | 2574 | /* If we don't have a definition in a regular file, then we can't |
9968d831 L |
2575 | resolve locally unless it has non-default visibility . The sym |
2576 | is either undefined or dynamic. */ | |
2577 | else if (!h->def_regular | |
2578 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
f6c52c13 AM |
2579 | return FALSE; |
2580 | ||
2581 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 2582 | if (h->forced_local) |
f6c52c13 AM |
2583 | return TRUE; |
2584 | ||
2585 | /* As do non-dynamic symbols. */ | |
2586 | if (h->dynindx == -1) | |
2587 | return TRUE; | |
2588 | ||
2589 | /* At this point, we know the symbol is defined and dynamic. In an | |
2590 | executable it must resolve locally, likewise when building symbolic | |
2591 | shared libraries. */ | |
2592 | if (info->executable || info->symbolic) | |
2593 | return TRUE; | |
2594 | ||
2595 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2596 | with default visibility might not resolve locally. */ | |
2597 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2598 | return FALSE; | |
2599 | ||
2600 | /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */ | |
2601 | if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED) | |
2602 | return TRUE; | |
2603 | ||
1c16dfa5 L |
2604 | /* STV_PROTECTED non-function symbols are local. */ |
2605 | if (h->type != STT_FUNC) | |
2606 | return TRUE; | |
2607 | ||
f6c52c13 AM |
2608 | /* Function pointer equality tests may require that STV_PROTECTED |
2609 | symbols be treated as dynamic symbols, even when we know that the | |
2610 | dynamic linker will resolve them locally. */ | |
2611 | return local_protected; | |
2612 | } | |
e1918d23 AM |
2613 | |
2614 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2615 | aligned. Returns the first TLS output section. */ | |
2616 | ||
2617 | struct bfd_section * | |
2618 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2619 | { | |
2620 | struct bfd_section *sec, *tls; | |
2621 | unsigned int align = 0; | |
2622 | ||
2623 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2624 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2625 | break; | |
2626 | tls = sec; | |
2627 | ||
2628 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2629 | if (sec->alignment_power > align) | |
2630 | align = sec->alignment_power; | |
2631 | ||
2632 | elf_hash_table (info)->tls_sec = tls; | |
2633 | ||
2634 | /* Ensure the alignment of the first section is the largest alignment, | |
2635 | so that the tls segment starts aligned. */ | |
2636 | if (tls != NULL) | |
2637 | tls->alignment_power = align; | |
2638 | ||
2639 | return tls; | |
2640 | } | |
0ad989f9 L |
2641 | |
2642 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2643 | static bfd_boolean | |
2644 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2645 | Elf_Internal_Sym *sym) | |
2646 | { | |
2647 | /* Local symbols do not count, but target specific ones might. */ | |
2648 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2649 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2650 | return FALSE; | |
2651 | ||
2652 | /* Function symbols do not count. */ | |
2653 | if (ELF_ST_TYPE (sym->st_info) == STT_FUNC) | |
2654 | return FALSE; | |
2655 | ||
2656 | /* If the section is undefined, then so is the symbol. */ | |
2657 | if (sym->st_shndx == SHN_UNDEF) | |
2658 | return FALSE; | |
2659 | ||
2660 | /* If the symbol is defined in the common section, then | |
2661 | it is a common definition and so does not count. */ | |
2662 | if (sym->st_shndx == SHN_COMMON) | |
2663 | return FALSE; | |
2664 | ||
2665 | /* If the symbol is in a target specific section then we | |
2666 | must rely upon the backend to tell us what it is. */ | |
2667 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2668 | /* FIXME - this function is not coded yet: | |
2669 | ||
2670 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2671 | ||
2672 | Instead for now assume that the definition is not global, | |
2673 | Even if this is wrong, at least the linker will behave | |
2674 | in the same way that it used to do. */ | |
2675 | return FALSE; | |
2676 | ||
2677 | return TRUE; | |
2678 | } | |
2679 | ||
2680 | /* Search the symbol table of the archive element of the archive ABFD | |
2681 | whose archive map contains a mention of SYMDEF, and determine if | |
2682 | the symbol is defined in this element. */ | |
2683 | static bfd_boolean | |
2684 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2685 | { | |
2686 | Elf_Internal_Shdr * hdr; | |
2687 | bfd_size_type symcount; | |
2688 | bfd_size_type extsymcount; | |
2689 | bfd_size_type extsymoff; | |
2690 | Elf_Internal_Sym *isymbuf; | |
2691 | Elf_Internal_Sym *isym; | |
2692 | Elf_Internal_Sym *isymend; | |
2693 | bfd_boolean result; | |
2694 | ||
2695 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2696 | if (abfd == NULL) | |
2697 | return FALSE; | |
2698 | ||
2699 | if (! bfd_check_format (abfd, bfd_object)) | |
2700 | return FALSE; | |
2701 | ||
2702 | /* If we have already included the element containing this symbol in the | |
2703 | link then we do not need to include it again. Just claim that any symbol | |
2704 | it contains is not a definition, so that our caller will not decide to | |
2705 | (re)include this element. */ | |
2706 | if (abfd->archive_pass) | |
2707 | return FALSE; | |
2708 | ||
2709 | /* Select the appropriate symbol table. */ | |
2710 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2711 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2712 | else | |
2713 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2714 | ||
2715 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2716 | ||
2717 | /* The sh_info field of the symtab header tells us where the | |
2718 | external symbols start. We don't care about the local symbols. */ | |
2719 | if (elf_bad_symtab (abfd)) | |
2720 | { | |
2721 | extsymcount = symcount; | |
2722 | extsymoff = 0; | |
2723 | } | |
2724 | else | |
2725 | { | |
2726 | extsymcount = symcount - hdr->sh_info; | |
2727 | extsymoff = hdr->sh_info; | |
2728 | } | |
2729 | ||
2730 | if (extsymcount == 0) | |
2731 | return FALSE; | |
2732 | ||
2733 | /* Read in the symbol table. */ | |
2734 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2735 | NULL, NULL, NULL); | |
2736 | if (isymbuf == NULL) | |
2737 | return FALSE; | |
2738 | ||
2739 | /* Scan the symbol table looking for SYMDEF. */ | |
2740 | result = FALSE; | |
2741 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2742 | { | |
2743 | const char *name; | |
2744 | ||
2745 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
2746 | isym->st_name); | |
2747 | if (name == NULL) | |
2748 | break; | |
2749 | ||
2750 | if (strcmp (name, symdef->name) == 0) | |
2751 | { | |
2752 | result = is_global_data_symbol_definition (abfd, isym); | |
2753 | break; | |
2754 | } | |
2755 | } | |
2756 | ||
2757 | free (isymbuf); | |
2758 | ||
2759 | return result; | |
2760 | } | |
2761 | \f | |
5a580b3a AM |
2762 | /* Add an entry to the .dynamic table. */ |
2763 | ||
2764 | bfd_boolean | |
2765 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
2766 | bfd_vma tag, | |
2767 | bfd_vma val) | |
2768 | { | |
2769 | struct elf_link_hash_table *hash_table; | |
2770 | const struct elf_backend_data *bed; | |
2771 | asection *s; | |
2772 | bfd_size_type newsize; | |
2773 | bfd_byte *newcontents; | |
2774 | Elf_Internal_Dyn dyn; | |
2775 | ||
2776 | hash_table = elf_hash_table (info); | |
2777 | if (! is_elf_hash_table (hash_table)) | |
2778 | return FALSE; | |
2779 | ||
8fdd7217 NC |
2780 | if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL) |
2781 | _bfd_error_handler | |
2782 | (_("warning: creating a DT_TEXTREL in a shared object.")); | |
2783 | ||
5a580b3a AM |
2784 | bed = get_elf_backend_data (hash_table->dynobj); |
2785 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2786 | BFD_ASSERT (s != NULL); | |
2787 | ||
eea6121a | 2788 | newsize = s->size + bed->s->sizeof_dyn; |
5a580b3a AM |
2789 | newcontents = bfd_realloc (s->contents, newsize); |
2790 | if (newcontents == NULL) | |
2791 | return FALSE; | |
2792 | ||
2793 | dyn.d_tag = tag; | |
2794 | dyn.d_un.d_val = val; | |
eea6121a | 2795 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 2796 | |
eea6121a | 2797 | s->size = newsize; |
5a580b3a AM |
2798 | s->contents = newcontents; |
2799 | ||
2800 | return TRUE; | |
2801 | } | |
2802 | ||
2803 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
2804 | otherwise just check whether one already exists. Returns -1 on error, | |
2805 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
2806 | ||
4ad4eba5 | 2807 | static int |
7e9f0867 AM |
2808 | elf_add_dt_needed_tag (bfd *abfd, |
2809 | struct bfd_link_info *info, | |
4ad4eba5 AM |
2810 | const char *soname, |
2811 | bfd_boolean do_it) | |
5a580b3a AM |
2812 | { |
2813 | struct elf_link_hash_table *hash_table; | |
2814 | bfd_size_type oldsize; | |
2815 | bfd_size_type strindex; | |
2816 | ||
7e9f0867 AM |
2817 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
2818 | return -1; | |
2819 | ||
5a580b3a AM |
2820 | hash_table = elf_hash_table (info); |
2821 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
2822 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
2823 | if (strindex == (bfd_size_type) -1) | |
2824 | return -1; | |
2825 | ||
2826 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
2827 | { | |
2828 | asection *sdyn; | |
2829 | const struct elf_backend_data *bed; | |
2830 | bfd_byte *extdyn; | |
2831 | ||
2832 | bed = get_elf_backend_data (hash_table->dynobj); | |
2833 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
7e9f0867 AM |
2834 | if (sdyn != NULL) |
2835 | for (extdyn = sdyn->contents; | |
2836 | extdyn < sdyn->contents + sdyn->size; | |
2837 | extdyn += bed->s->sizeof_dyn) | |
2838 | { | |
2839 | Elf_Internal_Dyn dyn; | |
5a580b3a | 2840 | |
7e9f0867 AM |
2841 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
2842 | if (dyn.d_tag == DT_NEEDED | |
2843 | && dyn.d_un.d_val == strindex) | |
2844 | { | |
2845 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2846 | return 1; | |
2847 | } | |
2848 | } | |
5a580b3a AM |
2849 | } |
2850 | ||
2851 | if (do_it) | |
2852 | { | |
7e9f0867 AM |
2853 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
2854 | return -1; | |
2855 | ||
5a580b3a AM |
2856 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
2857 | return -1; | |
2858 | } | |
2859 | else | |
2860 | /* We were just checking for existence of the tag. */ | |
2861 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2862 | ||
2863 | return 0; | |
2864 | } | |
2865 | ||
77cfaee6 AM |
2866 | /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols |
2867 | belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no | |
ec13b3bb AM |
2868 | references from regular objects to these symbols. |
2869 | ||
2870 | ??? Should we do something about references from other dynamic | |
2871 | obects? If not, we potentially lose some warnings about undefined | |
2872 | symbols. But how can we recover the initial undefined / undefweak | |
2873 | state? */ | |
77cfaee6 AM |
2874 | |
2875 | struct elf_smash_syms_data | |
2876 | { | |
2877 | bfd *not_needed; | |
2878 | struct elf_link_hash_table *htab; | |
2879 | bfd_boolean twiddled; | |
2880 | }; | |
2881 | ||
2882 | static bfd_boolean | |
2883 | elf_smash_syms (struct elf_link_hash_entry *h, void *data) | |
2884 | { | |
2885 | struct elf_smash_syms_data *inf = (struct elf_smash_syms_data *) data; | |
2886 | struct bfd_link_hash_entry *bh; | |
2887 | ||
2888 | switch (h->root.type) | |
2889 | { | |
2890 | default: | |
2891 | case bfd_link_hash_new: | |
2892 | return TRUE; | |
2893 | ||
2894 | case bfd_link_hash_undefined: | |
11f25ea6 AM |
2895 | if (h->root.u.undef.abfd != inf->not_needed) |
2896 | return TRUE; | |
4ea42fb7 AM |
2897 | if (h->root.u.undef.weak != NULL |
2898 | && h->root.u.undef.weak != inf->not_needed) | |
11f25ea6 AM |
2899 | { |
2900 | /* Symbol was undefweak in u.undef.weak bfd, and has become | |
2901 | undefined in as-needed lib. Restore weak. */ | |
2902 | h->root.type = bfd_link_hash_undefweak; | |
2903 | h->root.u.undef.abfd = h->root.u.undef.weak; | |
2904 | if (h->root.u.undef.next != NULL | |
2905 | || inf->htab->root.undefs_tail == &h->root) | |
2906 | inf->twiddled = TRUE; | |
2907 | return TRUE; | |
2908 | } | |
2909 | break; | |
2910 | ||
77cfaee6 AM |
2911 | case bfd_link_hash_undefweak: |
2912 | if (h->root.u.undef.abfd != inf->not_needed) | |
2913 | return TRUE; | |
2914 | break; | |
2915 | ||
2916 | case bfd_link_hash_defined: | |
2917 | case bfd_link_hash_defweak: | |
2918 | if (h->root.u.def.section->owner != inf->not_needed) | |
2919 | return TRUE; | |
2920 | break; | |
2921 | ||
2922 | case bfd_link_hash_common: | |
2923 | if (h->root.u.c.p->section->owner != inf->not_needed) | |
2924 | return TRUE; | |
2925 | break; | |
2926 | ||
2927 | case bfd_link_hash_warning: | |
2928 | case bfd_link_hash_indirect: | |
2929 | elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data); | |
2930 | if (h->root.u.i.link->type != bfd_link_hash_new) | |
2931 | return TRUE; | |
2932 | if (h->root.u.i.link->u.undef.abfd != inf->not_needed) | |
2933 | return TRUE; | |
2934 | break; | |
2935 | } | |
2936 | ||
11f25ea6 AM |
2937 | /* There is no way we can undo symbol table state from defined or |
2938 | defweak back to undefined. */ | |
2939 | if (h->ref_regular) | |
2940 | abort (); | |
2941 | ||
2e8b3a61 AM |
2942 | /* Set sym back to newly created state, but keep undef.next if it is |
2943 | being used as a list pointer. */ | |
77cfaee6 | 2944 | bh = h->root.u.undef.next; |
2e8b3a61 AM |
2945 | if (bh == &h->root) |
2946 | bh = NULL; | |
77cfaee6 AM |
2947 | if (bh != NULL || inf->htab->root.undefs_tail == &h->root) |
2948 | inf->twiddled = TRUE; | |
2949 | (*inf->htab->root.table.newfunc) (&h->root.root, | |
2950 | &inf->htab->root.table, | |
2951 | h->root.root.string); | |
2952 | h->root.u.undef.next = bh; | |
2953 | h->root.u.undef.abfd = inf->not_needed; | |
2954 | h->non_elf = 0; | |
2955 | return TRUE; | |
2956 | } | |
2957 | ||
5a580b3a | 2958 | /* Sort symbol by value and section. */ |
4ad4eba5 AM |
2959 | static int |
2960 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
2961 | { |
2962 | const struct elf_link_hash_entry *h1; | |
2963 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 2964 | bfd_signed_vma vdiff; |
5a580b3a AM |
2965 | |
2966 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
2967 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
2968 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
2969 | if (vdiff != 0) | |
2970 | return vdiff > 0 ? 1 : -1; | |
2971 | else | |
2972 | { | |
2973 | long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
2974 | if (sdiff != 0) | |
2975 | return sdiff > 0 ? 1 : -1; | |
2976 | } | |
5a580b3a AM |
2977 | return 0; |
2978 | } | |
4ad4eba5 | 2979 | |
5a580b3a AM |
2980 | /* This function is used to adjust offsets into .dynstr for |
2981 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
2982 | ||
2983 | static bfd_boolean | |
2984 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
2985 | { | |
2986 | struct elf_strtab_hash *dynstr = data; | |
2987 | ||
2988 | if (h->root.type == bfd_link_hash_warning) | |
2989 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2990 | ||
2991 | if (h->dynindx != -1) | |
2992 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
2993 | return TRUE; | |
2994 | } | |
2995 | ||
2996 | /* Assign string offsets in .dynstr, update all structures referencing | |
2997 | them. */ | |
2998 | ||
4ad4eba5 AM |
2999 | static bfd_boolean |
3000 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3001 | { |
3002 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3003 | struct elf_link_local_dynamic_entry *entry; | |
3004 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3005 | bfd *dynobj = hash_table->dynobj; | |
3006 | asection *sdyn; | |
3007 | bfd_size_type size; | |
3008 | const struct elf_backend_data *bed; | |
3009 | bfd_byte *extdyn; | |
3010 | ||
3011 | _bfd_elf_strtab_finalize (dynstr); | |
3012 | size = _bfd_elf_strtab_size (dynstr); | |
3013 | ||
3014 | bed = get_elf_backend_data (dynobj); | |
3015 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
3016 | BFD_ASSERT (sdyn != NULL); | |
3017 | ||
3018 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3019 | for (extdyn = sdyn->contents; | |
eea6121a | 3020 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3021 | extdyn += bed->s->sizeof_dyn) |
3022 | { | |
3023 | Elf_Internal_Dyn dyn; | |
3024 | ||
3025 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3026 | switch (dyn.d_tag) | |
3027 | { | |
3028 | case DT_STRSZ: | |
3029 | dyn.d_un.d_val = size; | |
3030 | break; | |
3031 | case DT_NEEDED: | |
3032 | case DT_SONAME: | |
3033 | case DT_RPATH: | |
3034 | case DT_RUNPATH: | |
3035 | case DT_FILTER: | |
3036 | case DT_AUXILIARY: | |
3037 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
3038 | break; | |
3039 | default: | |
3040 | continue; | |
3041 | } | |
3042 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3043 | } | |
3044 | ||
3045 | /* Now update local dynamic symbols. */ | |
3046 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3047 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3048 | entry->isym.st_name); | |
3049 | ||
3050 | /* And the rest of dynamic symbols. */ | |
3051 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3052 | ||
3053 | /* Adjust version definitions. */ | |
3054 | if (elf_tdata (output_bfd)->cverdefs) | |
3055 | { | |
3056 | asection *s; | |
3057 | bfd_byte *p; | |
3058 | bfd_size_type i; | |
3059 | Elf_Internal_Verdef def; | |
3060 | Elf_Internal_Verdaux defaux; | |
3061 | ||
3062 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
3063 | p = s->contents; | |
3064 | do | |
3065 | { | |
3066 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3067 | &def); | |
3068 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3069 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3070 | continue; | |
5a580b3a AM |
3071 | for (i = 0; i < def.vd_cnt; ++i) |
3072 | { | |
3073 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3074 | (Elf_External_Verdaux *) p, &defaux); | |
3075 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3076 | defaux.vda_name); | |
3077 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3078 | &defaux, (Elf_External_Verdaux *) p); | |
3079 | p += sizeof (Elf_External_Verdaux); | |
3080 | } | |
3081 | } | |
3082 | while (def.vd_next); | |
3083 | } | |
3084 | ||
3085 | /* Adjust version references. */ | |
3086 | if (elf_tdata (output_bfd)->verref) | |
3087 | { | |
3088 | asection *s; | |
3089 | bfd_byte *p; | |
3090 | bfd_size_type i; | |
3091 | Elf_Internal_Verneed need; | |
3092 | Elf_Internal_Vernaux needaux; | |
3093 | ||
3094 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
3095 | p = s->contents; | |
3096 | do | |
3097 | { | |
3098 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3099 | &need); | |
3100 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3101 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3102 | (Elf_External_Verneed *) p); | |
3103 | p += sizeof (Elf_External_Verneed); | |
3104 | for (i = 0; i < need.vn_cnt; ++i) | |
3105 | { | |
3106 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3107 | (Elf_External_Vernaux *) p, &needaux); | |
3108 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3109 | needaux.vna_name); | |
3110 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3111 | &needaux, | |
3112 | (Elf_External_Vernaux *) p); | |
3113 | p += sizeof (Elf_External_Vernaux); | |
3114 | } | |
3115 | } | |
3116 | while (need.vn_next); | |
3117 | } | |
3118 | ||
3119 | return TRUE; | |
3120 | } | |
3121 | \f | |
4ad4eba5 AM |
3122 | /* Add symbols from an ELF object file to the linker hash table. */ |
3123 | ||
3124 | static bfd_boolean | |
3125 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3126 | { | |
3127 | bfd_boolean (*add_symbol_hook) | |
555cd476 | 3128 | (bfd *, struct bfd_link_info *, Elf_Internal_Sym *, |
4ad4eba5 AM |
3129 | const char **, flagword *, asection **, bfd_vma *); |
3130 | bfd_boolean (*check_relocs) | |
3131 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
85fbca6a NC |
3132 | bfd_boolean (*check_directives) |
3133 | (bfd *, struct bfd_link_info *); | |
4ad4eba5 AM |
3134 | bfd_boolean collect; |
3135 | Elf_Internal_Shdr *hdr; | |
3136 | bfd_size_type symcount; | |
3137 | bfd_size_type extsymcount; | |
3138 | bfd_size_type extsymoff; | |
3139 | struct elf_link_hash_entry **sym_hash; | |
3140 | bfd_boolean dynamic; | |
3141 | Elf_External_Versym *extversym = NULL; | |
3142 | Elf_External_Versym *ever; | |
3143 | struct elf_link_hash_entry *weaks; | |
3144 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
3145 | bfd_size_type nondeflt_vers_cnt = 0; | |
3146 | Elf_Internal_Sym *isymbuf = NULL; | |
3147 | Elf_Internal_Sym *isym; | |
3148 | Elf_Internal_Sym *isymend; | |
3149 | const struct elf_backend_data *bed; | |
3150 | bfd_boolean add_needed; | |
3151 | struct elf_link_hash_table * hash_table; | |
3152 | bfd_size_type amt; | |
3153 | ||
3154 | hash_table = elf_hash_table (info); | |
3155 | ||
3156 | bed = get_elf_backend_data (abfd); | |
3157 | add_symbol_hook = bed->elf_add_symbol_hook; | |
3158 | collect = bed->collect; | |
3159 | ||
3160 | if ((abfd->flags & DYNAMIC) == 0) | |
3161 | dynamic = FALSE; | |
3162 | else | |
3163 | { | |
3164 | dynamic = TRUE; | |
3165 | ||
3166 | /* You can't use -r against a dynamic object. Also, there's no | |
3167 | hope of using a dynamic object which does not exactly match | |
3168 | the format of the output file. */ | |
3169 | if (info->relocatable | |
3170 | || !is_elf_hash_table (hash_table) | |
3171 | || hash_table->root.creator != abfd->xvec) | |
3172 | { | |
9a0789ec NC |
3173 | if (info->relocatable) |
3174 | bfd_set_error (bfd_error_invalid_operation); | |
3175 | else | |
3176 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3177 | goto error_return; |
3178 | } | |
3179 | } | |
3180 | ||
3181 | /* As a GNU extension, any input sections which are named | |
3182 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3183 | symbol. This differs from .gnu.warning sections, which generate | |
3184 | warnings when they are included in an output file. */ | |
3185 | if (info->executable) | |
3186 | { | |
3187 | asection *s; | |
3188 | ||
3189 | for (s = abfd->sections; s != NULL; s = s->next) | |
3190 | { | |
3191 | const char *name; | |
3192 | ||
3193 | name = bfd_get_section_name (abfd, s); | |
3194 | if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0) | |
3195 | { | |
3196 | char *msg; | |
3197 | bfd_size_type sz; | |
4ad4eba5 AM |
3198 | |
3199 | name += sizeof ".gnu.warning." - 1; | |
3200 | ||
3201 | /* If this is a shared object, then look up the symbol | |
3202 | in the hash table. If it is there, and it is already | |
3203 | been defined, then we will not be using the entry | |
3204 | from this shared object, so we don't need to warn. | |
3205 | FIXME: If we see the definition in a regular object | |
3206 | later on, we will warn, but we shouldn't. The only | |
3207 | fix is to keep track of what warnings we are supposed | |
3208 | to emit, and then handle them all at the end of the | |
3209 | link. */ | |
3210 | if (dynamic) | |
3211 | { | |
3212 | struct elf_link_hash_entry *h; | |
3213 | ||
3214 | h = elf_link_hash_lookup (hash_table, name, | |
3215 | FALSE, FALSE, TRUE); | |
3216 | ||
3217 | /* FIXME: What about bfd_link_hash_common? */ | |
3218 | if (h != NULL | |
3219 | && (h->root.type == bfd_link_hash_defined | |
3220 | || h->root.type == bfd_link_hash_defweak)) | |
3221 | { | |
3222 | /* We don't want to issue this warning. Clobber | |
3223 | the section size so that the warning does not | |
3224 | get copied into the output file. */ | |
eea6121a | 3225 | s->size = 0; |
4ad4eba5 AM |
3226 | continue; |
3227 | } | |
3228 | } | |
3229 | ||
eea6121a | 3230 | sz = s->size; |
370a0e1b | 3231 | msg = bfd_alloc (abfd, sz + 1); |
4ad4eba5 AM |
3232 | if (msg == NULL) |
3233 | goto error_return; | |
3234 | ||
370a0e1b | 3235 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
4ad4eba5 AM |
3236 | goto error_return; |
3237 | ||
370a0e1b | 3238 | msg[sz] = '\0'; |
4ad4eba5 AM |
3239 | |
3240 | if (! (_bfd_generic_link_add_one_symbol | |
3241 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3242 | FALSE, collect, NULL))) | |
3243 | goto error_return; | |
3244 | ||
3245 | if (! info->relocatable) | |
3246 | { | |
3247 | /* Clobber the section size so that the warning does | |
3248 | not get copied into the output file. */ | |
eea6121a | 3249 | s->size = 0; |
11d2f718 AM |
3250 | |
3251 | /* Also set SEC_EXCLUDE, so that symbols defined in | |
3252 | the warning section don't get copied to the output. */ | |
3253 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3254 | } |
3255 | } | |
3256 | } | |
3257 | } | |
3258 | ||
3259 | add_needed = TRUE; | |
3260 | if (! dynamic) | |
3261 | { | |
3262 | /* If we are creating a shared library, create all the dynamic | |
3263 | sections immediately. We need to attach them to something, | |
3264 | so we attach them to this BFD, provided it is the right | |
3265 | format. FIXME: If there are no input BFD's of the same | |
3266 | format as the output, we can't make a shared library. */ | |
3267 | if (info->shared | |
3268 | && is_elf_hash_table (hash_table) | |
3269 | && hash_table->root.creator == abfd->xvec | |
3270 | && ! hash_table->dynamic_sections_created) | |
3271 | { | |
3272 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3273 | goto error_return; | |
3274 | } | |
3275 | } | |
3276 | else if (!is_elf_hash_table (hash_table)) | |
3277 | goto error_return; | |
3278 | else | |
3279 | { | |
3280 | asection *s; | |
3281 | const char *soname = NULL; | |
3282 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; | |
3283 | int ret; | |
3284 | ||
3285 | /* ld --just-symbols and dynamic objects don't mix very well. | |
3286 | Test for --just-symbols by looking at info set up by | |
3287 | _bfd_elf_link_just_syms. */ | |
3288 | if ((s = abfd->sections) != NULL | |
3289 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
3290 | goto error_return; | |
3291 | ||
3292 | /* If this dynamic lib was specified on the command line with | |
3293 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3294 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3295 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3296 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3297 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3298 | all. */ | |
3299 | add_needed = (elf_dyn_lib_class (abfd) | |
3300 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3301 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3302 | |
3303 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3304 | if (s != NULL) | |
3305 | { | |
3306 | bfd_byte *dynbuf; | |
3307 | bfd_byte *extdyn; | |
3308 | int elfsec; | |
3309 | unsigned long shlink; | |
3310 | ||
eea6121a | 3311 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
4ad4eba5 AM |
3312 | goto error_free_dyn; |
3313 | ||
3314 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
3315 | if (elfsec == -1) | |
3316 | goto error_free_dyn; | |
3317 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3318 | ||
3319 | for (extdyn = dynbuf; | |
eea6121a | 3320 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3321 | extdyn += bed->s->sizeof_dyn) |
3322 | { | |
3323 | Elf_Internal_Dyn dyn; | |
3324 | ||
3325 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3326 | if (dyn.d_tag == DT_SONAME) | |
3327 | { | |
3328 | unsigned int tagv = dyn.d_un.d_val; | |
3329 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3330 | if (soname == NULL) | |
3331 | goto error_free_dyn; | |
3332 | } | |
3333 | if (dyn.d_tag == DT_NEEDED) | |
3334 | { | |
3335 | struct bfd_link_needed_list *n, **pn; | |
3336 | char *fnm, *anm; | |
3337 | unsigned int tagv = dyn.d_un.d_val; | |
3338 | ||
3339 | amt = sizeof (struct bfd_link_needed_list); | |
3340 | n = bfd_alloc (abfd, amt); | |
3341 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3342 | if (n == NULL || fnm == NULL) | |
3343 | goto error_free_dyn; | |
3344 | amt = strlen (fnm) + 1; | |
3345 | anm = bfd_alloc (abfd, amt); | |
3346 | if (anm == NULL) | |
3347 | goto error_free_dyn; | |
3348 | memcpy (anm, fnm, amt); | |
3349 | n->name = anm; | |
3350 | n->by = abfd; | |
3351 | n->next = NULL; | |
3352 | for (pn = & hash_table->needed; | |
3353 | *pn != NULL; | |
3354 | pn = &(*pn)->next) | |
3355 | ; | |
3356 | *pn = n; | |
3357 | } | |
3358 | if (dyn.d_tag == DT_RUNPATH) | |
3359 | { | |
3360 | struct bfd_link_needed_list *n, **pn; | |
3361 | char *fnm, *anm; | |
3362 | unsigned int tagv = dyn.d_un.d_val; | |
3363 | ||
3364 | amt = sizeof (struct bfd_link_needed_list); | |
3365 | n = bfd_alloc (abfd, amt); | |
3366 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3367 | if (n == NULL || fnm == NULL) | |
3368 | goto error_free_dyn; | |
3369 | amt = strlen (fnm) + 1; | |
3370 | anm = bfd_alloc (abfd, amt); | |
3371 | if (anm == NULL) | |
3372 | goto error_free_dyn; | |
3373 | memcpy (anm, fnm, amt); | |
3374 | n->name = anm; | |
3375 | n->by = abfd; | |
3376 | n->next = NULL; | |
3377 | for (pn = & runpath; | |
3378 | *pn != NULL; | |
3379 | pn = &(*pn)->next) | |
3380 | ; | |
3381 | *pn = n; | |
3382 | } | |
3383 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3384 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3385 | { | |
3386 | struct bfd_link_needed_list *n, **pn; | |
3387 | char *fnm, *anm; | |
3388 | unsigned int tagv = dyn.d_un.d_val; | |
3389 | ||
3390 | amt = sizeof (struct bfd_link_needed_list); | |
3391 | n = bfd_alloc (abfd, amt); | |
3392 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3393 | if (n == NULL || fnm == NULL) | |
3394 | goto error_free_dyn; | |
3395 | amt = strlen (fnm) + 1; | |
3396 | anm = bfd_alloc (abfd, amt); | |
3397 | if (anm == NULL) | |
3398 | { | |
3399 | error_free_dyn: | |
3400 | free (dynbuf); | |
3401 | goto error_return; | |
3402 | } | |
3403 | memcpy (anm, fnm, amt); | |
3404 | n->name = anm; | |
3405 | n->by = abfd; | |
3406 | n->next = NULL; | |
3407 | for (pn = & rpath; | |
3408 | *pn != NULL; | |
3409 | pn = &(*pn)->next) | |
3410 | ; | |
3411 | *pn = n; | |
3412 | } | |
3413 | } | |
3414 | ||
3415 | free (dynbuf); | |
3416 | } | |
3417 | ||
3418 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3419 | frees all more recently bfd_alloc'd blocks as well. */ | |
3420 | if (runpath) | |
3421 | rpath = runpath; | |
3422 | ||
3423 | if (rpath) | |
3424 | { | |
3425 | struct bfd_link_needed_list **pn; | |
3426 | for (pn = & hash_table->runpath; | |
3427 | *pn != NULL; | |
3428 | pn = &(*pn)->next) | |
3429 | ; | |
3430 | *pn = rpath; | |
3431 | } | |
3432 | ||
3433 | /* We do not want to include any of the sections in a dynamic | |
3434 | object in the output file. We hack by simply clobbering the | |
3435 | list of sections in the BFD. This could be handled more | |
3436 | cleanly by, say, a new section flag; the existing | |
3437 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3438 | still implies that the section takes up space in the output | |
3439 | file. */ | |
3440 | bfd_section_list_clear (abfd); | |
3441 | ||
4ad4eba5 AM |
3442 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3443 | object. If the object has a DT_SONAME entry, we use it. | |
3444 | Otherwise, if the generic linker stuck something in | |
3445 | elf_dt_name, we use that. Otherwise, we just use the file | |
3446 | name. */ | |
3447 | if (soname == NULL || *soname == '\0') | |
3448 | { | |
3449 | soname = elf_dt_name (abfd); | |
3450 | if (soname == NULL || *soname == '\0') | |
3451 | soname = bfd_get_filename (abfd); | |
3452 | } | |
3453 | ||
3454 | /* Save the SONAME because sometimes the linker emulation code | |
3455 | will need to know it. */ | |
3456 | elf_dt_name (abfd) = soname; | |
3457 | ||
7e9f0867 | 3458 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3459 | if (ret < 0) |
3460 | goto error_return; | |
3461 | ||
3462 | /* If we have already included this dynamic object in the | |
3463 | link, just ignore it. There is no reason to include a | |
3464 | particular dynamic object more than once. */ | |
3465 | if (ret > 0) | |
3466 | return TRUE; | |
3467 | } | |
3468 | ||
3469 | /* If this is a dynamic object, we always link against the .dynsym | |
3470 | symbol table, not the .symtab symbol table. The dynamic linker | |
3471 | will only see the .dynsym symbol table, so there is no reason to | |
3472 | look at .symtab for a dynamic object. */ | |
3473 | ||
3474 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3475 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3476 | else | |
3477 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3478 | ||
3479 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3480 | ||
3481 | /* The sh_info field of the symtab header tells us where the | |
3482 | external symbols start. We don't care about the local symbols at | |
3483 | this point. */ | |
3484 | if (elf_bad_symtab (abfd)) | |
3485 | { | |
3486 | extsymcount = symcount; | |
3487 | extsymoff = 0; | |
3488 | } | |
3489 | else | |
3490 | { | |
3491 | extsymcount = symcount - hdr->sh_info; | |
3492 | extsymoff = hdr->sh_info; | |
3493 | } | |
3494 | ||
3495 | sym_hash = NULL; | |
3496 | if (extsymcount != 0) | |
3497 | { | |
3498 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3499 | NULL, NULL, NULL); | |
3500 | if (isymbuf == NULL) | |
3501 | goto error_return; | |
3502 | ||
3503 | /* We store a pointer to the hash table entry for each external | |
3504 | symbol. */ | |
3505 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3506 | sym_hash = bfd_alloc (abfd, amt); | |
3507 | if (sym_hash == NULL) | |
3508 | goto error_free_sym; | |
3509 | elf_sym_hashes (abfd) = sym_hash; | |
3510 | } | |
3511 | ||
3512 | if (dynamic) | |
3513 | { | |
3514 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
3515 | if (!_bfd_elf_slurp_version_tables (abfd, |
3516 | info->default_imported_symver)) | |
4ad4eba5 AM |
3517 | goto error_free_sym; |
3518 | ||
3519 | /* Read in the symbol versions, but don't bother to convert them | |
3520 | to internal format. */ | |
3521 | if (elf_dynversym (abfd) != 0) | |
3522 | { | |
3523 | Elf_Internal_Shdr *versymhdr; | |
3524 | ||
3525 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
3526 | extversym = bfd_malloc (versymhdr->sh_size); | |
3527 | if (extversym == NULL) | |
3528 | goto error_free_sym; | |
3529 | amt = versymhdr->sh_size; | |
3530 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3531 | || bfd_bread (extversym, amt, abfd) != amt) | |
3532 | goto error_free_vers; | |
3533 | } | |
3534 | } | |
3535 | ||
3536 | weaks = NULL; | |
3537 | ||
3538 | ever = extversym != NULL ? extversym + extsymoff : NULL; | |
3539 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3540 | isym < isymend; | |
3541 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3542 | { | |
3543 | int bind; | |
3544 | bfd_vma value; | |
af44c138 | 3545 | asection *sec, *new_sec; |
4ad4eba5 AM |
3546 | flagword flags; |
3547 | const char *name; | |
3548 | struct elf_link_hash_entry *h; | |
3549 | bfd_boolean definition; | |
3550 | bfd_boolean size_change_ok; | |
3551 | bfd_boolean type_change_ok; | |
3552 | bfd_boolean new_weakdef; | |
3553 | bfd_boolean override; | |
3554 | unsigned int old_alignment; | |
3555 | bfd *old_bfd; | |
3556 | ||
3557 | override = FALSE; | |
3558 | ||
3559 | flags = BSF_NO_FLAGS; | |
3560 | sec = NULL; | |
3561 | value = isym->st_value; | |
3562 | *sym_hash = NULL; | |
3563 | ||
3564 | bind = ELF_ST_BIND (isym->st_info); | |
3565 | if (bind == STB_LOCAL) | |
3566 | { | |
3567 | /* This should be impossible, since ELF requires that all | |
3568 | global symbols follow all local symbols, and that sh_info | |
3569 | point to the first global symbol. Unfortunately, Irix 5 | |
3570 | screws this up. */ | |
3571 | continue; | |
3572 | } | |
3573 | else if (bind == STB_GLOBAL) | |
3574 | { | |
3575 | if (isym->st_shndx != SHN_UNDEF | |
3576 | && isym->st_shndx != SHN_COMMON) | |
3577 | flags = BSF_GLOBAL; | |
3578 | } | |
3579 | else if (bind == STB_WEAK) | |
3580 | flags = BSF_WEAK; | |
3581 | else | |
3582 | { | |
3583 | /* Leave it up to the processor backend. */ | |
3584 | } | |
3585 | ||
3586 | if (isym->st_shndx == SHN_UNDEF) | |
3587 | sec = bfd_und_section_ptr; | |
3588 | else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
3589 | { | |
3590 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3591 | if (sec == NULL) | |
3592 | sec = bfd_abs_section_ptr; | |
529fcb95 PB |
3593 | else if (sec->kept_section) |
3594 | { | |
1f02cbd9 | 3595 | /* Symbols from discarded section are undefined, and have |
3b36f7e6 | 3596 | default visibility. */ |
529fcb95 PB |
3597 | sec = bfd_und_section_ptr; |
3598 | isym->st_shndx = SHN_UNDEF; | |
1f02cbd9 JB |
3599 | isym->st_other = STV_DEFAULT |
3600 | | (isym->st_other & ~ ELF_ST_VISIBILITY(-1)); | |
529fcb95 | 3601 | } |
4ad4eba5 AM |
3602 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
3603 | value -= sec->vma; | |
3604 | } | |
3605 | else if (isym->st_shndx == SHN_ABS) | |
3606 | sec = bfd_abs_section_ptr; | |
3607 | else if (isym->st_shndx == SHN_COMMON) | |
3608 | { | |
3609 | sec = bfd_com_section_ptr; | |
3610 | /* What ELF calls the size we call the value. What ELF | |
3611 | calls the value we call the alignment. */ | |
3612 | value = isym->st_size; | |
3613 | } | |
3614 | else | |
3615 | { | |
3616 | /* Leave it up to the processor backend. */ | |
3617 | } | |
3618 | ||
3619 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3620 | isym->st_name); | |
3621 | if (name == NULL) | |
3622 | goto error_free_vers; | |
3623 | ||
3624 | if (isym->st_shndx == SHN_COMMON | |
3625 | && ELF_ST_TYPE (isym->st_info) == STT_TLS) | |
3626 | { | |
3627 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3628 | ||
3629 | if (tcomm == NULL) | |
3630 | { | |
3496cb2a L |
3631 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", |
3632 | (SEC_ALLOC | |
3633 | | SEC_IS_COMMON | |
3634 | | SEC_LINKER_CREATED | |
3635 | | SEC_THREAD_LOCAL)); | |
3636 | if (tcomm == NULL) | |
4ad4eba5 AM |
3637 | goto error_free_vers; |
3638 | } | |
3639 | sec = tcomm; | |
3640 | } | |
3641 | else if (add_symbol_hook) | |
3642 | { | |
3643 | if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec, | |
3644 | &value)) | |
3645 | goto error_free_vers; | |
3646 | ||
3647 | /* The hook function sets the name to NULL if this symbol | |
3648 | should be skipped for some reason. */ | |
3649 | if (name == NULL) | |
3650 | continue; | |
3651 | } | |
3652 | ||
3653 | /* Sanity check that all possibilities were handled. */ | |
3654 | if (sec == NULL) | |
3655 | { | |
3656 | bfd_set_error (bfd_error_bad_value); | |
3657 | goto error_free_vers; | |
3658 | } | |
3659 | ||
3660 | if (bfd_is_und_section (sec) | |
3661 | || bfd_is_com_section (sec)) | |
3662 | definition = FALSE; | |
3663 | else | |
3664 | definition = TRUE; | |
3665 | ||
3666 | size_change_ok = FALSE; | |
3667 | type_change_ok = get_elf_backend_data (abfd)->type_change_ok; | |
3668 | old_alignment = 0; | |
3669 | old_bfd = NULL; | |
af44c138 | 3670 | new_sec = sec; |
4ad4eba5 AM |
3671 | |
3672 | if (is_elf_hash_table (hash_table)) | |
3673 | { | |
3674 | Elf_Internal_Versym iver; | |
3675 | unsigned int vernum = 0; | |
3676 | bfd_boolean skip; | |
3677 | ||
fc0e6df6 | 3678 | if (ever == NULL) |
4ad4eba5 | 3679 | { |
fc0e6df6 PB |
3680 | if (info->default_imported_symver) |
3681 | /* Use the default symbol version created earlier. */ | |
3682 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
3683 | else | |
3684 | iver.vs_vers = 0; | |
3685 | } | |
3686 | else | |
3687 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
3688 | ||
3689 | vernum = iver.vs_vers & VERSYM_VERSION; | |
3690 | ||
3691 | /* If this is a hidden symbol, or if it is not version | |
3692 | 1, we append the version name to the symbol name. | |
3693 | However, we do not modify a non-hidden absolute | |
3694 | symbol, because it might be the version symbol | |
3695 | itself. FIXME: What if it isn't? */ | |
3696 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 | |
3697 | || (vernum > 1 && ! bfd_is_abs_section (sec))) | |
3698 | { | |
3699 | const char *verstr; | |
3700 | size_t namelen, verlen, newlen; | |
3701 | char *newname, *p; | |
3702 | ||
3703 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 3704 | { |
fc0e6df6 PB |
3705 | if (vernum > elf_tdata (abfd)->cverdefs) |
3706 | verstr = NULL; | |
3707 | else if (vernum > 1) | |
3708 | verstr = | |
3709 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
3710 | else | |
3711 | verstr = ""; | |
4ad4eba5 | 3712 | |
fc0e6df6 | 3713 | if (verstr == NULL) |
4ad4eba5 | 3714 | { |
fc0e6df6 PB |
3715 | (*_bfd_error_handler) |
3716 | (_("%B: %s: invalid version %u (max %d)"), | |
3717 | abfd, name, vernum, | |
3718 | elf_tdata (abfd)->cverdefs); | |
3719 | bfd_set_error (bfd_error_bad_value); | |
3720 | goto error_free_vers; | |
4ad4eba5 | 3721 | } |
fc0e6df6 PB |
3722 | } |
3723 | else | |
3724 | { | |
3725 | /* We cannot simply test for the number of | |
3726 | entries in the VERNEED section since the | |
3727 | numbers for the needed versions do not start | |
3728 | at 0. */ | |
3729 | Elf_Internal_Verneed *t; | |
3730 | ||
3731 | verstr = NULL; | |
3732 | for (t = elf_tdata (abfd)->verref; | |
3733 | t != NULL; | |
3734 | t = t->vn_nextref) | |
4ad4eba5 | 3735 | { |
fc0e6df6 | 3736 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 3737 | |
fc0e6df6 PB |
3738 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
3739 | { | |
3740 | if (a->vna_other == vernum) | |
4ad4eba5 | 3741 | { |
fc0e6df6 PB |
3742 | verstr = a->vna_nodename; |
3743 | break; | |
4ad4eba5 | 3744 | } |
4ad4eba5 | 3745 | } |
fc0e6df6 PB |
3746 | if (a != NULL) |
3747 | break; | |
3748 | } | |
3749 | if (verstr == NULL) | |
3750 | { | |
3751 | (*_bfd_error_handler) | |
3752 | (_("%B: %s: invalid needed version %d"), | |
3753 | abfd, name, vernum); | |
3754 | bfd_set_error (bfd_error_bad_value); | |
3755 | goto error_free_vers; | |
4ad4eba5 | 3756 | } |
4ad4eba5 | 3757 | } |
fc0e6df6 PB |
3758 | |
3759 | namelen = strlen (name); | |
3760 | verlen = strlen (verstr); | |
3761 | newlen = namelen + verlen + 2; | |
3762 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3763 | && isym->st_shndx != SHN_UNDEF) | |
3764 | ++newlen; | |
3765 | ||
3766 | newname = bfd_alloc (abfd, newlen); | |
3767 | if (newname == NULL) | |
3768 | goto error_free_vers; | |
3769 | memcpy (newname, name, namelen); | |
3770 | p = newname + namelen; | |
3771 | *p++ = ELF_VER_CHR; | |
3772 | /* If this is a defined non-hidden version symbol, | |
3773 | we add another @ to the name. This indicates the | |
3774 | default version of the symbol. */ | |
3775 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3776 | && isym->st_shndx != SHN_UNDEF) | |
3777 | *p++ = ELF_VER_CHR; | |
3778 | memcpy (p, verstr, verlen + 1); | |
3779 | ||
3780 | name = newname; | |
4ad4eba5 AM |
3781 | } |
3782 | ||
af44c138 L |
3783 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, |
3784 | &value, &old_alignment, | |
4ad4eba5 AM |
3785 | sym_hash, &skip, &override, |
3786 | &type_change_ok, &size_change_ok)) | |
3787 | goto error_free_vers; | |
3788 | ||
3789 | if (skip) | |
3790 | continue; | |
3791 | ||
3792 | if (override) | |
3793 | definition = FALSE; | |
3794 | ||
3795 | h = *sym_hash; | |
3796 | while (h->root.type == bfd_link_hash_indirect | |
3797 | || h->root.type == bfd_link_hash_warning) | |
3798 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3799 | ||
3800 | /* Remember the old alignment if this is a common symbol, so | |
3801 | that we don't reduce the alignment later on. We can't | |
3802 | check later, because _bfd_generic_link_add_one_symbol | |
3803 | will set a default for the alignment which we want to | |
3804 | override. We also remember the old bfd where the existing | |
3805 | definition comes from. */ | |
3806 | switch (h->root.type) | |
3807 | { | |
3808 | default: | |
3809 | break; | |
3810 | ||
3811 | case bfd_link_hash_defined: | |
3812 | case bfd_link_hash_defweak: | |
3813 | old_bfd = h->root.u.def.section->owner; | |
3814 | break; | |
3815 | ||
3816 | case bfd_link_hash_common: | |
3817 | old_bfd = h->root.u.c.p->section->owner; | |
3818 | old_alignment = h->root.u.c.p->alignment_power; | |
3819 | break; | |
3820 | } | |
3821 | ||
3822 | if (elf_tdata (abfd)->verdef != NULL | |
3823 | && ! override | |
3824 | && vernum > 1 | |
3825 | && definition) | |
3826 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
3827 | } | |
3828 | ||
3829 | if (! (_bfd_generic_link_add_one_symbol | |
3830 | (info, abfd, name, flags, sec, value, NULL, FALSE, collect, | |
3831 | (struct bfd_link_hash_entry **) sym_hash))) | |
3832 | goto error_free_vers; | |
3833 | ||
3834 | h = *sym_hash; | |
3835 | while (h->root.type == bfd_link_hash_indirect | |
3836 | || h->root.type == bfd_link_hash_warning) | |
3837 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3838 | *sym_hash = h; | |
3839 | ||
3840 | new_weakdef = FALSE; | |
3841 | if (dynamic | |
3842 | && definition | |
3843 | && (flags & BSF_WEAK) != 0 | |
3844 | && ELF_ST_TYPE (isym->st_info) != STT_FUNC | |
3845 | && is_elf_hash_table (hash_table) | |
f6e332e6 | 3846 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
3847 | { |
3848 | /* Keep a list of all weak defined non function symbols from | |
3849 | a dynamic object, using the weakdef field. Later in this | |
3850 | function we will set the weakdef field to the correct | |
3851 | value. We only put non-function symbols from dynamic | |
3852 | objects on this list, because that happens to be the only | |
3853 | time we need to know the normal symbol corresponding to a | |
3854 | weak symbol, and the information is time consuming to | |
3855 | figure out. If the weakdef field is not already NULL, | |
3856 | then this symbol was already defined by some previous | |
3857 | dynamic object, and we will be using that previous | |
3858 | definition anyhow. */ | |
3859 | ||
f6e332e6 | 3860 | h->u.weakdef = weaks; |
4ad4eba5 AM |
3861 | weaks = h; |
3862 | new_weakdef = TRUE; | |
3863 | } | |
3864 | ||
3865 | /* Set the alignment of a common symbol. */ | |
af44c138 L |
3866 | if ((isym->st_shndx == SHN_COMMON |
3867 | || bfd_is_com_section (sec)) | |
4ad4eba5 AM |
3868 | && h->root.type == bfd_link_hash_common) |
3869 | { | |
3870 | unsigned int align; | |
3871 | ||
af44c138 L |
3872 | if (isym->st_shndx == SHN_COMMON) |
3873 | align = bfd_log2 (isym->st_value); | |
3874 | else | |
3875 | { | |
3876 | /* The new symbol is a common symbol in a shared object. | |
3877 | We need to get the alignment from the section. */ | |
3878 | align = new_sec->alignment_power; | |
3879 | } | |
4ad4eba5 AM |
3880 | if (align > old_alignment |
3881 | /* Permit an alignment power of zero if an alignment of one | |
3882 | is specified and no other alignments have been specified. */ | |
3883 | || (isym->st_value == 1 && old_alignment == 0)) | |
3884 | h->root.u.c.p->alignment_power = align; | |
3885 | else | |
3886 | h->root.u.c.p->alignment_power = old_alignment; | |
3887 | } | |
3888 | ||
3889 | if (is_elf_hash_table (hash_table)) | |
3890 | { | |
4ad4eba5 | 3891 | bfd_boolean dynsym; |
4ad4eba5 AM |
3892 | |
3893 | /* Check the alignment when a common symbol is involved. This | |
3894 | can change when a common symbol is overridden by a normal | |
3895 | definition or a common symbol is ignored due to the old | |
3896 | normal definition. We need to make sure the maximum | |
3897 | alignment is maintained. */ | |
3898 | if ((old_alignment || isym->st_shndx == SHN_COMMON) | |
3899 | && h->root.type != bfd_link_hash_common) | |
3900 | { | |
3901 | unsigned int common_align; | |
3902 | unsigned int normal_align; | |
3903 | unsigned int symbol_align; | |
3904 | bfd *normal_bfd; | |
3905 | bfd *common_bfd; | |
3906 | ||
3907 | symbol_align = ffs (h->root.u.def.value) - 1; | |
3908 | if (h->root.u.def.section->owner != NULL | |
3909 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
3910 | { | |
3911 | normal_align = h->root.u.def.section->alignment_power; | |
3912 | if (normal_align > symbol_align) | |
3913 | normal_align = symbol_align; | |
3914 | } | |
3915 | else | |
3916 | normal_align = symbol_align; | |
3917 | ||
3918 | if (old_alignment) | |
3919 | { | |
3920 | common_align = old_alignment; | |
3921 | common_bfd = old_bfd; | |
3922 | normal_bfd = abfd; | |
3923 | } | |
3924 | else | |
3925 | { | |
3926 | common_align = bfd_log2 (isym->st_value); | |
3927 | common_bfd = abfd; | |
3928 | normal_bfd = old_bfd; | |
3929 | } | |
3930 | ||
3931 | if (normal_align < common_align) | |
3932 | (*_bfd_error_handler) | |
d003868e AM |
3933 | (_("Warning: alignment %u of symbol `%s' in %B" |
3934 | " is smaller than %u in %B"), | |
3935 | normal_bfd, common_bfd, | |
3936 | 1 << normal_align, name, 1 << common_align); | |
4ad4eba5 AM |
3937 | } |
3938 | ||
3939 | /* Remember the symbol size and type. */ | |
3940 | if (isym->st_size != 0 | |
3941 | && (definition || h->size == 0)) | |
3942 | { | |
3943 | if (h->size != 0 && h->size != isym->st_size && ! size_change_ok) | |
3944 | (*_bfd_error_handler) | |
d003868e AM |
3945 | (_("Warning: size of symbol `%s' changed" |
3946 | " from %lu in %B to %lu in %B"), | |
3947 | old_bfd, abfd, | |
4ad4eba5 | 3948 | name, (unsigned long) h->size, |
d003868e | 3949 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
3950 | |
3951 | h->size = isym->st_size; | |
3952 | } | |
3953 | ||
3954 | /* If this is a common symbol, then we always want H->SIZE | |
3955 | to be the size of the common symbol. The code just above | |
3956 | won't fix the size if a common symbol becomes larger. We | |
3957 | don't warn about a size change here, because that is | |
3958 | covered by --warn-common. */ | |
3959 | if (h->root.type == bfd_link_hash_common) | |
3960 | h->size = h->root.u.c.size; | |
3961 | ||
3962 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
3963 | && (definition || h->type == STT_NOTYPE)) | |
3964 | { | |
3965 | if (h->type != STT_NOTYPE | |
3966 | && h->type != ELF_ST_TYPE (isym->st_info) | |
3967 | && ! type_change_ok) | |
3968 | (*_bfd_error_handler) | |
d003868e AM |
3969 | (_("Warning: type of symbol `%s' changed" |
3970 | " from %d to %d in %B"), | |
3971 | abfd, name, h->type, ELF_ST_TYPE (isym->st_info)); | |
4ad4eba5 AM |
3972 | |
3973 | h->type = ELF_ST_TYPE (isym->st_info); | |
3974 | } | |
3975 | ||
3976 | /* If st_other has a processor-specific meaning, specific | |
3977 | code might be needed here. We never merge the visibility | |
3978 | attribute with the one from a dynamic object. */ | |
3979 | if (bed->elf_backend_merge_symbol_attribute) | |
3980 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
3981 | dynamic); | |
3982 | ||
b58f81ae DJ |
3983 | /* If this symbol has default visibility and the user has requested |
3984 | we not re-export it, then mark it as hidden. */ | |
3985 | if (definition && !dynamic | |
3986 | && (abfd->no_export | |
3987 | || (abfd->my_archive && abfd->my_archive->no_export)) | |
3988 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) | |
3989 | isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1)); | |
3990 | ||
4ad4eba5 AM |
3991 | if (isym->st_other != 0 && !dynamic) |
3992 | { | |
3993 | unsigned char hvis, symvis, other, nvis; | |
3994 | ||
3995 | /* Take the balance of OTHER from the definition. */ | |
3996 | other = (definition ? isym->st_other : h->other); | |
3997 | other &= ~ ELF_ST_VISIBILITY (-1); | |
3998 | ||
3999 | /* Combine visibilities, using the most constraining one. */ | |
4000 | hvis = ELF_ST_VISIBILITY (h->other); | |
4001 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
4002 | if (! hvis) | |
4003 | nvis = symvis; | |
4004 | else if (! symvis) | |
4005 | nvis = hvis; | |
4006 | else | |
4007 | nvis = hvis < symvis ? hvis : symvis; | |
4008 | ||
4009 | h->other = other | nvis; | |
4010 | } | |
4011 | ||
4012 | /* Set a flag in the hash table entry indicating the type of | |
4013 | reference or definition we just found. Keep a count of | |
4014 | the number of dynamic symbols we find. A dynamic symbol | |
4015 | is one which is referenced or defined by both a regular | |
4016 | object and a shared object. */ | |
4ad4eba5 AM |
4017 | dynsym = FALSE; |
4018 | if (! dynamic) | |
4019 | { | |
4020 | if (! definition) | |
4021 | { | |
f5385ebf | 4022 | h->ref_regular = 1; |
4ad4eba5 | 4023 | if (bind != STB_WEAK) |
f5385ebf | 4024 | h->ref_regular_nonweak = 1; |
4ad4eba5 AM |
4025 | } |
4026 | else | |
f5385ebf | 4027 | h->def_regular = 1; |
4ad4eba5 | 4028 | if (! info->executable |
f5385ebf AM |
4029 | || h->def_dynamic |
4030 | || h->ref_dynamic) | |
4ad4eba5 AM |
4031 | dynsym = TRUE; |
4032 | } | |
4033 | else | |
4034 | { | |
4035 | if (! definition) | |
f5385ebf | 4036 | h->ref_dynamic = 1; |
4ad4eba5 | 4037 | else |
f5385ebf AM |
4038 | h->def_dynamic = 1; |
4039 | if (h->def_regular | |
4040 | || h->ref_regular | |
f6e332e6 | 4041 | || (h->u.weakdef != NULL |
4ad4eba5 | 4042 | && ! new_weakdef |
f6e332e6 | 4043 | && h->u.weakdef->dynindx != -1)) |
4ad4eba5 AM |
4044 | dynsym = TRUE; |
4045 | } | |
4046 | ||
4ad4eba5 AM |
4047 | /* Check to see if we need to add an indirect symbol for |
4048 | the default name. */ | |
4049 | if (definition || h->root.type == bfd_link_hash_common) | |
4050 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4051 | &sec, &value, &dynsym, | |
4052 | override)) | |
4053 | goto error_free_vers; | |
4054 | ||
4055 | if (definition && !dynamic) | |
4056 | { | |
4057 | char *p = strchr (name, ELF_VER_CHR); | |
4058 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4059 | { | |
4060 | /* Queue non-default versions so that .symver x, x@FOO | |
4061 | aliases can be checked. */ | |
4062 | if (! nondeflt_vers) | |
4063 | { | |
4064 | amt = (isymend - isym + 1) | |
4065 | * sizeof (struct elf_link_hash_entry *); | |
4066 | nondeflt_vers = bfd_malloc (amt); | |
4067 | } | |
4068 | nondeflt_vers [nondeflt_vers_cnt++] = h; | |
4069 | } | |
4070 | } | |
4071 | ||
4072 | if (dynsym && h->dynindx == -1) | |
4073 | { | |
c152c796 | 4074 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4075 | goto error_free_vers; |
f6e332e6 | 4076 | if (h->u.weakdef != NULL |
4ad4eba5 | 4077 | && ! new_weakdef |
f6e332e6 | 4078 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 4079 | { |
f6e332e6 | 4080 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
4081 | goto error_free_vers; |
4082 | } | |
4083 | } | |
4084 | else if (dynsym && h->dynindx != -1) | |
4085 | /* If the symbol already has a dynamic index, but | |
4086 | visibility says it should not be visible, turn it into | |
4087 | a local symbol. */ | |
4088 | switch (ELF_ST_VISIBILITY (h->other)) | |
4089 | { | |
4090 | case STV_INTERNAL: | |
4091 | case STV_HIDDEN: | |
4092 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4093 | dynsym = FALSE; | |
4094 | break; | |
4095 | } | |
4096 | ||
4097 | if (!add_needed | |
4098 | && definition | |
4099 | && dynsym | |
f5385ebf | 4100 | && h->ref_regular) |
4ad4eba5 AM |
4101 | { |
4102 | int ret; | |
4103 | const char *soname = elf_dt_name (abfd); | |
4104 | ||
4105 | /* A symbol from a library loaded via DT_NEEDED of some | |
4106 | other library is referenced by a regular object. | |
e56f61be L |
4107 | Add a DT_NEEDED entry for it. Issue an error if |
4108 | --no-add-needed is used. */ | |
4109 | if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) | |
4110 | { | |
4111 | (*_bfd_error_handler) | |
4112 | (_("%s: invalid DSO for symbol `%s' definition"), | |
d003868e | 4113 | abfd, name); |
e56f61be L |
4114 | bfd_set_error (bfd_error_bad_value); |
4115 | goto error_free_vers; | |
4116 | } | |
4117 | ||
a5db907e AM |
4118 | elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED; |
4119 | ||
4ad4eba5 | 4120 | add_needed = TRUE; |
7e9f0867 | 4121 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4122 | if (ret < 0) |
4123 | goto error_free_vers; | |
4124 | ||
4125 | BFD_ASSERT (ret == 0); | |
4126 | } | |
4127 | } | |
4128 | } | |
4129 | ||
4130 | /* Now that all the symbols from this input file are created, handle | |
4131 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
4132 | if (nondeflt_vers != NULL) | |
4133 | { | |
4134 | bfd_size_type cnt, symidx; | |
4135 | ||
4136 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
4137 | { | |
4138 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
4139 | char *shortname, *p; | |
4140 | ||
4141 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
4142 | if (p == NULL | |
4143 | || (h->root.type != bfd_link_hash_defined | |
4144 | && h->root.type != bfd_link_hash_defweak)) | |
4145 | continue; | |
4146 | ||
4147 | amt = p - h->root.root.string; | |
4148 | shortname = bfd_malloc (amt + 1); | |
4149 | memcpy (shortname, h->root.root.string, amt); | |
4150 | shortname[amt] = '\0'; | |
4151 | ||
4152 | hi = (struct elf_link_hash_entry *) | |
4153 | bfd_link_hash_lookup (&hash_table->root, shortname, | |
4154 | FALSE, FALSE, FALSE); | |
4155 | if (hi != NULL | |
4156 | && hi->root.type == h->root.type | |
4157 | && hi->root.u.def.value == h->root.u.def.value | |
4158 | && hi->root.u.def.section == h->root.u.def.section) | |
4159 | { | |
4160 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4161 | hi->root.type = bfd_link_hash_indirect; | |
4162 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
4163 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); | |
4164 | sym_hash = elf_sym_hashes (abfd); | |
4165 | if (sym_hash) | |
4166 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4167 | if (sym_hash[symidx] == hi) | |
4168 | { | |
4169 | sym_hash[symidx] = h; | |
4170 | break; | |
4171 | } | |
4172 | } | |
4173 | free (shortname); | |
4174 | } | |
4175 | free (nondeflt_vers); | |
4176 | nondeflt_vers = NULL; | |
4177 | } | |
4178 | ||
4179 | if (extversym != NULL) | |
4180 | { | |
4181 | free (extversym); | |
4182 | extversym = NULL; | |
4183 | } | |
4184 | ||
4185 | if (isymbuf != NULL) | |
4186 | free (isymbuf); | |
4187 | isymbuf = NULL; | |
4188 | ||
ec13b3bb AM |
4189 | if (!add_needed |
4190 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
77cfaee6 | 4191 | { |
ec13b3bb AM |
4192 | /* Remove symbols defined in an as-needed shared lib that wasn't |
4193 | needed. */ | |
77cfaee6 AM |
4194 | struct elf_smash_syms_data inf; |
4195 | inf.not_needed = abfd; | |
4196 | inf.htab = hash_table; | |
4197 | inf.twiddled = FALSE; | |
4198 | elf_link_hash_traverse (hash_table, elf_smash_syms, &inf); | |
4199 | if (inf.twiddled) | |
4200 | bfd_link_repair_undef_list (&hash_table->root); | |
4201 | weaks = NULL; | |
4202 | } | |
4203 | ||
4ad4eba5 AM |
4204 | /* Now set the weakdefs field correctly for all the weak defined |
4205 | symbols we found. The only way to do this is to search all the | |
4206 | symbols. Since we only need the information for non functions in | |
4207 | dynamic objects, that's the only time we actually put anything on | |
4208 | the list WEAKS. We need this information so that if a regular | |
4209 | object refers to a symbol defined weakly in a dynamic object, the | |
4210 | real symbol in the dynamic object is also put in the dynamic | |
4211 | symbols; we also must arrange for both symbols to point to the | |
4212 | same memory location. We could handle the general case of symbol | |
4213 | aliasing, but a general symbol alias can only be generated in | |
4214 | assembler code, handling it correctly would be very time | |
4215 | consuming, and other ELF linkers don't handle general aliasing | |
4216 | either. */ | |
4217 | if (weaks != NULL) | |
4218 | { | |
4219 | struct elf_link_hash_entry **hpp; | |
4220 | struct elf_link_hash_entry **hppend; | |
4221 | struct elf_link_hash_entry **sorted_sym_hash; | |
4222 | struct elf_link_hash_entry *h; | |
4223 | size_t sym_count; | |
4224 | ||
4225 | /* Since we have to search the whole symbol list for each weak | |
4226 | defined symbol, search time for N weak defined symbols will be | |
4227 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
4228 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
4229 | sorted_sym_hash = bfd_malloc (amt); | |
4230 | if (sorted_sym_hash == NULL) | |
4231 | goto error_return; | |
4232 | sym_hash = sorted_sym_hash; | |
4233 | hpp = elf_sym_hashes (abfd); | |
4234 | hppend = hpp + extsymcount; | |
4235 | sym_count = 0; | |
4236 | for (; hpp < hppend; hpp++) | |
4237 | { | |
4238 | h = *hpp; | |
4239 | if (h != NULL | |
4240 | && h->root.type == bfd_link_hash_defined | |
4241 | && h->type != STT_FUNC) | |
4242 | { | |
4243 | *sym_hash = h; | |
4244 | sym_hash++; | |
4245 | sym_count++; | |
4246 | } | |
4247 | } | |
4248 | ||
4249 | qsort (sorted_sym_hash, sym_count, | |
4250 | sizeof (struct elf_link_hash_entry *), | |
4251 | elf_sort_symbol); | |
4252 | ||
4253 | while (weaks != NULL) | |
4254 | { | |
4255 | struct elf_link_hash_entry *hlook; | |
4256 | asection *slook; | |
4257 | bfd_vma vlook; | |
4258 | long ilook; | |
4259 | size_t i, j, idx; | |
4260 | ||
4261 | hlook = weaks; | |
f6e332e6 AM |
4262 | weaks = hlook->u.weakdef; |
4263 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
4264 | |
4265 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
4266 | || hlook->root.type == bfd_link_hash_defweak | |
4267 | || hlook->root.type == bfd_link_hash_common | |
4268 | || hlook->root.type == bfd_link_hash_indirect); | |
4269 | slook = hlook->root.u.def.section; | |
4270 | vlook = hlook->root.u.def.value; | |
4271 | ||
4272 | ilook = -1; | |
4273 | i = 0; | |
4274 | j = sym_count; | |
4275 | while (i < j) | |
4276 | { | |
4277 | bfd_signed_vma vdiff; | |
4278 | idx = (i + j) / 2; | |
4279 | h = sorted_sym_hash [idx]; | |
4280 | vdiff = vlook - h->root.u.def.value; | |
4281 | if (vdiff < 0) | |
4282 | j = idx; | |
4283 | else if (vdiff > 0) | |
4284 | i = idx + 1; | |
4285 | else | |
4286 | { | |
a9b881be | 4287 | long sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
4288 | if (sdiff < 0) |
4289 | j = idx; | |
4290 | else if (sdiff > 0) | |
4291 | i = idx + 1; | |
4292 | else | |
4293 | { | |
4294 | ilook = idx; | |
4295 | break; | |
4296 | } | |
4297 | } | |
4298 | } | |
4299 | ||
4300 | /* We didn't find a value/section match. */ | |
4301 | if (ilook == -1) | |
4302 | continue; | |
4303 | ||
4304 | for (i = ilook; i < sym_count; i++) | |
4305 | { | |
4306 | h = sorted_sym_hash [i]; | |
4307 | ||
4308 | /* Stop if value or section doesn't match. */ | |
4309 | if (h->root.u.def.value != vlook | |
4310 | || h->root.u.def.section != slook) | |
4311 | break; | |
4312 | else if (h != hlook) | |
4313 | { | |
f6e332e6 | 4314 | hlook->u.weakdef = h; |
4ad4eba5 AM |
4315 | |
4316 | /* If the weak definition is in the list of dynamic | |
4317 | symbols, make sure the real definition is put | |
4318 | there as well. */ | |
4319 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
4320 | { | |
c152c796 | 4321 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 AM |
4322 | goto error_return; |
4323 | } | |
4324 | ||
4325 | /* If the real definition is in the list of dynamic | |
4326 | symbols, make sure the weak definition is put | |
4327 | there as well. If we don't do this, then the | |
4328 | dynamic loader might not merge the entries for the | |
4329 | real definition and the weak definition. */ | |
4330 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
4331 | { | |
c152c796 | 4332 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4ad4eba5 AM |
4333 | goto error_return; |
4334 | } | |
4335 | break; | |
4336 | } | |
4337 | } | |
4338 | } | |
4339 | ||
4340 | free (sorted_sym_hash); | |
4341 | } | |
4342 | ||
85fbca6a NC |
4343 | check_directives = get_elf_backend_data (abfd)->check_directives; |
4344 | if (check_directives) | |
4345 | check_directives (abfd, info); | |
4346 | ||
4ad4eba5 AM |
4347 | /* If this object is the same format as the output object, and it is |
4348 | not a shared library, then let the backend look through the | |
4349 | relocs. | |
4350 | ||
4351 | This is required to build global offset table entries and to | |
4352 | arrange for dynamic relocs. It is not required for the | |
4353 | particular common case of linking non PIC code, even when linking | |
4354 | against shared libraries, but unfortunately there is no way of | |
4355 | knowing whether an object file has been compiled PIC or not. | |
4356 | Looking through the relocs is not particularly time consuming. | |
4357 | The problem is that we must either (1) keep the relocs in memory, | |
4358 | which causes the linker to require additional runtime memory or | |
4359 | (2) read the relocs twice from the input file, which wastes time. | |
4360 | This would be a good case for using mmap. | |
4361 | ||
4362 | I have no idea how to handle linking PIC code into a file of a | |
4363 | different format. It probably can't be done. */ | |
4364 | check_relocs = get_elf_backend_data (abfd)->check_relocs; | |
4365 | if (! dynamic | |
4366 | && is_elf_hash_table (hash_table) | |
4367 | && hash_table->root.creator == abfd->xvec | |
4368 | && check_relocs != NULL) | |
4369 | { | |
4370 | asection *o; | |
4371 | ||
4372 | for (o = abfd->sections; o != NULL; o = o->next) | |
4373 | { | |
4374 | Elf_Internal_Rela *internal_relocs; | |
4375 | bfd_boolean ok; | |
4376 | ||
4377 | if ((o->flags & SEC_RELOC) == 0 | |
4378 | || o->reloc_count == 0 | |
4379 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4380 | && (o->flags & SEC_DEBUGGING) != 0) | |
4381 | || bfd_is_abs_section (o->output_section)) | |
4382 | continue; | |
4383 | ||
4384 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4385 | info->keep_memory); | |
4386 | if (internal_relocs == NULL) | |
4387 | goto error_return; | |
4388 | ||
4389 | ok = (*check_relocs) (abfd, info, o, internal_relocs); | |
4390 | ||
4391 | if (elf_section_data (o)->relocs != internal_relocs) | |
4392 | free (internal_relocs); | |
4393 | ||
4394 | if (! ok) | |
4395 | goto error_return; | |
4396 | } | |
4397 | } | |
4398 | ||
4399 | /* If this is a non-traditional link, try to optimize the handling | |
4400 | of the .stab/.stabstr sections. */ | |
4401 | if (! dynamic | |
4402 | && ! info->traditional_format | |
4403 | && is_elf_hash_table (hash_table) | |
4404 | && (info->strip != strip_all && info->strip != strip_debugger)) | |
4405 | { | |
4406 | asection *stabstr; | |
4407 | ||
4408 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4409 | if (stabstr != NULL) | |
4410 | { | |
4411 | bfd_size_type string_offset = 0; | |
4412 | asection *stab; | |
4413 | ||
4414 | for (stab = abfd->sections; stab; stab = stab->next) | |
4415 | if (strncmp (".stab", stab->name, 5) == 0 | |
4416 | && (!stab->name[5] || | |
4417 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4418 | && (stab->flags & SEC_MERGE) == 0 | |
4419 | && !bfd_is_abs_section (stab->output_section)) | |
4420 | { | |
4421 | struct bfd_elf_section_data *secdata; | |
4422 | ||
4423 | secdata = elf_section_data (stab); | |
4424 | if (! _bfd_link_section_stabs (abfd, | |
3722b82f | 4425 | &hash_table->stab_info, |
4ad4eba5 AM |
4426 | stab, stabstr, |
4427 | &secdata->sec_info, | |
4428 | &string_offset)) | |
4429 | goto error_return; | |
4430 | if (secdata->sec_info) | |
4431 | stab->sec_info_type = ELF_INFO_TYPE_STABS; | |
4432 | } | |
4433 | } | |
4434 | } | |
4435 | ||
77cfaee6 | 4436 | if (is_elf_hash_table (hash_table) && add_needed) |
4ad4eba5 AM |
4437 | { |
4438 | /* Add this bfd to the loaded list. */ | |
4439 | struct elf_link_loaded_list *n; | |
4440 | ||
4441 | n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); | |
4442 | if (n == NULL) | |
4443 | goto error_return; | |
4444 | n->abfd = abfd; | |
4445 | n->next = hash_table->loaded; | |
4446 | hash_table->loaded = n; | |
4447 | } | |
4448 | ||
4449 | return TRUE; | |
4450 | ||
4451 | error_free_vers: | |
4452 | if (nondeflt_vers != NULL) | |
4453 | free (nondeflt_vers); | |
4454 | if (extversym != NULL) | |
4455 | free (extversym); | |
4456 | error_free_sym: | |
4457 | if (isymbuf != NULL) | |
4458 | free (isymbuf); | |
4459 | error_return: | |
4460 | return FALSE; | |
4461 | } | |
4462 | ||
8387904d AM |
4463 | /* Return the linker hash table entry of a symbol that might be |
4464 | satisfied by an archive symbol. Return -1 on error. */ | |
4465 | ||
4466 | struct elf_link_hash_entry * | |
4467 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
4468 | struct bfd_link_info *info, | |
4469 | const char *name) | |
4470 | { | |
4471 | struct elf_link_hash_entry *h; | |
4472 | char *p, *copy; | |
4473 | size_t len, first; | |
4474 | ||
4475 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
4476 | if (h != NULL) | |
4477 | return h; | |
4478 | ||
4479 | /* If this is a default version (the name contains @@), look up the | |
4480 | symbol again with only one `@' as well as without the version. | |
4481 | The effect is that references to the symbol with and without the | |
4482 | version will be matched by the default symbol in the archive. */ | |
4483 | ||
4484 | p = strchr (name, ELF_VER_CHR); | |
4485 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4486 | return h; | |
4487 | ||
4488 | /* First check with only one `@'. */ | |
4489 | len = strlen (name); | |
4490 | copy = bfd_alloc (abfd, len); | |
4491 | if (copy == NULL) | |
4492 | return (struct elf_link_hash_entry *) 0 - 1; | |
4493 | ||
4494 | first = p - name + 1; | |
4495 | memcpy (copy, name, first); | |
4496 | memcpy (copy + first, name + first + 1, len - first); | |
4497 | ||
4498 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE); | |
4499 | if (h == NULL) | |
4500 | { | |
4501 | /* We also need to check references to the symbol without the | |
4502 | version. */ | |
4503 | copy[first - 1] = '\0'; | |
4504 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
4505 | FALSE, FALSE, FALSE); | |
4506 | } | |
4507 | ||
4508 | bfd_release (abfd, copy); | |
4509 | return h; | |
4510 | } | |
4511 | ||
0ad989f9 L |
4512 | /* Add symbols from an ELF archive file to the linker hash table. We |
4513 | don't use _bfd_generic_link_add_archive_symbols because of a | |
4514 | problem which arises on UnixWare. The UnixWare libc.so is an | |
4515 | archive which includes an entry libc.so.1 which defines a bunch of | |
4516 | symbols. The libc.so archive also includes a number of other | |
4517 | object files, which also define symbols, some of which are the same | |
4518 | as those defined in libc.so.1. Correct linking requires that we | |
4519 | consider each object file in turn, and include it if it defines any | |
4520 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
4521 | this; it looks through the list of undefined symbols, and includes | |
4522 | any object file which defines them. When this algorithm is used on | |
4523 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
4524 | bunch of symbols. This means that some of the other objects in the | |
4525 | archive are not included in the link, which is incorrect since they | |
4526 | precede libc.so.1 in the archive. | |
4527 | ||
4528 | Fortunately, ELF archive handling is simpler than that done by | |
4529 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4530 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4531 | symbol is currently undefined, we know that we must pull in that | |
4532 | object file. | |
4533 | ||
4534 | Unfortunately, we do have to make multiple passes over the symbol | |
4535 | table until nothing further is resolved. */ | |
4536 | ||
4ad4eba5 AM |
4537 | static bfd_boolean |
4538 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
4539 | { |
4540 | symindex c; | |
4541 | bfd_boolean *defined = NULL; | |
4542 | bfd_boolean *included = NULL; | |
4543 | carsym *symdefs; | |
4544 | bfd_boolean loop; | |
4545 | bfd_size_type amt; | |
8387904d AM |
4546 | const struct elf_backend_data *bed; |
4547 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
4548 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
4549 | |
4550 | if (! bfd_has_map (abfd)) | |
4551 | { | |
4552 | /* An empty archive is a special case. */ | |
4553 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
4554 | return TRUE; | |
4555 | bfd_set_error (bfd_error_no_armap); | |
4556 | return FALSE; | |
4557 | } | |
4558 | ||
4559 | /* Keep track of all symbols we know to be already defined, and all | |
4560 | files we know to be already included. This is to speed up the | |
4561 | second and subsequent passes. */ | |
4562 | c = bfd_ardata (abfd)->symdef_count; | |
4563 | if (c == 0) | |
4564 | return TRUE; | |
4565 | amt = c; | |
4566 | amt *= sizeof (bfd_boolean); | |
4567 | defined = bfd_zmalloc (amt); | |
4568 | included = bfd_zmalloc (amt); | |
4569 | if (defined == NULL || included == NULL) | |
4570 | goto error_return; | |
4571 | ||
4572 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
4573 | bed = get_elf_backend_data (abfd); |
4574 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
4575 | |
4576 | do | |
4577 | { | |
4578 | file_ptr last; | |
4579 | symindex i; | |
4580 | carsym *symdef; | |
4581 | carsym *symdefend; | |
4582 | ||
4583 | loop = FALSE; | |
4584 | last = -1; | |
4585 | ||
4586 | symdef = symdefs; | |
4587 | symdefend = symdef + c; | |
4588 | for (i = 0; symdef < symdefend; symdef++, i++) | |
4589 | { | |
4590 | struct elf_link_hash_entry *h; | |
4591 | bfd *element; | |
4592 | struct bfd_link_hash_entry *undefs_tail; | |
4593 | symindex mark; | |
4594 | ||
4595 | if (defined[i] || included[i]) | |
4596 | continue; | |
4597 | if (symdef->file_offset == last) | |
4598 | { | |
4599 | included[i] = TRUE; | |
4600 | continue; | |
4601 | } | |
4602 | ||
8387904d AM |
4603 | h = archive_symbol_lookup (abfd, info, symdef->name); |
4604 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
4605 | goto error_return; | |
0ad989f9 L |
4606 | |
4607 | if (h == NULL) | |
4608 | continue; | |
4609 | ||
4610 | if (h->root.type == bfd_link_hash_common) | |
4611 | { | |
4612 | /* We currently have a common symbol. The archive map contains | |
4613 | a reference to this symbol, so we may want to include it. We | |
4614 | only want to include it however, if this archive element | |
4615 | contains a definition of the symbol, not just another common | |
4616 | declaration of it. | |
4617 | ||
4618 | Unfortunately some archivers (including GNU ar) will put | |
4619 | declarations of common symbols into their archive maps, as | |
4620 | well as real definitions, so we cannot just go by the archive | |
4621 | map alone. Instead we must read in the element's symbol | |
4622 | table and check that to see what kind of symbol definition | |
4623 | this is. */ | |
4624 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
4625 | continue; | |
4626 | } | |
4627 | else if (h->root.type != bfd_link_hash_undefined) | |
4628 | { | |
4629 | if (h->root.type != bfd_link_hash_undefweak) | |
4630 | defined[i] = TRUE; | |
4631 | continue; | |
4632 | } | |
4633 | ||
4634 | /* We need to include this archive member. */ | |
4635 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
4636 | if (element == NULL) | |
4637 | goto error_return; | |
4638 | ||
4639 | if (! bfd_check_format (element, bfd_object)) | |
4640 | goto error_return; | |
4641 | ||
4642 | /* Doublecheck that we have not included this object | |
4643 | already--it should be impossible, but there may be | |
4644 | something wrong with the archive. */ | |
4645 | if (element->archive_pass != 0) | |
4646 | { | |
4647 | bfd_set_error (bfd_error_bad_value); | |
4648 | goto error_return; | |
4649 | } | |
4650 | element->archive_pass = 1; | |
4651 | ||
4652 | undefs_tail = info->hash->undefs_tail; | |
4653 | ||
4654 | if (! (*info->callbacks->add_archive_element) (info, element, | |
4655 | symdef->name)) | |
4656 | goto error_return; | |
4657 | if (! bfd_link_add_symbols (element, info)) | |
4658 | goto error_return; | |
4659 | ||
4660 | /* If there are any new undefined symbols, we need to make | |
4661 | another pass through the archive in order to see whether | |
4662 | they can be defined. FIXME: This isn't perfect, because | |
4663 | common symbols wind up on undefs_tail and because an | |
4664 | undefined symbol which is defined later on in this pass | |
4665 | does not require another pass. This isn't a bug, but it | |
4666 | does make the code less efficient than it could be. */ | |
4667 | if (undefs_tail != info->hash->undefs_tail) | |
4668 | loop = TRUE; | |
4669 | ||
4670 | /* Look backward to mark all symbols from this object file | |
4671 | which we have already seen in this pass. */ | |
4672 | mark = i; | |
4673 | do | |
4674 | { | |
4675 | included[mark] = TRUE; | |
4676 | if (mark == 0) | |
4677 | break; | |
4678 | --mark; | |
4679 | } | |
4680 | while (symdefs[mark].file_offset == symdef->file_offset); | |
4681 | ||
4682 | /* We mark subsequent symbols from this object file as we go | |
4683 | on through the loop. */ | |
4684 | last = symdef->file_offset; | |
4685 | } | |
4686 | } | |
4687 | while (loop); | |
4688 | ||
4689 | free (defined); | |
4690 | free (included); | |
4691 | ||
4692 | return TRUE; | |
4693 | ||
4694 | error_return: | |
4695 | if (defined != NULL) | |
4696 | free (defined); | |
4697 | if (included != NULL) | |
4698 | free (included); | |
4699 | return FALSE; | |
4700 | } | |
4ad4eba5 AM |
4701 | |
4702 | /* Given an ELF BFD, add symbols to the global hash table as | |
4703 | appropriate. */ | |
4704 | ||
4705 | bfd_boolean | |
4706 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
4707 | { | |
4708 | switch (bfd_get_format (abfd)) | |
4709 | { | |
4710 | case bfd_object: | |
4711 | return elf_link_add_object_symbols (abfd, info); | |
4712 | case bfd_archive: | |
4713 | return elf_link_add_archive_symbols (abfd, info); | |
4714 | default: | |
4715 | bfd_set_error (bfd_error_wrong_format); | |
4716 | return FALSE; | |
4717 | } | |
4718 | } | |
5a580b3a AM |
4719 | \f |
4720 | /* This function will be called though elf_link_hash_traverse to store | |
4721 | all hash value of the exported symbols in an array. */ | |
4722 | ||
4723 | static bfd_boolean | |
4724 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
4725 | { | |
4726 | unsigned long **valuep = data; | |
4727 | const char *name; | |
4728 | char *p; | |
4729 | unsigned long ha; | |
4730 | char *alc = NULL; | |
4731 | ||
4732 | if (h->root.type == bfd_link_hash_warning) | |
4733 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4734 | ||
4735 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
4736 | if (h->dynindx == -1) | |
4737 | return TRUE; | |
4738 | ||
4739 | name = h->root.root.string; | |
4740 | p = strchr (name, ELF_VER_CHR); | |
4741 | if (p != NULL) | |
4742 | { | |
4743 | alc = bfd_malloc (p - name + 1); | |
4744 | memcpy (alc, name, p - name); | |
4745 | alc[p - name] = '\0'; | |
4746 | name = alc; | |
4747 | } | |
4748 | ||
4749 | /* Compute the hash value. */ | |
4750 | ha = bfd_elf_hash (name); | |
4751 | ||
4752 | /* Store the found hash value in the array given as the argument. */ | |
4753 | *(*valuep)++ = ha; | |
4754 | ||
4755 | /* And store it in the struct so that we can put it in the hash table | |
4756 | later. */ | |
f6e332e6 | 4757 | h->u.elf_hash_value = ha; |
5a580b3a AM |
4758 | |
4759 | if (alc != NULL) | |
4760 | free (alc); | |
4761 | ||
4762 | return TRUE; | |
4763 | } | |
4764 | ||
4765 | /* Array used to determine the number of hash table buckets to use | |
4766 | based on the number of symbols there are. If there are fewer than | |
4767 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
4768 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
4769 | than 32771 buckets. */ | |
4770 | ||
4771 | static const size_t elf_buckets[] = | |
4772 | { | |
4773 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
4774 | 16411, 32771, 0 | |
4775 | }; | |
4776 | ||
4777 | /* Compute bucket count for hashing table. We do not use a static set | |
4778 | of possible tables sizes anymore. Instead we determine for all | |
4779 | possible reasonable sizes of the table the outcome (i.e., the | |
4780 | number of collisions etc) and choose the best solution. The | |
4781 | weighting functions are not too simple to allow the table to grow | |
4782 | without bounds. Instead one of the weighting factors is the size. | |
4783 | Therefore the result is always a good payoff between few collisions | |
4784 | (= short chain lengths) and table size. */ | |
4785 | static size_t | |
4786 | compute_bucket_count (struct bfd_link_info *info) | |
4787 | { | |
4788 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; | |
4789 | size_t best_size = 0; | |
4790 | unsigned long int *hashcodes; | |
4791 | unsigned long int *hashcodesp; | |
4792 | unsigned long int i; | |
4793 | bfd_size_type amt; | |
4794 | ||
4795 | /* Compute the hash values for all exported symbols. At the same | |
4796 | time store the values in an array so that we could use them for | |
4797 | optimizations. */ | |
4798 | amt = dynsymcount; | |
4799 | amt *= sizeof (unsigned long int); | |
4800 | hashcodes = bfd_malloc (amt); | |
4801 | if (hashcodes == NULL) | |
4802 | return 0; | |
4803 | hashcodesp = hashcodes; | |
4804 | ||
4805 | /* Put all hash values in HASHCODES. */ | |
4806 | elf_link_hash_traverse (elf_hash_table (info), | |
4807 | elf_collect_hash_codes, &hashcodesp); | |
4808 | ||
4809 | /* We have a problem here. The following code to optimize the table | |
4810 | size requires an integer type with more the 32 bits. If | |
4811 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
4812 | #ifdef BFD_HOST_U_64_BIT | |
4813 | if (info->optimize) | |
4814 | { | |
4815 | unsigned long int nsyms = hashcodesp - hashcodes; | |
4816 | size_t minsize; | |
4817 | size_t maxsize; | |
4818 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
4819 | unsigned long int *counts ; | |
4820 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
4821 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); | |
4822 | ||
4823 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
4824 | that the hashing table must at least have NSYMS/4 and at most | |
4825 | 2*NSYMS buckets. */ | |
4826 | minsize = nsyms / 4; | |
4827 | if (minsize == 0) | |
4828 | minsize = 1; | |
4829 | best_size = maxsize = nsyms * 2; | |
4830 | ||
4831 | /* Create array where we count the collisions in. We must use bfd_malloc | |
4832 | since the size could be large. */ | |
4833 | amt = maxsize; | |
4834 | amt *= sizeof (unsigned long int); | |
4835 | counts = bfd_malloc (amt); | |
4836 | if (counts == NULL) | |
4837 | { | |
4838 | free (hashcodes); | |
4839 | return 0; | |
4840 | } | |
4841 | ||
4842 | /* Compute the "optimal" size for the hash table. The criteria is a | |
4843 | minimal chain length. The minor criteria is (of course) the size | |
4844 | of the table. */ | |
4845 | for (i = minsize; i < maxsize; ++i) | |
4846 | { | |
4847 | /* Walk through the array of hashcodes and count the collisions. */ | |
4848 | BFD_HOST_U_64_BIT max; | |
4849 | unsigned long int j; | |
4850 | unsigned long int fact; | |
4851 | ||
4852 | memset (counts, '\0', i * sizeof (unsigned long int)); | |
4853 | ||
4854 | /* Determine how often each hash bucket is used. */ | |
4855 | for (j = 0; j < nsyms; ++j) | |
4856 | ++counts[hashcodes[j] % i]; | |
4857 | ||
4858 | /* For the weight function we need some information about the | |
4859 | pagesize on the target. This is information need not be 100% | |
4860 | accurate. Since this information is not available (so far) we | |
4861 | define it here to a reasonable default value. If it is crucial | |
4862 | to have a better value some day simply define this value. */ | |
4863 | # ifndef BFD_TARGET_PAGESIZE | |
4864 | # define BFD_TARGET_PAGESIZE (4096) | |
4865 | # endif | |
4866 | ||
4867 | /* We in any case need 2 + NSYMS entries for the size values and | |
4868 | the chains. */ | |
4869 | max = (2 + nsyms) * (bed->s->arch_size / 8); | |
4870 | ||
4871 | # if 1 | |
4872 | /* Variant 1: optimize for short chains. We add the squares | |
4873 | of all the chain lengths (which favors many small chain | |
4874 | over a few long chains). */ | |
4875 | for (j = 0; j < i; ++j) | |
4876 | max += counts[j] * counts[j]; | |
4877 | ||
4878 | /* This adds penalties for the overall size of the table. */ | |
4879 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
4880 | max *= fact * fact; | |
4881 | # else | |
4882 | /* Variant 2: Optimize a lot more for small table. Here we | |
4883 | also add squares of the size but we also add penalties for | |
4884 | empty slots (the +1 term). */ | |
4885 | for (j = 0; j < i; ++j) | |
4886 | max += (1 + counts[j]) * (1 + counts[j]); | |
4887 | ||
4888 | /* The overall size of the table is considered, but not as | |
4889 | strong as in variant 1, where it is squared. */ | |
4890 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
4891 | max *= fact; | |
4892 | # endif | |
4893 | ||
4894 | /* Compare with current best results. */ | |
4895 | if (max < best_chlen) | |
4896 | { | |
4897 | best_chlen = max; | |
4898 | best_size = i; | |
4899 | } | |
4900 | } | |
4901 | ||
4902 | free (counts); | |
4903 | } | |
4904 | else | |
4905 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
4906 | { | |
4907 | /* This is the fallback solution if no 64bit type is available or if we | |
4908 | are not supposed to spend much time on optimizations. We select the | |
4909 | bucket count using a fixed set of numbers. */ | |
4910 | for (i = 0; elf_buckets[i] != 0; i++) | |
4911 | { | |
4912 | best_size = elf_buckets[i]; | |
4913 | if (dynsymcount < elf_buckets[i + 1]) | |
4914 | break; | |
4915 | } | |
4916 | } | |
4917 | ||
4918 | /* Free the arrays we needed. */ | |
4919 | free (hashcodes); | |
4920 | ||
4921 | return best_size; | |
4922 | } | |
4923 | ||
4924 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
4925 | called by the ELF linker emulation before_allocation routine. We | |
4926 | must set the sizes of the sections before the linker sets the | |
4927 | addresses of the various sections. */ | |
4928 | ||
4929 | bfd_boolean | |
4930 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
4931 | const char *soname, | |
4932 | const char *rpath, | |
4933 | const char *filter_shlib, | |
4934 | const char * const *auxiliary_filters, | |
4935 | struct bfd_link_info *info, | |
4936 | asection **sinterpptr, | |
4937 | struct bfd_elf_version_tree *verdefs) | |
4938 | { | |
4939 | bfd_size_type soname_indx; | |
4940 | bfd *dynobj; | |
4941 | const struct elf_backend_data *bed; | |
4942 | struct elf_assign_sym_version_info asvinfo; | |
4943 | ||
4944 | *sinterpptr = NULL; | |
4945 | ||
4946 | soname_indx = (bfd_size_type) -1; | |
4947 | ||
4948 | if (!is_elf_hash_table (info->hash)) | |
4949 | return TRUE; | |
4950 | ||
8c37241b | 4951 | elf_tdata (output_bfd)->relro = info->relro; |
5a580b3a AM |
4952 | if (info->execstack) |
4953 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
4954 | else if (info->noexecstack) | |
4955 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
4956 | else | |
4957 | { | |
4958 | bfd *inputobj; | |
4959 | asection *notesec = NULL; | |
4960 | int exec = 0; | |
4961 | ||
4962 | for (inputobj = info->input_bfds; | |
4963 | inputobj; | |
4964 | inputobj = inputobj->link_next) | |
4965 | { | |
4966 | asection *s; | |
4967 | ||
d457dcf6 | 4968 | if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED)) |
5a580b3a AM |
4969 | continue; |
4970 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
4971 | if (s) | |
4972 | { | |
4973 | if (s->flags & SEC_CODE) | |
4974 | exec = PF_X; | |
4975 | notesec = s; | |
4976 | } | |
4977 | else | |
4978 | exec = PF_X; | |
4979 | } | |
4980 | if (notesec) | |
4981 | { | |
4982 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
4983 | if (exec && info->relocatable | |
4984 | && notesec->output_section != bfd_abs_section_ptr) | |
4985 | notesec->output_section->flags |= SEC_CODE; | |
4986 | } | |
4987 | } | |
4988 | ||
4989 | /* Any syms created from now on start with -1 in | |
4990 | got.refcount/offset and plt.refcount/offset. */ | |
a6aa5195 AM |
4991 | elf_hash_table (info)->init_got_refcount |
4992 | = elf_hash_table (info)->init_got_offset; | |
4993 | elf_hash_table (info)->init_plt_refcount | |
4994 | = elf_hash_table (info)->init_plt_offset; | |
5a580b3a AM |
4995 | |
4996 | /* The backend may have to create some sections regardless of whether | |
4997 | we're dynamic or not. */ | |
4998 | bed = get_elf_backend_data (output_bfd); | |
4999 | if (bed->elf_backend_always_size_sections | |
5000 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
5001 | return FALSE; | |
5002 | ||
5003 | dynobj = elf_hash_table (info)->dynobj; | |
5004 | ||
5005 | /* If there were no dynamic objects in the link, there is nothing to | |
5006 | do here. */ | |
5007 | if (dynobj == NULL) | |
5008 | return TRUE; | |
5009 | ||
5010 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
5011 | return FALSE; | |
5012 | ||
5013 | if (elf_hash_table (info)->dynamic_sections_created) | |
5014 | { | |
5015 | struct elf_info_failed eif; | |
5016 | struct elf_link_hash_entry *h; | |
5017 | asection *dynstr; | |
5018 | struct bfd_elf_version_tree *t; | |
5019 | struct bfd_elf_version_expr *d; | |
5020 | bfd_boolean all_defined; | |
5021 | ||
5022 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
5023 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
5024 | ||
5025 | if (soname != NULL) | |
5026 | { | |
5027 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5028 | soname, TRUE); | |
5029 | if (soname_indx == (bfd_size_type) -1 | |
5030 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
5031 | return FALSE; | |
5032 | } | |
5033 | ||
5034 | if (info->symbolic) | |
5035 | { | |
5036 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
5037 | return FALSE; | |
5038 | info->flags |= DF_SYMBOLIC; | |
5039 | } | |
5040 | ||
5041 | if (rpath != NULL) | |
5042 | { | |
5043 | bfd_size_type indx; | |
5044 | ||
5045 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
5046 | TRUE); | |
5047 | if (indx == (bfd_size_type) -1 | |
5048 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
5049 | return FALSE; | |
5050 | ||
5051 | if (info->new_dtags) | |
5052 | { | |
5053 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
5054 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
5055 | return FALSE; | |
5056 | } | |
5057 | } | |
5058 | ||
5059 | if (filter_shlib != NULL) | |
5060 | { | |
5061 | bfd_size_type indx; | |
5062 | ||
5063 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5064 | filter_shlib, TRUE); | |
5065 | if (indx == (bfd_size_type) -1 | |
5066 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
5067 | return FALSE; | |
5068 | } | |
5069 | ||
5070 | if (auxiliary_filters != NULL) | |
5071 | { | |
5072 | const char * const *p; | |
5073 | ||
5074 | for (p = auxiliary_filters; *p != NULL; p++) | |
5075 | { | |
5076 | bfd_size_type indx; | |
5077 | ||
5078 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5079 | *p, TRUE); | |
5080 | if (indx == (bfd_size_type) -1 | |
5081 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
5082 | return FALSE; | |
5083 | } | |
5084 | } | |
5085 | ||
5086 | eif.info = info; | |
5087 | eif.verdefs = verdefs; | |
5088 | eif.failed = FALSE; | |
5089 | ||
5090 | /* If we are supposed to export all symbols into the dynamic symbol | |
5091 | table (this is not the normal case), then do so. */ | |
5092 | if (info->export_dynamic) | |
5093 | { | |
5094 | elf_link_hash_traverse (elf_hash_table (info), | |
5095 | _bfd_elf_export_symbol, | |
5096 | &eif); | |
5097 | if (eif.failed) | |
5098 | return FALSE; | |
5099 | } | |
5100 | ||
5101 | /* Make all global versions with definition. */ | |
5102 | for (t = verdefs; t != NULL; t = t->next) | |
5103 | for (d = t->globals.list; d != NULL; d = d->next) | |
5104 | if (!d->symver && d->symbol) | |
5105 | { | |
5106 | const char *verstr, *name; | |
5107 | size_t namelen, verlen, newlen; | |
5108 | char *newname, *p; | |
5109 | struct elf_link_hash_entry *newh; | |
5110 | ||
5111 | name = d->symbol; | |
5112 | namelen = strlen (name); | |
5113 | verstr = t->name; | |
5114 | verlen = strlen (verstr); | |
5115 | newlen = namelen + verlen + 3; | |
5116 | ||
5117 | newname = bfd_malloc (newlen); | |
5118 | if (newname == NULL) | |
5119 | return FALSE; | |
5120 | memcpy (newname, name, namelen); | |
5121 | ||
5122 | /* Check the hidden versioned definition. */ | |
5123 | p = newname + namelen; | |
5124 | *p++ = ELF_VER_CHR; | |
5125 | memcpy (p, verstr, verlen + 1); | |
5126 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5127 | newname, FALSE, FALSE, | |
5128 | FALSE); | |
5129 | if (newh == NULL | |
5130 | || (newh->root.type != bfd_link_hash_defined | |
5131 | && newh->root.type != bfd_link_hash_defweak)) | |
5132 | { | |
5133 | /* Check the default versioned definition. */ | |
5134 | *p++ = ELF_VER_CHR; | |
5135 | memcpy (p, verstr, verlen + 1); | |
5136 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5137 | newname, FALSE, FALSE, | |
5138 | FALSE); | |
5139 | } | |
5140 | free (newname); | |
5141 | ||
5142 | /* Mark this version if there is a definition and it is | |
5143 | not defined in a shared object. */ | |
5144 | if (newh != NULL | |
f5385ebf | 5145 | && !newh->def_dynamic |
5a580b3a AM |
5146 | && (newh->root.type == bfd_link_hash_defined |
5147 | || newh->root.type == bfd_link_hash_defweak)) | |
5148 | d->symver = 1; | |
5149 | } | |
5150 | ||
5151 | /* Attach all the symbols to their version information. */ | |
5152 | asvinfo.output_bfd = output_bfd; | |
5153 | asvinfo.info = info; | |
5154 | asvinfo.verdefs = verdefs; | |
5155 | asvinfo.failed = FALSE; | |
5156 | ||
5157 | elf_link_hash_traverse (elf_hash_table (info), | |
5158 | _bfd_elf_link_assign_sym_version, | |
5159 | &asvinfo); | |
5160 | if (asvinfo.failed) | |
5161 | return FALSE; | |
5162 | ||
5163 | if (!info->allow_undefined_version) | |
5164 | { | |
5165 | /* Check if all global versions have a definition. */ | |
5166 | all_defined = TRUE; | |
5167 | for (t = verdefs; t != NULL; t = t->next) | |
5168 | for (d = t->globals.list; d != NULL; d = d->next) | |
5169 | if (!d->symver && !d->script) | |
5170 | { | |
5171 | (*_bfd_error_handler) | |
5172 | (_("%s: undefined version: %s"), | |
5173 | d->pattern, t->name); | |
5174 | all_defined = FALSE; | |
5175 | } | |
5176 | ||
5177 | if (!all_defined) | |
5178 | { | |
5179 | bfd_set_error (bfd_error_bad_value); | |
5180 | return FALSE; | |
5181 | } | |
5182 | } | |
5183 | ||
5184 | /* Find all symbols which were defined in a dynamic object and make | |
5185 | the backend pick a reasonable value for them. */ | |
5186 | elf_link_hash_traverse (elf_hash_table (info), | |
5187 | _bfd_elf_adjust_dynamic_symbol, | |
5188 | &eif); | |
5189 | if (eif.failed) | |
5190 | return FALSE; | |
5191 | ||
5192 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 5193 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
5194 | now so that we know the final size of the .dynamic section. */ |
5195 | ||
5196 | /* If there are initialization and/or finalization functions to | |
5197 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
5198 | h = (info->init_function | |
5199 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5200 | info->init_function, FALSE, | |
5201 | FALSE, FALSE) | |
5202 | : NULL); | |
5203 | if (h != NULL | |
f5385ebf AM |
5204 | && (h->ref_regular |
5205 | || h->def_regular)) | |
5a580b3a AM |
5206 | { |
5207 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
5208 | return FALSE; | |
5209 | } | |
5210 | h = (info->fini_function | |
5211 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5212 | info->fini_function, FALSE, | |
5213 | FALSE, FALSE) | |
5214 | : NULL); | |
5215 | if (h != NULL | |
f5385ebf AM |
5216 | && (h->ref_regular |
5217 | || h->def_regular)) | |
5a580b3a AM |
5218 | { |
5219 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
5220 | return FALSE; | |
5221 | } | |
5222 | ||
5223 | if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL) | |
5224 | { | |
5225 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
5226 | if (! info->executable) | |
5227 | { | |
5228 | bfd *sub; | |
5229 | asection *o; | |
5230 | ||
5231 | for (sub = info->input_bfds; sub != NULL; | |
5232 | sub = sub->link_next) | |
5233 | for (o = sub->sections; o != NULL; o = o->next) | |
5234 | if (elf_section_data (o)->this_hdr.sh_type | |
5235 | == SHT_PREINIT_ARRAY) | |
5236 | { | |
5237 | (*_bfd_error_handler) | |
d003868e AM |
5238 | (_("%B: .preinit_array section is not allowed in DSO"), |
5239 | sub); | |
5a580b3a AM |
5240 | break; |
5241 | } | |
5242 | ||
5243 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5244 | return FALSE; | |
5245 | } | |
5246 | ||
5247 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
5248 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
5249 | return FALSE; | |
5250 | } | |
5251 | if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL) | |
5252 | { | |
5253 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
5254 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
5255 | return FALSE; | |
5256 | } | |
5257 | if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL) | |
5258 | { | |
5259 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
5260 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
5261 | return FALSE; | |
5262 | } | |
5263 | ||
5264 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5265 | /* If .dynstr is excluded from the link, we don't want any of | |
5266 | these tags. Strictly, we should be checking each section | |
5267 | individually; This quick check covers for the case where | |
5268 | someone does a /DISCARD/ : { *(*) }. */ | |
5269 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
5270 | { | |
5271 | bfd_size_type strsize; | |
5272 | ||
5273 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
5274 | if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0) | |
5275 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
5276 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
5277 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
5278 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
5279 | bed->s->sizeof_sym)) | |
5280 | return FALSE; | |
5281 | } | |
5282 | } | |
5283 | ||
5284 | /* The backend must work out the sizes of all the other dynamic | |
5285 | sections. */ | |
5286 | if (bed->elf_backend_size_dynamic_sections | |
5287 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
5288 | return FALSE; | |
5289 | ||
5290 | if (elf_hash_table (info)->dynamic_sections_created) | |
5291 | { | |
554220db | 5292 | unsigned long section_sym_count; |
5a580b3a | 5293 | asection *s; |
5a580b3a AM |
5294 | |
5295 | /* Set up the version definition section. */ | |
5296 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
5297 | BFD_ASSERT (s != NULL); | |
5298 | ||
5299 | /* We may have created additional version definitions if we are | |
5300 | just linking a regular application. */ | |
5301 | verdefs = asvinfo.verdefs; | |
5302 | ||
5303 | /* Skip anonymous version tag. */ | |
5304 | if (verdefs != NULL && verdefs->vernum == 0) | |
5305 | verdefs = verdefs->next; | |
5306 | ||
3e3b46e5 | 5307 | if (verdefs == NULL && !info->create_default_symver) |
8423293d | 5308 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5309 | else |
5310 | { | |
5311 | unsigned int cdefs; | |
5312 | bfd_size_type size; | |
5313 | struct bfd_elf_version_tree *t; | |
5314 | bfd_byte *p; | |
5315 | Elf_Internal_Verdef def; | |
5316 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
5317 | struct bfd_link_hash_entry *bh; |
5318 | struct elf_link_hash_entry *h; | |
5319 | const char *name; | |
5a580b3a AM |
5320 | |
5321 | cdefs = 0; | |
5322 | size = 0; | |
5323 | ||
5324 | /* Make space for the base version. */ | |
5325 | size += sizeof (Elf_External_Verdef); | |
5326 | size += sizeof (Elf_External_Verdaux); | |
5327 | ++cdefs; | |
5328 | ||
3e3b46e5 PB |
5329 | /* Make space for the default version. */ |
5330 | if (info->create_default_symver) | |
5331 | { | |
5332 | size += sizeof (Elf_External_Verdef); | |
5333 | ++cdefs; | |
5334 | } | |
5335 | ||
5a580b3a AM |
5336 | for (t = verdefs; t != NULL; t = t->next) |
5337 | { | |
5338 | struct bfd_elf_version_deps *n; | |
5339 | ||
5340 | size += sizeof (Elf_External_Verdef); | |
5341 | size += sizeof (Elf_External_Verdaux); | |
5342 | ++cdefs; | |
5343 | ||
5344 | for (n = t->deps; n != NULL; n = n->next) | |
5345 | size += sizeof (Elf_External_Verdaux); | |
5346 | } | |
5347 | ||
eea6121a AM |
5348 | s->size = size; |
5349 | s->contents = bfd_alloc (output_bfd, s->size); | |
5350 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5351 | return FALSE; |
5352 | ||
5353 | /* Fill in the version definition section. */ | |
5354 | ||
5355 | p = s->contents; | |
5356 | ||
5357 | def.vd_version = VER_DEF_CURRENT; | |
5358 | def.vd_flags = VER_FLG_BASE; | |
5359 | def.vd_ndx = 1; | |
5360 | def.vd_cnt = 1; | |
3e3b46e5 PB |
5361 | if (info->create_default_symver) |
5362 | { | |
5363 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
5364 | def.vd_next = sizeof (Elf_External_Verdef); | |
5365 | } | |
5366 | else | |
5367 | { | |
5368 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5369 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5370 | + sizeof (Elf_External_Verdaux)); | |
5371 | } | |
5a580b3a AM |
5372 | |
5373 | if (soname_indx != (bfd_size_type) -1) | |
5374 | { | |
5375 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5376 | soname_indx); | |
5377 | def.vd_hash = bfd_elf_hash (soname); | |
5378 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 5379 | name = soname; |
5a580b3a AM |
5380 | } |
5381 | else | |
5382 | { | |
5a580b3a AM |
5383 | bfd_size_type indx; |
5384 | ||
06084812 | 5385 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
5386 | def.vd_hash = bfd_elf_hash (name); |
5387 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5388 | name, FALSE); | |
5389 | if (indx == (bfd_size_type) -1) | |
5390 | return FALSE; | |
5391 | defaux.vda_name = indx; | |
5392 | } | |
5393 | defaux.vda_next = 0; | |
5394 | ||
5395 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5396 | (Elf_External_Verdef *) p); | |
5397 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
5398 | if (info->create_default_symver) |
5399 | { | |
5400 | /* Add a symbol representing this version. */ | |
5401 | bh = NULL; | |
5402 | if (! (_bfd_generic_link_add_one_symbol | |
5403 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5404 | 0, NULL, FALSE, | |
5405 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5406 | return FALSE; | |
5407 | h = (struct elf_link_hash_entry *) bh; | |
5408 | h->non_elf = 0; | |
5409 | h->def_regular = 1; | |
5410 | h->type = STT_OBJECT; | |
5411 | h->verinfo.vertree = NULL; | |
5412 | ||
5413 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
5414 | return FALSE; | |
5415 | ||
5416 | /* Create a duplicate of the base version with the same | |
5417 | aux block, but different flags. */ | |
5418 | def.vd_flags = 0; | |
5419 | def.vd_ndx = 2; | |
5420 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5421 | if (verdefs) | |
5422 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5423 | + sizeof (Elf_External_Verdaux)); | |
5424 | else | |
5425 | def.vd_next = 0; | |
5426 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5427 | (Elf_External_Verdef *) p); | |
5428 | p += sizeof (Elf_External_Verdef); | |
5429 | } | |
5a580b3a AM |
5430 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
5431 | (Elf_External_Verdaux *) p); | |
5432 | p += sizeof (Elf_External_Verdaux); | |
5433 | ||
5434 | for (t = verdefs; t != NULL; t = t->next) | |
5435 | { | |
5436 | unsigned int cdeps; | |
5437 | struct bfd_elf_version_deps *n; | |
5a580b3a AM |
5438 | |
5439 | cdeps = 0; | |
5440 | for (n = t->deps; n != NULL; n = n->next) | |
5441 | ++cdeps; | |
5442 | ||
5443 | /* Add a symbol representing this version. */ | |
5444 | bh = NULL; | |
5445 | if (! (_bfd_generic_link_add_one_symbol | |
5446 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5447 | 0, NULL, FALSE, | |
5448 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5449 | return FALSE; | |
5450 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
5451 | h->non_elf = 0; |
5452 | h->def_regular = 1; | |
5a580b3a AM |
5453 | h->type = STT_OBJECT; |
5454 | h->verinfo.vertree = t; | |
5455 | ||
c152c796 | 5456 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
5457 | return FALSE; |
5458 | ||
5459 | def.vd_version = VER_DEF_CURRENT; | |
5460 | def.vd_flags = 0; | |
5461 | if (t->globals.list == NULL | |
5462 | && t->locals.list == NULL | |
5463 | && ! t->used) | |
5464 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 5465 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
5466 | def.vd_cnt = cdeps + 1; |
5467 | def.vd_hash = bfd_elf_hash (t->name); | |
5468 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5469 | def.vd_next = 0; | |
5470 | if (t->next != NULL) | |
5471 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5472 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
5473 | ||
5474 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5475 | (Elf_External_Verdef *) p); | |
5476 | p += sizeof (Elf_External_Verdef); | |
5477 | ||
5478 | defaux.vda_name = h->dynstr_index; | |
5479 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5480 | h->dynstr_index); | |
5481 | defaux.vda_next = 0; | |
5482 | if (t->deps != NULL) | |
5483 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5484 | t->name_indx = defaux.vda_name; | |
5485 | ||
5486 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5487 | (Elf_External_Verdaux *) p); | |
5488 | p += sizeof (Elf_External_Verdaux); | |
5489 | ||
5490 | for (n = t->deps; n != NULL; n = n->next) | |
5491 | { | |
5492 | if (n->version_needed == NULL) | |
5493 | { | |
5494 | /* This can happen if there was an error in the | |
5495 | version script. */ | |
5496 | defaux.vda_name = 0; | |
5497 | } | |
5498 | else | |
5499 | { | |
5500 | defaux.vda_name = n->version_needed->name_indx; | |
5501 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5502 | defaux.vda_name); | |
5503 | } | |
5504 | if (n->next == NULL) | |
5505 | defaux.vda_next = 0; | |
5506 | else | |
5507 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5508 | ||
5509 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5510 | (Elf_External_Verdaux *) p); | |
5511 | p += sizeof (Elf_External_Verdaux); | |
5512 | } | |
5513 | } | |
5514 | ||
5515 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
5516 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
5517 | return FALSE; | |
5518 | ||
5519 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
5520 | } | |
5521 | ||
5522 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
5523 | { | |
5524 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
5525 | return FALSE; | |
5526 | } | |
5527 | else if (info->flags & DF_BIND_NOW) | |
5528 | { | |
5529 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
5530 | return FALSE; | |
5531 | } | |
5532 | ||
5533 | if (info->flags_1) | |
5534 | { | |
5535 | if (info->executable) | |
5536 | info->flags_1 &= ~ (DF_1_INITFIRST | |
5537 | | DF_1_NODELETE | |
5538 | | DF_1_NOOPEN); | |
5539 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
5540 | return FALSE; | |
5541 | } | |
5542 | ||
5543 | /* Work out the size of the version reference section. */ | |
5544 | ||
5545 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
5546 | BFD_ASSERT (s != NULL); | |
5547 | { | |
5548 | struct elf_find_verdep_info sinfo; | |
5549 | ||
5550 | sinfo.output_bfd = output_bfd; | |
5551 | sinfo.info = info; | |
5552 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
5553 | if (sinfo.vers == 0) | |
5554 | sinfo.vers = 1; | |
5555 | sinfo.failed = FALSE; | |
5556 | ||
5557 | elf_link_hash_traverse (elf_hash_table (info), | |
5558 | _bfd_elf_link_find_version_dependencies, | |
5559 | &sinfo); | |
5560 | ||
5561 | if (elf_tdata (output_bfd)->verref == NULL) | |
8423293d | 5562 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5563 | else |
5564 | { | |
5565 | Elf_Internal_Verneed *t; | |
5566 | unsigned int size; | |
5567 | unsigned int crefs; | |
5568 | bfd_byte *p; | |
5569 | ||
5570 | /* Build the version definition section. */ | |
5571 | size = 0; | |
5572 | crefs = 0; | |
5573 | for (t = elf_tdata (output_bfd)->verref; | |
5574 | t != NULL; | |
5575 | t = t->vn_nextref) | |
5576 | { | |
5577 | Elf_Internal_Vernaux *a; | |
5578 | ||
5579 | size += sizeof (Elf_External_Verneed); | |
5580 | ++crefs; | |
5581 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5582 | size += sizeof (Elf_External_Vernaux); | |
5583 | } | |
5584 | ||
eea6121a AM |
5585 | s->size = size; |
5586 | s->contents = bfd_alloc (output_bfd, s->size); | |
5a580b3a AM |
5587 | if (s->contents == NULL) |
5588 | return FALSE; | |
5589 | ||
5590 | p = s->contents; | |
5591 | for (t = elf_tdata (output_bfd)->verref; | |
5592 | t != NULL; | |
5593 | t = t->vn_nextref) | |
5594 | { | |
5595 | unsigned int caux; | |
5596 | Elf_Internal_Vernaux *a; | |
5597 | bfd_size_type indx; | |
5598 | ||
5599 | caux = 0; | |
5600 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5601 | ++caux; | |
5602 | ||
5603 | t->vn_version = VER_NEED_CURRENT; | |
5604 | t->vn_cnt = caux; | |
5605 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5606 | elf_dt_name (t->vn_bfd) != NULL | |
5607 | ? elf_dt_name (t->vn_bfd) | |
06084812 | 5608 | : lbasename (t->vn_bfd->filename), |
5a580b3a AM |
5609 | FALSE); |
5610 | if (indx == (bfd_size_type) -1) | |
5611 | return FALSE; | |
5612 | t->vn_file = indx; | |
5613 | t->vn_aux = sizeof (Elf_External_Verneed); | |
5614 | if (t->vn_nextref == NULL) | |
5615 | t->vn_next = 0; | |
5616 | else | |
5617 | t->vn_next = (sizeof (Elf_External_Verneed) | |
5618 | + caux * sizeof (Elf_External_Vernaux)); | |
5619 | ||
5620 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
5621 | (Elf_External_Verneed *) p); | |
5622 | p += sizeof (Elf_External_Verneed); | |
5623 | ||
5624 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5625 | { | |
5626 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
5627 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5628 | a->vna_nodename, FALSE); | |
5629 | if (indx == (bfd_size_type) -1) | |
5630 | return FALSE; | |
5631 | a->vna_name = indx; | |
5632 | if (a->vna_nextptr == NULL) | |
5633 | a->vna_next = 0; | |
5634 | else | |
5635 | a->vna_next = sizeof (Elf_External_Vernaux); | |
5636 | ||
5637 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
5638 | (Elf_External_Vernaux *) p); | |
5639 | p += sizeof (Elf_External_Vernaux); | |
5640 | } | |
5641 | } | |
5642 | ||
5643 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
5644 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
5645 | return FALSE; | |
5646 | ||
5647 | elf_tdata (output_bfd)->cverrefs = crefs; | |
5648 | } | |
5649 | } | |
5650 | ||
8423293d AM |
5651 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
5652 | && elf_tdata (output_bfd)->cverdefs == 0) | |
5653 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
5654 | §ion_sym_count) == 0) | |
5655 | { | |
5656 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
5657 | s->flags |= SEC_EXCLUDE; | |
5658 | } | |
5659 | } | |
5660 | return TRUE; | |
5661 | } | |
5662 | ||
5663 | bfd_boolean | |
5664 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5665 | { | |
5666 | if (!is_elf_hash_table (info->hash)) | |
5667 | return TRUE; | |
5668 | ||
5669 | if (elf_hash_table (info)->dynamic_sections_created) | |
5670 | { | |
5671 | bfd *dynobj; | |
5672 | const struct elf_backend_data *bed; | |
5673 | asection *s; | |
5674 | bfd_size_type dynsymcount; | |
5675 | unsigned long section_sym_count; | |
5676 | size_t bucketcount = 0; | |
5677 | size_t hash_entry_size; | |
5678 | unsigned int dtagcount; | |
5679 | ||
5680 | dynobj = elf_hash_table (info)->dynobj; | |
5681 | ||
5a580b3a AM |
5682 | /* Assign dynsym indicies. In a shared library we generate a |
5683 | section symbol for each output section, which come first. | |
5684 | Next come all of the back-end allocated local dynamic syms, | |
5685 | followed by the rest of the global symbols. */ | |
5686 | ||
554220db AM |
5687 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, |
5688 | §ion_sym_count); | |
5a580b3a AM |
5689 | |
5690 | /* Work out the size of the symbol version section. */ | |
5691 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
5692 | BFD_ASSERT (s != NULL); | |
8423293d AM |
5693 | if (dynsymcount != 0 |
5694 | && (s->flags & SEC_EXCLUDE) == 0) | |
5a580b3a | 5695 | { |
eea6121a AM |
5696 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
5697 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
5698 | if (s->contents == NULL) |
5699 | return FALSE; | |
5700 | ||
5701 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
5702 | return FALSE; | |
5703 | } | |
5704 | ||
5705 | /* Set the size of the .dynsym and .hash sections. We counted | |
5706 | the number of dynamic symbols in elf_link_add_object_symbols. | |
5707 | We will build the contents of .dynsym and .hash when we build | |
5708 | the final symbol table, because until then we do not know the | |
5709 | correct value to give the symbols. We built the .dynstr | |
5710 | section as we went along in elf_link_add_object_symbols. */ | |
5711 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
5712 | BFD_ASSERT (s != NULL); | |
8423293d | 5713 | bed = get_elf_backend_data (output_bfd); |
eea6121a | 5714 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a AM |
5715 | |
5716 | if (dynsymcount != 0) | |
5717 | { | |
554220db AM |
5718 | s->contents = bfd_alloc (output_bfd, s->size); |
5719 | if (s->contents == NULL) | |
5720 | return FALSE; | |
5a580b3a | 5721 | |
554220db AM |
5722 | /* The first entry in .dynsym is a dummy symbol. |
5723 | Clear all the section syms, in case we don't output them all. */ | |
5724 | ++section_sym_count; | |
5725 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a AM |
5726 | } |
5727 | ||
5728 | /* Compute the size of the hashing table. As a side effect this | |
5729 | computes the hash values for all the names we export. */ | |
5730 | bucketcount = compute_bucket_count (info); | |
5731 | ||
5732 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
5733 | BFD_ASSERT (s != NULL); | |
5734 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
eea6121a AM |
5735 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); |
5736 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
5737 | if (s->contents == NULL) |
5738 | return FALSE; | |
5739 | ||
5740 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
5741 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
5742 | s->contents + hash_entry_size); | |
5743 | ||
5744 | elf_hash_table (info)->bucketcount = bucketcount; | |
5745 | ||
5746 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5747 | BFD_ASSERT (s != NULL); | |
5748 | ||
4ad4eba5 | 5749 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 5750 | |
eea6121a | 5751 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
5752 | |
5753 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
5754 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
5755 | return FALSE; | |
5756 | } | |
5757 | ||
5758 | return TRUE; | |
5759 | } | |
c152c796 AM |
5760 | |
5761 | /* Final phase of ELF linker. */ | |
5762 | ||
5763 | /* A structure we use to avoid passing large numbers of arguments. */ | |
5764 | ||
5765 | struct elf_final_link_info | |
5766 | { | |
5767 | /* General link information. */ | |
5768 | struct bfd_link_info *info; | |
5769 | /* Output BFD. */ | |
5770 | bfd *output_bfd; | |
5771 | /* Symbol string table. */ | |
5772 | struct bfd_strtab_hash *symstrtab; | |
5773 | /* .dynsym section. */ | |
5774 | asection *dynsym_sec; | |
5775 | /* .hash section. */ | |
5776 | asection *hash_sec; | |
5777 | /* symbol version section (.gnu.version). */ | |
5778 | asection *symver_sec; | |
5779 | /* Buffer large enough to hold contents of any section. */ | |
5780 | bfd_byte *contents; | |
5781 | /* Buffer large enough to hold external relocs of any section. */ | |
5782 | void *external_relocs; | |
5783 | /* Buffer large enough to hold internal relocs of any section. */ | |
5784 | Elf_Internal_Rela *internal_relocs; | |
5785 | /* Buffer large enough to hold external local symbols of any input | |
5786 | BFD. */ | |
5787 | bfd_byte *external_syms; | |
5788 | /* And a buffer for symbol section indices. */ | |
5789 | Elf_External_Sym_Shndx *locsym_shndx; | |
5790 | /* Buffer large enough to hold internal local symbols of any input | |
5791 | BFD. */ | |
5792 | Elf_Internal_Sym *internal_syms; | |
5793 | /* Array large enough to hold a symbol index for each local symbol | |
5794 | of any input BFD. */ | |
5795 | long *indices; | |
5796 | /* Array large enough to hold a section pointer for each local | |
5797 | symbol of any input BFD. */ | |
5798 | asection **sections; | |
5799 | /* Buffer to hold swapped out symbols. */ | |
5800 | bfd_byte *symbuf; | |
5801 | /* And one for symbol section indices. */ | |
5802 | Elf_External_Sym_Shndx *symshndxbuf; | |
5803 | /* Number of swapped out symbols in buffer. */ | |
5804 | size_t symbuf_count; | |
5805 | /* Number of symbols which fit in symbuf. */ | |
5806 | size_t symbuf_size; | |
5807 | /* And same for symshndxbuf. */ | |
5808 | size_t shndxbuf_size; | |
5809 | }; | |
5810 | ||
5811 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
5812 | ||
5813 | struct elf_outext_info | |
5814 | { | |
5815 | bfd_boolean failed; | |
5816 | bfd_boolean localsyms; | |
5817 | struct elf_final_link_info *finfo; | |
5818 | }; | |
5819 | ||
5820 | /* When performing a relocatable link, the input relocations are | |
5821 | preserved. But, if they reference global symbols, the indices | |
5822 | referenced must be updated. Update all the relocations in | |
5823 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ | |
5824 | ||
5825 | static void | |
5826 | elf_link_adjust_relocs (bfd *abfd, | |
5827 | Elf_Internal_Shdr *rel_hdr, | |
5828 | unsigned int count, | |
5829 | struct elf_link_hash_entry **rel_hash) | |
5830 | { | |
5831 | unsigned int i; | |
5832 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5833 | bfd_byte *erela; | |
5834 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
5835 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
5836 | bfd_vma r_type_mask; | |
5837 | int r_sym_shift; | |
5838 | ||
5839 | if (rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
5840 | { | |
5841 | swap_in = bed->s->swap_reloc_in; | |
5842 | swap_out = bed->s->swap_reloc_out; | |
5843 | } | |
5844 | else if (rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
5845 | { | |
5846 | swap_in = bed->s->swap_reloca_in; | |
5847 | swap_out = bed->s->swap_reloca_out; | |
5848 | } | |
5849 | else | |
5850 | abort (); | |
5851 | ||
5852 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
5853 | abort (); | |
5854 | ||
5855 | if (bed->s->arch_size == 32) | |
5856 | { | |
5857 | r_type_mask = 0xff; | |
5858 | r_sym_shift = 8; | |
5859 | } | |
5860 | else | |
5861 | { | |
5862 | r_type_mask = 0xffffffff; | |
5863 | r_sym_shift = 32; | |
5864 | } | |
5865 | ||
5866 | erela = rel_hdr->contents; | |
5867 | for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) | |
5868 | { | |
5869 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
5870 | unsigned int j; | |
5871 | ||
5872 | if (*rel_hash == NULL) | |
5873 | continue; | |
5874 | ||
5875 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
5876 | ||
5877 | (*swap_in) (abfd, erela, irela); | |
5878 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
5879 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
5880 | | (irela[j].r_info & r_type_mask)); | |
5881 | (*swap_out) (abfd, irela, erela); | |
5882 | } | |
5883 | } | |
5884 | ||
5885 | struct elf_link_sort_rela | |
5886 | { | |
5887 | union { | |
5888 | bfd_vma offset; | |
5889 | bfd_vma sym_mask; | |
5890 | } u; | |
5891 | enum elf_reloc_type_class type; | |
5892 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
5893 | Elf_Internal_Rela rela[1]; | |
5894 | }; | |
5895 | ||
5896 | static int | |
5897 | elf_link_sort_cmp1 (const void *A, const void *B) | |
5898 | { | |
5899 | const struct elf_link_sort_rela *a = A; | |
5900 | const struct elf_link_sort_rela *b = B; | |
5901 | int relativea, relativeb; | |
5902 | ||
5903 | relativea = a->type == reloc_class_relative; | |
5904 | relativeb = b->type == reloc_class_relative; | |
5905 | ||
5906 | if (relativea < relativeb) | |
5907 | return 1; | |
5908 | if (relativea > relativeb) | |
5909 | return -1; | |
5910 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
5911 | return -1; | |
5912 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
5913 | return 1; | |
5914 | if (a->rela->r_offset < b->rela->r_offset) | |
5915 | return -1; | |
5916 | if (a->rela->r_offset > b->rela->r_offset) | |
5917 | return 1; | |
5918 | return 0; | |
5919 | } | |
5920 | ||
5921 | static int | |
5922 | elf_link_sort_cmp2 (const void *A, const void *B) | |
5923 | { | |
5924 | const struct elf_link_sort_rela *a = A; | |
5925 | const struct elf_link_sort_rela *b = B; | |
5926 | int copya, copyb; | |
5927 | ||
5928 | if (a->u.offset < b->u.offset) | |
5929 | return -1; | |
5930 | if (a->u.offset > b->u.offset) | |
5931 | return 1; | |
5932 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); | |
5933 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
5934 | if (copya < copyb) | |
5935 | return -1; | |
5936 | if (copya > copyb) | |
5937 | return 1; | |
5938 | if (a->rela->r_offset < b->rela->r_offset) | |
5939 | return -1; | |
5940 | if (a->rela->r_offset > b->rela->r_offset) | |
5941 | return 1; | |
5942 | return 0; | |
5943 | } | |
5944 | ||
5945 | static size_t | |
5946 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
5947 | { | |
5948 | asection *reldyn; | |
5949 | bfd_size_type count, size; | |
5950 | size_t i, ret, sort_elt, ext_size; | |
5951 | bfd_byte *sort, *s_non_relative, *p; | |
5952 | struct elf_link_sort_rela *sq; | |
5953 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5954 | int i2e = bed->s->int_rels_per_ext_rel; | |
5955 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
5956 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
5957 | struct bfd_link_order *lo; | |
5958 | bfd_vma r_sym_mask; | |
5959 | ||
5960 | reldyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
eea6121a | 5961 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
5962 | { |
5963 | reldyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
eea6121a | 5964 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
5965 | return 0; |
5966 | ext_size = bed->s->sizeof_rel; | |
5967 | swap_in = bed->s->swap_reloc_in; | |
5968 | swap_out = bed->s->swap_reloc_out; | |
5969 | } | |
5970 | else | |
5971 | { | |
5972 | ext_size = bed->s->sizeof_rela; | |
5973 | swap_in = bed->s->swap_reloca_in; | |
5974 | swap_out = bed->s->swap_reloca_out; | |
5975 | } | |
eea6121a | 5976 | count = reldyn->size / ext_size; |
c152c796 AM |
5977 | |
5978 | size = 0; | |
8423293d | 5979 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
5980 | if (lo->type == bfd_indirect_link_order) |
5981 | { | |
5982 | asection *o = lo->u.indirect.section; | |
eea6121a | 5983 | size += o->size; |
c152c796 AM |
5984 | } |
5985 | ||
eea6121a | 5986 | if (size != reldyn->size) |
c152c796 AM |
5987 | return 0; |
5988 | ||
5989 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
5990 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
5991 | sort = bfd_zmalloc (sort_elt * count); | |
5992 | if (sort == NULL) | |
5993 | { | |
5994 | (*info->callbacks->warning) | |
5995 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
5996 | return 0; | |
5997 | } | |
5998 | ||
5999 | if (bed->s->arch_size == 32) | |
6000 | r_sym_mask = ~(bfd_vma) 0xff; | |
6001 | else | |
6002 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
6003 | ||
8423293d | 6004 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
6005 | if (lo->type == bfd_indirect_link_order) |
6006 | { | |
6007 | bfd_byte *erel, *erelend; | |
6008 | asection *o = lo->u.indirect.section; | |
6009 | ||
1da212d6 AM |
6010 | if (o->contents == NULL && o->size != 0) |
6011 | { | |
6012 | /* This is a reloc section that is being handled as a normal | |
6013 | section. See bfd_section_from_shdr. We can't combine | |
6014 | relocs in this case. */ | |
6015 | free (sort); | |
6016 | return 0; | |
6017 | } | |
c152c796 | 6018 | erel = o->contents; |
eea6121a | 6019 | erelend = o->contents + o->size; |
c152c796 AM |
6020 | p = sort + o->output_offset / ext_size * sort_elt; |
6021 | while (erel < erelend) | |
6022 | { | |
6023 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6024 | (*swap_in) (abfd, erel, s->rela); | |
6025 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
6026 | s->u.sym_mask = r_sym_mask; | |
6027 | p += sort_elt; | |
6028 | erel += ext_size; | |
6029 | } | |
6030 | } | |
6031 | ||
6032 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
6033 | ||
6034 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
6035 | { | |
6036 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6037 | if (s->type != reloc_class_relative) | |
6038 | break; | |
6039 | } | |
6040 | ret = i; | |
6041 | s_non_relative = p; | |
6042 | ||
6043 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
6044 | for (; i < count; i++, p += sort_elt) | |
6045 | { | |
6046 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
6047 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
6048 | sq = sp; | |
6049 | sp->u.offset = sq->rela->r_offset; | |
6050 | } | |
6051 | ||
6052 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
6053 | ||
8423293d | 6054 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
6055 | if (lo->type == bfd_indirect_link_order) |
6056 | { | |
6057 | bfd_byte *erel, *erelend; | |
6058 | asection *o = lo->u.indirect.section; | |
6059 | ||
6060 | erel = o->contents; | |
eea6121a | 6061 | erelend = o->contents + o->size; |
c152c796 AM |
6062 | p = sort + o->output_offset / ext_size * sort_elt; |
6063 | while (erel < erelend) | |
6064 | { | |
6065 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6066 | (*swap_out) (abfd, s->rela, erel); | |
6067 | p += sort_elt; | |
6068 | erel += ext_size; | |
6069 | } | |
6070 | } | |
6071 | ||
6072 | free (sort); | |
6073 | *psec = reldyn; | |
6074 | return ret; | |
6075 | } | |
6076 | ||
6077 | /* Flush the output symbols to the file. */ | |
6078 | ||
6079 | static bfd_boolean | |
6080 | elf_link_flush_output_syms (struct elf_final_link_info *finfo, | |
6081 | const struct elf_backend_data *bed) | |
6082 | { | |
6083 | if (finfo->symbuf_count > 0) | |
6084 | { | |
6085 | Elf_Internal_Shdr *hdr; | |
6086 | file_ptr pos; | |
6087 | bfd_size_type amt; | |
6088 | ||
6089 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
6090 | pos = hdr->sh_offset + hdr->sh_size; | |
6091 | amt = finfo->symbuf_count * bed->s->sizeof_sym; | |
6092 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
6093 | || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) | |
6094 | return FALSE; | |
6095 | ||
6096 | hdr->sh_size += amt; | |
6097 | finfo->symbuf_count = 0; | |
6098 | } | |
6099 | ||
6100 | return TRUE; | |
6101 | } | |
6102 | ||
6103 | /* Add a symbol to the output symbol table. */ | |
6104 | ||
6105 | static bfd_boolean | |
6106 | elf_link_output_sym (struct elf_final_link_info *finfo, | |
6107 | const char *name, | |
6108 | Elf_Internal_Sym *elfsym, | |
6109 | asection *input_sec, | |
6110 | struct elf_link_hash_entry *h) | |
6111 | { | |
6112 | bfd_byte *dest; | |
6113 | Elf_External_Sym_Shndx *destshndx; | |
6114 | bfd_boolean (*output_symbol_hook) | |
6115 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, | |
6116 | struct elf_link_hash_entry *); | |
6117 | const struct elf_backend_data *bed; | |
6118 | ||
6119 | bed = get_elf_backend_data (finfo->output_bfd); | |
6120 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; | |
6121 | if (output_symbol_hook != NULL) | |
6122 | { | |
6123 | if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h)) | |
6124 | return FALSE; | |
6125 | } | |
6126 | ||
6127 | if (name == NULL || *name == '\0') | |
6128 | elfsym->st_name = 0; | |
6129 | else if (input_sec->flags & SEC_EXCLUDE) | |
6130 | elfsym->st_name = 0; | |
6131 | else | |
6132 | { | |
6133 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
6134 | name, TRUE, FALSE); | |
6135 | if (elfsym->st_name == (unsigned long) -1) | |
6136 | return FALSE; | |
6137 | } | |
6138 | ||
6139 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
6140 | { | |
6141 | if (! elf_link_flush_output_syms (finfo, bed)) | |
6142 | return FALSE; | |
6143 | } | |
6144 | ||
6145 | dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; | |
6146 | destshndx = finfo->symshndxbuf; | |
6147 | if (destshndx != NULL) | |
6148 | { | |
6149 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
6150 | { | |
6151 | bfd_size_type amt; | |
6152 | ||
6153 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); | |
6154 | finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2); | |
6155 | if (destshndx == NULL) | |
6156 | return FALSE; | |
6157 | memset ((char *) destshndx + amt, 0, amt); | |
6158 | finfo->shndxbuf_size *= 2; | |
6159 | } | |
6160 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
6161 | } | |
6162 | ||
6163 | bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); | |
6164 | finfo->symbuf_count += 1; | |
6165 | bfd_get_symcount (finfo->output_bfd) += 1; | |
6166 | ||
6167 | return TRUE; | |
6168 | } | |
6169 | ||
6170 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in | |
6171 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
6172 | versioned symbol that would normally require an explicit version. | |
6173 | We also handle the case that a DSO references a hidden symbol | |
6174 | which may be satisfied by a versioned symbol in another DSO. */ | |
6175 | ||
6176 | static bfd_boolean | |
6177 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
6178 | const struct elf_backend_data *bed, | |
6179 | struct elf_link_hash_entry *h) | |
6180 | { | |
6181 | bfd *abfd; | |
6182 | struct elf_link_loaded_list *loaded; | |
6183 | ||
6184 | if (!is_elf_hash_table (info->hash)) | |
6185 | return FALSE; | |
6186 | ||
6187 | switch (h->root.type) | |
6188 | { | |
6189 | default: | |
6190 | abfd = NULL; | |
6191 | break; | |
6192 | ||
6193 | case bfd_link_hash_undefined: | |
6194 | case bfd_link_hash_undefweak: | |
6195 | abfd = h->root.u.undef.abfd; | |
6196 | if ((abfd->flags & DYNAMIC) == 0 | |
e56f61be | 6197 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
6198 | return FALSE; |
6199 | break; | |
6200 | ||
6201 | case bfd_link_hash_defined: | |
6202 | case bfd_link_hash_defweak: | |
6203 | abfd = h->root.u.def.section->owner; | |
6204 | break; | |
6205 | ||
6206 | case bfd_link_hash_common: | |
6207 | abfd = h->root.u.c.p->section->owner; | |
6208 | break; | |
6209 | } | |
6210 | BFD_ASSERT (abfd != NULL); | |
6211 | ||
6212 | for (loaded = elf_hash_table (info)->loaded; | |
6213 | loaded != NULL; | |
6214 | loaded = loaded->next) | |
6215 | { | |
6216 | bfd *input; | |
6217 | Elf_Internal_Shdr *hdr; | |
6218 | bfd_size_type symcount; | |
6219 | bfd_size_type extsymcount; | |
6220 | bfd_size_type extsymoff; | |
6221 | Elf_Internal_Shdr *versymhdr; | |
6222 | Elf_Internal_Sym *isym; | |
6223 | Elf_Internal_Sym *isymend; | |
6224 | Elf_Internal_Sym *isymbuf; | |
6225 | Elf_External_Versym *ever; | |
6226 | Elf_External_Versym *extversym; | |
6227 | ||
6228 | input = loaded->abfd; | |
6229 | ||
6230 | /* We check each DSO for a possible hidden versioned definition. */ | |
6231 | if (input == abfd | |
6232 | || (input->flags & DYNAMIC) == 0 | |
6233 | || elf_dynversym (input) == 0) | |
6234 | continue; | |
6235 | ||
6236 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
6237 | ||
6238 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
6239 | if (elf_bad_symtab (input)) | |
6240 | { | |
6241 | extsymcount = symcount; | |
6242 | extsymoff = 0; | |
6243 | } | |
6244 | else | |
6245 | { | |
6246 | extsymcount = symcount - hdr->sh_info; | |
6247 | extsymoff = hdr->sh_info; | |
6248 | } | |
6249 | ||
6250 | if (extsymcount == 0) | |
6251 | continue; | |
6252 | ||
6253 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
6254 | NULL, NULL, NULL); | |
6255 | if (isymbuf == NULL) | |
6256 | return FALSE; | |
6257 | ||
6258 | /* Read in any version definitions. */ | |
6259 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
6260 | extversym = bfd_malloc (versymhdr->sh_size); | |
6261 | if (extversym == NULL) | |
6262 | goto error_ret; | |
6263 | ||
6264 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
6265 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
6266 | != versymhdr->sh_size)) | |
6267 | { | |
6268 | free (extversym); | |
6269 | error_ret: | |
6270 | free (isymbuf); | |
6271 | return FALSE; | |
6272 | } | |
6273 | ||
6274 | ever = extversym + extsymoff; | |
6275 | isymend = isymbuf + extsymcount; | |
6276 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
6277 | { | |
6278 | const char *name; | |
6279 | Elf_Internal_Versym iver; | |
6280 | unsigned short version_index; | |
6281 | ||
6282 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
6283 | || isym->st_shndx == SHN_UNDEF) | |
6284 | continue; | |
6285 | ||
6286 | name = bfd_elf_string_from_elf_section (input, | |
6287 | hdr->sh_link, | |
6288 | isym->st_name); | |
6289 | if (strcmp (name, h->root.root.string) != 0) | |
6290 | continue; | |
6291 | ||
6292 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
6293 | ||
6294 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
6295 | { | |
6296 | /* If we have a non-hidden versioned sym, then it should | |
6297 | have provided a definition for the undefined sym. */ | |
6298 | abort (); | |
6299 | } | |
6300 | ||
6301 | version_index = iver.vs_vers & VERSYM_VERSION; | |
6302 | if (version_index == 1 || version_index == 2) | |
6303 | { | |
6304 | /* This is the base or first version. We can use it. */ | |
6305 | free (extversym); | |
6306 | free (isymbuf); | |
6307 | return TRUE; | |
6308 | } | |
6309 | } | |
6310 | ||
6311 | free (extversym); | |
6312 | free (isymbuf); | |
6313 | } | |
6314 | ||
6315 | return FALSE; | |
6316 | } | |
6317 | ||
6318 | /* Add an external symbol to the symbol table. This is called from | |
6319 | the hash table traversal routine. When generating a shared object, | |
6320 | we go through the symbol table twice. The first time we output | |
6321 | anything that might have been forced to local scope in a version | |
6322 | script. The second time we output the symbols that are still | |
6323 | global symbols. */ | |
6324 | ||
6325 | static bfd_boolean | |
6326 | elf_link_output_extsym (struct elf_link_hash_entry *h, void *data) | |
6327 | { | |
6328 | struct elf_outext_info *eoinfo = data; | |
6329 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
6330 | bfd_boolean strip; | |
6331 | Elf_Internal_Sym sym; | |
6332 | asection *input_sec; | |
6333 | const struct elf_backend_data *bed; | |
6334 | ||
6335 | if (h->root.type == bfd_link_hash_warning) | |
6336 | { | |
6337 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6338 | if (h->root.type == bfd_link_hash_new) | |
6339 | return TRUE; | |
6340 | } | |
6341 | ||
6342 | /* Decide whether to output this symbol in this pass. */ | |
6343 | if (eoinfo->localsyms) | |
6344 | { | |
f5385ebf | 6345 | if (!h->forced_local) |
c152c796 AM |
6346 | return TRUE; |
6347 | } | |
6348 | else | |
6349 | { | |
f5385ebf | 6350 | if (h->forced_local) |
c152c796 AM |
6351 | return TRUE; |
6352 | } | |
6353 | ||
6354 | bed = get_elf_backend_data (finfo->output_bfd); | |
6355 | ||
6356 | /* If we have an undefined symbol reference here then it must have | |
6357 | come from a shared library that is being linked in. (Undefined | |
6358 | references in regular files have already been handled). If we | |
6359 | are reporting errors for this situation then do so now. */ | |
6360 | if (h->root.type == bfd_link_hash_undefined | |
f5385ebf AM |
6361 | && h->ref_dynamic |
6362 | && !h->ref_regular | |
c152c796 AM |
6363 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h) |
6364 | && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
6365 | { | |
6366 | if (! ((*finfo->info->callbacks->undefined_symbol) | |
6367 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
6368 | NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) | |
6369 | { | |
6370 | eoinfo->failed = TRUE; | |
6371 | return FALSE; | |
6372 | } | |
6373 | } | |
6374 | ||
6375 | /* We should also warn if a forced local symbol is referenced from | |
6376 | shared libraries. */ | |
6377 | if (! finfo->info->relocatable | |
6378 | && (! finfo->info->shared) | |
f5385ebf AM |
6379 | && h->forced_local |
6380 | && h->ref_dynamic | |
6381 | && !h->dynamic_def | |
6382 | && !h->dynamic_weak | |
c152c796 AM |
6383 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) |
6384 | { | |
6385 | (*_bfd_error_handler) | |
d003868e | 6386 | (_("%B: %s symbol `%s' in %B is referenced by DSO"), |
cfca085c L |
6387 | finfo->output_bfd, |
6388 | h->root.u.def.section == bfd_abs_section_ptr | |
6389 | ? finfo->output_bfd : h->root.u.def.section->owner, | |
c152c796 AM |
6390 | ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
6391 | ? "internal" | |
6392 | : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
d003868e AM |
6393 | ? "hidden" : "local", |
6394 | h->root.root.string); | |
c152c796 AM |
6395 | eoinfo->failed = TRUE; |
6396 | return FALSE; | |
6397 | } | |
6398 | ||
6399 | /* We don't want to output symbols that have never been mentioned by | |
6400 | a regular file, or that we have been told to strip. However, if | |
6401 | h->indx is set to -2, the symbol is used by a reloc and we must | |
6402 | output it. */ | |
6403 | if (h->indx == -2) | |
6404 | strip = FALSE; | |
f5385ebf | 6405 | else if ((h->def_dynamic |
77cfaee6 AM |
6406 | || h->ref_dynamic |
6407 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
6408 | && !h->def_regular |
6409 | && !h->ref_regular) | |
c152c796 AM |
6410 | strip = TRUE; |
6411 | else if (finfo->info->strip == strip_all) | |
6412 | strip = TRUE; | |
6413 | else if (finfo->info->strip == strip_some | |
6414 | && bfd_hash_lookup (finfo->info->keep_hash, | |
6415 | h->root.root.string, FALSE, FALSE) == NULL) | |
6416 | strip = TRUE; | |
6417 | else if (finfo->info->strip_discarded | |
6418 | && (h->root.type == bfd_link_hash_defined | |
6419 | || h->root.type == bfd_link_hash_defweak) | |
6420 | && elf_discarded_section (h->root.u.def.section)) | |
6421 | strip = TRUE; | |
6422 | else | |
6423 | strip = FALSE; | |
6424 | ||
6425 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
6426 | nothing else to do unless it is a forced local symbol. */ | |
6427 | if (strip | |
6428 | && h->dynindx == -1 | |
f5385ebf | 6429 | && !h->forced_local) |
c152c796 AM |
6430 | return TRUE; |
6431 | ||
6432 | sym.st_value = 0; | |
6433 | sym.st_size = h->size; | |
6434 | sym.st_other = h->other; | |
f5385ebf | 6435 | if (h->forced_local) |
c152c796 AM |
6436 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); |
6437 | else if (h->root.type == bfd_link_hash_undefweak | |
6438 | || h->root.type == bfd_link_hash_defweak) | |
6439 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
6440 | else | |
6441 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
6442 | ||
6443 | switch (h->root.type) | |
6444 | { | |
6445 | default: | |
6446 | case bfd_link_hash_new: | |
6447 | case bfd_link_hash_warning: | |
6448 | abort (); | |
6449 | return FALSE; | |
6450 | ||
6451 | case bfd_link_hash_undefined: | |
6452 | case bfd_link_hash_undefweak: | |
6453 | input_sec = bfd_und_section_ptr; | |
6454 | sym.st_shndx = SHN_UNDEF; | |
6455 | break; | |
6456 | ||
6457 | case bfd_link_hash_defined: | |
6458 | case bfd_link_hash_defweak: | |
6459 | { | |
6460 | input_sec = h->root.u.def.section; | |
6461 | if (input_sec->output_section != NULL) | |
6462 | { | |
6463 | sym.st_shndx = | |
6464 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
6465 | input_sec->output_section); | |
6466 | if (sym.st_shndx == SHN_BAD) | |
6467 | { | |
6468 | (*_bfd_error_handler) | |
d003868e AM |
6469 | (_("%B: could not find output section %A for input section %A"), |
6470 | finfo->output_bfd, input_sec->output_section, input_sec); | |
c152c796 AM |
6471 | eoinfo->failed = TRUE; |
6472 | return FALSE; | |
6473 | } | |
6474 | ||
6475 | /* ELF symbols in relocatable files are section relative, | |
6476 | but in nonrelocatable files they are virtual | |
6477 | addresses. */ | |
6478 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
6479 | if (! finfo->info->relocatable) | |
6480 | { | |
6481 | sym.st_value += input_sec->output_section->vma; | |
6482 | if (h->type == STT_TLS) | |
6483 | { | |
6484 | /* STT_TLS symbols are relative to PT_TLS segment | |
6485 | base. */ | |
6486 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
6487 | sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
6488 | } | |
6489 | } | |
6490 | } | |
6491 | else | |
6492 | { | |
6493 | BFD_ASSERT (input_sec->owner == NULL | |
6494 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
6495 | sym.st_shndx = SHN_UNDEF; | |
6496 | input_sec = bfd_und_section_ptr; | |
6497 | } | |
6498 | } | |
6499 | break; | |
6500 | ||
6501 | case bfd_link_hash_common: | |
6502 | input_sec = h->root.u.c.p->section; | |
6503 | sym.st_shndx = SHN_COMMON; | |
6504 | sym.st_value = 1 << h->root.u.c.p->alignment_power; | |
6505 | break; | |
6506 | ||
6507 | case bfd_link_hash_indirect: | |
6508 | /* These symbols are created by symbol versioning. They point | |
6509 | to the decorated version of the name. For example, if the | |
6510 | symbol foo@@GNU_1.2 is the default, which should be used when | |
6511 | foo is used with no version, then we add an indirect symbol | |
6512 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
6513 | since the indirected symbol is already in the hash table. */ | |
6514 | return TRUE; | |
6515 | } | |
6516 | ||
6517 | /* Give the processor backend a chance to tweak the symbol value, | |
6518 | and also to finish up anything that needs to be done for this | |
6519 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
6520 | forced local syms when non-shared is due to a historical quirk. */ | |
6521 | if ((h->dynindx != -1 | |
f5385ebf | 6522 | || h->forced_local) |
c152c796 AM |
6523 | && ((finfo->info->shared |
6524 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
6525 | || h->root.type != bfd_link_hash_undefweak)) | |
f5385ebf | 6526 | || !h->forced_local) |
c152c796 AM |
6527 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
6528 | { | |
6529 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
6530 | (finfo->output_bfd, finfo->info, h, &sym))) | |
6531 | { | |
6532 | eoinfo->failed = TRUE; | |
6533 | return FALSE; | |
6534 | } | |
6535 | } | |
6536 | ||
6537 | /* If we are marking the symbol as undefined, and there are no | |
6538 | non-weak references to this symbol from a regular object, then | |
6539 | mark the symbol as weak undefined; if there are non-weak | |
6540 | references, mark the symbol as strong. We can't do this earlier, | |
6541 | because it might not be marked as undefined until the | |
6542 | finish_dynamic_symbol routine gets through with it. */ | |
6543 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 6544 | && h->ref_regular |
c152c796 AM |
6545 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
6546 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
6547 | { | |
6548 | int bindtype; | |
6549 | ||
f5385ebf | 6550 | if (h->ref_regular_nonweak) |
c152c796 AM |
6551 | bindtype = STB_GLOBAL; |
6552 | else | |
6553 | bindtype = STB_WEAK; | |
6554 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); | |
6555 | } | |
6556 | ||
6557 | /* If a non-weak symbol with non-default visibility is not defined | |
6558 | locally, it is a fatal error. */ | |
6559 | if (! finfo->info->relocatable | |
6560 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT | |
6561 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
6562 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 6563 | && !h->def_regular) |
c152c796 AM |
6564 | { |
6565 | (*_bfd_error_handler) | |
d003868e AM |
6566 | (_("%B: %s symbol `%s' isn't defined"), |
6567 | finfo->output_bfd, | |
6568 | ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED | |
6569 | ? "protected" | |
6570 | : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL | |
6571 | ? "internal" : "hidden", | |
6572 | h->root.root.string); | |
c152c796 AM |
6573 | eoinfo->failed = TRUE; |
6574 | return FALSE; | |
6575 | } | |
6576 | ||
6577 | /* If this symbol should be put in the .dynsym section, then put it | |
6578 | there now. We already know the symbol index. We also fill in | |
6579 | the entry in the .hash section. */ | |
6580 | if (h->dynindx != -1 | |
6581 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
6582 | { | |
6583 | size_t bucketcount; | |
6584 | size_t bucket; | |
6585 | size_t hash_entry_size; | |
6586 | bfd_byte *bucketpos; | |
6587 | bfd_vma chain; | |
6588 | bfd_byte *esym; | |
6589 | ||
6590 | sym.st_name = h->dynstr_index; | |
6591 | esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; | |
6592 | bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); | |
6593 | ||
6594 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
f6e332e6 | 6595 | bucket = h->u.elf_hash_value % bucketcount; |
c152c796 AM |
6596 | hash_entry_size |
6597 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; | |
6598 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents | |
6599 | + (bucket + 2) * hash_entry_size); | |
6600 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
6601 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); | |
6602 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, | |
6603 | ((bfd_byte *) finfo->hash_sec->contents | |
6604 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
6605 | ||
6606 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
6607 | { | |
6608 | Elf_Internal_Versym iversym; | |
6609 | Elf_External_Versym *eversym; | |
6610 | ||
f5385ebf | 6611 | if (!h->def_regular) |
c152c796 AM |
6612 | { |
6613 | if (h->verinfo.verdef == NULL) | |
6614 | iversym.vs_vers = 0; | |
6615 | else | |
6616 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
6617 | } | |
6618 | else | |
6619 | { | |
6620 | if (h->verinfo.vertree == NULL) | |
6621 | iversym.vs_vers = 1; | |
6622 | else | |
6623 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
3e3b46e5 PB |
6624 | if (finfo->info->create_default_symver) |
6625 | iversym.vs_vers++; | |
c152c796 AM |
6626 | } |
6627 | ||
f5385ebf | 6628 | if (h->hidden) |
c152c796 AM |
6629 | iversym.vs_vers |= VERSYM_HIDDEN; |
6630 | ||
6631 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; | |
6632 | eversym += h->dynindx; | |
6633 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
6634 | } | |
6635 | } | |
6636 | ||
6637 | /* If we're stripping it, then it was just a dynamic symbol, and | |
6638 | there's nothing else to do. */ | |
6639 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) | |
6640 | return TRUE; | |
6641 | ||
6642 | h->indx = bfd_get_symcount (finfo->output_bfd); | |
6643 | ||
6644 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h)) | |
6645 | { | |
6646 | eoinfo->failed = TRUE; | |
6647 | return FALSE; | |
6648 | } | |
6649 | ||
6650 | return TRUE; | |
6651 | } | |
6652 | ||
cdd3575c AM |
6653 | /* Return TRUE if special handling is done for relocs in SEC against |
6654 | symbols defined in discarded sections. */ | |
6655 | ||
c152c796 AM |
6656 | static bfd_boolean |
6657 | elf_section_ignore_discarded_relocs (asection *sec) | |
6658 | { | |
6659 | const struct elf_backend_data *bed; | |
6660 | ||
cdd3575c AM |
6661 | switch (sec->sec_info_type) |
6662 | { | |
6663 | case ELF_INFO_TYPE_STABS: | |
6664 | case ELF_INFO_TYPE_EH_FRAME: | |
6665 | return TRUE; | |
6666 | default: | |
6667 | break; | |
6668 | } | |
c152c796 AM |
6669 | |
6670 | bed = get_elf_backend_data (sec->owner); | |
6671 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
6672 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
6673 | return TRUE; | |
6674 | ||
6675 | return FALSE; | |
6676 | } | |
6677 | ||
9e66c942 AM |
6678 | enum action_discarded |
6679 | { | |
6680 | COMPLAIN = 1, | |
6681 | PRETEND = 2 | |
6682 | }; | |
6683 | ||
6684 | /* Return a mask saying how ld should treat relocations in SEC against | |
6685 | symbols defined in discarded sections. If this function returns | |
6686 | COMPLAIN set, ld will issue a warning message. If this function | |
6687 | returns PRETEND set, and the discarded section was link-once and the | |
6688 | same size as the kept link-once section, ld will pretend that the | |
6689 | symbol was actually defined in the kept section. Otherwise ld will | |
6690 | zero the reloc (at least that is the intent, but some cooperation by | |
6691 | the target dependent code is needed, particularly for REL targets). */ | |
6692 | ||
6693 | static unsigned int | |
6694 | elf_action_discarded (asection *sec) | |
cdd3575c | 6695 | { |
9e66c942 AM |
6696 | if (sec->flags & SEC_DEBUGGING) |
6697 | return PRETEND; | |
cdd3575c AM |
6698 | |
6699 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 6700 | return 0; |
cdd3575c AM |
6701 | |
6702 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 6703 | return 0; |
cdd3575c | 6704 | |
27b56da8 | 6705 | if (strcmp (".PARISC.unwind", sec->name) == 0) |
9e66c942 | 6706 | return 0; |
327c1315 AM |
6707 | |
6708 | if (strcmp (".fixup", sec->name) == 0) | |
9e66c942 | 6709 | return 0; |
27b56da8 | 6710 | |
9e66c942 | 6711 | return COMPLAIN | PRETEND; |
cdd3575c AM |
6712 | } |
6713 | ||
3d7f7666 L |
6714 | /* Find a match between a section and a member of a section group. */ |
6715 | ||
6716 | static asection * | |
6717 | match_group_member (asection *sec, asection *group) | |
6718 | { | |
6719 | asection *first = elf_next_in_group (group); | |
6720 | asection *s = first; | |
6721 | ||
6722 | while (s != NULL) | |
6723 | { | |
6724 | if (bfd_elf_match_symbols_in_sections (s, sec)) | |
6725 | return s; | |
6726 | ||
6727 | if (s == first) | |
6728 | break; | |
6729 | } | |
6730 | ||
6731 | return NULL; | |
6732 | } | |
6733 | ||
01b3c8ab L |
6734 | /* Check if the kept section of a discarded section SEC can be used |
6735 | to replace it. Return the replacement if it is OK. Otherwise return | |
6736 | NULL. */ | |
6737 | ||
6738 | asection * | |
6739 | _bfd_elf_check_kept_section (asection *sec) | |
6740 | { | |
6741 | asection *kept; | |
6742 | ||
6743 | kept = sec->kept_section; | |
6744 | if (kept != NULL) | |
6745 | { | |
6746 | if (elf_sec_group (sec) != NULL) | |
6747 | kept = match_group_member (sec, kept); | |
6748 | if (kept != NULL && sec->size != kept->size) | |
6749 | kept = NULL; | |
6750 | } | |
6751 | return kept; | |
6752 | } | |
6753 | ||
c152c796 AM |
6754 | /* Link an input file into the linker output file. This function |
6755 | handles all the sections and relocations of the input file at once. | |
6756 | This is so that we only have to read the local symbols once, and | |
6757 | don't have to keep them in memory. */ | |
6758 | ||
6759 | static bfd_boolean | |
6760 | elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) | |
6761 | { | |
6762 | bfd_boolean (*relocate_section) | |
6763 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
6764 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
6765 | bfd *output_bfd; | |
6766 | Elf_Internal_Shdr *symtab_hdr; | |
6767 | size_t locsymcount; | |
6768 | size_t extsymoff; | |
6769 | Elf_Internal_Sym *isymbuf; | |
6770 | Elf_Internal_Sym *isym; | |
6771 | Elf_Internal_Sym *isymend; | |
6772 | long *pindex; | |
6773 | asection **ppsection; | |
6774 | asection *o; | |
6775 | const struct elf_backend_data *bed; | |
6776 | bfd_boolean emit_relocs; | |
6777 | struct elf_link_hash_entry **sym_hashes; | |
6778 | ||
6779 | output_bfd = finfo->output_bfd; | |
6780 | bed = get_elf_backend_data (output_bfd); | |
6781 | relocate_section = bed->elf_backend_relocate_section; | |
6782 | ||
6783 | /* If this is a dynamic object, we don't want to do anything here: | |
6784 | we don't want the local symbols, and we don't want the section | |
6785 | contents. */ | |
6786 | if ((input_bfd->flags & DYNAMIC) != 0) | |
6787 | return TRUE; | |
6788 | ||
6789 | emit_relocs = (finfo->info->relocatable | |
eac338cf | 6790 | || finfo->info->emitrelocations); |
c152c796 AM |
6791 | |
6792 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
6793 | if (elf_bad_symtab (input_bfd)) | |
6794 | { | |
6795 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
6796 | extsymoff = 0; | |
6797 | } | |
6798 | else | |
6799 | { | |
6800 | locsymcount = symtab_hdr->sh_info; | |
6801 | extsymoff = symtab_hdr->sh_info; | |
6802 | } | |
6803 | ||
6804 | /* Read the local symbols. */ | |
6805 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
6806 | if (isymbuf == NULL && locsymcount != 0) | |
6807 | { | |
6808 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
6809 | finfo->internal_syms, | |
6810 | finfo->external_syms, | |
6811 | finfo->locsym_shndx); | |
6812 | if (isymbuf == NULL) | |
6813 | return FALSE; | |
6814 | } | |
6815 | ||
6816 | /* Find local symbol sections and adjust values of symbols in | |
6817 | SEC_MERGE sections. Write out those local symbols we know are | |
6818 | going into the output file. */ | |
6819 | isymend = isymbuf + locsymcount; | |
6820 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
6821 | isym < isymend; | |
6822 | isym++, pindex++, ppsection++) | |
6823 | { | |
6824 | asection *isec; | |
6825 | const char *name; | |
6826 | Elf_Internal_Sym osym; | |
6827 | ||
6828 | *pindex = -1; | |
6829 | ||
6830 | if (elf_bad_symtab (input_bfd)) | |
6831 | { | |
6832 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
6833 | { | |
6834 | *ppsection = NULL; | |
6835 | continue; | |
6836 | } | |
6837 | } | |
6838 | ||
6839 | if (isym->st_shndx == SHN_UNDEF) | |
6840 | isec = bfd_und_section_ptr; | |
6841 | else if (isym->st_shndx < SHN_LORESERVE | |
6842 | || isym->st_shndx > SHN_HIRESERVE) | |
6843 | { | |
6844 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); | |
6845 | if (isec | |
6846 | && isec->sec_info_type == ELF_INFO_TYPE_MERGE | |
6847 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) | |
6848 | isym->st_value = | |
6849 | _bfd_merged_section_offset (output_bfd, &isec, | |
6850 | elf_section_data (isec)->sec_info, | |
753731ee | 6851 | isym->st_value); |
c152c796 AM |
6852 | } |
6853 | else if (isym->st_shndx == SHN_ABS) | |
6854 | isec = bfd_abs_section_ptr; | |
6855 | else if (isym->st_shndx == SHN_COMMON) | |
6856 | isec = bfd_com_section_ptr; | |
6857 | else | |
6858 | { | |
6859 | /* Who knows? */ | |
6860 | isec = NULL; | |
6861 | } | |
6862 | ||
6863 | *ppsection = isec; | |
6864 | ||
6865 | /* Don't output the first, undefined, symbol. */ | |
6866 | if (ppsection == finfo->sections) | |
6867 | continue; | |
6868 | ||
6869 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
6870 | { | |
6871 | /* We never output section symbols. Instead, we use the | |
6872 | section symbol of the corresponding section in the output | |
6873 | file. */ | |
6874 | continue; | |
6875 | } | |
6876 | ||
6877 | /* If we are stripping all symbols, we don't want to output this | |
6878 | one. */ | |
6879 | if (finfo->info->strip == strip_all) | |
6880 | continue; | |
6881 | ||
6882 | /* If we are discarding all local symbols, we don't want to | |
6883 | output this one. If we are generating a relocatable output | |
6884 | file, then some of the local symbols may be required by | |
6885 | relocs; we output them below as we discover that they are | |
6886 | needed. */ | |
6887 | if (finfo->info->discard == discard_all) | |
6888 | continue; | |
6889 | ||
6890 | /* If this symbol is defined in a section which we are | |
6891 | discarding, we don't need to keep it, but note that | |
6892 | linker_mark is only reliable for sections that have contents. | |
6893 | For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE | |
6894 | as well as linker_mark. */ | |
6895 | if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
ccf5f610 PB |
6896 | && (isec == NULL |
6897 | || (! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0) | |
c152c796 AM |
6898 | || (! finfo->info->relocatable |
6899 | && (isec->flags & SEC_EXCLUDE) != 0))) | |
6900 | continue; | |
6901 | ||
e75a280b L |
6902 | /* If the section is not in the output BFD's section list, it is not |
6903 | being output. */ | |
6904 | if (bfd_section_removed_from_list (output_bfd, isec->output_section)) | |
6905 | continue; | |
6906 | ||
c152c796 AM |
6907 | /* Get the name of the symbol. */ |
6908 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
6909 | isym->st_name); | |
6910 | if (name == NULL) | |
6911 | return FALSE; | |
6912 | ||
6913 | /* See if we are discarding symbols with this name. */ | |
6914 | if ((finfo->info->strip == strip_some | |
6915 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) | |
6916 | == NULL)) | |
6917 | || (((finfo->info->discard == discard_sec_merge | |
6918 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) | |
6919 | || finfo->info->discard == discard_l) | |
6920 | && bfd_is_local_label_name (input_bfd, name))) | |
6921 | continue; | |
6922 | ||
6923 | /* If we get here, we are going to output this symbol. */ | |
6924 | ||
6925 | osym = *isym; | |
6926 | ||
6927 | /* Adjust the section index for the output file. */ | |
6928 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
6929 | isec->output_section); | |
6930 | if (osym.st_shndx == SHN_BAD) | |
6931 | return FALSE; | |
6932 | ||
6933 | *pindex = bfd_get_symcount (output_bfd); | |
6934 | ||
6935 | /* ELF symbols in relocatable files are section relative, but | |
6936 | in executable files they are virtual addresses. Note that | |
6937 | this code assumes that all ELF sections have an associated | |
6938 | BFD section with a reasonable value for output_offset; below | |
6939 | we assume that they also have a reasonable value for | |
6940 | output_section. Any special sections must be set up to meet | |
6941 | these requirements. */ | |
6942 | osym.st_value += isec->output_offset; | |
6943 | if (! finfo->info->relocatable) | |
6944 | { | |
6945 | osym.st_value += isec->output_section->vma; | |
6946 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
6947 | { | |
6948 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
6949 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
6950 | osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
6951 | } | |
6952 | } | |
6953 | ||
6954 | if (! elf_link_output_sym (finfo, name, &osym, isec, NULL)) | |
6955 | return FALSE; | |
6956 | } | |
6957 | ||
6958 | /* Relocate the contents of each section. */ | |
6959 | sym_hashes = elf_sym_hashes (input_bfd); | |
6960 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
6961 | { | |
6962 | bfd_byte *contents; | |
6963 | ||
6964 | if (! o->linker_mark) | |
6965 | { | |
6966 | /* This section was omitted from the link. */ | |
6967 | continue; | |
6968 | } | |
6969 | ||
6970 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 6971 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
6972 | continue; |
6973 | ||
6974 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
6975 | { | |
6976 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
6977 | or somesuch. */ | |
6978 | continue; | |
6979 | } | |
6980 | ||
6981 | /* Get the contents of the section. They have been cached by a | |
6982 | relaxation routine. Note that o is a section in an input | |
6983 | file, so the contents field will not have been set by any of | |
6984 | the routines which work on output files. */ | |
6985 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
6986 | contents = elf_section_data (o)->this_hdr.contents; | |
6987 | else | |
6988 | { | |
eea6121a AM |
6989 | bfd_size_type amt = o->rawsize ? o->rawsize : o->size; |
6990 | ||
c152c796 | 6991 | contents = finfo->contents; |
eea6121a | 6992 | if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt)) |
c152c796 AM |
6993 | return FALSE; |
6994 | } | |
6995 | ||
6996 | if ((o->flags & SEC_RELOC) != 0) | |
6997 | { | |
6998 | Elf_Internal_Rela *internal_relocs; | |
6999 | bfd_vma r_type_mask; | |
7000 | int r_sym_shift; | |
7001 | ||
7002 | /* Get the swapped relocs. */ | |
7003 | internal_relocs | |
7004 | = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, | |
7005 | finfo->internal_relocs, FALSE); | |
7006 | if (internal_relocs == NULL | |
7007 | && o->reloc_count > 0) | |
7008 | return FALSE; | |
7009 | ||
7010 | if (bed->s->arch_size == 32) | |
7011 | { | |
7012 | r_type_mask = 0xff; | |
7013 | r_sym_shift = 8; | |
7014 | } | |
7015 | else | |
7016 | { | |
7017 | r_type_mask = 0xffffffff; | |
7018 | r_sym_shift = 32; | |
7019 | } | |
7020 | ||
7021 | /* Run through the relocs looking for any against symbols | |
7022 | from discarded sections and section symbols from | |
7023 | removed link-once sections. Complain about relocs | |
7024 | against discarded sections. Zero relocs against removed | |
7025 | link-once sections. Preserve debug information as much | |
7026 | as we can. */ | |
7027 | if (!elf_section_ignore_discarded_relocs (o)) | |
7028 | { | |
7029 | Elf_Internal_Rela *rel, *relend; | |
9e66c942 | 7030 | unsigned int action = elf_action_discarded (o); |
c152c796 AM |
7031 | |
7032 | rel = internal_relocs; | |
7033 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
7034 | for ( ; rel < relend; rel++) | |
7035 | { | |
7036 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
cdd3575c AM |
7037 | asection **ps, *sec; |
7038 | struct elf_link_hash_entry *h = NULL; | |
7039 | const char *sym_name; | |
c152c796 | 7040 | |
ee75fd95 AM |
7041 | if (r_symndx == STN_UNDEF) |
7042 | continue; | |
7043 | ||
c152c796 AM |
7044 | if (r_symndx >= locsymcount |
7045 | || (elf_bad_symtab (input_bfd) | |
7046 | && finfo->sections[r_symndx] == NULL)) | |
7047 | { | |
c152c796 | 7048 | h = sym_hashes[r_symndx - extsymoff]; |
dce669a1 | 7049 | |
8c19749a NC |
7050 | /* Badly formatted input files can contain relocs that |
7051 | reference non-existant symbols. Check here so that | |
7052 | we do not seg fault. */ | |
7053 | if (h == NULL) | |
7054 | { | |
7055 | char buffer [32]; | |
7056 | ||
7057 | sprintf_vma (buffer, rel->r_info); | |
7058 | (*_bfd_error_handler) | |
7059 | (_("error: %B contains a reloc (0x%s) for section %A " | |
7060 | "that references a non-existent global symbol"), | |
7061 | input_bfd, o, buffer); | |
7062 | bfd_set_error (bfd_error_bad_value); | |
7063 | return FALSE; | |
7064 | } | |
3b36f7e6 | 7065 | |
c152c796 AM |
7066 | while (h->root.type == bfd_link_hash_indirect |
7067 | || h->root.type == bfd_link_hash_warning) | |
7068 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7069 | ||
cdd3575c AM |
7070 | if (h->root.type != bfd_link_hash_defined |
7071 | && h->root.type != bfd_link_hash_defweak) | |
7072 | continue; | |
7073 | ||
7074 | ps = &h->root.u.def.section; | |
7075 | sym_name = h->root.root.string; | |
c152c796 AM |
7076 | } |
7077 | else | |
7078 | { | |
cdd3575c AM |
7079 | Elf_Internal_Sym *sym = isymbuf + r_symndx; |
7080 | ps = &finfo->sections[r_symndx]; | |
26c61ae5 L |
7081 | sym_name = bfd_elf_sym_name (input_bfd, |
7082 | symtab_hdr, | |
7083 | sym, *ps); | |
cdd3575c | 7084 | } |
c152c796 | 7085 | |
cdd3575c AM |
7086 | /* Complain if the definition comes from a |
7087 | discarded section. */ | |
7088 | if ((sec = *ps) != NULL && elf_discarded_section (sec)) | |
7089 | { | |
87e5235d | 7090 | BFD_ASSERT (r_symndx != 0); |
9e66c942 | 7091 | if (action & COMPLAIN) |
e1fffbe6 AM |
7092 | (*finfo->info->callbacks->einfo) |
7093 | (_("%X`%s' referenced in section `%A' of %B: " | |
7094 | "defined in discarded section `%A' of %B"), | |
7095 | sym_name, o, input_bfd, sec, sec->owner); | |
cdd3575c | 7096 | |
87e5235d AM |
7097 | /* Try to do the best we can to support buggy old |
7098 | versions of gcc. If we've warned, or this is | |
7099 | debugging info, pretend that the symbol is | |
7100 | really defined in the kept linkonce section. | |
7101 | FIXME: This is quite broken. Modifying the | |
7102 | symbol here means we will be changing all later | |
7103 | uses of the symbol, not just in this section. | |
7104 | The only thing that makes this half reasonable | |
7105 | is that we warn in non-debug sections, and | |
7106 | debug sections tend to come after other | |
7107 | sections. */ | |
01b3c8ab | 7108 | if (action & PRETEND) |
87e5235d | 7109 | { |
01b3c8ab L |
7110 | asection *kept; |
7111 | ||
7112 | kept = _bfd_elf_check_kept_section (sec); | |
7113 | if (kept != NULL) | |
87e5235d AM |
7114 | { |
7115 | *ps = kept; | |
7116 | continue; | |
7117 | } | |
7118 | } | |
7119 | ||
cdd3575c AM |
7120 | /* Remove the symbol reference from the reloc, but |
7121 | don't kill the reloc completely. This is so that | |
7122 | a zero value will be written into the section, | |
7123 | which may have non-zero contents put there by the | |
7124 | assembler. Zero in things like an eh_frame fde | |
7125 | pc_begin allows stack unwinders to recognize the | |
7126 | fde as bogus. */ | |
7127 | rel->r_info &= r_type_mask; | |
7128 | rel->r_addend = 0; | |
c152c796 AM |
7129 | } |
7130 | } | |
7131 | } | |
7132 | ||
7133 | /* Relocate the section by invoking a back end routine. | |
7134 | ||
7135 | The back end routine is responsible for adjusting the | |
7136 | section contents as necessary, and (if using Rela relocs | |
7137 | and generating a relocatable output file) adjusting the | |
7138 | reloc addend as necessary. | |
7139 | ||
7140 | The back end routine does not have to worry about setting | |
7141 | the reloc address or the reloc symbol index. | |
7142 | ||
7143 | The back end routine is given a pointer to the swapped in | |
7144 | internal symbols, and can access the hash table entries | |
7145 | for the external symbols via elf_sym_hashes (input_bfd). | |
7146 | ||
7147 | When generating relocatable output, the back end routine | |
7148 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
7149 | output symbol is going to be a section symbol | |
7150 | corresponding to the output section, which will require | |
7151 | the addend to be adjusted. */ | |
7152 | ||
7153 | if (! (*relocate_section) (output_bfd, finfo->info, | |
7154 | input_bfd, o, contents, | |
7155 | internal_relocs, | |
7156 | isymbuf, | |
7157 | finfo->sections)) | |
7158 | return FALSE; | |
7159 | ||
7160 | if (emit_relocs) | |
7161 | { | |
7162 | Elf_Internal_Rela *irela; | |
7163 | Elf_Internal_Rela *irelaend; | |
7164 | bfd_vma last_offset; | |
7165 | struct elf_link_hash_entry **rel_hash; | |
eac338cf | 7166 | struct elf_link_hash_entry **rel_hash_list; |
c152c796 AM |
7167 | Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2; |
7168 | unsigned int next_erel; | |
c152c796 AM |
7169 | bfd_boolean rela_normal; |
7170 | ||
7171 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
7172 | rela_normal = (bed->rela_normal | |
7173 | && (input_rel_hdr->sh_entsize | |
7174 | == bed->s->sizeof_rela)); | |
7175 | ||
7176 | /* Adjust the reloc addresses and symbol indices. */ | |
7177 | ||
7178 | irela = internal_relocs; | |
7179 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
7180 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
7181 | + elf_section_data (o->output_section)->rel_count | |
7182 | + elf_section_data (o->output_section)->rel_count2); | |
eac338cf | 7183 | rel_hash_list = rel_hash; |
c152c796 AM |
7184 | last_offset = o->output_offset; |
7185 | if (!finfo->info->relocatable) | |
7186 | last_offset += o->output_section->vma; | |
7187 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
7188 | { | |
7189 | unsigned long r_symndx; | |
7190 | asection *sec; | |
7191 | Elf_Internal_Sym sym; | |
7192 | ||
7193 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
7194 | { | |
7195 | rel_hash++; | |
7196 | next_erel = 0; | |
7197 | } | |
7198 | ||
7199 | irela->r_offset = _bfd_elf_section_offset (output_bfd, | |
7200 | finfo->info, o, | |
7201 | irela->r_offset); | |
7202 | if (irela->r_offset >= (bfd_vma) -2) | |
7203 | { | |
7204 | /* This is a reloc for a deleted entry or somesuch. | |
7205 | Turn it into an R_*_NONE reloc, at the same | |
7206 | offset as the last reloc. elf_eh_frame.c and | |
7207 | elf_bfd_discard_info rely on reloc offsets | |
7208 | being ordered. */ | |
7209 | irela->r_offset = last_offset; | |
7210 | irela->r_info = 0; | |
7211 | irela->r_addend = 0; | |
7212 | continue; | |
7213 | } | |
7214 | ||
7215 | irela->r_offset += o->output_offset; | |
7216 | ||
7217 | /* Relocs in an executable have to be virtual addresses. */ | |
7218 | if (!finfo->info->relocatable) | |
7219 | irela->r_offset += o->output_section->vma; | |
7220 | ||
7221 | last_offset = irela->r_offset; | |
7222 | ||
7223 | r_symndx = irela->r_info >> r_sym_shift; | |
7224 | if (r_symndx == STN_UNDEF) | |
7225 | continue; | |
7226 | ||
7227 | if (r_symndx >= locsymcount | |
7228 | || (elf_bad_symtab (input_bfd) | |
7229 | && finfo->sections[r_symndx] == NULL)) | |
7230 | { | |
7231 | struct elf_link_hash_entry *rh; | |
7232 | unsigned long indx; | |
7233 | ||
7234 | /* This is a reloc against a global symbol. We | |
7235 | have not yet output all the local symbols, so | |
7236 | we do not know the symbol index of any global | |
7237 | symbol. We set the rel_hash entry for this | |
7238 | reloc to point to the global hash table entry | |
7239 | for this symbol. The symbol index is then | |
ee75fd95 | 7240 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
7241 | indx = r_symndx - extsymoff; |
7242 | rh = elf_sym_hashes (input_bfd)[indx]; | |
7243 | while (rh->root.type == bfd_link_hash_indirect | |
7244 | || rh->root.type == bfd_link_hash_warning) | |
7245 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
7246 | ||
7247 | /* Setting the index to -2 tells | |
7248 | elf_link_output_extsym that this symbol is | |
7249 | used by a reloc. */ | |
7250 | BFD_ASSERT (rh->indx < 0); | |
7251 | rh->indx = -2; | |
7252 | ||
7253 | *rel_hash = rh; | |
7254 | ||
7255 | continue; | |
7256 | } | |
7257 | ||
7258 | /* This is a reloc against a local symbol. */ | |
7259 | ||
7260 | *rel_hash = NULL; | |
7261 | sym = isymbuf[r_symndx]; | |
7262 | sec = finfo->sections[r_symndx]; | |
7263 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) | |
7264 | { | |
7265 | /* I suppose the backend ought to fill in the | |
7266 | section of any STT_SECTION symbol against a | |
6a8d1586 AM |
7267 | processor specific section. */ |
7268 | r_symndx = 0; | |
7269 | if (bfd_is_abs_section (sec)) | |
7270 | ; | |
c152c796 AM |
7271 | else if (sec == NULL || sec->owner == NULL) |
7272 | { | |
7273 | bfd_set_error (bfd_error_bad_value); | |
7274 | return FALSE; | |
7275 | } | |
7276 | else | |
7277 | { | |
6a8d1586 AM |
7278 | asection *osec = sec->output_section; |
7279 | ||
7280 | /* If we have discarded a section, the output | |
7281 | section will be the absolute section. In | |
7282 | case of discarded link-once and discarded | |
7283 | SEC_MERGE sections, use the kept section. */ | |
7284 | if (bfd_is_abs_section (osec) | |
7285 | && sec->kept_section != NULL | |
7286 | && sec->kept_section->output_section != NULL) | |
7287 | { | |
7288 | osec = sec->kept_section->output_section; | |
7289 | irela->r_addend -= osec->vma; | |
7290 | } | |
7291 | ||
7292 | if (!bfd_is_abs_section (osec)) | |
7293 | { | |
7294 | r_symndx = osec->target_index; | |
7295 | BFD_ASSERT (r_symndx != 0); | |
7296 | } | |
c152c796 AM |
7297 | } |
7298 | ||
7299 | /* Adjust the addend according to where the | |
7300 | section winds up in the output section. */ | |
7301 | if (rela_normal) | |
7302 | irela->r_addend += sec->output_offset; | |
7303 | } | |
7304 | else | |
7305 | { | |
7306 | if (finfo->indices[r_symndx] == -1) | |
7307 | { | |
7308 | unsigned long shlink; | |
7309 | const char *name; | |
7310 | asection *osec; | |
7311 | ||
7312 | if (finfo->info->strip == strip_all) | |
7313 | { | |
7314 | /* You can't do ld -r -s. */ | |
7315 | bfd_set_error (bfd_error_invalid_operation); | |
7316 | return FALSE; | |
7317 | } | |
7318 | ||
7319 | /* This symbol was skipped earlier, but | |
7320 | since it is needed by a reloc, we | |
7321 | must output it now. */ | |
7322 | shlink = symtab_hdr->sh_link; | |
7323 | name = (bfd_elf_string_from_elf_section | |
7324 | (input_bfd, shlink, sym.st_name)); | |
7325 | if (name == NULL) | |
7326 | return FALSE; | |
7327 | ||
7328 | osec = sec->output_section; | |
7329 | sym.st_shndx = | |
7330 | _bfd_elf_section_from_bfd_section (output_bfd, | |
7331 | osec); | |
7332 | if (sym.st_shndx == SHN_BAD) | |
7333 | return FALSE; | |
7334 | ||
7335 | sym.st_value += sec->output_offset; | |
7336 | if (! finfo->info->relocatable) | |
7337 | { | |
7338 | sym.st_value += osec->vma; | |
7339 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
7340 | { | |
7341 | /* STT_TLS symbols are relative to PT_TLS | |
7342 | segment base. */ | |
7343 | BFD_ASSERT (elf_hash_table (finfo->info) | |
7344 | ->tls_sec != NULL); | |
7345 | sym.st_value -= (elf_hash_table (finfo->info) | |
7346 | ->tls_sec->vma); | |
7347 | } | |
7348 | } | |
7349 | ||
7350 | finfo->indices[r_symndx] | |
7351 | = bfd_get_symcount (output_bfd); | |
7352 | ||
7353 | if (! elf_link_output_sym (finfo, name, &sym, sec, | |
7354 | NULL)) | |
7355 | return FALSE; | |
7356 | } | |
7357 | ||
7358 | r_symndx = finfo->indices[r_symndx]; | |
7359 | } | |
7360 | ||
7361 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
7362 | | (irela->r_info & r_type_mask)); | |
7363 | } | |
7364 | ||
7365 | /* Swap out the relocs. */ | |
c152c796 | 7366 | if (input_rel_hdr->sh_size != 0 |
eac338cf PB |
7367 | && !bed->elf_backend_emit_relocs (output_bfd, o, |
7368 | input_rel_hdr, | |
7369 | internal_relocs, | |
7370 | rel_hash_list)) | |
c152c796 AM |
7371 | return FALSE; |
7372 | ||
7373 | input_rel_hdr2 = elf_section_data (o)->rel_hdr2; | |
7374 | if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0) | |
7375 | { | |
7376 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) | |
7377 | * bed->s->int_rels_per_ext_rel); | |
eac338cf PB |
7378 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
7379 | if (!bed->elf_backend_emit_relocs (output_bfd, o, | |
7380 | input_rel_hdr2, | |
7381 | internal_relocs, | |
7382 | rel_hash_list)) | |
c152c796 AM |
7383 | return FALSE; |
7384 | } | |
7385 | } | |
7386 | } | |
7387 | ||
7388 | /* Write out the modified section contents. */ | |
7389 | if (bed->elf_backend_write_section | |
7390 | && (*bed->elf_backend_write_section) (output_bfd, o, contents)) | |
7391 | { | |
7392 | /* Section written out. */ | |
7393 | } | |
7394 | else switch (o->sec_info_type) | |
7395 | { | |
7396 | case ELF_INFO_TYPE_STABS: | |
7397 | if (! (_bfd_write_section_stabs | |
7398 | (output_bfd, | |
7399 | &elf_hash_table (finfo->info)->stab_info, | |
7400 | o, &elf_section_data (o)->sec_info, contents))) | |
7401 | return FALSE; | |
7402 | break; | |
7403 | case ELF_INFO_TYPE_MERGE: | |
7404 | if (! _bfd_write_merged_section (output_bfd, o, | |
7405 | elf_section_data (o)->sec_info)) | |
7406 | return FALSE; | |
7407 | break; | |
7408 | case ELF_INFO_TYPE_EH_FRAME: | |
7409 | { | |
7410 | if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, | |
7411 | o, contents)) | |
7412 | return FALSE; | |
7413 | } | |
7414 | break; | |
7415 | default: | |
7416 | { | |
c152c796 AM |
7417 | if (! (o->flags & SEC_EXCLUDE) |
7418 | && ! bfd_set_section_contents (output_bfd, o->output_section, | |
7419 | contents, | |
7420 | (file_ptr) o->output_offset, | |
eea6121a | 7421 | o->size)) |
c152c796 AM |
7422 | return FALSE; |
7423 | } | |
7424 | break; | |
7425 | } | |
7426 | } | |
7427 | ||
7428 | return TRUE; | |
7429 | } | |
7430 | ||
7431 | /* Generate a reloc when linking an ELF file. This is a reloc | |
7432 | requested by the linker, and does come from any input file. This | |
7433 | is used to build constructor and destructor tables when linking | |
7434 | with -Ur. */ | |
7435 | ||
7436 | static bfd_boolean | |
7437 | elf_reloc_link_order (bfd *output_bfd, | |
7438 | struct bfd_link_info *info, | |
7439 | asection *output_section, | |
7440 | struct bfd_link_order *link_order) | |
7441 | { | |
7442 | reloc_howto_type *howto; | |
7443 | long indx; | |
7444 | bfd_vma offset; | |
7445 | bfd_vma addend; | |
7446 | struct elf_link_hash_entry **rel_hash_ptr; | |
7447 | Elf_Internal_Shdr *rel_hdr; | |
7448 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
7449 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
7450 | bfd_byte *erel; | |
7451 | unsigned int i; | |
7452 | ||
7453 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
7454 | if (howto == NULL) | |
7455 | { | |
7456 | bfd_set_error (bfd_error_bad_value); | |
7457 | return FALSE; | |
7458 | } | |
7459 | ||
7460 | addend = link_order->u.reloc.p->addend; | |
7461 | ||
7462 | /* Figure out the symbol index. */ | |
7463 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
7464 | + elf_section_data (output_section)->rel_count | |
7465 | + elf_section_data (output_section)->rel_count2); | |
7466 | if (link_order->type == bfd_section_reloc_link_order) | |
7467 | { | |
7468 | indx = link_order->u.reloc.p->u.section->target_index; | |
7469 | BFD_ASSERT (indx != 0); | |
7470 | *rel_hash_ptr = NULL; | |
7471 | } | |
7472 | else | |
7473 | { | |
7474 | struct elf_link_hash_entry *h; | |
7475 | ||
7476 | /* Treat a reloc against a defined symbol as though it were | |
7477 | actually against the section. */ | |
7478 | h = ((struct elf_link_hash_entry *) | |
7479 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
7480 | link_order->u.reloc.p->u.name, | |
7481 | FALSE, FALSE, TRUE)); | |
7482 | if (h != NULL | |
7483 | && (h->root.type == bfd_link_hash_defined | |
7484 | || h->root.type == bfd_link_hash_defweak)) | |
7485 | { | |
7486 | asection *section; | |
7487 | ||
7488 | section = h->root.u.def.section; | |
7489 | indx = section->output_section->target_index; | |
7490 | *rel_hash_ptr = NULL; | |
7491 | /* It seems that we ought to add the symbol value to the | |
7492 | addend here, but in practice it has already been added | |
7493 | because it was passed to constructor_callback. */ | |
7494 | addend += section->output_section->vma + section->output_offset; | |
7495 | } | |
7496 | else if (h != NULL) | |
7497 | { | |
7498 | /* Setting the index to -2 tells elf_link_output_extsym that | |
7499 | this symbol is used by a reloc. */ | |
7500 | h->indx = -2; | |
7501 | *rel_hash_ptr = h; | |
7502 | indx = 0; | |
7503 | } | |
7504 | else | |
7505 | { | |
7506 | if (! ((*info->callbacks->unattached_reloc) | |
7507 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) | |
7508 | return FALSE; | |
7509 | indx = 0; | |
7510 | } | |
7511 | } | |
7512 | ||
7513 | /* If this is an inplace reloc, we must write the addend into the | |
7514 | object file. */ | |
7515 | if (howto->partial_inplace && addend != 0) | |
7516 | { | |
7517 | bfd_size_type size; | |
7518 | bfd_reloc_status_type rstat; | |
7519 | bfd_byte *buf; | |
7520 | bfd_boolean ok; | |
7521 | const char *sym_name; | |
7522 | ||
7523 | size = bfd_get_reloc_size (howto); | |
7524 | buf = bfd_zmalloc (size); | |
7525 | if (buf == NULL) | |
7526 | return FALSE; | |
7527 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
7528 | switch (rstat) | |
7529 | { | |
7530 | case bfd_reloc_ok: | |
7531 | break; | |
7532 | ||
7533 | default: | |
7534 | case bfd_reloc_outofrange: | |
7535 | abort (); | |
7536 | ||
7537 | case bfd_reloc_overflow: | |
7538 | if (link_order->type == bfd_section_reloc_link_order) | |
7539 | sym_name = bfd_section_name (output_bfd, | |
7540 | link_order->u.reloc.p->u.section); | |
7541 | else | |
7542 | sym_name = link_order->u.reloc.p->u.name; | |
7543 | if (! ((*info->callbacks->reloc_overflow) | |
dfeffb9f L |
7544 | (info, NULL, sym_name, howto->name, addend, NULL, |
7545 | NULL, (bfd_vma) 0))) | |
c152c796 AM |
7546 | { |
7547 | free (buf); | |
7548 | return FALSE; | |
7549 | } | |
7550 | break; | |
7551 | } | |
7552 | ok = bfd_set_section_contents (output_bfd, output_section, buf, | |
7553 | link_order->offset, size); | |
7554 | free (buf); | |
7555 | if (! ok) | |
7556 | return FALSE; | |
7557 | } | |
7558 | ||
7559 | /* The address of a reloc is relative to the section in a | |
7560 | relocatable file, and is a virtual address in an executable | |
7561 | file. */ | |
7562 | offset = link_order->offset; | |
7563 | if (! info->relocatable) | |
7564 | offset += output_section->vma; | |
7565 | ||
7566 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
7567 | { | |
7568 | irel[i].r_offset = offset; | |
7569 | irel[i].r_info = 0; | |
7570 | irel[i].r_addend = 0; | |
7571 | } | |
7572 | if (bed->s->arch_size == 32) | |
7573 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
7574 | else | |
7575 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
7576 | ||
7577 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
7578 | erel = rel_hdr->contents; | |
7579 | if (rel_hdr->sh_type == SHT_REL) | |
7580 | { | |
7581 | erel += (elf_section_data (output_section)->rel_count | |
7582 | * bed->s->sizeof_rel); | |
7583 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); | |
7584 | } | |
7585 | else | |
7586 | { | |
7587 | irel[0].r_addend = addend; | |
7588 | erel += (elf_section_data (output_section)->rel_count | |
7589 | * bed->s->sizeof_rela); | |
7590 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); | |
7591 | } | |
7592 | ||
7593 | ++elf_section_data (output_section)->rel_count; | |
7594 | ||
7595 | return TRUE; | |
7596 | } | |
7597 | ||
0b52efa6 PB |
7598 | |
7599 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
7600 | ||
7601 | static bfd_vma | |
7602 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
7603 | { | |
7604 | Elf_Internal_Shdr **elf_shdrp; | |
7605 | asection *s; | |
7606 | int elfsec; | |
7607 | ||
7608 | s = p->u.indirect.section; | |
7609 | elf_shdrp = elf_elfsections (s->owner); | |
7610 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
7611 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
7612 | /* PR 290: |
7613 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
7614 | SHF_LINK_ORDER. But it doesn't set theh sh_link or | |
7615 | sh_info fields. Hence we could get the situation | |
7616 | where elfsec is 0. */ | |
7617 | if (elfsec == 0) | |
7618 | { | |
7619 | const struct elf_backend_data *bed | |
7620 | = get_elf_backend_data (s->owner); | |
7621 | if (bed->link_order_error_handler) | |
d003868e AM |
7622 | bed->link_order_error_handler |
7623 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); | |
185d09ad L |
7624 | return 0; |
7625 | } | |
7626 | else | |
7627 | { | |
7628 | s = elf_shdrp[elfsec]->bfd_section; | |
7629 | return s->output_section->vma + s->output_offset; | |
7630 | } | |
0b52efa6 PB |
7631 | } |
7632 | ||
7633 | ||
7634 | /* Compare two sections based on the locations of the sections they are | |
7635 | linked to. Used by elf_fixup_link_order. */ | |
7636 | ||
7637 | static int | |
7638 | compare_link_order (const void * a, const void * b) | |
7639 | { | |
7640 | bfd_vma apos; | |
7641 | bfd_vma bpos; | |
7642 | ||
7643 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
7644 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
7645 | if (apos < bpos) | |
7646 | return -1; | |
7647 | return apos > bpos; | |
7648 | } | |
7649 | ||
7650 | ||
7651 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
7652 | order as their linked sections. Returns false if this could not be done | |
7653 | because an output section includes both ordered and unordered | |
7654 | sections. Ideally we'd do this in the linker proper. */ | |
7655 | ||
7656 | static bfd_boolean | |
7657 | elf_fixup_link_order (bfd *abfd, asection *o) | |
7658 | { | |
7659 | int seen_linkorder; | |
7660 | int seen_other; | |
7661 | int n; | |
7662 | struct bfd_link_order *p; | |
7663 | bfd *sub; | |
7664 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7665 | int elfsec; | |
7666 | struct bfd_link_order **sections; | |
7667 | asection *s; | |
7668 | bfd_vma offset; | |
3b36f7e6 | 7669 | |
0b52efa6 PB |
7670 | seen_other = 0; |
7671 | seen_linkorder = 0; | |
8423293d | 7672 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
7673 | { |
7674 | if (p->type == bfd_indirect_link_order | |
7675 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
7676 | == bfd_target_elf_flavour) | |
7677 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
7678 | { | |
7679 | s = p->u.indirect.section; | |
7680 | elfsec = _bfd_elf_section_from_bfd_section (sub, s); | |
7681 | if (elfsec != -1 | |
7682 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER) | |
7683 | seen_linkorder++; | |
7684 | else | |
7685 | seen_other++; | |
7686 | } | |
7687 | else | |
7688 | seen_other++; | |
7689 | } | |
7690 | ||
7691 | if (!seen_linkorder) | |
7692 | return TRUE; | |
7693 | ||
7694 | if (seen_other && seen_linkorder) | |
08ccf96b | 7695 | { |
d003868e AM |
7696 | (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), |
7697 | o); | |
08ccf96b L |
7698 | bfd_set_error (bfd_error_bad_value); |
7699 | return FALSE; | |
7700 | } | |
3b36f7e6 | 7701 | |
0b52efa6 PB |
7702 | sections = (struct bfd_link_order **) |
7703 | xmalloc (seen_linkorder * sizeof (struct bfd_link_order *)); | |
7704 | seen_linkorder = 0; | |
3b36f7e6 | 7705 | |
8423293d | 7706 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
7707 | { |
7708 | sections[seen_linkorder++] = p; | |
7709 | } | |
7710 | /* Sort the input sections in the order of their linked section. */ | |
7711 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
7712 | compare_link_order); | |
7713 | ||
7714 | /* Change the offsets of the sections. */ | |
7715 | offset = 0; | |
7716 | for (n = 0; n < seen_linkorder; n++) | |
7717 | { | |
7718 | s = sections[n]->u.indirect.section; | |
7719 | offset &= ~(bfd_vma)((1 << s->alignment_power) - 1); | |
7720 | s->output_offset = offset; | |
7721 | sections[n]->offset = offset; | |
7722 | offset += sections[n]->size; | |
7723 | } | |
7724 | ||
7725 | return TRUE; | |
7726 | } | |
7727 | ||
7728 | ||
c152c796 AM |
7729 | /* Do the final step of an ELF link. */ |
7730 | ||
7731 | bfd_boolean | |
7732 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
7733 | { | |
7734 | bfd_boolean dynamic; | |
7735 | bfd_boolean emit_relocs; | |
7736 | bfd *dynobj; | |
7737 | struct elf_final_link_info finfo; | |
7738 | register asection *o; | |
7739 | register struct bfd_link_order *p; | |
7740 | register bfd *sub; | |
7741 | bfd_size_type max_contents_size; | |
7742 | bfd_size_type max_external_reloc_size; | |
7743 | bfd_size_type max_internal_reloc_count; | |
7744 | bfd_size_type max_sym_count; | |
7745 | bfd_size_type max_sym_shndx_count; | |
7746 | file_ptr off; | |
7747 | Elf_Internal_Sym elfsym; | |
7748 | unsigned int i; | |
7749 | Elf_Internal_Shdr *symtab_hdr; | |
7750 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
7751 | Elf_Internal_Shdr *symstrtab_hdr; | |
7752 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7753 | struct elf_outext_info eoinfo; | |
7754 | bfd_boolean merged; | |
7755 | size_t relativecount = 0; | |
7756 | asection *reldyn = 0; | |
7757 | bfd_size_type amt; | |
7758 | ||
7759 | if (! is_elf_hash_table (info->hash)) | |
7760 | return FALSE; | |
7761 | ||
7762 | if (info->shared) | |
7763 | abfd->flags |= DYNAMIC; | |
7764 | ||
7765 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
7766 | dynobj = elf_hash_table (info)->dynobj; | |
7767 | ||
7768 | emit_relocs = (info->relocatable | |
7769 | || info->emitrelocations | |
7770 | || bed->elf_backend_emit_relocs); | |
7771 | ||
7772 | finfo.info = info; | |
7773 | finfo.output_bfd = abfd; | |
7774 | finfo.symstrtab = _bfd_elf_stringtab_init (); | |
7775 | if (finfo.symstrtab == NULL) | |
7776 | return FALSE; | |
7777 | ||
7778 | if (! dynamic) | |
7779 | { | |
7780 | finfo.dynsym_sec = NULL; | |
7781 | finfo.hash_sec = NULL; | |
7782 | finfo.symver_sec = NULL; | |
7783 | } | |
7784 | else | |
7785 | { | |
7786 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
7787 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
7788 | BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL); | |
7789 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
7790 | /* Note that it is OK if symver_sec is NULL. */ | |
7791 | } | |
7792 | ||
7793 | finfo.contents = NULL; | |
7794 | finfo.external_relocs = NULL; | |
7795 | finfo.internal_relocs = NULL; | |
7796 | finfo.external_syms = NULL; | |
7797 | finfo.locsym_shndx = NULL; | |
7798 | finfo.internal_syms = NULL; | |
7799 | finfo.indices = NULL; | |
7800 | finfo.sections = NULL; | |
7801 | finfo.symbuf = NULL; | |
7802 | finfo.symshndxbuf = NULL; | |
7803 | finfo.symbuf_count = 0; | |
7804 | finfo.shndxbuf_size = 0; | |
7805 | ||
7806 | /* Count up the number of relocations we will output for each output | |
7807 | section, so that we know the sizes of the reloc sections. We | |
7808 | also figure out some maximum sizes. */ | |
7809 | max_contents_size = 0; | |
7810 | max_external_reloc_size = 0; | |
7811 | max_internal_reloc_count = 0; | |
7812 | max_sym_count = 0; | |
7813 | max_sym_shndx_count = 0; | |
7814 | merged = FALSE; | |
7815 | for (o = abfd->sections; o != NULL; o = o->next) | |
7816 | { | |
7817 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
7818 | o->reloc_count = 0; | |
7819 | ||
8423293d | 7820 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
7821 | { |
7822 | unsigned int reloc_count = 0; | |
7823 | struct bfd_elf_section_data *esdi = NULL; | |
7824 | unsigned int *rel_count1; | |
7825 | ||
7826 | if (p->type == bfd_section_reloc_link_order | |
7827 | || p->type == bfd_symbol_reloc_link_order) | |
7828 | reloc_count = 1; | |
7829 | else if (p->type == bfd_indirect_link_order) | |
7830 | { | |
7831 | asection *sec; | |
7832 | ||
7833 | sec = p->u.indirect.section; | |
7834 | esdi = elf_section_data (sec); | |
7835 | ||
7836 | /* Mark all sections which are to be included in the | |
7837 | link. This will normally be every section. We need | |
7838 | to do this so that we can identify any sections which | |
7839 | the linker has decided to not include. */ | |
7840 | sec->linker_mark = TRUE; | |
7841 | ||
7842 | if (sec->flags & SEC_MERGE) | |
7843 | merged = TRUE; | |
7844 | ||
7845 | if (info->relocatable || info->emitrelocations) | |
7846 | reloc_count = sec->reloc_count; | |
7847 | else if (bed->elf_backend_count_relocs) | |
7848 | { | |
7849 | Elf_Internal_Rela * relocs; | |
7850 | ||
7851 | relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
7852 | info->keep_memory); | |
7853 | ||
7854 | reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs); | |
7855 | ||
7856 | if (elf_section_data (o)->relocs != relocs) | |
7857 | free (relocs); | |
7858 | } | |
7859 | ||
eea6121a AM |
7860 | if (sec->rawsize > max_contents_size) |
7861 | max_contents_size = sec->rawsize; | |
7862 | if (sec->size > max_contents_size) | |
7863 | max_contents_size = sec->size; | |
c152c796 AM |
7864 | |
7865 | /* We are interested in just local symbols, not all | |
7866 | symbols. */ | |
7867 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
7868 | && (sec->owner->flags & DYNAMIC) == 0) | |
7869 | { | |
7870 | size_t sym_count; | |
7871 | ||
7872 | if (elf_bad_symtab (sec->owner)) | |
7873 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
7874 | / bed->s->sizeof_sym); | |
7875 | else | |
7876 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
7877 | ||
7878 | if (sym_count > max_sym_count) | |
7879 | max_sym_count = sym_count; | |
7880 | ||
7881 | if (sym_count > max_sym_shndx_count | |
7882 | && elf_symtab_shndx (sec->owner) != 0) | |
7883 | max_sym_shndx_count = sym_count; | |
7884 | ||
7885 | if ((sec->flags & SEC_RELOC) != 0) | |
7886 | { | |
7887 | size_t ext_size; | |
7888 | ||
7889 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
7890 | if (ext_size > max_external_reloc_size) | |
7891 | max_external_reloc_size = ext_size; | |
7892 | if (sec->reloc_count > max_internal_reloc_count) | |
7893 | max_internal_reloc_count = sec->reloc_count; | |
7894 | } | |
7895 | } | |
7896 | } | |
7897 | ||
7898 | if (reloc_count == 0) | |
7899 | continue; | |
7900 | ||
7901 | o->reloc_count += reloc_count; | |
7902 | ||
7903 | /* MIPS may have a mix of REL and RELA relocs on sections. | |
7904 | To support this curious ABI we keep reloc counts in | |
7905 | elf_section_data too. We must be careful to add the | |
7906 | relocations from the input section to the right output | |
7907 | count. FIXME: Get rid of one count. We have | |
7908 | o->reloc_count == esdo->rel_count + esdo->rel_count2. */ | |
7909 | rel_count1 = &esdo->rel_count; | |
7910 | if (esdi != NULL) | |
7911 | { | |
7912 | bfd_boolean same_size; | |
7913 | bfd_size_type entsize1; | |
7914 | ||
7915 | entsize1 = esdi->rel_hdr.sh_entsize; | |
7916 | BFD_ASSERT (entsize1 == bed->s->sizeof_rel | |
7917 | || entsize1 == bed->s->sizeof_rela); | |
7918 | same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel); | |
7919 | ||
7920 | if (!same_size) | |
7921 | rel_count1 = &esdo->rel_count2; | |
7922 | ||
7923 | if (esdi->rel_hdr2 != NULL) | |
7924 | { | |
7925 | bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize; | |
7926 | unsigned int alt_count; | |
7927 | unsigned int *rel_count2; | |
7928 | ||
7929 | BFD_ASSERT (entsize2 != entsize1 | |
7930 | && (entsize2 == bed->s->sizeof_rel | |
7931 | || entsize2 == bed->s->sizeof_rela)); | |
7932 | ||
7933 | rel_count2 = &esdo->rel_count2; | |
7934 | if (!same_size) | |
7935 | rel_count2 = &esdo->rel_count; | |
7936 | ||
7937 | /* The following is probably too simplistic if the | |
7938 | backend counts output relocs unusually. */ | |
7939 | BFD_ASSERT (bed->elf_backend_count_relocs == NULL); | |
7940 | alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2); | |
7941 | *rel_count2 += alt_count; | |
7942 | reloc_count -= alt_count; | |
7943 | } | |
7944 | } | |
7945 | *rel_count1 += reloc_count; | |
7946 | } | |
7947 | ||
7948 | if (o->reloc_count > 0) | |
7949 | o->flags |= SEC_RELOC; | |
7950 | else | |
7951 | { | |
7952 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
7953 | set it (this is probably a bug) and if it is set | |
7954 | assign_section_numbers will create a reloc section. */ | |
7955 | o->flags &=~ SEC_RELOC; | |
7956 | } | |
7957 | ||
7958 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
7959 | zero. This is done in elf_fake_sections as well, but forcing | |
7960 | the VMA to 0 here will ensure that relocs against these | |
7961 | sections are handled correctly. */ | |
7962 | if ((o->flags & SEC_ALLOC) == 0 | |
7963 | && ! o->user_set_vma) | |
7964 | o->vma = 0; | |
7965 | } | |
7966 | ||
7967 | if (! info->relocatable && merged) | |
7968 | elf_link_hash_traverse (elf_hash_table (info), | |
7969 | _bfd_elf_link_sec_merge_syms, abfd); | |
7970 | ||
7971 | /* Figure out the file positions for everything but the symbol table | |
7972 | and the relocs. We set symcount to force assign_section_numbers | |
7973 | to create a symbol table. */ | |
7974 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; | |
7975 | BFD_ASSERT (! abfd->output_has_begun); | |
7976 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
7977 | goto error_return; | |
7978 | ||
ee75fd95 | 7979 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
7980 | for (o = abfd->sections; o != NULL; o = o->next) |
7981 | { | |
7982 | if ((o->flags & SEC_RELOC) != 0) | |
7983 | { | |
7984 | if (!(_bfd_elf_link_size_reloc_section | |
7985 | (abfd, &elf_section_data (o)->rel_hdr, o))) | |
7986 | goto error_return; | |
7987 | ||
7988 | if (elf_section_data (o)->rel_hdr2 | |
7989 | && !(_bfd_elf_link_size_reloc_section | |
7990 | (abfd, elf_section_data (o)->rel_hdr2, o))) | |
7991 | goto error_return; | |
7992 | } | |
7993 | ||
7994 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
7995 | to count upwards while actually outputting the relocations. */ | |
7996 | elf_section_data (o)->rel_count = 0; | |
7997 | elf_section_data (o)->rel_count2 = 0; | |
7998 | } | |
7999 | ||
8000 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
8001 | ||
8002 | /* We have now assigned file positions for all the sections except | |
8003 | .symtab and .strtab. We start the .symtab section at the current | |
8004 | file position, and write directly to it. We build the .strtab | |
8005 | section in memory. */ | |
8006 | bfd_get_symcount (abfd) = 0; | |
8007 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
8008 | /* sh_name is set in prep_headers. */ | |
8009 | symtab_hdr->sh_type = SHT_SYMTAB; | |
8010 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
8011 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
8012 | /* sh_link is set in assign_section_numbers. */ | |
8013 | /* sh_info is set below. */ | |
8014 | /* sh_offset is set just below. */ | |
8015 | symtab_hdr->sh_addralign = 1 << bed->s->log_file_align; | |
8016 | ||
8017 | off = elf_tdata (abfd)->next_file_pos; | |
8018 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
8019 | ||
8020 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
8021 | incorrect. We do not yet know the size of the .symtab section. | |
8022 | We correct next_file_pos below, after we do know the size. */ | |
8023 | ||
8024 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
8025 | continuously seeking to the right position in the file. */ | |
8026 | if (! info->keep_memory || max_sym_count < 20) | |
8027 | finfo.symbuf_size = 20; | |
8028 | else | |
8029 | finfo.symbuf_size = max_sym_count; | |
8030 | amt = finfo.symbuf_size; | |
8031 | amt *= bed->s->sizeof_sym; | |
8032 | finfo.symbuf = bfd_malloc (amt); | |
8033 | if (finfo.symbuf == NULL) | |
8034 | goto error_return; | |
8035 | if (elf_numsections (abfd) > SHN_LORESERVE) | |
8036 | { | |
8037 | /* Wild guess at number of output symbols. realloc'd as needed. */ | |
8038 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
8039 | finfo.shndxbuf_size = amt; | |
8040 | amt *= sizeof (Elf_External_Sym_Shndx); | |
8041 | finfo.symshndxbuf = bfd_zmalloc (amt); | |
8042 | if (finfo.symshndxbuf == NULL) | |
8043 | goto error_return; | |
8044 | } | |
8045 | ||
8046 | /* Start writing out the symbol table. The first symbol is always a | |
8047 | dummy symbol. */ | |
8048 | if (info->strip != strip_all | |
8049 | || emit_relocs) | |
8050 | { | |
8051 | elfsym.st_value = 0; | |
8052 | elfsym.st_size = 0; | |
8053 | elfsym.st_info = 0; | |
8054 | elfsym.st_other = 0; | |
8055 | elfsym.st_shndx = SHN_UNDEF; | |
8056 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr, | |
8057 | NULL)) | |
8058 | goto error_return; | |
8059 | } | |
8060 | ||
c152c796 AM |
8061 | /* Output a symbol for each section. We output these even if we are |
8062 | discarding local symbols, since they are used for relocs. These | |
8063 | symbols have no names. We store the index of each one in the | |
8064 | index field of the section, so that we can find it again when | |
8065 | outputting relocs. */ | |
8066 | if (info->strip != strip_all | |
8067 | || emit_relocs) | |
8068 | { | |
8069 | elfsym.st_size = 0; | |
8070 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8071 | elfsym.st_other = 0; | |
8072 | for (i = 1; i < elf_numsections (abfd); i++) | |
8073 | { | |
8074 | o = bfd_section_from_elf_index (abfd, i); | |
8075 | if (o != NULL) | |
8076 | o->target_index = bfd_get_symcount (abfd); | |
8077 | elfsym.st_shndx = i; | |
8078 | if (info->relocatable || o == NULL) | |
8079 | elfsym.st_value = 0; | |
8080 | else | |
8081 | elfsym.st_value = o->vma; | |
8082 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL)) | |
8083 | goto error_return; | |
8084 | if (i == SHN_LORESERVE - 1) | |
8085 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
8086 | } | |
8087 | } | |
8088 | ||
8089 | /* Allocate some memory to hold information read in from the input | |
8090 | files. */ | |
8091 | if (max_contents_size != 0) | |
8092 | { | |
8093 | finfo.contents = bfd_malloc (max_contents_size); | |
8094 | if (finfo.contents == NULL) | |
8095 | goto error_return; | |
8096 | } | |
8097 | ||
8098 | if (max_external_reloc_size != 0) | |
8099 | { | |
8100 | finfo.external_relocs = bfd_malloc (max_external_reloc_size); | |
8101 | if (finfo.external_relocs == NULL) | |
8102 | goto error_return; | |
8103 | } | |
8104 | ||
8105 | if (max_internal_reloc_count != 0) | |
8106 | { | |
8107 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
8108 | amt *= sizeof (Elf_Internal_Rela); | |
8109 | finfo.internal_relocs = bfd_malloc (amt); | |
8110 | if (finfo.internal_relocs == NULL) | |
8111 | goto error_return; | |
8112 | } | |
8113 | ||
8114 | if (max_sym_count != 0) | |
8115 | { | |
8116 | amt = max_sym_count * bed->s->sizeof_sym; | |
8117 | finfo.external_syms = bfd_malloc (amt); | |
8118 | if (finfo.external_syms == NULL) | |
8119 | goto error_return; | |
8120 | ||
8121 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8122 | finfo.internal_syms = bfd_malloc (amt); | |
8123 | if (finfo.internal_syms == NULL) | |
8124 | goto error_return; | |
8125 | ||
8126 | amt = max_sym_count * sizeof (long); | |
8127 | finfo.indices = bfd_malloc (amt); | |
8128 | if (finfo.indices == NULL) | |
8129 | goto error_return; | |
8130 | ||
8131 | amt = max_sym_count * sizeof (asection *); | |
8132 | finfo.sections = bfd_malloc (amt); | |
8133 | if (finfo.sections == NULL) | |
8134 | goto error_return; | |
8135 | } | |
8136 | ||
8137 | if (max_sym_shndx_count != 0) | |
8138 | { | |
8139 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8140 | finfo.locsym_shndx = bfd_malloc (amt); | |
8141 | if (finfo.locsym_shndx == NULL) | |
8142 | goto error_return; | |
8143 | } | |
8144 | ||
8145 | if (elf_hash_table (info)->tls_sec) | |
8146 | { | |
8147 | bfd_vma base, end = 0; | |
8148 | asection *sec; | |
8149 | ||
8150 | for (sec = elf_hash_table (info)->tls_sec; | |
8151 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
8152 | sec = sec->next) | |
8153 | { | |
eea6121a | 8154 | bfd_vma size = sec->size; |
c152c796 AM |
8155 | |
8156 | if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
8157 | { | |
8158 | struct bfd_link_order *o; | |
8159 | ||
8423293d | 8160 | for (o = sec->map_head.link_order; o != NULL; o = o->next) |
c152c796 AM |
8161 | if (size < o->offset + o->size) |
8162 | size = o->offset + o->size; | |
8163 | } | |
8164 | end = sec->vma + size; | |
8165 | } | |
8166 | base = elf_hash_table (info)->tls_sec->vma; | |
8167 | end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power); | |
8168 | elf_hash_table (info)->tls_size = end - base; | |
8169 | } | |
8170 | ||
0b52efa6 PB |
8171 | /* Reorder SHF_LINK_ORDER sections. */ |
8172 | for (o = abfd->sections; o != NULL; o = o->next) | |
8173 | { | |
8174 | if (!elf_fixup_link_order (abfd, o)) | |
8175 | return FALSE; | |
8176 | } | |
8177 | ||
c152c796 AM |
8178 | /* Since ELF permits relocations to be against local symbols, we |
8179 | must have the local symbols available when we do the relocations. | |
8180 | Since we would rather only read the local symbols once, and we | |
8181 | would rather not keep them in memory, we handle all the | |
8182 | relocations for a single input file at the same time. | |
8183 | ||
8184 | Unfortunately, there is no way to know the total number of local | |
8185 | symbols until we have seen all of them, and the local symbol | |
8186 | indices precede the global symbol indices. This means that when | |
8187 | we are generating relocatable output, and we see a reloc against | |
8188 | a global symbol, we can not know the symbol index until we have | |
8189 | finished examining all the local symbols to see which ones we are | |
8190 | going to output. To deal with this, we keep the relocations in | |
8191 | memory, and don't output them until the end of the link. This is | |
8192 | an unfortunate waste of memory, but I don't see a good way around | |
8193 | it. Fortunately, it only happens when performing a relocatable | |
8194 | link, which is not the common case. FIXME: If keep_memory is set | |
8195 | we could write the relocs out and then read them again; I don't | |
8196 | know how bad the memory loss will be. */ | |
8197 | ||
8198 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8199 | sub->output_has_begun = FALSE; | |
8200 | for (o = abfd->sections; o != NULL; o = o->next) | |
8201 | { | |
8423293d | 8202 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
8203 | { |
8204 | if (p->type == bfd_indirect_link_order | |
8205 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
8206 | == bfd_target_elf_flavour) | |
8207 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
8208 | { | |
8209 | if (! sub->output_has_begun) | |
8210 | { | |
8211 | if (! elf_link_input_bfd (&finfo, sub)) | |
8212 | goto error_return; | |
8213 | sub->output_has_begun = TRUE; | |
8214 | } | |
8215 | } | |
8216 | else if (p->type == bfd_section_reloc_link_order | |
8217 | || p->type == bfd_symbol_reloc_link_order) | |
8218 | { | |
8219 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
8220 | goto error_return; | |
8221 | } | |
8222 | else | |
8223 | { | |
8224 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
8225 | goto error_return; | |
8226 | } | |
8227 | } | |
8228 | } | |
8229 | ||
8230 | /* Output any global symbols that got converted to local in a | |
8231 | version script or due to symbol visibility. We do this in a | |
8232 | separate step since ELF requires all local symbols to appear | |
8233 | prior to any global symbols. FIXME: We should only do this if | |
8234 | some global symbols were, in fact, converted to become local. | |
8235 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
8236 | eoinfo.failed = FALSE; | |
8237 | eoinfo.finfo = &finfo; | |
8238 | eoinfo.localsyms = TRUE; | |
8239 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8240 | &eoinfo); | |
8241 | if (eoinfo.failed) | |
8242 | return FALSE; | |
8243 | ||
8244 | /* That wrote out all the local symbols. Finish up the symbol table | |
8245 | with the global symbols. Even if we want to strip everything we | |
8246 | can, we still need to deal with those global symbols that got | |
8247 | converted to local in a version script. */ | |
8248 | ||
8249 | /* The sh_info field records the index of the first non local symbol. */ | |
8250 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
8251 | ||
8252 | if (dynamic | |
8253 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
8254 | { | |
8255 | Elf_Internal_Sym sym; | |
8256 | bfd_byte *dynsym = finfo.dynsym_sec->contents; | |
8257 | long last_local = 0; | |
8258 | ||
8259 | /* Write out the section symbols for the output sections. */ | |
67687978 | 8260 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
c152c796 AM |
8261 | { |
8262 | asection *s; | |
8263 | ||
8264 | sym.st_size = 0; | |
8265 | sym.st_name = 0; | |
8266 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8267 | sym.st_other = 0; | |
8268 | ||
8269 | for (s = abfd->sections; s != NULL; s = s->next) | |
8270 | { | |
8271 | int indx; | |
8272 | bfd_byte *dest; | |
8273 | long dynindx; | |
8274 | ||
c152c796 | 8275 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
8276 | if (dynindx <= 0) |
8277 | continue; | |
8278 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
8279 | BFD_ASSERT (indx > 0); |
8280 | sym.st_shndx = indx; | |
8281 | sym.st_value = s->vma; | |
8282 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
8c37241b JJ |
8283 | if (last_local < dynindx) |
8284 | last_local = dynindx; | |
c152c796 AM |
8285 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
8286 | } | |
c152c796 AM |
8287 | } |
8288 | ||
8289 | /* Write out the local dynsyms. */ | |
8290 | if (elf_hash_table (info)->dynlocal) | |
8291 | { | |
8292 | struct elf_link_local_dynamic_entry *e; | |
8293 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
8294 | { | |
8295 | asection *s; | |
8296 | bfd_byte *dest; | |
8297 | ||
8298 | sym.st_size = e->isym.st_size; | |
8299 | sym.st_other = e->isym.st_other; | |
8300 | ||
8301 | /* Copy the internal symbol as is. | |
8302 | Note that we saved a word of storage and overwrote | |
8303 | the original st_name with the dynstr_index. */ | |
8304 | sym = e->isym; | |
8305 | ||
8306 | if (e->isym.st_shndx != SHN_UNDEF | |
8307 | && (e->isym.st_shndx < SHN_LORESERVE | |
8308 | || e->isym.st_shndx > SHN_HIRESERVE)) | |
8309 | { | |
8310 | s = bfd_section_from_elf_index (e->input_bfd, | |
8311 | e->isym.st_shndx); | |
8312 | ||
8313 | sym.st_shndx = | |
8314 | elf_section_data (s->output_section)->this_idx; | |
8315 | sym.st_value = (s->output_section->vma | |
8316 | + s->output_offset | |
8317 | + e->isym.st_value); | |
8318 | } | |
8319 | ||
8320 | if (last_local < e->dynindx) | |
8321 | last_local = e->dynindx; | |
8322 | ||
8323 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
8324 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
8325 | } | |
8326 | } | |
8327 | ||
8328 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = | |
8329 | last_local + 1; | |
8330 | } | |
8331 | ||
8332 | /* We get the global symbols from the hash table. */ | |
8333 | eoinfo.failed = FALSE; | |
8334 | eoinfo.localsyms = FALSE; | |
8335 | eoinfo.finfo = &finfo; | |
8336 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8337 | &eoinfo); | |
8338 | if (eoinfo.failed) | |
8339 | return FALSE; | |
8340 | ||
8341 | /* If backend needs to output some symbols not present in the hash | |
8342 | table, do it now. */ | |
8343 | if (bed->elf_backend_output_arch_syms) | |
8344 | { | |
8345 | typedef bfd_boolean (*out_sym_func) | |
8346 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
8347 | struct elf_link_hash_entry *); | |
8348 | ||
8349 | if (! ((*bed->elf_backend_output_arch_syms) | |
8350 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
8351 | return FALSE; | |
8352 | } | |
8353 | ||
8354 | /* Flush all symbols to the file. */ | |
8355 | if (! elf_link_flush_output_syms (&finfo, bed)) | |
8356 | return FALSE; | |
8357 | ||
8358 | /* Now we know the size of the symtab section. */ | |
8359 | off += symtab_hdr->sh_size; | |
8360 | ||
8361 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
8362 | if (symtab_shndx_hdr->sh_name != 0) | |
8363 | { | |
8364 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
8365 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
8366 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
8367 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
8368 | symtab_shndx_hdr->sh_size = amt; | |
8369 | ||
8370 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
8371 | off, TRUE); | |
8372 | ||
8373 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
8374 | || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) | |
8375 | return FALSE; | |
8376 | } | |
8377 | ||
8378 | ||
8379 | /* Finish up and write out the symbol string table (.strtab) | |
8380 | section. */ | |
8381 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
8382 | /* sh_name was set in prep_headers. */ | |
8383 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
8384 | symstrtab_hdr->sh_flags = 0; | |
8385 | symstrtab_hdr->sh_addr = 0; | |
8386 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
8387 | symstrtab_hdr->sh_entsize = 0; | |
8388 | symstrtab_hdr->sh_link = 0; | |
8389 | symstrtab_hdr->sh_info = 0; | |
8390 | /* sh_offset is set just below. */ | |
8391 | symstrtab_hdr->sh_addralign = 1; | |
8392 | ||
8393 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); | |
8394 | elf_tdata (abfd)->next_file_pos = off; | |
8395 | ||
8396 | if (bfd_get_symcount (abfd) > 0) | |
8397 | { | |
8398 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
8399 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
8400 | return FALSE; | |
8401 | } | |
8402 | ||
8403 | /* Adjust the relocs to have the correct symbol indices. */ | |
8404 | for (o = abfd->sections; o != NULL; o = o->next) | |
8405 | { | |
8406 | if ((o->flags & SEC_RELOC) == 0) | |
8407 | continue; | |
8408 | ||
8409 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, | |
8410 | elf_section_data (o)->rel_count, | |
8411 | elf_section_data (o)->rel_hashes); | |
8412 | if (elf_section_data (o)->rel_hdr2 != NULL) | |
8413 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, | |
8414 | elf_section_data (o)->rel_count2, | |
8415 | (elf_section_data (o)->rel_hashes | |
8416 | + elf_section_data (o)->rel_count)); | |
8417 | ||
8418 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
8419 | trying to swap the relocs out itself. */ | |
8420 | o->reloc_count = 0; | |
8421 | } | |
8422 | ||
8423 | if (dynamic && info->combreloc && dynobj != NULL) | |
8424 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
8425 | ||
8426 | /* If we are linking against a dynamic object, or generating a | |
8427 | shared library, finish up the dynamic linking information. */ | |
8428 | if (dynamic) | |
8429 | { | |
8430 | bfd_byte *dyncon, *dynconend; | |
8431 | ||
8432 | /* Fix up .dynamic entries. */ | |
8433 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
8434 | BFD_ASSERT (o != NULL); | |
8435 | ||
8436 | dyncon = o->contents; | |
eea6121a | 8437 | dynconend = o->contents + o->size; |
c152c796 AM |
8438 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
8439 | { | |
8440 | Elf_Internal_Dyn dyn; | |
8441 | const char *name; | |
8442 | unsigned int type; | |
8443 | ||
8444 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
8445 | ||
8446 | switch (dyn.d_tag) | |
8447 | { | |
8448 | default: | |
8449 | continue; | |
8450 | case DT_NULL: | |
8451 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
8452 | { | |
8453 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
8454 | { | |
8455 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
8456 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
8457 | default: continue; | |
8458 | } | |
8459 | dyn.d_un.d_val = relativecount; | |
8460 | relativecount = 0; | |
8461 | break; | |
8462 | } | |
8463 | continue; | |
8464 | ||
8465 | case DT_INIT: | |
8466 | name = info->init_function; | |
8467 | goto get_sym; | |
8468 | case DT_FINI: | |
8469 | name = info->fini_function; | |
8470 | get_sym: | |
8471 | { | |
8472 | struct elf_link_hash_entry *h; | |
8473 | ||
8474 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
8475 | FALSE, FALSE, TRUE); | |
8476 | if (h != NULL | |
8477 | && (h->root.type == bfd_link_hash_defined | |
8478 | || h->root.type == bfd_link_hash_defweak)) | |
8479 | { | |
8480 | dyn.d_un.d_val = h->root.u.def.value; | |
8481 | o = h->root.u.def.section; | |
8482 | if (o->output_section != NULL) | |
8483 | dyn.d_un.d_val += (o->output_section->vma | |
8484 | + o->output_offset); | |
8485 | else | |
8486 | { | |
8487 | /* The symbol is imported from another shared | |
8488 | library and does not apply to this one. */ | |
8489 | dyn.d_un.d_val = 0; | |
8490 | } | |
8491 | break; | |
8492 | } | |
8493 | } | |
8494 | continue; | |
8495 | ||
8496 | case DT_PREINIT_ARRAYSZ: | |
8497 | name = ".preinit_array"; | |
8498 | goto get_size; | |
8499 | case DT_INIT_ARRAYSZ: | |
8500 | name = ".init_array"; | |
8501 | goto get_size; | |
8502 | case DT_FINI_ARRAYSZ: | |
8503 | name = ".fini_array"; | |
8504 | get_size: | |
8505 | o = bfd_get_section_by_name (abfd, name); | |
8506 | if (o == NULL) | |
8507 | { | |
8508 | (*_bfd_error_handler) | |
d003868e | 8509 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
8510 | goto error_return; |
8511 | } | |
eea6121a | 8512 | if (o->size == 0) |
c152c796 AM |
8513 | (*_bfd_error_handler) |
8514 | (_("warning: %s section has zero size"), name); | |
eea6121a | 8515 | dyn.d_un.d_val = o->size; |
c152c796 AM |
8516 | break; |
8517 | ||
8518 | case DT_PREINIT_ARRAY: | |
8519 | name = ".preinit_array"; | |
8520 | goto get_vma; | |
8521 | case DT_INIT_ARRAY: | |
8522 | name = ".init_array"; | |
8523 | goto get_vma; | |
8524 | case DT_FINI_ARRAY: | |
8525 | name = ".fini_array"; | |
8526 | goto get_vma; | |
8527 | ||
8528 | case DT_HASH: | |
8529 | name = ".hash"; | |
8530 | goto get_vma; | |
8531 | case DT_STRTAB: | |
8532 | name = ".dynstr"; | |
8533 | goto get_vma; | |
8534 | case DT_SYMTAB: | |
8535 | name = ".dynsym"; | |
8536 | goto get_vma; | |
8537 | case DT_VERDEF: | |
8538 | name = ".gnu.version_d"; | |
8539 | goto get_vma; | |
8540 | case DT_VERNEED: | |
8541 | name = ".gnu.version_r"; | |
8542 | goto get_vma; | |
8543 | case DT_VERSYM: | |
8544 | name = ".gnu.version"; | |
8545 | get_vma: | |
8546 | o = bfd_get_section_by_name (abfd, name); | |
8547 | if (o == NULL) | |
8548 | { | |
8549 | (*_bfd_error_handler) | |
d003868e | 8550 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
8551 | goto error_return; |
8552 | } | |
8553 | dyn.d_un.d_ptr = o->vma; | |
8554 | break; | |
8555 | ||
8556 | case DT_REL: | |
8557 | case DT_RELA: | |
8558 | case DT_RELSZ: | |
8559 | case DT_RELASZ: | |
8560 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
8561 | type = SHT_REL; | |
8562 | else | |
8563 | type = SHT_RELA; | |
8564 | dyn.d_un.d_val = 0; | |
8565 | for (i = 1; i < elf_numsections (abfd); i++) | |
8566 | { | |
8567 | Elf_Internal_Shdr *hdr; | |
8568 | ||
8569 | hdr = elf_elfsections (abfd)[i]; | |
8570 | if (hdr->sh_type == type | |
8571 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
8572 | { | |
8573 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
8574 | dyn.d_un.d_val += hdr->sh_size; | |
8575 | else | |
8576 | { | |
8577 | if (dyn.d_un.d_val == 0 | |
8578 | || hdr->sh_addr < dyn.d_un.d_val) | |
8579 | dyn.d_un.d_val = hdr->sh_addr; | |
8580 | } | |
8581 | } | |
8582 | } | |
8583 | break; | |
8584 | } | |
8585 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
8586 | } | |
8587 | } | |
8588 | ||
8589 | /* If we have created any dynamic sections, then output them. */ | |
8590 | if (dynobj != NULL) | |
8591 | { | |
8592 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
8593 | goto error_return; | |
8594 | ||
8595 | for (o = dynobj->sections; o != NULL; o = o->next) | |
8596 | { | |
8597 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 8598 | || o->size == 0 |
c152c796 AM |
8599 | || o->output_section == bfd_abs_section_ptr) |
8600 | continue; | |
8601 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
8602 | { | |
8603 | /* At this point, we are only interested in sections | |
8604 | created by _bfd_elf_link_create_dynamic_sections. */ | |
8605 | continue; | |
8606 | } | |
3722b82f AM |
8607 | if (elf_hash_table (info)->stab_info.stabstr == o) |
8608 | continue; | |
eea6121a AM |
8609 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
8610 | continue; | |
c152c796 AM |
8611 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
8612 | != SHT_STRTAB) | |
8613 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
8614 | { | |
8615 | if (! bfd_set_section_contents (abfd, o->output_section, | |
8616 | o->contents, | |
8617 | (file_ptr) o->output_offset, | |
eea6121a | 8618 | o->size)) |
c152c796 AM |
8619 | goto error_return; |
8620 | } | |
8621 | else | |
8622 | { | |
8623 | /* The contents of the .dynstr section are actually in a | |
8624 | stringtab. */ | |
8625 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
8626 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
8627 | || ! _bfd_elf_strtab_emit (abfd, | |
8628 | elf_hash_table (info)->dynstr)) | |
8629 | goto error_return; | |
8630 | } | |
8631 | } | |
8632 | } | |
8633 | ||
8634 | if (info->relocatable) | |
8635 | { | |
8636 | bfd_boolean failed = FALSE; | |
8637 | ||
8638 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
8639 | if (failed) | |
8640 | goto error_return; | |
8641 | } | |
8642 | ||
8643 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 8644 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
8645 | { |
8646 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
8647 | goto error_return; | |
8648 | } | |
8649 | ||
8650 | if (info->eh_frame_hdr) | |
8651 | { | |
8652 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) | |
8653 | goto error_return; | |
8654 | } | |
8655 | ||
8656 | if (finfo.symstrtab != NULL) | |
8657 | _bfd_stringtab_free (finfo.symstrtab); | |
8658 | if (finfo.contents != NULL) | |
8659 | free (finfo.contents); | |
8660 | if (finfo.external_relocs != NULL) | |
8661 | free (finfo.external_relocs); | |
8662 | if (finfo.internal_relocs != NULL) | |
8663 | free (finfo.internal_relocs); | |
8664 | if (finfo.external_syms != NULL) | |
8665 | free (finfo.external_syms); | |
8666 | if (finfo.locsym_shndx != NULL) | |
8667 | free (finfo.locsym_shndx); | |
8668 | if (finfo.internal_syms != NULL) | |
8669 | free (finfo.internal_syms); | |
8670 | if (finfo.indices != NULL) | |
8671 | free (finfo.indices); | |
8672 | if (finfo.sections != NULL) | |
8673 | free (finfo.sections); | |
8674 | if (finfo.symbuf != NULL) | |
8675 | free (finfo.symbuf); | |
8676 | if (finfo.symshndxbuf != NULL) | |
8677 | free (finfo.symshndxbuf); | |
8678 | for (o = abfd->sections; o != NULL; o = o->next) | |
8679 | { | |
8680 | if ((o->flags & SEC_RELOC) != 0 | |
8681 | && elf_section_data (o)->rel_hashes != NULL) | |
8682 | free (elf_section_data (o)->rel_hashes); | |
8683 | } | |
8684 | ||
8685 | elf_tdata (abfd)->linker = TRUE; | |
8686 | ||
8687 | return TRUE; | |
8688 | ||
8689 | error_return: | |
8690 | if (finfo.symstrtab != NULL) | |
8691 | _bfd_stringtab_free (finfo.symstrtab); | |
8692 | if (finfo.contents != NULL) | |
8693 | free (finfo.contents); | |
8694 | if (finfo.external_relocs != NULL) | |
8695 | free (finfo.external_relocs); | |
8696 | if (finfo.internal_relocs != NULL) | |
8697 | free (finfo.internal_relocs); | |
8698 | if (finfo.external_syms != NULL) | |
8699 | free (finfo.external_syms); | |
8700 | if (finfo.locsym_shndx != NULL) | |
8701 | free (finfo.locsym_shndx); | |
8702 | if (finfo.internal_syms != NULL) | |
8703 | free (finfo.internal_syms); | |
8704 | if (finfo.indices != NULL) | |
8705 | free (finfo.indices); | |
8706 | if (finfo.sections != NULL) | |
8707 | free (finfo.sections); | |
8708 | if (finfo.symbuf != NULL) | |
8709 | free (finfo.symbuf); | |
8710 | if (finfo.symshndxbuf != NULL) | |
8711 | free (finfo.symshndxbuf); | |
8712 | for (o = abfd->sections; o != NULL; o = o->next) | |
8713 | { | |
8714 | if ((o->flags & SEC_RELOC) != 0 | |
8715 | && elf_section_data (o)->rel_hashes != NULL) | |
8716 | free (elf_section_data (o)->rel_hashes); | |
8717 | } | |
8718 | ||
8719 | return FALSE; | |
8720 | } | |
8721 | \f | |
8722 | /* Garbage collect unused sections. */ | |
8723 | ||
8724 | /* The mark phase of garbage collection. For a given section, mark | |
8725 | it and any sections in this section's group, and all the sections | |
8726 | which define symbols to which it refers. */ | |
8727 | ||
8728 | typedef asection * (*gc_mark_hook_fn) | |
8729 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
8730 | struct elf_link_hash_entry *, Elf_Internal_Sym *); | |
8731 | ||
ccfa59ea AM |
8732 | bfd_boolean |
8733 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
8734 | asection *sec, | |
8735 | gc_mark_hook_fn gc_mark_hook) | |
c152c796 AM |
8736 | { |
8737 | bfd_boolean ret; | |
39c2f51b | 8738 | bfd_boolean is_eh; |
c152c796 AM |
8739 | asection *group_sec; |
8740 | ||
8741 | sec->gc_mark = 1; | |
8742 | ||
8743 | /* Mark all the sections in the group. */ | |
8744 | group_sec = elf_section_data (sec)->next_in_group; | |
8745 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 8746 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
8747 | return FALSE; |
8748 | ||
8749 | /* Look through the section relocs. */ | |
8750 | ret = TRUE; | |
39c2f51b | 8751 | is_eh = strcmp (sec->name, ".eh_frame") == 0; |
c152c796 AM |
8752 | if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0) |
8753 | { | |
8754 | Elf_Internal_Rela *relstart, *rel, *relend; | |
8755 | Elf_Internal_Shdr *symtab_hdr; | |
8756 | struct elf_link_hash_entry **sym_hashes; | |
8757 | size_t nlocsyms; | |
8758 | size_t extsymoff; | |
8759 | bfd *input_bfd = sec->owner; | |
8760 | const struct elf_backend_data *bed = get_elf_backend_data (input_bfd); | |
8761 | Elf_Internal_Sym *isym = NULL; | |
8762 | int r_sym_shift; | |
8763 | ||
8764 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
8765 | sym_hashes = elf_sym_hashes (input_bfd); | |
8766 | ||
8767 | /* Read the local symbols. */ | |
8768 | if (elf_bad_symtab (input_bfd)) | |
8769 | { | |
8770 | nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
8771 | extsymoff = 0; | |
8772 | } | |
8773 | else | |
8774 | extsymoff = nlocsyms = symtab_hdr->sh_info; | |
8775 | ||
8776 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
8777 | if (isym == NULL && nlocsyms != 0) | |
8778 | { | |
8779 | isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0, | |
8780 | NULL, NULL, NULL); | |
8781 | if (isym == NULL) | |
8782 | return FALSE; | |
8783 | } | |
8784 | ||
8785 | /* Read the relocations. */ | |
8786 | relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL, | |
8787 | info->keep_memory); | |
8788 | if (relstart == NULL) | |
8789 | { | |
8790 | ret = FALSE; | |
8791 | goto out1; | |
8792 | } | |
8793 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
8794 | ||
8795 | if (bed->s->arch_size == 32) | |
8796 | r_sym_shift = 8; | |
8797 | else | |
8798 | r_sym_shift = 32; | |
8799 | ||
8800 | for (rel = relstart; rel < relend; rel++) | |
8801 | { | |
8802 | unsigned long r_symndx; | |
8803 | asection *rsec; | |
8804 | struct elf_link_hash_entry *h; | |
8805 | ||
8806 | r_symndx = rel->r_info >> r_sym_shift; | |
8807 | if (r_symndx == 0) | |
8808 | continue; | |
8809 | ||
8810 | if (r_symndx >= nlocsyms | |
8811 | || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL) | |
8812 | { | |
8813 | h = sym_hashes[r_symndx - extsymoff]; | |
20f0a1ad AM |
8814 | while (h->root.type == bfd_link_hash_indirect |
8815 | || h->root.type == bfd_link_hash_warning) | |
8816 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 AM |
8817 | rsec = (*gc_mark_hook) (sec, info, rel, h, NULL); |
8818 | } | |
8819 | else | |
8820 | { | |
8821 | rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]); | |
8822 | } | |
8823 | ||
8824 | if (rsec && !rsec->gc_mark) | |
8825 | { | |
8826 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
8827 | rsec->gc_mark = 1; | |
39c2f51b AM |
8828 | else if (is_eh) |
8829 | rsec->gc_mark_from_eh = 1; | |
ccfa59ea | 8830 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) |
c152c796 AM |
8831 | { |
8832 | ret = FALSE; | |
8833 | goto out2; | |
8834 | } | |
8835 | } | |
8836 | } | |
8837 | ||
8838 | out2: | |
8839 | if (elf_section_data (sec)->relocs != relstart) | |
8840 | free (relstart); | |
8841 | out1: | |
8842 | if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym) | |
8843 | { | |
8844 | if (! info->keep_memory) | |
8845 | free (isym); | |
8846 | else | |
8847 | symtab_hdr->contents = (unsigned char *) isym; | |
8848 | } | |
8849 | } | |
8850 | ||
8851 | return ret; | |
8852 | } | |
8853 | ||
8854 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ | |
8855 | ||
8856 | static bfd_boolean | |
8857 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr) | |
8858 | { | |
8859 | int *idx = idxptr; | |
8860 | ||
8861 | if (h->root.type == bfd_link_hash_warning) | |
8862 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8863 | ||
8864 | if (h->dynindx != -1 | |
8865 | && ((h->root.type != bfd_link_hash_defined | |
8866 | && h->root.type != bfd_link_hash_defweak) | |
8867 | || h->root.u.def.section->gc_mark)) | |
8868 | h->dynindx = (*idx)++; | |
8869 | ||
8870 | return TRUE; | |
8871 | } | |
8872 | ||
8873 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
8874 | ||
8875 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
8876 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
8877 | ||
8878 | static bfd_boolean | |
8879 | elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook) | |
8880 | { | |
8881 | bfd *sub; | |
8882 | ||
8883 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8884 | { | |
8885 | asection *o; | |
8886 | ||
8887 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
8888 | continue; | |
8889 | ||
8890 | for (o = sub->sections; o != NULL; o = o->next) | |
8891 | { | |
7c2c8505 AM |
8892 | /* Keep debug and special sections. */ |
8893 | if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 | |
8894 | || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0) | |
c152c796 AM |
8895 | o->gc_mark = 1; |
8896 | ||
8897 | if (o->gc_mark) | |
8898 | continue; | |
8899 | ||
39c2f51b AM |
8900 | /* Keep .gcc_except_table.* if the associated .text.* is |
8901 | marked. This isn't very nice, but the proper solution, | |
8902 | splitting .eh_frame up and using comdat doesn't pan out | |
8903 | easily due to needing special relocs to handle the | |
8904 | difference of two symbols in separate sections. | |
8905 | Don't keep code sections referenced by .eh_frame. */ | |
8906 | if (o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0) | |
8907 | { | |
8908 | if (strncmp (o->name, ".gcc_except_table.", 18) == 0) | |
8909 | { | |
8910 | unsigned long len; | |
8911 | char *fn_name; | |
8912 | asection *fn_text; | |
8913 | ||
8914 | len = strlen (o->name + 18) + 1; | |
8915 | fn_name = bfd_malloc (len + 6); | |
8916 | if (fn_name == NULL) | |
8917 | return FALSE; | |
8918 | memcpy (fn_name, ".text.", 6); | |
8919 | memcpy (fn_name + 6, o->name + 18, len); | |
8920 | fn_text = bfd_get_section_by_name (sub, fn_name); | |
8921 | free (fn_name); | |
8922 | if (fn_text != NULL && fn_text->gc_mark) | |
8923 | o->gc_mark = 1; | |
8924 | } | |
8925 | ||
8926 | /* If not using specially named exception table section, | |
8927 | then keep whatever we are using. */ | |
8928 | else | |
8929 | o->gc_mark = 1; | |
8930 | ||
8931 | if (o->gc_mark) | |
8932 | continue; | |
8933 | } | |
8934 | ||
c152c796 AM |
8935 | /* Skip sweeping sections already excluded. */ |
8936 | if (o->flags & SEC_EXCLUDE) | |
8937 | continue; | |
8938 | ||
8939 | /* Since this is early in the link process, it is simple | |
8940 | to remove a section from the output. */ | |
8941 | o->flags |= SEC_EXCLUDE; | |
8942 | ||
8943 | /* But we also have to update some of the relocation | |
8944 | info we collected before. */ | |
8945 | if (gc_sweep_hook | |
e8aaee2a AM |
8946 | && (o->flags & SEC_RELOC) != 0 |
8947 | && o->reloc_count > 0 | |
8948 | && !bfd_is_abs_section (o->output_section)) | |
c152c796 AM |
8949 | { |
8950 | Elf_Internal_Rela *internal_relocs; | |
8951 | bfd_boolean r; | |
8952 | ||
8953 | internal_relocs | |
8954 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
8955 | info->keep_memory); | |
8956 | if (internal_relocs == NULL) | |
8957 | return FALSE; | |
8958 | ||
8959 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
8960 | ||
8961 | if (elf_section_data (o)->relocs != internal_relocs) | |
8962 | free (internal_relocs); | |
8963 | ||
8964 | if (!r) | |
8965 | return FALSE; | |
8966 | } | |
8967 | } | |
8968 | } | |
8969 | ||
8970 | /* Remove the symbols that were in the swept sections from the dynamic | |
8971 | symbol table. GCFIXME: Anyone know how to get them out of the | |
8972 | static symbol table as well? */ | |
8973 | { | |
8974 | int i = 0; | |
8975 | ||
8976 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i); | |
8977 | ||
8978 | elf_hash_table (info)->dynsymcount = i; | |
8979 | } | |
8980 | ||
8981 | return TRUE; | |
8982 | } | |
8983 | ||
8984 | /* Propagate collected vtable information. This is called through | |
8985 | elf_link_hash_traverse. */ | |
8986 | ||
8987 | static bfd_boolean | |
8988 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
8989 | { | |
8990 | if (h->root.type == bfd_link_hash_warning) | |
8991 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8992 | ||
8993 | /* Those that are not vtables. */ | |
f6e332e6 | 8994 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
8995 | return TRUE; |
8996 | ||
8997 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 8998 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
8999 | return TRUE; |
9000 | ||
9001 | /* If we've already been done, exit. */ | |
f6e332e6 | 9002 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
9003 | return TRUE; |
9004 | ||
9005 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 9006 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 9007 | |
f6e332e6 | 9008 | if (h->vtable->used == NULL) |
c152c796 AM |
9009 | { |
9010 | /* None of this table's entries were referenced. Re-use the | |
9011 | parent's table. */ | |
f6e332e6 AM |
9012 | h->vtable->used = h->vtable->parent->vtable->used; |
9013 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
9014 | } |
9015 | else | |
9016 | { | |
9017 | size_t n; | |
9018 | bfd_boolean *cu, *pu; | |
9019 | ||
9020 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 9021 | cu = h->vtable->used; |
c152c796 | 9022 | cu[-1] = TRUE; |
f6e332e6 | 9023 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
9024 | if (pu != NULL) |
9025 | { | |
9026 | const struct elf_backend_data *bed; | |
9027 | unsigned int log_file_align; | |
9028 | ||
9029 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
9030 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 9031 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
9032 | while (n--) |
9033 | { | |
9034 | if (*pu) | |
9035 | *cu = TRUE; | |
9036 | pu++; | |
9037 | cu++; | |
9038 | } | |
9039 | } | |
9040 | } | |
9041 | ||
9042 | return TRUE; | |
9043 | } | |
9044 | ||
9045 | static bfd_boolean | |
9046 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
9047 | { | |
9048 | asection *sec; | |
9049 | bfd_vma hstart, hend; | |
9050 | Elf_Internal_Rela *relstart, *relend, *rel; | |
9051 | const struct elf_backend_data *bed; | |
9052 | unsigned int log_file_align; | |
9053 | ||
9054 | if (h->root.type == bfd_link_hash_warning) | |
9055 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9056 | ||
9057 | /* Take care of both those symbols that do not describe vtables as | |
9058 | well as those that are not loaded. */ | |
f6e332e6 | 9059 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
9060 | return TRUE; |
9061 | ||
9062 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
9063 | || h->root.type == bfd_link_hash_defweak); | |
9064 | ||
9065 | sec = h->root.u.def.section; | |
9066 | hstart = h->root.u.def.value; | |
9067 | hend = hstart + h->size; | |
9068 | ||
9069 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
9070 | if (!relstart) | |
9071 | return *(bfd_boolean *) okp = FALSE; | |
9072 | bed = get_elf_backend_data (sec->owner); | |
9073 | log_file_align = bed->s->log_file_align; | |
9074 | ||
9075 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
9076 | ||
9077 | for (rel = relstart; rel < relend; ++rel) | |
9078 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
9079 | { | |
9080 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
9081 | if (h->vtable->used |
9082 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
9083 | { |
9084 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 9085 | if (h->vtable->used[entry]) |
c152c796 AM |
9086 | continue; |
9087 | } | |
9088 | /* Otherwise, kill it. */ | |
9089 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
9090 | } | |
9091 | ||
9092 | return TRUE; | |
9093 | } | |
9094 | ||
715df9b8 EB |
9095 | /* Mark sections containing dynamically referenced symbols. This is called |
9096 | through elf_link_hash_traverse. */ | |
9097 | ||
9098 | static bfd_boolean | |
9099 | elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, | |
9100 | void *okp ATTRIBUTE_UNUSED) | |
9101 | { | |
9102 | if (h->root.type == bfd_link_hash_warning) | |
9103 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9104 | ||
9105 | if ((h->root.type == bfd_link_hash_defined | |
9106 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 9107 | && h->ref_dynamic) |
715df9b8 EB |
9108 | h->root.u.def.section->flags |= SEC_KEEP; |
9109 | ||
9110 | return TRUE; | |
9111 | } | |
3b36f7e6 | 9112 | |
c152c796 AM |
9113 | /* Do mark and sweep of unused sections. */ |
9114 | ||
9115 | bfd_boolean | |
9116 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
9117 | { | |
9118 | bfd_boolean ok = TRUE; | |
9119 | bfd *sub; | |
9120 | asection * (*gc_mark_hook) | |
9121 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
9122 | struct elf_link_hash_entry *h, Elf_Internal_Sym *); | |
9123 | ||
57316bff | 9124 | if (!info->gc_sections) |
0e58fcf3 | 9125 | return bfd_generic_gc_sections (abfd, info); |
57316bff | 9126 | |
c152c796 AM |
9127 | if (!get_elf_backend_data (abfd)->can_gc_sections |
9128 | || info->relocatable | |
9129 | || info->emitrelocations | |
715df9b8 EB |
9130 | || info->shared |
9131 | || !is_elf_hash_table (info->hash)) | |
c152c796 AM |
9132 | { |
9133 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
9134 | return TRUE; | |
9135 | } | |
9136 | ||
9137 | /* Apply transitive closure to the vtable entry usage info. */ | |
9138 | elf_link_hash_traverse (elf_hash_table (info), | |
9139 | elf_gc_propagate_vtable_entries_used, | |
9140 | &ok); | |
9141 | if (!ok) | |
9142 | return FALSE; | |
9143 | ||
9144 | /* Kill the vtable relocations that were not used. */ | |
9145 | elf_link_hash_traverse (elf_hash_table (info), | |
9146 | elf_gc_smash_unused_vtentry_relocs, | |
9147 | &ok); | |
9148 | if (!ok) | |
9149 | return FALSE; | |
9150 | ||
715df9b8 EB |
9151 | /* Mark dynamically referenced symbols. */ |
9152 | if (elf_hash_table (info)->dynamic_sections_created) | |
9153 | elf_link_hash_traverse (elf_hash_table (info), | |
9154 | elf_gc_mark_dynamic_ref_symbol, | |
9155 | &ok); | |
9156 | if (!ok) | |
9157 | return FALSE; | |
c152c796 | 9158 | |
715df9b8 | 9159 | /* Grovel through relocs to find out who stays ... */ |
c152c796 AM |
9160 | gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook; |
9161 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
9162 | { | |
9163 | asection *o; | |
9164 | ||
9165 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
9166 | continue; | |
9167 | ||
9168 | for (o = sub->sections; o != NULL; o = o->next) | |
39c2f51b AM |
9169 | if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark) |
9170 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
9171 | return FALSE; | |
c152c796 AM |
9172 | } |
9173 | ||
9174 | /* ... and mark SEC_EXCLUDE for those that go. */ | |
9175 | if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook)) | |
9176 | return FALSE; | |
9177 | ||
9178 | return TRUE; | |
9179 | } | |
9180 | \f | |
9181 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
9182 | ||
9183 | bfd_boolean | |
9184 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
9185 | asection *sec, | |
9186 | struct elf_link_hash_entry *h, | |
9187 | bfd_vma offset) | |
9188 | { | |
9189 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
9190 | struct elf_link_hash_entry **search, *child; | |
9191 | bfd_size_type extsymcount; | |
9192 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9193 | ||
9194 | /* The sh_info field of the symtab header tells us where the | |
9195 | external symbols start. We don't care about the local symbols at | |
9196 | this point. */ | |
9197 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
9198 | if (!elf_bad_symtab (abfd)) | |
9199 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
9200 | ||
9201 | sym_hashes = elf_sym_hashes (abfd); | |
9202 | sym_hashes_end = sym_hashes + extsymcount; | |
9203 | ||
9204 | /* Hunt down the child symbol, which is in this section at the same | |
9205 | offset as the relocation. */ | |
9206 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
9207 | { | |
9208 | if ((child = *search) != NULL | |
9209 | && (child->root.type == bfd_link_hash_defined | |
9210 | || child->root.type == bfd_link_hash_defweak) | |
9211 | && child->root.u.def.section == sec | |
9212 | && child->root.u.def.value == offset) | |
9213 | goto win; | |
9214 | } | |
9215 | ||
d003868e AM |
9216 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
9217 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
9218 | bfd_set_error (bfd_error_invalid_operation); |
9219 | return FALSE; | |
9220 | ||
9221 | win: | |
f6e332e6 AM |
9222 | if (!child->vtable) |
9223 | { | |
9224 | child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable)); | |
9225 | if (!child->vtable) | |
9226 | return FALSE; | |
9227 | } | |
c152c796 AM |
9228 | if (!h) |
9229 | { | |
9230 | /* This *should* only be the absolute section. It could potentially | |
9231 | be that someone has defined a non-global vtable though, which | |
9232 | would be bad. It isn't worth paging in the local symbols to be | |
9233 | sure though; that case should simply be handled by the assembler. */ | |
9234 | ||
f6e332e6 | 9235 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
9236 | } |
9237 | else | |
f6e332e6 | 9238 | child->vtable->parent = h; |
c152c796 AM |
9239 | |
9240 | return TRUE; | |
9241 | } | |
9242 | ||
9243 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
9244 | ||
9245 | bfd_boolean | |
9246 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
9247 | asection *sec ATTRIBUTE_UNUSED, | |
9248 | struct elf_link_hash_entry *h, | |
9249 | bfd_vma addend) | |
9250 | { | |
9251 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9252 | unsigned int log_file_align = bed->s->log_file_align; | |
9253 | ||
f6e332e6 AM |
9254 | if (!h->vtable) |
9255 | { | |
9256 | h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable)); | |
9257 | if (!h->vtable) | |
9258 | return FALSE; | |
9259 | } | |
9260 | ||
9261 | if (addend >= h->vtable->size) | |
c152c796 AM |
9262 | { |
9263 | size_t size, bytes, file_align; | |
f6e332e6 | 9264 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
9265 | |
9266 | /* While the symbol is undefined, we have to be prepared to handle | |
9267 | a zero size. */ | |
9268 | file_align = 1 << log_file_align; | |
9269 | if (h->root.type == bfd_link_hash_undefined) | |
9270 | size = addend + file_align; | |
9271 | else | |
9272 | { | |
9273 | size = h->size; | |
9274 | if (addend >= size) | |
9275 | { | |
9276 | /* Oops! We've got a reference past the defined end of | |
9277 | the table. This is probably a bug -- shall we warn? */ | |
9278 | size = addend + file_align; | |
9279 | } | |
9280 | } | |
9281 | size = (size + file_align - 1) & -file_align; | |
9282 | ||
9283 | /* Allocate one extra entry for use as a "done" flag for the | |
9284 | consolidation pass. */ | |
9285 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
9286 | ||
9287 | if (ptr) | |
9288 | { | |
9289 | ptr = bfd_realloc (ptr - 1, bytes); | |
9290 | ||
9291 | if (ptr != NULL) | |
9292 | { | |
9293 | size_t oldbytes; | |
9294 | ||
f6e332e6 | 9295 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
9296 | * sizeof (bfd_boolean)); |
9297 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
9298 | } | |
9299 | } | |
9300 | else | |
9301 | ptr = bfd_zmalloc (bytes); | |
9302 | ||
9303 | if (ptr == NULL) | |
9304 | return FALSE; | |
9305 | ||
9306 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
9307 | h->vtable->used = ptr + 1; |
9308 | h->vtable->size = size; | |
c152c796 AM |
9309 | } |
9310 | ||
f6e332e6 | 9311 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
9312 | |
9313 | return TRUE; | |
9314 | } | |
9315 | ||
9316 | struct alloc_got_off_arg { | |
9317 | bfd_vma gotoff; | |
9318 | unsigned int got_elt_size; | |
9319 | }; | |
9320 | ||
9321 | /* We need a special top-level link routine to convert got reference counts | |
9322 | to real got offsets. */ | |
9323 | ||
9324 | static bfd_boolean | |
9325 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
9326 | { | |
9327 | struct alloc_got_off_arg *gofarg = arg; | |
9328 | ||
9329 | if (h->root.type == bfd_link_hash_warning) | |
9330 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9331 | ||
9332 | if (h->got.refcount > 0) | |
9333 | { | |
9334 | h->got.offset = gofarg->gotoff; | |
9335 | gofarg->gotoff += gofarg->got_elt_size; | |
9336 | } | |
9337 | else | |
9338 | h->got.offset = (bfd_vma) -1; | |
9339 | ||
9340 | return TRUE; | |
9341 | } | |
9342 | ||
9343 | /* And an accompanying bit to work out final got entry offsets once | |
9344 | we're done. Should be called from final_link. */ | |
9345 | ||
9346 | bfd_boolean | |
9347 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
9348 | struct bfd_link_info *info) | |
9349 | { | |
9350 | bfd *i; | |
9351 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9352 | bfd_vma gotoff; | |
9353 | unsigned int got_elt_size = bed->s->arch_size / 8; | |
9354 | struct alloc_got_off_arg gofarg; | |
9355 | ||
9356 | if (! is_elf_hash_table (info->hash)) | |
9357 | return FALSE; | |
9358 | ||
9359 | /* The GOT offset is relative to the .got section, but the GOT header is | |
9360 | put into the .got.plt section, if the backend uses it. */ | |
9361 | if (bed->want_got_plt) | |
9362 | gotoff = 0; | |
9363 | else | |
9364 | gotoff = bed->got_header_size; | |
9365 | ||
9366 | /* Do the local .got entries first. */ | |
9367 | for (i = info->input_bfds; i; i = i->link_next) | |
9368 | { | |
9369 | bfd_signed_vma *local_got; | |
9370 | bfd_size_type j, locsymcount; | |
9371 | Elf_Internal_Shdr *symtab_hdr; | |
9372 | ||
9373 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
9374 | continue; | |
9375 | ||
9376 | local_got = elf_local_got_refcounts (i); | |
9377 | if (!local_got) | |
9378 | continue; | |
9379 | ||
9380 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
9381 | if (elf_bad_symtab (i)) | |
9382 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9383 | else | |
9384 | locsymcount = symtab_hdr->sh_info; | |
9385 | ||
9386 | for (j = 0; j < locsymcount; ++j) | |
9387 | { | |
9388 | if (local_got[j] > 0) | |
9389 | { | |
9390 | local_got[j] = gotoff; | |
9391 | gotoff += got_elt_size; | |
9392 | } | |
9393 | else | |
9394 | local_got[j] = (bfd_vma) -1; | |
9395 | } | |
9396 | } | |
9397 | ||
9398 | /* Then the global .got entries. .plt refcounts are handled by | |
9399 | adjust_dynamic_symbol */ | |
9400 | gofarg.gotoff = gotoff; | |
9401 | gofarg.got_elt_size = got_elt_size; | |
9402 | elf_link_hash_traverse (elf_hash_table (info), | |
9403 | elf_gc_allocate_got_offsets, | |
9404 | &gofarg); | |
9405 | return TRUE; | |
9406 | } | |
9407 | ||
9408 | /* Many folk need no more in the way of final link than this, once | |
9409 | got entry reference counting is enabled. */ | |
9410 | ||
9411 | bfd_boolean | |
9412 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
9413 | { | |
9414 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
9415 | return FALSE; | |
9416 | ||
9417 | /* Invoke the regular ELF backend linker to do all the work. */ | |
9418 | return bfd_elf_final_link (abfd, info); | |
9419 | } | |
9420 | ||
9421 | bfd_boolean | |
9422 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
9423 | { | |
9424 | struct elf_reloc_cookie *rcookie = cookie; | |
9425 | ||
9426 | if (rcookie->bad_symtab) | |
9427 | rcookie->rel = rcookie->rels; | |
9428 | ||
9429 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
9430 | { | |
9431 | unsigned long r_symndx; | |
9432 | ||
9433 | if (! rcookie->bad_symtab) | |
9434 | if (rcookie->rel->r_offset > offset) | |
9435 | return FALSE; | |
9436 | if (rcookie->rel->r_offset != offset) | |
9437 | continue; | |
9438 | ||
9439 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
9440 | if (r_symndx == SHN_UNDEF) | |
9441 | return TRUE; | |
9442 | ||
9443 | if (r_symndx >= rcookie->locsymcount | |
9444 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
9445 | { | |
9446 | struct elf_link_hash_entry *h; | |
9447 | ||
9448 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
9449 | ||
9450 | while (h->root.type == bfd_link_hash_indirect | |
9451 | || h->root.type == bfd_link_hash_warning) | |
9452 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9453 | ||
9454 | if ((h->root.type == bfd_link_hash_defined | |
9455 | || h->root.type == bfd_link_hash_defweak) | |
9456 | && elf_discarded_section (h->root.u.def.section)) | |
9457 | return TRUE; | |
9458 | else | |
9459 | return FALSE; | |
9460 | } | |
9461 | else | |
9462 | { | |
9463 | /* It's not a relocation against a global symbol, | |
9464 | but it could be a relocation against a local | |
9465 | symbol for a discarded section. */ | |
9466 | asection *isec; | |
9467 | Elf_Internal_Sym *isym; | |
9468 | ||
9469 | /* Need to: get the symbol; get the section. */ | |
9470 | isym = &rcookie->locsyms[r_symndx]; | |
9471 | if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
9472 | { | |
9473 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); | |
9474 | if (isec != NULL && elf_discarded_section (isec)) | |
9475 | return TRUE; | |
9476 | } | |
9477 | } | |
9478 | return FALSE; | |
9479 | } | |
9480 | return FALSE; | |
9481 | } | |
9482 | ||
9483 | /* Discard unneeded references to discarded sections. | |
9484 | Returns TRUE if any section's size was changed. */ | |
9485 | /* This function assumes that the relocations are in sorted order, | |
9486 | which is true for all known assemblers. */ | |
9487 | ||
9488 | bfd_boolean | |
9489 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) | |
9490 | { | |
9491 | struct elf_reloc_cookie cookie; | |
9492 | asection *stab, *eh; | |
9493 | Elf_Internal_Shdr *symtab_hdr; | |
9494 | const struct elf_backend_data *bed; | |
9495 | bfd *abfd; | |
9496 | unsigned int count; | |
9497 | bfd_boolean ret = FALSE; | |
9498 | ||
9499 | if (info->traditional_format | |
9500 | || !is_elf_hash_table (info->hash)) | |
9501 | return FALSE; | |
9502 | ||
9503 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) | |
9504 | { | |
9505 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
9506 | continue; | |
9507 | ||
9508 | bed = get_elf_backend_data (abfd); | |
9509 | ||
9510 | if ((abfd->flags & DYNAMIC) != 0) | |
9511 | continue; | |
9512 | ||
9513 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); | |
9514 | if (info->relocatable | |
9515 | || (eh != NULL | |
eea6121a | 9516 | && (eh->size == 0 |
c152c796 AM |
9517 | || bfd_is_abs_section (eh->output_section)))) |
9518 | eh = NULL; | |
9519 | ||
9520 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
9521 | if (stab != NULL | |
eea6121a | 9522 | && (stab->size == 0 |
c152c796 AM |
9523 | || bfd_is_abs_section (stab->output_section) |
9524 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) | |
9525 | stab = NULL; | |
9526 | ||
9527 | if (stab == NULL | |
9528 | && eh == NULL | |
9529 | && bed->elf_backend_discard_info == NULL) | |
9530 | continue; | |
9531 | ||
9532 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
9533 | cookie.abfd = abfd; | |
9534 | cookie.sym_hashes = elf_sym_hashes (abfd); | |
9535 | cookie.bad_symtab = elf_bad_symtab (abfd); | |
9536 | if (cookie.bad_symtab) | |
9537 | { | |
9538 | cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9539 | cookie.extsymoff = 0; | |
9540 | } | |
9541 | else | |
9542 | { | |
9543 | cookie.locsymcount = symtab_hdr->sh_info; | |
9544 | cookie.extsymoff = symtab_hdr->sh_info; | |
9545 | } | |
9546 | ||
9547 | if (bed->s->arch_size == 32) | |
9548 | cookie.r_sym_shift = 8; | |
9549 | else | |
9550 | cookie.r_sym_shift = 32; | |
9551 | ||
9552 | cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
9553 | if (cookie.locsyms == NULL && cookie.locsymcount != 0) | |
9554 | { | |
9555 | cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
9556 | cookie.locsymcount, 0, | |
9557 | NULL, NULL, NULL); | |
9558 | if (cookie.locsyms == NULL) | |
9559 | return FALSE; | |
9560 | } | |
9561 | ||
9562 | if (stab != NULL) | |
9563 | { | |
9564 | cookie.rels = NULL; | |
9565 | count = stab->reloc_count; | |
9566 | if (count != 0) | |
9567 | cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL, | |
9568 | info->keep_memory); | |
9569 | if (cookie.rels != NULL) | |
9570 | { | |
9571 | cookie.rel = cookie.rels; | |
9572 | cookie.relend = cookie.rels; | |
9573 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
9574 | if (_bfd_discard_section_stabs (abfd, stab, | |
9575 | elf_section_data (stab)->sec_info, | |
9576 | bfd_elf_reloc_symbol_deleted_p, | |
9577 | &cookie)) | |
9578 | ret = TRUE; | |
9579 | if (elf_section_data (stab)->relocs != cookie.rels) | |
9580 | free (cookie.rels); | |
9581 | } | |
9582 | } | |
9583 | ||
9584 | if (eh != NULL) | |
9585 | { | |
9586 | cookie.rels = NULL; | |
9587 | count = eh->reloc_count; | |
9588 | if (count != 0) | |
9589 | cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL, | |
9590 | info->keep_memory); | |
9591 | cookie.rel = cookie.rels; | |
9592 | cookie.relend = cookie.rels; | |
9593 | if (cookie.rels != NULL) | |
9594 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
9595 | ||
9596 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, | |
9597 | bfd_elf_reloc_symbol_deleted_p, | |
9598 | &cookie)) | |
9599 | ret = TRUE; | |
9600 | ||
9601 | if (cookie.rels != NULL | |
9602 | && elf_section_data (eh)->relocs != cookie.rels) | |
9603 | free (cookie.rels); | |
9604 | } | |
9605 | ||
9606 | if (bed->elf_backend_discard_info != NULL | |
9607 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
9608 | ret = TRUE; | |
9609 | ||
9610 | if (cookie.locsyms != NULL | |
9611 | && symtab_hdr->contents != (unsigned char *) cookie.locsyms) | |
9612 | { | |
9613 | if (! info->keep_memory) | |
9614 | free (cookie.locsyms); | |
9615 | else | |
9616 | symtab_hdr->contents = (unsigned char *) cookie.locsyms; | |
9617 | } | |
9618 | } | |
9619 | ||
9620 | if (info->eh_frame_hdr | |
9621 | && !info->relocatable | |
9622 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
9623 | ret = TRUE; | |
9624 | ||
9625 | return ret; | |
9626 | } | |
082b7297 L |
9627 | |
9628 | void | |
9629 | _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec) | |
9630 | { | |
9631 | flagword flags; | |
6d2cd210 | 9632 | const char *name, *p; |
082b7297 L |
9633 | struct bfd_section_already_linked *l; |
9634 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
3d7f7666 L |
9635 | asection *group; |
9636 | ||
9637 | /* A single member comdat group section may be discarded by a | |
9638 | linkonce section. See below. */ | |
9639 | if (sec->output_section == bfd_abs_section_ptr) | |
9640 | return; | |
082b7297 L |
9641 | |
9642 | flags = sec->flags; | |
3d7f7666 L |
9643 | |
9644 | /* Check if it belongs to a section group. */ | |
9645 | group = elf_sec_group (sec); | |
9646 | ||
9647 | /* Return if it isn't a linkonce section nor a member of a group. A | |
9648 | comdat group section also has SEC_LINK_ONCE set. */ | |
9649 | if ((flags & SEC_LINK_ONCE) == 0 && group == NULL) | |
082b7297 L |
9650 | return; |
9651 | ||
3d7f7666 L |
9652 | if (group) |
9653 | { | |
9654 | /* If this is the member of a single member comdat group, check if | |
9655 | the group should be discarded. */ | |
9656 | if (elf_next_in_group (sec) == sec | |
9657 | && (group->flags & SEC_LINK_ONCE) != 0) | |
9658 | sec = group; | |
9659 | else | |
9660 | return; | |
9661 | } | |
9662 | ||
082b7297 L |
9663 | /* FIXME: When doing a relocatable link, we may have trouble |
9664 | copying relocations in other sections that refer to local symbols | |
9665 | in the section being discarded. Those relocations will have to | |
9666 | be converted somehow; as of this writing I'm not sure that any of | |
9667 | the backends handle that correctly. | |
9668 | ||
9669 | It is tempting to instead not discard link once sections when | |
9670 | doing a relocatable link (technically, they should be discarded | |
9671 | whenever we are building constructors). However, that fails, | |
9672 | because the linker winds up combining all the link once sections | |
9673 | into a single large link once section, which defeats the purpose | |
9674 | of having link once sections in the first place. | |
9675 | ||
9676 | Also, not merging link once sections in a relocatable link | |
9677 | causes trouble for MIPS ELF, which relies on link once semantics | |
9678 | to handle the .reginfo section correctly. */ | |
9679 | ||
9680 | name = bfd_get_section_name (abfd, sec); | |
9681 | ||
6d2cd210 JJ |
9682 | if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0 |
9683 | && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) | |
9684 | p++; | |
9685 | else | |
9686 | p = name; | |
9687 | ||
9688 | already_linked_list = bfd_section_already_linked_table_lookup (p); | |
082b7297 L |
9689 | |
9690 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
9691 | { | |
9692 | /* We may have 3 different sections on the list: group section, | |
9693 | comdat section and linkonce section. SEC may be a linkonce or | |
9694 | group section. We match a group section with a group section, | |
9695 | a linkonce section with a linkonce section, and ignore comdat | |
9696 | section. */ | |
3d7f7666 | 9697 | if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) |
6d2cd210 | 9698 | && strcmp (name, l->sec->name) == 0 |
082b7297 L |
9699 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL) |
9700 | { | |
9701 | /* The section has already been linked. See if we should | |
6d2cd210 | 9702 | issue a warning. */ |
082b7297 L |
9703 | switch (flags & SEC_LINK_DUPLICATES) |
9704 | { | |
9705 | default: | |
9706 | abort (); | |
9707 | ||
9708 | case SEC_LINK_DUPLICATES_DISCARD: | |
9709 | break; | |
9710 | ||
9711 | case SEC_LINK_DUPLICATES_ONE_ONLY: | |
9712 | (*_bfd_error_handler) | |
c93625e2 | 9713 | (_("%B: ignoring duplicate section `%A'"), |
d003868e | 9714 | abfd, sec); |
082b7297 L |
9715 | break; |
9716 | ||
9717 | case SEC_LINK_DUPLICATES_SAME_SIZE: | |
9718 | if (sec->size != l->sec->size) | |
9719 | (*_bfd_error_handler) | |
c93625e2 | 9720 | (_("%B: duplicate section `%A' has different size"), |
d003868e | 9721 | abfd, sec); |
082b7297 | 9722 | break; |
ea5158d8 DJ |
9723 | |
9724 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: | |
9725 | if (sec->size != l->sec->size) | |
9726 | (*_bfd_error_handler) | |
c93625e2 | 9727 | (_("%B: duplicate section `%A' has different size"), |
ea5158d8 DJ |
9728 | abfd, sec); |
9729 | else if (sec->size != 0) | |
9730 | { | |
9731 | bfd_byte *sec_contents, *l_sec_contents; | |
9732 | ||
9733 | if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents)) | |
9734 | (*_bfd_error_handler) | |
c93625e2 | 9735 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
9736 | abfd, sec); |
9737 | else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec, | |
9738 | &l_sec_contents)) | |
9739 | (*_bfd_error_handler) | |
c93625e2 | 9740 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
9741 | l->sec->owner, l->sec); |
9742 | else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) | |
9743 | (*_bfd_error_handler) | |
c93625e2 | 9744 | (_("%B: warning: duplicate section `%A' has different contents"), |
ea5158d8 DJ |
9745 | abfd, sec); |
9746 | ||
9747 | if (sec_contents) | |
9748 | free (sec_contents); | |
9749 | if (l_sec_contents) | |
9750 | free (l_sec_contents); | |
9751 | } | |
9752 | break; | |
082b7297 L |
9753 | } |
9754 | ||
9755 | /* Set the output_section field so that lang_add_section | |
9756 | does not create a lang_input_section structure for this | |
9757 | section. Since there might be a symbol in the section | |
9758 | being discarded, we must retain a pointer to the section | |
9759 | which we are really going to use. */ | |
9760 | sec->output_section = bfd_abs_section_ptr; | |
9761 | sec->kept_section = l->sec; | |
3b36f7e6 | 9762 | |
082b7297 | 9763 | if (flags & SEC_GROUP) |
3d7f7666 L |
9764 | { |
9765 | asection *first = elf_next_in_group (sec); | |
9766 | asection *s = first; | |
9767 | ||
9768 | while (s != NULL) | |
9769 | { | |
9770 | s->output_section = bfd_abs_section_ptr; | |
9771 | /* Record which group discards it. */ | |
9772 | s->kept_section = l->sec; | |
9773 | s = elf_next_in_group (s); | |
9774 | /* These lists are circular. */ | |
9775 | if (s == first) | |
9776 | break; | |
9777 | } | |
9778 | } | |
082b7297 L |
9779 | |
9780 | return; | |
9781 | } | |
9782 | } | |
9783 | ||
3d7f7666 L |
9784 | if (group) |
9785 | { | |
9786 | /* If this is the member of a single member comdat group and the | |
9787 | group hasn't be discarded, we check if it matches a linkonce | |
9788 | section. We only record the discarded comdat group. Otherwise | |
9789 | the undiscarded group will be discarded incorrectly later since | |
9790 | itself has been recorded. */ | |
6d2cd210 JJ |
9791 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
9792 | if ((l->sec->flags & SEC_GROUP) == 0 | |
9793 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL | |
9794 | && bfd_elf_match_symbols_in_sections (l->sec, | |
9795 | elf_next_in_group (sec))) | |
9796 | { | |
9797 | elf_next_in_group (sec)->output_section = bfd_abs_section_ptr; | |
9798 | elf_next_in_group (sec)->kept_section = l->sec; | |
9799 | group->output_section = bfd_abs_section_ptr; | |
9800 | break; | |
9801 | } | |
9802 | if (l == NULL) | |
3d7f7666 L |
9803 | return; |
9804 | } | |
9805 | else | |
9806 | /* There is no direct match. But for linkonce section, we should | |
9807 | check if there is a match with comdat group member. We always | |
9808 | record the linkonce section, discarded or not. */ | |
6d2cd210 JJ |
9809 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
9810 | if (l->sec->flags & SEC_GROUP) | |
9811 | { | |
9812 | asection *first = elf_next_in_group (l->sec); | |
9813 | ||
9814 | if (first != NULL | |
9815 | && elf_next_in_group (first) == first | |
9816 | && bfd_elf_match_symbols_in_sections (first, sec)) | |
9817 | { | |
9818 | sec->output_section = bfd_abs_section_ptr; | |
9819 | sec->kept_section = l->sec; | |
9820 | break; | |
9821 | } | |
9822 | } | |
9823 | ||
082b7297 L |
9824 | /* This is the first section with this name. Record it. */ |
9825 | bfd_section_already_linked_table_insert (already_linked_list, sec); | |
9826 | } | |
81e1b023 | 9827 | |
f652615e | 9828 | static void |
3b2175db PB |
9829 | bfd_elf_set_symbol (struct elf_link_hash_entry *h, bfd_vma val, |
9830 | struct bfd_section *s) | |
f652615e L |
9831 | { |
9832 | h->root.type = bfd_link_hash_defined; | |
3b2175db | 9833 | h->root.u.def.section = s ? s : bfd_abs_section_ptr; |
f652615e L |
9834 | h->root.u.def.value = val; |
9835 | h->def_regular = 1; | |
9836 | h->type = STT_OBJECT; | |
9837 | h->other = STV_HIDDEN | (h->other & ~ ELF_ST_VISIBILITY (-1)); | |
9838 | h->forced_local = 1; | |
9839 | } | |
9840 | ||
b116d4a7 AM |
9841 | /* Set NAME to VAL if the symbol exists and is not defined in a regular |
9842 | object file. If S is NULL it is an absolute symbol, otherwise it is | |
9843 | relative to that section. */ | |
81e1b023 L |
9844 | |
9845 | void | |
9846 | _bfd_elf_provide_symbol (struct bfd_link_info *info, const char *name, | |
3b2175db | 9847 | bfd_vma val, struct bfd_section *s) |
81e1b023 L |
9848 | { |
9849 | struct elf_link_hash_entry *h; | |
0291d291 | 9850 | |
81e1b023 L |
9851 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, |
9852 | FALSE); | |
b116d4a7 | 9853 | if (h != NULL && !h->def_regular) |
3b2175db | 9854 | bfd_elf_set_symbol (h, val, s); |
f652615e L |
9855 | } |
9856 | ||
b116d4a7 AM |
9857 | /* Set START and END to boundaries of SEC if they exist and are not |
9858 | defined in regular object files. */ | |
f652615e L |
9859 | |
9860 | void | |
9861 | _bfd_elf_provide_section_bound_symbols (struct bfd_link_info *info, | |
9862 | asection *sec, | |
9863 | const char *start, | |
9864 | const char *end) | |
9865 | { | |
9866 | struct elf_link_hash_entry *hs, *he; | |
9867 | bfd_vma start_val, end_val; | |
9868 | bfd_boolean do_start, do_end; | |
9869 | ||
9870 | /* Check if we need them or not first. */ | |
9871 | hs = elf_link_hash_lookup (elf_hash_table (info), start, FALSE, | |
9872 | FALSE, FALSE); | |
b116d4a7 | 9873 | do_start = hs != NULL && !hs->def_regular; |
f652615e L |
9874 | |
9875 | he = elf_link_hash_lookup (elf_hash_table (info), end, FALSE, | |
9876 | FALSE, FALSE); | |
b116d4a7 | 9877 | do_end = he != NULL && !he->def_regular; |
f652615e L |
9878 | |
9879 | if (!do_start && !do_end) | |
9880 | return; | |
9881 | ||
9882 | if (sec != NULL) | |
81e1b023 | 9883 | { |
f652615e L |
9884 | start_val = sec->vma; |
9885 | end_val = start_val + sec->size; | |
81e1b023 | 9886 | } |
f652615e L |
9887 | else |
9888 | { | |
9889 | /* We have to choose those values very carefully. Some targets, | |
01d9ad14 | 9890 | like alpha, may have relocation overflow with 0. "__bss_start" |
f652615e L |
9891 | should be defined in all cases. */ |
9892 | struct elf_link_hash_entry *h | |
01d9ad14 | 9893 | = elf_link_hash_lookup (elf_hash_table (info), "__bss_start", |
f652615e L |
9894 | FALSE, FALSE, FALSE); |
9895 | if (h != NULL && h->root.type == bfd_link_hash_defined) | |
9896 | start_val = h->root.u.def.value; | |
9897 | else | |
9898 | start_val = 0; | |
9899 | end_val = start_val; | |
9900 | } | |
9901 | ||
9902 | if (do_start) | |
3b2175db | 9903 | bfd_elf_set_symbol (hs, start_val, NULL); |
f652615e L |
9904 | |
9905 | if (do_end) | |
3b2175db | 9906 | bfd_elf_set_symbol (he, end_val, NULL); |
81e1b023 | 9907 | } |