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