Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
7e9f0867 | 2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 |
7898deda | 3 | Free Software Foundation, Inc. |
252b5132 | 4 | |
8fdd7217 | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
8fdd7217 NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
252b5132 | 11 | |
8fdd7217 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
8fdd7217 NC |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
252b5132 RH |
20 | |
21 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
23 | #include "bfdlink.h" | |
24 | #include "libbfd.h" | |
25 | #define ARCH_SIZE 0 | |
26 | #include "elf-bfd.h" | |
4ad4eba5 | 27 | #include "safe-ctype.h" |
ccf2f652 | 28 | #include "libiberty.h" |
252b5132 | 29 | |
b34976b6 | 30 | bfd_boolean |
268b6b39 | 31 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
32 | { |
33 | flagword flags; | |
aad5d350 | 34 | asection *s; |
252b5132 | 35 | struct elf_link_hash_entry *h; |
14a793b2 | 36 | struct bfd_link_hash_entry *bh; |
9c5bfbb7 | 37 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
38 | int ptralign; |
39 | ||
40 | /* This function may be called more than once. */ | |
aad5d350 AM |
41 | s = bfd_get_section_by_name (abfd, ".got"); |
42 | if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0) | |
b34976b6 | 43 | return TRUE; |
252b5132 RH |
44 | |
45 | switch (bed->s->arch_size) | |
46 | { | |
bb0deeff AO |
47 | case 32: |
48 | ptralign = 2; | |
49 | break; | |
50 | ||
51 | case 64: | |
52 | ptralign = 3; | |
53 | break; | |
54 | ||
55 | default: | |
56 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 57 | return FALSE; |
252b5132 RH |
58 | } |
59 | ||
e5a52504 | 60 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
61 | |
62 | s = bfd_make_section (abfd, ".got"); | |
63 | if (s == NULL | |
64 | || !bfd_set_section_flags (abfd, s, flags) | |
65 | || !bfd_set_section_alignment (abfd, s, ptralign)) | |
b34976b6 | 66 | return FALSE; |
252b5132 RH |
67 | |
68 | if (bed->want_got_plt) | |
69 | { | |
70 | s = bfd_make_section (abfd, ".got.plt"); | |
71 | if (s == NULL | |
72 | || !bfd_set_section_flags (abfd, s, flags) | |
73 | || !bfd_set_section_alignment (abfd, s, ptralign)) | |
b34976b6 | 74 | return FALSE; |
252b5132 RH |
75 | } |
76 | ||
2517a57f AM |
77 | if (bed->want_got_sym) |
78 | { | |
79 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
80 | (or .got.plt) section. We don't do this in the linker script | |
81 | because we don't want to define the symbol if we are not creating | |
82 | a global offset table. */ | |
14a793b2 | 83 | bh = NULL; |
2517a57f AM |
84 | if (!(_bfd_generic_link_add_one_symbol |
85 | (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, | |
268b6b39 | 86 | bed->got_symbol_offset, NULL, FALSE, bed->collect, &bh))) |
b34976b6 | 87 | return FALSE; |
14a793b2 | 88 | h = (struct elf_link_hash_entry *) bh; |
f5385ebf | 89 | h->def_regular = 1; |
2517a57f | 90 | h->type = STT_OBJECT; |
e6857c0c | 91 | h->other = STV_HIDDEN; |
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 | |
7e9f0867 AM |
106 | /* Create a strtab to hold the dynamic symbol names. */ |
107 | static bfd_boolean | |
108 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
109 | { | |
110 | struct elf_link_hash_table *hash_table; | |
111 | ||
112 | hash_table = elf_hash_table (info); | |
113 | if (hash_table->dynobj == NULL) | |
114 | hash_table->dynobj = abfd; | |
115 | ||
116 | if (hash_table->dynstr == NULL) | |
117 | { | |
118 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
119 | if (hash_table->dynstr == NULL) | |
120 | return FALSE; | |
121 | } | |
122 | return TRUE; | |
123 | } | |
124 | ||
45d6a902 AM |
125 | /* Create some sections which will be filled in with dynamic linking |
126 | information. ABFD is an input file which requires dynamic sections | |
127 | to be created. The dynamic sections take up virtual memory space | |
128 | when the final executable is run, so we need to create them before | |
129 | addresses are assigned to the output sections. We work out the | |
130 | actual contents and size of these sections later. */ | |
252b5132 | 131 | |
b34976b6 | 132 | bfd_boolean |
268b6b39 | 133 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 134 | { |
45d6a902 AM |
135 | flagword flags; |
136 | register asection *s; | |
137 | struct elf_link_hash_entry *h; | |
138 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 139 | const struct elf_backend_data *bed; |
252b5132 | 140 | |
0eddce27 | 141 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
142 | return FALSE; |
143 | ||
144 | if (elf_hash_table (info)->dynamic_sections_created) | |
145 | return TRUE; | |
146 | ||
7e9f0867 AM |
147 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
148 | return FALSE; | |
45d6a902 | 149 | |
7e9f0867 | 150 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
151 | bed = get_elf_backend_data (abfd); |
152 | ||
153 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
154 | |
155 | /* A dynamically linked executable has a .interp section, but a | |
156 | shared library does not. */ | |
36af4a4e | 157 | if (info->executable) |
252b5132 | 158 | { |
45d6a902 AM |
159 | s = bfd_make_section (abfd, ".interp"); |
160 | if (s == NULL | |
161 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
162 | return FALSE; | |
163 | } | |
bb0deeff | 164 | |
0eddce27 | 165 | if (! info->traditional_format) |
45d6a902 AM |
166 | { |
167 | s = bfd_make_section (abfd, ".eh_frame_hdr"); | |
168 | if (s == NULL | |
169 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
170 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
171 | return FALSE; | |
172 | elf_hash_table (info)->eh_info.hdr_sec = s; | |
173 | } | |
bb0deeff | 174 | |
45d6a902 AM |
175 | /* Create sections to hold version informations. These are removed |
176 | if they are not needed. */ | |
177 | s = bfd_make_section (abfd, ".gnu.version_d"); | |
178 | if (s == NULL | |
179 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
180 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
181 | return FALSE; | |
182 | ||
183 | s = bfd_make_section (abfd, ".gnu.version"); | |
184 | if (s == NULL | |
185 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
186 | || ! bfd_set_section_alignment (abfd, s, 1)) | |
187 | return FALSE; | |
188 | ||
189 | s = bfd_make_section (abfd, ".gnu.version_r"); | |
190 | if (s == NULL | |
191 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
192 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
193 | return FALSE; | |
194 | ||
195 | s = bfd_make_section (abfd, ".dynsym"); | |
196 | if (s == NULL | |
197 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
198 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
199 | return FALSE; | |
200 | ||
201 | s = bfd_make_section (abfd, ".dynstr"); | |
202 | if (s == NULL | |
203 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
204 | return FALSE; | |
205 | ||
45d6a902 AM |
206 | s = bfd_make_section (abfd, ".dynamic"); |
207 | if (s == NULL | |
208 | || ! bfd_set_section_flags (abfd, s, flags) | |
209 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
210 | return FALSE; | |
211 | ||
212 | /* The special symbol _DYNAMIC is always set to the start of the | |
213 | .dynamic section. This call occurs before we have processed the | |
214 | symbols for any dynamic object, so we don't have to worry about | |
215 | overriding a dynamic definition. We could set _DYNAMIC in a | |
216 | linker script, but we only want to define it if we are, in fact, | |
217 | creating a .dynamic section. We don't want to define it if there | |
218 | is no .dynamic section, since on some ELF platforms the start up | |
219 | code examines it to decide how to initialize the process. */ | |
220 | bh = NULL; | |
221 | if (! (_bfd_generic_link_add_one_symbol | |
268b6b39 AM |
222 | (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE, |
223 | get_elf_backend_data (abfd)->collect, &bh))) | |
45d6a902 AM |
224 | return FALSE; |
225 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf | 226 | h->def_regular = 1; |
45d6a902 AM |
227 | h->type = STT_OBJECT; |
228 | ||
36af4a4e | 229 | if (! info->executable |
c152c796 | 230 | && ! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
231 | return FALSE; |
232 | ||
233 | s = bfd_make_section (abfd, ".hash"); | |
234 | if (s == NULL | |
235 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
236 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
237 | return FALSE; | |
238 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
239 | ||
240 | /* Let the backend create the rest of the sections. This lets the | |
241 | backend set the right flags. The backend will normally create | |
242 | the .got and .plt sections. */ | |
243 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
244 | return FALSE; | |
245 | ||
246 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
247 | ||
248 | return TRUE; | |
249 | } | |
250 | ||
251 | /* Create dynamic sections when linking against a dynamic object. */ | |
252 | ||
253 | bfd_boolean | |
268b6b39 | 254 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
255 | { |
256 | flagword flags, pltflags; | |
257 | asection *s; | |
9c5bfbb7 | 258 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 259 | |
252b5132 RH |
260 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
261 | .rel[a].bss sections. */ | |
e5a52504 | 262 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
263 | |
264 | pltflags = flags; | |
252b5132 | 265 | if (bed->plt_not_loaded) |
6df4d94c MM |
266 | /* We do not clear SEC_ALLOC here because we still want the OS to |
267 | allocate space for the section; it's just that there's nothing | |
268 | to read in from the object file. */ | |
5d1634d7 | 269 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
270 | else |
271 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
272 | if (bed->plt_readonly) |
273 | pltflags |= SEC_READONLY; | |
274 | ||
275 | s = bfd_make_section (abfd, ".plt"); | |
276 | if (s == NULL | |
277 | || ! bfd_set_section_flags (abfd, s, pltflags) | |
278 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) | |
b34976b6 | 279 | return FALSE; |
252b5132 RH |
280 | |
281 | if (bed->want_plt_sym) | |
282 | { | |
283 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the | |
284 | .plt section. */ | |
14a793b2 AM |
285 | struct elf_link_hash_entry *h; |
286 | struct bfd_link_hash_entry *bh = NULL; | |
287 | ||
252b5132 | 288 | if (! (_bfd_generic_link_add_one_symbol |
268b6b39 AM |
289 | (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL, |
290 | FALSE, get_elf_backend_data (abfd)->collect, &bh))) | |
b34976b6 | 291 | return FALSE; |
14a793b2 | 292 | h = (struct elf_link_hash_entry *) bh; |
f5385ebf | 293 | h->def_regular = 1; |
252b5132 RH |
294 | h->type = STT_OBJECT; |
295 | ||
36af4a4e | 296 | if (! info->executable |
c152c796 | 297 | && ! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 298 | return FALSE; |
252b5132 RH |
299 | } |
300 | ||
3e932841 | 301 | s = bfd_make_section (abfd, |
bf572ba0 | 302 | bed->default_use_rela_p ? ".rela.plt" : ".rel.plt"); |
252b5132 RH |
303 | if (s == NULL |
304 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
45d6a902 | 305 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 306 | return FALSE; |
252b5132 RH |
307 | |
308 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 309 | return FALSE; |
252b5132 | 310 | |
3018b441 RH |
311 | if (bed->want_dynbss) |
312 | { | |
313 | /* The .dynbss section is a place to put symbols which are defined | |
314 | by dynamic objects, are referenced by regular objects, and are | |
315 | not functions. We must allocate space for them in the process | |
316 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
317 | initialize them at run time. The linker script puts the .dynbss | |
318 | section into the .bss section of the final image. */ | |
319 | s = bfd_make_section (abfd, ".dynbss"); | |
320 | if (s == NULL | |
77f3d027 | 321 | || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED)) |
b34976b6 | 322 | return FALSE; |
252b5132 | 323 | |
3018b441 | 324 | /* The .rel[a].bss section holds copy relocs. This section is not |
252b5132 RH |
325 | normally needed. We need to create it here, though, so that the |
326 | linker will map it to an output section. We can't just create it | |
327 | only if we need it, because we will not know whether we need it | |
328 | until we have seen all the input files, and the first time the | |
329 | main linker code calls BFD after examining all the input files | |
330 | (size_dynamic_sections) the input sections have already been | |
331 | mapped to the output sections. If the section turns out not to | |
332 | be needed, we can discard it later. We will never need this | |
333 | section when generating a shared object, since they do not use | |
334 | copy relocs. */ | |
3018b441 RH |
335 | if (! info->shared) |
336 | { | |
3e932841 KH |
337 | s = bfd_make_section (abfd, |
338 | (bed->default_use_rela_p | |
339 | ? ".rela.bss" : ".rel.bss")); | |
3018b441 RH |
340 | if (s == NULL |
341 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
45d6a902 | 342 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 343 | return FALSE; |
3018b441 | 344 | } |
252b5132 RH |
345 | } |
346 | ||
b34976b6 | 347 | return TRUE; |
252b5132 RH |
348 | } |
349 | \f | |
252b5132 RH |
350 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
351 | read the input files, since we need to have a list of all of them | |
352 | before we can determine the final sizes of the output sections. | |
353 | Note that we may actually call this function even though we are not | |
354 | going to output any dynamic symbols; in some cases we know that a | |
355 | symbol should be in the dynamic symbol table, but only if there is | |
356 | one. */ | |
357 | ||
b34976b6 | 358 | bfd_boolean |
c152c796 AM |
359 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
360 | struct elf_link_hash_entry *h) | |
252b5132 RH |
361 | { |
362 | if (h->dynindx == -1) | |
363 | { | |
2b0f7ef9 | 364 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 365 | char *p; |
252b5132 | 366 | const char *name; |
252b5132 RH |
367 | bfd_size_type indx; |
368 | ||
7a13edea NC |
369 | /* XXX: The ABI draft says the linker must turn hidden and |
370 | internal symbols into STB_LOCAL symbols when producing the | |
371 | DSO. However, if ld.so honors st_other in the dynamic table, | |
372 | this would not be necessary. */ | |
373 | switch (ELF_ST_VISIBILITY (h->other)) | |
374 | { | |
375 | case STV_INTERNAL: | |
376 | case STV_HIDDEN: | |
9d6eee78 L |
377 | if (h->root.type != bfd_link_hash_undefined |
378 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 379 | { |
f5385ebf | 380 | h->forced_local = 1; |
b34976b6 | 381 | return TRUE; |
7a13edea | 382 | } |
0444bdd4 | 383 | |
7a13edea NC |
384 | default: |
385 | break; | |
386 | } | |
387 | ||
252b5132 RH |
388 | h->dynindx = elf_hash_table (info)->dynsymcount; |
389 | ++elf_hash_table (info)->dynsymcount; | |
390 | ||
391 | dynstr = elf_hash_table (info)->dynstr; | |
392 | if (dynstr == NULL) | |
393 | { | |
394 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 395 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 396 | if (dynstr == NULL) |
b34976b6 | 397 | return FALSE; |
252b5132 RH |
398 | } |
399 | ||
400 | /* We don't put any version information in the dynamic string | |
aad5d350 | 401 | table. */ |
252b5132 RH |
402 | name = h->root.root.string; |
403 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
404 | if (p != NULL) |
405 | /* We know that the p points into writable memory. In fact, | |
406 | there are only a few symbols that have read-only names, being | |
407 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
408 | by the backends. Most symbols will have names pointing into | |
409 | an ELF string table read from a file, or to objalloc memory. */ | |
410 | *p = 0; | |
411 | ||
412 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
413 | ||
414 | if (p != NULL) | |
415 | *p = ELF_VER_CHR; | |
252b5132 RH |
416 | |
417 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 418 | return FALSE; |
252b5132 RH |
419 | h->dynstr_index = indx; |
420 | } | |
421 | ||
b34976b6 | 422 | return TRUE; |
252b5132 | 423 | } |
45d6a902 AM |
424 | \f |
425 | /* Record an assignment to a symbol made by a linker script. We need | |
426 | this in case some dynamic object refers to this symbol. */ | |
427 | ||
428 | bfd_boolean | |
268b6b39 AM |
429 | bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED, |
430 | struct bfd_link_info *info, | |
431 | const char *name, | |
432 | bfd_boolean provide) | |
45d6a902 AM |
433 | { |
434 | struct elf_link_hash_entry *h; | |
435 | ||
0eddce27 | 436 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
437 | return TRUE; |
438 | ||
439 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE); | |
440 | if (h == NULL) | |
441 | return FALSE; | |
442 | ||
02bb6eae AO |
443 | /* Since we're defining the symbol, don't let it seem to have not |
444 | been defined. record_dynamic_symbol and size_dynamic_sections | |
a010d60f AM |
445 | may depend on this. |
446 | ??? Changing bfd_link_hash_undefined to bfd_link_hash_new (or | |
447 | to bfd_link_hash_undefweak, see linker.c:link_action) runs the risk | |
448 | of some later symbol manipulation setting the symbol back to | |
449 | bfd_link_hash_undefined, and the linker trying to add the symbol to | |
450 | the undefs list twice. */ | |
02bb6eae AO |
451 | if (h->root.type == bfd_link_hash_undefweak |
452 | || h->root.type == bfd_link_hash_undefined) | |
453 | h->root.type = bfd_link_hash_new; | |
454 | ||
45d6a902 | 455 | if (h->root.type == bfd_link_hash_new) |
f5385ebf | 456 | h->non_elf = 0; |
45d6a902 AM |
457 | |
458 | /* If this symbol is being provided by the linker script, and it is | |
459 | currently defined by a dynamic object, but not by a regular | |
460 | object, then mark it as undefined so that the generic linker will | |
461 | force the correct value. */ | |
462 | if (provide | |
f5385ebf AM |
463 | && h->def_dynamic |
464 | && !h->def_regular) | |
45d6a902 AM |
465 | h->root.type = bfd_link_hash_undefined; |
466 | ||
467 | /* If this symbol is not being provided by the linker script, and it is | |
468 | currently defined by a dynamic object, but not by a regular object, | |
469 | then clear out any version information because the symbol will not be | |
470 | associated with the dynamic object any more. */ | |
471 | if (!provide | |
f5385ebf AM |
472 | && h->def_dynamic |
473 | && !h->def_regular) | |
45d6a902 AM |
474 | h->verinfo.verdef = NULL; |
475 | ||
f5385ebf | 476 | h->def_regular = 1; |
45d6a902 | 477 | |
f5385ebf AM |
478 | if ((h->def_dynamic |
479 | || h->ref_dynamic | |
45d6a902 AM |
480 | || info->shared) |
481 | && h->dynindx == -1) | |
482 | { | |
c152c796 | 483 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
484 | return FALSE; |
485 | ||
486 | /* If this is a weak defined symbol, and we know a corresponding | |
487 | real symbol from the same dynamic object, make sure the real | |
488 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
489 | if (h->u.weakdef != NULL |
490 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 491 | { |
f6e332e6 | 492 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
493 | return FALSE; |
494 | } | |
495 | } | |
496 | ||
497 | return TRUE; | |
498 | } | |
42751cf3 | 499 | |
8c58d23b AM |
500 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
501 | success, and 2 on a failure caused by attempting to record a symbol | |
502 | in a discarded section, eg. a discarded link-once section symbol. */ | |
503 | ||
504 | int | |
c152c796 AM |
505 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
506 | bfd *input_bfd, | |
507 | long input_indx) | |
8c58d23b AM |
508 | { |
509 | bfd_size_type amt; | |
510 | struct elf_link_local_dynamic_entry *entry; | |
511 | struct elf_link_hash_table *eht; | |
512 | struct elf_strtab_hash *dynstr; | |
513 | unsigned long dynstr_index; | |
514 | char *name; | |
515 | Elf_External_Sym_Shndx eshndx; | |
516 | char esym[sizeof (Elf64_External_Sym)]; | |
517 | ||
0eddce27 | 518 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
519 | return 0; |
520 | ||
521 | /* See if the entry exists already. */ | |
522 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
523 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
524 | return 1; | |
525 | ||
526 | amt = sizeof (*entry); | |
268b6b39 | 527 | entry = bfd_alloc (input_bfd, amt); |
8c58d23b AM |
528 | if (entry == NULL) |
529 | return 0; | |
530 | ||
531 | /* Go find the symbol, so that we can find it's name. */ | |
532 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 533 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
534 | { |
535 | bfd_release (input_bfd, entry); | |
536 | return 0; | |
537 | } | |
538 | ||
539 | if (entry->isym.st_shndx != SHN_UNDEF | |
540 | && (entry->isym.st_shndx < SHN_LORESERVE | |
541 | || entry->isym.st_shndx > SHN_HIRESERVE)) | |
542 | { | |
543 | asection *s; | |
544 | ||
545 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
546 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
547 | { | |
548 | /* We can still bfd_release here as nothing has done another | |
549 | bfd_alloc. We can't do this later in this function. */ | |
550 | bfd_release (input_bfd, entry); | |
551 | return 2; | |
552 | } | |
553 | } | |
554 | ||
555 | name = (bfd_elf_string_from_elf_section | |
556 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
557 | entry->isym.st_name)); | |
558 | ||
559 | dynstr = elf_hash_table (info)->dynstr; | |
560 | if (dynstr == NULL) | |
561 | { | |
562 | /* Create a strtab to hold the dynamic symbol names. */ | |
563 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
564 | if (dynstr == NULL) | |
565 | return 0; | |
566 | } | |
567 | ||
b34976b6 | 568 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
569 | if (dynstr_index == (unsigned long) -1) |
570 | return 0; | |
571 | entry->isym.st_name = dynstr_index; | |
572 | ||
573 | eht = elf_hash_table (info); | |
574 | ||
575 | entry->next = eht->dynlocal; | |
576 | eht->dynlocal = entry; | |
577 | entry->input_bfd = input_bfd; | |
578 | entry->input_indx = input_indx; | |
579 | eht->dynsymcount++; | |
580 | ||
581 | /* Whatever binding the symbol had before, it's now local. */ | |
582 | entry->isym.st_info | |
583 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
584 | ||
585 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
586 | ||
587 | return 1; | |
588 | } | |
589 | ||
30b30c21 | 590 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 591 | |
30b30c21 | 592 | long |
268b6b39 AM |
593 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
594 | bfd *input_bfd, | |
595 | long input_indx) | |
30b30c21 RH |
596 | { |
597 | struct elf_link_local_dynamic_entry *e; | |
598 | ||
599 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
600 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
601 | return e->dynindx; | |
602 | return -1; | |
603 | } | |
604 | ||
605 | /* This function is used to renumber the dynamic symbols, if some of | |
606 | them are removed because they are marked as local. This is called | |
607 | via elf_link_hash_traverse. */ | |
608 | ||
b34976b6 | 609 | static bfd_boolean |
268b6b39 AM |
610 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
611 | void *data) | |
42751cf3 | 612 | { |
268b6b39 | 613 | size_t *count = data; |
30b30c21 | 614 | |
e92d460e AM |
615 | if (h->root.type == bfd_link_hash_warning) |
616 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
617 | ||
42751cf3 | 618 | if (h->dynindx != -1) |
30b30c21 RH |
619 | h->dynindx = ++(*count); |
620 | ||
b34976b6 | 621 | return TRUE; |
42751cf3 | 622 | } |
30b30c21 | 623 | |
aee6f5b4 AO |
624 | /* Return true if the dynamic symbol for a given section should be |
625 | omitted when creating a shared library. */ | |
626 | bfd_boolean | |
627 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
628 | struct bfd_link_info *info, | |
629 | asection *p) | |
630 | { | |
631 | switch (elf_section_data (p)->this_hdr.sh_type) | |
632 | { | |
633 | case SHT_PROGBITS: | |
634 | case SHT_NOBITS: | |
635 | /* If sh_type is yet undecided, assume it could be | |
636 | SHT_PROGBITS/SHT_NOBITS. */ | |
637 | case SHT_NULL: | |
638 | if (strcmp (p->name, ".got") == 0 | |
639 | || strcmp (p->name, ".got.plt") == 0 | |
640 | || strcmp (p->name, ".plt") == 0) | |
641 | { | |
642 | asection *ip; | |
643 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
644 | ||
645 | if (dynobj != NULL | |
1da212d6 | 646 | && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL |
aee6f5b4 AO |
647 | && (ip->flags & SEC_LINKER_CREATED) |
648 | && ip->output_section == p) | |
649 | return TRUE; | |
650 | } | |
651 | return FALSE; | |
652 | ||
653 | /* There shouldn't be section relative relocations | |
654 | against any other section. */ | |
655 | default: | |
656 | return TRUE; | |
657 | } | |
658 | } | |
659 | ||
062e2358 | 660 | /* Assign dynsym indices. In a shared library we generate a section |
30b30c21 RH |
661 | symbol for each output section, which come first. Next come all of |
662 | the back-end allocated local dynamic syms, followed by the rest of | |
663 | the global symbols. */ | |
664 | ||
665 | unsigned long | |
268b6b39 | 666 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info) |
30b30c21 RH |
667 | { |
668 | unsigned long dynsymcount = 0; | |
669 | ||
670 | if (info->shared) | |
671 | { | |
aee6f5b4 | 672 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
673 | asection *p; |
674 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 675 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
676 | && (p->flags & SEC_ALLOC) != 0 |
677 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
678 | elf_section_data (p)->dynindx = ++dynsymcount; | |
30b30c21 RH |
679 | } |
680 | ||
681 | if (elf_hash_table (info)->dynlocal) | |
682 | { | |
683 | struct elf_link_local_dynamic_entry *p; | |
684 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
685 | p->dynindx = ++dynsymcount; | |
686 | } | |
687 | ||
688 | elf_link_hash_traverse (elf_hash_table (info), | |
689 | elf_link_renumber_hash_table_dynsyms, | |
690 | &dynsymcount); | |
691 | ||
692 | /* There is an unused NULL entry at the head of the table which | |
693 | we must account for in our count. Unless there weren't any | |
694 | symbols, which means we'll have no table at all. */ | |
695 | if (dynsymcount != 0) | |
696 | ++dynsymcount; | |
697 | ||
698 | return elf_hash_table (info)->dynsymcount = dynsymcount; | |
699 | } | |
252b5132 | 700 | |
45d6a902 AM |
701 | /* This function is called when we want to define a new symbol. It |
702 | handles the various cases which arise when we find a definition in | |
703 | a dynamic object, or when there is already a definition in a | |
704 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
705 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
706 | OVERRIDE if the old symbol is overriding a new definition. We set | |
707 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
708 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
709 | change, we mean that we shouldn't warn if the type or size does | |
0f8a2703 | 710 | change. */ |
45d6a902 AM |
711 | |
712 | bfd_boolean | |
268b6b39 AM |
713 | _bfd_elf_merge_symbol (bfd *abfd, |
714 | struct bfd_link_info *info, | |
715 | const char *name, | |
716 | Elf_Internal_Sym *sym, | |
717 | asection **psec, | |
718 | bfd_vma *pvalue, | |
719 | struct elf_link_hash_entry **sym_hash, | |
720 | bfd_boolean *skip, | |
721 | bfd_boolean *override, | |
722 | bfd_boolean *type_change_ok, | |
0f8a2703 | 723 | bfd_boolean *size_change_ok) |
252b5132 | 724 | { |
7479dfd4 | 725 | asection *sec, *oldsec; |
45d6a902 AM |
726 | struct elf_link_hash_entry *h; |
727 | struct elf_link_hash_entry *flip; | |
728 | int bind; | |
729 | bfd *oldbfd; | |
730 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
a5db907e | 731 | bfd_boolean newweak, oldweak, old_asneeded; |
45d6a902 AM |
732 | |
733 | *skip = FALSE; | |
734 | *override = FALSE; | |
735 | ||
736 | sec = *psec; | |
737 | bind = ELF_ST_BIND (sym->st_info); | |
738 | ||
739 | if (! bfd_is_und_section (sec)) | |
740 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
741 | else | |
742 | h = ((struct elf_link_hash_entry *) | |
743 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
744 | if (h == NULL) | |
745 | return FALSE; | |
746 | *sym_hash = h; | |
252b5132 | 747 | |
45d6a902 AM |
748 | /* This code is for coping with dynamic objects, and is only useful |
749 | if we are doing an ELF link. */ | |
750 | if (info->hash->creator != abfd->xvec) | |
751 | return TRUE; | |
252b5132 | 752 | |
45d6a902 AM |
753 | /* For merging, we only care about real symbols. */ |
754 | ||
755 | while (h->root.type == bfd_link_hash_indirect | |
756 | || h->root.type == bfd_link_hash_warning) | |
757 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
758 | ||
759 | /* If we just created the symbol, mark it as being an ELF symbol. | |
760 | Other than that, there is nothing to do--there is no merge issue | |
761 | with a newly defined symbol--so we just return. */ | |
762 | ||
763 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 764 | { |
f5385ebf | 765 | h->non_elf = 0; |
45d6a902 AM |
766 | return TRUE; |
767 | } | |
252b5132 | 768 | |
7479dfd4 L |
769 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
770 | existing symbol. */ | |
252b5132 | 771 | |
45d6a902 AM |
772 | switch (h->root.type) |
773 | { | |
774 | default: | |
775 | oldbfd = NULL; | |
7479dfd4 | 776 | oldsec = NULL; |
45d6a902 | 777 | break; |
252b5132 | 778 | |
45d6a902 AM |
779 | case bfd_link_hash_undefined: |
780 | case bfd_link_hash_undefweak: | |
781 | oldbfd = h->root.u.undef.abfd; | |
7479dfd4 | 782 | oldsec = NULL; |
45d6a902 AM |
783 | break; |
784 | ||
785 | case bfd_link_hash_defined: | |
786 | case bfd_link_hash_defweak: | |
787 | oldbfd = h->root.u.def.section->owner; | |
7479dfd4 | 788 | oldsec = h->root.u.def.section; |
45d6a902 AM |
789 | break; |
790 | ||
791 | case bfd_link_hash_common: | |
792 | oldbfd = h->root.u.c.p->section->owner; | |
7479dfd4 | 793 | oldsec = h->root.u.c.p->section; |
45d6a902 AM |
794 | break; |
795 | } | |
796 | ||
797 | /* In cases involving weak versioned symbols, we may wind up trying | |
798 | to merge a symbol with itself. Catch that here, to avoid the | |
799 | confusion that results if we try to override a symbol with | |
800 | itself. The additional tests catch cases like | |
801 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
802 | dynamic object, which we do want to handle here. */ | |
803 | if (abfd == oldbfd | |
804 | && ((abfd->flags & DYNAMIC) == 0 | |
f5385ebf | 805 | || !h->def_regular)) |
45d6a902 AM |
806 | return TRUE; |
807 | ||
808 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
809 | respectively, is from a dynamic object. */ | |
810 | ||
811 | if ((abfd->flags & DYNAMIC) != 0) | |
812 | newdyn = TRUE; | |
813 | else | |
814 | newdyn = FALSE; | |
815 | ||
816 | if (oldbfd != NULL) | |
817 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
818 | else | |
819 | { | |
820 | asection *hsec; | |
821 | ||
822 | /* This code handles the special SHN_MIPS_{TEXT,DATA} section | |
823 | indices used by MIPS ELF. */ | |
824 | switch (h->root.type) | |
252b5132 | 825 | { |
45d6a902 AM |
826 | default: |
827 | hsec = NULL; | |
828 | break; | |
252b5132 | 829 | |
45d6a902 AM |
830 | case bfd_link_hash_defined: |
831 | case bfd_link_hash_defweak: | |
832 | hsec = h->root.u.def.section; | |
833 | break; | |
252b5132 | 834 | |
45d6a902 AM |
835 | case bfd_link_hash_common: |
836 | hsec = h->root.u.c.p->section; | |
837 | break; | |
252b5132 | 838 | } |
252b5132 | 839 | |
45d6a902 AM |
840 | if (hsec == NULL) |
841 | olddyn = FALSE; | |
842 | else | |
843 | olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0; | |
844 | } | |
252b5132 | 845 | |
45d6a902 AM |
846 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
847 | respectively, appear to be a definition rather than reference. */ | |
848 | ||
849 | if (bfd_is_und_section (sec) || bfd_is_com_section (sec)) | |
850 | newdef = FALSE; | |
851 | else | |
852 | newdef = TRUE; | |
853 | ||
854 | if (h->root.type == bfd_link_hash_undefined | |
855 | || h->root.type == bfd_link_hash_undefweak | |
856 | || h->root.type == bfd_link_hash_common) | |
857 | olddef = FALSE; | |
858 | else | |
859 | olddef = TRUE; | |
860 | ||
a5db907e AM |
861 | /* If the old definition came from an as-needed dynamic library which |
862 | wasn't found to be needed, treat the sym as undefined. */ | |
863 | old_asneeded = FALSE; | |
864 | if (newdyn | |
865 | && olddyn | |
866 | && (elf_dyn_lib_class (oldbfd) & DYN_AS_NEEDED) != 0) | |
867 | old_asneeded = TRUE; | |
868 | ||
7479dfd4 L |
869 | /* Check TLS symbol. */ |
870 | if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS) | |
871 | && ELF_ST_TYPE (sym->st_info) != h->type) | |
872 | { | |
873 | bfd *ntbfd, *tbfd; | |
874 | bfd_boolean ntdef, tdef; | |
875 | asection *ntsec, *tsec; | |
876 | ||
877 | if (h->type == STT_TLS) | |
878 | { | |
879 | ntbfd = abfd; | |
880 | ntsec = sec; | |
881 | ntdef = newdef; | |
882 | tbfd = oldbfd; | |
883 | tsec = oldsec; | |
884 | tdef = olddef; | |
885 | } | |
886 | else | |
887 | { | |
888 | ntbfd = oldbfd; | |
889 | ntsec = oldsec; | |
890 | ntdef = olddef; | |
891 | tbfd = abfd; | |
892 | tsec = sec; | |
893 | tdef = newdef; | |
894 | } | |
895 | ||
896 | if (tdef && ntdef) | |
897 | (*_bfd_error_handler) | |
898 | (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), | |
899 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); | |
900 | else if (!tdef && !ntdef) | |
901 | (*_bfd_error_handler) | |
902 | (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), | |
903 | tbfd, ntbfd, h->root.root.string); | |
904 | else if (tdef) | |
905 | (*_bfd_error_handler) | |
906 | (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), | |
907 | tbfd, tsec, ntbfd, h->root.root.string); | |
908 | else | |
909 | (*_bfd_error_handler) | |
910 | (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), | |
911 | tbfd, ntbfd, ntsec, h->root.root.string); | |
912 | ||
913 | bfd_set_error (bfd_error_bad_value); | |
914 | return FALSE; | |
915 | } | |
916 | ||
4cc11e76 | 917 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
918 | object or is weak in all dynamic objects. Internal and hidden |
919 | visibility will make it unavailable to dynamic objects. */ | |
f5385ebf | 920 | if (newdyn && !h->dynamic_def) |
45d6a902 AM |
921 | { |
922 | if (!bfd_is_und_section (sec)) | |
f5385ebf | 923 | h->dynamic_def = 1; |
45d6a902 | 924 | else |
252b5132 | 925 | { |
45d6a902 AM |
926 | /* Check if this symbol is weak in all dynamic objects. If it |
927 | is the first time we see it in a dynamic object, we mark | |
928 | if it is weak. Otherwise, we clear it. */ | |
f5385ebf | 929 | if (!h->ref_dynamic) |
79349b09 | 930 | { |
45d6a902 | 931 | if (bind == STB_WEAK) |
f5385ebf | 932 | h->dynamic_weak = 1; |
252b5132 | 933 | } |
45d6a902 | 934 | else if (bind != STB_WEAK) |
f5385ebf | 935 | h->dynamic_weak = 0; |
252b5132 | 936 | } |
45d6a902 | 937 | } |
252b5132 | 938 | |
45d6a902 AM |
939 | /* If the old symbol has non-default visibility, we ignore the new |
940 | definition from a dynamic object. */ | |
941 | if (newdyn | |
9c7a29a3 | 942 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
943 | && !bfd_is_und_section (sec)) |
944 | { | |
945 | *skip = TRUE; | |
946 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 947 | h->ref_dynamic = 1; |
45d6a902 AM |
948 | /* A protected symbol has external availability. Make sure it is |
949 | recorded as dynamic. | |
950 | ||
951 | FIXME: Should we check type and size for protected symbol? */ | |
952 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 953 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
954 | else |
955 | return TRUE; | |
956 | } | |
957 | else if (!newdyn | |
9c7a29a3 | 958 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 959 | && h->def_dynamic) |
45d6a902 AM |
960 | { |
961 | /* If the new symbol with non-default visibility comes from a | |
962 | relocatable file and the old definition comes from a dynamic | |
963 | object, we remove the old definition. */ | |
964 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
965 | h = *sym_hash; | |
1de1a317 | 966 | |
f6e332e6 | 967 | if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root) |
1de1a317 L |
968 | && bfd_is_und_section (sec)) |
969 | { | |
970 | /* If the new symbol is undefined and the old symbol was | |
971 | also undefined before, we need to make sure | |
972 | _bfd_generic_link_add_one_symbol doesn't mess | |
f6e332e6 | 973 | up the linker hash table undefs list. Since the old |
1de1a317 L |
974 | definition came from a dynamic object, it is still on the |
975 | undefs list. */ | |
976 | h->root.type = bfd_link_hash_undefined; | |
1de1a317 L |
977 | h->root.u.undef.abfd = abfd; |
978 | } | |
979 | else | |
980 | { | |
981 | h->root.type = bfd_link_hash_new; | |
982 | h->root.u.undef.abfd = NULL; | |
983 | } | |
984 | ||
f5385ebf | 985 | if (h->def_dynamic) |
252b5132 | 986 | { |
f5385ebf AM |
987 | h->def_dynamic = 0; |
988 | h->ref_dynamic = 1; | |
989 | h->dynamic_def = 1; | |
45d6a902 AM |
990 | } |
991 | /* FIXME: Should we check type and size for protected symbol? */ | |
992 | h->size = 0; | |
993 | h->type = 0; | |
994 | return TRUE; | |
995 | } | |
14a793b2 | 996 | |
79349b09 AM |
997 | /* Differentiate strong and weak symbols. */ |
998 | newweak = bind == STB_WEAK; | |
999 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1000 | || h->root.type == bfd_link_hash_undefweak); | |
14a793b2 | 1001 | |
15b43f48 AM |
1002 | /* If a new weak symbol definition comes from a regular file and the |
1003 | old symbol comes from a dynamic library, we treat the new one as | |
1004 | strong. Similarly, an old weak symbol definition from a regular | |
1005 | file is treated as strong when the new symbol comes from a dynamic | |
1006 | library. Further, an old weak symbol from a dynamic library is | |
1007 | treated as strong if the new symbol is from a dynamic library. | |
1008 | This reflects the way glibc's ld.so works. | |
1009 | ||
1010 | Do this before setting *type_change_ok or *size_change_ok so that | |
1011 | we warn properly when dynamic library symbols are overridden. */ | |
1012 | ||
1013 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1014 | newweak = FALSE; |
15b43f48 | 1015 | if (olddef && newdyn) |
0f8a2703 AM |
1016 | oldweak = FALSE; |
1017 | ||
79349b09 AM |
1018 | /* It's OK to change the type if either the existing symbol or the |
1019 | new symbol is weak. A type change is also OK if the old symbol | |
1020 | is undefined and the new symbol is defined. */ | |
252b5132 | 1021 | |
79349b09 AM |
1022 | if (oldweak |
1023 | || newweak | |
1024 | || (newdef | |
1025 | && h->root.type == bfd_link_hash_undefined)) | |
1026 | *type_change_ok = TRUE; | |
1027 | ||
1028 | /* It's OK to change the size if either the existing symbol or the | |
1029 | new symbol is weak, or if the old symbol is undefined. */ | |
1030 | ||
1031 | if (*type_change_ok | |
1032 | || h->root.type == bfd_link_hash_undefined) | |
1033 | *size_change_ok = TRUE; | |
45d6a902 | 1034 | |
45d6a902 AM |
1035 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1036 | symbol, respectively, appears to be a common symbol in a dynamic | |
1037 | object. If a symbol appears in an uninitialized section, and is | |
1038 | not weak, and is not a function, then it may be a common symbol | |
1039 | which was resolved when the dynamic object was created. We want | |
1040 | to treat such symbols specially, because they raise special | |
1041 | considerations when setting the symbol size: if the symbol | |
1042 | appears as a common symbol in a regular object, and the size in | |
1043 | the regular object is larger, we must make sure that we use the | |
1044 | larger size. This problematic case can always be avoided in C, | |
1045 | but it must be handled correctly when using Fortran shared | |
1046 | libraries. | |
1047 | ||
1048 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1049 | likewise for OLDDYNCOMMON and OLDDEF. | |
1050 | ||
1051 | Note that this test is just a heuristic, and that it is quite | |
1052 | possible to have an uninitialized symbol in a shared object which | |
1053 | is really a definition, rather than a common symbol. This could | |
1054 | lead to some minor confusion when the symbol really is a common | |
1055 | symbol in some regular object. However, I think it will be | |
1056 | harmless. */ | |
1057 | ||
1058 | if (newdyn | |
1059 | && newdef | |
79349b09 | 1060 | && !newweak |
45d6a902 AM |
1061 | && (sec->flags & SEC_ALLOC) != 0 |
1062 | && (sec->flags & SEC_LOAD) == 0 | |
1063 | && sym->st_size > 0 | |
45d6a902 AM |
1064 | && ELF_ST_TYPE (sym->st_info) != STT_FUNC) |
1065 | newdyncommon = TRUE; | |
1066 | else | |
1067 | newdyncommon = FALSE; | |
1068 | ||
1069 | if (olddyn | |
1070 | && olddef | |
a5db907e | 1071 | && !old_asneeded |
45d6a902 | 1072 | && h->root.type == bfd_link_hash_defined |
f5385ebf | 1073 | && h->def_dynamic |
45d6a902 AM |
1074 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1075 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1076 | && h->size > 0 | |
1077 | && h->type != STT_FUNC) | |
1078 | olddyncommon = TRUE; | |
1079 | else | |
1080 | olddyncommon = FALSE; | |
1081 | ||
45d6a902 AM |
1082 | /* If both the old and the new symbols look like common symbols in a |
1083 | dynamic object, set the size of the symbol to the larger of the | |
1084 | two. */ | |
1085 | ||
1086 | if (olddyncommon | |
1087 | && newdyncommon | |
1088 | && sym->st_size != h->size) | |
1089 | { | |
1090 | /* Since we think we have two common symbols, issue a multiple | |
1091 | common warning if desired. Note that we only warn if the | |
1092 | size is different. If the size is the same, we simply let | |
1093 | the old symbol override the new one as normally happens with | |
1094 | symbols defined in dynamic objects. */ | |
1095 | ||
1096 | if (! ((*info->callbacks->multiple_common) | |
1097 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1098 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1099 | return FALSE; | |
252b5132 | 1100 | |
45d6a902 AM |
1101 | if (sym->st_size > h->size) |
1102 | h->size = sym->st_size; | |
252b5132 | 1103 | |
45d6a902 | 1104 | *size_change_ok = TRUE; |
252b5132 RH |
1105 | } |
1106 | ||
45d6a902 AM |
1107 | /* If we are looking at a dynamic object, and we have found a |
1108 | definition, we need to see if the symbol was already defined by | |
1109 | some other object. If so, we want to use the existing | |
1110 | definition, and we do not want to report a multiple symbol | |
1111 | definition error; we do this by clobbering *PSEC to be | |
1112 | bfd_und_section_ptr. | |
1113 | ||
1114 | We treat a common symbol as a definition if the symbol in the | |
1115 | shared library is a function, since common symbols always | |
1116 | represent variables; this can cause confusion in principle, but | |
1117 | any such confusion would seem to indicate an erroneous program or | |
1118 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1119 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1120 | |
1121 | if (newdyn | |
1122 | && newdef | |
a5db907e | 1123 | && ((olddef && !old_asneeded) |
45d6a902 | 1124 | || (h->root.type == bfd_link_hash_common |
79349b09 | 1125 | && (newweak |
0f8a2703 | 1126 | || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))) |
45d6a902 AM |
1127 | { |
1128 | *override = TRUE; | |
1129 | newdef = FALSE; | |
1130 | newdyncommon = FALSE; | |
252b5132 | 1131 | |
45d6a902 AM |
1132 | *psec = sec = bfd_und_section_ptr; |
1133 | *size_change_ok = TRUE; | |
252b5132 | 1134 | |
45d6a902 AM |
1135 | /* If we get here when the old symbol is a common symbol, then |
1136 | we are explicitly letting it override a weak symbol or | |
1137 | function in a dynamic object, and we don't want to warn about | |
1138 | a type change. If the old symbol is a defined symbol, a type | |
1139 | change warning may still be appropriate. */ | |
252b5132 | 1140 | |
45d6a902 AM |
1141 | if (h->root.type == bfd_link_hash_common) |
1142 | *type_change_ok = TRUE; | |
1143 | } | |
1144 | ||
1145 | /* Handle the special case of an old common symbol merging with a | |
1146 | new symbol which looks like a common symbol in a shared object. | |
1147 | We change *PSEC and *PVALUE to make the new symbol look like a | |
1148 | common symbol, and let _bfd_generic_link_add_one_symbol will do | |
1149 | the right thing. */ | |
1150 | ||
1151 | if (newdyncommon | |
1152 | && h->root.type == bfd_link_hash_common) | |
1153 | { | |
1154 | *override = TRUE; | |
1155 | newdef = FALSE; | |
1156 | newdyncommon = FALSE; | |
1157 | *pvalue = sym->st_size; | |
1158 | *psec = sec = bfd_com_section_ptr; | |
1159 | *size_change_ok = TRUE; | |
1160 | } | |
1161 | ||
1162 | /* If the old symbol is from a dynamic object, and the new symbol is | |
1163 | a definition which is not from a dynamic object, then the new | |
1164 | symbol overrides the old symbol. Symbols from regular files | |
1165 | always take precedence over symbols from dynamic objects, even if | |
1166 | they are defined after the dynamic object in the link. | |
1167 | ||
1168 | As above, we again permit a common symbol in a regular object to | |
1169 | override a definition in a shared object if the shared object | |
0f8a2703 | 1170 | symbol is a function or is weak. */ |
45d6a902 AM |
1171 | |
1172 | flip = NULL; | |
a5db907e | 1173 | if ((!newdyn || old_asneeded) |
45d6a902 AM |
1174 | && (newdef |
1175 | || (bfd_is_com_section (sec) | |
79349b09 AM |
1176 | && (oldweak |
1177 | || h->type == STT_FUNC))) | |
45d6a902 AM |
1178 | && olddyn |
1179 | && olddef | |
f5385ebf | 1180 | && h->def_dynamic) |
45d6a902 AM |
1181 | { |
1182 | /* Change the hash table entry to undefined, and let | |
1183 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1184 | new definition. */ | |
1185 | ||
1186 | h->root.type = bfd_link_hash_undefined; | |
1187 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1188 | *size_change_ok = TRUE; | |
1189 | ||
1190 | olddef = FALSE; | |
1191 | olddyncommon = FALSE; | |
1192 | ||
1193 | /* We again permit a type change when a common symbol may be | |
1194 | overriding a function. */ | |
1195 | ||
1196 | if (bfd_is_com_section (sec)) | |
1197 | *type_change_ok = TRUE; | |
1198 | ||
1199 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1200 | flip = *sym_hash; | |
1201 | else | |
1202 | /* This union may have been set to be non-NULL when this symbol | |
1203 | was seen in a dynamic object. We must force the union to be | |
1204 | NULL, so that it is correct for a regular symbol. */ | |
1205 | h->verinfo.vertree = NULL; | |
1206 | } | |
1207 | ||
1208 | /* Handle the special case of a new common symbol merging with an | |
1209 | old symbol that looks like it might be a common symbol defined in | |
1210 | a shared object. Note that we have already handled the case in | |
1211 | which a new common symbol should simply override the definition | |
1212 | in the shared library. */ | |
1213 | ||
1214 | if (! newdyn | |
1215 | && bfd_is_com_section (sec) | |
1216 | && olddyncommon) | |
1217 | { | |
1218 | /* It would be best if we could set the hash table entry to a | |
1219 | common symbol, but we don't know what to use for the section | |
1220 | or the alignment. */ | |
1221 | if (! ((*info->callbacks->multiple_common) | |
1222 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1223 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1224 | return FALSE; | |
1225 | ||
4cc11e76 | 1226 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1227 | larger, pretend that the new symbol has its size. */ |
1228 | ||
1229 | if (h->size > *pvalue) | |
1230 | *pvalue = h->size; | |
1231 | ||
1232 | /* FIXME: We no longer know the alignment required by the symbol | |
1233 | in the dynamic object, so we just wind up using the one from | |
1234 | the regular object. */ | |
1235 | ||
1236 | olddef = FALSE; | |
1237 | olddyncommon = FALSE; | |
1238 | ||
1239 | h->root.type = bfd_link_hash_undefined; | |
1240 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1241 | ||
1242 | *size_change_ok = TRUE; | |
1243 | *type_change_ok = TRUE; | |
1244 | ||
1245 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1246 | flip = *sym_hash; | |
1247 | else | |
1248 | h->verinfo.vertree = NULL; | |
1249 | } | |
1250 | ||
1251 | if (flip != NULL) | |
1252 | { | |
1253 | /* Handle the case where we had a versioned symbol in a dynamic | |
1254 | library and now find a definition in a normal object. In this | |
1255 | case, we make the versioned symbol point to the normal one. */ | |
9c5bfbb7 | 1256 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1257 | flip->root.type = h->root.type; |
1258 | h->root.type = bfd_link_hash_indirect; | |
1259 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
1260 | (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h); | |
1261 | flip->root.u.undef.abfd = h->root.u.undef.abfd; | |
f5385ebf | 1262 | if (h->def_dynamic) |
45d6a902 | 1263 | { |
f5385ebf AM |
1264 | h->def_dynamic = 0; |
1265 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1266 | } |
1267 | } | |
1268 | ||
45d6a902 AM |
1269 | return TRUE; |
1270 | } | |
1271 | ||
1272 | /* This function is called to create an indirect symbol from the | |
1273 | default for the symbol with the default version if needed. The | |
1274 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1275 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 AM |
1276 | |
1277 | bfd_boolean | |
268b6b39 AM |
1278 | _bfd_elf_add_default_symbol (bfd *abfd, |
1279 | struct bfd_link_info *info, | |
1280 | struct elf_link_hash_entry *h, | |
1281 | const char *name, | |
1282 | Elf_Internal_Sym *sym, | |
1283 | asection **psec, | |
1284 | bfd_vma *value, | |
1285 | bfd_boolean *dynsym, | |
0f8a2703 | 1286 | bfd_boolean override) |
45d6a902 AM |
1287 | { |
1288 | bfd_boolean type_change_ok; | |
1289 | bfd_boolean size_change_ok; | |
1290 | bfd_boolean skip; | |
1291 | char *shortname; | |
1292 | struct elf_link_hash_entry *hi; | |
1293 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1294 | const struct elf_backend_data *bed; |
45d6a902 AM |
1295 | bfd_boolean collect; |
1296 | bfd_boolean dynamic; | |
1297 | char *p; | |
1298 | size_t len, shortlen; | |
1299 | asection *sec; | |
1300 | ||
1301 | /* If this symbol has a version, and it is the default version, we | |
1302 | create an indirect symbol from the default name to the fully | |
1303 | decorated name. This will cause external references which do not | |
1304 | specify a version to be bound to this version of the symbol. */ | |
1305 | p = strchr (name, ELF_VER_CHR); | |
1306 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1307 | return TRUE; | |
1308 | ||
1309 | if (override) | |
1310 | { | |
4cc11e76 | 1311 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1312 | need to create the indirect symbol from the default name. */ |
1313 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1314 | FALSE, FALSE); | |
1315 | BFD_ASSERT (hi != NULL); | |
1316 | if (hi == h) | |
1317 | return TRUE; | |
1318 | while (hi->root.type == bfd_link_hash_indirect | |
1319 | || hi->root.type == bfd_link_hash_warning) | |
1320 | { | |
1321 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1322 | if (hi == h) | |
1323 | return TRUE; | |
1324 | } | |
1325 | } | |
1326 | ||
1327 | bed = get_elf_backend_data (abfd); | |
1328 | collect = bed->collect; | |
1329 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1330 | ||
1331 | shortlen = p - name; | |
1332 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1333 | if (shortname == NULL) | |
1334 | return FALSE; | |
1335 | memcpy (shortname, name, shortlen); | |
1336 | shortname[shortlen] = '\0'; | |
1337 | ||
1338 | /* We are going to create a new symbol. Merge it with any existing | |
1339 | symbol with this name. For the purposes of the merge, act as | |
1340 | though we were defining the symbol we just defined, although we | |
1341 | actually going to define an indirect symbol. */ | |
1342 | type_change_ok = FALSE; | |
1343 | size_change_ok = FALSE; | |
1344 | sec = *psec; | |
1345 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
1346 | &hi, &skip, &override, &type_change_ok, | |
0f8a2703 | 1347 | &size_change_ok)) |
45d6a902 AM |
1348 | return FALSE; |
1349 | ||
1350 | if (skip) | |
1351 | goto nondefault; | |
1352 | ||
1353 | if (! override) | |
1354 | { | |
1355 | bh = &hi->root; | |
1356 | if (! (_bfd_generic_link_add_one_symbol | |
1357 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1358 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1359 | return FALSE; |
1360 | hi = (struct elf_link_hash_entry *) bh; | |
1361 | } | |
1362 | else | |
1363 | { | |
1364 | /* In this case the symbol named SHORTNAME is overriding the | |
1365 | indirect symbol we want to add. We were planning on making | |
1366 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1367 | is the name without a version. NAME is the fully versioned | |
1368 | name, and it is the default version. | |
1369 | ||
1370 | Overriding means that we already saw a definition for the | |
1371 | symbol SHORTNAME in a regular object, and it is overriding | |
1372 | the symbol defined in the dynamic object. | |
1373 | ||
1374 | When this happens, we actually want to change NAME, the | |
1375 | symbol we just added, to refer to SHORTNAME. This will cause | |
1376 | references to NAME in the shared object to become references | |
1377 | to SHORTNAME in the regular object. This is what we expect | |
1378 | when we override a function in a shared object: that the | |
1379 | references in the shared object will be mapped to the | |
1380 | definition in the regular object. */ | |
1381 | ||
1382 | while (hi->root.type == bfd_link_hash_indirect | |
1383 | || hi->root.type == bfd_link_hash_warning) | |
1384 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1385 | ||
1386 | h->root.type = bfd_link_hash_indirect; | |
1387 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1388 | if (h->def_dynamic) |
45d6a902 | 1389 | { |
f5385ebf AM |
1390 | h->def_dynamic = 0; |
1391 | hi->ref_dynamic = 1; | |
1392 | if (hi->ref_regular | |
1393 | || hi->def_regular) | |
45d6a902 | 1394 | { |
c152c796 | 1395 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1396 | return FALSE; |
1397 | } | |
1398 | } | |
1399 | ||
1400 | /* Now set HI to H, so that the following code will set the | |
1401 | other fields correctly. */ | |
1402 | hi = h; | |
1403 | } | |
1404 | ||
1405 | /* If there is a duplicate definition somewhere, then HI may not | |
1406 | point to an indirect symbol. We will have reported an error to | |
1407 | the user in that case. */ | |
1408 | ||
1409 | if (hi->root.type == bfd_link_hash_indirect) | |
1410 | { | |
1411 | struct elf_link_hash_entry *ht; | |
1412 | ||
45d6a902 AM |
1413 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
1414 | (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi); | |
1415 | ||
1416 | /* See if the new flags lead us to realize that the symbol must | |
1417 | be dynamic. */ | |
1418 | if (! *dynsym) | |
1419 | { | |
1420 | if (! dynamic) | |
1421 | { | |
1422 | if (info->shared | |
f5385ebf | 1423 | || hi->ref_dynamic) |
45d6a902 AM |
1424 | *dynsym = TRUE; |
1425 | } | |
1426 | else | |
1427 | { | |
f5385ebf | 1428 | if (hi->ref_regular) |
45d6a902 AM |
1429 | *dynsym = TRUE; |
1430 | } | |
1431 | } | |
1432 | } | |
1433 | ||
1434 | /* We also need to define an indirection from the nondefault version | |
1435 | of the symbol. */ | |
1436 | ||
1437 | nondefault: | |
1438 | len = strlen (name); | |
1439 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1440 | if (shortname == NULL) | |
1441 | return FALSE; | |
1442 | memcpy (shortname, name, shortlen); | |
1443 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1444 | ||
1445 | /* Once again, merge with any existing symbol. */ | |
1446 | type_change_ok = FALSE; | |
1447 | size_change_ok = FALSE; | |
1448 | sec = *psec; | |
1449 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
1450 | &hi, &skip, &override, &type_change_ok, | |
0f8a2703 | 1451 | &size_change_ok)) |
45d6a902 AM |
1452 | return FALSE; |
1453 | ||
1454 | if (skip) | |
1455 | return TRUE; | |
1456 | ||
1457 | if (override) | |
1458 | { | |
1459 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1460 | the type of override we do in the case above unless it is | |
4cc11e76 | 1461 | overridden by a versioned definition. */ |
45d6a902 AM |
1462 | if (hi->root.type != bfd_link_hash_defined |
1463 | && hi->root.type != bfd_link_hash_defweak) | |
1464 | (*_bfd_error_handler) | |
d003868e AM |
1465 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1466 | abfd, shortname); | |
45d6a902 AM |
1467 | } |
1468 | else | |
1469 | { | |
1470 | bh = &hi->root; | |
1471 | if (! (_bfd_generic_link_add_one_symbol | |
1472 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1473 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1474 | return FALSE; |
1475 | hi = (struct elf_link_hash_entry *) bh; | |
1476 | ||
1477 | /* If there is a duplicate definition somewhere, then HI may not | |
1478 | point to an indirect symbol. We will have reported an error | |
1479 | to the user in that case. */ | |
1480 | ||
1481 | if (hi->root.type == bfd_link_hash_indirect) | |
1482 | { | |
45d6a902 AM |
1483 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); |
1484 | ||
1485 | /* See if the new flags lead us to realize that the symbol | |
1486 | must be dynamic. */ | |
1487 | if (! *dynsym) | |
1488 | { | |
1489 | if (! dynamic) | |
1490 | { | |
1491 | if (info->shared | |
f5385ebf | 1492 | || hi->ref_dynamic) |
45d6a902 AM |
1493 | *dynsym = TRUE; |
1494 | } | |
1495 | else | |
1496 | { | |
f5385ebf | 1497 | if (hi->ref_regular) |
45d6a902 AM |
1498 | *dynsym = TRUE; |
1499 | } | |
1500 | } | |
1501 | } | |
1502 | } | |
1503 | ||
1504 | return TRUE; | |
1505 | } | |
1506 | \f | |
1507 | /* This routine is used to export all defined symbols into the dynamic | |
1508 | symbol table. It is called via elf_link_hash_traverse. */ | |
1509 | ||
1510 | bfd_boolean | |
268b6b39 | 1511 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1512 | { |
268b6b39 | 1513 | struct elf_info_failed *eif = data; |
45d6a902 AM |
1514 | |
1515 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
1516 | if (h->root.type == bfd_link_hash_indirect) | |
1517 | return TRUE; | |
1518 | ||
1519 | if (h->root.type == bfd_link_hash_warning) | |
1520 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1521 | ||
1522 | if (h->dynindx == -1 | |
f5385ebf AM |
1523 | && (h->def_regular |
1524 | || h->ref_regular)) | |
45d6a902 AM |
1525 | { |
1526 | struct bfd_elf_version_tree *t; | |
1527 | struct bfd_elf_version_expr *d; | |
1528 | ||
1529 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1530 | { | |
108ba305 | 1531 | if (t->globals.list != NULL) |
45d6a902 | 1532 | { |
108ba305 JJ |
1533 | d = (*t->match) (&t->globals, NULL, h->root.root.string); |
1534 | if (d != NULL) | |
1535 | goto doit; | |
45d6a902 AM |
1536 | } |
1537 | ||
108ba305 | 1538 | if (t->locals.list != NULL) |
45d6a902 | 1539 | { |
108ba305 JJ |
1540 | d = (*t->match) (&t->locals, NULL, h->root.root.string); |
1541 | if (d != NULL) | |
1542 | return TRUE; | |
45d6a902 AM |
1543 | } |
1544 | } | |
1545 | ||
1546 | if (!eif->verdefs) | |
1547 | { | |
1548 | doit: | |
c152c796 | 1549 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
1550 | { |
1551 | eif->failed = TRUE; | |
1552 | return FALSE; | |
1553 | } | |
1554 | } | |
1555 | } | |
1556 | ||
1557 | return TRUE; | |
1558 | } | |
1559 | \f | |
1560 | /* Look through the symbols which are defined in other shared | |
1561 | libraries and referenced here. Update the list of version | |
1562 | dependencies. This will be put into the .gnu.version_r section. | |
1563 | This function is called via elf_link_hash_traverse. */ | |
1564 | ||
1565 | bfd_boolean | |
268b6b39 AM |
1566 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1567 | void *data) | |
45d6a902 | 1568 | { |
268b6b39 | 1569 | struct elf_find_verdep_info *rinfo = data; |
45d6a902 AM |
1570 | Elf_Internal_Verneed *t; |
1571 | Elf_Internal_Vernaux *a; | |
1572 | bfd_size_type amt; | |
1573 | ||
1574 | if (h->root.type == bfd_link_hash_warning) | |
1575 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1576 | ||
1577 | /* We only care about symbols defined in shared objects with version | |
1578 | information. */ | |
f5385ebf AM |
1579 | if (!h->def_dynamic |
1580 | || h->def_regular | |
45d6a902 AM |
1581 | || h->dynindx == -1 |
1582 | || h->verinfo.verdef == NULL) | |
1583 | return TRUE; | |
1584 | ||
1585 | /* See if we already know about this version. */ | |
1586 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1587 | { | |
1588 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1589 | continue; | |
1590 | ||
1591 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1592 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1593 | return TRUE; | |
1594 | ||
1595 | break; | |
1596 | } | |
1597 | ||
1598 | /* This is a new version. Add it to tree we are building. */ | |
1599 | ||
1600 | if (t == NULL) | |
1601 | { | |
1602 | amt = sizeof *t; | |
268b6b39 | 1603 | t = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1604 | if (t == NULL) |
1605 | { | |
1606 | rinfo->failed = TRUE; | |
1607 | return FALSE; | |
1608 | } | |
1609 | ||
1610 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1611 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1612 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1613 | } | |
1614 | ||
1615 | amt = sizeof *a; | |
268b6b39 | 1616 | a = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1617 | |
1618 | /* Note that we are copying a string pointer here, and testing it | |
1619 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1620 | discard the string data when low in memory, this will have to be | |
1621 | fixed. */ | |
1622 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1623 | ||
1624 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1625 | a->vna_nextptr = t->vn_auxptr; | |
1626 | ||
1627 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1628 | ++rinfo->vers; | |
1629 | ||
1630 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1631 | ||
1632 | t->vn_auxptr = a; | |
1633 | ||
1634 | return TRUE; | |
1635 | } | |
1636 | ||
1637 | /* Figure out appropriate versions for all the symbols. We may not | |
1638 | have the version number script until we have read all of the input | |
1639 | files, so until that point we don't know which symbols should be | |
1640 | local. This function is called via elf_link_hash_traverse. */ | |
1641 | ||
1642 | bfd_boolean | |
268b6b39 | 1643 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
1644 | { |
1645 | struct elf_assign_sym_version_info *sinfo; | |
1646 | struct bfd_link_info *info; | |
9c5bfbb7 | 1647 | const struct elf_backend_data *bed; |
45d6a902 AM |
1648 | struct elf_info_failed eif; |
1649 | char *p; | |
1650 | bfd_size_type amt; | |
1651 | ||
268b6b39 | 1652 | sinfo = data; |
45d6a902 AM |
1653 | info = sinfo->info; |
1654 | ||
1655 | if (h->root.type == bfd_link_hash_warning) | |
1656 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1657 | ||
1658 | /* Fix the symbol flags. */ | |
1659 | eif.failed = FALSE; | |
1660 | eif.info = info; | |
1661 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1662 | { | |
1663 | if (eif.failed) | |
1664 | sinfo->failed = TRUE; | |
1665 | return FALSE; | |
1666 | } | |
1667 | ||
1668 | /* We only need version numbers for symbols defined in regular | |
1669 | objects. */ | |
f5385ebf | 1670 | if (!h->def_regular) |
45d6a902 AM |
1671 | return TRUE; |
1672 | ||
1673 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1674 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1675 | if (p != NULL && h->verinfo.vertree == NULL) | |
1676 | { | |
1677 | struct bfd_elf_version_tree *t; | |
1678 | bfd_boolean hidden; | |
1679 | ||
1680 | hidden = TRUE; | |
1681 | ||
1682 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1683 | not a hidden symbol. */ | |
1684 | ++p; | |
1685 | if (*p == ELF_VER_CHR) | |
1686 | { | |
1687 | hidden = FALSE; | |
1688 | ++p; | |
1689 | } | |
1690 | ||
1691 | /* If there is no version string, we can just return out. */ | |
1692 | if (*p == '\0') | |
1693 | { | |
1694 | if (hidden) | |
f5385ebf | 1695 | h->hidden = 1; |
45d6a902 AM |
1696 | return TRUE; |
1697 | } | |
1698 | ||
1699 | /* Look for the version. If we find it, it is no longer weak. */ | |
1700 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1701 | { | |
1702 | if (strcmp (t->name, p) == 0) | |
1703 | { | |
1704 | size_t len; | |
1705 | char *alc; | |
1706 | struct bfd_elf_version_expr *d; | |
1707 | ||
1708 | len = p - h->root.root.string; | |
268b6b39 | 1709 | alc = bfd_malloc (len); |
45d6a902 AM |
1710 | if (alc == NULL) |
1711 | return FALSE; | |
1712 | memcpy (alc, h->root.root.string, len - 1); | |
1713 | alc[len - 1] = '\0'; | |
1714 | if (alc[len - 2] == ELF_VER_CHR) | |
1715 | alc[len - 2] = '\0'; | |
1716 | ||
1717 | h->verinfo.vertree = t; | |
1718 | t->used = TRUE; | |
1719 | d = NULL; | |
1720 | ||
108ba305 JJ |
1721 | if (t->globals.list != NULL) |
1722 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
1723 | |
1724 | /* See if there is anything to force this symbol to | |
1725 | local scope. */ | |
108ba305 | 1726 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 1727 | { |
108ba305 JJ |
1728 | d = (*t->match) (&t->locals, NULL, alc); |
1729 | if (d != NULL | |
1730 | && h->dynindx != -1 | |
1731 | && info->shared | |
1732 | && ! info->export_dynamic) | |
1733 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
1734 | } |
1735 | ||
1736 | free (alc); | |
1737 | break; | |
1738 | } | |
1739 | } | |
1740 | ||
1741 | /* If we are building an application, we need to create a | |
1742 | version node for this version. */ | |
36af4a4e | 1743 | if (t == NULL && info->executable) |
45d6a902 AM |
1744 | { |
1745 | struct bfd_elf_version_tree **pp; | |
1746 | int version_index; | |
1747 | ||
1748 | /* If we aren't going to export this symbol, we don't need | |
1749 | to worry about it. */ | |
1750 | if (h->dynindx == -1) | |
1751 | return TRUE; | |
1752 | ||
1753 | amt = sizeof *t; | |
108ba305 | 1754 | t = bfd_zalloc (sinfo->output_bfd, amt); |
45d6a902 AM |
1755 | if (t == NULL) |
1756 | { | |
1757 | sinfo->failed = TRUE; | |
1758 | return FALSE; | |
1759 | } | |
1760 | ||
45d6a902 | 1761 | t->name = p; |
45d6a902 AM |
1762 | t->name_indx = (unsigned int) -1; |
1763 | t->used = TRUE; | |
1764 | ||
1765 | version_index = 1; | |
1766 | /* Don't count anonymous version tag. */ | |
1767 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1768 | version_index = 0; | |
1769 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1770 | ++version_index; | |
1771 | t->vernum = version_index; | |
1772 | ||
1773 | *pp = t; | |
1774 | ||
1775 | h->verinfo.vertree = t; | |
1776 | } | |
1777 | else if (t == NULL) | |
1778 | { | |
1779 | /* We could not find the version for a symbol when | |
1780 | generating a shared archive. Return an error. */ | |
1781 | (*_bfd_error_handler) | |
d003868e AM |
1782 | (_("%B: undefined versioned symbol name %s"), |
1783 | sinfo->output_bfd, h->root.root.string); | |
45d6a902 AM |
1784 | bfd_set_error (bfd_error_bad_value); |
1785 | sinfo->failed = TRUE; | |
1786 | return FALSE; | |
1787 | } | |
1788 | ||
1789 | if (hidden) | |
f5385ebf | 1790 | h->hidden = 1; |
45d6a902 AM |
1791 | } |
1792 | ||
1793 | /* If we don't have a version for this symbol, see if we can find | |
1794 | something. */ | |
1795 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
1796 | { | |
1797 | struct bfd_elf_version_tree *t; | |
1798 | struct bfd_elf_version_tree *local_ver; | |
1799 | struct bfd_elf_version_expr *d; | |
1800 | ||
1801 | /* See if can find what version this symbol is in. If the | |
1802 | symbol is supposed to be local, then don't actually register | |
1803 | it. */ | |
1804 | local_ver = NULL; | |
1805 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1806 | { | |
108ba305 | 1807 | if (t->globals.list != NULL) |
45d6a902 AM |
1808 | { |
1809 | bfd_boolean matched; | |
1810 | ||
1811 | matched = FALSE; | |
108ba305 JJ |
1812 | d = NULL; |
1813 | while ((d = (*t->match) (&t->globals, d, | |
1814 | h->root.root.string)) != NULL) | |
1815 | if (d->symver) | |
1816 | matched = TRUE; | |
1817 | else | |
1818 | { | |
1819 | /* There is a version without definition. Make | |
1820 | the symbol the default definition for this | |
1821 | version. */ | |
1822 | h->verinfo.vertree = t; | |
1823 | local_ver = NULL; | |
1824 | d->script = 1; | |
1825 | break; | |
1826 | } | |
45d6a902 AM |
1827 | if (d != NULL) |
1828 | break; | |
1829 | else if (matched) | |
1830 | /* There is no undefined version for this symbol. Hide the | |
1831 | default one. */ | |
1832 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1833 | } | |
1834 | ||
108ba305 | 1835 | if (t->locals.list != NULL) |
45d6a902 | 1836 | { |
108ba305 JJ |
1837 | d = NULL; |
1838 | while ((d = (*t->match) (&t->locals, d, | |
1839 | h->root.root.string)) != NULL) | |
45d6a902 | 1840 | { |
108ba305 | 1841 | local_ver = t; |
45d6a902 | 1842 | /* If the match is "*", keep looking for a more |
108ba305 JJ |
1843 | explicit, perhaps even global, match. |
1844 | XXX: Shouldn't this be !d->wildcard instead? */ | |
1845 | if (d->pattern[0] != '*' || d->pattern[1] != '\0') | |
1846 | break; | |
45d6a902 AM |
1847 | } |
1848 | ||
1849 | if (d != NULL) | |
1850 | break; | |
1851 | } | |
1852 | } | |
1853 | ||
1854 | if (local_ver != NULL) | |
1855 | { | |
1856 | h->verinfo.vertree = local_ver; | |
1857 | if (h->dynindx != -1 | |
1858 | && info->shared | |
1859 | && ! info->export_dynamic) | |
1860 | { | |
1861 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1862 | } | |
1863 | } | |
1864 | } | |
1865 | ||
1866 | return TRUE; | |
1867 | } | |
1868 | \f | |
45d6a902 AM |
1869 | /* Read and swap the relocs from the section indicated by SHDR. This |
1870 | may be either a REL or a RELA section. The relocations are | |
1871 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
1872 | which should have already been allocated to contain enough space. | |
1873 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
1874 | relocations should be stored. | |
1875 | ||
1876 | Returns FALSE if something goes wrong. */ | |
1877 | ||
1878 | static bfd_boolean | |
268b6b39 | 1879 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 1880 | asection *sec, |
268b6b39 AM |
1881 | Elf_Internal_Shdr *shdr, |
1882 | void *external_relocs, | |
1883 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 1884 | { |
9c5bfbb7 | 1885 | const struct elf_backend_data *bed; |
268b6b39 | 1886 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
1887 | const bfd_byte *erela; |
1888 | const bfd_byte *erelaend; | |
1889 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
1890 | Elf_Internal_Shdr *symtab_hdr; |
1891 | size_t nsyms; | |
45d6a902 | 1892 | |
45d6a902 AM |
1893 | /* Position ourselves at the start of the section. */ |
1894 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
1895 | return FALSE; | |
1896 | ||
1897 | /* Read the relocations. */ | |
1898 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
1899 | return FALSE; | |
1900 | ||
243ef1e0 L |
1901 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1902 | nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
1903 | ||
45d6a902 AM |
1904 | bed = get_elf_backend_data (abfd); |
1905 | ||
1906 | /* Convert the external relocations to the internal format. */ | |
1907 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
1908 | swap_in = bed->s->swap_reloc_in; | |
1909 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
1910 | swap_in = bed->s->swap_reloca_in; | |
1911 | else | |
1912 | { | |
1913 | bfd_set_error (bfd_error_wrong_format); | |
1914 | return FALSE; | |
1915 | } | |
1916 | ||
1917 | erela = external_relocs; | |
51992aec | 1918 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
1919 | irela = internal_relocs; |
1920 | while (erela < erelaend) | |
1921 | { | |
243ef1e0 L |
1922 | bfd_vma r_symndx; |
1923 | ||
45d6a902 | 1924 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
1925 | r_symndx = ELF32_R_SYM (irela->r_info); |
1926 | if (bed->s->arch_size == 64) | |
1927 | r_symndx >>= 24; | |
1928 | if ((size_t) r_symndx >= nsyms) | |
1929 | { | |
1930 | (*_bfd_error_handler) | |
d003868e AM |
1931 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" |
1932 | " for offset 0x%lx in section `%A'"), | |
1933 | abfd, sec, | |
1934 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
1935 | bfd_set_error (bfd_error_bad_value); |
1936 | return FALSE; | |
1937 | } | |
45d6a902 AM |
1938 | irela += bed->s->int_rels_per_ext_rel; |
1939 | erela += shdr->sh_entsize; | |
1940 | } | |
1941 | ||
1942 | return TRUE; | |
1943 | } | |
1944 | ||
1945 | /* Read and swap the relocs for a section O. They may have been | |
1946 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
1947 | not NULL, they are used as buffers to read into. They are known to | |
1948 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
1949 | the return value is allocated using either malloc or bfd_alloc, | |
1950 | according to the KEEP_MEMORY argument. If O has two relocation | |
1951 | sections (both REL and RELA relocations), then the REL_HDR | |
1952 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
1953 | REL_HDR2 relocations. */ | |
1954 | ||
1955 | Elf_Internal_Rela * | |
268b6b39 AM |
1956 | _bfd_elf_link_read_relocs (bfd *abfd, |
1957 | asection *o, | |
1958 | void *external_relocs, | |
1959 | Elf_Internal_Rela *internal_relocs, | |
1960 | bfd_boolean keep_memory) | |
45d6a902 AM |
1961 | { |
1962 | Elf_Internal_Shdr *rel_hdr; | |
268b6b39 | 1963 | void *alloc1 = NULL; |
45d6a902 | 1964 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 1965 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1966 | |
1967 | if (elf_section_data (o)->relocs != NULL) | |
1968 | return elf_section_data (o)->relocs; | |
1969 | ||
1970 | if (o->reloc_count == 0) | |
1971 | return NULL; | |
1972 | ||
1973 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
1974 | ||
1975 | if (internal_relocs == NULL) | |
1976 | { | |
1977 | bfd_size_type size; | |
1978 | ||
1979 | size = o->reloc_count; | |
1980 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
1981 | if (keep_memory) | |
268b6b39 | 1982 | internal_relocs = bfd_alloc (abfd, size); |
45d6a902 | 1983 | else |
268b6b39 | 1984 | internal_relocs = alloc2 = bfd_malloc (size); |
45d6a902 AM |
1985 | if (internal_relocs == NULL) |
1986 | goto error_return; | |
1987 | } | |
1988 | ||
1989 | if (external_relocs == NULL) | |
1990 | { | |
1991 | bfd_size_type size = rel_hdr->sh_size; | |
1992 | ||
1993 | if (elf_section_data (o)->rel_hdr2) | |
1994 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
268b6b39 | 1995 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
1996 | if (alloc1 == NULL) |
1997 | goto error_return; | |
1998 | external_relocs = alloc1; | |
1999 | } | |
2000 | ||
243ef1e0 | 2001 | if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, |
45d6a902 AM |
2002 | external_relocs, |
2003 | internal_relocs)) | |
2004 | goto error_return; | |
51992aec AM |
2005 | if (elf_section_data (o)->rel_hdr2 |
2006 | && (!elf_link_read_relocs_from_section | |
2007 | (abfd, o, | |
2008 | elf_section_data (o)->rel_hdr2, | |
2009 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
2010 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
2011 | * bed->s->int_rels_per_ext_rel)))) | |
45d6a902 AM |
2012 | goto error_return; |
2013 | ||
2014 | /* Cache the results for next time, if we can. */ | |
2015 | if (keep_memory) | |
2016 | elf_section_data (o)->relocs = internal_relocs; | |
2017 | ||
2018 | if (alloc1 != NULL) | |
2019 | free (alloc1); | |
2020 | ||
2021 | /* Don't free alloc2, since if it was allocated we are passing it | |
2022 | back (under the name of internal_relocs). */ | |
2023 | ||
2024 | return internal_relocs; | |
2025 | ||
2026 | error_return: | |
2027 | if (alloc1 != NULL) | |
2028 | free (alloc1); | |
2029 | if (alloc2 != NULL) | |
2030 | free (alloc2); | |
2031 | return NULL; | |
2032 | } | |
2033 | ||
2034 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2035 | section header for a section containing relocations for O. */ | |
2036 | ||
2037 | bfd_boolean | |
268b6b39 AM |
2038 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2039 | Elf_Internal_Shdr *rel_hdr, | |
2040 | asection *o) | |
45d6a902 AM |
2041 | { |
2042 | bfd_size_type reloc_count; | |
2043 | bfd_size_type num_rel_hashes; | |
2044 | ||
2045 | /* Figure out how many relocations there will be. */ | |
2046 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
2047 | reloc_count = elf_section_data (o)->rel_count; | |
2048 | else | |
2049 | reloc_count = elf_section_data (o)->rel_count2; | |
2050 | ||
2051 | num_rel_hashes = o->reloc_count; | |
2052 | if (num_rel_hashes < reloc_count) | |
2053 | num_rel_hashes = reloc_count; | |
2054 | ||
2055 | /* That allows us to calculate the size of the section. */ | |
2056 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
2057 | ||
2058 | /* The contents field must last into write_object_contents, so we | |
2059 | allocate it with bfd_alloc rather than malloc. Also since we | |
2060 | cannot be sure that the contents will actually be filled in, | |
2061 | we zero the allocated space. */ | |
268b6b39 | 2062 | rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2063 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2064 | return FALSE; | |
2065 | ||
2066 | /* We only allocate one set of hash entries, so we only do it the | |
2067 | first time we are called. */ | |
2068 | if (elf_section_data (o)->rel_hashes == NULL | |
2069 | && num_rel_hashes) | |
2070 | { | |
2071 | struct elf_link_hash_entry **p; | |
2072 | ||
268b6b39 | 2073 | p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
2074 | if (p == NULL) |
2075 | return FALSE; | |
2076 | ||
2077 | elf_section_data (o)->rel_hashes = p; | |
2078 | } | |
2079 | ||
2080 | return TRUE; | |
2081 | } | |
2082 | ||
2083 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2084 | originated from the section given by INPUT_REL_HDR) to the | |
2085 | OUTPUT_BFD. */ | |
2086 | ||
2087 | bfd_boolean | |
268b6b39 AM |
2088 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2089 | asection *input_section, | |
2090 | Elf_Internal_Shdr *input_rel_hdr, | |
2091 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 AM |
2092 | { |
2093 | Elf_Internal_Rela *irela; | |
2094 | Elf_Internal_Rela *irelaend; | |
2095 | bfd_byte *erel; | |
2096 | Elf_Internal_Shdr *output_rel_hdr; | |
2097 | asection *output_section; | |
2098 | unsigned int *rel_countp = NULL; | |
9c5bfbb7 | 2099 | const struct elf_backend_data *bed; |
268b6b39 | 2100 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
45d6a902 AM |
2101 | |
2102 | output_section = input_section->output_section; | |
2103 | output_rel_hdr = NULL; | |
2104 | ||
2105 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
2106 | == input_rel_hdr->sh_entsize) | |
2107 | { | |
2108 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
2109 | rel_countp = &elf_section_data (output_section)->rel_count; | |
2110 | } | |
2111 | else if (elf_section_data (output_section)->rel_hdr2 | |
2112 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
2113 | == input_rel_hdr->sh_entsize)) | |
2114 | { | |
2115 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2116 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2117 | } | |
2118 | else | |
2119 | { | |
2120 | (*_bfd_error_handler) | |
d003868e AM |
2121 | (_("%B: relocation size mismatch in %B section %A"), |
2122 | output_bfd, input_section->owner, input_section); | |
45d6a902 AM |
2123 | bfd_set_error (bfd_error_wrong_object_format); |
2124 | return FALSE; | |
2125 | } | |
2126 | ||
2127 | bed = get_elf_backend_data (output_bfd); | |
2128 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2129 | swap_out = bed->s->swap_reloc_out; | |
2130 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2131 | swap_out = bed->s->swap_reloca_out; | |
2132 | else | |
2133 | abort (); | |
2134 | ||
2135 | erel = output_rel_hdr->contents; | |
2136 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2137 | irela = internal_relocs; | |
2138 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2139 | * bed->s->int_rels_per_ext_rel); | |
2140 | while (irela < irelaend) | |
2141 | { | |
2142 | (*swap_out) (output_bfd, irela, erel); | |
2143 | irela += bed->s->int_rels_per_ext_rel; | |
2144 | erel += input_rel_hdr->sh_entsize; | |
2145 | } | |
2146 | ||
2147 | /* Bump the counter, so that we know where to add the next set of | |
2148 | relocations. */ | |
2149 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2150 | ||
2151 | return TRUE; | |
2152 | } | |
2153 | \f | |
2154 | /* Fix up the flags for a symbol. This handles various cases which | |
2155 | can only be fixed after all the input files are seen. This is | |
2156 | currently called by both adjust_dynamic_symbol and | |
2157 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2158 | the face of future changes. */ | |
2159 | ||
2160 | bfd_boolean | |
268b6b39 AM |
2161 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2162 | struct elf_info_failed *eif) | |
45d6a902 AM |
2163 | { |
2164 | /* If this symbol was mentioned in a non-ELF file, try to set | |
2165 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2166 | permit a non-ELF file to correctly refer to a symbol defined in | |
2167 | an ELF dynamic object. */ | |
f5385ebf | 2168 | if (h->non_elf) |
45d6a902 AM |
2169 | { |
2170 | while (h->root.type == bfd_link_hash_indirect) | |
2171 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2172 | ||
2173 | if (h->root.type != bfd_link_hash_defined | |
2174 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2175 | { |
2176 | h->ref_regular = 1; | |
2177 | h->ref_regular_nonweak = 1; | |
2178 | } | |
45d6a902 AM |
2179 | else |
2180 | { | |
2181 | if (h->root.u.def.section->owner != NULL | |
2182 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2183 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2184 | { |
2185 | h->ref_regular = 1; | |
2186 | h->ref_regular_nonweak = 1; | |
2187 | } | |
45d6a902 | 2188 | else |
f5385ebf | 2189 | h->def_regular = 1; |
45d6a902 AM |
2190 | } |
2191 | ||
2192 | if (h->dynindx == -1 | |
f5385ebf AM |
2193 | && (h->def_dynamic |
2194 | || h->ref_dynamic)) | |
45d6a902 | 2195 | { |
c152c796 | 2196 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2197 | { |
2198 | eif->failed = TRUE; | |
2199 | return FALSE; | |
2200 | } | |
2201 | } | |
2202 | } | |
2203 | else | |
2204 | { | |
f5385ebf | 2205 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2206 | was first seen in a non-ELF file. Fortunately, if the symbol |
2207 | was first seen in an ELF file, we're probably OK unless the | |
2208 | symbol was defined in a non-ELF file. Catch that case here. | |
2209 | FIXME: We're still in trouble if the symbol was first seen in | |
2210 | a dynamic object, and then later in a non-ELF regular object. */ | |
2211 | if ((h->root.type == bfd_link_hash_defined | |
2212 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2213 | && !h->def_regular |
45d6a902 AM |
2214 | && (h->root.u.def.section->owner != NULL |
2215 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2216 | != bfd_target_elf_flavour) | |
2217 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2218 | && !h->def_dynamic))) |
2219 | h->def_regular = 1; | |
45d6a902 AM |
2220 | } |
2221 | ||
2222 | /* If this is a final link, and the symbol was defined as a common | |
2223 | symbol in a regular object file, and there was no definition in | |
2224 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2225 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2226 | flag will not have been set. */ |
2227 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2228 | && !h->def_regular |
2229 | && h->ref_regular | |
2230 | && !h->def_dynamic | |
45d6a902 | 2231 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) |
f5385ebf | 2232 | h->def_regular = 1; |
45d6a902 AM |
2233 | |
2234 | /* If -Bsymbolic was used (which means to bind references to global | |
2235 | symbols to the definition within the shared object), and this | |
2236 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2237 | need a PLT entry. Likewise, if the symbol has non-default |
2238 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2239 | will force it local. */ |
f5385ebf | 2240 | if (h->needs_plt |
45d6a902 | 2241 | && eif->info->shared |
0eddce27 | 2242 | && is_elf_hash_table (eif->info->hash) |
45d6a902 | 2243 | && (eif->info->symbolic |
c1be741f | 2244 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2245 | && h->def_regular) |
45d6a902 | 2246 | { |
9c5bfbb7 | 2247 | const struct elf_backend_data *bed; |
45d6a902 AM |
2248 | bfd_boolean force_local; |
2249 | ||
2250 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2251 | ||
2252 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL | |
2253 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2254 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2255 | } | |
2256 | ||
2257 | /* If a weak undefined symbol has non-default visibility, we also | |
2258 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2259 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
2260 | && h->root.type == bfd_link_hash_undefweak) |
2261 | { | |
9c5bfbb7 | 2262 | const struct elf_backend_data *bed; |
45d6a902 AM |
2263 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2264 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2265 | } | |
2266 | ||
2267 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2268 | the real definition in the dynamic object, copy interesting flags | |
2269 | over to the real definition. */ | |
f6e332e6 | 2270 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2271 | { |
2272 | struct elf_link_hash_entry *weakdef; | |
2273 | ||
f6e332e6 | 2274 | weakdef = h->u.weakdef; |
45d6a902 AM |
2275 | if (h->root.type == bfd_link_hash_indirect) |
2276 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2277 | ||
2278 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2279 | || h->root.type == bfd_link_hash_defweak); | |
2280 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2281 | || weakdef->root.type == bfd_link_hash_defweak); | |
f5385ebf | 2282 | BFD_ASSERT (weakdef->def_dynamic); |
45d6a902 AM |
2283 | |
2284 | /* If the real definition is defined by a regular object file, | |
2285 | don't do anything special. See the longer description in | |
2286 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
f5385ebf | 2287 | if (weakdef->def_regular) |
f6e332e6 | 2288 | h->u.weakdef = NULL; |
45d6a902 AM |
2289 | else |
2290 | { | |
9c5bfbb7 | 2291 | const struct elf_backend_data *bed; |
45d6a902 AM |
2292 | |
2293 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2294 | (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h); | |
2295 | } | |
2296 | } | |
2297 | ||
2298 | return TRUE; | |
2299 | } | |
2300 | ||
2301 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2302 | called via elf_link_hash_traverse, and also calls itself | |
2303 | recursively. */ | |
2304 | ||
2305 | bfd_boolean | |
268b6b39 | 2306 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2307 | { |
268b6b39 | 2308 | struct elf_info_failed *eif = data; |
45d6a902 | 2309 | bfd *dynobj; |
9c5bfbb7 | 2310 | const struct elf_backend_data *bed; |
45d6a902 | 2311 | |
0eddce27 | 2312 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2313 | return FALSE; |
2314 | ||
2315 | if (h->root.type == bfd_link_hash_warning) | |
2316 | { | |
2317 | h->plt = elf_hash_table (eif->info)->init_offset; | |
2318 | h->got = elf_hash_table (eif->info)->init_offset; | |
2319 | ||
2320 | /* When warning symbols are created, they **replace** the "real" | |
2321 | entry in the hash table, thus we never get to see the real | |
2322 | symbol in a hash traversal. So look at it now. */ | |
2323 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2324 | } | |
2325 | ||
2326 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2327 | if (h->root.type == bfd_link_hash_indirect) | |
2328 | return TRUE; | |
2329 | ||
2330 | /* Fix the symbol flags. */ | |
2331 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2332 | return FALSE; | |
2333 | ||
2334 | /* If this symbol does not require a PLT entry, and it is not | |
2335 | defined by a dynamic object, or is not referenced by a regular | |
2336 | object, ignore it. We do have to handle a weak defined symbol, | |
2337 | even if no regular object refers to it, if we decided to add it | |
2338 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2339 | about symbols which are defined by one dynamic object and | |
2340 | referenced by another one? */ | |
f5385ebf AM |
2341 | if (!h->needs_plt |
2342 | && (h->def_regular | |
2343 | || !h->def_dynamic | |
2344 | || (!h->ref_regular | |
f6e332e6 | 2345 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 AM |
2346 | { |
2347 | h->plt = elf_hash_table (eif->info)->init_offset; | |
2348 | return TRUE; | |
2349 | } | |
2350 | ||
2351 | /* If we've already adjusted this symbol, don't do it again. This | |
2352 | can happen via a recursive call. */ | |
f5385ebf | 2353 | if (h->dynamic_adjusted) |
45d6a902 AM |
2354 | return TRUE; |
2355 | ||
2356 | /* Don't look at this symbol again. Note that we must set this | |
2357 | after checking the above conditions, because we may look at a | |
2358 | symbol once, decide not to do anything, and then get called | |
2359 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2360 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2361 | |
2362 | /* If this is a weak definition, and we know a real definition, and | |
2363 | the real symbol is not itself defined by a regular object file, | |
2364 | then get a good value for the real definition. We handle the | |
2365 | real symbol first, for the convenience of the backend routine. | |
2366 | ||
2367 | Note that there is a confusing case here. If the real definition | |
2368 | is defined by a regular object file, we don't get the real symbol | |
2369 | from the dynamic object, but we do get the weak symbol. If the | |
2370 | processor backend uses a COPY reloc, then if some routine in the | |
2371 | dynamic object changes the real symbol, we will not see that | |
2372 | change in the corresponding weak symbol. This is the way other | |
2373 | ELF linkers work as well, and seems to be a result of the shared | |
2374 | library model. | |
2375 | ||
2376 | I will clarify this issue. Most SVR4 shared libraries define the | |
2377 | variable _timezone and define timezone as a weak synonym. The | |
2378 | tzset call changes _timezone. If you write | |
2379 | extern int timezone; | |
2380 | int _timezone = 5; | |
2381 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2382 | you might expect that, since timezone is a synonym for _timezone, | |
2383 | the same number will print both times. However, if the processor | |
2384 | backend uses a COPY reloc, then actually timezone will be copied | |
2385 | into your process image, and, since you define _timezone | |
2386 | yourself, _timezone will not. Thus timezone and _timezone will | |
2387 | wind up at different memory locations. The tzset call will set | |
2388 | _timezone, leaving timezone unchanged. */ | |
2389 | ||
f6e332e6 | 2390 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2391 | { |
2392 | /* If we get to this point, we know there is an implicit | |
2393 | reference by a regular object file via the weak symbol H. | |
2394 | FIXME: Is this really true? What if the traversal finds | |
f6e332e6 AM |
2395 | H->U.WEAKDEF before it finds H? */ |
2396 | h->u.weakdef->ref_regular = 1; | |
45d6a902 | 2397 | |
f6e332e6 | 2398 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2399 | return FALSE; |
2400 | } | |
2401 | ||
2402 | /* If a symbol has no type and no size and does not require a PLT | |
2403 | entry, then we are probably about to do the wrong thing here: we | |
2404 | are probably going to create a COPY reloc for an empty object. | |
2405 | This case can arise when a shared object is built with assembly | |
2406 | code, and the assembly code fails to set the symbol type. */ | |
2407 | if (h->size == 0 | |
2408 | && h->type == STT_NOTYPE | |
f5385ebf | 2409 | && !h->needs_plt) |
45d6a902 AM |
2410 | (*_bfd_error_handler) |
2411 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2412 | h->root.root.string); | |
2413 | ||
2414 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2415 | bed = get_elf_backend_data (dynobj); | |
2416 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2417 | { | |
2418 | eif->failed = TRUE; | |
2419 | return FALSE; | |
2420 | } | |
2421 | ||
2422 | return TRUE; | |
2423 | } | |
2424 | ||
2425 | /* Adjust all external symbols pointing into SEC_MERGE sections | |
2426 | to reflect the object merging within the sections. */ | |
2427 | ||
2428 | bfd_boolean | |
268b6b39 | 2429 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2430 | { |
2431 | asection *sec; | |
2432 | ||
2433 | if (h->root.type == bfd_link_hash_warning) | |
2434 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2435 | ||
2436 | if ((h->root.type == bfd_link_hash_defined | |
2437 | || h->root.type == bfd_link_hash_defweak) | |
2438 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2439 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2440 | { | |
268b6b39 | 2441 | bfd *output_bfd = data; |
45d6a902 AM |
2442 | |
2443 | h->root.u.def.value = | |
2444 | _bfd_merged_section_offset (output_bfd, | |
2445 | &h->root.u.def.section, | |
2446 | elf_section_data (sec)->sec_info, | |
753731ee | 2447 | h->root.u.def.value); |
45d6a902 AM |
2448 | } |
2449 | ||
2450 | return TRUE; | |
2451 | } | |
986a241f RH |
2452 | |
2453 | /* Returns false if the symbol referred to by H should be considered | |
2454 | to resolve local to the current module, and true if it should be | |
2455 | considered to bind dynamically. */ | |
2456 | ||
2457 | bfd_boolean | |
268b6b39 AM |
2458 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2459 | struct bfd_link_info *info, | |
2460 | bfd_boolean ignore_protected) | |
986a241f RH |
2461 | { |
2462 | bfd_boolean binding_stays_local_p; | |
2463 | ||
2464 | if (h == NULL) | |
2465 | return FALSE; | |
2466 | ||
2467 | while (h->root.type == bfd_link_hash_indirect | |
2468 | || h->root.type == bfd_link_hash_warning) | |
2469 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2470 | ||
2471 | /* If it was forced local, then clearly it's not dynamic. */ | |
2472 | if (h->dynindx == -1) | |
2473 | return FALSE; | |
f5385ebf | 2474 | if (h->forced_local) |
986a241f RH |
2475 | return FALSE; |
2476 | ||
2477 | /* Identify the cases where name binding rules say that a | |
2478 | visible symbol resolves locally. */ | |
2479 | binding_stays_local_p = info->executable || info->symbolic; | |
2480 | ||
2481 | switch (ELF_ST_VISIBILITY (h->other)) | |
2482 | { | |
2483 | case STV_INTERNAL: | |
2484 | case STV_HIDDEN: | |
2485 | return FALSE; | |
2486 | ||
2487 | case STV_PROTECTED: | |
2488 | /* Proper resolution for function pointer equality may require | |
2489 | that these symbols perhaps be resolved dynamically, even though | |
2490 | we should be resolving them to the current module. */ | |
1c16dfa5 | 2491 | if (!ignore_protected || h->type != STT_FUNC) |
986a241f RH |
2492 | binding_stays_local_p = TRUE; |
2493 | break; | |
2494 | ||
2495 | default: | |
986a241f RH |
2496 | break; |
2497 | } | |
2498 | ||
aa37626c | 2499 | /* If it isn't defined locally, then clearly it's dynamic. */ |
f5385ebf | 2500 | if (!h->def_regular) |
aa37626c L |
2501 | return TRUE; |
2502 | ||
986a241f RH |
2503 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2504 | us that it remains local. */ | |
2505 | return !binding_stays_local_p; | |
2506 | } | |
f6c52c13 AM |
2507 | |
2508 | /* Return true if the symbol referred to by H should be considered | |
2509 | to resolve local to the current module, and false otherwise. Differs | |
2510 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2511 | undefined symbols and weak symbols. */ | |
2512 | ||
2513 | bfd_boolean | |
268b6b39 AM |
2514 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2515 | struct bfd_link_info *info, | |
2516 | bfd_boolean local_protected) | |
f6c52c13 AM |
2517 | { |
2518 | /* If it's a local sym, of course we resolve locally. */ | |
2519 | if (h == NULL) | |
2520 | return TRUE; | |
2521 | ||
7e2294f9 AO |
2522 | /* Common symbols that become definitions don't get the DEF_REGULAR |
2523 | flag set, so test it first, and don't bail out. */ | |
2524 | if (ELF_COMMON_DEF_P (h)) | |
2525 | /* Do nothing. */; | |
f6c52c13 AM |
2526 | /* If we don't have a definition in a regular file, then we can't |
2527 | resolve locally. The sym is either undefined or dynamic. */ | |
f5385ebf | 2528 | else if (!h->def_regular) |
f6c52c13 AM |
2529 | return FALSE; |
2530 | ||
2531 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 2532 | if (h->forced_local) |
f6c52c13 AM |
2533 | return TRUE; |
2534 | ||
2535 | /* As do non-dynamic symbols. */ | |
2536 | if (h->dynindx == -1) | |
2537 | return TRUE; | |
2538 | ||
2539 | /* At this point, we know the symbol is defined and dynamic. In an | |
2540 | executable it must resolve locally, likewise when building symbolic | |
2541 | shared libraries. */ | |
2542 | if (info->executable || info->symbolic) | |
2543 | return TRUE; | |
2544 | ||
2545 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2546 | with default visibility might not resolve locally. */ | |
2547 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2548 | return FALSE; | |
2549 | ||
2550 | /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */ | |
2551 | if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED) | |
2552 | return TRUE; | |
2553 | ||
1c16dfa5 L |
2554 | /* STV_PROTECTED non-function symbols are local. */ |
2555 | if (h->type != STT_FUNC) | |
2556 | return TRUE; | |
2557 | ||
f6c52c13 AM |
2558 | /* Function pointer equality tests may require that STV_PROTECTED |
2559 | symbols be treated as dynamic symbols, even when we know that the | |
2560 | dynamic linker will resolve them locally. */ | |
2561 | return local_protected; | |
2562 | } | |
e1918d23 AM |
2563 | |
2564 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2565 | aligned. Returns the first TLS output section. */ | |
2566 | ||
2567 | struct bfd_section * | |
2568 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2569 | { | |
2570 | struct bfd_section *sec, *tls; | |
2571 | unsigned int align = 0; | |
2572 | ||
2573 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2574 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2575 | break; | |
2576 | tls = sec; | |
2577 | ||
2578 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2579 | if (sec->alignment_power > align) | |
2580 | align = sec->alignment_power; | |
2581 | ||
2582 | elf_hash_table (info)->tls_sec = tls; | |
2583 | ||
2584 | /* Ensure the alignment of the first section is the largest alignment, | |
2585 | so that the tls segment starts aligned. */ | |
2586 | if (tls != NULL) | |
2587 | tls->alignment_power = align; | |
2588 | ||
2589 | return tls; | |
2590 | } | |
0ad989f9 L |
2591 | |
2592 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2593 | static bfd_boolean | |
2594 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2595 | Elf_Internal_Sym *sym) | |
2596 | { | |
2597 | /* Local symbols do not count, but target specific ones might. */ | |
2598 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2599 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2600 | return FALSE; | |
2601 | ||
2602 | /* Function symbols do not count. */ | |
2603 | if (ELF_ST_TYPE (sym->st_info) == STT_FUNC) | |
2604 | return FALSE; | |
2605 | ||
2606 | /* If the section is undefined, then so is the symbol. */ | |
2607 | if (sym->st_shndx == SHN_UNDEF) | |
2608 | return FALSE; | |
2609 | ||
2610 | /* If the symbol is defined in the common section, then | |
2611 | it is a common definition and so does not count. */ | |
2612 | if (sym->st_shndx == SHN_COMMON) | |
2613 | return FALSE; | |
2614 | ||
2615 | /* If the symbol is in a target specific section then we | |
2616 | must rely upon the backend to tell us what it is. */ | |
2617 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2618 | /* FIXME - this function is not coded yet: | |
2619 | ||
2620 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2621 | ||
2622 | Instead for now assume that the definition is not global, | |
2623 | Even if this is wrong, at least the linker will behave | |
2624 | in the same way that it used to do. */ | |
2625 | return FALSE; | |
2626 | ||
2627 | return TRUE; | |
2628 | } | |
2629 | ||
2630 | /* Search the symbol table of the archive element of the archive ABFD | |
2631 | whose archive map contains a mention of SYMDEF, and determine if | |
2632 | the symbol is defined in this element. */ | |
2633 | static bfd_boolean | |
2634 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2635 | { | |
2636 | Elf_Internal_Shdr * hdr; | |
2637 | bfd_size_type symcount; | |
2638 | bfd_size_type extsymcount; | |
2639 | bfd_size_type extsymoff; | |
2640 | Elf_Internal_Sym *isymbuf; | |
2641 | Elf_Internal_Sym *isym; | |
2642 | Elf_Internal_Sym *isymend; | |
2643 | bfd_boolean result; | |
2644 | ||
2645 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2646 | if (abfd == NULL) | |
2647 | return FALSE; | |
2648 | ||
2649 | if (! bfd_check_format (abfd, bfd_object)) | |
2650 | return FALSE; | |
2651 | ||
2652 | /* If we have already included the element containing this symbol in the | |
2653 | link then we do not need to include it again. Just claim that any symbol | |
2654 | it contains is not a definition, so that our caller will not decide to | |
2655 | (re)include this element. */ | |
2656 | if (abfd->archive_pass) | |
2657 | return FALSE; | |
2658 | ||
2659 | /* Select the appropriate symbol table. */ | |
2660 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2661 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2662 | else | |
2663 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2664 | ||
2665 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2666 | ||
2667 | /* The sh_info field of the symtab header tells us where the | |
2668 | external symbols start. We don't care about the local symbols. */ | |
2669 | if (elf_bad_symtab (abfd)) | |
2670 | { | |
2671 | extsymcount = symcount; | |
2672 | extsymoff = 0; | |
2673 | } | |
2674 | else | |
2675 | { | |
2676 | extsymcount = symcount - hdr->sh_info; | |
2677 | extsymoff = hdr->sh_info; | |
2678 | } | |
2679 | ||
2680 | if (extsymcount == 0) | |
2681 | return FALSE; | |
2682 | ||
2683 | /* Read in the symbol table. */ | |
2684 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2685 | NULL, NULL, NULL); | |
2686 | if (isymbuf == NULL) | |
2687 | return FALSE; | |
2688 | ||
2689 | /* Scan the symbol table looking for SYMDEF. */ | |
2690 | result = FALSE; | |
2691 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2692 | { | |
2693 | const char *name; | |
2694 | ||
2695 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
2696 | isym->st_name); | |
2697 | if (name == NULL) | |
2698 | break; | |
2699 | ||
2700 | if (strcmp (name, symdef->name) == 0) | |
2701 | { | |
2702 | result = is_global_data_symbol_definition (abfd, isym); | |
2703 | break; | |
2704 | } | |
2705 | } | |
2706 | ||
2707 | free (isymbuf); | |
2708 | ||
2709 | return result; | |
2710 | } | |
2711 | \f | |
5a580b3a AM |
2712 | /* Add an entry to the .dynamic table. */ |
2713 | ||
2714 | bfd_boolean | |
2715 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
2716 | bfd_vma tag, | |
2717 | bfd_vma val) | |
2718 | { | |
2719 | struct elf_link_hash_table *hash_table; | |
2720 | const struct elf_backend_data *bed; | |
2721 | asection *s; | |
2722 | bfd_size_type newsize; | |
2723 | bfd_byte *newcontents; | |
2724 | Elf_Internal_Dyn dyn; | |
2725 | ||
2726 | hash_table = elf_hash_table (info); | |
2727 | if (! is_elf_hash_table (hash_table)) | |
2728 | return FALSE; | |
2729 | ||
8fdd7217 NC |
2730 | if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL) |
2731 | _bfd_error_handler | |
2732 | (_("warning: creating a DT_TEXTREL in a shared object.")); | |
2733 | ||
5a580b3a AM |
2734 | bed = get_elf_backend_data (hash_table->dynobj); |
2735 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2736 | BFD_ASSERT (s != NULL); | |
2737 | ||
eea6121a | 2738 | newsize = s->size + bed->s->sizeof_dyn; |
5a580b3a AM |
2739 | newcontents = bfd_realloc (s->contents, newsize); |
2740 | if (newcontents == NULL) | |
2741 | return FALSE; | |
2742 | ||
2743 | dyn.d_tag = tag; | |
2744 | dyn.d_un.d_val = val; | |
eea6121a | 2745 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 2746 | |
eea6121a | 2747 | s->size = newsize; |
5a580b3a AM |
2748 | s->contents = newcontents; |
2749 | ||
2750 | return TRUE; | |
2751 | } | |
2752 | ||
2753 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
2754 | otherwise just check whether one already exists. Returns -1 on error, | |
2755 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
2756 | ||
4ad4eba5 | 2757 | static int |
7e9f0867 AM |
2758 | elf_add_dt_needed_tag (bfd *abfd, |
2759 | struct bfd_link_info *info, | |
4ad4eba5 AM |
2760 | const char *soname, |
2761 | bfd_boolean do_it) | |
5a580b3a AM |
2762 | { |
2763 | struct elf_link_hash_table *hash_table; | |
2764 | bfd_size_type oldsize; | |
2765 | bfd_size_type strindex; | |
2766 | ||
7e9f0867 AM |
2767 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
2768 | return -1; | |
2769 | ||
5a580b3a AM |
2770 | hash_table = elf_hash_table (info); |
2771 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
2772 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
2773 | if (strindex == (bfd_size_type) -1) | |
2774 | return -1; | |
2775 | ||
2776 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
2777 | { | |
2778 | asection *sdyn; | |
2779 | const struct elf_backend_data *bed; | |
2780 | bfd_byte *extdyn; | |
2781 | ||
2782 | bed = get_elf_backend_data (hash_table->dynobj); | |
2783 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
7e9f0867 AM |
2784 | if (sdyn != NULL) |
2785 | for (extdyn = sdyn->contents; | |
2786 | extdyn < sdyn->contents + sdyn->size; | |
2787 | extdyn += bed->s->sizeof_dyn) | |
2788 | { | |
2789 | Elf_Internal_Dyn dyn; | |
5a580b3a | 2790 | |
7e9f0867 AM |
2791 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
2792 | if (dyn.d_tag == DT_NEEDED | |
2793 | && dyn.d_un.d_val == strindex) | |
2794 | { | |
2795 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2796 | return 1; | |
2797 | } | |
2798 | } | |
5a580b3a AM |
2799 | } |
2800 | ||
2801 | if (do_it) | |
2802 | { | |
7e9f0867 AM |
2803 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
2804 | return -1; | |
2805 | ||
5a580b3a AM |
2806 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
2807 | return -1; | |
2808 | } | |
2809 | else | |
2810 | /* We were just checking for existence of the tag. */ | |
2811 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2812 | ||
2813 | return 0; | |
2814 | } | |
2815 | ||
2816 | /* Sort symbol by value and section. */ | |
4ad4eba5 AM |
2817 | static int |
2818 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
2819 | { |
2820 | const struct elf_link_hash_entry *h1; | |
2821 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 2822 | bfd_signed_vma vdiff; |
5a580b3a AM |
2823 | |
2824 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
2825 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
2826 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
2827 | if (vdiff != 0) | |
2828 | return vdiff > 0 ? 1 : -1; | |
2829 | else | |
2830 | { | |
2831 | long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
2832 | if (sdiff != 0) | |
2833 | return sdiff > 0 ? 1 : -1; | |
2834 | } | |
5a580b3a AM |
2835 | return 0; |
2836 | } | |
4ad4eba5 | 2837 | |
5a580b3a AM |
2838 | /* This function is used to adjust offsets into .dynstr for |
2839 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
2840 | ||
2841 | static bfd_boolean | |
2842 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
2843 | { | |
2844 | struct elf_strtab_hash *dynstr = data; | |
2845 | ||
2846 | if (h->root.type == bfd_link_hash_warning) | |
2847 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2848 | ||
2849 | if (h->dynindx != -1) | |
2850 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
2851 | return TRUE; | |
2852 | } | |
2853 | ||
2854 | /* Assign string offsets in .dynstr, update all structures referencing | |
2855 | them. */ | |
2856 | ||
4ad4eba5 AM |
2857 | static bfd_boolean |
2858 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
2859 | { |
2860 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
2861 | struct elf_link_local_dynamic_entry *entry; | |
2862 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
2863 | bfd *dynobj = hash_table->dynobj; | |
2864 | asection *sdyn; | |
2865 | bfd_size_type size; | |
2866 | const struct elf_backend_data *bed; | |
2867 | bfd_byte *extdyn; | |
2868 | ||
2869 | _bfd_elf_strtab_finalize (dynstr); | |
2870 | size = _bfd_elf_strtab_size (dynstr); | |
2871 | ||
2872 | bed = get_elf_backend_data (dynobj); | |
2873 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
2874 | BFD_ASSERT (sdyn != NULL); | |
2875 | ||
2876 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
2877 | for (extdyn = sdyn->contents; | |
eea6121a | 2878 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
2879 | extdyn += bed->s->sizeof_dyn) |
2880 | { | |
2881 | Elf_Internal_Dyn dyn; | |
2882 | ||
2883 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
2884 | switch (dyn.d_tag) | |
2885 | { | |
2886 | case DT_STRSZ: | |
2887 | dyn.d_un.d_val = size; | |
2888 | break; | |
2889 | case DT_NEEDED: | |
2890 | case DT_SONAME: | |
2891 | case DT_RPATH: | |
2892 | case DT_RUNPATH: | |
2893 | case DT_FILTER: | |
2894 | case DT_AUXILIARY: | |
2895 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
2896 | break; | |
2897 | default: | |
2898 | continue; | |
2899 | } | |
2900 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
2901 | } | |
2902 | ||
2903 | /* Now update local dynamic symbols. */ | |
2904 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
2905 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
2906 | entry->isym.st_name); | |
2907 | ||
2908 | /* And the rest of dynamic symbols. */ | |
2909 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
2910 | ||
2911 | /* Adjust version definitions. */ | |
2912 | if (elf_tdata (output_bfd)->cverdefs) | |
2913 | { | |
2914 | asection *s; | |
2915 | bfd_byte *p; | |
2916 | bfd_size_type i; | |
2917 | Elf_Internal_Verdef def; | |
2918 | Elf_Internal_Verdaux defaux; | |
2919 | ||
2920 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
2921 | p = s->contents; | |
2922 | do | |
2923 | { | |
2924 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
2925 | &def); | |
2926 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
2927 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
2928 | continue; | |
5a580b3a AM |
2929 | for (i = 0; i < def.vd_cnt; ++i) |
2930 | { | |
2931 | _bfd_elf_swap_verdaux_in (output_bfd, | |
2932 | (Elf_External_Verdaux *) p, &defaux); | |
2933 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
2934 | defaux.vda_name); | |
2935 | _bfd_elf_swap_verdaux_out (output_bfd, | |
2936 | &defaux, (Elf_External_Verdaux *) p); | |
2937 | p += sizeof (Elf_External_Verdaux); | |
2938 | } | |
2939 | } | |
2940 | while (def.vd_next); | |
2941 | } | |
2942 | ||
2943 | /* Adjust version references. */ | |
2944 | if (elf_tdata (output_bfd)->verref) | |
2945 | { | |
2946 | asection *s; | |
2947 | bfd_byte *p; | |
2948 | bfd_size_type i; | |
2949 | Elf_Internal_Verneed need; | |
2950 | Elf_Internal_Vernaux needaux; | |
2951 | ||
2952 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
2953 | p = s->contents; | |
2954 | do | |
2955 | { | |
2956 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
2957 | &need); | |
2958 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
2959 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
2960 | (Elf_External_Verneed *) p); | |
2961 | p += sizeof (Elf_External_Verneed); | |
2962 | for (i = 0; i < need.vn_cnt; ++i) | |
2963 | { | |
2964 | _bfd_elf_swap_vernaux_in (output_bfd, | |
2965 | (Elf_External_Vernaux *) p, &needaux); | |
2966 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
2967 | needaux.vna_name); | |
2968 | _bfd_elf_swap_vernaux_out (output_bfd, | |
2969 | &needaux, | |
2970 | (Elf_External_Vernaux *) p); | |
2971 | p += sizeof (Elf_External_Vernaux); | |
2972 | } | |
2973 | } | |
2974 | while (need.vn_next); | |
2975 | } | |
2976 | ||
2977 | return TRUE; | |
2978 | } | |
2979 | \f | |
4ad4eba5 AM |
2980 | /* Add symbols from an ELF object file to the linker hash table. */ |
2981 | ||
2982 | static bfd_boolean | |
2983 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
2984 | { | |
2985 | bfd_boolean (*add_symbol_hook) | |
555cd476 | 2986 | (bfd *, struct bfd_link_info *, Elf_Internal_Sym *, |
4ad4eba5 AM |
2987 | const char **, flagword *, asection **, bfd_vma *); |
2988 | bfd_boolean (*check_relocs) | |
2989 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
85fbca6a NC |
2990 | bfd_boolean (*check_directives) |
2991 | (bfd *, struct bfd_link_info *); | |
4ad4eba5 AM |
2992 | bfd_boolean collect; |
2993 | Elf_Internal_Shdr *hdr; | |
2994 | bfd_size_type symcount; | |
2995 | bfd_size_type extsymcount; | |
2996 | bfd_size_type extsymoff; | |
2997 | struct elf_link_hash_entry **sym_hash; | |
2998 | bfd_boolean dynamic; | |
2999 | Elf_External_Versym *extversym = NULL; | |
3000 | Elf_External_Versym *ever; | |
3001 | struct elf_link_hash_entry *weaks; | |
3002 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
3003 | bfd_size_type nondeflt_vers_cnt = 0; | |
3004 | Elf_Internal_Sym *isymbuf = NULL; | |
3005 | Elf_Internal_Sym *isym; | |
3006 | Elf_Internal_Sym *isymend; | |
3007 | const struct elf_backend_data *bed; | |
3008 | bfd_boolean add_needed; | |
3009 | struct elf_link_hash_table * hash_table; | |
3010 | bfd_size_type amt; | |
3011 | ||
3012 | hash_table = elf_hash_table (info); | |
3013 | ||
3014 | bed = get_elf_backend_data (abfd); | |
3015 | add_symbol_hook = bed->elf_add_symbol_hook; | |
3016 | collect = bed->collect; | |
3017 | ||
3018 | if ((abfd->flags & DYNAMIC) == 0) | |
3019 | dynamic = FALSE; | |
3020 | else | |
3021 | { | |
3022 | dynamic = TRUE; | |
3023 | ||
3024 | /* You can't use -r against a dynamic object. Also, there's no | |
3025 | hope of using a dynamic object which does not exactly match | |
3026 | the format of the output file. */ | |
3027 | if (info->relocatable | |
3028 | || !is_elf_hash_table (hash_table) | |
3029 | || hash_table->root.creator != abfd->xvec) | |
3030 | { | |
9a0789ec NC |
3031 | if (info->relocatable) |
3032 | bfd_set_error (bfd_error_invalid_operation); | |
3033 | else | |
3034 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3035 | goto error_return; |
3036 | } | |
3037 | } | |
3038 | ||
3039 | /* As a GNU extension, any input sections which are named | |
3040 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3041 | symbol. This differs from .gnu.warning sections, which generate | |
3042 | warnings when they are included in an output file. */ | |
3043 | if (info->executable) | |
3044 | { | |
3045 | asection *s; | |
3046 | ||
3047 | for (s = abfd->sections; s != NULL; s = s->next) | |
3048 | { | |
3049 | const char *name; | |
3050 | ||
3051 | name = bfd_get_section_name (abfd, s); | |
3052 | if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0) | |
3053 | { | |
3054 | char *msg; | |
3055 | bfd_size_type sz; | |
3056 | bfd_size_type prefix_len; | |
3057 | const char * gnu_warning_prefix = _("warning: "); | |
3058 | ||
3059 | name += sizeof ".gnu.warning." - 1; | |
3060 | ||
3061 | /* If this is a shared object, then look up the symbol | |
3062 | in the hash table. If it is there, and it is already | |
3063 | been defined, then we will not be using the entry | |
3064 | from this shared object, so we don't need to warn. | |
3065 | FIXME: If we see the definition in a regular object | |
3066 | later on, we will warn, but we shouldn't. The only | |
3067 | fix is to keep track of what warnings we are supposed | |
3068 | to emit, and then handle them all at the end of the | |
3069 | link. */ | |
3070 | if (dynamic) | |
3071 | { | |
3072 | struct elf_link_hash_entry *h; | |
3073 | ||
3074 | h = elf_link_hash_lookup (hash_table, name, | |
3075 | FALSE, FALSE, TRUE); | |
3076 | ||
3077 | /* FIXME: What about bfd_link_hash_common? */ | |
3078 | if (h != NULL | |
3079 | && (h->root.type == bfd_link_hash_defined | |
3080 | || h->root.type == bfd_link_hash_defweak)) | |
3081 | { | |
3082 | /* We don't want to issue this warning. Clobber | |
3083 | the section size so that the warning does not | |
3084 | get copied into the output file. */ | |
eea6121a | 3085 | s->size = 0; |
4ad4eba5 AM |
3086 | continue; |
3087 | } | |
3088 | } | |
3089 | ||
eea6121a | 3090 | sz = s->size; |
4ad4eba5 AM |
3091 | prefix_len = strlen (gnu_warning_prefix); |
3092 | msg = bfd_alloc (abfd, prefix_len + sz + 1); | |
3093 | if (msg == NULL) | |
3094 | goto error_return; | |
3095 | ||
3096 | strcpy (msg, gnu_warning_prefix); | |
3097 | if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz)) | |
3098 | goto error_return; | |
3099 | ||
3100 | msg[prefix_len + sz] = '\0'; | |
3101 | ||
3102 | if (! (_bfd_generic_link_add_one_symbol | |
3103 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3104 | FALSE, collect, NULL))) | |
3105 | goto error_return; | |
3106 | ||
3107 | if (! info->relocatable) | |
3108 | { | |
3109 | /* Clobber the section size so that the warning does | |
3110 | not get copied into the output file. */ | |
eea6121a | 3111 | s->size = 0; |
4ad4eba5 AM |
3112 | } |
3113 | } | |
3114 | } | |
3115 | } | |
3116 | ||
3117 | add_needed = TRUE; | |
3118 | if (! dynamic) | |
3119 | { | |
3120 | /* If we are creating a shared library, create all the dynamic | |
3121 | sections immediately. We need to attach them to something, | |
3122 | so we attach them to this BFD, provided it is the right | |
3123 | format. FIXME: If there are no input BFD's of the same | |
3124 | format as the output, we can't make a shared library. */ | |
3125 | if (info->shared | |
3126 | && is_elf_hash_table (hash_table) | |
3127 | && hash_table->root.creator == abfd->xvec | |
3128 | && ! hash_table->dynamic_sections_created) | |
3129 | { | |
3130 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3131 | goto error_return; | |
3132 | } | |
3133 | } | |
3134 | else if (!is_elf_hash_table (hash_table)) | |
3135 | goto error_return; | |
3136 | else | |
3137 | { | |
3138 | asection *s; | |
3139 | const char *soname = NULL; | |
3140 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; | |
3141 | int ret; | |
3142 | ||
3143 | /* ld --just-symbols and dynamic objects don't mix very well. | |
3144 | Test for --just-symbols by looking at info set up by | |
3145 | _bfd_elf_link_just_syms. */ | |
3146 | if ((s = abfd->sections) != NULL | |
3147 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
3148 | goto error_return; | |
3149 | ||
3150 | /* If this dynamic lib was specified on the command line with | |
3151 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3152 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3153 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3154 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3155 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3156 | all. */ | |
3157 | add_needed = (elf_dyn_lib_class (abfd) | |
3158 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3159 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3160 | |
3161 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3162 | if (s != NULL) | |
3163 | { | |
3164 | bfd_byte *dynbuf; | |
3165 | bfd_byte *extdyn; | |
3166 | int elfsec; | |
3167 | unsigned long shlink; | |
3168 | ||
eea6121a | 3169 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
4ad4eba5 AM |
3170 | goto error_free_dyn; |
3171 | ||
3172 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
3173 | if (elfsec == -1) | |
3174 | goto error_free_dyn; | |
3175 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3176 | ||
3177 | for (extdyn = dynbuf; | |
eea6121a | 3178 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3179 | extdyn += bed->s->sizeof_dyn) |
3180 | { | |
3181 | Elf_Internal_Dyn dyn; | |
3182 | ||
3183 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3184 | if (dyn.d_tag == DT_SONAME) | |
3185 | { | |
3186 | unsigned int tagv = dyn.d_un.d_val; | |
3187 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3188 | if (soname == NULL) | |
3189 | goto error_free_dyn; | |
3190 | } | |
3191 | if (dyn.d_tag == DT_NEEDED) | |
3192 | { | |
3193 | struct bfd_link_needed_list *n, **pn; | |
3194 | char *fnm, *anm; | |
3195 | unsigned int tagv = dyn.d_un.d_val; | |
3196 | ||
3197 | amt = sizeof (struct bfd_link_needed_list); | |
3198 | n = bfd_alloc (abfd, amt); | |
3199 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3200 | if (n == NULL || fnm == NULL) | |
3201 | goto error_free_dyn; | |
3202 | amt = strlen (fnm) + 1; | |
3203 | anm = bfd_alloc (abfd, amt); | |
3204 | if (anm == NULL) | |
3205 | goto error_free_dyn; | |
3206 | memcpy (anm, fnm, amt); | |
3207 | n->name = anm; | |
3208 | n->by = abfd; | |
3209 | n->next = NULL; | |
3210 | for (pn = & hash_table->needed; | |
3211 | *pn != NULL; | |
3212 | pn = &(*pn)->next) | |
3213 | ; | |
3214 | *pn = n; | |
3215 | } | |
3216 | if (dyn.d_tag == DT_RUNPATH) | |
3217 | { | |
3218 | struct bfd_link_needed_list *n, **pn; | |
3219 | char *fnm, *anm; | |
3220 | unsigned int tagv = dyn.d_un.d_val; | |
3221 | ||
3222 | amt = sizeof (struct bfd_link_needed_list); | |
3223 | n = bfd_alloc (abfd, amt); | |
3224 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3225 | if (n == NULL || fnm == NULL) | |
3226 | goto error_free_dyn; | |
3227 | amt = strlen (fnm) + 1; | |
3228 | anm = bfd_alloc (abfd, amt); | |
3229 | if (anm == NULL) | |
3230 | goto error_free_dyn; | |
3231 | memcpy (anm, fnm, amt); | |
3232 | n->name = anm; | |
3233 | n->by = abfd; | |
3234 | n->next = NULL; | |
3235 | for (pn = & runpath; | |
3236 | *pn != NULL; | |
3237 | pn = &(*pn)->next) | |
3238 | ; | |
3239 | *pn = n; | |
3240 | } | |
3241 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3242 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3243 | { | |
3244 | struct bfd_link_needed_list *n, **pn; | |
3245 | char *fnm, *anm; | |
3246 | unsigned int tagv = dyn.d_un.d_val; | |
3247 | ||
3248 | amt = sizeof (struct bfd_link_needed_list); | |
3249 | n = bfd_alloc (abfd, amt); | |
3250 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3251 | if (n == NULL || fnm == NULL) | |
3252 | goto error_free_dyn; | |
3253 | amt = strlen (fnm) + 1; | |
3254 | anm = bfd_alloc (abfd, amt); | |
3255 | if (anm == NULL) | |
3256 | { | |
3257 | error_free_dyn: | |
3258 | free (dynbuf); | |
3259 | goto error_return; | |
3260 | } | |
3261 | memcpy (anm, fnm, amt); | |
3262 | n->name = anm; | |
3263 | n->by = abfd; | |
3264 | n->next = NULL; | |
3265 | for (pn = & rpath; | |
3266 | *pn != NULL; | |
3267 | pn = &(*pn)->next) | |
3268 | ; | |
3269 | *pn = n; | |
3270 | } | |
3271 | } | |
3272 | ||
3273 | free (dynbuf); | |
3274 | } | |
3275 | ||
3276 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3277 | frees all more recently bfd_alloc'd blocks as well. */ | |
3278 | if (runpath) | |
3279 | rpath = runpath; | |
3280 | ||
3281 | if (rpath) | |
3282 | { | |
3283 | struct bfd_link_needed_list **pn; | |
3284 | for (pn = & hash_table->runpath; | |
3285 | *pn != NULL; | |
3286 | pn = &(*pn)->next) | |
3287 | ; | |
3288 | *pn = rpath; | |
3289 | } | |
3290 | ||
3291 | /* We do not want to include any of the sections in a dynamic | |
3292 | object in the output file. We hack by simply clobbering the | |
3293 | list of sections in the BFD. This could be handled more | |
3294 | cleanly by, say, a new section flag; the existing | |
3295 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3296 | still implies that the section takes up space in the output | |
3297 | file. */ | |
3298 | bfd_section_list_clear (abfd); | |
3299 | ||
4ad4eba5 AM |
3300 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3301 | object. If the object has a DT_SONAME entry, we use it. | |
3302 | Otherwise, if the generic linker stuck something in | |
3303 | elf_dt_name, we use that. Otherwise, we just use the file | |
3304 | name. */ | |
3305 | if (soname == NULL || *soname == '\0') | |
3306 | { | |
3307 | soname = elf_dt_name (abfd); | |
3308 | if (soname == NULL || *soname == '\0') | |
3309 | soname = bfd_get_filename (abfd); | |
3310 | } | |
3311 | ||
3312 | /* Save the SONAME because sometimes the linker emulation code | |
3313 | will need to know it. */ | |
3314 | elf_dt_name (abfd) = soname; | |
3315 | ||
7e9f0867 | 3316 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3317 | if (ret < 0) |
3318 | goto error_return; | |
3319 | ||
3320 | /* If we have already included this dynamic object in the | |
3321 | link, just ignore it. There is no reason to include a | |
3322 | particular dynamic object more than once. */ | |
3323 | if (ret > 0) | |
3324 | return TRUE; | |
3325 | } | |
3326 | ||
3327 | /* If this is a dynamic object, we always link against the .dynsym | |
3328 | symbol table, not the .symtab symbol table. The dynamic linker | |
3329 | will only see the .dynsym symbol table, so there is no reason to | |
3330 | look at .symtab for a dynamic object. */ | |
3331 | ||
3332 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3333 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3334 | else | |
3335 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3336 | ||
3337 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3338 | ||
3339 | /* The sh_info field of the symtab header tells us where the | |
3340 | external symbols start. We don't care about the local symbols at | |
3341 | this point. */ | |
3342 | if (elf_bad_symtab (abfd)) | |
3343 | { | |
3344 | extsymcount = symcount; | |
3345 | extsymoff = 0; | |
3346 | } | |
3347 | else | |
3348 | { | |
3349 | extsymcount = symcount - hdr->sh_info; | |
3350 | extsymoff = hdr->sh_info; | |
3351 | } | |
3352 | ||
3353 | sym_hash = NULL; | |
3354 | if (extsymcount != 0) | |
3355 | { | |
3356 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3357 | NULL, NULL, NULL); | |
3358 | if (isymbuf == NULL) | |
3359 | goto error_return; | |
3360 | ||
3361 | /* We store a pointer to the hash table entry for each external | |
3362 | symbol. */ | |
3363 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3364 | sym_hash = bfd_alloc (abfd, amt); | |
3365 | if (sym_hash == NULL) | |
3366 | goto error_free_sym; | |
3367 | elf_sym_hashes (abfd) = sym_hash; | |
3368 | } | |
3369 | ||
3370 | if (dynamic) | |
3371 | { | |
3372 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
3373 | if (!_bfd_elf_slurp_version_tables (abfd, |
3374 | info->default_imported_symver)) | |
4ad4eba5 AM |
3375 | goto error_free_sym; |
3376 | ||
3377 | /* Read in the symbol versions, but don't bother to convert them | |
3378 | to internal format. */ | |
3379 | if (elf_dynversym (abfd) != 0) | |
3380 | { | |
3381 | Elf_Internal_Shdr *versymhdr; | |
3382 | ||
3383 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
3384 | extversym = bfd_malloc (versymhdr->sh_size); | |
3385 | if (extversym == NULL) | |
3386 | goto error_free_sym; | |
3387 | amt = versymhdr->sh_size; | |
3388 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3389 | || bfd_bread (extversym, amt, abfd) != amt) | |
3390 | goto error_free_vers; | |
3391 | } | |
3392 | } | |
3393 | ||
3394 | weaks = NULL; | |
3395 | ||
3396 | ever = extversym != NULL ? extversym + extsymoff : NULL; | |
3397 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3398 | isym < isymend; | |
3399 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3400 | { | |
3401 | int bind; | |
3402 | bfd_vma value; | |
3403 | asection *sec; | |
3404 | flagword flags; | |
3405 | const char *name; | |
3406 | struct elf_link_hash_entry *h; | |
3407 | bfd_boolean definition; | |
3408 | bfd_boolean size_change_ok; | |
3409 | bfd_boolean type_change_ok; | |
3410 | bfd_boolean new_weakdef; | |
3411 | bfd_boolean override; | |
3412 | unsigned int old_alignment; | |
3413 | bfd *old_bfd; | |
3414 | ||
3415 | override = FALSE; | |
3416 | ||
3417 | flags = BSF_NO_FLAGS; | |
3418 | sec = NULL; | |
3419 | value = isym->st_value; | |
3420 | *sym_hash = NULL; | |
3421 | ||
3422 | bind = ELF_ST_BIND (isym->st_info); | |
3423 | if (bind == STB_LOCAL) | |
3424 | { | |
3425 | /* This should be impossible, since ELF requires that all | |
3426 | global symbols follow all local symbols, and that sh_info | |
3427 | point to the first global symbol. Unfortunately, Irix 5 | |
3428 | screws this up. */ | |
3429 | continue; | |
3430 | } | |
3431 | else if (bind == STB_GLOBAL) | |
3432 | { | |
3433 | if (isym->st_shndx != SHN_UNDEF | |
3434 | && isym->st_shndx != SHN_COMMON) | |
3435 | flags = BSF_GLOBAL; | |
3436 | } | |
3437 | else if (bind == STB_WEAK) | |
3438 | flags = BSF_WEAK; | |
3439 | else | |
3440 | { | |
3441 | /* Leave it up to the processor backend. */ | |
3442 | } | |
3443 | ||
3444 | if (isym->st_shndx == SHN_UNDEF) | |
3445 | sec = bfd_und_section_ptr; | |
3446 | else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
3447 | { | |
3448 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3449 | if (sec == NULL) | |
3450 | sec = bfd_abs_section_ptr; | |
529fcb95 PB |
3451 | else if (sec->kept_section) |
3452 | { | |
3453 | /* Symbols from discarded section are undefined. */ | |
3454 | sec = bfd_und_section_ptr; | |
3455 | isym->st_shndx = SHN_UNDEF; | |
3456 | } | |
4ad4eba5 AM |
3457 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
3458 | value -= sec->vma; | |
3459 | } | |
3460 | else if (isym->st_shndx == SHN_ABS) | |
3461 | sec = bfd_abs_section_ptr; | |
3462 | else if (isym->st_shndx == SHN_COMMON) | |
3463 | { | |
3464 | sec = bfd_com_section_ptr; | |
3465 | /* What ELF calls the size we call the value. What ELF | |
3466 | calls the value we call the alignment. */ | |
3467 | value = isym->st_size; | |
3468 | } | |
3469 | else | |
3470 | { | |
3471 | /* Leave it up to the processor backend. */ | |
3472 | } | |
3473 | ||
3474 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3475 | isym->st_name); | |
3476 | if (name == NULL) | |
3477 | goto error_free_vers; | |
3478 | ||
3479 | if (isym->st_shndx == SHN_COMMON | |
3480 | && ELF_ST_TYPE (isym->st_info) == STT_TLS) | |
3481 | { | |
3482 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3483 | ||
3484 | if (tcomm == NULL) | |
3485 | { | |
3486 | tcomm = bfd_make_section (abfd, ".tcommon"); | |
3487 | if (tcomm == NULL | |
3488 | || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC | |
3489 | | SEC_IS_COMMON | |
3490 | | SEC_LINKER_CREATED | |
3491 | | SEC_THREAD_LOCAL))) | |
3492 | goto error_free_vers; | |
3493 | } | |
3494 | sec = tcomm; | |
3495 | } | |
3496 | else if (add_symbol_hook) | |
3497 | { | |
3498 | if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec, | |
3499 | &value)) | |
3500 | goto error_free_vers; | |
3501 | ||
3502 | /* The hook function sets the name to NULL if this symbol | |
3503 | should be skipped for some reason. */ | |
3504 | if (name == NULL) | |
3505 | continue; | |
3506 | } | |
3507 | ||
3508 | /* Sanity check that all possibilities were handled. */ | |
3509 | if (sec == NULL) | |
3510 | { | |
3511 | bfd_set_error (bfd_error_bad_value); | |
3512 | goto error_free_vers; | |
3513 | } | |
3514 | ||
3515 | if (bfd_is_und_section (sec) | |
3516 | || bfd_is_com_section (sec)) | |
3517 | definition = FALSE; | |
3518 | else | |
3519 | definition = TRUE; | |
3520 | ||
3521 | size_change_ok = FALSE; | |
3522 | type_change_ok = get_elf_backend_data (abfd)->type_change_ok; | |
3523 | old_alignment = 0; | |
3524 | old_bfd = NULL; | |
3525 | ||
3526 | if (is_elf_hash_table (hash_table)) | |
3527 | { | |
3528 | Elf_Internal_Versym iver; | |
3529 | unsigned int vernum = 0; | |
3530 | bfd_boolean skip; | |
3531 | ||
fc0e6df6 | 3532 | if (ever == NULL) |
4ad4eba5 | 3533 | { |
fc0e6df6 PB |
3534 | if (info->default_imported_symver) |
3535 | /* Use the default symbol version created earlier. */ | |
3536 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
3537 | else | |
3538 | iver.vs_vers = 0; | |
3539 | } | |
3540 | else | |
3541 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
3542 | ||
3543 | vernum = iver.vs_vers & VERSYM_VERSION; | |
3544 | ||
3545 | /* If this is a hidden symbol, or if it is not version | |
3546 | 1, we append the version name to the symbol name. | |
3547 | However, we do not modify a non-hidden absolute | |
3548 | symbol, because it might be the version symbol | |
3549 | itself. FIXME: What if it isn't? */ | |
3550 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 | |
3551 | || (vernum > 1 && ! bfd_is_abs_section (sec))) | |
3552 | { | |
3553 | const char *verstr; | |
3554 | size_t namelen, verlen, newlen; | |
3555 | char *newname, *p; | |
3556 | ||
3557 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 3558 | { |
fc0e6df6 PB |
3559 | if (vernum > elf_tdata (abfd)->cverdefs) |
3560 | verstr = NULL; | |
3561 | else if (vernum > 1) | |
3562 | verstr = | |
3563 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
3564 | else | |
3565 | verstr = ""; | |
4ad4eba5 | 3566 | |
fc0e6df6 | 3567 | if (verstr == NULL) |
4ad4eba5 | 3568 | { |
fc0e6df6 PB |
3569 | (*_bfd_error_handler) |
3570 | (_("%B: %s: invalid version %u (max %d)"), | |
3571 | abfd, name, vernum, | |
3572 | elf_tdata (abfd)->cverdefs); | |
3573 | bfd_set_error (bfd_error_bad_value); | |
3574 | goto error_free_vers; | |
4ad4eba5 | 3575 | } |
fc0e6df6 PB |
3576 | } |
3577 | else | |
3578 | { | |
3579 | /* We cannot simply test for the number of | |
3580 | entries in the VERNEED section since the | |
3581 | numbers for the needed versions do not start | |
3582 | at 0. */ | |
3583 | Elf_Internal_Verneed *t; | |
3584 | ||
3585 | verstr = NULL; | |
3586 | for (t = elf_tdata (abfd)->verref; | |
3587 | t != NULL; | |
3588 | t = t->vn_nextref) | |
4ad4eba5 | 3589 | { |
fc0e6df6 | 3590 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 3591 | |
fc0e6df6 PB |
3592 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
3593 | { | |
3594 | if (a->vna_other == vernum) | |
4ad4eba5 | 3595 | { |
fc0e6df6 PB |
3596 | verstr = a->vna_nodename; |
3597 | break; | |
4ad4eba5 | 3598 | } |
4ad4eba5 | 3599 | } |
fc0e6df6 PB |
3600 | if (a != NULL) |
3601 | break; | |
3602 | } | |
3603 | if (verstr == NULL) | |
3604 | { | |
3605 | (*_bfd_error_handler) | |
3606 | (_("%B: %s: invalid needed version %d"), | |
3607 | abfd, name, vernum); | |
3608 | bfd_set_error (bfd_error_bad_value); | |
3609 | goto error_free_vers; | |
4ad4eba5 | 3610 | } |
4ad4eba5 | 3611 | } |
fc0e6df6 PB |
3612 | |
3613 | namelen = strlen (name); | |
3614 | verlen = strlen (verstr); | |
3615 | newlen = namelen + verlen + 2; | |
3616 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3617 | && isym->st_shndx != SHN_UNDEF) | |
3618 | ++newlen; | |
3619 | ||
3620 | newname = bfd_alloc (abfd, newlen); | |
3621 | if (newname == NULL) | |
3622 | goto error_free_vers; | |
3623 | memcpy (newname, name, namelen); | |
3624 | p = newname + namelen; | |
3625 | *p++ = ELF_VER_CHR; | |
3626 | /* If this is a defined non-hidden version symbol, | |
3627 | we add another @ to the name. This indicates the | |
3628 | default version of the symbol. */ | |
3629 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3630 | && isym->st_shndx != SHN_UNDEF) | |
3631 | *p++ = ELF_VER_CHR; | |
3632 | memcpy (p, verstr, verlen + 1); | |
3633 | ||
3634 | name = newname; | |
4ad4eba5 AM |
3635 | } |
3636 | ||
3637 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, | |
3638 | sym_hash, &skip, &override, | |
3639 | &type_change_ok, &size_change_ok)) | |
3640 | goto error_free_vers; | |
3641 | ||
3642 | if (skip) | |
3643 | continue; | |
3644 | ||
3645 | if (override) | |
3646 | definition = FALSE; | |
3647 | ||
3648 | h = *sym_hash; | |
3649 | while (h->root.type == bfd_link_hash_indirect | |
3650 | || h->root.type == bfd_link_hash_warning) | |
3651 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3652 | ||
3653 | /* Remember the old alignment if this is a common symbol, so | |
3654 | that we don't reduce the alignment later on. We can't | |
3655 | check later, because _bfd_generic_link_add_one_symbol | |
3656 | will set a default for the alignment which we want to | |
3657 | override. We also remember the old bfd where the existing | |
3658 | definition comes from. */ | |
3659 | switch (h->root.type) | |
3660 | { | |
3661 | default: | |
3662 | break; | |
3663 | ||
3664 | case bfd_link_hash_defined: | |
3665 | case bfd_link_hash_defweak: | |
3666 | old_bfd = h->root.u.def.section->owner; | |
3667 | break; | |
3668 | ||
3669 | case bfd_link_hash_common: | |
3670 | old_bfd = h->root.u.c.p->section->owner; | |
3671 | old_alignment = h->root.u.c.p->alignment_power; | |
3672 | break; | |
3673 | } | |
3674 | ||
3675 | if (elf_tdata (abfd)->verdef != NULL | |
3676 | && ! override | |
3677 | && vernum > 1 | |
3678 | && definition) | |
3679 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
3680 | } | |
3681 | ||
3682 | if (! (_bfd_generic_link_add_one_symbol | |
3683 | (info, abfd, name, flags, sec, value, NULL, FALSE, collect, | |
3684 | (struct bfd_link_hash_entry **) sym_hash))) | |
3685 | goto error_free_vers; | |
3686 | ||
3687 | h = *sym_hash; | |
3688 | while (h->root.type == bfd_link_hash_indirect | |
3689 | || h->root.type == bfd_link_hash_warning) | |
3690 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3691 | *sym_hash = h; | |
3692 | ||
3693 | new_weakdef = FALSE; | |
3694 | if (dynamic | |
3695 | && definition | |
3696 | && (flags & BSF_WEAK) != 0 | |
3697 | && ELF_ST_TYPE (isym->st_info) != STT_FUNC | |
3698 | && is_elf_hash_table (hash_table) | |
f6e332e6 | 3699 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
3700 | { |
3701 | /* Keep a list of all weak defined non function symbols from | |
3702 | a dynamic object, using the weakdef field. Later in this | |
3703 | function we will set the weakdef field to the correct | |
3704 | value. We only put non-function symbols from dynamic | |
3705 | objects on this list, because that happens to be the only | |
3706 | time we need to know the normal symbol corresponding to a | |
3707 | weak symbol, and the information is time consuming to | |
3708 | figure out. If the weakdef field is not already NULL, | |
3709 | then this symbol was already defined by some previous | |
3710 | dynamic object, and we will be using that previous | |
3711 | definition anyhow. */ | |
3712 | ||
f6e332e6 | 3713 | h->u.weakdef = weaks; |
4ad4eba5 AM |
3714 | weaks = h; |
3715 | new_weakdef = TRUE; | |
3716 | } | |
3717 | ||
3718 | /* Set the alignment of a common symbol. */ | |
3719 | if (isym->st_shndx == SHN_COMMON | |
3720 | && h->root.type == bfd_link_hash_common) | |
3721 | { | |
3722 | unsigned int align; | |
3723 | ||
3724 | align = bfd_log2 (isym->st_value); | |
3725 | if (align > old_alignment | |
3726 | /* Permit an alignment power of zero if an alignment of one | |
3727 | is specified and no other alignments have been specified. */ | |
3728 | || (isym->st_value == 1 && old_alignment == 0)) | |
3729 | h->root.u.c.p->alignment_power = align; | |
3730 | else | |
3731 | h->root.u.c.p->alignment_power = old_alignment; | |
3732 | } | |
3733 | ||
3734 | if (is_elf_hash_table (hash_table)) | |
3735 | { | |
4ad4eba5 | 3736 | bfd_boolean dynsym; |
4ad4eba5 AM |
3737 | |
3738 | /* Check the alignment when a common symbol is involved. This | |
3739 | can change when a common symbol is overridden by a normal | |
3740 | definition or a common symbol is ignored due to the old | |
3741 | normal definition. We need to make sure the maximum | |
3742 | alignment is maintained. */ | |
3743 | if ((old_alignment || isym->st_shndx == SHN_COMMON) | |
3744 | && h->root.type != bfd_link_hash_common) | |
3745 | { | |
3746 | unsigned int common_align; | |
3747 | unsigned int normal_align; | |
3748 | unsigned int symbol_align; | |
3749 | bfd *normal_bfd; | |
3750 | bfd *common_bfd; | |
3751 | ||
3752 | symbol_align = ffs (h->root.u.def.value) - 1; | |
3753 | if (h->root.u.def.section->owner != NULL | |
3754 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
3755 | { | |
3756 | normal_align = h->root.u.def.section->alignment_power; | |
3757 | if (normal_align > symbol_align) | |
3758 | normal_align = symbol_align; | |
3759 | } | |
3760 | else | |
3761 | normal_align = symbol_align; | |
3762 | ||
3763 | if (old_alignment) | |
3764 | { | |
3765 | common_align = old_alignment; | |
3766 | common_bfd = old_bfd; | |
3767 | normal_bfd = abfd; | |
3768 | } | |
3769 | else | |
3770 | { | |
3771 | common_align = bfd_log2 (isym->st_value); | |
3772 | common_bfd = abfd; | |
3773 | normal_bfd = old_bfd; | |
3774 | } | |
3775 | ||
3776 | if (normal_align < common_align) | |
3777 | (*_bfd_error_handler) | |
d003868e AM |
3778 | (_("Warning: alignment %u of symbol `%s' in %B" |
3779 | " is smaller than %u in %B"), | |
3780 | normal_bfd, common_bfd, | |
3781 | 1 << normal_align, name, 1 << common_align); | |
4ad4eba5 AM |
3782 | } |
3783 | ||
3784 | /* Remember the symbol size and type. */ | |
3785 | if (isym->st_size != 0 | |
3786 | && (definition || h->size == 0)) | |
3787 | { | |
3788 | if (h->size != 0 && h->size != isym->st_size && ! size_change_ok) | |
3789 | (*_bfd_error_handler) | |
d003868e AM |
3790 | (_("Warning: size of symbol `%s' changed" |
3791 | " from %lu in %B to %lu in %B"), | |
3792 | old_bfd, abfd, | |
4ad4eba5 | 3793 | name, (unsigned long) h->size, |
d003868e | 3794 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
3795 | |
3796 | h->size = isym->st_size; | |
3797 | } | |
3798 | ||
3799 | /* If this is a common symbol, then we always want H->SIZE | |
3800 | to be the size of the common symbol. The code just above | |
3801 | won't fix the size if a common symbol becomes larger. We | |
3802 | don't warn about a size change here, because that is | |
3803 | covered by --warn-common. */ | |
3804 | if (h->root.type == bfd_link_hash_common) | |
3805 | h->size = h->root.u.c.size; | |
3806 | ||
3807 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
3808 | && (definition || h->type == STT_NOTYPE)) | |
3809 | { | |
3810 | if (h->type != STT_NOTYPE | |
3811 | && h->type != ELF_ST_TYPE (isym->st_info) | |
3812 | && ! type_change_ok) | |
3813 | (*_bfd_error_handler) | |
d003868e AM |
3814 | (_("Warning: type of symbol `%s' changed" |
3815 | " from %d to %d in %B"), | |
3816 | abfd, name, h->type, ELF_ST_TYPE (isym->st_info)); | |
4ad4eba5 AM |
3817 | |
3818 | h->type = ELF_ST_TYPE (isym->st_info); | |
3819 | } | |
3820 | ||
3821 | /* If st_other has a processor-specific meaning, specific | |
3822 | code might be needed here. We never merge the visibility | |
3823 | attribute with the one from a dynamic object. */ | |
3824 | if (bed->elf_backend_merge_symbol_attribute) | |
3825 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
3826 | dynamic); | |
3827 | ||
b58f81ae DJ |
3828 | /* If this symbol has default visibility and the user has requested |
3829 | we not re-export it, then mark it as hidden. */ | |
3830 | if (definition && !dynamic | |
3831 | && (abfd->no_export | |
3832 | || (abfd->my_archive && abfd->my_archive->no_export)) | |
3833 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) | |
3834 | isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1)); | |
3835 | ||
4ad4eba5 AM |
3836 | if (isym->st_other != 0 && !dynamic) |
3837 | { | |
3838 | unsigned char hvis, symvis, other, nvis; | |
3839 | ||
3840 | /* Take the balance of OTHER from the definition. */ | |
3841 | other = (definition ? isym->st_other : h->other); | |
3842 | other &= ~ ELF_ST_VISIBILITY (-1); | |
3843 | ||
3844 | /* Combine visibilities, using the most constraining one. */ | |
3845 | hvis = ELF_ST_VISIBILITY (h->other); | |
3846 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
3847 | if (! hvis) | |
3848 | nvis = symvis; | |
3849 | else if (! symvis) | |
3850 | nvis = hvis; | |
3851 | else | |
3852 | nvis = hvis < symvis ? hvis : symvis; | |
3853 | ||
3854 | h->other = other | nvis; | |
3855 | } | |
3856 | ||
3857 | /* Set a flag in the hash table entry indicating the type of | |
3858 | reference or definition we just found. Keep a count of | |
3859 | the number of dynamic symbols we find. A dynamic symbol | |
3860 | is one which is referenced or defined by both a regular | |
3861 | object and a shared object. */ | |
4ad4eba5 AM |
3862 | dynsym = FALSE; |
3863 | if (! dynamic) | |
3864 | { | |
3865 | if (! definition) | |
3866 | { | |
f5385ebf | 3867 | h->ref_regular = 1; |
4ad4eba5 | 3868 | if (bind != STB_WEAK) |
f5385ebf | 3869 | h->ref_regular_nonweak = 1; |
4ad4eba5 AM |
3870 | } |
3871 | else | |
f5385ebf | 3872 | h->def_regular = 1; |
4ad4eba5 | 3873 | if (! info->executable |
f5385ebf AM |
3874 | || h->def_dynamic |
3875 | || h->ref_dynamic) | |
4ad4eba5 AM |
3876 | dynsym = TRUE; |
3877 | } | |
3878 | else | |
3879 | { | |
3880 | if (! definition) | |
f5385ebf | 3881 | h->ref_dynamic = 1; |
4ad4eba5 | 3882 | else |
f5385ebf AM |
3883 | h->def_dynamic = 1; |
3884 | if (h->def_regular | |
3885 | || h->ref_regular | |
f6e332e6 | 3886 | || (h->u.weakdef != NULL |
4ad4eba5 | 3887 | && ! new_weakdef |
f6e332e6 | 3888 | && h->u.weakdef->dynindx != -1)) |
4ad4eba5 AM |
3889 | dynsym = TRUE; |
3890 | } | |
3891 | ||
4ad4eba5 AM |
3892 | /* Check to see if we need to add an indirect symbol for |
3893 | the default name. */ | |
3894 | if (definition || h->root.type == bfd_link_hash_common) | |
3895 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
3896 | &sec, &value, &dynsym, | |
3897 | override)) | |
3898 | goto error_free_vers; | |
3899 | ||
3900 | if (definition && !dynamic) | |
3901 | { | |
3902 | char *p = strchr (name, ELF_VER_CHR); | |
3903 | if (p != NULL && p[1] != ELF_VER_CHR) | |
3904 | { | |
3905 | /* Queue non-default versions so that .symver x, x@FOO | |
3906 | aliases can be checked. */ | |
3907 | if (! nondeflt_vers) | |
3908 | { | |
3909 | amt = (isymend - isym + 1) | |
3910 | * sizeof (struct elf_link_hash_entry *); | |
3911 | nondeflt_vers = bfd_malloc (amt); | |
3912 | } | |
3913 | nondeflt_vers [nondeflt_vers_cnt++] = h; | |
3914 | } | |
3915 | } | |
3916 | ||
3917 | if (dynsym && h->dynindx == -1) | |
3918 | { | |
c152c796 | 3919 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 3920 | goto error_free_vers; |
f6e332e6 | 3921 | if (h->u.weakdef != NULL |
4ad4eba5 | 3922 | && ! new_weakdef |
f6e332e6 | 3923 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 3924 | { |
f6e332e6 | 3925 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
3926 | goto error_free_vers; |
3927 | } | |
3928 | } | |
3929 | else if (dynsym && h->dynindx != -1) | |
3930 | /* If the symbol already has a dynamic index, but | |
3931 | visibility says it should not be visible, turn it into | |
3932 | a local symbol. */ | |
3933 | switch (ELF_ST_VISIBILITY (h->other)) | |
3934 | { | |
3935 | case STV_INTERNAL: | |
3936 | case STV_HIDDEN: | |
3937 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
3938 | dynsym = FALSE; | |
3939 | break; | |
3940 | } | |
3941 | ||
3942 | if (!add_needed | |
3943 | && definition | |
3944 | && dynsym | |
f5385ebf | 3945 | && h->ref_regular) |
4ad4eba5 AM |
3946 | { |
3947 | int ret; | |
3948 | const char *soname = elf_dt_name (abfd); | |
3949 | ||
3950 | /* A symbol from a library loaded via DT_NEEDED of some | |
3951 | other library is referenced by a regular object. | |
e56f61be L |
3952 | Add a DT_NEEDED entry for it. Issue an error if |
3953 | --no-add-needed is used. */ | |
3954 | if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) | |
3955 | { | |
3956 | (*_bfd_error_handler) | |
3957 | (_("%s: invalid DSO for symbol `%s' definition"), | |
d003868e | 3958 | abfd, name); |
e56f61be L |
3959 | bfd_set_error (bfd_error_bad_value); |
3960 | goto error_free_vers; | |
3961 | } | |
3962 | ||
a5db907e AM |
3963 | elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED; |
3964 | ||
4ad4eba5 | 3965 | add_needed = TRUE; |
7e9f0867 | 3966 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3967 | if (ret < 0) |
3968 | goto error_free_vers; | |
3969 | ||
3970 | BFD_ASSERT (ret == 0); | |
3971 | } | |
3972 | } | |
3973 | } | |
3974 | ||
3975 | /* Now that all the symbols from this input file are created, handle | |
3976 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
3977 | if (nondeflt_vers != NULL) | |
3978 | { | |
3979 | bfd_size_type cnt, symidx; | |
3980 | ||
3981 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
3982 | { | |
3983 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
3984 | char *shortname, *p; | |
3985 | ||
3986 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
3987 | if (p == NULL | |
3988 | || (h->root.type != bfd_link_hash_defined | |
3989 | && h->root.type != bfd_link_hash_defweak)) | |
3990 | continue; | |
3991 | ||
3992 | amt = p - h->root.root.string; | |
3993 | shortname = bfd_malloc (amt + 1); | |
3994 | memcpy (shortname, h->root.root.string, amt); | |
3995 | shortname[amt] = '\0'; | |
3996 | ||
3997 | hi = (struct elf_link_hash_entry *) | |
3998 | bfd_link_hash_lookup (&hash_table->root, shortname, | |
3999 | FALSE, FALSE, FALSE); | |
4000 | if (hi != NULL | |
4001 | && hi->root.type == h->root.type | |
4002 | && hi->root.u.def.value == h->root.u.def.value | |
4003 | && hi->root.u.def.section == h->root.u.def.section) | |
4004 | { | |
4005 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4006 | hi->root.type = bfd_link_hash_indirect; | |
4007 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
4008 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); | |
4009 | sym_hash = elf_sym_hashes (abfd); | |
4010 | if (sym_hash) | |
4011 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4012 | if (sym_hash[symidx] == hi) | |
4013 | { | |
4014 | sym_hash[symidx] = h; | |
4015 | break; | |
4016 | } | |
4017 | } | |
4018 | free (shortname); | |
4019 | } | |
4020 | free (nondeflt_vers); | |
4021 | nondeflt_vers = NULL; | |
4022 | } | |
4023 | ||
4024 | if (extversym != NULL) | |
4025 | { | |
4026 | free (extversym); | |
4027 | extversym = NULL; | |
4028 | } | |
4029 | ||
4030 | if (isymbuf != NULL) | |
4031 | free (isymbuf); | |
4032 | isymbuf = NULL; | |
4033 | ||
4034 | /* Now set the weakdefs field correctly for all the weak defined | |
4035 | symbols we found. The only way to do this is to search all the | |
4036 | symbols. Since we only need the information for non functions in | |
4037 | dynamic objects, that's the only time we actually put anything on | |
4038 | the list WEAKS. We need this information so that if a regular | |
4039 | object refers to a symbol defined weakly in a dynamic object, the | |
4040 | real symbol in the dynamic object is also put in the dynamic | |
4041 | symbols; we also must arrange for both symbols to point to the | |
4042 | same memory location. We could handle the general case of symbol | |
4043 | aliasing, but a general symbol alias can only be generated in | |
4044 | assembler code, handling it correctly would be very time | |
4045 | consuming, and other ELF linkers don't handle general aliasing | |
4046 | either. */ | |
4047 | if (weaks != NULL) | |
4048 | { | |
4049 | struct elf_link_hash_entry **hpp; | |
4050 | struct elf_link_hash_entry **hppend; | |
4051 | struct elf_link_hash_entry **sorted_sym_hash; | |
4052 | struct elf_link_hash_entry *h; | |
4053 | size_t sym_count; | |
4054 | ||
4055 | /* Since we have to search the whole symbol list for each weak | |
4056 | defined symbol, search time for N weak defined symbols will be | |
4057 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
4058 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
4059 | sorted_sym_hash = bfd_malloc (amt); | |
4060 | if (sorted_sym_hash == NULL) | |
4061 | goto error_return; | |
4062 | sym_hash = sorted_sym_hash; | |
4063 | hpp = elf_sym_hashes (abfd); | |
4064 | hppend = hpp + extsymcount; | |
4065 | sym_count = 0; | |
4066 | for (; hpp < hppend; hpp++) | |
4067 | { | |
4068 | h = *hpp; | |
4069 | if (h != NULL | |
4070 | && h->root.type == bfd_link_hash_defined | |
4071 | && h->type != STT_FUNC) | |
4072 | { | |
4073 | *sym_hash = h; | |
4074 | sym_hash++; | |
4075 | sym_count++; | |
4076 | } | |
4077 | } | |
4078 | ||
4079 | qsort (sorted_sym_hash, sym_count, | |
4080 | sizeof (struct elf_link_hash_entry *), | |
4081 | elf_sort_symbol); | |
4082 | ||
4083 | while (weaks != NULL) | |
4084 | { | |
4085 | struct elf_link_hash_entry *hlook; | |
4086 | asection *slook; | |
4087 | bfd_vma vlook; | |
4088 | long ilook; | |
4089 | size_t i, j, idx; | |
4090 | ||
4091 | hlook = weaks; | |
f6e332e6 AM |
4092 | weaks = hlook->u.weakdef; |
4093 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
4094 | |
4095 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
4096 | || hlook->root.type == bfd_link_hash_defweak | |
4097 | || hlook->root.type == bfd_link_hash_common | |
4098 | || hlook->root.type == bfd_link_hash_indirect); | |
4099 | slook = hlook->root.u.def.section; | |
4100 | vlook = hlook->root.u.def.value; | |
4101 | ||
4102 | ilook = -1; | |
4103 | i = 0; | |
4104 | j = sym_count; | |
4105 | while (i < j) | |
4106 | { | |
4107 | bfd_signed_vma vdiff; | |
4108 | idx = (i + j) / 2; | |
4109 | h = sorted_sym_hash [idx]; | |
4110 | vdiff = vlook - h->root.u.def.value; | |
4111 | if (vdiff < 0) | |
4112 | j = idx; | |
4113 | else if (vdiff > 0) | |
4114 | i = idx + 1; | |
4115 | else | |
4116 | { | |
a9b881be | 4117 | long sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
4118 | if (sdiff < 0) |
4119 | j = idx; | |
4120 | else if (sdiff > 0) | |
4121 | i = idx + 1; | |
4122 | else | |
4123 | { | |
4124 | ilook = idx; | |
4125 | break; | |
4126 | } | |
4127 | } | |
4128 | } | |
4129 | ||
4130 | /* We didn't find a value/section match. */ | |
4131 | if (ilook == -1) | |
4132 | continue; | |
4133 | ||
4134 | for (i = ilook; i < sym_count; i++) | |
4135 | { | |
4136 | h = sorted_sym_hash [i]; | |
4137 | ||
4138 | /* Stop if value or section doesn't match. */ | |
4139 | if (h->root.u.def.value != vlook | |
4140 | || h->root.u.def.section != slook) | |
4141 | break; | |
4142 | else if (h != hlook) | |
4143 | { | |
f6e332e6 | 4144 | hlook->u.weakdef = h; |
4ad4eba5 AM |
4145 | |
4146 | /* If the weak definition is in the list of dynamic | |
4147 | symbols, make sure the real definition is put | |
4148 | there as well. */ | |
4149 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
4150 | { | |
c152c796 | 4151 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 AM |
4152 | goto error_return; |
4153 | } | |
4154 | ||
4155 | /* If the real definition is in the list of dynamic | |
4156 | symbols, make sure the weak definition is put | |
4157 | there as well. If we don't do this, then the | |
4158 | dynamic loader might not merge the entries for the | |
4159 | real definition and the weak definition. */ | |
4160 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
4161 | { | |
c152c796 | 4162 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4ad4eba5 AM |
4163 | goto error_return; |
4164 | } | |
4165 | break; | |
4166 | } | |
4167 | } | |
4168 | } | |
4169 | ||
4170 | free (sorted_sym_hash); | |
4171 | } | |
4172 | ||
85fbca6a NC |
4173 | check_directives = get_elf_backend_data (abfd)->check_directives; |
4174 | if (check_directives) | |
4175 | check_directives (abfd, info); | |
4176 | ||
4ad4eba5 AM |
4177 | /* If this object is the same format as the output object, and it is |
4178 | not a shared library, then let the backend look through the | |
4179 | relocs. | |
4180 | ||
4181 | This is required to build global offset table entries and to | |
4182 | arrange for dynamic relocs. It is not required for the | |
4183 | particular common case of linking non PIC code, even when linking | |
4184 | against shared libraries, but unfortunately there is no way of | |
4185 | knowing whether an object file has been compiled PIC or not. | |
4186 | Looking through the relocs is not particularly time consuming. | |
4187 | The problem is that we must either (1) keep the relocs in memory, | |
4188 | which causes the linker to require additional runtime memory or | |
4189 | (2) read the relocs twice from the input file, which wastes time. | |
4190 | This would be a good case for using mmap. | |
4191 | ||
4192 | I have no idea how to handle linking PIC code into a file of a | |
4193 | different format. It probably can't be done. */ | |
4194 | check_relocs = get_elf_backend_data (abfd)->check_relocs; | |
4195 | if (! dynamic | |
4196 | && is_elf_hash_table (hash_table) | |
4197 | && hash_table->root.creator == abfd->xvec | |
4198 | && check_relocs != NULL) | |
4199 | { | |
4200 | asection *o; | |
4201 | ||
4202 | for (o = abfd->sections; o != NULL; o = o->next) | |
4203 | { | |
4204 | Elf_Internal_Rela *internal_relocs; | |
4205 | bfd_boolean ok; | |
4206 | ||
4207 | if ((o->flags & SEC_RELOC) == 0 | |
4208 | || o->reloc_count == 0 | |
4209 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4210 | && (o->flags & SEC_DEBUGGING) != 0) | |
4211 | || bfd_is_abs_section (o->output_section)) | |
4212 | continue; | |
4213 | ||
4214 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4215 | info->keep_memory); | |
4216 | if (internal_relocs == NULL) | |
4217 | goto error_return; | |
4218 | ||
4219 | ok = (*check_relocs) (abfd, info, o, internal_relocs); | |
4220 | ||
4221 | if (elf_section_data (o)->relocs != internal_relocs) | |
4222 | free (internal_relocs); | |
4223 | ||
4224 | if (! ok) | |
4225 | goto error_return; | |
4226 | } | |
4227 | } | |
4228 | ||
4229 | /* If this is a non-traditional link, try to optimize the handling | |
4230 | of the .stab/.stabstr sections. */ | |
4231 | if (! dynamic | |
4232 | && ! info->traditional_format | |
4233 | && is_elf_hash_table (hash_table) | |
4234 | && (info->strip != strip_all && info->strip != strip_debugger)) | |
4235 | { | |
4236 | asection *stabstr; | |
4237 | ||
4238 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4239 | if (stabstr != NULL) | |
4240 | { | |
4241 | bfd_size_type string_offset = 0; | |
4242 | asection *stab; | |
4243 | ||
4244 | for (stab = abfd->sections; stab; stab = stab->next) | |
4245 | if (strncmp (".stab", stab->name, 5) == 0 | |
4246 | && (!stab->name[5] || | |
4247 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4248 | && (stab->flags & SEC_MERGE) == 0 | |
4249 | && !bfd_is_abs_section (stab->output_section)) | |
4250 | { | |
4251 | struct bfd_elf_section_data *secdata; | |
4252 | ||
4253 | secdata = elf_section_data (stab); | |
4254 | if (! _bfd_link_section_stabs (abfd, | |
3722b82f | 4255 | &hash_table->stab_info, |
4ad4eba5 AM |
4256 | stab, stabstr, |
4257 | &secdata->sec_info, | |
4258 | &string_offset)) | |
4259 | goto error_return; | |
4260 | if (secdata->sec_info) | |
4261 | stab->sec_info_type = ELF_INFO_TYPE_STABS; | |
4262 | } | |
4263 | } | |
4264 | } | |
4265 | ||
4ad4eba5 AM |
4266 | if (is_elf_hash_table (hash_table)) |
4267 | { | |
4268 | /* Add this bfd to the loaded list. */ | |
4269 | struct elf_link_loaded_list *n; | |
4270 | ||
4271 | n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); | |
4272 | if (n == NULL) | |
4273 | goto error_return; | |
4274 | n->abfd = abfd; | |
4275 | n->next = hash_table->loaded; | |
4276 | hash_table->loaded = n; | |
4277 | } | |
4278 | ||
4279 | return TRUE; | |
4280 | ||
4281 | error_free_vers: | |
4282 | if (nondeflt_vers != NULL) | |
4283 | free (nondeflt_vers); | |
4284 | if (extversym != NULL) | |
4285 | free (extversym); | |
4286 | error_free_sym: | |
4287 | if (isymbuf != NULL) | |
4288 | free (isymbuf); | |
4289 | error_return: | |
4290 | return FALSE; | |
4291 | } | |
4292 | ||
8387904d AM |
4293 | /* Return the linker hash table entry of a symbol that might be |
4294 | satisfied by an archive symbol. Return -1 on error. */ | |
4295 | ||
4296 | struct elf_link_hash_entry * | |
4297 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
4298 | struct bfd_link_info *info, | |
4299 | const char *name) | |
4300 | { | |
4301 | struct elf_link_hash_entry *h; | |
4302 | char *p, *copy; | |
4303 | size_t len, first; | |
4304 | ||
4305 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
4306 | if (h != NULL) | |
4307 | return h; | |
4308 | ||
4309 | /* If this is a default version (the name contains @@), look up the | |
4310 | symbol again with only one `@' as well as without the version. | |
4311 | The effect is that references to the symbol with and without the | |
4312 | version will be matched by the default symbol in the archive. */ | |
4313 | ||
4314 | p = strchr (name, ELF_VER_CHR); | |
4315 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4316 | return h; | |
4317 | ||
4318 | /* First check with only one `@'. */ | |
4319 | len = strlen (name); | |
4320 | copy = bfd_alloc (abfd, len); | |
4321 | if (copy == NULL) | |
4322 | return (struct elf_link_hash_entry *) 0 - 1; | |
4323 | ||
4324 | first = p - name + 1; | |
4325 | memcpy (copy, name, first); | |
4326 | memcpy (copy + first, name + first + 1, len - first); | |
4327 | ||
4328 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE); | |
4329 | if (h == NULL) | |
4330 | { | |
4331 | /* We also need to check references to the symbol without the | |
4332 | version. */ | |
4333 | copy[first - 1] = '\0'; | |
4334 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
4335 | FALSE, FALSE, FALSE); | |
4336 | } | |
4337 | ||
4338 | bfd_release (abfd, copy); | |
4339 | return h; | |
4340 | } | |
4341 | ||
0ad989f9 L |
4342 | /* Add symbols from an ELF archive file to the linker hash table. We |
4343 | don't use _bfd_generic_link_add_archive_symbols because of a | |
4344 | problem which arises on UnixWare. The UnixWare libc.so is an | |
4345 | archive which includes an entry libc.so.1 which defines a bunch of | |
4346 | symbols. The libc.so archive also includes a number of other | |
4347 | object files, which also define symbols, some of which are the same | |
4348 | as those defined in libc.so.1. Correct linking requires that we | |
4349 | consider each object file in turn, and include it if it defines any | |
4350 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
4351 | this; it looks through the list of undefined symbols, and includes | |
4352 | any object file which defines them. When this algorithm is used on | |
4353 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
4354 | bunch of symbols. This means that some of the other objects in the | |
4355 | archive are not included in the link, which is incorrect since they | |
4356 | precede libc.so.1 in the archive. | |
4357 | ||
4358 | Fortunately, ELF archive handling is simpler than that done by | |
4359 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4360 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4361 | symbol is currently undefined, we know that we must pull in that | |
4362 | object file. | |
4363 | ||
4364 | Unfortunately, we do have to make multiple passes over the symbol | |
4365 | table until nothing further is resolved. */ | |
4366 | ||
4ad4eba5 AM |
4367 | static bfd_boolean |
4368 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
4369 | { |
4370 | symindex c; | |
4371 | bfd_boolean *defined = NULL; | |
4372 | bfd_boolean *included = NULL; | |
4373 | carsym *symdefs; | |
4374 | bfd_boolean loop; | |
4375 | bfd_size_type amt; | |
8387904d AM |
4376 | const struct elf_backend_data *bed; |
4377 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
4378 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
4379 | |
4380 | if (! bfd_has_map (abfd)) | |
4381 | { | |
4382 | /* An empty archive is a special case. */ | |
4383 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
4384 | return TRUE; | |
4385 | bfd_set_error (bfd_error_no_armap); | |
4386 | return FALSE; | |
4387 | } | |
4388 | ||
4389 | /* Keep track of all symbols we know to be already defined, and all | |
4390 | files we know to be already included. This is to speed up the | |
4391 | second and subsequent passes. */ | |
4392 | c = bfd_ardata (abfd)->symdef_count; | |
4393 | if (c == 0) | |
4394 | return TRUE; | |
4395 | amt = c; | |
4396 | amt *= sizeof (bfd_boolean); | |
4397 | defined = bfd_zmalloc (amt); | |
4398 | included = bfd_zmalloc (amt); | |
4399 | if (defined == NULL || included == NULL) | |
4400 | goto error_return; | |
4401 | ||
4402 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
4403 | bed = get_elf_backend_data (abfd); |
4404 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
4405 | |
4406 | do | |
4407 | { | |
4408 | file_ptr last; | |
4409 | symindex i; | |
4410 | carsym *symdef; | |
4411 | carsym *symdefend; | |
4412 | ||
4413 | loop = FALSE; | |
4414 | last = -1; | |
4415 | ||
4416 | symdef = symdefs; | |
4417 | symdefend = symdef + c; | |
4418 | for (i = 0; symdef < symdefend; symdef++, i++) | |
4419 | { | |
4420 | struct elf_link_hash_entry *h; | |
4421 | bfd *element; | |
4422 | struct bfd_link_hash_entry *undefs_tail; | |
4423 | symindex mark; | |
4424 | ||
4425 | if (defined[i] || included[i]) | |
4426 | continue; | |
4427 | if (symdef->file_offset == last) | |
4428 | { | |
4429 | included[i] = TRUE; | |
4430 | continue; | |
4431 | } | |
4432 | ||
8387904d AM |
4433 | h = archive_symbol_lookup (abfd, info, symdef->name); |
4434 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
4435 | goto error_return; | |
0ad989f9 L |
4436 | |
4437 | if (h == NULL) | |
4438 | continue; | |
4439 | ||
4440 | if (h->root.type == bfd_link_hash_common) | |
4441 | { | |
4442 | /* We currently have a common symbol. The archive map contains | |
4443 | a reference to this symbol, so we may want to include it. We | |
4444 | only want to include it however, if this archive element | |
4445 | contains a definition of the symbol, not just another common | |
4446 | declaration of it. | |
4447 | ||
4448 | Unfortunately some archivers (including GNU ar) will put | |
4449 | declarations of common symbols into their archive maps, as | |
4450 | well as real definitions, so we cannot just go by the archive | |
4451 | map alone. Instead we must read in the element's symbol | |
4452 | table and check that to see what kind of symbol definition | |
4453 | this is. */ | |
4454 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
4455 | continue; | |
4456 | } | |
4457 | else if (h->root.type != bfd_link_hash_undefined) | |
4458 | { | |
4459 | if (h->root.type != bfd_link_hash_undefweak) | |
4460 | defined[i] = TRUE; | |
4461 | continue; | |
4462 | } | |
4463 | ||
4464 | /* We need to include this archive member. */ | |
4465 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
4466 | if (element == NULL) | |
4467 | goto error_return; | |
4468 | ||
4469 | if (! bfd_check_format (element, bfd_object)) | |
4470 | goto error_return; | |
4471 | ||
4472 | /* Doublecheck that we have not included this object | |
4473 | already--it should be impossible, but there may be | |
4474 | something wrong with the archive. */ | |
4475 | if (element->archive_pass != 0) | |
4476 | { | |
4477 | bfd_set_error (bfd_error_bad_value); | |
4478 | goto error_return; | |
4479 | } | |
4480 | element->archive_pass = 1; | |
4481 | ||
4482 | undefs_tail = info->hash->undefs_tail; | |
4483 | ||
4484 | if (! (*info->callbacks->add_archive_element) (info, element, | |
4485 | symdef->name)) | |
4486 | goto error_return; | |
4487 | if (! bfd_link_add_symbols (element, info)) | |
4488 | goto error_return; | |
4489 | ||
4490 | /* If there are any new undefined symbols, we need to make | |
4491 | another pass through the archive in order to see whether | |
4492 | they can be defined. FIXME: This isn't perfect, because | |
4493 | common symbols wind up on undefs_tail and because an | |
4494 | undefined symbol which is defined later on in this pass | |
4495 | does not require another pass. This isn't a bug, but it | |
4496 | does make the code less efficient than it could be. */ | |
4497 | if (undefs_tail != info->hash->undefs_tail) | |
4498 | loop = TRUE; | |
4499 | ||
4500 | /* Look backward to mark all symbols from this object file | |
4501 | which we have already seen in this pass. */ | |
4502 | mark = i; | |
4503 | do | |
4504 | { | |
4505 | included[mark] = TRUE; | |
4506 | if (mark == 0) | |
4507 | break; | |
4508 | --mark; | |
4509 | } | |
4510 | while (symdefs[mark].file_offset == symdef->file_offset); | |
4511 | ||
4512 | /* We mark subsequent symbols from this object file as we go | |
4513 | on through the loop. */ | |
4514 | last = symdef->file_offset; | |
4515 | } | |
4516 | } | |
4517 | while (loop); | |
4518 | ||
4519 | free (defined); | |
4520 | free (included); | |
4521 | ||
4522 | return TRUE; | |
4523 | ||
4524 | error_return: | |
4525 | if (defined != NULL) | |
4526 | free (defined); | |
4527 | if (included != NULL) | |
4528 | free (included); | |
4529 | return FALSE; | |
4530 | } | |
4ad4eba5 AM |
4531 | |
4532 | /* Given an ELF BFD, add symbols to the global hash table as | |
4533 | appropriate. */ | |
4534 | ||
4535 | bfd_boolean | |
4536 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
4537 | { | |
4538 | switch (bfd_get_format (abfd)) | |
4539 | { | |
4540 | case bfd_object: | |
4541 | return elf_link_add_object_symbols (abfd, info); | |
4542 | case bfd_archive: | |
4543 | return elf_link_add_archive_symbols (abfd, info); | |
4544 | default: | |
4545 | bfd_set_error (bfd_error_wrong_format); | |
4546 | return FALSE; | |
4547 | } | |
4548 | } | |
5a580b3a AM |
4549 | \f |
4550 | /* This function will be called though elf_link_hash_traverse to store | |
4551 | all hash value of the exported symbols in an array. */ | |
4552 | ||
4553 | static bfd_boolean | |
4554 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
4555 | { | |
4556 | unsigned long **valuep = data; | |
4557 | const char *name; | |
4558 | char *p; | |
4559 | unsigned long ha; | |
4560 | char *alc = NULL; | |
4561 | ||
4562 | if (h->root.type == bfd_link_hash_warning) | |
4563 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4564 | ||
4565 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
4566 | if (h->dynindx == -1) | |
4567 | return TRUE; | |
4568 | ||
4569 | name = h->root.root.string; | |
4570 | p = strchr (name, ELF_VER_CHR); | |
4571 | if (p != NULL) | |
4572 | { | |
4573 | alc = bfd_malloc (p - name + 1); | |
4574 | memcpy (alc, name, p - name); | |
4575 | alc[p - name] = '\0'; | |
4576 | name = alc; | |
4577 | } | |
4578 | ||
4579 | /* Compute the hash value. */ | |
4580 | ha = bfd_elf_hash (name); | |
4581 | ||
4582 | /* Store the found hash value in the array given as the argument. */ | |
4583 | *(*valuep)++ = ha; | |
4584 | ||
4585 | /* And store it in the struct so that we can put it in the hash table | |
4586 | later. */ | |
f6e332e6 | 4587 | h->u.elf_hash_value = ha; |
5a580b3a AM |
4588 | |
4589 | if (alc != NULL) | |
4590 | free (alc); | |
4591 | ||
4592 | return TRUE; | |
4593 | } | |
4594 | ||
4595 | /* Array used to determine the number of hash table buckets to use | |
4596 | based on the number of symbols there are. If there are fewer than | |
4597 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
4598 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
4599 | than 32771 buckets. */ | |
4600 | ||
4601 | static const size_t elf_buckets[] = | |
4602 | { | |
4603 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
4604 | 16411, 32771, 0 | |
4605 | }; | |
4606 | ||
4607 | /* Compute bucket count for hashing table. We do not use a static set | |
4608 | of possible tables sizes anymore. Instead we determine for all | |
4609 | possible reasonable sizes of the table the outcome (i.e., the | |
4610 | number of collisions etc) and choose the best solution. The | |
4611 | weighting functions are not too simple to allow the table to grow | |
4612 | without bounds. Instead one of the weighting factors is the size. | |
4613 | Therefore the result is always a good payoff between few collisions | |
4614 | (= short chain lengths) and table size. */ | |
4615 | static size_t | |
4616 | compute_bucket_count (struct bfd_link_info *info) | |
4617 | { | |
4618 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; | |
4619 | size_t best_size = 0; | |
4620 | unsigned long int *hashcodes; | |
4621 | unsigned long int *hashcodesp; | |
4622 | unsigned long int i; | |
4623 | bfd_size_type amt; | |
4624 | ||
4625 | /* Compute the hash values for all exported symbols. At the same | |
4626 | time store the values in an array so that we could use them for | |
4627 | optimizations. */ | |
4628 | amt = dynsymcount; | |
4629 | amt *= sizeof (unsigned long int); | |
4630 | hashcodes = bfd_malloc (amt); | |
4631 | if (hashcodes == NULL) | |
4632 | return 0; | |
4633 | hashcodesp = hashcodes; | |
4634 | ||
4635 | /* Put all hash values in HASHCODES. */ | |
4636 | elf_link_hash_traverse (elf_hash_table (info), | |
4637 | elf_collect_hash_codes, &hashcodesp); | |
4638 | ||
4639 | /* We have a problem here. The following code to optimize the table | |
4640 | size requires an integer type with more the 32 bits. If | |
4641 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
4642 | #ifdef BFD_HOST_U_64_BIT | |
4643 | if (info->optimize) | |
4644 | { | |
4645 | unsigned long int nsyms = hashcodesp - hashcodes; | |
4646 | size_t minsize; | |
4647 | size_t maxsize; | |
4648 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
4649 | unsigned long int *counts ; | |
4650 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
4651 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); | |
4652 | ||
4653 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
4654 | that the hashing table must at least have NSYMS/4 and at most | |
4655 | 2*NSYMS buckets. */ | |
4656 | minsize = nsyms / 4; | |
4657 | if (minsize == 0) | |
4658 | minsize = 1; | |
4659 | best_size = maxsize = nsyms * 2; | |
4660 | ||
4661 | /* Create array where we count the collisions in. We must use bfd_malloc | |
4662 | since the size could be large. */ | |
4663 | amt = maxsize; | |
4664 | amt *= sizeof (unsigned long int); | |
4665 | counts = bfd_malloc (amt); | |
4666 | if (counts == NULL) | |
4667 | { | |
4668 | free (hashcodes); | |
4669 | return 0; | |
4670 | } | |
4671 | ||
4672 | /* Compute the "optimal" size for the hash table. The criteria is a | |
4673 | minimal chain length. The minor criteria is (of course) the size | |
4674 | of the table. */ | |
4675 | for (i = minsize; i < maxsize; ++i) | |
4676 | { | |
4677 | /* Walk through the array of hashcodes and count the collisions. */ | |
4678 | BFD_HOST_U_64_BIT max; | |
4679 | unsigned long int j; | |
4680 | unsigned long int fact; | |
4681 | ||
4682 | memset (counts, '\0', i * sizeof (unsigned long int)); | |
4683 | ||
4684 | /* Determine how often each hash bucket is used. */ | |
4685 | for (j = 0; j < nsyms; ++j) | |
4686 | ++counts[hashcodes[j] % i]; | |
4687 | ||
4688 | /* For the weight function we need some information about the | |
4689 | pagesize on the target. This is information need not be 100% | |
4690 | accurate. Since this information is not available (so far) we | |
4691 | define it here to a reasonable default value. If it is crucial | |
4692 | to have a better value some day simply define this value. */ | |
4693 | # ifndef BFD_TARGET_PAGESIZE | |
4694 | # define BFD_TARGET_PAGESIZE (4096) | |
4695 | # endif | |
4696 | ||
4697 | /* We in any case need 2 + NSYMS entries for the size values and | |
4698 | the chains. */ | |
4699 | max = (2 + nsyms) * (bed->s->arch_size / 8); | |
4700 | ||
4701 | # if 1 | |
4702 | /* Variant 1: optimize for short chains. We add the squares | |
4703 | of all the chain lengths (which favors many small chain | |
4704 | over a few long chains). */ | |
4705 | for (j = 0; j < i; ++j) | |
4706 | max += counts[j] * counts[j]; | |
4707 | ||
4708 | /* This adds penalties for the overall size of the table. */ | |
4709 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
4710 | max *= fact * fact; | |
4711 | # else | |
4712 | /* Variant 2: Optimize a lot more for small table. Here we | |
4713 | also add squares of the size but we also add penalties for | |
4714 | empty slots (the +1 term). */ | |
4715 | for (j = 0; j < i; ++j) | |
4716 | max += (1 + counts[j]) * (1 + counts[j]); | |
4717 | ||
4718 | /* The overall size of the table is considered, but not as | |
4719 | strong as in variant 1, where it is squared. */ | |
4720 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
4721 | max *= fact; | |
4722 | # endif | |
4723 | ||
4724 | /* Compare with current best results. */ | |
4725 | if (max < best_chlen) | |
4726 | { | |
4727 | best_chlen = max; | |
4728 | best_size = i; | |
4729 | } | |
4730 | } | |
4731 | ||
4732 | free (counts); | |
4733 | } | |
4734 | else | |
4735 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
4736 | { | |
4737 | /* This is the fallback solution if no 64bit type is available or if we | |
4738 | are not supposed to spend much time on optimizations. We select the | |
4739 | bucket count using a fixed set of numbers. */ | |
4740 | for (i = 0; elf_buckets[i] != 0; i++) | |
4741 | { | |
4742 | best_size = elf_buckets[i]; | |
4743 | if (dynsymcount < elf_buckets[i + 1]) | |
4744 | break; | |
4745 | } | |
4746 | } | |
4747 | ||
4748 | /* Free the arrays we needed. */ | |
4749 | free (hashcodes); | |
4750 | ||
4751 | return best_size; | |
4752 | } | |
4753 | ||
4754 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
4755 | called by the ELF linker emulation before_allocation routine. We | |
4756 | must set the sizes of the sections before the linker sets the | |
4757 | addresses of the various sections. */ | |
4758 | ||
4759 | bfd_boolean | |
4760 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
4761 | const char *soname, | |
4762 | const char *rpath, | |
4763 | const char *filter_shlib, | |
4764 | const char * const *auxiliary_filters, | |
4765 | struct bfd_link_info *info, | |
4766 | asection **sinterpptr, | |
4767 | struct bfd_elf_version_tree *verdefs) | |
4768 | { | |
4769 | bfd_size_type soname_indx; | |
4770 | bfd *dynobj; | |
4771 | const struct elf_backend_data *bed; | |
4772 | struct elf_assign_sym_version_info asvinfo; | |
4773 | ||
4774 | *sinterpptr = NULL; | |
4775 | ||
4776 | soname_indx = (bfd_size_type) -1; | |
4777 | ||
4778 | if (!is_elf_hash_table (info->hash)) | |
4779 | return TRUE; | |
4780 | ||
8c37241b | 4781 | elf_tdata (output_bfd)->relro = info->relro; |
5a580b3a AM |
4782 | if (info->execstack) |
4783 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
4784 | else if (info->noexecstack) | |
4785 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
4786 | else | |
4787 | { | |
4788 | bfd *inputobj; | |
4789 | asection *notesec = NULL; | |
4790 | int exec = 0; | |
4791 | ||
4792 | for (inputobj = info->input_bfds; | |
4793 | inputobj; | |
4794 | inputobj = inputobj->link_next) | |
4795 | { | |
4796 | asection *s; | |
4797 | ||
4798 | if (inputobj->flags & DYNAMIC) | |
4799 | continue; | |
4800 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
4801 | if (s) | |
4802 | { | |
4803 | if (s->flags & SEC_CODE) | |
4804 | exec = PF_X; | |
4805 | notesec = s; | |
4806 | } | |
4807 | else | |
4808 | exec = PF_X; | |
4809 | } | |
4810 | if (notesec) | |
4811 | { | |
4812 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
4813 | if (exec && info->relocatable | |
4814 | && notesec->output_section != bfd_abs_section_ptr) | |
4815 | notesec->output_section->flags |= SEC_CODE; | |
4816 | } | |
4817 | } | |
4818 | ||
4819 | /* Any syms created from now on start with -1 in | |
4820 | got.refcount/offset and plt.refcount/offset. */ | |
4821 | elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset; | |
4822 | ||
4823 | /* The backend may have to create some sections regardless of whether | |
4824 | we're dynamic or not. */ | |
4825 | bed = get_elf_backend_data (output_bfd); | |
4826 | if (bed->elf_backend_always_size_sections | |
4827 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
4828 | return FALSE; | |
4829 | ||
4830 | dynobj = elf_hash_table (info)->dynobj; | |
4831 | ||
4832 | /* If there were no dynamic objects in the link, there is nothing to | |
4833 | do here. */ | |
4834 | if (dynobj == NULL) | |
4835 | return TRUE; | |
4836 | ||
4837 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
4838 | return FALSE; | |
4839 | ||
4840 | if (elf_hash_table (info)->dynamic_sections_created) | |
4841 | { | |
4842 | struct elf_info_failed eif; | |
4843 | struct elf_link_hash_entry *h; | |
4844 | asection *dynstr; | |
4845 | struct bfd_elf_version_tree *t; | |
4846 | struct bfd_elf_version_expr *d; | |
4847 | bfd_boolean all_defined; | |
4848 | ||
4849 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
4850 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
4851 | ||
4852 | if (soname != NULL) | |
4853 | { | |
4854 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
4855 | soname, TRUE); | |
4856 | if (soname_indx == (bfd_size_type) -1 | |
4857 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
4858 | return FALSE; | |
4859 | } | |
4860 | ||
4861 | if (info->symbolic) | |
4862 | { | |
4863 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
4864 | return FALSE; | |
4865 | info->flags |= DF_SYMBOLIC; | |
4866 | } | |
4867 | ||
4868 | if (rpath != NULL) | |
4869 | { | |
4870 | bfd_size_type indx; | |
4871 | ||
4872 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
4873 | TRUE); | |
4874 | if (indx == (bfd_size_type) -1 | |
4875 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
4876 | return FALSE; | |
4877 | ||
4878 | if (info->new_dtags) | |
4879 | { | |
4880 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
4881 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
4882 | return FALSE; | |
4883 | } | |
4884 | } | |
4885 | ||
4886 | if (filter_shlib != NULL) | |
4887 | { | |
4888 | bfd_size_type indx; | |
4889 | ||
4890 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
4891 | filter_shlib, TRUE); | |
4892 | if (indx == (bfd_size_type) -1 | |
4893 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
4894 | return FALSE; | |
4895 | } | |
4896 | ||
4897 | if (auxiliary_filters != NULL) | |
4898 | { | |
4899 | const char * const *p; | |
4900 | ||
4901 | for (p = auxiliary_filters; *p != NULL; p++) | |
4902 | { | |
4903 | bfd_size_type indx; | |
4904 | ||
4905 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
4906 | *p, TRUE); | |
4907 | if (indx == (bfd_size_type) -1 | |
4908 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
4909 | return FALSE; | |
4910 | } | |
4911 | } | |
4912 | ||
4913 | eif.info = info; | |
4914 | eif.verdefs = verdefs; | |
4915 | eif.failed = FALSE; | |
4916 | ||
4917 | /* If we are supposed to export all symbols into the dynamic symbol | |
4918 | table (this is not the normal case), then do so. */ | |
4919 | if (info->export_dynamic) | |
4920 | { | |
4921 | elf_link_hash_traverse (elf_hash_table (info), | |
4922 | _bfd_elf_export_symbol, | |
4923 | &eif); | |
4924 | if (eif.failed) | |
4925 | return FALSE; | |
4926 | } | |
4927 | ||
4928 | /* Make all global versions with definition. */ | |
4929 | for (t = verdefs; t != NULL; t = t->next) | |
4930 | for (d = t->globals.list; d != NULL; d = d->next) | |
4931 | if (!d->symver && d->symbol) | |
4932 | { | |
4933 | const char *verstr, *name; | |
4934 | size_t namelen, verlen, newlen; | |
4935 | char *newname, *p; | |
4936 | struct elf_link_hash_entry *newh; | |
4937 | ||
4938 | name = d->symbol; | |
4939 | namelen = strlen (name); | |
4940 | verstr = t->name; | |
4941 | verlen = strlen (verstr); | |
4942 | newlen = namelen + verlen + 3; | |
4943 | ||
4944 | newname = bfd_malloc (newlen); | |
4945 | if (newname == NULL) | |
4946 | return FALSE; | |
4947 | memcpy (newname, name, namelen); | |
4948 | ||
4949 | /* Check the hidden versioned definition. */ | |
4950 | p = newname + namelen; | |
4951 | *p++ = ELF_VER_CHR; | |
4952 | memcpy (p, verstr, verlen + 1); | |
4953 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
4954 | newname, FALSE, FALSE, | |
4955 | FALSE); | |
4956 | if (newh == NULL | |
4957 | || (newh->root.type != bfd_link_hash_defined | |
4958 | && newh->root.type != bfd_link_hash_defweak)) | |
4959 | { | |
4960 | /* Check the default versioned definition. */ | |
4961 | *p++ = ELF_VER_CHR; | |
4962 | memcpy (p, verstr, verlen + 1); | |
4963 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
4964 | newname, FALSE, FALSE, | |
4965 | FALSE); | |
4966 | } | |
4967 | free (newname); | |
4968 | ||
4969 | /* Mark this version if there is a definition and it is | |
4970 | not defined in a shared object. */ | |
4971 | if (newh != NULL | |
f5385ebf | 4972 | && !newh->def_dynamic |
5a580b3a AM |
4973 | && (newh->root.type == bfd_link_hash_defined |
4974 | || newh->root.type == bfd_link_hash_defweak)) | |
4975 | d->symver = 1; | |
4976 | } | |
4977 | ||
4978 | /* Attach all the symbols to their version information. */ | |
4979 | asvinfo.output_bfd = output_bfd; | |
4980 | asvinfo.info = info; | |
4981 | asvinfo.verdefs = verdefs; | |
4982 | asvinfo.failed = FALSE; | |
4983 | ||
4984 | elf_link_hash_traverse (elf_hash_table (info), | |
4985 | _bfd_elf_link_assign_sym_version, | |
4986 | &asvinfo); | |
4987 | if (asvinfo.failed) | |
4988 | return FALSE; | |
4989 | ||
4990 | if (!info->allow_undefined_version) | |
4991 | { | |
4992 | /* Check if all global versions have a definition. */ | |
4993 | all_defined = TRUE; | |
4994 | for (t = verdefs; t != NULL; t = t->next) | |
4995 | for (d = t->globals.list; d != NULL; d = d->next) | |
4996 | if (!d->symver && !d->script) | |
4997 | { | |
4998 | (*_bfd_error_handler) | |
4999 | (_("%s: undefined version: %s"), | |
5000 | d->pattern, t->name); | |
5001 | all_defined = FALSE; | |
5002 | } | |
5003 | ||
5004 | if (!all_defined) | |
5005 | { | |
5006 | bfd_set_error (bfd_error_bad_value); | |
5007 | return FALSE; | |
5008 | } | |
5009 | } | |
5010 | ||
5011 | /* Find all symbols which were defined in a dynamic object and make | |
5012 | the backend pick a reasonable value for them. */ | |
5013 | elf_link_hash_traverse (elf_hash_table (info), | |
5014 | _bfd_elf_adjust_dynamic_symbol, | |
5015 | &eif); | |
5016 | if (eif.failed) | |
5017 | return FALSE; | |
5018 | ||
5019 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 5020 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
5021 | now so that we know the final size of the .dynamic section. */ |
5022 | ||
5023 | /* If there are initialization and/or finalization functions to | |
5024 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
5025 | h = (info->init_function | |
5026 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5027 | info->init_function, FALSE, | |
5028 | FALSE, FALSE) | |
5029 | : NULL); | |
5030 | if (h != NULL | |
f5385ebf AM |
5031 | && (h->ref_regular |
5032 | || h->def_regular)) | |
5a580b3a AM |
5033 | { |
5034 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
5035 | return FALSE; | |
5036 | } | |
5037 | h = (info->fini_function | |
5038 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5039 | info->fini_function, FALSE, | |
5040 | FALSE, FALSE) | |
5041 | : NULL); | |
5042 | if (h != NULL | |
f5385ebf AM |
5043 | && (h->ref_regular |
5044 | || h->def_regular)) | |
5a580b3a AM |
5045 | { |
5046 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
5047 | return FALSE; | |
5048 | } | |
5049 | ||
5050 | if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL) | |
5051 | { | |
5052 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
5053 | if (! info->executable) | |
5054 | { | |
5055 | bfd *sub; | |
5056 | asection *o; | |
5057 | ||
5058 | for (sub = info->input_bfds; sub != NULL; | |
5059 | sub = sub->link_next) | |
5060 | for (o = sub->sections; o != NULL; o = o->next) | |
5061 | if (elf_section_data (o)->this_hdr.sh_type | |
5062 | == SHT_PREINIT_ARRAY) | |
5063 | { | |
5064 | (*_bfd_error_handler) | |
d003868e AM |
5065 | (_("%B: .preinit_array section is not allowed in DSO"), |
5066 | sub); | |
5a580b3a AM |
5067 | break; |
5068 | } | |
5069 | ||
5070 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5071 | return FALSE; | |
5072 | } | |
5073 | ||
5074 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
5075 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
5076 | return FALSE; | |
5077 | } | |
5078 | if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL) | |
5079 | { | |
5080 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
5081 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
5082 | return FALSE; | |
5083 | } | |
5084 | if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL) | |
5085 | { | |
5086 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
5087 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
5088 | return FALSE; | |
5089 | } | |
5090 | ||
5091 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5092 | /* If .dynstr is excluded from the link, we don't want any of | |
5093 | these tags. Strictly, we should be checking each section | |
5094 | individually; This quick check covers for the case where | |
5095 | someone does a /DISCARD/ : { *(*) }. */ | |
5096 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
5097 | { | |
5098 | bfd_size_type strsize; | |
5099 | ||
5100 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
5101 | if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0) | |
5102 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
5103 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
5104 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
5105 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
5106 | bed->s->sizeof_sym)) | |
5107 | return FALSE; | |
5108 | } | |
5109 | } | |
5110 | ||
5111 | /* The backend must work out the sizes of all the other dynamic | |
5112 | sections. */ | |
5113 | if (bed->elf_backend_size_dynamic_sections | |
5114 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
5115 | return FALSE; | |
5116 | ||
5117 | if (elf_hash_table (info)->dynamic_sections_created) | |
5118 | { | |
5119 | bfd_size_type dynsymcount; | |
5120 | asection *s; | |
5121 | size_t bucketcount = 0; | |
5122 | size_t hash_entry_size; | |
5123 | unsigned int dtagcount; | |
5124 | ||
5125 | /* Set up the version definition section. */ | |
5126 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
5127 | BFD_ASSERT (s != NULL); | |
5128 | ||
5129 | /* We may have created additional version definitions if we are | |
5130 | just linking a regular application. */ | |
5131 | verdefs = asvinfo.verdefs; | |
5132 | ||
5133 | /* Skip anonymous version tag. */ | |
5134 | if (verdefs != NULL && verdefs->vernum == 0) | |
5135 | verdefs = verdefs->next; | |
5136 | ||
3e3b46e5 | 5137 | if (verdefs == NULL && !info->create_default_symver) |
5a580b3a AM |
5138 | _bfd_strip_section_from_output (info, s); |
5139 | else | |
5140 | { | |
5141 | unsigned int cdefs; | |
5142 | bfd_size_type size; | |
5143 | struct bfd_elf_version_tree *t; | |
5144 | bfd_byte *p; | |
5145 | Elf_Internal_Verdef def; | |
5146 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
5147 | struct bfd_link_hash_entry *bh; |
5148 | struct elf_link_hash_entry *h; | |
5149 | const char *name; | |
5a580b3a AM |
5150 | |
5151 | cdefs = 0; | |
5152 | size = 0; | |
5153 | ||
5154 | /* Make space for the base version. */ | |
5155 | size += sizeof (Elf_External_Verdef); | |
5156 | size += sizeof (Elf_External_Verdaux); | |
5157 | ++cdefs; | |
5158 | ||
3e3b46e5 PB |
5159 | /* Make space for the default version. */ |
5160 | if (info->create_default_symver) | |
5161 | { | |
5162 | size += sizeof (Elf_External_Verdef); | |
5163 | ++cdefs; | |
5164 | } | |
5165 | ||
5a580b3a AM |
5166 | for (t = verdefs; t != NULL; t = t->next) |
5167 | { | |
5168 | struct bfd_elf_version_deps *n; | |
5169 | ||
5170 | size += sizeof (Elf_External_Verdef); | |
5171 | size += sizeof (Elf_External_Verdaux); | |
5172 | ++cdefs; | |
5173 | ||
5174 | for (n = t->deps; n != NULL; n = n->next) | |
5175 | size += sizeof (Elf_External_Verdaux); | |
5176 | } | |
5177 | ||
eea6121a AM |
5178 | s->size = size; |
5179 | s->contents = bfd_alloc (output_bfd, s->size); | |
5180 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5181 | return FALSE; |
5182 | ||
5183 | /* Fill in the version definition section. */ | |
5184 | ||
5185 | p = s->contents; | |
5186 | ||
5187 | def.vd_version = VER_DEF_CURRENT; | |
5188 | def.vd_flags = VER_FLG_BASE; | |
5189 | def.vd_ndx = 1; | |
5190 | def.vd_cnt = 1; | |
3e3b46e5 PB |
5191 | if (info->create_default_symver) |
5192 | { | |
5193 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
5194 | def.vd_next = sizeof (Elf_External_Verdef); | |
5195 | } | |
5196 | else | |
5197 | { | |
5198 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5199 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5200 | + sizeof (Elf_External_Verdaux)); | |
5201 | } | |
5a580b3a AM |
5202 | |
5203 | if (soname_indx != (bfd_size_type) -1) | |
5204 | { | |
5205 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5206 | soname_indx); | |
5207 | def.vd_hash = bfd_elf_hash (soname); | |
5208 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 5209 | name = soname; |
5a580b3a AM |
5210 | } |
5211 | else | |
5212 | { | |
5a580b3a AM |
5213 | bfd_size_type indx; |
5214 | ||
5215 | name = basename (output_bfd->filename); | |
5216 | def.vd_hash = bfd_elf_hash (name); | |
5217 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5218 | name, FALSE); | |
5219 | if (indx == (bfd_size_type) -1) | |
5220 | return FALSE; | |
5221 | defaux.vda_name = indx; | |
5222 | } | |
5223 | defaux.vda_next = 0; | |
5224 | ||
5225 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5226 | (Elf_External_Verdef *) p); | |
5227 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
5228 | if (info->create_default_symver) |
5229 | { | |
5230 | /* Add a symbol representing this version. */ | |
5231 | bh = NULL; | |
5232 | if (! (_bfd_generic_link_add_one_symbol | |
5233 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5234 | 0, NULL, FALSE, | |
5235 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5236 | return FALSE; | |
5237 | h = (struct elf_link_hash_entry *) bh; | |
5238 | h->non_elf = 0; | |
5239 | h->def_regular = 1; | |
5240 | h->type = STT_OBJECT; | |
5241 | h->verinfo.vertree = NULL; | |
5242 | ||
5243 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
5244 | return FALSE; | |
5245 | ||
5246 | /* Create a duplicate of the base version with the same | |
5247 | aux block, but different flags. */ | |
5248 | def.vd_flags = 0; | |
5249 | def.vd_ndx = 2; | |
5250 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5251 | if (verdefs) | |
5252 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5253 | + sizeof (Elf_External_Verdaux)); | |
5254 | else | |
5255 | def.vd_next = 0; | |
5256 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5257 | (Elf_External_Verdef *) p); | |
5258 | p += sizeof (Elf_External_Verdef); | |
5259 | } | |
5a580b3a AM |
5260 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
5261 | (Elf_External_Verdaux *) p); | |
5262 | p += sizeof (Elf_External_Verdaux); | |
5263 | ||
5264 | for (t = verdefs; t != NULL; t = t->next) | |
5265 | { | |
5266 | unsigned int cdeps; | |
5267 | struct bfd_elf_version_deps *n; | |
5a580b3a AM |
5268 | |
5269 | cdeps = 0; | |
5270 | for (n = t->deps; n != NULL; n = n->next) | |
5271 | ++cdeps; | |
5272 | ||
5273 | /* Add a symbol representing this version. */ | |
5274 | bh = NULL; | |
5275 | if (! (_bfd_generic_link_add_one_symbol | |
5276 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5277 | 0, NULL, FALSE, | |
5278 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5279 | return FALSE; | |
5280 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
5281 | h->non_elf = 0; |
5282 | h->def_regular = 1; | |
5a580b3a AM |
5283 | h->type = STT_OBJECT; |
5284 | h->verinfo.vertree = t; | |
5285 | ||
c152c796 | 5286 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
5287 | return FALSE; |
5288 | ||
5289 | def.vd_version = VER_DEF_CURRENT; | |
5290 | def.vd_flags = 0; | |
5291 | if (t->globals.list == NULL | |
5292 | && t->locals.list == NULL | |
5293 | && ! t->used) | |
5294 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 5295 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
5296 | def.vd_cnt = cdeps + 1; |
5297 | def.vd_hash = bfd_elf_hash (t->name); | |
5298 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5299 | def.vd_next = 0; | |
5300 | if (t->next != NULL) | |
5301 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5302 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
5303 | ||
5304 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5305 | (Elf_External_Verdef *) p); | |
5306 | p += sizeof (Elf_External_Verdef); | |
5307 | ||
5308 | defaux.vda_name = h->dynstr_index; | |
5309 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5310 | h->dynstr_index); | |
5311 | defaux.vda_next = 0; | |
5312 | if (t->deps != NULL) | |
5313 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5314 | t->name_indx = defaux.vda_name; | |
5315 | ||
5316 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5317 | (Elf_External_Verdaux *) p); | |
5318 | p += sizeof (Elf_External_Verdaux); | |
5319 | ||
5320 | for (n = t->deps; n != NULL; n = n->next) | |
5321 | { | |
5322 | if (n->version_needed == NULL) | |
5323 | { | |
5324 | /* This can happen if there was an error in the | |
5325 | version script. */ | |
5326 | defaux.vda_name = 0; | |
5327 | } | |
5328 | else | |
5329 | { | |
5330 | defaux.vda_name = n->version_needed->name_indx; | |
5331 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5332 | defaux.vda_name); | |
5333 | } | |
5334 | if (n->next == NULL) | |
5335 | defaux.vda_next = 0; | |
5336 | else | |
5337 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5338 | ||
5339 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5340 | (Elf_External_Verdaux *) p); | |
5341 | p += sizeof (Elf_External_Verdaux); | |
5342 | } | |
5343 | } | |
5344 | ||
5345 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
5346 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
5347 | return FALSE; | |
5348 | ||
5349 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
5350 | } | |
5351 | ||
5352 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
5353 | { | |
5354 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
5355 | return FALSE; | |
5356 | } | |
5357 | else if (info->flags & DF_BIND_NOW) | |
5358 | { | |
5359 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
5360 | return FALSE; | |
5361 | } | |
5362 | ||
5363 | if (info->flags_1) | |
5364 | { | |
5365 | if (info->executable) | |
5366 | info->flags_1 &= ~ (DF_1_INITFIRST | |
5367 | | DF_1_NODELETE | |
5368 | | DF_1_NOOPEN); | |
5369 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
5370 | return FALSE; | |
5371 | } | |
5372 | ||
5373 | /* Work out the size of the version reference section. */ | |
5374 | ||
5375 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
5376 | BFD_ASSERT (s != NULL); | |
5377 | { | |
5378 | struct elf_find_verdep_info sinfo; | |
5379 | ||
5380 | sinfo.output_bfd = output_bfd; | |
5381 | sinfo.info = info; | |
5382 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
5383 | if (sinfo.vers == 0) | |
5384 | sinfo.vers = 1; | |
5385 | sinfo.failed = FALSE; | |
5386 | ||
5387 | elf_link_hash_traverse (elf_hash_table (info), | |
5388 | _bfd_elf_link_find_version_dependencies, | |
5389 | &sinfo); | |
5390 | ||
5391 | if (elf_tdata (output_bfd)->verref == NULL) | |
5392 | _bfd_strip_section_from_output (info, s); | |
5393 | else | |
5394 | { | |
5395 | Elf_Internal_Verneed *t; | |
5396 | unsigned int size; | |
5397 | unsigned int crefs; | |
5398 | bfd_byte *p; | |
5399 | ||
5400 | /* Build the version definition section. */ | |
5401 | size = 0; | |
5402 | crefs = 0; | |
5403 | for (t = elf_tdata (output_bfd)->verref; | |
5404 | t != NULL; | |
5405 | t = t->vn_nextref) | |
5406 | { | |
5407 | Elf_Internal_Vernaux *a; | |
5408 | ||
5409 | size += sizeof (Elf_External_Verneed); | |
5410 | ++crefs; | |
5411 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5412 | size += sizeof (Elf_External_Vernaux); | |
5413 | } | |
5414 | ||
eea6121a AM |
5415 | s->size = size; |
5416 | s->contents = bfd_alloc (output_bfd, s->size); | |
5a580b3a AM |
5417 | if (s->contents == NULL) |
5418 | return FALSE; | |
5419 | ||
5420 | p = s->contents; | |
5421 | for (t = elf_tdata (output_bfd)->verref; | |
5422 | t != NULL; | |
5423 | t = t->vn_nextref) | |
5424 | { | |
5425 | unsigned int caux; | |
5426 | Elf_Internal_Vernaux *a; | |
5427 | bfd_size_type indx; | |
5428 | ||
5429 | caux = 0; | |
5430 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5431 | ++caux; | |
5432 | ||
5433 | t->vn_version = VER_NEED_CURRENT; | |
5434 | t->vn_cnt = caux; | |
5435 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5436 | elf_dt_name (t->vn_bfd) != NULL | |
5437 | ? elf_dt_name (t->vn_bfd) | |
5438 | : basename (t->vn_bfd->filename), | |
5439 | FALSE); | |
5440 | if (indx == (bfd_size_type) -1) | |
5441 | return FALSE; | |
5442 | t->vn_file = indx; | |
5443 | t->vn_aux = sizeof (Elf_External_Verneed); | |
5444 | if (t->vn_nextref == NULL) | |
5445 | t->vn_next = 0; | |
5446 | else | |
5447 | t->vn_next = (sizeof (Elf_External_Verneed) | |
5448 | + caux * sizeof (Elf_External_Vernaux)); | |
5449 | ||
5450 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
5451 | (Elf_External_Verneed *) p); | |
5452 | p += sizeof (Elf_External_Verneed); | |
5453 | ||
5454 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5455 | { | |
5456 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
5457 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5458 | a->vna_nodename, FALSE); | |
5459 | if (indx == (bfd_size_type) -1) | |
5460 | return FALSE; | |
5461 | a->vna_name = indx; | |
5462 | if (a->vna_nextptr == NULL) | |
5463 | a->vna_next = 0; | |
5464 | else | |
5465 | a->vna_next = sizeof (Elf_External_Vernaux); | |
5466 | ||
5467 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
5468 | (Elf_External_Vernaux *) p); | |
5469 | p += sizeof (Elf_External_Vernaux); | |
5470 | } | |
5471 | } | |
5472 | ||
5473 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
5474 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
5475 | return FALSE; | |
5476 | ||
5477 | elf_tdata (output_bfd)->cverrefs = crefs; | |
5478 | } | |
5479 | } | |
5480 | ||
5481 | /* Assign dynsym indicies. In a shared library we generate a | |
5482 | section symbol for each output section, which come first. | |
5483 | Next come all of the back-end allocated local dynamic syms, | |
5484 | followed by the rest of the global symbols. */ | |
5485 | ||
5486 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info); | |
5487 | ||
5488 | /* Work out the size of the symbol version section. */ | |
5489 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
5490 | BFD_ASSERT (s != NULL); | |
5491 | if (dynsymcount == 0 | |
3e3b46e5 PB |
5492 | || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL |
5493 | && !info->create_default_symver)) | |
5a580b3a AM |
5494 | { |
5495 | _bfd_strip_section_from_output (info, s); | |
5496 | /* The DYNSYMCOUNT might have changed if we were going to | |
5497 | output a dynamic symbol table entry for S. */ | |
5498 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info); | |
5499 | } | |
5500 | else | |
5501 | { | |
eea6121a AM |
5502 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
5503 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
5504 | if (s->contents == NULL) |
5505 | return FALSE; | |
5506 | ||
5507 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
5508 | return FALSE; | |
5509 | } | |
5510 | ||
5511 | /* Set the size of the .dynsym and .hash sections. We counted | |
5512 | the number of dynamic symbols in elf_link_add_object_symbols. | |
5513 | We will build the contents of .dynsym and .hash when we build | |
5514 | the final symbol table, because until then we do not know the | |
5515 | correct value to give the symbols. We built the .dynstr | |
5516 | section as we went along in elf_link_add_object_symbols. */ | |
5517 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
5518 | BFD_ASSERT (s != NULL); | |
eea6121a AM |
5519 | s->size = dynsymcount * bed->s->sizeof_sym; |
5520 | s->contents = bfd_alloc (output_bfd, s->size); | |
5521 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5522 | return FALSE; |
5523 | ||
5524 | if (dynsymcount != 0) | |
5525 | { | |
5526 | Elf_Internal_Sym isym; | |
5527 | ||
5528 | /* The first entry in .dynsym is a dummy symbol. */ | |
5529 | isym.st_value = 0; | |
5530 | isym.st_size = 0; | |
5531 | isym.st_name = 0; | |
5532 | isym.st_info = 0; | |
5533 | isym.st_other = 0; | |
5534 | isym.st_shndx = 0; | |
5535 | bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0); | |
5536 | } | |
5537 | ||
5538 | /* Compute the size of the hashing table. As a side effect this | |
5539 | computes the hash values for all the names we export. */ | |
5540 | bucketcount = compute_bucket_count (info); | |
5541 | ||
5542 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
5543 | BFD_ASSERT (s != NULL); | |
5544 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
eea6121a AM |
5545 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); |
5546 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
5547 | if (s->contents == NULL) |
5548 | return FALSE; | |
5549 | ||
5550 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
5551 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
5552 | s->contents + hash_entry_size); | |
5553 | ||
5554 | elf_hash_table (info)->bucketcount = bucketcount; | |
5555 | ||
5556 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5557 | BFD_ASSERT (s != NULL); | |
5558 | ||
4ad4eba5 | 5559 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 5560 | |
eea6121a | 5561 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
5562 | |
5563 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
5564 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
5565 | return FALSE; | |
5566 | } | |
5567 | ||
5568 | return TRUE; | |
5569 | } | |
c152c796 AM |
5570 | |
5571 | /* Final phase of ELF linker. */ | |
5572 | ||
5573 | /* A structure we use to avoid passing large numbers of arguments. */ | |
5574 | ||
5575 | struct elf_final_link_info | |
5576 | { | |
5577 | /* General link information. */ | |
5578 | struct bfd_link_info *info; | |
5579 | /* Output BFD. */ | |
5580 | bfd *output_bfd; | |
5581 | /* Symbol string table. */ | |
5582 | struct bfd_strtab_hash *symstrtab; | |
5583 | /* .dynsym section. */ | |
5584 | asection *dynsym_sec; | |
5585 | /* .hash section. */ | |
5586 | asection *hash_sec; | |
5587 | /* symbol version section (.gnu.version). */ | |
5588 | asection *symver_sec; | |
5589 | /* Buffer large enough to hold contents of any section. */ | |
5590 | bfd_byte *contents; | |
5591 | /* Buffer large enough to hold external relocs of any section. */ | |
5592 | void *external_relocs; | |
5593 | /* Buffer large enough to hold internal relocs of any section. */ | |
5594 | Elf_Internal_Rela *internal_relocs; | |
5595 | /* Buffer large enough to hold external local symbols of any input | |
5596 | BFD. */ | |
5597 | bfd_byte *external_syms; | |
5598 | /* And a buffer for symbol section indices. */ | |
5599 | Elf_External_Sym_Shndx *locsym_shndx; | |
5600 | /* Buffer large enough to hold internal local symbols of any input | |
5601 | BFD. */ | |
5602 | Elf_Internal_Sym *internal_syms; | |
5603 | /* Array large enough to hold a symbol index for each local symbol | |
5604 | of any input BFD. */ | |
5605 | long *indices; | |
5606 | /* Array large enough to hold a section pointer for each local | |
5607 | symbol of any input BFD. */ | |
5608 | asection **sections; | |
5609 | /* Buffer to hold swapped out symbols. */ | |
5610 | bfd_byte *symbuf; | |
5611 | /* And one for symbol section indices. */ | |
5612 | Elf_External_Sym_Shndx *symshndxbuf; | |
5613 | /* Number of swapped out symbols in buffer. */ | |
5614 | size_t symbuf_count; | |
5615 | /* Number of symbols which fit in symbuf. */ | |
5616 | size_t symbuf_size; | |
5617 | /* And same for symshndxbuf. */ | |
5618 | size_t shndxbuf_size; | |
5619 | }; | |
5620 | ||
5621 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
5622 | ||
5623 | struct elf_outext_info | |
5624 | { | |
5625 | bfd_boolean failed; | |
5626 | bfd_boolean localsyms; | |
5627 | struct elf_final_link_info *finfo; | |
5628 | }; | |
5629 | ||
5630 | /* When performing a relocatable link, the input relocations are | |
5631 | preserved. But, if they reference global symbols, the indices | |
5632 | referenced must be updated. Update all the relocations in | |
5633 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ | |
5634 | ||
5635 | static void | |
5636 | elf_link_adjust_relocs (bfd *abfd, | |
5637 | Elf_Internal_Shdr *rel_hdr, | |
5638 | unsigned int count, | |
5639 | struct elf_link_hash_entry **rel_hash) | |
5640 | { | |
5641 | unsigned int i; | |
5642 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5643 | bfd_byte *erela; | |
5644 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
5645 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
5646 | bfd_vma r_type_mask; | |
5647 | int r_sym_shift; | |
5648 | ||
5649 | if (rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
5650 | { | |
5651 | swap_in = bed->s->swap_reloc_in; | |
5652 | swap_out = bed->s->swap_reloc_out; | |
5653 | } | |
5654 | else if (rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
5655 | { | |
5656 | swap_in = bed->s->swap_reloca_in; | |
5657 | swap_out = bed->s->swap_reloca_out; | |
5658 | } | |
5659 | else | |
5660 | abort (); | |
5661 | ||
5662 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
5663 | abort (); | |
5664 | ||
5665 | if (bed->s->arch_size == 32) | |
5666 | { | |
5667 | r_type_mask = 0xff; | |
5668 | r_sym_shift = 8; | |
5669 | } | |
5670 | else | |
5671 | { | |
5672 | r_type_mask = 0xffffffff; | |
5673 | r_sym_shift = 32; | |
5674 | } | |
5675 | ||
5676 | erela = rel_hdr->contents; | |
5677 | for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) | |
5678 | { | |
5679 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
5680 | unsigned int j; | |
5681 | ||
5682 | if (*rel_hash == NULL) | |
5683 | continue; | |
5684 | ||
5685 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
5686 | ||
5687 | (*swap_in) (abfd, erela, irela); | |
5688 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
5689 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
5690 | | (irela[j].r_info & r_type_mask)); | |
5691 | (*swap_out) (abfd, irela, erela); | |
5692 | } | |
5693 | } | |
5694 | ||
5695 | struct elf_link_sort_rela | |
5696 | { | |
5697 | union { | |
5698 | bfd_vma offset; | |
5699 | bfd_vma sym_mask; | |
5700 | } u; | |
5701 | enum elf_reloc_type_class type; | |
5702 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
5703 | Elf_Internal_Rela rela[1]; | |
5704 | }; | |
5705 | ||
5706 | static int | |
5707 | elf_link_sort_cmp1 (const void *A, const void *B) | |
5708 | { | |
5709 | const struct elf_link_sort_rela *a = A; | |
5710 | const struct elf_link_sort_rela *b = B; | |
5711 | int relativea, relativeb; | |
5712 | ||
5713 | relativea = a->type == reloc_class_relative; | |
5714 | relativeb = b->type == reloc_class_relative; | |
5715 | ||
5716 | if (relativea < relativeb) | |
5717 | return 1; | |
5718 | if (relativea > relativeb) | |
5719 | return -1; | |
5720 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
5721 | return -1; | |
5722 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
5723 | return 1; | |
5724 | if (a->rela->r_offset < b->rela->r_offset) | |
5725 | return -1; | |
5726 | if (a->rela->r_offset > b->rela->r_offset) | |
5727 | return 1; | |
5728 | return 0; | |
5729 | } | |
5730 | ||
5731 | static int | |
5732 | elf_link_sort_cmp2 (const void *A, const void *B) | |
5733 | { | |
5734 | const struct elf_link_sort_rela *a = A; | |
5735 | const struct elf_link_sort_rela *b = B; | |
5736 | int copya, copyb; | |
5737 | ||
5738 | if (a->u.offset < b->u.offset) | |
5739 | return -1; | |
5740 | if (a->u.offset > b->u.offset) | |
5741 | return 1; | |
5742 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); | |
5743 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
5744 | if (copya < copyb) | |
5745 | return -1; | |
5746 | if (copya > copyb) | |
5747 | return 1; | |
5748 | if (a->rela->r_offset < b->rela->r_offset) | |
5749 | return -1; | |
5750 | if (a->rela->r_offset > b->rela->r_offset) | |
5751 | return 1; | |
5752 | return 0; | |
5753 | } | |
5754 | ||
5755 | static size_t | |
5756 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
5757 | { | |
5758 | asection *reldyn; | |
5759 | bfd_size_type count, size; | |
5760 | size_t i, ret, sort_elt, ext_size; | |
5761 | bfd_byte *sort, *s_non_relative, *p; | |
5762 | struct elf_link_sort_rela *sq; | |
5763 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5764 | int i2e = bed->s->int_rels_per_ext_rel; | |
5765 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
5766 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
5767 | struct bfd_link_order *lo; | |
5768 | bfd_vma r_sym_mask; | |
5769 | ||
5770 | reldyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
eea6121a | 5771 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
5772 | { |
5773 | reldyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
eea6121a | 5774 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
5775 | return 0; |
5776 | ext_size = bed->s->sizeof_rel; | |
5777 | swap_in = bed->s->swap_reloc_in; | |
5778 | swap_out = bed->s->swap_reloc_out; | |
5779 | } | |
5780 | else | |
5781 | { | |
5782 | ext_size = bed->s->sizeof_rela; | |
5783 | swap_in = bed->s->swap_reloca_in; | |
5784 | swap_out = bed->s->swap_reloca_out; | |
5785 | } | |
eea6121a | 5786 | count = reldyn->size / ext_size; |
c152c796 AM |
5787 | |
5788 | size = 0; | |
5789 | for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next) | |
5790 | if (lo->type == bfd_indirect_link_order) | |
5791 | { | |
5792 | asection *o = lo->u.indirect.section; | |
eea6121a | 5793 | size += o->size; |
c152c796 AM |
5794 | } |
5795 | ||
eea6121a | 5796 | if (size != reldyn->size) |
c152c796 AM |
5797 | return 0; |
5798 | ||
5799 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
5800 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
5801 | sort = bfd_zmalloc (sort_elt * count); | |
5802 | if (sort == NULL) | |
5803 | { | |
5804 | (*info->callbacks->warning) | |
5805 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
5806 | return 0; | |
5807 | } | |
5808 | ||
5809 | if (bed->s->arch_size == 32) | |
5810 | r_sym_mask = ~(bfd_vma) 0xff; | |
5811 | else | |
5812 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
5813 | ||
5814 | for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next) | |
5815 | if (lo->type == bfd_indirect_link_order) | |
5816 | { | |
5817 | bfd_byte *erel, *erelend; | |
5818 | asection *o = lo->u.indirect.section; | |
5819 | ||
1da212d6 AM |
5820 | if (o->contents == NULL && o->size != 0) |
5821 | { | |
5822 | /* This is a reloc section that is being handled as a normal | |
5823 | section. See bfd_section_from_shdr. We can't combine | |
5824 | relocs in this case. */ | |
5825 | free (sort); | |
5826 | return 0; | |
5827 | } | |
c152c796 | 5828 | erel = o->contents; |
eea6121a | 5829 | erelend = o->contents + o->size; |
c152c796 AM |
5830 | p = sort + o->output_offset / ext_size * sort_elt; |
5831 | while (erel < erelend) | |
5832 | { | |
5833 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
5834 | (*swap_in) (abfd, erel, s->rela); | |
5835 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
5836 | s->u.sym_mask = r_sym_mask; | |
5837 | p += sort_elt; | |
5838 | erel += ext_size; | |
5839 | } | |
5840 | } | |
5841 | ||
5842 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
5843 | ||
5844 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
5845 | { | |
5846 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
5847 | if (s->type != reloc_class_relative) | |
5848 | break; | |
5849 | } | |
5850 | ret = i; | |
5851 | s_non_relative = p; | |
5852 | ||
5853 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
5854 | for (; i < count; i++, p += sort_elt) | |
5855 | { | |
5856 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
5857 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
5858 | sq = sp; | |
5859 | sp->u.offset = sq->rela->r_offset; | |
5860 | } | |
5861 | ||
5862 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
5863 | ||
5864 | for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next) | |
5865 | if (lo->type == bfd_indirect_link_order) | |
5866 | { | |
5867 | bfd_byte *erel, *erelend; | |
5868 | asection *o = lo->u.indirect.section; | |
5869 | ||
5870 | erel = o->contents; | |
eea6121a | 5871 | erelend = o->contents + o->size; |
c152c796 AM |
5872 | p = sort + o->output_offset / ext_size * sort_elt; |
5873 | while (erel < erelend) | |
5874 | { | |
5875 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
5876 | (*swap_out) (abfd, s->rela, erel); | |
5877 | p += sort_elt; | |
5878 | erel += ext_size; | |
5879 | } | |
5880 | } | |
5881 | ||
5882 | free (sort); | |
5883 | *psec = reldyn; | |
5884 | return ret; | |
5885 | } | |
5886 | ||
5887 | /* Flush the output symbols to the file. */ | |
5888 | ||
5889 | static bfd_boolean | |
5890 | elf_link_flush_output_syms (struct elf_final_link_info *finfo, | |
5891 | const struct elf_backend_data *bed) | |
5892 | { | |
5893 | if (finfo->symbuf_count > 0) | |
5894 | { | |
5895 | Elf_Internal_Shdr *hdr; | |
5896 | file_ptr pos; | |
5897 | bfd_size_type amt; | |
5898 | ||
5899 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
5900 | pos = hdr->sh_offset + hdr->sh_size; | |
5901 | amt = finfo->symbuf_count * bed->s->sizeof_sym; | |
5902 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
5903 | || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) | |
5904 | return FALSE; | |
5905 | ||
5906 | hdr->sh_size += amt; | |
5907 | finfo->symbuf_count = 0; | |
5908 | } | |
5909 | ||
5910 | return TRUE; | |
5911 | } | |
5912 | ||
5913 | /* Add a symbol to the output symbol table. */ | |
5914 | ||
5915 | static bfd_boolean | |
5916 | elf_link_output_sym (struct elf_final_link_info *finfo, | |
5917 | const char *name, | |
5918 | Elf_Internal_Sym *elfsym, | |
5919 | asection *input_sec, | |
5920 | struct elf_link_hash_entry *h) | |
5921 | { | |
5922 | bfd_byte *dest; | |
5923 | Elf_External_Sym_Shndx *destshndx; | |
5924 | bfd_boolean (*output_symbol_hook) | |
5925 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, | |
5926 | struct elf_link_hash_entry *); | |
5927 | const struct elf_backend_data *bed; | |
5928 | ||
5929 | bed = get_elf_backend_data (finfo->output_bfd); | |
5930 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; | |
5931 | if (output_symbol_hook != NULL) | |
5932 | { | |
5933 | if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h)) | |
5934 | return FALSE; | |
5935 | } | |
5936 | ||
5937 | if (name == NULL || *name == '\0') | |
5938 | elfsym->st_name = 0; | |
5939 | else if (input_sec->flags & SEC_EXCLUDE) | |
5940 | elfsym->st_name = 0; | |
5941 | else | |
5942 | { | |
5943 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
5944 | name, TRUE, FALSE); | |
5945 | if (elfsym->st_name == (unsigned long) -1) | |
5946 | return FALSE; | |
5947 | } | |
5948 | ||
5949 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
5950 | { | |
5951 | if (! elf_link_flush_output_syms (finfo, bed)) | |
5952 | return FALSE; | |
5953 | } | |
5954 | ||
5955 | dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; | |
5956 | destshndx = finfo->symshndxbuf; | |
5957 | if (destshndx != NULL) | |
5958 | { | |
5959 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
5960 | { | |
5961 | bfd_size_type amt; | |
5962 | ||
5963 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); | |
5964 | finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2); | |
5965 | if (destshndx == NULL) | |
5966 | return FALSE; | |
5967 | memset ((char *) destshndx + amt, 0, amt); | |
5968 | finfo->shndxbuf_size *= 2; | |
5969 | } | |
5970 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
5971 | } | |
5972 | ||
5973 | bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); | |
5974 | finfo->symbuf_count += 1; | |
5975 | bfd_get_symcount (finfo->output_bfd) += 1; | |
5976 | ||
5977 | return TRUE; | |
5978 | } | |
5979 | ||
5980 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in | |
5981 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
5982 | versioned symbol that would normally require an explicit version. | |
5983 | We also handle the case that a DSO references a hidden symbol | |
5984 | which may be satisfied by a versioned symbol in another DSO. */ | |
5985 | ||
5986 | static bfd_boolean | |
5987 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
5988 | const struct elf_backend_data *bed, | |
5989 | struct elf_link_hash_entry *h) | |
5990 | { | |
5991 | bfd *abfd; | |
5992 | struct elf_link_loaded_list *loaded; | |
5993 | ||
5994 | if (!is_elf_hash_table (info->hash)) | |
5995 | return FALSE; | |
5996 | ||
5997 | switch (h->root.type) | |
5998 | { | |
5999 | default: | |
6000 | abfd = NULL; | |
6001 | break; | |
6002 | ||
6003 | case bfd_link_hash_undefined: | |
6004 | case bfd_link_hash_undefweak: | |
6005 | abfd = h->root.u.undef.abfd; | |
6006 | if ((abfd->flags & DYNAMIC) == 0 | |
e56f61be | 6007 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
6008 | return FALSE; |
6009 | break; | |
6010 | ||
6011 | case bfd_link_hash_defined: | |
6012 | case bfd_link_hash_defweak: | |
6013 | abfd = h->root.u.def.section->owner; | |
6014 | break; | |
6015 | ||
6016 | case bfd_link_hash_common: | |
6017 | abfd = h->root.u.c.p->section->owner; | |
6018 | break; | |
6019 | } | |
6020 | BFD_ASSERT (abfd != NULL); | |
6021 | ||
6022 | for (loaded = elf_hash_table (info)->loaded; | |
6023 | loaded != NULL; | |
6024 | loaded = loaded->next) | |
6025 | { | |
6026 | bfd *input; | |
6027 | Elf_Internal_Shdr *hdr; | |
6028 | bfd_size_type symcount; | |
6029 | bfd_size_type extsymcount; | |
6030 | bfd_size_type extsymoff; | |
6031 | Elf_Internal_Shdr *versymhdr; | |
6032 | Elf_Internal_Sym *isym; | |
6033 | Elf_Internal_Sym *isymend; | |
6034 | Elf_Internal_Sym *isymbuf; | |
6035 | Elf_External_Versym *ever; | |
6036 | Elf_External_Versym *extversym; | |
6037 | ||
6038 | input = loaded->abfd; | |
6039 | ||
6040 | /* We check each DSO for a possible hidden versioned definition. */ | |
6041 | if (input == abfd | |
6042 | || (input->flags & DYNAMIC) == 0 | |
6043 | || elf_dynversym (input) == 0) | |
6044 | continue; | |
6045 | ||
6046 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
6047 | ||
6048 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
6049 | if (elf_bad_symtab (input)) | |
6050 | { | |
6051 | extsymcount = symcount; | |
6052 | extsymoff = 0; | |
6053 | } | |
6054 | else | |
6055 | { | |
6056 | extsymcount = symcount - hdr->sh_info; | |
6057 | extsymoff = hdr->sh_info; | |
6058 | } | |
6059 | ||
6060 | if (extsymcount == 0) | |
6061 | continue; | |
6062 | ||
6063 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
6064 | NULL, NULL, NULL); | |
6065 | if (isymbuf == NULL) | |
6066 | return FALSE; | |
6067 | ||
6068 | /* Read in any version definitions. */ | |
6069 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
6070 | extversym = bfd_malloc (versymhdr->sh_size); | |
6071 | if (extversym == NULL) | |
6072 | goto error_ret; | |
6073 | ||
6074 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
6075 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
6076 | != versymhdr->sh_size)) | |
6077 | { | |
6078 | free (extversym); | |
6079 | error_ret: | |
6080 | free (isymbuf); | |
6081 | return FALSE; | |
6082 | } | |
6083 | ||
6084 | ever = extversym + extsymoff; | |
6085 | isymend = isymbuf + extsymcount; | |
6086 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
6087 | { | |
6088 | const char *name; | |
6089 | Elf_Internal_Versym iver; | |
6090 | unsigned short version_index; | |
6091 | ||
6092 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
6093 | || isym->st_shndx == SHN_UNDEF) | |
6094 | continue; | |
6095 | ||
6096 | name = bfd_elf_string_from_elf_section (input, | |
6097 | hdr->sh_link, | |
6098 | isym->st_name); | |
6099 | if (strcmp (name, h->root.root.string) != 0) | |
6100 | continue; | |
6101 | ||
6102 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
6103 | ||
6104 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
6105 | { | |
6106 | /* If we have a non-hidden versioned sym, then it should | |
6107 | have provided a definition for the undefined sym. */ | |
6108 | abort (); | |
6109 | } | |
6110 | ||
6111 | version_index = iver.vs_vers & VERSYM_VERSION; | |
6112 | if (version_index == 1 || version_index == 2) | |
6113 | { | |
6114 | /* This is the base or first version. We can use it. */ | |
6115 | free (extversym); | |
6116 | free (isymbuf); | |
6117 | return TRUE; | |
6118 | } | |
6119 | } | |
6120 | ||
6121 | free (extversym); | |
6122 | free (isymbuf); | |
6123 | } | |
6124 | ||
6125 | return FALSE; | |
6126 | } | |
6127 | ||
6128 | /* Add an external symbol to the symbol table. This is called from | |
6129 | the hash table traversal routine. When generating a shared object, | |
6130 | we go through the symbol table twice. The first time we output | |
6131 | anything that might have been forced to local scope in a version | |
6132 | script. The second time we output the symbols that are still | |
6133 | global symbols. */ | |
6134 | ||
6135 | static bfd_boolean | |
6136 | elf_link_output_extsym (struct elf_link_hash_entry *h, void *data) | |
6137 | { | |
6138 | struct elf_outext_info *eoinfo = data; | |
6139 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
6140 | bfd_boolean strip; | |
6141 | Elf_Internal_Sym sym; | |
6142 | asection *input_sec; | |
6143 | const struct elf_backend_data *bed; | |
6144 | ||
6145 | if (h->root.type == bfd_link_hash_warning) | |
6146 | { | |
6147 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6148 | if (h->root.type == bfd_link_hash_new) | |
6149 | return TRUE; | |
6150 | } | |
6151 | ||
6152 | /* Decide whether to output this symbol in this pass. */ | |
6153 | if (eoinfo->localsyms) | |
6154 | { | |
f5385ebf | 6155 | if (!h->forced_local) |
c152c796 AM |
6156 | return TRUE; |
6157 | } | |
6158 | else | |
6159 | { | |
f5385ebf | 6160 | if (h->forced_local) |
c152c796 AM |
6161 | return TRUE; |
6162 | } | |
6163 | ||
6164 | bed = get_elf_backend_data (finfo->output_bfd); | |
6165 | ||
6166 | /* If we have an undefined symbol reference here then it must have | |
6167 | come from a shared library that is being linked in. (Undefined | |
6168 | references in regular files have already been handled). If we | |
6169 | are reporting errors for this situation then do so now. */ | |
6170 | if (h->root.type == bfd_link_hash_undefined | |
f5385ebf AM |
6171 | && h->ref_dynamic |
6172 | && !h->ref_regular | |
7e9f0867 | 6173 | && (elf_dyn_lib_class (h->root.u.undef.abfd) & DYN_AS_NEEDED) == 0 |
c152c796 AM |
6174 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h) |
6175 | && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
6176 | { | |
6177 | if (! ((*finfo->info->callbacks->undefined_symbol) | |
6178 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
6179 | NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) | |
6180 | { | |
6181 | eoinfo->failed = TRUE; | |
6182 | return FALSE; | |
6183 | } | |
6184 | } | |
6185 | ||
6186 | /* We should also warn if a forced local symbol is referenced from | |
6187 | shared libraries. */ | |
6188 | if (! finfo->info->relocatable | |
6189 | && (! finfo->info->shared) | |
f5385ebf AM |
6190 | && h->forced_local |
6191 | && h->ref_dynamic | |
6192 | && !h->dynamic_def | |
6193 | && !h->dynamic_weak | |
c152c796 AM |
6194 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) |
6195 | { | |
6196 | (*_bfd_error_handler) | |
d003868e AM |
6197 | (_("%B: %s symbol `%s' in %B is referenced by DSO"), |
6198 | finfo->output_bfd, h->root.u.def.section->owner, | |
c152c796 AM |
6199 | ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
6200 | ? "internal" | |
6201 | : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
d003868e AM |
6202 | ? "hidden" : "local", |
6203 | h->root.root.string); | |
c152c796 AM |
6204 | eoinfo->failed = TRUE; |
6205 | return FALSE; | |
6206 | } | |
6207 | ||
6208 | /* We don't want to output symbols that have never been mentioned by | |
6209 | a regular file, or that we have been told to strip. However, if | |
6210 | h->indx is set to -2, the symbol is used by a reloc and we must | |
6211 | output it. */ | |
6212 | if (h->indx == -2) | |
6213 | strip = FALSE; | |
f5385ebf AM |
6214 | else if ((h->def_dynamic |
6215 | || h->ref_dynamic) | |
6216 | && !h->def_regular | |
6217 | && !h->ref_regular) | |
c152c796 AM |
6218 | strip = TRUE; |
6219 | else if (finfo->info->strip == strip_all) | |
6220 | strip = TRUE; | |
6221 | else if (finfo->info->strip == strip_some | |
6222 | && bfd_hash_lookup (finfo->info->keep_hash, | |
6223 | h->root.root.string, FALSE, FALSE) == NULL) | |
6224 | strip = TRUE; | |
6225 | else if (finfo->info->strip_discarded | |
6226 | && (h->root.type == bfd_link_hash_defined | |
6227 | || h->root.type == bfd_link_hash_defweak) | |
6228 | && elf_discarded_section (h->root.u.def.section)) | |
6229 | strip = TRUE; | |
6230 | else | |
6231 | strip = FALSE; | |
6232 | ||
6233 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
6234 | nothing else to do unless it is a forced local symbol. */ | |
6235 | if (strip | |
6236 | && h->dynindx == -1 | |
f5385ebf | 6237 | && !h->forced_local) |
c152c796 AM |
6238 | return TRUE; |
6239 | ||
6240 | sym.st_value = 0; | |
6241 | sym.st_size = h->size; | |
6242 | sym.st_other = h->other; | |
f5385ebf | 6243 | if (h->forced_local) |
c152c796 AM |
6244 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); |
6245 | else if (h->root.type == bfd_link_hash_undefweak | |
6246 | || h->root.type == bfd_link_hash_defweak) | |
6247 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
6248 | else | |
6249 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
6250 | ||
6251 | switch (h->root.type) | |
6252 | { | |
6253 | default: | |
6254 | case bfd_link_hash_new: | |
6255 | case bfd_link_hash_warning: | |
6256 | abort (); | |
6257 | return FALSE; | |
6258 | ||
6259 | case bfd_link_hash_undefined: | |
6260 | case bfd_link_hash_undefweak: | |
6261 | input_sec = bfd_und_section_ptr; | |
6262 | sym.st_shndx = SHN_UNDEF; | |
6263 | break; | |
6264 | ||
6265 | case bfd_link_hash_defined: | |
6266 | case bfd_link_hash_defweak: | |
6267 | { | |
6268 | input_sec = h->root.u.def.section; | |
6269 | if (input_sec->output_section != NULL) | |
6270 | { | |
6271 | sym.st_shndx = | |
6272 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
6273 | input_sec->output_section); | |
6274 | if (sym.st_shndx == SHN_BAD) | |
6275 | { | |
6276 | (*_bfd_error_handler) | |
d003868e AM |
6277 | (_("%B: could not find output section %A for input section %A"), |
6278 | finfo->output_bfd, input_sec->output_section, input_sec); | |
c152c796 AM |
6279 | eoinfo->failed = TRUE; |
6280 | return FALSE; | |
6281 | } | |
6282 | ||
6283 | /* ELF symbols in relocatable files are section relative, | |
6284 | but in nonrelocatable files they are virtual | |
6285 | addresses. */ | |
6286 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
6287 | if (! finfo->info->relocatable) | |
6288 | { | |
6289 | sym.st_value += input_sec->output_section->vma; | |
6290 | if (h->type == STT_TLS) | |
6291 | { | |
6292 | /* STT_TLS symbols are relative to PT_TLS segment | |
6293 | base. */ | |
6294 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
6295 | sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
6296 | } | |
6297 | } | |
6298 | } | |
6299 | else | |
6300 | { | |
6301 | BFD_ASSERT (input_sec->owner == NULL | |
6302 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
6303 | sym.st_shndx = SHN_UNDEF; | |
6304 | input_sec = bfd_und_section_ptr; | |
6305 | } | |
6306 | } | |
6307 | break; | |
6308 | ||
6309 | case bfd_link_hash_common: | |
6310 | input_sec = h->root.u.c.p->section; | |
6311 | sym.st_shndx = SHN_COMMON; | |
6312 | sym.st_value = 1 << h->root.u.c.p->alignment_power; | |
6313 | break; | |
6314 | ||
6315 | case bfd_link_hash_indirect: | |
6316 | /* These symbols are created by symbol versioning. They point | |
6317 | to the decorated version of the name. For example, if the | |
6318 | symbol foo@@GNU_1.2 is the default, which should be used when | |
6319 | foo is used with no version, then we add an indirect symbol | |
6320 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
6321 | since the indirected symbol is already in the hash table. */ | |
6322 | return TRUE; | |
6323 | } | |
6324 | ||
6325 | /* Give the processor backend a chance to tweak the symbol value, | |
6326 | and also to finish up anything that needs to be done for this | |
6327 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
6328 | forced local syms when non-shared is due to a historical quirk. */ | |
6329 | if ((h->dynindx != -1 | |
f5385ebf | 6330 | || h->forced_local) |
c152c796 AM |
6331 | && ((finfo->info->shared |
6332 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
6333 | || h->root.type != bfd_link_hash_undefweak)) | |
f5385ebf | 6334 | || !h->forced_local) |
c152c796 AM |
6335 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
6336 | { | |
6337 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
6338 | (finfo->output_bfd, finfo->info, h, &sym))) | |
6339 | { | |
6340 | eoinfo->failed = TRUE; | |
6341 | return FALSE; | |
6342 | } | |
6343 | } | |
6344 | ||
6345 | /* If we are marking the symbol as undefined, and there are no | |
6346 | non-weak references to this symbol from a regular object, then | |
6347 | mark the symbol as weak undefined; if there are non-weak | |
6348 | references, mark the symbol as strong. We can't do this earlier, | |
6349 | because it might not be marked as undefined until the | |
6350 | finish_dynamic_symbol routine gets through with it. */ | |
6351 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 6352 | && h->ref_regular |
c152c796 AM |
6353 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
6354 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
6355 | { | |
6356 | int bindtype; | |
6357 | ||
f5385ebf | 6358 | if (h->ref_regular_nonweak) |
c152c796 AM |
6359 | bindtype = STB_GLOBAL; |
6360 | else | |
6361 | bindtype = STB_WEAK; | |
6362 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); | |
6363 | } | |
6364 | ||
6365 | /* If a non-weak symbol with non-default visibility is not defined | |
6366 | locally, it is a fatal error. */ | |
6367 | if (! finfo->info->relocatable | |
6368 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT | |
6369 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
6370 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 6371 | && !h->def_regular) |
c152c796 AM |
6372 | { |
6373 | (*_bfd_error_handler) | |
d003868e AM |
6374 | (_("%B: %s symbol `%s' isn't defined"), |
6375 | finfo->output_bfd, | |
6376 | ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED | |
6377 | ? "protected" | |
6378 | : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL | |
6379 | ? "internal" : "hidden", | |
6380 | h->root.root.string); | |
c152c796 AM |
6381 | eoinfo->failed = TRUE; |
6382 | return FALSE; | |
6383 | } | |
6384 | ||
6385 | /* If this symbol should be put in the .dynsym section, then put it | |
6386 | there now. We already know the symbol index. We also fill in | |
6387 | the entry in the .hash section. */ | |
6388 | if (h->dynindx != -1 | |
6389 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
6390 | { | |
6391 | size_t bucketcount; | |
6392 | size_t bucket; | |
6393 | size_t hash_entry_size; | |
6394 | bfd_byte *bucketpos; | |
6395 | bfd_vma chain; | |
6396 | bfd_byte *esym; | |
6397 | ||
6398 | sym.st_name = h->dynstr_index; | |
6399 | esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; | |
6400 | bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); | |
6401 | ||
6402 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
f6e332e6 | 6403 | bucket = h->u.elf_hash_value % bucketcount; |
c152c796 AM |
6404 | hash_entry_size |
6405 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; | |
6406 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents | |
6407 | + (bucket + 2) * hash_entry_size); | |
6408 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
6409 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); | |
6410 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, | |
6411 | ((bfd_byte *) finfo->hash_sec->contents | |
6412 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
6413 | ||
6414 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
6415 | { | |
6416 | Elf_Internal_Versym iversym; | |
6417 | Elf_External_Versym *eversym; | |
6418 | ||
f5385ebf | 6419 | if (!h->def_regular) |
c152c796 AM |
6420 | { |
6421 | if (h->verinfo.verdef == NULL) | |
6422 | iversym.vs_vers = 0; | |
6423 | else | |
6424 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
6425 | } | |
6426 | else | |
6427 | { | |
6428 | if (h->verinfo.vertree == NULL) | |
6429 | iversym.vs_vers = 1; | |
6430 | else | |
6431 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
3e3b46e5 PB |
6432 | if (finfo->info->create_default_symver) |
6433 | iversym.vs_vers++; | |
c152c796 AM |
6434 | } |
6435 | ||
f5385ebf | 6436 | if (h->hidden) |
c152c796 AM |
6437 | iversym.vs_vers |= VERSYM_HIDDEN; |
6438 | ||
6439 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; | |
6440 | eversym += h->dynindx; | |
6441 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
6442 | } | |
6443 | } | |
6444 | ||
6445 | /* If we're stripping it, then it was just a dynamic symbol, and | |
6446 | there's nothing else to do. */ | |
6447 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) | |
6448 | return TRUE; | |
6449 | ||
6450 | h->indx = bfd_get_symcount (finfo->output_bfd); | |
6451 | ||
6452 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h)) | |
6453 | { | |
6454 | eoinfo->failed = TRUE; | |
6455 | return FALSE; | |
6456 | } | |
6457 | ||
6458 | return TRUE; | |
6459 | } | |
6460 | ||
cdd3575c AM |
6461 | /* Return TRUE if special handling is done for relocs in SEC against |
6462 | symbols defined in discarded sections. */ | |
6463 | ||
c152c796 AM |
6464 | static bfd_boolean |
6465 | elf_section_ignore_discarded_relocs (asection *sec) | |
6466 | { | |
6467 | const struct elf_backend_data *bed; | |
6468 | ||
cdd3575c AM |
6469 | switch (sec->sec_info_type) |
6470 | { | |
6471 | case ELF_INFO_TYPE_STABS: | |
6472 | case ELF_INFO_TYPE_EH_FRAME: | |
6473 | return TRUE; | |
6474 | default: | |
6475 | break; | |
6476 | } | |
c152c796 AM |
6477 | |
6478 | bed = get_elf_backend_data (sec->owner); | |
6479 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
6480 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
6481 | return TRUE; | |
6482 | ||
6483 | return FALSE; | |
6484 | } | |
6485 | ||
9e66c942 AM |
6486 | enum action_discarded |
6487 | { | |
6488 | COMPLAIN = 1, | |
6489 | PRETEND = 2 | |
6490 | }; | |
6491 | ||
6492 | /* Return a mask saying how ld should treat relocations in SEC against | |
6493 | symbols defined in discarded sections. If this function returns | |
6494 | COMPLAIN set, ld will issue a warning message. If this function | |
6495 | returns PRETEND set, and the discarded section was link-once and the | |
6496 | same size as the kept link-once section, ld will pretend that the | |
6497 | symbol was actually defined in the kept section. Otherwise ld will | |
6498 | zero the reloc (at least that is the intent, but some cooperation by | |
6499 | the target dependent code is needed, particularly for REL targets). */ | |
6500 | ||
6501 | static unsigned int | |
6502 | elf_action_discarded (asection *sec) | |
cdd3575c | 6503 | { |
9e66c942 AM |
6504 | if (sec->flags & SEC_DEBUGGING) |
6505 | return PRETEND; | |
cdd3575c AM |
6506 | |
6507 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 6508 | return 0; |
cdd3575c AM |
6509 | |
6510 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 6511 | return 0; |
cdd3575c | 6512 | |
27b56da8 | 6513 | if (strcmp (".PARISC.unwind", sec->name) == 0) |
9e66c942 | 6514 | return 0; |
327c1315 AM |
6515 | |
6516 | if (strcmp (".fixup", sec->name) == 0) | |
9e66c942 | 6517 | return 0; |
27b56da8 | 6518 | |
9e66c942 | 6519 | return COMPLAIN | PRETEND; |
cdd3575c AM |
6520 | } |
6521 | ||
3d7f7666 L |
6522 | /* Find a match between a section and a member of a section group. */ |
6523 | ||
6524 | static asection * | |
6525 | match_group_member (asection *sec, asection *group) | |
6526 | { | |
6527 | asection *first = elf_next_in_group (group); | |
6528 | asection *s = first; | |
6529 | ||
6530 | while (s != NULL) | |
6531 | { | |
6532 | if (bfd_elf_match_symbols_in_sections (s, sec)) | |
6533 | return s; | |
6534 | ||
6535 | if (s == first) | |
6536 | break; | |
6537 | } | |
6538 | ||
6539 | return NULL; | |
6540 | } | |
6541 | ||
c152c796 AM |
6542 | /* Link an input file into the linker output file. This function |
6543 | handles all the sections and relocations of the input file at once. | |
6544 | This is so that we only have to read the local symbols once, and | |
6545 | don't have to keep them in memory. */ | |
6546 | ||
6547 | static bfd_boolean | |
6548 | elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) | |
6549 | { | |
6550 | bfd_boolean (*relocate_section) | |
6551 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
6552 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
6553 | bfd *output_bfd; | |
6554 | Elf_Internal_Shdr *symtab_hdr; | |
6555 | size_t locsymcount; | |
6556 | size_t extsymoff; | |
6557 | Elf_Internal_Sym *isymbuf; | |
6558 | Elf_Internal_Sym *isym; | |
6559 | Elf_Internal_Sym *isymend; | |
6560 | long *pindex; | |
6561 | asection **ppsection; | |
6562 | asection *o; | |
6563 | const struct elf_backend_data *bed; | |
6564 | bfd_boolean emit_relocs; | |
6565 | struct elf_link_hash_entry **sym_hashes; | |
6566 | ||
6567 | output_bfd = finfo->output_bfd; | |
6568 | bed = get_elf_backend_data (output_bfd); | |
6569 | relocate_section = bed->elf_backend_relocate_section; | |
6570 | ||
6571 | /* If this is a dynamic object, we don't want to do anything here: | |
6572 | we don't want the local symbols, and we don't want the section | |
6573 | contents. */ | |
6574 | if ((input_bfd->flags & DYNAMIC) != 0) | |
6575 | return TRUE; | |
6576 | ||
6577 | emit_relocs = (finfo->info->relocatable | |
6578 | || finfo->info->emitrelocations | |
6579 | || bed->elf_backend_emit_relocs); | |
6580 | ||
6581 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
6582 | if (elf_bad_symtab (input_bfd)) | |
6583 | { | |
6584 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
6585 | extsymoff = 0; | |
6586 | } | |
6587 | else | |
6588 | { | |
6589 | locsymcount = symtab_hdr->sh_info; | |
6590 | extsymoff = symtab_hdr->sh_info; | |
6591 | } | |
6592 | ||
6593 | /* Read the local symbols. */ | |
6594 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
6595 | if (isymbuf == NULL && locsymcount != 0) | |
6596 | { | |
6597 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
6598 | finfo->internal_syms, | |
6599 | finfo->external_syms, | |
6600 | finfo->locsym_shndx); | |
6601 | if (isymbuf == NULL) | |
6602 | return FALSE; | |
6603 | } | |
6604 | ||
6605 | /* Find local symbol sections and adjust values of symbols in | |
6606 | SEC_MERGE sections. Write out those local symbols we know are | |
6607 | going into the output file. */ | |
6608 | isymend = isymbuf + locsymcount; | |
6609 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
6610 | isym < isymend; | |
6611 | isym++, pindex++, ppsection++) | |
6612 | { | |
6613 | asection *isec; | |
6614 | const char *name; | |
6615 | Elf_Internal_Sym osym; | |
6616 | ||
6617 | *pindex = -1; | |
6618 | ||
6619 | if (elf_bad_symtab (input_bfd)) | |
6620 | { | |
6621 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
6622 | { | |
6623 | *ppsection = NULL; | |
6624 | continue; | |
6625 | } | |
6626 | } | |
6627 | ||
6628 | if (isym->st_shndx == SHN_UNDEF) | |
6629 | isec = bfd_und_section_ptr; | |
6630 | else if (isym->st_shndx < SHN_LORESERVE | |
6631 | || isym->st_shndx > SHN_HIRESERVE) | |
6632 | { | |
6633 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); | |
6634 | if (isec | |
6635 | && isec->sec_info_type == ELF_INFO_TYPE_MERGE | |
6636 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) | |
6637 | isym->st_value = | |
6638 | _bfd_merged_section_offset (output_bfd, &isec, | |
6639 | elf_section_data (isec)->sec_info, | |
753731ee | 6640 | isym->st_value); |
c152c796 AM |
6641 | } |
6642 | else if (isym->st_shndx == SHN_ABS) | |
6643 | isec = bfd_abs_section_ptr; | |
6644 | else if (isym->st_shndx == SHN_COMMON) | |
6645 | isec = bfd_com_section_ptr; | |
6646 | else | |
6647 | { | |
6648 | /* Who knows? */ | |
6649 | isec = NULL; | |
6650 | } | |
6651 | ||
6652 | *ppsection = isec; | |
6653 | ||
6654 | /* Don't output the first, undefined, symbol. */ | |
6655 | if (ppsection == finfo->sections) | |
6656 | continue; | |
6657 | ||
6658 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
6659 | { | |
6660 | /* We never output section symbols. Instead, we use the | |
6661 | section symbol of the corresponding section in the output | |
6662 | file. */ | |
6663 | continue; | |
6664 | } | |
6665 | ||
6666 | /* If we are stripping all symbols, we don't want to output this | |
6667 | one. */ | |
6668 | if (finfo->info->strip == strip_all) | |
6669 | continue; | |
6670 | ||
6671 | /* If we are discarding all local symbols, we don't want to | |
6672 | output this one. If we are generating a relocatable output | |
6673 | file, then some of the local symbols may be required by | |
6674 | relocs; we output them below as we discover that they are | |
6675 | needed. */ | |
6676 | if (finfo->info->discard == discard_all) | |
6677 | continue; | |
6678 | ||
6679 | /* If this symbol is defined in a section which we are | |
6680 | discarding, we don't need to keep it, but note that | |
6681 | linker_mark is only reliable for sections that have contents. | |
6682 | For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE | |
6683 | as well as linker_mark. */ | |
6684 | if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
6685 | && isec != NULL | |
6686 | && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0) | |
6687 | || (! finfo->info->relocatable | |
6688 | && (isec->flags & SEC_EXCLUDE) != 0))) | |
6689 | continue; | |
6690 | ||
6691 | /* Get the name of the symbol. */ | |
6692 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
6693 | isym->st_name); | |
6694 | if (name == NULL) | |
6695 | return FALSE; | |
6696 | ||
6697 | /* See if we are discarding symbols with this name. */ | |
6698 | if ((finfo->info->strip == strip_some | |
6699 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) | |
6700 | == NULL)) | |
6701 | || (((finfo->info->discard == discard_sec_merge | |
6702 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) | |
6703 | || finfo->info->discard == discard_l) | |
6704 | && bfd_is_local_label_name (input_bfd, name))) | |
6705 | continue; | |
6706 | ||
6707 | /* If we get here, we are going to output this symbol. */ | |
6708 | ||
6709 | osym = *isym; | |
6710 | ||
6711 | /* Adjust the section index for the output file. */ | |
6712 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
6713 | isec->output_section); | |
6714 | if (osym.st_shndx == SHN_BAD) | |
6715 | return FALSE; | |
6716 | ||
6717 | *pindex = bfd_get_symcount (output_bfd); | |
6718 | ||
6719 | /* ELF symbols in relocatable files are section relative, but | |
6720 | in executable files they are virtual addresses. Note that | |
6721 | this code assumes that all ELF sections have an associated | |
6722 | BFD section with a reasonable value for output_offset; below | |
6723 | we assume that they also have a reasonable value for | |
6724 | output_section. Any special sections must be set up to meet | |
6725 | these requirements. */ | |
6726 | osym.st_value += isec->output_offset; | |
6727 | if (! finfo->info->relocatable) | |
6728 | { | |
6729 | osym.st_value += isec->output_section->vma; | |
6730 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
6731 | { | |
6732 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
6733 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
6734 | osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
6735 | } | |
6736 | } | |
6737 | ||
6738 | if (! elf_link_output_sym (finfo, name, &osym, isec, NULL)) | |
6739 | return FALSE; | |
6740 | } | |
6741 | ||
6742 | /* Relocate the contents of each section. */ | |
6743 | sym_hashes = elf_sym_hashes (input_bfd); | |
6744 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
6745 | { | |
6746 | bfd_byte *contents; | |
6747 | ||
6748 | if (! o->linker_mark) | |
6749 | { | |
6750 | /* This section was omitted from the link. */ | |
6751 | continue; | |
6752 | } | |
6753 | ||
6754 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 6755 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
6756 | continue; |
6757 | ||
6758 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
6759 | { | |
6760 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
6761 | or somesuch. */ | |
6762 | continue; | |
6763 | } | |
6764 | ||
6765 | /* Get the contents of the section. They have been cached by a | |
6766 | relaxation routine. Note that o is a section in an input | |
6767 | file, so the contents field will not have been set by any of | |
6768 | the routines which work on output files. */ | |
6769 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
6770 | contents = elf_section_data (o)->this_hdr.contents; | |
6771 | else | |
6772 | { | |
eea6121a AM |
6773 | bfd_size_type amt = o->rawsize ? o->rawsize : o->size; |
6774 | ||
c152c796 | 6775 | contents = finfo->contents; |
eea6121a | 6776 | if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt)) |
c152c796 AM |
6777 | return FALSE; |
6778 | } | |
6779 | ||
6780 | if ((o->flags & SEC_RELOC) != 0) | |
6781 | { | |
6782 | Elf_Internal_Rela *internal_relocs; | |
6783 | bfd_vma r_type_mask; | |
6784 | int r_sym_shift; | |
6785 | ||
6786 | /* Get the swapped relocs. */ | |
6787 | internal_relocs | |
6788 | = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, | |
6789 | finfo->internal_relocs, FALSE); | |
6790 | if (internal_relocs == NULL | |
6791 | && o->reloc_count > 0) | |
6792 | return FALSE; | |
6793 | ||
6794 | if (bed->s->arch_size == 32) | |
6795 | { | |
6796 | r_type_mask = 0xff; | |
6797 | r_sym_shift = 8; | |
6798 | } | |
6799 | else | |
6800 | { | |
6801 | r_type_mask = 0xffffffff; | |
6802 | r_sym_shift = 32; | |
6803 | } | |
6804 | ||
6805 | /* Run through the relocs looking for any against symbols | |
6806 | from discarded sections and section symbols from | |
6807 | removed link-once sections. Complain about relocs | |
6808 | against discarded sections. Zero relocs against removed | |
6809 | link-once sections. Preserve debug information as much | |
6810 | as we can. */ | |
6811 | if (!elf_section_ignore_discarded_relocs (o)) | |
6812 | { | |
6813 | Elf_Internal_Rela *rel, *relend; | |
9e66c942 | 6814 | unsigned int action = elf_action_discarded (o); |
c152c796 AM |
6815 | |
6816 | rel = internal_relocs; | |
6817 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
6818 | for ( ; rel < relend; rel++) | |
6819 | { | |
6820 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
cdd3575c AM |
6821 | asection **ps, *sec; |
6822 | struct elf_link_hash_entry *h = NULL; | |
6823 | const char *sym_name; | |
c152c796 | 6824 | |
ee75fd95 AM |
6825 | if (r_symndx == STN_UNDEF) |
6826 | continue; | |
6827 | ||
c152c796 AM |
6828 | if (r_symndx >= locsymcount |
6829 | || (elf_bad_symtab (input_bfd) | |
6830 | && finfo->sections[r_symndx] == NULL)) | |
6831 | { | |
c152c796 AM |
6832 | h = sym_hashes[r_symndx - extsymoff]; |
6833 | while (h->root.type == bfd_link_hash_indirect | |
6834 | || h->root.type == bfd_link_hash_warning) | |
6835 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6836 | ||
cdd3575c AM |
6837 | if (h->root.type != bfd_link_hash_defined |
6838 | && h->root.type != bfd_link_hash_defweak) | |
6839 | continue; | |
6840 | ||
6841 | ps = &h->root.u.def.section; | |
6842 | sym_name = h->root.root.string; | |
c152c796 AM |
6843 | } |
6844 | else | |
6845 | { | |
cdd3575c AM |
6846 | Elf_Internal_Sym *sym = isymbuf + r_symndx; |
6847 | ps = &finfo->sections[r_symndx]; | |
be8dd2ca | 6848 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym); |
cdd3575c | 6849 | } |
c152c796 | 6850 | |
cdd3575c AM |
6851 | /* Complain if the definition comes from a |
6852 | discarded section. */ | |
6853 | if ((sec = *ps) != NULL && elf_discarded_section (sec)) | |
6854 | { | |
87e5235d | 6855 | asection *kept; |
3d7f7666 | 6856 | |
87e5235d | 6857 | BFD_ASSERT (r_symndx != 0); |
9e66c942 | 6858 | if (action & COMPLAIN) |
cdd3575c | 6859 | { |
d003868e AM |
6860 | (*_bfd_error_handler) |
6861 | (_("`%s' referenced in section `%A' of %B: " | |
6862 | "defined in discarded section `%A' of %B\n"), | |
6863 | o, input_bfd, sec, sec->owner, sym_name); | |
cdd3575c AM |
6864 | } |
6865 | ||
87e5235d AM |
6866 | /* Try to do the best we can to support buggy old |
6867 | versions of gcc. If we've warned, or this is | |
6868 | debugging info, pretend that the symbol is | |
6869 | really defined in the kept linkonce section. | |
6870 | FIXME: This is quite broken. Modifying the | |
6871 | symbol here means we will be changing all later | |
6872 | uses of the symbol, not just in this section. | |
6873 | The only thing that makes this half reasonable | |
6874 | is that we warn in non-debug sections, and | |
6875 | debug sections tend to come after other | |
6876 | sections. */ | |
6877 | kept = sec->kept_section; | |
9e66c942 | 6878 | if (kept != NULL && (action & PRETEND)) |
87e5235d AM |
6879 | { |
6880 | if (elf_sec_group (sec) != NULL) | |
6881 | kept = match_group_member (sec, kept); | |
6882 | if (kept != NULL | |
6883 | && sec->size == kept->size) | |
6884 | { | |
6885 | *ps = kept; | |
6886 | continue; | |
6887 | } | |
6888 | } | |
6889 | ||
cdd3575c AM |
6890 | /* Remove the symbol reference from the reloc, but |
6891 | don't kill the reloc completely. This is so that | |
6892 | a zero value will be written into the section, | |
6893 | which may have non-zero contents put there by the | |
6894 | assembler. Zero in things like an eh_frame fde | |
6895 | pc_begin allows stack unwinders to recognize the | |
6896 | fde as bogus. */ | |
6897 | rel->r_info &= r_type_mask; | |
6898 | rel->r_addend = 0; | |
c152c796 AM |
6899 | } |
6900 | } | |
6901 | } | |
6902 | ||
6903 | /* Relocate the section by invoking a back end routine. | |
6904 | ||
6905 | The back end routine is responsible for adjusting the | |
6906 | section contents as necessary, and (if using Rela relocs | |
6907 | and generating a relocatable output file) adjusting the | |
6908 | reloc addend as necessary. | |
6909 | ||
6910 | The back end routine does not have to worry about setting | |
6911 | the reloc address or the reloc symbol index. | |
6912 | ||
6913 | The back end routine is given a pointer to the swapped in | |
6914 | internal symbols, and can access the hash table entries | |
6915 | for the external symbols via elf_sym_hashes (input_bfd). | |
6916 | ||
6917 | When generating relocatable output, the back end routine | |
6918 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
6919 | output symbol is going to be a section symbol | |
6920 | corresponding to the output section, which will require | |
6921 | the addend to be adjusted. */ | |
6922 | ||
6923 | if (! (*relocate_section) (output_bfd, finfo->info, | |
6924 | input_bfd, o, contents, | |
6925 | internal_relocs, | |
6926 | isymbuf, | |
6927 | finfo->sections)) | |
6928 | return FALSE; | |
6929 | ||
6930 | if (emit_relocs) | |
6931 | { | |
6932 | Elf_Internal_Rela *irela; | |
6933 | Elf_Internal_Rela *irelaend; | |
6934 | bfd_vma last_offset; | |
6935 | struct elf_link_hash_entry **rel_hash; | |
6936 | Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2; | |
6937 | unsigned int next_erel; | |
6938 | bfd_boolean (*reloc_emitter) | |
6939 | (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *); | |
6940 | bfd_boolean rela_normal; | |
6941 | ||
6942 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
6943 | rela_normal = (bed->rela_normal | |
6944 | && (input_rel_hdr->sh_entsize | |
6945 | == bed->s->sizeof_rela)); | |
6946 | ||
6947 | /* Adjust the reloc addresses and symbol indices. */ | |
6948 | ||
6949 | irela = internal_relocs; | |
6950 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
6951 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
6952 | + elf_section_data (o->output_section)->rel_count | |
6953 | + elf_section_data (o->output_section)->rel_count2); | |
6954 | last_offset = o->output_offset; | |
6955 | if (!finfo->info->relocatable) | |
6956 | last_offset += o->output_section->vma; | |
6957 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
6958 | { | |
6959 | unsigned long r_symndx; | |
6960 | asection *sec; | |
6961 | Elf_Internal_Sym sym; | |
6962 | ||
6963 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
6964 | { | |
6965 | rel_hash++; | |
6966 | next_erel = 0; | |
6967 | } | |
6968 | ||
6969 | irela->r_offset = _bfd_elf_section_offset (output_bfd, | |
6970 | finfo->info, o, | |
6971 | irela->r_offset); | |
6972 | if (irela->r_offset >= (bfd_vma) -2) | |
6973 | { | |
6974 | /* This is a reloc for a deleted entry or somesuch. | |
6975 | Turn it into an R_*_NONE reloc, at the same | |
6976 | offset as the last reloc. elf_eh_frame.c and | |
6977 | elf_bfd_discard_info rely on reloc offsets | |
6978 | being ordered. */ | |
6979 | irela->r_offset = last_offset; | |
6980 | irela->r_info = 0; | |
6981 | irela->r_addend = 0; | |
6982 | continue; | |
6983 | } | |
6984 | ||
6985 | irela->r_offset += o->output_offset; | |
6986 | ||
6987 | /* Relocs in an executable have to be virtual addresses. */ | |
6988 | if (!finfo->info->relocatable) | |
6989 | irela->r_offset += o->output_section->vma; | |
6990 | ||
6991 | last_offset = irela->r_offset; | |
6992 | ||
6993 | r_symndx = irela->r_info >> r_sym_shift; | |
6994 | if (r_symndx == STN_UNDEF) | |
6995 | continue; | |
6996 | ||
6997 | if (r_symndx >= locsymcount | |
6998 | || (elf_bad_symtab (input_bfd) | |
6999 | && finfo->sections[r_symndx] == NULL)) | |
7000 | { | |
7001 | struct elf_link_hash_entry *rh; | |
7002 | unsigned long indx; | |
7003 | ||
7004 | /* This is a reloc against a global symbol. We | |
7005 | have not yet output all the local symbols, so | |
7006 | we do not know the symbol index of any global | |
7007 | symbol. We set the rel_hash entry for this | |
7008 | reloc to point to the global hash table entry | |
7009 | for this symbol. The symbol index is then | |
ee75fd95 | 7010 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
7011 | indx = r_symndx - extsymoff; |
7012 | rh = elf_sym_hashes (input_bfd)[indx]; | |
7013 | while (rh->root.type == bfd_link_hash_indirect | |
7014 | || rh->root.type == bfd_link_hash_warning) | |
7015 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
7016 | ||
7017 | /* Setting the index to -2 tells | |
7018 | elf_link_output_extsym that this symbol is | |
7019 | used by a reloc. */ | |
7020 | BFD_ASSERT (rh->indx < 0); | |
7021 | rh->indx = -2; | |
7022 | ||
7023 | *rel_hash = rh; | |
7024 | ||
7025 | continue; | |
7026 | } | |
7027 | ||
7028 | /* This is a reloc against a local symbol. */ | |
7029 | ||
7030 | *rel_hash = NULL; | |
7031 | sym = isymbuf[r_symndx]; | |
7032 | sec = finfo->sections[r_symndx]; | |
7033 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) | |
7034 | { | |
7035 | /* I suppose the backend ought to fill in the | |
7036 | section of any STT_SECTION symbol against a | |
6a8d1586 AM |
7037 | processor specific section. */ |
7038 | r_symndx = 0; | |
7039 | if (bfd_is_abs_section (sec)) | |
7040 | ; | |
c152c796 AM |
7041 | else if (sec == NULL || sec->owner == NULL) |
7042 | { | |
7043 | bfd_set_error (bfd_error_bad_value); | |
7044 | return FALSE; | |
7045 | } | |
7046 | else | |
7047 | { | |
6a8d1586 AM |
7048 | asection *osec = sec->output_section; |
7049 | ||
7050 | /* If we have discarded a section, the output | |
7051 | section will be the absolute section. In | |
7052 | case of discarded link-once and discarded | |
7053 | SEC_MERGE sections, use the kept section. */ | |
7054 | if (bfd_is_abs_section (osec) | |
7055 | && sec->kept_section != NULL | |
7056 | && sec->kept_section->output_section != NULL) | |
7057 | { | |
7058 | osec = sec->kept_section->output_section; | |
7059 | irela->r_addend -= osec->vma; | |
7060 | } | |
7061 | ||
7062 | if (!bfd_is_abs_section (osec)) | |
7063 | { | |
7064 | r_symndx = osec->target_index; | |
7065 | BFD_ASSERT (r_symndx != 0); | |
7066 | } | |
c152c796 AM |
7067 | } |
7068 | ||
7069 | /* Adjust the addend according to where the | |
7070 | section winds up in the output section. */ | |
7071 | if (rela_normal) | |
7072 | irela->r_addend += sec->output_offset; | |
7073 | } | |
7074 | else | |
7075 | { | |
7076 | if (finfo->indices[r_symndx] == -1) | |
7077 | { | |
7078 | unsigned long shlink; | |
7079 | const char *name; | |
7080 | asection *osec; | |
7081 | ||
7082 | if (finfo->info->strip == strip_all) | |
7083 | { | |
7084 | /* You can't do ld -r -s. */ | |
7085 | bfd_set_error (bfd_error_invalid_operation); | |
7086 | return FALSE; | |
7087 | } | |
7088 | ||
7089 | /* This symbol was skipped earlier, but | |
7090 | since it is needed by a reloc, we | |
7091 | must output it now. */ | |
7092 | shlink = symtab_hdr->sh_link; | |
7093 | name = (bfd_elf_string_from_elf_section | |
7094 | (input_bfd, shlink, sym.st_name)); | |
7095 | if (name == NULL) | |
7096 | return FALSE; | |
7097 | ||
7098 | osec = sec->output_section; | |
7099 | sym.st_shndx = | |
7100 | _bfd_elf_section_from_bfd_section (output_bfd, | |
7101 | osec); | |
7102 | if (sym.st_shndx == SHN_BAD) | |
7103 | return FALSE; | |
7104 | ||
7105 | sym.st_value += sec->output_offset; | |
7106 | if (! finfo->info->relocatable) | |
7107 | { | |
7108 | sym.st_value += osec->vma; | |
7109 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
7110 | { | |
7111 | /* STT_TLS symbols are relative to PT_TLS | |
7112 | segment base. */ | |
7113 | BFD_ASSERT (elf_hash_table (finfo->info) | |
7114 | ->tls_sec != NULL); | |
7115 | sym.st_value -= (elf_hash_table (finfo->info) | |
7116 | ->tls_sec->vma); | |
7117 | } | |
7118 | } | |
7119 | ||
7120 | finfo->indices[r_symndx] | |
7121 | = bfd_get_symcount (output_bfd); | |
7122 | ||
7123 | if (! elf_link_output_sym (finfo, name, &sym, sec, | |
7124 | NULL)) | |
7125 | return FALSE; | |
7126 | } | |
7127 | ||
7128 | r_symndx = finfo->indices[r_symndx]; | |
7129 | } | |
7130 | ||
7131 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
7132 | | (irela->r_info & r_type_mask)); | |
7133 | } | |
7134 | ||
7135 | /* Swap out the relocs. */ | |
7136 | if (bed->elf_backend_emit_relocs | |
7137 | && !(finfo->info->relocatable | |
7138 | || finfo->info->emitrelocations)) | |
7139 | reloc_emitter = bed->elf_backend_emit_relocs; | |
7140 | else | |
7141 | reloc_emitter = _bfd_elf_link_output_relocs; | |
7142 | ||
7143 | if (input_rel_hdr->sh_size != 0 | |
7144 | && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr, | |
7145 | internal_relocs)) | |
7146 | return FALSE; | |
7147 | ||
7148 | input_rel_hdr2 = elf_section_data (o)->rel_hdr2; | |
7149 | if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0) | |
7150 | { | |
7151 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) | |
7152 | * bed->s->int_rels_per_ext_rel); | |
7153 | if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2, | |
7154 | internal_relocs)) | |
7155 | return FALSE; | |
7156 | } | |
7157 | } | |
7158 | } | |
7159 | ||
7160 | /* Write out the modified section contents. */ | |
7161 | if (bed->elf_backend_write_section | |
7162 | && (*bed->elf_backend_write_section) (output_bfd, o, contents)) | |
7163 | { | |
7164 | /* Section written out. */ | |
7165 | } | |
7166 | else switch (o->sec_info_type) | |
7167 | { | |
7168 | case ELF_INFO_TYPE_STABS: | |
7169 | if (! (_bfd_write_section_stabs | |
7170 | (output_bfd, | |
7171 | &elf_hash_table (finfo->info)->stab_info, | |
7172 | o, &elf_section_data (o)->sec_info, contents))) | |
7173 | return FALSE; | |
7174 | break; | |
7175 | case ELF_INFO_TYPE_MERGE: | |
7176 | if (! _bfd_write_merged_section (output_bfd, o, | |
7177 | elf_section_data (o)->sec_info)) | |
7178 | return FALSE; | |
7179 | break; | |
7180 | case ELF_INFO_TYPE_EH_FRAME: | |
7181 | { | |
7182 | if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, | |
7183 | o, contents)) | |
7184 | return FALSE; | |
7185 | } | |
7186 | break; | |
7187 | default: | |
7188 | { | |
c152c796 AM |
7189 | if (! (o->flags & SEC_EXCLUDE) |
7190 | && ! bfd_set_section_contents (output_bfd, o->output_section, | |
7191 | contents, | |
7192 | (file_ptr) o->output_offset, | |
eea6121a | 7193 | o->size)) |
c152c796 AM |
7194 | return FALSE; |
7195 | } | |
7196 | break; | |
7197 | } | |
7198 | } | |
7199 | ||
7200 | return TRUE; | |
7201 | } | |
7202 | ||
7203 | /* Generate a reloc when linking an ELF file. This is a reloc | |
7204 | requested by the linker, and does come from any input file. This | |
7205 | is used to build constructor and destructor tables when linking | |
7206 | with -Ur. */ | |
7207 | ||
7208 | static bfd_boolean | |
7209 | elf_reloc_link_order (bfd *output_bfd, | |
7210 | struct bfd_link_info *info, | |
7211 | asection *output_section, | |
7212 | struct bfd_link_order *link_order) | |
7213 | { | |
7214 | reloc_howto_type *howto; | |
7215 | long indx; | |
7216 | bfd_vma offset; | |
7217 | bfd_vma addend; | |
7218 | struct elf_link_hash_entry **rel_hash_ptr; | |
7219 | Elf_Internal_Shdr *rel_hdr; | |
7220 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
7221 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
7222 | bfd_byte *erel; | |
7223 | unsigned int i; | |
7224 | ||
7225 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
7226 | if (howto == NULL) | |
7227 | { | |
7228 | bfd_set_error (bfd_error_bad_value); | |
7229 | return FALSE; | |
7230 | } | |
7231 | ||
7232 | addend = link_order->u.reloc.p->addend; | |
7233 | ||
7234 | /* Figure out the symbol index. */ | |
7235 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
7236 | + elf_section_data (output_section)->rel_count | |
7237 | + elf_section_data (output_section)->rel_count2); | |
7238 | if (link_order->type == bfd_section_reloc_link_order) | |
7239 | { | |
7240 | indx = link_order->u.reloc.p->u.section->target_index; | |
7241 | BFD_ASSERT (indx != 0); | |
7242 | *rel_hash_ptr = NULL; | |
7243 | } | |
7244 | else | |
7245 | { | |
7246 | struct elf_link_hash_entry *h; | |
7247 | ||
7248 | /* Treat a reloc against a defined symbol as though it were | |
7249 | actually against the section. */ | |
7250 | h = ((struct elf_link_hash_entry *) | |
7251 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
7252 | link_order->u.reloc.p->u.name, | |
7253 | FALSE, FALSE, TRUE)); | |
7254 | if (h != NULL | |
7255 | && (h->root.type == bfd_link_hash_defined | |
7256 | || h->root.type == bfd_link_hash_defweak)) | |
7257 | { | |
7258 | asection *section; | |
7259 | ||
7260 | section = h->root.u.def.section; | |
7261 | indx = section->output_section->target_index; | |
7262 | *rel_hash_ptr = NULL; | |
7263 | /* It seems that we ought to add the symbol value to the | |
7264 | addend here, but in practice it has already been added | |
7265 | because it was passed to constructor_callback. */ | |
7266 | addend += section->output_section->vma + section->output_offset; | |
7267 | } | |
7268 | else if (h != NULL) | |
7269 | { | |
7270 | /* Setting the index to -2 tells elf_link_output_extsym that | |
7271 | this symbol is used by a reloc. */ | |
7272 | h->indx = -2; | |
7273 | *rel_hash_ptr = h; | |
7274 | indx = 0; | |
7275 | } | |
7276 | else | |
7277 | { | |
7278 | if (! ((*info->callbacks->unattached_reloc) | |
7279 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) | |
7280 | return FALSE; | |
7281 | indx = 0; | |
7282 | } | |
7283 | } | |
7284 | ||
7285 | /* If this is an inplace reloc, we must write the addend into the | |
7286 | object file. */ | |
7287 | if (howto->partial_inplace && addend != 0) | |
7288 | { | |
7289 | bfd_size_type size; | |
7290 | bfd_reloc_status_type rstat; | |
7291 | bfd_byte *buf; | |
7292 | bfd_boolean ok; | |
7293 | const char *sym_name; | |
7294 | ||
7295 | size = bfd_get_reloc_size (howto); | |
7296 | buf = bfd_zmalloc (size); | |
7297 | if (buf == NULL) | |
7298 | return FALSE; | |
7299 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
7300 | switch (rstat) | |
7301 | { | |
7302 | case bfd_reloc_ok: | |
7303 | break; | |
7304 | ||
7305 | default: | |
7306 | case bfd_reloc_outofrange: | |
7307 | abort (); | |
7308 | ||
7309 | case bfd_reloc_overflow: | |
7310 | if (link_order->type == bfd_section_reloc_link_order) | |
7311 | sym_name = bfd_section_name (output_bfd, | |
7312 | link_order->u.reloc.p->u.section); | |
7313 | else | |
7314 | sym_name = link_order->u.reloc.p->u.name; | |
7315 | if (! ((*info->callbacks->reloc_overflow) | |
dfeffb9f L |
7316 | (info, NULL, sym_name, howto->name, addend, NULL, |
7317 | NULL, (bfd_vma) 0))) | |
c152c796 AM |
7318 | { |
7319 | free (buf); | |
7320 | return FALSE; | |
7321 | } | |
7322 | break; | |
7323 | } | |
7324 | ok = bfd_set_section_contents (output_bfd, output_section, buf, | |
7325 | link_order->offset, size); | |
7326 | free (buf); | |
7327 | if (! ok) | |
7328 | return FALSE; | |
7329 | } | |
7330 | ||
7331 | /* The address of a reloc is relative to the section in a | |
7332 | relocatable file, and is a virtual address in an executable | |
7333 | file. */ | |
7334 | offset = link_order->offset; | |
7335 | if (! info->relocatable) | |
7336 | offset += output_section->vma; | |
7337 | ||
7338 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
7339 | { | |
7340 | irel[i].r_offset = offset; | |
7341 | irel[i].r_info = 0; | |
7342 | irel[i].r_addend = 0; | |
7343 | } | |
7344 | if (bed->s->arch_size == 32) | |
7345 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
7346 | else | |
7347 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
7348 | ||
7349 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
7350 | erel = rel_hdr->contents; | |
7351 | if (rel_hdr->sh_type == SHT_REL) | |
7352 | { | |
7353 | erel += (elf_section_data (output_section)->rel_count | |
7354 | * bed->s->sizeof_rel); | |
7355 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); | |
7356 | } | |
7357 | else | |
7358 | { | |
7359 | irel[0].r_addend = addend; | |
7360 | erel += (elf_section_data (output_section)->rel_count | |
7361 | * bed->s->sizeof_rela); | |
7362 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); | |
7363 | } | |
7364 | ||
7365 | ++elf_section_data (output_section)->rel_count; | |
7366 | ||
7367 | return TRUE; | |
7368 | } | |
7369 | ||
0b52efa6 PB |
7370 | |
7371 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
7372 | ||
7373 | static bfd_vma | |
7374 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
7375 | { | |
7376 | Elf_Internal_Shdr **elf_shdrp; | |
7377 | asection *s; | |
7378 | int elfsec; | |
7379 | ||
7380 | s = p->u.indirect.section; | |
7381 | elf_shdrp = elf_elfsections (s->owner); | |
7382 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
7383 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
7384 | /* PR 290: |
7385 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
7386 | SHF_LINK_ORDER. But it doesn't set theh sh_link or | |
7387 | sh_info fields. Hence we could get the situation | |
7388 | where elfsec is 0. */ | |
7389 | if (elfsec == 0) | |
7390 | { | |
7391 | const struct elf_backend_data *bed | |
7392 | = get_elf_backend_data (s->owner); | |
7393 | if (bed->link_order_error_handler) | |
d003868e AM |
7394 | bed->link_order_error_handler |
7395 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); | |
185d09ad L |
7396 | return 0; |
7397 | } | |
7398 | else | |
7399 | { | |
7400 | s = elf_shdrp[elfsec]->bfd_section; | |
7401 | return s->output_section->vma + s->output_offset; | |
7402 | } | |
0b52efa6 PB |
7403 | } |
7404 | ||
7405 | ||
7406 | /* Compare two sections based on the locations of the sections they are | |
7407 | linked to. Used by elf_fixup_link_order. */ | |
7408 | ||
7409 | static int | |
7410 | compare_link_order (const void * a, const void * b) | |
7411 | { | |
7412 | bfd_vma apos; | |
7413 | bfd_vma bpos; | |
7414 | ||
7415 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
7416 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
7417 | if (apos < bpos) | |
7418 | return -1; | |
7419 | return apos > bpos; | |
7420 | } | |
7421 | ||
7422 | ||
7423 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
7424 | order as their linked sections. Returns false if this could not be done | |
7425 | because an output section includes both ordered and unordered | |
7426 | sections. Ideally we'd do this in the linker proper. */ | |
7427 | ||
7428 | static bfd_boolean | |
7429 | elf_fixup_link_order (bfd *abfd, asection *o) | |
7430 | { | |
7431 | int seen_linkorder; | |
7432 | int seen_other; | |
7433 | int n; | |
7434 | struct bfd_link_order *p; | |
7435 | bfd *sub; | |
7436 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7437 | int elfsec; | |
7438 | struct bfd_link_order **sections; | |
7439 | asection *s; | |
7440 | bfd_vma offset; | |
7441 | ||
7442 | seen_other = 0; | |
7443 | seen_linkorder = 0; | |
7444 | for (p = o->link_order_head; p != NULL; p = p->next) | |
7445 | { | |
7446 | if (p->type == bfd_indirect_link_order | |
7447 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
7448 | == bfd_target_elf_flavour) | |
7449 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
7450 | { | |
7451 | s = p->u.indirect.section; | |
7452 | elfsec = _bfd_elf_section_from_bfd_section (sub, s); | |
7453 | if (elfsec != -1 | |
7454 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER) | |
7455 | seen_linkorder++; | |
7456 | else | |
7457 | seen_other++; | |
7458 | } | |
7459 | else | |
7460 | seen_other++; | |
7461 | } | |
7462 | ||
7463 | if (!seen_linkorder) | |
7464 | return TRUE; | |
7465 | ||
7466 | if (seen_other && seen_linkorder) | |
08ccf96b | 7467 | { |
d003868e AM |
7468 | (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), |
7469 | o); | |
08ccf96b L |
7470 | bfd_set_error (bfd_error_bad_value); |
7471 | return FALSE; | |
7472 | } | |
0b52efa6 PB |
7473 | |
7474 | sections = (struct bfd_link_order **) | |
7475 | xmalloc (seen_linkorder * sizeof (struct bfd_link_order *)); | |
7476 | seen_linkorder = 0; | |
7477 | ||
7478 | for (p = o->link_order_head; p != NULL; p = p->next) | |
7479 | { | |
7480 | sections[seen_linkorder++] = p; | |
7481 | } | |
7482 | /* Sort the input sections in the order of their linked section. */ | |
7483 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
7484 | compare_link_order); | |
7485 | ||
7486 | /* Change the offsets of the sections. */ | |
7487 | offset = 0; | |
7488 | for (n = 0; n < seen_linkorder; n++) | |
7489 | { | |
7490 | s = sections[n]->u.indirect.section; | |
7491 | offset &= ~(bfd_vma)((1 << s->alignment_power) - 1); | |
7492 | s->output_offset = offset; | |
7493 | sections[n]->offset = offset; | |
7494 | offset += sections[n]->size; | |
7495 | } | |
7496 | ||
7497 | return TRUE; | |
7498 | } | |
7499 | ||
7500 | ||
c152c796 AM |
7501 | /* Do the final step of an ELF link. */ |
7502 | ||
7503 | bfd_boolean | |
7504 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
7505 | { | |
7506 | bfd_boolean dynamic; | |
7507 | bfd_boolean emit_relocs; | |
7508 | bfd *dynobj; | |
7509 | struct elf_final_link_info finfo; | |
7510 | register asection *o; | |
7511 | register struct bfd_link_order *p; | |
7512 | register bfd *sub; | |
7513 | bfd_size_type max_contents_size; | |
7514 | bfd_size_type max_external_reloc_size; | |
7515 | bfd_size_type max_internal_reloc_count; | |
7516 | bfd_size_type max_sym_count; | |
7517 | bfd_size_type max_sym_shndx_count; | |
7518 | file_ptr off; | |
7519 | Elf_Internal_Sym elfsym; | |
7520 | unsigned int i; | |
7521 | Elf_Internal_Shdr *symtab_hdr; | |
7522 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
7523 | Elf_Internal_Shdr *symstrtab_hdr; | |
7524 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7525 | struct elf_outext_info eoinfo; | |
7526 | bfd_boolean merged; | |
7527 | size_t relativecount = 0; | |
7528 | asection *reldyn = 0; | |
7529 | bfd_size_type amt; | |
7530 | ||
7531 | if (! is_elf_hash_table (info->hash)) | |
7532 | return FALSE; | |
7533 | ||
7534 | if (info->shared) | |
7535 | abfd->flags |= DYNAMIC; | |
7536 | ||
7537 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
7538 | dynobj = elf_hash_table (info)->dynobj; | |
7539 | ||
7540 | emit_relocs = (info->relocatable | |
7541 | || info->emitrelocations | |
7542 | || bed->elf_backend_emit_relocs); | |
7543 | ||
7544 | finfo.info = info; | |
7545 | finfo.output_bfd = abfd; | |
7546 | finfo.symstrtab = _bfd_elf_stringtab_init (); | |
7547 | if (finfo.symstrtab == NULL) | |
7548 | return FALSE; | |
7549 | ||
7550 | if (! dynamic) | |
7551 | { | |
7552 | finfo.dynsym_sec = NULL; | |
7553 | finfo.hash_sec = NULL; | |
7554 | finfo.symver_sec = NULL; | |
7555 | } | |
7556 | else | |
7557 | { | |
7558 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
7559 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
7560 | BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL); | |
7561 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
7562 | /* Note that it is OK if symver_sec is NULL. */ | |
7563 | } | |
7564 | ||
7565 | finfo.contents = NULL; | |
7566 | finfo.external_relocs = NULL; | |
7567 | finfo.internal_relocs = NULL; | |
7568 | finfo.external_syms = NULL; | |
7569 | finfo.locsym_shndx = NULL; | |
7570 | finfo.internal_syms = NULL; | |
7571 | finfo.indices = NULL; | |
7572 | finfo.sections = NULL; | |
7573 | finfo.symbuf = NULL; | |
7574 | finfo.symshndxbuf = NULL; | |
7575 | finfo.symbuf_count = 0; | |
7576 | finfo.shndxbuf_size = 0; | |
7577 | ||
7578 | /* Count up the number of relocations we will output for each output | |
7579 | section, so that we know the sizes of the reloc sections. We | |
7580 | also figure out some maximum sizes. */ | |
7581 | max_contents_size = 0; | |
7582 | max_external_reloc_size = 0; | |
7583 | max_internal_reloc_count = 0; | |
7584 | max_sym_count = 0; | |
7585 | max_sym_shndx_count = 0; | |
7586 | merged = FALSE; | |
7587 | for (o = abfd->sections; o != NULL; o = o->next) | |
7588 | { | |
7589 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
7590 | o->reloc_count = 0; | |
7591 | ||
7592 | for (p = o->link_order_head; p != NULL; p = p->next) | |
7593 | { | |
7594 | unsigned int reloc_count = 0; | |
7595 | struct bfd_elf_section_data *esdi = NULL; | |
7596 | unsigned int *rel_count1; | |
7597 | ||
7598 | if (p->type == bfd_section_reloc_link_order | |
7599 | || p->type == bfd_symbol_reloc_link_order) | |
7600 | reloc_count = 1; | |
7601 | else if (p->type == bfd_indirect_link_order) | |
7602 | { | |
7603 | asection *sec; | |
7604 | ||
7605 | sec = p->u.indirect.section; | |
7606 | esdi = elf_section_data (sec); | |
7607 | ||
7608 | /* Mark all sections which are to be included in the | |
7609 | link. This will normally be every section. We need | |
7610 | to do this so that we can identify any sections which | |
7611 | the linker has decided to not include. */ | |
7612 | sec->linker_mark = TRUE; | |
7613 | ||
7614 | if (sec->flags & SEC_MERGE) | |
7615 | merged = TRUE; | |
7616 | ||
7617 | if (info->relocatable || info->emitrelocations) | |
7618 | reloc_count = sec->reloc_count; | |
7619 | else if (bed->elf_backend_count_relocs) | |
7620 | { | |
7621 | Elf_Internal_Rela * relocs; | |
7622 | ||
7623 | relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
7624 | info->keep_memory); | |
7625 | ||
7626 | reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs); | |
7627 | ||
7628 | if (elf_section_data (o)->relocs != relocs) | |
7629 | free (relocs); | |
7630 | } | |
7631 | ||
eea6121a AM |
7632 | if (sec->rawsize > max_contents_size) |
7633 | max_contents_size = sec->rawsize; | |
7634 | if (sec->size > max_contents_size) | |
7635 | max_contents_size = sec->size; | |
c152c796 AM |
7636 | |
7637 | /* We are interested in just local symbols, not all | |
7638 | symbols. */ | |
7639 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
7640 | && (sec->owner->flags & DYNAMIC) == 0) | |
7641 | { | |
7642 | size_t sym_count; | |
7643 | ||
7644 | if (elf_bad_symtab (sec->owner)) | |
7645 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
7646 | / bed->s->sizeof_sym); | |
7647 | else | |
7648 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
7649 | ||
7650 | if (sym_count > max_sym_count) | |
7651 | max_sym_count = sym_count; | |
7652 | ||
7653 | if (sym_count > max_sym_shndx_count | |
7654 | && elf_symtab_shndx (sec->owner) != 0) | |
7655 | max_sym_shndx_count = sym_count; | |
7656 | ||
7657 | if ((sec->flags & SEC_RELOC) != 0) | |
7658 | { | |
7659 | size_t ext_size; | |
7660 | ||
7661 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
7662 | if (ext_size > max_external_reloc_size) | |
7663 | max_external_reloc_size = ext_size; | |
7664 | if (sec->reloc_count > max_internal_reloc_count) | |
7665 | max_internal_reloc_count = sec->reloc_count; | |
7666 | } | |
7667 | } | |
7668 | } | |
7669 | ||
7670 | if (reloc_count == 0) | |
7671 | continue; | |
7672 | ||
7673 | o->reloc_count += reloc_count; | |
7674 | ||
7675 | /* MIPS may have a mix of REL and RELA relocs on sections. | |
7676 | To support this curious ABI we keep reloc counts in | |
7677 | elf_section_data too. We must be careful to add the | |
7678 | relocations from the input section to the right output | |
7679 | count. FIXME: Get rid of one count. We have | |
7680 | o->reloc_count == esdo->rel_count + esdo->rel_count2. */ | |
7681 | rel_count1 = &esdo->rel_count; | |
7682 | if (esdi != NULL) | |
7683 | { | |
7684 | bfd_boolean same_size; | |
7685 | bfd_size_type entsize1; | |
7686 | ||
7687 | entsize1 = esdi->rel_hdr.sh_entsize; | |
7688 | BFD_ASSERT (entsize1 == bed->s->sizeof_rel | |
7689 | || entsize1 == bed->s->sizeof_rela); | |
7690 | same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel); | |
7691 | ||
7692 | if (!same_size) | |
7693 | rel_count1 = &esdo->rel_count2; | |
7694 | ||
7695 | if (esdi->rel_hdr2 != NULL) | |
7696 | { | |
7697 | bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize; | |
7698 | unsigned int alt_count; | |
7699 | unsigned int *rel_count2; | |
7700 | ||
7701 | BFD_ASSERT (entsize2 != entsize1 | |
7702 | && (entsize2 == bed->s->sizeof_rel | |
7703 | || entsize2 == bed->s->sizeof_rela)); | |
7704 | ||
7705 | rel_count2 = &esdo->rel_count2; | |
7706 | if (!same_size) | |
7707 | rel_count2 = &esdo->rel_count; | |
7708 | ||
7709 | /* The following is probably too simplistic if the | |
7710 | backend counts output relocs unusually. */ | |
7711 | BFD_ASSERT (bed->elf_backend_count_relocs == NULL); | |
7712 | alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2); | |
7713 | *rel_count2 += alt_count; | |
7714 | reloc_count -= alt_count; | |
7715 | } | |
7716 | } | |
7717 | *rel_count1 += reloc_count; | |
7718 | } | |
7719 | ||
7720 | if (o->reloc_count > 0) | |
7721 | o->flags |= SEC_RELOC; | |
7722 | else | |
7723 | { | |
7724 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
7725 | set it (this is probably a bug) and if it is set | |
7726 | assign_section_numbers will create a reloc section. */ | |
7727 | o->flags &=~ SEC_RELOC; | |
7728 | } | |
7729 | ||
7730 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
7731 | zero. This is done in elf_fake_sections as well, but forcing | |
7732 | the VMA to 0 here will ensure that relocs against these | |
7733 | sections are handled correctly. */ | |
7734 | if ((o->flags & SEC_ALLOC) == 0 | |
7735 | && ! o->user_set_vma) | |
7736 | o->vma = 0; | |
7737 | } | |
7738 | ||
7739 | if (! info->relocatable && merged) | |
7740 | elf_link_hash_traverse (elf_hash_table (info), | |
7741 | _bfd_elf_link_sec_merge_syms, abfd); | |
7742 | ||
7743 | /* Figure out the file positions for everything but the symbol table | |
7744 | and the relocs. We set symcount to force assign_section_numbers | |
7745 | to create a symbol table. */ | |
7746 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; | |
7747 | BFD_ASSERT (! abfd->output_has_begun); | |
7748 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
7749 | goto error_return; | |
7750 | ||
ee75fd95 | 7751 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
7752 | for (o = abfd->sections; o != NULL; o = o->next) |
7753 | { | |
7754 | if ((o->flags & SEC_RELOC) != 0) | |
7755 | { | |
7756 | if (!(_bfd_elf_link_size_reloc_section | |
7757 | (abfd, &elf_section_data (o)->rel_hdr, o))) | |
7758 | goto error_return; | |
7759 | ||
7760 | if (elf_section_data (o)->rel_hdr2 | |
7761 | && !(_bfd_elf_link_size_reloc_section | |
7762 | (abfd, elf_section_data (o)->rel_hdr2, o))) | |
7763 | goto error_return; | |
7764 | } | |
7765 | ||
7766 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
7767 | to count upwards while actually outputting the relocations. */ | |
7768 | elf_section_data (o)->rel_count = 0; | |
7769 | elf_section_data (o)->rel_count2 = 0; | |
7770 | } | |
7771 | ||
7772 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
7773 | ||
7774 | /* We have now assigned file positions for all the sections except | |
7775 | .symtab and .strtab. We start the .symtab section at the current | |
7776 | file position, and write directly to it. We build the .strtab | |
7777 | section in memory. */ | |
7778 | bfd_get_symcount (abfd) = 0; | |
7779 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7780 | /* sh_name is set in prep_headers. */ | |
7781 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7782 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
7783 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7784 | /* sh_link is set in assign_section_numbers. */ | |
7785 | /* sh_info is set below. */ | |
7786 | /* sh_offset is set just below. */ | |
7787 | symtab_hdr->sh_addralign = 1 << bed->s->log_file_align; | |
7788 | ||
7789 | off = elf_tdata (abfd)->next_file_pos; | |
7790 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
7791 | ||
7792 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
7793 | incorrect. We do not yet know the size of the .symtab section. | |
7794 | We correct next_file_pos below, after we do know the size. */ | |
7795 | ||
7796 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
7797 | continuously seeking to the right position in the file. */ | |
7798 | if (! info->keep_memory || max_sym_count < 20) | |
7799 | finfo.symbuf_size = 20; | |
7800 | else | |
7801 | finfo.symbuf_size = max_sym_count; | |
7802 | amt = finfo.symbuf_size; | |
7803 | amt *= bed->s->sizeof_sym; | |
7804 | finfo.symbuf = bfd_malloc (amt); | |
7805 | if (finfo.symbuf == NULL) | |
7806 | goto error_return; | |
7807 | if (elf_numsections (abfd) > SHN_LORESERVE) | |
7808 | { | |
7809 | /* Wild guess at number of output symbols. realloc'd as needed. */ | |
7810 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
7811 | finfo.shndxbuf_size = amt; | |
7812 | amt *= sizeof (Elf_External_Sym_Shndx); | |
7813 | finfo.symshndxbuf = bfd_zmalloc (amt); | |
7814 | if (finfo.symshndxbuf == NULL) | |
7815 | goto error_return; | |
7816 | } | |
7817 | ||
7818 | /* Start writing out the symbol table. The first symbol is always a | |
7819 | dummy symbol. */ | |
7820 | if (info->strip != strip_all | |
7821 | || emit_relocs) | |
7822 | { | |
7823 | elfsym.st_value = 0; | |
7824 | elfsym.st_size = 0; | |
7825 | elfsym.st_info = 0; | |
7826 | elfsym.st_other = 0; | |
7827 | elfsym.st_shndx = SHN_UNDEF; | |
7828 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr, | |
7829 | NULL)) | |
7830 | goto error_return; | |
7831 | } | |
7832 | ||
7833 | #if 0 | |
7834 | /* Some standard ELF linkers do this, but we don't because it causes | |
7835 | bootstrap comparison failures. */ | |
7836 | /* Output a file symbol for the output file as the second symbol. | |
7837 | We output this even if we are discarding local symbols, although | |
7838 | I'm not sure if this is correct. */ | |
7839 | elfsym.st_value = 0; | |
7840 | elfsym.st_size = 0; | |
7841 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7842 | elfsym.st_other = 0; | |
7843 | elfsym.st_shndx = SHN_ABS; | |
7844 | if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd), | |
7845 | &elfsym, bfd_abs_section_ptr, NULL)) | |
7846 | goto error_return; | |
7847 | #endif | |
7848 | ||
7849 | /* Output a symbol for each section. We output these even if we are | |
7850 | discarding local symbols, since they are used for relocs. These | |
7851 | symbols have no names. We store the index of each one in the | |
7852 | index field of the section, so that we can find it again when | |
7853 | outputting relocs. */ | |
7854 | if (info->strip != strip_all | |
7855 | || emit_relocs) | |
7856 | { | |
7857 | elfsym.st_size = 0; | |
7858 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7859 | elfsym.st_other = 0; | |
7860 | for (i = 1; i < elf_numsections (abfd); i++) | |
7861 | { | |
7862 | o = bfd_section_from_elf_index (abfd, i); | |
7863 | if (o != NULL) | |
7864 | o->target_index = bfd_get_symcount (abfd); | |
7865 | elfsym.st_shndx = i; | |
7866 | if (info->relocatable || o == NULL) | |
7867 | elfsym.st_value = 0; | |
7868 | else | |
7869 | elfsym.st_value = o->vma; | |
7870 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL)) | |
7871 | goto error_return; | |
7872 | if (i == SHN_LORESERVE - 1) | |
7873 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
7874 | } | |
7875 | } | |
7876 | ||
7877 | /* Allocate some memory to hold information read in from the input | |
7878 | files. */ | |
7879 | if (max_contents_size != 0) | |
7880 | { | |
7881 | finfo.contents = bfd_malloc (max_contents_size); | |
7882 | if (finfo.contents == NULL) | |
7883 | goto error_return; | |
7884 | } | |
7885 | ||
7886 | if (max_external_reloc_size != 0) | |
7887 | { | |
7888 | finfo.external_relocs = bfd_malloc (max_external_reloc_size); | |
7889 | if (finfo.external_relocs == NULL) | |
7890 | goto error_return; | |
7891 | } | |
7892 | ||
7893 | if (max_internal_reloc_count != 0) | |
7894 | { | |
7895 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
7896 | amt *= sizeof (Elf_Internal_Rela); | |
7897 | finfo.internal_relocs = bfd_malloc (amt); | |
7898 | if (finfo.internal_relocs == NULL) | |
7899 | goto error_return; | |
7900 | } | |
7901 | ||
7902 | if (max_sym_count != 0) | |
7903 | { | |
7904 | amt = max_sym_count * bed->s->sizeof_sym; | |
7905 | finfo.external_syms = bfd_malloc (amt); | |
7906 | if (finfo.external_syms == NULL) | |
7907 | goto error_return; | |
7908 | ||
7909 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
7910 | finfo.internal_syms = bfd_malloc (amt); | |
7911 | if (finfo.internal_syms == NULL) | |
7912 | goto error_return; | |
7913 | ||
7914 | amt = max_sym_count * sizeof (long); | |
7915 | finfo.indices = bfd_malloc (amt); | |
7916 | if (finfo.indices == NULL) | |
7917 | goto error_return; | |
7918 | ||
7919 | amt = max_sym_count * sizeof (asection *); | |
7920 | finfo.sections = bfd_malloc (amt); | |
7921 | if (finfo.sections == NULL) | |
7922 | goto error_return; | |
7923 | } | |
7924 | ||
7925 | if (max_sym_shndx_count != 0) | |
7926 | { | |
7927 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
7928 | finfo.locsym_shndx = bfd_malloc (amt); | |
7929 | if (finfo.locsym_shndx == NULL) | |
7930 | goto error_return; | |
7931 | } | |
7932 | ||
7933 | if (elf_hash_table (info)->tls_sec) | |
7934 | { | |
7935 | bfd_vma base, end = 0; | |
7936 | asection *sec; | |
7937 | ||
7938 | for (sec = elf_hash_table (info)->tls_sec; | |
7939 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
7940 | sec = sec->next) | |
7941 | { | |
eea6121a | 7942 | bfd_vma size = sec->size; |
c152c796 AM |
7943 | |
7944 | if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
7945 | { | |
7946 | struct bfd_link_order *o; | |
7947 | ||
7948 | for (o = sec->link_order_head; o != NULL; o = o->next) | |
7949 | if (size < o->offset + o->size) | |
7950 | size = o->offset + o->size; | |
7951 | } | |
7952 | end = sec->vma + size; | |
7953 | } | |
7954 | base = elf_hash_table (info)->tls_sec->vma; | |
7955 | end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power); | |
7956 | elf_hash_table (info)->tls_size = end - base; | |
7957 | } | |
7958 | ||
0b52efa6 PB |
7959 | /* Reorder SHF_LINK_ORDER sections. */ |
7960 | for (o = abfd->sections; o != NULL; o = o->next) | |
7961 | { | |
7962 | if (!elf_fixup_link_order (abfd, o)) | |
7963 | return FALSE; | |
7964 | } | |
7965 | ||
c152c796 AM |
7966 | /* Since ELF permits relocations to be against local symbols, we |
7967 | must have the local symbols available when we do the relocations. | |
7968 | Since we would rather only read the local symbols once, and we | |
7969 | would rather not keep them in memory, we handle all the | |
7970 | relocations for a single input file at the same time. | |
7971 | ||
7972 | Unfortunately, there is no way to know the total number of local | |
7973 | symbols until we have seen all of them, and the local symbol | |
7974 | indices precede the global symbol indices. This means that when | |
7975 | we are generating relocatable output, and we see a reloc against | |
7976 | a global symbol, we can not know the symbol index until we have | |
7977 | finished examining all the local symbols to see which ones we are | |
7978 | going to output. To deal with this, we keep the relocations in | |
7979 | memory, and don't output them until the end of the link. This is | |
7980 | an unfortunate waste of memory, but I don't see a good way around | |
7981 | it. Fortunately, it only happens when performing a relocatable | |
7982 | link, which is not the common case. FIXME: If keep_memory is set | |
7983 | we could write the relocs out and then read them again; I don't | |
7984 | know how bad the memory loss will be. */ | |
7985 | ||
7986 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
7987 | sub->output_has_begun = FALSE; | |
7988 | for (o = abfd->sections; o != NULL; o = o->next) | |
7989 | { | |
7990 | for (p = o->link_order_head; p != NULL; p = p->next) | |
7991 | { | |
7992 | if (p->type == bfd_indirect_link_order | |
7993 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
7994 | == bfd_target_elf_flavour) | |
7995 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
7996 | { | |
7997 | if (! sub->output_has_begun) | |
7998 | { | |
7999 | if (! elf_link_input_bfd (&finfo, sub)) | |
8000 | goto error_return; | |
8001 | sub->output_has_begun = TRUE; | |
8002 | } | |
8003 | } | |
8004 | else if (p->type == bfd_section_reloc_link_order | |
8005 | || p->type == bfd_symbol_reloc_link_order) | |
8006 | { | |
8007 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
8008 | goto error_return; | |
8009 | } | |
8010 | else | |
8011 | { | |
8012 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
8013 | goto error_return; | |
8014 | } | |
8015 | } | |
8016 | } | |
8017 | ||
8018 | /* Output any global symbols that got converted to local in a | |
8019 | version script or due to symbol visibility. We do this in a | |
8020 | separate step since ELF requires all local symbols to appear | |
8021 | prior to any global symbols. FIXME: We should only do this if | |
8022 | some global symbols were, in fact, converted to become local. | |
8023 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
8024 | eoinfo.failed = FALSE; | |
8025 | eoinfo.finfo = &finfo; | |
8026 | eoinfo.localsyms = TRUE; | |
8027 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8028 | &eoinfo); | |
8029 | if (eoinfo.failed) | |
8030 | return FALSE; | |
8031 | ||
8032 | /* That wrote out all the local symbols. Finish up the symbol table | |
8033 | with the global symbols. Even if we want to strip everything we | |
8034 | can, we still need to deal with those global symbols that got | |
8035 | converted to local in a version script. */ | |
8036 | ||
8037 | /* The sh_info field records the index of the first non local symbol. */ | |
8038 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
8039 | ||
8040 | if (dynamic | |
8041 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
8042 | { | |
8043 | Elf_Internal_Sym sym; | |
8044 | bfd_byte *dynsym = finfo.dynsym_sec->contents; | |
8045 | long last_local = 0; | |
8046 | ||
8047 | /* Write out the section symbols for the output sections. */ | |
8048 | if (info->shared) | |
8049 | { | |
8050 | asection *s; | |
8051 | ||
8052 | sym.st_size = 0; | |
8053 | sym.st_name = 0; | |
8054 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8055 | sym.st_other = 0; | |
8056 | ||
8057 | for (s = abfd->sections; s != NULL; s = s->next) | |
8058 | { | |
8059 | int indx; | |
8060 | bfd_byte *dest; | |
8061 | long dynindx; | |
8062 | ||
c152c796 | 8063 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
8064 | if (dynindx <= 0) |
8065 | continue; | |
8066 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
8067 | BFD_ASSERT (indx > 0); |
8068 | sym.st_shndx = indx; | |
8069 | sym.st_value = s->vma; | |
8070 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
8c37241b JJ |
8071 | if (last_local < dynindx) |
8072 | last_local = dynindx; | |
c152c796 AM |
8073 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
8074 | } | |
c152c796 AM |
8075 | } |
8076 | ||
8077 | /* Write out the local dynsyms. */ | |
8078 | if (elf_hash_table (info)->dynlocal) | |
8079 | { | |
8080 | struct elf_link_local_dynamic_entry *e; | |
8081 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
8082 | { | |
8083 | asection *s; | |
8084 | bfd_byte *dest; | |
8085 | ||
8086 | sym.st_size = e->isym.st_size; | |
8087 | sym.st_other = e->isym.st_other; | |
8088 | ||
8089 | /* Copy the internal symbol as is. | |
8090 | Note that we saved a word of storage and overwrote | |
8091 | the original st_name with the dynstr_index. */ | |
8092 | sym = e->isym; | |
8093 | ||
8094 | if (e->isym.st_shndx != SHN_UNDEF | |
8095 | && (e->isym.st_shndx < SHN_LORESERVE | |
8096 | || e->isym.st_shndx > SHN_HIRESERVE)) | |
8097 | { | |
8098 | s = bfd_section_from_elf_index (e->input_bfd, | |
8099 | e->isym.st_shndx); | |
8100 | ||
8101 | sym.st_shndx = | |
8102 | elf_section_data (s->output_section)->this_idx; | |
8103 | sym.st_value = (s->output_section->vma | |
8104 | + s->output_offset | |
8105 | + e->isym.st_value); | |
8106 | } | |
8107 | ||
8108 | if (last_local < e->dynindx) | |
8109 | last_local = e->dynindx; | |
8110 | ||
8111 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
8112 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
8113 | } | |
8114 | } | |
8115 | ||
8116 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = | |
8117 | last_local + 1; | |
8118 | } | |
8119 | ||
8120 | /* We get the global symbols from the hash table. */ | |
8121 | eoinfo.failed = FALSE; | |
8122 | eoinfo.localsyms = FALSE; | |
8123 | eoinfo.finfo = &finfo; | |
8124 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8125 | &eoinfo); | |
8126 | if (eoinfo.failed) | |
8127 | return FALSE; | |
8128 | ||
8129 | /* If backend needs to output some symbols not present in the hash | |
8130 | table, do it now. */ | |
8131 | if (bed->elf_backend_output_arch_syms) | |
8132 | { | |
8133 | typedef bfd_boolean (*out_sym_func) | |
8134 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
8135 | struct elf_link_hash_entry *); | |
8136 | ||
8137 | if (! ((*bed->elf_backend_output_arch_syms) | |
8138 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
8139 | return FALSE; | |
8140 | } | |
8141 | ||
8142 | /* Flush all symbols to the file. */ | |
8143 | if (! elf_link_flush_output_syms (&finfo, bed)) | |
8144 | return FALSE; | |
8145 | ||
8146 | /* Now we know the size of the symtab section. */ | |
8147 | off += symtab_hdr->sh_size; | |
8148 | ||
8149 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
8150 | if (symtab_shndx_hdr->sh_name != 0) | |
8151 | { | |
8152 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
8153 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
8154 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
8155 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
8156 | symtab_shndx_hdr->sh_size = amt; | |
8157 | ||
8158 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
8159 | off, TRUE); | |
8160 | ||
8161 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
8162 | || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) | |
8163 | return FALSE; | |
8164 | } | |
8165 | ||
8166 | ||
8167 | /* Finish up and write out the symbol string table (.strtab) | |
8168 | section. */ | |
8169 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
8170 | /* sh_name was set in prep_headers. */ | |
8171 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
8172 | symstrtab_hdr->sh_flags = 0; | |
8173 | symstrtab_hdr->sh_addr = 0; | |
8174 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
8175 | symstrtab_hdr->sh_entsize = 0; | |
8176 | symstrtab_hdr->sh_link = 0; | |
8177 | symstrtab_hdr->sh_info = 0; | |
8178 | /* sh_offset is set just below. */ | |
8179 | symstrtab_hdr->sh_addralign = 1; | |
8180 | ||
8181 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); | |
8182 | elf_tdata (abfd)->next_file_pos = off; | |
8183 | ||
8184 | if (bfd_get_symcount (abfd) > 0) | |
8185 | { | |
8186 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
8187 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
8188 | return FALSE; | |
8189 | } | |
8190 | ||
8191 | /* Adjust the relocs to have the correct symbol indices. */ | |
8192 | for (o = abfd->sections; o != NULL; o = o->next) | |
8193 | { | |
8194 | if ((o->flags & SEC_RELOC) == 0) | |
8195 | continue; | |
8196 | ||
8197 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, | |
8198 | elf_section_data (o)->rel_count, | |
8199 | elf_section_data (o)->rel_hashes); | |
8200 | if (elf_section_data (o)->rel_hdr2 != NULL) | |
8201 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, | |
8202 | elf_section_data (o)->rel_count2, | |
8203 | (elf_section_data (o)->rel_hashes | |
8204 | + elf_section_data (o)->rel_count)); | |
8205 | ||
8206 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
8207 | trying to swap the relocs out itself. */ | |
8208 | o->reloc_count = 0; | |
8209 | } | |
8210 | ||
8211 | if (dynamic && info->combreloc && dynobj != NULL) | |
8212 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
8213 | ||
8214 | /* If we are linking against a dynamic object, or generating a | |
8215 | shared library, finish up the dynamic linking information. */ | |
8216 | if (dynamic) | |
8217 | { | |
8218 | bfd_byte *dyncon, *dynconend; | |
8219 | ||
8220 | /* Fix up .dynamic entries. */ | |
8221 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
8222 | BFD_ASSERT (o != NULL); | |
8223 | ||
8224 | dyncon = o->contents; | |
eea6121a | 8225 | dynconend = o->contents + o->size; |
c152c796 AM |
8226 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
8227 | { | |
8228 | Elf_Internal_Dyn dyn; | |
8229 | const char *name; | |
8230 | unsigned int type; | |
8231 | ||
8232 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
8233 | ||
8234 | switch (dyn.d_tag) | |
8235 | { | |
8236 | default: | |
8237 | continue; | |
8238 | case DT_NULL: | |
8239 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
8240 | { | |
8241 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
8242 | { | |
8243 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
8244 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
8245 | default: continue; | |
8246 | } | |
8247 | dyn.d_un.d_val = relativecount; | |
8248 | relativecount = 0; | |
8249 | break; | |
8250 | } | |
8251 | continue; | |
8252 | ||
8253 | case DT_INIT: | |
8254 | name = info->init_function; | |
8255 | goto get_sym; | |
8256 | case DT_FINI: | |
8257 | name = info->fini_function; | |
8258 | get_sym: | |
8259 | { | |
8260 | struct elf_link_hash_entry *h; | |
8261 | ||
8262 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
8263 | FALSE, FALSE, TRUE); | |
8264 | if (h != NULL | |
8265 | && (h->root.type == bfd_link_hash_defined | |
8266 | || h->root.type == bfd_link_hash_defweak)) | |
8267 | { | |
8268 | dyn.d_un.d_val = h->root.u.def.value; | |
8269 | o = h->root.u.def.section; | |
8270 | if (o->output_section != NULL) | |
8271 | dyn.d_un.d_val += (o->output_section->vma | |
8272 | + o->output_offset); | |
8273 | else | |
8274 | { | |
8275 | /* The symbol is imported from another shared | |
8276 | library and does not apply to this one. */ | |
8277 | dyn.d_un.d_val = 0; | |
8278 | } | |
8279 | break; | |
8280 | } | |
8281 | } | |
8282 | continue; | |
8283 | ||
8284 | case DT_PREINIT_ARRAYSZ: | |
8285 | name = ".preinit_array"; | |
8286 | goto get_size; | |
8287 | case DT_INIT_ARRAYSZ: | |
8288 | name = ".init_array"; | |
8289 | goto get_size; | |
8290 | case DT_FINI_ARRAYSZ: | |
8291 | name = ".fini_array"; | |
8292 | get_size: | |
8293 | o = bfd_get_section_by_name (abfd, name); | |
8294 | if (o == NULL) | |
8295 | { | |
8296 | (*_bfd_error_handler) | |
d003868e | 8297 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
8298 | goto error_return; |
8299 | } | |
eea6121a | 8300 | if (o->size == 0) |
c152c796 AM |
8301 | (*_bfd_error_handler) |
8302 | (_("warning: %s section has zero size"), name); | |
eea6121a | 8303 | dyn.d_un.d_val = o->size; |
c152c796 AM |
8304 | break; |
8305 | ||
8306 | case DT_PREINIT_ARRAY: | |
8307 | name = ".preinit_array"; | |
8308 | goto get_vma; | |
8309 | case DT_INIT_ARRAY: | |
8310 | name = ".init_array"; | |
8311 | goto get_vma; | |
8312 | case DT_FINI_ARRAY: | |
8313 | name = ".fini_array"; | |
8314 | goto get_vma; | |
8315 | ||
8316 | case DT_HASH: | |
8317 | name = ".hash"; | |
8318 | goto get_vma; | |
8319 | case DT_STRTAB: | |
8320 | name = ".dynstr"; | |
8321 | goto get_vma; | |
8322 | case DT_SYMTAB: | |
8323 | name = ".dynsym"; | |
8324 | goto get_vma; | |
8325 | case DT_VERDEF: | |
8326 | name = ".gnu.version_d"; | |
8327 | goto get_vma; | |
8328 | case DT_VERNEED: | |
8329 | name = ".gnu.version_r"; | |
8330 | goto get_vma; | |
8331 | case DT_VERSYM: | |
8332 | name = ".gnu.version"; | |
8333 | get_vma: | |
8334 | o = bfd_get_section_by_name (abfd, name); | |
8335 | if (o == NULL) | |
8336 | { | |
8337 | (*_bfd_error_handler) | |
d003868e | 8338 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
8339 | goto error_return; |
8340 | } | |
8341 | dyn.d_un.d_ptr = o->vma; | |
8342 | break; | |
8343 | ||
8344 | case DT_REL: | |
8345 | case DT_RELA: | |
8346 | case DT_RELSZ: | |
8347 | case DT_RELASZ: | |
8348 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
8349 | type = SHT_REL; | |
8350 | else | |
8351 | type = SHT_RELA; | |
8352 | dyn.d_un.d_val = 0; | |
8353 | for (i = 1; i < elf_numsections (abfd); i++) | |
8354 | { | |
8355 | Elf_Internal_Shdr *hdr; | |
8356 | ||
8357 | hdr = elf_elfsections (abfd)[i]; | |
8358 | if (hdr->sh_type == type | |
8359 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
8360 | { | |
8361 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
8362 | dyn.d_un.d_val += hdr->sh_size; | |
8363 | else | |
8364 | { | |
8365 | if (dyn.d_un.d_val == 0 | |
8366 | || hdr->sh_addr < dyn.d_un.d_val) | |
8367 | dyn.d_un.d_val = hdr->sh_addr; | |
8368 | } | |
8369 | } | |
8370 | } | |
8371 | break; | |
8372 | } | |
8373 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
8374 | } | |
8375 | } | |
8376 | ||
8377 | /* If we have created any dynamic sections, then output them. */ | |
8378 | if (dynobj != NULL) | |
8379 | { | |
8380 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
8381 | goto error_return; | |
8382 | ||
8383 | for (o = dynobj->sections; o != NULL; o = o->next) | |
8384 | { | |
8385 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 8386 | || o->size == 0 |
c152c796 AM |
8387 | || o->output_section == bfd_abs_section_ptr) |
8388 | continue; | |
8389 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
8390 | { | |
8391 | /* At this point, we are only interested in sections | |
8392 | created by _bfd_elf_link_create_dynamic_sections. */ | |
8393 | continue; | |
8394 | } | |
3722b82f AM |
8395 | if (elf_hash_table (info)->stab_info.stabstr == o) |
8396 | continue; | |
eea6121a AM |
8397 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
8398 | continue; | |
c152c796 AM |
8399 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
8400 | != SHT_STRTAB) | |
8401 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
8402 | { | |
8403 | if (! bfd_set_section_contents (abfd, o->output_section, | |
8404 | o->contents, | |
8405 | (file_ptr) o->output_offset, | |
eea6121a | 8406 | o->size)) |
c152c796 AM |
8407 | goto error_return; |
8408 | } | |
8409 | else | |
8410 | { | |
8411 | /* The contents of the .dynstr section are actually in a | |
8412 | stringtab. */ | |
8413 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
8414 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
8415 | || ! _bfd_elf_strtab_emit (abfd, | |
8416 | elf_hash_table (info)->dynstr)) | |
8417 | goto error_return; | |
8418 | } | |
8419 | } | |
8420 | } | |
8421 | ||
8422 | if (info->relocatable) | |
8423 | { | |
8424 | bfd_boolean failed = FALSE; | |
8425 | ||
8426 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
8427 | if (failed) | |
8428 | goto error_return; | |
8429 | } | |
8430 | ||
8431 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 8432 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
8433 | { |
8434 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
8435 | goto error_return; | |
8436 | } | |
8437 | ||
8438 | if (info->eh_frame_hdr) | |
8439 | { | |
8440 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) | |
8441 | goto error_return; | |
8442 | } | |
8443 | ||
8444 | if (finfo.symstrtab != NULL) | |
8445 | _bfd_stringtab_free (finfo.symstrtab); | |
8446 | if (finfo.contents != NULL) | |
8447 | free (finfo.contents); | |
8448 | if (finfo.external_relocs != NULL) | |
8449 | free (finfo.external_relocs); | |
8450 | if (finfo.internal_relocs != NULL) | |
8451 | free (finfo.internal_relocs); | |
8452 | if (finfo.external_syms != NULL) | |
8453 | free (finfo.external_syms); | |
8454 | if (finfo.locsym_shndx != NULL) | |
8455 | free (finfo.locsym_shndx); | |
8456 | if (finfo.internal_syms != NULL) | |
8457 | free (finfo.internal_syms); | |
8458 | if (finfo.indices != NULL) | |
8459 | free (finfo.indices); | |
8460 | if (finfo.sections != NULL) | |
8461 | free (finfo.sections); | |
8462 | if (finfo.symbuf != NULL) | |
8463 | free (finfo.symbuf); | |
8464 | if (finfo.symshndxbuf != NULL) | |
8465 | free (finfo.symshndxbuf); | |
8466 | for (o = abfd->sections; o != NULL; o = o->next) | |
8467 | { | |
8468 | if ((o->flags & SEC_RELOC) != 0 | |
8469 | && elf_section_data (o)->rel_hashes != NULL) | |
8470 | free (elf_section_data (o)->rel_hashes); | |
8471 | } | |
8472 | ||
8473 | elf_tdata (abfd)->linker = TRUE; | |
8474 | ||
8475 | return TRUE; | |
8476 | ||
8477 | error_return: | |
8478 | if (finfo.symstrtab != NULL) | |
8479 | _bfd_stringtab_free (finfo.symstrtab); | |
8480 | if (finfo.contents != NULL) | |
8481 | free (finfo.contents); | |
8482 | if (finfo.external_relocs != NULL) | |
8483 | free (finfo.external_relocs); | |
8484 | if (finfo.internal_relocs != NULL) | |
8485 | free (finfo.internal_relocs); | |
8486 | if (finfo.external_syms != NULL) | |
8487 | free (finfo.external_syms); | |
8488 | if (finfo.locsym_shndx != NULL) | |
8489 | free (finfo.locsym_shndx); | |
8490 | if (finfo.internal_syms != NULL) | |
8491 | free (finfo.internal_syms); | |
8492 | if (finfo.indices != NULL) | |
8493 | free (finfo.indices); | |
8494 | if (finfo.sections != NULL) | |
8495 | free (finfo.sections); | |
8496 | if (finfo.symbuf != NULL) | |
8497 | free (finfo.symbuf); | |
8498 | if (finfo.symshndxbuf != NULL) | |
8499 | free (finfo.symshndxbuf); | |
8500 | for (o = abfd->sections; o != NULL; o = o->next) | |
8501 | { | |
8502 | if ((o->flags & SEC_RELOC) != 0 | |
8503 | && elf_section_data (o)->rel_hashes != NULL) | |
8504 | free (elf_section_data (o)->rel_hashes); | |
8505 | } | |
8506 | ||
8507 | return FALSE; | |
8508 | } | |
8509 | \f | |
8510 | /* Garbage collect unused sections. */ | |
8511 | ||
8512 | /* The mark phase of garbage collection. For a given section, mark | |
8513 | it and any sections in this section's group, and all the sections | |
8514 | which define symbols to which it refers. */ | |
8515 | ||
8516 | typedef asection * (*gc_mark_hook_fn) | |
8517 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
8518 | struct elf_link_hash_entry *, Elf_Internal_Sym *); | |
8519 | ||
ccfa59ea AM |
8520 | bfd_boolean |
8521 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
8522 | asection *sec, | |
8523 | gc_mark_hook_fn gc_mark_hook) | |
c152c796 AM |
8524 | { |
8525 | bfd_boolean ret; | |
8526 | asection *group_sec; | |
8527 | ||
8528 | sec->gc_mark = 1; | |
8529 | ||
8530 | /* Mark all the sections in the group. */ | |
8531 | group_sec = elf_section_data (sec)->next_in_group; | |
8532 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 8533 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
8534 | return FALSE; |
8535 | ||
8536 | /* Look through the section relocs. */ | |
8537 | ret = TRUE; | |
8538 | if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0) | |
8539 | { | |
8540 | Elf_Internal_Rela *relstart, *rel, *relend; | |
8541 | Elf_Internal_Shdr *symtab_hdr; | |
8542 | struct elf_link_hash_entry **sym_hashes; | |
8543 | size_t nlocsyms; | |
8544 | size_t extsymoff; | |
8545 | bfd *input_bfd = sec->owner; | |
8546 | const struct elf_backend_data *bed = get_elf_backend_data (input_bfd); | |
8547 | Elf_Internal_Sym *isym = NULL; | |
8548 | int r_sym_shift; | |
8549 | ||
8550 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
8551 | sym_hashes = elf_sym_hashes (input_bfd); | |
8552 | ||
8553 | /* Read the local symbols. */ | |
8554 | if (elf_bad_symtab (input_bfd)) | |
8555 | { | |
8556 | nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
8557 | extsymoff = 0; | |
8558 | } | |
8559 | else | |
8560 | extsymoff = nlocsyms = symtab_hdr->sh_info; | |
8561 | ||
8562 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
8563 | if (isym == NULL && nlocsyms != 0) | |
8564 | { | |
8565 | isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0, | |
8566 | NULL, NULL, NULL); | |
8567 | if (isym == NULL) | |
8568 | return FALSE; | |
8569 | } | |
8570 | ||
8571 | /* Read the relocations. */ | |
8572 | relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL, | |
8573 | info->keep_memory); | |
8574 | if (relstart == NULL) | |
8575 | { | |
8576 | ret = FALSE; | |
8577 | goto out1; | |
8578 | } | |
8579 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
8580 | ||
8581 | if (bed->s->arch_size == 32) | |
8582 | r_sym_shift = 8; | |
8583 | else | |
8584 | r_sym_shift = 32; | |
8585 | ||
8586 | for (rel = relstart; rel < relend; rel++) | |
8587 | { | |
8588 | unsigned long r_symndx; | |
8589 | asection *rsec; | |
8590 | struct elf_link_hash_entry *h; | |
8591 | ||
8592 | r_symndx = rel->r_info >> r_sym_shift; | |
8593 | if (r_symndx == 0) | |
8594 | continue; | |
8595 | ||
8596 | if (r_symndx >= nlocsyms | |
8597 | || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL) | |
8598 | { | |
8599 | h = sym_hashes[r_symndx - extsymoff]; | |
20f0a1ad AM |
8600 | while (h->root.type == bfd_link_hash_indirect |
8601 | || h->root.type == bfd_link_hash_warning) | |
8602 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 AM |
8603 | rsec = (*gc_mark_hook) (sec, info, rel, h, NULL); |
8604 | } | |
8605 | else | |
8606 | { | |
8607 | rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]); | |
8608 | } | |
8609 | ||
8610 | if (rsec && !rsec->gc_mark) | |
8611 | { | |
8612 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
8613 | rsec->gc_mark = 1; | |
ccfa59ea | 8614 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) |
c152c796 AM |
8615 | { |
8616 | ret = FALSE; | |
8617 | goto out2; | |
8618 | } | |
8619 | } | |
8620 | } | |
8621 | ||
8622 | out2: | |
8623 | if (elf_section_data (sec)->relocs != relstart) | |
8624 | free (relstart); | |
8625 | out1: | |
8626 | if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym) | |
8627 | { | |
8628 | if (! info->keep_memory) | |
8629 | free (isym); | |
8630 | else | |
8631 | symtab_hdr->contents = (unsigned char *) isym; | |
8632 | } | |
8633 | } | |
8634 | ||
8635 | return ret; | |
8636 | } | |
8637 | ||
8638 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ | |
8639 | ||
8640 | static bfd_boolean | |
8641 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr) | |
8642 | { | |
8643 | int *idx = idxptr; | |
8644 | ||
8645 | if (h->root.type == bfd_link_hash_warning) | |
8646 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8647 | ||
8648 | if (h->dynindx != -1 | |
8649 | && ((h->root.type != bfd_link_hash_defined | |
8650 | && h->root.type != bfd_link_hash_defweak) | |
8651 | || h->root.u.def.section->gc_mark)) | |
8652 | h->dynindx = (*idx)++; | |
8653 | ||
8654 | return TRUE; | |
8655 | } | |
8656 | ||
8657 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
8658 | ||
8659 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
8660 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
8661 | ||
8662 | static bfd_boolean | |
8663 | elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook) | |
8664 | { | |
8665 | bfd *sub; | |
8666 | ||
8667 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8668 | { | |
8669 | asection *o; | |
8670 | ||
8671 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
8672 | continue; | |
8673 | ||
8674 | for (o = sub->sections; o != NULL; o = o->next) | |
8675 | { | |
7c2c8505 AM |
8676 | /* Keep debug and special sections. */ |
8677 | if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 | |
8678 | || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0) | |
c152c796 AM |
8679 | o->gc_mark = 1; |
8680 | ||
8681 | if (o->gc_mark) | |
8682 | continue; | |
8683 | ||
8684 | /* Skip sweeping sections already excluded. */ | |
8685 | if (o->flags & SEC_EXCLUDE) | |
8686 | continue; | |
8687 | ||
8688 | /* Since this is early in the link process, it is simple | |
8689 | to remove a section from the output. */ | |
8690 | o->flags |= SEC_EXCLUDE; | |
8691 | ||
8692 | /* But we also have to update some of the relocation | |
8693 | info we collected before. */ | |
8694 | if (gc_sweep_hook | |
8695 | && (o->flags & SEC_RELOC) && o->reloc_count > 0) | |
8696 | { | |
8697 | Elf_Internal_Rela *internal_relocs; | |
8698 | bfd_boolean r; | |
8699 | ||
8700 | internal_relocs | |
8701 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
8702 | info->keep_memory); | |
8703 | if (internal_relocs == NULL) | |
8704 | return FALSE; | |
8705 | ||
8706 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
8707 | ||
8708 | if (elf_section_data (o)->relocs != internal_relocs) | |
8709 | free (internal_relocs); | |
8710 | ||
8711 | if (!r) | |
8712 | return FALSE; | |
8713 | } | |
8714 | } | |
8715 | } | |
8716 | ||
8717 | /* Remove the symbols that were in the swept sections from the dynamic | |
8718 | symbol table. GCFIXME: Anyone know how to get them out of the | |
8719 | static symbol table as well? */ | |
8720 | { | |
8721 | int i = 0; | |
8722 | ||
8723 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i); | |
8724 | ||
8725 | elf_hash_table (info)->dynsymcount = i; | |
8726 | } | |
8727 | ||
8728 | return TRUE; | |
8729 | } | |
8730 | ||
8731 | /* Propagate collected vtable information. This is called through | |
8732 | elf_link_hash_traverse. */ | |
8733 | ||
8734 | static bfd_boolean | |
8735 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
8736 | { | |
8737 | if (h->root.type == bfd_link_hash_warning) | |
8738 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8739 | ||
8740 | /* Those that are not vtables. */ | |
f6e332e6 | 8741 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
8742 | return TRUE; |
8743 | ||
8744 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 8745 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
8746 | return TRUE; |
8747 | ||
8748 | /* If we've already been done, exit. */ | |
f6e332e6 | 8749 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
8750 | return TRUE; |
8751 | ||
8752 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 8753 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 8754 | |
f6e332e6 | 8755 | if (h->vtable->used == NULL) |
c152c796 AM |
8756 | { |
8757 | /* None of this table's entries were referenced. Re-use the | |
8758 | parent's table. */ | |
f6e332e6 AM |
8759 | h->vtable->used = h->vtable->parent->vtable->used; |
8760 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
8761 | } |
8762 | else | |
8763 | { | |
8764 | size_t n; | |
8765 | bfd_boolean *cu, *pu; | |
8766 | ||
8767 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 8768 | cu = h->vtable->used; |
c152c796 | 8769 | cu[-1] = TRUE; |
f6e332e6 | 8770 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
8771 | if (pu != NULL) |
8772 | { | |
8773 | const struct elf_backend_data *bed; | |
8774 | unsigned int log_file_align; | |
8775 | ||
8776 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
8777 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 8778 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
8779 | while (n--) |
8780 | { | |
8781 | if (*pu) | |
8782 | *cu = TRUE; | |
8783 | pu++; | |
8784 | cu++; | |
8785 | } | |
8786 | } | |
8787 | } | |
8788 | ||
8789 | return TRUE; | |
8790 | } | |
8791 | ||
8792 | static bfd_boolean | |
8793 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
8794 | { | |
8795 | asection *sec; | |
8796 | bfd_vma hstart, hend; | |
8797 | Elf_Internal_Rela *relstart, *relend, *rel; | |
8798 | const struct elf_backend_data *bed; | |
8799 | unsigned int log_file_align; | |
8800 | ||
8801 | if (h->root.type == bfd_link_hash_warning) | |
8802 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8803 | ||
8804 | /* Take care of both those symbols that do not describe vtables as | |
8805 | well as those that are not loaded. */ | |
f6e332e6 | 8806 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
8807 | return TRUE; |
8808 | ||
8809 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
8810 | || h->root.type == bfd_link_hash_defweak); | |
8811 | ||
8812 | sec = h->root.u.def.section; | |
8813 | hstart = h->root.u.def.value; | |
8814 | hend = hstart + h->size; | |
8815 | ||
8816 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
8817 | if (!relstart) | |
8818 | return *(bfd_boolean *) okp = FALSE; | |
8819 | bed = get_elf_backend_data (sec->owner); | |
8820 | log_file_align = bed->s->log_file_align; | |
8821 | ||
8822 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
8823 | ||
8824 | for (rel = relstart; rel < relend; ++rel) | |
8825 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
8826 | { | |
8827 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
8828 | if (h->vtable->used |
8829 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
8830 | { |
8831 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 8832 | if (h->vtable->used[entry]) |
c152c796 AM |
8833 | continue; |
8834 | } | |
8835 | /* Otherwise, kill it. */ | |
8836 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
8837 | } | |
8838 | ||
8839 | return TRUE; | |
8840 | } | |
8841 | ||
715df9b8 EB |
8842 | /* Mark sections containing dynamically referenced symbols. This is called |
8843 | through elf_link_hash_traverse. */ | |
8844 | ||
8845 | static bfd_boolean | |
8846 | elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, | |
8847 | void *okp ATTRIBUTE_UNUSED) | |
8848 | { | |
8849 | if (h->root.type == bfd_link_hash_warning) | |
8850 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8851 | ||
8852 | if ((h->root.type == bfd_link_hash_defined | |
8853 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 8854 | && h->ref_dynamic) |
715df9b8 EB |
8855 | h->root.u.def.section->flags |= SEC_KEEP; |
8856 | ||
8857 | return TRUE; | |
8858 | } | |
8859 | ||
c152c796 AM |
8860 | /* Do mark and sweep of unused sections. */ |
8861 | ||
8862 | bfd_boolean | |
8863 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
8864 | { | |
8865 | bfd_boolean ok = TRUE; | |
8866 | bfd *sub; | |
8867 | asection * (*gc_mark_hook) | |
8868 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
8869 | struct elf_link_hash_entry *h, Elf_Internal_Sym *); | |
8870 | ||
8871 | if (!get_elf_backend_data (abfd)->can_gc_sections | |
8872 | || info->relocatable | |
8873 | || info->emitrelocations | |
715df9b8 EB |
8874 | || info->shared |
8875 | || !is_elf_hash_table (info->hash)) | |
c152c796 AM |
8876 | { |
8877 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
8878 | return TRUE; | |
8879 | } | |
8880 | ||
8881 | /* Apply transitive closure to the vtable entry usage info. */ | |
8882 | elf_link_hash_traverse (elf_hash_table (info), | |
8883 | elf_gc_propagate_vtable_entries_used, | |
8884 | &ok); | |
8885 | if (!ok) | |
8886 | return FALSE; | |
8887 | ||
8888 | /* Kill the vtable relocations that were not used. */ | |
8889 | elf_link_hash_traverse (elf_hash_table (info), | |
8890 | elf_gc_smash_unused_vtentry_relocs, | |
8891 | &ok); | |
8892 | if (!ok) | |
8893 | return FALSE; | |
8894 | ||
715df9b8 EB |
8895 | /* Mark dynamically referenced symbols. */ |
8896 | if (elf_hash_table (info)->dynamic_sections_created) | |
8897 | elf_link_hash_traverse (elf_hash_table (info), | |
8898 | elf_gc_mark_dynamic_ref_symbol, | |
8899 | &ok); | |
8900 | if (!ok) | |
8901 | return FALSE; | |
c152c796 | 8902 | |
715df9b8 | 8903 | /* Grovel through relocs to find out who stays ... */ |
c152c796 AM |
8904 | gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook; |
8905 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8906 | { | |
8907 | asection *o; | |
8908 | ||
8909 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
8910 | continue; | |
8911 | ||
8912 | for (o = sub->sections; o != NULL; o = o->next) | |
8913 | { | |
8914 | if (o->flags & SEC_KEEP) | |
715df9b8 EB |
8915 | { |
8916 | /* _bfd_elf_discard_section_eh_frame knows how to discard | |
8917 | orphaned FDEs so don't mark sections referenced by the | |
8918 | EH frame section. */ | |
8919 | if (strcmp (o->name, ".eh_frame") == 0) | |
8920 | o->gc_mark = 1; | |
ccfa59ea | 8921 | else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) |
715df9b8 EB |
8922 | return FALSE; |
8923 | } | |
c152c796 AM |
8924 | } |
8925 | } | |
8926 | ||
8927 | /* ... and mark SEC_EXCLUDE for those that go. */ | |
8928 | if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook)) | |
8929 | return FALSE; | |
8930 | ||
8931 | return TRUE; | |
8932 | } | |
8933 | \f | |
8934 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
8935 | ||
8936 | bfd_boolean | |
8937 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
8938 | asection *sec, | |
8939 | struct elf_link_hash_entry *h, | |
8940 | bfd_vma offset) | |
8941 | { | |
8942 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
8943 | struct elf_link_hash_entry **search, *child; | |
8944 | bfd_size_type extsymcount; | |
8945 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8946 | ||
8947 | /* The sh_info field of the symtab header tells us where the | |
8948 | external symbols start. We don't care about the local symbols at | |
8949 | this point. */ | |
8950 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
8951 | if (!elf_bad_symtab (abfd)) | |
8952 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
8953 | ||
8954 | sym_hashes = elf_sym_hashes (abfd); | |
8955 | sym_hashes_end = sym_hashes + extsymcount; | |
8956 | ||
8957 | /* Hunt down the child symbol, which is in this section at the same | |
8958 | offset as the relocation. */ | |
8959 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
8960 | { | |
8961 | if ((child = *search) != NULL | |
8962 | && (child->root.type == bfd_link_hash_defined | |
8963 | || child->root.type == bfd_link_hash_defweak) | |
8964 | && child->root.u.def.section == sec | |
8965 | && child->root.u.def.value == offset) | |
8966 | goto win; | |
8967 | } | |
8968 | ||
d003868e AM |
8969 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
8970 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
8971 | bfd_set_error (bfd_error_invalid_operation); |
8972 | return FALSE; | |
8973 | ||
8974 | win: | |
f6e332e6 AM |
8975 | if (!child->vtable) |
8976 | { | |
8977 | child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable)); | |
8978 | if (!child->vtable) | |
8979 | return FALSE; | |
8980 | } | |
c152c796 AM |
8981 | if (!h) |
8982 | { | |
8983 | /* This *should* only be the absolute section. It could potentially | |
8984 | be that someone has defined a non-global vtable though, which | |
8985 | would be bad. It isn't worth paging in the local symbols to be | |
8986 | sure though; that case should simply be handled by the assembler. */ | |
8987 | ||
f6e332e6 | 8988 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
8989 | } |
8990 | else | |
f6e332e6 | 8991 | child->vtable->parent = h; |
c152c796 AM |
8992 | |
8993 | return TRUE; | |
8994 | } | |
8995 | ||
8996 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
8997 | ||
8998 | bfd_boolean | |
8999 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
9000 | asection *sec ATTRIBUTE_UNUSED, | |
9001 | struct elf_link_hash_entry *h, | |
9002 | bfd_vma addend) | |
9003 | { | |
9004 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9005 | unsigned int log_file_align = bed->s->log_file_align; | |
9006 | ||
f6e332e6 AM |
9007 | if (!h->vtable) |
9008 | { | |
9009 | h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable)); | |
9010 | if (!h->vtable) | |
9011 | return FALSE; | |
9012 | } | |
9013 | ||
9014 | if (addend >= h->vtable->size) | |
c152c796 AM |
9015 | { |
9016 | size_t size, bytes, file_align; | |
f6e332e6 | 9017 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
9018 | |
9019 | /* While the symbol is undefined, we have to be prepared to handle | |
9020 | a zero size. */ | |
9021 | file_align = 1 << log_file_align; | |
9022 | if (h->root.type == bfd_link_hash_undefined) | |
9023 | size = addend + file_align; | |
9024 | else | |
9025 | { | |
9026 | size = h->size; | |
9027 | if (addend >= size) | |
9028 | { | |
9029 | /* Oops! We've got a reference past the defined end of | |
9030 | the table. This is probably a bug -- shall we warn? */ | |
9031 | size = addend + file_align; | |
9032 | } | |
9033 | } | |
9034 | size = (size + file_align - 1) & -file_align; | |
9035 | ||
9036 | /* Allocate one extra entry for use as a "done" flag for the | |
9037 | consolidation pass. */ | |
9038 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
9039 | ||
9040 | if (ptr) | |
9041 | { | |
9042 | ptr = bfd_realloc (ptr - 1, bytes); | |
9043 | ||
9044 | if (ptr != NULL) | |
9045 | { | |
9046 | size_t oldbytes; | |
9047 | ||
f6e332e6 | 9048 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
9049 | * sizeof (bfd_boolean)); |
9050 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
9051 | } | |
9052 | } | |
9053 | else | |
9054 | ptr = bfd_zmalloc (bytes); | |
9055 | ||
9056 | if (ptr == NULL) | |
9057 | return FALSE; | |
9058 | ||
9059 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
9060 | h->vtable->used = ptr + 1; |
9061 | h->vtable->size = size; | |
c152c796 AM |
9062 | } |
9063 | ||
f6e332e6 | 9064 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
9065 | |
9066 | return TRUE; | |
9067 | } | |
9068 | ||
9069 | struct alloc_got_off_arg { | |
9070 | bfd_vma gotoff; | |
9071 | unsigned int got_elt_size; | |
9072 | }; | |
9073 | ||
9074 | /* We need a special top-level link routine to convert got reference counts | |
9075 | to real got offsets. */ | |
9076 | ||
9077 | static bfd_boolean | |
9078 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
9079 | { | |
9080 | struct alloc_got_off_arg *gofarg = arg; | |
9081 | ||
9082 | if (h->root.type == bfd_link_hash_warning) | |
9083 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9084 | ||
9085 | if (h->got.refcount > 0) | |
9086 | { | |
9087 | h->got.offset = gofarg->gotoff; | |
9088 | gofarg->gotoff += gofarg->got_elt_size; | |
9089 | } | |
9090 | else | |
9091 | h->got.offset = (bfd_vma) -1; | |
9092 | ||
9093 | return TRUE; | |
9094 | } | |
9095 | ||
9096 | /* And an accompanying bit to work out final got entry offsets once | |
9097 | we're done. Should be called from final_link. */ | |
9098 | ||
9099 | bfd_boolean | |
9100 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
9101 | struct bfd_link_info *info) | |
9102 | { | |
9103 | bfd *i; | |
9104 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9105 | bfd_vma gotoff; | |
9106 | unsigned int got_elt_size = bed->s->arch_size / 8; | |
9107 | struct alloc_got_off_arg gofarg; | |
9108 | ||
9109 | if (! is_elf_hash_table (info->hash)) | |
9110 | return FALSE; | |
9111 | ||
9112 | /* The GOT offset is relative to the .got section, but the GOT header is | |
9113 | put into the .got.plt section, if the backend uses it. */ | |
9114 | if (bed->want_got_plt) | |
9115 | gotoff = 0; | |
9116 | else | |
9117 | gotoff = bed->got_header_size; | |
9118 | ||
9119 | /* Do the local .got entries first. */ | |
9120 | for (i = info->input_bfds; i; i = i->link_next) | |
9121 | { | |
9122 | bfd_signed_vma *local_got; | |
9123 | bfd_size_type j, locsymcount; | |
9124 | Elf_Internal_Shdr *symtab_hdr; | |
9125 | ||
9126 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
9127 | continue; | |
9128 | ||
9129 | local_got = elf_local_got_refcounts (i); | |
9130 | if (!local_got) | |
9131 | continue; | |
9132 | ||
9133 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
9134 | if (elf_bad_symtab (i)) | |
9135 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9136 | else | |
9137 | locsymcount = symtab_hdr->sh_info; | |
9138 | ||
9139 | for (j = 0; j < locsymcount; ++j) | |
9140 | { | |
9141 | if (local_got[j] > 0) | |
9142 | { | |
9143 | local_got[j] = gotoff; | |
9144 | gotoff += got_elt_size; | |
9145 | } | |
9146 | else | |
9147 | local_got[j] = (bfd_vma) -1; | |
9148 | } | |
9149 | } | |
9150 | ||
9151 | /* Then the global .got entries. .plt refcounts are handled by | |
9152 | adjust_dynamic_symbol */ | |
9153 | gofarg.gotoff = gotoff; | |
9154 | gofarg.got_elt_size = got_elt_size; | |
9155 | elf_link_hash_traverse (elf_hash_table (info), | |
9156 | elf_gc_allocate_got_offsets, | |
9157 | &gofarg); | |
9158 | return TRUE; | |
9159 | } | |
9160 | ||
9161 | /* Many folk need no more in the way of final link than this, once | |
9162 | got entry reference counting is enabled. */ | |
9163 | ||
9164 | bfd_boolean | |
9165 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
9166 | { | |
9167 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
9168 | return FALSE; | |
9169 | ||
9170 | /* Invoke the regular ELF backend linker to do all the work. */ | |
9171 | return bfd_elf_final_link (abfd, info); | |
9172 | } | |
9173 | ||
9174 | bfd_boolean | |
9175 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
9176 | { | |
9177 | struct elf_reloc_cookie *rcookie = cookie; | |
9178 | ||
9179 | if (rcookie->bad_symtab) | |
9180 | rcookie->rel = rcookie->rels; | |
9181 | ||
9182 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
9183 | { | |
9184 | unsigned long r_symndx; | |
9185 | ||
9186 | if (! rcookie->bad_symtab) | |
9187 | if (rcookie->rel->r_offset > offset) | |
9188 | return FALSE; | |
9189 | if (rcookie->rel->r_offset != offset) | |
9190 | continue; | |
9191 | ||
9192 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
9193 | if (r_symndx == SHN_UNDEF) | |
9194 | return TRUE; | |
9195 | ||
9196 | if (r_symndx >= rcookie->locsymcount | |
9197 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
9198 | { | |
9199 | struct elf_link_hash_entry *h; | |
9200 | ||
9201 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
9202 | ||
9203 | while (h->root.type == bfd_link_hash_indirect | |
9204 | || h->root.type == bfd_link_hash_warning) | |
9205 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9206 | ||
9207 | if ((h->root.type == bfd_link_hash_defined | |
9208 | || h->root.type == bfd_link_hash_defweak) | |
9209 | && elf_discarded_section (h->root.u.def.section)) | |
9210 | return TRUE; | |
9211 | else | |
9212 | return FALSE; | |
9213 | } | |
9214 | else | |
9215 | { | |
9216 | /* It's not a relocation against a global symbol, | |
9217 | but it could be a relocation against a local | |
9218 | symbol for a discarded section. */ | |
9219 | asection *isec; | |
9220 | Elf_Internal_Sym *isym; | |
9221 | ||
9222 | /* Need to: get the symbol; get the section. */ | |
9223 | isym = &rcookie->locsyms[r_symndx]; | |
9224 | if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
9225 | { | |
9226 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); | |
9227 | if (isec != NULL && elf_discarded_section (isec)) | |
9228 | return TRUE; | |
9229 | } | |
9230 | } | |
9231 | return FALSE; | |
9232 | } | |
9233 | return FALSE; | |
9234 | } | |
9235 | ||
9236 | /* Discard unneeded references to discarded sections. | |
9237 | Returns TRUE if any section's size was changed. */ | |
9238 | /* This function assumes that the relocations are in sorted order, | |
9239 | which is true for all known assemblers. */ | |
9240 | ||
9241 | bfd_boolean | |
9242 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) | |
9243 | { | |
9244 | struct elf_reloc_cookie cookie; | |
9245 | asection *stab, *eh; | |
9246 | Elf_Internal_Shdr *symtab_hdr; | |
9247 | const struct elf_backend_data *bed; | |
9248 | bfd *abfd; | |
9249 | unsigned int count; | |
9250 | bfd_boolean ret = FALSE; | |
9251 | ||
9252 | if (info->traditional_format | |
9253 | || !is_elf_hash_table (info->hash)) | |
9254 | return FALSE; | |
9255 | ||
9256 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) | |
9257 | { | |
9258 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
9259 | continue; | |
9260 | ||
9261 | bed = get_elf_backend_data (abfd); | |
9262 | ||
9263 | if ((abfd->flags & DYNAMIC) != 0) | |
9264 | continue; | |
9265 | ||
9266 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); | |
9267 | if (info->relocatable | |
9268 | || (eh != NULL | |
eea6121a | 9269 | && (eh->size == 0 |
c152c796 AM |
9270 | || bfd_is_abs_section (eh->output_section)))) |
9271 | eh = NULL; | |
9272 | ||
9273 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
9274 | if (stab != NULL | |
eea6121a | 9275 | && (stab->size == 0 |
c152c796 AM |
9276 | || bfd_is_abs_section (stab->output_section) |
9277 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) | |
9278 | stab = NULL; | |
9279 | ||
9280 | if (stab == NULL | |
9281 | && eh == NULL | |
9282 | && bed->elf_backend_discard_info == NULL) | |
9283 | continue; | |
9284 | ||
9285 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
9286 | cookie.abfd = abfd; | |
9287 | cookie.sym_hashes = elf_sym_hashes (abfd); | |
9288 | cookie.bad_symtab = elf_bad_symtab (abfd); | |
9289 | if (cookie.bad_symtab) | |
9290 | { | |
9291 | cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9292 | cookie.extsymoff = 0; | |
9293 | } | |
9294 | else | |
9295 | { | |
9296 | cookie.locsymcount = symtab_hdr->sh_info; | |
9297 | cookie.extsymoff = symtab_hdr->sh_info; | |
9298 | } | |
9299 | ||
9300 | if (bed->s->arch_size == 32) | |
9301 | cookie.r_sym_shift = 8; | |
9302 | else | |
9303 | cookie.r_sym_shift = 32; | |
9304 | ||
9305 | cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
9306 | if (cookie.locsyms == NULL && cookie.locsymcount != 0) | |
9307 | { | |
9308 | cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
9309 | cookie.locsymcount, 0, | |
9310 | NULL, NULL, NULL); | |
9311 | if (cookie.locsyms == NULL) | |
9312 | return FALSE; | |
9313 | } | |
9314 | ||
9315 | if (stab != NULL) | |
9316 | { | |
9317 | cookie.rels = NULL; | |
9318 | count = stab->reloc_count; | |
9319 | if (count != 0) | |
9320 | cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL, | |
9321 | info->keep_memory); | |
9322 | if (cookie.rels != NULL) | |
9323 | { | |
9324 | cookie.rel = cookie.rels; | |
9325 | cookie.relend = cookie.rels; | |
9326 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
9327 | if (_bfd_discard_section_stabs (abfd, stab, | |
9328 | elf_section_data (stab)->sec_info, | |
9329 | bfd_elf_reloc_symbol_deleted_p, | |
9330 | &cookie)) | |
9331 | ret = TRUE; | |
9332 | if (elf_section_data (stab)->relocs != cookie.rels) | |
9333 | free (cookie.rels); | |
9334 | } | |
9335 | } | |
9336 | ||
9337 | if (eh != NULL) | |
9338 | { | |
9339 | cookie.rels = NULL; | |
9340 | count = eh->reloc_count; | |
9341 | if (count != 0) | |
9342 | cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL, | |
9343 | info->keep_memory); | |
9344 | cookie.rel = cookie.rels; | |
9345 | cookie.relend = cookie.rels; | |
9346 | if (cookie.rels != NULL) | |
9347 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
9348 | ||
9349 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, | |
9350 | bfd_elf_reloc_symbol_deleted_p, | |
9351 | &cookie)) | |
9352 | ret = TRUE; | |
9353 | ||
9354 | if (cookie.rels != NULL | |
9355 | && elf_section_data (eh)->relocs != cookie.rels) | |
9356 | free (cookie.rels); | |
9357 | } | |
9358 | ||
9359 | if (bed->elf_backend_discard_info != NULL | |
9360 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
9361 | ret = TRUE; | |
9362 | ||
9363 | if (cookie.locsyms != NULL | |
9364 | && symtab_hdr->contents != (unsigned char *) cookie.locsyms) | |
9365 | { | |
9366 | if (! info->keep_memory) | |
9367 | free (cookie.locsyms); | |
9368 | else | |
9369 | symtab_hdr->contents = (unsigned char *) cookie.locsyms; | |
9370 | } | |
9371 | } | |
9372 | ||
9373 | if (info->eh_frame_hdr | |
9374 | && !info->relocatable | |
9375 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
9376 | ret = TRUE; | |
9377 | ||
9378 | return ret; | |
9379 | } | |
082b7297 L |
9380 | |
9381 | void | |
9382 | _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec) | |
9383 | { | |
9384 | flagword flags; | |
6d2cd210 | 9385 | const char *name, *p; |
082b7297 L |
9386 | struct bfd_section_already_linked *l; |
9387 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
3d7f7666 L |
9388 | asection *group; |
9389 | ||
9390 | /* A single member comdat group section may be discarded by a | |
9391 | linkonce section. See below. */ | |
9392 | if (sec->output_section == bfd_abs_section_ptr) | |
9393 | return; | |
082b7297 L |
9394 | |
9395 | flags = sec->flags; | |
3d7f7666 L |
9396 | |
9397 | /* Check if it belongs to a section group. */ | |
9398 | group = elf_sec_group (sec); | |
9399 | ||
9400 | /* Return if it isn't a linkonce section nor a member of a group. A | |
9401 | comdat group section also has SEC_LINK_ONCE set. */ | |
9402 | if ((flags & SEC_LINK_ONCE) == 0 && group == NULL) | |
082b7297 L |
9403 | return; |
9404 | ||
3d7f7666 L |
9405 | if (group) |
9406 | { | |
9407 | /* If this is the member of a single member comdat group, check if | |
9408 | the group should be discarded. */ | |
9409 | if (elf_next_in_group (sec) == sec | |
9410 | && (group->flags & SEC_LINK_ONCE) != 0) | |
9411 | sec = group; | |
9412 | else | |
9413 | return; | |
9414 | } | |
9415 | ||
082b7297 L |
9416 | /* FIXME: When doing a relocatable link, we may have trouble |
9417 | copying relocations in other sections that refer to local symbols | |
9418 | in the section being discarded. Those relocations will have to | |
9419 | be converted somehow; as of this writing I'm not sure that any of | |
9420 | the backends handle that correctly. | |
9421 | ||
9422 | It is tempting to instead not discard link once sections when | |
9423 | doing a relocatable link (technically, they should be discarded | |
9424 | whenever we are building constructors). However, that fails, | |
9425 | because the linker winds up combining all the link once sections | |
9426 | into a single large link once section, which defeats the purpose | |
9427 | of having link once sections in the first place. | |
9428 | ||
9429 | Also, not merging link once sections in a relocatable link | |
9430 | causes trouble for MIPS ELF, which relies on link once semantics | |
9431 | to handle the .reginfo section correctly. */ | |
9432 | ||
9433 | name = bfd_get_section_name (abfd, sec); | |
9434 | ||
6d2cd210 JJ |
9435 | if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0 |
9436 | && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) | |
9437 | p++; | |
9438 | else | |
9439 | p = name; | |
9440 | ||
9441 | already_linked_list = bfd_section_already_linked_table_lookup (p); | |
082b7297 L |
9442 | |
9443 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
9444 | { | |
9445 | /* We may have 3 different sections on the list: group section, | |
9446 | comdat section and linkonce section. SEC may be a linkonce or | |
9447 | group section. We match a group section with a group section, | |
9448 | a linkonce section with a linkonce section, and ignore comdat | |
9449 | section. */ | |
3d7f7666 | 9450 | if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) |
6d2cd210 | 9451 | && strcmp (name, l->sec->name) == 0 |
082b7297 L |
9452 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL) |
9453 | { | |
9454 | /* The section has already been linked. See if we should | |
6d2cd210 | 9455 | issue a warning. */ |
082b7297 L |
9456 | switch (flags & SEC_LINK_DUPLICATES) |
9457 | { | |
9458 | default: | |
9459 | abort (); | |
9460 | ||
9461 | case SEC_LINK_DUPLICATES_DISCARD: | |
9462 | break; | |
9463 | ||
9464 | case SEC_LINK_DUPLICATES_ONE_ONLY: | |
9465 | (*_bfd_error_handler) | |
d003868e AM |
9466 | (_("%B: ignoring duplicate section `%A'\n"), |
9467 | abfd, sec); | |
082b7297 L |
9468 | break; |
9469 | ||
9470 | case SEC_LINK_DUPLICATES_SAME_SIZE: | |
9471 | if (sec->size != l->sec->size) | |
9472 | (*_bfd_error_handler) | |
d003868e AM |
9473 | (_("%B: duplicate section `%A' has different size\n"), |
9474 | abfd, sec); | |
082b7297 | 9475 | break; |
ea5158d8 DJ |
9476 | |
9477 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: | |
9478 | if (sec->size != l->sec->size) | |
9479 | (*_bfd_error_handler) | |
9480 | (_("%B: duplicate section `%A' has different size\n"), | |
9481 | abfd, sec); | |
9482 | else if (sec->size != 0) | |
9483 | { | |
9484 | bfd_byte *sec_contents, *l_sec_contents; | |
9485 | ||
9486 | if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents)) | |
9487 | (*_bfd_error_handler) | |
9488 | (_("%B: warning: could not read contents of section `%A'\n"), | |
9489 | abfd, sec); | |
9490 | else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec, | |
9491 | &l_sec_contents)) | |
9492 | (*_bfd_error_handler) | |
9493 | (_("%B: warning: could not read contents of section `%A'\n"), | |
9494 | l->sec->owner, l->sec); | |
9495 | else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) | |
9496 | (*_bfd_error_handler) | |
9497 | (_("%B: warning: duplicate section `%A' has different contents\n"), | |
9498 | abfd, sec); | |
9499 | ||
9500 | if (sec_contents) | |
9501 | free (sec_contents); | |
9502 | if (l_sec_contents) | |
9503 | free (l_sec_contents); | |
9504 | } | |
9505 | break; | |
082b7297 L |
9506 | } |
9507 | ||
9508 | /* Set the output_section field so that lang_add_section | |
9509 | does not create a lang_input_section structure for this | |
9510 | section. Since there might be a symbol in the section | |
9511 | being discarded, we must retain a pointer to the section | |
9512 | which we are really going to use. */ | |
9513 | sec->output_section = bfd_abs_section_ptr; | |
9514 | sec->kept_section = l->sec; | |
9515 | ||
9516 | if (flags & SEC_GROUP) | |
3d7f7666 L |
9517 | { |
9518 | asection *first = elf_next_in_group (sec); | |
9519 | asection *s = first; | |
9520 | ||
9521 | while (s != NULL) | |
9522 | { | |
9523 | s->output_section = bfd_abs_section_ptr; | |
9524 | /* Record which group discards it. */ | |
9525 | s->kept_section = l->sec; | |
9526 | s = elf_next_in_group (s); | |
9527 | /* These lists are circular. */ | |
9528 | if (s == first) | |
9529 | break; | |
9530 | } | |
9531 | } | |
082b7297 L |
9532 | |
9533 | return; | |
9534 | } | |
9535 | } | |
9536 | ||
3d7f7666 L |
9537 | if (group) |
9538 | { | |
9539 | /* If this is the member of a single member comdat group and the | |
9540 | group hasn't be discarded, we check if it matches a linkonce | |
9541 | section. We only record the discarded comdat group. Otherwise | |
9542 | the undiscarded group will be discarded incorrectly later since | |
9543 | itself has been recorded. */ | |
6d2cd210 JJ |
9544 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
9545 | if ((l->sec->flags & SEC_GROUP) == 0 | |
9546 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL | |
9547 | && bfd_elf_match_symbols_in_sections (l->sec, | |
9548 | elf_next_in_group (sec))) | |
9549 | { | |
9550 | elf_next_in_group (sec)->output_section = bfd_abs_section_ptr; | |
9551 | elf_next_in_group (sec)->kept_section = l->sec; | |
9552 | group->output_section = bfd_abs_section_ptr; | |
9553 | break; | |
9554 | } | |
9555 | if (l == NULL) | |
3d7f7666 L |
9556 | return; |
9557 | } | |
9558 | else | |
9559 | /* There is no direct match. But for linkonce section, we should | |
9560 | check if there is a match with comdat group member. We always | |
9561 | record the linkonce section, discarded or not. */ | |
6d2cd210 JJ |
9562 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
9563 | if (l->sec->flags & SEC_GROUP) | |
9564 | { | |
9565 | asection *first = elf_next_in_group (l->sec); | |
9566 | ||
9567 | if (first != NULL | |
9568 | && elf_next_in_group (first) == first | |
9569 | && bfd_elf_match_symbols_in_sections (first, sec)) | |
9570 | { | |
9571 | sec->output_section = bfd_abs_section_ptr; | |
9572 | sec->kept_section = l->sec; | |
9573 | break; | |
9574 | } | |
9575 | } | |
9576 | ||
082b7297 L |
9577 | /* This is the first section with this name. Record it. */ |
9578 | bfd_section_already_linked_table_insert (already_linked_list, sec); | |
9579 | } |