Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
051d5130 | 2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 |
7898deda | 3 | Free Software Foundation, Inc. |
252b5132 RH |
4 | |
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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 | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
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 | ||
60 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
61 | | SEC_LINKER_CREATED); | |
62 | ||
63 | s = bfd_make_section (abfd, ".got"); | |
64 | if (s == NULL | |
65 | || !bfd_set_section_flags (abfd, s, flags) | |
66 | || !bfd_set_section_alignment (abfd, s, ptralign)) | |
b34976b6 | 67 | return FALSE; |
252b5132 RH |
68 | |
69 | if (bed->want_got_plt) | |
70 | { | |
71 | s = bfd_make_section (abfd, ".got.plt"); | |
72 | if (s == NULL | |
73 | || !bfd_set_section_flags (abfd, s, flags) | |
74 | || !bfd_set_section_alignment (abfd, s, ptralign)) | |
b34976b6 | 75 | return FALSE; |
252b5132 RH |
76 | } |
77 | ||
2517a57f AM |
78 | if (bed->want_got_sym) |
79 | { | |
80 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
81 | (or .got.plt) section. We don't do this in the linker script | |
82 | because we don't want to define the symbol if we are not creating | |
83 | a global offset table. */ | |
14a793b2 | 84 | bh = NULL; |
2517a57f AM |
85 | if (!(_bfd_generic_link_add_one_symbol |
86 | (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, | |
268b6b39 | 87 | bed->got_symbol_offset, NULL, FALSE, bed->collect, &bh))) |
b34976b6 | 88 | return FALSE; |
14a793b2 | 89 | h = (struct elf_link_hash_entry *) bh; |
2517a57f AM |
90 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
91 | h->type = STT_OBJECT; | |
252b5132 | 92 | |
36af4a4e | 93 | if (! info->executable |
c152c796 | 94 | && ! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 95 | return FALSE; |
252b5132 | 96 | |
2517a57f AM |
97 | elf_hash_table (info)->hgot = h; |
98 | } | |
252b5132 RH |
99 | |
100 | /* The first bit of the global offset table is the header. */ | |
101 | s->_raw_size += bed->got_header_size + bed->got_symbol_offset; | |
102 | ||
b34976b6 | 103 | return TRUE; |
252b5132 RH |
104 | } |
105 | \f | |
45d6a902 AM |
106 | /* Create some sections which will be filled in with dynamic linking |
107 | information. ABFD is an input file which requires dynamic sections | |
108 | to be created. The dynamic sections take up virtual memory space | |
109 | when the final executable is run, so we need to create them before | |
110 | addresses are assigned to the output sections. We work out the | |
111 | actual contents and size of these sections later. */ | |
252b5132 | 112 | |
b34976b6 | 113 | bfd_boolean |
268b6b39 | 114 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 115 | { |
45d6a902 AM |
116 | flagword flags; |
117 | register asection *s; | |
118 | struct elf_link_hash_entry *h; | |
119 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 120 | const struct elf_backend_data *bed; |
252b5132 | 121 | |
0eddce27 | 122 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
123 | return FALSE; |
124 | ||
125 | if (elf_hash_table (info)->dynamic_sections_created) | |
126 | return TRUE; | |
127 | ||
128 | /* Make sure that all dynamic sections use the same input BFD. */ | |
129 | if (elf_hash_table (info)->dynobj == NULL) | |
130 | elf_hash_table (info)->dynobj = abfd; | |
131 | else | |
132 | abfd = elf_hash_table (info)->dynobj; | |
133 | ||
134 | /* Note that we set the SEC_IN_MEMORY flag for all of these | |
135 | sections. */ | |
136 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | |
137 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
138 | ||
139 | /* A dynamically linked executable has a .interp section, but a | |
140 | shared library does not. */ | |
36af4a4e | 141 | if (info->executable) |
252b5132 | 142 | { |
45d6a902 AM |
143 | s = bfd_make_section (abfd, ".interp"); |
144 | if (s == NULL | |
145 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
146 | return FALSE; | |
147 | } | |
bb0deeff | 148 | |
0eddce27 | 149 | if (! info->traditional_format) |
45d6a902 AM |
150 | { |
151 | s = bfd_make_section (abfd, ".eh_frame_hdr"); | |
152 | if (s == NULL | |
153 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
154 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
155 | return FALSE; | |
156 | elf_hash_table (info)->eh_info.hdr_sec = s; | |
157 | } | |
bb0deeff | 158 | |
45d6a902 AM |
159 | bed = get_elf_backend_data (abfd); |
160 | ||
161 | /* Create sections to hold version informations. These are removed | |
162 | if they are not needed. */ | |
163 | s = bfd_make_section (abfd, ".gnu.version_d"); | |
164 | if (s == NULL | |
165 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
166 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
167 | return FALSE; | |
168 | ||
169 | s = bfd_make_section (abfd, ".gnu.version"); | |
170 | if (s == NULL | |
171 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
172 | || ! bfd_set_section_alignment (abfd, s, 1)) | |
173 | return FALSE; | |
174 | ||
175 | s = bfd_make_section (abfd, ".gnu.version_r"); | |
176 | if (s == NULL | |
177 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
178 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
179 | return FALSE; | |
180 | ||
181 | s = bfd_make_section (abfd, ".dynsym"); | |
182 | if (s == NULL | |
183 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
184 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
185 | return FALSE; | |
186 | ||
187 | s = bfd_make_section (abfd, ".dynstr"); | |
188 | if (s == NULL | |
189 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
190 | return FALSE; | |
191 | ||
192 | /* Create a strtab to hold the dynamic symbol names. */ | |
193 | if (elf_hash_table (info)->dynstr == NULL) | |
194 | { | |
195 | elf_hash_table (info)->dynstr = _bfd_elf_strtab_init (); | |
196 | if (elf_hash_table (info)->dynstr == NULL) | |
197 | return FALSE; | |
252b5132 RH |
198 | } |
199 | ||
45d6a902 AM |
200 | s = bfd_make_section (abfd, ".dynamic"); |
201 | if (s == NULL | |
202 | || ! bfd_set_section_flags (abfd, s, flags) | |
203 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
204 | return FALSE; | |
205 | ||
206 | /* The special symbol _DYNAMIC is always set to the start of the | |
207 | .dynamic section. This call occurs before we have processed the | |
208 | symbols for any dynamic object, so we don't have to worry about | |
209 | overriding a dynamic definition. We could set _DYNAMIC in a | |
210 | linker script, but we only want to define it if we are, in fact, | |
211 | creating a .dynamic section. We don't want to define it if there | |
212 | is no .dynamic section, since on some ELF platforms the start up | |
213 | code examines it to decide how to initialize the process. */ | |
214 | bh = NULL; | |
215 | if (! (_bfd_generic_link_add_one_symbol | |
268b6b39 AM |
216 | (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE, |
217 | get_elf_backend_data (abfd)->collect, &bh))) | |
45d6a902 AM |
218 | return FALSE; |
219 | h = (struct elf_link_hash_entry *) bh; | |
220 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
221 | h->type = STT_OBJECT; | |
222 | ||
36af4a4e | 223 | if (! info->executable |
c152c796 | 224 | && ! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
225 | return FALSE; |
226 | ||
227 | s = bfd_make_section (abfd, ".hash"); | |
228 | if (s == NULL | |
229 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
230 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
231 | return FALSE; | |
232 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
233 | ||
234 | /* Let the backend create the rest of the sections. This lets the | |
235 | backend set the right flags. The backend will normally create | |
236 | the .got and .plt sections. */ | |
237 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
238 | return FALSE; | |
239 | ||
240 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
241 | ||
242 | return TRUE; | |
243 | } | |
244 | ||
245 | /* Create dynamic sections when linking against a dynamic object. */ | |
246 | ||
247 | bfd_boolean | |
268b6b39 | 248 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
249 | { |
250 | flagword flags, pltflags; | |
251 | asection *s; | |
9c5bfbb7 | 252 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 253 | |
252b5132 RH |
254 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
255 | .rel[a].bss sections. */ | |
256 | ||
257 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
258 | | SEC_LINKER_CREATED); | |
259 | ||
260 | pltflags = flags; | |
261 | pltflags |= SEC_CODE; | |
262 | if (bed->plt_not_loaded) | |
5d1634d7 | 263 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
252b5132 RH |
264 | if (bed->plt_readonly) |
265 | pltflags |= SEC_READONLY; | |
266 | ||
267 | s = bfd_make_section (abfd, ".plt"); | |
268 | if (s == NULL | |
269 | || ! bfd_set_section_flags (abfd, s, pltflags) | |
270 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) | |
b34976b6 | 271 | return FALSE; |
252b5132 RH |
272 | |
273 | if (bed->want_plt_sym) | |
274 | { | |
275 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the | |
276 | .plt section. */ | |
14a793b2 AM |
277 | struct elf_link_hash_entry *h; |
278 | struct bfd_link_hash_entry *bh = NULL; | |
279 | ||
252b5132 | 280 | if (! (_bfd_generic_link_add_one_symbol |
268b6b39 AM |
281 | (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL, |
282 | FALSE, get_elf_backend_data (abfd)->collect, &bh))) | |
b34976b6 | 283 | return FALSE; |
14a793b2 | 284 | h = (struct elf_link_hash_entry *) bh; |
252b5132 RH |
285 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
286 | h->type = STT_OBJECT; | |
287 | ||
36af4a4e | 288 | if (! info->executable |
c152c796 | 289 | && ! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 290 | return FALSE; |
252b5132 RH |
291 | } |
292 | ||
3e932841 | 293 | s = bfd_make_section (abfd, |
bf572ba0 | 294 | bed->default_use_rela_p ? ".rela.plt" : ".rel.plt"); |
252b5132 RH |
295 | if (s == NULL |
296 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
45d6a902 | 297 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 298 | return FALSE; |
252b5132 RH |
299 | |
300 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 301 | return FALSE; |
252b5132 | 302 | |
3018b441 RH |
303 | if (bed->want_dynbss) |
304 | { | |
305 | /* The .dynbss section is a place to put symbols which are defined | |
306 | by dynamic objects, are referenced by regular objects, and are | |
307 | not functions. We must allocate space for them in the process | |
308 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
309 | initialize them at run time. The linker script puts the .dynbss | |
310 | section into the .bss section of the final image. */ | |
311 | s = bfd_make_section (abfd, ".dynbss"); | |
312 | if (s == NULL | |
77f3d027 | 313 | || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED)) |
b34976b6 | 314 | return FALSE; |
252b5132 | 315 | |
3018b441 | 316 | /* The .rel[a].bss section holds copy relocs. This section is not |
252b5132 RH |
317 | normally needed. We need to create it here, though, so that the |
318 | linker will map it to an output section. We can't just create it | |
319 | only if we need it, because we will not know whether we need it | |
320 | until we have seen all the input files, and the first time the | |
321 | main linker code calls BFD after examining all the input files | |
322 | (size_dynamic_sections) the input sections have already been | |
323 | mapped to the output sections. If the section turns out not to | |
324 | be needed, we can discard it later. We will never need this | |
325 | section when generating a shared object, since they do not use | |
326 | copy relocs. */ | |
3018b441 RH |
327 | if (! info->shared) |
328 | { | |
3e932841 KH |
329 | s = bfd_make_section (abfd, |
330 | (bed->default_use_rela_p | |
331 | ? ".rela.bss" : ".rel.bss")); | |
3018b441 RH |
332 | if (s == NULL |
333 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
45d6a902 | 334 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 335 | return FALSE; |
3018b441 | 336 | } |
252b5132 RH |
337 | } |
338 | ||
b34976b6 | 339 | return TRUE; |
252b5132 RH |
340 | } |
341 | \f | |
252b5132 RH |
342 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
343 | read the input files, since we need to have a list of all of them | |
344 | before we can determine the final sizes of the output sections. | |
345 | Note that we may actually call this function even though we are not | |
346 | going to output any dynamic symbols; in some cases we know that a | |
347 | symbol should be in the dynamic symbol table, but only if there is | |
348 | one. */ | |
349 | ||
b34976b6 | 350 | bfd_boolean |
c152c796 AM |
351 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
352 | struct elf_link_hash_entry *h) | |
252b5132 RH |
353 | { |
354 | if (h->dynindx == -1) | |
355 | { | |
2b0f7ef9 | 356 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 357 | char *p; |
252b5132 | 358 | const char *name; |
252b5132 RH |
359 | bfd_size_type indx; |
360 | ||
7a13edea NC |
361 | /* XXX: The ABI draft says the linker must turn hidden and |
362 | internal symbols into STB_LOCAL symbols when producing the | |
363 | DSO. However, if ld.so honors st_other in the dynamic table, | |
364 | this would not be necessary. */ | |
365 | switch (ELF_ST_VISIBILITY (h->other)) | |
366 | { | |
367 | case STV_INTERNAL: | |
368 | case STV_HIDDEN: | |
9d6eee78 L |
369 | if (h->root.type != bfd_link_hash_undefined |
370 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 L |
371 | { |
372 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; | |
b34976b6 | 373 | return TRUE; |
7a13edea | 374 | } |
0444bdd4 | 375 | |
7a13edea NC |
376 | default: |
377 | break; | |
378 | } | |
379 | ||
252b5132 RH |
380 | h->dynindx = elf_hash_table (info)->dynsymcount; |
381 | ++elf_hash_table (info)->dynsymcount; | |
382 | ||
383 | dynstr = elf_hash_table (info)->dynstr; | |
384 | if (dynstr == NULL) | |
385 | { | |
386 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 387 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 388 | if (dynstr == NULL) |
b34976b6 | 389 | return FALSE; |
252b5132 RH |
390 | } |
391 | ||
392 | /* We don't put any version information in the dynamic string | |
aad5d350 | 393 | table. */ |
252b5132 RH |
394 | name = h->root.root.string; |
395 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
396 | if (p != NULL) |
397 | /* We know that the p points into writable memory. In fact, | |
398 | there are only a few symbols that have read-only names, being | |
399 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
400 | by the backends. Most symbols will have names pointing into | |
401 | an ELF string table read from a file, or to objalloc memory. */ | |
402 | *p = 0; | |
403 | ||
404 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
405 | ||
406 | if (p != NULL) | |
407 | *p = ELF_VER_CHR; | |
252b5132 RH |
408 | |
409 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 410 | return FALSE; |
252b5132 RH |
411 | h->dynstr_index = indx; |
412 | } | |
413 | ||
b34976b6 | 414 | return TRUE; |
252b5132 | 415 | } |
45d6a902 AM |
416 | \f |
417 | /* Record an assignment to a symbol made by a linker script. We need | |
418 | this in case some dynamic object refers to this symbol. */ | |
419 | ||
420 | bfd_boolean | |
268b6b39 AM |
421 | bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED, |
422 | struct bfd_link_info *info, | |
423 | const char *name, | |
424 | bfd_boolean provide) | |
45d6a902 AM |
425 | { |
426 | struct elf_link_hash_entry *h; | |
427 | ||
0eddce27 | 428 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
429 | return TRUE; |
430 | ||
431 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE); | |
432 | if (h == NULL) | |
433 | return FALSE; | |
434 | ||
02bb6eae AO |
435 | /* Since we're defining the symbol, don't let it seem to have not |
436 | been defined. record_dynamic_symbol and size_dynamic_sections | |
437 | may depend on this. */ | |
438 | if (h->root.type == bfd_link_hash_undefweak | |
439 | || h->root.type == bfd_link_hash_undefined) | |
440 | h->root.type = bfd_link_hash_new; | |
441 | ||
45d6a902 AM |
442 | if (h->root.type == bfd_link_hash_new) |
443 | h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF; | |
444 | ||
445 | /* If this symbol is being provided by the linker script, and it is | |
446 | currently defined by a dynamic object, but not by a regular | |
447 | object, then mark it as undefined so that the generic linker will | |
448 | force the correct value. */ | |
449 | if (provide | |
450 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
451 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
452 | h->root.type = bfd_link_hash_undefined; | |
453 | ||
454 | /* If this symbol is not being provided by the linker script, and it is | |
455 | currently defined by a dynamic object, but not by a regular object, | |
456 | then clear out any version information because the symbol will not be | |
457 | associated with the dynamic object any more. */ | |
458 | if (!provide | |
459 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
460 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
461 | h->verinfo.verdef = NULL; | |
462 | ||
463 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
464 | ||
465 | if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC | |
466 | | ELF_LINK_HASH_REF_DYNAMIC)) != 0 | |
467 | || info->shared) | |
468 | && h->dynindx == -1) | |
469 | { | |
c152c796 | 470 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
471 | return FALSE; |
472 | ||
473 | /* If this is a weak defined symbol, and we know a corresponding | |
474 | real symbol from the same dynamic object, make sure the real | |
475 | symbol is also made into a dynamic symbol. */ | |
476 | if (h->weakdef != NULL | |
477 | && h->weakdef->dynindx == -1) | |
478 | { | |
c152c796 | 479 | if (! bfd_elf_link_record_dynamic_symbol (info, h->weakdef)) |
45d6a902 AM |
480 | return FALSE; |
481 | } | |
482 | } | |
483 | ||
484 | return TRUE; | |
485 | } | |
42751cf3 | 486 | |
8c58d23b AM |
487 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
488 | success, and 2 on a failure caused by attempting to record a symbol | |
489 | in a discarded section, eg. a discarded link-once section symbol. */ | |
490 | ||
491 | int | |
c152c796 AM |
492 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
493 | bfd *input_bfd, | |
494 | long input_indx) | |
8c58d23b AM |
495 | { |
496 | bfd_size_type amt; | |
497 | struct elf_link_local_dynamic_entry *entry; | |
498 | struct elf_link_hash_table *eht; | |
499 | struct elf_strtab_hash *dynstr; | |
500 | unsigned long dynstr_index; | |
501 | char *name; | |
502 | Elf_External_Sym_Shndx eshndx; | |
503 | char esym[sizeof (Elf64_External_Sym)]; | |
504 | ||
0eddce27 | 505 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
506 | return 0; |
507 | ||
508 | /* See if the entry exists already. */ | |
509 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
510 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
511 | return 1; | |
512 | ||
513 | amt = sizeof (*entry); | |
268b6b39 | 514 | entry = bfd_alloc (input_bfd, amt); |
8c58d23b AM |
515 | if (entry == NULL) |
516 | return 0; | |
517 | ||
518 | /* Go find the symbol, so that we can find it's name. */ | |
519 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 520 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
521 | { |
522 | bfd_release (input_bfd, entry); | |
523 | return 0; | |
524 | } | |
525 | ||
526 | if (entry->isym.st_shndx != SHN_UNDEF | |
527 | && (entry->isym.st_shndx < SHN_LORESERVE | |
528 | || entry->isym.st_shndx > SHN_HIRESERVE)) | |
529 | { | |
530 | asection *s; | |
531 | ||
532 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
533 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
534 | { | |
535 | /* We can still bfd_release here as nothing has done another | |
536 | bfd_alloc. We can't do this later in this function. */ | |
537 | bfd_release (input_bfd, entry); | |
538 | return 2; | |
539 | } | |
540 | } | |
541 | ||
542 | name = (bfd_elf_string_from_elf_section | |
543 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
544 | entry->isym.st_name)); | |
545 | ||
546 | dynstr = elf_hash_table (info)->dynstr; | |
547 | if (dynstr == NULL) | |
548 | { | |
549 | /* Create a strtab to hold the dynamic symbol names. */ | |
550 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
551 | if (dynstr == NULL) | |
552 | return 0; | |
553 | } | |
554 | ||
b34976b6 | 555 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
556 | if (dynstr_index == (unsigned long) -1) |
557 | return 0; | |
558 | entry->isym.st_name = dynstr_index; | |
559 | ||
560 | eht = elf_hash_table (info); | |
561 | ||
562 | entry->next = eht->dynlocal; | |
563 | eht->dynlocal = entry; | |
564 | entry->input_bfd = input_bfd; | |
565 | entry->input_indx = input_indx; | |
566 | eht->dynsymcount++; | |
567 | ||
568 | /* Whatever binding the symbol had before, it's now local. */ | |
569 | entry->isym.st_info | |
570 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
571 | ||
572 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
573 | ||
574 | return 1; | |
575 | } | |
576 | ||
30b30c21 | 577 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 578 | |
30b30c21 | 579 | long |
268b6b39 AM |
580 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
581 | bfd *input_bfd, | |
582 | long input_indx) | |
30b30c21 RH |
583 | { |
584 | struct elf_link_local_dynamic_entry *e; | |
585 | ||
586 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
587 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
588 | return e->dynindx; | |
589 | return -1; | |
590 | } | |
591 | ||
592 | /* This function is used to renumber the dynamic symbols, if some of | |
593 | them are removed because they are marked as local. This is called | |
594 | via elf_link_hash_traverse. */ | |
595 | ||
b34976b6 | 596 | static bfd_boolean |
268b6b39 AM |
597 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
598 | void *data) | |
42751cf3 | 599 | { |
268b6b39 | 600 | size_t *count = data; |
30b30c21 | 601 | |
e92d460e AM |
602 | if (h->root.type == bfd_link_hash_warning) |
603 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
604 | ||
42751cf3 | 605 | if (h->dynindx != -1) |
30b30c21 RH |
606 | h->dynindx = ++(*count); |
607 | ||
b34976b6 | 608 | return TRUE; |
42751cf3 | 609 | } |
30b30c21 | 610 | |
062e2358 | 611 | /* Assign dynsym indices. In a shared library we generate a section |
30b30c21 RH |
612 | symbol for each output section, which come first. Next come all of |
613 | the back-end allocated local dynamic syms, followed by the rest of | |
614 | the global symbols. */ | |
615 | ||
616 | unsigned long | |
268b6b39 | 617 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info) |
30b30c21 RH |
618 | { |
619 | unsigned long dynsymcount = 0; | |
620 | ||
621 | if (info->shared) | |
622 | { | |
623 | asection *p; | |
624 | for (p = output_bfd->sections; p ; p = p->next) | |
bc0ba537 AM |
625 | if ((p->flags & SEC_EXCLUDE) == 0) |
626 | elf_section_data (p)->dynindx = ++dynsymcount; | |
30b30c21 RH |
627 | } |
628 | ||
629 | if (elf_hash_table (info)->dynlocal) | |
630 | { | |
631 | struct elf_link_local_dynamic_entry *p; | |
632 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
633 | p->dynindx = ++dynsymcount; | |
634 | } | |
635 | ||
636 | elf_link_hash_traverse (elf_hash_table (info), | |
637 | elf_link_renumber_hash_table_dynsyms, | |
638 | &dynsymcount); | |
639 | ||
640 | /* There is an unused NULL entry at the head of the table which | |
641 | we must account for in our count. Unless there weren't any | |
642 | symbols, which means we'll have no table at all. */ | |
643 | if (dynsymcount != 0) | |
644 | ++dynsymcount; | |
645 | ||
646 | return elf_hash_table (info)->dynsymcount = dynsymcount; | |
647 | } | |
252b5132 | 648 | |
45d6a902 AM |
649 | /* This function is called when we want to define a new symbol. It |
650 | handles the various cases which arise when we find a definition in | |
651 | a dynamic object, or when there is already a definition in a | |
652 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
653 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
654 | OVERRIDE if the old symbol is overriding a new definition. We set | |
655 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
656 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
657 | change, we mean that we shouldn't warn if the type or size does | |
0f8a2703 | 658 | change. */ |
45d6a902 AM |
659 | |
660 | bfd_boolean | |
268b6b39 AM |
661 | _bfd_elf_merge_symbol (bfd *abfd, |
662 | struct bfd_link_info *info, | |
663 | const char *name, | |
664 | Elf_Internal_Sym *sym, | |
665 | asection **psec, | |
666 | bfd_vma *pvalue, | |
667 | struct elf_link_hash_entry **sym_hash, | |
668 | bfd_boolean *skip, | |
669 | bfd_boolean *override, | |
670 | bfd_boolean *type_change_ok, | |
0f8a2703 | 671 | bfd_boolean *size_change_ok) |
252b5132 | 672 | { |
45d6a902 AM |
673 | asection *sec; |
674 | struct elf_link_hash_entry *h; | |
675 | struct elf_link_hash_entry *flip; | |
676 | int bind; | |
677 | bfd *oldbfd; | |
678 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
79349b09 | 679 | bfd_boolean newweak, oldweak; |
45d6a902 AM |
680 | |
681 | *skip = FALSE; | |
682 | *override = FALSE; | |
683 | ||
684 | sec = *psec; | |
685 | bind = ELF_ST_BIND (sym->st_info); | |
686 | ||
687 | if (! bfd_is_und_section (sec)) | |
688 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
689 | else | |
690 | h = ((struct elf_link_hash_entry *) | |
691 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
692 | if (h == NULL) | |
693 | return FALSE; | |
694 | *sym_hash = h; | |
252b5132 | 695 | |
45d6a902 AM |
696 | /* This code is for coping with dynamic objects, and is only useful |
697 | if we are doing an ELF link. */ | |
698 | if (info->hash->creator != abfd->xvec) | |
699 | return TRUE; | |
252b5132 | 700 | |
45d6a902 AM |
701 | /* For merging, we only care about real symbols. */ |
702 | ||
703 | while (h->root.type == bfd_link_hash_indirect | |
704 | || h->root.type == bfd_link_hash_warning) | |
705 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
706 | ||
707 | /* If we just created the symbol, mark it as being an ELF symbol. | |
708 | Other than that, there is nothing to do--there is no merge issue | |
709 | with a newly defined symbol--so we just return. */ | |
710 | ||
711 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 712 | { |
45d6a902 AM |
713 | h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF; |
714 | return TRUE; | |
715 | } | |
252b5132 | 716 | |
45d6a902 | 717 | /* OLDBFD is a BFD associated with the existing symbol. */ |
252b5132 | 718 | |
45d6a902 AM |
719 | switch (h->root.type) |
720 | { | |
721 | default: | |
722 | oldbfd = NULL; | |
723 | break; | |
252b5132 | 724 | |
45d6a902 AM |
725 | case bfd_link_hash_undefined: |
726 | case bfd_link_hash_undefweak: | |
727 | oldbfd = h->root.u.undef.abfd; | |
728 | break; | |
729 | ||
730 | case bfd_link_hash_defined: | |
731 | case bfd_link_hash_defweak: | |
732 | oldbfd = h->root.u.def.section->owner; | |
733 | break; | |
734 | ||
735 | case bfd_link_hash_common: | |
736 | oldbfd = h->root.u.c.p->section->owner; | |
737 | break; | |
738 | } | |
739 | ||
740 | /* In cases involving weak versioned symbols, we may wind up trying | |
741 | to merge a symbol with itself. Catch that here, to avoid the | |
742 | confusion that results if we try to override a symbol with | |
743 | itself. The additional tests catch cases like | |
744 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
745 | dynamic object, which we do want to handle here. */ | |
746 | if (abfd == oldbfd | |
747 | && ((abfd->flags & DYNAMIC) == 0 | |
748 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)) | |
749 | return TRUE; | |
750 | ||
751 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
752 | respectively, is from a dynamic object. */ | |
753 | ||
754 | if ((abfd->flags & DYNAMIC) != 0) | |
755 | newdyn = TRUE; | |
756 | else | |
757 | newdyn = FALSE; | |
758 | ||
759 | if (oldbfd != NULL) | |
760 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
761 | else | |
762 | { | |
763 | asection *hsec; | |
764 | ||
765 | /* This code handles the special SHN_MIPS_{TEXT,DATA} section | |
766 | indices used by MIPS ELF. */ | |
767 | switch (h->root.type) | |
252b5132 | 768 | { |
45d6a902 AM |
769 | default: |
770 | hsec = NULL; | |
771 | break; | |
252b5132 | 772 | |
45d6a902 AM |
773 | case bfd_link_hash_defined: |
774 | case bfd_link_hash_defweak: | |
775 | hsec = h->root.u.def.section; | |
776 | break; | |
252b5132 | 777 | |
45d6a902 AM |
778 | case bfd_link_hash_common: |
779 | hsec = h->root.u.c.p->section; | |
780 | break; | |
252b5132 | 781 | } |
252b5132 | 782 | |
45d6a902 AM |
783 | if (hsec == NULL) |
784 | olddyn = FALSE; | |
785 | else | |
786 | olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0; | |
787 | } | |
252b5132 | 788 | |
45d6a902 AM |
789 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
790 | respectively, appear to be a definition rather than reference. */ | |
791 | ||
792 | if (bfd_is_und_section (sec) || bfd_is_com_section (sec)) | |
793 | newdef = FALSE; | |
794 | else | |
795 | newdef = TRUE; | |
796 | ||
797 | if (h->root.type == bfd_link_hash_undefined | |
798 | || h->root.type == bfd_link_hash_undefweak | |
799 | || h->root.type == bfd_link_hash_common) | |
800 | olddef = FALSE; | |
801 | else | |
802 | olddef = TRUE; | |
803 | ||
4cc11e76 | 804 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
805 | object or is weak in all dynamic objects. Internal and hidden |
806 | visibility will make it unavailable to dynamic objects. */ | |
807 | if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0) | |
808 | { | |
809 | if (!bfd_is_und_section (sec)) | |
810 | h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF; | |
811 | else | |
252b5132 | 812 | { |
45d6a902 AM |
813 | /* Check if this symbol is weak in all dynamic objects. If it |
814 | is the first time we see it in a dynamic object, we mark | |
815 | if it is weak. Otherwise, we clear it. */ | |
816 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0) | |
79349b09 | 817 | { |
45d6a902 AM |
818 | if (bind == STB_WEAK) |
819 | h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK; | |
252b5132 | 820 | } |
45d6a902 AM |
821 | else if (bind != STB_WEAK) |
822 | h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK; | |
252b5132 | 823 | } |
45d6a902 | 824 | } |
252b5132 | 825 | |
45d6a902 AM |
826 | /* If the old symbol has non-default visibility, we ignore the new |
827 | definition from a dynamic object. */ | |
828 | if (newdyn | |
9c7a29a3 | 829 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
830 | && !bfd_is_und_section (sec)) |
831 | { | |
832 | *skip = TRUE; | |
833 | /* Make sure this symbol is dynamic. */ | |
834 | h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
835 | /* A protected symbol has external availability. Make sure it is | |
836 | recorded as dynamic. | |
837 | ||
838 | FIXME: Should we check type and size for protected symbol? */ | |
839 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 840 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
841 | else |
842 | return TRUE; | |
843 | } | |
844 | else if (!newdyn | |
9c7a29a3 | 845 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
45d6a902 AM |
846 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0) |
847 | { | |
848 | /* If the new symbol with non-default visibility comes from a | |
849 | relocatable file and the old definition comes from a dynamic | |
850 | object, we remove the old definition. */ | |
851 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
852 | h = *sym_hash; | |
1de1a317 L |
853 | |
854 | if ((h->root.und_next || info->hash->undefs_tail == &h->root) | |
855 | && bfd_is_und_section (sec)) | |
856 | { | |
857 | /* If the new symbol is undefined and the old symbol was | |
858 | also undefined before, we need to make sure | |
859 | _bfd_generic_link_add_one_symbol doesn't mess | |
860 | up the linker hash table undefs list. Since the old | |
861 | definition came from a dynamic object, it is still on the | |
862 | undefs list. */ | |
863 | h->root.type = bfd_link_hash_undefined; | |
864 | /* FIXME: What if the new symbol is weak undefined? */ | |
865 | h->root.u.undef.abfd = abfd; | |
866 | } | |
867 | else | |
868 | { | |
869 | h->root.type = bfd_link_hash_new; | |
870 | h->root.u.undef.abfd = NULL; | |
871 | } | |
872 | ||
45d6a902 | 873 | if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) |
252b5132 | 874 | { |
45d6a902 | 875 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC; |
22d5e339 L |
876 | h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_DYNAMIC |
877 | | ELF_LINK_DYNAMIC_DEF); | |
45d6a902 AM |
878 | } |
879 | /* FIXME: Should we check type and size for protected symbol? */ | |
880 | h->size = 0; | |
881 | h->type = 0; | |
882 | return TRUE; | |
883 | } | |
14a793b2 | 884 | |
79349b09 AM |
885 | /* Differentiate strong and weak symbols. */ |
886 | newweak = bind == STB_WEAK; | |
887 | oldweak = (h->root.type == bfd_link_hash_defweak | |
888 | || h->root.type == bfd_link_hash_undefweak); | |
14a793b2 | 889 | |
0f8a2703 AM |
890 | /* If a new weak symbol comes from a regular file and the old symbol |
891 | comes from a dynamic library, we treat the new one as strong. | |
892 | Similarly, an old weak symbol from a regular file is treated as | |
893 | strong when the new symbol comes from a dynamic library. Further, | |
894 | an old weak symbol from a dynamic library is treated as strong if | |
895 | the new symbol is from a dynamic library. This reflects the way | |
896 | glibc's ld.so works. */ | |
897 | if (!newdyn && olddyn) | |
898 | newweak = FALSE; | |
899 | if (newdyn) | |
900 | oldweak = FALSE; | |
901 | ||
79349b09 AM |
902 | /* It's OK to change the type if either the existing symbol or the |
903 | new symbol is weak. A type change is also OK if the old symbol | |
904 | is undefined and the new symbol is defined. */ | |
252b5132 | 905 | |
79349b09 AM |
906 | if (oldweak |
907 | || newweak | |
908 | || (newdef | |
909 | && h->root.type == bfd_link_hash_undefined)) | |
910 | *type_change_ok = TRUE; | |
911 | ||
912 | /* It's OK to change the size if either the existing symbol or the | |
913 | new symbol is weak, or if the old symbol is undefined. */ | |
914 | ||
915 | if (*type_change_ok | |
916 | || h->root.type == bfd_link_hash_undefined) | |
917 | *size_change_ok = TRUE; | |
45d6a902 | 918 | |
45d6a902 AM |
919 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
920 | symbol, respectively, appears to be a common symbol in a dynamic | |
921 | object. If a symbol appears in an uninitialized section, and is | |
922 | not weak, and is not a function, then it may be a common symbol | |
923 | which was resolved when the dynamic object was created. We want | |
924 | to treat such symbols specially, because they raise special | |
925 | considerations when setting the symbol size: if the symbol | |
926 | appears as a common symbol in a regular object, and the size in | |
927 | the regular object is larger, we must make sure that we use the | |
928 | larger size. This problematic case can always be avoided in C, | |
929 | but it must be handled correctly when using Fortran shared | |
930 | libraries. | |
931 | ||
932 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
933 | likewise for OLDDYNCOMMON and OLDDEF. | |
934 | ||
935 | Note that this test is just a heuristic, and that it is quite | |
936 | possible to have an uninitialized symbol in a shared object which | |
937 | is really a definition, rather than a common symbol. This could | |
938 | lead to some minor confusion when the symbol really is a common | |
939 | symbol in some regular object. However, I think it will be | |
940 | harmless. */ | |
941 | ||
942 | if (newdyn | |
943 | && newdef | |
79349b09 | 944 | && !newweak |
45d6a902 AM |
945 | && (sec->flags & SEC_ALLOC) != 0 |
946 | && (sec->flags & SEC_LOAD) == 0 | |
947 | && sym->st_size > 0 | |
45d6a902 AM |
948 | && ELF_ST_TYPE (sym->st_info) != STT_FUNC) |
949 | newdyncommon = TRUE; | |
950 | else | |
951 | newdyncommon = FALSE; | |
952 | ||
953 | if (olddyn | |
954 | && olddef | |
955 | && h->root.type == bfd_link_hash_defined | |
956 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
957 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 | |
958 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
959 | && h->size > 0 | |
960 | && h->type != STT_FUNC) | |
961 | olddyncommon = TRUE; | |
962 | else | |
963 | olddyncommon = FALSE; | |
964 | ||
45d6a902 AM |
965 | /* If both the old and the new symbols look like common symbols in a |
966 | dynamic object, set the size of the symbol to the larger of the | |
967 | two. */ | |
968 | ||
969 | if (olddyncommon | |
970 | && newdyncommon | |
971 | && sym->st_size != h->size) | |
972 | { | |
973 | /* Since we think we have two common symbols, issue a multiple | |
974 | common warning if desired. Note that we only warn if the | |
975 | size is different. If the size is the same, we simply let | |
976 | the old symbol override the new one as normally happens with | |
977 | symbols defined in dynamic objects. */ | |
978 | ||
979 | if (! ((*info->callbacks->multiple_common) | |
980 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
981 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
982 | return FALSE; | |
252b5132 | 983 | |
45d6a902 AM |
984 | if (sym->st_size > h->size) |
985 | h->size = sym->st_size; | |
252b5132 | 986 | |
45d6a902 | 987 | *size_change_ok = TRUE; |
252b5132 RH |
988 | } |
989 | ||
45d6a902 AM |
990 | /* If we are looking at a dynamic object, and we have found a |
991 | definition, we need to see if the symbol was already defined by | |
992 | some other object. If so, we want to use the existing | |
993 | definition, and we do not want to report a multiple symbol | |
994 | definition error; we do this by clobbering *PSEC to be | |
995 | bfd_und_section_ptr. | |
996 | ||
997 | We treat a common symbol as a definition if the symbol in the | |
998 | shared library is a function, since common symbols always | |
999 | represent variables; this can cause confusion in principle, but | |
1000 | any such confusion would seem to indicate an erroneous program or | |
1001 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1002 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1003 | |
1004 | if (newdyn | |
1005 | && newdef | |
1006 | && (olddef | |
1007 | || (h->root.type == bfd_link_hash_common | |
79349b09 | 1008 | && (newweak |
0f8a2703 | 1009 | || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))) |
45d6a902 AM |
1010 | { |
1011 | *override = TRUE; | |
1012 | newdef = FALSE; | |
1013 | newdyncommon = FALSE; | |
252b5132 | 1014 | |
45d6a902 AM |
1015 | *psec = sec = bfd_und_section_ptr; |
1016 | *size_change_ok = TRUE; | |
252b5132 | 1017 | |
45d6a902 AM |
1018 | /* If we get here when the old symbol is a common symbol, then |
1019 | we are explicitly letting it override a weak symbol or | |
1020 | function in a dynamic object, and we don't want to warn about | |
1021 | a type change. If the old symbol is a defined symbol, a type | |
1022 | change warning may still be appropriate. */ | |
252b5132 | 1023 | |
45d6a902 AM |
1024 | if (h->root.type == bfd_link_hash_common) |
1025 | *type_change_ok = TRUE; | |
1026 | } | |
1027 | ||
1028 | /* Handle the special case of an old common symbol merging with a | |
1029 | new symbol which looks like a common symbol in a shared object. | |
1030 | We change *PSEC and *PVALUE to make the new symbol look like a | |
1031 | common symbol, and let _bfd_generic_link_add_one_symbol will do | |
1032 | the right thing. */ | |
1033 | ||
1034 | if (newdyncommon | |
1035 | && h->root.type == bfd_link_hash_common) | |
1036 | { | |
1037 | *override = TRUE; | |
1038 | newdef = FALSE; | |
1039 | newdyncommon = FALSE; | |
1040 | *pvalue = sym->st_size; | |
1041 | *psec = sec = bfd_com_section_ptr; | |
1042 | *size_change_ok = TRUE; | |
1043 | } | |
1044 | ||
1045 | /* If the old symbol is from a dynamic object, and the new symbol is | |
1046 | a definition which is not from a dynamic object, then the new | |
1047 | symbol overrides the old symbol. Symbols from regular files | |
1048 | always take precedence over symbols from dynamic objects, even if | |
1049 | they are defined after the dynamic object in the link. | |
1050 | ||
1051 | As above, we again permit a common symbol in a regular object to | |
1052 | override a definition in a shared object if the shared object | |
0f8a2703 | 1053 | symbol is a function or is weak. */ |
45d6a902 AM |
1054 | |
1055 | flip = NULL; | |
1056 | if (! newdyn | |
1057 | && (newdef | |
1058 | || (bfd_is_com_section (sec) | |
79349b09 AM |
1059 | && (oldweak |
1060 | || h->type == STT_FUNC))) | |
45d6a902 AM |
1061 | && olddyn |
1062 | && olddef | |
0f8a2703 | 1063 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0) |
45d6a902 AM |
1064 | { |
1065 | /* Change the hash table entry to undefined, and let | |
1066 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1067 | new definition. */ | |
1068 | ||
1069 | h->root.type = bfd_link_hash_undefined; | |
1070 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1071 | *size_change_ok = TRUE; | |
1072 | ||
1073 | olddef = FALSE; | |
1074 | olddyncommon = FALSE; | |
1075 | ||
1076 | /* We again permit a type change when a common symbol may be | |
1077 | overriding a function. */ | |
1078 | ||
1079 | if (bfd_is_com_section (sec)) | |
1080 | *type_change_ok = TRUE; | |
1081 | ||
1082 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1083 | flip = *sym_hash; | |
1084 | else | |
1085 | /* This union may have been set to be non-NULL when this symbol | |
1086 | was seen in a dynamic object. We must force the union to be | |
1087 | NULL, so that it is correct for a regular symbol. */ | |
1088 | h->verinfo.vertree = NULL; | |
1089 | } | |
1090 | ||
1091 | /* Handle the special case of a new common symbol merging with an | |
1092 | old symbol that looks like it might be a common symbol defined in | |
1093 | a shared object. Note that we have already handled the case in | |
1094 | which a new common symbol should simply override the definition | |
1095 | in the shared library. */ | |
1096 | ||
1097 | if (! newdyn | |
1098 | && bfd_is_com_section (sec) | |
1099 | && olddyncommon) | |
1100 | { | |
1101 | /* It would be best if we could set the hash table entry to a | |
1102 | common symbol, but we don't know what to use for the section | |
1103 | or the alignment. */ | |
1104 | if (! ((*info->callbacks->multiple_common) | |
1105 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1106 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1107 | return FALSE; | |
1108 | ||
4cc11e76 | 1109 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1110 | larger, pretend that the new symbol has its size. */ |
1111 | ||
1112 | if (h->size > *pvalue) | |
1113 | *pvalue = h->size; | |
1114 | ||
1115 | /* FIXME: We no longer know the alignment required by the symbol | |
1116 | in the dynamic object, so we just wind up using the one from | |
1117 | the regular object. */ | |
1118 | ||
1119 | olddef = FALSE; | |
1120 | olddyncommon = FALSE; | |
1121 | ||
1122 | h->root.type = bfd_link_hash_undefined; | |
1123 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1124 | ||
1125 | *size_change_ok = TRUE; | |
1126 | *type_change_ok = TRUE; | |
1127 | ||
1128 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1129 | flip = *sym_hash; | |
1130 | else | |
1131 | h->verinfo.vertree = NULL; | |
1132 | } | |
1133 | ||
1134 | if (flip != NULL) | |
1135 | { | |
1136 | /* Handle the case where we had a versioned symbol in a dynamic | |
1137 | library and now find a definition in a normal object. In this | |
1138 | case, we make the versioned symbol point to the normal one. */ | |
9c5bfbb7 | 1139 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1140 | flip->root.type = h->root.type; |
1141 | h->root.type = bfd_link_hash_indirect; | |
1142 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
1143 | (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h); | |
1144 | flip->root.u.undef.abfd = h->root.u.undef.abfd; | |
1145 | if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) | |
1146 | { | |
1147 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC; | |
1148 | flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
1149 | } | |
1150 | } | |
1151 | ||
45d6a902 AM |
1152 | return TRUE; |
1153 | } | |
1154 | ||
1155 | /* This function is called to create an indirect symbol from the | |
1156 | default for the symbol with the default version if needed. The | |
1157 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1158 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 AM |
1159 | |
1160 | bfd_boolean | |
268b6b39 AM |
1161 | _bfd_elf_add_default_symbol (bfd *abfd, |
1162 | struct bfd_link_info *info, | |
1163 | struct elf_link_hash_entry *h, | |
1164 | const char *name, | |
1165 | Elf_Internal_Sym *sym, | |
1166 | asection **psec, | |
1167 | bfd_vma *value, | |
1168 | bfd_boolean *dynsym, | |
0f8a2703 | 1169 | bfd_boolean override) |
45d6a902 AM |
1170 | { |
1171 | bfd_boolean type_change_ok; | |
1172 | bfd_boolean size_change_ok; | |
1173 | bfd_boolean skip; | |
1174 | char *shortname; | |
1175 | struct elf_link_hash_entry *hi; | |
1176 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1177 | const struct elf_backend_data *bed; |
45d6a902 AM |
1178 | bfd_boolean collect; |
1179 | bfd_boolean dynamic; | |
1180 | char *p; | |
1181 | size_t len, shortlen; | |
1182 | asection *sec; | |
1183 | ||
1184 | /* If this symbol has a version, and it is the default version, we | |
1185 | create an indirect symbol from the default name to the fully | |
1186 | decorated name. This will cause external references which do not | |
1187 | specify a version to be bound to this version of the symbol. */ | |
1188 | p = strchr (name, ELF_VER_CHR); | |
1189 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1190 | return TRUE; | |
1191 | ||
1192 | if (override) | |
1193 | { | |
4cc11e76 | 1194 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1195 | need to create the indirect symbol from the default name. */ |
1196 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1197 | FALSE, FALSE); | |
1198 | BFD_ASSERT (hi != NULL); | |
1199 | if (hi == h) | |
1200 | return TRUE; | |
1201 | while (hi->root.type == bfd_link_hash_indirect | |
1202 | || hi->root.type == bfd_link_hash_warning) | |
1203 | { | |
1204 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1205 | if (hi == h) | |
1206 | return TRUE; | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | bed = get_elf_backend_data (abfd); | |
1211 | collect = bed->collect; | |
1212 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1213 | ||
1214 | shortlen = p - name; | |
1215 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1216 | if (shortname == NULL) | |
1217 | return FALSE; | |
1218 | memcpy (shortname, name, shortlen); | |
1219 | shortname[shortlen] = '\0'; | |
1220 | ||
1221 | /* We are going to create a new symbol. Merge it with any existing | |
1222 | symbol with this name. For the purposes of the merge, act as | |
1223 | though we were defining the symbol we just defined, although we | |
1224 | actually going to define an indirect symbol. */ | |
1225 | type_change_ok = FALSE; | |
1226 | size_change_ok = FALSE; | |
1227 | sec = *psec; | |
1228 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
1229 | &hi, &skip, &override, &type_change_ok, | |
0f8a2703 | 1230 | &size_change_ok)) |
45d6a902 AM |
1231 | return FALSE; |
1232 | ||
1233 | if (skip) | |
1234 | goto nondefault; | |
1235 | ||
1236 | if (! override) | |
1237 | { | |
1238 | bh = &hi->root; | |
1239 | if (! (_bfd_generic_link_add_one_symbol | |
1240 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1241 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1242 | return FALSE; |
1243 | hi = (struct elf_link_hash_entry *) bh; | |
1244 | } | |
1245 | else | |
1246 | { | |
1247 | /* In this case the symbol named SHORTNAME is overriding the | |
1248 | indirect symbol we want to add. We were planning on making | |
1249 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1250 | is the name without a version. NAME is the fully versioned | |
1251 | name, and it is the default version. | |
1252 | ||
1253 | Overriding means that we already saw a definition for the | |
1254 | symbol SHORTNAME in a regular object, and it is overriding | |
1255 | the symbol defined in the dynamic object. | |
1256 | ||
1257 | When this happens, we actually want to change NAME, the | |
1258 | symbol we just added, to refer to SHORTNAME. This will cause | |
1259 | references to NAME in the shared object to become references | |
1260 | to SHORTNAME in the regular object. This is what we expect | |
1261 | when we override a function in a shared object: that the | |
1262 | references in the shared object will be mapped to the | |
1263 | definition in the regular object. */ | |
1264 | ||
1265 | while (hi->root.type == bfd_link_hash_indirect | |
1266 | || hi->root.type == bfd_link_hash_warning) | |
1267 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1268 | ||
1269 | h->root.type = bfd_link_hash_indirect; | |
1270 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
1271 | if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) | |
1272 | { | |
1273 | h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC; | |
1274 | hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
1275 | if (hi->elf_link_hash_flags | |
1276 | & (ELF_LINK_HASH_REF_REGULAR | |
1277 | | ELF_LINK_HASH_DEF_REGULAR)) | |
1278 | { | |
c152c796 | 1279 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1280 | return FALSE; |
1281 | } | |
1282 | } | |
1283 | ||
1284 | /* Now set HI to H, so that the following code will set the | |
1285 | other fields correctly. */ | |
1286 | hi = h; | |
1287 | } | |
1288 | ||
1289 | /* If there is a duplicate definition somewhere, then HI may not | |
1290 | point to an indirect symbol. We will have reported an error to | |
1291 | the user in that case. */ | |
1292 | ||
1293 | if (hi->root.type == bfd_link_hash_indirect) | |
1294 | { | |
1295 | struct elf_link_hash_entry *ht; | |
1296 | ||
45d6a902 AM |
1297 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
1298 | (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi); | |
1299 | ||
1300 | /* See if the new flags lead us to realize that the symbol must | |
1301 | be dynamic. */ | |
1302 | if (! *dynsym) | |
1303 | { | |
1304 | if (! dynamic) | |
1305 | { | |
1306 | if (info->shared | |
1307 | || ((hi->elf_link_hash_flags | |
1308 | & ELF_LINK_HASH_REF_DYNAMIC) != 0)) | |
1309 | *dynsym = TRUE; | |
1310 | } | |
1311 | else | |
1312 | { | |
1313 | if ((hi->elf_link_hash_flags | |
1314 | & ELF_LINK_HASH_REF_REGULAR) != 0) | |
1315 | *dynsym = TRUE; | |
1316 | } | |
1317 | } | |
1318 | } | |
1319 | ||
1320 | /* We also need to define an indirection from the nondefault version | |
1321 | of the symbol. */ | |
1322 | ||
1323 | nondefault: | |
1324 | len = strlen (name); | |
1325 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1326 | if (shortname == NULL) | |
1327 | return FALSE; | |
1328 | memcpy (shortname, name, shortlen); | |
1329 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1330 | ||
1331 | /* Once again, merge with any existing symbol. */ | |
1332 | type_change_ok = FALSE; | |
1333 | size_change_ok = FALSE; | |
1334 | sec = *psec; | |
1335 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
1336 | &hi, &skip, &override, &type_change_ok, | |
0f8a2703 | 1337 | &size_change_ok)) |
45d6a902 AM |
1338 | return FALSE; |
1339 | ||
1340 | if (skip) | |
1341 | return TRUE; | |
1342 | ||
1343 | if (override) | |
1344 | { | |
1345 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1346 | the type of override we do in the case above unless it is | |
4cc11e76 | 1347 | overridden by a versioned definition. */ |
45d6a902 AM |
1348 | if (hi->root.type != bfd_link_hash_defined |
1349 | && hi->root.type != bfd_link_hash_defweak) | |
1350 | (*_bfd_error_handler) | |
1351 | (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"), | |
1352 | bfd_archive_filename (abfd), shortname); | |
1353 | } | |
1354 | else | |
1355 | { | |
1356 | bh = &hi->root; | |
1357 | if (! (_bfd_generic_link_add_one_symbol | |
1358 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1359 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1360 | return FALSE; |
1361 | hi = (struct elf_link_hash_entry *) bh; | |
1362 | ||
1363 | /* If there is a duplicate definition somewhere, then HI may not | |
1364 | point to an indirect symbol. We will have reported an error | |
1365 | to the user in that case. */ | |
1366 | ||
1367 | if (hi->root.type == bfd_link_hash_indirect) | |
1368 | { | |
45d6a902 AM |
1369 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); |
1370 | ||
1371 | /* See if the new flags lead us to realize that the symbol | |
1372 | must be dynamic. */ | |
1373 | if (! *dynsym) | |
1374 | { | |
1375 | if (! dynamic) | |
1376 | { | |
1377 | if (info->shared | |
1378 | || ((hi->elf_link_hash_flags | |
1379 | & ELF_LINK_HASH_REF_DYNAMIC) != 0)) | |
1380 | *dynsym = TRUE; | |
1381 | } | |
1382 | else | |
1383 | { | |
1384 | if ((hi->elf_link_hash_flags | |
1385 | & ELF_LINK_HASH_REF_REGULAR) != 0) | |
1386 | *dynsym = TRUE; | |
1387 | } | |
1388 | } | |
1389 | } | |
1390 | } | |
1391 | ||
1392 | return TRUE; | |
1393 | } | |
1394 | \f | |
1395 | /* This routine is used to export all defined symbols into the dynamic | |
1396 | symbol table. It is called via elf_link_hash_traverse. */ | |
1397 | ||
1398 | bfd_boolean | |
268b6b39 | 1399 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1400 | { |
268b6b39 | 1401 | struct elf_info_failed *eif = data; |
45d6a902 AM |
1402 | |
1403 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
1404 | if (h->root.type == bfd_link_hash_indirect) | |
1405 | return TRUE; | |
1406 | ||
1407 | if (h->root.type == bfd_link_hash_warning) | |
1408 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1409 | ||
1410 | if (h->dynindx == -1 | |
1411 | && (h->elf_link_hash_flags | |
1412 | & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0) | |
1413 | { | |
1414 | struct bfd_elf_version_tree *t; | |
1415 | struct bfd_elf_version_expr *d; | |
1416 | ||
1417 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1418 | { | |
108ba305 | 1419 | if (t->globals.list != NULL) |
45d6a902 | 1420 | { |
108ba305 JJ |
1421 | d = (*t->match) (&t->globals, NULL, h->root.root.string); |
1422 | if (d != NULL) | |
1423 | goto doit; | |
45d6a902 AM |
1424 | } |
1425 | ||
108ba305 | 1426 | if (t->locals.list != NULL) |
45d6a902 | 1427 | { |
108ba305 JJ |
1428 | d = (*t->match) (&t->locals, NULL, h->root.root.string); |
1429 | if (d != NULL) | |
1430 | return TRUE; | |
45d6a902 AM |
1431 | } |
1432 | } | |
1433 | ||
1434 | if (!eif->verdefs) | |
1435 | { | |
1436 | doit: | |
c152c796 | 1437 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
1438 | { |
1439 | eif->failed = TRUE; | |
1440 | return FALSE; | |
1441 | } | |
1442 | } | |
1443 | } | |
1444 | ||
1445 | return TRUE; | |
1446 | } | |
1447 | \f | |
1448 | /* Look through the symbols which are defined in other shared | |
1449 | libraries and referenced here. Update the list of version | |
1450 | dependencies. This will be put into the .gnu.version_r section. | |
1451 | This function is called via elf_link_hash_traverse. */ | |
1452 | ||
1453 | bfd_boolean | |
268b6b39 AM |
1454 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1455 | void *data) | |
45d6a902 | 1456 | { |
268b6b39 | 1457 | struct elf_find_verdep_info *rinfo = data; |
45d6a902 AM |
1458 | Elf_Internal_Verneed *t; |
1459 | Elf_Internal_Vernaux *a; | |
1460 | bfd_size_type amt; | |
1461 | ||
1462 | if (h->root.type == bfd_link_hash_warning) | |
1463 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1464 | ||
1465 | /* We only care about symbols defined in shared objects with version | |
1466 | information. */ | |
1467 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
1468 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
1469 | || h->dynindx == -1 | |
1470 | || h->verinfo.verdef == NULL) | |
1471 | return TRUE; | |
1472 | ||
1473 | /* See if we already know about this version. */ | |
1474 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1475 | { | |
1476 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1477 | continue; | |
1478 | ||
1479 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1480 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1481 | return TRUE; | |
1482 | ||
1483 | break; | |
1484 | } | |
1485 | ||
1486 | /* This is a new version. Add it to tree we are building. */ | |
1487 | ||
1488 | if (t == NULL) | |
1489 | { | |
1490 | amt = sizeof *t; | |
268b6b39 | 1491 | t = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1492 | if (t == NULL) |
1493 | { | |
1494 | rinfo->failed = TRUE; | |
1495 | return FALSE; | |
1496 | } | |
1497 | ||
1498 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1499 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1500 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1501 | } | |
1502 | ||
1503 | amt = sizeof *a; | |
268b6b39 | 1504 | a = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1505 | |
1506 | /* Note that we are copying a string pointer here, and testing it | |
1507 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1508 | discard the string data when low in memory, this will have to be | |
1509 | fixed. */ | |
1510 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1511 | ||
1512 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1513 | a->vna_nextptr = t->vn_auxptr; | |
1514 | ||
1515 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1516 | ++rinfo->vers; | |
1517 | ||
1518 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1519 | ||
1520 | t->vn_auxptr = a; | |
1521 | ||
1522 | return TRUE; | |
1523 | } | |
1524 | ||
1525 | /* Figure out appropriate versions for all the symbols. We may not | |
1526 | have the version number script until we have read all of the input | |
1527 | files, so until that point we don't know which symbols should be | |
1528 | local. This function is called via elf_link_hash_traverse. */ | |
1529 | ||
1530 | bfd_boolean | |
268b6b39 | 1531 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
1532 | { |
1533 | struct elf_assign_sym_version_info *sinfo; | |
1534 | struct bfd_link_info *info; | |
9c5bfbb7 | 1535 | const struct elf_backend_data *bed; |
45d6a902 AM |
1536 | struct elf_info_failed eif; |
1537 | char *p; | |
1538 | bfd_size_type amt; | |
1539 | ||
268b6b39 | 1540 | sinfo = data; |
45d6a902 AM |
1541 | info = sinfo->info; |
1542 | ||
1543 | if (h->root.type == bfd_link_hash_warning) | |
1544 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1545 | ||
1546 | /* Fix the symbol flags. */ | |
1547 | eif.failed = FALSE; | |
1548 | eif.info = info; | |
1549 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1550 | { | |
1551 | if (eif.failed) | |
1552 | sinfo->failed = TRUE; | |
1553 | return FALSE; | |
1554 | } | |
1555 | ||
1556 | /* We only need version numbers for symbols defined in regular | |
1557 | objects. */ | |
1558 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1559 | return TRUE; | |
1560 | ||
1561 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1562 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1563 | if (p != NULL && h->verinfo.vertree == NULL) | |
1564 | { | |
1565 | struct bfd_elf_version_tree *t; | |
1566 | bfd_boolean hidden; | |
1567 | ||
1568 | hidden = TRUE; | |
1569 | ||
1570 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1571 | not a hidden symbol. */ | |
1572 | ++p; | |
1573 | if (*p == ELF_VER_CHR) | |
1574 | { | |
1575 | hidden = FALSE; | |
1576 | ++p; | |
1577 | } | |
1578 | ||
1579 | /* If there is no version string, we can just return out. */ | |
1580 | if (*p == '\0') | |
1581 | { | |
1582 | if (hidden) | |
1583 | h->elf_link_hash_flags |= ELF_LINK_HIDDEN; | |
1584 | return TRUE; | |
1585 | } | |
1586 | ||
1587 | /* Look for the version. If we find it, it is no longer weak. */ | |
1588 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1589 | { | |
1590 | if (strcmp (t->name, p) == 0) | |
1591 | { | |
1592 | size_t len; | |
1593 | char *alc; | |
1594 | struct bfd_elf_version_expr *d; | |
1595 | ||
1596 | len = p - h->root.root.string; | |
268b6b39 | 1597 | alc = bfd_malloc (len); |
45d6a902 AM |
1598 | if (alc == NULL) |
1599 | return FALSE; | |
1600 | memcpy (alc, h->root.root.string, len - 1); | |
1601 | alc[len - 1] = '\0'; | |
1602 | if (alc[len - 2] == ELF_VER_CHR) | |
1603 | alc[len - 2] = '\0'; | |
1604 | ||
1605 | h->verinfo.vertree = t; | |
1606 | t->used = TRUE; | |
1607 | d = NULL; | |
1608 | ||
108ba305 JJ |
1609 | if (t->globals.list != NULL) |
1610 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
1611 | |
1612 | /* See if there is anything to force this symbol to | |
1613 | local scope. */ | |
108ba305 | 1614 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 1615 | { |
108ba305 JJ |
1616 | d = (*t->match) (&t->locals, NULL, alc); |
1617 | if (d != NULL | |
1618 | && h->dynindx != -1 | |
1619 | && info->shared | |
1620 | && ! info->export_dynamic) | |
1621 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
1622 | } |
1623 | ||
1624 | free (alc); | |
1625 | break; | |
1626 | } | |
1627 | } | |
1628 | ||
1629 | /* If we are building an application, we need to create a | |
1630 | version node for this version. */ | |
36af4a4e | 1631 | if (t == NULL && info->executable) |
45d6a902 AM |
1632 | { |
1633 | struct bfd_elf_version_tree **pp; | |
1634 | int version_index; | |
1635 | ||
1636 | /* If we aren't going to export this symbol, we don't need | |
1637 | to worry about it. */ | |
1638 | if (h->dynindx == -1) | |
1639 | return TRUE; | |
1640 | ||
1641 | amt = sizeof *t; | |
108ba305 | 1642 | t = bfd_zalloc (sinfo->output_bfd, amt); |
45d6a902 AM |
1643 | if (t == NULL) |
1644 | { | |
1645 | sinfo->failed = TRUE; | |
1646 | return FALSE; | |
1647 | } | |
1648 | ||
45d6a902 | 1649 | t->name = p; |
45d6a902 AM |
1650 | t->name_indx = (unsigned int) -1; |
1651 | t->used = TRUE; | |
1652 | ||
1653 | version_index = 1; | |
1654 | /* Don't count anonymous version tag. */ | |
1655 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1656 | version_index = 0; | |
1657 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1658 | ++version_index; | |
1659 | t->vernum = version_index; | |
1660 | ||
1661 | *pp = t; | |
1662 | ||
1663 | h->verinfo.vertree = t; | |
1664 | } | |
1665 | else if (t == NULL) | |
1666 | { | |
1667 | /* We could not find the version for a symbol when | |
1668 | generating a shared archive. Return an error. */ | |
1669 | (*_bfd_error_handler) | |
1670 | (_("%s: undefined versioned symbol name %s"), | |
1671 | bfd_get_filename (sinfo->output_bfd), h->root.root.string); | |
1672 | bfd_set_error (bfd_error_bad_value); | |
1673 | sinfo->failed = TRUE; | |
1674 | return FALSE; | |
1675 | } | |
1676 | ||
1677 | if (hidden) | |
1678 | h->elf_link_hash_flags |= ELF_LINK_HIDDEN; | |
1679 | } | |
1680 | ||
1681 | /* If we don't have a version for this symbol, see if we can find | |
1682 | something. */ | |
1683 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
1684 | { | |
1685 | struct bfd_elf_version_tree *t; | |
1686 | struct bfd_elf_version_tree *local_ver; | |
1687 | struct bfd_elf_version_expr *d; | |
1688 | ||
1689 | /* See if can find what version this symbol is in. If the | |
1690 | symbol is supposed to be local, then don't actually register | |
1691 | it. */ | |
1692 | local_ver = NULL; | |
1693 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1694 | { | |
108ba305 | 1695 | if (t->globals.list != NULL) |
45d6a902 AM |
1696 | { |
1697 | bfd_boolean matched; | |
1698 | ||
1699 | matched = FALSE; | |
108ba305 JJ |
1700 | d = NULL; |
1701 | while ((d = (*t->match) (&t->globals, d, | |
1702 | h->root.root.string)) != NULL) | |
1703 | if (d->symver) | |
1704 | matched = TRUE; | |
1705 | else | |
1706 | { | |
1707 | /* There is a version without definition. Make | |
1708 | the symbol the default definition for this | |
1709 | version. */ | |
1710 | h->verinfo.vertree = t; | |
1711 | local_ver = NULL; | |
1712 | d->script = 1; | |
1713 | break; | |
1714 | } | |
45d6a902 AM |
1715 | if (d != NULL) |
1716 | break; | |
1717 | else if (matched) | |
1718 | /* There is no undefined version for this symbol. Hide the | |
1719 | default one. */ | |
1720 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1721 | } | |
1722 | ||
108ba305 | 1723 | if (t->locals.list != NULL) |
45d6a902 | 1724 | { |
108ba305 JJ |
1725 | d = NULL; |
1726 | while ((d = (*t->match) (&t->locals, d, | |
1727 | h->root.root.string)) != NULL) | |
45d6a902 | 1728 | { |
108ba305 | 1729 | local_ver = t; |
45d6a902 | 1730 | /* If the match is "*", keep looking for a more |
108ba305 JJ |
1731 | explicit, perhaps even global, match. |
1732 | XXX: Shouldn't this be !d->wildcard instead? */ | |
1733 | if (d->pattern[0] != '*' || d->pattern[1] != '\0') | |
1734 | break; | |
45d6a902 AM |
1735 | } |
1736 | ||
1737 | if (d != NULL) | |
1738 | break; | |
1739 | } | |
1740 | } | |
1741 | ||
1742 | if (local_ver != NULL) | |
1743 | { | |
1744 | h->verinfo.vertree = local_ver; | |
1745 | if (h->dynindx != -1 | |
1746 | && info->shared | |
1747 | && ! info->export_dynamic) | |
1748 | { | |
1749 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1750 | } | |
1751 | } | |
1752 | } | |
1753 | ||
1754 | return TRUE; | |
1755 | } | |
1756 | \f | |
45d6a902 AM |
1757 | /* Read and swap the relocs from the section indicated by SHDR. This |
1758 | may be either a REL or a RELA section. The relocations are | |
1759 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
1760 | which should have already been allocated to contain enough space. | |
1761 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
1762 | relocations should be stored. | |
1763 | ||
1764 | Returns FALSE if something goes wrong. */ | |
1765 | ||
1766 | static bfd_boolean | |
268b6b39 | 1767 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 1768 | asection *sec, |
268b6b39 AM |
1769 | Elf_Internal_Shdr *shdr, |
1770 | void *external_relocs, | |
1771 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 1772 | { |
9c5bfbb7 | 1773 | const struct elf_backend_data *bed; |
268b6b39 | 1774 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
1775 | const bfd_byte *erela; |
1776 | const bfd_byte *erelaend; | |
1777 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
1778 | Elf_Internal_Shdr *symtab_hdr; |
1779 | size_t nsyms; | |
45d6a902 | 1780 | |
45d6a902 AM |
1781 | /* Position ourselves at the start of the section. */ |
1782 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
1783 | return FALSE; | |
1784 | ||
1785 | /* Read the relocations. */ | |
1786 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
1787 | return FALSE; | |
1788 | ||
243ef1e0 L |
1789 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1790 | nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
1791 | ||
45d6a902 AM |
1792 | bed = get_elf_backend_data (abfd); |
1793 | ||
1794 | /* Convert the external relocations to the internal format. */ | |
1795 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
1796 | swap_in = bed->s->swap_reloc_in; | |
1797 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
1798 | swap_in = bed->s->swap_reloca_in; | |
1799 | else | |
1800 | { | |
1801 | bfd_set_error (bfd_error_wrong_format); | |
1802 | return FALSE; | |
1803 | } | |
1804 | ||
1805 | erela = external_relocs; | |
51992aec | 1806 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
1807 | irela = internal_relocs; |
1808 | while (erela < erelaend) | |
1809 | { | |
243ef1e0 L |
1810 | bfd_vma r_symndx; |
1811 | ||
45d6a902 | 1812 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
1813 | r_symndx = ELF32_R_SYM (irela->r_info); |
1814 | if (bed->s->arch_size == 64) | |
1815 | r_symndx >>= 24; | |
1816 | if ((size_t) r_symndx >= nsyms) | |
1817 | { | |
1818 | (*_bfd_error_handler) | |
1819 | (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"), | |
1820 | bfd_archive_filename (abfd), (unsigned long) r_symndx, | |
1821 | (unsigned long) nsyms, irela->r_offset, sec->name); | |
1822 | bfd_set_error (bfd_error_bad_value); | |
1823 | return FALSE; | |
1824 | } | |
45d6a902 AM |
1825 | irela += bed->s->int_rels_per_ext_rel; |
1826 | erela += shdr->sh_entsize; | |
1827 | } | |
1828 | ||
1829 | return TRUE; | |
1830 | } | |
1831 | ||
1832 | /* Read and swap the relocs for a section O. They may have been | |
1833 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
1834 | not NULL, they are used as buffers to read into. They are known to | |
1835 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
1836 | the return value is allocated using either malloc or bfd_alloc, | |
1837 | according to the KEEP_MEMORY argument. If O has two relocation | |
1838 | sections (both REL and RELA relocations), then the REL_HDR | |
1839 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
1840 | REL_HDR2 relocations. */ | |
1841 | ||
1842 | Elf_Internal_Rela * | |
268b6b39 AM |
1843 | _bfd_elf_link_read_relocs (bfd *abfd, |
1844 | asection *o, | |
1845 | void *external_relocs, | |
1846 | Elf_Internal_Rela *internal_relocs, | |
1847 | bfd_boolean keep_memory) | |
45d6a902 AM |
1848 | { |
1849 | Elf_Internal_Shdr *rel_hdr; | |
268b6b39 | 1850 | void *alloc1 = NULL; |
45d6a902 | 1851 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 1852 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1853 | |
1854 | if (elf_section_data (o)->relocs != NULL) | |
1855 | return elf_section_data (o)->relocs; | |
1856 | ||
1857 | if (o->reloc_count == 0) | |
1858 | return NULL; | |
1859 | ||
1860 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
1861 | ||
1862 | if (internal_relocs == NULL) | |
1863 | { | |
1864 | bfd_size_type size; | |
1865 | ||
1866 | size = o->reloc_count; | |
1867 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
1868 | if (keep_memory) | |
268b6b39 | 1869 | internal_relocs = bfd_alloc (abfd, size); |
45d6a902 | 1870 | else |
268b6b39 | 1871 | internal_relocs = alloc2 = bfd_malloc (size); |
45d6a902 AM |
1872 | if (internal_relocs == NULL) |
1873 | goto error_return; | |
1874 | } | |
1875 | ||
1876 | if (external_relocs == NULL) | |
1877 | { | |
1878 | bfd_size_type size = rel_hdr->sh_size; | |
1879 | ||
1880 | if (elf_section_data (o)->rel_hdr2) | |
1881 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
268b6b39 | 1882 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
1883 | if (alloc1 == NULL) |
1884 | goto error_return; | |
1885 | external_relocs = alloc1; | |
1886 | } | |
1887 | ||
243ef1e0 | 1888 | if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, |
45d6a902 AM |
1889 | external_relocs, |
1890 | internal_relocs)) | |
1891 | goto error_return; | |
51992aec AM |
1892 | if (elf_section_data (o)->rel_hdr2 |
1893 | && (!elf_link_read_relocs_from_section | |
1894 | (abfd, o, | |
1895 | elf_section_data (o)->rel_hdr2, | |
1896 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
1897 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
1898 | * bed->s->int_rels_per_ext_rel)))) | |
45d6a902 AM |
1899 | goto error_return; |
1900 | ||
1901 | /* Cache the results for next time, if we can. */ | |
1902 | if (keep_memory) | |
1903 | elf_section_data (o)->relocs = internal_relocs; | |
1904 | ||
1905 | if (alloc1 != NULL) | |
1906 | free (alloc1); | |
1907 | ||
1908 | /* Don't free alloc2, since if it was allocated we are passing it | |
1909 | back (under the name of internal_relocs). */ | |
1910 | ||
1911 | return internal_relocs; | |
1912 | ||
1913 | error_return: | |
1914 | if (alloc1 != NULL) | |
1915 | free (alloc1); | |
1916 | if (alloc2 != NULL) | |
1917 | free (alloc2); | |
1918 | return NULL; | |
1919 | } | |
1920 | ||
1921 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
1922 | section header for a section containing relocations for O. */ | |
1923 | ||
1924 | bfd_boolean | |
268b6b39 AM |
1925 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
1926 | Elf_Internal_Shdr *rel_hdr, | |
1927 | asection *o) | |
45d6a902 AM |
1928 | { |
1929 | bfd_size_type reloc_count; | |
1930 | bfd_size_type num_rel_hashes; | |
1931 | ||
1932 | /* Figure out how many relocations there will be. */ | |
1933 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
1934 | reloc_count = elf_section_data (o)->rel_count; | |
1935 | else | |
1936 | reloc_count = elf_section_data (o)->rel_count2; | |
1937 | ||
1938 | num_rel_hashes = o->reloc_count; | |
1939 | if (num_rel_hashes < reloc_count) | |
1940 | num_rel_hashes = reloc_count; | |
1941 | ||
1942 | /* That allows us to calculate the size of the section. */ | |
1943 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
1944 | ||
1945 | /* The contents field must last into write_object_contents, so we | |
1946 | allocate it with bfd_alloc rather than malloc. Also since we | |
1947 | cannot be sure that the contents will actually be filled in, | |
1948 | we zero the allocated space. */ | |
268b6b39 | 1949 | rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
1950 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
1951 | return FALSE; | |
1952 | ||
1953 | /* We only allocate one set of hash entries, so we only do it the | |
1954 | first time we are called. */ | |
1955 | if (elf_section_data (o)->rel_hashes == NULL | |
1956 | && num_rel_hashes) | |
1957 | { | |
1958 | struct elf_link_hash_entry **p; | |
1959 | ||
268b6b39 | 1960 | p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
1961 | if (p == NULL) |
1962 | return FALSE; | |
1963 | ||
1964 | elf_section_data (o)->rel_hashes = p; | |
1965 | } | |
1966 | ||
1967 | return TRUE; | |
1968 | } | |
1969 | ||
1970 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
1971 | originated from the section given by INPUT_REL_HDR) to the | |
1972 | OUTPUT_BFD. */ | |
1973 | ||
1974 | bfd_boolean | |
268b6b39 AM |
1975 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
1976 | asection *input_section, | |
1977 | Elf_Internal_Shdr *input_rel_hdr, | |
1978 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 AM |
1979 | { |
1980 | Elf_Internal_Rela *irela; | |
1981 | Elf_Internal_Rela *irelaend; | |
1982 | bfd_byte *erel; | |
1983 | Elf_Internal_Shdr *output_rel_hdr; | |
1984 | asection *output_section; | |
1985 | unsigned int *rel_countp = NULL; | |
9c5bfbb7 | 1986 | const struct elf_backend_data *bed; |
268b6b39 | 1987 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
45d6a902 AM |
1988 | |
1989 | output_section = input_section->output_section; | |
1990 | output_rel_hdr = NULL; | |
1991 | ||
1992 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
1993 | == input_rel_hdr->sh_entsize) | |
1994 | { | |
1995 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
1996 | rel_countp = &elf_section_data (output_section)->rel_count; | |
1997 | } | |
1998 | else if (elf_section_data (output_section)->rel_hdr2 | |
1999 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
2000 | == input_rel_hdr->sh_entsize)) | |
2001 | { | |
2002 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2003 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2004 | } | |
2005 | else | |
2006 | { | |
2007 | (*_bfd_error_handler) | |
2008 | (_("%s: relocation size mismatch in %s section %s"), | |
2009 | bfd_get_filename (output_bfd), | |
2010 | bfd_archive_filename (input_section->owner), | |
2011 | input_section->name); | |
2012 | bfd_set_error (bfd_error_wrong_object_format); | |
2013 | return FALSE; | |
2014 | } | |
2015 | ||
2016 | bed = get_elf_backend_data (output_bfd); | |
2017 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2018 | swap_out = bed->s->swap_reloc_out; | |
2019 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2020 | swap_out = bed->s->swap_reloca_out; | |
2021 | else | |
2022 | abort (); | |
2023 | ||
2024 | erel = output_rel_hdr->contents; | |
2025 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2026 | irela = internal_relocs; | |
2027 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2028 | * bed->s->int_rels_per_ext_rel); | |
2029 | while (irela < irelaend) | |
2030 | { | |
2031 | (*swap_out) (output_bfd, irela, erel); | |
2032 | irela += bed->s->int_rels_per_ext_rel; | |
2033 | erel += input_rel_hdr->sh_entsize; | |
2034 | } | |
2035 | ||
2036 | /* Bump the counter, so that we know where to add the next set of | |
2037 | relocations. */ | |
2038 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2039 | ||
2040 | return TRUE; | |
2041 | } | |
2042 | \f | |
2043 | /* Fix up the flags for a symbol. This handles various cases which | |
2044 | can only be fixed after all the input files are seen. This is | |
2045 | currently called by both adjust_dynamic_symbol and | |
2046 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2047 | the face of future changes. */ | |
2048 | ||
2049 | bfd_boolean | |
268b6b39 AM |
2050 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2051 | struct elf_info_failed *eif) | |
45d6a902 AM |
2052 | { |
2053 | /* If this symbol was mentioned in a non-ELF file, try to set | |
2054 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2055 | permit a non-ELF file to correctly refer to a symbol defined in | |
2056 | an ELF dynamic object. */ | |
2057 | if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0) | |
2058 | { | |
2059 | while (h->root.type == bfd_link_hash_indirect) | |
2060 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2061 | ||
2062 | if (h->root.type != bfd_link_hash_defined | |
2063 | && h->root.type != bfd_link_hash_defweak) | |
2064 | h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR | |
2065 | | ELF_LINK_HASH_REF_REGULAR_NONWEAK); | |
2066 | else | |
2067 | { | |
2068 | if (h->root.u.def.section->owner != NULL | |
2069 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2070 | == bfd_target_elf_flavour)) | |
2071 | h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR | |
2072 | | ELF_LINK_HASH_REF_REGULAR_NONWEAK); | |
2073 | else | |
2074 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2075 | } | |
2076 | ||
2077 | if (h->dynindx == -1 | |
2078 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
2079 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)) | |
2080 | { | |
c152c796 | 2081 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2082 | { |
2083 | eif->failed = TRUE; | |
2084 | return FALSE; | |
2085 | } | |
2086 | } | |
2087 | } | |
2088 | else | |
2089 | { | |
2090 | /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol | |
2091 | was first seen in a non-ELF file. Fortunately, if the symbol | |
2092 | was first seen in an ELF file, we're probably OK unless the | |
2093 | symbol was defined in a non-ELF file. Catch that case here. | |
2094 | FIXME: We're still in trouble if the symbol was first seen in | |
2095 | a dynamic object, and then later in a non-ELF regular object. */ | |
2096 | if ((h->root.type == bfd_link_hash_defined | |
2097 | || h->root.type == bfd_link_hash_defweak) | |
2098 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
2099 | && (h->root.u.def.section->owner != NULL | |
2100 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2101 | != bfd_target_elf_flavour) | |
2102 | : (bfd_is_abs_section (h->root.u.def.section) | |
2103 | && (h->elf_link_hash_flags | |
2104 | & ELF_LINK_HASH_DEF_DYNAMIC) == 0))) | |
2105 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2106 | } | |
2107 | ||
2108 | /* If this is a final link, and the symbol was defined as a common | |
2109 | symbol in a regular object file, and there was no definition in | |
2110 | any dynamic object, then the linker will have allocated space for | |
2111 | the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR | |
2112 | flag will not have been set. */ | |
2113 | if (h->root.type == bfd_link_hash_defined | |
2114 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
2115 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0 | |
2116 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
2117 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
2118 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2119 | ||
2120 | /* If -Bsymbolic was used (which means to bind references to global | |
2121 | symbols to the definition within the shared object), and this | |
2122 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2123 | need a PLT entry. Likewise, if the symbol has non-default |
2124 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2125 | will force it local. */ |
45d6a902 AM |
2126 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0 |
2127 | && eif->info->shared | |
0eddce27 | 2128 | && is_elf_hash_table (eif->info->hash) |
45d6a902 | 2129 | && (eif->info->symbolic |
c1be741f | 2130 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
45d6a902 AM |
2131 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) |
2132 | { | |
9c5bfbb7 | 2133 | const struct elf_backend_data *bed; |
45d6a902 AM |
2134 | bfd_boolean force_local; |
2135 | ||
2136 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2137 | ||
2138 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL | |
2139 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2140 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2141 | } | |
2142 | ||
2143 | /* If a weak undefined symbol has non-default visibility, we also | |
2144 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2145 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
2146 | && h->root.type == bfd_link_hash_undefweak) |
2147 | { | |
9c5bfbb7 | 2148 | const struct elf_backend_data *bed; |
45d6a902 AM |
2149 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2150 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2151 | } | |
2152 | ||
2153 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2154 | the real definition in the dynamic object, copy interesting flags | |
2155 | over to the real definition. */ | |
2156 | if (h->weakdef != NULL) | |
2157 | { | |
2158 | struct elf_link_hash_entry *weakdef; | |
2159 | ||
2160 | weakdef = h->weakdef; | |
2161 | if (h->root.type == bfd_link_hash_indirect) | |
2162 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2163 | ||
2164 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2165 | || h->root.type == bfd_link_hash_defweak); | |
2166 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2167 | || weakdef->root.type == bfd_link_hash_defweak); | |
2168 | BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC); | |
2169 | ||
2170 | /* If the real definition is defined by a regular object file, | |
2171 | don't do anything special. See the longer description in | |
2172 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
2173 | if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) | |
2174 | h->weakdef = NULL; | |
2175 | else | |
2176 | { | |
9c5bfbb7 | 2177 | const struct elf_backend_data *bed; |
45d6a902 AM |
2178 | |
2179 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2180 | (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h); | |
2181 | } | |
2182 | } | |
2183 | ||
2184 | return TRUE; | |
2185 | } | |
2186 | ||
2187 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2188 | called via elf_link_hash_traverse, and also calls itself | |
2189 | recursively. */ | |
2190 | ||
2191 | bfd_boolean | |
268b6b39 | 2192 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2193 | { |
268b6b39 | 2194 | struct elf_info_failed *eif = data; |
45d6a902 | 2195 | bfd *dynobj; |
9c5bfbb7 | 2196 | const struct elf_backend_data *bed; |
45d6a902 | 2197 | |
0eddce27 | 2198 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2199 | return FALSE; |
2200 | ||
2201 | if (h->root.type == bfd_link_hash_warning) | |
2202 | { | |
2203 | h->plt = elf_hash_table (eif->info)->init_offset; | |
2204 | h->got = elf_hash_table (eif->info)->init_offset; | |
2205 | ||
2206 | /* When warning symbols are created, they **replace** the "real" | |
2207 | entry in the hash table, thus we never get to see the real | |
2208 | symbol in a hash traversal. So look at it now. */ | |
2209 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2210 | } | |
2211 | ||
2212 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2213 | if (h->root.type == bfd_link_hash_indirect) | |
2214 | return TRUE; | |
2215 | ||
2216 | /* Fix the symbol flags. */ | |
2217 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2218 | return FALSE; | |
2219 | ||
2220 | /* If this symbol does not require a PLT entry, and it is not | |
2221 | defined by a dynamic object, or is not referenced by a regular | |
2222 | object, ignore it. We do have to handle a weak defined symbol, | |
2223 | even if no regular object refers to it, if we decided to add it | |
2224 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2225 | about symbols which are defined by one dynamic object and | |
2226 | referenced by another one? */ | |
2227 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0 | |
2228 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
2229 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
2230 | || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0 | |
2231 | && (h->weakdef == NULL || h->weakdef->dynindx == -1)))) | |
2232 | { | |
2233 | h->plt = elf_hash_table (eif->info)->init_offset; | |
2234 | return TRUE; | |
2235 | } | |
2236 | ||
2237 | /* If we've already adjusted this symbol, don't do it again. This | |
2238 | can happen via a recursive call. */ | |
2239 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0) | |
2240 | return TRUE; | |
2241 | ||
2242 | /* Don't look at this symbol again. Note that we must set this | |
2243 | after checking the above conditions, because we may look at a | |
2244 | symbol once, decide not to do anything, and then get called | |
2245 | recursively later after REF_REGULAR is set below. */ | |
2246 | h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED; | |
2247 | ||
2248 | /* If this is a weak definition, and we know a real definition, and | |
2249 | the real symbol is not itself defined by a regular object file, | |
2250 | then get a good value for the real definition. We handle the | |
2251 | real symbol first, for the convenience of the backend routine. | |
2252 | ||
2253 | Note that there is a confusing case here. If the real definition | |
2254 | is defined by a regular object file, we don't get the real symbol | |
2255 | from the dynamic object, but we do get the weak symbol. If the | |
2256 | processor backend uses a COPY reloc, then if some routine in the | |
2257 | dynamic object changes the real symbol, we will not see that | |
2258 | change in the corresponding weak symbol. This is the way other | |
2259 | ELF linkers work as well, and seems to be a result of the shared | |
2260 | library model. | |
2261 | ||
2262 | I will clarify this issue. Most SVR4 shared libraries define the | |
2263 | variable _timezone and define timezone as a weak synonym. The | |
2264 | tzset call changes _timezone. If you write | |
2265 | extern int timezone; | |
2266 | int _timezone = 5; | |
2267 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2268 | you might expect that, since timezone is a synonym for _timezone, | |
2269 | the same number will print both times. However, if the processor | |
2270 | backend uses a COPY reloc, then actually timezone will be copied | |
2271 | into your process image, and, since you define _timezone | |
2272 | yourself, _timezone will not. Thus timezone and _timezone will | |
2273 | wind up at different memory locations. The tzset call will set | |
2274 | _timezone, leaving timezone unchanged. */ | |
2275 | ||
2276 | if (h->weakdef != NULL) | |
2277 | { | |
2278 | /* If we get to this point, we know there is an implicit | |
2279 | reference by a regular object file via the weak symbol H. | |
2280 | FIXME: Is this really true? What if the traversal finds | |
2281 | H->WEAKDEF before it finds H? */ | |
2282 | h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; | |
2283 | ||
268b6b39 | 2284 | if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, eif)) |
45d6a902 AM |
2285 | return FALSE; |
2286 | } | |
2287 | ||
2288 | /* If a symbol has no type and no size and does not require a PLT | |
2289 | entry, then we are probably about to do the wrong thing here: we | |
2290 | are probably going to create a COPY reloc for an empty object. | |
2291 | This case can arise when a shared object is built with assembly | |
2292 | code, and the assembly code fails to set the symbol type. */ | |
2293 | if (h->size == 0 | |
2294 | && h->type == STT_NOTYPE | |
2295 | && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0) | |
2296 | (*_bfd_error_handler) | |
2297 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2298 | h->root.root.string); | |
2299 | ||
2300 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2301 | bed = get_elf_backend_data (dynobj); | |
2302 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2303 | { | |
2304 | eif->failed = TRUE; | |
2305 | return FALSE; | |
2306 | } | |
2307 | ||
2308 | return TRUE; | |
2309 | } | |
2310 | ||
2311 | /* Adjust all external symbols pointing into SEC_MERGE sections | |
2312 | to reflect the object merging within the sections. */ | |
2313 | ||
2314 | bfd_boolean | |
268b6b39 | 2315 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2316 | { |
2317 | asection *sec; | |
2318 | ||
2319 | if (h->root.type == bfd_link_hash_warning) | |
2320 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2321 | ||
2322 | if ((h->root.type == bfd_link_hash_defined | |
2323 | || h->root.type == bfd_link_hash_defweak) | |
2324 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2325 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2326 | { | |
268b6b39 | 2327 | bfd *output_bfd = data; |
45d6a902 AM |
2328 | |
2329 | h->root.u.def.value = | |
2330 | _bfd_merged_section_offset (output_bfd, | |
2331 | &h->root.u.def.section, | |
2332 | elf_section_data (sec)->sec_info, | |
268b6b39 | 2333 | h->root.u.def.value, 0); |
45d6a902 AM |
2334 | } |
2335 | ||
2336 | return TRUE; | |
2337 | } | |
986a241f RH |
2338 | |
2339 | /* Returns false if the symbol referred to by H should be considered | |
2340 | to resolve local to the current module, and true if it should be | |
2341 | considered to bind dynamically. */ | |
2342 | ||
2343 | bfd_boolean | |
268b6b39 AM |
2344 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2345 | struct bfd_link_info *info, | |
2346 | bfd_boolean ignore_protected) | |
986a241f RH |
2347 | { |
2348 | bfd_boolean binding_stays_local_p; | |
2349 | ||
2350 | if (h == NULL) | |
2351 | return FALSE; | |
2352 | ||
2353 | while (h->root.type == bfd_link_hash_indirect | |
2354 | || h->root.type == bfd_link_hash_warning) | |
2355 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2356 | ||
2357 | /* If it was forced local, then clearly it's not dynamic. */ | |
2358 | if (h->dynindx == -1) | |
2359 | return FALSE; | |
2360 | if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) | |
2361 | return FALSE; | |
2362 | ||
2363 | /* Identify the cases where name binding rules say that a | |
2364 | visible symbol resolves locally. */ | |
2365 | binding_stays_local_p = info->executable || info->symbolic; | |
2366 | ||
2367 | switch (ELF_ST_VISIBILITY (h->other)) | |
2368 | { | |
2369 | case STV_INTERNAL: | |
2370 | case STV_HIDDEN: | |
2371 | return FALSE; | |
2372 | ||
2373 | case STV_PROTECTED: | |
2374 | /* Proper resolution for function pointer equality may require | |
2375 | that these symbols perhaps be resolved dynamically, even though | |
2376 | we should be resolving them to the current module. */ | |
2377 | if (!ignore_protected) | |
2378 | binding_stays_local_p = TRUE; | |
2379 | break; | |
2380 | ||
2381 | default: | |
986a241f RH |
2382 | break; |
2383 | } | |
2384 | ||
aa37626c L |
2385 | /* If it isn't defined locally, then clearly it's dynamic. */ |
2386 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
2387 | return TRUE; | |
2388 | ||
986a241f RH |
2389 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2390 | us that it remains local. */ | |
2391 | return !binding_stays_local_p; | |
2392 | } | |
f6c52c13 AM |
2393 | |
2394 | /* Return true if the symbol referred to by H should be considered | |
2395 | to resolve local to the current module, and false otherwise. Differs | |
2396 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2397 | undefined symbols and weak symbols. */ | |
2398 | ||
2399 | bfd_boolean | |
268b6b39 AM |
2400 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2401 | struct bfd_link_info *info, | |
2402 | bfd_boolean local_protected) | |
f6c52c13 AM |
2403 | { |
2404 | /* If it's a local sym, of course we resolve locally. */ | |
2405 | if (h == NULL) | |
2406 | return TRUE; | |
2407 | ||
2408 | /* If we don't have a definition in a regular file, then we can't | |
2409 | resolve locally. The sym is either undefined or dynamic. */ | |
2410 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
2411 | return FALSE; | |
2412 | ||
2413 | /* Forced local symbols resolve locally. */ | |
2414 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) | |
2415 | return TRUE; | |
2416 | ||
2417 | /* As do non-dynamic symbols. */ | |
2418 | if (h->dynindx == -1) | |
2419 | return TRUE; | |
2420 | ||
2421 | /* At this point, we know the symbol is defined and dynamic. In an | |
2422 | executable it must resolve locally, likewise when building symbolic | |
2423 | shared libraries. */ | |
2424 | if (info->executable || info->symbolic) | |
2425 | return TRUE; | |
2426 | ||
2427 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2428 | with default visibility might not resolve locally. */ | |
2429 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2430 | return FALSE; | |
2431 | ||
2432 | /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */ | |
2433 | if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED) | |
2434 | return TRUE; | |
2435 | ||
2436 | /* Function pointer equality tests may require that STV_PROTECTED | |
2437 | symbols be treated as dynamic symbols, even when we know that the | |
2438 | dynamic linker will resolve them locally. */ | |
2439 | return local_protected; | |
2440 | } | |
e1918d23 AM |
2441 | |
2442 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2443 | aligned. Returns the first TLS output section. */ | |
2444 | ||
2445 | struct bfd_section * | |
2446 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2447 | { | |
2448 | struct bfd_section *sec, *tls; | |
2449 | unsigned int align = 0; | |
2450 | ||
2451 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2452 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2453 | break; | |
2454 | tls = sec; | |
2455 | ||
2456 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2457 | if (sec->alignment_power > align) | |
2458 | align = sec->alignment_power; | |
2459 | ||
2460 | elf_hash_table (info)->tls_sec = tls; | |
2461 | ||
2462 | /* Ensure the alignment of the first section is the largest alignment, | |
2463 | so that the tls segment starts aligned. */ | |
2464 | if (tls != NULL) | |
2465 | tls->alignment_power = align; | |
2466 | ||
2467 | return tls; | |
2468 | } | |
0ad989f9 L |
2469 | |
2470 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2471 | static bfd_boolean | |
2472 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2473 | Elf_Internal_Sym *sym) | |
2474 | { | |
2475 | /* Local symbols do not count, but target specific ones might. */ | |
2476 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2477 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2478 | return FALSE; | |
2479 | ||
2480 | /* Function symbols do not count. */ | |
2481 | if (ELF_ST_TYPE (sym->st_info) == STT_FUNC) | |
2482 | return FALSE; | |
2483 | ||
2484 | /* If the section is undefined, then so is the symbol. */ | |
2485 | if (sym->st_shndx == SHN_UNDEF) | |
2486 | return FALSE; | |
2487 | ||
2488 | /* If the symbol is defined in the common section, then | |
2489 | it is a common definition and so does not count. */ | |
2490 | if (sym->st_shndx == SHN_COMMON) | |
2491 | return FALSE; | |
2492 | ||
2493 | /* If the symbol is in a target specific section then we | |
2494 | must rely upon the backend to tell us what it is. */ | |
2495 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2496 | /* FIXME - this function is not coded yet: | |
2497 | ||
2498 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2499 | ||
2500 | Instead for now assume that the definition is not global, | |
2501 | Even if this is wrong, at least the linker will behave | |
2502 | in the same way that it used to do. */ | |
2503 | return FALSE; | |
2504 | ||
2505 | return TRUE; | |
2506 | } | |
2507 | ||
2508 | /* Search the symbol table of the archive element of the archive ABFD | |
2509 | whose archive map contains a mention of SYMDEF, and determine if | |
2510 | the symbol is defined in this element. */ | |
2511 | static bfd_boolean | |
2512 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2513 | { | |
2514 | Elf_Internal_Shdr * hdr; | |
2515 | bfd_size_type symcount; | |
2516 | bfd_size_type extsymcount; | |
2517 | bfd_size_type extsymoff; | |
2518 | Elf_Internal_Sym *isymbuf; | |
2519 | Elf_Internal_Sym *isym; | |
2520 | Elf_Internal_Sym *isymend; | |
2521 | bfd_boolean result; | |
2522 | ||
2523 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2524 | if (abfd == NULL) | |
2525 | return FALSE; | |
2526 | ||
2527 | if (! bfd_check_format (abfd, bfd_object)) | |
2528 | return FALSE; | |
2529 | ||
2530 | /* If we have already included the element containing this symbol in the | |
2531 | link then we do not need to include it again. Just claim that any symbol | |
2532 | it contains is not a definition, so that our caller will not decide to | |
2533 | (re)include this element. */ | |
2534 | if (abfd->archive_pass) | |
2535 | return FALSE; | |
2536 | ||
2537 | /* Select the appropriate symbol table. */ | |
2538 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2539 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2540 | else | |
2541 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2542 | ||
2543 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2544 | ||
2545 | /* The sh_info field of the symtab header tells us where the | |
2546 | external symbols start. We don't care about the local symbols. */ | |
2547 | if (elf_bad_symtab (abfd)) | |
2548 | { | |
2549 | extsymcount = symcount; | |
2550 | extsymoff = 0; | |
2551 | } | |
2552 | else | |
2553 | { | |
2554 | extsymcount = symcount - hdr->sh_info; | |
2555 | extsymoff = hdr->sh_info; | |
2556 | } | |
2557 | ||
2558 | if (extsymcount == 0) | |
2559 | return FALSE; | |
2560 | ||
2561 | /* Read in the symbol table. */ | |
2562 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2563 | NULL, NULL, NULL); | |
2564 | if (isymbuf == NULL) | |
2565 | return FALSE; | |
2566 | ||
2567 | /* Scan the symbol table looking for SYMDEF. */ | |
2568 | result = FALSE; | |
2569 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2570 | { | |
2571 | const char *name; | |
2572 | ||
2573 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
2574 | isym->st_name); | |
2575 | if (name == NULL) | |
2576 | break; | |
2577 | ||
2578 | if (strcmp (name, symdef->name) == 0) | |
2579 | { | |
2580 | result = is_global_data_symbol_definition (abfd, isym); | |
2581 | break; | |
2582 | } | |
2583 | } | |
2584 | ||
2585 | free (isymbuf); | |
2586 | ||
2587 | return result; | |
2588 | } | |
2589 | \f | |
5a580b3a AM |
2590 | /* Add an entry to the .dynamic table. */ |
2591 | ||
2592 | bfd_boolean | |
2593 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
2594 | bfd_vma tag, | |
2595 | bfd_vma val) | |
2596 | { | |
2597 | struct elf_link_hash_table *hash_table; | |
2598 | const struct elf_backend_data *bed; | |
2599 | asection *s; | |
2600 | bfd_size_type newsize; | |
2601 | bfd_byte *newcontents; | |
2602 | Elf_Internal_Dyn dyn; | |
2603 | ||
2604 | hash_table = elf_hash_table (info); | |
2605 | if (! is_elf_hash_table (hash_table)) | |
2606 | return FALSE; | |
2607 | ||
2608 | bed = get_elf_backend_data (hash_table->dynobj); | |
2609 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2610 | BFD_ASSERT (s != NULL); | |
2611 | ||
2612 | newsize = s->_raw_size + bed->s->sizeof_dyn; | |
2613 | newcontents = bfd_realloc (s->contents, newsize); | |
2614 | if (newcontents == NULL) | |
2615 | return FALSE; | |
2616 | ||
2617 | dyn.d_tag = tag; | |
2618 | dyn.d_un.d_val = val; | |
2619 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->_raw_size); | |
2620 | ||
2621 | s->_raw_size = newsize; | |
2622 | s->contents = newcontents; | |
2623 | ||
2624 | return TRUE; | |
2625 | } | |
2626 | ||
2627 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
2628 | otherwise just check whether one already exists. Returns -1 on error, | |
2629 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
2630 | ||
4ad4eba5 AM |
2631 | static int |
2632 | elf_add_dt_needed_tag (struct bfd_link_info *info, | |
2633 | const char *soname, | |
2634 | bfd_boolean do_it) | |
5a580b3a AM |
2635 | { |
2636 | struct elf_link_hash_table *hash_table; | |
2637 | bfd_size_type oldsize; | |
2638 | bfd_size_type strindex; | |
2639 | ||
2640 | hash_table = elf_hash_table (info); | |
2641 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
2642 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
2643 | if (strindex == (bfd_size_type) -1) | |
2644 | return -1; | |
2645 | ||
2646 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
2647 | { | |
2648 | asection *sdyn; | |
2649 | const struct elf_backend_data *bed; | |
2650 | bfd_byte *extdyn; | |
2651 | ||
2652 | bed = get_elf_backend_data (hash_table->dynobj); | |
2653 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2654 | BFD_ASSERT (sdyn != NULL); | |
2655 | ||
2656 | for (extdyn = sdyn->contents; | |
2657 | extdyn < sdyn->contents + sdyn->_raw_size; | |
2658 | extdyn += bed->s->sizeof_dyn) | |
2659 | { | |
2660 | Elf_Internal_Dyn dyn; | |
2661 | ||
2662 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); | |
2663 | if (dyn.d_tag == DT_NEEDED | |
2664 | && dyn.d_un.d_val == strindex) | |
2665 | { | |
2666 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2667 | return 1; | |
2668 | } | |
2669 | } | |
2670 | } | |
2671 | ||
2672 | if (do_it) | |
2673 | { | |
2674 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) | |
2675 | return -1; | |
2676 | } | |
2677 | else | |
2678 | /* We were just checking for existence of the tag. */ | |
2679 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2680 | ||
2681 | return 0; | |
2682 | } | |
2683 | ||
2684 | /* Sort symbol by value and section. */ | |
4ad4eba5 AM |
2685 | static int |
2686 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
2687 | { |
2688 | const struct elf_link_hash_entry *h1; | |
2689 | const struct elf_link_hash_entry *h2; | |
2690 | bfd_signed_vma vdiff; | |
2691 | ||
2692 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
2693 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
2694 | vdiff = h1->root.u.def.value - h2->root.u.def.value; | |
2695 | if (vdiff != 0) | |
2696 | return vdiff > 0 ? 1 : -1; | |
2697 | else | |
2698 | { | |
2699 | long sdiff = h1->root.u.def.section - h2->root.u.def.section; | |
2700 | if (sdiff != 0) | |
2701 | return sdiff > 0 ? 1 : -1; | |
2702 | } | |
2703 | return 0; | |
2704 | } | |
4ad4eba5 | 2705 | |
5a580b3a AM |
2706 | /* This function is used to adjust offsets into .dynstr for |
2707 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
2708 | ||
2709 | static bfd_boolean | |
2710 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
2711 | { | |
2712 | struct elf_strtab_hash *dynstr = data; | |
2713 | ||
2714 | if (h->root.type == bfd_link_hash_warning) | |
2715 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2716 | ||
2717 | if (h->dynindx != -1) | |
2718 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
2719 | return TRUE; | |
2720 | } | |
2721 | ||
2722 | /* Assign string offsets in .dynstr, update all structures referencing | |
2723 | them. */ | |
2724 | ||
4ad4eba5 AM |
2725 | static bfd_boolean |
2726 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
2727 | { |
2728 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
2729 | struct elf_link_local_dynamic_entry *entry; | |
2730 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
2731 | bfd *dynobj = hash_table->dynobj; | |
2732 | asection *sdyn; | |
2733 | bfd_size_type size; | |
2734 | const struct elf_backend_data *bed; | |
2735 | bfd_byte *extdyn; | |
2736 | ||
2737 | _bfd_elf_strtab_finalize (dynstr); | |
2738 | size = _bfd_elf_strtab_size (dynstr); | |
2739 | ||
2740 | bed = get_elf_backend_data (dynobj); | |
2741 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
2742 | BFD_ASSERT (sdyn != NULL); | |
2743 | ||
2744 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
2745 | for (extdyn = sdyn->contents; | |
2746 | extdyn < sdyn->contents + sdyn->_raw_size; | |
2747 | extdyn += bed->s->sizeof_dyn) | |
2748 | { | |
2749 | Elf_Internal_Dyn dyn; | |
2750 | ||
2751 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
2752 | switch (dyn.d_tag) | |
2753 | { | |
2754 | case DT_STRSZ: | |
2755 | dyn.d_un.d_val = size; | |
2756 | break; | |
2757 | case DT_NEEDED: | |
2758 | case DT_SONAME: | |
2759 | case DT_RPATH: | |
2760 | case DT_RUNPATH: | |
2761 | case DT_FILTER: | |
2762 | case DT_AUXILIARY: | |
2763 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
2764 | break; | |
2765 | default: | |
2766 | continue; | |
2767 | } | |
2768 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
2769 | } | |
2770 | ||
2771 | /* Now update local dynamic symbols. */ | |
2772 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
2773 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
2774 | entry->isym.st_name); | |
2775 | ||
2776 | /* And the rest of dynamic symbols. */ | |
2777 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
2778 | ||
2779 | /* Adjust version definitions. */ | |
2780 | if (elf_tdata (output_bfd)->cverdefs) | |
2781 | { | |
2782 | asection *s; | |
2783 | bfd_byte *p; | |
2784 | bfd_size_type i; | |
2785 | Elf_Internal_Verdef def; | |
2786 | Elf_Internal_Verdaux defaux; | |
2787 | ||
2788 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
2789 | p = s->contents; | |
2790 | do | |
2791 | { | |
2792 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
2793 | &def); | |
2794 | p += sizeof (Elf_External_Verdef); | |
2795 | for (i = 0; i < def.vd_cnt; ++i) | |
2796 | { | |
2797 | _bfd_elf_swap_verdaux_in (output_bfd, | |
2798 | (Elf_External_Verdaux *) p, &defaux); | |
2799 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
2800 | defaux.vda_name); | |
2801 | _bfd_elf_swap_verdaux_out (output_bfd, | |
2802 | &defaux, (Elf_External_Verdaux *) p); | |
2803 | p += sizeof (Elf_External_Verdaux); | |
2804 | } | |
2805 | } | |
2806 | while (def.vd_next); | |
2807 | } | |
2808 | ||
2809 | /* Adjust version references. */ | |
2810 | if (elf_tdata (output_bfd)->verref) | |
2811 | { | |
2812 | asection *s; | |
2813 | bfd_byte *p; | |
2814 | bfd_size_type i; | |
2815 | Elf_Internal_Verneed need; | |
2816 | Elf_Internal_Vernaux needaux; | |
2817 | ||
2818 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
2819 | p = s->contents; | |
2820 | do | |
2821 | { | |
2822 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
2823 | &need); | |
2824 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
2825 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
2826 | (Elf_External_Verneed *) p); | |
2827 | p += sizeof (Elf_External_Verneed); | |
2828 | for (i = 0; i < need.vn_cnt; ++i) | |
2829 | { | |
2830 | _bfd_elf_swap_vernaux_in (output_bfd, | |
2831 | (Elf_External_Vernaux *) p, &needaux); | |
2832 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
2833 | needaux.vna_name); | |
2834 | _bfd_elf_swap_vernaux_out (output_bfd, | |
2835 | &needaux, | |
2836 | (Elf_External_Vernaux *) p); | |
2837 | p += sizeof (Elf_External_Vernaux); | |
2838 | } | |
2839 | } | |
2840 | while (need.vn_next); | |
2841 | } | |
2842 | ||
2843 | return TRUE; | |
2844 | } | |
2845 | \f | |
4ad4eba5 AM |
2846 | /* Add symbols from an ELF object file to the linker hash table. */ |
2847 | ||
2848 | static bfd_boolean | |
2849 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
2850 | { | |
2851 | bfd_boolean (*add_symbol_hook) | |
555cd476 | 2852 | (bfd *, struct bfd_link_info *, Elf_Internal_Sym *, |
4ad4eba5 AM |
2853 | const char **, flagword *, asection **, bfd_vma *); |
2854 | bfd_boolean (*check_relocs) | |
2855 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
2856 | bfd_boolean collect; | |
2857 | Elf_Internal_Shdr *hdr; | |
2858 | bfd_size_type symcount; | |
2859 | bfd_size_type extsymcount; | |
2860 | bfd_size_type extsymoff; | |
2861 | struct elf_link_hash_entry **sym_hash; | |
2862 | bfd_boolean dynamic; | |
2863 | Elf_External_Versym *extversym = NULL; | |
2864 | Elf_External_Versym *ever; | |
2865 | struct elf_link_hash_entry *weaks; | |
2866 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
2867 | bfd_size_type nondeflt_vers_cnt = 0; | |
2868 | Elf_Internal_Sym *isymbuf = NULL; | |
2869 | Elf_Internal_Sym *isym; | |
2870 | Elf_Internal_Sym *isymend; | |
2871 | const struct elf_backend_data *bed; | |
2872 | bfd_boolean add_needed; | |
2873 | struct elf_link_hash_table * hash_table; | |
2874 | bfd_size_type amt; | |
2875 | ||
2876 | hash_table = elf_hash_table (info); | |
2877 | ||
2878 | bed = get_elf_backend_data (abfd); | |
2879 | add_symbol_hook = bed->elf_add_symbol_hook; | |
2880 | collect = bed->collect; | |
2881 | ||
2882 | if ((abfd->flags & DYNAMIC) == 0) | |
2883 | dynamic = FALSE; | |
2884 | else | |
2885 | { | |
2886 | dynamic = TRUE; | |
2887 | ||
2888 | /* You can't use -r against a dynamic object. Also, there's no | |
2889 | hope of using a dynamic object which does not exactly match | |
2890 | the format of the output file. */ | |
2891 | if (info->relocatable | |
2892 | || !is_elf_hash_table (hash_table) | |
2893 | || hash_table->root.creator != abfd->xvec) | |
2894 | { | |
2895 | bfd_set_error (bfd_error_invalid_operation); | |
2896 | goto error_return; | |
2897 | } | |
2898 | } | |
2899 | ||
2900 | /* As a GNU extension, any input sections which are named | |
2901 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
2902 | symbol. This differs from .gnu.warning sections, which generate | |
2903 | warnings when they are included in an output file. */ | |
2904 | if (info->executable) | |
2905 | { | |
2906 | asection *s; | |
2907 | ||
2908 | for (s = abfd->sections; s != NULL; s = s->next) | |
2909 | { | |
2910 | const char *name; | |
2911 | ||
2912 | name = bfd_get_section_name (abfd, s); | |
2913 | if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0) | |
2914 | { | |
2915 | char *msg; | |
2916 | bfd_size_type sz; | |
2917 | bfd_size_type prefix_len; | |
2918 | const char * gnu_warning_prefix = _("warning: "); | |
2919 | ||
2920 | name += sizeof ".gnu.warning." - 1; | |
2921 | ||
2922 | /* If this is a shared object, then look up the symbol | |
2923 | in the hash table. If it is there, and it is already | |
2924 | been defined, then we will not be using the entry | |
2925 | from this shared object, so we don't need to warn. | |
2926 | FIXME: If we see the definition in a regular object | |
2927 | later on, we will warn, but we shouldn't. The only | |
2928 | fix is to keep track of what warnings we are supposed | |
2929 | to emit, and then handle them all at the end of the | |
2930 | link. */ | |
2931 | if (dynamic) | |
2932 | { | |
2933 | struct elf_link_hash_entry *h; | |
2934 | ||
2935 | h = elf_link_hash_lookup (hash_table, name, | |
2936 | FALSE, FALSE, TRUE); | |
2937 | ||
2938 | /* FIXME: What about bfd_link_hash_common? */ | |
2939 | if (h != NULL | |
2940 | && (h->root.type == bfd_link_hash_defined | |
2941 | || h->root.type == bfd_link_hash_defweak)) | |
2942 | { | |
2943 | /* We don't want to issue this warning. Clobber | |
2944 | the section size so that the warning does not | |
2945 | get copied into the output file. */ | |
2946 | s->_raw_size = 0; | |
2947 | continue; | |
2948 | } | |
2949 | } | |
2950 | ||
2951 | sz = bfd_section_size (abfd, s); | |
2952 | prefix_len = strlen (gnu_warning_prefix); | |
2953 | msg = bfd_alloc (abfd, prefix_len + sz + 1); | |
2954 | if (msg == NULL) | |
2955 | goto error_return; | |
2956 | ||
2957 | strcpy (msg, gnu_warning_prefix); | |
2958 | if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz)) | |
2959 | goto error_return; | |
2960 | ||
2961 | msg[prefix_len + sz] = '\0'; | |
2962 | ||
2963 | if (! (_bfd_generic_link_add_one_symbol | |
2964 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
2965 | FALSE, collect, NULL))) | |
2966 | goto error_return; | |
2967 | ||
2968 | if (! info->relocatable) | |
2969 | { | |
2970 | /* Clobber the section size so that the warning does | |
2971 | not get copied into the output file. */ | |
2972 | s->_raw_size = 0; | |
2973 | } | |
2974 | } | |
2975 | } | |
2976 | } | |
2977 | ||
2978 | add_needed = TRUE; | |
2979 | if (! dynamic) | |
2980 | { | |
2981 | /* If we are creating a shared library, create all the dynamic | |
2982 | sections immediately. We need to attach them to something, | |
2983 | so we attach them to this BFD, provided it is the right | |
2984 | format. FIXME: If there are no input BFD's of the same | |
2985 | format as the output, we can't make a shared library. */ | |
2986 | if (info->shared | |
2987 | && is_elf_hash_table (hash_table) | |
2988 | && hash_table->root.creator == abfd->xvec | |
2989 | && ! hash_table->dynamic_sections_created) | |
2990 | { | |
2991 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
2992 | goto error_return; | |
2993 | } | |
2994 | } | |
2995 | else if (!is_elf_hash_table (hash_table)) | |
2996 | goto error_return; | |
2997 | else | |
2998 | { | |
2999 | asection *s; | |
3000 | const char *soname = NULL; | |
3001 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; | |
3002 | int ret; | |
3003 | ||
3004 | /* ld --just-symbols and dynamic objects don't mix very well. | |
3005 | Test for --just-symbols by looking at info set up by | |
3006 | _bfd_elf_link_just_syms. */ | |
3007 | if ((s = abfd->sections) != NULL | |
3008 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
3009 | goto error_return; | |
3010 | ||
3011 | /* If this dynamic lib was specified on the command line with | |
3012 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3013 | tag unless the lib is actually used. Similary for libs brought | |
3014 | in by another lib's DT_NEEDED. */ | |
3015 | add_needed = elf_dyn_lib_class (abfd) == DYN_NORMAL; | |
3016 | ||
3017 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3018 | if (s != NULL) | |
3019 | { | |
3020 | bfd_byte *dynbuf; | |
3021 | bfd_byte *extdyn; | |
3022 | int elfsec; | |
3023 | unsigned long shlink; | |
3024 | ||
3025 | dynbuf = bfd_malloc (s->_raw_size); | |
3026 | if (dynbuf == NULL) | |
3027 | goto error_return; | |
3028 | ||
3029 | if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size)) | |
3030 | goto error_free_dyn; | |
3031 | ||
3032 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
3033 | if (elfsec == -1) | |
3034 | goto error_free_dyn; | |
3035 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3036 | ||
3037 | for (extdyn = dynbuf; | |
3038 | extdyn < dynbuf + s->_raw_size; | |
3039 | extdyn += bed->s->sizeof_dyn) | |
3040 | { | |
3041 | Elf_Internal_Dyn dyn; | |
3042 | ||
3043 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3044 | if (dyn.d_tag == DT_SONAME) | |
3045 | { | |
3046 | unsigned int tagv = dyn.d_un.d_val; | |
3047 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3048 | if (soname == NULL) | |
3049 | goto error_free_dyn; | |
3050 | } | |
3051 | if (dyn.d_tag == DT_NEEDED) | |
3052 | { | |
3053 | struct bfd_link_needed_list *n, **pn; | |
3054 | char *fnm, *anm; | |
3055 | unsigned int tagv = dyn.d_un.d_val; | |
3056 | ||
3057 | amt = sizeof (struct bfd_link_needed_list); | |
3058 | n = bfd_alloc (abfd, amt); | |
3059 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3060 | if (n == NULL || fnm == NULL) | |
3061 | goto error_free_dyn; | |
3062 | amt = strlen (fnm) + 1; | |
3063 | anm = bfd_alloc (abfd, amt); | |
3064 | if (anm == NULL) | |
3065 | goto error_free_dyn; | |
3066 | memcpy (anm, fnm, amt); | |
3067 | n->name = anm; | |
3068 | n->by = abfd; | |
3069 | n->next = NULL; | |
3070 | for (pn = & hash_table->needed; | |
3071 | *pn != NULL; | |
3072 | pn = &(*pn)->next) | |
3073 | ; | |
3074 | *pn = n; | |
3075 | } | |
3076 | if (dyn.d_tag == DT_RUNPATH) | |
3077 | { | |
3078 | struct bfd_link_needed_list *n, **pn; | |
3079 | char *fnm, *anm; | |
3080 | unsigned int tagv = dyn.d_un.d_val; | |
3081 | ||
3082 | amt = sizeof (struct bfd_link_needed_list); | |
3083 | n = bfd_alloc (abfd, amt); | |
3084 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3085 | if (n == NULL || fnm == NULL) | |
3086 | goto error_free_dyn; | |
3087 | amt = strlen (fnm) + 1; | |
3088 | anm = bfd_alloc (abfd, amt); | |
3089 | if (anm == NULL) | |
3090 | goto error_free_dyn; | |
3091 | memcpy (anm, fnm, amt); | |
3092 | n->name = anm; | |
3093 | n->by = abfd; | |
3094 | n->next = NULL; | |
3095 | for (pn = & runpath; | |
3096 | *pn != NULL; | |
3097 | pn = &(*pn)->next) | |
3098 | ; | |
3099 | *pn = n; | |
3100 | } | |
3101 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3102 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3103 | { | |
3104 | struct bfd_link_needed_list *n, **pn; | |
3105 | char *fnm, *anm; | |
3106 | unsigned int tagv = dyn.d_un.d_val; | |
3107 | ||
3108 | amt = sizeof (struct bfd_link_needed_list); | |
3109 | n = bfd_alloc (abfd, amt); | |
3110 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3111 | if (n == NULL || fnm == NULL) | |
3112 | goto error_free_dyn; | |
3113 | amt = strlen (fnm) + 1; | |
3114 | anm = bfd_alloc (abfd, amt); | |
3115 | if (anm == NULL) | |
3116 | { | |
3117 | error_free_dyn: | |
3118 | free (dynbuf); | |
3119 | goto error_return; | |
3120 | } | |
3121 | memcpy (anm, fnm, amt); | |
3122 | n->name = anm; | |
3123 | n->by = abfd; | |
3124 | n->next = NULL; | |
3125 | for (pn = & rpath; | |
3126 | *pn != NULL; | |
3127 | pn = &(*pn)->next) | |
3128 | ; | |
3129 | *pn = n; | |
3130 | } | |
3131 | } | |
3132 | ||
3133 | free (dynbuf); | |
3134 | } | |
3135 | ||
3136 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3137 | frees all more recently bfd_alloc'd blocks as well. */ | |
3138 | if (runpath) | |
3139 | rpath = runpath; | |
3140 | ||
3141 | if (rpath) | |
3142 | { | |
3143 | struct bfd_link_needed_list **pn; | |
3144 | for (pn = & hash_table->runpath; | |
3145 | *pn != NULL; | |
3146 | pn = &(*pn)->next) | |
3147 | ; | |
3148 | *pn = rpath; | |
3149 | } | |
3150 | ||
3151 | /* We do not want to include any of the sections in a dynamic | |
3152 | object in the output file. We hack by simply clobbering the | |
3153 | list of sections in the BFD. This could be handled more | |
3154 | cleanly by, say, a new section flag; the existing | |
3155 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3156 | still implies that the section takes up space in the output | |
3157 | file. */ | |
3158 | bfd_section_list_clear (abfd); | |
3159 | ||
3160 | /* If this is the first dynamic object found in the link, create | |
3161 | the special sections required for dynamic linking. */ | |
3162 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3163 | goto error_return; | |
3164 | ||
3165 | /* Find the name to use in a DT_NEEDED entry that refers to this | |
3166 | object. If the object has a DT_SONAME entry, we use it. | |
3167 | Otherwise, if the generic linker stuck something in | |
3168 | elf_dt_name, we use that. Otherwise, we just use the file | |
3169 | name. */ | |
3170 | if (soname == NULL || *soname == '\0') | |
3171 | { | |
3172 | soname = elf_dt_name (abfd); | |
3173 | if (soname == NULL || *soname == '\0') | |
3174 | soname = bfd_get_filename (abfd); | |
3175 | } | |
3176 | ||
3177 | /* Save the SONAME because sometimes the linker emulation code | |
3178 | will need to know it. */ | |
3179 | elf_dt_name (abfd) = soname; | |
3180 | ||
3181 | ret = elf_add_dt_needed_tag (info, soname, add_needed); | |
3182 | if (ret < 0) | |
3183 | goto error_return; | |
3184 | ||
3185 | /* If we have already included this dynamic object in the | |
3186 | link, just ignore it. There is no reason to include a | |
3187 | particular dynamic object more than once. */ | |
3188 | if (ret > 0) | |
3189 | return TRUE; | |
3190 | } | |
3191 | ||
3192 | /* If this is a dynamic object, we always link against the .dynsym | |
3193 | symbol table, not the .symtab symbol table. The dynamic linker | |
3194 | will only see the .dynsym symbol table, so there is no reason to | |
3195 | look at .symtab for a dynamic object. */ | |
3196 | ||
3197 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3198 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3199 | else | |
3200 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3201 | ||
3202 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3203 | ||
3204 | /* The sh_info field of the symtab header tells us where the | |
3205 | external symbols start. We don't care about the local symbols at | |
3206 | this point. */ | |
3207 | if (elf_bad_symtab (abfd)) | |
3208 | { | |
3209 | extsymcount = symcount; | |
3210 | extsymoff = 0; | |
3211 | } | |
3212 | else | |
3213 | { | |
3214 | extsymcount = symcount - hdr->sh_info; | |
3215 | extsymoff = hdr->sh_info; | |
3216 | } | |
3217 | ||
3218 | sym_hash = NULL; | |
3219 | if (extsymcount != 0) | |
3220 | { | |
3221 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3222 | NULL, NULL, NULL); | |
3223 | if (isymbuf == NULL) | |
3224 | goto error_return; | |
3225 | ||
3226 | /* We store a pointer to the hash table entry for each external | |
3227 | symbol. */ | |
3228 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3229 | sym_hash = bfd_alloc (abfd, amt); | |
3230 | if (sym_hash == NULL) | |
3231 | goto error_free_sym; | |
3232 | elf_sym_hashes (abfd) = sym_hash; | |
3233 | } | |
3234 | ||
3235 | if (dynamic) | |
3236 | { | |
3237 | /* Read in any version definitions. */ | |
3238 | if (! _bfd_elf_slurp_version_tables (abfd)) | |
3239 | goto error_free_sym; | |
3240 | ||
3241 | /* Read in the symbol versions, but don't bother to convert them | |
3242 | to internal format. */ | |
3243 | if (elf_dynversym (abfd) != 0) | |
3244 | { | |
3245 | Elf_Internal_Shdr *versymhdr; | |
3246 | ||
3247 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
3248 | extversym = bfd_malloc (versymhdr->sh_size); | |
3249 | if (extversym == NULL) | |
3250 | goto error_free_sym; | |
3251 | amt = versymhdr->sh_size; | |
3252 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3253 | || bfd_bread (extversym, amt, abfd) != amt) | |
3254 | goto error_free_vers; | |
3255 | } | |
3256 | } | |
3257 | ||
3258 | weaks = NULL; | |
3259 | ||
3260 | ever = extversym != NULL ? extversym + extsymoff : NULL; | |
3261 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3262 | isym < isymend; | |
3263 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3264 | { | |
3265 | int bind; | |
3266 | bfd_vma value; | |
3267 | asection *sec; | |
3268 | flagword flags; | |
3269 | const char *name; | |
3270 | struct elf_link_hash_entry *h; | |
3271 | bfd_boolean definition; | |
3272 | bfd_boolean size_change_ok; | |
3273 | bfd_boolean type_change_ok; | |
3274 | bfd_boolean new_weakdef; | |
3275 | bfd_boolean override; | |
3276 | unsigned int old_alignment; | |
3277 | bfd *old_bfd; | |
3278 | ||
3279 | override = FALSE; | |
3280 | ||
3281 | flags = BSF_NO_FLAGS; | |
3282 | sec = NULL; | |
3283 | value = isym->st_value; | |
3284 | *sym_hash = NULL; | |
3285 | ||
3286 | bind = ELF_ST_BIND (isym->st_info); | |
3287 | if (bind == STB_LOCAL) | |
3288 | { | |
3289 | /* This should be impossible, since ELF requires that all | |
3290 | global symbols follow all local symbols, and that sh_info | |
3291 | point to the first global symbol. Unfortunately, Irix 5 | |
3292 | screws this up. */ | |
3293 | continue; | |
3294 | } | |
3295 | else if (bind == STB_GLOBAL) | |
3296 | { | |
3297 | if (isym->st_shndx != SHN_UNDEF | |
3298 | && isym->st_shndx != SHN_COMMON) | |
3299 | flags = BSF_GLOBAL; | |
3300 | } | |
3301 | else if (bind == STB_WEAK) | |
3302 | flags = BSF_WEAK; | |
3303 | else | |
3304 | { | |
3305 | /* Leave it up to the processor backend. */ | |
3306 | } | |
3307 | ||
3308 | if (isym->st_shndx == SHN_UNDEF) | |
3309 | sec = bfd_und_section_ptr; | |
3310 | else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
3311 | { | |
3312 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3313 | if (sec == NULL) | |
3314 | sec = bfd_abs_section_ptr; | |
3315 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) | |
3316 | value -= sec->vma; | |
3317 | } | |
3318 | else if (isym->st_shndx == SHN_ABS) | |
3319 | sec = bfd_abs_section_ptr; | |
3320 | else if (isym->st_shndx == SHN_COMMON) | |
3321 | { | |
3322 | sec = bfd_com_section_ptr; | |
3323 | /* What ELF calls the size we call the value. What ELF | |
3324 | calls the value we call the alignment. */ | |
3325 | value = isym->st_size; | |
3326 | } | |
3327 | else | |
3328 | { | |
3329 | /* Leave it up to the processor backend. */ | |
3330 | } | |
3331 | ||
3332 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3333 | isym->st_name); | |
3334 | if (name == NULL) | |
3335 | goto error_free_vers; | |
3336 | ||
3337 | if (isym->st_shndx == SHN_COMMON | |
3338 | && ELF_ST_TYPE (isym->st_info) == STT_TLS) | |
3339 | { | |
3340 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3341 | ||
3342 | if (tcomm == NULL) | |
3343 | { | |
3344 | tcomm = bfd_make_section (abfd, ".tcommon"); | |
3345 | if (tcomm == NULL | |
3346 | || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC | |
3347 | | SEC_IS_COMMON | |
3348 | | SEC_LINKER_CREATED | |
3349 | | SEC_THREAD_LOCAL))) | |
3350 | goto error_free_vers; | |
3351 | } | |
3352 | sec = tcomm; | |
3353 | } | |
3354 | else if (add_symbol_hook) | |
3355 | { | |
3356 | if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec, | |
3357 | &value)) | |
3358 | goto error_free_vers; | |
3359 | ||
3360 | /* The hook function sets the name to NULL if this symbol | |
3361 | should be skipped for some reason. */ | |
3362 | if (name == NULL) | |
3363 | continue; | |
3364 | } | |
3365 | ||
3366 | /* Sanity check that all possibilities were handled. */ | |
3367 | if (sec == NULL) | |
3368 | { | |
3369 | bfd_set_error (bfd_error_bad_value); | |
3370 | goto error_free_vers; | |
3371 | } | |
3372 | ||
3373 | if (bfd_is_und_section (sec) | |
3374 | || bfd_is_com_section (sec)) | |
3375 | definition = FALSE; | |
3376 | else | |
3377 | definition = TRUE; | |
3378 | ||
3379 | size_change_ok = FALSE; | |
3380 | type_change_ok = get_elf_backend_data (abfd)->type_change_ok; | |
3381 | old_alignment = 0; | |
3382 | old_bfd = NULL; | |
3383 | ||
3384 | if (is_elf_hash_table (hash_table)) | |
3385 | { | |
3386 | Elf_Internal_Versym iver; | |
3387 | unsigned int vernum = 0; | |
3388 | bfd_boolean skip; | |
3389 | ||
3390 | if (ever != NULL) | |
3391 | { | |
3392 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
3393 | vernum = iver.vs_vers & VERSYM_VERSION; | |
3394 | ||
3395 | /* If this is a hidden symbol, or if it is not version | |
3396 | 1, we append the version name to the symbol name. | |
3397 | However, we do not modify a non-hidden absolute | |
3398 | symbol, because it might be the version symbol | |
3399 | itself. FIXME: What if it isn't? */ | |
3400 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 | |
3401 | || (vernum > 1 && ! bfd_is_abs_section (sec))) | |
3402 | { | |
3403 | const char *verstr; | |
3404 | size_t namelen, verlen, newlen; | |
3405 | char *newname, *p; | |
3406 | ||
3407 | if (isym->st_shndx != SHN_UNDEF) | |
3408 | { | |
3409 | if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info) | |
3410 | { | |
3411 | (*_bfd_error_handler) | |
3412 | (_("%s: %s: invalid version %u (max %d)"), | |
3413 | bfd_archive_filename (abfd), name, vernum, | |
3414 | elf_tdata (abfd)->dynverdef_hdr.sh_info); | |
3415 | bfd_set_error (bfd_error_bad_value); | |
3416 | goto error_free_vers; | |
3417 | } | |
3418 | else if (vernum > 1) | |
3419 | verstr = | |
3420 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
3421 | else | |
3422 | verstr = ""; | |
3423 | } | |
3424 | else | |
3425 | { | |
3426 | /* We cannot simply test for the number of | |
3427 | entries in the VERNEED section since the | |
3428 | numbers for the needed versions do not start | |
3429 | at 0. */ | |
3430 | Elf_Internal_Verneed *t; | |
3431 | ||
3432 | verstr = NULL; | |
3433 | for (t = elf_tdata (abfd)->verref; | |
3434 | t != NULL; | |
3435 | t = t->vn_nextref) | |
3436 | { | |
3437 | Elf_Internal_Vernaux *a; | |
3438 | ||
3439 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
3440 | { | |
3441 | if (a->vna_other == vernum) | |
3442 | { | |
3443 | verstr = a->vna_nodename; | |
3444 | break; | |
3445 | } | |
3446 | } | |
3447 | if (a != NULL) | |
3448 | break; | |
3449 | } | |
3450 | if (verstr == NULL) | |
3451 | { | |
3452 | (*_bfd_error_handler) | |
3453 | (_("%s: %s: invalid needed version %d"), | |
3454 | bfd_archive_filename (abfd), name, vernum); | |
3455 | bfd_set_error (bfd_error_bad_value); | |
3456 | goto error_free_vers; | |
3457 | } | |
3458 | } | |
3459 | ||
3460 | namelen = strlen (name); | |
3461 | verlen = strlen (verstr); | |
3462 | newlen = namelen + verlen + 2; | |
3463 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3464 | && isym->st_shndx != SHN_UNDEF) | |
3465 | ++newlen; | |
3466 | ||
3467 | newname = bfd_alloc (abfd, newlen); | |
3468 | if (newname == NULL) | |
3469 | goto error_free_vers; | |
3470 | memcpy (newname, name, namelen); | |
3471 | p = newname + namelen; | |
3472 | *p++ = ELF_VER_CHR; | |
3473 | /* If this is a defined non-hidden version symbol, | |
3474 | we add another @ to the name. This indicates the | |
3475 | default version of the symbol. */ | |
3476 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3477 | && isym->st_shndx != SHN_UNDEF) | |
3478 | *p++ = ELF_VER_CHR; | |
3479 | memcpy (p, verstr, verlen + 1); | |
3480 | ||
3481 | name = newname; | |
3482 | } | |
3483 | } | |
3484 | ||
3485 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, | |
3486 | sym_hash, &skip, &override, | |
3487 | &type_change_ok, &size_change_ok)) | |
3488 | goto error_free_vers; | |
3489 | ||
3490 | if (skip) | |
3491 | continue; | |
3492 | ||
3493 | if (override) | |
3494 | definition = FALSE; | |
3495 | ||
3496 | h = *sym_hash; | |
3497 | while (h->root.type == bfd_link_hash_indirect | |
3498 | || h->root.type == bfd_link_hash_warning) | |
3499 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3500 | ||
3501 | /* Remember the old alignment if this is a common symbol, so | |
3502 | that we don't reduce the alignment later on. We can't | |
3503 | check later, because _bfd_generic_link_add_one_symbol | |
3504 | will set a default for the alignment which we want to | |
3505 | override. We also remember the old bfd where the existing | |
3506 | definition comes from. */ | |
3507 | switch (h->root.type) | |
3508 | { | |
3509 | default: | |
3510 | break; | |
3511 | ||
3512 | case bfd_link_hash_defined: | |
3513 | case bfd_link_hash_defweak: | |
3514 | old_bfd = h->root.u.def.section->owner; | |
3515 | break; | |
3516 | ||
3517 | case bfd_link_hash_common: | |
3518 | old_bfd = h->root.u.c.p->section->owner; | |
3519 | old_alignment = h->root.u.c.p->alignment_power; | |
3520 | break; | |
3521 | } | |
3522 | ||
3523 | if (elf_tdata (abfd)->verdef != NULL | |
3524 | && ! override | |
3525 | && vernum > 1 | |
3526 | && definition) | |
3527 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
3528 | } | |
3529 | ||
3530 | if (! (_bfd_generic_link_add_one_symbol | |
3531 | (info, abfd, name, flags, sec, value, NULL, FALSE, collect, | |
3532 | (struct bfd_link_hash_entry **) sym_hash))) | |
3533 | goto error_free_vers; | |
3534 | ||
3535 | h = *sym_hash; | |
3536 | while (h->root.type == bfd_link_hash_indirect | |
3537 | || h->root.type == bfd_link_hash_warning) | |
3538 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3539 | *sym_hash = h; | |
3540 | ||
3541 | new_weakdef = FALSE; | |
3542 | if (dynamic | |
3543 | && definition | |
3544 | && (flags & BSF_WEAK) != 0 | |
3545 | && ELF_ST_TYPE (isym->st_info) != STT_FUNC | |
3546 | && is_elf_hash_table (hash_table) | |
3547 | && h->weakdef == NULL) | |
3548 | { | |
3549 | /* Keep a list of all weak defined non function symbols from | |
3550 | a dynamic object, using the weakdef field. Later in this | |
3551 | function we will set the weakdef field to the correct | |
3552 | value. We only put non-function symbols from dynamic | |
3553 | objects on this list, because that happens to be the only | |
3554 | time we need to know the normal symbol corresponding to a | |
3555 | weak symbol, and the information is time consuming to | |
3556 | figure out. If the weakdef field is not already NULL, | |
3557 | then this symbol was already defined by some previous | |
3558 | dynamic object, and we will be using that previous | |
3559 | definition anyhow. */ | |
3560 | ||
3561 | h->weakdef = weaks; | |
3562 | weaks = h; | |
3563 | new_weakdef = TRUE; | |
3564 | } | |
3565 | ||
3566 | /* Set the alignment of a common symbol. */ | |
3567 | if (isym->st_shndx == SHN_COMMON | |
3568 | && h->root.type == bfd_link_hash_common) | |
3569 | { | |
3570 | unsigned int align; | |
3571 | ||
3572 | align = bfd_log2 (isym->st_value); | |
3573 | if (align > old_alignment | |
3574 | /* Permit an alignment power of zero if an alignment of one | |
3575 | is specified and no other alignments have been specified. */ | |
3576 | || (isym->st_value == 1 && old_alignment == 0)) | |
3577 | h->root.u.c.p->alignment_power = align; | |
3578 | else | |
3579 | h->root.u.c.p->alignment_power = old_alignment; | |
3580 | } | |
3581 | ||
3582 | if (is_elf_hash_table (hash_table)) | |
3583 | { | |
3584 | int old_flags; | |
3585 | bfd_boolean dynsym; | |
3586 | int new_flag; | |
3587 | ||
3588 | /* Check the alignment when a common symbol is involved. This | |
3589 | can change when a common symbol is overridden by a normal | |
3590 | definition or a common symbol is ignored due to the old | |
3591 | normal definition. We need to make sure the maximum | |
3592 | alignment is maintained. */ | |
3593 | if ((old_alignment || isym->st_shndx == SHN_COMMON) | |
3594 | && h->root.type != bfd_link_hash_common) | |
3595 | { | |
3596 | unsigned int common_align; | |
3597 | unsigned int normal_align; | |
3598 | unsigned int symbol_align; | |
3599 | bfd *normal_bfd; | |
3600 | bfd *common_bfd; | |
3601 | ||
3602 | symbol_align = ffs (h->root.u.def.value) - 1; | |
3603 | if (h->root.u.def.section->owner != NULL | |
3604 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
3605 | { | |
3606 | normal_align = h->root.u.def.section->alignment_power; | |
3607 | if (normal_align > symbol_align) | |
3608 | normal_align = symbol_align; | |
3609 | } | |
3610 | else | |
3611 | normal_align = symbol_align; | |
3612 | ||
3613 | if (old_alignment) | |
3614 | { | |
3615 | common_align = old_alignment; | |
3616 | common_bfd = old_bfd; | |
3617 | normal_bfd = abfd; | |
3618 | } | |
3619 | else | |
3620 | { | |
3621 | common_align = bfd_log2 (isym->st_value); | |
3622 | common_bfd = abfd; | |
3623 | normal_bfd = old_bfd; | |
3624 | } | |
3625 | ||
3626 | if (normal_align < common_align) | |
3627 | (*_bfd_error_handler) | |
3628 | (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"), | |
3629 | 1 << normal_align, | |
3630 | name, | |
3631 | bfd_archive_filename (normal_bfd), | |
3632 | 1 << common_align, | |
3633 | bfd_archive_filename (common_bfd)); | |
3634 | } | |
3635 | ||
3636 | /* Remember the symbol size and type. */ | |
3637 | if (isym->st_size != 0 | |
3638 | && (definition || h->size == 0)) | |
3639 | { | |
3640 | if (h->size != 0 && h->size != isym->st_size && ! size_change_ok) | |
3641 | (*_bfd_error_handler) | |
3642 | (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"), | |
3643 | name, (unsigned long) h->size, | |
3644 | bfd_archive_filename (old_bfd), | |
3645 | (unsigned long) isym->st_size, | |
3646 | bfd_archive_filename (abfd)); | |
3647 | ||
3648 | h->size = isym->st_size; | |
3649 | } | |
3650 | ||
3651 | /* If this is a common symbol, then we always want H->SIZE | |
3652 | to be the size of the common symbol. The code just above | |
3653 | won't fix the size if a common symbol becomes larger. We | |
3654 | don't warn about a size change here, because that is | |
3655 | covered by --warn-common. */ | |
3656 | if (h->root.type == bfd_link_hash_common) | |
3657 | h->size = h->root.u.c.size; | |
3658 | ||
3659 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
3660 | && (definition || h->type == STT_NOTYPE)) | |
3661 | { | |
3662 | if (h->type != STT_NOTYPE | |
3663 | && h->type != ELF_ST_TYPE (isym->st_info) | |
3664 | && ! type_change_ok) | |
3665 | (*_bfd_error_handler) | |
3666 | (_("Warning: type of symbol `%s' changed from %d to %d in %s"), | |
3667 | name, h->type, ELF_ST_TYPE (isym->st_info), | |
3668 | bfd_archive_filename (abfd)); | |
3669 | ||
3670 | h->type = ELF_ST_TYPE (isym->st_info); | |
3671 | } | |
3672 | ||
3673 | /* If st_other has a processor-specific meaning, specific | |
3674 | code might be needed here. We never merge the visibility | |
3675 | attribute with the one from a dynamic object. */ | |
3676 | if (bed->elf_backend_merge_symbol_attribute) | |
3677 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
3678 | dynamic); | |
3679 | ||
3680 | if (isym->st_other != 0 && !dynamic) | |
3681 | { | |
3682 | unsigned char hvis, symvis, other, nvis; | |
3683 | ||
3684 | /* Take the balance of OTHER from the definition. */ | |
3685 | other = (definition ? isym->st_other : h->other); | |
3686 | other &= ~ ELF_ST_VISIBILITY (-1); | |
3687 | ||
3688 | /* Combine visibilities, using the most constraining one. */ | |
3689 | hvis = ELF_ST_VISIBILITY (h->other); | |
3690 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
3691 | if (! hvis) | |
3692 | nvis = symvis; | |
3693 | else if (! symvis) | |
3694 | nvis = hvis; | |
3695 | else | |
3696 | nvis = hvis < symvis ? hvis : symvis; | |
3697 | ||
3698 | h->other = other | nvis; | |
3699 | } | |
3700 | ||
3701 | /* Set a flag in the hash table entry indicating the type of | |
3702 | reference or definition we just found. Keep a count of | |
3703 | the number of dynamic symbols we find. A dynamic symbol | |
3704 | is one which is referenced or defined by both a regular | |
3705 | object and a shared object. */ | |
3706 | old_flags = h->elf_link_hash_flags; | |
3707 | dynsym = FALSE; | |
3708 | if (! dynamic) | |
3709 | { | |
3710 | if (! definition) | |
3711 | { | |
3712 | new_flag = ELF_LINK_HASH_REF_REGULAR; | |
3713 | if (bind != STB_WEAK) | |
3714 | new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK; | |
3715 | } | |
3716 | else | |
3717 | new_flag = ELF_LINK_HASH_DEF_REGULAR; | |
3718 | if (! info->executable | |
3719 | || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC | |
3720 | | ELF_LINK_HASH_REF_DYNAMIC)) != 0) | |
3721 | dynsym = TRUE; | |
3722 | } | |
3723 | else | |
3724 | { | |
3725 | if (! definition) | |
3726 | new_flag = ELF_LINK_HASH_REF_DYNAMIC; | |
3727 | else | |
3728 | new_flag = ELF_LINK_HASH_DEF_DYNAMIC; | |
3729 | if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR | |
3730 | | ELF_LINK_HASH_REF_REGULAR)) != 0 | |
3731 | || (h->weakdef != NULL | |
3732 | && ! new_weakdef | |
3733 | && h->weakdef->dynindx != -1)) | |
3734 | dynsym = TRUE; | |
3735 | } | |
3736 | ||
3737 | h->elf_link_hash_flags |= new_flag; | |
3738 | ||
3739 | /* Check to see if we need to add an indirect symbol for | |
3740 | the default name. */ | |
3741 | if (definition || h->root.type == bfd_link_hash_common) | |
3742 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
3743 | &sec, &value, &dynsym, | |
3744 | override)) | |
3745 | goto error_free_vers; | |
3746 | ||
3747 | if (definition && !dynamic) | |
3748 | { | |
3749 | char *p = strchr (name, ELF_VER_CHR); | |
3750 | if (p != NULL && p[1] != ELF_VER_CHR) | |
3751 | { | |
3752 | /* Queue non-default versions so that .symver x, x@FOO | |
3753 | aliases can be checked. */ | |
3754 | if (! nondeflt_vers) | |
3755 | { | |
3756 | amt = (isymend - isym + 1) | |
3757 | * sizeof (struct elf_link_hash_entry *); | |
3758 | nondeflt_vers = bfd_malloc (amt); | |
3759 | } | |
3760 | nondeflt_vers [nondeflt_vers_cnt++] = h; | |
3761 | } | |
3762 | } | |
3763 | ||
3764 | if (dynsym && h->dynindx == -1) | |
3765 | { | |
c152c796 | 3766 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 AM |
3767 | goto error_free_vers; |
3768 | if (h->weakdef != NULL | |
3769 | && ! new_weakdef | |
3770 | && h->weakdef->dynindx == -1) | |
3771 | { | |
c152c796 | 3772 | if (! bfd_elf_link_record_dynamic_symbol (info, h->weakdef)) |
4ad4eba5 AM |
3773 | goto error_free_vers; |
3774 | } | |
3775 | } | |
3776 | else if (dynsym && h->dynindx != -1) | |
3777 | /* If the symbol already has a dynamic index, but | |
3778 | visibility says it should not be visible, turn it into | |
3779 | a local symbol. */ | |
3780 | switch (ELF_ST_VISIBILITY (h->other)) | |
3781 | { | |
3782 | case STV_INTERNAL: | |
3783 | case STV_HIDDEN: | |
3784 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
3785 | dynsym = FALSE; | |
3786 | break; | |
3787 | } | |
3788 | ||
3789 | if (!add_needed | |
3790 | && definition | |
3791 | && dynsym | |
3792 | && (h->elf_link_hash_flags | |
3793 | & ELF_LINK_HASH_REF_REGULAR) != 0) | |
3794 | { | |
3795 | int ret; | |
3796 | const char *soname = elf_dt_name (abfd); | |
3797 | ||
3798 | /* A symbol from a library loaded via DT_NEEDED of some | |
3799 | other library is referenced by a regular object. | |
3800 | Add a DT_NEEDED entry for it. */ | |
3801 | add_needed = TRUE; | |
3802 | ret = elf_add_dt_needed_tag (info, soname, add_needed); | |
3803 | if (ret < 0) | |
3804 | goto error_free_vers; | |
3805 | ||
3806 | BFD_ASSERT (ret == 0); | |
3807 | } | |
3808 | } | |
3809 | } | |
3810 | ||
3811 | /* Now that all the symbols from this input file are created, handle | |
3812 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
3813 | if (nondeflt_vers != NULL) | |
3814 | { | |
3815 | bfd_size_type cnt, symidx; | |
3816 | ||
3817 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
3818 | { | |
3819 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
3820 | char *shortname, *p; | |
3821 | ||
3822 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
3823 | if (p == NULL | |
3824 | || (h->root.type != bfd_link_hash_defined | |
3825 | && h->root.type != bfd_link_hash_defweak)) | |
3826 | continue; | |
3827 | ||
3828 | amt = p - h->root.root.string; | |
3829 | shortname = bfd_malloc (amt + 1); | |
3830 | memcpy (shortname, h->root.root.string, amt); | |
3831 | shortname[amt] = '\0'; | |
3832 | ||
3833 | hi = (struct elf_link_hash_entry *) | |
3834 | bfd_link_hash_lookup (&hash_table->root, shortname, | |
3835 | FALSE, FALSE, FALSE); | |
3836 | if (hi != NULL | |
3837 | && hi->root.type == h->root.type | |
3838 | && hi->root.u.def.value == h->root.u.def.value | |
3839 | && hi->root.u.def.section == h->root.u.def.section) | |
3840 | { | |
3841 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
3842 | hi->root.type = bfd_link_hash_indirect; | |
3843 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
3844 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); | |
3845 | sym_hash = elf_sym_hashes (abfd); | |
3846 | if (sym_hash) | |
3847 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
3848 | if (sym_hash[symidx] == hi) | |
3849 | { | |
3850 | sym_hash[symidx] = h; | |
3851 | break; | |
3852 | } | |
3853 | } | |
3854 | free (shortname); | |
3855 | } | |
3856 | free (nondeflt_vers); | |
3857 | nondeflt_vers = NULL; | |
3858 | } | |
3859 | ||
3860 | if (extversym != NULL) | |
3861 | { | |
3862 | free (extversym); | |
3863 | extversym = NULL; | |
3864 | } | |
3865 | ||
3866 | if (isymbuf != NULL) | |
3867 | free (isymbuf); | |
3868 | isymbuf = NULL; | |
3869 | ||
3870 | /* Now set the weakdefs field correctly for all the weak defined | |
3871 | symbols we found. The only way to do this is to search all the | |
3872 | symbols. Since we only need the information for non functions in | |
3873 | dynamic objects, that's the only time we actually put anything on | |
3874 | the list WEAKS. We need this information so that if a regular | |
3875 | object refers to a symbol defined weakly in a dynamic object, the | |
3876 | real symbol in the dynamic object is also put in the dynamic | |
3877 | symbols; we also must arrange for both symbols to point to the | |
3878 | same memory location. We could handle the general case of symbol | |
3879 | aliasing, but a general symbol alias can only be generated in | |
3880 | assembler code, handling it correctly would be very time | |
3881 | consuming, and other ELF linkers don't handle general aliasing | |
3882 | either. */ | |
3883 | if (weaks != NULL) | |
3884 | { | |
3885 | struct elf_link_hash_entry **hpp; | |
3886 | struct elf_link_hash_entry **hppend; | |
3887 | struct elf_link_hash_entry **sorted_sym_hash; | |
3888 | struct elf_link_hash_entry *h; | |
3889 | size_t sym_count; | |
3890 | ||
3891 | /* Since we have to search the whole symbol list for each weak | |
3892 | defined symbol, search time for N weak defined symbols will be | |
3893 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
3894 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3895 | sorted_sym_hash = bfd_malloc (amt); | |
3896 | if (sorted_sym_hash == NULL) | |
3897 | goto error_return; | |
3898 | sym_hash = sorted_sym_hash; | |
3899 | hpp = elf_sym_hashes (abfd); | |
3900 | hppend = hpp + extsymcount; | |
3901 | sym_count = 0; | |
3902 | for (; hpp < hppend; hpp++) | |
3903 | { | |
3904 | h = *hpp; | |
3905 | if (h != NULL | |
3906 | && h->root.type == bfd_link_hash_defined | |
3907 | && h->type != STT_FUNC) | |
3908 | { | |
3909 | *sym_hash = h; | |
3910 | sym_hash++; | |
3911 | sym_count++; | |
3912 | } | |
3913 | } | |
3914 | ||
3915 | qsort (sorted_sym_hash, sym_count, | |
3916 | sizeof (struct elf_link_hash_entry *), | |
3917 | elf_sort_symbol); | |
3918 | ||
3919 | while (weaks != NULL) | |
3920 | { | |
3921 | struct elf_link_hash_entry *hlook; | |
3922 | asection *slook; | |
3923 | bfd_vma vlook; | |
3924 | long ilook; | |
3925 | size_t i, j, idx; | |
3926 | ||
3927 | hlook = weaks; | |
3928 | weaks = hlook->weakdef; | |
3929 | hlook->weakdef = NULL; | |
3930 | ||
3931 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
3932 | || hlook->root.type == bfd_link_hash_defweak | |
3933 | || hlook->root.type == bfd_link_hash_common | |
3934 | || hlook->root.type == bfd_link_hash_indirect); | |
3935 | slook = hlook->root.u.def.section; | |
3936 | vlook = hlook->root.u.def.value; | |
3937 | ||
3938 | ilook = -1; | |
3939 | i = 0; | |
3940 | j = sym_count; | |
3941 | while (i < j) | |
3942 | { | |
3943 | bfd_signed_vma vdiff; | |
3944 | idx = (i + j) / 2; | |
3945 | h = sorted_sym_hash [idx]; | |
3946 | vdiff = vlook - h->root.u.def.value; | |
3947 | if (vdiff < 0) | |
3948 | j = idx; | |
3949 | else if (vdiff > 0) | |
3950 | i = idx + 1; | |
3951 | else | |
3952 | { | |
3953 | long sdiff = slook - h->root.u.def.section; | |
3954 | if (sdiff < 0) | |
3955 | j = idx; | |
3956 | else if (sdiff > 0) | |
3957 | i = idx + 1; | |
3958 | else | |
3959 | { | |
3960 | ilook = idx; | |
3961 | break; | |
3962 | } | |
3963 | } | |
3964 | } | |
3965 | ||
3966 | /* We didn't find a value/section match. */ | |
3967 | if (ilook == -1) | |
3968 | continue; | |
3969 | ||
3970 | for (i = ilook; i < sym_count; i++) | |
3971 | { | |
3972 | h = sorted_sym_hash [i]; | |
3973 | ||
3974 | /* Stop if value or section doesn't match. */ | |
3975 | if (h->root.u.def.value != vlook | |
3976 | || h->root.u.def.section != slook) | |
3977 | break; | |
3978 | else if (h != hlook) | |
3979 | { | |
3980 | hlook->weakdef = h; | |
3981 | ||
3982 | /* If the weak definition is in the list of dynamic | |
3983 | symbols, make sure the real definition is put | |
3984 | there as well. */ | |
3985 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
3986 | { | |
c152c796 | 3987 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 AM |
3988 | goto error_return; |
3989 | } | |
3990 | ||
3991 | /* If the real definition is in the list of dynamic | |
3992 | symbols, make sure the weak definition is put | |
3993 | there as well. If we don't do this, then the | |
3994 | dynamic loader might not merge the entries for the | |
3995 | real definition and the weak definition. */ | |
3996 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
3997 | { | |
c152c796 | 3998 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4ad4eba5 AM |
3999 | goto error_return; |
4000 | } | |
4001 | break; | |
4002 | } | |
4003 | } | |
4004 | } | |
4005 | ||
4006 | free (sorted_sym_hash); | |
4007 | } | |
4008 | ||
4009 | /* If this object is the same format as the output object, and it is | |
4010 | not a shared library, then let the backend look through the | |
4011 | relocs. | |
4012 | ||
4013 | This is required to build global offset table entries and to | |
4014 | arrange for dynamic relocs. It is not required for the | |
4015 | particular common case of linking non PIC code, even when linking | |
4016 | against shared libraries, but unfortunately there is no way of | |
4017 | knowing whether an object file has been compiled PIC or not. | |
4018 | Looking through the relocs is not particularly time consuming. | |
4019 | The problem is that we must either (1) keep the relocs in memory, | |
4020 | which causes the linker to require additional runtime memory or | |
4021 | (2) read the relocs twice from the input file, which wastes time. | |
4022 | This would be a good case for using mmap. | |
4023 | ||
4024 | I have no idea how to handle linking PIC code into a file of a | |
4025 | different format. It probably can't be done. */ | |
4026 | check_relocs = get_elf_backend_data (abfd)->check_relocs; | |
4027 | if (! dynamic | |
4028 | && is_elf_hash_table (hash_table) | |
4029 | && hash_table->root.creator == abfd->xvec | |
4030 | && check_relocs != NULL) | |
4031 | { | |
4032 | asection *o; | |
4033 | ||
4034 | for (o = abfd->sections; o != NULL; o = o->next) | |
4035 | { | |
4036 | Elf_Internal_Rela *internal_relocs; | |
4037 | bfd_boolean ok; | |
4038 | ||
4039 | if ((o->flags & SEC_RELOC) == 0 | |
4040 | || o->reloc_count == 0 | |
4041 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4042 | && (o->flags & SEC_DEBUGGING) != 0) | |
4043 | || bfd_is_abs_section (o->output_section)) | |
4044 | continue; | |
4045 | ||
4046 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4047 | info->keep_memory); | |
4048 | if (internal_relocs == NULL) | |
4049 | goto error_return; | |
4050 | ||
4051 | ok = (*check_relocs) (abfd, info, o, internal_relocs); | |
4052 | ||
4053 | if (elf_section_data (o)->relocs != internal_relocs) | |
4054 | free (internal_relocs); | |
4055 | ||
4056 | if (! ok) | |
4057 | goto error_return; | |
4058 | } | |
4059 | } | |
4060 | ||
4061 | /* If this is a non-traditional link, try to optimize the handling | |
4062 | of the .stab/.stabstr sections. */ | |
4063 | if (! dynamic | |
4064 | && ! info->traditional_format | |
4065 | && is_elf_hash_table (hash_table) | |
4066 | && (info->strip != strip_all && info->strip != strip_debugger)) | |
4067 | { | |
4068 | asection *stabstr; | |
4069 | ||
4070 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4071 | if (stabstr != NULL) | |
4072 | { | |
4073 | bfd_size_type string_offset = 0; | |
4074 | asection *stab; | |
4075 | ||
4076 | for (stab = abfd->sections; stab; stab = stab->next) | |
4077 | if (strncmp (".stab", stab->name, 5) == 0 | |
4078 | && (!stab->name[5] || | |
4079 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4080 | && (stab->flags & SEC_MERGE) == 0 | |
4081 | && !bfd_is_abs_section (stab->output_section)) | |
4082 | { | |
4083 | struct bfd_elf_section_data *secdata; | |
4084 | ||
4085 | secdata = elf_section_data (stab); | |
4086 | if (! _bfd_link_section_stabs (abfd, | |
4087 | & hash_table->stab_info, | |
4088 | stab, stabstr, | |
4089 | &secdata->sec_info, | |
4090 | &string_offset)) | |
4091 | goto error_return; | |
4092 | if (secdata->sec_info) | |
4093 | stab->sec_info_type = ELF_INFO_TYPE_STABS; | |
4094 | } | |
4095 | } | |
4096 | } | |
4097 | ||
4098 | if (! info->relocatable | |
4099 | && ! dynamic | |
4100 | && is_elf_hash_table (hash_table)) | |
4101 | { | |
4102 | asection *s; | |
4103 | ||
4104 | for (s = abfd->sections; s != NULL; s = s->next) | |
4105 | if ((s->flags & SEC_MERGE) != 0 | |
4106 | && !bfd_is_abs_section (s->output_section)) | |
4107 | { | |
4108 | struct bfd_elf_section_data *secdata; | |
4109 | ||
4110 | secdata = elf_section_data (s); | |
4111 | if (! _bfd_merge_section (abfd, | |
4112 | & hash_table->merge_info, | |
4113 | s, &secdata->sec_info)) | |
4114 | goto error_return; | |
4115 | else if (secdata->sec_info) | |
4116 | s->sec_info_type = ELF_INFO_TYPE_MERGE; | |
4117 | } | |
4118 | } | |
4119 | ||
4120 | if (is_elf_hash_table (hash_table)) | |
4121 | { | |
4122 | /* Add this bfd to the loaded list. */ | |
4123 | struct elf_link_loaded_list *n; | |
4124 | ||
4125 | n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); | |
4126 | if (n == NULL) | |
4127 | goto error_return; | |
4128 | n->abfd = abfd; | |
4129 | n->next = hash_table->loaded; | |
4130 | hash_table->loaded = n; | |
4131 | } | |
4132 | ||
4133 | return TRUE; | |
4134 | ||
4135 | error_free_vers: | |
4136 | if (nondeflt_vers != NULL) | |
4137 | free (nondeflt_vers); | |
4138 | if (extversym != NULL) | |
4139 | free (extversym); | |
4140 | error_free_sym: | |
4141 | if (isymbuf != NULL) | |
4142 | free (isymbuf); | |
4143 | error_return: | |
4144 | return FALSE; | |
4145 | } | |
4146 | ||
0ad989f9 L |
4147 | /* Add symbols from an ELF archive file to the linker hash table. We |
4148 | don't use _bfd_generic_link_add_archive_symbols because of a | |
4149 | problem which arises on UnixWare. The UnixWare libc.so is an | |
4150 | archive which includes an entry libc.so.1 which defines a bunch of | |
4151 | symbols. The libc.so archive also includes a number of other | |
4152 | object files, which also define symbols, some of which are the same | |
4153 | as those defined in libc.so.1. Correct linking requires that we | |
4154 | consider each object file in turn, and include it if it defines any | |
4155 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
4156 | this; it looks through the list of undefined symbols, and includes | |
4157 | any object file which defines them. When this algorithm is used on | |
4158 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
4159 | bunch of symbols. This means that some of the other objects in the | |
4160 | archive are not included in the link, which is incorrect since they | |
4161 | precede libc.so.1 in the archive. | |
4162 | ||
4163 | Fortunately, ELF archive handling is simpler than that done by | |
4164 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4165 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4166 | symbol is currently undefined, we know that we must pull in that | |
4167 | object file. | |
4168 | ||
4169 | Unfortunately, we do have to make multiple passes over the symbol | |
4170 | table until nothing further is resolved. */ | |
4171 | ||
4ad4eba5 AM |
4172 | static bfd_boolean |
4173 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
4174 | { |
4175 | symindex c; | |
4176 | bfd_boolean *defined = NULL; | |
4177 | bfd_boolean *included = NULL; | |
4178 | carsym *symdefs; | |
4179 | bfd_boolean loop; | |
4180 | bfd_size_type amt; | |
4181 | ||
4182 | if (! bfd_has_map (abfd)) | |
4183 | { | |
4184 | /* An empty archive is a special case. */ | |
4185 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
4186 | return TRUE; | |
4187 | bfd_set_error (bfd_error_no_armap); | |
4188 | return FALSE; | |
4189 | } | |
4190 | ||
4191 | /* Keep track of all symbols we know to be already defined, and all | |
4192 | files we know to be already included. This is to speed up the | |
4193 | second and subsequent passes. */ | |
4194 | c = bfd_ardata (abfd)->symdef_count; | |
4195 | if (c == 0) | |
4196 | return TRUE; | |
4197 | amt = c; | |
4198 | amt *= sizeof (bfd_boolean); | |
4199 | defined = bfd_zmalloc (amt); | |
4200 | included = bfd_zmalloc (amt); | |
4201 | if (defined == NULL || included == NULL) | |
4202 | goto error_return; | |
4203 | ||
4204 | symdefs = bfd_ardata (abfd)->symdefs; | |
4205 | ||
4206 | do | |
4207 | { | |
4208 | file_ptr last; | |
4209 | symindex i; | |
4210 | carsym *symdef; | |
4211 | carsym *symdefend; | |
4212 | ||
4213 | loop = FALSE; | |
4214 | last = -1; | |
4215 | ||
4216 | symdef = symdefs; | |
4217 | symdefend = symdef + c; | |
4218 | for (i = 0; symdef < symdefend; symdef++, i++) | |
4219 | { | |
4220 | struct elf_link_hash_entry *h; | |
4221 | bfd *element; | |
4222 | struct bfd_link_hash_entry *undefs_tail; | |
4223 | symindex mark; | |
4224 | ||
4225 | if (defined[i] || included[i]) | |
4226 | continue; | |
4227 | if (symdef->file_offset == last) | |
4228 | { | |
4229 | included[i] = TRUE; | |
4230 | continue; | |
4231 | } | |
4232 | ||
4233 | h = elf_link_hash_lookup (elf_hash_table (info), symdef->name, | |
4234 | FALSE, FALSE, FALSE); | |
4235 | ||
4236 | if (h == NULL) | |
4237 | { | |
4238 | char *p, *copy; | |
4239 | size_t len, first; | |
4240 | ||
4241 | /* If this is a default version (the name contains @@), | |
4242 | look up the symbol again with only one `@' as well | |
4243 | as without the version. The effect is that references | |
4244 | to the symbol with and without the version will be | |
4245 | matched by the default symbol in the archive. */ | |
4246 | ||
4247 | p = strchr (symdef->name, ELF_VER_CHR); | |
4248 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4249 | continue; | |
4250 | ||
4251 | /* First check with only one `@'. */ | |
4252 | len = strlen (symdef->name); | |
4253 | copy = bfd_alloc (abfd, len); | |
4254 | if (copy == NULL) | |
4255 | goto error_return; | |
4256 | first = p - symdef->name + 1; | |
4257 | memcpy (copy, symdef->name, first); | |
4258 | memcpy (copy + first, symdef->name + first + 1, len - first); | |
4259 | ||
4260 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
4261 | FALSE, FALSE, FALSE); | |
4262 | ||
4263 | if (h == NULL) | |
4264 | { | |
4265 | /* We also need to check references to the symbol | |
4266 | without the version. */ | |
4267 | ||
4268 | copy[first - 1] = '\0'; | |
4269 | h = elf_link_hash_lookup (elf_hash_table (info), | |
4270 | copy, FALSE, FALSE, FALSE); | |
4271 | } | |
4272 | ||
4273 | bfd_release (abfd, copy); | |
4274 | } | |
4275 | ||
4276 | if (h == NULL) | |
4277 | continue; | |
4278 | ||
4279 | if (h->root.type == bfd_link_hash_common) | |
4280 | { | |
4281 | /* We currently have a common symbol. The archive map contains | |
4282 | a reference to this symbol, so we may want to include it. We | |
4283 | only want to include it however, if this archive element | |
4284 | contains a definition of the symbol, not just another common | |
4285 | declaration of it. | |
4286 | ||
4287 | Unfortunately some archivers (including GNU ar) will put | |
4288 | declarations of common symbols into their archive maps, as | |
4289 | well as real definitions, so we cannot just go by the archive | |
4290 | map alone. Instead we must read in the element's symbol | |
4291 | table and check that to see what kind of symbol definition | |
4292 | this is. */ | |
4293 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
4294 | continue; | |
4295 | } | |
4296 | else if (h->root.type != bfd_link_hash_undefined) | |
4297 | { | |
4298 | if (h->root.type != bfd_link_hash_undefweak) | |
4299 | defined[i] = TRUE; | |
4300 | continue; | |
4301 | } | |
4302 | ||
4303 | /* We need to include this archive member. */ | |
4304 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
4305 | if (element == NULL) | |
4306 | goto error_return; | |
4307 | ||
4308 | if (! bfd_check_format (element, bfd_object)) | |
4309 | goto error_return; | |
4310 | ||
4311 | /* Doublecheck that we have not included this object | |
4312 | already--it should be impossible, but there may be | |
4313 | something wrong with the archive. */ | |
4314 | if (element->archive_pass != 0) | |
4315 | { | |
4316 | bfd_set_error (bfd_error_bad_value); | |
4317 | goto error_return; | |
4318 | } | |
4319 | element->archive_pass = 1; | |
4320 | ||
4321 | undefs_tail = info->hash->undefs_tail; | |
4322 | ||
4323 | if (! (*info->callbacks->add_archive_element) (info, element, | |
4324 | symdef->name)) | |
4325 | goto error_return; | |
4326 | if (! bfd_link_add_symbols (element, info)) | |
4327 | goto error_return; | |
4328 | ||
4329 | /* If there are any new undefined symbols, we need to make | |
4330 | another pass through the archive in order to see whether | |
4331 | they can be defined. FIXME: This isn't perfect, because | |
4332 | common symbols wind up on undefs_tail and because an | |
4333 | undefined symbol which is defined later on in this pass | |
4334 | does not require another pass. This isn't a bug, but it | |
4335 | does make the code less efficient than it could be. */ | |
4336 | if (undefs_tail != info->hash->undefs_tail) | |
4337 | loop = TRUE; | |
4338 | ||
4339 | /* Look backward to mark all symbols from this object file | |
4340 | which we have already seen in this pass. */ | |
4341 | mark = i; | |
4342 | do | |
4343 | { | |
4344 | included[mark] = TRUE; | |
4345 | if (mark == 0) | |
4346 | break; | |
4347 | --mark; | |
4348 | } | |
4349 | while (symdefs[mark].file_offset == symdef->file_offset); | |
4350 | ||
4351 | /* We mark subsequent symbols from this object file as we go | |
4352 | on through the loop. */ | |
4353 | last = symdef->file_offset; | |
4354 | } | |
4355 | } | |
4356 | while (loop); | |
4357 | ||
4358 | free (defined); | |
4359 | free (included); | |
4360 | ||
4361 | return TRUE; | |
4362 | ||
4363 | error_return: | |
4364 | if (defined != NULL) | |
4365 | free (defined); | |
4366 | if (included != NULL) | |
4367 | free (included); | |
4368 | return FALSE; | |
4369 | } | |
4ad4eba5 AM |
4370 | |
4371 | /* Given an ELF BFD, add symbols to the global hash table as | |
4372 | appropriate. */ | |
4373 | ||
4374 | bfd_boolean | |
4375 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
4376 | { | |
4377 | switch (bfd_get_format (abfd)) | |
4378 | { | |
4379 | case bfd_object: | |
4380 | return elf_link_add_object_symbols (abfd, info); | |
4381 | case bfd_archive: | |
4382 | return elf_link_add_archive_symbols (abfd, info); | |
4383 | default: | |
4384 | bfd_set_error (bfd_error_wrong_format); | |
4385 | return FALSE; | |
4386 | } | |
4387 | } | |
5a580b3a AM |
4388 | \f |
4389 | /* This function will be called though elf_link_hash_traverse to store | |
4390 | all hash value of the exported symbols in an array. */ | |
4391 | ||
4392 | static bfd_boolean | |
4393 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
4394 | { | |
4395 | unsigned long **valuep = data; | |
4396 | const char *name; | |
4397 | char *p; | |
4398 | unsigned long ha; | |
4399 | char *alc = NULL; | |
4400 | ||
4401 | if (h->root.type == bfd_link_hash_warning) | |
4402 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4403 | ||
4404 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
4405 | if (h->dynindx == -1) | |
4406 | return TRUE; | |
4407 | ||
4408 | name = h->root.root.string; | |
4409 | p = strchr (name, ELF_VER_CHR); | |
4410 | if (p != NULL) | |
4411 | { | |
4412 | alc = bfd_malloc (p - name + 1); | |
4413 | memcpy (alc, name, p - name); | |
4414 | alc[p - name] = '\0'; | |
4415 | name = alc; | |
4416 | } | |
4417 | ||
4418 | /* Compute the hash value. */ | |
4419 | ha = bfd_elf_hash (name); | |
4420 | ||
4421 | /* Store the found hash value in the array given as the argument. */ | |
4422 | *(*valuep)++ = ha; | |
4423 | ||
4424 | /* And store it in the struct so that we can put it in the hash table | |
4425 | later. */ | |
4426 | h->elf_hash_value = ha; | |
4427 | ||
4428 | if (alc != NULL) | |
4429 | free (alc); | |
4430 | ||
4431 | return TRUE; | |
4432 | } | |
4433 | ||
4434 | /* Array used to determine the number of hash table buckets to use | |
4435 | based on the number of symbols there are. If there are fewer than | |
4436 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
4437 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
4438 | than 32771 buckets. */ | |
4439 | ||
4440 | static const size_t elf_buckets[] = | |
4441 | { | |
4442 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
4443 | 16411, 32771, 0 | |
4444 | }; | |
4445 | ||
4446 | /* Compute bucket count for hashing table. We do not use a static set | |
4447 | of possible tables sizes anymore. Instead we determine for all | |
4448 | possible reasonable sizes of the table the outcome (i.e., the | |
4449 | number of collisions etc) and choose the best solution. The | |
4450 | weighting functions are not too simple to allow the table to grow | |
4451 | without bounds. Instead one of the weighting factors is the size. | |
4452 | Therefore the result is always a good payoff between few collisions | |
4453 | (= short chain lengths) and table size. */ | |
4454 | static size_t | |
4455 | compute_bucket_count (struct bfd_link_info *info) | |
4456 | { | |
4457 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; | |
4458 | size_t best_size = 0; | |
4459 | unsigned long int *hashcodes; | |
4460 | unsigned long int *hashcodesp; | |
4461 | unsigned long int i; | |
4462 | bfd_size_type amt; | |
4463 | ||
4464 | /* Compute the hash values for all exported symbols. At the same | |
4465 | time store the values in an array so that we could use them for | |
4466 | optimizations. */ | |
4467 | amt = dynsymcount; | |
4468 | amt *= sizeof (unsigned long int); | |
4469 | hashcodes = bfd_malloc (amt); | |
4470 | if (hashcodes == NULL) | |
4471 | return 0; | |
4472 | hashcodesp = hashcodes; | |
4473 | ||
4474 | /* Put all hash values in HASHCODES. */ | |
4475 | elf_link_hash_traverse (elf_hash_table (info), | |
4476 | elf_collect_hash_codes, &hashcodesp); | |
4477 | ||
4478 | /* We have a problem here. The following code to optimize the table | |
4479 | size requires an integer type with more the 32 bits. If | |
4480 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
4481 | #ifdef BFD_HOST_U_64_BIT | |
4482 | if (info->optimize) | |
4483 | { | |
4484 | unsigned long int nsyms = hashcodesp - hashcodes; | |
4485 | size_t minsize; | |
4486 | size_t maxsize; | |
4487 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
4488 | unsigned long int *counts ; | |
4489 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
4490 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); | |
4491 | ||
4492 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
4493 | that the hashing table must at least have NSYMS/4 and at most | |
4494 | 2*NSYMS buckets. */ | |
4495 | minsize = nsyms / 4; | |
4496 | if (minsize == 0) | |
4497 | minsize = 1; | |
4498 | best_size = maxsize = nsyms * 2; | |
4499 | ||
4500 | /* Create array where we count the collisions in. We must use bfd_malloc | |
4501 | since the size could be large. */ | |
4502 | amt = maxsize; | |
4503 | amt *= sizeof (unsigned long int); | |
4504 | counts = bfd_malloc (amt); | |
4505 | if (counts == NULL) | |
4506 | { | |
4507 | free (hashcodes); | |
4508 | return 0; | |
4509 | } | |
4510 | ||
4511 | /* Compute the "optimal" size for the hash table. The criteria is a | |
4512 | minimal chain length. The minor criteria is (of course) the size | |
4513 | of the table. */ | |
4514 | for (i = minsize; i < maxsize; ++i) | |
4515 | { | |
4516 | /* Walk through the array of hashcodes and count the collisions. */ | |
4517 | BFD_HOST_U_64_BIT max; | |
4518 | unsigned long int j; | |
4519 | unsigned long int fact; | |
4520 | ||
4521 | memset (counts, '\0', i * sizeof (unsigned long int)); | |
4522 | ||
4523 | /* Determine how often each hash bucket is used. */ | |
4524 | for (j = 0; j < nsyms; ++j) | |
4525 | ++counts[hashcodes[j] % i]; | |
4526 | ||
4527 | /* For the weight function we need some information about the | |
4528 | pagesize on the target. This is information need not be 100% | |
4529 | accurate. Since this information is not available (so far) we | |
4530 | define it here to a reasonable default value. If it is crucial | |
4531 | to have a better value some day simply define this value. */ | |
4532 | # ifndef BFD_TARGET_PAGESIZE | |
4533 | # define BFD_TARGET_PAGESIZE (4096) | |
4534 | # endif | |
4535 | ||
4536 | /* We in any case need 2 + NSYMS entries for the size values and | |
4537 | the chains. */ | |
4538 | max = (2 + nsyms) * (bed->s->arch_size / 8); | |
4539 | ||
4540 | # if 1 | |
4541 | /* Variant 1: optimize for short chains. We add the squares | |
4542 | of all the chain lengths (which favors many small chain | |
4543 | over a few long chains). */ | |
4544 | for (j = 0; j < i; ++j) | |
4545 | max += counts[j] * counts[j]; | |
4546 | ||
4547 | /* This adds penalties for the overall size of the table. */ | |
4548 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
4549 | max *= fact * fact; | |
4550 | # else | |
4551 | /* Variant 2: Optimize a lot more for small table. Here we | |
4552 | also add squares of the size but we also add penalties for | |
4553 | empty slots (the +1 term). */ | |
4554 | for (j = 0; j < i; ++j) | |
4555 | max += (1 + counts[j]) * (1 + counts[j]); | |
4556 | ||
4557 | /* The overall size of the table is considered, but not as | |
4558 | strong as in variant 1, where it is squared. */ | |
4559 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
4560 | max *= fact; | |
4561 | # endif | |
4562 | ||
4563 | /* Compare with current best results. */ | |
4564 | if (max < best_chlen) | |
4565 | { | |
4566 | best_chlen = max; | |
4567 | best_size = i; | |
4568 | } | |
4569 | } | |
4570 | ||
4571 | free (counts); | |
4572 | } | |
4573 | else | |
4574 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
4575 | { | |
4576 | /* This is the fallback solution if no 64bit type is available or if we | |
4577 | are not supposed to spend much time on optimizations. We select the | |
4578 | bucket count using a fixed set of numbers. */ | |
4579 | for (i = 0; elf_buckets[i] != 0; i++) | |
4580 | { | |
4581 | best_size = elf_buckets[i]; | |
4582 | if (dynsymcount < elf_buckets[i + 1]) | |
4583 | break; | |
4584 | } | |
4585 | } | |
4586 | ||
4587 | /* Free the arrays we needed. */ | |
4588 | free (hashcodes); | |
4589 | ||
4590 | return best_size; | |
4591 | } | |
4592 | ||
4593 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
4594 | called by the ELF linker emulation before_allocation routine. We | |
4595 | must set the sizes of the sections before the linker sets the | |
4596 | addresses of the various sections. */ | |
4597 | ||
4598 | bfd_boolean | |
4599 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
4600 | const char *soname, | |
4601 | const char *rpath, | |
4602 | const char *filter_shlib, | |
4603 | const char * const *auxiliary_filters, | |
4604 | struct bfd_link_info *info, | |
4605 | asection **sinterpptr, | |
4606 | struct bfd_elf_version_tree *verdefs) | |
4607 | { | |
4608 | bfd_size_type soname_indx; | |
4609 | bfd *dynobj; | |
4610 | const struct elf_backend_data *bed; | |
4611 | struct elf_assign_sym_version_info asvinfo; | |
4612 | ||
4613 | *sinterpptr = NULL; | |
4614 | ||
4615 | soname_indx = (bfd_size_type) -1; | |
4616 | ||
4617 | if (!is_elf_hash_table (info->hash)) | |
4618 | return TRUE; | |
4619 | ||
4620 | if (info->execstack) | |
4621 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
4622 | else if (info->noexecstack) | |
4623 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
4624 | else | |
4625 | { | |
4626 | bfd *inputobj; | |
4627 | asection *notesec = NULL; | |
4628 | int exec = 0; | |
4629 | ||
4630 | for (inputobj = info->input_bfds; | |
4631 | inputobj; | |
4632 | inputobj = inputobj->link_next) | |
4633 | { | |
4634 | asection *s; | |
4635 | ||
4636 | if (inputobj->flags & DYNAMIC) | |
4637 | continue; | |
4638 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
4639 | if (s) | |
4640 | { | |
4641 | if (s->flags & SEC_CODE) | |
4642 | exec = PF_X; | |
4643 | notesec = s; | |
4644 | } | |
4645 | else | |
4646 | exec = PF_X; | |
4647 | } | |
4648 | if (notesec) | |
4649 | { | |
4650 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
4651 | if (exec && info->relocatable | |
4652 | && notesec->output_section != bfd_abs_section_ptr) | |
4653 | notesec->output_section->flags |= SEC_CODE; | |
4654 | } | |
4655 | } | |
4656 | ||
4657 | /* Any syms created from now on start with -1 in | |
4658 | got.refcount/offset and plt.refcount/offset. */ | |
4659 | elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset; | |
4660 | ||
4661 | /* The backend may have to create some sections regardless of whether | |
4662 | we're dynamic or not. */ | |
4663 | bed = get_elf_backend_data (output_bfd); | |
4664 | if (bed->elf_backend_always_size_sections | |
4665 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
4666 | return FALSE; | |
4667 | ||
4668 | dynobj = elf_hash_table (info)->dynobj; | |
4669 | ||
4670 | /* If there were no dynamic objects in the link, there is nothing to | |
4671 | do here. */ | |
4672 | if (dynobj == NULL) | |
4673 | return TRUE; | |
4674 | ||
4675 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
4676 | return FALSE; | |
4677 | ||
4678 | if (elf_hash_table (info)->dynamic_sections_created) | |
4679 | { | |
4680 | struct elf_info_failed eif; | |
4681 | struct elf_link_hash_entry *h; | |
4682 | asection *dynstr; | |
4683 | struct bfd_elf_version_tree *t; | |
4684 | struct bfd_elf_version_expr *d; | |
4685 | bfd_boolean all_defined; | |
4686 | ||
4687 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
4688 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
4689 | ||
4690 | if (soname != NULL) | |
4691 | { | |
4692 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
4693 | soname, TRUE); | |
4694 | if (soname_indx == (bfd_size_type) -1 | |
4695 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
4696 | return FALSE; | |
4697 | } | |
4698 | ||
4699 | if (info->symbolic) | |
4700 | { | |
4701 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
4702 | return FALSE; | |
4703 | info->flags |= DF_SYMBOLIC; | |
4704 | } | |
4705 | ||
4706 | if (rpath != NULL) | |
4707 | { | |
4708 | bfd_size_type indx; | |
4709 | ||
4710 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
4711 | TRUE); | |
4712 | if (indx == (bfd_size_type) -1 | |
4713 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
4714 | return FALSE; | |
4715 | ||
4716 | if (info->new_dtags) | |
4717 | { | |
4718 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
4719 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
4720 | return FALSE; | |
4721 | } | |
4722 | } | |
4723 | ||
4724 | if (filter_shlib != NULL) | |
4725 | { | |
4726 | bfd_size_type indx; | |
4727 | ||
4728 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
4729 | filter_shlib, TRUE); | |
4730 | if (indx == (bfd_size_type) -1 | |
4731 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
4732 | return FALSE; | |
4733 | } | |
4734 | ||
4735 | if (auxiliary_filters != NULL) | |
4736 | { | |
4737 | const char * const *p; | |
4738 | ||
4739 | for (p = auxiliary_filters; *p != NULL; p++) | |
4740 | { | |
4741 | bfd_size_type indx; | |
4742 | ||
4743 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
4744 | *p, TRUE); | |
4745 | if (indx == (bfd_size_type) -1 | |
4746 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
4747 | return FALSE; | |
4748 | } | |
4749 | } | |
4750 | ||
4751 | eif.info = info; | |
4752 | eif.verdefs = verdefs; | |
4753 | eif.failed = FALSE; | |
4754 | ||
4755 | /* If we are supposed to export all symbols into the dynamic symbol | |
4756 | table (this is not the normal case), then do so. */ | |
4757 | if (info->export_dynamic) | |
4758 | { | |
4759 | elf_link_hash_traverse (elf_hash_table (info), | |
4760 | _bfd_elf_export_symbol, | |
4761 | &eif); | |
4762 | if (eif.failed) | |
4763 | return FALSE; | |
4764 | } | |
4765 | ||
4766 | /* Make all global versions with definition. */ | |
4767 | for (t = verdefs; t != NULL; t = t->next) | |
4768 | for (d = t->globals.list; d != NULL; d = d->next) | |
4769 | if (!d->symver && d->symbol) | |
4770 | { | |
4771 | const char *verstr, *name; | |
4772 | size_t namelen, verlen, newlen; | |
4773 | char *newname, *p; | |
4774 | struct elf_link_hash_entry *newh; | |
4775 | ||
4776 | name = d->symbol; | |
4777 | namelen = strlen (name); | |
4778 | verstr = t->name; | |
4779 | verlen = strlen (verstr); | |
4780 | newlen = namelen + verlen + 3; | |
4781 | ||
4782 | newname = bfd_malloc (newlen); | |
4783 | if (newname == NULL) | |
4784 | return FALSE; | |
4785 | memcpy (newname, name, namelen); | |
4786 | ||
4787 | /* Check the hidden versioned definition. */ | |
4788 | p = newname + namelen; | |
4789 | *p++ = ELF_VER_CHR; | |
4790 | memcpy (p, verstr, verlen + 1); | |
4791 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
4792 | newname, FALSE, FALSE, | |
4793 | FALSE); | |
4794 | if (newh == NULL | |
4795 | || (newh->root.type != bfd_link_hash_defined | |
4796 | && newh->root.type != bfd_link_hash_defweak)) | |
4797 | { | |
4798 | /* Check the default versioned definition. */ | |
4799 | *p++ = ELF_VER_CHR; | |
4800 | memcpy (p, verstr, verlen + 1); | |
4801 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
4802 | newname, FALSE, FALSE, | |
4803 | FALSE); | |
4804 | } | |
4805 | free (newname); | |
4806 | ||
4807 | /* Mark this version if there is a definition and it is | |
4808 | not defined in a shared object. */ | |
4809 | if (newh != NULL | |
4810 | && ((newh->elf_link_hash_flags | |
4811 | & ELF_LINK_HASH_DEF_DYNAMIC) == 0) | |
4812 | && (newh->root.type == bfd_link_hash_defined | |
4813 | || newh->root.type == bfd_link_hash_defweak)) | |
4814 | d->symver = 1; | |
4815 | } | |
4816 | ||
4817 | /* Attach all the symbols to their version information. */ | |
4818 | asvinfo.output_bfd = output_bfd; | |
4819 | asvinfo.info = info; | |
4820 | asvinfo.verdefs = verdefs; | |
4821 | asvinfo.failed = FALSE; | |
4822 | ||
4823 | elf_link_hash_traverse (elf_hash_table (info), | |
4824 | _bfd_elf_link_assign_sym_version, | |
4825 | &asvinfo); | |
4826 | if (asvinfo.failed) | |
4827 | return FALSE; | |
4828 | ||
4829 | if (!info->allow_undefined_version) | |
4830 | { | |
4831 | /* Check if all global versions have a definition. */ | |
4832 | all_defined = TRUE; | |
4833 | for (t = verdefs; t != NULL; t = t->next) | |
4834 | for (d = t->globals.list; d != NULL; d = d->next) | |
4835 | if (!d->symver && !d->script) | |
4836 | { | |
4837 | (*_bfd_error_handler) | |
4838 | (_("%s: undefined version: %s"), | |
4839 | d->pattern, t->name); | |
4840 | all_defined = FALSE; | |
4841 | } | |
4842 | ||
4843 | if (!all_defined) | |
4844 | { | |
4845 | bfd_set_error (bfd_error_bad_value); | |
4846 | return FALSE; | |
4847 | } | |
4848 | } | |
4849 | ||
4850 | /* Find all symbols which were defined in a dynamic object and make | |
4851 | the backend pick a reasonable value for them. */ | |
4852 | elf_link_hash_traverse (elf_hash_table (info), | |
4853 | _bfd_elf_adjust_dynamic_symbol, | |
4854 | &eif); | |
4855 | if (eif.failed) | |
4856 | return FALSE; | |
4857 | ||
4858 | /* Add some entries to the .dynamic section. We fill in some of the | |
4859 | values later, in elf_bfd_final_link, but we must add the entries | |
4860 | now so that we know the final size of the .dynamic section. */ | |
4861 | ||
4862 | /* If there are initialization and/or finalization functions to | |
4863 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
4864 | h = (info->init_function | |
4865 | ? elf_link_hash_lookup (elf_hash_table (info), | |
4866 | info->init_function, FALSE, | |
4867 | FALSE, FALSE) | |
4868 | : NULL); | |
4869 | if (h != NULL | |
4870 | && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR | |
4871 | | ELF_LINK_HASH_DEF_REGULAR)) != 0) | |
4872 | { | |
4873 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
4874 | return FALSE; | |
4875 | } | |
4876 | h = (info->fini_function | |
4877 | ? elf_link_hash_lookup (elf_hash_table (info), | |
4878 | info->fini_function, FALSE, | |
4879 | FALSE, FALSE) | |
4880 | : NULL); | |
4881 | if (h != NULL | |
4882 | && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR | |
4883 | | ELF_LINK_HASH_DEF_REGULAR)) != 0) | |
4884 | { | |
4885 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
4886 | return FALSE; | |
4887 | } | |
4888 | ||
4889 | if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL) | |
4890 | { | |
4891 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
4892 | if (! info->executable) | |
4893 | { | |
4894 | bfd *sub; | |
4895 | asection *o; | |
4896 | ||
4897 | for (sub = info->input_bfds; sub != NULL; | |
4898 | sub = sub->link_next) | |
4899 | for (o = sub->sections; o != NULL; o = o->next) | |
4900 | if (elf_section_data (o)->this_hdr.sh_type | |
4901 | == SHT_PREINIT_ARRAY) | |
4902 | { | |
4903 | (*_bfd_error_handler) | |
4904 | (_("%s: .preinit_array section is not allowed in DSO"), | |
4905 | bfd_archive_filename (sub)); | |
4906 | break; | |
4907 | } | |
4908 | ||
4909 | bfd_set_error (bfd_error_nonrepresentable_section); | |
4910 | return FALSE; | |
4911 | } | |
4912 | ||
4913 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
4914 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
4915 | return FALSE; | |
4916 | } | |
4917 | if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL) | |
4918 | { | |
4919 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
4920 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
4921 | return FALSE; | |
4922 | } | |
4923 | if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL) | |
4924 | { | |
4925 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
4926 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
4927 | return FALSE; | |
4928 | } | |
4929 | ||
4930 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
4931 | /* If .dynstr is excluded from the link, we don't want any of | |
4932 | these tags. Strictly, we should be checking each section | |
4933 | individually; This quick check covers for the case where | |
4934 | someone does a /DISCARD/ : { *(*) }. */ | |
4935 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
4936 | { | |
4937 | bfd_size_type strsize; | |
4938 | ||
4939 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
4940 | if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0) | |
4941 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
4942 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
4943 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
4944 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
4945 | bed->s->sizeof_sym)) | |
4946 | return FALSE; | |
4947 | } | |
4948 | } | |
4949 | ||
4950 | /* The backend must work out the sizes of all the other dynamic | |
4951 | sections. */ | |
4952 | if (bed->elf_backend_size_dynamic_sections | |
4953 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
4954 | return FALSE; | |
4955 | ||
4956 | if (elf_hash_table (info)->dynamic_sections_created) | |
4957 | { | |
4958 | bfd_size_type dynsymcount; | |
4959 | asection *s; | |
4960 | size_t bucketcount = 0; | |
4961 | size_t hash_entry_size; | |
4962 | unsigned int dtagcount; | |
4963 | ||
4964 | /* Set up the version definition section. */ | |
4965 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
4966 | BFD_ASSERT (s != NULL); | |
4967 | ||
4968 | /* We may have created additional version definitions if we are | |
4969 | just linking a regular application. */ | |
4970 | verdefs = asvinfo.verdefs; | |
4971 | ||
4972 | /* Skip anonymous version tag. */ | |
4973 | if (verdefs != NULL && verdefs->vernum == 0) | |
4974 | verdefs = verdefs->next; | |
4975 | ||
4976 | if (verdefs == NULL) | |
4977 | _bfd_strip_section_from_output (info, s); | |
4978 | else | |
4979 | { | |
4980 | unsigned int cdefs; | |
4981 | bfd_size_type size; | |
4982 | struct bfd_elf_version_tree *t; | |
4983 | bfd_byte *p; | |
4984 | Elf_Internal_Verdef def; | |
4985 | Elf_Internal_Verdaux defaux; | |
4986 | ||
4987 | cdefs = 0; | |
4988 | size = 0; | |
4989 | ||
4990 | /* Make space for the base version. */ | |
4991 | size += sizeof (Elf_External_Verdef); | |
4992 | size += sizeof (Elf_External_Verdaux); | |
4993 | ++cdefs; | |
4994 | ||
4995 | for (t = verdefs; t != NULL; t = t->next) | |
4996 | { | |
4997 | struct bfd_elf_version_deps *n; | |
4998 | ||
4999 | size += sizeof (Elf_External_Verdef); | |
5000 | size += sizeof (Elf_External_Verdaux); | |
5001 | ++cdefs; | |
5002 | ||
5003 | for (n = t->deps; n != NULL; n = n->next) | |
5004 | size += sizeof (Elf_External_Verdaux); | |
5005 | } | |
5006 | ||
5007 | s->_raw_size = size; | |
5008 | s->contents = bfd_alloc (output_bfd, s->_raw_size); | |
5009 | if (s->contents == NULL && s->_raw_size != 0) | |
5010 | return FALSE; | |
5011 | ||
5012 | /* Fill in the version definition section. */ | |
5013 | ||
5014 | p = s->contents; | |
5015 | ||
5016 | def.vd_version = VER_DEF_CURRENT; | |
5017 | def.vd_flags = VER_FLG_BASE; | |
5018 | def.vd_ndx = 1; | |
5019 | def.vd_cnt = 1; | |
5020 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5021 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5022 | + sizeof (Elf_External_Verdaux)); | |
5023 | ||
5024 | if (soname_indx != (bfd_size_type) -1) | |
5025 | { | |
5026 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5027 | soname_indx); | |
5028 | def.vd_hash = bfd_elf_hash (soname); | |
5029 | defaux.vda_name = soname_indx; | |
5030 | } | |
5031 | else | |
5032 | { | |
5033 | const char *name; | |
5034 | bfd_size_type indx; | |
5035 | ||
5036 | name = basename (output_bfd->filename); | |
5037 | def.vd_hash = bfd_elf_hash (name); | |
5038 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5039 | name, FALSE); | |
5040 | if (indx == (bfd_size_type) -1) | |
5041 | return FALSE; | |
5042 | defaux.vda_name = indx; | |
5043 | } | |
5044 | defaux.vda_next = 0; | |
5045 | ||
5046 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5047 | (Elf_External_Verdef *) p); | |
5048 | p += sizeof (Elf_External_Verdef); | |
5049 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5050 | (Elf_External_Verdaux *) p); | |
5051 | p += sizeof (Elf_External_Verdaux); | |
5052 | ||
5053 | for (t = verdefs; t != NULL; t = t->next) | |
5054 | { | |
5055 | unsigned int cdeps; | |
5056 | struct bfd_elf_version_deps *n; | |
5057 | struct elf_link_hash_entry *h; | |
5058 | struct bfd_link_hash_entry *bh; | |
5059 | ||
5060 | cdeps = 0; | |
5061 | for (n = t->deps; n != NULL; n = n->next) | |
5062 | ++cdeps; | |
5063 | ||
5064 | /* Add a symbol representing this version. */ | |
5065 | bh = NULL; | |
5066 | if (! (_bfd_generic_link_add_one_symbol | |
5067 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5068 | 0, NULL, FALSE, | |
5069 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5070 | return FALSE; | |
5071 | h = (struct elf_link_hash_entry *) bh; | |
5072 | h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF; | |
5073 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
5074 | h->type = STT_OBJECT; | |
5075 | h->verinfo.vertree = t; | |
5076 | ||
c152c796 | 5077 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
5078 | return FALSE; |
5079 | ||
5080 | def.vd_version = VER_DEF_CURRENT; | |
5081 | def.vd_flags = 0; | |
5082 | if (t->globals.list == NULL | |
5083 | && t->locals.list == NULL | |
5084 | && ! t->used) | |
5085 | def.vd_flags |= VER_FLG_WEAK; | |
5086 | def.vd_ndx = t->vernum + 1; | |
5087 | def.vd_cnt = cdeps + 1; | |
5088 | def.vd_hash = bfd_elf_hash (t->name); | |
5089 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5090 | def.vd_next = 0; | |
5091 | if (t->next != NULL) | |
5092 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5093 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
5094 | ||
5095 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5096 | (Elf_External_Verdef *) p); | |
5097 | p += sizeof (Elf_External_Verdef); | |
5098 | ||
5099 | defaux.vda_name = h->dynstr_index; | |
5100 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5101 | h->dynstr_index); | |
5102 | defaux.vda_next = 0; | |
5103 | if (t->deps != NULL) | |
5104 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5105 | t->name_indx = defaux.vda_name; | |
5106 | ||
5107 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5108 | (Elf_External_Verdaux *) p); | |
5109 | p += sizeof (Elf_External_Verdaux); | |
5110 | ||
5111 | for (n = t->deps; n != NULL; n = n->next) | |
5112 | { | |
5113 | if (n->version_needed == NULL) | |
5114 | { | |
5115 | /* This can happen if there was an error in the | |
5116 | version script. */ | |
5117 | defaux.vda_name = 0; | |
5118 | } | |
5119 | else | |
5120 | { | |
5121 | defaux.vda_name = n->version_needed->name_indx; | |
5122 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5123 | defaux.vda_name); | |
5124 | } | |
5125 | if (n->next == NULL) | |
5126 | defaux.vda_next = 0; | |
5127 | else | |
5128 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5129 | ||
5130 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5131 | (Elf_External_Verdaux *) p); | |
5132 | p += sizeof (Elf_External_Verdaux); | |
5133 | } | |
5134 | } | |
5135 | ||
5136 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
5137 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
5138 | return FALSE; | |
5139 | ||
5140 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
5141 | } | |
5142 | ||
5143 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
5144 | { | |
5145 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
5146 | return FALSE; | |
5147 | } | |
5148 | else if (info->flags & DF_BIND_NOW) | |
5149 | { | |
5150 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
5151 | return FALSE; | |
5152 | } | |
5153 | ||
5154 | if (info->flags_1) | |
5155 | { | |
5156 | if (info->executable) | |
5157 | info->flags_1 &= ~ (DF_1_INITFIRST | |
5158 | | DF_1_NODELETE | |
5159 | | DF_1_NOOPEN); | |
5160 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
5161 | return FALSE; | |
5162 | } | |
5163 | ||
5164 | /* Work out the size of the version reference section. */ | |
5165 | ||
5166 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
5167 | BFD_ASSERT (s != NULL); | |
5168 | { | |
5169 | struct elf_find_verdep_info sinfo; | |
5170 | ||
5171 | sinfo.output_bfd = output_bfd; | |
5172 | sinfo.info = info; | |
5173 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
5174 | if (sinfo.vers == 0) | |
5175 | sinfo.vers = 1; | |
5176 | sinfo.failed = FALSE; | |
5177 | ||
5178 | elf_link_hash_traverse (elf_hash_table (info), | |
5179 | _bfd_elf_link_find_version_dependencies, | |
5180 | &sinfo); | |
5181 | ||
5182 | if (elf_tdata (output_bfd)->verref == NULL) | |
5183 | _bfd_strip_section_from_output (info, s); | |
5184 | else | |
5185 | { | |
5186 | Elf_Internal_Verneed *t; | |
5187 | unsigned int size; | |
5188 | unsigned int crefs; | |
5189 | bfd_byte *p; | |
5190 | ||
5191 | /* Build the version definition section. */ | |
5192 | size = 0; | |
5193 | crefs = 0; | |
5194 | for (t = elf_tdata (output_bfd)->verref; | |
5195 | t != NULL; | |
5196 | t = t->vn_nextref) | |
5197 | { | |
5198 | Elf_Internal_Vernaux *a; | |
5199 | ||
5200 | size += sizeof (Elf_External_Verneed); | |
5201 | ++crefs; | |
5202 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5203 | size += sizeof (Elf_External_Vernaux); | |
5204 | } | |
5205 | ||
5206 | s->_raw_size = size; | |
5207 | s->contents = bfd_alloc (output_bfd, s->_raw_size); | |
5208 | if (s->contents == NULL) | |
5209 | return FALSE; | |
5210 | ||
5211 | p = s->contents; | |
5212 | for (t = elf_tdata (output_bfd)->verref; | |
5213 | t != NULL; | |
5214 | t = t->vn_nextref) | |
5215 | { | |
5216 | unsigned int caux; | |
5217 | Elf_Internal_Vernaux *a; | |
5218 | bfd_size_type indx; | |
5219 | ||
5220 | caux = 0; | |
5221 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5222 | ++caux; | |
5223 | ||
5224 | t->vn_version = VER_NEED_CURRENT; | |
5225 | t->vn_cnt = caux; | |
5226 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5227 | elf_dt_name (t->vn_bfd) != NULL | |
5228 | ? elf_dt_name (t->vn_bfd) | |
5229 | : basename (t->vn_bfd->filename), | |
5230 | FALSE); | |
5231 | if (indx == (bfd_size_type) -1) | |
5232 | return FALSE; | |
5233 | t->vn_file = indx; | |
5234 | t->vn_aux = sizeof (Elf_External_Verneed); | |
5235 | if (t->vn_nextref == NULL) | |
5236 | t->vn_next = 0; | |
5237 | else | |
5238 | t->vn_next = (sizeof (Elf_External_Verneed) | |
5239 | + caux * sizeof (Elf_External_Vernaux)); | |
5240 | ||
5241 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
5242 | (Elf_External_Verneed *) p); | |
5243 | p += sizeof (Elf_External_Verneed); | |
5244 | ||
5245 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5246 | { | |
5247 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
5248 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5249 | a->vna_nodename, FALSE); | |
5250 | if (indx == (bfd_size_type) -1) | |
5251 | return FALSE; | |
5252 | a->vna_name = indx; | |
5253 | if (a->vna_nextptr == NULL) | |
5254 | a->vna_next = 0; | |
5255 | else | |
5256 | a->vna_next = sizeof (Elf_External_Vernaux); | |
5257 | ||
5258 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
5259 | (Elf_External_Vernaux *) p); | |
5260 | p += sizeof (Elf_External_Vernaux); | |
5261 | } | |
5262 | } | |
5263 | ||
5264 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
5265 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
5266 | return FALSE; | |
5267 | ||
5268 | elf_tdata (output_bfd)->cverrefs = crefs; | |
5269 | } | |
5270 | } | |
5271 | ||
5272 | /* Assign dynsym indicies. In a shared library we generate a | |
5273 | section symbol for each output section, which come first. | |
5274 | Next come all of the back-end allocated local dynamic syms, | |
5275 | followed by the rest of the global symbols. */ | |
5276 | ||
5277 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info); | |
5278 | ||
5279 | /* Work out the size of the symbol version section. */ | |
5280 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
5281 | BFD_ASSERT (s != NULL); | |
5282 | if (dynsymcount == 0 | |
5283 | || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL)) | |
5284 | { | |
5285 | _bfd_strip_section_from_output (info, s); | |
5286 | /* The DYNSYMCOUNT might have changed if we were going to | |
5287 | output a dynamic symbol table entry for S. */ | |
5288 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info); | |
5289 | } | |
5290 | else | |
5291 | { | |
5292 | s->_raw_size = dynsymcount * sizeof (Elf_External_Versym); | |
5293 | s->contents = bfd_zalloc (output_bfd, s->_raw_size); | |
5294 | if (s->contents == NULL) | |
5295 | return FALSE; | |
5296 | ||
5297 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
5298 | return FALSE; | |
5299 | } | |
5300 | ||
5301 | /* Set the size of the .dynsym and .hash sections. We counted | |
5302 | the number of dynamic symbols in elf_link_add_object_symbols. | |
5303 | We will build the contents of .dynsym and .hash when we build | |
5304 | the final symbol table, because until then we do not know the | |
5305 | correct value to give the symbols. We built the .dynstr | |
5306 | section as we went along in elf_link_add_object_symbols. */ | |
5307 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
5308 | BFD_ASSERT (s != NULL); | |
5309 | s->_raw_size = dynsymcount * bed->s->sizeof_sym; | |
5310 | s->contents = bfd_alloc (output_bfd, s->_raw_size); | |
5311 | if (s->contents == NULL && s->_raw_size != 0) | |
5312 | return FALSE; | |
5313 | ||
5314 | if (dynsymcount != 0) | |
5315 | { | |
5316 | Elf_Internal_Sym isym; | |
5317 | ||
5318 | /* The first entry in .dynsym is a dummy symbol. */ | |
5319 | isym.st_value = 0; | |
5320 | isym.st_size = 0; | |
5321 | isym.st_name = 0; | |
5322 | isym.st_info = 0; | |
5323 | isym.st_other = 0; | |
5324 | isym.st_shndx = 0; | |
5325 | bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0); | |
5326 | } | |
5327 | ||
5328 | /* Compute the size of the hashing table. As a side effect this | |
5329 | computes the hash values for all the names we export. */ | |
5330 | bucketcount = compute_bucket_count (info); | |
5331 | ||
5332 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
5333 | BFD_ASSERT (s != NULL); | |
5334 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
5335 | s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
5336 | s->contents = bfd_zalloc (output_bfd, s->_raw_size); | |
5337 | if (s->contents == NULL) | |
5338 | return FALSE; | |
5339 | ||
5340 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
5341 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
5342 | s->contents + hash_entry_size); | |
5343 | ||
5344 | elf_hash_table (info)->bucketcount = bucketcount; | |
5345 | ||
5346 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5347 | BFD_ASSERT (s != NULL); | |
5348 | ||
4ad4eba5 | 5349 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a AM |
5350 | |
5351 | s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
5352 | ||
5353 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
5354 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
5355 | return FALSE; | |
5356 | } | |
5357 | ||
5358 | return TRUE; | |
5359 | } | |
c152c796 AM |
5360 | |
5361 | /* Final phase of ELF linker. */ | |
5362 | ||
5363 | /* A structure we use to avoid passing large numbers of arguments. */ | |
5364 | ||
5365 | struct elf_final_link_info | |
5366 | { | |
5367 | /* General link information. */ | |
5368 | struct bfd_link_info *info; | |
5369 | /* Output BFD. */ | |
5370 | bfd *output_bfd; | |
5371 | /* Symbol string table. */ | |
5372 | struct bfd_strtab_hash *symstrtab; | |
5373 | /* .dynsym section. */ | |
5374 | asection *dynsym_sec; | |
5375 | /* .hash section. */ | |
5376 | asection *hash_sec; | |
5377 | /* symbol version section (.gnu.version). */ | |
5378 | asection *symver_sec; | |
5379 | /* Buffer large enough to hold contents of any section. */ | |
5380 | bfd_byte *contents; | |
5381 | /* Buffer large enough to hold external relocs of any section. */ | |
5382 | void *external_relocs; | |
5383 | /* Buffer large enough to hold internal relocs of any section. */ | |
5384 | Elf_Internal_Rela *internal_relocs; | |
5385 | /* Buffer large enough to hold external local symbols of any input | |
5386 | BFD. */ | |
5387 | bfd_byte *external_syms; | |
5388 | /* And a buffer for symbol section indices. */ | |
5389 | Elf_External_Sym_Shndx *locsym_shndx; | |
5390 | /* Buffer large enough to hold internal local symbols of any input | |
5391 | BFD. */ | |
5392 | Elf_Internal_Sym *internal_syms; | |
5393 | /* Array large enough to hold a symbol index for each local symbol | |
5394 | of any input BFD. */ | |
5395 | long *indices; | |
5396 | /* Array large enough to hold a section pointer for each local | |
5397 | symbol of any input BFD. */ | |
5398 | asection **sections; | |
5399 | /* Buffer to hold swapped out symbols. */ | |
5400 | bfd_byte *symbuf; | |
5401 | /* And one for symbol section indices. */ | |
5402 | Elf_External_Sym_Shndx *symshndxbuf; | |
5403 | /* Number of swapped out symbols in buffer. */ | |
5404 | size_t symbuf_count; | |
5405 | /* Number of symbols which fit in symbuf. */ | |
5406 | size_t symbuf_size; | |
5407 | /* And same for symshndxbuf. */ | |
5408 | size_t shndxbuf_size; | |
5409 | }; | |
5410 | ||
5411 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
5412 | ||
5413 | struct elf_outext_info | |
5414 | { | |
5415 | bfd_boolean failed; | |
5416 | bfd_boolean localsyms; | |
5417 | struct elf_final_link_info *finfo; | |
5418 | }; | |
5419 | ||
5420 | /* When performing a relocatable link, the input relocations are | |
5421 | preserved. But, if they reference global symbols, the indices | |
5422 | referenced must be updated. Update all the relocations in | |
5423 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ | |
5424 | ||
5425 | static void | |
5426 | elf_link_adjust_relocs (bfd *abfd, | |
5427 | Elf_Internal_Shdr *rel_hdr, | |
5428 | unsigned int count, | |
5429 | struct elf_link_hash_entry **rel_hash) | |
5430 | { | |
5431 | unsigned int i; | |
5432 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5433 | bfd_byte *erela; | |
5434 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
5435 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
5436 | bfd_vma r_type_mask; | |
5437 | int r_sym_shift; | |
5438 | ||
5439 | if (rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
5440 | { | |
5441 | swap_in = bed->s->swap_reloc_in; | |
5442 | swap_out = bed->s->swap_reloc_out; | |
5443 | } | |
5444 | else if (rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
5445 | { | |
5446 | swap_in = bed->s->swap_reloca_in; | |
5447 | swap_out = bed->s->swap_reloca_out; | |
5448 | } | |
5449 | else | |
5450 | abort (); | |
5451 | ||
5452 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
5453 | abort (); | |
5454 | ||
5455 | if (bed->s->arch_size == 32) | |
5456 | { | |
5457 | r_type_mask = 0xff; | |
5458 | r_sym_shift = 8; | |
5459 | } | |
5460 | else | |
5461 | { | |
5462 | r_type_mask = 0xffffffff; | |
5463 | r_sym_shift = 32; | |
5464 | } | |
5465 | ||
5466 | erela = rel_hdr->contents; | |
5467 | for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) | |
5468 | { | |
5469 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
5470 | unsigned int j; | |
5471 | ||
5472 | if (*rel_hash == NULL) | |
5473 | continue; | |
5474 | ||
5475 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
5476 | ||
5477 | (*swap_in) (abfd, erela, irela); | |
5478 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
5479 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
5480 | | (irela[j].r_info & r_type_mask)); | |
5481 | (*swap_out) (abfd, irela, erela); | |
5482 | } | |
5483 | } | |
5484 | ||
5485 | struct elf_link_sort_rela | |
5486 | { | |
5487 | union { | |
5488 | bfd_vma offset; | |
5489 | bfd_vma sym_mask; | |
5490 | } u; | |
5491 | enum elf_reloc_type_class type; | |
5492 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
5493 | Elf_Internal_Rela rela[1]; | |
5494 | }; | |
5495 | ||
5496 | static int | |
5497 | elf_link_sort_cmp1 (const void *A, const void *B) | |
5498 | { | |
5499 | const struct elf_link_sort_rela *a = A; | |
5500 | const struct elf_link_sort_rela *b = B; | |
5501 | int relativea, relativeb; | |
5502 | ||
5503 | relativea = a->type == reloc_class_relative; | |
5504 | relativeb = b->type == reloc_class_relative; | |
5505 | ||
5506 | if (relativea < relativeb) | |
5507 | return 1; | |
5508 | if (relativea > relativeb) | |
5509 | return -1; | |
5510 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
5511 | return -1; | |
5512 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
5513 | return 1; | |
5514 | if (a->rela->r_offset < b->rela->r_offset) | |
5515 | return -1; | |
5516 | if (a->rela->r_offset > b->rela->r_offset) | |
5517 | return 1; | |
5518 | return 0; | |
5519 | } | |
5520 | ||
5521 | static int | |
5522 | elf_link_sort_cmp2 (const void *A, const void *B) | |
5523 | { | |
5524 | const struct elf_link_sort_rela *a = A; | |
5525 | const struct elf_link_sort_rela *b = B; | |
5526 | int copya, copyb; | |
5527 | ||
5528 | if (a->u.offset < b->u.offset) | |
5529 | return -1; | |
5530 | if (a->u.offset > b->u.offset) | |
5531 | return 1; | |
5532 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); | |
5533 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
5534 | if (copya < copyb) | |
5535 | return -1; | |
5536 | if (copya > copyb) | |
5537 | return 1; | |
5538 | if (a->rela->r_offset < b->rela->r_offset) | |
5539 | return -1; | |
5540 | if (a->rela->r_offset > b->rela->r_offset) | |
5541 | return 1; | |
5542 | return 0; | |
5543 | } | |
5544 | ||
5545 | static size_t | |
5546 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
5547 | { | |
5548 | asection *reldyn; | |
5549 | bfd_size_type count, size; | |
5550 | size_t i, ret, sort_elt, ext_size; | |
5551 | bfd_byte *sort, *s_non_relative, *p; | |
5552 | struct elf_link_sort_rela *sq; | |
5553 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5554 | int i2e = bed->s->int_rels_per_ext_rel; | |
5555 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
5556 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
5557 | struct bfd_link_order *lo; | |
5558 | bfd_vma r_sym_mask; | |
5559 | ||
5560 | reldyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
5561 | if (reldyn == NULL || reldyn->_raw_size == 0) | |
5562 | { | |
5563 | reldyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
5564 | if (reldyn == NULL || reldyn->_raw_size == 0) | |
5565 | return 0; | |
5566 | ext_size = bed->s->sizeof_rel; | |
5567 | swap_in = bed->s->swap_reloc_in; | |
5568 | swap_out = bed->s->swap_reloc_out; | |
5569 | } | |
5570 | else | |
5571 | { | |
5572 | ext_size = bed->s->sizeof_rela; | |
5573 | swap_in = bed->s->swap_reloca_in; | |
5574 | swap_out = bed->s->swap_reloca_out; | |
5575 | } | |
5576 | count = reldyn->_raw_size / ext_size; | |
5577 | ||
5578 | size = 0; | |
5579 | for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next) | |
5580 | if (lo->type == bfd_indirect_link_order) | |
5581 | { | |
5582 | asection *o = lo->u.indirect.section; | |
5583 | size += o->_raw_size; | |
5584 | } | |
5585 | ||
5586 | if (size != reldyn->_raw_size) | |
5587 | return 0; | |
5588 | ||
5589 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
5590 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
5591 | sort = bfd_zmalloc (sort_elt * count); | |
5592 | if (sort == NULL) | |
5593 | { | |
5594 | (*info->callbacks->warning) | |
5595 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
5596 | return 0; | |
5597 | } | |
5598 | ||
5599 | if (bed->s->arch_size == 32) | |
5600 | r_sym_mask = ~(bfd_vma) 0xff; | |
5601 | else | |
5602 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
5603 | ||
5604 | for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next) | |
5605 | if (lo->type == bfd_indirect_link_order) | |
5606 | { | |
5607 | bfd_byte *erel, *erelend; | |
5608 | asection *o = lo->u.indirect.section; | |
5609 | ||
5610 | erel = o->contents; | |
5611 | erelend = o->contents + o->_raw_size; | |
5612 | p = sort + o->output_offset / ext_size * sort_elt; | |
5613 | while (erel < erelend) | |
5614 | { | |
5615 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
5616 | (*swap_in) (abfd, erel, s->rela); | |
5617 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
5618 | s->u.sym_mask = r_sym_mask; | |
5619 | p += sort_elt; | |
5620 | erel += ext_size; | |
5621 | } | |
5622 | } | |
5623 | ||
5624 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
5625 | ||
5626 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
5627 | { | |
5628 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
5629 | if (s->type != reloc_class_relative) | |
5630 | break; | |
5631 | } | |
5632 | ret = i; | |
5633 | s_non_relative = p; | |
5634 | ||
5635 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
5636 | for (; i < count; i++, p += sort_elt) | |
5637 | { | |
5638 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
5639 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
5640 | sq = sp; | |
5641 | sp->u.offset = sq->rela->r_offset; | |
5642 | } | |
5643 | ||
5644 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
5645 | ||
5646 | for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next) | |
5647 | if (lo->type == bfd_indirect_link_order) | |
5648 | { | |
5649 | bfd_byte *erel, *erelend; | |
5650 | asection *o = lo->u.indirect.section; | |
5651 | ||
5652 | erel = o->contents; | |
5653 | erelend = o->contents + o->_raw_size; | |
5654 | p = sort + o->output_offset / ext_size * sort_elt; | |
5655 | while (erel < erelend) | |
5656 | { | |
5657 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
5658 | (*swap_out) (abfd, s->rela, erel); | |
5659 | p += sort_elt; | |
5660 | erel += ext_size; | |
5661 | } | |
5662 | } | |
5663 | ||
5664 | free (sort); | |
5665 | *psec = reldyn; | |
5666 | return ret; | |
5667 | } | |
5668 | ||
5669 | /* Flush the output symbols to the file. */ | |
5670 | ||
5671 | static bfd_boolean | |
5672 | elf_link_flush_output_syms (struct elf_final_link_info *finfo, | |
5673 | const struct elf_backend_data *bed) | |
5674 | { | |
5675 | if (finfo->symbuf_count > 0) | |
5676 | { | |
5677 | Elf_Internal_Shdr *hdr; | |
5678 | file_ptr pos; | |
5679 | bfd_size_type amt; | |
5680 | ||
5681 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
5682 | pos = hdr->sh_offset + hdr->sh_size; | |
5683 | amt = finfo->symbuf_count * bed->s->sizeof_sym; | |
5684 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
5685 | || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) | |
5686 | return FALSE; | |
5687 | ||
5688 | hdr->sh_size += amt; | |
5689 | finfo->symbuf_count = 0; | |
5690 | } | |
5691 | ||
5692 | return TRUE; | |
5693 | } | |
5694 | ||
5695 | /* Add a symbol to the output symbol table. */ | |
5696 | ||
5697 | static bfd_boolean | |
5698 | elf_link_output_sym (struct elf_final_link_info *finfo, | |
5699 | const char *name, | |
5700 | Elf_Internal_Sym *elfsym, | |
5701 | asection *input_sec, | |
5702 | struct elf_link_hash_entry *h) | |
5703 | { | |
5704 | bfd_byte *dest; | |
5705 | Elf_External_Sym_Shndx *destshndx; | |
5706 | bfd_boolean (*output_symbol_hook) | |
5707 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, | |
5708 | struct elf_link_hash_entry *); | |
5709 | const struct elf_backend_data *bed; | |
5710 | ||
5711 | bed = get_elf_backend_data (finfo->output_bfd); | |
5712 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; | |
5713 | if (output_symbol_hook != NULL) | |
5714 | { | |
5715 | if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h)) | |
5716 | return FALSE; | |
5717 | } | |
5718 | ||
5719 | if (name == NULL || *name == '\0') | |
5720 | elfsym->st_name = 0; | |
5721 | else if (input_sec->flags & SEC_EXCLUDE) | |
5722 | elfsym->st_name = 0; | |
5723 | else | |
5724 | { | |
5725 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
5726 | name, TRUE, FALSE); | |
5727 | if (elfsym->st_name == (unsigned long) -1) | |
5728 | return FALSE; | |
5729 | } | |
5730 | ||
5731 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
5732 | { | |
5733 | if (! elf_link_flush_output_syms (finfo, bed)) | |
5734 | return FALSE; | |
5735 | } | |
5736 | ||
5737 | dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; | |
5738 | destshndx = finfo->symshndxbuf; | |
5739 | if (destshndx != NULL) | |
5740 | { | |
5741 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
5742 | { | |
5743 | bfd_size_type amt; | |
5744 | ||
5745 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); | |
5746 | finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2); | |
5747 | if (destshndx == NULL) | |
5748 | return FALSE; | |
5749 | memset ((char *) destshndx + amt, 0, amt); | |
5750 | finfo->shndxbuf_size *= 2; | |
5751 | } | |
5752 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
5753 | } | |
5754 | ||
5755 | bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); | |
5756 | finfo->symbuf_count += 1; | |
5757 | bfd_get_symcount (finfo->output_bfd) += 1; | |
5758 | ||
5759 | return TRUE; | |
5760 | } | |
5761 | ||
5762 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in | |
5763 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
5764 | versioned symbol that would normally require an explicit version. | |
5765 | We also handle the case that a DSO references a hidden symbol | |
5766 | which may be satisfied by a versioned symbol in another DSO. */ | |
5767 | ||
5768 | static bfd_boolean | |
5769 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
5770 | const struct elf_backend_data *bed, | |
5771 | struct elf_link_hash_entry *h) | |
5772 | { | |
5773 | bfd *abfd; | |
5774 | struct elf_link_loaded_list *loaded; | |
5775 | ||
5776 | if (!is_elf_hash_table (info->hash)) | |
5777 | return FALSE; | |
5778 | ||
5779 | switch (h->root.type) | |
5780 | { | |
5781 | default: | |
5782 | abfd = NULL; | |
5783 | break; | |
5784 | ||
5785 | case bfd_link_hash_undefined: | |
5786 | case bfd_link_hash_undefweak: | |
5787 | abfd = h->root.u.undef.abfd; | |
5788 | if ((abfd->flags & DYNAMIC) == 0 | |
5789 | || elf_dyn_lib_class (abfd) != DYN_DT_NEEDED) | |
5790 | return FALSE; | |
5791 | break; | |
5792 | ||
5793 | case bfd_link_hash_defined: | |
5794 | case bfd_link_hash_defweak: | |
5795 | abfd = h->root.u.def.section->owner; | |
5796 | break; | |
5797 | ||
5798 | case bfd_link_hash_common: | |
5799 | abfd = h->root.u.c.p->section->owner; | |
5800 | break; | |
5801 | } | |
5802 | BFD_ASSERT (abfd != NULL); | |
5803 | ||
5804 | for (loaded = elf_hash_table (info)->loaded; | |
5805 | loaded != NULL; | |
5806 | loaded = loaded->next) | |
5807 | { | |
5808 | bfd *input; | |
5809 | Elf_Internal_Shdr *hdr; | |
5810 | bfd_size_type symcount; | |
5811 | bfd_size_type extsymcount; | |
5812 | bfd_size_type extsymoff; | |
5813 | Elf_Internal_Shdr *versymhdr; | |
5814 | Elf_Internal_Sym *isym; | |
5815 | Elf_Internal_Sym *isymend; | |
5816 | Elf_Internal_Sym *isymbuf; | |
5817 | Elf_External_Versym *ever; | |
5818 | Elf_External_Versym *extversym; | |
5819 | ||
5820 | input = loaded->abfd; | |
5821 | ||
5822 | /* We check each DSO for a possible hidden versioned definition. */ | |
5823 | if (input == abfd | |
5824 | || (input->flags & DYNAMIC) == 0 | |
5825 | || elf_dynversym (input) == 0) | |
5826 | continue; | |
5827 | ||
5828 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
5829 | ||
5830 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
5831 | if (elf_bad_symtab (input)) | |
5832 | { | |
5833 | extsymcount = symcount; | |
5834 | extsymoff = 0; | |
5835 | } | |
5836 | else | |
5837 | { | |
5838 | extsymcount = symcount - hdr->sh_info; | |
5839 | extsymoff = hdr->sh_info; | |
5840 | } | |
5841 | ||
5842 | if (extsymcount == 0) | |
5843 | continue; | |
5844 | ||
5845 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
5846 | NULL, NULL, NULL); | |
5847 | if (isymbuf == NULL) | |
5848 | return FALSE; | |
5849 | ||
5850 | /* Read in any version definitions. */ | |
5851 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
5852 | extversym = bfd_malloc (versymhdr->sh_size); | |
5853 | if (extversym == NULL) | |
5854 | goto error_ret; | |
5855 | ||
5856 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
5857 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
5858 | != versymhdr->sh_size)) | |
5859 | { | |
5860 | free (extversym); | |
5861 | error_ret: | |
5862 | free (isymbuf); | |
5863 | return FALSE; | |
5864 | } | |
5865 | ||
5866 | ever = extversym + extsymoff; | |
5867 | isymend = isymbuf + extsymcount; | |
5868 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
5869 | { | |
5870 | const char *name; | |
5871 | Elf_Internal_Versym iver; | |
5872 | unsigned short version_index; | |
5873 | ||
5874 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
5875 | || isym->st_shndx == SHN_UNDEF) | |
5876 | continue; | |
5877 | ||
5878 | name = bfd_elf_string_from_elf_section (input, | |
5879 | hdr->sh_link, | |
5880 | isym->st_name); | |
5881 | if (strcmp (name, h->root.root.string) != 0) | |
5882 | continue; | |
5883 | ||
5884 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
5885 | ||
5886 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
5887 | { | |
5888 | /* If we have a non-hidden versioned sym, then it should | |
5889 | have provided a definition for the undefined sym. */ | |
5890 | abort (); | |
5891 | } | |
5892 | ||
5893 | version_index = iver.vs_vers & VERSYM_VERSION; | |
5894 | if (version_index == 1 || version_index == 2) | |
5895 | { | |
5896 | /* This is the base or first version. We can use it. */ | |
5897 | free (extversym); | |
5898 | free (isymbuf); | |
5899 | return TRUE; | |
5900 | } | |
5901 | } | |
5902 | ||
5903 | free (extversym); | |
5904 | free (isymbuf); | |
5905 | } | |
5906 | ||
5907 | return FALSE; | |
5908 | } | |
5909 | ||
5910 | /* Add an external symbol to the symbol table. This is called from | |
5911 | the hash table traversal routine. When generating a shared object, | |
5912 | we go through the symbol table twice. The first time we output | |
5913 | anything that might have been forced to local scope in a version | |
5914 | script. The second time we output the symbols that are still | |
5915 | global symbols. */ | |
5916 | ||
5917 | static bfd_boolean | |
5918 | elf_link_output_extsym (struct elf_link_hash_entry *h, void *data) | |
5919 | { | |
5920 | struct elf_outext_info *eoinfo = data; | |
5921 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
5922 | bfd_boolean strip; | |
5923 | Elf_Internal_Sym sym; | |
5924 | asection *input_sec; | |
5925 | const struct elf_backend_data *bed; | |
5926 | ||
5927 | if (h->root.type == bfd_link_hash_warning) | |
5928 | { | |
5929 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5930 | if (h->root.type == bfd_link_hash_new) | |
5931 | return TRUE; | |
5932 | } | |
5933 | ||
5934 | /* Decide whether to output this symbol in this pass. */ | |
5935 | if (eoinfo->localsyms) | |
5936 | { | |
5937 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
5938 | return TRUE; | |
5939 | } | |
5940 | else | |
5941 | { | |
5942 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) | |
5943 | return TRUE; | |
5944 | } | |
5945 | ||
5946 | bed = get_elf_backend_data (finfo->output_bfd); | |
5947 | ||
5948 | /* If we have an undefined symbol reference here then it must have | |
5949 | come from a shared library that is being linked in. (Undefined | |
5950 | references in regular files have already been handled). If we | |
5951 | are reporting errors for this situation then do so now. */ | |
5952 | if (h->root.type == bfd_link_hash_undefined | |
5953 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0 | |
5954 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0 | |
5955 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h) | |
5956 | && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
5957 | { | |
5958 | if (! ((*finfo->info->callbacks->undefined_symbol) | |
5959 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
5960 | NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) | |
5961 | { | |
5962 | eoinfo->failed = TRUE; | |
5963 | return FALSE; | |
5964 | } | |
5965 | } | |
5966 | ||
5967 | /* We should also warn if a forced local symbol is referenced from | |
5968 | shared libraries. */ | |
5969 | if (! finfo->info->relocatable | |
5970 | && (! finfo->info->shared) | |
5971 | && (h->elf_link_hash_flags | |
5972 | & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK)) | |
5973 | == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC) | |
5974 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) | |
5975 | { | |
5976 | (*_bfd_error_handler) | |
5977 | (_("%s: %s symbol `%s' in %s is referenced by DSO"), | |
5978 | bfd_get_filename (finfo->output_bfd), | |
5979 | ELF_ST_VISIBILITY (h->other) == STV_INTERNAL | |
5980 | ? "internal" | |
5981 | : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
5982 | ? "hidden" : "local", | |
5983 | h->root.root.string, | |
5984 | bfd_archive_filename (h->root.u.def.section->owner)); | |
5985 | eoinfo->failed = TRUE; | |
5986 | return FALSE; | |
5987 | } | |
5988 | ||
5989 | /* We don't want to output symbols that have never been mentioned by | |
5990 | a regular file, or that we have been told to strip. However, if | |
5991 | h->indx is set to -2, the symbol is used by a reloc and we must | |
5992 | output it. */ | |
5993 | if (h->indx == -2) | |
5994 | strip = FALSE; | |
5995 | else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
5996 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0) | |
5997 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
5998 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0) | |
5999 | strip = TRUE; | |
6000 | else if (finfo->info->strip == strip_all) | |
6001 | strip = TRUE; | |
6002 | else if (finfo->info->strip == strip_some | |
6003 | && bfd_hash_lookup (finfo->info->keep_hash, | |
6004 | h->root.root.string, FALSE, FALSE) == NULL) | |
6005 | strip = TRUE; | |
6006 | else if (finfo->info->strip_discarded | |
6007 | && (h->root.type == bfd_link_hash_defined | |
6008 | || h->root.type == bfd_link_hash_defweak) | |
6009 | && elf_discarded_section (h->root.u.def.section)) | |
6010 | strip = TRUE; | |
6011 | else | |
6012 | strip = FALSE; | |
6013 | ||
6014 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
6015 | nothing else to do unless it is a forced local symbol. */ | |
6016 | if (strip | |
6017 | && h->dynindx == -1 | |
6018 | && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
6019 | return TRUE; | |
6020 | ||
6021 | sym.st_value = 0; | |
6022 | sym.st_size = h->size; | |
6023 | sym.st_other = h->other; | |
6024 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) | |
6025 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); | |
6026 | else if (h->root.type == bfd_link_hash_undefweak | |
6027 | || h->root.type == bfd_link_hash_defweak) | |
6028 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
6029 | else | |
6030 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
6031 | ||
6032 | switch (h->root.type) | |
6033 | { | |
6034 | default: | |
6035 | case bfd_link_hash_new: | |
6036 | case bfd_link_hash_warning: | |
6037 | abort (); | |
6038 | return FALSE; | |
6039 | ||
6040 | case bfd_link_hash_undefined: | |
6041 | case bfd_link_hash_undefweak: | |
6042 | input_sec = bfd_und_section_ptr; | |
6043 | sym.st_shndx = SHN_UNDEF; | |
6044 | break; | |
6045 | ||
6046 | case bfd_link_hash_defined: | |
6047 | case bfd_link_hash_defweak: | |
6048 | { | |
6049 | input_sec = h->root.u.def.section; | |
6050 | if (input_sec->output_section != NULL) | |
6051 | { | |
6052 | sym.st_shndx = | |
6053 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
6054 | input_sec->output_section); | |
6055 | if (sym.st_shndx == SHN_BAD) | |
6056 | { | |
6057 | (*_bfd_error_handler) | |
6058 | (_("%s: could not find output section %s for input section %s"), | |
6059 | bfd_get_filename (finfo->output_bfd), | |
6060 | input_sec->output_section->name, | |
6061 | input_sec->name); | |
6062 | eoinfo->failed = TRUE; | |
6063 | return FALSE; | |
6064 | } | |
6065 | ||
6066 | /* ELF symbols in relocatable files are section relative, | |
6067 | but in nonrelocatable files they are virtual | |
6068 | addresses. */ | |
6069 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
6070 | if (! finfo->info->relocatable) | |
6071 | { | |
6072 | sym.st_value += input_sec->output_section->vma; | |
6073 | if (h->type == STT_TLS) | |
6074 | { | |
6075 | /* STT_TLS symbols are relative to PT_TLS segment | |
6076 | base. */ | |
6077 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
6078 | sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
6079 | } | |
6080 | } | |
6081 | } | |
6082 | else | |
6083 | { | |
6084 | BFD_ASSERT (input_sec->owner == NULL | |
6085 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
6086 | sym.st_shndx = SHN_UNDEF; | |
6087 | input_sec = bfd_und_section_ptr; | |
6088 | } | |
6089 | } | |
6090 | break; | |
6091 | ||
6092 | case bfd_link_hash_common: | |
6093 | input_sec = h->root.u.c.p->section; | |
6094 | sym.st_shndx = SHN_COMMON; | |
6095 | sym.st_value = 1 << h->root.u.c.p->alignment_power; | |
6096 | break; | |
6097 | ||
6098 | case bfd_link_hash_indirect: | |
6099 | /* These symbols are created by symbol versioning. They point | |
6100 | to the decorated version of the name. For example, if the | |
6101 | symbol foo@@GNU_1.2 is the default, which should be used when | |
6102 | foo is used with no version, then we add an indirect symbol | |
6103 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
6104 | since the indirected symbol is already in the hash table. */ | |
6105 | return TRUE; | |
6106 | } | |
6107 | ||
6108 | /* Give the processor backend a chance to tweak the symbol value, | |
6109 | and also to finish up anything that needs to be done for this | |
6110 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
6111 | forced local syms when non-shared is due to a historical quirk. */ | |
6112 | if ((h->dynindx != -1 | |
6113 | || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) | |
6114 | && ((finfo->info->shared | |
6115 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
6116 | || h->root.type != bfd_link_hash_undefweak)) | |
6117 | || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
6118 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
6119 | { | |
6120 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
6121 | (finfo->output_bfd, finfo->info, h, &sym))) | |
6122 | { | |
6123 | eoinfo->failed = TRUE; | |
6124 | return FALSE; | |
6125 | } | |
6126 | } | |
6127 | ||
6128 | /* If we are marking the symbol as undefined, and there are no | |
6129 | non-weak references to this symbol from a regular object, then | |
6130 | mark the symbol as weak undefined; if there are non-weak | |
6131 | references, mark the symbol as strong. We can't do this earlier, | |
6132 | because it might not be marked as undefined until the | |
6133 | finish_dynamic_symbol routine gets through with it. */ | |
6134 | if (sym.st_shndx == SHN_UNDEF | |
6135 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0 | |
6136 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL | |
6137 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
6138 | { | |
6139 | int bindtype; | |
6140 | ||
6141 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0) | |
6142 | bindtype = STB_GLOBAL; | |
6143 | else | |
6144 | bindtype = STB_WEAK; | |
6145 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); | |
6146 | } | |
6147 | ||
6148 | /* If a non-weak symbol with non-default visibility is not defined | |
6149 | locally, it is a fatal error. */ | |
6150 | if (! finfo->info->relocatable | |
6151 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT | |
6152 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
6153 | && h->root.type == bfd_link_hash_undefined | |
6154 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
6155 | { | |
6156 | (*_bfd_error_handler) | |
6157 | (_("%s: %s symbol `%s' isn't defined"), | |
6158 | bfd_get_filename (finfo->output_bfd), | |
6159 | ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED | |
6160 | ? "protected" | |
6161 | : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL | |
6162 | ? "internal" : "hidden", | |
6163 | h->root.root.string); | |
6164 | eoinfo->failed = TRUE; | |
6165 | return FALSE; | |
6166 | } | |
6167 | ||
6168 | /* If this symbol should be put in the .dynsym section, then put it | |
6169 | there now. We already know the symbol index. We also fill in | |
6170 | the entry in the .hash section. */ | |
6171 | if (h->dynindx != -1 | |
6172 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
6173 | { | |
6174 | size_t bucketcount; | |
6175 | size_t bucket; | |
6176 | size_t hash_entry_size; | |
6177 | bfd_byte *bucketpos; | |
6178 | bfd_vma chain; | |
6179 | bfd_byte *esym; | |
6180 | ||
6181 | sym.st_name = h->dynstr_index; | |
6182 | esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; | |
6183 | bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); | |
6184 | ||
6185 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
6186 | bucket = h->elf_hash_value % bucketcount; | |
6187 | hash_entry_size | |
6188 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; | |
6189 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents | |
6190 | + (bucket + 2) * hash_entry_size); | |
6191 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
6192 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); | |
6193 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, | |
6194 | ((bfd_byte *) finfo->hash_sec->contents | |
6195 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
6196 | ||
6197 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
6198 | { | |
6199 | Elf_Internal_Versym iversym; | |
6200 | Elf_External_Versym *eversym; | |
6201 | ||
6202 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
6203 | { | |
6204 | if (h->verinfo.verdef == NULL) | |
6205 | iversym.vs_vers = 0; | |
6206 | else | |
6207 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
6208 | } | |
6209 | else | |
6210 | { | |
6211 | if (h->verinfo.vertree == NULL) | |
6212 | iversym.vs_vers = 1; | |
6213 | else | |
6214 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
6215 | } | |
6216 | ||
6217 | if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0) | |
6218 | iversym.vs_vers |= VERSYM_HIDDEN; | |
6219 | ||
6220 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; | |
6221 | eversym += h->dynindx; | |
6222 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
6223 | } | |
6224 | } | |
6225 | ||
6226 | /* If we're stripping it, then it was just a dynamic symbol, and | |
6227 | there's nothing else to do. */ | |
6228 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) | |
6229 | return TRUE; | |
6230 | ||
6231 | h->indx = bfd_get_symcount (finfo->output_bfd); | |
6232 | ||
6233 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h)) | |
6234 | { | |
6235 | eoinfo->failed = TRUE; | |
6236 | return FALSE; | |
6237 | } | |
6238 | ||
6239 | return TRUE; | |
6240 | } | |
6241 | ||
6242 | static bfd_boolean | |
6243 | elf_section_ignore_discarded_relocs (asection *sec) | |
6244 | { | |
6245 | const struct elf_backend_data *bed; | |
6246 | ||
6247 | switch (sec->sec_info_type) | |
6248 | { | |
6249 | case ELF_INFO_TYPE_STABS: | |
6250 | case ELF_INFO_TYPE_EH_FRAME: | |
6251 | return TRUE; | |
6252 | default: | |
6253 | break; | |
6254 | } | |
6255 | ||
6256 | bed = get_elf_backend_data (sec->owner); | |
6257 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
6258 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
6259 | return TRUE; | |
6260 | ||
6261 | return FALSE; | |
6262 | } | |
6263 | ||
6264 | /* Link an input file into the linker output file. This function | |
6265 | handles all the sections and relocations of the input file at once. | |
6266 | This is so that we only have to read the local symbols once, and | |
6267 | don't have to keep them in memory. */ | |
6268 | ||
6269 | static bfd_boolean | |
6270 | elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) | |
6271 | { | |
6272 | bfd_boolean (*relocate_section) | |
6273 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
6274 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
6275 | bfd *output_bfd; | |
6276 | Elf_Internal_Shdr *symtab_hdr; | |
6277 | size_t locsymcount; | |
6278 | size_t extsymoff; | |
6279 | Elf_Internal_Sym *isymbuf; | |
6280 | Elf_Internal_Sym *isym; | |
6281 | Elf_Internal_Sym *isymend; | |
6282 | long *pindex; | |
6283 | asection **ppsection; | |
6284 | asection *o; | |
6285 | const struct elf_backend_data *bed; | |
6286 | bfd_boolean emit_relocs; | |
6287 | struct elf_link_hash_entry **sym_hashes; | |
6288 | ||
6289 | output_bfd = finfo->output_bfd; | |
6290 | bed = get_elf_backend_data (output_bfd); | |
6291 | relocate_section = bed->elf_backend_relocate_section; | |
6292 | ||
6293 | /* If this is a dynamic object, we don't want to do anything here: | |
6294 | we don't want the local symbols, and we don't want the section | |
6295 | contents. */ | |
6296 | if ((input_bfd->flags & DYNAMIC) != 0) | |
6297 | return TRUE; | |
6298 | ||
6299 | emit_relocs = (finfo->info->relocatable | |
6300 | || finfo->info->emitrelocations | |
6301 | || bed->elf_backend_emit_relocs); | |
6302 | ||
6303 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
6304 | if (elf_bad_symtab (input_bfd)) | |
6305 | { | |
6306 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
6307 | extsymoff = 0; | |
6308 | } | |
6309 | else | |
6310 | { | |
6311 | locsymcount = symtab_hdr->sh_info; | |
6312 | extsymoff = symtab_hdr->sh_info; | |
6313 | } | |
6314 | ||
6315 | /* Read the local symbols. */ | |
6316 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
6317 | if (isymbuf == NULL && locsymcount != 0) | |
6318 | { | |
6319 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
6320 | finfo->internal_syms, | |
6321 | finfo->external_syms, | |
6322 | finfo->locsym_shndx); | |
6323 | if (isymbuf == NULL) | |
6324 | return FALSE; | |
6325 | } | |
6326 | ||
6327 | /* Find local symbol sections and adjust values of symbols in | |
6328 | SEC_MERGE sections. Write out those local symbols we know are | |
6329 | going into the output file. */ | |
6330 | isymend = isymbuf + locsymcount; | |
6331 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
6332 | isym < isymend; | |
6333 | isym++, pindex++, ppsection++) | |
6334 | { | |
6335 | asection *isec; | |
6336 | const char *name; | |
6337 | Elf_Internal_Sym osym; | |
6338 | ||
6339 | *pindex = -1; | |
6340 | ||
6341 | if (elf_bad_symtab (input_bfd)) | |
6342 | { | |
6343 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
6344 | { | |
6345 | *ppsection = NULL; | |
6346 | continue; | |
6347 | } | |
6348 | } | |
6349 | ||
6350 | if (isym->st_shndx == SHN_UNDEF) | |
6351 | isec = bfd_und_section_ptr; | |
6352 | else if (isym->st_shndx < SHN_LORESERVE | |
6353 | || isym->st_shndx > SHN_HIRESERVE) | |
6354 | { | |
6355 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); | |
6356 | if (isec | |
6357 | && isec->sec_info_type == ELF_INFO_TYPE_MERGE | |
6358 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) | |
6359 | isym->st_value = | |
6360 | _bfd_merged_section_offset (output_bfd, &isec, | |
6361 | elf_section_data (isec)->sec_info, | |
6362 | isym->st_value, 0); | |
6363 | } | |
6364 | else if (isym->st_shndx == SHN_ABS) | |
6365 | isec = bfd_abs_section_ptr; | |
6366 | else if (isym->st_shndx == SHN_COMMON) | |
6367 | isec = bfd_com_section_ptr; | |
6368 | else | |
6369 | { | |
6370 | /* Who knows? */ | |
6371 | isec = NULL; | |
6372 | } | |
6373 | ||
6374 | *ppsection = isec; | |
6375 | ||
6376 | /* Don't output the first, undefined, symbol. */ | |
6377 | if (ppsection == finfo->sections) | |
6378 | continue; | |
6379 | ||
6380 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
6381 | { | |
6382 | /* We never output section symbols. Instead, we use the | |
6383 | section symbol of the corresponding section in the output | |
6384 | file. */ | |
6385 | continue; | |
6386 | } | |
6387 | ||
6388 | /* If we are stripping all symbols, we don't want to output this | |
6389 | one. */ | |
6390 | if (finfo->info->strip == strip_all) | |
6391 | continue; | |
6392 | ||
6393 | /* If we are discarding all local symbols, we don't want to | |
6394 | output this one. If we are generating a relocatable output | |
6395 | file, then some of the local symbols may be required by | |
6396 | relocs; we output them below as we discover that they are | |
6397 | needed. */ | |
6398 | if (finfo->info->discard == discard_all) | |
6399 | continue; | |
6400 | ||
6401 | /* If this symbol is defined in a section which we are | |
6402 | discarding, we don't need to keep it, but note that | |
6403 | linker_mark is only reliable for sections that have contents. | |
6404 | For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE | |
6405 | as well as linker_mark. */ | |
6406 | if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
6407 | && isec != NULL | |
6408 | && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0) | |
6409 | || (! finfo->info->relocatable | |
6410 | && (isec->flags & SEC_EXCLUDE) != 0))) | |
6411 | continue; | |
6412 | ||
6413 | /* Get the name of the symbol. */ | |
6414 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
6415 | isym->st_name); | |
6416 | if (name == NULL) | |
6417 | return FALSE; | |
6418 | ||
6419 | /* See if we are discarding symbols with this name. */ | |
6420 | if ((finfo->info->strip == strip_some | |
6421 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) | |
6422 | == NULL)) | |
6423 | || (((finfo->info->discard == discard_sec_merge | |
6424 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) | |
6425 | || finfo->info->discard == discard_l) | |
6426 | && bfd_is_local_label_name (input_bfd, name))) | |
6427 | continue; | |
6428 | ||
6429 | /* If we get here, we are going to output this symbol. */ | |
6430 | ||
6431 | osym = *isym; | |
6432 | ||
6433 | /* Adjust the section index for the output file. */ | |
6434 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
6435 | isec->output_section); | |
6436 | if (osym.st_shndx == SHN_BAD) | |
6437 | return FALSE; | |
6438 | ||
6439 | *pindex = bfd_get_symcount (output_bfd); | |
6440 | ||
6441 | /* ELF symbols in relocatable files are section relative, but | |
6442 | in executable files they are virtual addresses. Note that | |
6443 | this code assumes that all ELF sections have an associated | |
6444 | BFD section with a reasonable value for output_offset; below | |
6445 | we assume that they also have a reasonable value for | |
6446 | output_section. Any special sections must be set up to meet | |
6447 | these requirements. */ | |
6448 | osym.st_value += isec->output_offset; | |
6449 | if (! finfo->info->relocatable) | |
6450 | { | |
6451 | osym.st_value += isec->output_section->vma; | |
6452 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
6453 | { | |
6454 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
6455 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
6456 | osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
6457 | } | |
6458 | } | |
6459 | ||
6460 | if (! elf_link_output_sym (finfo, name, &osym, isec, NULL)) | |
6461 | return FALSE; | |
6462 | } | |
6463 | ||
6464 | /* Relocate the contents of each section. */ | |
6465 | sym_hashes = elf_sym_hashes (input_bfd); | |
6466 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
6467 | { | |
6468 | bfd_byte *contents; | |
6469 | ||
6470 | if (! o->linker_mark) | |
6471 | { | |
6472 | /* This section was omitted from the link. */ | |
6473 | continue; | |
6474 | } | |
6475 | ||
6476 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
6477 | || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0)) | |
6478 | continue; | |
6479 | ||
6480 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
6481 | { | |
6482 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
6483 | or somesuch. */ | |
6484 | continue; | |
6485 | } | |
6486 | ||
6487 | /* Get the contents of the section. They have been cached by a | |
6488 | relaxation routine. Note that o is a section in an input | |
6489 | file, so the contents field will not have been set by any of | |
6490 | the routines which work on output files. */ | |
6491 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
6492 | contents = elf_section_data (o)->this_hdr.contents; | |
6493 | else | |
6494 | { | |
6495 | contents = finfo->contents; | |
6496 | if (! bfd_get_section_contents (input_bfd, o, contents, 0, | |
6497 | o->_raw_size)) | |
6498 | return FALSE; | |
6499 | } | |
6500 | ||
6501 | if ((o->flags & SEC_RELOC) != 0) | |
6502 | { | |
6503 | Elf_Internal_Rela *internal_relocs; | |
6504 | bfd_vma r_type_mask; | |
6505 | int r_sym_shift; | |
6506 | ||
6507 | /* Get the swapped relocs. */ | |
6508 | internal_relocs | |
6509 | = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, | |
6510 | finfo->internal_relocs, FALSE); | |
6511 | if (internal_relocs == NULL | |
6512 | && o->reloc_count > 0) | |
6513 | return FALSE; | |
6514 | ||
6515 | if (bed->s->arch_size == 32) | |
6516 | { | |
6517 | r_type_mask = 0xff; | |
6518 | r_sym_shift = 8; | |
6519 | } | |
6520 | else | |
6521 | { | |
6522 | r_type_mask = 0xffffffff; | |
6523 | r_sym_shift = 32; | |
6524 | } | |
6525 | ||
6526 | /* Run through the relocs looking for any against symbols | |
6527 | from discarded sections and section symbols from | |
6528 | removed link-once sections. Complain about relocs | |
6529 | against discarded sections. Zero relocs against removed | |
6530 | link-once sections. Preserve debug information as much | |
6531 | as we can. */ | |
6532 | if (!elf_section_ignore_discarded_relocs (o)) | |
6533 | { | |
6534 | Elf_Internal_Rela *rel, *relend; | |
6535 | ||
6536 | rel = internal_relocs; | |
6537 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
6538 | for ( ; rel < relend; rel++) | |
6539 | { | |
6540 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
6541 | asection *sec; | |
6542 | ||
6543 | if (r_symndx >= locsymcount | |
6544 | || (elf_bad_symtab (input_bfd) | |
6545 | && finfo->sections[r_symndx] == NULL)) | |
6546 | { | |
6547 | struct elf_link_hash_entry *h; | |
6548 | ||
6549 | h = sym_hashes[r_symndx - extsymoff]; | |
6550 | while (h->root.type == bfd_link_hash_indirect | |
6551 | || h->root.type == bfd_link_hash_warning) | |
6552 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6553 | ||
6554 | /* Complain if the definition comes from a | |
6555 | discarded section. */ | |
6556 | sec = h->root.u.def.section; | |
6557 | if ((h->root.type == bfd_link_hash_defined | |
6558 | || h->root.type == bfd_link_hash_defweak) | |
6559 | && elf_discarded_section (sec)) | |
6560 | { | |
6561 | if ((o->flags & SEC_DEBUGGING) != 0) | |
6562 | { | |
6563 | BFD_ASSERT (r_symndx != 0); | |
6564 | /* Try to preserve debug information. */ | |
6565 | if ((o->flags & SEC_DEBUGGING) != 0 | |
6566 | && sec->kept_section != NULL | |
6567 | && sec->_raw_size == sec->kept_section->_raw_size) | |
6568 | h->root.u.def.section | |
6569 | = sec->kept_section; | |
6570 | else | |
6571 | memset (rel, 0, sizeof (*rel)); | |
6572 | } | |
6573 | else | |
6574 | finfo->info->callbacks->error_handler | |
6575 | (LD_DEFINITION_IN_DISCARDED_SECTION, | |
6576 | _("%T: discarded in section `%s' from %s\n"), | |
6577 | h->root.root.string, | |
6578 | h->root.root.string, | |
6579 | h->root.u.def.section->name, | |
6580 | bfd_archive_filename (h->root.u.def.section->owner)); | |
6581 | } | |
6582 | } | |
6583 | else | |
6584 | { | |
6585 | sec = finfo->sections[r_symndx]; | |
6586 | ||
6587 | if (sec != NULL && elf_discarded_section (sec)) | |
6588 | { | |
6589 | if ((o->flags & SEC_DEBUGGING) != 0 | |
6590 | || (sec->flags & SEC_LINK_ONCE) != 0) | |
6591 | { | |
6592 | BFD_ASSERT (r_symndx != 0); | |
6593 | /* Try to preserve debug information. */ | |
6594 | if ((o->flags & SEC_DEBUGGING) != 0 | |
6595 | && sec->kept_section != NULL | |
6596 | && sec->_raw_size == sec->kept_section->_raw_size) | |
6597 | finfo->sections[r_symndx] | |
6598 | = sec->kept_section; | |
6599 | else | |
6600 | { | |
6601 | rel->r_info &= r_type_mask; | |
6602 | rel->r_addend = 0; | |
6603 | } | |
6604 | } | |
6605 | else | |
6606 | { | |
6607 | static int count; | |
6608 | int ok; | |
6609 | char *buf; | |
6610 | ||
6611 | ok = asprintf (&buf, "local symbol %d", | |
6612 | count++); | |
6613 | if (ok <= 0) | |
6614 | buf = (char *) "local symbol"; | |
6615 | finfo->info->callbacks->error_handler | |
6616 | (LD_DEFINITION_IN_DISCARDED_SECTION, | |
6617 | _("%T: discarded in section `%s' from %s\n"), | |
6618 | buf, buf, sec->name, | |
6619 | bfd_archive_filename (input_bfd)); | |
6620 | if (ok != -1) | |
6621 | free (buf); | |
6622 | } | |
6623 | } | |
6624 | } | |
6625 | } | |
6626 | } | |
6627 | ||
6628 | /* Relocate the section by invoking a back end routine. | |
6629 | ||
6630 | The back end routine is responsible for adjusting the | |
6631 | section contents as necessary, and (if using Rela relocs | |
6632 | and generating a relocatable output file) adjusting the | |
6633 | reloc addend as necessary. | |
6634 | ||
6635 | The back end routine does not have to worry about setting | |
6636 | the reloc address or the reloc symbol index. | |
6637 | ||
6638 | The back end routine is given a pointer to the swapped in | |
6639 | internal symbols, and can access the hash table entries | |
6640 | for the external symbols via elf_sym_hashes (input_bfd). | |
6641 | ||
6642 | When generating relocatable output, the back end routine | |
6643 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
6644 | output symbol is going to be a section symbol | |
6645 | corresponding to the output section, which will require | |
6646 | the addend to be adjusted. */ | |
6647 | ||
6648 | if (! (*relocate_section) (output_bfd, finfo->info, | |
6649 | input_bfd, o, contents, | |
6650 | internal_relocs, | |
6651 | isymbuf, | |
6652 | finfo->sections)) | |
6653 | return FALSE; | |
6654 | ||
6655 | if (emit_relocs) | |
6656 | { | |
6657 | Elf_Internal_Rela *irela; | |
6658 | Elf_Internal_Rela *irelaend; | |
6659 | bfd_vma last_offset; | |
6660 | struct elf_link_hash_entry **rel_hash; | |
6661 | Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2; | |
6662 | unsigned int next_erel; | |
6663 | bfd_boolean (*reloc_emitter) | |
6664 | (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *); | |
6665 | bfd_boolean rela_normal; | |
6666 | ||
6667 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
6668 | rela_normal = (bed->rela_normal | |
6669 | && (input_rel_hdr->sh_entsize | |
6670 | == bed->s->sizeof_rela)); | |
6671 | ||
6672 | /* Adjust the reloc addresses and symbol indices. */ | |
6673 | ||
6674 | irela = internal_relocs; | |
6675 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
6676 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
6677 | + elf_section_data (o->output_section)->rel_count | |
6678 | + elf_section_data (o->output_section)->rel_count2); | |
6679 | last_offset = o->output_offset; | |
6680 | if (!finfo->info->relocatable) | |
6681 | last_offset += o->output_section->vma; | |
6682 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
6683 | { | |
6684 | unsigned long r_symndx; | |
6685 | asection *sec; | |
6686 | Elf_Internal_Sym sym; | |
6687 | ||
6688 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
6689 | { | |
6690 | rel_hash++; | |
6691 | next_erel = 0; | |
6692 | } | |
6693 | ||
6694 | irela->r_offset = _bfd_elf_section_offset (output_bfd, | |
6695 | finfo->info, o, | |
6696 | irela->r_offset); | |
6697 | if (irela->r_offset >= (bfd_vma) -2) | |
6698 | { | |
6699 | /* This is a reloc for a deleted entry or somesuch. | |
6700 | Turn it into an R_*_NONE reloc, at the same | |
6701 | offset as the last reloc. elf_eh_frame.c and | |
6702 | elf_bfd_discard_info rely on reloc offsets | |
6703 | being ordered. */ | |
6704 | irela->r_offset = last_offset; | |
6705 | irela->r_info = 0; | |
6706 | irela->r_addend = 0; | |
6707 | continue; | |
6708 | } | |
6709 | ||
6710 | irela->r_offset += o->output_offset; | |
6711 | ||
6712 | /* Relocs in an executable have to be virtual addresses. */ | |
6713 | if (!finfo->info->relocatable) | |
6714 | irela->r_offset += o->output_section->vma; | |
6715 | ||
6716 | last_offset = irela->r_offset; | |
6717 | ||
6718 | r_symndx = irela->r_info >> r_sym_shift; | |
6719 | if (r_symndx == STN_UNDEF) | |
6720 | continue; | |
6721 | ||
6722 | if (r_symndx >= locsymcount | |
6723 | || (elf_bad_symtab (input_bfd) | |
6724 | && finfo->sections[r_symndx] == NULL)) | |
6725 | { | |
6726 | struct elf_link_hash_entry *rh; | |
6727 | unsigned long indx; | |
6728 | ||
6729 | /* This is a reloc against a global symbol. We | |
6730 | have not yet output all the local symbols, so | |
6731 | we do not know the symbol index of any global | |
6732 | symbol. We set the rel_hash entry for this | |
6733 | reloc to point to the global hash table entry | |
6734 | for this symbol. The symbol index is then | |
6735 | set at the end of elf_bfd_final_link. */ | |
6736 | indx = r_symndx - extsymoff; | |
6737 | rh = elf_sym_hashes (input_bfd)[indx]; | |
6738 | while (rh->root.type == bfd_link_hash_indirect | |
6739 | || rh->root.type == bfd_link_hash_warning) | |
6740 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
6741 | ||
6742 | /* Setting the index to -2 tells | |
6743 | elf_link_output_extsym that this symbol is | |
6744 | used by a reloc. */ | |
6745 | BFD_ASSERT (rh->indx < 0); | |
6746 | rh->indx = -2; | |
6747 | ||
6748 | *rel_hash = rh; | |
6749 | ||
6750 | continue; | |
6751 | } | |
6752 | ||
6753 | /* This is a reloc against a local symbol. */ | |
6754 | ||
6755 | *rel_hash = NULL; | |
6756 | sym = isymbuf[r_symndx]; | |
6757 | sec = finfo->sections[r_symndx]; | |
6758 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) | |
6759 | { | |
6760 | /* I suppose the backend ought to fill in the | |
6761 | section of any STT_SECTION symbol against a | |
6762 | processor specific section. If we have | |
6763 | discarded a section, the output_section will | |
6764 | be the absolute section. */ | |
6765 | if (bfd_is_abs_section (sec) | |
6766 | || (sec != NULL | |
6767 | && bfd_is_abs_section (sec->output_section))) | |
6768 | r_symndx = 0; | |
6769 | else if (sec == NULL || sec->owner == NULL) | |
6770 | { | |
6771 | bfd_set_error (bfd_error_bad_value); | |
6772 | return FALSE; | |
6773 | } | |
6774 | else | |
6775 | { | |
6776 | r_symndx = sec->output_section->target_index; | |
6777 | BFD_ASSERT (r_symndx != 0); | |
6778 | } | |
6779 | ||
6780 | /* Adjust the addend according to where the | |
6781 | section winds up in the output section. */ | |
6782 | if (rela_normal) | |
6783 | irela->r_addend += sec->output_offset; | |
6784 | } | |
6785 | else | |
6786 | { | |
6787 | if (finfo->indices[r_symndx] == -1) | |
6788 | { | |
6789 | unsigned long shlink; | |
6790 | const char *name; | |
6791 | asection *osec; | |
6792 | ||
6793 | if (finfo->info->strip == strip_all) | |
6794 | { | |
6795 | /* You can't do ld -r -s. */ | |
6796 | bfd_set_error (bfd_error_invalid_operation); | |
6797 | return FALSE; | |
6798 | } | |
6799 | ||
6800 | /* This symbol was skipped earlier, but | |
6801 | since it is needed by a reloc, we | |
6802 | must output it now. */ | |
6803 | shlink = symtab_hdr->sh_link; | |
6804 | name = (bfd_elf_string_from_elf_section | |
6805 | (input_bfd, shlink, sym.st_name)); | |
6806 | if (name == NULL) | |
6807 | return FALSE; | |
6808 | ||
6809 | osec = sec->output_section; | |
6810 | sym.st_shndx = | |
6811 | _bfd_elf_section_from_bfd_section (output_bfd, | |
6812 | osec); | |
6813 | if (sym.st_shndx == SHN_BAD) | |
6814 | return FALSE; | |
6815 | ||
6816 | sym.st_value += sec->output_offset; | |
6817 | if (! finfo->info->relocatable) | |
6818 | { | |
6819 | sym.st_value += osec->vma; | |
6820 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
6821 | { | |
6822 | /* STT_TLS symbols are relative to PT_TLS | |
6823 | segment base. */ | |
6824 | BFD_ASSERT (elf_hash_table (finfo->info) | |
6825 | ->tls_sec != NULL); | |
6826 | sym.st_value -= (elf_hash_table (finfo->info) | |
6827 | ->tls_sec->vma); | |
6828 | } | |
6829 | } | |
6830 | ||
6831 | finfo->indices[r_symndx] | |
6832 | = bfd_get_symcount (output_bfd); | |
6833 | ||
6834 | if (! elf_link_output_sym (finfo, name, &sym, sec, | |
6835 | NULL)) | |
6836 | return FALSE; | |
6837 | } | |
6838 | ||
6839 | r_symndx = finfo->indices[r_symndx]; | |
6840 | } | |
6841 | ||
6842 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
6843 | | (irela->r_info & r_type_mask)); | |
6844 | } | |
6845 | ||
6846 | /* Swap out the relocs. */ | |
6847 | if (bed->elf_backend_emit_relocs | |
6848 | && !(finfo->info->relocatable | |
6849 | || finfo->info->emitrelocations)) | |
6850 | reloc_emitter = bed->elf_backend_emit_relocs; | |
6851 | else | |
6852 | reloc_emitter = _bfd_elf_link_output_relocs; | |
6853 | ||
6854 | if (input_rel_hdr->sh_size != 0 | |
6855 | && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr, | |
6856 | internal_relocs)) | |
6857 | return FALSE; | |
6858 | ||
6859 | input_rel_hdr2 = elf_section_data (o)->rel_hdr2; | |
6860 | if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0) | |
6861 | { | |
6862 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) | |
6863 | * bed->s->int_rels_per_ext_rel); | |
6864 | if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2, | |
6865 | internal_relocs)) | |
6866 | return FALSE; | |
6867 | } | |
6868 | } | |
6869 | } | |
6870 | ||
6871 | /* Write out the modified section contents. */ | |
6872 | if (bed->elf_backend_write_section | |
6873 | && (*bed->elf_backend_write_section) (output_bfd, o, contents)) | |
6874 | { | |
6875 | /* Section written out. */ | |
6876 | } | |
6877 | else switch (o->sec_info_type) | |
6878 | { | |
6879 | case ELF_INFO_TYPE_STABS: | |
6880 | if (! (_bfd_write_section_stabs | |
6881 | (output_bfd, | |
6882 | &elf_hash_table (finfo->info)->stab_info, | |
6883 | o, &elf_section_data (o)->sec_info, contents))) | |
6884 | return FALSE; | |
6885 | break; | |
6886 | case ELF_INFO_TYPE_MERGE: | |
6887 | if (! _bfd_write_merged_section (output_bfd, o, | |
6888 | elf_section_data (o)->sec_info)) | |
6889 | return FALSE; | |
6890 | break; | |
6891 | case ELF_INFO_TYPE_EH_FRAME: | |
6892 | { | |
6893 | if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, | |
6894 | o, contents)) | |
6895 | return FALSE; | |
6896 | } | |
6897 | break; | |
6898 | default: | |
6899 | { | |
6900 | bfd_size_type sec_size; | |
6901 | ||
6902 | sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size); | |
6903 | if (! (o->flags & SEC_EXCLUDE) | |
6904 | && ! bfd_set_section_contents (output_bfd, o->output_section, | |
6905 | contents, | |
6906 | (file_ptr) o->output_offset, | |
6907 | sec_size)) | |
6908 | return FALSE; | |
6909 | } | |
6910 | break; | |
6911 | } | |
6912 | } | |
6913 | ||
6914 | return TRUE; | |
6915 | } | |
6916 | ||
6917 | /* Generate a reloc when linking an ELF file. This is a reloc | |
6918 | requested by the linker, and does come from any input file. This | |
6919 | is used to build constructor and destructor tables when linking | |
6920 | with -Ur. */ | |
6921 | ||
6922 | static bfd_boolean | |
6923 | elf_reloc_link_order (bfd *output_bfd, | |
6924 | struct bfd_link_info *info, | |
6925 | asection *output_section, | |
6926 | struct bfd_link_order *link_order) | |
6927 | { | |
6928 | reloc_howto_type *howto; | |
6929 | long indx; | |
6930 | bfd_vma offset; | |
6931 | bfd_vma addend; | |
6932 | struct elf_link_hash_entry **rel_hash_ptr; | |
6933 | Elf_Internal_Shdr *rel_hdr; | |
6934 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
6935 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
6936 | bfd_byte *erel; | |
6937 | unsigned int i; | |
6938 | ||
6939 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
6940 | if (howto == NULL) | |
6941 | { | |
6942 | bfd_set_error (bfd_error_bad_value); | |
6943 | return FALSE; | |
6944 | } | |
6945 | ||
6946 | addend = link_order->u.reloc.p->addend; | |
6947 | ||
6948 | /* Figure out the symbol index. */ | |
6949 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
6950 | + elf_section_data (output_section)->rel_count | |
6951 | + elf_section_data (output_section)->rel_count2); | |
6952 | if (link_order->type == bfd_section_reloc_link_order) | |
6953 | { | |
6954 | indx = link_order->u.reloc.p->u.section->target_index; | |
6955 | BFD_ASSERT (indx != 0); | |
6956 | *rel_hash_ptr = NULL; | |
6957 | } | |
6958 | else | |
6959 | { | |
6960 | struct elf_link_hash_entry *h; | |
6961 | ||
6962 | /* Treat a reloc against a defined symbol as though it were | |
6963 | actually against the section. */ | |
6964 | h = ((struct elf_link_hash_entry *) | |
6965 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
6966 | link_order->u.reloc.p->u.name, | |
6967 | FALSE, FALSE, TRUE)); | |
6968 | if (h != NULL | |
6969 | && (h->root.type == bfd_link_hash_defined | |
6970 | || h->root.type == bfd_link_hash_defweak)) | |
6971 | { | |
6972 | asection *section; | |
6973 | ||
6974 | section = h->root.u.def.section; | |
6975 | indx = section->output_section->target_index; | |
6976 | *rel_hash_ptr = NULL; | |
6977 | /* It seems that we ought to add the symbol value to the | |
6978 | addend here, but in practice it has already been added | |
6979 | because it was passed to constructor_callback. */ | |
6980 | addend += section->output_section->vma + section->output_offset; | |
6981 | } | |
6982 | else if (h != NULL) | |
6983 | { | |
6984 | /* Setting the index to -2 tells elf_link_output_extsym that | |
6985 | this symbol is used by a reloc. */ | |
6986 | h->indx = -2; | |
6987 | *rel_hash_ptr = h; | |
6988 | indx = 0; | |
6989 | } | |
6990 | else | |
6991 | { | |
6992 | if (! ((*info->callbacks->unattached_reloc) | |
6993 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) | |
6994 | return FALSE; | |
6995 | indx = 0; | |
6996 | } | |
6997 | } | |
6998 | ||
6999 | /* If this is an inplace reloc, we must write the addend into the | |
7000 | object file. */ | |
7001 | if (howto->partial_inplace && addend != 0) | |
7002 | { | |
7003 | bfd_size_type size; | |
7004 | bfd_reloc_status_type rstat; | |
7005 | bfd_byte *buf; | |
7006 | bfd_boolean ok; | |
7007 | const char *sym_name; | |
7008 | ||
7009 | size = bfd_get_reloc_size (howto); | |
7010 | buf = bfd_zmalloc (size); | |
7011 | if (buf == NULL) | |
7012 | return FALSE; | |
7013 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
7014 | switch (rstat) | |
7015 | { | |
7016 | case bfd_reloc_ok: | |
7017 | break; | |
7018 | ||
7019 | default: | |
7020 | case bfd_reloc_outofrange: | |
7021 | abort (); | |
7022 | ||
7023 | case bfd_reloc_overflow: | |
7024 | if (link_order->type == bfd_section_reloc_link_order) | |
7025 | sym_name = bfd_section_name (output_bfd, | |
7026 | link_order->u.reloc.p->u.section); | |
7027 | else | |
7028 | sym_name = link_order->u.reloc.p->u.name; | |
7029 | if (! ((*info->callbacks->reloc_overflow) | |
7030 | (info, sym_name, howto->name, addend, NULL, NULL, 0))) | |
7031 | { | |
7032 | free (buf); | |
7033 | return FALSE; | |
7034 | } | |
7035 | break; | |
7036 | } | |
7037 | ok = bfd_set_section_contents (output_bfd, output_section, buf, | |
7038 | link_order->offset, size); | |
7039 | free (buf); | |
7040 | if (! ok) | |
7041 | return FALSE; | |
7042 | } | |
7043 | ||
7044 | /* The address of a reloc is relative to the section in a | |
7045 | relocatable file, and is a virtual address in an executable | |
7046 | file. */ | |
7047 | offset = link_order->offset; | |
7048 | if (! info->relocatable) | |
7049 | offset += output_section->vma; | |
7050 | ||
7051 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
7052 | { | |
7053 | irel[i].r_offset = offset; | |
7054 | irel[i].r_info = 0; | |
7055 | irel[i].r_addend = 0; | |
7056 | } | |
7057 | if (bed->s->arch_size == 32) | |
7058 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
7059 | else | |
7060 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
7061 | ||
7062 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
7063 | erel = rel_hdr->contents; | |
7064 | if (rel_hdr->sh_type == SHT_REL) | |
7065 | { | |
7066 | erel += (elf_section_data (output_section)->rel_count | |
7067 | * bed->s->sizeof_rel); | |
7068 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); | |
7069 | } | |
7070 | else | |
7071 | { | |
7072 | irel[0].r_addend = addend; | |
7073 | erel += (elf_section_data (output_section)->rel_count | |
7074 | * bed->s->sizeof_rela); | |
7075 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); | |
7076 | } | |
7077 | ||
7078 | ++elf_section_data (output_section)->rel_count; | |
7079 | ||
7080 | return TRUE; | |
7081 | } | |
7082 | ||
7083 | /* Do the final step of an ELF link. */ | |
7084 | ||
7085 | bfd_boolean | |
7086 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
7087 | { | |
7088 | bfd_boolean dynamic; | |
7089 | bfd_boolean emit_relocs; | |
7090 | bfd *dynobj; | |
7091 | struct elf_final_link_info finfo; | |
7092 | register asection *o; | |
7093 | register struct bfd_link_order *p; | |
7094 | register bfd *sub; | |
7095 | bfd_size_type max_contents_size; | |
7096 | bfd_size_type max_external_reloc_size; | |
7097 | bfd_size_type max_internal_reloc_count; | |
7098 | bfd_size_type max_sym_count; | |
7099 | bfd_size_type max_sym_shndx_count; | |
7100 | file_ptr off; | |
7101 | Elf_Internal_Sym elfsym; | |
7102 | unsigned int i; | |
7103 | Elf_Internal_Shdr *symtab_hdr; | |
7104 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
7105 | Elf_Internal_Shdr *symstrtab_hdr; | |
7106 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7107 | struct elf_outext_info eoinfo; | |
7108 | bfd_boolean merged; | |
7109 | size_t relativecount = 0; | |
7110 | asection *reldyn = 0; | |
7111 | bfd_size_type amt; | |
7112 | ||
7113 | if (! is_elf_hash_table (info->hash)) | |
7114 | return FALSE; | |
7115 | ||
7116 | if (info->shared) | |
7117 | abfd->flags |= DYNAMIC; | |
7118 | ||
7119 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
7120 | dynobj = elf_hash_table (info)->dynobj; | |
7121 | ||
7122 | emit_relocs = (info->relocatable | |
7123 | || info->emitrelocations | |
7124 | || bed->elf_backend_emit_relocs); | |
7125 | ||
7126 | finfo.info = info; | |
7127 | finfo.output_bfd = abfd; | |
7128 | finfo.symstrtab = _bfd_elf_stringtab_init (); | |
7129 | if (finfo.symstrtab == NULL) | |
7130 | return FALSE; | |
7131 | ||
7132 | if (! dynamic) | |
7133 | { | |
7134 | finfo.dynsym_sec = NULL; | |
7135 | finfo.hash_sec = NULL; | |
7136 | finfo.symver_sec = NULL; | |
7137 | } | |
7138 | else | |
7139 | { | |
7140 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
7141 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
7142 | BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL); | |
7143 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
7144 | /* Note that it is OK if symver_sec is NULL. */ | |
7145 | } | |
7146 | ||
7147 | finfo.contents = NULL; | |
7148 | finfo.external_relocs = NULL; | |
7149 | finfo.internal_relocs = NULL; | |
7150 | finfo.external_syms = NULL; | |
7151 | finfo.locsym_shndx = NULL; | |
7152 | finfo.internal_syms = NULL; | |
7153 | finfo.indices = NULL; | |
7154 | finfo.sections = NULL; | |
7155 | finfo.symbuf = NULL; | |
7156 | finfo.symshndxbuf = NULL; | |
7157 | finfo.symbuf_count = 0; | |
7158 | finfo.shndxbuf_size = 0; | |
7159 | ||
7160 | /* Count up the number of relocations we will output for each output | |
7161 | section, so that we know the sizes of the reloc sections. We | |
7162 | also figure out some maximum sizes. */ | |
7163 | max_contents_size = 0; | |
7164 | max_external_reloc_size = 0; | |
7165 | max_internal_reloc_count = 0; | |
7166 | max_sym_count = 0; | |
7167 | max_sym_shndx_count = 0; | |
7168 | merged = FALSE; | |
7169 | for (o = abfd->sections; o != NULL; o = o->next) | |
7170 | { | |
7171 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
7172 | o->reloc_count = 0; | |
7173 | ||
7174 | for (p = o->link_order_head; p != NULL; p = p->next) | |
7175 | { | |
7176 | unsigned int reloc_count = 0; | |
7177 | struct bfd_elf_section_data *esdi = NULL; | |
7178 | unsigned int *rel_count1; | |
7179 | ||
7180 | if (p->type == bfd_section_reloc_link_order | |
7181 | || p->type == bfd_symbol_reloc_link_order) | |
7182 | reloc_count = 1; | |
7183 | else if (p->type == bfd_indirect_link_order) | |
7184 | { | |
7185 | asection *sec; | |
7186 | ||
7187 | sec = p->u.indirect.section; | |
7188 | esdi = elf_section_data (sec); | |
7189 | ||
7190 | /* Mark all sections which are to be included in the | |
7191 | link. This will normally be every section. We need | |
7192 | to do this so that we can identify any sections which | |
7193 | the linker has decided to not include. */ | |
7194 | sec->linker_mark = TRUE; | |
7195 | ||
7196 | if (sec->flags & SEC_MERGE) | |
7197 | merged = TRUE; | |
7198 | ||
7199 | if (info->relocatable || info->emitrelocations) | |
7200 | reloc_count = sec->reloc_count; | |
7201 | else if (bed->elf_backend_count_relocs) | |
7202 | { | |
7203 | Elf_Internal_Rela * relocs; | |
7204 | ||
7205 | relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
7206 | info->keep_memory); | |
7207 | ||
7208 | reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs); | |
7209 | ||
7210 | if (elf_section_data (o)->relocs != relocs) | |
7211 | free (relocs); | |
7212 | } | |
7213 | ||
7214 | if (sec->_raw_size > max_contents_size) | |
7215 | max_contents_size = sec->_raw_size; | |
7216 | if (sec->_cooked_size > max_contents_size) | |
7217 | max_contents_size = sec->_cooked_size; | |
7218 | ||
7219 | /* We are interested in just local symbols, not all | |
7220 | symbols. */ | |
7221 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
7222 | && (sec->owner->flags & DYNAMIC) == 0) | |
7223 | { | |
7224 | size_t sym_count; | |
7225 | ||
7226 | if (elf_bad_symtab (sec->owner)) | |
7227 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
7228 | / bed->s->sizeof_sym); | |
7229 | else | |
7230 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
7231 | ||
7232 | if (sym_count > max_sym_count) | |
7233 | max_sym_count = sym_count; | |
7234 | ||
7235 | if (sym_count > max_sym_shndx_count | |
7236 | && elf_symtab_shndx (sec->owner) != 0) | |
7237 | max_sym_shndx_count = sym_count; | |
7238 | ||
7239 | if ((sec->flags & SEC_RELOC) != 0) | |
7240 | { | |
7241 | size_t ext_size; | |
7242 | ||
7243 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
7244 | if (ext_size > max_external_reloc_size) | |
7245 | max_external_reloc_size = ext_size; | |
7246 | if (sec->reloc_count > max_internal_reloc_count) | |
7247 | max_internal_reloc_count = sec->reloc_count; | |
7248 | } | |
7249 | } | |
7250 | } | |
7251 | ||
7252 | if (reloc_count == 0) | |
7253 | continue; | |
7254 | ||
7255 | o->reloc_count += reloc_count; | |
7256 | ||
7257 | /* MIPS may have a mix of REL and RELA relocs on sections. | |
7258 | To support this curious ABI we keep reloc counts in | |
7259 | elf_section_data too. We must be careful to add the | |
7260 | relocations from the input section to the right output | |
7261 | count. FIXME: Get rid of one count. We have | |
7262 | o->reloc_count == esdo->rel_count + esdo->rel_count2. */ | |
7263 | rel_count1 = &esdo->rel_count; | |
7264 | if (esdi != NULL) | |
7265 | { | |
7266 | bfd_boolean same_size; | |
7267 | bfd_size_type entsize1; | |
7268 | ||
7269 | entsize1 = esdi->rel_hdr.sh_entsize; | |
7270 | BFD_ASSERT (entsize1 == bed->s->sizeof_rel | |
7271 | || entsize1 == bed->s->sizeof_rela); | |
7272 | same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel); | |
7273 | ||
7274 | if (!same_size) | |
7275 | rel_count1 = &esdo->rel_count2; | |
7276 | ||
7277 | if (esdi->rel_hdr2 != NULL) | |
7278 | { | |
7279 | bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize; | |
7280 | unsigned int alt_count; | |
7281 | unsigned int *rel_count2; | |
7282 | ||
7283 | BFD_ASSERT (entsize2 != entsize1 | |
7284 | && (entsize2 == bed->s->sizeof_rel | |
7285 | || entsize2 == bed->s->sizeof_rela)); | |
7286 | ||
7287 | rel_count2 = &esdo->rel_count2; | |
7288 | if (!same_size) | |
7289 | rel_count2 = &esdo->rel_count; | |
7290 | ||
7291 | /* The following is probably too simplistic if the | |
7292 | backend counts output relocs unusually. */ | |
7293 | BFD_ASSERT (bed->elf_backend_count_relocs == NULL); | |
7294 | alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2); | |
7295 | *rel_count2 += alt_count; | |
7296 | reloc_count -= alt_count; | |
7297 | } | |
7298 | } | |
7299 | *rel_count1 += reloc_count; | |
7300 | } | |
7301 | ||
7302 | if (o->reloc_count > 0) | |
7303 | o->flags |= SEC_RELOC; | |
7304 | else | |
7305 | { | |
7306 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
7307 | set it (this is probably a bug) and if it is set | |
7308 | assign_section_numbers will create a reloc section. */ | |
7309 | o->flags &=~ SEC_RELOC; | |
7310 | } | |
7311 | ||
7312 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
7313 | zero. This is done in elf_fake_sections as well, but forcing | |
7314 | the VMA to 0 here will ensure that relocs against these | |
7315 | sections are handled correctly. */ | |
7316 | if ((o->flags & SEC_ALLOC) == 0 | |
7317 | && ! o->user_set_vma) | |
7318 | o->vma = 0; | |
7319 | } | |
7320 | ||
7321 | if (! info->relocatable && merged) | |
7322 | elf_link_hash_traverse (elf_hash_table (info), | |
7323 | _bfd_elf_link_sec_merge_syms, abfd); | |
7324 | ||
7325 | /* Figure out the file positions for everything but the symbol table | |
7326 | and the relocs. We set symcount to force assign_section_numbers | |
7327 | to create a symbol table. */ | |
7328 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; | |
7329 | BFD_ASSERT (! abfd->output_has_begun); | |
7330 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
7331 | goto error_return; | |
7332 | ||
7333 | /* That created the reloc sections. Set their sizes, and assign | |
7334 | them file positions, and allocate some buffers. */ | |
7335 | for (o = abfd->sections; o != NULL; o = o->next) | |
7336 | { | |
7337 | if ((o->flags & SEC_RELOC) != 0) | |
7338 | { | |
7339 | if (!(_bfd_elf_link_size_reloc_section | |
7340 | (abfd, &elf_section_data (o)->rel_hdr, o))) | |
7341 | goto error_return; | |
7342 | ||
7343 | if (elf_section_data (o)->rel_hdr2 | |
7344 | && !(_bfd_elf_link_size_reloc_section | |
7345 | (abfd, elf_section_data (o)->rel_hdr2, o))) | |
7346 | goto error_return; | |
7347 | } | |
7348 | ||
7349 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
7350 | to count upwards while actually outputting the relocations. */ | |
7351 | elf_section_data (o)->rel_count = 0; | |
7352 | elf_section_data (o)->rel_count2 = 0; | |
7353 | } | |
7354 | ||
7355 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
7356 | ||
7357 | /* We have now assigned file positions for all the sections except | |
7358 | .symtab and .strtab. We start the .symtab section at the current | |
7359 | file position, and write directly to it. We build the .strtab | |
7360 | section in memory. */ | |
7361 | bfd_get_symcount (abfd) = 0; | |
7362 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7363 | /* sh_name is set in prep_headers. */ | |
7364 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7365 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
7366 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7367 | /* sh_link is set in assign_section_numbers. */ | |
7368 | /* sh_info is set below. */ | |
7369 | /* sh_offset is set just below. */ | |
7370 | symtab_hdr->sh_addralign = 1 << bed->s->log_file_align; | |
7371 | ||
7372 | off = elf_tdata (abfd)->next_file_pos; | |
7373 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
7374 | ||
7375 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
7376 | incorrect. We do not yet know the size of the .symtab section. | |
7377 | We correct next_file_pos below, after we do know the size. */ | |
7378 | ||
7379 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
7380 | continuously seeking to the right position in the file. */ | |
7381 | if (! info->keep_memory || max_sym_count < 20) | |
7382 | finfo.symbuf_size = 20; | |
7383 | else | |
7384 | finfo.symbuf_size = max_sym_count; | |
7385 | amt = finfo.symbuf_size; | |
7386 | amt *= bed->s->sizeof_sym; | |
7387 | finfo.symbuf = bfd_malloc (amt); | |
7388 | if (finfo.symbuf == NULL) | |
7389 | goto error_return; | |
7390 | if (elf_numsections (abfd) > SHN_LORESERVE) | |
7391 | { | |
7392 | /* Wild guess at number of output symbols. realloc'd as needed. */ | |
7393 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
7394 | finfo.shndxbuf_size = amt; | |
7395 | amt *= sizeof (Elf_External_Sym_Shndx); | |
7396 | finfo.symshndxbuf = bfd_zmalloc (amt); | |
7397 | if (finfo.symshndxbuf == NULL) | |
7398 | goto error_return; | |
7399 | } | |
7400 | ||
7401 | /* Start writing out the symbol table. The first symbol is always a | |
7402 | dummy symbol. */ | |
7403 | if (info->strip != strip_all | |
7404 | || emit_relocs) | |
7405 | { | |
7406 | elfsym.st_value = 0; | |
7407 | elfsym.st_size = 0; | |
7408 | elfsym.st_info = 0; | |
7409 | elfsym.st_other = 0; | |
7410 | elfsym.st_shndx = SHN_UNDEF; | |
7411 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr, | |
7412 | NULL)) | |
7413 | goto error_return; | |
7414 | } | |
7415 | ||
7416 | #if 0 | |
7417 | /* Some standard ELF linkers do this, but we don't because it causes | |
7418 | bootstrap comparison failures. */ | |
7419 | /* Output a file symbol for the output file as the second symbol. | |
7420 | We output this even if we are discarding local symbols, although | |
7421 | I'm not sure if this is correct. */ | |
7422 | elfsym.st_value = 0; | |
7423 | elfsym.st_size = 0; | |
7424 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7425 | elfsym.st_other = 0; | |
7426 | elfsym.st_shndx = SHN_ABS; | |
7427 | if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd), | |
7428 | &elfsym, bfd_abs_section_ptr, NULL)) | |
7429 | goto error_return; | |
7430 | #endif | |
7431 | ||
7432 | /* Output a symbol for each section. We output these even if we are | |
7433 | discarding local symbols, since they are used for relocs. These | |
7434 | symbols have no names. We store the index of each one in the | |
7435 | index field of the section, so that we can find it again when | |
7436 | outputting relocs. */ | |
7437 | if (info->strip != strip_all | |
7438 | || emit_relocs) | |
7439 | { | |
7440 | elfsym.st_size = 0; | |
7441 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7442 | elfsym.st_other = 0; | |
7443 | for (i = 1; i < elf_numsections (abfd); i++) | |
7444 | { | |
7445 | o = bfd_section_from_elf_index (abfd, i); | |
7446 | if (o != NULL) | |
7447 | o->target_index = bfd_get_symcount (abfd); | |
7448 | elfsym.st_shndx = i; | |
7449 | if (info->relocatable || o == NULL) | |
7450 | elfsym.st_value = 0; | |
7451 | else | |
7452 | elfsym.st_value = o->vma; | |
7453 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL)) | |
7454 | goto error_return; | |
7455 | if (i == SHN_LORESERVE - 1) | |
7456 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
7457 | } | |
7458 | } | |
7459 | ||
7460 | /* Allocate some memory to hold information read in from the input | |
7461 | files. */ | |
7462 | if (max_contents_size != 0) | |
7463 | { | |
7464 | finfo.contents = bfd_malloc (max_contents_size); | |
7465 | if (finfo.contents == NULL) | |
7466 | goto error_return; | |
7467 | } | |
7468 | ||
7469 | if (max_external_reloc_size != 0) | |
7470 | { | |
7471 | finfo.external_relocs = bfd_malloc (max_external_reloc_size); | |
7472 | if (finfo.external_relocs == NULL) | |
7473 | goto error_return; | |
7474 | } | |
7475 | ||
7476 | if (max_internal_reloc_count != 0) | |
7477 | { | |
7478 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
7479 | amt *= sizeof (Elf_Internal_Rela); | |
7480 | finfo.internal_relocs = bfd_malloc (amt); | |
7481 | if (finfo.internal_relocs == NULL) | |
7482 | goto error_return; | |
7483 | } | |
7484 | ||
7485 | if (max_sym_count != 0) | |
7486 | { | |
7487 | amt = max_sym_count * bed->s->sizeof_sym; | |
7488 | finfo.external_syms = bfd_malloc (amt); | |
7489 | if (finfo.external_syms == NULL) | |
7490 | goto error_return; | |
7491 | ||
7492 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
7493 | finfo.internal_syms = bfd_malloc (amt); | |
7494 | if (finfo.internal_syms == NULL) | |
7495 | goto error_return; | |
7496 | ||
7497 | amt = max_sym_count * sizeof (long); | |
7498 | finfo.indices = bfd_malloc (amt); | |
7499 | if (finfo.indices == NULL) | |
7500 | goto error_return; | |
7501 | ||
7502 | amt = max_sym_count * sizeof (asection *); | |
7503 | finfo.sections = bfd_malloc (amt); | |
7504 | if (finfo.sections == NULL) | |
7505 | goto error_return; | |
7506 | } | |
7507 | ||
7508 | if (max_sym_shndx_count != 0) | |
7509 | { | |
7510 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
7511 | finfo.locsym_shndx = bfd_malloc (amt); | |
7512 | if (finfo.locsym_shndx == NULL) | |
7513 | goto error_return; | |
7514 | } | |
7515 | ||
7516 | if (elf_hash_table (info)->tls_sec) | |
7517 | { | |
7518 | bfd_vma base, end = 0; | |
7519 | asection *sec; | |
7520 | ||
7521 | for (sec = elf_hash_table (info)->tls_sec; | |
7522 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
7523 | sec = sec->next) | |
7524 | { | |
7525 | bfd_vma size = sec->_raw_size; | |
7526 | ||
7527 | if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
7528 | { | |
7529 | struct bfd_link_order *o; | |
7530 | ||
7531 | for (o = sec->link_order_head; o != NULL; o = o->next) | |
7532 | if (size < o->offset + o->size) | |
7533 | size = o->offset + o->size; | |
7534 | } | |
7535 | end = sec->vma + size; | |
7536 | } | |
7537 | base = elf_hash_table (info)->tls_sec->vma; | |
7538 | end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power); | |
7539 | elf_hash_table (info)->tls_size = end - base; | |
7540 | } | |
7541 | ||
7542 | /* Since ELF permits relocations to be against local symbols, we | |
7543 | must have the local symbols available when we do the relocations. | |
7544 | Since we would rather only read the local symbols once, and we | |
7545 | would rather not keep them in memory, we handle all the | |
7546 | relocations for a single input file at the same time. | |
7547 | ||
7548 | Unfortunately, there is no way to know the total number of local | |
7549 | symbols until we have seen all of them, and the local symbol | |
7550 | indices precede the global symbol indices. This means that when | |
7551 | we are generating relocatable output, and we see a reloc against | |
7552 | a global symbol, we can not know the symbol index until we have | |
7553 | finished examining all the local symbols to see which ones we are | |
7554 | going to output. To deal with this, we keep the relocations in | |
7555 | memory, and don't output them until the end of the link. This is | |
7556 | an unfortunate waste of memory, but I don't see a good way around | |
7557 | it. Fortunately, it only happens when performing a relocatable | |
7558 | link, which is not the common case. FIXME: If keep_memory is set | |
7559 | we could write the relocs out and then read them again; I don't | |
7560 | know how bad the memory loss will be. */ | |
7561 | ||
7562 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
7563 | sub->output_has_begun = FALSE; | |
7564 | for (o = abfd->sections; o != NULL; o = o->next) | |
7565 | { | |
7566 | for (p = o->link_order_head; p != NULL; p = p->next) | |
7567 | { | |
7568 | if (p->type == bfd_indirect_link_order | |
7569 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
7570 | == bfd_target_elf_flavour) | |
7571 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
7572 | { | |
7573 | if (! sub->output_has_begun) | |
7574 | { | |
7575 | if (! elf_link_input_bfd (&finfo, sub)) | |
7576 | goto error_return; | |
7577 | sub->output_has_begun = TRUE; | |
7578 | } | |
7579 | } | |
7580 | else if (p->type == bfd_section_reloc_link_order | |
7581 | || p->type == bfd_symbol_reloc_link_order) | |
7582 | { | |
7583 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
7584 | goto error_return; | |
7585 | } | |
7586 | else | |
7587 | { | |
7588 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
7589 | goto error_return; | |
7590 | } | |
7591 | } | |
7592 | } | |
7593 | ||
7594 | /* Output any global symbols that got converted to local in a | |
7595 | version script or due to symbol visibility. We do this in a | |
7596 | separate step since ELF requires all local symbols to appear | |
7597 | prior to any global symbols. FIXME: We should only do this if | |
7598 | some global symbols were, in fact, converted to become local. | |
7599 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
7600 | eoinfo.failed = FALSE; | |
7601 | eoinfo.finfo = &finfo; | |
7602 | eoinfo.localsyms = TRUE; | |
7603 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
7604 | &eoinfo); | |
7605 | if (eoinfo.failed) | |
7606 | return FALSE; | |
7607 | ||
7608 | /* That wrote out all the local symbols. Finish up the symbol table | |
7609 | with the global symbols. Even if we want to strip everything we | |
7610 | can, we still need to deal with those global symbols that got | |
7611 | converted to local in a version script. */ | |
7612 | ||
7613 | /* The sh_info field records the index of the first non local symbol. */ | |
7614 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
7615 | ||
7616 | if (dynamic | |
7617 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
7618 | { | |
7619 | Elf_Internal_Sym sym; | |
7620 | bfd_byte *dynsym = finfo.dynsym_sec->contents; | |
7621 | long last_local = 0; | |
7622 | ||
7623 | /* Write out the section symbols for the output sections. */ | |
7624 | if (info->shared) | |
7625 | { | |
7626 | asection *s; | |
7627 | ||
7628 | sym.st_size = 0; | |
7629 | sym.st_name = 0; | |
7630 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7631 | sym.st_other = 0; | |
7632 | ||
7633 | for (s = abfd->sections; s != NULL; s = s->next) | |
7634 | { | |
7635 | int indx; | |
7636 | bfd_byte *dest; | |
7637 | long dynindx; | |
7638 | ||
7639 | indx = elf_section_data (s)->this_idx; | |
7640 | dynindx = elf_section_data (s)->dynindx; | |
7641 | BFD_ASSERT (indx > 0); | |
7642 | sym.st_shndx = indx; | |
7643 | sym.st_value = s->vma; | |
7644 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
7645 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
7646 | } | |
7647 | ||
7648 | last_local = bfd_count_sections (abfd); | |
7649 | } | |
7650 | ||
7651 | /* Write out the local dynsyms. */ | |
7652 | if (elf_hash_table (info)->dynlocal) | |
7653 | { | |
7654 | struct elf_link_local_dynamic_entry *e; | |
7655 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
7656 | { | |
7657 | asection *s; | |
7658 | bfd_byte *dest; | |
7659 | ||
7660 | sym.st_size = e->isym.st_size; | |
7661 | sym.st_other = e->isym.st_other; | |
7662 | ||
7663 | /* Copy the internal symbol as is. | |
7664 | Note that we saved a word of storage and overwrote | |
7665 | the original st_name with the dynstr_index. */ | |
7666 | sym = e->isym; | |
7667 | ||
7668 | if (e->isym.st_shndx != SHN_UNDEF | |
7669 | && (e->isym.st_shndx < SHN_LORESERVE | |
7670 | || e->isym.st_shndx > SHN_HIRESERVE)) | |
7671 | { | |
7672 | s = bfd_section_from_elf_index (e->input_bfd, | |
7673 | e->isym.st_shndx); | |
7674 | ||
7675 | sym.st_shndx = | |
7676 | elf_section_data (s->output_section)->this_idx; | |
7677 | sym.st_value = (s->output_section->vma | |
7678 | + s->output_offset | |
7679 | + e->isym.st_value); | |
7680 | } | |
7681 | ||
7682 | if (last_local < e->dynindx) | |
7683 | last_local = e->dynindx; | |
7684 | ||
7685 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
7686 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
7687 | } | |
7688 | } | |
7689 | ||
7690 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = | |
7691 | last_local + 1; | |
7692 | } | |
7693 | ||
7694 | /* We get the global symbols from the hash table. */ | |
7695 | eoinfo.failed = FALSE; | |
7696 | eoinfo.localsyms = FALSE; | |
7697 | eoinfo.finfo = &finfo; | |
7698 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
7699 | &eoinfo); | |
7700 | if (eoinfo.failed) | |
7701 | return FALSE; | |
7702 | ||
7703 | /* If backend needs to output some symbols not present in the hash | |
7704 | table, do it now. */ | |
7705 | if (bed->elf_backend_output_arch_syms) | |
7706 | { | |
7707 | typedef bfd_boolean (*out_sym_func) | |
7708 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
7709 | struct elf_link_hash_entry *); | |
7710 | ||
7711 | if (! ((*bed->elf_backend_output_arch_syms) | |
7712 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
7713 | return FALSE; | |
7714 | } | |
7715 | ||
7716 | /* Flush all symbols to the file. */ | |
7717 | if (! elf_link_flush_output_syms (&finfo, bed)) | |
7718 | return FALSE; | |
7719 | ||
7720 | /* Now we know the size of the symtab section. */ | |
7721 | off += symtab_hdr->sh_size; | |
7722 | ||
7723 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
7724 | if (symtab_shndx_hdr->sh_name != 0) | |
7725 | { | |
7726 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
7727 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
7728 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
7729 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
7730 | symtab_shndx_hdr->sh_size = amt; | |
7731 | ||
7732 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
7733 | off, TRUE); | |
7734 | ||
7735 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
7736 | || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) | |
7737 | return FALSE; | |
7738 | } | |
7739 | ||
7740 | ||
7741 | /* Finish up and write out the symbol string table (.strtab) | |
7742 | section. */ | |
7743 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7744 | /* sh_name was set in prep_headers. */ | |
7745 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7746 | symstrtab_hdr->sh_flags = 0; | |
7747 | symstrtab_hdr->sh_addr = 0; | |
7748 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
7749 | symstrtab_hdr->sh_entsize = 0; | |
7750 | symstrtab_hdr->sh_link = 0; | |
7751 | symstrtab_hdr->sh_info = 0; | |
7752 | /* sh_offset is set just below. */ | |
7753 | symstrtab_hdr->sh_addralign = 1; | |
7754 | ||
7755 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); | |
7756 | elf_tdata (abfd)->next_file_pos = off; | |
7757 | ||
7758 | if (bfd_get_symcount (abfd) > 0) | |
7759 | { | |
7760 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
7761 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
7762 | return FALSE; | |
7763 | } | |
7764 | ||
7765 | /* Adjust the relocs to have the correct symbol indices. */ | |
7766 | for (o = abfd->sections; o != NULL; o = o->next) | |
7767 | { | |
7768 | if ((o->flags & SEC_RELOC) == 0) | |
7769 | continue; | |
7770 | ||
7771 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, | |
7772 | elf_section_data (o)->rel_count, | |
7773 | elf_section_data (o)->rel_hashes); | |
7774 | if (elf_section_data (o)->rel_hdr2 != NULL) | |
7775 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, | |
7776 | elf_section_data (o)->rel_count2, | |
7777 | (elf_section_data (o)->rel_hashes | |
7778 | + elf_section_data (o)->rel_count)); | |
7779 | ||
7780 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
7781 | trying to swap the relocs out itself. */ | |
7782 | o->reloc_count = 0; | |
7783 | } | |
7784 | ||
7785 | if (dynamic && info->combreloc && dynobj != NULL) | |
7786 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
7787 | ||
7788 | /* If we are linking against a dynamic object, or generating a | |
7789 | shared library, finish up the dynamic linking information. */ | |
7790 | if (dynamic) | |
7791 | { | |
7792 | bfd_byte *dyncon, *dynconend; | |
7793 | ||
7794 | /* Fix up .dynamic entries. */ | |
7795 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
7796 | BFD_ASSERT (o != NULL); | |
7797 | ||
7798 | dyncon = o->contents; | |
7799 | dynconend = o->contents + o->_raw_size; | |
7800 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
7801 | { | |
7802 | Elf_Internal_Dyn dyn; | |
7803 | const char *name; | |
7804 | unsigned int type; | |
7805 | ||
7806 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
7807 | ||
7808 | switch (dyn.d_tag) | |
7809 | { | |
7810 | default: | |
7811 | continue; | |
7812 | case DT_NULL: | |
7813 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
7814 | { | |
7815 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
7816 | { | |
7817 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
7818 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
7819 | default: continue; | |
7820 | } | |
7821 | dyn.d_un.d_val = relativecount; | |
7822 | relativecount = 0; | |
7823 | break; | |
7824 | } | |
7825 | continue; | |
7826 | ||
7827 | case DT_INIT: | |
7828 | name = info->init_function; | |
7829 | goto get_sym; | |
7830 | case DT_FINI: | |
7831 | name = info->fini_function; | |
7832 | get_sym: | |
7833 | { | |
7834 | struct elf_link_hash_entry *h; | |
7835 | ||
7836 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
7837 | FALSE, FALSE, TRUE); | |
7838 | if (h != NULL | |
7839 | && (h->root.type == bfd_link_hash_defined | |
7840 | || h->root.type == bfd_link_hash_defweak)) | |
7841 | { | |
7842 | dyn.d_un.d_val = h->root.u.def.value; | |
7843 | o = h->root.u.def.section; | |
7844 | if (o->output_section != NULL) | |
7845 | dyn.d_un.d_val += (o->output_section->vma | |
7846 | + o->output_offset); | |
7847 | else | |
7848 | { | |
7849 | /* The symbol is imported from another shared | |
7850 | library and does not apply to this one. */ | |
7851 | dyn.d_un.d_val = 0; | |
7852 | } | |
7853 | break; | |
7854 | } | |
7855 | } | |
7856 | continue; | |
7857 | ||
7858 | case DT_PREINIT_ARRAYSZ: | |
7859 | name = ".preinit_array"; | |
7860 | goto get_size; | |
7861 | case DT_INIT_ARRAYSZ: | |
7862 | name = ".init_array"; | |
7863 | goto get_size; | |
7864 | case DT_FINI_ARRAYSZ: | |
7865 | name = ".fini_array"; | |
7866 | get_size: | |
7867 | o = bfd_get_section_by_name (abfd, name); | |
7868 | if (o == NULL) | |
7869 | { | |
7870 | (*_bfd_error_handler) | |
7871 | (_("%s: could not find output section %s"), | |
7872 | bfd_get_filename (abfd), name); | |
7873 | goto error_return; | |
7874 | } | |
7875 | if (o->_raw_size == 0) | |
7876 | (*_bfd_error_handler) | |
7877 | (_("warning: %s section has zero size"), name); | |
7878 | dyn.d_un.d_val = o->_raw_size; | |
7879 | break; | |
7880 | ||
7881 | case DT_PREINIT_ARRAY: | |
7882 | name = ".preinit_array"; | |
7883 | goto get_vma; | |
7884 | case DT_INIT_ARRAY: | |
7885 | name = ".init_array"; | |
7886 | goto get_vma; | |
7887 | case DT_FINI_ARRAY: | |
7888 | name = ".fini_array"; | |
7889 | goto get_vma; | |
7890 | ||
7891 | case DT_HASH: | |
7892 | name = ".hash"; | |
7893 | goto get_vma; | |
7894 | case DT_STRTAB: | |
7895 | name = ".dynstr"; | |
7896 | goto get_vma; | |
7897 | case DT_SYMTAB: | |
7898 | name = ".dynsym"; | |
7899 | goto get_vma; | |
7900 | case DT_VERDEF: | |
7901 | name = ".gnu.version_d"; | |
7902 | goto get_vma; | |
7903 | case DT_VERNEED: | |
7904 | name = ".gnu.version_r"; | |
7905 | goto get_vma; | |
7906 | case DT_VERSYM: | |
7907 | name = ".gnu.version"; | |
7908 | get_vma: | |
7909 | o = bfd_get_section_by_name (abfd, name); | |
7910 | if (o == NULL) | |
7911 | { | |
7912 | (*_bfd_error_handler) | |
7913 | (_("%s: could not find output section %s"), | |
7914 | bfd_get_filename (abfd), name); | |
7915 | goto error_return; | |
7916 | } | |
7917 | dyn.d_un.d_ptr = o->vma; | |
7918 | break; | |
7919 | ||
7920 | case DT_REL: | |
7921 | case DT_RELA: | |
7922 | case DT_RELSZ: | |
7923 | case DT_RELASZ: | |
7924 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
7925 | type = SHT_REL; | |
7926 | else | |
7927 | type = SHT_RELA; | |
7928 | dyn.d_un.d_val = 0; | |
7929 | for (i = 1; i < elf_numsections (abfd); i++) | |
7930 | { | |
7931 | Elf_Internal_Shdr *hdr; | |
7932 | ||
7933 | hdr = elf_elfsections (abfd)[i]; | |
7934 | if (hdr->sh_type == type | |
7935 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
7936 | { | |
7937 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
7938 | dyn.d_un.d_val += hdr->sh_size; | |
7939 | else | |
7940 | { | |
7941 | if (dyn.d_un.d_val == 0 | |
7942 | || hdr->sh_addr < dyn.d_un.d_val) | |
7943 | dyn.d_un.d_val = hdr->sh_addr; | |
7944 | } | |
7945 | } | |
7946 | } | |
7947 | break; | |
7948 | } | |
7949 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
7950 | } | |
7951 | } | |
7952 | ||
7953 | /* If we have created any dynamic sections, then output them. */ | |
7954 | if (dynobj != NULL) | |
7955 | { | |
7956 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
7957 | goto error_return; | |
7958 | ||
7959 | for (o = dynobj->sections; o != NULL; o = o->next) | |
7960 | { | |
7961 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
7962 | || o->_raw_size == 0 | |
7963 | || o->output_section == bfd_abs_section_ptr) | |
7964 | continue; | |
7965 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
7966 | { | |
7967 | /* At this point, we are only interested in sections | |
7968 | created by _bfd_elf_link_create_dynamic_sections. */ | |
7969 | continue; | |
7970 | } | |
7971 | if ((elf_section_data (o->output_section)->this_hdr.sh_type | |
7972 | != SHT_STRTAB) | |
7973 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
7974 | { | |
7975 | if (! bfd_set_section_contents (abfd, o->output_section, | |
7976 | o->contents, | |
7977 | (file_ptr) o->output_offset, | |
7978 | o->_raw_size)) | |
7979 | goto error_return; | |
7980 | } | |
7981 | else | |
7982 | { | |
7983 | /* The contents of the .dynstr section are actually in a | |
7984 | stringtab. */ | |
7985 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
7986 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
7987 | || ! _bfd_elf_strtab_emit (abfd, | |
7988 | elf_hash_table (info)->dynstr)) | |
7989 | goto error_return; | |
7990 | } | |
7991 | } | |
7992 | } | |
7993 | ||
7994 | if (info->relocatable) | |
7995 | { | |
7996 | bfd_boolean failed = FALSE; | |
7997 | ||
7998 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
7999 | if (failed) | |
8000 | goto error_return; | |
8001 | } | |
8002 | ||
8003 | /* If we have optimized stabs strings, output them. */ | |
8004 | if (elf_hash_table (info)->stab_info != NULL) | |
8005 | { | |
8006 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
8007 | goto error_return; | |
8008 | } | |
8009 | ||
8010 | if (info->eh_frame_hdr) | |
8011 | { | |
8012 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) | |
8013 | goto error_return; | |
8014 | } | |
8015 | ||
8016 | if (finfo.symstrtab != NULL) | |
8017 | _bfd_stringtab_free (finfo.symstrtab); | |
8018 | if (finfo.contents != NULL) | |
8019 | free (finfo.contents); | |
8020 | if (finfo.external_relocs != NULL) | |
8021 | free (finfo.external_relocs); | |
8022 | if (finfo.internal_relocs != NULL) | |
8023 | free (finfo.internal_relocs); | |
8024 | if (finfo.external_syms != NULL) | |
8025 | free (finfo.external_syms); | |
8026 | if (finfo.locsym_shndx != NULL) | |
8027 | free (finfo.locsym_shndx); | |
8028 | if (finfo.internal_syms != NULL) | |
8029 | free (finfo.internal_syms); | |
8030 | if (finfo.indices != NULL) | |
8031 | free (finfo.indices); | |
8032 | if (finfo.sections != NULL) | |
8033 | free (finfo.sections); | |
8034 | if (finfo.symbuf != NULL) | |
8035 | free (finfo.symbuf); | |
8036 | if (finfo.symshndxbuf != NULL) | |
8037 | free (finfo.symshndxbuf); | |
8038 | for (o = abfd->sections; o != NULL; o = o->next) | |
8039 | { | |
8040 | if ((o->flags & SEC_RELOC) != 0 | |
8041 | && elf_section_data (o)->rel_hashes != NULL) | |
8042 | free (elf_section_data (o)->rel_hashes); | |
8043 | } | |
8044 | ||
8045 | elf_tdata (abfd)->linker = TRUE; | |
8046 | ||
8047 | return TRUE; | |
8048 | ||
8049 | error_return: | |
8050 | if (finfo.symstrtab != NULL) | |
8051 | _bfd_stringtab_free (finfo.symstrtab); | |
8052 | if (finfo.contents != NULL) | |
8053 | free (finfo.contents); | |
8054 | if (finfo.external_relocs != NULL) | |
8055 | free (finfo.external_relocs); | |
8056 | if (finfo.internal_relocs != NULL) | |
8057 | free (finfo.internal_relocs); | |
8058 | if (finfo.external_syms != NULL) | |
8059 | free (finfo.external_syms); | |
8060 | if (finfo.locsym_shndx != NULL) | |
8061 | free (finfo.locsym_shndx); | |
8062 | if (finfo.internal_syms != NULL) | |
8063 | free (finfo.internal_syms); | |
8064 | if (finfo.indices != NULL) | |
8065 | free (finfo.indices); | |
8066 | if (finfo.sections != NULL) | |
8067 | free (finfo.sections); | |
8068 | if (finfo.symbuf != NULL) | |
8069 | free (finfo.symbuf); | |
8070 | if (finfo.symshndxbuf != NULL) | |
8071 | free (finfo.symshndxbuf); | |
8072 | for (o = abfd->sections; o != NULL; o = o->next) | |
8073 | { | |
8074 | if ((o->flags & SEC_RELOC) != 0 | |
8075 | && elf_section_data (o)->rel_hashes != NULL) | |
8076 | free (elf_section_data (o)->rel_hashes); | |
8077 | } | |
8078 | ||
8079 | return FALSE; | |
8080 | } | |
8081 | \f | |
8082 | /* Garbage collect unused sections. */ | |
8083 | ||
8084 | /* The mark phase of garbage collection. For a given section, mark | |
8085 | it and any sections in this section's group, and all the sections | |
8086 | which define symbols to which it refers. */ | |
8087 | ||
8088 | typedef asection * (*gc_mark_hook_fn) | |
8089 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
8090 | struct elf_link_hash_entry *, Elf_Internal_Sym *); | |
8091 | ||
8092 | static bfd_boolean | |
8093 | elf_gc_mark (struct bfd_link_info *info, | |
8094 | asection *sec, | |
8095 | gc_mark_hook_fn gc_mark_hook) | |
8096 | { | |
8097 | bfd_boolean ret; | |
8098 | asection *group_sec; | |
8099 | ||
8100 | sec->gc_mark = 1; | |
8101 | ||
8102 | /* Mark all the sections in the group. */ | |
8103 | group_sec = elf_section_data (sec)->next_in_group; | |
8104 | if (group_sec && !group_sec->gc_mark) | |
8105 | if (!elf_gc_mark (info, group_sec, gc_mark_hook)) | |
8106 | return FALSE; | |
8107 | ||
8108 | /* Look through the section relocs. */ | |
8109 | ret = TRUE; | |
8110 | if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0) | |
8111 | { | |
8112 | Elf_Internal_Rela *relstart, *rel, *relend; | |
8113 | Elf_Internal_Shdr *symtab_hdr; | |
8114 | struct elf_link_hash_entry **sym_hashes; | |
8115 | size_t nlocsyms; | |
8116 | size_t extsymoff; | |
8117 | bfd *input_bfd = sec->owner; | |
8118 | const struct elf_backend_data *bed = get_elf_backend_data (input_bfd); | |
8119 | Elf_Internal_Sym *isym = NULL; | |
8120 | int r_sym_shift; | |
8121 | ||
8122 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
8123 | sym_hashes = elf_sym_hashes (input_bfd); | |
8124 | ||
8125 | /* Read the local symbols. */ | |
8126 | if (elf_bad_symtab (input_bfd)) | |
8127 | { | |
8128 | nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
8129 | extsymoff = 0; | |
8130 | } | |
8131 | else | |
8132 | extsymoff = nlocsyms = symtab_hdr->sh_info; | |
8133 | ||
8134 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
8135 | if (isym == NULL && nlocsyms != 0) | |
8136 | { | |
8137 | isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0, | |
8138 | NULL, NULL, NULL); | |
8139 | if (isym == NULL) | |
8140 | return FALSE; | |
8141 | } | |
8142 | ||
8143 | /* Read the relocations. */ | |
8144 | relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL, | |
8145 | info->keep_memory); | |
8146 | if (relstart == NULL) | |
8147 | { | |
8148 | ret = FALSE; | |
8149 | goto out1; | |
8150 | } | |
8151 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
8152 | ||
8153 | if (bed->s->arch_size == 32) | |
8154 | r_sym_shift = 8; | |
8155 | else | |
8156 | r_sym_shift = 32; | |
8157 | ||
8158 | for (rel = relstart; rel < relend; rel++) | |
8159 | { | |
8160 | unsigned long r_symndx; | |
8161 | asection *rsec; | |
8162 | struct elf_link_hash_entry *h; | |
8163 | ||
8164 | r_symndx = rel->r_info >> r_sym_shift; | |
8165 | if (r_symndx == 0) | |
8166 | continue; | |
8167 | ||
8168 | if (r_symndx >= nlocsyms | |
8169 | || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL) | |
8170 | { | |
8171 | h = sym_hashes[r_symndx - extsymoff]; | |
8172 | rsec = (*gc_mark_hook) (sec, info, rel, h, NULL); | |
8173 | } | |
8174 | else | |
8175 | { | |
8176 | rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]); | |
8177 | } | |
8178 | ||
8179 | if (rsec && !rsec->gc_mark) | |
8180 | { | |
8181 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
8182 | rsec->gc_mark = 1; | |
8183 | else if (!elf_gc_mark (info, rsec, gc_mark_hook)) | |
8184 | { | |
8185 | ret = FALSE; | |
8186 | goto out2; | |
8187 | } | |
8188 | } | |
8189 | } | |
8190 | ||
8191 | out2: | |
8192 | if (elf_section_data (sec)->relocs != relstart) | |
8193 | free (relstart); | |
8194 | out1: | |
8195 | if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym) | |
8196 | { | |
8197 | if (! info->keep_memory) | |
8198 | free (isym); | |
8199 | else | |
8200 | symtab_hdr->contents = (unsigned char *) isym; | |
8201 | } | |
8202 | } | |
8203 | ||
8204 | return ret; | |
8205 | } | |
8206 | ||
8207 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ | |
8208 | ||
8209 | static bfd_boolean | |
8210 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr) | |
8211 | { | |
8212 | int *idx = idxptr; | |
8213 | ||
8214 | if (h->root.type == bfd_link_hash_warning) | |
8215 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8216 | ||
8217 | if (h->dynindx != -1 | |
8218 | && ((h->root.type != bfd_link_hash_defined | |
8219 | && h->root.type != bfd_link_hash_defweak) | |
8220 | || h->root.u.def.section->gc_mark)) | |
8221 | h->dynindx = (*idx)++; | |
8222 | ||
8223 | return TRUE; | |
8224 | } | |
8225 | ||
8226 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
8227 | ||
8228 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
8229 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
8230 | ||
8231 | static bfd_boolean | |
8232 | elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook) | |
8233 | { | |
8234 | bfd *sub; | |
8235 | ||
8236 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8237 | { | |
8238 | asection *o; | |
8239 | ||
8240 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
8241 | continue; | |
8242 | ||
8243 | for (o = sub->sections; o != NULL; o = o->next) | |
8244 | { | |
8245 | /* Keep special sections. Keep .debug sections. */ | |
8246 | if ((o->flags & SEC_LINKER_CREATED) | |
8247 | || (o->flags & SEC_DEBUGGING)) | |
8248 | o->gc_mark = 1; | |
8249 | ||
8250 | if (o->gc_mark) | |
8251 | continue; | |
8252 | ||
8253 | /* Skip sweeping sections already excluded. */ | |
8254 | if (o->flags & SEC_EXCLUDE) | |
8255 | continue; | |
8256 | ||
8257 | /* Since this is early in the link process, it is simple | |
8258 | to remove a section from the output. */ | |
8259 | o->flags |= SEC_EXCLUDE; | |
8260 | ||
8261 | /* But we also have to update some of the relocation | |
8262 | info we collected before. */ | |
8263 | if (gc_sweep_hook | |
8264 | && (o->flags & SEC_RELOC) && o->reloc_count > 0) | |
8265 | { | |
8266 | Elf_Internal_Rela *internal_relocs; | |
8267 | bfd_boolean r; | |
8268 | ||
8269 | internal_relocs | |
8270 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
8271 | info->keep_memory); | |
8272 | if (internal_relocs == NULL) | |
8273 | return FALSE; | |
8274 | ||
8275 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
8276 | ||
8277 | if (elf_section_data (o)->relocs != internal_relocs) | |
8278 | free (internal_relocs); | |
8279 | ||
8280 | if (!r) | |
8281 | return FALSE; | |
8282 | } | |
8283 | } | |
8284 | } | |
8285 | ||
8286 | /* Remove the symbols that were in the swept sections from the dynamic | |
8287 | symbol table. GCFIXME: Anyone know how to get them out of the | |
8288 | static symbol table as well? */ | |
8289 | { | |
8290 | int i = 0; | |
8291 | ||
8292 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i); | |
8293 | ||
8294 | elf_hash_table (info)->dynsymcount = i; | |
8295 | } | |
8296 | ||
8297 | return TRUE; | |
8298 | } | |
8299 | ||
8300 | /* Propagate collected vtable information. This is called through | |
8301 | elf_link_hash_traverse. */ | |
8302 | ||
8303 | static bfd_boolean | |
8304 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
8305 | { | |
8306 | if (h->root.type == bfd_link_hash_warning) | |
8307 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8308 | ||
8309 | /* Those that are not vtables. */ | |
8310 | if (h->vtable_parent == NULL) | |
8311 | return TRUE; | |
8312 | ||
8313 | /* Those vtables that do not have parents, we cannot merge. */ | |
8314 | if (h->vtable_parent == (struct elf_link_hash_entry *) -1) | |
8315 | return TRUE; | |
8316 | ||
8317 | /* If we've already been done, exit. */ | |
8318 | if (h->vtable_entries_used && h->vtable_entries_used[-1]) | |
8319 | return TRUE; | |
8320 | ||
8321 | /* Make sure the parent's table is up to date. */ | |
8322 | elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp); | |
8323 | ||
8324 | if (h->vtable_entries_used == NULL) | |
8325 | { | |
8326 | /* None of this table's entries were referenced. Re-use the | |
8327 | parent's table. */ | |
8328 | h->vtable_entries_used = h->vtable_parent->vtable_entries_used; | |
8329 | h->vtable_entries_size = h->vtable_parent->vtable_entries_size; | |
8330 | } | |
8331 | else | |
8332 | { | |
8333 | size_t n; | |
8334 | bfd_boolean *cu, *pu; | |
8335 | ||
8336 | /* Or the parent's entries into ours. */ | |
8337 | cu = h->vtable_entries_used; | |
8338 | cu[-1] = TRUE; | |
8339 | pu = h->vtable_parent->vtable_entries_used; | |
8340 | if (pu != NULL) | |
8341 | { | |
8342 | const struct elf_backend_data *bed; | |
8343 | unsigned int log_file_align; | |
8344 | ||
8345 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
8346 | log_file_align = bed->s->log_file_align; | |
8347 | n = h->vtable_parent->vtable_entries_size >> log_file_align; | |
8348 | while (n--) | |
8349 | { | |
8350 | if (*pu) | |
8351 | *cu = TRUE; | |
8352 | pu++; | |
8353 | cu++; | |
8354 | } | |
8355 | } | |
8356 | } | |
8357 | ||
8358 | return TRUE; | |
8359 | } | |
8360 | ||
8361 | static bfd_boolean | |
8362 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
8363 | { | |
8364 | asection *sec; | |
8365 | bfd_vma hstart, hend; | |
8366 | Elf_Internal_Rela *relstart, *relend, *rel; | |
8367 | const struct elf_backend_data *bed; | |
8368 | unsigned int log_file_align; | |
8369 | ||
8370 | if (h->root.type == bfd_link_hash_warning) | |
8371 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8372 | ||
8373 | /* Take care of both those symbols that do not describe vtables as | |
8374 | well as those that are not loaded. */ | |
8375 | if (h->vtable_parent == NULL) | |
8376 | return TRUE; | |
8377 | ||
8378 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
8379 | || h->root.type == bfd_link_hash_defweak); | |
8380 | ||
8381 | sec = h->root.u.def.section; | |
8382 | hstart = h->root.u.def.value; | |
8383 | hend = hstart + h->size; | |
8384 | ||
8385 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
8386 | if (!relstart) | |
8387 | return *(bfd_boolean *) okp = FALSE; | |
8388 | bed = get_elf_backend_data (sec->owner); | |
8389 | log_file_align = bed->s->log_file_align; | |
8390 | ||
8391 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
8392 | ||
8393 | for (rel = relstart; rel < relend; ++rel) | |
8394 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
8395 | { | |
8396 | /* If the entry is in use, do nothing. */ | |
8397 | if (h->vtable_entries_used | |
8398 | && (rel->r_offset - hstart) < h->vtable_entries_size) | |
8399 | { | |
8400 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
8401 | if (h->vtable_entries_used[entry]) | |
8402 | continue; | |
8403 | } | |
8404 | /* Otherwise, kill it. */ | |
8405 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
8406 | } | |
8407 | ||
8408 | return TRUE; | |
8409 | } | |
8410 | ||
8411 | /* Do mark and sweep of unused sections. */ | |
8412 | ||
8413 | bfd_boolean | |
8414 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
8415 | { | |
8416 | bfd_boolean ok = TRUE; | |
8417 | bfd *sub; | |
8418 | asection * (*gc_mark_hook) | |
8419 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
8420 | struct elf_link_hash_entry *h, Elf_Internal_Sym *); | |
8421 | ||
8422 | if (!get_elf_backend_data (abfd)->can_gc_sections | |
8423 | || info->relocatable | |
8424 | || info->emitrelocations | |
8425 | || !is_elf_hash_table (info->hash) | |
8426 | || elf_hash_table (info)->dynamic_sections_created) | |
8427 | { | |
8428 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
8429 | return TRUE; | |
8430 | } | |
8431 | ||
8432 | /* Apply transitive closure to the vtable entry usage info. */ | |
8433 | elf_link_hash_traverse (elf_hash_table (info), | |
8434 | elf_gc_propagate_vtable_entries_used, | |
8435 | &ok); | |
8436 | if (!ok) | |
8437 | return FALSE; | |
8438 | ||
8439 | /* Kill the vtable relocations that were not used. */ | |
8440 | elf_link_hash_traverse (elf_hash_table (info), | |
8441 | elf_gc_smash_unused_vtentry_relocs, | |
8442 | &ok); | |
8443 | if (!ok) | |
8444 | return FALSE; | |
8445 | ||
8446 | /* Grovel through relocs to find out who stays ... */ | |
8447 | ||
8448 | gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook; | |
8449 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8450 | { | |
8451 | asection *o; | |
8452 | ||
8453 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
8454 | continue; | |
8455 | ||
8456 | for (o = sub->sections; o != NULL; o = o->next) | |
8457 | { | |
8458 | if (o->flags & SEC_KEEP) | |
8459 | if (!elf_gc_mark (info, o, gc_mark_hook)) | |
8460 | return FALSE; | |
8461 | } | |
8462 | } | |
8463 | ||
8464 | /* ... and mark SEC_EXCLUDE for those that go. */ | |
8465 | if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook)) | |
8466 | return FALSE; | |
8467 | ||
8468 | return TRUE; | |
8469 | } | |
8470 | \f | |
8471 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
8472 | ||
8473 | bfd_boolean | |
8474 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
8475 | asection *sec, | |
8476 | struct elf_link_hash_entry *h, | |
8477 | bfd_vma offset) | |
8478 | { | |
8479 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
8480 | struct elf_link_hash_entry **search, *child; | |
8481 | bfd_size_type extsymcount; | |
8482 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8483 | ||
8484 | /* The sh_info field of the symtab header tells us where the | |
8485 | external symbols start. We don't care about the local symbols at | |
8486 | this point. */ | |
8487 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
8488 | if (!elf_bad_symtab (abfd)) | |
8489 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
8490 | ||
8491 | sym_hashes = elf_sym_hashes (abfd); | |
8492 | sym_hashes_end = sym_hashes + extsymcount; | |
8493 | ||
8494 | /* Hunt down the child symbol, which is in this section at the same | |
8495 | offset as the relocation. */ | |
8496 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
8497 | { | |
8498 | if ((child = *search) != NULL | |
8499 | && (child->root.type == bfd_link_hash_defined | |
8500 | || child->root.type == bfd_link_hash_defweak) | |
8501 | && child->root.u.def.section == sec | |
8502 | && child->root.u.def.value == offset) | |
8503 | goto win; | |
8504 | } | |
8505 | ||
8506 | (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT", | |
8507 | bfd_archive_filename (abfd), sec->name, | |
8508 | (unsigned long) offset); | |
8509 | bfd_set_error (bfd_error_invalid_operation); | |
8510 | return FALSE; | |
8511 | ||
8512 | win: | |
8513 | if (!h) | |
8514 | { | |
8515 | /* This *should* only be the absolute section. It could potentially | |
8516 | be that someone has defined a non-global vtable though, which | |
8517 | would be bad. It isn't worth paging in the local symbols to be | |
8518 | sure though; that case should simply be handled by the assembler. */ | |
8519 | ||
8520 | child->vtable_parent = (struct elf_link_hash_entry *) -1; | |
8521 | } | |
8522 | else | |
8523 | child->vtable_parent = h; | |
8524 | ||
8525 | return TRUE; | |
8526 | } | |
8527 | ||
8528 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
8529 | ||
8530 | bfd_boolean | |
8531 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
8532 | asection *sec ATTRIBUTE_UNUSED, | |
8533 | struct elf_link_hash_entry *h, | |
8534 | bfd_vma addend) | |
8535 | { | |
8536 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8537 | unsigned int log_file_align = bed->s->log_file_align; | |
8538 | ||
8539 | if (addend >= h->vtable_entries_size) | |
8540 | { | |
8541 | size_t size, bytes, file_align; | |
8542 | bfd_boolean *ptr = h->vtable_entries_used; | |
8543 | ||
8544 | /* While the symbol is undefined, we have to be prepared to handle | |
8545 | a zero size. */ | |
8546 | file_align = 1 << log_file_align; | |
8547 | if (h->root.type == bfd_link_hash_undefined) | |
8548 | size = addend + file_align; | |
8549 | else | |
8550 | { | |
8551 | size = h->size; | |
8552 | if (addend >= size) | |
8553 | { | |
8554 | /* Oops! We've got a reference past the defined end of | |
8555 | the table. This is probably a bug -- shall we warn? */ | |
8556 | size = addend + file_align; | |
8557 | } | |
8558 | } | |
8559 | size = (size + file_align - 1) & -file_align; | |
8560 | ||
8561 | /* Allocate one extra entry for use as a "done" flag for the | |
8562 | consolidation pass. */ | |
8563 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
8564 | ||
8565 | if (ptr) | |
8566 | { | |
8567 | ptr = bfd_realloc (ptr - 1, bytes); | |
8568 | ||
8569 | if (ptr != NULL) | |
8570 | { | |
8571 | size_t oldbytes; | |
8572 | ||
8573 | oldbytes = (((h->vtable_entries_size >> log_file_align) + 1) | |
8574 | * sizeof (bfd_boolean)); | |
8575 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
8576 | } | |
8577 | } | |
8578 | else | |
8579 | ptr = bfd_zmalloc (bytes); | |
8580 | ||
8581 | if (ptr == NULL) | |
8582 | return FALSE; | |
8583 | ||
8584 | /* And arrange for that done flag to be at index -1. */ | |
8585 | h->vtable_entries_used = ptr + 1; | |
8586 | h->vtable_entries_size = size; | |
8587 | } | |
8588 | ||
8589 | h->vtable_entries_used[addend >> log_file_align] = TRUE; | |
8590 | ||
8591 | return TRUE; | |
8592 | } | |
8593 | ||
8594 | struct alloc_got_off_arg { | |
8595 | bfd_vma gotoff; | |
8596 | unsigned int got_elt_size; | |
8597 | }; | |
8598 | ||
8599 | /* We need a special top-level link routine to convert got reference counts | |
8600 | to real got offsets. */ | |
8601 | ||
8602 | static bfd_boolean | |
8603 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
8604 | { | |
8605 | struct alloc_got_off_arg *gofarg = arg; | |
8606 | ||
8607 | if (h->root.type == bfd_link_hash_warning) | |
8608 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8609 | ||
8610 | if (h->got.refcount > 0) | |
8611 | { | |
8612 | h->got.offset = gofarg->gotoff; | |
8613 | gofarg->gotoff += gofarg->got_elt_size; | |
8614 | } | |
8615 | else | |
8616 | h->got.offset = (bfd_vma) -1; | |
8617 | ||
8618 | return TRUE; | |
8619 | } | |
8620 | ||
8621 | /* And an accompanying bit to work out final got entry offsets once | |
8622 | we're done. Should be called from final_link. */ | |
8623 | ||
8624 | bfd_boolean | |
8625 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
8626 | struct bfd_link_info *info) | |
8627 | { | |
8628 | bfd *i; | |
8629 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8630 | bfd_vma gotoff; | |
8631 | unsigned int got_elt_size = bed->s->arch_size / 8; | |
8632 | struct alloc_got_off_arg gofarg; | |
8633 | ||
8634 | if (! is_elf_hash_table (info->hash)) | |
8635 | return FALSE; | |
8636 | ||
8637 | /* The GOT offset is relative to the .got section, but the GOT header is | |
8638 | put into the .got.plt section, if the backend uses it. */ | |
8639 | if (bed->want_got_plt) | |
8640 | gotoff = 0; | |
8641 | else | |
8642 | gotoff = bed->got_header_size; | |
8643 | ||
8644 | /* Do the local .got entries first. */ | |
8645 | for (i = info->input_bfds; i; i = i->link_next) | |
8646 | { | |
8647 | bfd_signed_vma *local_got; | |
8648 | bfd_size_type j, locsymcount; | |
8649 | Elf_Internal_Shdr *symtab_hdr; | |
8650 | ||
8651 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
8652 | continue; | |
8653 | ||
8654 | local_got = elf_local_got_refcounts (i); | |
8655 | if (!local_got) | |
8656 | continue; | |
8657 | ||
8658 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
8659 | if (elf_bad_symtab (i)) | |
8660 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
8661 | else | |
8662 | locsymcount = symtab_hdr->sh_info; | |
8663 | ||
8664 | for (j = 0; j < locsymcount; ++j) | |
8665 | { | |
8666 | if (local_got[j] > 0) | |
8667 | { | |
8668 | local_got[j] = gotoff; | |
8669 | gotoff += got_elt_size; | |
8670 | } | |
8671 | else | |
8672 | local_got[j] = (bfd_vma) -1; | |
8673 | } | |
8674 | } | |
8675 | ||
8676 | /* Then the global .got entries. .plt refcounts are handled by | |
8677 | adjust_dynamic_symbol */ | |
8678 | gofarg.gotoff = gotoff; | |
8679 | gofarg.got_elt_size = got_elt_size; | |
8680 | elf_link_hash_traverse (elf_hash_table (info), | |
8681 | elf_gc_allocate_got_offsets, | |
8682 | &gofarg); | |
8683 | return TRUE; | |
8684 | } | |
8685 | ||
8686 | /* Many folk need no more in the way of final link than this, once | |
8687 | got entry reference counting is enabled. */ | |
8688 | ||
8689 | bfd_boolean | |
8690 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
8691 | { | |
8692 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
8693 | return FALSE; | |
8694 | ||
8695 | /* Invoke the regular ELF backend linker to do all the work. */ | |
8696 | return bfd_elf_final_link (abfd, info); | |
8697 | } | |
8698 | ||
8699 | bfd_boolean | |
8700 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
8701 | { | |
8702 | struct elf_reloc_cookie *rcookie = cookie; | |
8703 | ||
8704 | if (rcookie->bad_symtab) | |
8705 | rcookie->rel = rcookie->rels; | |
8706 | ||
8707 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
8708 | { | |
8709 | unsigned long r_symndx; | |
8710 | ||
8711 | if (! rcookie->bad_symtab) | |
8712 | if (rcookie->rel->r_offset > offset) | |
8713 | return FALSE; | |
8714 | if (rcookie->rel->r_offset != offset) | |
8715 | continue; | |
8716 | ||
8717 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
8718 | if (r_symndx == SHN_UNDEF) | |
8719 | return TRUE; | |
8720 | ||
8721 | if (r_symndx >= rcookie->locsymcount | |
8722 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
8723 | { | |
8724 | struct elf_link_hash_entry *h; | |
8725 | ||
8726 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
8727 | ||
8728 | while (h->root.type == bfd_link_hash_indirect | |
8729 | || h->root.type == bfd_link_hash_warning) | |
8730 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8731 | ||
8732 | if ((h->root.type == bfd_link_hash_defined | |
8733 | || h->root.type == bfd_link_hash_defweak) | |
8734 | && elf_discarded_section (h->root.u.def.section)) | |
8735 | return TRUE; | |
8736 | else | |
8737 | return FALSE; | |
8738 | } | |
8739 | else | |
8740 | { | |
8741 | /* It's not a relocation against a global symbol, | |
8742 | but it could be a relocation against a local | |
8743 | symbol for a discarded section. */ | |
8744 | asection *isec; | |
8745 | Elf_Internal_Sym *isym; | |
8746 | ||
8747 | /* Need to: get the symbol; get the section. */ | |
8748 | isym = &rcookie->locsyms[r_symndx]; | |
8749 | if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
8750 | { | |
8751 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); | |
8752 | if (isec != NULL && elf_discarded_section (isec)) | |
8753 | return TRUE; | |
8754 | } | |
8755 | } | |
8756 | return FALSE; | |
8757 | } | |
8758 | return FALSE; | |
8759 | } | |
8760 | ||
8761 | /* Discard unneeded references to discarded sections. | |
8762 | Returns TRUE if any section's size was changed. */ | |
8763 | /* This function assumes that the relocations are in sorted order, | |
8764 | which is true for all known assemblers. */ | |
8765 | ||
8766 | bfd_boolean | |
8767 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) | |
8768 | { | |
8769 | struct elf_reloc_cookie cookie; | |
8770 | asection *stab, *eh; | |
8771 | Elf_Internal_Shdr *symtab_hdr; | |
8772 | const struct elf_backend_data *bed; | |
8773 | bfd *abfd; | |
8774 | unsigned int count; | |
8775 | bfd_boolean ret = FALSE; | |
8776 | ||
8777 | if (info->traditional_format | |
8778 | || !is_elf_hash_table (info->hash)) | |
8779 | return FALSE; | |
8780 | ||
8781 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) | |
8782 | { | |
8783 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
8784 | continue; | |
8785 | ||
8786 | bed = get_elf_backend_data (abfd); | |
8787 | ||
8788 | if ((abfd->flags & DYNAMIC) != 0) | |
8789 | continue; | |
8790 | ||
8791 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); | |
8792 | if (info->relocatable | |
8793 | || (eh != NULL | |
8794 | && (eh->_raw_size == 0 | |
8795 | || bfd_is_abs_section (eh->output_section)))) | |
8796 | eh = NULL; | |
8797 | ||
8798 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
8799 | if (stab != NULL | |
8800 | && (stab->_raw_size == 0 | |
8801 | || bfd_is_abs_section (stab->output_section) | |
8802 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) | |
8803 | stab = NULL; | |
8804 | ||
8805 | if (stab == NULL | |
8806 | && eh == NULL | |
8807 | && bed->elf_backend_discard_info == NULL) | |
8808 | continue; | |
8809 | ||
8810 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
8811 | cookie.abfd = abfd; | |
8812 | cookie.sym_hashes = elf_sym_hashes (abfd); | |
8813 | cookie.bad_symtab = elf_bad_symtab (abfd); | |
8814 | if (cookie.bad_symtab) | |
8815 | { | |
8816 | cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
8817 | cookie.extsymoff = 0; | |
8818 | } | |
8819 | else | |
8820 | { | |
8821 | cookie.locsymcount = symtab_hdr->sh_info; | |
8822 | cookie.extsymoff = symtab_hdr->sh_info; | |
8823 | } | |
8824 | ||
8825 | if (bed->s->arch_size == 32) | |
8826 | cookie.r_sym_shift = 8; | |
8827 | else | |
8828 | cookie.r_sym_shift = 32; | |
8829 | ||
8830 | cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
8831 | if (cookie.locsyms == NULL && cookie.locsymcount != 0) | |
8832 | { | |
8833 | cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
8834 | cookie.locsymcount, 0, | |
8835 | NULL, NULL, NULL); | |
8836 | if (cookie.locsyms == NULL) | |
8837 | return FALSE; | |
8838 | } | |
8839 | ||
8840 | if (stab != NULL) | |
8841 | { | |
8842 | cookie.rels = NULL; | |
8843 | count = stab->reloc_count; | |
8844 | if (count != 0) | |
8845 | cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL, | |
8846 | info->keep_memory); | |
8847 | if (cookie.rels != NULL) | |
8848 | { | |
8849 | cookie.rel = cookie.rels; | |
8850 | cookie.relend = cookie.rels; | |
8851 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
8852 | if (_bfd_discard_section_stabs (abfd, stab, | |
8853 | elf_section_data (stab)->sec_info, | |
8854 | bfd_elf_reloc_symbol_deleted_p, | |
8855 | &cookie)) | |
8856 | ret = TRUE; | |
8857 | if (elf_section_data (stab)->relocs != cookie.rels) | |
8858 | free (cookie.rels); | |
8859 | } | |
8860 | } | |
8861 | ||
8862 | if (eh != NULL) | |
8863 | { | |
8864 | cookie.rels = NULL; | |
8865 | count = eh->reloc_count; | |
8866 | if (count != 0) | |
8867 | cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL, | |
8868 | info->keep_memory); | |
8869 | cookie.rel = cookie.rels; | |
8870 | cookie.relend = cookie.rels; | |
8871 | if (cookie.rels != NULL) | |
8872 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
8873 | ||
8874 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, | |
8875 | bfd_elf_reloc_symbol_deleted_p, | |
8876 | &cookie)) | |
8877 | ret = TRUE; | |
8878 | ||
8879 | if (cookie.rels != NULL | |
8880 | && elf_section_data (eh)->relocs != cookie.rels) | |
8881 | free (cookie.rels); | |
8882 | } | |
8883 | ||
8884 | if (bed->elf_backend_discard_info != NULL | |
8885 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
8886 | ret = TRUE; | |
8887 | ||
8888 | if (cookie.locsyms != NULL | |
8889 | && symtab_hdr->contents != (unsigned char *) cookie.locsyms) | |
8890 | { | |
8891 | if (! info->keep_memory) | |
8892 | free (cookie.locsyms); | |
8893 | else | |
8894 | symtab_hdr->contents = (unsigned char *) cookie.locsyms; | |
8895 | } | |
8896 | } | |
8897 | ||
8898 | if (info->eh_frame_hdr | |
8899 | && !info->relocatable | |
8900 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
8901 | ret = TRUE; | |
8902 | ||
8903 | return ret; | |
8904 | } |